nfs-rs 0.6.1

An asynchronous pure Rust client library for NFSv3, experimental NFSv4.0, and NFSv4.1
Documentation
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
// Copyright 2025 NetApp Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

//! Lifecycle seam used by private language adapters.

use crate::{NFSVersion, NfsError, OperationOutcome, RecoveryAction, Result};
use async_trait::async_trait;
use std::collections::VecDeque;
use std::fmt;
use std::future::Future;
use std::sync::atomic::{AtomicU8, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use tokio::sync::Notify;

const READY: u8 = 0;
const CLOSING: u8 = 1;
const CLOSED: u8 = 2;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ClientLifecycle {
    Ready,
    Closing,
    Closed,
}

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ResourceKey(u64);

impl fmt::Display for ResourceKey {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(formatter)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CoreOperation {
    pub name: String,
    pub safe_path: Option<String>,
}

#[async_trait]
pub trait ClientDriver: fmt::Debug + Send + Sync + 'static {
    async fn execute(&self, operation: CoreOperation) -> Result<()>;
    async fn close_resource(&self, key: ResourceKey) -> Result<()>;
    async fn umount(&self) -> Result<()>;
}

#[derive(Debug, Default)]
pub struct ClientCloseReport {
    errors: Vec<Arc<NfsError>>,
}

impl ClientCloseReport {
    pub fn errors(&self) -> &[Arc<NfsError>] {
        &self.errors
    }
}

#[derive(Debug)]
pub struct ClientCore {
    driver: Arc<dyn ClientDriver>,
    lifecycle: AtomicU8,
    next_resource_key: AtomicU64,
    in_flight: DrainCounter,
    resources: Mutex<Vec<ResourceKey>>,
    owned_tasks: DrainCounter,
    recovery_events: Mutex<RecoveryEventQueue>,
    close_state: Mutex<CloseState>,
    close_notify: Notify,
    lifecycle_notify: Notify,
}

#[derive(Debug, Default)]
struct CloseState {
    started: bool,
    report: Option<Arc<ClientCloseReport>>,
}

#[derive(Debug, Default)]
struct DrainCounter {
    count: AtomicU64,
    notify: Notify,
}

impl DrainCounter {
    fn increment(&self) {
        self.count.fetch_add(1, Ordering::AcqRel);
    }

    fn decrement(&self) {
        if self.count.fetch_sub(1, Ordering::AcqRel) == 1 {
            self.notify.notify_waiters();
        }
    }

    fn count(&self) -> u64 {
        self.count.load(Ordering::Acquire)
    }

    async fn wait_for_zero(&self) {
        while self.count() != 0 {
            let notified = self.notify.notified();
            if self.count() != 0 {
                notified.await;
            }
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CoreRecoveryEvent {
    pub operation: String,
    pub safe_path: Option<String>,
    pub protocol: NFSVersion,
    pub outcome: OperationOutcome,
    pub recovery: RecoveryAction,
    pub completed_bytes: Option<u64>,
    pub message: String,
}

#[derive(Debug)]
struct RecoveryEventQueue {
    capacity: usize,
    dropped: u64,
    events: VecDeque<CoreRecoveryEvent>,
}

impl ClientCore {
    pub fn new(driver: Arc<dyn ClientDriver>) -> Arc<Self> {
        Self::build(driver, 256)
    }

    pub fn with_recovery_event_capacity(
        driver: Arc<dyn ClientDriver>,
        recovery_event_capacity: usize,
    ) -> Result<Arc<Self>> {
        if recovery_event_capacity == 0 {
            return Err(NfsError::InvalidInput(
                "recovery-event capacity must be positive".to_string(),
            ));
        }
        Ok(Self::build(driver, recovery_event_capacity))
    }

    fn build(driver: Arc<dyn ClientDriver>, recovery_event_capacity: usize) -> Arc<Self> {
        Arc::new(Self {
            driver,
            lifecycle: AtomicU8::new(READY),
            next_resource_key: AtomicU64::new(1),
            in_flight: DrainCounter::default(),
            resources: Mutex::new(Vec::new()),
            owned_tasks: DrainCounter::default(),
            recovery_events: Mutex::new(RecoveryEventQueue {
                capacity: recovery_event_capacity,
                dropped: 0,
                events: VecDeque::new(),
            }),
            close_state: Mutex::new(CloseState::default()),
            close_notify: Notify::new(),
            lifecycle_notify: Notify::new(),
        })
    }

    fn ensure_ready(&self) -> Result<()> {
        if self.lifecycle.load(Ordering::Acquire) == READY {
            Ok(())
        } else {
            Err(NfsError::ClientClosed(
                "connected client is closing or closed".to_string(),
            ))
        }
    }

    pub fn lifecycle(&self) -> ClientLifecycle {
        match self.lifecycle.load(Ordering::Acquire) {
            READY => ClientLifecycle::Ready,
            CLOSING => ClientLifecycle::Closing,
            _ => ClientLifecycle::Closed,
        }
    }

    pub async fn wait_for_lifecycle(&self, expected: ClientLifecycle) {
        while self.lifecycle() != expected {
            let notified = self.lifecycle_notify.notified();
            if self.lifecycle() != expected {
                notified.await;
            }
        }
    }

    pub async fn execute(self: &Arc<Self>, operation: CoreOperation) -> Result<()> {
        let _operation = self.begin_operation()?;
        self.driver.execute(operation).await
    }

    pub fn record_recovery_event(&self, event: CoreRecoveryEvent) -> Result<()> {
        let mut queue = self
            .recovery_events
            .lock()
            .map_err(|_| NfsError::Rpc("recovery-event queue lock poisoned".to_string()))?;
        if queue.events.len() == queue.capacity {
            queue.events.pop_front();
            queue.dropped = queue.dropped.saturating_add(1);
        }
        queue.events.push_back(event);
        Ok(())
    }

    pub fn recovery_events(&self) -> Result<Vec<CoreRecoveryEvent>> {
        self.recovery_events
            .lock()
            .map(|queue| queue.events.iter().cloned().collect())
            .map_err(|_| NfsError::Rpc("recovery-event queue lock poisoned".to_string()))
    }

    pub fn drain_recovery_events(&self) -> Result<Vec<CoreRecoveryEvent>> {
        self.recovery_events
            .lock()
            .map(|mut queue| queue.events.drain(..).collect())
            .map_err(|_| NfsError::Rpc("recovery-event queue lock poisoned".to_string()))
    }

    pub fn dropped_recovery_event_count(&self) -> Result<u64> {
        self.recovery_events
            .lock()
            .map(|queue| queue.dropped)
            .map_err(|_| NfsError::Rpc("recovery-event queue lock poisoned".to_string()))
    }

    pub fn register_resource(&self) -> Result<ResourceKey> {
        let key = self.allocate_resource_key()?;
        self.publish_resource(key)?;
        Ok(key)
    }

    pub fn allocate_resource_key(&self) -> Result<ResourceKey> {
        self.ensure_ready()?;
        Ok(ResourceKey(
            self.next_resource_key.fetch_add(1, Ordering::Relaxed),
        ))
    }

    pub fn publish_resource(&self, key: ResourceKey) -> Result<()> {
        let mut resources = self
            .resources
            .lock()
            .map_err(|_| NfsError::Rpc("client resource registry lock poisoned".to_string()))?;
        self.ensure_ready()?;
        resources.push(key);
        Ok(())
    }

    pub fn unregister_resource(&self, key: ResourceKey) -> Result<bool> {
        let mut resources = self
            .resources
            .lock()
            .map_err(|_| NfsError::Rpc("client resource registry lock poisoned".to_string()))?;
        let Some(position) = resources.iter().position(|candidate| *candidate == key) else {
            return Ok(false);
        };
        resources.remove(position);
        Ok(true)
    }

    pub fn resource_count(&self) -> usize {
        self.resources
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .len()
    }

    pub fn begin_operation(self: &Arc<Self>) -> Result<OperationGuard> {
        self.ensure_ready()?;
        self.in_flight.increment();
        if let Err(error) = self.ensure_ready() {
            self.finish_operation();
            return Err(error);
        }
        Ok(OperationGuard {
            core: Some(Arc::clone(self)),
        })
    }

    fn finish_operation(&self) {
        self.in_flight.decrement();
    }

    pub fn spawn_owned<F>(self: &Arc<Self>, future: F) -> Result<()>
    where
        F: Future<Output = ()> + Send + 'static,
    {
        self.ensure_ready()?;
        self.owned_tasks.increment();
        if let Err(error) = self.ensure_ready() {
            self.finish_owned_task();
            return Err(error);
        }
        let core = Arc::clone(self);
        tokio::spawn(async move {
            let _guard = OwnedTaskGuard { core };
            future.await;
        });
        Ok(())
    }

    fn finish_owned_task(&self) {
        self.owned_tasks.decrement();
    }

    pub fn owned_task_count(&self) -> u64 {
        self.owned_tasks.count()
    }

    pub async fn close(self: &Arc<Self>) -> Arc<ClientCloseReport> {
        let start_cleanup = {
            let mut state = self
                .close_state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            if state.started {
                false
            } else {
                state.started = true;
                true
            }
        };
        if start_cleanup {
            self.lifecycle.store(CLOSING, Ordering::Release);
            self.lifecycle_notify.notify_waiters();
            let core = Arc::clone(self);
            tokio::spawn(async move {
                core.run_close().await;
            });
        }

        loop {
            let notified = self.close_notify.notified();
            if let Some(report) = self
                .close_state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .report
                .clone()
            {
                return report;
            }
            notified.await;
        }
    }

    async fn run_close(&self) {
        self.in_flight.wait_for_zero().await;
        self.owned_tasks.wait_for_zero().await;
        let mut errors = Vec::new();
        if self.close_state.is_poisoned() {
            errors.push(Arc::new(NfsError::Rpc(
                "client close-state lock poisoned".to_string(),
            )));
        }
        if self.resources.is_poisoned() {
            errors.push(Arc::new(NfsError::Rpc(
                "client resource registry lock poisoned".to_string(),
            )));
        }
        let resources = {
            let mut resources = self
                .resources
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            std::mem::take(&mut *resources)
        };
        for key in resources {
            if let Err(error) = self.driver.close_resource(key).await {
                errors.push(Arc::new(error));
            }
        }
        if let Err(error) = self.driver.umount().await {
            errors.push(Arc::new(error));
        }
        self.lifecycle.store(CLOSED, Ordering::Release);
        self.lifecycle_notify.notify_waiters();
        let report = Arc::new(ClientCloseReport { errors });
        self.close_state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .report = Some(report);
        self.close_notify.notify_waiters();
    }
}

#[derive(Debug)]
pub struct OperationGuard {
    core: Option<Arc<ClientCore>>,
}

impl Drop for OperationGuard {
    fn drop(&mut self) {
        if let Some(core) = self.core.take() {
            core.finish_operation();
        }
    }
}

struct OwnedTaskGuard {
    core: Arc<ClientCore>,
}

impl Drop for OwnedTaskGuard {
    fn drop(&mut self) {
        self.core.finish_owned_task();
    }
}

#[cfg(test)]
mod poison_tests {
    use super::*;
    use std::panic::{AssertUnwindSafe, catch_unwind};

    #[derive(Debug, Default)]
    struct Driver {
        closed: Mutex<Vec<ResourceKey>>,
    }

    #[async_trait]
    impl ClientDriver for Driver {
        async fn execute(&self, _operation: CoreOperation) -> Result<()> {
            Ok(())
        }

        async fn close_resource(&self, key: ResourceKey) -> Result<()> {
            self.closed.lock().unwrap().push(key);
            Ok(())
        }

        async fn umount(&self) -> Result<()> {
            Ok(())
        }
    }

    #[tokio::test]
    async fn poisoned_resource_registry_is_reported_without_skipping_cleanup() {
        let driver = Arc::new(Driver::default());
        let core = ClientCore::new(driver.clone());
        let key = core.register_resource().unwrap();
        let _ = catch_unwind(AssertUnwindSafe(|| {
            let _guard = core.resources.lock().unwrap();
            panic!("poison resource registry");
        }));

        let report = tokio::time::timeout(std::time::Duration::from_secs(1), core.close())
            .await
            .expect("poisoned close must terminate");
        assert!(
            report.errors()[0]
                .to_string()
                .contains("registry lock poisoned")
        );
        assert_eq!(*driver.closed.lock().unwrap(), vec![key]);
    }

    #[tokio::test]
    async fn poisoned_close_state_is_reported_and_publishes_terminal_report() {
        let core = ClientCore::new(Arc::new(Driver::default()));
        let _ = catch_unwind(AssertUnwindSafe(|| {
            let _guard = core.close_state.lock().unwrap();
            panic!("poison close state");
        }));

        let report = tokio::time::timeout(std::time::Duration::from_secs(1), core.close())
            .await
            .expect("poisoned close must terminate");
        assert!(
            report.errors()[0]
                .to_string()
                .contains("close-state lock poisoned")
        );
        assert_eq!(core.lifecycle(), ClientLifecycle::Closed);
    }
}