]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
1da177e4 LT |
3 | * File operations used by nfsd. Some of these have been ripped from |
4 | * other parts of the kernel because they weren't exported, others | |
5 | * are partial duplicates with added or changed functionality. | |
6 | * | |
7 | * Note that several functions dget() the dentry upon which they want | |
8 | * to act, most notably those that create directory entries. Response | |
9 | * dentry's are dput()'d if necessary in the release callback. | |
10 | * So if you notice code paths that apparently fail to dput() the | |
11 | * dentry, don't worry--they have been taken care of. | |
12 | * | |
13 | * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de> | |
14 | * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp> | |
15 | */ | |
16 | ||
1da177e4 LT |
17 | #include <linux/fs.h> |
18 | #include <linux/file.h> | |
d6b29d7c | 19 | #include <linux/splice.h> |
95d871f0 | 20 | #include <linux/falloc.h> |
1da177e4 | 21 | #include <linux/fcntl.h> |
1da177e4 | 22 | #include <linux/namei.h> |
1da177e4 | 23 | #include <linux/delay.h> |
0eeca283 | 24 | #include <linux/fsnotify.h> |
1da177e4 | 25 | #include <linux/posix_acl_xattr.h> |
1da177e4 | 26 | #include <linux/xattr.h> |
9a74af21 BH |
27 | #include <linux/jhash.h> |
28 | #include <linux/ima.h> | |
cbcc268b | 29 | #include <linux/pagemap.h> |
5a0e3ad6 | 30 | #include <linux/slab.h> |
7c0f6ba6 | 31 | #include <linux/uaccess.h> |
f501912a BM |
32 | #include <linux/exportfs.h> |
33 | #include <linux/writeback.h> | |
18032ca0 | 34 | #include <linux/security.h> |
9a74af21 | 35 | |
9a74af21 | 36 | #include "xdr3.h" |
9a74af21 | 37 | |
5be196e5 | 38 | #ifdef CONFIG_NFSD_V4 |
ffa0160a | 39 | #include "../internal.h" |
2ca72e17 BF |
40 | #include "acl.h" |
41 | #include "idmap.h" | |
a2f4c3fa | 42 | #include "xdr4.h" |
1da177e4 LT |
43 | #endif /* CONFIG_NFSD_V4 */ |
44 | ||
9a74af21 BH |
45 | #include "nfsd.h" |
46 | #include "vfs.h" | |
b4935239 | 47 | #include "filecache.h" |
6e8b50d1 | 48 | #include "trace.h" |
1da177e4 LT |
49 | |
50 | #define NFSDDBG_FACILITY NFSDDBG_FILEOP | |
1da177e4 | 51 | |
cb12fae1 JL |
52 | /** |
53 | * nfserrno - Map Linux errnos to NFS errnos | |
54 | * @errno: POSIX(-ish) error code to be mapped | |
55 | * | |
56 | * Returns the appropriate (net-endian) nfserr_* (or nfs_ok if errno is 0). If | |
57 | * it's an error we don't expect, log it once and return nfserr_io. | |
58 | */ | |
59 | __be32 | |
60 | nfserrno (int errno) | |
61 | { | |
62 | static struct { | |
63 | __be32 nfserr; | |
64 | int syserr; | |
65 | } nfs_errtbl[] = { | |
66 | { nfs_ok, 0 }, | |
67 | { nfserr_perm, -EPERM }, | |
68 | { nfserr_noent, -ENOENT }, | |
69 | { nfserr_io, -EIO }, | |
70 | { nfserr_nxio, -ENXIO }, | |
71 | { nfserr_fbig, -E2BIG }, | |
72 | { nfserr_stale, -EBADF }, | |
73 | { nfserr_acces, -EACCES }, | |
74 | { nfserr_exist, -EEXIST }, | |
75 | { nfserr_xdev, -EXDEV }, | |
76 | { nfserr_mlink, -EMLINK }, | |
77 | { nfserr_nodev, -ENODEV }, | |
78 | { nfserr_notdir, -ENOTDIR }, | |
79 | { nfserr_isdir, -EISDIR }, | |
80 | { nfserr_inval, -EINVAL }, | |
81 | { nfserr_fbig, -EFBIG }, | |
82 | { nfserr_nospc, -ENOSPC }, | |
83 | { nfserr_rofs, -EROFS }, | |
84 | { nfserr_mlink, -EMLINK }, | |
85 | { nfserr_nametoolong, -ENAMETOOLONG }, | |
86 | { nfserr_notempty, -ENOTEMPTY }, | |
87 | { nfserr_dquot, -EDQUOT }, | |
88 | { nfserr_stale, -ESTALE }, | |
89 | { nfserr_jukebox, -ETIMEDOUT }, | |
90 | { nfserr_jukebox, -ERESTARTSYS }, | |
91 | { nfserr_jukebox, -EAGAIN }, | |
92 | { nfserr_jukebox, -EWOULDBLOCK }, | |
93 | { nfserr_jukebox, -ENOMEM }, | |
94 | { nfserr_io, -ETXTBSY }, | |
95 | { nfserr_notsupp, -EOPNOTSUPP }, | |
96 | { nfserr_toosmall, -ETOOSMALL }, | |
97 | { nfserr_serverfault, -ESERVERFAULT }, | |
98 | { nfserr_serverfault, -ENFILE }, | |
99 | { nfserr_io, -EREMOTEIO }, | |
100 | { nfserr_stale, -EOPENSTALE }, | |
101 | { nfserr_io, -EUCLEAN }, | |
102 | { nfserr_perm, -ENOKEY }, | |
103 | { nfserr_no_grace, -ENOGRACE}, | |
104 | }; | |
105 | int i; | |
106 | ||
107 | for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) { | |
108 | if (nfs_errtbl[i].syserr == errno) | |
109 | return nfs_errtbl[i].nfserr; | |
110 | } | |
111 | WARN_ONCE(1, "nfsd: non-standard errno: %d\n", errno); | |
112 | return nfserr_io; | |
113 | } | |
114 | ||
1da177e4 LT |
115 | /* |
116 | * Called from nfsd_lookup and encode_dirent. Check if we have crossed | |
117 | * a mount point. | |
e0bb89ef | 118 | * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged, |
1da177e4 LT |
119 | * or nfs_ok having possibly changed *dpp and *expp |
120 | */ | |
121 | int | |
122 | nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp, | |
123 | struct svc_export **expp) | |
124 | { | |
125 | struct svc_export *exp = *expp, *exp2 = NULL; | |
126 | struct dentry *dentry = *dpp; | |
91c9fa8f AV |
127 | struct path path = {.mnt = mntget(exp->ex_path.mnt), |
128 | .dentry = dget(dentry)}; | |
6264d69d | 129 | int err = 0; |
1da177e4 | 130 | |
7cc90cc3 | 131 | err = follow_down(&path); |
cc53ce53 DH |
132 | if (err < 0) |
133 | goto out; | |
99bbf6ec N |
134 | if (path.mnt == exp->ex_path.mnt && path.dentry == dentry && |
135 | nfsd_mountpoint(dentry, exp) == 2) { | |
136 | /* This is only a mountpoint in some other namespace */ | |
137 | path_put(&path); | |
138 | goto out; | |
139 | } | |
1da177e4 | 140 | |
91c9fa8f | 141 | exp2 = rqst_exp_get_by_name(rqstp, &path); |
1da177e4 | 142 | if (IS_ERR(exp2)) { |
3b6cee7b BF |
143 | err = PTR_ERR(exp2); |
144 | /* | |
145 | * We normally allow NFS clients to continue | |
146 | * "underneath" a mountpoint that is not exported. | |
147 | * The exception is V4ROOT, where no traversal is ever | |
148 | * allowed without an explicit export of the new | |
149 | * directory. | |
150 | */ | |
151 | if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT)) | |
152 | err = 0; | |
91c9fa8f | 153 | path_put(&path); |
1da177e4 LT |
154 | goto out; |
155 | } | |
3c394dda SD |
156 | if (nfsd_v4client(rqstp) || |
157 | (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) { | |
1da177e4 | 158 | /* successfully crossed mount point */ |
1644ccc8 | 159 | /* |
91c9fa8f AV |
160 | * This is subtle: path.dentry is *not* on path.mnt |
161 | * at this point. The only reason we are safe is that | |
162 | * original mnt is pinned down by exp, so we should | |
163 | * put path *before* putting exp | |
1644ccc8 | 164 | */ |
91c9fa8f AV |
165 | *dpp = path.dentry; |
166 | path.dentry = dentry; | |
1644ccc8 | 167 | *expp = exp2; |
91c9fa8f | 168 | exp2 = exp; |
1da177e4 | 169 | } |
91c9fa8f AV |
170 | path_put(&path); |
171 | exp_put(exp2); | |
1da177e4 LT |
172 | out: |
173 | return err; | |
174 | } | |
175 | ||
289ede45 BF |
176 | static void follow_to_parent(struct path *path) |
177 | { | |
178 | struct dentry *dp; | |
179 | ||
180 | while (path->dentry == path->mnt->mnt_root && follow_up(path)) | |
181 | ; | |
182 | dp = dget_parent(path->dentry); | |
183 | dput(path->dentry); | |
184 | path->dentry = dp; | |
185 | } | |
186 | ||
187 | static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp) | |
188 | { | |
189 | struct svc_export *exp2; | |
190 | struct path path = {.mnt = mntget((*exp)->ex_path.mnt), | |
191 | .dentry = dget(dparent)}; | |
192 | ||
193 | follow_to_parent(&path); | |
194 | ||
195 | exp2 = rqst_exp_parent(rqstp, &path); | |
196 | if (PTR_ERR(exp2) == -ENOENT) { | |
197 | *dentryp = dget(dparent); | |
198 | } else if (IS_ERR(exp2)) { | |
199 | path_put(&path); | |
200 | return PTR_ERR(exp2); | |
201 | } else { | |
202 | *dentryp = dget(path.dentry); | |
203 | exp_put(*exp); | |
204 | *exp = exp2; | |
205 | } | |
206 | path_put(&path); | |
207 | return 0; | |
208 | } | |
209 | ||
82ead7fe BF |
210 | /* |
211 | * For nfsd purposes, we treat V4ROOT exports as though there was an | |
212 | * export at *every* directory. | |
99bbf6ec N |
213 | * We return: |
214 | * '1' if this dentry *must* be an export point, | |
215 | * '2' if it might be, if there is really a mount here, and | |
216 | * '0' if there is no chance of an export point here. | |
82ead7fe | 217 | */ |
3227fa41 | 218 | int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp) |
82ead7fe | 219 | { |
99bbf6ec N |
220 | if (!d_inode(dentry)) |
221 | return 0; | |
222 | if (exp->ex_flags & NFSEXP_V4ROOT) | |
82ead7fe | 223 | return 1; |
11fcee02 TM |
224 | if (nfsd4_is_junction(dentry)) |
225 | return 1; | |
99bbf6ec N |
226 | if (d_mountpoint(dentry)) |
227 | /* | |
228 | * Might only be a mountpoint in a different namespace, | |
229 | * but we need to check. | |
230 | */ | |
231 | return 2; | |
232 | return 0; | |
82ead7fe BF |
233 | } |
234 | ||
6264d69d | 235 | __be32 |
6c0a654d | 236 | nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp, |
5a022fc8 | 237 | const char *name, unsigned int len, |
6c0a654d | 238 | struct svc_export **exp_ret, struct dentry **dentry_ret) |
1da177e4 LT |
239 | { |
240 | struct svc_export *exp; | |
241 | struct dentry *dparent; | |
242 | struct dentry *dentry; | |
6264d69d | 243 | int host_err; |
1da177e4 LT |
244 | |
245 | dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name); | |
246 | ||
1da177e4 | 247 | dparent = fhp->fh_dentry; |
bf18f163 | 248 | exp = exp_get(fhp->fh_export); |
1da177e4 | 249 | |
1da177e4 LT |
250 | /* Lookup the name, but don't follow links */ |
251 | if (isdotent(name, len)) { | |
252 | if (len==1) | |
253 | dentry = dget(dparent); | |
54775491 | 254 | else if (dparent != exp->ex_path.dentry) |
1da177e4 | 255 | dentry = dget_parent(dparent); |
fed83811 | 256 | else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp)) |
1da177e4 LT |
257 | dentry = dget(dparent); /* .. == . just like at / */ |
258 | else { | |
259 | /* checking mountpoint crossing is very different when stepping up */ | |
289ede45 BF |
260 | host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry); |
261 | if (host_err) | |
1da177e4 | 262 | goto out_nfserr; |
1da177e4 LT |
263 | } |
264 | } else { | |
19d008b4 | 265 | dentry = lookup_one_len_unlocked(name, dparent, len); |
6264d69d | 266 | host_err = PTR_ERR(dentry); |
1da177e4 LT |
267 | if (IS_ERR(dentry)) |
268 | goto out_nfserr; | |
82ead7fe | 269 | if (nfsd_mountpoint(dentry, exp)) { |
19d008b4 N |
270 | host_err = nfsd_cross_mnt(rqstp, &dentry, &exp); |
271 | if (host_err) { | |
1da177e4 LT |
272 | dput(dentry); |
273 | goto out_nfserr; | |
274 | } | |
275 | } | |
276 | } | |
6c0a654d BF |
277 | *dentry_ret = dentry; |
278 | *exp_ret = exp; | |
279 | return 0; | |
280 | ||
281 | out_nfserr: | |
282 | exp_put(exp); | |
283 | return nfserrno(host_err); | |
284 | } | |
285 | ||
19d008b4 N |
286 | /** |
287 | * nfsd_lookup - look up a single path component for nfsd | |
288 | * | |
289 | * @rqstp: the request context | |
290 | * @fhp: the file handle of the directory | |
291 | * @name: the component name, or %NULL to look up parent | |
292 | * @len: length of name to examine | |
293 | * @resfh: pointer to pre-initialised filehandle to hold result. | |
294 | * | |
6c0a654d BF |
295 | * Look up one component of a pathname. |
296 | * N.B. After this call _both_ fhp and resfh need an fh_put | |
297 | * | |
298 | * If the lookup would cross a mountpoint, and the mounted filesystem | |
299 | * is exported to the client with NFSEXP_NOHIDE, then the lookup is | |
300 | * accepted as it stands and the mounted directory is | |
301 | * returned. Otherwise the covered directory is returned. | |
302 | * NOTE: this mountpoint crossing is not supported properly by all | |
303 | * clients and is explicitly disallowed for NFSv3 | |
19d008b4 | 304 | * |
6c0a654d BF |
305 | */ |
306 | __be32 | |
307 | nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name, | |
19d008b4 | 308 | unsigned int len, struct svc_fh *resfh) |
6c0a654d BF |
309 | { |
310 | struct svc_export *exp; | |
311 | struct dentry *dentry; | |
312 | __be32 err; | |
313 | ||
29a78a3e BF |
314 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC); |
315 | if (err) | |
316 | return err; | |
6c0a654d BF |
317 | err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry); |
318 | if (err) | |
319 | return err; | |
32c1eb0c AA |
320 | err = check_nfsd_access(exp, rqstp); |
321 | if (err) | |
322 | goto out; | |
1da177e4 LT |
323 | /* |
324 | * Note: we compose the file handle now, but as the | |
325 | * dentry may be negative, it may need to be updated. | |
326 | */ | |
327 | err = fh_compose(resfh, exp, dentry, fhp); | |
2b0143b5 | 328 | if (!err && d_really_is_negative(dentry)) |
1da177e4 | 329 | err = nfserr_noent; |
32c1eb0c | 330 | out: |
1da177e4 | 331 | dput(dentry); |
1da177e4 LT |
332 | exp_put(exp); |
333 | return err; | |
1da177e4 LT |
334 | } |
335 | ||
f501912a BM |
336 | /* |
337 | * Commit metadata changes to stable storage. | |
338 | */ | |
339 | static int | |
57f64034 | 340 | commit_inode_metadata(struct inode *inode) |
f501912a | 341 | { |
f501912a | 342 | const struct export_operations *export_ops = inode->i_sb->s_export_op; |
f501912a | 343 | |
c3765016 CH |
344 | if (export_ops->commit_metadata) |
345 | return export_ops->commit_metadata(inode); | |
346 | return sync_inode_metadata(inode, 1); | |
f501912a | 347 | } |
6c0a654d | 348 | |
57f64034 TM |
349 | static int |
350 | commit_metadata(struct svc_fh *fhp) | |
351 | { | |
352 | struct inode *inode = d_inode(fhp->fh_dentry); | |
353 | ||
354 | if (!EX_ISSYNC(fhp->fh_export)) | |
355 | return 0; | |
356 | return commit_inode_metadata(inode); | |
357 | } | |
358 | ||
1da177e4 | 359 | /* |
818e5a22 CH |
360 | * Go over the attributes and take care of the small differences between |
361 | * NFS semantics and what Linux expects. | |
1da177e4 | 362 | */ |
818e5a22 CH |
363 | static void |
364 | nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap) | |
1da177e4 | 365 | { |
00801cd9 N |
366 | /* Ignore mode updates on symlinks */ |
367 | if (S_ISLNK(inode->i_mode)) | |
368 | iap->ia_valid &= ~ATTR_MODE; | |
369 | ||
ca456252 | 370 | /* sanitize the mode change */ |
1da177e4 LT |
371 | if (iap->ia_valid & ATTR_MODE) { |
372 | iap->ia_mode &= S_IALLUGO; | |
dee3209d | 373 | iap->ia_mode |= (inode->i_mode & ~S_IALLUGO); |
ca456252 JL |
374 | } |
375 | ||
376 | /* Revoke setuid/setgid on chown */ | |
0953e620 | 377 | if (!S_ISDIR(inode->i_mode) && |
c4fa6d7c | 378 | ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) { |
ca456252 JL |
379 | iap->ia_valid |= ATTR_KILL_PRIV; |
380 | if (iap->ia_valid & ATTR_MODE) { | |
381 | /* we're setting mode too, just clear the s*id bits */ | |
8a0ce7d9 | 382 | iap->ia_mode &= ~S_ISUID; |
ca456252 JL |
383 | if (iap->ia_mode & S_IXGRP) |
384 | iap->ia_mode &= ~S_ISGID; | |
385 | } else { | |
386 | /* set ATTR_KILL_* bits and let VFS handle it */ | |
244640ce CB |
387 | iap->ia_valid |= ATTR_KILL_SUID; |
388 | iap->ia_valid |= | |
389 | setattr_should_drop_sgid(&init_user_ns, inode); | |
8a0ce7d9 | 390 | } |
1da177e4 | 391 | } |
818e5a22 CH |
392 | } |
393 | ||
0839ffb8 BF |
394 | static __be32 |
395 | nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp, | |
396 | struct iattr *iap) | |
397 | { | |
398 | struct inode *inode = d_inode(fhp->fh_dentry); | |
0839ffb8 BF |
399 | |
400 | if (iap->ia_size < inode->i_size) { | |
401 | __be32 err; | |
402 | ||
403 | err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, | |
404 | NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE); | |
405 | if (err) | |
406 | return err; | |
407 | } | |
f7e33bdb | 408 | return nfserrno(get_write_access(inode)); |
0839ffb8 BF |
409 | } |
410 | ||
c0aa1913 CL |
411 | static int __nfsd_setattr(struct dentry *dentry, struct iattr *iap) |
412 | { | |
413 | int host_err; | |
414 | ||
415 | if (iap->ia_valid & ATTR_SIZE) { | |
416 | /* | |
417 | * RFC5661, Section 18.30.4: | |
418 | * Changing the size of a file with SETATTR indirectly | |
419 | * changes the time_modify and change attributes. | |
420 | * | |
421 | * (and similar for the older RFCs) | |
422 | */ | |
423 | struct iattr size_attr = { | |
424 | .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME, | |
425 | .ia_size = iap->ia_size, | |
426 | }; | |
427 | ||
428 | if (iap->ia_size < 0) | |
429 | return -EFBIG; | |
430 | ||
431 | host_err = notify_change(&init_user_ns, dentry, &size_attr, NULL); | |
432 | if (host_err) | |
433 | return host_err; | |
434 | iap->ia_valid &= ~ATTR_SIZE; | |
435 | ||
436 | /* | |
437 | * Avoid the additional setattr call below if the only other | |
438 | * attribute that the client sends is the mtime, as we update | |
439 | * it as part of the size change above. | |
440 | */ | |
441 | if ((iap->ia_valid & ~ATTR_MTIME) == 0) | |
442 | return 0; | |
443 | } | |
444 | ||
445 | if (!iap->ia_valid) | |
446 | return 0; | |
447 | ||
448 | iap->ia_valid |= ATTR_CTIME; | |
449 | return notify_change(&init_user_ns, dentry, iap, NULL); | |
450 | } | |
451 | ||
452 | /** | |
453 | * nfsd_setattr - Set various file attributes. | |
454 | * @rqstp: controlling RPC transaction | |
455 | * @fhp: filehandle of target | |
456 | * @attr: attributes to set | |
457 | * @check_guard: set to 1 if guardtime is a valid timestamp | |
458 | * @guardtime: do not act if ctime.tv_sec does not match this timestamp | |
459 | * | |
460 | * This call may adjust the contents of @attr (in particular, this | |
461 | * call may change the bits in the na_iattr.ia_valid field). | |
462 | * | |
463 | * Returns nfs_ok on success, otherwise an NFS status code is | |
464 | * returned. Caller must release @fhp by calling fh_put in either | |
465 | * case. | |
818e5a22 CH |
466 | */ |
467 | __be32 | |
7fe2a71d N |
468 | nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, |
469 | struct nfsd_attrs *attr, | |
2a1aa489 | 470 | int check_guard, time64_t guardtime) |
818e5a22 CH |
471 | { |
472 | struct dentry *dentry; | |
473 | struct inode *inode; | |
7fe2a71d | 474 | struct iattr *iap = attr->na_iattr; |
818e5a22 CH |
475 | int accmode = NFSD_MAY_SATTR; |
476 | umode_t ftype = 0; | |
477 | __be32 err; | |
c0aa1913 | 478 | int host_err; |
9f67f189 | 479 | bool get_write_count; |
758e99fe | 480 | bool size_change = (iap->ia_valid & ATTR_SIZE); |
34b91dda | 481 | int retries; |
818e5a22 | 482 | |
255fbca6 | 483 | if (iap->ia_valid & ATTR_SIZE) { |
818e5a22 | 484 | accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE; |
818e5a22 | 485 | ftype = S_IFREG; |
255fbca6 | 486 | } |
487 | ||
488 | /* | |
489 | * If utimes(2) and friends are called with times not NULL, we should | |
490 | * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission | |
e977cc83 | 491 | * will return EACCES, when the caller's effective UID does not match |
255fbca6 | 492 | * the owner of the file, and the caller is not privileged. In this |
493 | * situation, we should return EPERM(notify_change will return this). | |
494 | */ | |
495 | if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) { | |
496 | accmode |= NFSD_MAY_OWNER_OVERRIDE; | |
497 | if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET))) | |
498 | accmode |= NFSD_MAY_WRITE; | |
499 | } | |
818e5a22 | 500 | |
9f67f189 BF |
501 | /* Callers that do fh_verify should do the fh_want_write: */ |
502 | get_write_count = !fhp->fh_dentry; | |
503 | ||
818e5a22 CH |
504 | /* Get inode */ |
505 | err = fh_verify(rqstp, fhp, ftype, accmode); | |
506 | if (err) | |
758e99fe | 507 | return err; |
9f67f189 BF |
508 | if (get_write_count) { |
509 | host_err = fh_want_write(fhp); | |
510 | if (host_err) | |
758e99fe | 511 | goto out; |
9f67f189 | 512 | } |
818e5a22 CH |
513 | |
514 | dentry = fhp->fh_dentry; | |
2b0143b5 | 515 | inode = d_inode(dentry); |
818e5a22 | 516 | |
818e5a22 CH |
517 | nfsd_sanitize_attrs(inode, iap); |
518 | ||
758e99fe CH |
519 | if (check_guard && guardtime != inode->i_ctime.tv_sec) |
520 | return nfserr_notsync; | |
521 | ||
818e5a22 CH |
522 | /* |
523 | * The size case is special, it changes the file in addition to the | |
783112f7 CH |
524 | * attributes, and file systems don't expect it to be mixed with |
525 | * "random" attribute changes. We thus split out the size change | |
526 | * into a separate call to ->setattr, and do the rest as a separate | |
527 | * setattr call. | |
818e5a22 | 528 | */ |
758e99fe | 529 | if (size_change) { |
0839ffb8 BF |
530 | err = nfsd_get_write_access(rqstp, fhp, iap); |
531 | if (err) | |
758e99fe | 532 | return err; |
783112f7 | 533 | } |
f0c63124 | 534 | |
bb4d53d6 | 535 | inode_lock(inode); |
34b91dda | 536 | for (retries = 1;;) { |
f029f286 JL |
537 | struct iattr attrs; |
538 | ||
539 | /* | |
540 | * notify_change() can alter its iattr argument, making | |
541 | * @iap unsuitable for submission multiple times. Make a | |
542 | * copy for every loop iteration. | |
543 | */ | |
544 | attrs = *iap; | |
545 | host_err = __nfsd_setattr(dentry, &attrs); | |
34b91dda CL |
546 | if (host_err != -EAGAIN || !retries--) |
547 | break; | |
548 | if (!nfsd_wait_for_delegreturn(rqstp, inode)) | |
549 | break; | |
550 | } | |
d6a97d3f N |
551 | if (attr->na_seclabel && attr->na_seclabel->len) |
552 | attr->na_labelerr = security_inode_setsecctx(dentry, | |
553 | attr->na_seclabel->data, attr->na_seclabel->len); | |
c0cbe707 N |
554 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL) && attr->na_pacl) |
555 | attr->na_aclerr = set_posix_acl(&init_user_ns, | |
138060ba | 556 | dentry, ACL_TYPE_ACCESS, |
c0cbe707 N |
557 | attr->na_pacl); |
558 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL) && | |
559 | !attr->na_aclerr && attr->na_dpacl && S_ISDIR(inode->i_mode)) | |
560 | attr->na_aclerr = set_posix_acl(&init_user_ns, | |
138060ba | 561 | dentry, ACL_TYPE_DEFAULT, |
c0cbe707 | 562 | attr->na_dpacl); |
bb4d53d6 | 563 | inode_unlock(inode); |
0839ffb8 BF |
564 | if (size_change) |
565 | put_write_access(inode); | |
0839ffb8 | 566 | out: |
758e99fe CH |
567 | if (!host_err) |
568 | host_err = commit_metadata(fhp); | |
569 | return nfserrno(host_err); | |
1da177e4 LT |
570 | } |
571 | ||
5be196e5 | 572 | #if defined(CONFIG_NFSD_V4) |
9b4146e8 CL |
573 | /* |
574 | * NFS junction information is stored in an extended attribute. | |
575 | */ | |
576 | #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs" | |
577 | ||
578 | /** | |
579 | * nfsd4_is_junction - Test if an object could be an NFS junction | |
580 | * | |
581 | * @dentry: object to test | |
582 | * | |
583 | * Returns 1 if "dentry" appears to contain NFS junction information. | |
584 | * Otherwise 0 is returned. | |
585 | */ | |
11fcee02 TM |
586 | int nfsd4_is_junction(struct dentry *dentry) |
587 | { | |
2b0143b5 | 588 | struct inode *inode = d_inode(dentry); |
11fcee02 TM |
589 | |
590 | if (inode == NULL) | |
591 | return 0; | |
592 | if (inode->i_mode & S_IXUGO) | |
593 | return 0; | |
594 | if (!(inode->i_mode & S_ISVTX)) | |
595 | return 0; | |
c7c7a1a1 TA |
596 | if (vfs_getxattr(&init_user_ns, dentry, NFSD_JUNCTION_XATTR_NAME, |
597 | NULL, 0) <= 0) | |
11fcee02 TM |
598 | return 0; |
599 | return 1; | |
600 | } | |
18032ca0 | 601 | |
a2f4c3fa TM |
602 | static struct nfsd4_compound_state *nfsd4_get_cstate(struct svc_rqst *rqstp) |
603 | { | |
604 | return &((struct nfsd4_compoundres *)rqstp->rq_resp)->cstate; | |
605 | } | |
606 | ||
607 | __be32 nfsd4_clone_file_range(struct svc_rqst *rqstp, | |
608 | struct nfsd_file *nf_src, u64 src_pos, | |
609 | struct nfsd_file *nf_dst, u64 dst_pos, | |
610 | u64 count, bool sync) | |
ffa0160a | 611 | { |
b66ae6dd TM |
612 | struct file *src = nf_src->nf_file; |
613 | struct file *dst = nf_dst->nf_file; | |
555dbf1a | 614 | errseq_t since; |
42ec3d4c | 615 | loff_t cloned; |
1b28d756 | 616 | __be32 ret = 0; |
42ec3d4c | 617 | |
555dbf1a | 618 | since = READ_ONCE(dst->f_wb_err); |
452ce659 | 619 | cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0); |
1b28d756 TM |
620 | if (cloned < 0) { |
621 | ret = nfserrno(cloned); | |
622 | goto out_err; | |
623 | } | |
624 | if (count && cloned != count) { | |
625 | ret = nfserrno(-EINVAL); | |
626 | goto out_err; | |
627 | } | |
a25e3726 TM |
628 | if (sync) { |
629 | loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX; | |
630 | int status = vfs_fsync_range(dst, dst_pos, dst_end, 0); | |
57f64034 | 631 | |
555dbf1a TM |
632 | if (!status) |
633 | status = filemap_check_wb_err(dst->f_mapping, since); | |
57f64034 TM |
634 | if (!status) |
635 | status = commit_inode_metadata(file_inode(src)); | |
1b28d756 | 636 | if (status < 0) { |
75acacb6 CL |
637 | struct nfsd_net *nn = net_generic(nf_dst->nf_net, |
638 | nfsd_net_id); | |
639 | ||
a2f4c3fa TM |
640 | trace_nfsd_clone_file_range_err(rqstp, |
641 | &nfsd4_get_cstate(rqstp)->save_fh, | |
642 | src_pos, | |
643 | &nfsd4_get_cstate(rqstp)->current_fh, | |
644 | dst_pos, | |
645 | count, status); | |
75acacb6 CL |
646 | nfsd_reset_write_verifier(nn); |
647 | trace_nfsd_writeverf_reset(nn, rqstp, status); | |
1b28d756 TM |
648 | ret = nfserrno(status); |
649 | } | |
a25e3726 | 650 | } |
1b28d756 | 651 | out_err: |
1b28d756 | 652 | return ret; |
ffa0160a CH |
653 | } |
654 | ||
29ae7f9d AS |
655 | ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst, |
656 | u64 dst_pos, u64 count) | |
657 | { | |
868f9f2f | 658 | ssize_t ret; |
29ae7f9d AS |
659 | |
660 | /* | |
661 | * Limit copy to 4MB to prevent indefinitely blocking an nfsd | |
662 | * thread and client rpc slot. The choice of 4MB is somewhat | |
663 | * arbitrary. We might instead base this on r/wsize, or make it | |
664 | * tunable, or use a time instead of a byte limit, or implement | |
665 | * asynchronous copy. In theory a client could also recognize a | |
666 | * limit like this and pipeline multiple COPY requests. | |
667 | */ | |
668 | count = min_t(u64, count, 1 << 22); | |
868f9f2f AG |
669 | ret = vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0); |
670 | ||
671 | if (ret == -EOPNOTSUPP || ret == -EXDEV) | |
10bc8e4a AG |
672 | ret = vfs_copy_file_range(src, src_pos, dst, dst_pos, count, |
673 | COPY_FILE_SPLICE); | |
868f9f2f | 674 | return ret; |
29ae7f9d AS |
675 | } |
676 | ||
95d871f0 AS |
677 | __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp, |
678 | struct file *file, loff_t offset, loff_t len, | |
679 | int flags) | |
680 | { | |
95d871f0 AS |
681 | int error; |
682 | ||
683 | if (!S_ISREG(file_inode(file)->i_mode)) | |
684 | return nfserr_inval; | |
685 | ||
95d871f0 AS |
686 | error = vfs_fallocate(file, flags, offset, len); |
687 | if (!error) | |
688 | error = commit_metadata(fhp); | |
689 | ||
690 | return nfserrno(error); | |
691 | } | |
6a85d6c7 | 692 | #endif /* defined(CONFIG_NFSD_V4) */ |
1da177e4 | 693 | |
1da177e4 LT |
694 | /* |
695 | * Check server access rights to a file system object | |
696 | */ | |
697 | struct accessmap { | |
698 | u32 access; | |
699 | int how; | |
700 | }; | |
701 | static struct accessmap nfs3_regaccess[] = { | |
8837abca MS |
702 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
703 | { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC }, | |
704 | { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC }, | |
705 | { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE }, | |
1da177e4 | 706 | |
c11d7fd1 FL |
707 | #ifdef CONFIG_NFSD_V4 |
708 | { NFS4_ACCESS_XAREAD, NFSD_MAY_READ }, | |
709 | { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE }, | |
710 | { NFS4_ACCESS_XALIST, NFSD_MAY_READ }, | |
711 | #endif | |
712 | ||
1da177e4 LT |
713 | { 0, 0 } |
714 | }; | |
715 | ||
716 | static struct accessmap nfs3_diraccess[] = { | |
8837abca MS |
717 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
718 | { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC }, | |
719 | { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC}, | |
720 | { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE }, | |
721 | { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE }, | |
1da177e4 | 722 | |
c11d7fd1 FL |
723 | #ifdef CONFIG_NFSD_V4 |
724 | { NFS4_ACCESS_XAREAD, NFSD_MAY_READ }, | |
725 | { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE }, | |
726 | { NFS4_ACCESS_XALIST, NFSD_MAY_READ }, | |
727 | #endif | |
728 | ||
1da177e4 LT |
729 | { 0, 0 } |
730 | }; | |
731 | ||
732 | static struct accessmap nfs3_anyaccess[] = { | |
733 | /* Some clients - Solaris 2.6 at least, make an access call | |
734 | * to the server to check for access for things like /dev/null | |
735 | * (which really, the server doesn't care about). So | |
736 | * We provide simple access checking for them, looking | |
737 | * mainly at mode bits, and we make sure to ignore read-only | |
738 | * filesystem checks | |
739 | */ | |
8837abca MS |
740 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
741 | { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC }, | |
742 | { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS }, | |
743 | { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS }, | |
1da177e4 LT |
744 | |
745 | { 0, 0 } | |
746 | }; | |
747 | ||
6264d69d | 748 | __be32 |
1da177e4 LT |
749 | nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported) |
750 | { | |
751 | struct accessmap *map; | |
752 | struct svc_export *export; | |
753 | struct dentry *dentry; | |
754 | u32 query, result = 0, sresult = 0; | |
6264d69d | 755 | __be32 error; |
1da177e4 | 756 | |
8837abca | 757 | error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP); |
1da177e4 LT |
758 | if (error) |
759 | goto out; | |
760 | ||
761 | export = fhp->fh_export; | |
762 | dentry = fhp->fh_dentry; | |
763 | ||
e36cb0b8 | 764 | if (d_is_reg(dentry)) |
1da177e4 | 765 | map = nfs3_regaccess; |
e36cb0b8 | 766 | else if (d_is_dir(dentry)) |
1da177e4 LT |
767 | map = nfs3_diraccess; |
768 | else | |
769 | map = nfs3_anyaccess; | |
770 | ||
771 | ||
772 | query = *access; | |
773 | for (; map->access; map++) { | |
774 | if (map->access & query) { | |
6264d69d | 775 | __be32 err2; |
1da177e4 LT |
776 | |
777 | sresult |= map->access; | |
778 | ||
0ec757df | 779 | err2 = nfsd_permission(rqstp, export, dentry, map->how); |
1da177e4 LT |
780 | switch (err2) { |
781 | case nfs_ok: | |
782 | result |= map->access; | |
783 | break; | |
784 | ||
785 | /* the following error codes just mean the access was not allowed, | |
786 | * rather than an error occurred */ | |
787 | case nfserr_rofs: | |
788 | case nfserr_acces: | |
789 | case nfserr_perm: | |
790 | /* simply don't "or" in the access bit. */ | |
791 | break; | |
792 | default: | |
793 | error = err2; | |
794 | goto out; | |
795 | } | |
796 | } | |
797 | } | |
798 | *access = result; | |
799 | if (supported) | |
800 | *supported = sresult; | |
801 | ||
802 | out: | |
803 | return error; | |
804 | } | |
1da177e4 | 805 | |
65294c1f | 806 | int nfsd_open_break_lease(struct inode *inode, int access) |
105f4622 BF |
807 | { |
808 | unsigned int mode; | |
1da177e4 | 809 | |
105f4622 BF |
810 | if (access & NFSD_MAY_NOT_BREAK_LEASE) |
811 | return 0; | |
812 | mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY; | |
813 | return break_lease(inode, mode | O_NONBLOCK); | |
814 | } | |
1da177e4 LT |
815 | |
816 | /* | |
817 | * Open an existing file or directory. | |
999448a8 BS |
818 | * The may_flags argument indicates the type of open (read/write/lock) |
819 | * and additional flags. | |
1da177e4 LT |
820 | * N.B. After this call fhp needs an fh_put |
821 | */ | |
65294c1f JL |
822 | static __be32 |
823 | __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, | |
999448a8 | 824 | int may_flags, struct file **filp) |
1da177e4 | 825 | { |
765927b2 | 826 | struct path path; |
1da177e4 | 827 | struct inode *inode; |
8519f994 | 828 | struct file *file; |
6264d69d AV |
829 | int flags = O_RDONLY|O_LARGEFILE; |
830 | __be32 err; | |
91885258 | 831 | int host_err = 0; |
1da177e4 | 832 | |
765927b2 AV |
833 | path.mnt = fhp->fh_export->ex_path.mnt; |
834 | path.dentry = fhp->fh_dentry; | |
2b0143b5 | 835 | inode = d_inode(path.dentry); |
1da177e4 | 836 | |
1da177e4 | 837 | err = nfserr_perm; |
999448a8 | 838 | if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE)) |
1da177e4 | 839 | goto out; |
1da177e4 LT |
840 | |
841 | if (!inode->i_fop) | |
842 | goto out; | |
843 | ||
999448a8 | 844 | host_err = nfsd_open_break_lease(inode, may_flags); |
6264d69d | 845 | if (host_err) /* NOMEM or WOULDBLOCK */ |
1da177e4 LT |
846 | goto out_nfserr; |
847 | ||
999448a8 BS |
848 | if (may_flags & NFSD_MAY_WRITE) { |
849 | if (may_flags & NFSD_MAY_READ) | |
9ecb6a08 BF |
850 | flags = O_RDWR|O_LARGEFILE; |
851 | else | |
852 | flags = O_WRONLY|O_LARGEFILE; | |
1da177e4 | 853 | } |
999448a8 | 854 | |
8519f994 KM |
855 | file = dentry_open(&path, flags, current_cred()); |
856 | if (IS_ERR(file)) { | |
857 | host_err = PTR_ERR(file); | |
858 | goto out_nfserr; | |
859 | } | |
860 | ||
6035a27b | 861 | host_err = ima_file_check(file, may_flags); |
8519f994 | 862 | if (host_err) { |
fd891454 | 863 | fput(file); |
8519f994 | 864 | goto out_nfserr; |
06effdbb BS |
865 | } |
866 | ||
8519f994 KM |
867 | if (may_flags & NFSD_MAY_64BIT_COOKIE) |
868 | file->f_mode |= FMODE_64BITHASH; | |
869 | else | |
870 | file->f_mode |= FMODE_32BITHASH; | |
871 | ||
872 | *filp = file; | |
1da177e4 | 873 | out_nfserr: |
6264d69d | 874 | err = nfserrno(host_err); |
1da177e4 | 875 | out: |
65294c1f JL |
876 | return err; |
877 | } | |
878 | ||
879 | __be32 | |
880 | nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, | |
881 | int may_flags, struct file **filp) | |
882 | { | |
883 | __be32 err; | |
12bcbd40 | 884 | bool retried = false; |
65294c1f JL |
885 | |
886 | validate_process_creds(); | |
887 | /* | |
888 | * If we get here, then the client has already done an "open", | |
889 | * and (hopefully) checked permission - so allow OWNER_OVERRIDE | |
890 | * in case a chmod has now revoked permission. | |
891 | * | |
892 | * Arguably we should also allow the owner override for | |
893 | * directories, but we never have and it doesn't seem to have | |
894 | * caused anyone a problem. If we were to change this, note | |
895 | * also that our filldir callbacks would need a variant of | |
896 | * lookup_one_len that doesn't check permissions. | |
897 | */ | |
898 | if (type == S_IFREG) | |
899 | may_flags |= NFSD_MAY_OWNER_OVERRIDE; | |
12bcbd40 | 900 | retry: |
65294c1f | 901 | err = fh_verify(rqstp, fhp, type, may_flags); |
12bcbd40 | 902 | if (!err) { |
65294c1f | 903 | err = __nfsd_open(rqstp, fhp, type, may_flags, filp); |
12bcbd40 JL |
904 | if (err == nfserr_stale && !retried) { |
905 | retried = true; | |
906 | fh_put(fhp); | |
907 | goto retry; | |
908 | } | |
909 | } | |
65294c1f JL |
910 | validate_process_creds(); |
911 | return err; | |
912 | } | |
913 | ||
f4d84c52 CL |
914 | /** |
915 | * nfsd_open_verified - Open a regular file for the filecache | |
916 | * @rqstp: RPC request | |
917 | * @fhp: NFS filehandle of the file to open | |
918 | * @may_flags: internal permission flags | |
919 | * @filp: OUT: open "struct file *" | |
920 | * | |
921 | * Returns an nfsstat value in network byte order. | |
922 | */ | |
65294c1f | 923 | __be32 |
f4d84c52 CL |
924 | nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, int may_flags, |
925 | struct file **filp) | |
65294c1f JL |
926 | { |
927 | __be32 err; | |
928 | ||
929 | validate_process_creds(); | |
f4d84c52 | 930 | err = __nfsd_open(rqstp, fhp, S_IFREG, may_flags, filp); |
e0e81739 | 931 | validate_process_creds(); |
1da177e4 LT |
932 | return err; |
933 | } | |
934 | ||
1da177e4 | 935 | /* |
cf8208d0 JA |
936 | * Grab and keep cached pages associated with a file in the svc_rqst |
937 | * so that they can be passed to the network sendmsg/sendpage routines | |
938 | * directly. They will be released after the sending has completed. | |
1da177e4 LT |
939 | */ |
940 | static int | |
cf8208d0 JA |
941 | nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, |
942 | struct splice_desc *sd) | |
1da177e4 | 943 | { |
cf8208d0 | 944 | struct svc_rqst *rqstp = sd->u.data; |
bfbfb618 AV |
945 | struct page *page = buf->page; // may be a compound one |
946 | unsigned offset = buf->offset; | |
ac8db824 | 947 | struct page *last_page; |
bfbfb618 | 948 | |
ac8db824 | 949 | last_page = page + (offset + sd->len - 1) / PAGE_SIZE; |
bf59a6cc JL |
950 | for (page += offset / PAGE_SIZE; page <= last_page; page++) { |
951 | /* | |
952 | * Skip page replacement when extending the contents | |
953 | * of the current page. | |
954 | */ | |
955 | if (page == *(rqstp->rq_next_page - 1)) | |
956 | continue; | |
ac8db824 | 957 | svc_rqst_replace_page(rqstp, page); |
bf59a6cc | 958 | } |
bfbfb618 AV |
959 | if (rqstp->rq_res.page_len == 0) // first call |
960 | rqstp->rq_res.page_base = offset % PAGE_SIZE; | |
c7e0b781 | 961 | rqstp->rq_res.page_len += sd->len; |
c7e0b781 | 962 | return sd->len; |
1da177e4 LT |
963 | } |
964 | ||
cf8208d0 JA |
965 | static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe, |
966 | struct splice_desc *sd) | |
967 | { | |
968 | return __splice_from_pipe(pipe, sd, nfsd_splice_actor); | |
969 | } | |
970 | ||
83a63072 TM |
971 | static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len, |
972 | size_t expected) | |
973 | { | |
974 | if (expected != 0 && len == 0) | |
975 | return 1; | |
976 | if (offset+len >= i_size_read(file_inode(file))) | |
977 | return 1; | |
978 | return 0; | |
979 | } | |
980 | ||
87c5942e CL |
981 | static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp, |
982 | struct file *file, loff_t offset, | |
83a63072 | 983 | unsigned long *count, u32 *eof, ssize_t host_err) |
1da177e4 | 984 | { |
6264d69d | 985 | if (host_err >= 0) { |
20ad856e | 986 | nfsd_stats_io_read_add(fhp->fh_export, host_err); |
83a63072 | 987 | *eof = nfsd_eof_on_read(file, offset, host_err, *count); |
6264d69d | 988 | *count = host_err; |
2a12a9d7 | 989 | fsnotify_access(file); |
87c5942e | 990 | trace_nfsd_read_io_done(rqstp, fhp, offset, *count); |
dc97618d | 991 | return 0; |
87c5942e CL |
992 | } else { |
993 | trace_nfsd_read_err(rqstp, fhp, offset, host_err); | |
dc97618d | 994 | return nfserrno(host_err); |
87c5942e | 995 | } |
dc97618d BF |
996 | } |
997 | ||
87c5942e | 998 | __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp, |
83a63072 TM |
999 | struct file *file, loff_t offset, unsigned long *count, |
1000 | u32 *eof) | |
dc97618d BF |
1001 | { |
1002 | struct splice_desc sd = { | |
1003 | .len = 0, | |
1004 | .total_len = *count, | |
1005 | .pos = offset, | |
1006 | .u.data = rqstp, | |
1007 | }; | |
83a63072 | 1008 | ssize_t host_err; |
dc97618d | 1009 | |
87c5942e | 1010 | trace_nfsd_read_splice(rqstp, fhp, offset, *count); |
dc97618d BF |
1011 | rqstp->rq_next_page = rqstp->rq_respages + 1; |
1012 | host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor); | |
83a63072 | 1013 | return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err); |
dc97618d BF |
1014 | } |
1015 | ||
87c5942e CL |
1016 | __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp, |
1017 | struct file *file, loff_t offset, | |
83a63072 TM |
1018 | struct kvec *vec, int vlen, unsigned long *count, |
1019 | u32 *eof) | |
dc97618d | 1020 | { |
73da852e | 1021 | struct iov_iter iter; |
83a63072 TM |
1022 | loff_t ppos = offset; |
1023 | ssize_t host_err; | |
dc97618d | 1024 | |
87c5942e | 1025 | trace_nfsd_read_vector(rqstp, fhp, offset, *count); |
de4eda9d | 1026 | iov_iter_kvec(&iter, ITER_DEST, vec, vlen, *count); |
83a63072 TM |
1027 | host_err = vfs_iter_read(file, &iter, &ppos, 0); |
1028 | return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err); | |
1da177e4 LT |
1029 | } |
1030 | ||
d911df7b BF |
1031 | /* |
1032 | * Gathered writes: If another process is currently writing to the file, | |
1033 | * there's a high chance this is another nfsd (triggered by a bulk write | |
1034 | * from a client's biod). Rather than syncing the file with each write | |
1035 | * request, we sleep for 10 msec. | |
1036 | * | |
1037 | * I don't know if this roughly approximates C. Juszak's idea of | |
1038 | * gathered writes, but it's a nice and simple solution (IMHO), and it | |
1039 | * seems to work:-) | |
1040 | * | |
1041 | * Note: we do this only in the NFSv2 case, since v3 and higher have a | |
1042 | * better tool (separate unstable writes and commits) for solving this | |
1043 | * problem. | |
1044 | */ | |
1045 | static int wait_for_concurrent_writes(struct file *file) | |
1046 | { | |
496ad9aa | 1047 | struct inode *inode = file_inode(file); |
d911df7b BF |
1048 | static ino_t last_ino; |
1049 | static dev_t last_dev; | |
1050 | int err = 0; | |
1051 | ||
1052 | if (atomic_read(&inode->i_writecount) > 1 | |
1053 | || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) { | |
1054 | dprintk("nfsd: write defer %d\n", task_pid_nr(current)); | |
1055 | msleep(10); | |
1056 | dprintk("nfsd: write resume %d\n", task_pid_nr(current)); | |
1057 | } | |
1058 | ||
1059 | if (inode->i_state & I_DIRTY) { | |
1060 | dprintk("nfsd: write sync %d\n", task_pid_nr(current)); | |
8018ab05 | 1061 | err = vfs_fsync(file, 0); |
d911df7b BF |
1062 | } |
1063 | last_ino = inode->i_ino; | |
1064 | last_dev = inode->i_sb->s_dev; | |
1065 | return err; | |
1066 | } | |
1067 | ||
af90f707 | 1068 | __be32 |
16f8f894 | 1069 | nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf, |
1da177e4 | 1070 | loff_t offset, struct kvec *vec, int vlen, |
19e0663f TM |
1071 | unsigned long *cnt, int stable, |
1072 | __be32 *verf) | |
1da177e4 | 1073 | { |
fb7622c2 | 1074 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
16f8f894 | 1075 | struct file *file = nf->nf_file; |
01cbf385 | 1076 | struct super_block *sb = file_inode(file)->i_sb; |
1da177e4 | 1077 | struct svc_export *exp; |
73da852e | 1078 | struct iov_iter iter; |
555dbf1a | 1079 | errseq_t since; |
d890be15 | 1080 | __be32 nfserr; |
6264d69d | 1081 | int host_err; |
48e03bc5 | 1082 | int use_wgather; |
e49dbbf3 | 1083 | loff_t pos = offset; |
01cbf385 | 1084 | unsigned long exp_op_flags = 0; |
8658452e | 1085 | unsigned int pflags = current->flags; |
ddef7ed2 | 1086 | rwf_t flags = 0; |
01cbf385 | 1087 | bool restore_flags = false; |
8658452e | 1088 | |
d890be15 CL |
1089 | trace_nfsd_write_opened(rqstp, fhp, offset, *cnt); |
1090 | ||
01cbf385 TM |
1091 | if (sb->s_export_op) |
1092 | exp_op_flags = sb->s_export_op->flags; | |
1093 | ||
1094 | if (test_bit(RQ_LOCAL, &rqstp->rq_flags) && | |
1095 | !(exp_op_flags & EXPORT_OP_REMOTE_FS)) { | |
8658452e | 1096 | /* |
a37b0715 N |
1097 | * We want throttling in balance_dirty_pages() |
1098 | * and shrink_inactive_list() to only consider | |
1099 | * the backingdev we are writing to, so that nfs to | |
8658452e N |
1100 | * localhost doesn't cause nfsd to lock up due to all |
1101 | * the client's dirty pages or its congested queue. | |
1102 | */ | |
a37b0715 | 1103 | current->flags |= PF_LOCAL_THROTTLE; |
01cbf385 TM |
1104 | restore_flags = true; |
1105 | } | |
1da177e4 | 1106 | |
865d50b2 | 1107 | exp = fhp->fh_export; |
48e03bc5 | 1108 | use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp); |
1da177e4 | 1109 | |
1da177e4 | 1110 | if (!EX_ISSYNC(exp)) |
54bbb7d2 | 1111 | stable = NFS_UNSTABLE; |
1da177e4 | 1112 | |
24368aad CH |
1113 | if (stable && !use_wgather) |
1114 | flags |= RWF_SYNC; | |
1115 | ||
de4eda9d | 1116 | iov_iter_kvec(&iter, ITER_SOURCE, vec, vlen, *cnt); |
555dbf1a | 1117 | since = READ_ONCE(file->f_wb_err); |
33388b3a | 1118 | if (verf) |
3988a578 | 1119 | nfsd_copy_write_verifier(verf, nn); |
3c0949e5 | 1120 | file_start_write(file); |
33388b3a | 1121 | host_err = vfs_iter_write(file, &iter, &pos, flags); |
3c0949e5 | 1122 | file_end_write(file); |
7bf94c6b | 1123 | if (host_err < 0) { |
3988a578 | 1124 | nfsd_reset_write_verifier(nn); |
75acacb6 | 1125 | trace_nfsd_writeverf_reset(nn, rqstp, host_err); |
e4636d53 | 1126 | goto out_nfserr; |
7bf94c6b | 1127 | } |
09a80f2a | 1128 | *cnt = host_err; |
20ad856e | 1129 | nfsd_stats_io_write_add(exp, *cnt); |
2a12a9d7 | 1130 | fsnotify_modify(file); |
555dbf1a TM |
1131 | host_err = filemap_check_wb_err(file->f_mapping, since); |
1132 | if (host_err < 0) | |
1133 | goto out_nfserr; | |
1da177e4 | 1134 | |
bbf2f098 | 1135 | if (stable && use_wgather) { |
24368aad | 1136 | host_err = wait_for_concurrent_writes(file); |
75acacb6 | 1137 | if (host_err < 0) { |
3988a578 | 1138 | nfsd_reset_write_verifier(nn); |
75acacb6 CL |
1139 | trace_nfsd_writeverf_reset(nn, rqstp, host_err); |
1140 | } | |
bbf2f098 | 1141 | } |
1da177e4 | 1142 | |
e4636d53 | 1143 | out_nfserr: |
d890be15 CL |
1144 | if (host_err >= 0) { |
1145 | trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt); | |
1146 | nfserr = nfs_ok; | |
1147 | } else { | |
1148 | trace_nfsd_write_err(rqstp, fhp, offset, host_err); | |
1149 | nfserr = nfserrno(host_err); | |
1150 | } | |
01cbf385 | 1151 | if (restore_flags) |
a37b0715 | 1152 | current_restore_flags(pflags, PF_LOCAL_THROTTLE); |
d890be15 | 1153 | return nfserr; |
1da177e4 LT |
1154 | } |
1155 | ||
dc97618d BF |
1156 | /* |
1157 | * Read data from a file. count must contain the requested read count | |
1158 | * on entry. On return, *count contains the number of bytes actually read. | |
1159 | * N.B. After this call fhp needs an fh_put | |
1160 | */ | |
1161 | __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, | |
83a63072 TM |
1162 | loff_t offset, struct kvec *vec, int vlen, unsigned long *count, |
1163 | u32 *eof) | |
dc97618d | 1164 | { |
48cd7b51 | 1165 | struct nfsd_file *nf; |
dc97618d | 1166 | struct file *file; |
dc97618d BF |
1167 | __be32 err; |
1168 | ||
f394b62b | 1169 | trace_nfsd_read_start(rqstp, fhp, offset, *count); |
4d1ea845 | 1170 | err = nfsd_file_acquire_gc(rqstp, fhp, NFSD_MAY_READ, &nf); |
dc97618d BF |
1171 | if (err) |
1172 | return err; | |
1173 | ||
48cd7b51 | 1174 | file = nf->nf_file; |
a4058c5b | 1175 | if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags)) |
83a63072 | 1176 | err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof); |
a4058c5b | 1177 | else |
83a63072 | 1178 | err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof); |
6e8b50d1 | 1179 | |
48cd7b51 | 1180 | nfsd_file_put(nf); |
dc97618d | 1181 | |
f394b62b | 1182 | trace_nfsd_read_done(rqstp, fhp, offset, *count); |
6e8b50d1 | 1183 | |
fa0a2126 BF |
1184 | return err; |
1185 | } | |
1186 | ||
1da177e4 LT |
1187 | /* |
1188 | * Write data to a file. | |
1189 | * The stable flag requests synchronous writes. | |
1190 | * N.B. After this call fhp needs an fh_put | |
1191 | */ | |
6264d69d | 1192 | __be32 |
52e380e0 | 1193 | nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset, |
19e0663f TM |
1194 | struct kvec *vec, int vlen, unsigned long *cnt, int stable, |
1195 | __be32 *verf) | |
1da177e4 | 1196 | { |
b4935239 JL |
1197 | struct nfsd_file *nf; |
1198 | __be32 err; | |
1da177e4 | 1199 | |
f394b62b | 1200 | trace_nfsd_write_start(rqstp, fhp, offset, *cnt); |
6e8b50d1 | 1201 | |
4d1ea845 | 1202 | err = nfsd_file_acquire_gc(rqstp, fhp, NFSD_MAY_WRITE, &nf); |
52e380e0 KM |
1203 | if (err) |
1204 | goto out; | |
1205 | ||
16f8f894 | 1206 | err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec, |
19e0663f | 1207 | vlen, cnt, stable, verf); |
b4935239 | 1208 | nfsd_file_put(nf); |
1da177e4 | 1209 | out: |
f394b62b | 1210 | trace_nfsd_write_done(rqstp, fhp, offset, *cnt); |
1da177e4 LT |
1211 | return err; |
1212 | } | |
1213 | ||
3f965021 CL |
1214 | /** |
1215 | * nfsd_commit - Commit pending writes to stable storage | |
1216 | * @rqstp: RPC request being processed | |
1217 | * @fhp: NFS filehandle | |
c2528490 | 1218 | * @nf: target file |
3f965021 CL |
1219 | * @offset: raw offset from beginning of file |
1220 | * @count: raw count of bytes to sync | |
1221 | * @verf: filled in with the server's current write verifier | |
aa696a6f | 1222 | * |
3f965021 CL |
1223 | * Note: we guarantee that data that lies within the range specified |
1224 | * by the 'offset' and 'count' parameters will be synced. The server | |
1225 | * is permitted to sync data that lies outside this range at the | |
1226 | * same time. | |
1da177e4 LT |
1227 | * |
1228 | * Unfortunately we cannot lock the file to make sure we return full WCC | |
1229 | * data to the client, as locking happens lower down in the filesystem. | |
3f965021 CL |
1230 | * |
1231 | * Return values: | |
1232 | * An nfsstat value in network byte order. | |
1da177e4 | 1233 | */ |
6264d69d | 1234 | __be32 |
c2528490 CL |
1235 | nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf, |
1236 | u64 offset, u32 count, __be32 *verf) | |
1da177e4 | 1237 | { |
c2528490 | 1238 | __be32 err = nfs_ok; |
3f965021 CL |
1239 | u64 maxbytes; |
1240 | loff_t start, end; | |
2c445a0e | 1241 | struct nfsd_net *nn; |
3f965021 CL |
1242 | |
1243 | /* | |
1244 | * Convert the client-provided (offset, count) range to a | |
1245 | * (start, end) range. If the client-provided range falls | |
1246 | * outside the maximum file size of the underlying FS, | |
1247 | * clamp the sync range appropriately. | |
1248 | */ | |
1249 | start = 0; | |
1250 | end = LLONG_MAX; | |
1251 | maxbytes = (u64)fhp->fh_dentry->d_sb->s_maxbytes; | |
1252 | if (offset < maxbytes) { | |
1253 | start = offset; | |
1254 | if (count && (offset + count - 1 < maxbytes)) | |
1255 | end = offset + count - 1; | |
1256 | } | |
1257 | ||
2c445a0e | 1258 | nn = net_generic(nf->nf_net, nfsd_net_id); |
1da177e4 | 1259 | if (EX_ISSYNC(fhp->fh_export)) { |
555dbf1a TM |
1260 | errseq_t since = READ_ONCE(nf->nf_file->f_wb_err); |
1261 | int err2; | |
aa696a6f | 1262 | |
3f965021 | 1263 | err2 = vfs_fsync_range(nf->nf_file, start, end, 0); |
bbf2f098 TM |
1264 | switch (err2) { |
1265 | case 0: | |
3988a578 | 1266 | nfsd_copy_write_verifier(verf, nn); |
555dbf1a TM |
1267 | err2 = filemap_check_wb_err(nf->nf_file->f_mapping, |
1268 | since); | |
8a9ffb8c | 1269 | err = nfserrno(err2); |
bbf2f098 TM |
1270 | break; |
1271 | case -EINVAL: | |
1da177e4 | 1272 | err = nfserr_notsupp; |
bbf2f098 TM |
1273 | break; |
1274 | default: | |
3988a578 | 1275 | nfsd_reset_write_verifier(nn); |
75acacb6 | 1276 | trace_nfsd_writeverf_reset(nn, rqstp, err2); |
8a9ffb8c | 1277 | err = nfserrno(err2); |
bbf2f098 | 1278 | } |
524ff1af | 1279 | } else |
3988a578 | 1280 | nfsd_copy_write_verifier(verf, nn); |
1da177e4 | 1281 | |
1da177e4 LT |
1282 | return err; |
1283 | } | |
1da177e4 | 1284 | |
5f46e950 CL |
1285 | /** |
1286 | * nfsd_create_setattr - Set a created file's attributes | |
1287 | * @rqstp: RPC transaction being executed | |
1288 | * @fhp: NFS filehandle of parent directory | |
1289 | * @resfhp: NFS filehandle of new object | |
7fe2a71d | 1290 | * @attrs: requested attributes of new object |
5f46e950 CL |
1291 | * |
1292 | * Returns nfs_ok on success, or an nfsstat in network byte order. | |
1293 | */ | |
1294 | __be32 | |
1295 | nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, | |
7fe2a71d | 1296 | struct svc_fh *resfhp, struct nfsd_attrs *attrs) |
5c002b3b | 1297 | { |
7fe2a71d | 1298 | struct iattr *iap = attrs->na_iattr; |
5f46e950 CL |
1299 | __be32 status; |
1300 | ||
5c002b3b | 1301 | /* |
5f46e950 | 1302 | * Mode has already been set by file creation. |
5c002b3b BF |
1303 | */ |
1304 | iap->ia_valid &= ~ATTR_MODE; | |
5f46e950 | 1305 | |
5c002b3b BF |
1306 | /* |
1307 | * Setting uid/gid works only for root. Irix appears to | |
1308 | * send along the gid on create when it tries to implement | |
1309 | * setgid directories via NFS: | |
1310 | */ | |
6fab8779 | 1311 | if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) |
5c002b3b | 1312 | iap->ia_valid &= ~(ATTR_UID|ATTR_GID); |
5f46e950 CL |
1313 | |
1314 | /* | |
1315 | * Callers expect new file metadata to be committed even | |
1316 | * if the attributes have not changed. | |
1317 | */ | |
5c002b3b | 1318 | if (iap->ia_valid) |
7fe2a71d | 1319 | status = nfsd_setattr(rqstp, resfhp, attrs, 0, (time64_t)0); |
5f46e950 CL |
1320 | else |
1321 | status = nfserrno(commit_metadata(resfhp)); | |
1322 | ||
1323 | /* | |
1324 | * Transactional filesystems had a chance to commit changes | |
1325 | * for both parent and child simultaneously making the | |
1326 | * following commit_metadata a noop in many cases. | |
1327 | */ | |
1328 | if (!status) | |
1329 | status = nfserrno(commit_metadata(fhp)); | |
1330 | ||
1331 | /* | |
1332 | * Update the new filehandle to pick up the new attributes. | |
1333 | */ | |
1334 | if (!status) | |
1335 | status = fh_update(resfhp); | |
1336 | ||
1337 | return status; | |
5c002b3b BF |
1338 | } |
1339 | ||
4ac35c2f | 1340 | /* HPUX client sometimes creates a file in mode 000, and sets size to 0. |
1341 | * setting size to 0 may fail for some specific file systems by the permission | |
1342 | * checking which requires WRITE permission but the mode is 000. | |
1343 | * we ignore the resizing(to 0) on the just new created file, since the size is | |
1344 | * 0 after file created. | |
1345 | * | |
1346 | * call this only after vfs_create() is called. | |
1347 | * */ | |
1348 | static void | |
1349 | nfsd_check_ignore_resizing(struct iattr *iap) | |
1350 | { | |
1351 | if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0)) | |
1352 | iap->ia_valid &= ~ATTR_SIZE; | |
1353 | } | |
1354 | ||
b44061d0 | 1355 | /* The parent directory should already be locked: */ |
6264d69d | 1356 | __be32 |
b44061d0 | 1357 | nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp, |
9558f930 | 1358 | struct nfsd_attrs *attrs, |
7fe2a71d | 1359 | int type, dev_t rdev, struct svc_fh *resfhp) |
1da177e4 | 1360 | { |
2b118859 | 1361 | struct dentry *dentry, *dchild; |
1da177e4 | 1362 | struct inode *dirp; |
7fe2a71d | 1363 | struct iattr *iap = attrs->na_iattr; |
6264d69d AV |
1364 | __be32 err; |
1365 | int host_err; | |
1da177e4 | 1366 | |
1da177e4 | 1367 | dentry = fhp->fh_dentry; |
2b0143b5 | 1368 | dirp = d_inode(dentry); |
1da177e4 | 1369 | |
b44061d0 | 1370 | dchild = dget(resfhp->fh_dentry); |
7eed34f1 OD |
1371 | err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE); |
1372 | if (err) | |
1373 | goto out; | |
1374 | ||
1da177e4 LT |
1375 | if (!(iap->ia_valid & ATTR_MODE)) |
1376 | iap->ia_mode = 0; | |
1377 | iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type; | |
1378 | ||
22cf8419 BF |
1379 | if (!IS_POSIXACL(dirp)) |
1380 | iap->ia_mode &= ~current_umask(); | |
1381 | ||
088406bc | 1382 | err = 0; |
1da177e4 LT |
1383 | switch (type) { |
1384 | case S_IFREG: | |
6521f891 | 1385 | host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true); |
4ac35c2f | 1386 | if (!host_err) |
1387 | nfsd_check_ignore_resizing(iap); | |
1da177e4 LT |
1388 | break; |
1389 | case S_IFDIR: | |
6521f891 | 1390 | host_err = vfs_mkdir(&init_user_ns, dirp, dchild, iap->ia_mode); |
3819bb0d AV |
1391 | if (!host_err && unlikely(d_unhashed(dchild))) { |
1392 | struct dentry *d; | |
1393 | d = lookup_one_len(dchild->d_name.name, | |
1394 | dchild->d_parent, | |
1395 | dchild->d_name.len); | |
1396 | if (IS_ERR(d)) { | |
1397 | host_err = PTR_ERR(d); | |
1398 | break; | |
1399 | } | |
1400 | if (unlikely(d_is_negative(d))) { | |
1401 | dput(d); | |
1402 | err = nfserr_serverfault; | |
1403 | goto out; | |
1404 | } | |
1405 | dput(resfhp->fh_dentry); | |
1406 | resfhp->fh_dentry = dget(d); | |
1407 | err = fh_update(resfhp); | |
1408 | dput(dchild); | |
1409 | dchild = d; | |
1410 | if (err) | |
1411 | goto out; | |
1412 | } | |
1da177e4 LT |
1413 | break; |
1414 | case S_IFCHR: | |
1415 | case S_IFBLK: | |
1416 | case S_IFIFO: | |
1417 | case S_IFSOCK: | |
6521f891 CB |
1418 | host_err = vfs_mknod(&init_user_ns, dirp, dchild, |
1419 | iap->ia_mode, rdev); | |
1da177e4 | 1420 | break; |
71423274 BF |
1421 | default: |
1422 | printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n", | |
1423 | type); | |
1424 | host_err = -EINVAL; | |
1da177e4 | 1425 | } |
4a55c101 | 1426 | if (host_err < 0) |
1da177e4 LT |
1427 | goto out_nfserr; |
1428 | ||
7fe2a71d | 1429 | err = nfsd_create_setattr(rqstp, fhp, resfhp, attrs); |
1da177e4 | 1430 | |
1da177e4 | 1431 | out: |
2b118859 | 1432 | dput(dchild); |
1da177e4 LT |
1433 | return err; |
1434 | ||
1435 | out_nfserr: | |
6264d69d | 1436 | err = nfserrno(host_err); |
1da177e4 LT |
1437 | goto out; |
1438 | } | |
1439 | ||
b44061d0 BF |
1440 | /* |
1441 | * Create a filesystem object (regular, directory, special). | |
1442 | * Note that the parent directory is left locked. | |
1443 | * | |
1444 | * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp | |
1445 | */ | |
1446 | __be32 | |
1447 | nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp, | |
7fe2a71d N |
1448 | char *fname, int flen, struct nfsd_attrs *attrs, |
1449 | int type, dev_t rdev, struct svc_fh *resfhp) | |
b44061d0 BF |
1450 | { |
1451 | struct dentry *dentry, *dchild = NULL; | |
b44061d0 BF |
1452 | __be32 err; |
1453 | int host_err; | |
1454 | ||
1455 | if (isdotent(fname, flen)) | |
1456 | return nfserr_exist; | |
1457 | ||
fa08139d | 1458 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP); |
b44061d0 BF |
1459 | if (err) |
1460 | return err; | |
1461 | ||
1462 | dentry = fhp->fh_dentry; | |
b44061d0 BF |
1463 | |
1464 | host_err = fh_want_write(fhp); | |
1465 | if (host_err) | |
1466 | return nfserrno(host_err); | |
1467 | ||
debf16f0 | 1468 | inode_lock_nested(dentry->d_inode, I_MUTEX_PARENT); |
b44061d0 BF |
1469 | dchild = lookup_one_len(fname, dentry, flen); |
1470 | host_err = PTR_ERR(dchild); | |
927bfc56 N |
1471 | if (IS_ERR(dchild)) { |
1472 | err = nfserrno(host_err); | |
1473 | goto out_unlock; | |
1474 | } | |
b44061d0 | 1475 | err = fh_compose(resfhp, fhp->fh_export, dchild, fhp); |
502aa0a5 JB |
1476 | /* |
1477 | * We unconditionally drop our ref to dchild as fh_compose will have | |
1478 | * already grabbed its own ref for it. | |
1479 | */ | |
1480 | dput(dchild); | |
1481 | if (err) | |
927bfc56 | 1482 | goto out_unlock; |
debf16f0 | 1483 | fh_fill_pre_attrs(fhp); |
9558f930 | 1484 | err = nfsd_create_locked(rqstp, fhp, attrs, type, rdev, resfhp); |
debf16f0 | 1485 | fh_fill_post_attrs(fhp); |
927bfc56 | 1486 | out_unlock: |
debf16f0 | 1487 | inode_unlock(dentry->d_inode); |
927bfc56 | 1488 | return err; |
b44061d0 BF |
1489 | } |
1490 | ||
1da177e4 LT |
1491 | /* |
1492 | * Read a symlink. On entry, *lenp must contain the maximum path length that | |
1493 | * fits into the buffer. On return, it contains the true length. | |
1494 | * N.B. After this call fhp needs an fh_put | |
1495 | */ | |
6264d69d | 1496 | __be32 |
1da177e4 LT |
1497 | nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp) |
1498 | { | |
6264d69d | 1499 | __be32 err; |
4d7edbc3 | 1500 | const char *link; |
68ac1234 | 1501 | struct path path; |
4d7edbc3 AV |
1502 | DEFINE_DELAYED_CALL(done); |
1503 | int len; | |
1da177e4 | 1504 | |
8837abca | 1505 | err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP); |
4d7edbc3 AV |
1506 | if (unlikely(err)) |
1507 | return err; | |
1da177e4 | 1508 | |
68ac1234 AV |
1509 | path.mnt = fhp->fh_export->ex_path.mnt; |
1510 | path.dentry = fhp->fh_dentry; | |
1da177e4 | 1511 | |
4d7edbc3 AV |
1512 | if (unlikely(!d_is_symlink(path.dentry))) |
1513 | return nfserr_inval; | |
1da177e4 | 1514 | |
68ac1234 | 1515 | touch_atime(&path); |
1da177e4 | 1516 | |
4d7edbc3 AV |
1517 | link = vfs_get_link(path.dentry, &done); |
1518 | if (IS_ERR(link)) | |
1519 | return nfserrno(PTR_ERR(link)); | |
1da177e4 | 1520 | |
4d7edbc3 AV |
1521 | len = strlen(link); |
1522 | if (len < *lenp) | |
1523 | *lenp = len; | |
1524 | memcpy(buf, link, *lenp); | |
1525 | do_delayed_call(&done); | |
1526 | return 0; | |
1da177e4 LT |
1527 | } |
1528 | ||
93adc1e3 N |
1529 | /** |
1530 | * nfsd_symlink - Create a symlink and look up its inode | |
1531 | * @rqstp: RPC transaction being executed | |
1532 | * @fhp: NFS filehandle of parent directory | |
1533 | * @fname: filename of the new symlink | |
1534 | * @flen: length of @fname | |
1535 | * @path: content of the new symlink (NUL-terminated) | |
1536 | * @attrs: requested attributes of new object | |
1537 | * @resfhp: NFS filehandle of new object | |
1538 | * | |
1da177e4 | 1539 | * N.B. After this call _both_ fhp and resfhp need an fh_put |
93adc1e3 N |
1540 | * |
1541 | * Returns nfs_ok on success, or an nfsstat in network byte order. | |
1da177e4 | 1542 | */ |
6264d69d | 1543 | __be32 |
1da177e4 | 1544 | nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp, |
93adc1e3 N |
1545 | char *fname, int flen, |
1546 | char *path, struct nfsd_attrs *attrs, | |
1547 | struct svc_fh *resfhp) | |
1da177e4 LT |
1548 | { |
1549 | struct dentry *dentry, *dnew; | |
6264d69d AV |
1550 | __be32 err, cerr; |
1551 | int host_err; | |
1da177e4 LT |
1552 | |
1553 | err = nfserr_noent; | |
52ee0433 | 1554 | if (!flen || path[0] == '\0') |
1da177e4 LT |
1555 | goto out; |
1556 | err = nfserr_exist; | |
1557 | if (isdotent(fname, flen)) | |
1558 | goto out; | |
1559 | ||
8837abca | 1560 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE); |
1da177e4 LT |
1561 | if (err) |
1562 | goto out; | |
4a55c101 JK |
1563 | |
1564 | host_err = fh_want_write(fhp); | |
927bfc56 N |
1565 | if (host_err) { |
1566 | err = nfserrno(host_err); | |
1567 | goto out; | |
1568 | } | |
4a55c101 | 1569 | |
1da177e4 | 1570 | dentry = fhp->fh_dentry; |
debf16f0 | 1571 | inode_lock_nested(dentry->d_inode, I_MUTEX_PARENT); |
1da177e4 | 1572 | dnew = lookup_one_len(fname, dentry, flen); |
927bfc56 N |
1573 | if (IS_ERR(dnew)) { |
1574 | err = nfserrno(PTR_ERR(dnew)); | |
debf16f0 | 1575 | inode_unlock(dentry->d_inode); |
927bfc56 N |
1576 | goto out_drop_write; |
1577 | } | |
debf16f0 | 1578 | fh_fill_pre_attrs(fhp); |
6521f891 | 1579 | host_err = vfs_symlink(&init_user_ns, d_inode(dentry), dnew, path); |
6264d69d | 1580 | err = nfserrno(host_err); |
93adc1e3 N |
1581 | cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp); |
1582 | if (!err) | |
1583 | nfsd_create_setattr(rqstp, fhp, resfhp, attrs); | |
debf16f0 N |
1584 | fh_fill_post_attrs(fhp); |
1585 | inode_unlock(dentry->d_inode); | |
f501912a BM |
1586 | if (!err) |
1587 | err = nfserrno(commit_metadata(fhp)); | |
1da177e4 LT |
1588 | dput(dnew); |
1589 | if (err==0) err = cerr; | |
927bfc56 N |
1590 | out_drop_write: |
1591 | fh_drop_write(fhp); | |
1da177e4 LT |
1592 | out: |
1593 | return err; | |
1da177e4 LT |
1594 | } |
1595 | ||
1596 | /* | |
1597 | * Create a hardlink | |
1598 | * N.B. After this call _both_ ffhp and tfhp need an fh_put | |
1599 | */ | |
6264d69d | 1600 | __be32 |
1da177e4 LT |
1601 | nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp, |
1602 | char *name, int len, struct svc_fh *tfhp) | |
1603 | { | |
1604 | struct dentry *ddir, *dnew, *dold; | |
55b13354 | 1605 | struct inode *dirp; |
6264d69d AV |
1606 | __be32 err; |
1607 | int host_err; | |
1da177e4 | 1608 | |
8837abca | 1609 | err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE); |
1da177e4 LT |
1610 | if (err) |
1611 | goto out; | |
7d818a7b | 1612 | err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP); |
1da177e4 LT |
1613 | if (err) |
1614 | goto out; | |
7d818a7b | 1615 | err = nfserr_isdir; |
e36cb0b8 | 1616 | if (d_is_dir(tfhp->fh_dentry)) |
7d818a7b | 1617 | goto out; |
1da177e4 LT |
1618 | err = nfserr_perm; |
1619 | if (!len) | |
1620 | goto out; | |
1621 | err = nfserr_exist; | |
1622 | if (isdotent(name, len)) | |
1623 | goto out; | |
1624 | ||
4a55c101 JK |
1625 | host_err = fh_want_write(tfhp); |
1626 | if (host_err) { | |
1627 | err = nfserrno(host_err); | |
1628 | goto out; | |
1629 | } | |
1630 | ||
1da177e4 | 1631 | ddir = ffhp->fh_dentry; |
2b0143b5 | 1632 | dirp = d_inode(ddir); |
debf16f0 | 1633 | inode_lock_nested(dirp, I_MUTEX_PARENT); |
1da177e4 LT |
1634 | |
1635 | dnew = lookup_one_len(name, ddir, len); | |
e18bcb33 N |
1636 | if (IS_ERR(dnew)) { |
1637 | err = nfserrno(PTR_ERR(dnew)); | |
1638 | goto out_unlock; | |
1639 | } | |
1da177e4 LT |
1640 | |
1641 | dold = tfhp->fh_dentry; | |
1da177e4 | 1642 | |
4795bb37 | 1643 | err = nfserr_noent; |
2b0143b5 | 1644 | if (d_really_is_negative(dold)) |
4a55c101 | 1645 | goto out_dput; |
debf16f0 | 1646 | fh_fill_pre_attrs(ffhp); |
6521f891 | 1647 | host_err = vfs_link(dold, &init_user_ns, dirp, dnew, NULL); |
debf16f0 N |
1648 | fh_fill_post_attrs(ffhp); |
1649 | inode_unlock(dirp); | |
6264d69d | 1650 | if (!host_err) { |
f501912a BM |
1651 | err = nfserrno(commit_metadata(ffhp)); |
1652 | if (!err) | |
1653 | err = nfserrno(commit_metadata(tfhp)); | |
1da177e4 | 1654 | } else { |
6264d69d | 1655 | if (host_err == -EXDEV && rqstp->rq_vers == 2) |
1da177e4 LT |
1656 | err = nfserr_acces; |
1657 | else | |
6264d69d | 1658 | err = nfserrno(host_err); |
1da177e4 | 1659 | } |
1da177e4 | 1660 | dput(dnew); |
e18bcb33 | 1661 | out_drop_write: |
4a55c101 | 1662 | fh_drop_write(tfhp); |
1da177e4 LT |
1663 | out: |
1664 | return err; | |
1665 | ||
e18bcb33 N |
1666 | out_dput: |
1667 | dput(dnew); | |
1668 | out_unlock: | |
debf16f0 | 1669 | inode_unlock(dirp); |
e18bcb33 | 1670 | goto out_drop_write; |
1da177e4 LT |
1671 | } |
1672 | ||
7775ec57 JL |
1673 | static void |
1674 | nfsd_close_cached_files(struct dentry *dentry) | |
1675 | { | |
1676 | struct inode *inode = d_inode(dentry); | |
1677 | ||
1678 | if (inode && S_ISREG(inode->i_mode)) | |
1679 | nfsd_file_close_inode_sync(inode); | |
1680 | } | |
1681 | ||
1682 | static bool | |
1683 | nfsd_has_cached_files(struct dentry *dentry) | |
1684 | { | |
1685 | bool ret = false; | |
1686 | struct inode *inode = d_inode(dentry); | |
1687 | ||
1688 | if (inode && S_ISREG(inode->i_mode)) | |
1689 | ret = nfsd_file_is_cached(inode); | |
1690 | return ret; | |
1691 | } | |
1692 | ||
1da177e4 LT |
1693 | /* |
1694 | * Rename a file | |
1695 | * N.B. After this call _both_ ffhp and tfhp need an fh_put | |
1696 | */ | |
6264d69d | 1697 | __be32 |
1da177e4 LT |
1698 | nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen, |
1699 | struct svc_fh *tfhp, char *tname, int tlen) | |
1700 | { | |
1701 | struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap; | |
1702 | struct inode *fdir, *tdir; | |
6264d69d AV |
1703 | __be32 err; |
1704 | int host_err; | |
7f84b488 | 1705 | bool close_cached = false; |
1da177e4 | 1706 | |
8837abca | 1707 | err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE); |
1da177e4 LT |
1708 | if (err) |
1709 | goto out; | |
8837abca | 1710 | err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE); |
1da177e4 LT |
1711 | if (err) |
1712 | goto out; | |
1713 | ||
1714 | fdentry = ffhp->fh_dentry; | |
2b0143b5 | 1715 | fdir = d_inode(fdentry); |
1da177e4 LT |
1716 | |
1717 | tdentry = tfhp->fh_dentry; | |
2b0143b5 | 1718 | tdir = d_inode(tdentry); |
1da177e4 | 1719 | |
1da177e4 LT |
1720 | err = nfserr_perm; |
1721 | if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen)) | |
1722 | goto out; | |
1723 | ||
7775ec57 | 1724 | retry: |
4a55c101 JK |
1725 | host_err = fh_want_write(ffhp); |
1726 | if (host_err) { | |
1727 | err = nfserrno(host_err); | |
1728 | goto out; | |
1729 | } | |
1730 | ||
1da177e4 | 1731 | trap = lock_rename(tdentry, fdentry); |
fcb5e3fa CL |
1732 | fh_fill_pre_attrs(ffhp); |
1733 | fh_fill_pre_attrs(tfhp); | |
1da177e4 LT |
1734 | |
1735 | odentry = lookup_one_len(fname, fdentry, flen); | |
6264d69d | 1736 | host_err = PTR_ERR(odentry); |
1da177e4 LT |
1737 | if (IS_ERR(odentry)) |
1738 | goto out_nfserr; | |
1739 | ||
6264d69d | 1740 | host_err = -ENOENT; |
2b0143b5 | 1741 | if (d_really_is_negative(odentry)) |
1da177e4 | 1742 | goto out_dput_old; |
6264d69d | 1743 | host_err = -EINVAL; |
1da177e4 LT |
1744 | if (odentry == trap) |
1745 | goto out_dput_old; | |
1746 | ||
1747 | ndentry = lookup_one_len(tname, tdentry, tlen); | |
6264d69d | 1748 | host_err = PTR_ERR(ndentry); |
1da177e4 LT |
1749 | if (IS_ERR(ndentry)) |
1750 | goto out_dput_old; | |
6264d69d | 1751 | host_err = -ENOTEMPTY; |
1da177e4 LT |
1752 | if (ndentry == trap) |
1753 | goto out_dput_new; | |
1754 | ||
9079b1eb DH |
1755 | host_err = -EXDEV; |
1756 | if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt) | |
1757 | goto out_dput_new; | |
aa387d6c BF |
1758 | if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry) |
1759 | goto out_dput_new; | |
9079b1eb | 1760 | |
7f84b488 JL |
1761 | if ((ndentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK) && |
1762 | nfsd_has_cached_files(ndentry)) { | |
1763 | close_cached = true; | |
7775ec57 JL |
1764 | goto out_dput_old; |
1765 | } else { | |
9fe61450 | 1766 | struct renamedata rd = { |
6521f891 | 1767 | .old_mnt_userns = &init_user_ns, |
9fe61450 CB |
1768 | .old_dir = fdir, |
1769 | .old_dentry = odentry, | |
6521f891 | 1770 | .new_mnt_userns = &init_user_ns, |
9fe61450 CB |
1771 | .new_dir = tdir, |
1772 | .new_dentry = ndentry, | |
1773 | }; | |
68c522af CL |
1774 | int retries; |
1775 | ||
1776 | for (retries = 1;;) { | |
1777 | host_err = vfs_rename(&rd); | |
1778 | if (host_err != -EAGAIN || !retries--) | |
1779 | break; | |
1780 | if (!nfsd_wait_for_delegreturn(rqstp, d_inode(odentry))) | |
1781 | break; | |
1782 | } | |
7775ec57 JL |
1783 | if (!host_err) { |
1784 | host_err = commit_metadata(tfhp); | |
1785 | if (!host_err) | |
1786 | host_err = commit_metadata(ffhp); | |
1787 | } | |
1da177e4 | 1788 | } |
1da177e4 LT |
1789 | out_dput_new: |
1790 | dput(ndentry); | |
1791 | out_dput_old: | |
1792 | dput(odentry); | |
1793 | out_nfserr: | |
6264d69d | 1794 | err = nfserrno(host_err); |
dd8dd403 | 1795 | |
7f84b488 | 1796 | if (!close_cached) { |
fcb5e3fa CL |
1797 | fh_fill_post_attrs(ffhp); |
1798 | fh_fill_post_attrs(tfhp); | |
7775ec57 | 1799 | } |
1da177e4 | 1800 | unlock_rename(tdentry, fdentry); |
4a55c101 | 1801 | fh_drop_write(ffhp); |
1da177e4 | 1802 | |
7775ec57 JL |
1803 | /* |
1804 | * If the target dentry has cached open files, then we need to try to | |
1805 | * close them prior to doing the rename. Flushing delayed fput | |
1806 | * shouldn't be done with locks held however, so we delay it until this | |
1807 | * point and then reattempt the whole shebang. | |
1808 | */ | |
7f84b488 JL |
1809 | if (close_cached) { |
1810 | close_cached = false; | |
7775ec57 JL |
1811 | nfsd_close_cached_files(ndentry); |
1812 | dput(ndentry); | |
1813 | goto retry; | |
1814 | } | |
1da177e4 LT |
1815 | out: |
1816 | return err; | |
1817 | } | |
1818 | ||
1819 | /* | |
1820 | * Unlink a file or directory | |
1821 | * N.B. After this call fhp needs an fh_put | |
1822 | */ | |
6264d69d | 1823 | __be32 |
1da177e4 LT |
1824 | nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, |
1825 | char *fname, int flen) | |
1826 | { | |
1827 | struct dentry *dentry, *rdentry; | |
1828 | struct inode *dirp; | |
e5d74a2d | 1829 | struct inode *rinode; |
6264d69d AV |
1830 | __be32 err; |
1831 | int host_err; | |
1da177e4 LT |
1832 | |
1833 | err = nfserr_acces; | |
1834 | if (!flen || isdotent(fname, flen)) | |
1835 | goto out; | |
8837abca | 1836 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE); |
1da177e4 LT |
1837 | if (err) |
1838 | goto out; | |
1839 | ||
4a55c101 JK |
1840 | host_err = fh_want_write(fhp); |
1841 | if (host_err) | |
1842 | goto out_nfserr; | |
1843 | ||
1da177e4 | 1844 | dentry = fhp->fh_dentry; |
2b0143b5 | 1845 | dirp = d_inode(dentry); |
debf16f0 | 1846 | inode_lock_nested(dirp, I_MUTEX_PARENT); |
1da177e4 LT |
1847 | |
1848 | rdentry = lookup_one_len(fname, dentry, flen); | |
6264d69d | 1849 | host_err = PTR_ERR(rdentry); |
1da177e4 | 1850 | if (IS_ERR(rdentry)) |
b677c0c6 | 1851 | goto out_unlock; |
1da177e4 | 1852 | |
2b0143b5 | 1853 | if (d_really_is_negative(rdentry)) { |
1da177e4 | 1854 | dput(rdentry); |
0ca0c9d7 | 1855 | host_err = -ENOENT; |
b677c0c6 | 1856 | goto out_unlock; |
1da177e4 | 1857 | } |
e5d74a2d YHH |
1858 | rinode = d_inode(rdentry); |
1859 | ihold(rinode); | |
1da177e4 LT |
1860 | |
1861 | if (!type) | |
2b0143b5 | 1862 | type = d_inode(rdentry)->i_mode & S_IFMT; |
1da177e4 | 1863 | |
debf16f0 | 1864 | fh_fill_pre_attrs(fhp); |
7775ec57 | 1865 | if (type != S_IFDIR) { |
5f5f8b6d CL |
1866 | int retries; |
1867 | ||
7f84b488 JL |
1868 | if (rdentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK) |
1869 | nfsd_close_cached_files(rdentry); | |
5f5f8b6d CL |
1870 | |
1871 | for (retries = 1;;) { | |
1872 | host_err = vfs_unlink(&init_user_ns, dirp, rdentry, NULL); | |
1873 | if (host_err != -EAGAIN || !retries--) | |
1874 | break; | |
1875 | if (!nfsd_wait_for_delegreturn(rqstp, rinode)) | |
1876 | break; | |
1877 | } | |
7775ec57 | 1878 | } else { |
6521f891 | 1879 | host_err = vfs_rmdir(&init_user_ns, dirp, rdentry); |
7775ec57 | 1880 | } |
debf16f0 | 1881 | fh_fill_post_attrs(fhp); |
7775ec57 | 1882 | |
debf16f0 | 1883 | inode_unlock(dirp); |
f501912a BM |
1884 | if (!host_err) |
1885 | host_err = commit_metadata(fhp); | |
541ce98c | 1886 | dput(rdentry); |
e5d74a2d | 1887 | iput(rinode); /* truncate the inode here */ |
541ce98c | 1888 | |
0ca0c9d7 BF |
1889 | out_drop_write: |
1890 | fh_drop_write(fhp); | |
1da177e4 | 1891 | out_nfserr: |
466e16f0 N |
1892 | if (host_err == -EBUSY) { |
1893 | /* name is mounted-on. There is no perfect | |
1894 | * error status. | |
1895 | */ | |
1896 | if (nfsd_v4client(rqstp)) | |
1897 | err = nfserr_file_open; | |
1898 | else | |
1899 | err = nfserr_acces; | |
1900 | } else { | |
1901 | err = nfserrno(host_err); | |
1902 | } | |
f193fbab YT |
1903 | out: |
1904 | return err; | |
b677c0c6 | 1905 | out_unlock: |
debf16f0 | 1906 | inode_unlock(dirp); |
b677c0c6 | 1907 | goto out_drop_write; |
1da177e4 LT |
1908 | } |
1909 | ||
14f7dd63 DW |
1910 | /* |
1911 | * We do this buffering because we must not call back into the file | |
1912 | * system's ->lookup() method from the filldir callback. That may well | |
1913 | * deadlock a number of file systems. | |
1914 | * | |
1915 | * This is based heavily on the implementation of same in XFS. | |
1916 | */ | |
1917 | struct buffered_dirent { | |
1918 | u64 ino; | |
1919 | loff_t offset; | |
1920 | int namlen; | |
1921 | unsigned int d_type; | |
1922 | char name[]; | |
1923 | }; | |
1924 | ||
1925 | struct readdir_data { | |
5c0ba4e0 | 1926 | struct dir_context ctx; |
14f7dd63 DW |
1927 | char *dirent; |
1928 | size_t used; | |
53c9c5c0 | 1929 | int full; |
14f7dd63 DW |
1930 | }; |
1931 | ||
25885a35 | 1932 | static bool nfsd_buffered_filldir(struct dir_context *ctx, const char *name, |
ac7576f4 MS |
1933 | int namlen, loff_t offset, u64 ino, |
1934 | unsigned int d_type) | |
14f7dd63 | 1935 | { |
ac7576f4 MS |
1936 | struct readdir_data *buf = |
1937 | container_of(ctx, struct readdir_data, ctx); | |
14f7dd63 DW |
1938 | struct buffered_dirent *de = (void *)(buf->dirent + buf->used); |
1939 | unsigned int reclen; | |
1940 | ||
1941 | reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64)); | |
53c9c5c0 AV |
1942 | if (buf->used + reclen > PAGE_SIZE) { |
1943 | buf->full = 1; | |
25885a35 | 1944 | return false; |
53c9c5c0 | 1945 | } |
14f7dd63 DW |
1946 | |
1947 | de->namlen = namlen; | |
1948 | de->offset = offset; | |
1949 | de->ino = ino; | |
1950 | de->d_type = d_type; | |
1951 | memcpy(de->name, name, namlen); | |
1952 | buf->used += reclen; | |
1953 | ||
25885a35 | 1954 | return true; |
14f7dd63 DW |
1955 | } |
1956 | ||
6019ce07 CL |
1957 | static __be32 nfsd_buffered_readdir(struct file *file, struct svc_fh *fhp, |
1958 | nfsd_filldir_t func, struct readdir_cd *cdp, | |
1959 | loff_t *offsetp) | |
2628b766 | 1960 | { |
14f7dd63 | 1961 | struct buffered_dirent *de; |
2628b766 | 1962 | int host_err; |
14f7dd63 DW |
1963 | int size; |
1964 | loff_t offset; | |
ac6614b7 AV |
1965 | struct readdir_data buf = { |
1966 | .ctx.actor = nfsd_buffered_filldir, | |
1967 | .dirent = (void *)__get_free_page(GFP_KERNEL) | |
1968 | }; | |
2628b766 | 1969 | |
14f7dd63 | 1970 | if (!buf.dirent) |
2f9092e1 | 1971 | return nfserrno(-ENOMEM); |
14f7dd63 DW |
1972 | |
1973 | offset = *offsetp; | |
2628b766 | 1974 | |
14f7dd63 DW |
1975 | while (1) { |
1976 | unsigned int reclen; | |
1977 | ||
b726e923 | 1978 | cdp->err = nfserr_eof; /* will be cleared on successful read */ |
14f7dd63 | 1979 | buf.used = 0; |
53c9c5c0 | 1980 | buf.full = 0; |
14f7dd63 | 1981 | |
5c0ba4e0 | 1982 | host_err = iterate_dir(file, &buf.ctx); |
53c9c5c0 AV |
1983 | if (buf.full) |
1984 | host_err = 0; | |
1985 | ||
1986 | if (host_err < 0) | |
14f7dd63 DW |
1987 | break; |
1988 | ||
1989 | size = buf.used; | |
1990 | ||
1991 | if (!size) | |
1992 | break; | |
1993 | ||
14f7dd63 DW |
1994 | de = (struct buffered_dirent *)buf.dirent; |
1995 | while (size > 0) { | |
1996 | offset = de->offset; | |
1997 | ||
1998 | if (func(cdp, de->name, de->namlen, de->offset, | |
1999 | de->ino, de->d_type)) | |
2f9092e1 | 2000 | break; |
14f7dd63 DW |
2001 | |
2002 | if (cdp->err != nfs_ok) | |
2f9092e1 | 2003 | break; |
14f7dd63 | 2004 | |
6019ce07 CL |
2005 | trace_nfsd_dirent(fhp, de->ino, de->name, de->namlen); |
2006 | ||
14f7dd63 DW |
2007 | reclen = ALIGN(sizeof(*de) + de->namlen, |
2008 | sizeof(u64)); | |
2009 | size -= reclen; | |
2010 | de = (struct buffered_dirent *)((char *)de + reclen); | |
2011 | } | |
2f9092e1 DW |
2012 | if (size > 0) /* We bailed out early */ |
2013 | break; | |
2014 | ||
c002a6c7 | 2015 | offset = vfs_llseek(file, 0, SEEK_CUR); |
14f7dd63 DW |
2016 | } |
2017 | ||
14f7dd63 | 2018 | free_page((unsigned long)(buf.dirent)); |
2628b766 DW |
2019 | |
2020 | if (host_err) | |
2021 | return nfserrno(host_err); | |
14f7dd63 DW |
2022 | |
2023 | *offsetp = offset; | |
2024 | return cdp->err; | |
2628b766 DW |
2025 | } |
2026 | ||
1da177e4 LT |
2027 | /* |
2028 | * Read entries from a directory. | |
2029 | * The NFSv3/4 verifier we ignore for now. | |
2030 | */ | |
6264d69d | 2031 | __be32 |
1da177e4 | 2032 | nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp, |
ac7576f4 | 2033 | struct readdir_cd *cdp, nfsd_filldir_t func) |
1da177e4 | 2034 | { |
6264d69d | 2035 | __be32 err; |
1da177e4 LT |
2036 | struct file *file; |
2037 | loff_t offset = *offsetp; | |
06effdbb BS |
2038 | int may_flags = NFSD_MAY_READ; |
2039 | ||
2040 | /* NFSv2 only supports 32 bit cookies */ | |
2041 | if (rqstp->rq_vers > 2) | |
2042 | may_flags |= NFSD_MAY_64BIT_COOKIE; | |
1da177e4 | 2043 | |
06effdbb | 2044 | err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file); |
1da177e4 LT |
2045 | if (err) |
2046 | goto out; | |
2047 | ||
b108fe6b | 2048 | offset = vfs_llseek(file, offset, SEEK_SET); |
1da177e4 LT |
2049 | if (offset < 0) { |
2050 | err = nfserrno((int)offset); | |
2051 | goto out_close; | |
2052 | } | |
2053 | ||
6019ce07 | 2054 | err = nfsd_buffered_readdir(file, fhp, func, cdp, offsetp); |
1da177e4 LT |
2055 | |
2056 | if (err == nfserr_eof || err == nfserr_toosmall) | |
2057 | err = nfs_ok; /* can still be found in ->err */ | |
2058 | out_close: | |
fd891454 | 2059 | fput(file); |
1da177e4 LT |
2060 | out: |
2061 | return err; | |
2062 | } | |
2063 | ||
2064 | /* | |
2065 | * Get file system stats | |
2066 | * N.B. After this call fhp needs an fh_put | |
2067 | */ | |
6264d69d | 2068 | __be32 |
04716e66 | 2069 | nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access) |
1da177e4 | 2070 | { |
ebabe9a9 CH |
2071 | __be32 err; |
2072 | ||
2073 | err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access); | |
f6360efb TI |
2074 | if (!err) { |
2075 | struct path path = { | |
2076 | .mnt = fhp->fh_export->ex_path.mnt, | |
2077 | .dentry = fhp->fh_dentry, | |
2078 | }; | |
2079 | if (vfs_statfs(&path, stat)) | |
2080 | err = nfserr_io; | |
2081 | } | |
1da177e4 LT |
2082 | return err; |
2083 | } | |
2084 | ||
c7d51402 | 2085 | static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp) |
e22841c6 | 2086 | { |
c7d51402 | 2087 | return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY; |
e22841c6 BF |
2088 | } |
2089 | ||
32119446 FL |
2090 | #ifdef CONFIG_NFSD_V4 |
2091 | /* | |
2092 | * Helper function to translate error numbers. In the case of xattr operations, | |
2093 | * some error codes need to be translated outside of the standard translations. | |
2094 | * | |
2095 | * ENODATA needs to be translated to nfserr_noxattr. | |
2096 | * E2BIG to nfserr_xattr2big. | |
2097 | * | |
2098 | * Additionally, vfs_listxattr can return -ERANGE. This means that the | |
2099 | * file has too many extended attributes to retrieve inside an | |
2100 | * XATTR_LIST_MAX sized buffer. This is a bug in the xattr implementation: | |
2101 | * filesystems will allow the adding of extended attributes until they hit | |
2102 | * their own internal limit. This limit may be larger than XATTR_LIST_MAX. | |
2103 | * So, at that point, the attributes are present and valid, but can't | |
2104 | * be retrieved using listxattr, since the upper level xattr code enforces | |
2105 | * the XATTR_LIST_MAX limit. | |
2106 | * | |
2107 | * This bug means that we need to deal with listxattr returning -ERANGE. The | |
2108 | * best mapping is to return TOOSMALL. | |
2109 | */ | |
2110 | static __be32 | |
2111 | nfsd_xattr_errno(int err) | |
2112 | { | |
2113 | switch (err) { | |
2114 | case -ENODATA: | |
2115 | return nfserr_noxattr; | |
2116 | case -E2BIG: | |
2117 | return nfserr_xattr2big; | |
2118 | case -ERANGE: | |
2119 | return nfserr_toosmall; | |
2120 | } | |
2121 | return nfserrno(err); | |
2122 | } | |
2123 | ||
2124 | /* | |
2125 | * Retrieve the specified user extended attribute. To avoid always | |
2126 | * having to allocate the maximum size (since we are not getting | |
2127 | * a maximum size from the RPC), do a probe + alloc. Hold a reader | |
2128 | * lock on i_rwsem to prevent the extended attribute from changing | |
2129 | * size while we're doing this. | |
2130 | */ | |
2131 | __be32 | |
2132 | nfsd_getxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name, | |
2133 | void **bufp, int *lenp) | |
2134 | { | |
2135 | ssize_t len; | |
2136 | __be32 err; | |
2137 | char *buf; | |
2138 | struct inode *inode; | |
2139 | struct dentry *dentry; | |
2140 | ||
2141 | err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ); | |
2142 | if (err) | |
2143 | return err; | |
2144 | ||
2145 | err = nfs_ok; | |
2146 | dentry = fhp->fh_dentry; | |
2147 | inode = d_inode(dentry); | |
2148 | ||
2149 | inode_lock_shared(inode); | |
2150 | ||
c7c7a1a1 | 2151 | len = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0); |
32119446 FL |
2152 | |
2153 | /* | |
2154 | * Zero-length attribute, just return. | |
2155 | */ | |
2156 | if (len == 0) { | |
2157 | *bufp = NULL; | |
2158 | *lenp = 0; | |
2159 | goto out; | |
2160 | } | |
2161 | ||
2162 | if (len < 0) { | |
2163 | err = nfsd_xattr_errno(len); | |
2164 | goto out; | |
2165 | } | |
2166 | ||
2167 | if (len > *lenp) { | |
2168 | err = nfserr_toosmall; | |
2169 | goto out; | |
2170 | } | |
2171 | ||
2172 | buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS); | |
2173 | if (buf == NULL) { | |
2174 | err = nfserr_jukebox; | |
2175 | goto out; | |
2176 | } | |
2177 | ||
c7c7a1a1 | 2178 | len = vfs_getxattr(&init_user_ns, dentry, name, buf, len); |
32119446 FL |
2179 | if (len <= 0) { |
2180 | kvfree(buf); | |
2181 | buf = NULL; | |
2182 | err = nfsd_xattr_errno(len); | |
2183 | } | |
2184 | ||
2185 | *lenp = len; | |
2186 | *bufp = buf; | |
2187 | ||
2188 | out: | |
2189 | inode_unlock_shared(inode); | |
2190 | ||
2191 | return err; | |
2192 | } | |
2193 | ||
2194 | /* | |
2195 | * Retrieve the xattr names. Since we can't know how many are | |
2196 | * user extended attributes, we must get all attributes here, | |
2197 | * and have the XDR encode filter out the "user." ones. | |
2198 | * | |
2199 | * While this could always just allocate an XATTR_LIST_MAX | |
2200 | * buffer, that's a waste, so do a probe + allocate. To | |
2201 | * avoid any changes between the probe and allocate, wrap | |
2202 | * this in inode_lock. | |
2203 | */ | |
2204 | __be32 | |
2205 | nfsd_listxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char **bufp, | |
2206 | int *lenp) | |
2207 | { | |
2208 | ssize_t len; | |
2209 | __be32 err; | |
2210 | char *buf; | |
2211 | struct inode *inode; | |
2212 | struct dentry *dentry; | |
2213 | ||
2214 | err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ); | |
2215 | if (err) | |
2216 | return err; | |
2217 | ||
2218 | dentry = fhp->fh_dentry; | |
2219 | inode = d_inode(dentry); | |
2220 | *lenp = 0; | |
2221 | ||
2222 | inode_lock_shared(inode); | |
2223 | ||
2224 | len = vfs_listxattr(dentry, NULL, 0); | |
2225 | if (len <= 0) { | |
2226 | err = nfsd_xattr_errno(len); | |
2227 | goto out; | |
2228 | } | |
2229 | ||
2230 | if (len > XATTR_LIST_MAX) { | |
2231 | err = nfserr_xattr2big; | |
2232 | goto out; | |
2233 | } | |
2234 | ||
2235 | /* | |
2236 | * We're holding i_rwsem - use GFP_NOFS. | |
2237 | */ | |
2238 | buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS); | |
2239 | if (buf == NULL) { | |
2240 | err = nfserr_jukebox; | |
2241 | goto out; | |
2242 | } | |
2243 | ||
2244 | len = vfs_listxattr(dentry, buf, len); | |
2245 | if (len <= 0) { | |
2246 | kvfree(buf); | |
2247 | err = nfsd_xattr_errno(len); | |
2248 | goto out; | |
2249 | } | |
2250 | ||
2251 | *lenp = len; | |
2252 | *bufp = buf; | |
2253 | ||
2254 | err = nfs_ok; | |
2255 | out: | |
2256 | inode_unlock_shared(inode); | |
2257 | ||
2258 | return err; | |
2259 | } | |
2260 | ||
bb4d53d6 N |
2261 | /** |
2262 | * nfsd_removexattr - Remove an extended attribute | |
2263 | * @rqstp: RPC transaction being executed | |
2264 | * @fhp: NFS filehandle of object with xattr to remove | |
2265 | * @name: name of xattr to remove (NUL-terminate) | |
2266 | * | |
2267 | * Pass in a NULL pointer for delegated_inode, and let the client deal | |
2268 | * with NFS4ERR_DELAY (same as with e.g. setattr and remove). | |
2269 | * | |
2270 | * Returns nfs_ok on success, or an nfsstat in network byte order. | |
32119446 FL |
2271 | */ |
2272 | __be32 | |
2273 | nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name) | |
2274 | { | |
8237284a CL |
2275 | __be32 err; |
2276 | int ret; | |
32119446 FL |
2277 | |
2278 | err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE); | |
2279 | if (err) | |
2280 | return err; | |
2281 | ||
2282 | ret = fh_want_write(fhp); | |
2283 | if (ret) | |
2284 | return nfserrno(ret); | |
2285 | ||
bb4d53d6 N |
2286 | inode_lock(fhp->fh_dentry->d_inode); |
2287 | fh_fill_pre_attrs(fhp); | |
32119446 | 2288 | |
c7c7a1a1 TA |
2289 | ret = __vfs_removexattr_locked(&init_user_ns, fhp->fh_dentry, |
2290 | name, NULL); | |
32119446 | 2291 | |
bb4d53d6 N |
2292 | fh_fill_post_attrs(fhp); |
2293 | inode_unlock(fhp->fh_dentry->d_inode); | |
32119446 FL |
2294 | fh_drop_write(fhp); |
2295 | ||
2296 | return nfsd_xattr_errno(ret); | |
2297 | } | |
2298 | ||
2299 | __be32 | |
2300 | nfsd_setxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name, | |
2301 | void *buf, u32 len, u32 flags) | |
2302 | { | |
8237284a CL |
2303 | __be32 err; |
2304 | int ret; | |
32119446 FL |
2305 | |
2306 | err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE); | |
2307 | if (err) | |
2308 | return err; | |
2309 | ||
2310 | ret = fh_want_write(fhp); | |
2311 | if (ret) | |
2312 | return nfserrno(ret); | |
bb4d53d6 N |
2313 | inode_lock(fhp->fh_dentry->d_inode); |
2314 | fh_fill_pre_attrs(fhp); | |
32119446 | 2315 | |
c7c7a1a1 TA |
2316 | ret = __vfs_setxattr_locked(&init_user_ns, fhp->fh_dentry, name, buf, |
2317 | len, flags, NULL); | |
bb4d53d6 N |
2318 | fh_fill_post_attrs(fhp); |
2319 | inode_unlock(fhp->fh_dentry->d_inode); | |
32119446 FL |
2320 | fh_drop_write(fhp); |
2321 | ||
2322 | return nfsd_xattr_errno(ret); | |
2323 | } | |
2324 | #endif | |
2325 | ||
1da177e4 LT |
2326 | /* |
2327 | * Check for a user's access permissions to this inode. | |
2328 | */ | |
6264d69d | 2329 | __be32 |
0ec757df BF |
2330 | nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp, |
2331 | struct dentry *dentry, int acc) | |
1da177e4 | 2332 | { |
2b0143b5 | 2333 | struct inode *inode = d_inode(dentry); |
1da177e4 LT |
2334 | int err; |
2335 | ||
aea93397 | 2336 | if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP) |
1da177e4 LT |
2337 | return 0; |
2338 | #if 0 | |
2339 | dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n", | |
2340 | acc, | |
8837abca MS |
2341 | (acc & NFSD_MAY_READ)? " read" : "", |
2342 | (acc & NFSD_MAY_WRITE)? " write" : "", | |
2343 | (acc & NFSD_MAY_EXEC)? " exec" : "", | |
2344 | (acc & NFSD_MAY_SATTR)? " sattr" : "", | |
2345 | (acc & NFSD_MAY_TRUNC)? " trunc" : "", | |
2346 | (acc & NFSD_MAY_LOCK)? " lock" : "", | |
2347 | (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "", | |
1da177e4 LT |
2348 | inode->i_mode, |
2349 | IS_IMMUTABLE(inode)? " immut" : "", | |
2350 | IS_APPEND(inode)? " append" : "", | |
2c463e95 | 2351 | __mnt_is_readonly(exp->ex_path.mnt)? " ro" : ""); |
1da177e4 | 2352 | dprintk(" owner %d/%d user %d/%d\n", |
5cc0a840 | 2353 | inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid()); |
1da177e4 LT |
2354 | #endif |
2355 | ||
2356 | /* Normally we reject any write/sattr etc access on a read-only file | |
2357 | * system. But if it is IRIX doing check on write-access for a | |
2358 | * device special file, we ignore rofs. | |
2359 | */ | |
8837abca MS |
2360 | if (!(acc & NFSD_MAY_LOCAL_ACCESS)) |
2361 | if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) { | |
2c463e95 DH |
2362 | if (exp_rdonly(rqstp, exp) || |
2363 | __mnt_is_readonly(exp->ex_path.mnt)) | |
1da177e4 | 2364 | return nfserr_rofs; |
8837abca | 2365 | if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode)) |
1da177e4 LT |
2366 | return nfserr_perm; |
2367 | } | |
8837abca | 2368 | if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode)) |
1da177e4 LT |
2369 | return nfserr_perm; |
2370 | ||
8837abca | 2371 | if (acc & NFSD_MAY_LOCK) { |
1da177e4 LT |
2372 | /* If we cannot rely on authentication in NLM requests, |
2373 | * just allow locks, otherwise require read permission, or | |
2374 | * ownership | |
2375 | */ | |
2376 | if (exp->ex_flags & NFSEXP_NOAUTHNLM) | |
2377 | return 0; | |
2378 | else | |
8837abca | 2379 | acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE; |
1da177e4 LT |
2380 | } |
2381 | /* | |
2382 | * The file owner always gets access permission for accesses that | |
2383 | * would normally be checked at open time. This is to make | |
2384 | * file access work even when the client has done a fchmod(fd, 0). | |
2385 | * | |
2386 | * However, `cp foo bar' should fail nevertheless when bar is | |
2387 | * readonly. A sensible way to do this might be to reject all | |
2388 | * attempts to truncate a read-only file, because a creat() call | |
2389 | * always implies file truncation. | |
2390 | * ... but this isn't really fair. A process may reasonably call | |
2391 | * ftruncate on an open file descriptor on a file with perm 000. | |
2392 | * We must trust the client to do permission checking - using "ACCESS" | |
2393 | * with NFSv3. | |
2394 | */ | |
8837abca | 2395 | if ((acc & NFSD_MAY_OWNER_OVERRIDE) && |
6fab8779 | 2396 | uid_eq(inode->i_uid, current_fsuid())) |
1da177e4 LT |
2397 | return 0; |
2398 | ||
8837abca | 2399 | /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */ |
47291baa CB |
2400 | err = inode_permission(&init_user_ns, inode, |
2401 | acc & (MAY_READ | MAY_WRITE | MAY_EXEC)); | |
1da177e4 LT |
2402 | |
2403 | /* Allow read access to binaries even when mode 111 */ | |
2404 | if (err == -EACCES && S_ISREG(inode->i_mode) && | |
a043226b BF |
2405 | (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) || |
2406 | acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC))) | |
47291baa | 2407 | err = inode_permission(&init_user_ns, inode, MAY_EXEC); |
1da177e4 LT |
2408 | |
2409 | return err? nfserrno(err) : 0; | |
2410 | } |