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