1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
// Copyright 2018 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

mod read_dir;

use std::cmp::min;
use std::collections::btree_map;
use std::collections::BTreeMap;
use std::ffi::CStr;
use std::ffi::CString;
use std::fs::File;
use std::io;
use std::io::Cursor;
use std::io::Read;
use std::io::Write;
use std::mem;
use std::mem::MaybeUninit;
use std::ops::Deref;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::FileExt;
use std::os::unix::io::AsRawFd;
use std::os::unix::io::FromRawFd;
use std::os::unix::io::RawFd;
use std::path::Path;
use std::str::FromStr;

use read_dir::read_dir;
use serde::Deserialize;
use serde::Serialize;

use crate::protocol::*;
use crate::syscall;

// Tlopen and Tlcreate flags.  Taken from "include/net/9p/9p.h" in the linux tree.
const P9_RDONLY: u32 = 0o00000000;
const P9_WRONLY: u32 = 0o00000001;
const P9_RDWR: u32 = 0o00000002;
const P9_NOACCESS: u32 = 0o00000003;
const P9_CREATE: u32 = 0o00000100;
const P9_EXCL: u32 = 0o00000200;
const P9_NOCTTY: u32 = 0o00000400;
const P9_TRUNC: u32 = 0o00001000;
const P9_APPEND: u32 = 0o00002000;
const P9_NONBLOCK: u32 = 0o00004000;
const P9_DSYNC: u32 = 0o00010000;
const P9_FASYNC: u32 = 0o00020000;
const P9_DIRECT: u32 = 0o00040000;
const P9_LARGEFILE: u32 = 0o00100000;
const P9_DIRECTORY: u32 = 0o00200000;
const P9_NOFOLLOW: u32 = 0o00400000;
const P9_NOATIME: u32 = 0o01000000;
const _P9_CLOEXEC: u32 = 0o02000000;
const P9_SYNC: u32 = 0o04000000;

// Mapping from 9P flags to libc flags.
const MAPPED_FLAGS: [(u32, i32); 16] = [
    (P9_WRONLY, libc::O_WRONLY),
    (P9_RDWR, libc::O_RDWR),
    (P9_CREATE, libc::O_CREAT),
    (P9_EXCL, libc::O_EXCL),
    (P9_NOCTTY, libc::O_NOCTTY),
    (P9_TRUNC, libc::O_TRUNC),
    (P9_APPEND, libc::O_APPEND),
    (P9_NONBLOCK, libc::O_NONBLOCK),
    (P9_DSYNC, libc::O_DSYNC),
    (P9_FASYNC, 0), // Unsupported
    (P9_DIRECT, libc::O_DIRECT),
    (P9_LARGEFILE, libc::O_LARGEFILE),
    (P9_DIRECTORY, libc::O_DIRECTORY),
    (P9_NOFOLLOW, libc::O_NOFOLLOW),
    (P9_NOATIME, libc::O_NOATIME),
    (P9_SYNC, libc::O_SYNC),
];

// 9P Qid types.  Taken from "include/net/9p/9p.h" in the linux tree.
const P9_QTDIR: u8 = 0x80;
const _P9_QTAPPEND: u8 = 0x40;
const _P9_QTEXCL: u8 = 0x20;
const _P9_QTMOUNT: u8 = 0x10;
const _P9_QTAUTH: u8 = 0x08;
const _P9_QTTMP: u8 = 0x04;
const P9_QTSYMLINK: u8 = 0x02;
const _P9_QTLINK: u8 = 0x01;
const P9_QTFILE: u8 = 0x00;

// Bitmask values for the getattr request.
const _P9_GETATTR_MODE: u64 = 0x00000001;
const _P9_GETATTR_NLINK: u64 = 0x00000002;
const _P9_GETATTR_UID: u64 = 0x00000004;
const _P9_GETATTR_GID: u64 = 0x00000008;
const _P9_GETATTR_RDEV: u64 = 0x00000010;
const _P9_GETATTR_ATIME: u64 = 0x00000020;
const _P9_GETATTR_MTIME: u64 = 0x00000040;
const _P9_GETATTR_CTIME: u64 = 0x00000080;
const _P9_GETATTR_INO: u64 = 0x00000100;
const _P9_GETATTR_SIZE: u64 = 0x00000200;
const _P9_GETATTR_BLOCKS: u64 = 0x00000400;

const _P9_GETATTR_BTIME: u64 = 0x00000800;
const _P9_GETATTR_GEN: u64 = 0x00001000;
const _P9_GETATTR_DATA_VERSION: u64 = 0x00002000;

const P9_GETATTR_BASIC: u64 = 0x000007ff; /* Mask for fields up to BLOCKS */
const _P9_GETATTR_ALL: u64 = 0x00003fff; /* Mask for All fields above */

// Bitmask values for the setattr request.
const P9_SETATTR_MODE: u32 = 0x00000001;
const P9_SETATTR_UID: u32 = 0x00000002;
const P9_SETATTR_GID: u32 = 0x00000004;
const P9_SETATTR_SIZE: u32 = 0x00000008;
const P9_SETATTR_ATIME: u32 = 0x00000010;
const P9_SETATTR_MTIME: u32 = 0x00000020;
const P9_SETATTR_CTIME: u32 = 0x00000040;
const P9_SETATTR_ATIME_SET: u32 = 0x00000080;
const P9_SETATTR_MTIME_SET: u32 = 0x00000100;

