1 #define MSNFS /* HACK HACK */
3 * File operations used by nfsd. Some of these have been ripped from
4 * other parts of the kernel because they weren't exported, others
5 * are partial duplicates with added or changed functionality.
7 * Note that several functions dget() the dentry upon which they want
8 * to act, most notably those that create directory entries. Response
9 * dentry's are dput()'d if necessary in the release callback.
10 * So if you notice code paths that apparently fail to dput() the
11 * dentry, don't worry--they have been taken care of.
13 * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
14 * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
18 #include <linux/file.h>
19 #include <linux/splice.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 <asm/uaccess.h>
32 #endif /* CONFIG_NFSD_V3 */
35 #include <linux/nfs4_acl.h>
36 #include <linux/nfsd_idmap.h>
37 #endif /* CONFIG_NFSD_V4 */
42 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
46 * This is a cache of readahead params that help us choose the proper
47 * readahead strategy. Initially, we set all readahead parameters to 0
48 * and let the VFS handle things.
49 * If you increase the number of cached files very much, you'll need to
50 * add a hash table here.
53 struct raparms *p_next;
58 struct file_ra_state p_ra;
59 unsigned int p_hindex;
62 struct raparm_hbucket {
63 struct raparms *pb_head;
65 } ____cacheline_aligned_in_smp;
67 #define RAPARM_HASH_BITS 4
68 #define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
69 #define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
70 static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
73 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
75 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
76 * or nfs_ok having possibly changed *dpp and *expp
79 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
80 struct svc_export **expp)
82 struct svc_export *exp = *expp, *exp2 = NULL;
83 struct dentry *dentry = *dpp;
84 struct path path = {.mnt = mntget(exp->ex_path.mnt),
85 .dentry = dget(dentry)};
88 while (d_mountpoint(path.dentry) && follow_down(&path))
91 exp2 = rqst_exp_get_by_name(rqstp, &path);
95 * We normally allow NFS clients to continue
96 * "underneath" a mountpoint that is not exported.
97 * The exception is V4ROOT, where no traversal is ever
98 * allowed without an explicit export of the new
101 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
106 if (nfsd_v4client(rqstp) ||
107 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
108 /* successfully crossed mount point */
110 * This is subtle: path.dentry is *not* on path.mnt
111 * at this point. The only reason we are safe is that
112 * original mnt is pinned down by exp, so we should
113 * put path *before* putting exp
116 path.dentry = dentry;
126 static void follow_to_parent(struct path *path)
130 while (path->dentry == path->mnt->mnt_root && follow_up(path))
132 dp = dget_parent(path->dentry);
137 static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
139 struct svc_export *exp2;
140 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
141 .dentry = dget(dparent)};
143 follow_to_parent(&path);
145 exp2 = rqst_exp_parent(rqstp, &path);
146 if (PTR_ERR(exp2) == -ENOENT) {
147 *dentryp = dget(dparent);
148 } else if (IS_ERR(exp2)) {
150 return PTR_ERR(exp2);
152 *dentryp = dget(path.dentry);
161 * For nfsd purposes, we treat V4ROOT exports as though there was an
162 * export at *every* directory.
164 int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
166 if (d_mountpoint(dentry))
168 if (!(exp->ex_flags & NFSEXP_V4ROOT))
170 return dentry->d_inode != NULL;
174 nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
175 const char *name, unsigned int len,
176 struct svc_export **exp_ret, struct dentry **dentry_ret)
178 struct svc_export *exp;
179 struct dentry *dparent;
180 struct dentry *dentry;
184 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
186 /* Obtain dentry and export. */
187 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
191 dparent = fhp->fh_dentry;
192 exp = fhp->fh_export;
195 /* Lookup the name, but don't follow links */
196 if (isdotent(name, len)) {
198 dentry = dget(dparent);
199 else if (dparent != exp->ex_path.dentry)
200 dentry = dget_parent(dparent);
201 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
202 dentry = dget(dparent); /* .. == . just like at / */
204 /* checking mountpoint crossing is very different when stepping up */
205 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
211 dentry = lookup_one_len(name, dparent, len);
212 host_err = PTR_ERR(dentry);
216 * check if we have crossed a mount point ...
218 if (nfsd_mountpoint(dentry, exp)) {
219 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
225 *dentry_ret = dentry;
231 return nfserrno(host_err);
235 * Look up one component of a pathname.
236 * N.B. After this call _both_ fhp and resfh need an fh_put
238 * If the lookup would cross a mountpoint, and the mounted filesystem
239 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
240 * accepted as it stands and the mounted directory is
241 * returned. Otherwise the covered directory is returned.
242 * NOTE: this mountpoint crossing is not supported properly by all
243 * clients and is explicitly disallowed for NFSv3
244 * NeilBrown <neilb@cse.unsw.edu.au>
247 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
248 unsigned int len, struct svc_fh *resfh)
250 struct svc_export *exp;
251 struct dentry *dentry;
254 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
257 err = check_nfsd_access(exp, rqstp);
261 * Note: we compose the file handle now, but as the
262 * dentry may be negative, it may need to be updated.
264 err = fh_compose(resfh, exp, dentry, fhp);
265 if (!err && !dentry->d_inode)
275 * Set various file attributes.
276 * N.B. After this call fhp needs an fh_put
279 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
280 int check_guard, time_t guardtime)
282 struct dentry *dentry;
284 int accmode = NFSD_MAY_SATTR;
290 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
291 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
292 if (iap->ia_valid & ATTR_SIZE)
296 err = fh_verify(rqstp, fhp, ftype, accmode);
300 dentry = fhp->fh_dentry;
301 inode = dentry->d_inode;
303 /* Ignore any mode updates on symlinks */
304 if (S_ISLNK(inode->i_mode))
305 iap->ia_valid &= ~ATTR_MODE;
311 * NFSv2 does not differentiate between "set-[ac]time-to-now"
312 * which only requires access, and "set-[ac]time-to-X" which
313 * requires ownership.
314 * So if it looks like it might be "set both to the same time which
315 * is close to now", and if inode_change_ok fails, then we
316 * convert to "set to now" instead of "set to explicit time"
318 * We only call inode_change_ok as the last test as technically
319 * it is not an interface that we should be using. It is only
320 * valid if the filesystem does not define it's own i_op->setattr.
322 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
323 #define MAX_TOUCH_TIME_ERROR (30*60)
324 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
325 iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
329 * Now just make sure time is in the right ballpark.
330 * Solaris, at least, doesn't seem to care what the time
331 * request is. We require it be within 30 minutes of now.
333 time_t delta = iap->ia_atime.tv_sec - get_seconds();
336 if (delta < MAX_TOUCH_TIME_ERROR &&
337 inode_change_ok(inode, iap) != 0) {
339 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
340 * This will cause notify_change to set these times
343 iap->ia_valid &= ~BOTH_TIME_SET;
348 * The size case is special.
349 * It changes the file as well as the attributes.
351 if (iap->ia_valid & ATTR_SIZE) {
352 if (iap->ia_size < inode->i_size) {
353 err = nfsd_permission(rqstp, fhp->fh_export, dentry,
354 NFSD_MAY_TRUNC|NFSD_MAY_OWNER_OVERRIDE);
360 * If we are changing the size of the file, then
361 * we need to break all leases.
363 host_err = break_lease(inode, O_WRONLY | O_NONBLOCK);
364 if (host_err == -EWOULDBLOCK)
365 host_err = -ETIMEDOUT;
366 if (host_err) /* ENOMEM or EWOULDBLOCK */
369 host_err = get_write_access(inode);
374 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
376 put_write_access(inode);
381 /* sanitize the mode change */
382 if (iap->ia_valid & ATTR_MODE) {
383 iap->ia_mode &= S_IALLUGO;
384 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
387 /* Revoke setuid/setgid on chown */
388 if (!S_ISDIR(inode->i_mode) &&
389 (((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid) ||
390 ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid))) {
391 iap->ia_valid |= ATTR_KILL_PRIV;
392 if (iap->ia_valid & ATTR_MODE) {
393 /* we're setting mode too, just clear the s*id bits */
394 iap->ia_mode &= ~S_ISUID;
395 if (iap->ia_mode & S_IXGRP)
396 iap->ia_mode &= ~S_ISGID;
398 /* set ATTR_KILL_* bits and let VFS handle it */
399 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
403 /* Change the attributes. */
405 iap->ia_valid |= ATTR_CTIME;
407 err = nfserr_notsync;
408 if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
410 host_err = notify_change(dentry, iap);
411 err = nfserrno(host_err);
415 put_write_access(inode);
417 if (EX_ISSYNC(fhp->fh_export))
418 write_inode_now(inode, 1);
423 err = nfserrno(host_err);
427 #if defined(CONFIG_NFSD_V2_ACL) || \
428 defined(CONFIG_NFSD_V3_ACL) || \
429 defined(CONFIG_NFSD_V4)
430 static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
435 buflen = vfs_getxattr(dentry, key, NULL, 0);
439 *buf = kmalloc(buflen, GFP_KERNEL);
443 ret = vfs_getxattr(dentry, key, *buf, buflen);
450 #if defined(CONFIG_NFSD_V4)
452 set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
459 buflen = posix_acl_xattr_size(pacl->a_count);
460 buf = kmalloc(buflen, GFP_KERNEL);
465 len = posix_acl_to_xattr(pacl, buf, buflen);
471 error = vfs_setxattr(dentry, key, buf, len, 0);
478 nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
479 struct nfs4_acl *acl)
483 struct dentry *dentry;
485 struct posix_acl *pacl = NULL, *dpacl = NULL;
486 unsigned int flags = 0;
489 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
493 dentry = fhp->fh_dentry;
494 inode = dentry->d_inode;
495 if (S_ISDIR(inode->i_mode))
496 flags = NFS4_ACL_DIR;
498 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
499 if (host_error == -EINVAL) {
500 return nfserr_attrnotsupp;
501 } else if (host_error < 0)
504 host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
508 if (S_ISDIR(inode->i_mode))
509 host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
512 posix_acl_release(pacl);
513 posix_acl_release(dpacl);
515 if (host_error == -EOPNOTSUPP)
516 return nfserr_attrnotsupp;
518 return nfserrno(host_error);
521 static struct posix_acl *
522 _get_posix_acl(struct dentry *dentry, char *key)
525 struct posix_acl *pacl = NULL;
528 buflen = nfsd_getxattr(dentry, key, &buf);
532 return ERR_PTR(buflen);
534 pacl = posix_acl_from_xattr(buf, buflen);
540 nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
542 struct inode *inode = dentry->d_inode;
544 struct posix_acl *pacl = NULL, *dpacl = NULL;
545 unsigned int flags = 0;
547 pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
548 if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
549 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
551 error = PTR_ERR(pacl);
556 if (S_ISDIR(inode->i_mode)) {
557 dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
558 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
560 else if (IS_ERR(dpacl)) {
561 error = PTR_ERR(dpacl);
565 flags = NFS4_ACL_DIR;
568 *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
570 error = PTR_ERR(*acl);
574 posix_acl_release(pacl);
575 posix_acl_release(dpacl);
579 #endif /* defined(CONFIG_NFS_V4) */
581 #ifdef CONFIG_NFSD_V3
583 * Check server access rights to a file system object
589 static struct accessmap nfs3_regaccess[] = {
590 { NFS3_ACCESS_READ, NFSD_MAY_READ },
591 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
592 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
593 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
598 static struct accessmap nfs3_diraccess[] = {
599 { NFS3_ACCESS_READ, NFSD_MAY_READ },
600 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
601 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
602 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
603 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
608 static struct accessmap nfs3_anyaccess[] = {
609 /* Some clients - Solaris 2.6 at least, make an access call
610 * to the server to check for access for things like /dev/null
611 * (which really, the server doesn't care about). So
612 * We provide simple access checking for them, looking
613 * mainly at mode bits, and we make sure to ignore read-only
616 { NFS3_ACCESS_READ, NFSD_MAY_READ },
617 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
618 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
619 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
625 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
627 struct accessmap *map;
628 struct svc_export *export;
629 struct dentry *dentry;
630 u32 query, result = 0, sresult = 0;
633 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
637 export = fhp->fh_export;
638 dentry = fhp->fh_dentry;
640 if (S_ISREG(dentry->d_inode->i_mode))
641 map = nfs3_regaccess;
642 else if (S_ISDIR(dentry->d_inode->i_mode))
643 map = nfs3_diraccess;
645 map = nfs3_anyaccess;
649 for (; map->access; map++) {
650 if (map->access & query) {
653 sresult |= map->access;
655 err2 = nfsd_permission(rqstp, export, dentry, map->how);
658 result |= map->access;
661 /* the following error codes just mean the access was not allowed,
662 * rather than an error occurred */
666 /* simply don't "or" in the access bit. */
676 *supported = sresult;
681 #endif /* CONFIG_NFSD_V3 */
686 * Open an existing file or directory.
687 * The access argument indicates the type of open (read/write/lock)
688 * N.B. After this call fhp needs an fh_put
691 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
692 int access, struct file **filp)
694 struct dentry *dentry;
696 int flags = O_RDONLY|O_LARGEFILE;
700 validate_process_creds();
703 * If we get here, then the client has already done an "open",
704 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
705 * in case a chmod has now revoked permission.
707 err = fh_verify(rqstp, fhp, type, access | NFSD_MAY_OWNER_OVERRIDE);
711 dentry = fhp->fh_dentry;
712 inode = dentry->d_inode;
714 /* Disallow write access to files with the append-only bit set
715 * or any access when mandatory locking enabled
718 if (IS_APPEND(inode) && (access & NFSD_MAY_WRITE))
721 * We must ignore files (but only files) which might have mandatory
722 * locks on them because there is no way to know if the accesser has
725 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
732 * Check to see if there are any leases on this file.
733 * This may block while leases are broken.
735 host_err = break_lease(inode, O_NONBLOCK | ((access & NFSD_MAY_WRITE) ? O_WRONLY : 0));
736 if (host_err == -EWOULDBLOCK)
737 host_err = -ETIMEDOUT;
738 if (host_err) /* NOMEM or WOULDBLOCK */
741 if (access & NFSD_MAY_WRITE) {
742 if (access & NFSD_MAY_READ)
743 flags = O_RDWR|O_LARGEFILE;
745 flags = O_WRONLY|O_LARGEFILE;
747 *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
748 flags, current_cred());
750 host_err = PTR_ERR(*filp);
752 host_err = ima_file_check(*filp, access);
754 err = nfserrno(host_err);
756 validate_process_creds();
764 nfsd_close(struct file *filp)
771 * As this calls fsync (not fdatasync) there is no need for a write_inode
774 static inline int nfsd_dosync(struct file *filp, struct dentry *dp,
775 const struct file_operations *fop)
777 struct inode *inode = dp->d_inode;
778 int (*fsync) (struct file *, struct dentry *, int);
781 err = filemap_write_and_wait(inode->i_mapping);
782 if (err == 0 && fop && (fsync = fop->fsync))
783 err = fsync(filp, dp, 0);
788 nfsd_sync(struct file *filp)
791 struct inode *inode = filp->f_path.dentry->d_inode;
792 dprintk("nfsd: sync file %s\n", filp->f_path.dentry->d_name.name);
793 mutex_lock(&inode->i_mutex);
794 err=nfsd_dosync(filp, filp->f_path.dentry, filp->f_op);
795 mutex_unlock(&inode->i_mutex);
801 nfsd_sync_dir(struct dentry *dp)
803 return nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
807 * Obtain the readahead parameters for the file
808 * specified by (dev, ino).
811 static inline struct raparms *
812 nfsd_get_raparms(dev_t dev, ino_t ino)
814 struct raparms *ra, **rap, **frap = NULL;
817 struct raparm_hbucket *rab;
819 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
820 rab = &raparm_hash[hash];
822 spin_lock(&rab->pb_lock);
823 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
824 if (ra->p_ino == ino && ra->p_dev == dev)
827 if (ra->p_count == 0)
830 depth = nfsdstats.ra_size*11/10;
832 spin_unlock(&rab->pb_lock);
842 if (rap != &rab->pb_head) {
844 ra->p_next = rab->pb_head;
848 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
849 spin_unlock(&rab->pb_lock);
854 * Grab and keep cached pages associated with a file in the svc_rqst
855 * so that they can be passed to the network sendmsg/sendpage routines
856 * directly. They will be released after the sending has completed.
859 nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
860 struct splice_desc *sd)
862 struct svc_rqst *rqstp = sd->u.data;
863 struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
864 struct page *page = buf->page;
868 ret = buf->ops->confirm(pipe, buf);
874 if (rqstp->rq_res.page_len == 0) {
879 rqstp->rq_res.page_base = buf->offset;
880 rqstp->rq_res.page_len = size;
881 } else if (page != pp[-1]) {
887 rqstp->rq_res.page_len += size;
889 rqstp->rq_res.page_len += size;
894 static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
895 struct splice_desc *sd)
897 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
900 static inline int svc_msnfs(struct svc_fh *ffhp)
903 return (ffhp->fh_export->ex_flags & NFSEXP_MSNFS);
910 nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
911 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
920 inode = file->f_path.dentry->d_inode;
922 if (svc_msnfs(fhp) && !lock_may_read(inode, offset, *count))
925 /* Get readahead parameters */
926 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
929 file->f_ra = ra->p_ra;
931 if (file->f_op->splice_read && rqstp->rq_splice_ok) {
932 struct splice_desc sd = {
939 rqstp->rq_resused = 1;
940 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
944 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
948 /* Write back readahead params */
950 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
951 spin_lock(&rab->pb_lock);
952 ra->p_ra = file->f_ra;
955 spin_unlock(&rab->pb_lock);
959 nfsdstats.io_read += host_err;
962 fsnotify_access(file->f_path.dentry);
964 err = nfserrno(host_err);
969 static void kill_suid(struct dentry *dentry)
972 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
974 mutex_lock(&dentry->d_inode->i_mutex);
975 notify_change(dentry, &ia);
976 mutex_unlock(&dentry->d_inode->i_mutex);
980 * Gathered writes: If another process is currently writing to the file,
981 * there's a high chance this is another nfsd (triggered by a bulk write
982 * from a client's biod). Rather than syncing the file with each write
983 * request, we sleep for 10 msec.
985 * I don't know if this roughly approximates C. Juszak's idea of
986 * gathered writes, but it's a nice and simple solution (IMHO), and it
989 * Note: we do this only in the NFSv2 case, since v3 and higher have a
990 * better tool (separate unstable writes and commits) for solving this
993 static int wait_for_concurrent_writes(struct file *file)
995 struct inode *inode = file->f_path.dentry->d_inode;
996 static ino_t last_ino;
997 static dev_t last_dev;
1000 if (atomic_read(&inode->i_writecount) > 1
1001 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
1002 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
1004 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1007 if (inode->i_state & I_DIRTY) {
1008 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
1009 err = nfsd_sync(file);
1011 last_ino = inode->i_ino;
1012 last_dev = inode->i_sb->s_dev;
1017 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1018 loff_t offset, struct kvec *vec, int vlen,
1019 unsigned long *cnt, int *stablep)
1021 struct svc_export *exp;
1022 struct dentry *dentry;
1023 struct inode *inode;
1027 int stable = *stablep;
1033 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1034 (!lock_may_write(file->f_path.dentry->d_inode, offset, *cnt)))
1038 dentry = file->f_path.dentry;
1039 inode = dentry->d_inode;
1040 exp = fhp->fh_export;
1043 * Request sync writes if
1044 * - the sync export option has been set, or
1045 * - the client requested O_SYNC behavior (NFSv3 feature).
1046 * - The file system doesn't support fsync().
1047 * When NFSv2 gathered writes have been configured for this volume,
1048 * flushing the data to disk is handled separately below.
1050 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1052 if (!file->f_op->fsync) {/* COMMIT3 cannot work */
1054 *stablep = 2; /* FILE_SYNC */
1057 if (!EX_ISSYNC(exp))
1059 if (stable && !use_wgather) {
1060 spin_lock(&file->f_lock);
1061 file->f_flags |= O_SYNC;
1062 spin_unlock(&file->f_lock);
1065 /* Write the data. */
1066 oldfs = get_fs(); set_fs(KERNEL_DS);
1067 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
1072 nfsdstats.io_write += host_err;
1073 fsnotify_modify(file->f_path.dentry);
1075 /* clear setuid/setgid flag after write */
1076 if (inode->i_mode & (S_ISUID | S_ISGID))
1079 if (stable && use_wgather)
1080 host_err = wait_for_concurrent_writes(file);
1083 dprintk("nfsd: write complete host_err=%d\n", host_err);
1087 err = nfserrno(host_err);
1093 * Read data from a file. count must contain the requested read count
1094 * on entry. On return, *count contains the number of bytes actually read.
1095 * N.B. After this call fhp needs an fh_put
1098 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1099 loff_t offset, struct kvec *vec, int vlen,
1100 unsigned long *count)
1105 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1106 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
1109 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1111 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1114 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1122 * Write data to a file.
1123 * The stable flag requests synchronous writes.
1124 * N.B. After this call fhp needs an fh_put
1127 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1128 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
1134 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1135 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
1138 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1141 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1146 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1154 #ifdef CONFIG_NFSD_V3
1156 * Commit all pending writes to stable storage.
1157 * Strictly speaking, we could sync just the indicated file region here,
1158 * but there's currently no way we can ask the VFS to do so.
1160 * Unfortunately we cannot lock the file to make sure we return full WCC
1161 * data to the client, as locking happens lower down in the filesystem.
1164 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1165 loff_t offset, unsigned long count)
1170 if ((u64)count > ~(u64)offset)
1171 return nfserr_inval;
1173 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1176 if (EX_ISSYNC(fhp->fh_export)) {
1177 if (file->f_op && file->f_op->fsync) {
1178 err = nfserrno(nfsd_sync(file));
1180 err = nfserr_notsupp;
1187 #endif /* CONFIG_NFSD_V3 */
1190 nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1194 * Mode has already been set earlier in create:
1196 iap->ia_valid &= ~ATTR_MODE;
1198 * Setting uid/gid works only for root. Irix appears to
1199 * send along the gid on create when it tries to implement
1200 * setgid directories via NFS:
1202 if (current_fsuid() != 0)
1203 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1205 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1209 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1210 * setting size to 0 may fail for some specific file systems by the permission
1211 * checking which requires WRITE permission but the mode is 000.
1212 * we ignore the resizing(to 0) on the just new created file, since the size is
1213 * 0 after file created.
1215 * call this only after vfs_create() is called.
1218 nfsd_check_ignore_resizing(struct iattr *iap)
1220 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1221 iap->ia_valid &= ~ATTR_SIZE;
1225 * Create a file (regular, directory, device, fifo); UNIX sockets
1226 * not yet implemented.
1227 * If the response fh has been verified, the parent directory should
1228 * already be locked. Note that the parent directory is left locked.
1230 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1233 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1234 char *fname, int flen, struct iattr *iap,
1235 int type, dev_t rdev, struct svc_fh *resfhp)
1237 struct dentry *dentry, *dchild = NULL;
1247 if (isdotent(fname, flen))
1250 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1254 dentry = fhp->fh_dentry;
1255 dirp = dentry->d_inode;
1257 err = nfserr_notdir;
1258 if (!dirp->i_op->lookup)
1261 * Check whether the response file handle has been verified yet.
1262 * If it has, the parent directory should already be locked.
1264 if (!resfhp->fh_dentry) {
1265 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
1266 fh_lock_nested(fhp, I_MUTEX_PARENT);
1267 dchild = lookup_one_len(fname, dentry, flen);
1268 host_err = PTR_ERR(dchild);
1271 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1275 /* called from nfsd_proc_create */
1276 dchild = dget(resfhp->fh_dentry);
1277 if (!fhp->fh_locked) {
1278 /* not actually possible */
1280 "nfsd_create: parent %s/%s not locked!\n",
1281 dentry->d_parent->d_name.name,
1282 dentry->d_name.name);
1288 * Make sure the child dentry is still negative ...
1291 if (dchild->d_inode) {
1292 dprintk("nfsd_create: dentry %s/%s not negative!\n",
1293 dentry->d_name.name, dchild->d_name.name);
1297 if (!(iap->ia_valid & ATTR_MODE))
1299 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1302 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1303 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1308 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1313 * Get the dir op function pointer.
1318 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1320 nfsd_check_ignore_resizing(iap);
1323 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
1329 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
1333 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1337 if (EX_ISSYNC(fhp->fh_export)) {
1338 err = nfserrno(nfsd_sync_dir(dentry));
1339 write_inode_now(dchild->d_inode, 1);
1342 err2 = nfsd_create_setattr(rqstp, resfhp, iap);
1345 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1347 * Update the file handle to get the new inode info.
1350 err = fh_update(resfhp);
1352 if (dchild && !IS_ERR(dchild))
1357 err = nfserrno(host_err);
1361 #ifdef CONFIG_NFSD_V3
1363 * NFSv3 version of nfsd_create
1366 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
1367 char *fname, int flen, struct iattr *iap,
1368 struct svc_fh *resfhp, int createmode, u32 *verifier,
1369 int *truncp, int *created)
1371 struct dentry *dentry, *dchild = NULL;
1376 __u32 v_mtime=0, v_atime=0;
1382 if (isdotent(fname, flen))
1384 if (!(iap->ia_valid & ATTR_MODE))
1386 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1390 dentry = fhp->fh_dentry;
1391 dirp = dentry->d_inode;
1393 /* Get all the sanity checks out of the way before
1394 * we lock the parent. */
1395 err = nfserr_notdir;
1396 if (!dirp->i_op->lookup)
1398 fh_lock_nested(fhp, I_MUTEX_PARENT);
1401 * Compose the response file handle.
1403 dchild = lookup_one_len(fname, dentry, flen);
1404 host_err = PTR_ERR(dchild);
1408 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1412 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1413 /* solaris7 gets confused (bugid 4218508) if these have
1414 * the high bit set, so just clear the high bits. If this is
1415 * ever changed to use different attrs for storing the
1416 * verifier, then do_open_lookup() will also need to be fixed
1419 v_mtime = verifier[0]&0x7fffffff;
1420 v_atime = verifier[1]&0x7fffffff;
1423 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1426 if (dchild->d_inode) {
1429 switch (createmode) {
1430 case NFS3_CREATE_UNCHECKED:
1431 if (! S_ISREG(dchild->d_inode->i_mode))
1434 /* in nfsv4, we need to treat this case a little
1435 * differently. we don't want to truncate the
1436 * file now; this would be wrong if the OPEN
1437 * fails for some other reason. furthermore,
1438 * if the size is nonzero, we should ignore it
1439 * according to spec!
1441 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1444 iap->ia_valid &= ATTR_SIZE;
1448 case NFS3_CREATE_EXCLUSIVE:
1449 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1450 && dchild->d_inode->i_atime.tv_sec == v_atime
1451 && dchild->d_inode->i_size == 0 )
1454 case NFS3_CREATE_GUARDED:
1457 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1461 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1463 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1469 if (EX_ISSYNC(fhp->fh_export)) {
1470 err = nfserrno(nfsd_sync_dir(dentry));
1471 /* setattr will sync the child (or not) */
1474 nfsd_check_ignore_resizing(iap);
1476 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1477 /* Cram the verifier into atime/mtime */
1478 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1479 | ATTR_MTIME_SET|ATTR_ATIME_SET;
1480 /* XXX someone who knows this better please fix it for nsec */
1481 iap->ia_mtime.tv_sec = v_mtime;
1482 iap->ia_atime.tv_sec = v_atime;
1483 iap->ia_mtime.tv_nsec = 0;
1484 iap->ia_atime.tv_nsec = 0;
1488 err2 = nfsd_create_setattr(rqstp, resfhp, iap);
1492 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1494 * Update the filehandle to get the new inode info.
1497 err = fh_update(resfhp);
1501 if (dchild && !IS_ERR(dchild))
1506 err = nfserrno(host_err);
1509 #endif /* CONFIG_NFSD_V3 */
1512 * Read a symlink. On entry, *lenp must contain the maximum path length that
1513 * fits into the buffer. On return, it contains the true length.
1514 * N.B. After this call fhp needs an fh_put
1517 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1519 struct dentry *dentry;
1520 struct inode *inode;
1525 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1529 dentry = fhp->fh_dentry;
1530 inode = dentry->d_inode;
1533 if (!inode->i_op->readlink)
1536 touch_atime(fhp->fh_export->ex_path.mnt, dentry);
1537 /* N.B. Why does this call need a get_fs()??
1538 * Remove the set_fs and watch the fireworks:-) --okir
1541 oldfs = get_fs(); set_fs(KERNEL_DS);
1542 host_err = inode->i_op->readlink(dentry, buf, *lenp);
1553 err = nfserrno(host_err);
1558 * Create a symlink and look up its inode
1559 * N.B. After this call _both_ fhp and resfhp need an fh_put
1562 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1563 char *fname, int flen,
1564 char *path, int plen,
1565 struct svc_fh *resfhp,
1568 struct dentry *dentry, *dnew;
1576 if (isdotent(fname, flen))
1579 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1583 dentry = fhp->fh_dentry;
1584 dnew = lookup_one_len(fname, dentry, flen);
1585 host_err = PTR_ERR(dnew);
1589 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1593 if (unlikely(path[plen] != 0)) {
1594 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1595 if (path_alloced == NULL)
1598 strncpy(path_alloced, path, plen);
1599 path_alloced[plen] = 0;
1600 host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
1601 kfree(path_alloced);
1604 host_err = vfs_symlink(dentry->d_inode, dnew, path);
1607 if (EX_ISSYNC(fhp->fh_export))
1608 host_err = nfsd_sync_dir(dentry);
1610 err = nfserrno(host_err);
1613 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1615 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1617 if (err==0) err = cerr;
1622 err = nfserrno(host_err);
1628 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1631 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1632 char *name, int len, struct svc_fh *tfhp)
1634 struct dentry *ddir, *dnew, *dold;
1635 struct inode *dirp, *dest;
1639 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1642 err = fh_verify(rqstp, tfhp, -S_IFDIR, NFSD_MAY_NOP);
1650 if (isdotent(name, len))
1653 fh_lock_nested(ffhp, I_MUTEX_PARENT);
1654 ddir = ffhp->fh_dentry;
1655 dirp = ddir->d_inode;
1657 dnew = lookup_one_len(name, ddir, len);
1658 host_err = PTR_ERR(dnew);
1662 dold = tfhp->fh_dentry;
1663 dest = dold->d_inode;
1665 host_err = mnt_want_write(tfhp->fh_export->ex_path.mnt);
1667 err = nfserrno(host_err);
1670 host_err = vfs_link(dold, dirp, dnew);
1672 if (EX_ISSYNC(ffhp->fh_export)) {
1673 err = nfserrno(nfsd_sync_dir(ddir));
1674 write_inode_now(dest, 1);
1678 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1681 err = nfserrno(host_err);
1683 mnt_drop_write(tfhp->fh_export->ex_path.mnt);
1692 err = nfserrno(host_err);
1698 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1701 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1702 struct svc_fh *tfhp, char *tname, int tlen)
1704 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1705 struct inode *fdir, *tdir;
1709 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1712 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1716 fdentry = ffhp->fh_dentry;
1717 fdir = fdentry->d_inode;
1719 tdentry = tfhp->fh_dentry;
1720 tdir = tdentry->d_inode;
1722 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1723 if (ffhp->fh_export != tfhp->fh_export)
1727 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1730 /* cannot use fh_lock as we need deadlock protective ordering
1731 * so do it by hand */
1732 trap = lock_rename(tdentry, fdentry);
1733 ffhp->fh_locked = tfhp->fh_locked = 1;
1737 odentry = lookup_one_len(fname, fdentry, flen);
1738 host_err = PTR_ERR(odentry);
1739 if (IS_ERR(odentry))
1743 if (!odentry->d_inode)
1746 if (odentry == trap)
1749 ndentry = lookup_one_len(tname, tdentry, tlen);
1750 host_err = PTR_ERR(ndentry);
1751 if (IS_ERR(ndentry))
1753 host_err = -ENOTEMPTY;
1754 if (ndentry == trap)
1757 if (svc_msnfs(ffhp) &&
1758 ((atomic_read(&odentry->d_count) > 1)
1759 || (atomic_read(&ndentry->d_count) > 1))) {
1765 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1767 host_err = mnt_want_write(ffhp->fh_export->ex_path.mnt);
1771 host_err = vfs_rename(fdir, odentry, tdir, ndentry);
1772 if (!host_err && EX_ISSYNC(tfhp->fh_export)) {
1773 host_err = nfsd_sync_dir(tdentry);
1775 host_err = nfsd_sync_dir(fdentry);
1778 mnt_drop_write(ffhp->fh_export->ex_path.mnt);
1785 err = nfserrno(host_err);
1787 /* we cannot reply on fh_unlock on the two filehandles,
1788 * as that would do the wrong thing if the two directories
1789 * were the same, so again we do it by hand
1791 fill_post_wcc(ffhp);
1792 fill_post_wcc(tfhp);
1793 unlock_rename(tdentry, fdentry);
1794 ffhp->fh_locked = tfhp->fh_locked = 0;
1801 * Unlink a file or directory
1802 * N.B. After this call fhp needs an fh_put
1805 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1806 char *fname, int flen)
1808 struct dentry *dentry, *rdentry;
1814 if (!flen || isdotent(fname, flen))
1816 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1820 fh_lock_nested(fhp, I_MUTEX_PARENT);
1821 dentry = fhp->fh_dentry;
1822 dirp = dentry->d_inode;
1824 rdentry = lookup_one_len(fname, dentry, flen);
1825 host_err = PTR_ERR(rdentry);
1826 if (IS_ERR(rdentry))
1829 if (!rdentry->d_inode) {
1836 type = rdentry->d_inode->i_mode & S_IFMT;
1838 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1842 if (type != S_IFDIR) { /* It's UNLINK */
1844 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1845 (atomic_read(&rdentry->d_count) > 1)) {
1849 host_err = vfs_unlink(dirp, rdentry);
1850 } else { /* It's RMDIR */
1851 host_err = vfs_rmdir(dirp, rdentry);
1858 if (EX_ISSYNC(fhp->fh_export))
1859 host_err = nfsd_sync_dir(dentry);
1862 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1864 err = nfserrno(host_err);
1870 * We do this buffering because we must not call back into the file
1871 * system's ->lookup() method from the filldir callback. That may well
1872 * deadlock a number of file systems.
1874 * This is based heavily on the implementation of same in XFS.
1876 struct buffered_dirent {
1880 unsigned int d_type;
1884 struct readdir_data {
1890 static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1891 loff_t offset, u64 ino, unsigned int d_type)
1893 struct readdir_data *buf = __buf;
1894 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1895 unsigned int reclen;
1897 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1898 if (buf->used + reclen > PAGE_SIZE) {
1903 de->namlen = namlen;
1904 de->offset = offset;
1906 de->d_type = d_type;
1907 memcpy(de->name, name, namlen);
1908 buf->used += reclen;
1913 static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
1914 struct readdir_cd *cdp, loff_t *offsetp)
1916 struct readdir_data buf;
1917 struct buffered_dirent *de;
1922 buf.dirent = (void *)__get_free_page(GFP_KERNEL);
1924 return nfserrno(-ENOMEM);
1929 struct inode *dir_inode = file->f_path.dentry->d_inode;
1930 unsigned int reclen;
1932 cdp->err = nfserr_eof; /* will be cleared on successful read */
1936 host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
1949 * Various filldir functions may end up calling back into
1950 * lookup_one_len() and the file system's ->lookup() method.
1951 * These expect i_mutex to be held, as it would within readdir.
1953 host_err = mutex_lock_killable(&dir_inode->i_mutex);
1957 de = (struct buffered_dirent *)buf.dirent;
1959 offset = de->offset;
1961 if (func(cdp, de->name, de->namlen, de->offset,
1962 de->ino, de->d_type))
1965 if (cdp->err != nfs_ok)
1968 reclen = ALIGN(sizeof(*de) + de->namlen,
1971 de = (struct buffered_dirent *)((char *)de + reclen);
1973 mutex_unlock(&dir_inode->i_mutex);
1974 if (size > 0) /* We bailed out early */
1977 offset = vfs_llseek(file, 0, SEEK_CUR);
1980 free_page((unsigned long)(buf.dirent));
1983 return nfserrno(host_err);
1990 * Read entries from a directory.
1991 * The NFSv3/4 verifier we ignore for now.
1994 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
1995 struct readdir_cd *cdp, filldir_t func)
1999 loff_t offset = *offsetp;
2001 err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file);
2005 offset = vfs_llseek(file, offset, 0);
2007 err = nfserrno((int)offset);
2011 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
2013 if (err == nfserr_eof || err == nfserr_toosmall)
2014 err = nfs_ok; /* can still be found in ->err */
2022 * Get file system stats
2023 * N.B. After this call fhp needs an fh_put
2026 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
2028 __be32 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
2029 if (!err && vfs_statfs(fhp->fh_dentry,stat))
2034 static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
2036 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
2040 * Check for a user's access permissions to this inode.
2043 nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2044 struct dentry *dentry, int acc)
2046 struct inode *inode = dentry->d_inode;
2050 if (acc == NFSD_MAY_NOP)
2053 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2055 (acc & NFSD_MAY_READ)? " read" : "",
2056 (acc & NFSD_MAY_WRITE)? " write" : "",
2057 (acc & NFSD_MAY_EXEC)? " exec" : "",
2058 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2059 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2060 (acc & NFSD_MAY_LOCK)? " lock" : "",
2061 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2063 IS_IMMUTABLE(inode)? " immut" : "",
2064 IS_APPEND(inode)? " append" : "",
2065 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
2066 dprintk(" owner %d/%d user %d/%d\n",
2067 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2070 /* Normally we reject any write/sattr etc access on a read-only file
2071 * system. But if it is IRIX doing check on write-access for a
2072 * device special file, we ignore rofs.
2074 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2075 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2076 if (exp_rdonly(rqstp, exp) ||
2077 __mnt_is_readonly(exp->ex_path.mnt))
2079 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
2082 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2085 if (acc & NFSD_MAY_LOCK) {
2086 /* If we cannot rely on authentication in NLM requests,
2087 * just allow locks, otherwise require read permission, or
2090 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2093 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2096 * The file owner always gets access permission for accesses that
2097 * would normally be checked at open time. This is to make
2098 * file access work even when the client has done a fchmod(fd, 0).
2100 * However, `cp foo bar' should fail nevertheless when bar is
2101 * readonly. A sensible way to do this might be to reject all
2102 * attempts to truncate a read-only file, because a creat() call
2103 * always implies file truncation.
2104 * ... but this isn't really fair. A process may reasonably call
2105 * ftruncate on an open file descriptor on a file with perm 000.
2106 * We must trust the client to do permission checking - using "ACCESS"
2109 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2110 inode->i_uid == current_fsuid())
2113 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2114 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
2116 /* Allow read access to binaries even when mode 111 */
2117 if (err == -EACCES && S_ISREG(inode->i_mode) &&
2118 acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE))
2119 err = inode_permission(inode, MAY_EXEC);
2123 /* Do integrity (permission) checking now, but defer incrementing
2124 * IMA counts to the actual file open.
2126 path.mnt = exp->ex_path.mnt;
2127 path.dentry = dentry;
2129 return err? nfserrno(err) : 0;
2133 nfsd_racache_shutdown(void)
2135 struct raparms *raparm, *last_raparm;
2138 dprintk("nfsd: freeing readahead buffers.\n");
2140 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2141 raparm = raparm_hash[i].pb_head;
2143 last_raparm = raparm;
2144 raparm = raparm->p_next;
2147 raparm_hash[i].pb_head = NULL;
2151 * Initialize readahead param cache
2154 nfsd_racache_init(int cache_size)
2159 struct raparms **raparm = NULL;
2162 if (raparm_hash[0].pb_head)
2164 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2167 cache_size = nperbucket * RAPARM_HASH_SIZE;
2169 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
2171 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2172 spin_lock_init(&raparm_hash[i].pb_lock);
2174 raparm = &raparm_hash[i].pb_head;
2175 for (j = 0; j < nperbucket; j++) {
2176 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2179 raparm = &(*raparm)->p_next;
2184 nfsdstats.ra_size = cache_size;
2188 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2189 nfsd_racache_shutdown();
2193 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2195 nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2197 struct inode *inode = fhp->fh_dentry->d_inode;
2201 struct posix_acl *acl;
2203 if (!IS_POSIXACL(inode))
2204 return ERR_PTR(-EOPNOTSUPP);
2207 case ACL_TYPE_ACCESS:
2208 name = POSIX_ACL_XATTR_ACCESS;
2210 case ACL_TYPE_DEFAULT:
2211 name = POSIX_ACL_XATTR_DEFAULT;
2214 return ERR_PTR(-EOPNOTSUPP);
2217 size = nfsd_getxattr(fhp->fh_dentry, name, &value);
2219 return ERR_PTR(size);
2221 acl = posix_acl_from_xattr(value, size);
2227 nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2229 struct inode *inode = fhp->fh_dentry->d_inode;
2235 if (!IS_POSIXACL(inode) ||
2236 !inode->i_op->setxattr || !inode->i_op->removexattr)
2239 case ACL_TYPE_ACCESS:
2240 name = POSIX_ACL_XATTR_ACCESS;
2242 case ACL_TYPE_DEFAULT:
2243 name = POSIX_ACL_XATTR_DEFAULT;
2249 if (acl && acl->a_count) {
2250 size = posix_acl_xattr_size(acl->a_count);
2251 value = kmalloc(size, GFP_KERNEL);
2254 error = posix_acl_to_xattr(acl, value, size);
2261 error = mnt_want_write(fhp->fh_export->ex_path.mnt);
2265 error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
2267 if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2270 error = vfs_removexattr(fhp->fh_dentry, name);
2271 if (error == -ENODATA)
2275 mnt_drop_write(fhp->fh_export->ex_path.mnt);
2281 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */