]>
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> | |
5a0e3ad6 | 29 | #include <linux/slab.h> |
7c0f6ba6 | 30 | #include <linux/uaccess.h> |
f501912a BM |
31 | #include <linux/exportfs.h> |
32 | #include <linux/writeback.h> | |
18032ca0 | 33 | #include <linux/security.h> |
9a74af21 BH |
34 | |
35 | #ifdef CONFIG_NFSD_V3 | |
36 | #include "xdr3.h" | |
37 | #endif /* CONFIG_NFSD_V3 */ | |
38 | ||
5be196e5 | 39 | #ifdef CONFIG_NFSD_V4 |
ffa0160a | 40 | #include "../internal.h" |
2ca72e17 BF |
41 | #include "acl.h" |
42 | #include "idmap.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 { | |
4335723e BF |
202 | /* |
203 | * In the nfsd4_open() case, this may be held across | |
204 | * subsequent open and delegation acquisition which may | |
205 | * need to take the child's i_mutex: | |
206 | */ | |
207 | fh_lock_nested(fhp, I_MUTEX_PARENT); | |
1da177e4 | 208 | dentry = lookup_one_len(name, dparent, len); |
6264d69d | 209 | host_err = PTR_ERR(dentry); |
1da177e4 LT |
210 | if (IS_ERR(dentry)) |
211 | goto out_nfserr; | |
82ead7fe | 212 | if (nfsd_mountpoint(dentry, exp)) { |
bbddca8e N |
213 | /* |
214 | * We don't need the i_mutex after all. It's | |
215 | * still possible we could open this (regular | |
216 | * files can be mountpoints too), but the | |
217 | * i_mutex is just there to prevent renames of | |
218 | * something that we might be about to delegate, | |
219 | * and a mountpoint won't be renamed: | |
220 | */ | |
221 | fh_unlock(fhp); | |
6264d69d | 222 | if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) { |
1da177e4 LT |
223 | dput(dentry); |
224 | goto out_nfserr; | |
225 | } | |
226 | } | |
227 | } | |
6c0a654d BF |
228 | *dentry_ret = dentry; |
229 | *exp_ret = exp; | |
230 | return 0; | |
231 | ||
232 | out_nfserr: | |
233 | exp_put(exp); | |
234 | return nfserrno(host_err); | |
235 | } | |
236 | ||
237 | /* | |
238 | * Look up one component of a pathname. | |
239 | * N.B. After this call _both_ fhp and resfh need an fh_put | |
240 | * | |
241 | * If the lookup would cross a mountpoint, and the mounted filesystem | |
242 | * is exported to the client with NFSEXP_NOHIDE, then the lookup is | |
243 | * accepted as it stands and the mounted directory is | |
244 | * returned. Otherwise the covered directory is returned. | |
245 | * NOTE: this mountpoint crossing is not supported properly by all | |
246 | * clients and is explicitly disallowed for NFSv3 | |
247 | * NeilBrown <neilb@cse.unsw.edu.au> | |
248 | */ | |
249 | __be32 | |
250 | nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name, | |
5a022fc8 | 251 | unsigned int len, struct svc_fh *resfh) |
6c0a654d BF |
252 | { |
253 | struct svc_export *exp; | |
254 | struct dentry *dentry; | |
255 | __be32 err; | |
256 | ||
29a78a3e BF |
257 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC); |
258 | if (err) | |
259 | return err; | |
6c0a654d BF |
260 | err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry); |
261 | if (err) | |
262 | return err; | |
32c1eb0c AA |
263 | err = check_nfsd_access(exp, rqstp); |
264 | if (err) | |
265 | goto out; | |
1da177e4 LT |
266 | /* |
267 | * Note: we compose the file handle now, but as the | |
268 | * dentry may be negative, it may need to be updated. | |
269 | */ | |
270 | err = fh_compose(resfh, exp, dentry, fhp); | |
2b0143b5 | 271 | if (!err && d_really_is_negative(dentry)) |
1da177e4 | 272 | err = nfserr_noent; |
32c1eb0c | 273 | out: |
1da177e4 | 274 | dput(dentry); |
1da177e4 LT |
275 | exp_put(exp); |
276 | return err; | |
1da177e4 LT |
277 | } |
278 | ||
f501912a BM |
279 | /* |
280 | * Commit metadata changes to stable storage. | |
281 | */ | |
282 | static int | |
57f64034 | 283 | commit_inode_metadata(struct inode *inode) |
f501912a | 284 | { |
f501912a | 285 | const struct export_operations *export_ops = inode->i_sb->s_export_op; |
f501912a | 286 | |
c3765016 CH |
287 | if (export_ops->commit_metadata) |
288 | return export_ops->commit_metadata(inode); | |
289 | return sync_inode_metadata(inode, 1); | |
f501912a | 290 | } |
6c0a654d | 291 | |
57f64034 TM |
292 | static int |
293 | commit_metadata(struct svc_fh *fhp) | |
294 | { | |
295 | struct inode *inode = d_inode(fhp->fh_dentry); | |
296 | ||
297 | if (!EX_ISSYNC(fhp->fh_export)) | |
298 | return 0; | |
299 | return commit_inode_metadata(inode); | |
300 | } | |
301 | ||
1da177e4 | 302 | /* |
818e5a22 CH |
303 | * Go over the attributes and take care of the small differences between |
304 | * NFS semantics and what Linux expects. | |
1da177e4 | 305 | */ |
818e5a22 CH |
306 | static void |
307 | nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap) | |
1da177e4 | 308 | { |
ca456252 | 309 | /* sanitize the mode change */ |
1da177e4 LT |
310 | if (iap->ia_valid & ATTR_MODE) { |
311 | iap->ia_mode &= S_IALLUGO; | |
dee3209d | 312 | iap->ia_mode |= (inode->i_mode & ~S_IALLUGO); |
ca456252 JL |
313 | } |
314 | ||
315 | /* Revoke setuid/setgid on chown */ | |
0953e620 | 316 | if (!S_ISDIR(inode->i_mode) && |
c4fa6d7c | 317 | ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) { |
ca456252 JL |
318 | iap->ia_valid |= ATTR_KILL_PRIV; |
319 | if (iap->ia_valid & ATTR_MODE) { | |
320 | /* we're setting mode too, just clear the s*id bits */ | |
8a0ce7d9 | 321 | iap->ia_mode &= ~S_ISUID; |
ca456252 JL |
322 | if (iap->ia_mode & S_IXGRP) |
323 | iap->ia_mode &= ~S_ISGID; | |
324 | } else { | |
325 | /* set ATTR_KILL_* bits and let VFS handle it */ | |
326 | iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID); | |
8a0ce7d9 | 327 | } |
1da177e4 | 328 | } |
818e5a22 CH |
329 | } |
330 | ||
0839ffb8 BF |
331 | static __be32 |
332 | nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp, | |
333 | struct iattr *iap) | |
334 | { | |
335 | struct inode *inode = d_inode(fhp->fh_dentry); | |
336 | int host_err; | |
337 | ||
338 | if (iap->ia_size < inode->i_size) { | |
339 | __be32 err; | |
340 | ||
341 | err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, | |
342 | NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE); | |
343 | if (err) | |
344 | return err; | |
345 | } | |
346 | ||
347 | host_err = get_write_access(inode); | |
348 | if (host_err) | |
349 | goto out_nfserrno; | |
350 | ||
351 | host_err = locks_verify_truncate(inode, NULL, iap->ia_size); | |
352 | if (host_err) | |
353 | goto out_put_write_access; | |
354 | return 0; | |
355 | ||
356 | out_put_write_access: | |
357 | put_write_access(inode); | |
358 | out_nfserrno: | |
359 | return nfserrno(host_err); | |
360 | } | |
361 | ||
818e5a22 CH |
362 | /* |
363 | * Set various file attributes. After this call fhp needs an fh_put. | |
364 | */ | |
365 | __be32 | |
366 | nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap, | |
2a1aa489 | 367 | int check_guard, time64_t guardtime) |
818e5a22 CH |
368 | { |
369 | struct dentry *dentry; | |
370 | struct inode *inode; | |
371 | int accmode = NFSD_MAY_SATTR; | |
372 | umode_t ftype = 0; | |
373 | __be32 err; | |
374 | int host_err; | |
9f67f189 | 375 | bool get_write_count; |
758e99fe | 376 | bool size_change = (iap->ia_valid & ATTR_SIZE); |
818e5a22 | 377 | |
255fbca6 | 378 | if (iap->ia_valid & ATTR_SIZE) { |
818e5a22 | 379 | accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE; |
818e5a22 | 380 | ftype = S_IFREG; |
255fbca6 | 381 | } |
382 | ||
383 | /* | |
384 | * If utimes(2) and friends are called with times not NULL, we should | |
385 | * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission | |
e977cc83 | 386 | * will return EACCES, when the caller's effective UID does not match |
255fbca6 | 387 | * the owner of the file, and the caller is not privileged. In this |
388 | * situation, we should return EPERM(notify_change will return this). | |
389 | */ | |
390 | if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) { | |
391 | accmode |= NFSD_MAY_OWNER_OVERRIDE; | |
392 | if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET))) | |
393 | accmode |= NFSD_MAY_WRITE; | |
394 | } | |
818e5a22 | 395 | |
9f67f189 BF |
396 | /* Callers that do fh_verify should do the fh_want_write: */ |
397 | get_write_count = !fhp->fh_dentry; | |
398 | ||
818e5a22 CH |
399 | /* Get inode */ |
400 | err = fh_verify(rqstp, fhp, ftype, accmode); | |
401 | if (err) | |
758e99fe | 402 | return err; |
9f67f189 BF |
403 | if (get_write_count) { |
404 | host_err = fh_want_write(fhp); | |
405 | if (host_err) | |
758e99fe | 406 | goto out; |
9f67f189 | 407 | } |
818e5a22 CH |
408 | |
409 | dentry = fhp->fh_dentry; | |
2b0143b5 | 410 | inode = d_inode(dentry); |
818e5a22 CH |
411 | |
412 | /* Ignore any mode updates on symlinks */ | |
413 | if (S_ISLNK(inode->i_mode)) | |
414 | iap->ia_valid &= ~ATTR_MODE; | |
415 | ||
416 | if (!iap->ia_valid) | |
758e99fe | 417 | return 0; |
1da177e4 | 418 | |
818e5a22 CH |
419 | nfsd_sanitize_attrs(inode, iap); |
420 | ||
758e99fe CH |
421 | if (check_guard && guardtime != inode->i_ctime.tv_sec) |
422 | return nfserr_notsync; | |
423 | ||
818e5a22 CH |
424 | /* |
425 | * The size case is special, it changes the file in addition to the | |
783112f7 CH |
426 | * attributes, and file systems don't expect it to be mixed with |
427 | * "random" attribute changes. We thus split out the size change | |
428 | * into a separate call to ->setattr, and do the rest as a separate | |
429 | * setattr call. | |
818e5a22 | 430 | */ |
758e99fe | 431 | if (size_change) { |
0839ffb8 BF |
432 | err = nfsd_get_write_access(rqstp, fhp, iap); |
433 | if (err) | |
758e99fe | 434 | return err; |
783112f7 | 435 | } |
f0c63124 | 436 | |
783112f7 CH |
437 | fh_lock(fhp); |
438 | if (size_change) { | |
f0c63124 | 439 | /* |
0839ffb8 BF |
440 | * RFC5661, Section 18.30.4: |
441 | * Changing the size of a file with SETATTR indirectly | |
442 | * changes the time_modify and change attributes. | |
443 | * | |
444 | * (and similar for the older RFCs) | |
f0c63124 | 445 | */ |
783112f7 CH |
446 | struct iattr size_attr = { |
447 | .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME, | |
448 | .ia_size = iap->ia_size, | |
449 | }; | |
450 | ||
451 | host_err = notify_change(dentry, &size_attr, NULL); | |
452 | if (host_err) | |
453 | goto out_unlock; | |
454 | iap->ia_valid &= ~ATTR_SIZE; | |
455 | ||
456 | /* | |
457 | * Avoid the additional setattr call below if the only other | |
458 | * attribute that the client sends is the mtime, as we update | |
459 | * it as part of the size change above. | |
460 | */ | |
461 | if ((iap->ia_valid & ~ATTR_MTIME) == 0) | |
462 | goto out_unlock; | |
1da177e4 | 463 | } |
987da479 | 464 | |
41f53350 | 465 | iap->ia_valid |= ATTR_CTIME; |
987da479 | 466 | host_err = notify_change(dentry, iap, NULL); |
987da479 | 467 | |
783112f7 CH |
468 | out_unlock: |
469 | fh_unlock(fhp); | |
0839ffb8 BF |
470 | if (size_change) |
471 | put_write_access(inode); | |
0839ffb8 | 472 | out: |
758e99fe CH |
473 | if (!host_err) |
474 | host_err = commit_metadata(fhp); | |
475 | return nfserrno(host_err); | |
1da177e4 LT |
476 | } |
477 | ||
5be196e5 | 478 | #if defined(CONFIG_NFSD_V4) |
9b4146e8 CL |
479 | /* |
480 | * NFS junction information is stored in an extended attribute. | |
481 | */ | |
482 | #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs" | |
483 | ||
484 | /** | |
485 | * nfsd4_is_junction - Test if an object could be an NFS junction | |
486 | * | |
487 | * @dentry: object to test | |
488 | * | |
489 | * Returns 1 if "dentry" appears to contain NFS junction information. | |
490 | * Otherwise 0 is returned. | |
491 | */ | |
11fcee02 TM |
492 | int nfsd4_is_junction(struct dentry *dentry) |
493 | { | |
2b0143b5 | 494 | struct inode *inode = d_inode(dentry); |
11fcee02 TM |
495 | |
496 | if (inode == NULL) | |
497 | return 0; | |
498 | if (inode->i_mode & S_IXUGO) | |
499 | return 0; | |
500 | if (!(inode->i_mode & S_ISVTX)) | |
501 | return 0; | |
9b4146e8 | 502 | if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0) |
11fcee02 TM |
503 | return 0; |
504 | return 1; | |
505 | } | |
18032ca0 DQ |
506 | #ifdef CONFIG_NFSD_V4_SECURITY_LABEL |
507 | __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp, | |
508 | struct xdr_netobj *label) | |
509 | { | |
510 | __be32 error; | |
511 | int host_error; | |
512 | struct dentry *dentry; | |
513 | ||
514 | error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR); | |
515 | if (error) | |
516 | return error; | |
517 | ||
518 | dentry = fhp->fh_dentry; | |
519 | ||
5955102c | 520 | inode_lock(d_inode(dentry)); |
18032ca0 | 521 | host_error = security_inode_setsecctx(dentry, label->data, label->len); |
5955102c | 522 | inode_unlock(d_inode(dentry)); |
18032ca0 DQ |
523 | return nfserrno(host_error); |
524 | } | |
525 | #else | |
526 | __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp, | |
527 | struct xdr_netobj *label) | |
528 | { | |
529 | return nfserr_notsupp; | |
530 | } | |
531 | #endif | |
532 | ||
b66ae6dd TM |
533 | __be32 nfsd4_clone_file_range(struct nfsd_file *nf_src, u64 src_pos, |
534 | struct nfsd_file *nf_dst, u64 dst_pos, u64 count, bool sync) | |
ffa0160a | 535 | { |
b66ae6dd TM |
536 | struct file *src = nf_src->nf_file; |
537 | struct file *dst = nf_dst->nf_file; | |
42ec3d4c | 538 | loff_t cloned; |
1b28d756 | 539 | __be32 ret = 0; |
42ec3d4c | 540 | |
1b28d756 | 541 | down_write(&nf_dst->nf_rwsem); |
452ce659 | 542 | cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0); |
1b28d756 TM |
543 | if (cloned < 0) { |
544 | ret = nfserrno(cloned); | |
545 | goto out_err; | |
546 | } | |
547 | if (count && cloned != count) { | |
548 | ret = nfserrno(-EINVAL); | |
549 | goto out_err; | |
550 | } | |
a25e3726 TM |
551 | if (sync) { |
552 | loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX; | |
553 | int status = vfs_fsync_range(dst, dst_pos, dst_end, 0); | |
57f64034 TM |
554 | |
555 | if (!status) | |
556 | status = commit_inode_metadata(file_inode(src)); | |
1b28d756 TM |
557 | if (status < 0) { |
558 | nfsd_reset_boot_verifier(net_generic(nf_dst->nf_net, | |
559 | nfsd_net_id)); | |
560 | ret = nfserrno(status); | |
561 | } | |
a25e3726 | 562 | } |
1b28d756 TM |
563 | out_err: |
564 | up_write(&nf_dst->nf_rwsem); | |
565 | return ret; | |
ffa0160a CH |
566 | } |
567 | ||
29ae7f9d AS |
568 | ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst, |
569 | u64 dst_pos, u64 count) | |
570 | { | |
571 | ||
572 | /* | |
573 | * Limit copy to 4MB to prevent indefinitely blocking an nfsd | |
574 | * thread and client rpc slot. The choice of 4MB is somewhat | |
575 | * arbitrary. We might instead base this on r/wsize, or make it | |
576 | * tunable, or use a time instead of a byte limit, or implement | |
577 | * asynchronous copy. In theory a client could also recognize a | |
578 | * limit like this and pipeline multiple COPY requests. | |
579 | */ | |
580 | count = min_t(u64, count, 1 << 22); | |
581 | return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0); | |
582 | } | |
583 | ||
95d871f0 AS |
584 | __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp, |
585 | struct file *file, loff_t offset, loff_t len, | |
586 | int flags) | |
587 | { | |
95d871f0 AS |
588 | int error; |
589 | ||
590 | if (!S_ISREG(file_inode(file)->i_mode)) | |
591 | return nfserr_inval; | |
592 | ||
95d871f0 AS |
593 | error = vfs_fallocate(file, flags, offset, len); |
594 | if (!error) | |
595 | error = commit_metadata(fhp); | |
596 | ||
597 | return nfserrno(error); | |
598 | } | |
6a85d6c7 | 599 | #endif /* defined(CONFIG_NFSD_V4) */ |
1da177e4 LT |
600 | |
601 | #ifdef CONFIG_NFSD_V3 | |
602 | /* | |
603 | * Check server access rights to a file system object | |
604 | */ | |
605 | struct accessmap { | |
606 | u32 access; | |
607 | int how; | |
608 | }; | |
609 | static struct accessmap nfs3_regaccess[] = { | |
8837abca MS |
610 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
611 | { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC }, | |
612 | { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC }, | |
613 | { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE }, | |
1da177e4 LT |
614 | |
615 | { 0, 0 } | |
616 | }; | |
617 | ||
618 | static struct accessmap nfs3_diraccess[] = { | |
8837abca MS |
619 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
620 | { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC }, | |
621 | { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC}, | |
622 | { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE }, | |
623 | { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE }, | |
1da177e4 LT |
624 | |
625 | { 0, 0 } | |
626 | }; | |
627 | ||
628 | static struct accessmap nfs3_anyaccess[] = { | |
629 | /* Some clients - Solaris 2.6 at least, make an access call | |
630 | * to the server to check for access for things like /dev/null | |
631 | * (which really, the server doesn't care about). So | |
632 | * We provide simple access checking for them, looking | |
633 | * mainly at mode bits, and we make sure to ignore read-only | |
634 | * filesystem checks | |
635 | */ | |
8837abca MS |
636 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
637 | { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC }, | |
638 | { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS }, | |
639 | { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS }, | |
1da177e4 LT |
640 | |
641 | { 0, 0 } | |
642 | }; | |
643 | ||
6264d69d | 644 | __be32 |
1da177e4 LT |
645 | nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported) |
646 | { | |
647 | struct accessmap *map; | |
648 | struct svc_export *export; | |
649 | struct dentry *dentry; | |
650 | u32 query, result = 0, sresult = 0; | |
6264d69d | 651 | __be32 error; |
1da177e4 | 652 | |
8837abca | 653 | error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP); |
1da177e4 LT |
654 | if (error) |
655 | goto out; | |
656 | ||
657 | export = fhp->fh_export; | |
658 | dentry = fhp->fh_dentry; | |
659 | ||
e36cb0b8 | 660 | if (d_is_reg(dentry)) |
1da177e4 | 661 | map = nfs3_regaccess; |
e36cb0b8 | 662 | else if (d_is_dir(dentry)) |
1da177e4 LT |
663 | map = nfs3_diraccess; |
664 | else | |
665 | map = nfs3_anyaccess; | |
666 | ||
667 | ||
668 | query = *access; | |
669 | for (; map->access; map++) { | |
670 | if (map->access & query) { | |
6264d69d | 671 | __be32 err2; |
1da177e4 LT |
672 | |
673 | sresult |= map->access; | |
674 | ||
0ec757df | 675 | err2 = nfsd_permission(rqstp, export, dentry, map->how); |
1da177e4 LT |
676 | switch (err2) { |
677 | case nfs_ok: | |
678 | result |= map->access; | |
679 | break; | |
680 | ||
681 | /* the following error codes just mean the access was not allowed, | |
682 | * rather than an error occurred */ | |
683 | case nfserr_rofs: | |
684 | case nfserr_acces: | |
685 | case nfserr_perm: | |
686 | /* simply don't "or" in the access bit. */ | |
687 | break; | |
688 | default: | |
689 | error = err2; | |
690 | goto out; | |
691 | } | |
692 | } | |
693 | } | |
694 | *access = result; | |
695 | if (supported) | |
696 | *supported = sresult; | |
697 | ||
698 | out: | |
699 | return error; | |
700 | } | |
701 | #endif /* CONFIG_NFSD_V3 */ | |
702 | ||
65294c1f | 703 | int nfsd_open_break_lease(struct inode *inode, int access) |
105f4622 BF |
704 | { |
705 | unsigned int mode; | |
1da177e4 | 706 | |
105f4622 BF |
707 | if (access & NFSD_MAY_NOT_BREAK_LEASE) |
708 | return 0; | |
709 | mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY; | |
710 | return break_lease(inode, mode | O_NONBLOCK); | |
711 | } | |
1da177e4 LT |
712 | |
713 | /* | |
714 | * Open an existing file or directory. | |
999448a8 BS |
715 | * The may_flags argument indicates the type of open (read/write/lock) |
716 | * and additional flags. | |
1da177e4 LT |
717 | * N.B. After this call fhp needs an fh_put |
718 | */ | |
65294c1f JL |
719 | static __be32 |
720 | __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, | |
999448a8 | 721 | int may_flags, struct file **filp) |
1da177e4 | 722 | { |
765927b2 | 723 | struct path path; |
1da177e4 | 724 | struct inode *inode; |
8519f994 | 725 | struct file *file; |
6264d69d AV |
726 | int flags = O_RDONLY|O_LARGEFILE; |
727 | __be32 err; | |
91885258 | 728 | int host_err = 0; |
1da177e4 | 729 | |
765927b2 AV |
730 | path.mnt = fhp->fh_export->ex_path.mnt; |
731 | path.dentry = fhp->fh_dentry; | |
2b0143b5 | 732 | inode = d_inode(path.dentry); |
1da177e4 LT |
733 | |
734 | /* Disallow write access to files with the append-only bit set | |
735 | * or any access when mandatory locking enabled | |
736 | */ | |
737 | err = nfserr_perm; | |
999448a8 | 738 | if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE)) |
1da177e4 | 739 | goto out; |
5e7fc436 BF |
740 | /* |
741 | * We must ignore files (but only files) which might have mandatory | |
742 | * locks on them because there is no way to know if the accesser has | |
743 | * the lock. | |
744 | */ | |
745 | if (S_ISREG((inode)->i_mode) && mandatory_lock(inode)) | |
1da177e4 LT |
746 | goto out; |
747 | ||
748 | if (!inode->i_fop) | |
749 | goto out; | |
750 | ||
999448a8 | 751 | host_err = nfsd_open_break_lease(inode, may_flags); |
6264d69d | 752 | if (host_err) /* NOMEM or WOULDBLOCK */ |
1da177e4 LT |
753 | goto out_nfserr; |
754 | ||
999448a8 BS |
755 | if (may_flags & NFSD_MAY_WRITE) { |
756 | if (may_flags & NFSD_MAY_READ) | |
9ecb6a08 BF |
757 | flags = O_RDWR|O_LARGEFILE; |
758 | else | |
759 | flags = O_WRONLY|O_LARGEFILE; | |
1da177e4 | 760 | } |
999448a8 | 761 | |
8519f994 KM |
762 | file = dentry_open(&path, flags, current_cred()); |
763 | if (IS_ERR(file)) { | |
764 | host_err = PTR_ERR(file); | |
765 | goto out_nfserr; | |
766 | } | |
767 | ||
6035a27b | 768 | host_err = ima_file_check(file, may_flags); |
8519f994 | 769 | if (host_err) { |
fd891454 | 770 | fput(file); |
8519f994 | 771 | goto out_nfserr; |
06effdbb BS |
772 | } |
773 | ||
8519f994 KM |
774 | if (may_flags & NFSD_MAY_64BIT_COOKIE) |
775 | file->f_mode |= FMODE_64BITHASH; | |
776 | else | |
777 | file->f_mode |= FMODE_32BITHASH; | |
778 | ||
779 | *filp = file; | |
1da177e4 | 780 | out_nfserr: |
6264d69d | 781 | err = nfserrno(host_err); |
1da177e4 | 782 | out: |
65294c1f JL |
783 | return err; |
784 | } | |
785 | ||
786 | __be32 | |
787 | nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, | |
788 | int may_flags, struct file **filp) | |
789 | { | |
790 | __be32 err; | |
791 | ||
792 | validate_process_creds(); | |
793 | /* | |
794 | * If we get here, then the client has already done an "open", | |
795 | * and (hopefully) checked permission - so allow OWNER_OVERRIDE | |
796 | * in case a chmod has now revoked permission. | |
797 | * | |
798 | * Arguably we should also allow the owner override for | |
799 | * directories, but we never have and it doesn't seem to have | |
800 | * caused anyone a problem. If we were to change this, note | |
801 | * also that our filldir callbacks would need a variant of | |
802 | * lookup_one_len that doesn't check permissions. | |
803 | */ | |
804 | if (type == S_IFREG) | |
805 | may_flags |= NFSD_MAY_OWNER_OVERRIDE; | |
806 | err = fh_verify(rqstp, fhp, type, may_flags); | |
807 | if (!err) | |
808 | err = __nfsd_open(rqstp, fhp, type, may_flags, filp); | |
809 | validate_process_creds(); | |
810 | return err; | |
811 | } | |
812 | ||
813 | __be32 | |
814 | nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, | |
815 | int may_flags, struct file **filp) | |
816 | { | |
817 | __be32 err; | |
818 | ||
819 | validate_process_creds(); | |
820 | err = __nfsd_open(rqstp, fhp, type, may_flags, filp); | |
e0e81739 | 821 | validate_process_creds(); |
1da177e4 LT |
822 | return err; |
823 | } | |
824 | ||
1da177e4 | 825 | /* |
cf8208d0 JA |
826 | * Grab and keep cached pages associated with a file in the svc_rqst |
827 | * so that they can be passed to the network sendmsg/sendpage routines | |
828 | * directly. They will be released after the sending has completed. | |
1da177e4 LT |
829 | */ |
830 | static int | |
cf8208d0 JA |
831 | nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, |
832 | struct splice_desc *sd) | |
1da177e4 | 833 | { |
cf8208d0 | 834 | struct svc_rqst *rqstp = sd->u.data; |
afc59400 | 835 | struct page **pp = rqstp->rq_next_page; |
cf8208d0 JA |
836 | struct page *page = buf->page; |
837 | size_t size; | |
cf8208d0 JA |
838 | |
839 | size = sd->len; | |
1da177e4 LT |
840 | |
841 | if (rqstp->rq_res.page_len == 0) { | |
842 | get_page(page); | |
afc59400 BF |
843 | put_page(*rqstp->rq_next_page); |
844 | *(rqstp->rq_next_page++) = page; | |
cf8208d0 | 845 | rqstp->rq_res.page_base = buf->offset; |
1da177e4 | 846 | rqstp->rq_res.page_len = size; |
44524359 | 847 | } else if (page != pp[-1]) { |
1da177e4 | 848 | get_page(page); |
afc59400 BF |
849 | if (*rqstp->rq_next_page) |
850 | put_page(*rqstp->rq_next_page); | |
851 | *(rqstp->rq_next_page++) = page; | |
1da177e4 | 852 | rqstp->rq_res.page_len += size; |
44524359 | 853 | } else |
1da177e4 | 854 | rqstp->rq_res.page_len += size; |
1da177e4 | 855 | |
1da177e4 LT |
856 | return size; |
857 | } | |
858 | ||
cf8208d0 JA |
859 | static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe, |
860 | struct splice_desc *sd) | |
861 | { | |
862 | return __splice_from_pipe(pipe, sd, nfsd_splice_actor); | |
863 | } | |
864 | ||
83a63072 TM |
865 | static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len, |
866 | size_t expected) | |
867 | { | |
868 | if (expected != 0 && len == 0) | |
869 | return 1; | |
870 | if (offset+len >= i_size_read(file_inode(file))) | |
871 | return 1; | |
872 | return 0; | |
873 | } | |
874 | ||
87c5942e CL |
875 | static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp, |
876 | struct file *file, loff_t offset, | |
83a63072 | 877 | unsigned long *count, u32 *eof, ssize_t host_err) |
1da177e4 | 878 | { |
6264d69d AV |
879 | if (host_err >= 0) { |
880 | nfsdstats.io_read += host_err; | |
83a63072 | 881 | *eof = nfsd_eof_on_read(file, offset, host_err, *count); |
6264d69d | 882 | *count = host_err; |
2a12a9d7 | 883 | fsnotify_access(file); |
87c5942e | 884 | trace_nfsd_read_io_done(rqstp, fhp, offset, *count); |
dc97618d | 885 | return 0; |
87c5942e CL |
886 | } else { |
887 | trace_nfsd_read_err(rqstp, fhp, offset, host_err); | |
dc97618d | 888 | return nfserrno(host_err); |
87c5942e | 889 | } |
dc97618d BF |
890 | } |
891 | ||
87c5942e | 892 | __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp, |
83a63072 TM |
893 | struct file *file, loff_t offset, unsigned long *count, |
894 | u32 *eof) | |
dc97618d BF |
895 | { |
896 | struct splice_desc sd = { | |
897 | .len = 0, | |
898 | .total_len = *count, | |
899 | .pos = offset, | |
900 | .u.data = rqstp, | |
901 | }; | |
83a63072 | 902 | ssize_t host_err; |
dc97618d | 903 | |
87c5942e | 904 | trace_nfsd_read_splice(rqstp, fhp, offset, *count); |
dc97618d BF |
905 | rqstp->rq_next_page = rqstp->rq_respages + 1; |
906 | host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor); | |
83a63072 | 907 | return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err); |
dc97618d BF |
908 | } |
909 | ||
87c5942e CL |
910 | __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp, |
911 | struct file *file, loff_t offset, | |
83a63072 TM |
912 | struct kvec *vec, int vlen, unsigned long *count, |
913 | u32 *eof) | |
dc97618d | 914 | { |
73da852e | 915 | struct iov_iter iter; |
83a63072 TM |
916 | loff_t ppos = offset; |
917 | ssize_t host_err; | |
dc97618d | 918 | |
87c5942e | 919 | trace_nfsd_read_vector(rqstp, fhp, offset, *count); |
aa563d7b | 920 | iov_iter_kvec(&iter, READ, vec, vlen, *count); |
83a63072 TM |
921 | host_err = vfs_iter_read(file, &iter, &ppos, 0); |
922 | return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err); | |
1da177e4 LT |
923 | } |
924 | ||
d911df7b BF |
925 | /* |
926 | * Gathered writes: If another process is currently writing to the file, | |
927 | * there's a high chance this is another nfsd (triggered by a bulk write | |
928 | * from a client's biod). Rather than syncing the file with each write | |
929 | * request, we sleep for 10 msec. | |
930 | * | |
931 | * I don't know if this roughly approximates C. Juszak's idea of | |
932 | * gathered writes, but it's a nice and simple solution (IMHO), and it | |
933 | * seems to work:-) | |
934 | * | |
935 | * Note: we do this only in the NFSv2 case, since v3 and higher have a | |
936 | * better tool (separate unstable writes and commits) for solving this | |
937 | * problem. | |
938 | */ | |
939 | static int wait_for_concurrent_writes(struct file *file) | |
940 | { | |
496ad9aa | 941 | struct inode *inode = file_inode(file); |
d911df7b BF |
942 | static ino_t last_ino; |
943 | static dev_t last_dev; | |
944 | int err = 0; | |
945 | ||
946 | if (atomic_read(&inode->i_writecount) > 1 | |
947 | || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) { | |
948 | dprintk("nfsd: write defer %d\n", task_pid_nr(current)); | |
949 | msleep(10); | |
950 | dprintk("nfsd: write resume %d\n", task_pid_nr(current)); | |
951 | } | |
952 | ||
953 | if (inode->i_state & I_DIRTY) { | |
954 | dprintk("nfsd: write sync %d\n", task_pid_nr(current)); | |
8018ab05 | 955 | err = vfs_fsync(file, 0); |
d911df7b BF |
956 | } |
957 | last_ino = inode->i_ino; | |
958 | last_dev = inode->i_sb->s_dev; | |
959 | return err; | |
960 | } | |
961 | ||
af90f707 | 962 | __be32 |
16f8f894 | 963 | nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf, |
1da177e4 | 964 | loff_t offset, struct kvec *vec, int vlen, |
54bbb7d2 | 965 | unsigned long *cnt, int stable) |
1da177e4 | 966 | { |
16f8f894 | 967 | struct file *file = nf->nf_file; |
1da177e4 | 968 | struct svc_export *exp; |
73da852e | 969 | struct iov_iter iter; |
d890be15 | 970 | __be32 nfserr; |
6264d69d | 971 | int host_err; |
48e03bc5 | 972 | int use_wgather; |
e49dbbf3 | 973 | loff_t pos = offset; |
8658452e | 974 | unsigned int pflags = current->flags; |
ddef7ed2 | 975 | rwf_t flags = 0; |
8658452e | 976 | |
d890be15 CL |
977 | trace_nfsd_write_opened(rqstp, fhp, offset, *cnt); |
978 | ||
7501cc2b | 979 | if (test_bit(RQ_LOCAL, &rqstp->rq_flags)) |
8658452e N |
980 | /* |
981 | * We want less throttling in balance_dirty_pages() | |
982 | * and shrink_inactive_list() so that nfs to | |
983 | * localhost doesn't cause nfsd to lock up due to all | |
984 | * the client's dirty pages or its congested queue. | |
985 | */ | |
986 | current->flags |= PF_LESS_THROTTLE; | |
1da177e4 | 987 | |
865d50b2 | 988 | exp = fhp->fh_export; |
48e03bc5 | 989 | use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp); |
1da177e4 | 990 | |
1da177e4 | 991 | if (!EX_ISSYNC(exp)) |
54bbb7d2 | 992 | stable = NFS_UNSTABLE; |
1da177e4 | 993 | |
24368aad CH |
994 | if (stable && !use_wgather) |
995 | flags |= RWF_SYNC; | |
996 | ||
aa563d7b | 997 | iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt); |
5011af4c TM |
998 | if (flags & RWF_SYNC) { |
999 | down_write(&nf->nf_rwsem); | |
1000 | host_err = vfs_iter_write(file, &iter, &pos, flags); | |
1001 | if (host_err < 0) | |
1002 | nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp), | |
1003 | nfsd_net_id)); | |
1004 | up_write(&nf->nf_rwsem); | |
1005 | } else { | |
1006 | down_read(&nf->nf_rwsem); | |
1007 | host_err = vfs_iter_write(file, &iter, &pos, flags); | |
1008 | up_read(&nf->nf_rwsem); | |
1009 | } | |
7bf94c6b TM |
1010 | if (host_err < 0) { |
1011 | nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp), | |
1012 | nfsd_net_id)); | |
e4636d53 | 1013 | goto out_nfserr; |
7bf94c6b | 1014 | } |
09a80f2a | 1015 | *cnt = host_err; |
d890be15 | 1016 | nfsdstats.io_write += *cnt; |
2a12a9d7 | 1017 | fsnotify_modify(file); |
1da177e4 | 1018 | |
bbf2f098 | 1019 | if (stable && use_wgather) { |
24368aad | 1020 | host_err = wait_for_concurrent_writes(file); |
bbf2f098 TM |
1021 | if (host_err < 0) |
1022 | nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp), | |
1023 | nfsd_net_id)); | |
1024 | } | |
1da177e4 | 1025 | |
e4636d53 | 1026 | out_nfserr: |
d890be15 CL |
1027 | if (host_err >= 0) { |
1028 | trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt); | |
1029 | nfserr = nfs_ok; | |
1030 | } else { | |
1031 | trace_nfsd_write_err(rqstp, fhp, offset, host_err); | |
1032 | nfserr = nfserrno(host_err); | |
1033 | } | |
7501cc2b | 1034 | if (test_bit(RQ_LOCAL, &rqstp->rq_flags)) |
717a94b5 | 1035 | current_restore_flags(pflags, PF_LESS_THROTTLE); |
d890be15 | 1036 | return nfserr; |
1da177e4 LT |
1037 | } |
1038 | ||
dc97618d BF |
1039 | /* |
1040 | * Read data from a file. count must contain the requested read count | |
1041 | * on entry. On return, *count contains the number of bytes actually read. | |
1042 | * N.B. After this call fhp needs an fh_put | |
1043 | */ | |
1044 | __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, | |
83a63072 TM |
1045 | loff_t offset, struct kvec *vec, int vlen, unsigned long *count, |
1046 | u32 *eof) | |
dc97618d | 1047 | { |
48cd7b51 | 1048 | struct nfsd_file *nf; |
dc97618d | 1049 | struct file *file; |
dc97618d BF |
1050 | __be32 err; |
1051 | ||
f394b62b | 1052 | trace_nfsd_read_start(rqstp, fhp, offset, *count); |
48cd7b51 | 1053 | err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf); |
dc97618d BF |
1054 | if (err) |
1055 | return err; | |
1056 | ||
48cd7b51 | 1057 | file = nf->nf_file; |
a4058c5b | 1058 | if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags)) |
83a63072 | 1059 | err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof); |
a4058c5b | 1060 | else |
83a63072 | 1061 | err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof); |
6e8b50d1 | 1062 | |
48cd7b51 | 1063 | nfsd_file_put(nf); |
dc97618d | 1064 | |
f394b62b | 1065 | trace_nfsd_read_done(rqstp, fhp, offset, *count); |
6e8b50d1 | 1066 | |
fa0a2126 BF |
1067 | return err; |
1068 | } | |
1069 | ||
1da177e4 LT |
1070 | /* |
1071 | * Write data to a file. | |
1072 | * The stable flag requests synchronous writes. | |
1073 | * N.B. After this call fhp needs an fh_put | |
1074 | */ | |
6264d69d | 1075 | __be32 |
52e380e0 KM |
1076 | nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset, |
1077 | struct kvec *vec, int vlen, unsigned long *cnt, int stable) | |
1da177e4 | 1078 | { |
b4935239 JL |
1079 | struct nfsd_file *nf; |
1080 | __be32 err; | |
1da177e4 | 1081 | |
f394b62b | 1082 | trace_nfsd_write_start(rqstp, fhp, offset, *cnt); |
6e8b50d1 | 1083 | |
b4935239 | 1084 | err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf); |
52e380e0 KM |
1085 | if (err) |
1086 | goto out; | |
1087 | ||
16f8f894 | 1088 | err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec, |
b4935239 JL |
1089 | vlen, cnt, stable); |
1090 | nfsd_file_put(nf); | |
1da177e4 | 1091 | out: |
f394b62b | 1092 | trace_nfsd_write_done(rqstp, fhp, offset, *cnt); |
1da177e4 LT |
1093 | return err; |
1094 | } | |
1095 | ||
1096 | #ifdef CONFIG_NFSD_V3 | |
1097 | /* | |
1098 | * Commit all pending writes to stable storage. | |
aa696a6f TM |
1099 | * |
1100 | * Note: we only guarantee that data that lies within the range specified | |
1101 | * by the 'offset' and 'count' parameters will be synced. | |
1da177e4 LT |
1102 | * |
1103 | * Unfortunately we cannot lock the file to make sure we return full WCC | |
1104 | * data to the client, as locking happens lower down in the filesystem. | |
1105 | */ | |
6264d69d | 1106 | __be32 |
1da177e4 | 1107 | nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp, |
524ff1af | 1108 | loff_t offset, unsigned long count, __be32 *verf) |
1da177e4 | 1109 | { |
5920afa3 JL |
1110 | struct nfsd_file *nf; |
1111 | loff_t end = LLONG_MAX; | |
1112 | __be32 err = nfserr_inval; | |
1da177e4 | 1113 | |
aa696a6f TM |
1114 | if (offset < 0) |
1115 | goto out; | |
1116 | if (count != 0) { | |
1117 | end = offset + (loff_t)count - 1; | |
1118 | if (end < offset) | |
1119 | goto out; | |
1120 | } | |
1da177e4 | 1121 | |
5920afa3 JL |
1122 | err = nfsd_file_acquire(rqstp, fhp, |
1123 | NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &nf); | |
8837abca | 1124 | if (err) |
aa696a6f | 1125 | goto out; |
1da177e4 | 1126 | if (EX_ISSYNC(fhp->fh_export)) { |
5011af4c | 1127 | int err2; |
aa696a6f | 1128 | |
5011af4c TM |
1129 | down_write(&nf->nf_rwsem); |
1130 | err2 = vfs_fsync_range(nf->nf_file, offset, end, 0); | |
bbf2f098 TM |
1131 | switch (err2) { |
1132 | case 0: | |
524ff1af TM |
1133 | nfsd_copy_boot_verifier(verf, net_generic(nf->nf_net, |
1134 | nfsd_net_id)); | |
bbf2f098 TM |
1135 | break; |
1136 | case -EINVAL: | |
1da177e4 | 1137 | err = nfserr_notsupp; |
bbf2f098 TM |
1138 | break; |
1139 | default: | |
1140 | err = nfserrno(err2); | |
1141 | nfsd_reset_boot_verifier(net_generic(nf->nf_net, | |
1142 | nfsd_net_id)); | |
1143 | } | |
5011af4c | 1144 | up_write(&nf->nf_rwsem); |
524ff1af TM |
1145 | } else |
1146 | nfsd_copy_boot_verifier(verf, net_generic(nf->nf_net, | |
1147 | nfsd_net_id)); | |
1da177e4 | 1148 | |
5920afa3 | 1149 | nfsd_file_put(nf); |
aa696a6f | 1150 | out: |
1da177e4 LT |
1151 | return err; |
1152 | } | |
1153 | #endif /* CONFIG_NFSD_V3 */ | |
1154 | ||
f2b0dee2 | 1155 | static __be32 |
5c002b3b BF |
1156 | nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp, |
1157 | struct iattr *iap) | |
1158 | { | |
1159 | /* | |
1160 | * Mode has already been set earlier in create: | |
1161 | */ | |
1162 | iap->ia_valid &= ~ATTR_MODE; | |
1163 | /* | |
1164 | * Setting uid/gid works only for root. Irix appears to | |
1165 | * send along the gid on create when it tries to implement | |
1166 | * setgid directories via NFS: | |
1167 | */ | |
6fab8779 | 1168 | if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) |
5c002b3b BF |
1169 | iap->ia_valid &= ~(ATTR_UID|ATTR_GID); |
1170 | if (iap->ia_valid) | |
2a1aa489 | 1171 | return nfsd_setattr(rqstp, resfhp, iap, 0, (time64_t)0); |
0f3a24b4 | 1172 | /* Callers expect file metadata to be committed here */ |
722b620d | 1173 | return nfserrno(commit_metadata(resfhp)); |
5c002b3b BF |
1174 | } |
1175 | ||
4ac35c2f | 1176 | /* HPUX client sometimes creates a file in mode 000, and sets size to 0. |
1177 | * setting size to 0 may fail for some specific file systems by the permission | |
1178 | * checking which requires WRITE permission but the mode is 000. | |
1179 | * we ignore the resizing(to 0) on the just new created file, since the size is | |
1180 | * 0 after file created. | |
1181 | * | |
1182 | * call this only after vfs_create() is called. | |
1183 | * */ | |
1184 | static void | |
1185 | nfsd_check_ignore_resizing(struct iattr *iap) | |
1186 | { | |
1187 | if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0)) | |
1188 | iap->ia_valid &= ~ATTR_SIZE; | |
1189 | } | |
1190 | ||
b44061d0 | 1191 | /* The parent directory should already be locked: */ |
6264d69d | 1192 | __be32 |
b44061d0 | 1193 | nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp, |
1da177e4 LT |
1194 | char *fname, int flen, struct iattr *iap, |
1195 | int type, dev_t rdev, struct svc_fh *resfhp) | |
1196 | { | |
2b118859 | 1197 | struct dentry *dentry, *dchild; |
1da177e4 | 1198 | struct inode *dirp; |
6264d69d | 1199 | __be32 err; |
5c002b3b | 1200 | __be32 err2; |
6264d69d | 1201 | int host_err; |
1da177e4 | 1202 | |
1da177e4 | 1203 | dentry = fhp->fh_dentry; |
2b0143b5 | 1204 | dirp = d_inode(dentry); |
1da177e4 | 1205 | |
b44061d0 BF |
1206 | dchild = dget(resfhp->fh_dentry); |
1207 | if (!fhp->fh_locked) { | |
1208 | WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n", | |
a6a9f18f | 1209 | dentry); |
b44061d0 BF |
1210 | err = nfserr_io; |
1211 | goto out; | |
1da177e4 | 1212 | } |
1da177e4 | 1213 | |
7eed34f1 OD |
1214 | err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE); |
1215 | if (err) | |
1216 | goto out; | |
1217 | ||
1da177e4 LT |
1218 | if (!(iap->ia_valid & ATTR_MODE)) |
1219 | iap->ia_mode = 0; | |
1220 | iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type; | |
1221 | ||
088406bc | 1222 | err = 0; |
4a55c101 | 1223 | host_err = 0; |
1da177e4 LT |
1224 | switch (type) { |
1225 | case S_IFREG: | |
312b63fb | 1226 | host_err = vfs_create(dirp, dchild, iap->ia_mode, true); |
4ac35c2f | 1227 | if (!host_err) |
1228 | nfsd_check_ignore_resizing(iap); | |
1da177e4 LT |
1229 | break; |
1230 | case S_IFDIR: | |
6264d69d | 1231 | host_err = vfs_mkdir(dirp, dchild, iap->ia_mode); |
3819bb0d AV |
1232 | if (!host_err && unlikely(d_unhashed(dchild))) { |
1233 | struct dentry *d; | |
1234 | d = lookup_one_len(dchild->d_name.name, | |
1235 | dchild->d_parent, | |
1236 | dchild->d_name.len); | |
1237 | if (IS_ERR(d)) { | |
1238 | host_err = PTR_ERR(d); | |
1239 | break; | |
1240 | } | |
1241 | if (unlikely(d_is_negative(d))) { | |
1242 | dput(d); | |
1243 | err = nfserr_serverfault; | |
1244 | goto out; | |
1245 | } | |
1246 | dput(resfhp->fh_dentry); | |
1247 | resfhp->fh_dentry = dget(d); | |
1248 | err = fh_update(resfhp); | |
1249 | dput(dchild); | |
1250 | dchild = d; | |
1251 | if (err) | |
1252 | goto out; | |
1253 | } | |
1da177e4 LT |
1254 | break; |
1255 | case S_IFCHR: | |
1256 | case S_IFBLK: | |
1257 | case S_IFIFO: | |
1258 | case S_IFSOCK: | |
6264d69d | 1259 | host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev); |
1da177e4 | 1260 | break; |
71423274 BF |
1261 | default: |
1262 | printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n", | |
1263 | type); | |
1264 | host_err = -EINVAL; | |
1da177e4 | 1265 | } |
4a55c101 | 1266 | if (host_err < 0) |
1da177e4 LT |
1267 | goto out_nfserr; |
1268 | ||
f501912a | 1269 | err = nfsd_create_setattr(rqstp, resfhp, iap); |
1da177e4 | 1270 | |
f501912a | 1271 | /* |
0f3a24b4 TM |
1272 | * nfsd_create_setattr already committed the child. Transactional |
1273 | * filesystems had a chance to commit changes for both parent and | |
b44061d0 | 1274 | * child simultaneously making the following commit_metadata a |
0f3a24b4 | 1275 | * noop. |
f501912a BM |
1276 | */ |
1277 | err2 = nfserrno(commit_metadata(fhp)); | |
5c002b3b BF |
1278 | if (err2) |
1279 | err = err2; | |
1da177e4 LT |
1280 | /* |
1281 | * Update the file handle to get the new inode info. | |
1282 | */ | |
1283 | if (!err) | |
1284 | err = fh_update(resfhp); | |
1285 | out: | |
2b118859 | 1286 | dput(dchild); |
1da177e4 LT |
1287 | return err; |
1288 | ||
1289 | out_nfserr: | |
6264d69d | 1290 | err = nfserrno(host_err); |
1da177e4 LT |
1291 | goto out; |
1292 | } | |
1293 | ||
b44061d0 BF |
1294 | /* |
1295 | * Create a filesystem object (regular, directory, special). | |
1296 | * Note that the parent directory is left locked. | |
1297 | * | |
1298 | * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp | |
1299 | */ | |
1300 | __be32 | |
1301 | nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp, | |
1302 | char *fname, int flen, struct iattr *iap, | |
1303 | int type, dev_t rdev, struct svc_fh *resfhp) | |
1304 | { | |
1305 | struct dentry *dentry, *dchild = NULL; | |
b44061d0 BF |
1306 | __be32 err; |
1307 | int host_err; | |
1308 | ||
1309 | if (isdotent(fname, flen)) | |
1310 | return nfserr_exist; | |
1311 | ||
fa08139d | 1312 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP); |
b44061d0 BF |
1313 | if (err) |
1314 | return err; | |
1315 | ||
1316 | dentry = fhp->fh_dentry; | |
b44061d0 BF |
1317 | |
1318 | host_err = fh_want_write(fhp); | |
1319 | if (host_err) | |
1320 | return nfserrno(host_err); | |
1321 | ||
1322 | fh_lock_nested(fhp, I_MUTEX_PARENT); | |
1323 | dchild = lookup_one_len(fname, dentry, flen); | |
1324 | host_err = PTR_ERR(dchild); | |
1325 | if (IS_ERR(dchild)) | |
1326 | return nfserrno(host_err); | |
1327 | err = fh_compose(resfhp, fhp->fh_export, dchild, fhp); | |
502aa0a5 JB |
1328 | /* |
1329 | * We unconditionally drop our ref to dchild as fh_compose will have | |
1330 | * already grabbed its own ref for it. | |
1331 | */ | |
1332 | dput(dchild); | |
1333 | if (err) | |
b44061d0 | 1334 | return err; |
b44061d0 BF |
1335 | return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type, |
1336 | rdev, resfhp); | |
1337 | } | |
1338 | ||
1da177e4 | 1339 | #ifdef CONFIG_NFSD_V3 |
ac6721a1 | 1340 | |
1da177e4 | 1341 | /* |
ac6721a1 | 1342 | * NFSv3 and NFSv4 version of nfsd_create |
1da177e4 | 1343 | */ |
6264d69d | 1344 | __be32 |
ac6721a1 | 1345 | do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp, |
1da177e4 LT |
1346 | char *fname, int flen, struct iattr *iap, |
1347 | struct svc_fh *resfhp, int createmode, u32 *verifier, | |
856121b2 | 1348 | bool *truncp, bool *created) |
1da177e4 LT |
1349 | { |
1350 | struct dentry *dentry, *dchild = NULL; | |
1351 | struct inode *dirp; | |
6264d69d AV |
1352 | __be32 err; |
1353 | int host_err; | |
1da177e4 | 1354 | __u32 v_mtime=0, v_atime=0; |
1da177e4 LT |
1355 | |
1356 | err = nfserr_perm; | |
1357 | if (!flen) | |
1358 | goto out; | |
1359 | err = nfserr_exist; | |
1360 | if (isdotent(fname, flen)) | |
1361 | goto out; | |
1362 | if (!(iap->ia_valid & ATTR_MODE)) | |
1363 | iap->ia_mode = 0; | |
1574dff8 | 1364 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC); |
1da177e4 LT |
1365 | if (err) |
1366 | goto out; | |
1367 | ||
1368 | dentry = fhp->fh_dentry; | |
2b0143b5 | 1369 | dirp = d_inode(dentry); |
1da177e4 | 1370 | |
4a55c101 JK |
1371 | host_err = fh_want_write(fhp); |
1372 | if (host_err) | |
1373 | goto out_nfserr; | |
1374 | ||
12fd3520 | 1375 | fh_lock_nested(fhp, I_MUTEX_PARENT); |
1da177e4 LT |
1376 | |
1377 | /* | |
1378 | * Compose the response file handle. | |
1379 | */ | |
1380 | dchild = lookup_one_len(fname, dentry, flen); | |
6264d69d | 1381 | host_err = PTR_ERR(dchild); |
1da177e4 LT |
1382 | if (IS_ERR(dchild)) |
1383 | goto out_nfserr; | |
1384 | ||
1574dff8 | 1385 | /* If file doesn't exist, check for permissions to create one */ |
2b0143b5 | 1386 | if (d_really_is_negative(dchild)) { |
1574dff8 SP |
1387 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE); |
1388 | if (err) | |
1389 | goto out; | |
1390 | } | |
1391 | ||
1da177e4 LT |
1392 | err = fh_compose(resfhp, fhp->fh_export, dchild, fhp); |
1393 | if (err) | |
1394 | goto out; | |
1395 | ||
ac6721a1 | 1396 | if (nfsd_create_is_exclusive(createmode)) { |
c397852c | 1397 | /* solaris7 gets confused (bugid 4218508) if these have |
749997e5 JL |
1398 | * the high bit set, so just clear the high bits. If this is |
1399 | * ever changed to use different attrs for storing the | |
1400 | * verifier, then do_open_lookup() will also need to be fixed | |
1401 | * accordingly. | |
1da177e4 LT |
1402 | */ |
1403 | v_mtime = verifier[0]&0x7fffffff; | |
1404 | v_atime = verifier[1]&0x7fffffff; | |
1da177e4 LT |
1405 | } |
1406 | ||
2b0143b5 | 1407 | if (d_really_is_positive(dchild)) { |
1da177e4 LT |
1408 | err = 0; |
1409 | ||
1410 | switch (createmode) { | |
1411 | case NFS3_CREATE_UNCHECKED: | |
e36cb0b8 | 1412 | if (! d_is_reg(dchild)) |
9dc4e6c4 | 1413 | goto out; |
1da177e4 LT |
1414 | else if (truncp) { |
1415 | /* in nfsv4, we need to treat this case a little | |
1416 | * differently. we don't want to truncate the | |
1417 | * file now; this would be wrong if the OPEN | |
1418 | * fails for some other reason. furthermore, | |
1419 | * if the size is nonzero, we should ignore it | |
1420 | * according to spec! | |
1421 | */ | |
1422 | *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size; | |
1423 | } | |
1424 | else { | |
1425 | iap->ia_valid &= ATTR_SIZE; | |
1426 | goto set_attr; | |
1427 | } | |
1428 | break; | |
1429 | case NFS3_CREATE_EXCLUSIVE: | |
2b0143b5 DH |
1430 | if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime |
1431 | && d_inode(dchild)->i_atime.tv_sec == v_atime | |
1432 | && d_inode(dchild)->i_size == 0 ) { | |
7007c90f | 1433 | if (created) |
384a7cca | 1434 | *created = true; |
1da177e4 | 1435 | break; |
7007c90f | 1436 | } |
0ac203cb | 1437 | /* fall through */ |
ac6721a1 | 1438 | case NFS4_CREATE_EXCLUSIVE4_1: |
2b0143b5 DH |
1439 | if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime |
1440 | && d_inode(dchild)->i_atime.tv_sec == v_atime | |
1441 | && d_inode(dchild)->i_size == 0 ) { | |
7007c90f | 1442 | if (created) |
384a7cca | 1443 | *created = true; |
ac6721a1 | 1444 | goto set_attr; |
7007c90f | 1445 | } |
0ac203cb | 1446 | /* fall through */ |
1da177e4 LT |
1447 | case NFS3_CREATE_GUARDED: |
1448 | err = nfserr_exist; | |
1449 | } | |
bad0dcff | 1450 | fh_drop_write(fhp); |
1da177e4 LT |
1451 | goto out; |
1452 | } | |
1453 | ||
312b63fb | 1454 | host_err = vfs_create(dirp, dchild, iap->ia_mode, true); |
463c3197 | 1455 | if (host_err < 0) { |
bad0dcff | 1456 | fh_drop_write(fhp); |
1da177e4 | 1457 | goto out_nfserr; |
463c3197 | 1458 | } |
81ac95c5 | 1459 | if (created) |
384a7cca | 1460 | *created = true; |
1da177e4 | 1461 | |
4ac35c2f | 1462 | nfsd_check_ignore_resizing(iap); |
1463 | ||
ac6721a1 | 1464 | if (nfsd_create_is_exclusive(createmode)) { |
c397852c | 1465 | /* Cram the verifier into atime/mtime */ |
1da177e4 | 1466 | iap->ia_valid = ATTR_MTIME|ATTR_ATIME |
c397852c | 1467 | | ATTR_MTIME_SET|ATTR_ATIME_SET; |
1da177e4 LT |
1468 | /* XXX someone who knows this better please fix it for nsec */ |
1469 | iap->ia_mtime.tv_sec = v_mtime; | |
1470 | iap->ia_atime.tv_sec = v_atime; | |
1471 | iap->ia_mtime.tv_nsec = 0; | |
1472 | iap->ia_atime.tv_nsec = 0; | |
1da177e4 LT |
1473 | } |
1474 | ||
1da177e4 | 1475 | set_attr: |
f501912a BM |
1476 | err = nfsd_create_setattr(rqstp, resfhp, iap); |
1477 | ||
1478 | /* | |
0f3a24b4 TM |
1479 | * nfsd_create_setattr already committed the child |
1480 | * (and possibly also the parent). | |
f501912a BM |
1481 | */ |
1482 | if (!err) | |
1483 | err = nfserrno(commit_metadata(fhp)); | |
f193fbab YT |
1484 | |
1485 | /* | |
1486 | * Update the filehandle to get the new inode info. | |
1487 | */ | |
1488 | if (!err) | |
1489 | err = fh_update(resfhp); | |
1da177e4 LT |
1490 | |
1491 | out: | |
1492 | fh_unlock(fhp); | |
1493 | if (dchild && !IS_ERR(dchild)) | |
1494 | dput(dchild); | |
4a55c101 | 1495 | fh_drop_write(fhp); |
1da177e4 LT |
1496 | return err; |
1497 | ||
1498 | out_nfserr: | |
6264d69d | 1499 | err = nfserrno(host_err); |
1da177e4 LT |
1500 | goto out; |
1501 | } | |
1502 | #endif /* CONFIG_NFSD_V3 */ | |
1503 | ||
1504 | /* | |
1505 | * Read a symlink. On entry, *lenp must contain the maximum path length that | |
1506 | * fits into the buffer. On return, it contains the true length. | |
1507 | * N.B. After this call fhp needs an fh_put | |
1508 | */ | |
6264d69d | 1509 | __be32 |
1da177e4 LT |
1510 | nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp) |
1511 | { | |
6264d69d | 1512 | __be32 err; |
4d7edbc3 | 1513 | const char *link; |
68ac1234 | 1514 | struct path path; |
4d7edbc3 AV |
1515 | DEFINE_DELAYED_CALL(done); |
1516 | int len; | |
1da177e4 | 1517 | |
8837abca | 1518 | err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP); |
4d7edbc3 AV |
1519 | if (unlikely(err)) |
1520 | return err; | |
1da177e4 | 1521 | |
68ac1234 AV |
1522 | path.mnt = fhp->fh_export->ex_path.mnt; |
1523 | path.dentry = fhp->fh_dentry; | |
1da177e4 | 1524 | |
4d7edbc3 AV |
1525 | if (unlikely(!d_is_symlink(path.dentry))) |
1526 | return nfserr_inval; | |
1da177e4 | 1527 | |
68ac1234 | 1528 | touch_atime(&path); |
1da177e4 | 1529 | |
4d7edbc3 AV |
1530 | link = vfs_get_link(path.dentry, &done); |
1531 | if (IS_ERR(link)) | |
1532 | return nfserrno(PTR_ERR(link)); | |
1da177e4 | 1533 | |
4d7edbc3 AV |
1534 | len = strlen(link); |
1535 | if (len < *lenp) | |
1536 | *lenp = len; | |
1537 | memcpy(buf, link, *lenp); | |
1538 | do_delayed_call(&done); | |
1539 | return 0; | |
1da177e4 LT |
1540 | } |
1541 | ||
1542 | /* | |
1543 | * Create a symlink and look up its inode | |
1544 | * N.B. After this call _both_ fhp and resfhp need an fh_put | |
1545 | */ | |
6264d69d | 1546 | __be32 |
1da177e4 LT |
1547 | nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp, |
1548 | char *fname, int flen, | |
52ee0433 | 1549 | char *path, |
1e444f5b | 1550 | struct svc_fh *resfhp) |
1da177e4 LT |
1551 | { |
1552 | struct dentry *dentry, *dnew; | |
6264d69d AV |
1553 | __be32 err, cerr; |
1554 | int host_err; | |
1da177e4 LT |
1555 | |
1556 | err = nfserr_noent; | |
52ee0433 | 1557 | if (!flen || path[0] == '\0') |
1da177e4 LT |
1558 | goto out; |
1559 | err = nfserr_exist; | |
1560 | if (isdotent(fname, flen)) | |
1561 | goto out; | |
1562 | ||
8837abca | 1563 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE); |
1da177e4 LT |
1564 | if (err) |
1565 | goto out; | |
4a55c101 JK |
1566 | |
1567 | host_err = fh_want_write(fhp); | |
1568 | if (host_err) | |
1569 | goto out_nfserr; | |
1570 | ||
1da177e4 LT |
1571 | fh_lock(fhp); |
1572 | dentry = fhp->fh_dentry; | |
1573 | dnew = lookup_one_len(fname, dentry, flen); | |
6264d69d | 1574 | host_err = PTR_ERR(dnew); |
1da177e4 LT |
1575 | if (IS_ERR(dnew)) |
1576 | goto out_nfserr; | |
1577 | ||
2b0143b5 | 1578 | host_err = vfs_symlink(d_inode(dentry), dnew, path); |
6264d69d | 1579 | err = nfserrno(host_err); |
f501912a BM |
1580 | if (!err) |
1581 | err = nfserrno(commit_metadata(fhp)); | |
1da177e4 LT |
1582 | fh_unlock(fhp); |
1583 | ||
bad0dcff | 1584 | fh_drop_write(fhp); |
75c3f29d | 1585 | |
1da177e4 LT |
1586 | cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp); |
1587 | dput(dnew); | |
1588 | if (err==0) err = cerr; | |
1589 | out: | |
1590 | return err; | |
1591 | ||
1592 | out_nfserr: | |
6264d69d | 1593 | err = nfserrno(host_err); |
1da177e4 LT |
1594 | goto out; |
1595 | } | |
1596 | ||
1597 | /* | |
1598 | * Create a hardlink | |
1599 | * N.B. After this call _both_ ffhp and tfhp need an fh_put | |
1600 | */ | |
6264d69d | 1601 | __be32 |
1da177e4 LT |
1602 | nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp, |
1603 | char *name, int len, struct svc_fh *tfhp) | |
1604 | { | |
1605 | struct dentry *ddir, *dnew, *dold; | |
55b13354 | 1606 | struct inode *dirp; |
6264d69d AV |
1607 | __be32 err; |
1608 | int host_err; | |
1da177e4 | 1609 | |
8837abca | 1610 | err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE); |
1da177e4 LT |
1611 | if (err) |
1612 | goto out; | |
7d818a7b | 1613 | err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP); |
1da177e4 LT |
1614 | if (err) |
1615 | goto out; | |
7d818a7b | 1616 | err = nfserr_isdir; |
e36cb0b8 | 1617 | if (d_is_dir(tfhp->fh_dentry)) |
7d818a7b | 1618 | goto out; |
1da177e4 LT |
1619 | err = nfserr_perm; |
1620 | if (!len) | |
1621 | goto out; | |
1622 | err = nfserr_exist; | |
1623 | if (isdotent(name, len)) | |
1624 | goto out; | |
1625 | ||
4a55c101 JK |
1626 | host_err = fh_want_write(tfhp); |
1627 | if (host_err) { | |
1628 | err = nfserrno(host_err); | |
1629 | goto out; | |
1630 | } | |
1631 | ||
12fd3520 | 1632 | fh_lock_nested(ffhp, I_MUTEX_PARENT); |
1da177e4 | 1633 | ddir = ffhp->fh_dentry; |
2b0143b5 | 1634 | dirp = d_inode(ddir); |
1da177e4 LT |
1635 | |
1636 | dnew = lookup_one_len(name, ddir, len); | |
6264d69d | 1637 | host_err = PTR_ERR(dnew); |
1da177e4 LT |
1638 | if (IS_ERR(dnew)) |
1639 | goto out_nfserr; | |
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; |
146a8595 | 1646 | host_err = vfs_link(dold, dirp, dnew, NULL); |
6264d69d | 1647 | if (!host_err) { |
f501912a BM |
1648 | err = nfserrno(commit_metadata(ffhp)); |
1649 | if (!err) | |
1650 | err = nfserrno(commit_metadata(tfhp)); | |
1da177e4 | 1651 | } else { |
6264d69d | 1652 | if (host_err == -EXDEV && rqstp->rq_vers == 2) |
1da177e4 LT |
1653 | err = nfserr_acces; |
1654 | else | |
6264d69d | 1655 | err = nfserrno(host_err); |
1da177e4 | 1656 | } |
75c3f29d | 1657 | out_dput: |
1da177e4 | 1658 | dput(dnew); |
270d56e5 DR |
1659 | out_unlock: |
1660 | fh_unlock(ffhp); | |
4a55c101 | 1661 | fh_drop_write(tfhp); |
1da177e4 LT |
1662 | out: |
1663 | return err; | |
1664 | ||
1665 | out_nfserr: | |
6264d69d | 1666 | err = nfserrno(host_err); |
270d56e5 | 1667 | goto out_unlock; |
1da177e4 LT |
1668 | } |
1669 | ||
7775ec57 JL |
1670 | static void |
1671 | nfsd_close_cached_files(struct dentry *dentry) | |
1672 | { | |
1673 | struct inode *inode = d_inode(dentry); | |
1674 | ||
1675 | if (inode && S_ISREG(inode->i_mode)) | |
1676 | nfsd_file_close_inode_sync(inode); | |
1677 | } | |
1678 | ||
1679 | static bool | |
1680 | nfsd_has_cached_files(struct dentry *dentry) | |
1681 | { | |
1682 | bool ret = false; | |
1683 | struct inode *inode = d_inode(dentry); | |
1684 | ||
1685 | if (inode && S_ISREG(inode->i_mode)) | |
1686 | ret = nfsd_file_is_cached(inode); | |
1687 | return ret; | |
1688 | } | |
1689 | ||
1da177e4 LT |
1690 | /* |
1691 | * Rename a file | |
1692 | * N.B. After this call _both_ ffhp and tfhp need an fh_put | |
1693 | */ | |
6264d69d | 1694 | __be32 |
1da177e4 LT |
1695 | nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen, |
1696 | struct svc_fh *tfhp, char *tname, int tlen) | |
1697 | { | |
1698 | struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap; | |
1699 | struct inode *fdir, *tdir; | |
6264d69d AV |
1700 | __be32 err; |
1701 | int host_err; | |
7775ec57 | 1702 | bool has_cached = false; |
1da177e4 | 1703 | |
8837abca | 1704 | err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE); |
1da177e4 LT |
1705 | if (err) |
1706 | goto out; | |
8837abca | 1707 | err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE); |
1da177e4 LT |
1708 | if (err) |
1709 | goto out; | |
1710 | ||
1711 | fdentry = ffhp->fh_dentry; | |
2b0143b5 | 1712 | fdir = d_inode(fdentry); |
1da177e4 LT |
1713 | |
1714 | tdentry = tfhp->fh_dentry; | |
2b0143b5 | 1715 | tdir = d_inode(tdentry); |
1da177e4 | 1716 | |
1da177e4 LT |
1717 | err = nfserr_perm; |
1718 | if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen)) | |
1719 | goto out; | |
1720 | ||
7775ec57 | 1721 | retry: |
4a55c101 JK |
1722 | host_err = fh_want_write(ffhp); |
1723 | if (host_err) { | |
1724 | err = nfserrno(host_err); | |
1725 | goto out; | |
1726 | } | |
1727 | ||
1da177e4 LT |
1728 | /* cannot use fh_lock as we need deadlock protective ordering |
1729 | * so do it by hand */ | |
1730 | trap = lock_rename(tdentry, fdentry); | |
aaf91ec1 | 1731 | ffhp->fh_locked = tfhp->fh_locked = true; |
1da177e4 LT |
1732 | fill_pre_wcc(ffhp); |
1733 | fill_pre_wcc(tfhp); | |
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 | |
7775ec57 JL |
1761 | if (nfsd_has_cached_files(ndentry)) { |
1762 | has_cached = true; | |
1763 | goto out_dput_old; | |
1764 | } else { | |
1765 | host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0); | |
1766 | if (!host_err) { | |
1767 | host_err = commit_metadata(tfhp); | |
1768 | if (!host_err) | |
1769 | host_err = commit_metadata(ffhp); | |
1770 | } | |
1da177e4 | 1771 | } |
1da177e4 LT |
1772 | out_dput_new: |
1773 | dput(ndentry); | |
1774 | out_dput_old: | |
1775 | dput(odentry); | |
1776 | out_nfserr: | |
6264d69d | 1777 | err = nfserrno(host_err); |
fbb74a34 BF |
1778 | /* |
1779 | * We cannot rely on fh_unlock on the two filehandles, | |
1da177e4 | 1780 | * as that would do the wrong thing if the two directories |
fbb74a34 | 1781 | * were the same, so again we do it by hand. |
1da177e4 | 1782 | */ |
7775ec57 JL |
1783 | if (!has_cached) { |
1784 | fill_post_wcc(ffhp); | |
1785 | fill_post_wcc(tfhp); | |
1786 | } | |
1da177e4 | 1787 | unlock_rename(tdentry, fdentry); |
aaf91ec1 | 1788 | ffhp->fh_locked = tfhp->fh_locked = false; |
4a55c101 | 1789 | fh_drop_write(ffhp); |
1da177e4 | 1790 | |
7775ec57 JL |
1791 | /* |
1792 | * If the target dentry has cached open files, then we need to try to | |
1793 | * close them prior to doing the rename. Flushing delayed fput | |
1794 | * shouldn't be done with locks held however, so we delay it until this | |
1795 | * point and then reattempt the whole shebang. | |
1796 | */ | |
1797 | if (has_cached) { | |
1798 | has_cached = false; | |
1799 | nfsd_close_cached_files(ndentry); | |
1800 | dput(ndentry); | |
1801 | goto retry; | |
1802 | } | |
1da177e4 LT |
1803 | out: |
1804 | return err; | |
1805 | } | |
1806 | ||
1807 | /* | |
1808 | * Unlink a file or directory | |
1809 | * N.B. After this call fhp needs an fh_put | |
1810 | */ | |
6264d69d | 1811 | __be32 |
1da177e4 LT |
1812 | nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, |
1813 | char *fname, int flen) | |
1814 | { | |
1815 | struct dentry *dentry, *rdentry; | |
1816 | struct inode *dirp; | |
6264d69d AV |
1817 | __be32 err; |
1818 | int host_err; | |
1da177e4 LT |
1819 | |
1820 | err = nfserr_acces; | |
1821 | if (!flen || isdotent(fname, flen)) | |
1822 | goto out; | |
8837abca | 1823 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE); |
1da177e4 LT |
1824 | if (err) |
1825 | goto out; | |
1826 | ||
4a55c101 JK |
1827 | host_err = fh_want_write(fhp); |
1828 | if (host_err) | |
1829 | goto out_nfserr; | |
1830 | ||
12fd3520 | 1831 | fh_lock_nested(fhp, I_MUTEX_PARENT); |
1da177e4 | 1832 | dentry = fhp->fh_dentry; |
2b0143b5 | 1833 | dirp = d_inode(dentry); |
1da177e4 LT |
1834 | |
1835 | rdentry = lookup_one_len(fname, dentry, flen); | |
6264d69d | 1836 | host_err = PTR_ERR(rdentry); |
1da177e4 | 1837 | if (IS_ERR(rdentry)) |
0ca0c9d7 | 1838 | goto out_drop_write; |
1da177e4 | 1839 | |
2b0143b5 | 1840 | if (d_really_is_negative(rdentry)) { |
1da177e4 | 1841 | dput(rdentry); |
0ca0c9d7 BF |
1842 | host_err = -ENOENT; |
1843 | goto out_drop_write; | |
1da177e4 LT |
1844 | } |
1845 | ||
1846 | if (!type) | |
2b0143b5 | 1847 | type = d_inode(rdentry)->i_mode & S_IFMT; |
1da177e4 | 1848 | |
7775ec57 JL |
1849 | if (type != S_IFDIR) { |
1850 | nfsd_close_cached_files(rdentry); | |
b21996e3 | 1851 | host_err = vfs_unlink(dirp, rdentry, NULL); |
7775ec57 | 1852 | } else { |
6264d69d | 1853 | host_err = vfs_rmdir(dirp, rdentry); |
7775ec57 JL |
1854 | } |
1855 | ||
f501912a BM |
1856 | if (!host_err) |
1857 | host_err = commit_metadata(fhp); | |
541ce98c BF |
1858 | dput(rdentry); |
1859 | ||
0ca0c9d7 BF |
1860 | out_drop_write: |
1861 | fh_drop_write(fhp); | |
1da177e4 | 1862 | out_nfserr: |
466e16f0 N |
1863 | if (host_err == -EBUSY) { |
1864 | /* name is mounted-on. There is no perfect | |
1865 | * error status. | |
1866 | */ | |
1867 | if (nfsd_v4client(rqstp)) | |
1868 | err = nfserr_file_open; | |
1869 | else | |
1870 | err = nfserr_acces; | |
1871 | } else { | |
1872 | err = nfserrno(host_err); | |
1873 | } | |
f193fbab YT |
1874 | out: |
1875 | return err; | |
1da177e4 LT |
1876 | } |
1877 | ||
14f7dd63 DW |
1878 | /* |
1879 | * We do this buffering because we must not call back into the file | |
1880 | * system's ->lookup() method from the filldir callback. That may well | |
1881 | * deadlock a number of file systems. | |
1882 | * | |
1883 | * This is based heavily on the implementation of same in XFS. | |
1884 | */ | |
1885 | struct buffered_dirent { | |
1886 | u64 ino; | |
1887 | loff_t offset; | |
1888 | int namlen; | |
1889 | unsigned int d_type; | |
1890 | char name[]; | |
1891 | }; | |
1892 | ||
1893 | struct readdir_data { | |
5c0ba4e0 | 1894 | struct dir_context ctx; |
14f7dd63 DW |
1895 | char *dirent; |
1896 | size_t used; | |
53c9c5c0 | 1897 | int full; |
14f7dd63 DW |
1898 | }; |
1899 | ||
ac7576f4 MS |
1900 | static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name, |
1901 | int namlen, loff_t offset, u64 ino, | |
1902 | unsigned int d_type) | |
14f7dd63 | 1903 | { |
ac7576f4 MS |
1904 | struct readdir_data *buf = |
1905 | container_of(ctx, struct readdir_data, ctx); | |
14f7dd63 DW |
1906 | struct buffered_dirent *de = (void *)(buf->dirent + buf->used); |
1907 | unsigned int reclen; | |
1908 | ||
1909 | reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64)); | |
53c9c5c0 AV |
1910 | if (buf->used + reclen > PAGE_SIZE) { |
1911 | buf->full = 1; | |
14f7dd63 | 1912 | return -EINVAL; |
53c9c5c0 | 1913 | } |
14f7dd63 DW |
1914 | |
1915 | de->namlen = namlen; | |
1916 | de->offset = offset; | |
1917 | de->ino = ino; | |
1918 | de->d_type = d_type; | |
1919 | memcpy(de->name, name, namlen); | |
1920 | buf->used += reclen; | |
1921 | ||
1922 | return 0; | |
1923 | } | |
1924 | ||
ac7576f4 | 1925 | static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func, |
2f9092e1 | 1926 | struct readdir_cd *cdp, loff_t *offsetp) |
2628b766 | 1927 | { |
14f7dd63 | 1928 | struct buffered_dirent *de; |
2628b766 | 1929 | int host_err; |
14f7dd63 DW |
1930 | int size; |
1931 | loff_t offset; | |
ac6614b7 AV |
1932 | struct readdir_data buf = { |
1933 | .ctx.actor = nfsd_buffered_filldir, | |
1934 | .dirent = (void *)__get_free_page(GFP_KERNEL) | |
1935 | }; | |
2628b766 | 1936 | |
14f7dd63 | 1937 | if (!buf.dirent) |
2f9092e1 | 1938 | return nfserrno(-ENOMEM); |
14f7dd63 DW |
1939 | |
1940 | offset = *offsetp; | |
2628b766 | 1941 | |
14f7dd63 DW |
1942 | while (1) { |
1943 | unsigned int reclen; | |
1944 | ||
b726e923 | 1945 | cdp->err = nfserr_eof; /* will be cleared on successful read */ |
14f7dd63 | 1946 | buf.used = 0; |
53c9c5c0 | 1947 | buf.full = 0; |
14f7dd63 | 1948 | |
5c0ba4e0 | 1949 | host_err = iterate_dir(file, &buf.ctx); |
53c9c5c0 AV |
1950 | if (buf.full) |
1951 | host_err = 0; | |
1952 | ||
1953 | if (host_err < 0) | |
14f7dd63 DW |
1954 | break; |
1955 | ||
1956 | size = buf.used; | |
1957 | ||
1958 | if (!size) | |
1959 | break; | |
1960 | ||
14f7dd63 DW |
1961 | de = (struct buffered_dirent *)buf.dirent; |
1962 | while (size > 0) { | |
1963 | offset = de->offset; | |
1964 | ||
1965 | if (func(cdp, de->name, de->namlen, de->offset, | |
1966 | de->ino, de->d_type)) | |
2f9092e1 | 1967 | break; |
14f7dd63 DW |
1968 | |
1969 | if (cdp->err != nfs_ok) | |
2f9092e1 | 1970 | break; |
14f7dd63 DW |
1971 | |
1972 | reclen = ALIGN(sizeof(*de) + de->namlen, | |
1973 | sizeof(u64)); | |
1974 | size -= reclen; | |
1975 | de = (struct buffered_dirent *)((char *)de + reclen); | |
1976 | } | |
2f9092e1 DW |
1977 | if (size > 0) /* We bailed out early */ |
1978 | break; | |
1979 | ||
c002a6c7 | 1980 | offset = vfs_llseek(file, 0, SEEK_CUR); |
14f7dd63 DW |
1981 | } |
1982 | ||
14f7dd63 | 1983 | free_page((unsigned long)(buf.dirent)); |
2628b766 DW |
1984 | |
1985 | if (host_err) | |
1986 | return nfserrno(host_err); | |
14f7dd63 DW |
1987 | |
1988 | *offsetp = offset; | |
1989 | return cdp->err; | |
2628b766 DW |
1990 | } |
1991 | ||
1da177e4 LT |
1992 | /* |
1993 | * Read entries from a directory. | |
1994 | * The NFSv3/4 verifier we ignore for now. | |
1995 | */ | |
6264d69d | 1996 | __be32 |
1da177e4 | 1997 | nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp, |
ac7576f4 | 1998 | struct readdir_cd *cdp, nfsd_filldir_t func) |
1da177e4 | 1999 | { |
6264d69d | 2000 | __be32 err; |
1da177e4 LT |
2001 | struct file *file; |
2002 | loff_t offset = *offsetp; | |
06effdbb BS |
2003 | int may_flags = NFSD_MAY_READ; |
2004 | ||
2005 | /* NFSv2 only supports 32 bit cookies */ | |
2006 | if (rqstp->rq_vers > 2) | |
2007 | may_flags |= NFSD_MAY_64BIT_COOKIE; | |
1da177e4 | 2008 | |
06effdbb | 2009 | err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file); |
1da177e4 LT |
2010 | if (err) |
2011 | goto out; | |
2012 | ||
b108fe6b | 2013 | offset = vfs_llseek(file, offset, SEEK_SET); |
1da177e4 LT |
2014 | if (offset < 0) { |
2015 | err = nfserrno((int)offset); | |
2016 | goto out_close; | |
2017 | } | |
2018 | ||
14f7dd63 | 2019 | err = nfsd_buffered_readdir(file, func, cdp, offsetp); |
1da177e4 LT |
2020 | |
2021 | if (err == nfserr_eof || err == nfserr_toosmall) | |
2022 | err = nfs_ok; /* can still be found in ->err */ | |
2023 | out_close: | |
fd891454 | 2024 | fput(file); |
1da177e4 LT |
2025 | out: |
2026 | return err; | |
2027 | } | |
2028 | ||
2029 | /* | |
2030 | * Get file system stats | |
2031 | * N.B. After this call fhp needs an fh_put | |
2032 | */ | |
6264d69d | 2033 | __be32 |
04716e66 | 2034 | nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access) |
1da177e4 | 2035 | { |
ebabe9a9 CH |
2036 | __be32 err; |
2037 | ||
2038 | err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access); | |
f6360efb TI |
2039 | if (!err) { |
2040 | struct path path = { | |
2041 | .mnt = fhp->fh_export->ex_path.mnt, | |
2042 | .dentry = fhp->fh_dentry, | |
2043 | }; | |
2044 | if (vfs_statfs(&path, stat)) | |
2045 | err = nfserr_io; | |
2046 | } | |
1da177e4 LT |
2047 | return err; |
2048 | } | |
2049 | ||
c7d51402 | 2050 | static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp) |
e22841c6 | 2051 | { |
c7d51402 | 2052 | return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY; |
e22841c6 BF |
2053 | } |
2054 | ||
1da177e4 LT |
2055 | /* |
2056 | * Check for a user's access permissions to this inode. | |
2057 | */ | |
6264d69d | 2058 | __be32 |
0ec757df BF |
2059 | nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp, |
2060 | struct dentry *dentry, int acc) | |
1da177e4 | 2061 | { |
2b0143b5 | 2062 | struct inode *inode = d_inode(dentry); |
1da177e4 LT |
2063 | int err; |
2064 | ||
aea93397 | 2065 | if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP) |
1da177e4 LT |
2066 | return 0; |
2067 | #if 0 | |
2068 | dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n", | |
2069 | acc, | |
8837abca MS |
2070 | (acc & NFSD_MAY_READ)? " read" : "", |
2071 | (acc & NFSD_MAY_WRITE)? " write" : "", | |
2072 | (acc & NFSD_MAY_EXEC)? " exec" : "", | |
2073 | (acc & NFSD_MAY_SATTR)? " sattr" : "", | |
2074 | (acc & NFSD_MAY_TRUNC)? " trunc" : "", | |
2075 | (acc & NFSD_MAY_LOCK)? " lock" : "", | |
2076 | (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "", | |
1da177e4 LT |
2077 | inode->i_mode, |
2078 | IS_IMMUTABLE(inode)? " immut" : "", | |
2079 | IS_APPEND(inode)? " append" : "", | |
2c463e95 | 2080 | __mnt_is_readonly(exp->ex_path.mnt)? " ro" : ""); |
1da177e4 | 2081 | dprintk(" owner %d/%d user %d/%d\n", |
5cc0a840 | 2082 | inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid()); |
1da177e4 LT |
2083 | #endif |
2084 | ||
2085 | /* Normally we reject any write/sattr etc access on a read-only file | |
2086 | * system. But if it is IRIX doing check on write-access for a | |
2087 | * device special file, we ignore rofs. | |
2088 | */ | |
8837abca MS |
2089 | if (!(acc & NFSD_MAY_LOCAL_ACCESS)) |
2090 | if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) { | |
2c463e95 DH |
2091 | if (exp_rdonly(rqstp, exp) || |
2092 | __mnt_is_readonly(exp->ex_path.mnt)) | |
1da177e4 | 2093 | return nfserr_rofs; |
8837abca | 2094 | if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode)) |
1da177e4 LT |
2095 | return nfserr_perm; |
2096 | } | |
8837abca | 2097 | if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode)) |
1da177e4 LT |
2098 | return nfserr_perm; |
2099 | ||
8837abca | 2100 | if (acc & NFSD_MAY_LOCK) { |
1da177e4 LT |
2101 | /* If we cannot rely on authentication in NLM requests, |
2102 | * just allow locks, otherwise require read permission, or | |
2103 | * ownership | |
2104 | */ | |
2105 | if (exp->ex_flags & NFSEXP_NOAUTHNLM) | |
2106 | return 0; | |
2107 | else | |
8837abca | 2108 | acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE; |
1da177e4 LT |
2109 | } |
2110 | /* | |
2111 | * The file owner always gets access permission for accesses that | |
2112 | * would normally be checked at open time. This is to make | |
2113 | * file access work even when the client has done a fchmod(fd, 0). | |
2114 | * | |
2115 | * However, `cp foo bar' should fail nevertheless when bar is | |
2116 | * readonly. A sensible way to do this might be to reject all | |
2117 | * attempts to truncate a read-only file, because a creat() call | |
2118 | * always implies file truncation. | |
2119 | * ... but this isn't really fair. A process may reasonably call | |
2120 | * ftruncate on an open file descriptor on a file with perm 000. | |
2121 | * We must trust the client to do permission checking - using "ACCESS" | |
2122 | * with NFSv3. | |
2123 | */ | |
8837abca | 2124 | if ((acc & NFSD_MAY_OWNER_OVERRIDE) && |
6fab8779 | 2125 | uid_eq(inode->i_uid, current_fsuid())) |
1da177e4 LT |
2126 | return 0; |
2127 | ||
8837abca | 2128 | /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */ |
f419a2e3 | 2129 | err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC)); |
1da177e4 LT |
2130 | |
2131 | /* Allow read access to binaries even when mode 111 */ | |
2132 | if (err == -EACCES && S_ISREG(inode->i_mode) && | |
a043226b BF |
2133 | (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) || |
2134 | acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC))) | |
f419a2e3 | 2135 | err = inode_permission(inode, MAY_EXEC); |
1da177e4 LT |
2136 | |
2137 | return err? nfserrno(err) : 0; | |
2138 | } |