// 9p lock constants. Taken from "include/net/9p/9p.h" in the linux kernel.
const _P9_LOCK_TYPE_RDLCK: u8 = 0;
const _P9_LOCK_TYPE_WRLCK: u8 = 1;
const P9_LOCK_TYPE_UNLCK: u8 = 2;
const _P9_LOCK_FLAGS_BLOCK: u8 = 1;
const _P9_LOCK_FLAGS_RECLAIM: u8 = 2;
const P9_LOCK_SUCCESS: u8 = 0;
const _P9_LOCK_BLOCKED: u8 = 1;
const _P9_LOCK_ERROR: u8 = 2;
const _P9_LOCK_GRACE: u8 = 3;

// Minimum and maximum message size that we'll expect from the client.
const MIN_MESSAGE_SIZE: u32 = 256;
const MAX_MESSAGE_SIZE: u32 = 64 * 1024 + 24; // 64 KiB of payload plus some extra for the header

#[derive(PartialEq, Eq)]
enum FileType {
    Regular,
    Directory,
    Other,
}

impl From<libc::mode_t> for FileType {
    fn from(mode: libc::mode_t) -> Self {
        match mode & libc::S_IFMT {
            libc::S_IFREG => FileType::Regular,
            libc::S_IFDIR => FileType::Directory,
            _ => FileType::Other,
        }
    }
}

// Represents state that the server is holding on behalf of a client. Fids are somewhat like file
// descriptors but are not restricted to open files and directories. Fids are identified by a unique
// 32-bit number chosen by the client. Most messages sent by clients include a fid on which to
// operate. The fid in a Tattach message represents the root of the file system tree that the client
// is allowed to access. A client can create more fids by walking the directory tree from that fid.
struct Fid {
    path: File,
    file: Option<File>,
    filetype: FileType,
}

impl From<libc::stat64> for Qid {
    fn from(st: libc::stat64) -> Qid {
        let ty = match st.st_mode & libc::S_IFMT {
            libc::S_IFDIR => P9_QTDIR,
            libc::S_IFREG => P9_QTFILE,
            libc::S_IFLNK => P9_QTSYMLINK,
            _ => 0,
        };

        Qid {
            ty,
            // TODO: deal with the 2038 problem before 2038
            version: st.st_mtime as u32,
            path: st.st_ino,
        }
    }
}

fn statat(d: &File, name: &CStr, flags: libc::c_int) -> io::Result<libc::stat64> {
    let mut st = MaybeUninit::<libc::stat64>::zeroed();

    // Safe because the kernel will only write data in `st` and we check the return
    // value.
    let res = unsafe {
        libc::fstatat64(
            d.as_raw_fd(),
            name.as_ptr(),
            st.as_mut_ptr(),
            flags | libc::AT_SYMLINK_NOFOLLOW,
        )
    };
    if res >= 0 {
        // Safe because the kernel guarantees that the struct is now fully initialized.
        Ok(unsafe { st.assume_init() })
    } else {
        Err(io::Error::last_os_error())
    }
}

fn stat(f: &File) -> io::Result<libc::stat64> {
    // Safe because this is a constant value and a valid C string.
    let pathname = unsafe { CStr::from_bytes_with_nul_unchecked(b"\0") };

    statat(f, pathname, libc::AT_EMPTY_PATH)
}

fn string_to_cstring(s: String) -> io::Result<CString> {
    CString::new(s).map_err(|_| io::Error::from_raw_os_error(libc::EINVAL))
}

fn error_to_rmessage(err: io::Error) -> Rmessage {
    let errno = if let Some(errno) = err.raw_os_error() {
        errno
    } else {
        // Make a best-effort guess based on the kind.
        match err.kind() {
            io::ErrorKind::NotFound => libc::ENOENT,
            io::ErrorKind::PermissionDenied => libc::EPERM,
            io::ErrorKind::ConnectionRefused => libc::ECONNREFUSED,
            io::ErrorKind::ConnectionReset => libc::ECONNRESET,
            io::ErrorKind::ConnectionAborted => libc::ECONNABORTED,
            io::ErrorKind::NotConnected => libc::ENOTCONN,
            io::ErrorKind::AddrInUse => libc::EADDRINUSE,
            io::ErrorKind::AddrNotAvailable => libc::EADDRNOTAVAIL,
            io::ErrorKind::BrokenPipe => libc::EPIPE,
            io::ErrorKind::AlreadyExists => libc::EEXIST,
            io::ErrorKind::WouldBlock => libc::EWOULDBLOCK,
            io::ErrorKind::InvalidInput => libc::EINVAL,
            io::ErrorKind::InvalidData => libc::EINVAL,
            io::ErrorKind::TimedOut => libc::ETIMEDOUT,
            io::ErrorKind::WriteZero => libc::EIO,
            io::ErrorKind::Interrupted => libc::EINTR,
            io::ErrorKind::Other => libc::EIO,
            io::ErrorKind::UnexpectedEof => libc::EIO,
            _ => libc::EIO,
        }
    };

    Rmessage::Lerror(Rlerror {
        ecode: errno as u32,
    })
}

// Sigh.. Cow requires the underlying type to implement Clone.
enum MaybeOwned<'b, T> {
    Borrowed(&'b T),
    Owned(T),
}

impl<'a, T> Deref for MaybeOwned<'a, T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        use MaybeOwned::*;
        match *self {
            Borrowed(borrowed) => borrowed,
            Owned(ref owned) => owned,
        }
    }
}

impl<'a, T> AsRef<T> for MaybeOwned<'a, T> {
    fn as_ref(&self) -> &T {
        use MaybeOwned::*;
        match self {
            Borrowed(borrowed) => borrowed,
            Owned(ref owned) => owned,
        }
    }
}

fn ebadf() -> io::Error {
    io::Error::from_raw_os_error(libc::EBADF)
}

pub type ServerIdMap<T> = BTreeMap<T, T>;
pub type ServerUidMap = ServerIdMap<libc::uid_t>;
pub type ServerGidMap = ServerIdMap<libc::gid_t>;

fn map_id_from_host<T: Clone + Ord>(map: &ServerIdMap<T>, id: T) -> T {
    map.get(&id).map_or(id.clone(), |v| v.clone())
}

// Performs an ascii case insensitive lookup and returns an O_PATH fd for the entry, if found.
fn ascii_casefold_lookup(proc: &File, parent: &File, name: &[u8]) -> io::Result<File> {
    let mut dir = open_fid(proc, parent, P9_DIRECTORY)?;
    let mut dirents = read_dir(&mut dir, 0)?;

    while let Some(entry) = dirents.next().transpose()? {
        if name.eq_ignore_ascii_case(entry.name.to_bytes()) {
            return lookup(parent, entry.name);
        }
    }

    Err(io::Error::from_raw_os_error(libc::ENOENT))
}

fn lookup(parent: &File, name: &CStr) -> io::Result<File> {
    // Safe because this doesn't modify any memory and we check the return value.
    let fd = syscall!(unsafe {
        libc::openat64(
            parent.as_raw_fd(),
            name.as_ptr(),
            libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC,
        )
    })?;

    // Safe because we just opened this fd.
    Ok(unsafe { File::from_raw_fd(fd) })
}

fn do_walk(
    proc: &File,
    wnames: Vec<String>,
    start: &File,
    ascii_casefold: bool,
    mds: &mut Vec<libc::stat64>,
) -> io::Result<File> {
    let mut current = MaybeOwned::Borrowed(start);

    for wname in wnames {
        let name = string_to_cstring(wname)?;
        current = MaybeOwned::Owned(lookup(current.as_ref(), &name).or_else(|e| {
            if ascii_casefold {
                if let Some(libc::ENOENT) = e.raw_os_error() {
                    return ascii_casefold_lookup(proc, current.as_ref(), name.to_bytes());
                }
            }

            Err(e)
        })?);
        mds.push(stat(&current)?);
    }

    match current {
        MaybeOwned::Owned(owned) => Ok(owned),
        MaybeOwned::Borrowed(borrowed) => borrowed.try_clone(),
    }
}

