Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * File operations used by nfsd. Some of these have been ripped from |
3 | * other parts of the kernel because they weren't exported, others | |
4 | * are partial duplicates with added or changed functionality. | |
5 | * | |
6 | * Note that several functions dget() the dentry upon which they want | |
7 | * to act, most notably those that create directory entries. Response | |
8 | * dentry's are dput()'d if necessary in the release callback. | |
9 | * So if you notice code paths that apparently fail to dput() the | |
10 | * dentry, don't worry--they have been taken care of. | |
11 | * | |
12 | * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de> | |
13 | * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp> | |
14 | */ | |
15 | ||
1da177e4 LT |
16 | #include <linux/fs.h> |
17 | #include <linux/file.h> | |
d6b29d7c | 18 | #include <linux/splice.h> |
1da177e4 | 19 | #include <linux/fcntl.h> |
1da177e4 | 20 | #include <linux/namei.h> |
1da177e4 | 21 | #include <linux/delay.h> |
0eeca283 | 22 | #include <linux/fsnotify.h> |
1da177e4 | 23 | #include <linux/posix_acl_xattr.h> |
1da177e4 | 24 | #include <linux/xattr.h> |
9a74af21 BH |
25 | #include <linux/jhash.h> |
26 | #include <linux/ima.h> | |
5a0e3ad6 | 27 | #include <linux/slab.h> |
9a74af21 | 28 | #include <asm/uaccess.h> |
f501912a BM |
29 | #include <linux/exportfs.h> |
30 | #include <linux/writeback.h> | |
18032ca0 | 31 | #include <linux/security.h> |
9a74af21 BH |
32 | |
33 | #ifdef CONFIG_NFSD_V3 | |
34 | #include "xdr3.h" | |
35 | #endif /* CONFIG_NFSD_V3 */ | |
36 | ||
5be196e5 | 37 | #ifdef CONFIG_NFSD_V4 |
2ca72e17 BF |
38 | #include "acl.h" |
39 | #include "idmap.h" | |
1da177e4 LT |
40 | #endif /* CONFIG_NFSD_V4 */ |
41 | ||
9a74af21 BH |
42 | #include "nfsd.h" |
43 | #include "vfs.h" | |
1da177e4 LT |
44 | |
45 | #define NFSDDBG_FACILITY NFSDDBG_FILEOP | |
1da177e4 LT |
46 | |
47 | ||
1da177e4 LT |
48 | /* |
49 | * This is a cache of readahead params that help us choose the proper | |
50 | * readahead strategy. Initially, we set all readahead parameters to 0 | |
51 | * and let the VFS handle things. | |
52 | * If you increase the number of cached files very much, you'll need to | |
53 | * add a hash table here. | |
54 | */ | |
55 | struct raparms { | |
56 | struct raparms *p_next; | |
57 | unsigned int p_count; | |
58 | ino_t p_ino; | |
59 | dev_t p_dev; | |
60 | int p_set; | |
61 | struct file_ra_state p_ra; | |
fce1456a | 62 | unsigned int p_hindex; |
1da177e4 LT |
63 | }; |
64 | ||
fce1456a GB |
65 | struct raparm_hbucket { |
66 | struct raparms *pb_head; | |
67 | spinlock_t pb_lock; | |
68 | } ____cacheline_aligned_in_smp; | |
69 | ||
fce1456a GB |
70 | #define RAPARM_HASH_BITS 4 |
71 | #define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS) | |
72 | #define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1) | |
73 | static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE]; | |
1da177e4 LT |
74 | |
75 | /* | |
76 | * Called from nfsd_lookup and encode_dirent. Check if we have crossed | |
77 | * a mount point. | |
e0bb89ef | 78 | * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged, |
1da177e4 LT |
79 | * or nfs_ok having possibly changed *dpp and *expp |
80 | */ | |
81 | int | |
82 | nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp, | |
83 | struct svc_export **expp) | |
84 | { | |
85 | struct svc_export *exp = *expp, *exp2 = NULL; | |
86 | struct dentry *dentry = *dpp; | |
91c9fa8f AV |
87 | struct path path = {.mnt = mntget(exp->ex_path.mnt), |
88 | .dentry = dget(dentry)}; | |
6264d69d | 89 | int err = 0; |
1da177e4 | 90 | |
7cc90cc3 | 91 | err = follow_down(&path); |
cc53ce53 DH |
92 | if (err < 0) |
93 | goto out; | |
1da177e4 | 94 | |
91c9fa8f | 95 | exp2 = rqst_exp_get_by_name(rqstp, &path); |
1da177e4 | 96 | if (IS_ERR(exp2)) { |
3b6cee7b BF |
97 | err = PTR_ERR(exp2); |
98 | /* | |
99 | * We normally allow NFS clients to continue | |
100 | * "underneath" a mountpoint that is not exported. | |
101 | * The exception is V4ROOT, where no traversal is ever | |
102 | * allowed without an explicit export of the new | |
103 | * directory. | |
104 | */ | |
105 | if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT)) | |
106 | err = 0; | |
91c9fa8f | 107 | path_put(&path); |
1da177e4 LT |
108 | goto out; |
109 | } | |
3c394dda SD |
110 | if (nfsd_v4client(rqstp) || |
111 | (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) { | |
1da177e4 | 112 | /* successfully crossed mount point */ |
1644ccc8 | 113 | /* |
91c9fa8f AV |
114 | * This is subtle: path.dentry is *not* on path.mnt |
115 | * at this point. The only reason we are safe is that | |
116 | * original mnt is pinned down by exp, so we should | |
117 | * put path *before* putting exp | |
1644ccc8 | 118 | */ |
91c9fa8f AV |
119 | *dpp = path.dentry; |
120 | path.dentry = dentry; | |
1644ccc8 | 121 | *expp = exp2; |
91c9fa8f | 122 | exp2 = exp; |
1da177e4 | 123 | } |
91c9fa8f AV |
124 | path_put(&path); |
125 | exp_put(exp2); | |
1da177e4 LT |
126 | out: |
127 | return err; | |
128 | } | |
129 | ||
289ede45 BF |
130 | static void follow_to_parent(struct path *path) |
131 | { | |
132 | struct dentry *dp; | |
133 | ||
134 | while (path->dentry == path->mnt->mnt_root && follow_up(path)) | |
135 | ; | |
136 | dp = dget_parent(path->dentry); | |
137 | dput(path->dentry); | |
138 | path->dentry = dp; | |
139 | } | |
140 | ||
141 | static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp) | |
142 | { | |
143 | struct svc_export *exp2; | |
144 | struct path path = {.mnt = mntget((*exp)->ex_path.mnt), | |
145 | .dentry = dget(dparent)}; | |
146 | ||
147 | follow_to_parent(&path); | |
148 | ||
149 | exp2 = rqst_exp_parent(rqstp, &path); | |
150 | if (PTR_ERR(exp2) == -ENOENT) { | |
151 | *dentryp = dget(dparent); | |
152 | } else if (IS_ERR(exp2)) { | |
153 | path_put(&path); | |
154 | return PTR_ERR(exp2); | |
155 | } else { | |
156 | *dentryp = dget(path.dentry); | |
157 | exp_put(*exp); | |
158 | *exp = exp2; | |
159 | } | |
160 | path_put(&path); | |
161 | return 0; | |
162 | } | |
163 | ||
82ead7fe BF |
164 | /* |
165 | * For nfsd purposes, we treat V4ROOT exports as though there was an | |
166 | * export at *every* directory. | |
167 | */ | |
3227fa41 | 168 | int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp) |
82ead7fe BF |
169 | { |
170 | if (d_mountpoint(dentry)) | |
171 | return 1; | |
11fcee02 TM |
172 | if (nfsd4_is_junction(dentry)) |
173 | return 1; | |
82ead7fe BF |
174 | if (!(exp->ex_flags & NFSEXP_V4ROOT)) |
175 | return 0; | |
176 | return dentry->d_inode != NULL; | |
177 | } | |
178 | ||
6264d69d | 179 | __be32 |
6c0a654d | 180 | nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp, |
5a022fc8 | 181 | const char *name, unsigned int len, |
6c0a654d | 182 | struct svc_export **exp_ret, struct dentry **dentry_ret) |
1da177e4 LT |
183 | { |
184 | struct svc_export *exp; | |
185 | struct dentry *dparent; | |
186 | struct dentry *dentry; | |
6264d69d | 187 | int host_err; |
1da177e4 LT |
188 | |
189 | dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name); | |
190 | ||
1da177e4 LT |
191 | dparent = fhp->fh_dentry; |
192 | exp = fhp->fh_export; | |
193 | exp_get(exp); | |
194 | ||
1da177e4 LT |
195 | /* Lookup the name, but don't follow links */ |
196 | if (isdotent(name, len)) { | |
197 | if (len==1) | |
198 | dentry = dget(dparent); | |
54775491 | 199 | else if (dparent != exp->ex_path.dentry) |
1da177e4 | 200 | dentry = dget_parent(dparent); |
fed83811 | 201 | else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp)) |
1da177e4 LT |
202 | dentry = dget(dparent); /* .. == . just like at / */ |
203 | else { | |
204 | /* checking mountpoint crossing is very different when stepping up */ | |
289ede45 BF |
205 | host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry); |
206 | if (host_err) | |
1da177e4 | 207 | goto out_nfserr; |
1da177e4 LT |
208 | } |
209 | } else { | |
210 | fh_lock(fhp); | |
211 | dentry = lookup_one_len(name, dparent, len); | |
6264d69d | 212 | host_err = PTR_ERR(dentry); |
1da177e4 LT |
213 | if (IS_ERR(dentry)) |
214 | goto out_nfserr; | |
215 | /* | |
216 | * check if we have crossed a mount point ... | |
217 | */ | |
82ead7fe | 218 | if (nfsd_mountpoint(dentry, exp)) { |
6264d69d | 219 | if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) { |
1da177e4 LT |
220 | dput(dentry); |
221 | goto out_nfserr; | |
222 | } | |
223 | } | |
224 | } | |
6c0a654d BF |
225 | *dentry_ret = dentry; |
226 | *exp_ret = exp; | |
227 | return 0; | |
228 | ||
229 | out_nfserr: | |
230 | exp_put(exp); | |
231 | return nfserrno(host_err); | |
232 | } | |
233 | ||
234 | /* | |
235 | * Look up one component of a pathname. | |
236 | * N.B. After this call _both_ fhp and resfh need an fh_put | |
237 | * | |
238 | * If the lookup would cross a mountpoint, and the mounted filesystem | |
239 | * is exported to the client with NFSEXP_NOHIDE, then the lookup is | |
240 | * accepted as it stands and the mounted directory is | |
241 | * returned. Otherwise the covered directory is returned. | |
242 | * NOTE: this mountpoint crossing is not supported properly by all | |
243 | * clients and is explicitly disallowed for NFSv3 | |
244 | * NeilBrown <neilb@cse.unsw.edu.au> | |
245 | */ | |
246 | __be32 | |
247 | nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name, | |
5a022fc8 | 248 | unsigned int len, struct svc_fh *resfh) |
6c0a654d BF |
249 | { |
250 | struct svc_export *exp; | |
251 | struct dentry *dentry; | |
252 | __be32 err; | |
253 | ||
29a78a3e BF |
254 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC); |
255 | if (err) | |
256 | return err; | |
6c0a654d BF |
257 | err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry); |
258 | if (err) | |
259 | return err; | |
32c1eb0c AA |
260 | err = check_nfsd_access(exp, rqstp); |
261 | if (err) | |
262 | goto out; | |
1da177e4 LT |
263 | /* |
264 | * Note: we compose the file handle now, but as the | |
265 | * dentry may be negative, it may need to be updated. | |
266 | */ | |
267 | err = fh_compose(resfh, exp, dentry, fhp); | |
268 | if (!err && !dentry->d_inode) | |
269 | err = nfserr_noent; | |
32c1eb0c | 270 | out: |
1da177e4 | 271 | dput(dentry); |
1da177e4 LT |
272 | exp_put(exp); |
273 | return err; | |
1da177e4 LT |
274 | } |
275 | ||
f501912a BM |
276 | /* |
277 | * Commit metadata changes to stable storage. | |
278 | */ | |
279 | static int | |
280 | commit_metadata(struct svc_fh *fhp) | |
281 | { | |
282 | struct inode *inode = fhp->fh_dentry->d_inode; | |
283 | const struct export_operations *export_ops = inode->i_sb->s_export_op; | |
f501912a BM |
284 | |
285 | if (!EX_ISSYNC(fhp->fh_export)) | |
286 | return 0; | |
287 | ||
c3765016 CH |
288 | if (export_ops->commit_metadata) |
289 | return export_ops->commit_metadata(inode); | |
290 | return sync_inode_metadata(inode, 1); | |
f501912a | 291 | } |
6c0a654d | 292 | |
1da177e4 | 293 | /* |
818e5a22 CH |
294 | * Go over the attributes and take care of the small differences between |
295 | * NFS semantics and what Linux expects. | |
1da177e4 | 296 | */ |
818e5a22 CH |
297 | static void |
298 | nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap) | |
1da177e4 | 299 | { |
9c85fca5 CH |
300 | /* |
301 | * NFSv2 does not differentiate between "set-[ac]time-to-now" | |
1da177e4 LT |
302 | * which only requires access, and "set-[ac]time-to-X" which |
303 | * requires ownership. | |
304 | * So if it looks like it might be "set both to the same time which | |
305 | * is close to now", and if inode_change_ok fails, then we | |
306 | * convert to "set to now" instead of "set to explicit time" | |
307 | * | |
308 | * We only call inode_change_ok as the last test as technically | |
818e5a22 | 309 | * it is not an interface that we should be using. |
1da177e4 LT |
310 | */ |
311 | #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET) | |
312 | #define MAX_TOUCH_TIME_ERROR (30*60) | |
9c85fca5 CH |
313 | if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET && |
314 | iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) { | |
315 | /* | |
316 | * Looks probable. | |
317 | * | |
318 | * Now just make sure time is in the right ballpark. | |
319 | * Solaris, at least, doesn't seem to care what the time | |
320 | * request is. We require it be within 30 minutes of now. | |
1da177e4 | 321 | */ |
9c85fca5 CH |
322 | time_t delta = iap->ia_atime.tv_sec - get_seconds(); |
323 | if (delta < 0) | |
324 | delta = -delta; | |
325 | if (delta < MAX_TOUCH_TIME_ERROR && | |
326 | inode_change_ok(inode, iap) != 0) { | |
327 | /* | |
328 | * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME. | |
329 | * This will cause notify_change to set these times | |
330 | * to "now" | |
331 | */ | |
332 | iap->ia_valid &= ~BOTH_TIME_SET; | |
333 | } | |
1da177e4 | 334 | } |
1da177e4 | 335 | |
ca456252 | 336 | /* sanitize the mode change */ |
1da177e4 LT |
337 | if (iap->ia_valid & ATTR_MODE) { |
338 | iap->ia_mode &= S_IALLUGO; | |
dee3209d | 339 | iap->ia_mode |= (inode->i_mode & ~S_IALLUGO); |
ca456252 JL |
340 | } |
341 | ||
342 | /* Revoke setuid/setgid on chown */ | |
0953e620 | 343 | if (!S_ISDIR(inode->i_mode) && |
c4fa6d7c | 344 | ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) { |
ca456252 JL |
345 | iap->ia_valid |= ATTR_KILL_PRIV; |
346 | if (iap->ia_valid & ATTR_MODE) { | |
347 | /* we're setting mode too, just clear the s*id bits */ | |
8a0ce7d9 | 348 | iap->ia_mode &= ~S_ISUID; |
ca456252 JL |
349 | if (iap->ia_mode & S_IXGRP) |
350 | iap->ia_mode &= ~S_ISGID; | |
351 | } else { | |
352 | /* set ATTR_KILL_* bits and let VFS handle it */ | |
353 | iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID); | |
8a0ce7d9 | 354 | } |
1da177e4 | 355 | } |
818e5a22 CH |
356 | } |
357 | ||
358 | static __be32 | |
359 | nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp, | |
360 | struct iattr *iap) | |
361 | { | |
362 | struct inode *inode = fhp->fh_dentry->d_inode; | |
363 | int host_err; | |
364 | ||
365 | if (iap->ia_size < inode->i_size) { | |
366 | __be32 err; | |
367 | ||
368 | err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, | |
369 | NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE); | |
370 | if (err) | |
371 | return err; | |
372 | } | |
373 | ||
374 | host_err = get_write_access(inode); | |
375 | if (host_err) | |
376 | goto out_nfserrno; | |
377 | ||
378 | host_err = locks_verify_truncate(inode, NULL, iap->ia_size); | |
379 | if (host_err) | |
380 | goto out_put_write_access; | |
381 | return 0; | |
382 | ||
383 | out_put_write_access: | |
384 | put_write_access(inode); | |
385 | out_nfserrno: | |
386 | return nfserrno(host_err); | |
387 | } | |
388 | ||
389 | /* | |
390 | * Set various file attributes. After this call fhp needs an fh_put. | |
391 | */ | |
392 | __be32 | |
393 | nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap, | |
394 | int check_guard, time_t guardtime) | |
395 | { | |
396 | struct dentry *dentry; | |
397 | struct inode *inode; | |
398 | int accmode = NFSD_MAY_SATTR; | |
399 | umode_t ftype = 0; | |
400 | __be32 err; | |
401 | int host_err; | |
402 | int size_change = 0; | |
403 | ||
404 | if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE)) | |
405 | accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE; | |
406 | if (iap->ia_valid & ATTR_SIZE) | |
407 | ftype = S_IFREG; | |
408 | ||
409 | /* Get inode */ | |
410 | err = fh_verify(rqstp, fhp, ftype, accmode); | |
411 | if (err) | |
412 | goto out; | |
413 | ||
414 | dentry = fhp->fh_dentry; | |
415 | inode = dentry->d_inode; | |
416 | ||
417 | /* Ignore any mode updates on symlinks */ | |
418 | if (S_ISLNK(inode->i_mode)) | |
419 | iap->ia_valid &= ~ATTR_MODE; | |
420 | ||
421 | if (!iap->ia_valid) | |
422 | goto out; | |
1da177e4 | 423 | |
818e5a22 CH |
424 | nfsd_sanitize_attrs(inode, iap); |
425 | ||
426 | /* | |
427 | * The size case is special, it changes the file in addition to the | |
428 | * attributes. | |
429 | */ | |
430 | if (iap->ia_valid & ATTR_SIZE) { | |
431 | err = nfsd_get_write_access(rqstp, fhp, iap); | |
432 | if (err) | |
433 | goto out; | |
434 | size_change = 1; | |
435 | } | |
1da177e4 LT |
436 | |
437 | iap->ia_valid |= ATTR_CTIME; | |
438 | ||
987da479 CH |
439 | if (check_guard && guardtime != inode->i_ctime.tv_sec) { |
440 | err = nfserr_notsync; | |
441 | goto out_put_write_access; | |
1da177e4 | 442 | } |
987da479 | 443 | |
987da479 CH |
444 | fh_lock(fhp); |
445 | host_err = notify_change(dentry, iap, NULL); | |
446 | fh_unlock(fhp); | |
447 | ||
987da479 | 448 | out_put_write_access: |
1da177e4 LT |
449 | if (size_change) |
450 | put_write_access(inode); | |
451 | if (!err) | |
b160fdab | 452 | commit_metadata(fhp); |
1da177e4 LT |
453 | out: |
454 | return err; | |
1da177e4 LT |
455 | } |
456 | ||
5be196e5 CH |
457 | #if defined(CONFIG_NFSD_V2_ACL) || \ |
458 | defined(CONFIG_NFSD_V3_ACL) || \ | |
459 | defined(CONFIG_NFSD_V4) | |
460 | static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf) | |
461 | { | |
462 | ssize_t buflen; | |
6c6a426f | 463 | ssize_t ret; |
5be196e5 CH |
464 | |
465 | buflen = vfs_getxattr(dentry, key, NULL, 0); | |
466 | if (buflen <= 0) | |
467 | return buflen; | |
1da177e4 | 468 | |
5be196e5 CH |
469 | *buf = kmalloc(buflen, GFP_KERNEL); |
470 | if (!*buf) | |
471 | return -ENOMEM; | |
472 | ||
6c6a426f KK |
473 | ret = vfs_getxattr(dentry, key, *buf, buflen); |
474 | if (ret < 0) | |
475 | kfree(*buf); | |
476 | return ret; | |
5be196e5 CH |
477 | } |
478 | #endif | |
479 | ||
480 | #if defined(CONFIG_NFSD_V4) | |
1da177e4 LT |
481 | static int |
482 | set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key) | |
483 | { | |
484 | int len; | |
485 | size_t buflen; | |
486 | char *buf = NULL; | |
487 | int error = 0; | |
1da177e4 LT |
488 | |
489 | buflen = posix_acl_xattr_size(pacl->a_count); | |
490 | buf = kmalloc(buflen, GFP_KERNEL); | |
491 | error = -ENOMEM; | |
492 | if (buf == NULL) | |
493 | goto out; | |
494 | ||
5f3a4a28 | 495 | len = posix_acl_to_xattr(&init_user_ns, pacl, buf, buflen); |
1da177e4 LT |
496 | if (len < 0) { |
497 | error = len; | |
498 | goto out; | |
499 | } | |
500 | ||
5be196e5 | 501 | error = vfs_setxattr(dentry, key, buf, len, 0); |
1da177e4 LT |
502 | out: |
503 | kfree(buf); | |
504 | return error; | |
505 | } | |
506 | ||
6264d69d | 507 | __be32 |
1da177e4 LT |
508 | nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp, |
509 | struct nfs4_acl *acl) | |
510 | { | |
6264d69d AV |
511 | __be32 error; |
512 | int host_error; | |
1da177e4 LT |
513 | struct dentry *dentry; |
514 | struct inode *inode; | |
515 | struct posix_acl *pacl = NULL, *dpacl = NULL; | |
516 | unsigned int flags = 0; | |
517 | ||
518 | /* Get inode */ | |
e281d810 | 519 | error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR); |
1da177e4 | 520 | if (error) |
4b2ca38a | 521 | return error; |
1da177e4 LT |
522 | |
523 | dentry = fhp->fh_dentry; | |
524 | inode = dentry->d_inode; | |
525 | if (S_ISDIR(inode->i_mode)) | |
526 | flags = NFS4_ACL_DIR; | |
527 | ||
6264d69d AV |
528 | host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags); |
529 | if (host_error == -EINVAL) { | |
4b2ca38a | 530 | return nfserr_attrnotsupp; |
6264d69d | 531 | } else if (host_error < 0) |
1da177e4 LT |
532 | goto out_nfserr; |
533 | ||
6264d69d AV |
534 | host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS); |
535 | if (host_error < 0) | |
4b2ca38a | 536 | goto out_release; |
1da177e4 | 537 | |
4b2ca38a | 538 | if (S_ISDIR(inode->i_mode)) |
6264d69d | 539 | host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT); |
1da177e4 | 540 | |
4b2ca38a | 541 | out_release: |
1da177e4 LT |
542 | posix_acl_release(pacl); |
543 | posix_acl_release(dpacl); | |
1da177e4 | 544 | out_nfserr: |
f34f9242 | 545 | if (host_error == -EOPNOTSUPP) |
4b2ca38a | 546 | return nfserr_attrnotsupp; |
f34f9242 | 547 | else |
4b2ca38a | 548 | return nfserrno(host_error); |
1da177e4 LT |
549 | } |
550 | ||
551 | static struct posix_acl * | |
552 | _get_posix_acl(struct dentry *dentry, char *key) | |
553 | { | |
5be196e5 | 554 | void *buf = NULL; |
1da177e4 | 555 | struct posix_acl *pacl = NULL; |
5be196e5 | 556 | int buflen; |
1da177e4 | 557 | |
5be196e5 CH |
558 | buflen = nfsd_getxattr(dentry, key, &buf); |
559 | if (!buflen) | |
560 | buflen = -ENODATA; | |
561 | if (buflen <= 0) | |
562 | return ERR_PTR(buflen); | |
1da177e4 | 563 | |
5f3a4a28 | 564 | pacl = posix_acl_from_xattr(&init_user_ns, buf, buflen); |
1da177e4 LT |
565 | kfree(buf); |
566 | return pacl; | |
1da177e4 LT |
567 | } |
568 | ||
569 | int | |
570 | nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl) | |
571 | { | |
572 | struct inode *inode = dentry->d_inode; | |
573 | int error = 0; | |
574 | struct posix_acl *pacl = NULL, *dpacl = NULL; | |
575 | unsigned int flags = 0; | |
576 | ||
9a59f452 | 577 | pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS); |
1da177e4 LT |
578 | if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA) |
579 | pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL); | |
580 | if (IS_ERR(pacl)) { | |
581 | error = PTR_ERR(pacl); | |
582 | pacl = NULL; | |
583 | goto out; | |
584 | } | |
585 | ||
586 | if (S_ISDIR(inode->i_mode)) { | |
9a59f452 | 587 | dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT); |
1da177e4 LT |
588 | if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA) |
589 | dpacl = NULL; | |
590 | else if (IS_ERR(dpacl)) { | |
591 | error = PTR_ERR(dpacl); | |
592 | dpacl = NULL; | |
593 | goto out; | |
594 | } | |
595 | flags = NFS4_ACL_DIR; | |
596 | } | |
597 | ||
598 | *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags); | |
599 | if (IS_ERR(*acl)) { | |
600 | error = PTR_ERR(*acl); | |
601 | *acl = NULL; | |
602 | } | |
603 | out: | |
604 | posix_acl_release(pacl); | |
605 | posix_acl_release(dpacl); | |
606 | return error; | |
607 | } | |
608 | ||
9b4146e8 CL |
609 | /* |
610 | * NFS junction information is stored in an extended attribute. | |
611 | */ | |
612 | #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs" | |
613 | ||
614 | /** | |
615 | * nfsd4_is_junction - Test if an object could be an NFS junction | |
616 | * | |
617 | * @dentry: object to test | |
618 | * | |
619 | * Returns 1 if "dentry" appears to contain NFS junction information. | |
620 | * Otherwise 0 is returned. | |
621 | */ | |
11fcee02 TM |
622 | int nfsd4_is_junction(struct dentry *dentry) |
623 | { | |
624 | struct inode *inode = dentry->d_inode; | |
625 | ||
626 | if (inode == NULL) | |
627 | return 0; | |
628 | if (inode->i_mode & S_IXUGO) | |
629 | return 0; | |
630 | if (!(inode->i_mode & S_ISVTX)) | |
631 | return 0; | |
9b4146e8 | 632 | if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0) |
11fcee02 TM |
633 | return 0; |
634 | return 1; | |
635 | } | |
18032ca0 DQ |
636 | #ifdef CONFIG_NFSD_V4_SECURITY_LABEL |
637 | __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp, | |
638 | struct xdr_netobj *label) | |
639 | { | |
640 | __be32 error; | |
641 | int host_error; | |
642 | struct dentry *dentry; | |
643 | ||
644 | error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR); | |
645 | if (error) | |
646 | return error; | |
647 | ||
648 | dentry = fhp->fh_dentry; | |
649 | ||
650 | mutex_lock(&dentry->d_inode->i_mutex); | |
651 | host_error = security_inode_setsecctx(dentry, label->data, label->len); | |
652 | mutex_unlock(&dentry->d_inode->i_mutex); | |
653 | return nfserrno(host_error); | |
654 | } | |
655 | #else | |
656 | __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp, | |
657 | struct xdr_netobj *label) | |
658 | { | |
659 | return nfserr_notsupp; | |
660 | } | |
661 | #endif | |
662 | ||
6a85d6c7 | 663 | #endif /* defined(CONFIG_NFSD_V4) */ |
1da177e4 LT |
664 | |
665 | #ifdef CONFIG_NFSD_V3 | |
666 | /* | |
667 | * Check server access rights to a file system object | |
668 | */ | |
669 | struct accessmap { | |
670 | u32 access; | |
671 | int how; | |
672 | }; | |
673 | static struct accessmap nfs3_regaccess[] = { | |
8837abca MS |
674 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
675 | { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC }, | |
676 | { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC }, | |
677 | { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE }, | |
1da177e4 LT |
678 | |
679 | { 0, 0 } | |
680 | }; | |
681 | ||
682 | static struct accessmap nfs3_diraccess[] = { | |
8837abca MS |
683 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
684 | { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC }, | |
685 | { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC}, | |
686 | { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE }, | |
687 | { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE }, | |
1da177e4 LT |
688 | |
689 | { 0, 0 } | |
690 | }; | |
691 | ||
692 | static struct accessmap nfs3_anyaccess[] = { | |
693 | /* Some clients - Solaris 2.6 at least, make an access call | |
694 | * to the server to check for access for things like /dev/null | |
695 | * (which really, the server doesn't care about). So | |
696 | * We provide simple access checking for them, looking | |
697 | * mainly at mode bits, and we make sure to ignore read-only | |
698 | * filesystem checks | |
699 | */ | |
8837abca MS |
700 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
701 | { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC }, | |
702 | { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS }, | |
703 | { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS }, | |
1da177e4 LT |
704 | |
705 | { 0, 0 } | |
706 | }; | |
707 | ||
6264d69d | 708 | __be32 |
1da177e4 LT |
709 | nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported) |
710 | { | |
711 | struct accessmap *map; | |
712 | struct svc_export *export; | |
713 | struct dentry *dentry; | |
714 | u32 query, result = 0, sresult = 0; | |
6264d69d | 715 | __be32 error; |
1da177e4 | 716 | |
8837abca | 717 | error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP); |
1da177e4 LT |
718 | if (error) |
719 | goto out; | |
720 | ||
721 | export = fhp->fh_export; | |
722 | dentry = fhp->fh_dentry; | |
723 | ||
724 | if (S_ISREG(dentry->d_inode->i_mode)) | |
725 | map = nfs3_regaccess; | |
726 | else if (S_ISDIR(dentry->d_inode->i_mode)) | |
727 | map = nfs3_diraccess; | |
728 | else | |
729 | map = nfs3_anyaccess; | |
730 | ||
731 | ||
732 | query = *access; | |
733 | for (; map->access; map++) { | |
734 | if (map->access & query) { | |
6264d69d | 735 | __be32 err2; |
1da177e4 LT |
736 | |
737 | sresult |= map->access; | |
738 | ||
0ec757df | 739 | err2 = nfsd_permission(rqstp, export, dentry, map->how); |
1da177e4 LT |
740 | switch (err2) { |
741 | case nfs_ok: | |
742 | result |= map->access; | |
743 | break; | |
744 | ||
745 | /* the following error codes just mean the access was not allowed, | |
746 | * rather than an error occurred */ | |
747 | case nfserr_rofs: | |
748 | case nfserr_acces: | |
749 | case nfserr_perm: | |
750 | /* simply don't "or" in the access bit. */ | |
751 | break; | |
752 | default: | |
753 | error = err2; | |
754 | goto out; | |
755 | } | |
756 | } | |
757 | } | |
758 | *access = result; | |
759 | if (supported) | |
760 | *supported = sresult; | |
761 | ||
762 | out: | |
763 | return error; | |
764 | } | |
765 | #endif /* CONFIG_NFSD_V3 */ | |
766 | ||
105f4622 BF |
767 | static int nfsd_open_break_lease(struct inode *inode, int access) |
768 | { | |
769 | unsigned int mode; | |
1da177e4 | 770 | |
105f4622 BF |
771 | if (access & NFSD_MAY_NOT_BREAK_LEASE) |
772 | return 0; | |
773 | mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY; | |
774 | return break_lease(inode, mode | O_NONBLOCK); | |
775 | } | |
1da177e4 LT |
776 | |
777 | /* | |
778 | * Open an existing file or directory. | |
999448a8 BS |
779 | * The may_flags argument indicates the type of open (read/write/lock) |
780 | * and additional flags. | |
1da177e4 LT |
781 | * N.B. After this call fhp needs an fh_put |
782 | */ | |
6264d69d | 783 | __be32 |
175a4eb7 | 784 | nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, |
999448a8 | 785 | int may_flags, struct file **filp) |
1da177e4 | 786 | { |
765927b2 | 787 | struct path path; |
1da177e4 | 788 | struct inode *inode; |
6264d69d AV |
789 | int flags = O_RDONLY|O_LARGEFILE; |
790 | __be32 err; | |
91885258 | 791 | int host_err = 0; |
1da177e4 | 792 | |
e0e81739 DH |
793 | validate_process_creds(); |
794 | ||
1da177e4 LT |
795 | /* |
796 | * If we get here, then the client has already done an "open", | |
797 | * and (hopefully) checked permission - so allow OWNER_OVERRIDE | |
798 | * in case a chmod has now revoked permission. | |
d91d0b56 BF |
799 | * |
800 | * Arguably we should also allow the owner override for | |
801 | * directories, but we never have and it doesn't seem to have | |
802 | * caused anyone a problem. If we were to change this, note | |
803 | * also that our filldir callbacks would need a variant of | |
804 | * lookup_one_len that doesn't check permissions. | |
1da177e4 | 805 | */ |
d91d0b56 BF |
806 | if (type == S_IFREG) |
807 | may_flags |= NFSD_MAY_OWNER_OVERRIDE; | |
808 | err = fh_verify(rqstp, fhp, type, may_flags); | |
1da177e4 LT |
809 | if (err) |
810 | goto out; | |
811 | ||
765927b2 AV |
812 | path.mnt = fhp->fh_export->ex_path.mnt; |
813 | path.dentry = fhp->fh_dentry; | |
814 | inode = path.dentry->d_inode; | |
1da177e4 LT |
815 | |
816 | /* Disallow write access to files with the append-only bit set | |
817 | * or any access when mandatory locking enabled | |
818 | */ | |
819 | err = nfserr_perm; | |
999448a8 | 820 | if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE)) |
1da177e4 | 821 | goto out; |
5e7fc436 BF |
822 | /* |
823 | * We must ignore files (but only files) which might have mandatory | |
824 | * locks on them because there is no way to know if the accesser has | |
825 | * the lock. | |
826 | */ | |
827 | if (S_ISREG((inode)->i_mode) && mandatory_lock(inode)) | |
1da177e4 LT |
828 | goto out; |
829 | ||
830 | if (!inode->i_fop) | |
831 | goto out; | |
832 | ||
999448a8 | 833 | host_err = nfsd_open_break_lease(inode, may_flags); |
6264d69d | 834 | if (host_err) /* NOMEM or WOULDBLOCK */ |
1da177e4 LT |
835 | goto out_nfserr; |
836 | ||
999448a8 BS |
837 | if (may_flags & NFSD_MAY_WRITE) { |
838 | if (may_flags & NFSD_MAY_READ) | |
9ecb6a08 BF |
839 | flags = O_RDWR|O_LARGEFILE; |
840 | else | |
841 | flags = O_WRONLY|O_LARGEFILE; | |
1da177e4 | 842 | } |
765927b2 | 843 | *filp = dentry_open(&path, flags, current_cred()); |
e4daf1ff | 844 | if (IS_ERR(*filp)) { |
6264d69d | 845 | host_err = PTR_ERR(*filp); |
e4daf1ff HJ |
846 | *filp = NULL; |
847 | } else { | |
999448a8 BS |
848 | host_err = ima_file_check(*filp, may_flags); |
849 | ||
06effdbb BS |
850 | if (may_flags & NFSD_MAY_64BIT_COOKIE) |
851 | (*filp)->f_mode |= FMODE_64BITHASH; | |
852 | else | |
853 | (*filp)->f_mode |= FMODE_32BITHASH; | |
854 | } | |
855 | ||
1da177e4 | 856 | out_nfserr: |
6264d69d | 857 | err = nfserrno(host_err); |
1da177e4 | 858 | out: |
e0e81739 | 859 | validate_process_creds(); |
1da177e4 LT |
860 | return err; |
861 | } | |
862 | ||
863 | /* | |
864 | * Close a file. | |
865 | */ | |
866 | void | |
867 | nfsd_close(struct file *filp) | |
868 | { | |
869 | fput(filp); | |
870 | } | |
871 | ||
1da177e4 LT |
872 | /* |
873 | * Obtain the readahead parameters for the file | |
874 | * specified by (dev, ino). | |
875 | */ | |
1da177e4 LT |
876 | |
877 | static inline struct raparms * | |
878 | nfsd_get_raparms(dev_t dev, ino_t ino) | |
879 | { | |
880 | struct raparms *ra, **rap, **frap = NULL; | |
881 | int depth = 0; | |
fce1456a GB |
882 | unsigned int hash; |
883 | struct raparm_hbucket *rab; | |
884 | ||
885 | hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK; | |
886 | rab = &raparm_hash[hash]; | |
1da177e4 | 887 | |
fce1456a GB |
888 | spin_lock(&rab->pb_lock); |
889 | for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) { | |
1da177e4 LT |
890 | if (ra->p_ino == ino && ra->p_dev == dev) |
891 | goto found; | |
892 | depth++; | |
893 | if (ra->p_count == 0) | |
894 | frap = rap; | |
895 | } | |
3aa6e0aa | 896 | depth = nfsdstats.ra_size; |
1da177e4 | 897 | if (!frap) { |
fce1456a | 898 | spin_unlock(&rab->pb_lock); |
1da177e4 LT |
899 | return NULL; |
900 | } | |
901 | rap = frap; | |
902 | ra = *frap; | |
903 | ra->p_dev = dev; | |
904 | ra->p_ino = ino; | |
905 | ra->p_set = 0; | |
fce1456a | 906 | ra->p_hindex = hash; |
1da177e4 | 907 | found: |
fce1456a | 908 | if (rap != &rab->pb_head) { |
1da177e4 | 909 | *rap = ra->p_next; |
fce1456a GB |
910 | ra->p_next = rab->pb_head; |
911 | rab->pb_head = ra; | |
1da177e4 LT |
912 | } |
913 | ra->p_count++; | |
914 | nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++; | |
fce1456a | 915 | spin_unlock(&rab->pb_lock); |
1da177e4 LT |
916 | return ra; |
917 | } | |
918 | ||
919 | /* | |
cf8208d0 JA |
920 | * Grab and keep cached pages associated with a file in the svc_rqst |
921 | * so that they can be passed to the network sendmsg/sendpage routines | |
922 | * directly. They will be released after the sending has completed. | |
1da177e4 LT |
923 | */ |
924 | static int | |
cf8208d0 JA |
925 | nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, |
926 | struct splice_desc *sd) | |
1da177e4 | 927 | { |
cf8208d0 | 928 | struct svc_rqst *rqstp = sd->u.data; |
afc59400 | 929 | struct page **pp = rqstp->rq_next_page; |
cf8208d0 JA |
930 | struct page *page = buf->page; |
931 | size_t size; | |
cf8208d0 JA |
932 | |
933 | size = sd->len; | |
1da177e4 LT |
934 | |
935 | if (rqstp->rq_res.page_len == 0) { | |
936 | get_page(page); | |
afc59400 BF |
937 | put_page(*rqstp->rq_next_page); |
938 | *(rqstp->rq_next_page++) = page; | |
cf8208d0 | 939 | rqstp->rq_res.page_base = buf->offset; |
1da177e4 | 940 | rqstp->rq_res.page_len = size; |
44524359 | 941 | } else if (page != pp[-1]) { |
1da177e4 | 942 | get_page(page); |
afc59400 BF |
943 | if (*rqstp->rq_next_page) |
944 | put_page(*rqstp->rq_next_page); | |
945 | *(rqstp->rq_next_page++) = page; | |
1da177e4 | 946 | rqstp->rq_res.page_len += size; |
44524359 | 947 | } else |
1da177e4 | 948 | rqstp->rq_res.page_len += size; |
1da177e4 | 949 | |
1da177e4 LT |
950 | return size; |
951 | } | |
952 | ||
cf8208d0 JA |
953 | static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe, |
954 | struct splice_desc *sd) | |
955 | { | |
956 | return __splice_from_pipe(pipe, sd, nfsd_splice_actor); | |
957 | } | |
958 | ||
6264d69d | 959 | static __be32 |
1da177e4 LT |
960 | nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file, |
961 | loff_t offset, struct kvec *vec, int vlen, unsigned long *count) | |
962 | { | |
1da177e4 | 963 | mm_segment_t oldfs; |
6264d69d AV |
964 | __be32 err; |
965 | int host_err; | |
1da177e4 LT |
966 | |
967 | err = nfserr_perm; | |
a8754bee | 968 | |
cf8208d0 JA |
969 | if (file->f_op->splice_read && rqstp->rq_splice_ok) { |
970 | struct splice_desc sd = { | |
971 | .len = 0, | |
972 | .total_len = *count, | |
973 | .pos = offset, | |
974 | .u.data = rqstp, | |
975 | }; | |
976 | ||
afc59400 | 977 | rqstp->rq_next_page = rqstp->rq_respages + 1; |
cf8208d0 | 978 | host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor); |
1da177e4 LT |
979 | } else { |
980 | oldfs = get_fs(); | |
981 | set_fs(KERNEL_DS); | |
6264d69d | 982 | host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset); |
1da177e4 LT |
983 | set_fs(oldfs); |
984 | } | |
985 | ||
6264d69d AV |
986 | if (host_err >= 0) { |
987 | nfsdstats.io_read += host_err; | |
988 | *count = host_err; | |
1da177e4 | 989 | err = 0; |
2a12a9d7 | 990 | fsnotify_access(file); |
1da177e4 | 991 | } else |
6264d69d | 992 | err = nfserrno(host_err); |
1da177e4 LT |
993 | return err; |
994 | } | |
995 | ||
9f708e40 NB |
996 | static void kill_suid(struct dentry *dentry) |
997 | { | |
998 | struct iattr ia; | |
b5376771 | 999 | ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV; |
9f708e40 | 1000 | |
1b1dcc1b | 1001 | mutex_lock(&dentry->d_inode->i_mutex); |
27ac0ffe BF |
1002 | /* |
1003 | * Note we call this on write, so notify_change will not | |
1004 | * encounter any conflicting delegations: | |
1005 | */ | |
1006 | notify_change(dentry, &ia, NULL); | |
1b1dcc1b | 1007 | mutex_unlock(&dentry->d_inode->i_mutex); |
9f708e40 NB |
1008 | } |
1009 | ||
d911df7b BF |
1010 | /* |
1011 | * Gathered writes: If another process is currently writing to the file, | |
1012 | * there's a high chance this is another nfsd (triggered by a bulk write | |
1013 | * from a client's biod). Rather than syncing the file with each write | |
1014 | * request, we sleep for 10 msec. | |
1015 | * | |
1016 | * I don't know if this roughly approximates C. Juszak's idea of | |
1017 | * gathered writes, but it's a nice and simple solution (IMHO), and it | |
1018 | * seems to work:-) | |
1019 | * | |
1020 | * Note: we do this only in the NFSv2 case, since v3 and higher have a | |
1021 | * better tool (separate unstable writes and commits) for solving this | |
1022 | * problem. | |
1023 | */ | |
1024 | static int wait_for_concurrent_writes(struct file *file) | |
1025 | { | |
496ad9aa | 1026 | struct inode *inode = file_inode(file); |
d911df7b BF |
1027 | static ino_t last_ino; |
1028 | static dev_t last_dev; | |
1029 | int err = 0; | |
1030 | ||
1031 | if (atomic_read(&inode->i_writecount) > 1 | |
1032 | || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) { | |
1033 | dprintk("nfsd: write defer %d\n", task_pid_nr(current)); | |
1034 | msleep(10); | |
1035 | dprintk("nfsd: write resume %d\n", task_pid_nr(current)); | |
1036 | } | |
1037 | ||
1038 | if (inode->i_state & I_DIRTY) { | |
1039 | dprintk("nfsd: write sync %d\n", task_pid_nr(current)); | |
8018ab05 | 1040 | err = vfs_fsync(file, 0); |
d911df7b BF |
1041 | } |
1042 | last_ino = inode->i_ino; | |
1043 | last_dev = inode->i_sb->s_dev; | |
1044 | return err; | |
1045 | } | |
1046 | ||
6264d69d | 1047 | static __be32 |
1da177e4 LT |
1048 | nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file, |
1049 | loff_t offset, struct kvec *vec, int vlen, | |
31dec253 | 1050 | unsigned long *cnt, int *stablep) |
1da177e4 LT |
1051 | { |
1052 | struct svc_export *exp; | |
1053 | struct dentry *dentry; | |
1054 | struct inode *inode; | |
1055 | mm_segment_t oldfs; | |
6264d69d AV |
1056 | __be32 err = 0; |
1057 | int host_err; | |
1da177e4 | 1058 | int stable = *stablep; |
48e03bc5 | 1059 | int use_wgather; |
e49dbbf3 | 1060 | loff_t pos = offset; |
1da177e4 | 1061 | |
7eaa36e2 | 1062 | dentry = file->f_path.dentry; |
1da177e4 LT |
1063 | inode = dentry->d_inode; |
1064 | exp = fhp->fh_export; | |
1065 | ||
48e03bc5 | 1066 | use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp); |
1da177e4 | 1067 | |
1da177e4 LT |
1068 | if (!EX_ISSYNC(exp)) |
1069 | stable = 0; | |
1da177e4 LT |
1070 | |
1071 | /* Write the data. */ | |
1072 | oldfs = get_fs(); set_fs(KERNEL_DS); | |
e49dbbf3 | 1073 | host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos); |
1da177e4 | 1074 | set_fs(oldfs); |
e4636d53 BF |
1075 | if (host_err < 0) |
1076 | goto out_nfserr; | |
1077 | *cnt = host_err; | |
1078 | nfsdstats.io_write += host_err; | |
2a12a9d7 | 1079 | fsnotify_modify(file); |
1da177e4 LT |
1080 | |
1081 | /* clear setuid/setgid flag after write */ | |
e4636d53 | 1082 | if (inode->i_mode & (S_ISUID | S_ISGID)) |
9f708e40 | 1083 | kill_suid(dentry); |
1da177e4 | 1084 | |
face1502 BF |
1085 | if (stable) { |
1086 | if (use_wgather) | |
1087 | host_err = wait_for_concurrent_writes(file); | |
1088 | else | |
1089 | host_err = vfs_fsync_range(file, offset, offset+*cnt, 0); | |
1090 | } | |
1da177e4 | 1091 | |
e4636d53 | 1092 | out_nfserr: |
6264d69d | 1093 | dprintk("nfsd: write complete host_err=%d\n", host_err); |
a0d24b29 | 1094 | if (host_err >= 0) |
1da177e4 | 1095 | err = 0; |
a0d24b29 | 1096 | else |
6264d69d | 1097 | err = nfserrno(host_err); |
1da177e4 LT |
1098 | return err; |
1099 | } | |
1100 | ||
1101 | /* | |
1102 | * Read data from a file. count must contain the requested read count | |
1103 | * on entry. On return, *count contains the number of bytes actually read. | |
1104 | * N.B. After this call fhp needs an fh_put | |
1105 | */ | |
039a87ca | 1106 | __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, |
fa0a2126 BF |
1107 | loff_t offset, struct kvec *vec, int vlen, unsigned long *count) |
1108 | { | |
1109 | struct file *file; | |
1110 | struct inode *inode; | |
1111 | struct raparms *ra; | |
1112 | __be32 err; | |
1113 | ||
1114 | err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); | |
1115 | if (err) | |
1116 | return err; | |
1117 | ||
496ad9aa | 1118 | inode = file_inode(file); |
fa0a2126 BF |
1119 | |
1120 | /* Get readahead parameters */ | |
1121 | ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino); | |
1122 | ||
1123 | if (ra && ra->p_set) | |
1124 | file->f_ra = ra->p_ra; | |
1125 | ||
1126 | err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count); | |
1127 | ||
1128 | /* Write back readahead params */ | |
1129 | if (ra) { | |
1130 | struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex]; | |
1131 | spin_lock(&rab->pb_lock); | |
1132 | ra->p_ra = file->f_ra; | |
1133 | ra->p_set = 1; | |
1134 | ra->p_count--; | |
1135 | spin_unlock(&rab->pb_lock); | |
1136 | } | |
1137 | ||
1138 | nfsd_close(file); | |
1139 | return err; | |
1140 | } | |
1141 | ||
039a87ca | 1142 | /* As above, but use the provided file descriptor. */ |
6264d69d | 1143 | __be32 |
039a87ca | 1144 | nfsd_read_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file, |
1da177e4 LT |
1145 | loff_t offset, struct kvec *vec, int vlen, |
1146 | unsigned long *count) | |
1147 | { | |
6264d69d | 1148 | __be32 err; |
1da177e4 LT |
1149 | |
1150 | if (file) { | |
0ec757df | 1151 | err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
8837abca | 1152 | NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE); |
1da177e4 LT |
1153 | if (err) |
1154 | goto out; | |
1155 | err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count); | |
039a87ca BF |
1156 | } else /* Note file may still be NULL in NFSv4 special stateid case: */ |
1157 | err = nfsd_read(rqstp, fhp, offset, vec, vlen, count); | |
1da177e4 LT |
1158 | out: |
1159 | return err; | |
1160 | } | |
1161 | ||
1162 | /* | |
1163 | * Write data to a file. | |
1164 | * The stable flag requests synchronous writes. | |
1165 | * N.B. After this call fhp needs an fh_put | |
1166 | */ | |
6264d69d | 1167 | __be32 |
1da177e4 | 1168 | nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file, |
31dec253 | 1169 | loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt, |
1da177e4 LT |
1170 | int *stablep) |
1171 | { | |
6264d69d | 1172 | __be32 err = 0; |
1da177e4 LT |
1173 | |
1174 | if (file) { | |
0ec757df | 1175 | err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
8837abca | 1176 | NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE); |
1da177e4 LT |
1177 | if (err) |
1178 | goto out; | |
1179 | err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt, | |
1180 | stablep); | |
1181 | } else { | |
8837abca | 1182 | err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file); |
1da177e4 LT |
1183 | if (err) |
1184 | goto out; | |
1185 | ||
1186 | if (cnt) | |
1187 | err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, | |
1188 | cnt, stablep); | |
1189 | nfsd_close(file); | |
1190 | } | |
1191 | out: | |
1192 | return err; | |
1193 | } | |
1194 | ||
1195 | #ifdef CONFIG_NFSD_V3 | |
1196 | /* | |
1197 | * Commit all pending writes to stable storage. | |
aa696a6f TM |
1198 | * |
1199 | * Note: we only guarantee that data that lies within the range specified | |
1200 | * by the 'offset' and 'count' parameters will be synced. | |
1da177e4 LT |
1201 | * |
1202 | * Unfortunately we cannot lock the file to make sure we return full WCC | |
1203 | * data to the client, as locking happens lower down in the filesystem. | |
1204 | */ | |
6264d69d | 1205 | __be32 |
1da177e4 LT |
1206 | nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp, |
1207 | loff_t offset, unsigned long count) | |
1208 | { | |
1209 | struct file *file; | |
aa696a6f TM |
1210 | loff_t end = LLONG_MAX; |
1211 | __be32 err = nfserr_inval; | |
1da177e4 | 1212 | |
aa696a6f TM |
1213 | if (offset < 0) |
1214 | goto out; | |
1215 | if (count != 0) { | |
1216 | end = offset + (loff_t)count - 1; | |
1217 | if (end < offset) | |
1218 | goto out; | |
1219 | } | |
1da177e4 | 1220 | |
91885258 JL |
1221 | err = nfsd_open(rqstp, fhp, S_IFREG, |
1222 | NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file); | |
8837abca | 1223 | if (err) |
aa696a6f | 1224 | goto out; |
1da177e4 | 1225 | if (EX_ISSYNC(fhp->fh_export)) { |
8018ab05 | 1226 | int err2 = vfs_fsync_range(file, offset, end, 0); |
aa696a6f TM |
1227 | |
1228 | if (err2 != -EINVAL) | |
1229 | err = nfserrno(err2); | |
1230 | else | |
1da177e4 | 1231 | err = nfserr_notsupp; |
1da177e4 LT |
1232 | } |
1233 | ||
1234 | nfsd_close(file); | |
aa696a6f | 1235 | out: |
1da177e4 LT |
1236 | return err; |
1237 | } | |
1238 | #endif /* CONFIG_NFSD_V3 */ | |
1239 | ||
f2b0dee2 | 1240 | static __be32 |
5c002b3b BF |
1241 | nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp, |
1242 | struct iattr *iap) | |
1243 | { | |
1244 | /* | |
1245 | * Mode has already been set earlier in create: | |
1246 | */ | |
1247 | iap->ia_valid &= ~ATTR_MODE; | |
1248 | /* | |
1249 | * Setting uid/gid works only for root. Irix appears to | |
1250 | * send along the gid on create when it tries to implement | |
1251 | * setgid directories via NFS: | |
1252 | */ | |
6fab8779 | 1253 | if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) |
5c002b3b BF |
1254 | iap->ia_valid &= ~(ATTR_UID|ATTR_GID); |
1255 | if (iap->ia_valid) | |
1256 | return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0); | |
1257 | return 0; | |
1258 | } | |
1259 | ||
4ac35c2f | 1260 | /* HPUX client sometimes creates a file in mode 000, and sets size to 0. |
1261 | * setting size to 0 may fail for some specific file systems by the permission | |
1262 | * checking which requires WRITE permission but the mode is 000. | |
1263 | * we ignore the resizing(to 0) on the just new created file, since the size is | |
1264 | * 0 after file created. | |
1265 | * | |
1266 | * call this only after vfs_create() is called. | |
1267 | * */ | |
1268 | static void | |
1269 | nfsd_check_ignore_resizing(struct iattr *iap) | |
1270 | { | |
1271 | if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0)) | |
1272 | iap->ia_valid &= ~ATTR_SIZE; | |
1273 | } | |
1274 | ||
1da177e4 LT |
1275 | /* |
1276 | * Create a file (regular, directory, device, fifo); UNIX sockets | |
1277 | * not yet implemented. | |
1278 | * If the response fh has been verified, the parent directory should | |
1279 | * already be locked. Note that the parent directory is left locked. | |
1280 | * | |
1281 | * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp | |
1282 | */ | |
6264d69d | 1283 | __be32 |
1da177e4 LT |
1284 | nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp, |
1285 | char *fname, int flen, struct iattr *iap, | |
1286 | int type, dev_t rdev, struct svc_fh *resfhp) | |
1287 | { | |
1288 | struct dentry *dentry, *dchild = NULL; | |
1289 | struct inode *dirp; | |
6264d69d | 1290 | __be32 err; |
5c002b3b | 1291 | __be32 err2; |
6264d69d | 1292 | int host_err; |
1da177e4 LT |
1293 | |
1294 | err = nfserr_perm; | |
1295 | if (!flen) | |
1296 | goto out; | |
1297 | err = nfserr_exist; | |
1298 | if (isdotent(fname, flen)) | |
1299 | goto out; | |
1300 | ||
8837abca | 1301 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE); |
1da177e4 LT |
1302 | if (err) |
1303 | goto out; | |
1304 | ||
1305 | dentry = fhp->fh_dentry; | |
1306 | dirp = dentry->d_inode; | |
1307 | ||
1308 | err = nfserr_notdir; | |
acfa4380 | 1309 | if (!dirp->i_op->lookup) |
1da177e4 LT |
1310 | goto out; |
1311 | /* | |
1312 | * Check whether the response file handle has been verified yet. | |
1313 | * If it has, the parent directory should already be locked. | |
1314 | */ | |
1315 | if (!resfhp->fh_dentry) { | |
4a55c101 JK |
1316 | host_err = fh_want_write(fhp); |
1317 | if (host_err) | |
1318 | goto out_nfserr; | |
1319 | ||
1da177e4 | 1320 | /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */ |
12fd3520 | 1321 | fh_lock_nested(fhp, I_MUTEX_PARENT); |
1da177e4 | 1322 | dchild = lookup_one_len(fname, dentry, flen); |
6264d69d | 1323 | host_err = PTR_ERR(dchild); |
1da177e4 LT |
1324 | if (IS_ERR(dchild)) |
1325 | goto out_nfserr; | |
1326 | err = fh_compose(resfhp, fhp->fh_export, dchild, fhp); | |
1327 | if (err) | |
1328 | goto out; | |
1329 | } else { | |
1330 | /* called from nfsd_proc_create */ | |
1331 | dchild = dget(resfhp->fh_dentry); | |
1332 | if (!fhp->fh_locked) { | |
1333 | /* not actually possible */ | |
1334 | printk(KERN_ERR | |
a6a9f18f AV |
1335 | "nfsd_create: parent %pd2 not locked!\n", |
1336 | dentry); | |
d75f2b9f | 1337 | err = nfserr_io; |
1da177e4 LT |
1338 | goto out; |
1339 | } | |
1340 | } | |
1341 | /* | |
1342 | * Make sure the child dentry is still negative ... | |
1343 | */ | |
1344 | err = nfserr_exist; | |
1345 | if (dchild->d_inode) { | |
a6a9f18f AV |
1346 | dprintk("nfsd_create: dentry %pd/%pd not negative!\n", |
1347 | dentry, dchild); | |
1da177e4 LT |
1348 | goto out; |
1349 | } | |
1350 | ||
1351 | if (!(iap->ia_valid & ATTR_MODE)) | |
1352 | iap->ia_mode = 0; | |
1353 | iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type; | |
1354 | ||
07cad1d2 MS |
1355 | err = nfserr_inval; |
1356 | if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) { | |
1357 | printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n", | |
1358 | type); | |
1359 | goto out; | |
1360 | } | |
1361 | ||
1da177e4 LT |
1362 | /* |
1363 | * Get the dir op function pointer. | |
1364 | */ | |
088406bc | 1365 | err = 0; |
4a55c101 | 1366 | host_err = 0; |
1da177e4 LT |
1367 | switch (type) { |
1368 | case S_IFREG: | |
312b63fb | 1369 | host_err = vfs_create(dirp, dchild, iap->ia_mode, true); |
4ac35c2f | 1370 | if (!host_err) |
1371 | nfsd_check_ignore_resizing(iap); | |
1da177e4 LT |
1372 | break; |
1373 | case S_IFDIR: | |
6264d69d | 1374 | host_err = vfs_mkdir(dirp, dchild, iap->ia_mode); |
1da177e4 LT |
1375 | break; |
1376 | case S_IFCHR: | |
1377 | case S_IFBLK: | |
1378 | case S_IFIFO: | |
1379 | case S_IFSOCK: | |
6264d69d | 1380 | host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev); |
1da177e4 | 1381 | break; |
1da177e4 | 1382 | } |
4a55c101 | 1383 | if (host_err < 0) |
1da177e4 LT |
1384 | goto out_nfserr; |
1385 | ||
f501912a | 1386 | err = nfsd_create_setattr(rqstp, resfhp, iap); |
1da177e4 | 1387 | |
f501912a BM |
1388 | /* |
1389 | * nfsd_setattr already committed the child. Transactional filesystems | |
1390 | * had a chance to commit changes for both parent and child | |
1391 | * simultaneously making the following commit_metadata a noop. | |
1392 | */ | |
1393 | err2 = nfserrno(commit_metadata(fhp)); | |
5c002b3b BF |
1394 | if (err2) |
1395 | err = err2; | |
1da177e4 LT |
1396 | /* |
1397 | * Update the file handle to get the new inode info. | |
1398 | */ | |
1399 | if (!err) | |
1400 | err = fh_update(resfhp); | |
1401 | out: | |
1402 | if (dchild && !IS_ERR(dchild)) | |
1403 | dput(dchild); | |
1404 | return err; | |
1405 | ||
1406 | out_nfserr: | |
6264d69d | 1407 | err = nfserrno(host_err); |
1da177e4 LT |
1408 | goto out; |
1409 | } | |
1410 | ||
1411 | #ifdef CONFIG_NFSD_V3 | |
ac6721a1 MJ |
1412 | |
1413 | static inline int nfsd_create_is_exclusive(int createmode) | |
1414 | { | |
1415 | return createmode == NFS3_CREATE_EXCLUSIVE | |
1416 | || createmode == NFS4_CREATE_EXCLUSIVE4_1; | |
1417 | } | |
1418 | ||
1da177e4 | 1419 | /* |
ac6721a1 | 1420 | * NFSv3 and NFSv4 version of nfsd_create |
1da177e4 | 1421 | */ |
6264d69d | 1422 | __be32 |
ac6721a1 | 1423 | do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp, |
1da177e4 LT |
1424 | char *fname, int flen, struct iattr *iap, |
1425 | struct svc_fh *resfhp, int createmode, u32 *verifier, | |
856121b2 | 1426 | bool *truncp, bool *created) |
1da177e4 LT |
1427 | { |
1428 | struct dentry *dentry, *dchild = NULL; | |
1429 | struct inode *dirp; | |
6264d69d AV |
1430 | __be32 err; |
1431 | int host_err; | |
1da177e4 | 1432 | __u32 v_mtime=0, v_atime=0; |
1da177e4 LT |
1433 | |
1434 | err = nfserr_perm; | |
1435 | if (!flen) | |
1436 | goto out; | |
1437 | err = nfserr_exist; | |
1438 | if (isdotent(fname, flen)) | |
1439 | goto out; | |
1440 | if (!(iap->ia_valid & ATTR_MODE)) | |
1441 | iap->ia_mode = 0; | |
1574dff8 | 1442 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC); |
1da177e4 LT |
1443 | if (err) |
1444 | goto out; | |
1445 | ||
1446 | dentry = fhp->fh_dentry; | |
1447 | dirp = dentry->d_inode; | |
1448 | ||
1449 | /* Get all the sanity checks out of the way before | |
1450 | * we lock the parent. */ | |
1451 | err = nfserr_notdir; | |
acfa4380 | 1452 | if (!dirp->i_op->lookup) |
1da177e4 | 1453 | goto out; |
4a55c101 JK |
1454 | |
1455 | host_err = fh_want_write(fhp); | |
1456 | if (host_err) | |
1457 | goto out_nfserr; | |
1458 | ||
12fd3520 | 1459 | fh_lock_nested(fhp, I_MUTEX_PARENT); |
1da177e4 LT |
1460 | |
1461 | /* | |
1462 | * Compose the response file handle. | |
1463 | */ | |
1464 | dchild = lookup_one_len(fname, dentry, flen); | |
6264d69d | 1465 | host_err = PTR_ERR(dchild); |
1da177e4 LT |
1466 | if (IS_ERR(dchild)) |
1467 | goto out_nfserr; | |
1468 | ||
1574dff8 SP |
1469 | /* If file doesn't exist, check for permissions to create one */ |
1470 | if (!dchild->d_inode) { | |
1471 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE); | |
1472 | if (err) | |
1473 | goto out; | |
1474 | } | |
1475 | ||
1da177e4 LT |
1476 | err = fh_compose(resfhp, fhp->fh_export, dchild, fhp); |
1477 | if (err) | |
1478 | goto out; | |
1479 | ||
ac6721a1 | 1480 | if (nfsd_create_is_exclusive(createmode)) { |
c397852c | 1481 | /* solaris7 gets confused (bugid 4218508) if these have |
749997e5 JL |
1482 | * the high bit set, so just clear the high bits. If this is |
1483 | * ever changed to use different attrs for storing the | |
1484 | * verifier, then do_open_lookup() will also need to be fixed | |
1485 | * accordingly. | |
1da177e4 LT |
1486 | */ |
1487 | v_mtime = verifier[0]&0x7fffffff; | |
1488 | v_atime = verifier[1]&0x7fffffff; | |
1da177e4 LT |
1489 | } |
1490 | ||
1491 | if (dchild->d_inode) { | |
1492 | err = 0; | |
1493 | ||
1494 | switch (createmode) { | |
1495 | case NFS3_CREATE_UNCHECKED: | |
1496 | if (! S_ISREG(dchild->d_inode->i_mode)) | |
9dc4e6c4 | 1497 | goto out; |
1da177e4 LT |
1498 | else if (truncp) { |
1499 | /* in nfsv4, we need to treat this case a little | |
1500 | * differently. we don't want to truncate the | |
1501 | * file now; this would be wrong if the OPEN | |
1502 | * fails for some other reason. furthermore, | |
1503 | * if the size is nonzero, we should ignore it | |
1504 | * according to spec! | |
1505 | */ | |
1506 | *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size; | |
1507 | } | |
1508 | else { | |
1509 | iap->ia_valid &= ATTR_SIZE; | |
1510 | goto set_attr; | |
1511 | } | |
1512 | break; | |
1513 | case NFS3_CREATE_EXCLUSIVE: | |
1514 | if ( dchild->d_inode->i_mtime.tv_sec == v_mtime | |
1515 | && dchild->d_inode->i_atime.tv_sec == v_atime | |
7007c90f NB |
1516 | && dchild->d_inode->i_size == 0 ) { |
1517 | if (created) | |
1518 | *created = 1; | |
1da177e4 | 1519 | break; |
7007c90f | 1520 | } |
ac6721a1 MJ |
1521 | case NFS4_CREATE_EXCLUSIVE4_1: |
1522 | if ( dchild->d_inode->i_mtime.tv_sec == v_mtime | |
1523 | && dchild->d_inode->i_atime.tv_sec == v_atime | |
7007c90f NB |
1524 | && dchild->d_inode->i_size == 0 ) { |
1525 | if (created) | |
1526 | *created = 1; | |
ac6721a1 | 1527 | goto set_attr; |
7007c90f | 1528 | } |
1da177e4 LT |
1529 | /* fallthru */ |
1530 | case NFS3_CREATE_GUARDED: | |
1531 | err = nfserr_exist; | |
1532 | } | |
bad0dcff | 1533 | fh_drop_write(fhp); |
1da177e4 LT |
1534 | goto out; |
1535 | } | |
1536 | ||
312b63fb | 1537 | host_err = vfs_create(dirp, dchild, iap->ia_mode, true); |
463c3197 | 1538 | if (host_err < 0) { |
bad0dcff | 1539 | fh_drop_write(fhp); |
1da177e4 | 1540 | goto out_nfserr; |
463c3197 | 1541 | } |
81ac95c5 BF |
1542 | if (created) |
1543 | *created = 1; | |
1da177e4 | 1544 | |
4ac35c2f | 1545 | nfsd_check_ignore_resizing(iap); |
1546 | ||
ac6721a1 | 1547 | if (nfsd_create_is_exclusive(createmode)) { |
c397852c | 1548 | /* Cram the verifier into atime/mtime */ |
1da177e4 | 1549 | iap->ia_valid = ATTR_MTIME|ATTR_ATIME |
c397852c | 1550 | | ATTR_MTIME_SET|ATTR_ATIME_SET; |
1da177e4 LT |
1551 | /* XXX someone who knows this better please fix it for nsec */ |
1552 | iap->ia_mtime.tv_sec = v_mtime; | |
1553 | iap->ia_atime.tv_sec = v_atime; | |
1554 | iap->ia_mtime.tv_nsec = 0; | |
1555 | iap->ia_atime.tv_nsec = 0; | |
1da177e4 LT |
1556 | } |
1557 | ||
1da177e4 | 1558 | set_attr: |
f501912a BM |
1559 | err = nfsd_create_setattr(rqstp, resfhp, iap); |
1560 | ||
1561 | /* | |
1562 | * nfsd_setattr already committed the child (and possibly also the parent). | |
1563 | */ | |
1564 | if (!err) | |
1565 | err = nfserrno(commit_metadata(fhp)); | |
f193fbab YT |
1566 | |
1567 | /* | |
1568 | * Update the filehandle to get the new inode info. | |
1569 | */ | |
1570 | if (!err) | |
1571 | err = fh_update(resfhp); | |
1da177e4 LT |
1572 | |
1573 | out: | |
1574 | fh_unlock(fhp); | |
1575 | if (dchild && !IS_ERR(dchild)) | |
1576 | dput(dchild); | |
4a55c101 | 1577 | fh_drop_write(fhp); |
1da177e4 LT |
1578 | return err; |
1579 | ||
1580 | out_nfserr: | |
6264d69d | 1581 | err = nfserrno(host_err); |
1da177e4 LT |
1582 | goto out; |
1583 | } | |
1584 | #endif /* CONFIG_NFSD_V3 */ | |
1585 | ||
1586 | /* | |
1587 | * Read a symlink. On entry, *lenp must contain the maximum path length that | |
1588 | * fits into the buffer. On return, it contains the true length. | |
1589 | * N.B. After this call fhp needs an fh_put | |
1590 | */ | |
6264d69d | 1591 | __be32 |
1da177e4 LT |
1592 | nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp) |
1593 | { | |
1da177e4 LT |
1594 | struct inode *inode; |
1595 | mm_segment_t oldfs; | |
6264d69d AV |
1596 | __be32 err; |
1597 | int host_err; | |
68ac1234 | 1598 | struct path path; |
1da177e4 | 1599 | |
8837abca | 1600 | err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP); |
1da177e4 LT |
1601 | if (err) |
1602 | goto out; | |
1603 | ||
68ac1234 AV |
1604 | path.mnt = fhp->fh_export->ex_path.mnt; |
1605 | path.dentry = fhp->fh_dentry; | |
1606 | inode = path.dentry->d_inode; | |
1da177e4 LT |
1607 | |
1608 | err = nfserr_inval; | |
acfa4380 | 1609 | if (!inode->i_op->readlink) |
1da177e4 LT |
1610 | goto out; |
1611 | ||
68ac1234 | 1612 | touch_atime(&path); |
1da177e4 LT |
1613 | /* N.B. Why does this call need a get_fs()?? |
1614 | * Remove the set_fs and watch the fireworks:-) --okir | |
1615 | */ | |
1616 | ||
1617 | oldfs = get_fs(); set_fs(KERNEL_DS); | |
fac7a17b | 1618 | host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp); |
1da177e4 LT |
1619 | set_fs(oldfs); |
1620 | ||
6264d69d | 1621 | if (host_err < 0) |
1da177e4 | 1622 | goto out_nfserr; |
6264d69d | 1623 | *lenp = host_err; |
1da177e4 LT |
1624 | err = 0; |
1625 | out: | |
1626 | return err; | |
1627 | ||
1628 | out_nfserr: | |
6264d69d | 1629 | err = nfserrno(host_err); |
1da177e4 LT |
1630 | goto out; |
1631 | } | |
1632 | ||
1633 | /* | |
1634 | * Create a symlink and look up its inode | |
1635 | * N.B. After this call _both_ fhp and resfhp need an fh_put | |
1636 | */ | |
6264d69d | 1637 | __be32 |
1da177e4 LT |
1638 | nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp, |
1639 | char *fname, int flen, | |
1640 | char *path, int plen, | |
1641 | struct svc_fh *resfhp, | |
1642 | struct iattr *iap) | |
1643 | { | |
1644 | struct dentry *dentry, *dnew; | |
6264d69d AV |
1645 | __be32 err, cerr; |
1646 | int host_err; | |
1da177e4 LT |
1647 | |
1648 | err = nfserr_noent; | |
1649 | if (!flen || !plen) | |
1650 | goto out; | |
1651 | err = nfserr_exist; | |
1652 | if (isdotent(fname, flen)) | |
1653 | goto out; | |
1654 | ||
8837abca | 1655 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE); |
1da177e4 LT |
1656 | if (err) |
1657 | goto out; | |
4a55c101 JK |
1658 | |
1659 | host_err = fh_want_write(fhp); | |
1660 | if (host_err) | |
1661 | goto out_nfserr; | |
1662 | ||
1da177e4 LT |
1663 | fh_lock(fhp); |
1664 | dentry = fhp->fh_dentry; | |
1665 | dnew = lookup_one_len(fname, dentry, flen); | |
6264d69d | 1666 | host_err = PTR_ERR(dnew); |
1da177e4 LT |
1667 | if (IS_ERR(dnew)) |
1668 | goto out_nfserr; | |
1669 | ||
1da177e4 LT |
1670 | if (unlikely(path[plen] != 0)) { |
1671 | char *path_alloced = kmalloc(plen+1, GFP_KERNEL); | |
1672 | if (path_alloced == NULL) | |
6264d69d | 1673 | host_err = -ENOMEM; |
1da177e4 LT |
1674 | else { |
1675 | strncpy(path_alloced, path, plen); | |
1676 | path_alloced[plen] = 0; | |
db2e747b | 1677 | host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced); |
1da177e4 LT |
1678 | kfree(path_alloced); |
1679 | } | |
1680 | } else | |
db2e747b | 1681 | host_err = vfs_symlink(dentry->d_inode, dnew, path); |
6264d69d | 1682 | err = nfserrno(host_err); |
f501912a BM |
1683 | if (!err) |
1684 | err = nfserrno(commit_metadata(fhp)); | |
1da177e4 LT |
1685 | fh_unlock(fhp); |
1686 | ||
bad0dcff | 1687 | fh_drop_write(fhp); |
75c3f29d | 1688 | |
1da177e4 LT |
1689 | cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp); |
1690 | dput(dnew); | |
1691 | if (err==0) err = cerr; | |
1692 | out: | |
1693 | return err; | |
1694 | ||
1695 | out_nfserr: | |
6264d69d | 1696 | err = nfserrno(host_err); |
1da177e4 LT |
1697 | goto out; |
1698 | } | |
1699 | ||
1700 | /* | |
1701 | * Create a hardlink | |
1702 | * N.B. After this call _both_ ffhp and tfhp need an fh_put | |
1703 | */ | |
6264d69d | 1704 | __be32 |
1da177e4 LT |
1705 | nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp, |
1706 | char *name, int len, struct svc_fh *tfhp) | |
1707 | { | |
1708 | struct dentry *ddir, *dnew, *dold; | |
55b13354 | 1709 | struct inode *dirp; |
6264d69d AV |
1710 | __be32 err; |
1711 | int host_err; | |
1da177e4 | 1712 | |
8837abca | 1713 | err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE); |
1da177e4 LT |
1714 | if (err) |
1715 | goto out; | |
7d818a7b | 1716 | err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP); |
1da177e4 LT |
1717 | if (err) |
1718 | goto out; | |
7d818a7b BF |
1719 | err = nfserr_isdir; |
1720 | if (S_ISDIR(tfhp->fh_dentry->d_inode->i_mode)) | |
1721 | goto out; | |
1da177e4 LT |
1722 | err = nfserr_perm; |
1723 | if (!len) | |
1724 | goto out; | |
1725 | err = nfserr_exist; | |
1726 | if (isdotent(name, len)) | |
1727 | goto out; | |
1728 | ||
4a55c101 JK |
1729 | host_err = fh_want_write(tfhp); |
1730 | if (host_err) { | |
1731 | err = nfserrno(host_err); | |
1732 | goto out; | |
1733 | } | |
1734 | ||
12fd3520 | 1735 | fh_lock_nested(ffhp, I_MUTEX_PARENT); |
1da177e4 LT |
1736 | ddir = ffhp->fh_dentry; |
1737 | dirp = ddir->d_inode; | |
1738 | ||
1739 | dnew = lookup_one_len(name, ddir, len); | |
6264d69d | 1740 | host_err = PTR_ERR(dnew); |
1da177e4 LT |
1741 | if (IS_ERR(dnew)) |
1742 | goto out_nfserr; | |
1743 | ||
1744 | dold = tfhp->fh_dentry; | |
1da177e4 | 1745 | |
4795bb37 BF |
1746 | err = nfserr_noent; |
1747 | if (!dold->d_inode) | |
4a55c101 | 1748 | goto out_dput; |
146a8595 | 1749 | host_err = vfs_link(dold, dirp, dnew, NULL); |
6264d69d | 1750 | if (!host_err) { |
f501912a BM |
1751 | err = nfserrno(commit_metadata(ffhp)); |
1752 | if (!err) | |
1753 | err = nfserrno(commit_metadata(tfhp)); | |
1da177e4 | 1754 | } else { |
6264d69d | 1755 | if (host_err == -EXDEV && rqstp->rq_vers == 2) |
1da177e4 LT |
1756 | err = nfserr_acces; |
1757 | else | |
6264d69d | 1758 | err = nfserrno(host_err); |
1da177e4 | 1759 | } |
75c3f29d | 1760 | out_dput: |
1da177e4 | 1761 | dput(dnew); |
270d56e5 DR |
1762 | out_unlock: |
1763 | fh_unlock(ffhp); | |
4a55c101 | 1764 | fh_drop_write(tfhp); |
1da177e4 LT |
1765 | out: |
1766 | return err; | |
1767 | ||
1768 | out_nfserr: | |
6264d69d | 1769 | err = nfserrno(host_err); |
270d56e5 | 1770 | goto out_unlock; |
1da177e4 LT |
1771 | } |
1772 | ||
1773 | /* | |
1774 | * Rename a file | |
1775 | * N.B. After this call _both_ ffhp and tfhp need an fh_put | |
1776 | */ | |
6264d69d | 1777 | __be32 |
1da177e4 LT |
1778 | nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen, |
1779 | struct svc_fh *tfhp, char *tname, int tlen) | |
1780 | { | |
1781 | struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap; | |
1782 | struct inode *fdir, *tdir; | |
6264d69d AV |
1783 | __be32 err; |
1784 | int host_err; | |
1da177e4 | 1785 | |
8837abca | 1786 | err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE); |
1da177e4 LT |
1787 | if (err) |
1788 | goto out; | |
8837abca | 1789 | err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE); |
1da177e4 LT |
1790 | if (err) |
1791 | goto out; | |
1792 | ||
1793 | fdentry = ffhp->fh_dentry; | |
1794 | fdir = fdentry->d_inode; | |
1795 | ||
1796 | tdentry = tfhp->fh_dentry; | |
1797 | tdir = tdentry->d_inode; | |
1798 | ||
1da177e4 LT |
1799 | err = nfserr_perm; |
1800 | if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen)) | |
1801 | goto out; | |
1802 | ||
4a55c101 JK |
1803 | host_err = fh_want_write(ffhp); |
1804 | if (host_err) { | |
1805 | err = nfserrno(host_err); | |
1806 | goto out; | |
1807 | } | |
1808 | ||
1da177e4 LT |
1809 | /* cannot use fh_lock as we need deadlock protective ordering |
1810 | * so do it by hand */ | |
1811 | trap = lock_rename(tdentry, fdentry); | |
1812 | ffhp->fh_locked = tfhp->fh_locked = 1; | |
1813 | fill_pre_wcc(ffhp); | |
1814 | fill_pre_wcc(tfhp); | |
1815 | ||
1816 | odentry = lookup_one_len(fname, fdentry, flen); | |
6264d69d | 1817 | host_err = PTR_ERR(odentry); |
1da177e4 LT |
1818 | if (IS_ERR(odentry)) |
1819 | goto out_nfserr; | |
1820 | ||
6264d69d | 1821 | host_err = -ENOENT; |
1da177e4 LT |
1822 | if (!odentry->d_inode) |
1823 | goto out_dput_old; | |
6264d69d | 1824 | host_err = -EINVAL; |
1da177e4 LT |
1825 | if (odentry == trap) |
1826 | goto out_dput_old; | |
1827 | ||
1828 | ndentry = lookup_one_len(tname, tdentry, tlen); | |
6264d69d | 1829 | host_err = PTR_ERR(ndentry); |
1da177e4 LT |
1830 | if (IS_ERR(ndentry)) |
1831 | goto out_dput_old; | |
6264d69d | 1832 | host_err = -ENOTEMPTY; |
1da177e4 LT |
1833 | if (ndentry == trap) |
1834 | goto out_dput_new; | |
1835 | ||
9079b1eb DH |
1836 | host_err = -EXDEV; |
1837 | if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt) | |
1838 | goto out_dput_new; | |
aa387d6c BF |
1839 | if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry) |
1840 | goto out_dput_new; | |
9079b1eb | 1841 | |
8e6d782c | 1842 | host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL); |
f501912a BM |
1843 | if (!host_err) { |
1844 | host_err = commit_metadata(tfhp); | |
6264d69d | 1845 | if (!host_err) |
f501912a | 1846 | host_err = commit_metadata(ffhp); |
1da177e4 | 1847 | } |
1da177e4 LT |
1848 | out_dput_new: |
1849 | dput(ndentry); | |
1850 | out_dput_old: | |
1851 | dput(odentry); | |
1852 | out_nfserr: | |
6264d69d | 1853 | err = nfserrno(host_err); |
1da177e4 LT |
1854 | |
1855 | /* we cannot reply on fh_unlock on the two filehandles, | |
1856 | * as that would do the wrong thing if the two directories | |
1857 | * were the same, so again we do it by hand | |
1858 | */ | |
1859 | fill_post_wcc(ffhp); | |
1860 | fill_post_wcc(tfhp); | |
1861 | unlock_rename(tdentry, fdentry); | |
1862 | ffhp->fh_locked = tfhp->fh_locked = 0; | |
4a55c101 | 1863 | fh_drop_write(ffhp); |
1da177e4 LT |
1864 | |
1865 | out: | |
1866 | return err; | |
1867 | } | |
1868 | ||
1869 | /* | |
1870 | * Unlink a file or directory | |
1871 | * N.B. After this call fhp needs an fh_put | |
1872 | */ | |
6264d69d | 1873 | __be32 |
1da177e4 LT |
1874 | nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, |
1875 | char *fname, int flen) | |
1876 | { | |
1877 | struct dentry *dentry, *rdentry; | |
1878 | struct inode *dirp; | |
6264d69d AV |
1879 | __be32 err; |
1880 | int host_err; | |
1da177e4 LT |
1881 | |
1882 | err = nfserr_acces; | |
1883 | if (!flen || isdotent(fname, flen)) | |
1884 | goto out; | |
8837abca | 1885 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE); |
1da177e4 LT |
1886 | if (err) |
1887 | goto out; | |
1888 | ||
4a55c101 JK |
1889 | host_err = fh_want_write(fhp); |
1890 | if (host_err) | |
1891 | goto out_nfserr; | |
1892 | ||
12fd3520 | 1893 | fh_lock_nested(fhp, I_MUTEX_PARENT); |
1da177e4 LT |
1894 | dentry = fhp->fh_dentry; |
1895 | dirp = dentry->d_inode; | |
1896 | ||
1897 | rdentry = lookup_one_len(fname, dentry, flen); | |
6264d69d | 1898 | host_err = PTR_ERR(rdentry); |
1da177e4 LT |
1899 | if (IS_ERR(rdentry)) |
1900 | goto out_nfserr; | |
1901 | ||
1902 | if (!rdentry->d_inode) { | |
1903 | dput(rdentry); | |
1904 | err = nfserr_noent; | |
1905 | goto out; | |
1906 | } | |
1907 | ||
1908 | if (!type) | |
1909 | type = rdentry->d_inode->i_mode & S_IFMT; | |
1910 | ||
9ce137ee | 1911 | if (type != S_IFDIR) |
b21996e3 | 1912 | host_err = vfs_unlink(dirp, rdentry, NULL); |
9ce137ee | 1913 | else |
6264d69d | 1914 | host_err = vfs_rmdir(dirp, rdentry); |
f501912a BM |
1915 | if (!host_err) |
1916 | host_err = commit_metadata(fhp); | |
541ce98c BF |
1917 | dput(rdentry); |
1918 | ||
1da177e4 | 1919 | out_nfserr: |
6264d69d | 1920 | err = nfserrno(host_err); |
f193fbab YT |
1921 | out: |
1922 | return err; | |
1da177e4 LT |
1923 | } |
1924 | ||
14f7dd63 DW |
1925 | /* |
1926 | * We do this buffering because we must not call back into the file | |
1927 | * system's ->lookup() method from the filldir callback. That may well | |
1928 | * deadlock a number of file systems. | |
1929 | * | |
1930 | * This is based heavily on the implementation of same in XFS. | |
1931 | */ | |
1932 | struct buffered_dirent { | |
1933 | u64 ino; | |
1934 | loff_t offset; | |
1935 | int namlen; | |
1936 | unsigned int d_type; | |
1937 | char name[]; | |
1938 | }; | |
1939 | ||
1940 | struct readdir_data { | |
5c0ba4e0 | 1941 | struct dir_context ctx; |
14f7dd63 DW |
1942 | char *dirent; |
1943 | size_t used; | |
53c9c5c0 | 1944 | int full; |
14f7dd63 DW |
1945 | }; |
1946 | ||
1947 | static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen, | |
1948 | loff_t offset, u64 ino, unsigned int d_type) | |
1949 | { | |
1950 | struct readdir_data *buf = __buf; | |
1951 | struct buffered_dirent *de = (void *)(buf->dirent + buf->used); | |
1952 | unsigned int reclen; | |
1953 | ||
1954 | reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64)); | |
53c9c5c0 AV |
1955 | if (buf->used + reclen > PAGE_SIZE) { |
1956 | buf->full = 1; | |
14f7dd63 | 1957 | return -EINVAL; |
53c9c5c0 | 1958 | } |
14f7dd63 DW |
1959 | |
1960 | de->namlen = namlen; | |
1961 | de->offset = offset; | |
1962 | de->ino = ino; | |
1963 | de->d_type = d_type; | |
1964 | memcpy(de->name, name, namlen); | |
1965 | buf->used += reclen; | |
1966 | ||
1967 | return 0; | |
1968 | } | |
1969 | ||
2f9092e1 DW |
1970 | static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func, |
1971 | struct readdir_cd *cdp, loff_t *offsetp) | |
2628b766 | 1972 | { |
14f7dd63 | 1973 | struct buffered_dirent *de; |
2628b766 | 1974 | int host_err; |
14f7dd63 DW |
1975 | int size; |
1976 | loff_t offset; | |
ac6614b7 AV |
1977 | struct readdir_data buf = { |
1978 | .ctx.actor = nfsd_buffered_filldir, | |
1979 | .dirent = (void *)__get_free_page(GFP_KERNEL) | |
1980 | }; | |
2628b766 | 1981 | |
14f7dd63 | 1982 | if (!buf.dirent) |
2f9092e1 | 1983 | return nfserrno(-ENOMEM); |
14f7dd63 DW |
1984 | |
1985 | offset = *offsetp; | |
2628b766 | 1986 | |
14f7dd63 | 1987 | while (1) { |
496ad9aa | 1988 | struct inode *dir_inode = file_inode(file); |
14f7dd63 DW |
1989 | unsigned int reclen; |
1990 | ||
b726e923 | 1991 | cdp->err = nfserr_eof; /* will be cleared on successful read */ |
14f7dd63 | 1992 | buf.used = 0; |
53c9c5c0 | 1993 | buf.full = 0; |
14f7dd63 | 1994 | |
5c0ba4e0 | 1995 | host_err = iterate_dir(file, &buf.ctx); |
53c9c5c0 AV |
1996 | if (buf.full) |
1997 | host_err = 0; | |
1998 | ||
1999 | if (host_err < 0) | |
14f7dd63 DW |
2000 | break; |
2001 | ||
2002 | size = buf.used; | |
2003 | ||
2004 | if (!size) | |
2005 | break; | |
2006 | ||
2f9092e1 DW |
2007 | /* |
2008 | * Various filldir functions may end up calling back into | |
2009 | * lookup_one_len() and the file system's ->lookup() method. | |
2010 | * These expect i_mutex to be held, as it would within readdir. | |
2011 | */ | |
2012 | host_err = mutex_lock_killable(&dir_inode->i_mutex); | |
2013 | if (host_err) | |
2014 | break; | |
2015 | ||
14f7dd63 DW |
2016 | de = (struct buffered_dirent *)buf.dirent; |
2017 | while (size > 0) { | |
2018 | offset = de->offset; | |
2019 | ||
2020 | if (func(cdp, de->name, de->namlen, de->offset, | |
2021 | de->ino, de->d_type)) | |
2f9092e1 | 2022 | break; |
14f7dd63 DW |
2023 | |
2024 | if (cdp->err != nfs_ok) | |
2f9092e1 | 2025 | break; |
14f7dd63 DW |
2026 | |
2027 | reclen = ALIGN(sizeof(*de) + de->namlen, | |
2028 | sizeof(u64)); | |
2029 | size -= reclen; | |
2030 | de = (struct buffered_dirent *)((char *)de + reclen); | |
2031 | } | |
2f9092e1 DW |
2032 | mutex_unlock(&dir_inode->i_mutex); |
2033 | if (size > 0) /* We bailed out early */ | |
2034 | break; | |
2035 | ||
c002a6c7 | 2036 | offset = vfs_llseek(file, 0, SEEK_CUR); |
14f7dd63 DW |
2037 | } |
2038 | ||
14f7dd63 | 2039 | free_page((unsigned long)(buf.dirent)); |
2628b766 DW |
2040 | |
2041 | if (host_err) | |
2042 | return nfserrno(host_err); | |
14f7dd63 DW |
2043 | |
2044 | *offsetp = offset; | |
2045 | return cdp->err; | |
2628b766 DW |
2046 | } |
2047 | ||
1da177e4 LT |
2048 | /* |
2049 | * Read entries from a directory. | |
2050 | * The NFSv3/4 verifier we ignore for now. | |
2051 | */ | |
6264d69d | 2052 | __be32 |
1da177e4 | 2053 | nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp, |
a0ad13ef | 2054 | struct readdir_cd *cdp, filldir_t func) |
1da177e4 | 2055 | { |
6264d69d | 2056 | __be32 err; |
1da177e4 LT |
2057 | struct file *file; |
2058 | loff_t offset = *offsetp; | |
06effdbb BS |
2059 | int may_flags = NFSD_MAY_READ; |
2060 | ||
2061 | /* NFSv2 only supports 32 bit cookies */ | |
2062 | if (rqstp->rq_vers > 2) | |
2063 | may_flags |= NFSD_MAY_64BIT_COOKIE; | |
1da177e4 | 2064 | |
06effdbb | 2065 | err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file); |
1da177e4 LT |
2066 | if (err) |
2067 | goto out; | |
2068 | ||
b108fe6b | 2069 | offset = vfs_llseek(file, offset, SEEK_SET); |
1da177e4 LT |
2070 | if (offset < 0) { |
2071 | err = nfserrno((int)offset); | |
2072 | goto out_close; | |
2073 | } | |
2074 | ||
14f7dd63 | 2075 | err = nfsd_buffered_readdir(file, func, cdp, offsetp); |
1da177e4 LT |
2076 | |
2077 | if (err == nfserr_eof || err == nfserr_toosmall) | |
2078 | err = nfs_ok; /* can still be found in ->err */ | |
2079 | out_close: | |
2080 | nfsd_close(file); | |
2081 | out: | |
2082 | return err; | |
2083 | } | |
2084 | ||
2085 | /* | |
2086 | * Get file system stats | |
2087 | * N.B. After this call fhp needs an fh_put | |
2088 | */ | |
6264d69d | 2089 | __be32 |
04716e66 | 2090 | nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access) |
1da177e4 | 2091 | { |
ebabe9a9 CH |
2092 | __be32 err; |
2093 | ||
2094 | err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access); | |
f6360efb TI |
2095 | if (!err) { |
2096 | struct path path = { | |
2097 | .mnt = fhp->fh_export->ex_path.mnt, | |
2098 | .dentry = fhp->fh_dentry, | |
2099 | }; | |
2100 | if (vfs_statfs(&path, stat)) | |
2101 | err = nfserr_io; | |
2102 | } | |
1da177e4 LT |
2103 | return err; |
2104 | } | |
2105 | ||
c7d51402 | 2106 | static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp) |
e22841c6 | 2107 | { |
c7d51402 | 2108 | return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY; |
e22841c6 BF |
2109 | } |
2110 | ||
1da177e4 LT |
2111 | /* |
2112 | * Check for a user's access permissions to this inode. | |
2113 | */ | |
6264d69d | 2114 | __be32 |
0ec757df BF |
2115 | nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp, |
2116 | struct dentry *dentry, int acc) | |
1da177e4 LT |
2117 | { |
2118 | struct inode *inode = dentry->d_inode; | |
2119 | int err; | |
2120 | ||
aea93397 | 2121 | if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP) |
1da177e4 LT |
2122 | return 0; |
2123 | #if 0 | |
2124 | dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n", | |
2125 | acc, | |
8837abca MS |
2126 | (acc & NFSD_MAY_READ)? " read" : "", |
2127 | (acc & NFSD_MAY_WRITE)? " write" : "", | |
2128 | (acc & NFSD_MAY_EXEC)? " exec" : "", | |
2129 | (acc & NFSD_MAY_SATTR)? " sattr" : "", | |
2130 | (acc & NFSD_MAY_TRUNC)? " trunc" : "", | |
2131 | (acc & NFSD_MAY_LOCK)? " lock" : "", | |
2132 | (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "", | |
1da177e4 LT |
2133 | inode->i_mode, |
2134 | IS_IMMUTABLE(inode)? " immut" : "", | |
2135 | IS_APPEND(inode)? " append" : "", | |
2c463e95 | 2136 | __mnt_is_readonly(exp->ex_path.mnt)? " ro" : ""); |
1da177e4 | 2137 | dprintk(" owner %d/%d user %d/%d\n", |
5cc0a840 | 2138 | inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid()); |
1da177e4 LT |
2139 | #endif |
2140 | ||
2141 | /* Normally we reject any write/sattr etc access on a read-only file | |
2142 | * system. But if it is IRIX doing check on write-access for a | |
2143 | * device special file, we ignore rofs. | |
2144 | */ | |
8837abca MS |
2145 | if (!(acc & NFSD_MAY_LOCAL_ACCESS)) |
2146 | if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) { | |
2c463e95 DH |
2147 | if (exp_rdonly(rqstp, exp) || |
2148 | __mnt_is_readonly(exp->ex_path.mnt)) | |
1da177e4 | 2149 | return nfserr_rofs; |
8837abca | 2150 | if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode)) |
1da177e4 LT |
2151 | return nfserr_perm; |
2152 | } | |
8837abca | 2153 | if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode)) |
1da177e4 LT |
2154 | return nfserr_perm; |
2155 | ||
8837abca | 2156 | if (acc & NFSD_MAY_LOCK) { |
1da177e4 LT |
2157 | /* If we cannot rely on authentication in NLM requests, |
2158 | * just allow locks, otherwise require read permission, or | |
2159 | * ownership | |
2160 | */ | |
2161 | if (exp->ex_flags & NFSEXP_NOAUTHNLM) | |
2162 | return 0; | |
2163 | else | |
8837abca | 2164 | acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE; |
1da177e4 LT |
2165 | } |
2166 | /* | |
2167 | * The file owner always gets access permission for accesses that | |
2168 | * would normally be checked at open time. This is to make | |
2169 | * file access work even when the client has done a fchmod(fd, 0). | |
2170 | * | |
2171 | * However, `cp foo bar' should fail nevertheless when bar is | |
2172 | * readonly. A sensible way to do this might be to reject all | |
2173 | * attempts to truncate a read-only file, because a creat() call | |
2174 | * always implies file truncation. | |
2175 | * ... but this isn't really fair. A process may reasonably call | |
2176 | * ftruncate on an open file descriptor on a file with perm 000. | |
2177 | * We must trust the client to do permission checking - using "ACCESS" | |
2178 | * with NFSv3. | |
2179 | */ | |
8837abca | 2180 | if ((acc & NFSD_MAY_OWNER_OVERRIDE) && |
6fab8779 | 2181 | uid_eq(inode->i_uid, current_fsuid())) |
1da177e4 LT |
2182 | return 0; |
2183 | ||
8837abca | 2184 | /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */ |
f419a2e3 | 2185 | err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC)); |
1da177e4 LT |
2186 | |
2187 | /* Allow read access to binaries even when mode 111 */ | |
2188 | if (err == -EACCES && S_ISREG(inode->i_mode) && | |
a043226b BF |
2189 | (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) || |
2190 | acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC))) | |
f419a2e3 | 2191 | err = inode_permission(inode, MAY_EXEC); |
1da177e4 LT |
2192 | |
2193 | return err? nfserrno(err) : 0; | |
2194 | } | |
2195 | ||
2196 | void | |
2197 | nfsd_racache_shutdown(void) | |
2198 | { | |
54a66e54 JL |
2199 | struct raparms *raparm, *last_raparm; |
2200 | unsigned int i; | |
2201 | ||
1da177e4 | 2202 | dprintk("nfsd: freeing readahead buffers.\n"); |
54a66e54 JL |
2203 | |
2204 | for (i = 0; i < RAPARM_HASH_SIZE; i++) { | |
2205 | raparm = raparm_hash[i].pb_head; | |
2206 | while(raparm) { | |
2207 | last_raparm = raparm; | |
2208 | raparm = raparm->p_next; | |
2209 | kfree(last_raparm); | |
2210 | } | |
2211 | raparm_hash[i].pb_head = NULL; | |
2212 | } | |
1da177e4 LT |
2213 | } |
2214 | /* | |
2215 | * Initialize readahead param cache | |
2216 | */ | |
2217 | int | |
2218 | nfsd_racache_init(int cache_size) | |
2219 | { | |
2220 | int i; | |
fce1456a GB |
2221 | int j = 0; |
2222 | int nperbucket; | |
54a66e54 | 2223 | struct raparms **raparm = NULL; |
1da177e4 | 2224 | |
fce1456a | 2225 | |
54a66e54 | 2226 | if (raparm_hash[0].pb_head) |
1da177e4 | 2227 | return 0; |
54a66e54 JL |
2228 | nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE); |
2229 | if (nperbucket < 2) | |
2230 | nperbucket = 2; | |
2231 | cache_size = nperbucket * RAPARM_HASH_SIZE; | |
4b3bb06b YB |
2232 | |
2233 | dprintk("nfsd: allocating %d readahead buffers.\n", cache_size); | |
54a66e54 JL |
2234 | |
2235 | for (i = 0; i < RAPARM_HASH_SIZE; i++) { | |
4b3bb06b | 2236 | spin_lock_init(&raparm_hash[i].pb_lock); |
54a66e54 JL |
2237 | |
2238 | raparm = &raparm_hash[i].pb_head; | |
2239 | for (j = 0; j < nperbucket; j++) { | |
2240 | *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL); | |
2241 | if (!*raparm) | |
2242 | goto out_nomem; | |
2243 | raparm = &(*raparm)->p_next; | |
2244 | } | |
2245 | *raparm = NULL; | |
4b3bb06b YB |
2246 | } |
2247 | ||
1da177e4 LT |
2248 | nfsdstats.ra_size = cache_size; |
2249 | return 0; | |
54a66e54 JL |
2250 | |
2251 | out_nomem: | |
2252 | dprintk("nfsd: kmalloc failed, freeing readahead buffers\n"); | |
2253 | nfsd_racache_shutdown(); | |
2254 | return -ENOMEM; | |
1da177e4 | 2255 | } |
a257cdd0 AG |
2256 | |
2257 | #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) | |
2258 | struct posix_acl * | |
2259 | nfsd_get_posix_acl(struct svc_fh *fhp, int type) | |
2260 | { | |
2261 | struct inode *inode = fhp->fh_dentry->d_inode; | |
2262 | char *name; | |
2263 | void *value = NULL; | |
2264 | ssize_t size; | |
2265 | struct posix_acl *acl; | |
2266 | ||
5be196e5 CH |
2267 | if (!IS_POSIXACL(inode)) |
2268 | return ERR_PTR(-EOPNOTSUPP); | |
2269 | ||
2270 | switch (type) { | |
2271 | case ACL_TYPE_ACCESS: | |
2272 | name = POSIX_ACL_XATTR_ACCESS; | |
2273 | break; | |
2274 | case ACL_TYPE_DEFAULT: | |
2275 | name = POSIX_ACL_XATTR_DEFAULT; | |
2276 | break; | |
2277 | default: | |
a257cdd0 | 2278 | return ERR_PTR(-EOPNOTSUPP); |
a257cdd0 AG |
2279 | } |
2280 | ||
5be196e5 CH |
2281 | size = nfsd_getxattr(fhp->fh_dentry, name, &value); |
2282 | if (size < 0) | |
2283 | return ERR_PTR(size); | |
a257cdd0 | 2284 | |
5f3a4a28 | 2285 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
a257cdd0 AG |
2286 | kfree(value); |
2287 | return acl; | |
2288 | } | |
2289 | ||
2290 | int | |
2291 | nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl) | |
2292 | { | |
2293 | struct inode *inode = fhp->fh_dentry->d_inode; | |
2294 | char *name; | |
2295 | void *value = NULL; | |
2296 | size_t size; | |
2297 | int error; | |
2298 | ||
acfa4380 | 2299 | if (!IS_POSIXACL(inode) || |
a257cdd0 AG |
2300 | !inode->i_op->setxattr || !inode->i_op->removexattr) |
2301 | return -EOPNOTSUPP; | |
2302 | switch(type) { | |
2303 | case ACL_TYPE_ACCESS: | |
334a13ec | 2304 | name = POSIX_ACL_XATTR_ACCESS; |
a257cdd0 AG |
2305 | break; |
2306 | case ACL_TYPE_DEFAULT: | |
334a13ec | 2307 | name = POSIX_ACL_XATTR_DEFAULT; |
a257cdd0 AG |
2308 | break; |
2309 | default: | |
2310 | return -EOPNOTSUPP; | |
2311 | } | |
2312 | ||
2313 | if (acl && acl->a_count) { | |
334a13ec | 2314 | size = posix_acl_xattr_size(acl->a_count); |
a257cdd0 AG |
2315 | value = kmalloc(size, GFP_KERNEL); |
2316 | if (!value) | |
2317 | return -ENOMEM; | |
5f3a4a28 | 2318 | error = posix_acl_to_xattr(&init_user_ns, acl, value, size); |
9ccfc29c | 2319 | if (error < 0) |
a257cdd0 | 2320 | goto getout; |
9ccfc29c | 2321 | size = error; |
a257cdd0 AG |
2322 | } else |
2323 | size = 0; | |
2324 | ||
bad0dcff | 2325 | error = fh_want_write(fhp); |
18f335af DH |
2326 | if (error) |
2327 | goto getout; | |
a257cdd0 | 2328 | if (size) |
5be196e5 | 2329 | error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0); |
a257cdd0 AG |
2330 | else { |
2331 | if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT) | |
2332 | error = 0; | |
2333 | else { | |
5be196e5 | 2334 | error = vfs_removexattr(fhp->fh_dentry, name); |
a257cdd0 AG |
2335 | if (error == -ENODATA) |
2336 | error = 0; | |
2337 | } | |
2338 | } | |
bad0dcff | 2339 | fh_drop_write(fhp); |
a257cdd0 AG |
2340 | |
2341 | getout: | |
2342 | kfree(value); | |
2343 | return error; | |
2344 | } | |
2345 | #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */ |