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
// Copyright 2023 Remi Bernotavicius

use derive_more::From;
use nfs4::*;
use paste::paste;
use rand::Rng as _;
use std::collections::VecDeque;
use std::io;
use std::path::{Component, Path};
use sun_rpc_client::{RpcClient, Transport};

pub type Result<T> = std::result::Result<T, Error>;

pub struct TempResult<T>(Result<T>);

impl<T> From<StatusResult<T>> for TempResult<T> {
    fn from(res: StatusResult<T>) -> Self {
        TempResult(match res {
            StatusResult::Ok(res) => Ok(res),
            StatusResult::Err(e) => Err(e.into()),
        })
    }
}

impl From<SetAttrStatusResult> for TempResult<SetAttrRes> {
    fn from(res: SetAttrStatusResult) -> Self {
        TempResult(match res.status {
            StatusResult::Ok(_) => Ok(res.res),
            StatusResult::Err(e) => Err(e.into()),
        })
    }
}

impl<T> From<LockStatusResult<T>> for TempResult<T> {
    fn from(res: LockStatusResult<T>) -> Self {
        TempResult(match res {
            LockStatusResult::Ok(res) => Ok(res),
            LockStatusResult::Err(e) => Err(e.into()),
        })
    }
}

#[derive(Debug, From)]
pub enum Error {
    SunRpc(sun_rpc_client::Error),
    Protocol(StatusError),
    Lock(LockStatusError),
    Io(std::io::Error),
    #[from(ignore)]
    CompoundResponseMismatch(String),
}

const NFS: u32 = 100003;
const NFS_CB: u32 = 0x40000000;
pub const NFS_PORT: u16 = 2049;
const COMPOUND_PROCEDURE: u32 = 1;

macro_rules! compound_op_impl_ {
    ($name:ident, $args:ident, $res:ty) => {
        impl CompoundRequest for $args {
            type Response = $res;
            type Geometry = ();

            fn into_arg_array(self) -> (Vec<ArgOp>, Self::Geometry) {
                (vec![self.into()], ())
            }

            fn process_reply(
                res_array: &mut VecDeque<ResOp>,
                _geometry: (),
            ) -> Result<Self::Response> {
                let op = res_array
                    .pop_front()
                    .ok_or(Error::CompoundResponseMismatch("too few replies".into()))?;

                if let ResOp::$name(res) = op {
                    TempResult::from(res).0
                } else {
                    Err(Error::CompoundResponseMismatch(format!(
                        "expected {}, got {op:?}",
                        stringify!($name)
                    )))
                }
            }
        }
    };
}

macro_rules! compound_op_impl {
    ($($name:ident)*) => ($(paste! {
        compound_op_impl_! { $name, [<$name Args>], [<$name Res>] }
    })*)
}

macro_rules! compound_op_impl_no_args {
    ($($name:ident)*) => ($(paste! {
        #[derive(Copy, Clone)]
        pub struct $name;

        impl From<$name> for ArgOp {
            fn from(_: $name) -> ArgOp {
                ArgOp::$name
            }
        }

        compound_op_impl_! { $name, $name, [<$name Res>] }
    })*)
}

macro_rules! compound_op_impl_no_args_no_ret {
    ($($name:ident)*) => ($(paste! {
        #[derive(Copy, Clone)]
        pub struct $name;

        impl From<$name> for ArgOp {
            fn from(_: $name) -> ArgOp {
                ArgOp::$name
            }
        }

        compound_op_impl_! { $name, $name, () }
    })*)
}

macro_rules! compound_op_impl_no_ret {
    ($($name:ident)*) => ($(paste! {
        compound_op_impl_! { $name, [<$name Args>], () }
    })*)
}

compound_op_impl! {
    Close
    Commit
    Create
    GetAttr
    Link
    Lock
    LockU
    Open
    OpenDowngrade
    Read
    ReadDir
    Remove
    Rename
    SecInfo
    SetAttr
    Write
    BindConnToSession
    ExchangeId
    CreateSession
    GetDirDelegation
    GetDeviceInfo
    GetDeviceList
    LayoutCommit
    LayoutGet
    LayoutReturn
    SecInfoNoName
    Sequence
    SetSsv
    TestStateId
    WantDelegation
}

compound_op_impl_no_ret! {
    DelegPurge
    DelegReturn
    LockT
    LookUp
    NVerify
    OpenAttr
    PutFh
    Verify
    BackchannelCtl
    DestroySession
    FreeStateid
    DestroyClientId
    ReclaimComplete
}

compound_op_impl_no_args! {
    GetFh
    ReadLink
}

compound_op_impl_no_args_no_ret! {
    PutPubFh
    LookUpP
    PutRootFh
    RestoreFh
    SaveFh
}

trait CompoundRequest {
    type Response;
    type Geometry;

    fn into_arg_array(self) -> (Vec<ArgOp>, Self::Geometry);

    fn process_reply(
        res_array: &mut VecDeque<ResOp>,
        geometry: Self::Geometry,
    ) -> Result<Self::Response>;
}

impl<T> CompoundRequest for Vec<T>
where
    T: CompoundRequest,
{
    type Response = Vec<<T as CompoundRequest>::Response>;
    type Geometry = Vec<<T as CompoundRequest>::Geometry>;

    fn into_arg_array(self) -> (Vec<ArgOp>, Self::Geometry) {
        let mut arg_ret = vec![];
        let mut geo_ret = vec![];
        for e in self {
            let (v, g) = e.into_arg_array();
            arg_ret.extend(v);
            geo_ret.push(g);
        }
        (arg_ret, geo_ret)
    }

    fn process_reply(
        res_array: &mut VecDeque<ResOp>,
        geometry: Self::Geometry,
    ) -> Result<Self::Response> {
        let mut ret = vec![];
        for geo in geometry {
            ret.push(T::process_reply(res_array, geo)?);
        }
        Ok(ret)
    }
}

struct ReturnSecond<A, B>(A, B);

impl<A, B> CompoundRequest for ReturnSecond<A, B>
where
    A: CompoundRequest,
    B: CompoundRequest,
{
    type Response = B::Response;
    type Geometry = (A::Geometry, B::Geometry);

    fn into_arg_array(self) -> (Vec<ArgOp>, Self::Geometry) {
        let (mut a1, g1) = self.0.into_arg_array();
        let (a2, g2) = self.1.into_arg_array();
        a1.extend(a2);
        (a1, (g1, g2))
    }

    fn process_reply(
        res_array: &mut VecDeque<ResOp>,
        geometry: Self::Geometry,
    ) -> Result<Self::Response> {
        A::process_reply(res_array, geometry.0)?;
        Ok(B::process_reply(res_array, geometry.1)?)
    }
}

macro_rules! tuple_impls {
    ($(($($n:tt $name:ident)+))+) => {$(paste! {
        impl<$($name),+> CompoundRequest for ($($name),+)
        where
            $($name: CompoundRequest,)+
        {
            type Response = ($(<$name as CompoundRequest>::Response),+);
            type Geometry = ($(<$name as CompoundRequest>::Geometry),+);

            fn into_arg_array(self) -> (Vec<ArgOp>, Self::Geometry) {
                $(let ([<v $n>], [<g $n>]) = self.$n.into_arg_array();)+

                let mut v = vec![];
                $(v.extend([<v $n>]);)+
                (
                    v,
                    ($([<g $n>]),+)
                )
            }

            fn process_reply(
                res_array: &mut VecDeque<ResOp>, geometry: Self::Geometry
            ) -> Result<Self::Response> {
                Ok((
                    $(
                        $name::process_reply(res_array, geometry.$n)?
                    ),+
                ))
            }
        }
    })+}
}

tuple_impls! {
    (0 A 1 B)
    (0 A 1 B 2 C)
    (0 A 1 B 2 C 3 D)
    (0 A 1 B 2 C 3 D 4 E)
    (0 A 1 B 2 C 3 D 4 E 5 F)
    (0 A 1 B 2 C 3 D 4 E 5 F 6 G)
    (0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H)
    (0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I)
    (0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J)
    (0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J 10 K)
    (0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J 10 K 11 L)
    (0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J 10 K 11 L 12 M)
    (0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J 10 K 11 L 12 M 13 N)
    (0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J 10 K 11 L 12 M 13 N 14 O)
    (0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J 10 K 11 L 12 M 13 N 14 O 15 P)
    (0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J 10 K 11 L 12 M 13 N 14 O 15 P 16 Q)
}

struct ClientWithoutSession {
    rpc_client: RpcClient,
}

impl ClientWithoutSession {
    fn new(rpc_client: RpcClient) -> Self {
        Self { rpc_client }
    }

    fn do_compound<'a, Args>(
        &mut self,
        mut transport: &mut impl Transport,
        args: Args,
    ) -> Result<Args::Response>
    where
        Args: CompoundRequest,
    {
        let (arg_array, geometry) = args.into_arg_array();
        let call_args = CompoundArgs {
            tag: "Test Client".into(),
            minor_version: 1,
            arg_array,
        };

        self.rpc_client
            .send_request(&mut transport, COMPOUND_PROCEDURE, call_args)?;

        let compound_reply: CompoundRes = self.rpc_client.receive_reply(&mut transport)?;

        if let StatusResult::Err(e) = compound_reply.status {
            return Err(e.into());
        }

        let mut res_array = compound_reply.res_array.into_iter().collect();
        let reply = Args::process_reply(&mut res_array, geometry)?;

        if !res_array.is_empty() {
            return Err(Error::CompoundResponseMismatch(format!(
                "trailing response: {res_array:?}"
            )));
        }

        Ok(reply)
    }
}

fn random_client_owner() -> ClientOwner {
    let mut rng = rand::thread_rng();
    ClientOwner {
        verifier: Verifier(0x0),
        owner_id: rng.gen::<u64>().to_be_bytes().into(),
    }
}

pub struct Client {
    raw_client: ClientWithoutSession,
    session: CreateSessionRes,
    sequence_id: SequenceId,
    client_id: ClientId,
    client_owner: ClientOwner,
    max_read: u64,
    max_write: u64,
}

impl Client {
    pub fn new(transport: &mut impl Transport) -> Result<Self> {
        let mut raw_client = ClientWithoutSession::new(RpcClient::new(NFS));

        let client_owner = random_client_owner();
        let eid_res = raw_client.do_compound(
            transport,
            ExchangeIdArgs {
                client_owner: client_owner.clone(),
                flags: ExchangeIdFlags::empty(),
                state_protect: StateProtect::None,
                client_impl_id: None,
            },
        )?;

        let client_id = eid_res.client_id;
        let session = raw_client.do_compound(
            transport,
            CreateSessionArgs {
                client_id,
                sequence_id: SequenceId(1),
                flags: CreateSessionFlags::empty(),
                fore_channel_attrs: ChannelAttrs {
                    header_pad_size: 0,
                    max_request_size: 1049620,
                    max_response_size: 1049480,
                    max_response_size_cached: 7584,
                    max_operations: 16,
                    max_requests: 64,
                    rdma_ird: None,
                },
                back_channel_attrs: ChannelAttrs {
                    header_pad_size: 0,
                    max_request_size: 4096,
                    max_response_size: 4096,
                    max_response_size_cached: 0,
                    max_operations: 16,
                    max_requests: 16,
                    rdma_ird: None,
                },
                program: NFS_CB,
                security_parameters: vec![],
            },
        )?;

        let mut client = Self {
            raw_client,
            session,
            sequence_id: SequenceId(1),
            client_id,
            client_owner,
            max_read: 0,
            max_write: 0,
        };

        let root_attrs = client
            .do_compound(
                transport,
                ReturnSecond(
                    (ReclaimCompleteArgs { one_fs: false }, PutRootFh),
                    GetAttrArgs {
                        attr_request: [FileAttributeId::MaxRead, FileAttributeId::MaxWrite]
                            .into_iter()
                            .collect(),
                    },
                ),
            )?
            .object_attributes;

        client.max_read = *root_attrs.get_as(FileAttributeId::MaxRead).unwrap();
        client.max_write = *root_attrs.get_as(FileAttributeId::MaxWrite).unwrap();

        Ok(client)
    }

    fn do_compound<'a, Args>(
        &mut self,
        transport: &mut impl Transport,
        args: Args,
    ) -> Result<Args::Response>
    where
        Args: CompoundRequest,
    {
        let sequence = SequenceArgs {
            session_id: self.session.session_id,
            sequence_id: self.sequence_id,
            slot_id: SlotId(0),
            highest_slot_id: SlotId(0),
            cache_this: false,
        };

        self.sequence_id.incr();

        Ok(self
            .raw_client
            .do_compound(transport, ReturnSecond(sequence, args))?)
    }

    pub fn get_attr(
        &mut self,
        transport: &mut impl Transport,
        path: impl AsRef<Path>,
    ) -> Result<GetAttrRes> {
        let mut get_attr_res = self.do_compound(
            transport,
            ReturnSecond(
                PutRootFh,
                GetAttrArgs {
                    attr_request: [FileAttributeId::SupportedAttrs].into_iter().collect(),
                },
            ),
        )?;

        let mut supported_attrs: EnumSet<FileAttributeId> = get_attr_res
            .object_attributes
            .remove_as(FileAttributeId::SupportedAttrs)
            .unwrap();

        supported_attrs.remove(FileAttributeId::TimeAccessSet);
        supported_attrs.remove(FileAttributeId::TimeModifySet);

        Ok(self.do_compound(
            transport,
            ReturnSecond(
                (
                    PutRootFh,
                    Vec::from_iter(path.as_ref().components().filter_map(|c| match c {
                        Component::Normal(p) => Some(LookUpArgs {
                            object_name: p.to_str().unwrap().into(),
                        }),
                        _ => None,
                    })),
                ),
                GetAttrArgs {
                    attr_request: supported_attrs,
                },
            ),
        )?)
    }

    pub fn look_up(
        &mut self,
        transport: &mut impl Transport,
        path: impl AsRef<Path>,
    ) -> Result<FileHandle> {
        Ok(self
            .do_compound(
                transport,
                ReturnSecond(
                    (
                        PutRootFh,
                        Vec::from_iter(path.as_ref().components().filter_map(|c| match c {
                            Component::Normal(p) => Some(LookUpArgs {
                                object_name: p.to_str().unwrap().into(),
                            }),
                            _ => None,
                        })),
                    ),
                    GetFh,
                ),
            )?
            .object)
    }

    pub fn read(
        &mut self,
        transport: &mut impl Transport,
        handle: FileHandle,
        offset: u64,
        count: u32,
    ) -> Result<ReadRes> {
        Ok(self.do_compound(
            transport,
            ReturnSecond(
                PutFhArgs { object: handle },
                ReadArgs {
                    state_id: StateId::anonymous(),
                    offset,
                    count,
                },
            ),
        )?)
    }

    pub fn read_all(
        &mut self,
        transport: &mut impl Transport,
        handle: FileHandle,
        mut sink: impl io::Write,
    ) -> Result<()> {
        let mut offset = 0;
        loop {
            let read_res = self.read(
                transport,
                handle.clone(),
                offset,
                self.max_read.try_into().unwrap(),
            )?;
            offset += read_res.data.len() as u64;
            sink.write_all(&read_res.data)?;
            if read_res.eof {
                break;
            }
        }
        Ok(())
    }

    pub fn write(
        &mut self,
        transport: &mut impl Transport,
        handle: FileHandle,
        offset: u64,
        data: Vec<u8>,
    ) -> Result<WriteRes> {
        Ok(self.do_compound(
            transport,
            ReturnSecond(
                PutFhArgs { object: handle },
                WriteArgs {
                    state_id: StateId::anonymous(),
                    offset,
                    stable: StableHow::FileSync,
                    data,
                },
            ),
        )?)
    }

    pub fn write_all(
        &mut self,
        transport: &mut impl Transport,
        handle: FileHandle,
        mut source: impl io::Read,
    ) -> Result<()> {
        let mut offset = 0;
        loop {
            let mut buf = vec![0; self.max_write as usize];
            let amount_read = source.read(&mut buf[..])?;
            if amount_read == 0 {
                break;
            }

            buf.resize(amount_read, 0);

            while buf.len() > 0 {
                let write_res = self.write(transport, handle.clone(), offset, buf.clone())?;
                buf = buf[write_res.count as usize..].to_owned();
            }

            offset += amount_read as u64;
        }
        Ok(())
    }

    pub fn create_file(
        &mut self,
        transport: &mut impl Transport,
        parent: FileHandle,
        name: &str,
    ) -> Result<FileHandle> {
        Ok(self
            .do_compound(
                transport,
                ReturnSecond(
                    (
                        PutFhArgs { object: parent },
                        OpenArgs {
                            sequence_id: SequenceId(0),
                            share_access: ShareAccess::WRITE,
                            share_deny: ShareDeny::NONE,
                            owner: StateOwner {
                                client_id: self.client_id,
                                opaque: self.client_owner.owner_id.clone(),
                            },
                            open_how: OpenFlag::OpenCreate(CreateHow::Exclusive {
                                create_verifier: Verifier(0),
                            }),
                            claim: OpenClaim::Null { file: name.into() },
                        },
                    ),
                    GetFh,
                ),
            )?
            .object)
    }
}