1 #define MSNFS /* HACK HACK */
5 * File operations used by nfsd. Some of these have been ripped from
6 * other parts of the kernel because they weren't exported, others
7 * are partial duplicates with added or changed functionality.
9 * Note that several functions dget() the dentry upon which they want
10 * to act, most notably those that create directory entries. Response
11 * dentry's are dput()'d if necessary in the release callback.
12 * So if you notice code paths that apparently fail to dput() the
13 * dentry, don't worry--they have been taken care of.
15 * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
16 * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
20 #include <linux/file.h>
21 #include <linux/splice.h>
22 #include <linux/fcntl.h>
23 #include <linux/namei.h>
24 #include <linux/delay.h>
25 #include <linux/quotaops.h>
26 #include <linux/fsnotify.h>
27 #include <linux/posix_acl_xattr.h>
28 #include <linux/xattr.h>
29 #include <linux/jhash.h>
30 #include <linux/ima.h>
31 #include <asm/uaccess.h>
35 #endif /* CONFIG_NFSD_V3 */
38 #include <linux/nfs4_acl.h>
39 #include <linux/nfsd_idmap.h>
40 #endif /* CONFIG_NFSD_V4 */
45 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
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.
56 struct raparms *p_next;
61 struct file_ra_state p_ra;
62 unsigned int p_hindex;
65 struct raparm_hbucket {
66 struct raparms *pb_head;
68 } ____cacheline_aligned_in_smp;
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];
76 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
78 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
79 * or nfs_ok having possibly changed *dpp and *expp
82 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
83 struct svc_export **expp)
85 struct svc_export *exp = *expp, *exp2 = NULL;
86 struct dentry *dentry = *dpp;
87 struct path path = {.mnt = mntget(exp->ex_path.mnt),
88 .dentry = dget(dentry)};
91 while (d_mountpoint(path.dentry) && follow_down(&path))
94 exp2 = rqst_exp_get_by_name(rqstp, &path);
98 * We normally allow NFS clients to continue
99 * "underneath" a mountpoint that is not exported.
100 * The exception is V4ROOT, where no traversal is ever
101 * allowed without an explicit export of the new
104 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
109 if (nfsd_v4client(rqstp) ||
110 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
111 /* successfully crossed mount point */
113 * This is subtle: path.dentry is *not* on path.mnt
114 * at this point. The only reason we are safe is that
115 * original mnt is pinned down by exp, so we should
116 * put path *before* putting exp
119 path.dentry = dentry;
129 static void follow_to_parent(struct path *path)
133 while (path->dentry == path->mnt->mnt_root && follow_up(path))
135 dp = dget_parent(path->dentry);
140 static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
142 struct svc_export *exp2;
143 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
144 .dentry = dget(dparent)};
146 follow_to_parent(&path);
148 exp2 = rqst_exp_parent(rqstp, &path);
149 if (PTR_ERR(exp2) == -ENOENT) {
150 *dentryp = dget(dparent);
151 } else if (IS_ERR(exp2)) {
153 return PTR_ERR(exp2);
155 *dentryp = dget(path.dentry);
164 * For nfsd purposes, we treat V4ROOT exports as though there was an
165 * export at *every* directory.
167 int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
169 if (d_mountpoint(dentry))
171 if (!(exp->ex_flags & NFSEXP_V4ROOT))
173 return dentry->d_inode != NULL;
177 nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
178 const char *name, unsigned int len,
179 struct svc_export **exp_ret, struct dentry **dentry_ret)
181 struct svc_export *exp;
182 struct dentry *dparent;
183 struct dentry *dentry;
187 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
189 /* Obtain dentry and export. */
190 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
194 dparent = fhp->fh_dentry;
195 exp = fhp->fh_export;
198 /* Lookup the name, but don't follow links */
199 if (isdotent(name, len)) {
201 dentry = dget(dparent);
202 else if (dparent != exp->ex_path.dentry)
203 dentry = dget_parent(dparent);
204 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
205 dentry = dget(dparent); /* .. == . just like at / */
207 /* checking mountpoint crossing is very different when stepping up */
208 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
214 dentry = lookup_one_len(name, dparent, len);
215 host_err = PTR_ERR(dentry);
219 * check if we have crossed a mount point ...
221 if (nfsd_mountpoint(dentry, exp)) {
222 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
228 *dentry_ret = dentry;
234 return nfserrno(host_err);
238 * Look up one component of a pathname.
239 * N.B. After this call _both_ fhp and resfh need an fh_put
241 * If the lookup would cross a mountpoint, and the mounted filesystem
242 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
243 * accepted as it stands and the mounted directory is
244 * returned. Otherwise the covered directory is returned.
245 * NOTE: this mountpoint crossing is not supported properly by all
246 * clients and is explicitly disallowed for NFSv3
247 * NeilBrown <neilb@cse.unsw.edu.au>
250 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
251 unsigned int len, struct svc_fh *resfh)
253 struct svc_export *exp;
254 struct dentry *dentry;
257 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
260 err = check_nfsd_access(exp, rqstp);
264 * Note: we compose the file handle now, but as the
265 * dentry may be negative, it may need to be updated.
267 err = fh_compose(resfh, exp, dentry, fhp);
268 if (!err && !dentry->d_inode)
278 * Set various file attributes.
279 * N.B. After this call fhp needs an fh_put
282 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
283 int check_guard, time_t guardtime)
285 struct dentry *dentry;
287 int accmode = NFSD_MAY_SATTR;
293 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
294 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
295 if (iap->ia_valid & ATTR_SIZE)
299 err = fh_verify(rqstp, fhp, ftype, accmode);
303 dentry = fhp->fh_dentry;
304 inode = dentry->d_inode;
306 /* Ignore any mode updates on symlinks */
307 if (S_ISLNK(inode->i_mode))
308 iap->ia_valid &= ~ATTR_MODE;
314 * NFSv2 does not differentiate between "set-[ac]time-to-now"
315 * which only requires access, and "set-[ac]time-to-X" which
316 * requires ownership.
317 * So if it looks like it might be "set both to the same time which
318 * is close to now", and if inode_change_ok fails, then we
319 * convert to "set to now" instead of "set to explicit time"
321 * We only call inode_change_ok as the last test as technically
322 * it is not an interface that we should be using. It is only
323 * valid if the filesystem does not define it's own i_op->setattr.
325 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
326 #define MAX_TOUCH_TIME_ERROR (30*60)
327 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
328 iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
332 * Now just make sure time is in the right ballpark.
333 * Solaris, at least, doesn't seem to care what the time
334 * request is. We require it be within 30 minutes of now.
336 time_t delta = iap->ia_atime.tv_sec - get_seconds();
339 if (delta < MAX_TOUCH_TIME_ERROR &&
340 inode_change_ok(inode, iap) != 0) {
342 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
343 * This will cause notify_change to set these times
346 iap->ia_valid &= ~BOTH_TIME_SET;
351 * The size case is special.
352 * It changes the file as well as the attributes.
354 if (iap->ia_valid & ATTR_SIZE) {
355 if (iap->ia_size < inode->i_size) {
356 err = nfsd_permission(rqstp, fhp->fh_export, dentry,
357 NFSD_MAY_TRUNC|NFSD_MAY_OWNER_OVERRIDE);
363 * If we are changing the size of the file, then
364 * we need to break all leases.
366 host_err = break_lease(inode, FMODE_WRITE | O_NONBLOCK);
367 if (host_err == -EWOULDBLOCK)
368 host_err = -ETIMEDOUT;
369 if (host_err) /* ENOMEM or EWOULDBLOCK */
372 host_err = get_write_access(inode);
377 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
379 put_write_access(inode);
385 /* sanitize the mode change */
386 if (iap->ia_valid & ATTR_MODE) {
387 iap->ia_mode &= S_IALLUGO;
388 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
391 /* Revoke setuid/setgid on chown */
392 if (!S_ISDIR(inode->i_mode) &&
393 (((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid) ||
394 ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid))) {
395 iap->ia_valid |= ATTR_KILL_PRIV;
396 if (iap->ia_valid & ATTR_MODE) {
397 /* we're setting mode too, just clear the s*id bits */
398 iap->ia_mode &= ~S_ISUID;
399 if (iap->ia_mode & S_IXGRP)
400 iap->ia_mode &= ~S_ISGID;
402 /* set ATTR_KILL_* bits and let VFS handle it */
403 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
407 /* Change the attributes. */
409 iap->ia_valid |= ATTR_CTIME;
411 err = nfserr_notsync;
412 if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
414 host_err = notify_change(dentry, iap);
415 err = nfserrno(host_err);
419 put_write_access(inode);
421 if (EX_ISSYNC(fhp->fh_export))
422 write_inode_now(inode, 1);
427 err = nfserrno(host_err);
431 #if defined(CONFIG_NFSD_V2_ACL) || \
432 defined(CONFIG_NFSD_V3_ACL) || \
433 defined(CONFIG_NFSD_V4)
434 static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
439 buflen = vfs_getxattr(dentry, key, NULL, 0);
443 *buf = kmalloc(buflen, GFP_KERNEL);
447 ret = vfs_getxattr(dentry, key, *buf, buflen);
454 #if defined(CONFIG_NFSD_V4)
456 set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
463 buflen = posix_acl_xattr_size(pacl->a_count);
464 buf = kmalloc(buflen, GFP_KERNEL);
469 len = posix_acl_to_xattr(pacl, buf, buflen);
475 error = vfs_setxattr(dentry, key, buf, len, 0);
482 nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
483 struct nfs4_acl *acl)
487 struct dentry *dentry;
489 struct posix_acl *pacl = NULL, *dpacl = NULL;
490 unsigned int flags = 0;
493 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
497 dentry = fhp->fh_dentry;
498 inode = dentry->d_inode;
499 if (S_ISDIR(inode->i_mode))
500 flags = NFS4_ACL_DIR;
502 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
503 if (host_error == -EINVAL) {
504 return nfserr_attrnotsupp;
505 } else if (host_error < 0)
508 host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
512 if (S_ISDIR(inode->i_mode))
513 host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
516 posix_acl_release(pacl);
517 posix_acl_release(dpacl);
519 if (host_error == -EOPNOTSUPP)
520 return nfserr_attrnotsupp;
522 return nfserrno(host_error);
525 static struct posix_acl *
526 _get_posix_acl(struct dentry *dentry, char *key)
529 struct posix_acl *pacl = NULL;
532 buflen = nfsd_getxattr(dentry, key, &buf);
536 return ERR_PTR(buflen);
538 pacl = posix_acl_from_xattr(buf, buflen);
544 nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
546 struct inode *inode = dentry->d_inode;
548 struct posix_acl *pacl = NULL, *dpacl = NULL;
549 unsigned int flags = 0;
551 pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
552 if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
553 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
555 error = PTR_ERR(pacl);
560 if (S_ISDIR(inode->i_mode)) {
561 dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
562 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
564 else if (IS_ERR(dpacl)) {
565 error = PTR_ERR(dpacl);
569 flags = NFS4_ACL_DIR;
572 *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
574 error = PTR_ERR(*acl);
578 posix_acl_release(pacl);
579 posix_acl_release(dpacl);
583 #endif /* defined(CONFIG_NFS_V4) */
585 #ifdef CONFIG_NFSD_V3
587 * Check server access rights to a file system object
593 static struct accessmap nfs3_regaccess[] = {
594 { NFS3_ACCESS_READ, NFSD_MAY_READ },
595 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
596 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
597 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
602 static struct accessmap nfs3_diraccess[] = {
603 { NFS3_ACCESS_READ, NFSD_MAY_READ },
604 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
605 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
606 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
607 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
612 static struct accessmap nfs3_anyaccess[] = {
613 /* Some clients - Solaris 2.6 at least, make an access call
614 * to the server to check for access for things like /dev/null
615 * (which really, the server doesn't care about). So
616 * We provide simple access checking for them, looking
617 * mainly at mode bits, and we make sure to ignore read-only
620 { NFS3_ACCESS_READ, NFSD_MAY_READ },
621 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
622 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
623 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
629 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
631 struct accessmap *map;
632 struct svc_export *export;
633 struct dentry *dentry;
634 u32 query, result = 0, sresult = 0;
637 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
641 export = fhp->fh_export;
642 dentry = fhp->fh_dentry;
644 if (S_ISREG(dentry->d_inode->i_mode))
645 map = nfs3_regaccess;
646 else if (S_ISDIR(dentry->d_inode->i_mode))
647 map = nfs3_diraccess;
649 map = nfs3_anyaccess;
653 for (; map->access; map++) {
654 if (map->access & query) {
657 sresult |= map->access;
659 err2 = nfsd_permission(rqstp, export, dentry, map->how);
662 result |= map->access;
665 /* the following error codes just mean the access was not allowed,
666 * rather than an error occurred */
670 /* simply don't "or" in the access bit. */
680 *supported = sresult;
685 #endif /* CONFIG_NFSD_V3 */
690 * Open an existing file or directory.
691 * The access argument indicates the type of open (read/write/lock)
692 * N.B. After this call fhp needs an fh_put
695 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
696 int access, struct file **filp)
698 struct dentry *dentry;
700 int flags = O_RDONLY|O_LARGEFILE;
704 validate_process_creds();
707 * If we get here, then the client has already done an "open",
708 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
709 * in case a chmod has now revoked permission.
711 err = fh_verify(rqstp, fhp, type, access | NFSD_MAY_OWNER_OVERRIDE);
715 dentry = fhp->fh_dentry;
716 inode = dentry->d_inode;
718 /* Disallow write access to files with the append-only bit set
719 * or any access when mandatory locking enabled
722 if (IS_APPEND(inode) && (access & NFSD_MAY_WRITE))
725 * We must ignore files (but only files) which might have mandatory
726 * locks on them because there is no way to know if the accesser has
729 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
736 * Check to see if there are any leases on this file.
737 * This may block while leases are broken.
739 host_err = break_lease(inode, O_NONBLOCK | ((access & NFSD_MAY_WRITE) ? FMODE_WRITE : 0));
740 if (host_err == -EWOULDBLOCK)
741 host_err = -ETIMEDOUT;
742 if (host_err) /* NOMEM or WOULDBLOCK */
745 if (access & NFSD_MAY_WRITE) {
746 if (access & NFSD_MAY_READ)
747 flags = O_RDWR|O_LARGEFILE;
749 flags = O_WRONLY|O_LARGEFILE;
753 *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
754 flags, current_cred());
756 host_err = PTR_ERR(*filp);
758 ima_counts_get(*filp);
760 err = nfserrno(host_err);
762 validate_process_creds();
770 nfsd_close(struct file *filp)
777 * As this calls fsync (not fdatasync) there is no need for a write_inode
780 static inline int nfsd_dosync(struct file *filp, struct dentry *dp,
781 const struct file_operations *fop)
783 struct inode *inode = dp->d_inode;
784 int (*fsync) (struct file *, struct dentry *, int);
787 err = filemap_fdatawrite(inode->i_mapping);
788 if (err == 0 && fop && (fsync = fop->fsync))
789 err = fsync(filp, dp, 0);
791 err = filemap_fdatawait(inode->i_mapping);
797 nfsd_sync(struct file *filp)
800 struct inode *inode = filp->f_path.dentry->d_inode;
801 dprintk("nfsd: sync file %s\n", filp->f_path.dentry->d_name.name);
802 mutex_lock(&inode->i_mutex);
803 err=nfsd_dosync(filp, filp->f_path.dentry, filp->f_op);
804 mutex_unlock(&inode->i_mutex);
810 nfsd_sync_dir(struct dentry *dp)
812 return nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
816 * Obtain the readahead parameters for the file
817 * specified by (dev, ino).
820 static inline struct raparms *
821 nfsd_get_raparms(dev_t dev, ino_t ino)
823 struct raparms *ra, **rap, **frap = NULL;
826 struct raparm_hbucket *rab;
828 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
829 rab = &raparm_hash[hash];
831 spin_lock(&rab->pb_lock);
832 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
833 if (ra->p_ino == ino && ra->p_dev == dev)
836 if (ra->p_count == 0)
839 depth = nfsdstats.ra_size*11/10;
841 spin_unlock(&rab->pb_lock);
851 if (rap != &rab->pb_head) {
853 ra->p_next = rab->pb_head;
857 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
858 spin_unlock(&rab->pb_lock);
863 * Grab and keep cached pages associated with a file in the svc_rqst
864 * so that they can be passed to the network sendmsg/sendpage routines
865 * directly. They will be released after the sending has completed.
868 nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
869 struct splice_desc *sd)
871 struct svc_rqst *rqstp = sd->u.data;
872 struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
873 struct page *page = buf->page;
877 ret = buf->ops->confirm(pipe, buf);
883 if (rqstp->rq_res.page_len == 0) {
888 rqstp->rq_res.page_base = buf->offset;
889 rqstp->rq_res.page_len = size;
890 } else if (page != pp[-1]) {
896 rqstp->rq_res.page_len += size;
898 rqstp->rq_res.page_len += size;
903 static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
904 struct splice_desc *sd)
906 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
909 static inline int svc_msnfs(struct svc_fh *ffhp)
912 return (ffhp->fh_export->ex_flags & NFSEXP_MSNFS);
919 nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
920 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
929 inode = file->f_path.dentry->d_inode;
931 if (svc_msnfs(fhp) && !lock_may_read(inode, offset, *count))
934 /* Get readahead parameters */
935 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
938 file->f_ra = ra->p_ra;
940 if (file->f_op->splice_read && rqstp->rq_splice_ok) {
941 struct splice_desc sd = {
948 rqstp->rq_resused = 1;
949 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
953 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
957 /* Write back readahead params */
959 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
960 spin_lock(&rab->pb_lock);
961 ra->p_ra = file->f_ra;
964 spin_unlock(&rab->pb_lock);
968 nfsdstats.io_read += host_err;
971 fsnotify_access(file->f_path.dentry);
973 err = nfserrno(host_err);
978 static void kill_suid(struct dentry *dentry)
981 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
983 mutex_lock(&dentry->d_inode->i_mutex);
984 notify_change(dentry, &ia);
985 mutex_unlock(&dentry->d_inode->i_mutex);
989 * Gathered writes: If another process is currently writing to the file,
990 * there's a high chance this is another nfsd (triggered by a bulk write
991 * from a client's biod). Rather than syncing the file with each write
992 * request, we sleep for 10 msec.
994 * I don't know if this roughly approximates C. Juszak's idea of
995 * gathered writes, but it's a nice and simple solution (IMHO), and it
998 * Note: we do this only in the NFSv2 case, since v3 and higher have a
999 * better tool (separate unstable writes and commits) for solving this
1002 static int wait_for_concurrent_writes(struct file *file)
1004 struct inode *inode = file->f_path.dentry->d_inode;
1005 static ino_t last_ino;
1006 static dev_t last_dev;
1009 if (atomic_read(&inode->i_writecount) > 1
1010 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
1011 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
1013 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1016 if (inode->i_state & I_DIRTY) {
1017 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
1018 err = nfsd_sync(file);
1020 last_ino = inode->i_ino;
1021 last_dev = inode->i_sb->s_dev;
1026 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1027 loff_t offset, struct kvec *vec, int vlen,
1028 unsigned long *cnt, int *stablep)
1030 struct svc_export *exp;
1031 struct dentry *dentry;
1032 struct inode *inode;
1036 int stable = *stablep;
1042 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1043 (!lock_may_write(file->f_path.dentry->d_inode, offset, *cnt)))
1047 dentry = file->f_path.dentry;
1048 inode = dentry->d_inode;
1049 exp = fhp->fh_export;
1052 * Request sync writes if
1053 * - the sync export option has been set, or
1054 * - the client requested O_SYNC behavior (NFSv3 feature).
1055 * - The file system doesn't support fsync().
1056 * When NFSv2 gathered writes have been configured for this volume,
1057 * flushing the data to disk is handled separately below.
1059 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1061 if (!file->f_op->fsync) {/* COMMIT3 cannot work */
1063 *stablep = 2; /* FILE_SYNC */
1066 if (!EX_ISSYNC(exp))
1068 if (stable && !use_wgather) {
1069 spin_lock(&file->f_lock);
1070 file->f_flags |= O_SYNC;
1071 spin_unlock(&file->f_lock);
1074 /* Write the data. */
1075 oldfs = get_fs(); set_fs(KERNEL_DS);
1076 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
1081 nfsdstats.io_write += host_err;
1082 fsnotify_modify(file->f_path.dentry);
1084 /* clear setuid/setgid flag after write */
1085 if (inode->i_mode & (S_ISUID | S_ISGID))
1088 if (stable && use_wgather)
1089 host_err = wait_for_concurrent_writes(file);
1092 dprintk("nfsd: write complete host_err=%d\n", host_err);
1096 err = nfserrno(host_err);
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
1107 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1108 loff_t offset, struct kvec *vec, int vlen,
1109 unsigned long *count)
1114 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1115 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
1118 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1120 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1123 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1131 * Write data to a file.
1132 * The stable flag requests synchronous writes.
1133 * N.B. After this call fhp needs an fh_put
1136 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1137 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
1143 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1144 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
1147 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1150 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1155 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1163 #ifdef CONFIG_NFSD_V3
1165 * Commit all pending writes to stable storage.
1166 * Strictly speaking, we could sync just the indicated file region here,
1167 * but there's currently no way we can ask the VFS to do so.
1169 * Unfortunately we cannot lock the file to make sure we return full WCC
1170 * data to the client, as locking happens lower down in the filesystem.
1173 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1174 loff_t offset, unsigned long count)
1179 if ((u64)count > ~(u64)offset)
1180 return nfserr_inval;
1182 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1185 if (EX_ISSYNC(fhp->fh_export)) {
1186 if (file->f_op && file->f_op->fsync) {
1187 err = nfserrno(nfsd_sync(file));
1189 err = nfserr_notsupp;
1196 #endif /* CONFIG_NFSD_V3 */
1199 nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1203 * Mode has already been set earlier in create:
1205 iap->ia_valid &= ~ATTR_MODE;
1207 * Setting uid/gid works only for root. Irix appears to
1208 * send along the gid on create when it tries to implement
1209 * setgid directories via NFS:
1211 if (current_fsuid() != 0)
1212 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1214 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1218 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1219 * setting size to 0 may fail for some specific file systems by the permission
1220 * checking which requires WRITE permission but the mode is 000.
1221 * we ignore the resizing(to 0) on the just new created file, since the size is
1222 * 0 after file created.
1224 * call this only after vfs_create() is called.
1227 nfsd_check_ignore_resizing(struct iattr *iap)
1229 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1230 iap->ia_valid &= ~ATTR_SIZE;
1234 * Create a file (regular, directory, device, fifo); UNIX sockets
1235 * not yet implemented.
1236 * If the response fh has been verified, the parent directory should
1237 * already be locked. Note that the parent directory is left locked.
1239 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1242 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1243 char *fname, int flen, struct iattr *iap,
1244 int type, dev_t rdev, struct svc_fh *resfhp)
1246 struct dentry *dentry, *dchild = NULL;
1256 if (isdotent(fname, flen))
1259 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1263 dentry = fhp->fh_dentry;
1264 dirp = dentry->d_inode;
1266 err = nfserr_notdir;
1267 if (!dirp->i_op->lookup)
1270 * Check whether the response file handle has been verified yet.
1271 * If it has, the parent directory should already be locked.
1273 if (!resfhp->fh_dentry) {
1274 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
1275 fh_lock_nested(fhp, I_MUTEX_PARENT);
1276 dchild = lookup_one_len(fname, dentry, flen);
1277 host_err = PTR_ERR(dchild);
1280 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1284 /* called from nfsd_proc_create */
1285 dchild = dget(resfhp->fh_dentry);
1286 if (!fhp->fh_locked) {
1287 /* not actually possible */
1289 "nfsd_create: parent %s/%s not locked!\n",
1290 dentry->d_parent->d_name.name,
1291 dentry->d_name.name);
1297 * Make sure the child dentry is still negative ...
1300 if (dchild->d_inode) {
1301 dprintk("nfsd_create: dentry %s/%s not negative!\n",
1302 dentry->d_name.name, dchild->d_name.name);
1306 if (!(iap->ia_valid & ATTR_MODE))
1308 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1311 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1312 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1317 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1322 * Get the dir op function pointer.
1327 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1329 nfsd_check_ignore_resizing(iap);
1332 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
1338 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
1342 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1346 if (EX_ISSYNC(fhp->fh_export)) {
1347 err = nfserrno(nfsd_sync_dir(dentry));
1348 write_inode_now(dchild->d_inode, 1);
1351 err2 = nfsd_create_setattr(rqstp, resfhp, iap);
1354 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1356 * Update the file handle to get the new inode info.
1359 err = fh_update(resfhp);
1361 if (dchild && !IS_ERR(dchild))
1366 err = nfserrno(host_err);
1370 #ifdef CONFIG_NFSD_V3
1372 * NFSv3 version of nfsd_create
1375 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
1376 char *fname, int flen, struct iattr *iap,
1377 struct svc_fh *resfhp, int createmode, u32 *verifier,
1378 int *truncp, int *created)
1380 struct dentry *dentry, *dchild = NULL;
1385 __u32 v_mtime=0, v_atime=0;
1391 if (isdotent(fname, flen))
1393 if (!(iap->ia_valid & ATTR_MODE))
1395 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1399 dentry = fhp->fh_dentry;
1400 dirp = dentry->d_inode;
1402 /* Get all the sanity checks out of the way before
1403 * we lock the parent. */
1404 err = nfserr_notdir;
1405 if (!dirp->i_op->lookup)
1407 fh_lock_nested(fhp, I_MUTEX_PARENT);
1410 * Compose the response file handle.
1412 dchild = lookup_one_len(fname, dentry, flen);
1413 host_err = PTR_ERR(dchild);
1417 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1421 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1422 /* solaris7 gets confused (bugid 4218508) if these have
1423 * the high bit set, so just clear the high bits. If this is
1424 * ever changed to use different attrs for storing the
1425 * verifier, then do_open_lookup() will also need to be fixed
1428 v_mtime = verifier[0]&0x7fffffff;
1429 v_atime = verifier[1]&0x7fffffff;
1432 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1435 if (dchild->d_inode) {
1438 switch (createmode) {
1439 case NFS3_CREATE_UNCHECKED:
1440 if (! S_ISREG(dchild->d_inode->i_mode))
1443 /* in nfsv4, we need to treat this case a little
1444 * differently. we don't want to truncate the
1445 * file now; this would be wrong if the OPEN
1446 * fails for some other reason. furthermore,
1447 * if the size is nonzero, we should ignore it
1448 * according to spec!
1450 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1453 iap->ia_valid &= ATTR_SIZE;
1457 case NFS3_CREATE_EXCLUSIVE:
1458 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1459 && dchild->d_inode->i_atime.tv_sec == v_atime
1460 && dchild->d_inode->i_size == 0 )
1463 case NFS3_CREATE_GUARDED:
1466 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1470 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1472 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1478 if (EX_ISSYNC(fhp->fh_export)) {
1479 err = nfserrno(nfsd_sync_dir(dentry));
1480 /* setattr will sync the child (or not) */
1483 nfsd_check_ignore_resizing(iap);
1485 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1486 /* Cram the verifier into atime/mtime */
1487 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1488 | ATTR_MTIME_SET|ATTR_ATIME_SET;
1489 /* XXX someone who knows this better please fix it for nsec */
1490 iap->ia_mtime.tv_sec = v_mtime;
1491 iap->ia_atime.tv_sec = v_atime;
1492 iap->ia_mtime.tv_nsec = 0;
1493 iap->ia_atime.tv_nsec = 0;
1497 err2 = nfsd_create_setattr(rqstp, resfhp, iap);
1501 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1503 * Update the filehandle to get the new inode info.
1506 err = fh_update(resfhp);
1510 if (dchild && !IS_ERR(dchild))
1515 err = nfserrno(host_err);
1518 #endif /* CONFIG_NFSD_V3 */
1521 * Read a symlink. On entry, *lenp must contain the maximum path length that
1522 * fits into the buffer. On return, it contains the true length.
1523 * N.B. After this call fhp needs an fh_put
1526 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1528 struct dentry *dentry;
1529 struct inode *inode;
1534 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1538 dentry = fhp->fh_dentry;
1539 inode = dentry->d_inode;
1542 if (!inode->i_op->readlink)
1545 touch_atime(fhp->fh_export->ex_path.mnt, dentry);
1546 /* N.B. Why does this call need a get_fs()??
1547 * Remove the set_fs and watch the fireworks:-) --okir
1550 oldfs = get_fs(); set_fs(KERNEL_DS);
1551 host_err = inode->i_op->readlink(dentry, buf, *lenp);
1562 err = nfserrno(host_err);
1567 * Create a symlink and look up its inode
1568 * N.B. After this call _both_ fhp and resfhp need an fh_put
1571 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1572 char *fname, int flen,
1573 char *path, int plen,
1574 struct svc_fh *resfhp,
1577 struct dentry *dentry, *dnew;
1585 if (isdotent(fname, flen))
1588 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1592 dentry = fhp->fh_dentry;
1593 dnew = lookup_one_len(fname, dentry, flen);
1594 host_err = PTR_ERR(dnew);
1598 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1602 if (unlikely(path[plen] != 0)) {
1603 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1604 if (path_alloced == NULL)
1607 strncpy(path_alloced, path, plen);
1608 path_alloced[plen] = 0;
1609 host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
1610 kfree(path_alloced);
1613 host_err = vfs_symlink(dentry->d_inode, dnew, path);
1616 if (EX_ISSYNC(fhp->fh_export))
1617 host_err = nfsd_sync_dir(dentry);
1619 err = nfserrno(host_err);
1622 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1624 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1626 if (err==0) err = cerr;
1631 err = nfserrno(host_err);
1637 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1640 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1641 char *name, int len, struct svc_fh *tfhp)
1643 struct dentry *ddir, *dnew, *dold;
1644 struct inode *dirp, *dest;
1648 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1651 err = fh_verify(rqstp, tfhp, -S_IFDIR, NFSD_MAY_NOP);
1659 if (isdotent(name, len))
1662 fh_lock_nested(ffhp, I_MUTEX_PARENT);
1663 ddir = ffhp->fh_dentry;
1664 dirp = ddir->d_inode;
1666 dnew = lookup_one_len(name, ddir, len);
1667 host_err = PTR_ERR(dnew);
1671 dold = tfhp->fh_dentry;
1672 dest = dold->d_inode;
1674 host_err = mnt_want_write(tfhp->fh_export->ex_path.mnt);
1676 err = nfserrno(host_err);
1679 host_err = vfs_link(dold, dirp, dnew);
1681 if (EX_ISSYNC(ffhp->fh_export)) {
1682 err = nfserrno(nfsd_sync_dir(ddir));
1683 write_inode_now(dest, 1);
1687 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1690 err = nfserrno(host_err);
1692 mnt_drop_write(tfhp->fh_export->ex_path.mnt);
1701 err = nfserrno(host_err);
1707 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1710 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1711 struct svc_fh *tfhp, char *tname, int tlen)
1713 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1714 struct inode *fdir, *tdir;
1718 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1721 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1725 fdentry = ffhp->fh_dentry;
1726 fdir = fdentry->d_inode;
1728 tdentry = tfhp->fh_dentry;
1729 tdir = tdentry->d_inode;
1731 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1732 if (ffhp->fh_export != tfhp->fh_export)
1736 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1739 /* cannot use fh_lock as we need deadlock protective ordering
1740 * so do it by hand */
1741 trap = lock_rename(tdentry, fdentry);
1742 ffhp->fh_locked = tfhp->fh_locked = 1;
1746 odentry = lookup_one_len(fname, fdentry, flen);
1747 host_err = PTR_ERR(odentry);
1748 if (IS_ERR(odentry))
1752 if (!odentry->d_inode)
1755 if (odentry == trap)
1758 ndentry = lookup_one_len(tname, tdentry, tlen);
1759 host_err = PTR_ERR(ndentry);
1760 if (IS_ERR(ndentry))
1762 host_err = -ENOTEMPTY;
1763 if (ndentry == trap)
1766 if (svc_msnfs(ffhp) &&
1767 ((atomic_read(&odentry->d_count) > 1)
1768 || (atomic_read(&ndentry->d_count) > 1))) {
1774 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1776 host_err = mnt_want_write(ffhp->fh_export->ex_path.mnt);
1780 host_err = vfs_rename(fdir, odentry, tdir, ndentry);
1781 if (!host_err && EX_ISSYNC(tfhp->fh_export)) {
1782 host_err = nfsd_sync_dir(tdentry);
1784 host_err = nfsd_sync_dir(fdentry);
1787 mnt_drop_write(ffhp->fh_export->ex_path.mnt);
1794 err = nfserrno(host_err);
1796 /* we cannot reply on fh_unlock on the two filehandles,
1797 * as that would do the wrong thing if the two directories
1798 * were the same, so again we do it by hand
1800 fill_post_wcc(ffhp);
1801 fill_post_wcc(tfhp);
1802 unlock_rename(tdentry, fdentry);
1803 ffhp->fh_locked = tfhp->fh_locked = 0;
1810 * Unlink a file or directory
1811 * N.B. After this call fhp needs an fh_put
1814 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1815 char *fname, int flen)
1817 struct dentry *dentry, *rdentry;
1823 if (!flen || isdotent(fname, flen))
1825 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1829 fh_lock_nested(fhp, I_MUTEX_PARENT);
1830 dentry = fhp->fh_dentry;
1831 dirp = dentry->d_inode;
1833 rdentry = lookup_one_len(fname, dentry, flen);
1834 host_err = PTR_ERR(rdentry);
1835 if (IS_ERR(rdentry))
1838 if (!rdentry->d_inode) {
1845 type = rdentry->d_inode->i_mode & S_IFMT;
1847 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1851 if (type != S_IFDIR) { /* It's UNLINK */
1853 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1854 (atomic_read(&rdentry->d_count) > 1)) {
1858 host_err = vfs_unlink(dirp, rdentry);
1859 } else { /* It's RMDIR */
1860 host_err = vfs_rmdir(dirp, rdentry);
1867 if (EX_ISSYNC(fhp->fh_export))
1868 host_err = nfsd_sync_dir(dentry);
1871 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1873 err = nfserrno(host_err);
1879 * We do this buffering because we must not call back into the file
1880 * system's ->lookup() method from the filldir callback. That may well
1881 * deadlock a number of file systems.
1883 * This is based heavily on the implementation of same in XFS.
1885 struct buffered_dirent {
1889 unsigned int d_type;
1893 struct readdir_data {
1899 static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1900 loff_t offset, u64 ino, unsigned int d_type)
1902 struct readdir_data *buf = __buf;
1903 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1904 unsigned int reclen;
1906 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1907 if (buf->used + reclen > PAGE_SIZE) {
1912 de->namlen = namlen;
1913 de->offset = offset;
1915 de->d_type = d_type;
1916 memcpy(de->name, name, namlen);
1917 buf->used += reclen;
1922 static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
1923 struct readdir_cd *cdp, loff_t *offsetp)
1925 struct readdir_data buf;
1926 struct buffered_dirent *de;
1931 buf.dirent = (void *)__get_free_page(GFP_KERNEL);
1933 return nfserrno(-ENOMEM);
1938 struct inode *dir_inode = file->f_path.dentry->d_inode;
1939 unsigned int reclen;
1941 cdp->err = nfserr_eof; /* will be cleared on successful read */
1945 host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
1958 * Various filldir functions may end up calling back into
1959 * lookup_one_len() and the file system's ->lookup() method.
1960 * These expect i_mutex to be held, as it would within readdir.
1962 host_err = mutex_lock_killable(&dir_inode->i_mutex);
1966 de = (struct buffered_dirent *)buf.dirent;
1968 offset = de->offset;
1970 if (func(cdp, de->name, de->namlen, de->offset,
1971 de->ino, de->d_type))
1974 if (cdp->err != nfs_ok)
1977 reclen = ALIGN(sizeof(*de) + de->namlen,
1980 de = (struct buffered_dirent *)((char *)de + reclen);
1982 mutex_unlock(&dir_inode->i_mutex);
1983 if (size > 0) /* We bailed out early */
1986 offset = vfs_llseek(file, 0, SEEK_CUR);
1989 free_page((unsigned long)(buf.dirent));
1992 return nfserrno(host_err);
1999 * Read entries from a directory.
2000 * The NFSv3/4 verifier we ignore for now.
2003 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
2004 struct readdir_cd *cdp, filldir_t func)
2008 loff_t offset = *offsetp;
2010 err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file);
2014 offset = vfs_llseek(file, offset, 0);
2016 err = nfserrno((int)offset);
2020 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
2022 if (err == nfserr_eof || err == nfserr_toosmall)
2023 err = nfs_ok; /* can still be found in ->err */
2031 * Get file system stats
2032 * N.B. After this call fhp needs an fh_put
2035 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
2037 __be32 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
2038 if (!err && vfs_statfs(fhp->fh_dentry,stat))
2043 static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
2045 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
2049 * Check for a user's access permissions to this inode.
2052 nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2053 struct dentry *dentry, int acc)
2055 struct inode *inode = dentry->d_inode;
2059 if (acc == NFSD_MAY_NOP)
2062 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2064 (acc & NFSD_MAY_READ)? " read" : "",
2065 (acc & NFSD_MAY_WRITE)? " write" : "",
2066 (acc & NFSD_MAY_EXEC)? " exec" : "",
2067 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2068 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2069 (acc & NFSD_MAY_LOCK)? " lock" : "",
2070 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2072 IS_IMMUTABLE(inode)? " immut" : "",
2073 IS_APPEND(inode)? " append" : "",
2074 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
2075 dprintk(" owner %d/%d user %d/%d\n",
2076 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2079 /* Normally we reject any write/sattr etc access on a read-only file
2080 * system. But if it is IRIX doing check on write-access for a
2081 * device special file, we ignore rofs.
2083 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2084 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2085 if (exp_rdonly(rqstp, exp) ||
2086 __mnt_is_readonly(exp->ex_path.mnt))
2088 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
2091 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2094 if (acc & NFSD_MAY_LOCK) {
2095 /* If we cannot rely on authentication in NLM requests,
2096 * just allow locks, otherwise require read permission, or
2099 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2102 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2105 * The file owner always gets access permission for accesses that
2106 * would normally be checked at open time. This is to make
2107 * file access work even when the client has done a fchmod(fd, 0).
2109 * However, `cp foo bar' should fail nevertheless when bar is
2110 * readonly. A sensible way to do this might be to reject all
2111 * attempts to truncate a read-only file, because a creat() call
2112 * always implies file truncation.
2113 * ... but this isn't really fair. A process may reasonably call
2114 * ftruncate on an open file descriptor on a file with perm 000.
2115 * We must trust the client to do permission checking - using "ACCESS"
2118 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2119 inode->i_uid == current_fsuid())
2122 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2123 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
2125 /* Allow read access to binaries even when mode 111 */
2126 if (err == -EACCES && S_ISREG(inode->i_mode) &&
2127 acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE))
2128 err = inode_permission(inode, MAY_EXEC);
2132 /* Do integrity (permission) checking now, but defer incrementing
2133 * IMA counts to the actual file open.
2135 path.mnt = exp->ex_path.mnt;
2136 path.dentry = dentry;
2137 err = ima_path_check(&path, acc & (MAY_READ | MAY_WRITE | MAY_EXEC),
2140 return err? nfserrno(err) : 0;
2144 nfsd_racache_shutdown(void)
2146 struct raparms *raparm, *last_raparm;
2149 dprintk("nfsd: freeing readahead buffers.\n");
2151 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2152 raparm = raparm_hash[i].pb_head;
2154 last_raparm = raparm;
2155 raparm = raparm->p_next;
2158 raparm_hash[i].pb_head = NULL;
2162 * Initialize readahead param cache
2165 nfsd_racache_init(int cache_size)
2170 struct raparms **raparm = NULL;
2173 if (raparm_hash[0].pb_head)
2175 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2178 cache_size = nperbucket * RAPARM_HASH_SIZE;
2180 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
2182 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2183 spin_lock_init(&raparm_hash[i].pb_lock);
2185 raparm = &raparm_hash[i].pb_head;
2186 for (j = 0; j < nperbucket; j++) {
2187 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2190 raparm = &(*raparm)->p_next;
2195 nfsdstats.ra_size = cache_size;
2199 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2200 nfsd_racache_shutdown();
2204 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2206 nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2208 struct inode *inode = fhp->fh_dentry->d_inode;
2212 struct posix_acl *acl;
2214 if (!IS_POSIXACL(inode))
2215 return ERR_PTR(-EOPNOTSUPP);
2218 case ACL_TYPE_ACCESS:
2219 name = POSIX_ACL_XATTR_ACCESS;
2221 case ACL_TYPE_DEFAULT:
2222 name = POSIX_ACL_XATTR_DEFAULT;
2225 return ERR_PTR(-EOPNOTSUPP);
2228 size = nfsd_getxattr(fhp->fh_dentry, name, &value);
2230 return ERR_PTR(size);
2232 acl = posix_acl_from_xattr(value, size);
2238 nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2240 struct inode *inode = fhp->fh_dentry->d_inode;
2246 if (!IS_POSIXACL(inode) ||
2247 !inode->i_op->setxattr || !inode->i_op->removexattr)
2250 case ACL_TYPE_ACCESS:
2251 name = POSIX_ACL_XATTR_ACCESS;
2253 case ACL_TYPE_DEFAULT:
2254 name = POSIX_ACL_XATTR_DEFAULT;
2260 if (acl && acl->a_count) {
2261 size = posix_acl_xattr_size(acl->a_count);
2262 value = kmalloc(size, GFP_KERNEL);
2265 error = posix_acl_to_xattr(acl, value, size);
2272 error = mnt_want_write(fhp->fh_export->ex_path.mnt);
2276 error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
2278 if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2281 error = vfs_removexattr(fhp->fh_dentry, name);
2282 if (error == -ENODATA)
2286 mnt_drop_write(fhp->fh_export->ex_path.mnt);
2292 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */