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
// 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
use super::{Mount, WRITE3args, WriteStable, nfs_fh3, stable_how};
use crate::error::{NfsError, Result};
use bytes::Bytes;
#[allow(unused)]
impl Mount {
pub async fn write_with_outcome(
&self,
fh: Bytes,
offset: u64,
data: Bytes,
) -> Result<crate::mount::WriteOutcome> {
if data.len() > u32::MAX as usize {
return Err(NfsError::InvalidInput(
"data length exceeds maximum".to_string(),
));
}
let count = data.len() as u32;
let ok = self
._write(WRITE3args {
file: nfs_fh3 { data: fh },
stable: WriteStable::FileSync,
count,
data,
offset,
})
.await?;
let verifier = ok
.verf
.0
.as_ref()
.try_into()
.map_err(|_| NfsError::Xdr("WRITE verifier must be 8 bytes".to_string()))?;
Ok(crate::mount::WriteOutcome {
count: ok.count.0,
stable: ok.committed == stable_how::FILE_SYNC,
verifier: Some(verifier),
})
}
pub async fn write_path(&self, path: &str, offset: u64, data: Bytes) -> Result<u32> {
self.write(self.lookup_path(path).await?.fh, offset, data)
.await
}
pub async fn write(&self, fh: Bytes, offset: u64, data: Bytes) -> Result<u32> {
if data.len() > u32::MAX as usize {
return Err(NfsError::InvalidInput(
"data length exceeds maximum".to_string(),
));
}
let count = data.len() as u32;
let args = WRITE3args {
file: nfs_fh3 { data: fh.clone() },
stable: WriteStable::FileSync,
count,
data,
offset,
};
let ok = self._write(args).await?;
// RFC 1813 ยง3.3.7: server may downgrade stability. If we requested
// FILE_SYNC but the server returned UNSTABLE or DATA_SYNC, issue
// a COMMIT to ensure data reaches stable storage.
if ok.committed != stable_how::FILE_SYNC {
tracing::debug!(
committed = ?ok.committed,
offset,
count = ok.count.0,
"server downgraded write stability, issuing COMMIT"
);
self.commit(fh, offset, ok.count.0).await?;
}
Ok(ok.count.0)
}
}
#[cfg(test)]
#[cfg(not(target_arch = "wasm32"))] // usize and u32 are the same for wasm32, so below test won't compile due to overflow in (u32::MAX as usize) + 1
mod tests {
use super::*;
#[tokio::test]
async fn mount_write_fh_data_exceeding_max_length() {
let mount = Mount {
rpc: crate::rpc::Client::new_dummy().await,
auth: crate::rpc::auth::Auth::new_null(),
dir: "/".to_string(),
fh: Bytes::new(),
dircount: 512,
maxcount: 4096,
rsize: 8192,
wsize: 16384,
};
let data = vec![0u8; (u32::MAX as usize) + 1];
let res = mount.write(Bytes::new(), 0, Bytes::from(data)).await;
assert!(matches!(res, Err(NfsError::InvalidInput(_))));
}
}