fn open_fid(proc: &File, path: &File, p9_flags: u32) -> io::Result<File> {
    let pathname = string_to_cstring(format!("self/fd/{}", path.as_raw_fd()))?;

    // We always open files with O_CLOEXEC.
    let mut flags: i32 = libc::O_CLOEXEC;
    for &(p9f, of) in &MAPPED_FLAGS {
        if (p9_flags & p9f) != 0 {
            flags |= of;
        }
    }

    if p9_flags & P9_NOACCESS == P9_RDONLY {
        flags |= libc::O_RDONLY;
    }

    // Safe because this doesn't modify any memory and we check the return value. We need to
    // clear the O_NOFOLLOW flag because we want to follow the proc symlink.
    let fd = syscall!(unsafe {
        libc::openat64(
            proc.as_raw_fd(),
            pathname.as_ptr(),
            flags & !libc::O_NOFOLLOW,
        )
    })?;

    // Safe because we just opened this fd and we know it is valid.
    Ok(unsafe { File::from_raw_fd(fd) })
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Config {
    pub root: Box<Path>,
    pub msize: u32,

    pub uid_map: ServerUidMap,
    pub gid_map: ServerGidMap,

    pub ascii_casefold: bool,
}

impl FromStr for Config {
    type Err = &'static str;

    fn from_str(params: &str) -> Result<Self, Self::Err> {
        let mut cfg = Self::default();
        if params.is_empty() {
            return Ok(cfg);
        }
        for opt in params.split(':') {
            let mut o = opt.splitn(2, '=');
            let kind = o.next().ok_or("`cfg` options mut not be empty")?;
            let value = o
                .next()
                .ok_or("`cfg` options must be of the form `kind=value`")?;
            match kind {
                "ascii_casefold" => {
                    let ascii_casefold = value
                        .parse()
                        .map_err(|_| "`ascii_casefold` must be a boolean")?;
                    cfg.ascii_casefold = ascii_casefold;
                }
                _ => return Err("unrecognized option for p9 config"),
            }
        }
        Ok(cfg)
    }
}

impl Default for Config {
    fn default() -> Config {
        Config {
            root: Path::new("/").into(),
            msize: MAX_MESSAGE_SIZE,
            uid_map: Default::default(),
            gid_map: Default::default(),
            ascii_casefold: false,
        }
    }
}
pub struct Server {
    fids: BTreeMap<u32, Fid>,
    proc: File,
    cfg: Config,
}

impl Server {
    pub fn new<P: Into<Box<Path>>>(
        root: P,
        uid_map: ServerUidMap,
        gid_map: ServerGidMap,
    ) -> io::Result<Server> {
        Server::with_config(Config {
            root: root.into(),
            msize: MAX_MESSAGE_SIZE,
            uid_map,
            gid_map,
            ascii_casefold: false,
        })
    }

    pub fn with_config(cfg: Config) -> io::Result<Server> {
        // Safe because this is a valid c-string.
        let proc_cstr = unsafe { CStr::from_bytes_with_nul_unchecked(b"/proc\0") };

        // Safe because this doesn't modify any memory and we check the return value.
        let fd = syscall!(unsafe {
            libc::openat64(
                libc::AT_FDCWD,
                proc_cstr.as_ptr(),
                libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC,
            )
        })?;

        // Safe because we just opened this fd and we know it is valid.
        let proc = unsafe { File::from_raw_fd(fd) };
        Ok(Server {
            fids: BTreeMap::new(),
            proc,
            cfg,
        })
    }

    pub fn keep_fds(&self) -> Vec<RawFd> {
        vec![self.proc.as_raw_fd()]
    }

    pub fn handle_message<R: Read, W: Write>(
        &mut self,
        reader: &mut R,
        writer: &mut W,
    ) -> io::Result<()> {
        let Tframe { tag, msg } = WireFormat::decode(&mut reader.take(self.cfg.msize as u64))?;

        let rmsg = match msg {
            Ok(Tmessage::Version(ref version)) => self.version(version).map(Rmessage::Version),
            Ok(Tmessage::Flush(ref flush)) => self.flush(flush).and(Ok(Rmessage::Flush)),
            Ok(Tmessage::Walk(walk)) => self.walk(walk).map(Rmessage::Walk),
            Ok(Tmessage::Read(ref read)) => self.read(read).map(Rmessage::Read),
            Ok(Tmessage::Write(ref write)) => self.write(write).map(Rmessage::Write),
            Ok(Tmessage::Clunk(ref clunk)) => self.clunk(clunk).and(Ok(Rmessage::Clunk)),
            Ok(Tmessage::Remove(ref remove)) => self.remove(remove).and(Ok(Rmessage::Remove)),
            Ok(Tmessage::Attach(ref attach)) => self.attach(attach).map(Rmessage::Attach),
            Ok(Tmessage::Auth(ref auth)) => self.auth(auth).map(Rmessage::Auth),
            Ok(Tmessage::Statfs(ref statfs)) => self.statfs(statfs).map(Rmessage::Statfs),
            Ok(Tmessage::Lopen(ref lopen)) => self.lopen(lopen).map(Rmessage::Lopen),
            Ok(Tmessage::Lcreate(lcreate)) => self.lcreate(lcreate).map(Rmessage::Lcreate),
            Ok(Tmessage::Symlink(ref symlink)) => self.symlink(symlink).map(Rmessage::Symlink),
            Ok(Tmessage::Mknod(ref mknod)) => self.mknod(mknod).map(Rmessage::Mknod),
            Ok(Tmessage::Rename(ref rename)) => self.rename(rename).and(Ok(Rmessage::Rename)),
            Ok(Tmessage::Readlink(ref readlink)) => self.readlink(readlink).map(Rmessage::Readlink),
            Ok(Tmessage::GetAttr(ref get_attr)) => self.get_attr(get_attr).map(Rmessage::GetAttr),
            Ok(Tmessage::SetAttr(ref set_attr)) => {
                self.set_attr(set_attr).and(Ok(Rmessage::SetAttr))
            }
            Ok(Tmessage::XattrWalk(ref xattr_walk)) => {
                self.xattr_walk(xattr_walk).map(Rmessage::XattrWalk)
            }
            Ok(Tmessage::XattrCreate(ref xattr_create)) => self
                .xattr_create(xattr_create)
                .and(Ok(Rmessage::XattrCreate)),
            Ok(Tmessage::Readdir(ref readdir)) => self.readdir(readdir).map(Rmessage::Readdir),
            Ok(Tmessage::Fsync(ref fsync)) => self.fsync(fsync).and(Ok(Rmessage::Fsync)),
            Ok(Tmessage::Lock(ref lock)) => self.lock(lock).map(Rmessage::Lock),
            Ok(Tmessage::GetLock(ref get_lock)) => self.get_lock(get_lock).map(Rmessage::GetLock),
            Ok(Tmessage::Link(link)) => self.link(link).and(Ok(Rmessage::Link)),
            Ok(Tmessage::Mkdir(mkdir)) => self.mkdir(mkdir).map(Rmessage::Mkdir),
            Ok(Tmessage::RenameAt(rename_at)) => {
                self.rename_at(rename_at).and(Ok(Rmessage::RenameAt))
            }
            Ok(Tmessage::UnlinkAt(unlink_at)) => {
                self.unlink_at(unlink_at).and(Ok(Rmessage::UnlinkAt))
            }
            Err(e) => {
                // The header was successfully decoded, but the body failed to decode - send an
                // error response for this tag.
                let error = format!("Tframe message decode failed: {}", e);
                Err(io::Error::new(io::ErrorKind::InvalidData, error))
            }
        };

        // Errors while handling requests are never fatal.
        let response = Rframe {
            tag,
            msg: rmsg.unwrap_or_else(error_to_rmessage),
        };

        response.encode(writer)?;
        writer.flush()
    }

    fn auth(&mut self, _auth: &Tauth) -> io::Result<Rauth> {
        // Returning an error for the auth message means that the server does not require
        // authentication.
        Err(io::Error::from_raw_os_error(libc::EOPNOTSUPP))
    }

    fn attach(&mut self, attach: &Tattach) -> io::Result<Rattach> {
        // TODO: Check attach parameters
        match self.fids.entry(attach.fid) {
            btree_map::Entry::Vacant(entry) => {
                let root = CString::new(self.cfg.root.as_os_str().as_bytes())
                    .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;

                // Safe because this doesn't modify any memory and we check the return value.
                let fd = syscall!(unsafe {
                    libc::openat64(
                        libc::AT_FDCWD,
                        root.as_ptr(),
                        libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC,
                    )
                })?;

                let root_path = unsafe { File::from_raw_fd(fd) };
                let st = stat(&root_path)?;

                let fid = Fid {
                    // Safe because we just opened this fd.
                    path: root_path,
                    file: None,
                    filetype: st.st_mode.into(),
                };
                let response = Rattach { qid: st.into() };
                entry.insert(fid);
                Ok(response)
            }
            btree_map::Entry::Occupied(_) => Err(io::Error::from_raw_os_error(libc::EBADF)),
        }
    }

    fn version(&mut self, version: &Tversion) -> io::Result<Rversion> {
        if version.msize < MIN_MESSAGE_SIZE {
            return Err(io::Error::from_raw_os_error(libc::EINVAL));
        }

        // A Tversion request clunks all open fids and terminates any pending I/O.
        self.fids.clear();
        self.cfg.msize = min(self.cfg.msize, version.msize);

        Ok(Rversion {
            msize: self.cfg.msize,
            version: if version.version == "9P2000.L" {
                String::from("9P2000.L")
            } else {
                String::from("unknown")
            },
        })
    }

    #[allow(clippy::unnecessary_wraps)]
    fn flush(&mut self, _flush: &Tflush) -> io::Result<()> {
        // TODO: Since everything is synchronous we can't actually flush requests.
        Ok(())
    }

    fn walk(&mut self, walk: Twalk) -> io::Result<Rwalk> {
        // `newfid` must not currently be in use unless it is the same as `fid`.
        if walk.fid != walk.newfid && self.fids.contains_key(&walk.newfid) {
            return Err(io::Error::from_raw_os_error(libc::EBADF));
        }

        // We need to walk the tree.  First get the starting path.
        let start = &self.fids.get(&walk.fid).ok_or_else(ebadf)?.path;

        // Now walk the tree and break on the first error, if any.
        let expected_len = walk.wnames.len();
        let mut mds = Vec::with_capacity(expected_len);
        match do_walk(
            &self.proc,
            walk.wnames,
            start,
            self.cfg.ascii_casefold,
            &mut mds,
        ) {
            Ok(end) => {
                // Store the new fid if the full walk succeeded.
                if mds.len() == expected_len {
                    let st = mds.last().copied().map(Ok).unwrap_or_else(|| stat(&end))?;
                    self.fids.insert(
                        walk.newfid,
                        Fid {
                            path: end,
                            file: None,
                            filetype: st.st_mode.into(),
                        },
                    );
                }
            }
            Err(e) => {
                // Only return an error if it occurred on the first component.
                if mds.is_empty() {
                    return Err(e);
                }
            }
        }

        Ok(Rwalk {
            wqids: mds.into_iter().map(Qid::from).collect(),
        })
    }

    fn read(&mut self, read: &Tread) -> io::Result<Rread> {
        // Thankfully, `read` cannot be used to read directories in 9P2000.L.
        let file = self
            .fids
            .get_mut(&read.fid)
            .and_then(|fid| fid.file.as_mut())
            .ok_or_else(ebadf)?;

        // Use an empty Rread struct to figure out the overhead of the header.
        let header_size = Rframe {
            tag: 0,
            msg: Rmessage::Read(Rread {
                data: Data(Vec::new()),
            }),
        }
        .byte_size();

        let capacity = min(self.cfg.msize - header_size, read.count);
        let mut buf = Data(vec![0u8; capacity as usize]);

        let count = file.read_at(&mut buf, read.offset)?;
        buf.truncate(count);

        Ok(Rread { data: buf })
    }

    fn write(&mut self, write: &Twrite) -> io::Result<Rwrite> {
        let file = self
            .fids
            .get_mut(&write.fid)
            .and_then(|fid| fid.file.as_mut())
            .ok_or_else(ebadf)?;

        let count = file.write_at(&write.data, write.offset)?;
        Ok(Rwrite {
            count: count as u32,
        })
    }

    fn clunk(&mut self, clunk: &Tclunk) -> io::Result<()> {
        match self.fids.entry(clunk.fid) {
            btree_map::Entry::Vacant(_) => Err(io::Error::from_raw_os_error(libc::EBADF)),
            btree_map::Entry::Occupied(entry) => {
                entry.remove();
                Ok(())
            }
        }
    }

    fn remove(&mut self, _remove: &Tremove) -> io::Result<()> {
        // Since a file could be linked into multiple locations, there is no way to know exactly
        // which path we are supposed to unlink. Linux uses unlink_at anyway, so we can just return
        // an error here.
        Err(io::Error::from_raw_os_error(libc::EOPNOTSUPP))
    }

    fn statfs(&mut self, statfs: &Tstatfs) -> io::Result<Rstatfs> {
        let fid = self.fids.get(&statfs.fid).ok_or_else(ebadf)?;
        let mut buf = MaybeUninit::zeroed();

        // Safe because this will only modify `out` and we check the return value.
        syscall!(unsafe { libc::fstatfs64(fid.path.as_raw_fd(), buf.as_mut_ptr()) })?;

        // Safe because this only has integer types and any value is valid.
        let out = unsafe { buf.assume_init() };
        Ok(Rstatfs {
            ty: out.f_type as u32,
            bsize: out.f_bsize as u32,
            blocks: out.f_blocks,
            bfree: out.f_bfree,
            bavail: out.f_bavail,
            files: out.f_files,
            ffree: out.f_ffree,
            // Safe because the fsid has only integer fields and the compiler will verify that is
            // the same width as the `fsid` field in Rstatfs.
            fsid: unsafe { mem::transmute(out.f_fsid) },
            namelen: out.f_namelen as u32,
        })
    }

    fn lopen(&mut self, lopen: &Tlopen) -> io::Result<Rlopen> {
        let fid = self.fids.get_mut(&lopen.fid).ok_or_else(ebadf)?;

        let file = open_fid(&self.proc, &fid.path, lopen.flags)?;
        let st = stat(&file)?;

        fid.file = Some(file);
        Ok(Rlopen {
            qid: st.into(),
            iounit: 0, // Allow the client to send requests up to the negotiated max message size.
        })
    }

    fn lcreate(&mut self, lcreate: Tlcreate) -> io::Result<Rlcreate> {
        let fid = self.fids.get_mut(&lcreate.fid).ok_or_else(ebadf)?;

        if fid.filetype != FileType::Directory {
            return Err(io::Error::from_raw_os_error(libc::ENOTDIR));
        }

        let mut flags: i32 = libc::O_CLOEXEC | libc::O_CREAT | libc::O_EXCL;
        for &(p9f, of) in &MAPPED_FLAGS {
            if (lcreate.flags & p9f) != 0 {
                flags |= of;
            }
        }
        if lcreate.flags & P9_NOACCESS == P9_RDONLY {
            flags |= libc::O_RDONLY;
        }

        let name = string_to_cstring(lcreate.name)?;

        // Safe because this doesn't modify any memory and we check the return value.
        let fd = syscall!(unsafe {
            libc::openat64(fid.path.as_raw_fd(), name.as_ptr(), flags, lcreate.mode)
        })?;

        // Safe because we just opened this fd and we know it is valid.
        let file = unsafe { File::from_raw_fd(fd) };
        let st = stat(&file)?;

        fid.file = Some(file);
        fid.filetype = FileType::Regular;

        // This fid now refers to the newly created file so we need to update the O_PATH fd for it
        // as well.
        fid.path = lookup(&fid.path, &name)?;

        Ok(Rlcreate {
            qid: st.into(),
            iounit: 0, // Allow the client to send requests up to the negotiated max message size.
        })
    }

    fn symlink(&mut self, _symlink: &Tsymlink) -> io::Result<Rsymlink> {
        // symlinks are not allowed.
        Err(io::Error::from_raw_os_error(libc::EACCES))
    }

    fn mknod(&mut self, _mknod: &Tmknod) -> io::Result<Rmknod> {
        // No nodes either.
        Err(io::Error::from_raw_os_error(libc::EACCES))
    }

    fn rename(&mut self, _rename: &Trename) -> io::Result<()> {
        // We cannot support this as an inode may be linked into multiple directories but we don't
        // know which one the client wants us to rename. Linux uses rename_at anyway, so we don't
        // need to worry about this.
        Err(io::Error::from_raw_os_error(libc::EOPNOTSUPP))
    }

    fn readlink(&mut self, readlink: &Treadlink) -> io::Result<Rreadlink> {
        let fid = self.fids.get(&readlink.fid).ok_or_else(ebadf)?;

        let mut link = vec![0; libc::PATH_MAX as usize];

        // Safe because this will only modify `link` and we check the return value.
        let len = syscall!(unsafe {
            libc::readlinkat(
                fid.path.as_raw_fd(),
                [0].as_ptr(),
                link.as_mut_ptr() as *mut libc::c_char,
                link.len(),
            )
        })? as usize;
        link.truncate(len);
        let target = String::from_utf8(link)
            .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
        Ok(Rreadlink { target })
    }

    #[allow(clippy::unnecessary_cast)] // nlink_t is u32 on 32-bit platforms
    fn get_attr(&mut self, get_attr: &Tgetattr) -> io::Result<Rgetattr> {
        let fid = self.fids.get_mut(&get_attr.fid).ok_or_else(ebadf)?;

        let st = stat(&fid.path)?;

        Ok(Rgetattr {
            valid: P9_GETATTR_BASIC,
            qid: st.into(),
            mode: st.st_mode,
            uid: map_id_from_host(&self.cfg.uid_map, st.st_uid),
            gid: map_id_from_host(&self.cfg.gid_map, st.st_gid),
            nlink: st.st_nlink as u64,
            rdev: st.st_rdev,
            size: st.st_size as u64,
            blksize: st.st_blksize as u64,
            blocks: st.st_blocks as u64,
            atime_sec: st.st_atime as u64,
            atime_nsec: st.st_atime_nsec as u64,
            mtime_sec: st.st_mtime as u64,
            mtime_nsec: st.st_mtime_nsec as u64,
            ctime_sec: st.st_ctime as u64,
            ctime_nsec: st.st_ctime_nsec as u64,
            btime_sec: 0,
            btime_nsec: 0,
            gen: 0,
            data_version: 0,
        })
    }

    fn set_attr(&mut self, set_attr: &Tsetattr) -> io::Result<()> {
        let fid = self.fids.get(&set_attr.fid).ok_or_else(ebadf)?;
        let path = string_to_cstring(format!("self/fd/{}", fid.path.as_raw_fd()))?;

        if set_attr.valid & P9_SETATTR_MODE != 0 {
            // Safe because this doesn't modify any memory and we check the return value.
            syscall!(unsafe {
                libc::fchmodat(self.proc.as_raw_fd(), path.as_ptr(), set_attr.mode, 0)
            })?;
        }

        if set_attr.valid & (P9_SETATTR_UID | P9_SETATTR_GID) != 0 {
            let uid = if set_attr.valid & P9_SETATTR_UID != 0 {
                set_attr.uid
            } else {
                -1i32 as u32
            };
            let gid = if set_attr.valid & P9_SETATTR_GID != 0 {
                set_attr.gid
            } else {
                -1i32 as u32
            };

            // Safe because this doesn't modify any memory and we check the return value.
            syscall!(unsafe { libc::fchownat(self.proc.as_raw_fd(), path.as_ptr(), uid, gid, 0) })?;
        }

        if set_attr.valid & P9_SETATTR_SIZE != 0 {
            let file = if fid.filetype == FileType::Directory {
                return Err(io::Error::from_raw_os_error(libc::EISDIR));
            } else if let Some(ref file) = fid.file {
                MaybeOwned::Borrowed(file)
            } else {
                MaybeOwned::Owned(open_fid(&self.proc, &fid.path, P9_NONBLOCK | P9_RDWR)?)
            };

            file.set_len(set_attr.size)?;
        }

        if set_attr.valid & (P9_SETATTR_ATIME | P9_SETATTR_MTIME) != 0 {
            let times = [
                libc::timespec {
                    tv_sec: set_attr.atime_sec as _,
                    tv_nsec: if set_attr.valid & P9_SETATTR_ATIME == 0 {
                        libc::UTIME_OMIT
                    } else if set_attr.valid & P9_SETATTR_ATIME_SET == 0 {
                        libc::UTIME_NOW
                    } else {
                        set_attr.atime_nsec as _
                    },
                },
                libc::timespec {
                    tv_sec: set_attr.mtime_sec as _,
                    tv_nsec: if set_attr.valid & P9_SETATTR_MTIME == 0 {
                        libc::UTIME_OMIT
                    } else if set_attr.valid & P9_SETATTR_MTIME_SET == 0 {
                        libc::UTIME_NOW
                    } else {
                        set_attr.mtime_nsec as _
                    },
                },
            ];

            // Safe because file is valid and we have initialized times fully.
            let ret = unsafe {
                libc::utimensat(
                    self.proc.as_raw_fd(),
                    path.as_ptr(),
                    &times as *const libc::timespec,
                    0,
                )
            };
            if ret < 0 {
                return Err(io::Error::last_os_error());
            }
        }

        // The ctime would have been updated by any of the above operations so we only
        // need to change it if it was the only option given.
        if set_attr.valid & P9_SETATTR_CTIME != 0 && set_attr.valid & (!P9_SETATTR_CTIME) == 0 {
            // Setting -1 as the uid and gid will not actually change anything but will
            // still update the ctime.
            let ret = unsafe {
                libc::fchownat(
                    self.proc.as_raw_fd(),
                    path.as_ptr(),
                    libc::uid_t::max_value(),
                    libc::gid_t::max_value(),
                    0,
                )
            };
            if ret < 0 {
                return Err(io::Error::last_os_error());
            }
        }

        Ok(())
    }

    fn xattr_walk(&mut self, _xattr_walk: &Txattrwalk) -> io::Result<Rxattrwalk> {
        Err(io::Error::from_raw_os_error(libc::EOPNOTSUPP))
    }

    fn xattr_create(&mut self, _xattr_create: &Txattrcreate) -> io::Result<()> {
        Err(io::Error::from_raw_os_error(libc::EOPNOTSUPP))
    }

    fn readdir(&mut self, readdir: &Treaddir) -> io::Result<Rreaddir> {
        let fid = self.fids.get_mut(&readdir.fid).ok_or_else(ebadf)?;

        if fid.filetype != FileType::Directory {
            return Err(io::Error::from_raw_os_error(libc::ENOTDIR));
        }

        // Use an empty Rreaddir struct to figure out the maximum number of bytes that
        // can be returned.
        let header_size = Rframe {
            tag: 0,
            msg: Rmessage::Readdir(Rreaddir {
                data: Data(Vec::new()),
            }),
        }
        .byte_size();
        let count = min(self.cfg.msize - header_size, readdir.count);
        let mut cursor = Cursor::new(Vec::with_capacity(count as usize));

        let dir = fid.file.as_mut().ok_or_else(ebadf)?;
        let mut dirents = read_dir(dir, readdir.offset as libc::off64_t)?;
        while let Some(dirent) = dirents.next().transpose()? {
            let st = statat(&fid.path, dirent.name, 0)?;

            let name = dirent
                .name
                .to_str()
                .map(String::from)
                .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;

            let entry = Dirent {
                qid: st.into(),
                offset: dirent.offset,
                ty: dirent.type_,
                name,
            };

            let byte_size = entry.byte_size() as usize;

            if cursor.get_ref().capacity() - cursor.get_ref().len() < byte_size {
                // No more room in the buffer.
                break;
            }

            entry.encode(&mut cursor)?;
        }

        Ok(Rreaddir {
            data: Data(cursor.into_inner()),
        })
    }

    fn fsync(&mut self, fsync: &Tfsync) -> io::Result<()> {
        let file = self
            .fids
            .get(&fsync.fid)
            .and_then(|fid| fid.file.as_ref())
            .ok_or_else(ebadf)?;

        if fsync.datasync == 0 {
            file.sync_all()?;
        } else {
            file.sync_data()?;
        }
        Ok(())
    }

    /// Implement posix byte range locking code.
    /// Our implementation mirrors that of QEMU/9p - that is to say,
    /// we essentially punt on mirroring lock state between client/server
    /// and defer lock semantics to the VFS layer on the client side. Aside
    /// from fd existence check we always return success. QEMU reference:
    /// <https://github.com/qemu/qemu/blob/754f756cc4c6d9d14b7230c62b5bb20f9d655888/hw/9pfs/9p.c#L3669>
    ///
    /// NOTE: this means that files locked on the client may be interefered with
    /// from either the server's side, or from other clients (guests). This
    /// tracks with QEMU implementation, and will be obviated if crosvm decides
    /// to drop 9p in favor of virtio-fs. QEMU only allows for a single client,
    /// and we leave it to users of the crate to provide actual lock handling.
    fn lock(&mut self, lock: &Tlock) -> io::Result<Rlock> {
        // Ensure fd passed in TLOCK request exists and has a mapping.
        let fd = self
            .fids
            .get(&lock.fid)
            .and_then(|fid| fid.file.as_ref())
            .ok_or_else(ebadf)?
            .as_raw_fd();

        syscall!(unsafe {
            // Safe because zero-filled libc::stat is a valid value, fstat
            // populates the struct fields.
            let mut stbuf: libc::stat64 = std::mem::zeroed();
            // Safe because this doesn't modify memory and we check the return value.
            libc::fstat64(fd, &mut stbuf)
        })?;

        Ok(Rlock {
            status: P9_LOCK_SUCCESS,
        })
    }

    ///
    /// Much like lock(), defer locking semantics to VFS and return success.
    ///
    fn get_lock(&mut self, get_lock: &Tgetlock) -> io::Result<Rgetlock> {
        // Ensure fd passed in GETTLOCK request exists and has a mapping.
        let fd = self
            .fids
            .get(&get_lock.fid)
            .and_then(|fid| fid.file.as_ref())
            .ok_or_else(ebadf)?
            .as_raw_fd();

        // Safe because this doesn't modify memory and we check the return value.
        syscall!(unsafe {
            let mut stbuf: libc::stat64 = std::mem::zeroed();
            libc::fstat64(fd, &mut stbuf)
        })?;

        Ok(Rgetlock {
            type_: P9_LOCK_TYPE_UNLCK,
            start: get_lock.start,
            length: get_lock.length,
            proc_id: get_lock.proc_id,
            client_id: get_lock.client_id.clone(),
        })
    }

    fn link(&mut self, link: Tlink) -> io::Result<()> {
        let target = self.fids.get(&link.fid).ok_or_else(ebadf)?;
        let path = string_to_cstring(format!("self/fd/{}", target.path.as_raw_fd()))?;

        let dir = self.fids.get(&link.dfid).ok_or_else(ebadf)?;
        let name = string_to_cstring(link.name)?;

        // Safe because this doesn't modify any memory and we check the return value.
        syscall!(unsafe {
            libc::linkat(
                self.proc.as_raw_fd(),
                path.as_ptr(),
                dir.path.as_raw_fd(),
                name.as_ptr(),
                libc::AT_SYMLINK_FOLLOW,
            )
        })?;
        Ok(())
    }

    fn mkdir(&mut self, mkdir: Tmkdir) -> io::Result<Rmkdir> {
        let fid = self.fids.get(&mkdir.dfid).ok_or_else(ebadf)?;
        let name = string_to_cstring(mkdir.name)?;

        // Safe because this doesn't modify any memory and we check the return value.
        syscall!(unsafe { libc::mkdirat(fid.path.as_raw_fd(), name.as_ptr(), mkdir.mode) })?;
        Ok(Rmkdir {
            qid: statat(&fid.path, &name, 0).map(Qid::from)?,
        })
    }

    fn rename_at(&mut self, rename_at: Trenameat) -> io::Result<()> {
        let olddir = self.fids.get(&rename_at.olddirfid).ok_or_else(ebadf)?;
        let oldname = string_to_cstring(rename_at.oldname)?;

        let newdir = self.fids.get(&rename_at.newdirfid).ok_or_else(ebadf)?;
        let newname = string_to_cstring(rename_at.newname)?;

        // Safe because this doesn't modify any memory and we check the return value.
        syscall!(unsafe {
            libc::renameat(
                olddir.path.as_raw_fd(),
                oldname.as_ptr(),
                newdir.path.as_raw_fd(),
                newname.as_ptr(),
            )
        })?;

        Ok(())
    }

    fn unlink_at(&mut self, unlink_at: Tunlinkat) -> io::Result<()> {
        let dir = self.fids.get(&unlink_at.dirfd).ok_or_else(ebadf)?;
        let name = string_to_cstring(unlink_at.name)?;

        syscall!(unsafe {
            libc::unlinkat(
                dir.path.as_raw_fd(),
                name.as_ptr(),
                unlink_at.flags as libc::c_int,
            )
        })?;

        Ok(())
    }
}

#[cfg(test)]
mod tests;