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>
19 #include <linux/config.h>
20 #include <linux/string.h>
21 #include <linux/time.h>
22 #include <linux/errno.h>
24 #include <linux/file.h>
25 #include <linux/mount.h>
26 #include <linux/major.h>
27 #include <linux/ext2_fs.h>
28 #include <linux/proc_fs.h>
29 #include <linux/stat.h>
30 #include <linux/fcntl.h>
31 #include <linux/net.h>
32 #include <linux/unistd.h>
33 #include <linux/slab.h>
34 #include <linux/pagemap.h>
36 #include <linux/module.h>
37 #include <linux/namei.h>
38 #include <linux/vfs.h>
39 #include <linux/delay.h>
40 #include <linux/sunrpc/svc.h>
41 #include <linux/nfsd/nfsd.h>
43 #include <linux/nfs3.h>
44 #include <linux/nfsd/xdr3.h>
45 #endif /* CONFIG_NFSD_V3 */
46 #include <linux/nfsd/nfsfh.h>
47 #include <linux/quotaops.h>
48 #include <linux/dnotify.h>
50 #include <linux/posix_acl.h>
51 #include <linux/posix_acl_xattr.h>
52 #include <linux/xattr_acl.h>
53 #include <linux/xattr.h>
54 #include <linux/nfs4.h>
55 #include <linux/nfs4_acl.h>
56 #include <linux/nfsd_idmap.h>
57 #include <linux/security.h>
58 #endif /* CONFIG_NFSD_V4 */
60 #include <asm/uaccess.h>
62 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
66 /* We must ignore files (but only files) which might have mandatory
67 * locks on them because there is no way to know if the accesser has
70 #define IS_ISMNDLK(i) (S_ISREG((i)->i_mode) && MANDATORY_LOCK(i))
73 * This is a cache of readahead params that help us choose the proper
74 * readahead strategy. Initially, we set all readahead parameters to 0
75 * and let the VFS handle things.
76 * If you increase the number of cached files very much, you'll need to
77 * add a hash table here.
80 struct raparms *p_next;
85 struct file_ra_state p_ra;
88 static struct raparms * raparml;
89 static struct raparms * raparm_cache;
92 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
94 * Returns -EAGAIN leaving *dpp and *expp unchanged,
95 * or nfs_ok having possibly changed *dpp and *expp
98 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
99 struct svc_export **expp)
101 struct svc_export *exp = *expp, *exp2 = NULL;
102 struct dentry *dentry = *dpp;
103 struct vfsmount *mnt = mntget(exp->ex_mnt);
104 struct dentry *mounts = dget(dentry);
107 while (follow_down(&mnt,&mounts)&&d_mountpoint(mounts));
109 exp2 = exp_get_by_name(exp->ex_client, mnt, mounts, &rqstp->rq_chandle);
116 if (exp2 && ((exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2))) {
117 /* successfully crossed mount point */
123 if (exp2) exp_put(exp2);
132 * Look up one component of a pathname.
133 * N.B. After this call _both_ fhp and resfh need an fh_put
135 * If the lookup would cross a mountpoint, and the mounted filesystem
136 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
137 * accepted as it stands and the mounted directory is
138 * returned. Otherwise the covered directory is returned.
139 * NOTE: this mountpoint crossing is not supported properly by all
140 * clients and is explicitly disallowed for NFSv3
141 * NeilBrown <neilb@cse.unsw.edu.au>
144 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
145 int len, struct svc_fh *resfh)
147 struct svc_export *exp;
148 struct dentry *dparent;
149 struct dentry *dentry;
152 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
154 /* Obtain dentry and export. */
155 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_EXEC);
159 dparent = fhp->fh_dentry;
160 exp = fhp->fh_export;
165 /* Lookup the name, but don't follow links */
166 if (isdotent(name, len)) {
168 dentry = dget(dparent);
169 else if (dparent != exp->ex_dentry) {
170 dentry = dget_parent(dparent);
171 } else if (!EX_NOHIDE(exp))
172 dentry = dget(dparent); /* .. == . just like at / */
174 /* checking mountpoint crossing is very different when stepping up */
175 struct svc_export *exp2 = NULL;
177 struct vfsmount *mnt = mntget(exp->ex_mnt);
178 dentry = dget(dparent);
179 while(dentry == mnt->mnt_root && follow_up(&mnt, &dentry))
181 dp = dget_parent(dentry);
185 exp2 = exp_parent(exp->ex_client, mnt, dentry,
195 dentry = dget(dparent);
204 dentry = lookup_one_len(name, dparent, len);
205 err = PTR_ERR(dentry);
209 * check if we have crossed a mount point ...
211 if (d_mountpoint(dentry)) {
212 if ((err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
219 * Note: we compose the file handle now, but as the
220 * dentry may be negative, it may need to be updated.
222 err = fh_compose(resfh, exp, dentry, fhp);
223 if (!err && !dentry->d_inode)
236 * Set various file attributes.
237 * N.B. After this call fhp needs an fh_put
240 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
241 int check_guard, time_t guardtime)
243 struct dentry *dentry;
245 int accmode = MAY_SATTR;
251 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
252 accmode |= MAY_WRITE|MAY_OWNER_OVERRIDE;
253 if (iap->ia_valid & ATTR_SIZE)
257 err = fh_verify(rqstp, fhp, ftype, accmode);
258 if (err || !iap->ia_valid)
261 dentry = fhp->fh_dentry;
262 inode = dentry->d_inode;
264 /* NFSv2 does not differentiate between "set-[ac]time-to-now"
265 * which only requires access, and "set-[ac]time-to-X" which
266 * requires ownership.
267 * So if it looks like it might be "set both to the same time which
268 * is close to now", and if inode_change_ok fails, then we
269 * convert to "set to now" instead of "set to explicit time"
271 * We only call inode_change_ok as the last test as technically
272 * it is not an interface that we should be using. It is only
273 * valid if the filesystem does not define it's own i_op->setattr.
275 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
276 #define MAX_TOUCH_TIME_ERROR (30*60)
277 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET
278 && iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec
280 /* Looks probable. Now just make sure time is in the right ballpark.
281 * Solaris, at least, doesn't seem to care what the time request is.
282 * We require it be within 30 minutes of now.
284 time_t delta = iap->ia_atime.tv_sec - get_seconds();
285 if (delta<0) delta = -delta;
286 if (delta < MAX_TOUCH_TIME_ERROR &&
287 inode_change_ok(inode, iap) != 0) {
288 /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
289 * this will cause notify_change to set these times to "now"
291 iap->ia_valid &= ~BOTH_TIME_SET;
295 /* The size case is special. It changes the file as well as the attributes. */
296 if (iap->ia_valid & ATTR_SIZE) {
297 if (iap->ia_size < inode->i_size) {
298 err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
304 * If we are changing the size of the file, then
305 * we need to break all leases.
307 err = break_lease(inode, FMODE_WRITE | O_NONBLOCK);
308 if (err == -EWOULDBLOCK)
310 if (err) /* ENOMEM or EWOULDBLOCK */
313 err = get_write_access(inode);
318 err = locks_verify_truncate(inode, NULL, iap->ia_size);
320 put_write_access(inode);
326 imode = inode->i_mode;
327 if (iap->ia_valid & ATTR_MODE) {
328 iap->ia_mode &= S_IALLUGO;
329 imode = iap->ia_mode |= (imode & ~S_IALLUGO);
332 /* Revoke setuid/setgid bit on chown/chgrp */
333 if ((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid)
334 iap->ia_valid |= ATTR_KILL_SUID;
335 if ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)
336 iap->ia_valid |= ATTR_KILL_SGID;
338 /* Change the attributes. */
340 iap->ia_valid |= ATTR_CTIME;
342 err = nfserr_notsync;
343 if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
345 err = notify_change(dentry, iap);
350 put_write_access(inode);
352 if (EX_ISSYNC(fhp->fh_export))
353 write_inode_now(inode, 1);
362 #if defined(CONFIG_NFSD_V4)
365 set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
371 struct inode *inode = dentry->d_inode;
373 buflen = posix_acl_xattr_size(pacl->a_count);
374 buf = kmalloc(buflen, GFP_KERNEL);
379 len = posix_acl_to_xattr(pacl, buf, buflen);
386 if (inode->i_op && inode->i_op->setxattr) {
388 security_inode_setxattr(dentry, key, buf, len, 0);
389 error = inode->i_op->setxattr(dentry, key, buf, len, 0);
391 security_inode_post_setxattr(dentry, key, buf, len, 0);
400 nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
401 struct nfs4_acl *acl)
404 struct dentry *dentry;
406 struct posix_acl *pacl = NULL, *dpacl = NULL;
407 unsigned int flags = 0;
410 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, MAY_SATTR);
414 dentry = fhp->fh_dentry;
415 inode = dentry->d_inode;
416 if (S_ISDIR(inode->i_mode))
417 flags = NFS4_ACL_DIR;
419 error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
420 if (error == -EINVAL) {
421 error = nfserr_attrnotsupp;
423 } else if (error < 0)
427 error = set_nfsv4_acl_one(dentry, pacl, XATTR_NAME_ACL_ACCESS);
433 error = set_nfsv4_acl_one(dentry, dpacl, XATTR_NAME_ACL_DEFAULT);
441 posix_acl_release(pacl);
442 posix_acl_release(dpacl);
445 error = nfserrno(error);
449 static struct posix_acl *
450 _get_posix_acl(struct dentry *dentry, char *key)
452 struct inode *inode = dentry->d_inode;
454 int buflen, error = 0;
455 struct posix_acl *pacl = NULL;
458 if (inode->i_op == NULL)
460 if (inode->i_op->getxattr == NULL)
463 error = security_inode_getxattr(dentry, key);
468 if (inode->i_op == NULL)
470 if (inode->i_op->getxattr == NULL)
473 error = security_inode_getxattr(dentry, key);
477 buflen = inode->i_op->getxattr(dentry, key, NULL, 0);
479 error = buflen < 0 ? buflen : -ENODATA;
483 buf = kmalloc(buflen, GFP_KERNEL);
489 error = inode->i_op->getxattr(dentry, key, buf, buflen);
493 pacl = posix_acl_from_xattr(buf, buflen);
498 pacl = ERR_PTR(error);
503 nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
505 struct inode *inode = dentry->d_inode;
507 struct posix_acl *pacl = NULL, *dpacl = NULL;
508 unsigned int flags = 0;
510 pacl = _get_posix_acl(dentry, XATTR_NAME_ACL_ACCESS);
511 if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
512 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
514 error = PTR_ERR(pacl);
519 if (S_ISDIR(inode->i_mode)) {
520 dpacl = _get_posix_acl(dentry, XATTR_NAME_ACL_DEFAULT);
521 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
523 else if (IS_ERR(dpacl)) {
524 error = PTR_ERR(dpacl);
528 flags = NFS4_ACL_DIR;
531 *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
533 error = PTR_ERR(*acl);
537 posix_acl_release(pacl);
538 posix_acl_release(dpacl);
542 #endif /* defined(CONFIG_NFS_V4) */
544 #ifdef CONFIG_NFSD_V3
546 * Check server access rights to a file system object
552 static struct accessmap nfs3_regaccess[] = {
553 { NFS3_ACCESS_READ, MAY_READ },
554 { NFS3_ACCESS_EXECUTE, MAY_EXEC },
555 { NFS3_ACCESS_MODIFY, MAY_WRITE|MAY_TRUNC },
556 { NFS3_ACCESS_EXTEND, MAY_WRITE },
561 static struct accessmap nfs3_diraccess[] = {
562 { NFS3_ACCESS_READ, MAY_READ },
563 { NFS3_ACCESS_LOOKUP, MAY_EXEC },
564 { NFS3_ACCESS_MODIFY, MAY_EXEC|MAY_WRITE|MAY_TRUNC },
565 { NFS3_ACCESS_EXTEND, MAY_EXEC|MAY_WRITE },
566 { NFS3_ACCESS_DELETE, MAY_REMOVE },
571 static struct accessmap nfs3_anyaccess[] = {
572 /* Some clients - Solaris 2.6 at least, make an access call
573 * to the server to check for access for things like /dev/null
574 * (which really, the server doesn't care about). So
575 * We provide simple access checking for them, looking
576 * mainly at mode bits, and we make sure to ignore read-only
579 { NFS3_ACCESS_READ, MAY_READ },
580 { NFS3_ACCESS_EXECUTE, MAY_EXEC },
581 { NFS3_ACCESS_MODIFY, MAY_WRITE|MAY_LOCAL_ACCESS },
582 { NFS3_ACCESS_EXTEND, MAY_WRITE|MAY_LOCAL_ACCESS },
588 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
590 struct accessmap *map;
591 struct svc_export *export;
592 struct dentry *dentry;
593 u32 query, result = 0, sresult = 0;
596 error = fh_verify(rqstp, fhp, 0, MAY_NOP);
600 export = fhp->fh_export;
601 dentry = fhp->fh_dentry;
603 if (S_ISREG(dentry->d_inode->i_mode))
604 map = nfs3_regaccess;
605 else if (S_ISDIR(dentry->d_inode->i_mode))
606 map = nfs3_diraccess;
608 map = nfs3_anyaccess;
612 for (; map->access; map++) {
613 if (map->access & query) {
616 sresult |= map->access;
618 err2 = nfsd_permission(export, dentry, map->how);
621 result |= map->access;
624 /* the following error codes just mean the access was not allowed,
625 * rather than an error occurred */
629 /* simply don't "or" in the access bit. */
639 *supported = sresult;
644 #endif /* CONFIG_NFSD_V3 */
649 * Open an existing file or directory.
650 * The access argument indicates the type of open (read/write/lock)
651 * N.B. After this call fhp needs an fh_put
654 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
655 int access, struct file **filp)
657 struct dentry *dentry;
659 int flags = O_RDONLY|O_LARGEFILE, err;
662 * If we get here, then the client has already done an "open",
663 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
664 * in case a chmod has now revoked permission.
666 err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE);
670 dentry = fhp->fh_dentry;
671 inode = dentry->d_inode;
673 /* Disallow write access to files with the append-only bit set
674 * or any access when mandatory locking enabled
677 if (IS_APPEND(inode) && (access & MAY_WRITE))
679 if (IS_ISMNDLK(inode))
686 * Check to see if there are any leases on this file.
687 * This may block while leases are broken.
689 err = break_lease(inode, O_NONBLOCK | ((access & MAY_WRITE) ? FMODE_WRITE : 0));
690 if (err == -EWOULDBLOCK)
692 if (err) /* NOMEM or WOULDBLOCK */
695 if (access & MAY_WRITE) {
696 flags = O_WRONLY|O_LARGEFILE;
700 *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_mnt), flags);
702 err = PTR_ERR(*filp);
714 nfsd_close(struct file *filp)
721 * As this calls fsync (not fdatasync) there is no need for a write_inode
724 static inline void nfsd_dosync(struct file *filp, struct dentry *dp,
725 struct file_operations *fop)
727 struct inode *inode = dp->d_inode;
728 int (*fsync) (struct file *, struct dentry *, int);
730 filemap_fdatawrite(inode->i_mapping);
731 if (fop && (fsync = fop->fsync))
733 filemap_fdatawait(inode->i_mapping);
738 nfsd_sync(struct file *filp)
740 struct inode *inode = filp->f_dentry->d_inode;
741 dprintk("nfsd: sync file %s\n", filp->f_dentry->d_name.name);
743 nfsd_dosync(filp, filp->f_dentry, filp->f_op);
748 nfsd_sync_dir(struct dentry *dp)
750 nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
754 * Obtain the readahead parameters for the file
755 * specified by (dev, ino).
757 static DEFINE_SPINLOCK(ra_lock);
759 static inline struct raparms *
760 nfsd_get_raparms(dev_t dev, ino_t ino)
762 struct raparms *ra, **rap, **frap = NULL;
766 for (rap = &raparm_cache; (ra = *rap); rap = &ra->p_next) {
767 if (ra->p_ino == ino && ra->p_dev == dev)
770 if (ra->p_count == 0)
773 depth = nfsdstats.ra_size*11/10;
775 spin_unlock(&ra_lock);
784 if (rap != &raparm_cache) {
786 ra->p_next = raparm_cache;
790 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
791 spin_unlock(&ra_lock);
796 * Grab and keep cached pages assosiated with a file in the svc_rqst
797 * so that they can be passed to the netowork sendmsg/sendpage routines
798 * directrly. They will be released after the sending has completed.
801 nfsd_read_actor(read_descriptor_t *desc, struct page *page, unsigned long offset , unsigned long size)
803 unsigned long count = desc->count;
804 struct svc_rqst *rqstp = desc->arg.data;
809 if (rqstp->rq_res.page_len == 0) {
811 rqstp->rq_respages[rqstp->rq_resused++] = page;
812 rqstp->rq_res.page_base = offset;
813 rqstp->rq_res.page_len = size;
814 } else if (page != rqstp->rq_respages[rqstp->rq_resused-1]) {
816 rqstp->rq_respages[rqstp->rq_resused++] = page;
817 rqstp->rq_res.page_len += size;
819 rqstp->rq_res.page_len += size;
822 desc->count = count - size;
823 desc->written += size;
828 nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
829 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
837 inode = file->f_dentry->d_inode;
839 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
840 (!lock_may_read(inode, offset, *count)))
844 /* Get readahead parameters */
845 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
848 file->f_ra = ra->p_ra;
850 if (file->f_op->sendfile) {
851 svc_pushback_unused_pages(rqstp);
852 err = file->f_op->sendfile(file, &offset, *count,
853 nfsd_read_actor, rqstp);
857 err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
861 /* Write back readahead params */
864 ra->p_ra = file->f_ra;
867 spin_unlock(&ra_lock);
871 nfsdstats.io_read += err;
874 dnotify_parent(file->f_dentry, DN_ACCESS);
882 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
883 loff_t offset, struct kvec *vec, int vlen,
884 unsigned long cnt, int *stablep)
886 struct svc_export *exp;
887 struct dentry *dentry;
891 int stable = *stablep;
896 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
897 (!lock_may_write(file->f_dentry->d_inode, offset, cnt)))
901 dentry = file->f_dentry;
902 inode = dentry->d_inode;
903 exp = fhp->fh_export;
906 * Request sync writes if
907 * - the sync export option has been set, or
908 * - the client requested O_SYNC behavior (NFSv3 feature).
909 * - The file system doesn't support fsync().
910 * When gathered writes have been configured for this volume,
911 * flushing the data to disk is handled separately below.
914 if (file->f_op->fsync == 0) {/* COMMIT3 cannot work */
916 *stablep = 2; /* FILE_SYNC */
921 if (stable && !EX_WGATHER(exp))
922 file->f_flags |= O_SYNC;
924 /* Write the data. */
925 oldfs = get_fs(); set_fs(KERNEL_DS);
926 err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
929 nfsdstats.io_write += cnt;
930 dnotify_parent(file->f_dentry, DN_MODIFY);
933 /* clear setuid/setgid flag after write */
934 if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) {
936 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
939 notify_change(dentry, &ia);
943 if (err >= 0 && stable) {
944 static ino_t last_ino;
945 static dev_t last_dev;
948 * Gathered writes: If another process is currently
949 * writing to the file, there's a high chance
950 * this is another nfsd (triggered by a bulk write
951 * from a client's biod). Rather than syncing the
952 * file with each write request, we sleep for 10 msec.
954 * I don't know if this roughly approximates
955 * C. Juszak's idea of gathered writes, but it's a
956 * nice and simple solution (IMHO), and it seems to
959 if (EX_WGATHER(exp)) {
960 if (atomic_read(&inode->i_writecount) > 1
961 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
962 dprintk("nfsd: write defer %d\n", current->pid);
964 dprintk("nfsd: write resume %d\n", current->pid);
967 if (inode->i_state & I_DIRTY) {
968 dprintk("nfsd: write sync %d\n", current->pid);
972 wake_up(&inode->i_wait);
975 last_ino = inode->i_ino;
976 last_dev = inode->i_sb->s_dev;
979 dprintk("nfsd: write complete err=%d\n", err);
989 * Read data from a file. count must contain the requested read count
990 * on entry. On return, *count contains the number of bytes actually read.
991 * N.B. After this call fhp needs an fh_put
994 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
995 loff_t offset, struct kvec *vec, int vlen,
996 unsigned long *count)
1001 err = nfsd_permission(fhp->fh_export, fhp->fh_dentry,
1002 MAY_READ|MAY_OWNER_OVERRIDE);
1005 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1007 err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file);
1010 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1018 * Write data to a file.
1019 * The stable flag requests synchronous writes.
1020 * N.B. After this call fhp needs an fh_put
1023 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1024 loff_t offset, struct kvec *vec, int vlen, unsigned long cnt,
1030 err = nfsd_permission(fhp->fh_export, fhp->fh_dentry,
1031 MAY_WRITE|MAY_OWNER_OVERRIDE);
1034 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1037 err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
1042 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1050 #ifdef CONFIG_NFSD_V3
1052 * Commit all pending writes to stable storage.
1053 * Strictly speaking, we could sync just the indicated file region here,
1054 * but there's currently no way we can ask the VFS to do so.
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.
1060 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1061 loff_t offset, unsigned long count)
1066 if ((u64)count > ~(u64)offset)
1067 return nfserr_inval;
1069 if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
1071 if (EX_ISSYNC(fhp->fh_export)) {
1072 if (file->f_op && file->f_op->fsync) {
1075 err = nfserr_notsupp;
1082 #endif /* CONFIG_NFSD_V3 */
1085 * Create a file (regular, directory, device, fifo); UNIX sockets
1086 * not yet implemented.
1087 * If the response fh has been verified, the parent directory should
1088 * already be locked. Note that the parent directory is left locked.
1090 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1093 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1094 char *fname, int flen, struct iattr *iap,
1095 int type, dev_t rdev, struct svc_fh *resfhp)
1097 struct dentry *dentry, *dchild = NULL;
1105 if (isdotent(fname, flen))
1108 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1112 dentry = fhp->fh_dentry;
1113 dirp = dentry->d_inode;
1115 err = nfserr_notdir;
1116 if(!dirp->i_op || !dirp->i_op->lookup)
1119 * Check whether the response file handle has been verified yet.
1120 * If it has, the parent directory should already be locked.
1122 if (!resfhp->fh_dentry) {
1123 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
1125 dchild = lookup_one_len(fname, dentry, flen);
1126 err = PTR_ERR(dchild);
1129 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1133 /* called from nfsd_proc_create */
1134 dchild = dget(resfhp->fh_dentry);
1135 if (!fhp->fh_locked) {
1136 /* not actually possible */
1138 "nfsd_create: parent %s/%s not locked!\n",
1139 dentry->d_parent->d_name.name,
1140 dentry->d_name.name);
1146 * Make sure the child dentry is still negative ...
1149 if (dchild->d_inode) {
1150 dprintk("nfsd_create: dentry %s/%s not negative!\n",
1151 dentry->d_name.name, dchild->d_name.name);
1155 if (!(iap->ia_valid & ATTR_MODE))
1157 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1160 * Get the dir op function pointer.
1165 err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1168 err = vfs_mkdir(dirp, dchild, iap->ia_mode);
1174 err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
1177 printk("nfsd: bad file type %o in nfsd_create\n", type);
1183 if (EX_ISSYNC(fhp->fh_export)) {
1184 nfsd_sync_dir(dentry);
1185 write_inode_now(dchild->d_inode, 1);
1189 /* Set file attributes. Mode has already been set and
1190 * setting uid/gid works only for root. Irix appears to
1191 * send along the gid when it tries to implement setgid
1192 * directories via NFS.
1195 if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0)
1196 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1198 * Update the file handle to get the new inode info.
1201 err = fh_update(resfhp);
1203 if (dchild && !IS_ERR(dchild))
1208 err = nfserrno(err);
1212 #ifdef CONFIG_NFSD_V3
1214 * NFSv3 version of nfsd_create
1217 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
1218 char *fname, int flen, struct iattr *iap,
1219 struct svc_fh *resfhp, int createmode, u32 *verifier,
1222 struct dentry *dentry, *dchild = NULL;
1225 __u32 v_mtime=0, v_atime=0;
1232 if (isdotent(fname, flen))
1234 if (!(iap->ia_valid & ATTR_MODE))
1236 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1240 dentry = fhp->fh_dentry;
1241 dirp = dentry->d_inode;
1243 /* Get all the sanity checks out of the way before
1244 * we lock the parent. */
1245 err = nfserr_notdir;
1246 if(!dirp->i_op || !dirp->i_op->lookup)
1251 * Compose the response file handle.
1253 dchild = lookup_one_len(fname, dentry, flen);
1254 err = PTR_ERR(dchild);
1258 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1262 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1263 /* while the verifier would fit in mtime+atime,
1264 * solaris7 gets confused (bugid 4218508) if these have
1265 * the high bit set, so we use the mode as well
1267 v_mtime = verifier[0]&0x7fffffff;
1268 v_atime = verifier[1]&0x7fffffff;
1270 | ((verifier[0]&0x80000000) >> (32-7)) /* u+x */
1271 | ((verifier[1]&0x80000000) >> (32-9)) /* u+r */
1275 if (dchild->d_inode) {
1278 switch (createmode) {
1279 case NFS3_CREATE_UNCHECKED:
1280 if (! S_ISREG(dchild->d_inode->i_mode))
1283 /* in nfsv4, we need to treat this case a little
1284 * differently. we don't want to truncate the
1285 * file now; this would be wrong if the OPEN
1286 * fails for some other reason. furthermore,
1287 * if the size is nonzero, we should ignore it
1288 * according to spec!
1290 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1293 iap->ia_valid &= ATTR_SIZE;
1297 case NFS3_CREATE_EXCLUSIVE:
1298 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1299 && dchild->d_inode->i_atime.tv_sec == v_atime
1300 && dchild->d_inode->i_mode == v_mode
1301 && dchild->d_inode->i_size == 0 )
1304 case NFS3_CREATE_GUARDED:
1310 err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1314 if (EX_ISSYNC(fhp->fh_export)) {
1315 nfsd_sync_dir(dentry);
1316 /* setattr will sync the child (or not) */
1320 * Update the filehandle to get the new inode info.
1322 err = fh_update(resfhp);
1326 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1327 /* Cram the verifier into atime/mtime/mode */
1328 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1329 | ATTR_MTIME_SET|ATTR_ATIME_SET
1331 /* XXX someone who knows this better please fix it for nsec */
1332 iap->ia_mtime.tv_sec = v_mtime;
1333 iap->ia_atime.tv_sec = v_atime;
1334 iap->ia_mtime.tv_nsec = 0;
1335 iap->ia_atime.tv_nsec = 0;
1336 iap->ia_mode = v_mode;
1339 /* Set file attributes.
1340 * Mode has already been set but we might need to reset it
1341 * for CREATE_EXCLUSIVE
1342 * Irix appears to send along the gid when it tries to
1343 * implement setgid directories via NFS. Clear out all that cruft.
1346 if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0)
1347 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1351 if (dchild && !IS_ERR(dchild))
1356 err = nfserrno(err);
1359 #endif /* CONFIG_NFSD_V3 */
1362 * Read a symlink. On entry, *lenp must contain the maximum path length that
1363 * fits into the buffer. On return, it contains the true length.
1364 * N.B. After this call fhp needs an fh_put
1367 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1369 struct dentry *dentry;
1370 struct inode *inode;
1374 err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
1378 dentry = fhp->fh_dentry;
1379 inode = dentry->d_inode;
1382 if (!inode->i_op || !inode->i_op->readlink)
1385 touch_atime(fhp->fh_export->ex_mnt, dentry);
1386 /* N.B. Why does this call need a get_fs()??
1387 * Remove the set_fs and watch the fireworks:-) --okir
1390 oldfs = get_fs(); set_fs(KERNEL_DS);
1391 err = inode->i_op->readlink(dentry, buf, *lenp);
1402 err = nfserrno(err);
1407 * Create a symlink and look up its inode
1408 * N.B. After this call _both_ fhp and resfhp need an fh_put
1411 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1412 char *fname, int flen,
1413 char *path, int plen,
1414 struct svc_fh *resfhp,
1417 struct dentry *dentry, *dnew;
1425 if (isdotent(fname, flen))
1428 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1432 dentry = fhp->fh_dentry;
1433 dnew = lookup_one_len(fname, dentry, flen);
1434 err = PTR_ERR(dnew);
1439 /* Only the MODE ATTRibute is even vaguely meaningful */
1440 if (iap && (iap->ia_valid & ATTR_MODE))
1441 mode = iap->ia_mode & S_IALLUGO;
1443 if (unlikely(path[plen] != 0)) {
1444 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1445 if (path_alloced == NULL)
1448 strncpy(path_alloced, path, plen);
1449 path_alloced[plen] = 0;
1450 err = vfs_symlink(dentry->d_inode, dnew, path_alloced, mode);
1451 kfree(path_alloced);
1454 err = vfs_symlink(dentry->d_inode, dnew, path, mode);
1457 if (EX_ISSYNC(fhp->fh_export))
1458 nfsd_sync_dir(dentry);
1460 err = nfserrno(err);
1463 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1465 if (err==0) err = cerr;
1470 err = nfserrno(err);
1476 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1479 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1480 char *name, int len, struct svc_fh *tfhp)
1482 struct dentry *ddir, *dnew, *dold;
1483 struct inode *dirp, *dest;
1486 err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
1489 err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
1497 if (isdotent(name, len))
1501 ddir = ffhp->fh_dentry;
1502 dirp = ddir->d_inode;
1504 dnew = lookup_one_len(name, ddir, len);
1505 err = PTR_ERR(dnew);
1509 dold = tfhp->fh_dentry;
1510 dest = dold->d_inode;
1512 err = vfs_link(dold, dirp, dnew);
1514 if (EX_ISSYNC(ffhp->fh_export)) {
1515 nfsd_sync_dir(ddir);
1516 write_inode_now(dest, 1);
1519 if (err == -EXDEV && rqstp->rq_vers == 2)
1522 err = nfserrno(err);
1531 err = nfserrno(err);
1537 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1540 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1541 struct svc_fh *tfhp, char *tname, int tlen)
1543 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1544 struct inode *fdir, *tdir;
1547 err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
1550 err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
1554 fdentry = ffhp->fh_dentry;
1555 fdir = fdentry->d_inode;
1557 tdentry = tfhp->fh_dentry;
1558 tdir = tdentry->d_inode;
1560 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1561 if (fdir->i_sb != tdir->i_sb)
1565 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1568 /* cannot use fh_lock as we need deadlock protective ordering
1569 * so do it by hand */
1570 trap = lock_rename(tdentry, fdentry);
1571 ffhp->fh_locked = tfhp->fh_locked = 1;
1575 odentry = lookup_one_len(fname, fdentry, flen);
1576 err = PTR_ERR(odentry);
1577 if (IS_ERR(odentry))
1581 if (!odentry->d_inode)
1584 if (odentry == trap)
1587 ndentry = lookup_one_len(tname, tdentry, tlen);
1588 err = PTR_ERR(ndentry);
1589 if (IS_ERR(ndentry))
1592 if (ndentry == trap)
1596 if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1597 ((atomic_read(&odentry->d_count) > 1)
1598 || (atomic_read(&ndentry->d_count) > 1))) {
1602 err = vfs_rename(fdir, odentry, tdir, ndentry);
1603 if (!err && EX_ISSYNC(tfhp->fh_export)) {
1604 nfsd_sync_dir(tdentry);
1605 nfsd_sync_dir(fdentry);
1614 err = nfserrno(err);
1616 /* we cannot reply on fh_unlock on the two filehandles,
1617 * as that would do the wrong thing if the two directories
1618 * were the same, so again we do it by hand
1620 fill_post_wcc(ffhp);
1621 fill_post_wcc(tfhp);
1622 unlock_rename(tdentry, fdentry);
1623 ffhp->fh_locked = tfhp->fh_locked = 0;
1630 * Unlink a file or directory
1631 * N.B. After this call fhp needs an fh_put
1634 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1635 char *fname, int flen)
1637 struct dentry *dentry, *rdentry;
1642 if (!flen || isdotent(fname, flen))
1644 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
1649 dentry = fhp->fh_dentry;
1650 dirp = dentry->d_inode;
1652 rdentry = lookup_one_len(fname, dentry, flen);
1653 err = PTR_ERR(rdentry);
1654 if (IS_ERR(rdentry))
1657 if (!rdentry->d_inode) {
1664 type = rdentry->d_inode->i_mode & S_IFMT;
1666 if (type != S_IFDIR) { /* It's UNLINK */
1668 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1669 (atomic_read(&rdentry->d_count) > 1)) {
1673 err = vfs_unlink(dirp, rdentry);
1674 } else { /* It's RMDIR */
1675 err = vfs_rmdir(dirp, rdentry);
1682 if (EX_ISSYNC(fhp->fh_export))
1683 nfsd_sync_dir(dentry);
1689 err = nfserrno(err);
1694 * Read entries from a directory.
1695 * The NFSv3/4 verifier we ignore for now.
1698 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
1699 struct readdir_cd *cdp, encode_dent_fn func)
1703 loff_t offset = *offsetp;
1705 err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
1709 offset = vfs_llseek(file, offset, 0);
1711 err = nfserrno((int)offset);
1716 * Read the directory entries. This silly loop is necessary because
1717 * readdir() is not guaranteed to fill up the entire buffer, but
1718 * may choose to do less.
1722 cdp->err = nfserr_eof; /* will be cleared on successful read */
1723 err = vfs_readdir(file, (filldir_t) func, cdp);
1724 } while (err >=0 && cdp->err == nfs_ok);
1726 err = nfserrno(err);
1729 *offsetp = vfs_llseek(file, 0, 1);
1731 if (err == nfserr_eof || err == nfserr_toosmall)
1732 err = nfs_ok; /* can still be found in ->err */
1740 * Get file system stats
1741 * N.B. After this call fhp needs an fh_put
1744 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat)
1746 int err = fh_verify(rqstp, fhp, 0, MAY_NOP);
1747 if (!err && vfs_statfs(fhp->fh_dentry->d_inode->i_sb,stat))
1753 * Check for a user's access permissions to this inode.
1756 nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
1758 struct inode *inode = dentry->d_inode;
1764 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1766 (acc & MAY_READ)? " read" : "",
1767 (acc & MAY_WRITE)? " write" : "",
1768 (acc & MAY_EXEC)? " exec" : "",
1769 (acc & MAY_SATTR)? " sattr" : "",
1770 (acc & MAY_TRUNC)? " trunc" : "",
1771 (acc & MAY_LOCK)? " lock" : "",
1772 (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
1774 IS_IMMUTABLE(inode)? " immut" : "",
1775 IS_APPEND(inode)? " append" : "",
1776 IS_RDONLY(inode)? " ro" : "");
1777 dprintk(" owner %d/%d user %d/%d\n",
1778 inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
1781 /* Normally we reject any write/sattr etc access on a read-only file
1782 * system. But if it is IRIX doing check on write-access for a
1783 * device special file, we ignore rofs.
1785 if (!(acc & MAY_LOCAL_ACCESS))
1786 if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
1787 if (EX_RDONLY(exp) || IS_RDONLY(inode)
1788 || (exp && MNT_IS_RDONLY(exp->ex_mnt)))
1790 if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
1793 if ((acc & MAY_TRUNC) && IS_APPEND(inode))
1796 if (acc & MAY_LOCK) {
1797 /* If we cannot rely on authentication in NLM requests,
1798 * just allow locks, otherwise require read permission, or
1801 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1804 acc = MAY_READ | MAY_OWNER_OVERRIDE;
1807 * The file owner always gets access permission for accesses that
1808 * would normally be checked at open time. This is to make
1809 * file access work even when the client has done a fchmod(fd, 0).
1811 * However, `cp foo bar' should fail nevertheless when bar is
1812 * readonly. A sensible way to do this might be to reject all
1813 * attempts to truncate a read-only file, because a creat() call
1814 * always implies file truncation.
1815 * ... but this isn't really fair. A process may reasonably call
1816 * ftruncate on an open file descriptor on a file with perm 000.
1817 * We must trust the client to do permission checking - using "ACCESS"
1820 if ((acc & MAY_OWNER_OVERRIDE) &&
1821 inode->i_uid == current->fsuid)
1824 err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC), NULL);
1826 /* Allow read access to binaries even when mode 111 */
1827 if (err == -EACCES && S_ISREG(inode->i_mode) &&
1828 acc == (MAY_READ | MAY_OWNER_OVERRIDE))
1829 err = permission(inode, MAY_EXEC, NULL);
1831 return err? nfserrno(err) : 0;
1835 nfsd_racache_shutdown(void)
1839 dprintk("nfsd: freeing readahead buffers.\n");
1841 raparm_cache = raparml = NULL;
1844 * Initialize readahead param cache
1847 nfsd_racache_init(int cache_size)
1853 raparml = kmalloc(sizeof(struct raparms) * cache_size, GFP_KERNEL);
1855 if (raparml != NULL) {
1856 dprintk("nfsd: allocating %d readahead buffers.\n",
1858 memset(raparml, 0, sizeof(struct raparms) * cache_size);
1859 for (i = 0; i < cache_size - 1; i++) {
1860 raparml[i].p_next = raparml + i + 1;
1862 raparm_cache = raparml;
1865 "nfsd: Could not allocate memory read-ahead cache.\n");
1868 nfsdstats.ra_size = cache_size;