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 in ksyms.c, 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>
18 #include <linux/config.h>
19 #include <linux/version.h>
20 #include <linux/string.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/locks.h>
25 #include <linux/major.h>
26 #include <linux/ext2_fs.h>
27 #include <linux/proc_fs.h>
28 #include <linux/stat.h>
29 #include <linux/fcntl.h>
30 #include <linux/net.h>
31 #include <linux/unistd.h>
32 #include <linux/slab.h>
34 #define __NO_VERSION__
35 #include <linux/module.h>
37 #include <linux/sunrpc/svc.h>
38 #include <linux/nfsd/nfsd.h>
40 #include <linux/nfs3.h>
41 #include <linux/nfsd/xdr3.h>
42 #endif /* CONFIG_NFSD_V3 */
43 #include <linux/nfsd/nfsfh.h>
44 #include <linux/quotaops.h>
46 #include <asm/uaccess.h>
48 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
52 /* We must ignore files (but only files) which might have mandatory
53 * locks on them because there is no way to know if the accesser has
56 #define IS_ISMNDLK(i) (S_ISREG((i)->i_mode) && MANDATORY_LOCK(i))
59 * This is a cache of readahead params that help us choose the proper
60 * readahead strategy. Initially, we set all readahead parameters to 0
61 * and let the VFS handle things.
62 * If you increase the number of cached files very much, you'll need to
63 * add a hash table here.
66 struct raparms *p_next;
70 unsigned long p_reada,
77 static struct raparms * raparml;
78 static struct raparms * raparm_cache;
81 * Look up one component of a pathname.
82 * N.B. After this call _both_ fhp and resfh need an fh_put
84 * If the lookup would cross a mountpoint, and the mounted filesystem
85 * is exported to the client with NFSEXP_CROSSMNT, then the lookup is
86 * accepted as it stands and the mounted directory is
87 * returned. Otherwise the covered directory is returned.
88 * NOTE: this mountpoint crossing is not supported properly by all
89 * clients and is explicitly disallowed for NFSv3
90 * NeilBrown <neilb@cse.unsw.edu.au>
93 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
94 int len, struct svc_fh *resfh)
96 struct svc_export *exp;
97 struct dentry *dparent;
98 struct dentry *dentry;
101 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
103 /* Obtain dentry and export. */
104 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_EXEC);
108 dparent = fhp->fh_dentry;
109 exp = fhp->fh_export;
113 /* Lookup the name, but don't follow links */
114 if (isdotent(name, len)) {
116 dentry = dget(dparent);
117 else { /* must be ".." */
118 /* checking mountpoint crossing is very different when stepping up */
119 if (dparent == exp->ex_dentry) {
120 if (!EX_CROSSMNT(exp))
121 dentry = dget(dparent); /* .. == . just like at / */
124 struct svc_export *exp2 = NULL;
126 struct vfsmount *mnt = mntget(exp->ex_mnt);
127 dentry = dget(dparent);
128 while(follow_up(&mnt, &dentry))
130 dp = dget(dentry->d_parent);
133 for ( ; exp2 == NULL && dp->d_parent != dp;
135 exp2 = exp_get(exp->ex_client, dp->d_inode->i_dev, dp->d_inode->i_ino);
138 dentry = dget(dparent);
145 dentry = dget(dparent->d_parent);
149 dentry = lookup_one_len(name, dparent, len);
150 err = PTR_ERR(dentry);
154 * check if we have crossed a mount point ...
156 if (d_mountpoint(dentry)) {
157 struct svc_export *exp2 = NULL;
158 struct vfsmount *mnt = mntget(exp->ex_mnt);
159 struct dentry *mounts = dget(dentry);
160 while (follow_down(&mnt,&mounts)&&d_mountpoint(mounts))
162 exp2 = exp_get(rqstp->rq_client,
163 mounts->d_inode->i_dev,
164 mounts->d_inode->i_ino);
165 if (exp2 && EX_CROSSMNT(exp2)) {
166 /* successfully crossed mount point */
176 * Note: we compose the file handle now, but as the
177 * dentry may be negative, it may need to be updated.
179 err = fh_compose(resfh, exp, dentry, fhp);
180 if (!err && !dentry->d_inode)
191 * Set various file attributes.
192 * N.B. After this call fhp needs an fh_put
195 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
196 int check_guard, time_t guardtime)
198 struct dentry *dentry;
200 int accmode = MAY_SATTR;
206 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
207 accmode |= MAY_WRITE|MAY_OWNER_OVERRIDE;
208 if (iap->ia_valid & ATTR_SIZE)
212 err = fh_verify(rqstp, fhp, ftype, accmode);
213 if (err || !iap->ia_valid)
216 dentry = fhp->fh_dentry;
217 inode = dentry->d_inode;
219 err = inode_change_ok(inode, iap);
220 /* could be a "touch" (utimes) request where the user is not the owner but does
221 * have write permission. In this case the user should be allowed to set
222 * both times to the current time. We could just assume any such SETATTR
223 * is intended to set the times to "now", but we do a couple of simple tests
224 * to increase our confidence.
226 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
227 #define MAX_TOUCH_TIME_ERROR (30*60)
229 && (iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET
230 && iap->ia_mtime == iap->ia_atime
232 /* looks good. now just make sure time is in the right ballpark.
233 * solaris, at least, doesn't seem to care what the time request is
235 time_t delta = iap->ia_atime - CURRENT_TIME;
236 if (delta<0) delta = -delta;
237 if (delta < MAX_TOUCH_TIME_ERROR) {
238 /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
239 * this will cause notify_change to set these times to "now"
241 iap->ia_valid &= ~BOTH_TIME_SET;
242 err = inode_change_ok(inode, iap);
249 /* The size case is special. It changes the file as well as the attributes. */
250 if (iap->ia_valid & ATTR_SIZE) {
251 if (iap->ia_size < inode->i_size) {
252 err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
258 * If we are changing the size of the file, then
259 * we need to break all leases.
261 err = get_lease(inode, FMODE_WRITE);
265 err = get_write_access(inode);
269 err = locks_verify_truncate(inode, NULL, iap->ia_size);
271 put_write_access(inode);
277 imode = inode->i_mode;
278 if (iap->ia_valid & ATTR_MODE) {
279 iap->ia_mode &= S_IALLUGO;
280 imode = iap->ia_mode |= (imode & ~S_IALLUGO);
283 /* Revoke setuid/setgid bit on chown/chgrp */
284 if ((iap->ia_valid & ATTR_UID) && (imode & S_ISUID)
285 && iap->ia_uid != inode->i_uid) {
286 iap->ia_valid |= ATTR_MODE;
287 iap->ia_mode = imode &= ~S_ISUID;
289 if ((iap->ia_valid & ATTR_GID) && (imode & S_ISGID)
290 && iap->ia_gid != inode->i_gid) {
291 iap->ia_valid |= ATTR_MODE;
292 iap->ia_mode = imode &= ~S_ISGID;
295 /* Change the attributes. */
298 iap->ia_valid |= ATTR_CTIME;
300 if (iap->ia_valid & ATTR_SIZE) {
304 err = nfserr_notsync;
305 if (!check_guard || guardtime == inode->i_ctime) {
306 err = notify_change(dentry, iap);
311 put_write_access(inode);
314 if (EX_ISSYNC(fhp->fh_export))
315 write_inode_now(inode, 1);
324 #ifdef CONFIG_NFSD_V3
326 * Check server access rights to a file system object
332 static struct accessmap nfs3_regaccess[] = {
333 { NFS3_ACCESS_READ, MAY_READ },
334 { NFS3_ACCESS_EXECUTE, MAY_EXEC },
335 { NFS3_ACCESS_MODIFY, MAY_WRITE|MAY_TRUNC },
336 { NFS3_ACCESS_EXTEND, MAY_WRITE },
341 static struct accessmap nfs3_diraccess[] = {
342 { NFS3_ACCESS_READ, MAY_READ },
343 { NFS3_ACCESS_LOOKUP, MAY_EXEC },
344 { NFS3_ACCESS_MODIFY, MAY_EXEC|MAY_WRITE|MAY_TRUNC },
345 { NFS3_ACCESS_EXTEND, MAY_EXEC|MAY_WRITE },
346 { NFS3_ACCESS_DELETE, MAY_REMOVE },
351 static struct accessmap nfs3_anyaccess[] = {
352 /* Some clients - Solaris 2.6 at least, make an access call
353 * to the server to check for access for things like /dev/null
354 * (which really, the server doesn't care about). So
355 * We provide simple access checking for them, looking
356 * mainly at mode bits
358 { NFS3_ACCESS_READ, MAY_READ },
359 { NFS3_ACCESS_EXECUTE, MAY_EXEC },
360 { NFS3_ACCESS_MODIFY, MAY_WRITE },
361 { NFS3_ACCESS_EXTEND, MAY_WRITE },
367 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access)
369 struct accessmap *map;
370 struct svc_export *export;
371 struct dentry *dentry;
372 u32 query, result = 0;
375 error = fh_verify(rqstp, fhp, 0, MAY_NOP);
379 export = fhp->fh_export;
380 dentry = fhp->fh_dentry;
382 if (S_ISREG(dentry->d_inode->i_mode))
383 map = nfs3_regaccess;
384 else if (S_ISDIR(dentry->d_inode->i_mode))
385 map = nfs3_diraccess;
387 map = nfs3_anyaccess;
391 for (; map->access; map++) {
392 if (map->access & query) {
394 err2 = nfsd_permission(export, dentry, map->how);
397 result |= map->access;
400 /* the following error codes just mean the access was not allowed,
401 * rather than an error occurred */
405 /* simply don't "or" in the access bit. */
418 #endif /* CONFIG_NFSD_V3 */
423 * Open an existing file or directory.
424 * The access argument indicates the type of open (read/write/lock)
425 * N.B. After this call fhp needs an fh_put
428 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
429 int access, struct file *filp)
431 struct dentry *dentry;
435 /* If we get here, then the client has already done an "open", and (hopefully)
436 * checked permission - so allow OWNER_OVERRIDE in case a chmod has now revoked
438 err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE);
442 dentry = fhp->fh_dentry;
443 inode = dentry->d_inode;
445 /* Disallow access to files with the append-only bit set or
446 * with mandatory locking enabled
449 if (IS_APPEND(inode) || IS_ISMNDLK(inode))
455 * Check to see if there are any leases on this file.
456 * This may block while leases are broken.
458 err = get_lease(inode, (access & MAY_WRITE) ? FMODE_WRITE : 0);
462 if ((access & MAY_WRITE) && (err = get_write_access(inode)) != 0)
465 memset(filp, 0, sizeof(*filp));
466 filp->f_op = fops_get(inode->i_fop);
467 atomic_set(&filp->f_count, 1);
468 filp->f_dentry = dentry;
469 filp->f_vfsmnt = fhp->fh_export->ex_mnt;
470 if (access & MAY_WRITE) {
471 filp->f_flags = O_WRONLY|O_LARGEFILE;
472 filp->f_mode = FMODE_WRITE;
475 filp->f_flags = O_RDONLY|O_LARGEFILE;
476 filp->f_mode = FMODE_READ;
480 if (filp->f_op && filp->f_op->open) {
481 err = filp->f_op->open(inode, filp);
483 fops_put(filp->f_op);
484 if (access & MAY_WRITE)
485 put_write_access(inode);
487 /* I nearly added put_filp() call here, but this filp
488 * is really on callers stack frame. -DaveM
490 atomic_dec(&filp->f_count);
504 nfsd_close(struct file *filp)
506 struct dentry *dentry = filp->f_dentry;
507 struct inode *inode = dentry->d_inode;
509 if (filp->f_op && filp->f_op->release)
510 filp->f_op->release(inode, filp);
511 fops_put(filp->f_op);
512 if (filp->f_mode & FMODE_WRITE)
513 put_write_access(inode);
518 * As this calls fsync (not fdatasync) there is no need for a write_inode
522 nfsd_sync(struct file *filp)
524 dprintk("nfsd: sync file %s\n", filp->f_dentry->d_name.name);
525 down(&filp->f_dentry->d_inode->i_sem);
526 filp->f_op->fsync(filp, filp->f_dentry, 0);
527 up(&filp->f_dentry->d_inode->i_sem);
531 nfsd_sync_dir(struct dentry *dp)
533 struct inode *inode = dp->d_inode;
534 int (*fsync) (struct file *, struct dentry *, int);
536 if (inode->i_fop && (fsync = inode->i_fop->fsync)) {
542 * Obtain the readahead parameters for the file
543 * specified by (dev, ino).
545 static inline struct raparms *
546 nfsd_get_raparms(dev_t dev, ino_t ino)
548 struct raparms *ra, **rap, **frap = NULL;
551 for (rap = &raparm_cache; (ra = *rap); rap = &ra->p_next) {
552 if (ra->p_ino == ino && ra->p_dev == dev)
555 if (ra->p_count == 0)
558 depth = nfsdstats.ra_size*11/10;
563 memset(ra, 0, sizeof(*ra));
567 if (rap != &raparm_cache) {
569 ra->p_next = raparm_cache;
573 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
578 * Read data from a file. count must contain the requested read count
579 * on entry. On return, *count contains the number of bytes actually read.
580 * N.B. After this call fhp needs an fh_put
583 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
584 char *buf, unsigned long *count)
591 err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file);
595 if (!file.f_op->read)
598 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
599 (!lock_may_read(file.f_dentry->d_inode, offset, *count)))
603 /* Get readahead parameters */
604 ra = nfsd_get_raparms(fhp->fh_export->ex_dev, fhp->fh_dentry->d_inode->i_ino);
606 file.f_reada = ra->p_reada;
607 file.f_ramax = ra->p_ramax;
608 file.f_raend = ra->p_raend;
609 file.f_ralen = ra->p_ralen;
610 file.f_rawin = ra->p_rawin;
614 oldfs = get_fs(); set_fs(KERNEL_DS);
615 err = file.f_op->read(&file, buf, *count, &file.f_pos);
618 /* Write back readahead params */
620 dprintk("nfsd: raparms %ld %ld %ld %ld %ld\n",
621 file.f_reada, file.f_ramax, file.f_raend,
622 file.f_ralen, file.f_rawin);
623 ra->p_reada = file.f_reada;
624 ra->p_ramax = file.f_ramax;
625 ra->p_raend = file.f_raend;
626 ra->p_ralen = file.f_ralen;
627 ra->p_rawin = file.f_rawin;
632 nfsdstats.io_read += err;
644 * Write data to a file.
645 * The stable flag requests synchronous writes.
646 * N.B. After this call fhp needs an fh_put
649 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
650 char *buf, unsigned long cnt, int *stablep)
652 struct svc_export *exp;
654 struct dentry *dentry;
658 int stable = *stablep;
660 err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
666 if (!file.f_op->write)
669 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
670 (!lock_may_write(file.f_dentry->d_inode, offset, cnt)))
674 dentry = file.f_dentry;
675 inode = dentry->d_inode;
676 exp = fhp->fh_export;
679 * Request sync writes if
680 * - the sync export option has been set, or
681 * - the client requested O_SYNC behavior (NFSv3 feature).
682 * - The file system doesn't support fsync().
683 * When gathered writes have been configured for this volume,
684 * flushing the data to disk is handled separately below.
687 if (file.f_op->fsync == 0) {/* COMMIT3 cannot work */
689 *stablep = 2; /* FILE_SYNC */
694 if (stable && !EX_WGATHER(exp))
695 file.f_flags |= O_SYNC;
697 file.f_pos = offset; /* set write offset */
699 /* Write the data. */
700 oldfs = get_fs(); set_fs(KERNEL_DS);
701 err = file.f_op->write(&file, buf, cnt, &file.f_pos);
703 nfsdstats.io_write += cnt;
706 /* clear setuid/setgid flag after write */
707 if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) {
710 ia.ia_valid = ATTR_MODE;
711 ia.ia_mode = inode->i_mode & ~(S_ISUID | S_ISGID);
712 notify_change(dentry, &ia);
715 if (err >= 0 && stable) {
716 static unsigned long last_ino;
717 static kdev_t last_dev = NODEV;
720 * Gathered writes: If another process is currently
721 * writing to the file, there's a high chance
722 * this is another nfsd (triggered by a bulk write
723 * from a client's biod). Rather than syncing the
724 * file with each write request, we sleep for 10 msec.
726 * I don't know if this roughly approximates
727 * C. Juszak's idea of gathered writes, but it's a
728 * nice and simple solution (IMHO), and it seems to
731 if (EX_WGATHER(exp)) {
732 if (atomic_read(&inode->i_writecount) > 1
733 || (last_ino == inode->i_ino && last_dev == inode->i_dev)) {
734 dprintk("nfsd: write defer %d\n", current->pid);
735 set_current_state(TASK_UNINTERRUPTIBLE);
736 schedule_timeout((HZ+99)/100);
737 current->state = TASK_RUNNING;
738 dprintk("nfsd: write resume %d\n", current->pid);
741 if (inode->i_state & I_DIRTY) {
742 dprintk("nfsd: write sync %d\n", current->pid);
746 wake_up(&inode->i_wait);
749 last_ino = inode->i_ino;
750 last_dev = inode->i_dev;
753 dprintk("nfsd: write complete err=%d\n", err);
765 #ifdef CONFIG_NFSD_V3
767 * Commit all pending writes to stable storage.
768 * Strictly speaking, we could sync just the indicated file region here,
769 * but there's currently no way we can ask the VFS to do so.
771 * Unfortunately we cannot lock the file to make sure we return full WCC
772 * data to the client, as locking happens lower down in the filesystem.
775 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
776 off_t offset, unsigned long count)
781 if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
783 if (EX_ISSYNC(fhp->fh_export)) {
784 if (file.f_op && file.f_op->fsync) {
787 err = nfserr_notsupp;
794 #endif /* CONFIG_NFSD_V3 */
797 * Create a file (regular, directory, device, fifo); UNIX sockets
798 * not yet implemented.
799 * If the response fh has been verified, the parent directory should
800 * already be locked. Note that the parent directory is left locked.
802 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
805 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
806 char *fname, int flen, struct iattr *iap,
807 int type, dev_t rdev, struct svc_fh *resfhp)
809 struct dentry *dentry, *dchild;
817 if (isdotent(fname, flen))
820 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
824 dentry = fhp->fh_dentry;
825 dirp = dentry->d_inode;
828 if(!dirp->i_op || !dirp->i_op->lookup)
831 * Check whether the response file handle has been verified yet.
832 * If it has, the parent directory should already be locked.
834 if (!resfhp->fh_dentry) {
835 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
837 dchild = lookup_one_len(fname, dentry, flen);
838 err = PTR_ERR(dchild);
841 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
845 /* called from nfsd_proc_create */
846 dchild = resfhp->fh_dentry;
847 if (!fhp->fh_locked) {
848 /* not actually possible */
850 "nfsd_create: parent %s/%s not locked!\n",
851 dentry->d_parent->d_name.name,
852 dentry->d_name.name);
858 * Make sure the child dentry is still negative ...
861 if (dchild->d_inode) {
862 dprintk("nfsd_create: dentry %s/%s not negative!\n",
863 dentry->d_name.name, dchild->d_name.name);
867 if (!(iap->ia_valid & ATTR_MODE))
869 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
872 * Get the dir op function pointer.
877 err = vfs_create(dirp, dchild, iap->ia_mode);
880 err = vfs_mkdir(dirp, dchild, iap->ia_mode);
886 err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
889 printk("nfsd: bad file type %o in nfsd_create\n", type);
895 if (EX_ISSYNC(fhp->fh_export)) {
896 nfsd_sync_dir(dentry);
897 write_inode_now(dchild->d_inode, 1);
901 /* Set file attributes. Mode has already been set and
902 * setting uid/gid works only for root. Irix appears to
903 * send along the gid when it tries to implement setgid
904 * directories via NFS.
907 if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0)
908 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
910 * Update the file handle to get the new inode info.
913 err = fh_update(resfhp);
922 #ifdef CONFIG_NFSD_V3
924 * NFSv3 version of nfsd_create
927 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
928 char *fname, int flen, struct iattr *iap,
929 struct svc_fh *resfhp, int createmode, u32 *verifier)
931 struct dentry *dentry, *dchild;
934 __u32 v_mtime=0, v_atime=0;
941 if (isdotent(fname, flen))
943 if (!(iap->ia_valid & ATTR_MODE))
945 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
949 dentry = fhp->fh_dentry;
950 dirp = dentry->d_inode;
952 /* Get all the sanity checks out of the way before
953 * we lock the parent. */
955 if(!dirp->i_op || !dirp->i_op->lookup)
960 * Compose the response file handle.
962 dchild = lookup_one_len(fname, dentry, flen);
963 err = PTR_ERR(dchild);
967 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
971 if (createmode == NFS3_CREATE_EXCLUSIVE) {
972 /* while the verifier would fit in mtime+atime,
973 * solaris7 gets confused (bugid 4218508) if these have
974 * the high bit set, so we use the mode as well
976 v_mtime = verifier[0]&0x7fffffff;
977 v_atime = verifier[1]&0x7fffffff;
979 | ((verifier[0]&0x80000000) >> (32-7)) /* u+x */
980 | ((verifier[1]&0x80000000) >> (32-9)) /* u+r */
984 if (dchild->d_inode) {
987 switch (createmode) {
988 case NFS3_CREATE_UNCHECKED:
989 if (! S_ISREG(dchild->d_inode->i_mode))
992 iap->ia_valid &= ATTR_SIZE;
996 case NFS3_CREATE_EXCLUSIVE:
997 if ( dchild->d_inode->i_mtime == v_mtime
998 && dchild->d_inode->i_atime == v_atime
999 && dchild->d_inode->i_mode == v_mode
1000 && dchild->d_inode->i_size == 0 )
1003 case NFS3_CREATE_GUARDED:
1009 err = vfs_create(dirp, dchild, iap->ia_mode);
1013 if (EX_ISSYNC(fhp->fh_export)) {
1014 nfsd_sync_dir(dentry);
1015 /* setattr will sync the child (or not) */
1019 * Update the filehandle to get the new inode info.
1021 err = fh_update(resfhp);
1025 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1026 /* Cram the verifier into atime/mtime/mode */
1027 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1028 | ATTR_MTIME_SET|ATTR_ATIME_SET
1030 iap->ia_mtime = v_mtime;
1031 iap->ia_atime = v_atime;
1032 iap->ia_mode = v_mode;
1035 /* Set file attributes.
1036 * Mode has already been set but we might need to reset it
1037 * for CREATE_EXCLUSIVE
1038 * Irix appears to send along the gid when it tries to
1039 * implement setgid directories via NFS. Clear out all that cruft.
1042 if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0)
1043 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1050 err = nfserrno(err);
1053 #endif /* CONFIG_NFSD_V3 */
1056 * Read a symlink. On entry, *lenp must contain the maximum path length that
1057 * fits into the buffer. On return, it contains the true length.
1058 * N.B. After this call fhp needs an fh_put
1061 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1063 struct dentry *dentry;
1064 struct inode *inode;
1068 err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
1072 dentry = fhp->fh_dentry;
1073 inode = dentry->d_inode;
1076 if (!inode->i_op || !inode->i_op->readlink)
1079 UPDATE_ATIME(inode);
1080 /* N.B. Why does this call need a get_fs()??
1081 * Remove the set_fs and watch the fireworks:-) --okir
1084 oldfs = get_fs(); set_fs(KERNEL_DS);
1085 err = inode->i_op->readlink(dentry, buf, *lenp);
1096 err = nfserrno(err);
1101 * Create a symlink and look up its inode
1102 * N.B. After this call _both_ fhp and resfhp need an fh_put
1105 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1106 char *fname, int flen,
1107 char *path, int plen,
1108 struct svc_fh *resfhp,
1111 struct dentry *dentry, *dnew;
1118 if (isdotent(fname, flen))
1121 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1125 dentry = fhp->fh_dentry;
1126 dnew = lookup_one_len(fname, dentry, flen);
1127 err = PTR_ERR(dnew);
1131 err = vfs_symlink(dentry->d_inode, dnew, path);
1133 if (EX_ISSYNC(fhp->fh_export))
1134 nfsd_sync_dir(dentry);
1136 iap->ia_valid &= ATTR_MODE /* ~(ATTR_MODE|ATTR_UID|ATTR_GID)*/;
1137 if (iap->ia_valid) {
1138 iap->ia_valid |= ATTR_CTIME;
1139 iap->ia_mode = (iap->ia_mode&S_IALLUGO)
1141 err = notify_change(dnew, iap);
1142 if (!err && EX_ISSYNC(fhp->fh_export))
1143 write_inode_now(dentry->d_inode, 1);
1147 err = nfserrno(err);
1150 /* Compose the fh so the dentry will be freed ... */
1151 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1152 if (err==0) err = cerr;
1157 err = nfserrno(err);
1163 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1166 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1167 char *name, int len, struct svc_fh *tfhp)
1169 struct dentry *ddir, *dnew, *dold;
1170 struct inode *dirp, *dest;
1173 err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
1176 err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
1184 if (isdotent(name, len))
1188 ddir = ffhp->fh_dentry;
1189 dirp = ddir->d_inode;
1191 dnew = lookup_one_len(name, ddir, len);
1192 err = PTR_ERR(dnew);
1196 dold = tfhp->fh_dentry;
1197 dest = dold->d_inode;
1199 err = vfs_link(dold, dirp, dnew);
1201 if (EX_ISSYNC(ffhp->fh_export)) {
1202 nfsd_sync_dir(ddir);
1203 write_inode_now(dest, 1);
1206 if (err == -EXDEV && rqstp->rq_vers == 2)
1209 err = nfserrno(err);
1218 err = nfserrno(err);
1224 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1227 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1228 struct svc_fh *tfhp, char *tname, int tlen)
1230 struct dentry *fdentry, *tdentry, *odentry, *ndentry;
1231 struct inode *fdir, *tdir;
1234 err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
1237 err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
1241 fdentry = ffhp->fh_dentry;
1242 fdir = fdentry->d_inode;
1244 tdentry = tfhp->fh_dentry;
1245 tdir = tdentry->d_inode;
1247 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1248 if (fdir->i_dev != tdir->i_dev)
1252 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1255 /* cannot use fh_lock as we need deadlock protective ordering
1256 * so do it by hand */
1257 double_down(&tdir->i_sem, &fdir->i_sem);
1258 ffhp->fh_locked = tfhp->fh_locked = 1;
1262 odentry = lookup_one_len(fname, fdentry, flen);
1263 err = PTR_ERR(odentry);
1264 if (IS_ERR(odentry))
1268 if (!odentry->d_inode)
1271 ndentry = lookup_one_len(tname, tdentry, tlen);
1272 err = PTR_ERR(ndentry);
1273 if (IS_ERR(ndentry))
1278 if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1279 ((atomic_read(&odentry->d_count) > 1)
1280 || (atomic_read(&ndentry->d_count) > 1))) {
1284 err = vfs_rename(fdir, odentry, tdir, ndentry);
1285 if (!err && EX_ISSYNC(tfhp->fh_export)) {
1286 nfsd_sync_dir(tdentry);
1287 nfsd_sync_dir(fdentry);
1295 err = nfserrno(err);
1297 /* we cannot reply on fh_unlock on the two filehandles,
1298 * as that would do the wrong thing if the two directories
1299 * were the same, so again we do it by hand
1301 fill_post_wcc(ffhp);
1302 fill_post_wcc(tfhp);
1303 double_up(&tdir->i_sem, &fdir->i_sem);
1304 ffhp->fh_locked = tfhp->fh_locked = 0;
1311 * Unlink a file or directory
1312 * N.B. After this call fhp needs an fh_put
1315 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1316 char *fname, int flen)
1318 struct dentry *dentry, *rdentry;
1323 if (!flen || isdotent(fname, flen))
1325 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
1330 dentry = fhp->fh_dentry;
1331 dirp = dentry->d_inode;
1333 rdentry = lookup_one_len(fname, dentry, flen);
1334 err = PTR_ERR(rdentry);
1335 if (IS_ERR(rdentry))
1338 if (!rdentry->d_inode) {
1344 if (type != S_IFDIR) { /* It's UNLINK */
1346 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1347 (atomic_read(&rdentry->d_count) > 1)) {
1351 err = vfs_unlink(dirp, rdentry);
1352 } else { /* It's RMDIR */
1353 err = vfs_rmdir(dirp, rdentry);
1360 if (EX_ISSYNC(fhp->fh_export))
1361 nfsd_sync_dir(dentry);
1367 err = nfserrno(err);
1372 * Read entries from a directory.
1373 * The verifier is an NFSv3 thing we ignore for now.
1376 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
1377 encode_dent_fn func, u32 *buffer, int *countp, u32 *verf)
1379 struct inode *inode;
1381 int oldlen, eof, err;
1383 struct readdir_cd cd;
1385 err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
1388 if (offset > ~(u32) 0)
1391 err = nfserr_notdir;
1392 if (!file.f_op->readdir)
1394 file.f_pos = offset;
1396 /* Set up the readdir context */
1397 memset(&cd, 0, sizeof(cd));
1400 cd.buflen = *countp; /* count of words */
1404 * Read the directory entries. This silly loop is necessary because
1405 * readdir() is not guaranteed to fill up the entire buffer, but
1406 * may choose to do less.
1408 inode = file.f_dentry->d_inode;
1409 down(&inode->i_sem);
1414 dprintk("nfsd: f_op->readdir(%x/%ld @ %d) buflen = %d (%d)\n",
1415 file.f_inode->i_dev, file.f_inode->i_ino,
1416 (int) file.f_pos, (int) oldlen, (int) cd.buflen);
1418 err = file.f_op->readdir(&file, &cd, (filldir_t) func);
1421 if (oldlen == cd.buflen)
1428 /* If we didn't fill the buffer completely, we're at EOF */
1432 if (rqstp->rq_vers == 3)
1433 (void)xdr_encode_hyper(cd.offset, file.f_pos);
1435 *cd.offset = htonl(file.f_pos);
1439 *p++ = 0; /* no more entries */
1440 *p++ = htonl(eof); /* end of directory */
1441 *countp = (caddr_t) p - (caddr_t) buffer;
1443 dprintk("nfsd: readdir result %d bytes, eof %d offset %d\n",
1445 cd.offset? ntohl(*cd.offset) : -1);
1454 err = nfserrno(err);
1459 * Get file system stats
1460 * N.B. After this call fhp needs an fh_put
1463 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct statfs *stat)
1465 int err = fh_verify(rqstp, fhp, 0, MAY_NOP);
1466 if (!err && vfs_statfs(fhp->fh_dentry->d_inode->i_sb,stat))
1472 * Check for a user's access permissions to this inode.
1475 nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
1477 struct inode *inode = dentry->d_inode;
1483 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1485 (acc & MAY_READ)? " read" : "",
1486 (acc & MAY_WRITE)? " write" : "",
1487 (acc & MAY_EXEC)? " exec" : "",
1488 (acc & MAY_SATTR)? " sattr" : "",
1489 (acc & MAY_TRUNC)? " trunc" : "",
1490 (acc & MAY_LOCK)? " lock" : "",
1491 (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
1493 IS_IMMUTABLE(inode)? " immut" : "",
1494 IS_APPEND(inode)? " append" : "",
1495 IS_RDONLY(inode)? " ro" : "");
1496 dprintk(" owner %d/%d user %d/%d\n",
1497 inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
1500 /* only care about readonly exports for files and
1501 * directories. links don't have meaningful write access,
1502 * and all else is local to the client
1504 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
1505 if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
1506 if (EX_RDONLY(exp) || IS_RDONLY(inode))
1508 if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
1511 if ((acc & MAY_TRUNC) && IS_APPEND(inode))
1514 if (acc & MAY_LOCK) {
1515 /* If we cannot rely on authentication in NLM requests,
1516 * just allow locks, otherwise require read permission, or
1519 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1522 acc = MAY_READ | MAY_OWNER_OVERRIDE;
1525 * The file owner always gets access permission for accesses that
1526 * would normally be checked at open time. This is to make
1527 * file access work even when the client has done a fchmod(fd, 0).
1529 * However, `cp foo bar' should fail nevertheless when bar is
1530 * readonly. A sensible way to do this might be to reject all
1531 * attempts to truncate a read-only file, because a creat() call
1532 * always implies file truncation.
1533 * ... but this isn't really fair. A process may reasonably call
1534 * ftruncate on an open file descriptor on a file with perm 000.
1535 * We must trust the client to do permission checking - using "ACCESS"
1538 if ((acc & MAY_OWNER_OVERRIDE) &&
1539 inode->i_uid == current->fsuid)
1542 acc &= ~ MAY_OWNER_OVERRIDE; /* This bit is no longer needed,
1543 and gets in the way later */
1545 err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
1547 /* Allow read access to binaries even when mode 111 */
1548 if (err == -EACCES && S_ISREG(inode->i_mode) && acc == MAY_READ)
1549 err = permission(inode, MAY_EXEC);
1551 return err? nfserrno(err) : 0;
1555 nfsd_racache_shutdown(void)
1559 dprintk("nfsd: freeing readahead buffers.\n");
1561 raparm_cache = raparml = NULL;
1564 * Initialize readahead param cache
1567 nfsd_racache_init(int cache_size)
1573 raparml = kmalloc(sizeof(struct raparms) * cache_size, GFP_KERNEL);
1575 if (raparml != NULL) {
1576 dprintk("nfsd: allocating %d readahead buffers.\n",
1578 memset(raparml, 0, sizeof(struct raparms) * cache_size);
1579 for (i = 0; i < cache_size - 1; i++) {
1580 raparml[i].p_next = raparml + i + 1;
1582 raparm_cache = raparml;
1585 "nfsd: Could not allocate memory read-ahead cache.\n");
1588 nfsdstats.ra_size = cache_size;