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