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_NOHIDE, 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 if (dparent != exp->ex_dentry)
118 dentry = dget(dparent->d_parent);
119 else if (!EX_NOHIDE(exp))
120 dentry = dget(dparent); /* .. == . just like at / */
122 /* checking mountpoint crossing is very different when stepping up */
123 struct svc_export *exp2 = NULL;
125 struct vfsmount *mnt = mntget(exp->ex_mnt);
126 dentry = dget(dparent);
127 while(follow_up(&mnt, &dentry))
129 dp = dget(dentry->d_parent);
132 for ( ; exp2 == NULL && dp->d_parent != dp;
134 exp2 = exp_get(exp->ex_client, dp->d_inode->i_dev, dp->d_inode->i_ino);
137 dentry = dget(dparent);
145 dentry = lookup_one_len(name, dparent, len);
146 err = PTR_ERR(dentry);
150 * check if we have crossed a mount point ...
152 if (d_mountpoint(dentry)) {
153 struct svc_export *exp2 = NULL;
154 struct vfsmount *mnt = mntget(exp->ex_mnt);
155 struct dentry *mounts = dget(dentry);
156 while (follow_down(&mnt,&mounts)&&d_mountpoint(mounts))
158 exp2 = exp_get(rqstp->rq_client,
159 mounts->d_inode->i_dev,
160 mounts->d_inode->i_ino);
161 if (exp2 && EX_NOHIDE(exp2)) {
162 /* successfully crossed mount point */
172 if (dentry->d_inode && dentry->d_inode->i_op &&
173 dentry->d_inode->i_op->revalidate &&
174 dentry->d_inode->i_op->revalidate(dentry))
177 err = fh_compose(resfh, exp, dentry, fhp);
178 if (!err && !dentry->d_inode)
189 * Set various file attributes.
190 * N.B. After this call fhp needs an fh_put
193 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
194 int check_guard, time_t guardtime)
196 struct dentry *dentry;
198 int accmode = MAY_SATTR;
204 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
205 accmode |= MAY_WRITE|MAY_OWNER_OVERRIDE;
206 if (iap->ia_valid & ATTR_SIZE)
210 err = fh_verify(rqstp, fhp, ftype, accmode);
211 if (err || !iap->ia_valid)
214 dentry = fhp->fh_dentry;
215 inode = dentry->d_inode;
217 /* NFSv2 does not differentiate between "set-[ac]time-to-now"
218 * which only requires access, and "set-[ac]time-to-X" which
219 * requires ownership.
220 * So if it looks like it might be "set both to the same time which
221 * is close to now", and if inode_change_ok fails, then we
222 * convert to "set to now" instead of "set to explicit time"
224 * We only call inode_change_ok as the last test as technically
225 * it is not an interface that we should be using. It is only
226 * valid if the filesystem does not define it's own i_op->setattr.
228 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
229 #define MAX_TOUCH_TIME_ERROR (30*60)
230 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET
231 && iap->ia_mtime == iap->ia_atime
233 /* Looks probable. Now just make sure time is in the right ballpark.
234 * Solaris, at least, doesn't seem to care what the time request is.
235 * We require it be within 30 minutes of now.
237 time_t delta = iap->ia_atime - CURRENT_TIME;
238 if (delta<0) delta = -delta;
239 if (delta < MAX_TOUCH_TIME_ERROR &&
240 inode_change_ok(inode, iap) != 0) {
241 /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
242 * this will cause notify_change to set these times to "now"
244 iap->ia_valid &= ~BOTH_TIME_SET;
248 /* The size case is special. It changes the file as well as the attributes. */
249 if (iap->ia_valid & ATTR_SIZE) {
250 if (iap->ia_size < inode->i_size) {
251 err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
257 * If we are changing the size of the file, then
258 * we need to break all leases.
260 err = get_lease(inode, FMODE_WRITE);
264 err = get_write_access(inode);
268 err = locks_verify_truncate(inode, NULL, iap->ia_size);
270 put_write_access(inode);
276 imode = inode->i_mode;
277 if (iap->ia_valid & ATTR_MODE) {
278 iap->ia_mode &= S_IALLUGO;
279 imode = iap->ia_mode |= (imode & ~S_IALLUGO);
282 /* Revoke setuid/setgid bit on chown/chgrp */
283 if ((iap->ia_valid & ATTR_UID)
286 && iap->ia_uid != inode->i_uid) {
287 iap->ia_valid |= ATTR_MODE;
288 iap->ia_mode = imode &= ~S_ISUID;
290 if ((iap->ia_valid & ATTR_GID)
291 && (imode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)
293 && iap->ia_gid != inode->i_gid) {
294 iap->ia_valid |= ATTR_MODE;
295 iap->ia_mode = imode &= ~S_ISGID;
298 /* Change the attributes. */
301 iap->ia_valid |= ATTR_CTIME;
303 if (iap->ia_valid & ATTR_SIZE) {
304 down_write(&inode->i_alloc_sem);
308 err = nfserr_notsync;
309 if (!check_guard || guardtime == inode->i_ctime) {
310 err = notify_change(dentry, iap);
315 up_write(&inode->i_alloc_sem);
316 put_write_access(inode);
319 if (EX_ISSYNC(fhp->fh_export))
320 write_inode_now(inode, 1);
329 #ifdef CONFIG_NFSD_V3
331 * Check server access rights to a file system object
337 static struct accessmap nfs3_regaccess[] = {
338 { NFS3_ACCESS_READ, MAY_READ },
339 { NFS3_ACCESS_EXECUTE, MAY_EXEC },
340 { NFS3_ACCESS_MODIFY, MAY_WRITE|MAY_TRUNC },
341 { NFS3_ACCESS_EXTEND, MAY_WRITE },
346 static struct accessmap nfs3_diraccess[] = {
347 { NFS3_ACCESS_READ, MAY_READ },
348 { NFS3_ACCESS_LOOKUP, MAY_EXEC },
349 { NFS3_ACCESS_MODIFY, MAY_EXEC|MAY_WRITE|MAY_TRUNC },
350 { NFS3_ACCESS_EXTEND, MAY_EXEC|MAY_WRITE },
351 { NFS3_ACCESS_DELETE, MAY_REMOVE },
356 static struct accessmap nfs3_anyaccess[] = {
357 /* Some clients - Solaris 2.6 at least, make an access call
358 * to the server to check for access for things like /dev/null
359 * (which really, the server doesn't care about). So
360 * We provide simple access checking for them, looking
361 * mainly at mode bits
363 { NFS3_ACCESS_READ, MAY_READ },
364 { NFS3_ACCESS_EXECUTE, MAY_EXEC },
365 { NFS3_ACCESS_MODIFY, MAY_WRITE },
366 { NFS3_ACCESS_EXTEND, MAY_WRITE },
372 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access)
374 struct accessmap *map;
375 struct svc_export *export;
376 struct dentry *dentry;
377 u32 query, result = 0;
380 error = fh_verify(rqstp, fhp, 0, MAY_NOP);
384 export = fhp->fh_export;
385 dentry = fhp->fh_dentry;
387 if (S_ISREG(dentry->d_inode->i_mode))
388 map = nfs3_regaccess;
389 else if (S_ISDIR(dentry->d_inode->i_mode))
390 map = nfs3_diraccess;
392 map = nfs3_anyaccess;
396 for (; map->access; map++) {
397 if (map->access & query) {
399 err2 = nfsd_permission(export, dentry, map->how);
402 result |= map->access;
405 /* the following error codes just mean the access was not allowed,
406 * rather than an error occurred */
410 /* simply don't "or" in the access bit. */
423 #endif /* CONFIG_NFSD_V3 */
428 * Open an existing file or directory.
429 * The access argument indicates the type of open (read/write/lock)
430 * N.B. After this call fhp needs an fh_put
433 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
434 int access, struct file *filp)
436 struct dentry *dentry;
440 /* If we get here, then the client has already done an "open", and (hopefully)
441 * checked permission - so allow OWNER_OVERRIDE in case a chmod has now revoked
443 err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE);
447 dentry = fhp->fh_dentry;
448 inode = dentry->d_inode;
450 /* Disallow access to files with the append-only bit set or
451 * with mandatory locking enabled
454 if (IS_APPEND(inode) || IS_ISMNDLK(inode))
460 * Check to see if there are any leases on this file.
461 * This may block while leases are broken.
463 err = get_lease(inode, (access & MAY_WRITE) ? FMODE_WRITE : 0);
467 if ((access & MAY_WRITE) && (err = get_write_access(inode)) != 0)
470 memset(filp, 0, sizeof(*filp));
471 filp->f_op = fops_get(inode->i_fop);
472 atomic_set(&filp->f_count, 1);
473 filp->f_dentry = dentry;
474 filp->f_vfsmnt = fhp->fh_export->ex_mnt;
475 filp->f_maxcount = INT_MAX;
477 if (access & MAY_WRITE) {
478 filp->f_flags = O_WRONLY|O_LARGEFILE;
479 filp->f_mode = FMODE_WRITE;
482 filp->f_flags = O_RDONLY|O_LARGEFILE;
483 filp->f_mode = FMODE_READ;
487 if (filp->f_op && filp->f_op->open) {
488 err = filp->f_op->open(inode, filp);
490 fops_put(filp->f_op);
491 if (access & MAY_WRITE)
492 put_write_access(inode);
494 /* I nearly added put_filp() call here, but this filp
495 * is really on callers stack frame. -DaveM
497 atomic_dec(&filp->f_count);
511 nfsd_close(struct file *filp)
513 struct dentry *dentry = filp->f_dentry;
514 struct inode *inode = dentry->d_inode;
516 if (filp->f_op && filp->f_op->release)
517 filp->f_op->release(inode, filp);
518 fops_put(filp->f_op);
519 if (filp->f_mode & FMODE_WRITE)
520 put_write_access(inode);
525 * As this calls fsync (not fdatasync) there is no need for a write_inode
528 inline void nfsd_dosync(struct file *filp, struct dentry *dp,
529 struct file_operations *fop)
531 struct inode *inode = dp->d_inode;
532 int (*fsync) (struct file *, struct dentry *, int);
534 filemap_fdatasync(inode->i_mapping);
535 if (fop && (fsync = fop->fsync))
537 filemap_fdatawait(inode->i_mapping);
542 nfsd_sync(struct file *filp)
544 struct inode *inode = filp->f_dentry->d_inode;
545 dprintk("nfsd: sync file %s\n", filp->f_dentry->d_name.name);
547 nfsd_dosync(filp, filp->f_dentry, filp->f_op);
552 nfsd_sync_dir(struct dentry *dp)
554 nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
558 * Obtain the readahead parameters for the file
559 * specified by (dev, ino).
561 static inline struct raparms *
562 nfsd_get_raparms(dev_t dev, ino_t ino)
564 struct raparms *ra, **rap, **frap = NULL;
567 for (rap = &raparm_cache; (ra = *rap); rap = &ra->p_next) {
568 if (ra->p_ino == ino && ra->p_dev == dev)
571 if (ra->p_count == 0)
574 depth = nfsdstats.ra_size*11/10;
587 if (rap != &raparm_cache) {
589 ra->p_next = raparm_cache;
593 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
597 /* copied from fs/read_write.c */
598 static inline loff_t llseek(struct file *file, loff_t offset, int origin)
600 loff_t (*fn)(struct file *, loff_t, int);
604 if (file->f_op && file->f_op->llseek)
605 fn = file->f_op->llseek;
607 retval = fn(file, offset, origin);
614 * Read data from a file. count must contain the requested read count
615 * on entry. On return, *count contains the number of bytes actually read.
616 * N.B. After this call fhp needs an fh_put
619 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
620 char *buf, unsigned long *count)
627 err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file);
631 if (!file.f_op->read)
634 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
635 (!lock_may_read(file.f_dentry->d_inode, offset, *count)))
639 /* Get readahead parameters */
640 ra = nfsd_get_raparms(fhp->fh_export->ex_dev, fhp->fh_dentry->d_inode->i_ino);
642 file.f_reada = ra->p_reada;
643 file.f_ramax = ra->p_ramax;
644 file.f_raend = ra->p_raend;
645 file.f_ralen = ra->p_ralen;
646 file.f_rawin = ra->p_rawin;
648 llseek(&file, offset, 0);
650 oldfs = get_fs(); set_fs(KERNEL_DS);
651 err = file.f_op->read(&file, buf, *count, &file.f_pos);
654 /* Write back readahead params */
656 dprintk("nfsd: raparms %ld %ld %ld %ld %ld\n",
657 file.f_reada, file.f_ramax, file.f_raend,
658 file.f_ralen, file.f_rawin);
659 ra->p_reada = file.f_reada;
660 ra->p_ramax = file.f_ramax;
661 ra->p_raend = file.f_raend;
662 ra->p_ralen = file.f_ralen;
663 ra->p_rawin = file.f_rawin;
668 nfsdstats.io_read += err;
680 * Write data to a file.
681 * The stable flag requests synchronous writes.
682 * N.B. After this call fhp needs an fh_put
685 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
686 char *buf, unsigned long cnt, int *stablep)
688 struct svc_export *exp;
690 struct dentry *dentry;
694 int stable = *stablep;
696 err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
702 if (!file.f_op->write)
705 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
706 (!lock_may_write(file.f_dentry->d_inode, offset, cnt)))
710 dentry = file.f_dentry;
711 inode = dentry->d_inode;
712 exp = fhp->fh_export;
715 * Request sync writes if
716 * - the sync export option has been set, or
717 * - the client requested O_SYNC behavior (NFSv3 feature).
718 * - The file system doesn't support fsync().
719 * When gathered writes have been configured for this volume,
720 * flushing the data to disk is handled separately below.
723 if (file.f_op->fsync == 0) {/* COMMIT3 cannot work */
725 *stablep = 2; /* FILE_SYNC */
730 if (stable && !EX_WGATHER(exp))
731 file.f_flags |= O_SYNC;
734 llseek(&file, offset, 0);
736 /* Write the data. */
737 oldfs = get_fs(); set_fs(KERNEL_DS);
738 err = file.f_op->write(&file, buf, cnt, &file.f_pos);
740 nfsdstats.io_write += cnt;
743 /* clear setuid/setgid flag after write */
744 if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) {
747 ia.ia_valid = ATTR_MODE;
748 ia.ia_mode = inode->i_mode & ~(S_ISUID | S_ISGID);
749 notify_change(dentry, &ia);
752 if (err >= 0 && stable) {
753 static unsigned long last_ino;
754 static kdev_t last_dev = NODEV;
757 * Gathered writes: If another process is currently
758 * writing to the file, there's a high chance
759 * this is another nfsd (triggered by a bulk write
760 * from a client's biod). Rather than syncing the
761 * file with each write request, we sleep for 10 msec.
763 * I don't know if this roughly approximates
764 * C. Juszak's idea of gathered writes, but it's a
765 * nice and simple solution (IMHO), and it seems to
768 if (EX_WGATHER(exp)) {
769 if (atomic_read(&inode->i_writecount) > 1
770 || (last_ino == inode->i_ino && last_dev == inode->i_dev)) {
771 dprintk("nfsd: write defer %d\n", current->pid);
772 set_current_state(TASK_UNINTERRUPTIBLE);
773 schedule_timeout((HZ+99)/100);
774 current->state = TASK_RUNNING;
775 dprintk("nfsd: write resume %d\n", current->pid);
778 if (inode->i_state & I_DIRTY) {
779 dprintk("nfsd: write sync %d\n", current->pid);
783 wake_up(&inode->i_wait);
786 last_ino = inode->i_ino;
787 last_dev = inode->i_dev;
790 dprintk("nfsd: write complete err=%d\n", err);
802 #ifdef CONFIG_NFSD_V3
804 * Commit all pending writes to stable storage.
805 * Strictly speaking, we could sync just the indicated file region here,
806 * but there's currently no way we can ask the VFS to do so.
808 * Unfortunately we cannot lock the file to make sure we return full WCC
809 * data to the client, as locking happens lower down in the filesystem.
812 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
813 off_t offset, unsigned long count)
818 if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
820 if (EX_ISSYNC(fhp->fh_export)) {
821 if (file.f_op && file.f_op->fsync) {
824 err = nfserr_notsupp;
831 #endif /* CONFIG_NFSD_V3 */
834 * Create a file (regular, directory, device, fifo); UNIX sockets
835 * not yet implemented.
836 * If the response fh has been verified, the parent directory should
837 * already be locked. Note that the parent directory is left locked.
839 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
842 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
843 char *fname, int flen, struct iattr *iap,
844 int type, dev_t rdev, struct svc_fh *resfhp)
846 struct dentry *dentry, *dchild;
854 if (isdotent(fname, flen))
857 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
861 dentry = fhp->fh_dentry;
862 dirp = dentry->d_inode;
865 if(!dirp->i_op || !dirp->i_op->lookup)
868 * Check whether the response file handle has been verified yet.
869 * If it has, the parent directory should already be locked.
871 if (!resfhp->fh_dentry) {
872 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
874 dchild = lookup_one_len(fname, dentry, flen);
875 err = PTR_ERR(dchild);
878 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
882 /* called from nfsd_proc_create */
883 dchild = resfhp->fh_dentry;
884 if (!fhp->fh_locked) {
885 /* not actually possible */
887 "nfsd_create: parent %s/%s not locked!\n",
888 dentry->d_parent->d_name.name,
889 dentry->d_name.name);
895 * Make sure the child dentry is still negative ...
898 if (dchild->d_inode) {
899 dprintk("nfsd_create: dentry %s/%s not negative!\n",
900 dentry->d_name.name, dchild->d_name.name);
904 if (!(iap->ia_valid & ATTR_MODE))
906 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
909 * Get the dir op function pointer.
914 err = vfs_create(dirp, dchild, iap->ia_mode);
917 err = vfs_mkdir(dirp, dchild, iap->ia_mode);
923 err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
926 printk("nfsd: bad file type %o in nfsd_create\n", type);
932 if (EX_ISSYNC(fhp->fh_export)) {
933 nfsd_sync_dir(dentry);
934 write_inode_now(dchild->d_inode, 1);
938 /* Set file attributes. Mode has already been set and
939 * setting uid/gid works only for root. Irix appears to
940 * send along the gid when it tries to implement setgid
941 * directories via NFS.
944 if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0)
945 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
947 * Update the file handle to get the new inode info.
950 err = fh_update(resfhp);
959 #ifdef CONFIG_NFSD_V3
961 * NFSv3 version of nfsd_create
964 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
965 char *fname, int flen, struct iattr *iap,
966 struct svc_fh *resfhp, int createmode, u32 *verifier)
968 struct dentry *dentry, *dchild;
971 __u32 v_mtime=0, v_atime=0;
978 if (isdotent(fname, flen))
980 if (!(iap->ia_valid & ATTR_MODE))
982 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
986 dentry = fhp->fh_dentry;
987 dirp = dentry->d_inode;
989 /* Get all the sanity checks out of the way before
990 * we lock the parent. */
992 if(!dirp->i_op || !dirp->i_op->lookup)
997 * Compose the response file handle.
999 dchild = lookup_one_len(fname, dentry, flen);
1000 err = PTR_ERR(dchild);
1004 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1008 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1009 /* while the verifier would fit in mtime+atime,
1010 * solaris7 gets confused (bugid 4218508) if these have
1011 * the high bit set, so we use the mode as well
1013 v_mtime = verifier[0]&0x7fffffff;
1014 v_atime = verifier[1]&0x7fffffff;
1016 | ((verifier[0]&0x80000000) >> (32-7)) /* u+x */
1017 | ((verifier[1]&0x80000000) >> (32-9)) /* u+r */
1021 if (dchild->d_inode) {
1024 switch (createmode) {
1025 case NFS3_CREATE_UNCHECKED:
1026 if (! S_ISREG(dchild->d_inode->i_mode))
1029 iap->ia_valid &= ATTR_SIZE;
1033 case NFS3_CREATE_EXCLUSIVE:
1034 if ( dchild->d_inode->i_mtime == v_mtime
1035 && dchild->d_inode->i_atime == v_atime
1036 && dchild->d_inode->i_mode == v_mode
1037 && dchild->d_inode->i_size == 0 )
1040 case NFS3_CREATE_GUARDED:
1046 err = vfs_create(dirp, dchild, iap->ia_mode);
1050 if (EX_ISSYNC(fhp->fh_export)) {
1051 nfsd_sync_dir(dentry);
1052 /* setattr will sync the child (or not) */
1056 * Update the filehandle to get the new inode info.
1058 err = fh_update(resfhp);
1062 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1063 /* Cram the verifier into atime/mtime/mode */
1064 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1065 | ATTR_MTIME_SET|ATTR_ATIME_SET
1067 iap->ia_mtime = v_mtime;
1068 iap->ia_atime = v_atime;
1069 iap->ia_mode = v_mode;
1072 /* Set file attributes.
1073 * Mode has already been set but we might need to reset it
1074 * for CREATE_EXCLUSIVE
1075 * Irix appears to send along the gid when it tries to
1076 * implement setgid directories via NFS. Clear out all that cruft.
1079 if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0)
1080 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1087 err = nfserrno(err);
1090 #endif /* CONFIG_NFSD_V3 */
1093 * Read a symlink. On entry, *lenp must contain the maximum path length that
1094 * fits into the buffer. On return, it contains the true length.
1095 * N.B. After this call fhp needs an fh_put
1098 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1100 struct dentry *dentry;
1101 struct inode *inode;
1105 err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
1109 dentry = fhp->fh_dentry;
1110 inode = dentry->d_inode;
1113 if (!inode->i_op || !inode->i_op->readlink)
1116 UPDATE_ATIME(inode);
1117 /* N.B. Why does this call need a get_fs()??
1118 * Remove the set_fs and watch the fireworks:-) --okir
1121 oldfs = get_fs(); set_fs(KERNEL_DS);
1122 err = inode->i_op->readlink(dentry, buf, *lenp);
1133 err = nfserrno(err);
1138 * Create a symlink and look up its inode
1139 * N.B. After this call _both_ fhp and resfhp need an fh_put
1142 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1143 char *fname, int flen,
1144 char *path, int plen,
1145 struct svc_fh *resfhp,
1148 struct dentry *dentry, *dnew;
1155 if (isdotent(fname, flen))
1158 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1162 dentry = fhp->fh_dentry;
1163 dnew = lookup_one_len(fname, dentry, flen);
1164 err = PTR_ERR(dnew);
1168 err = vfs_symlink(dentry->d_inode, dnew, path);
1170 if (EX_ISSYNC(fhp->fh_export))
1171 nfsd_sync_dir(dentry);
1173 iap->ia_valid &= ATTR_MODE /* ~(ATTR_MODE|ATTR_UID|ATTR_GID)*/;
1174 if (iap->ia_valid) {
1175 iap->ia_valid |= ATTR_CTIME;
1176 iap->ia_mode = (iap->ia_mode&S_IALLUGO)
1178 err = notify_change(dnew, iap);
1180 err = nfserrno(err);
1181 else if (EX_ISSYNC(fhp->fh_export))
1182 write_inode_now(dentry->d_inode, 1);
1186 err = nfserrno(err);
1189 /* Compose the fh so the dentry will be freed ... */
1190 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1191 if (err==0) err = cerr;
1196 err = nfserrno(err);
1202 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1205 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1206 char *name, int len, struct svc_fh *tfhp)
1208 struct dentry *ddir, *dnew, *dold;
1209 struct inode *dirp, *dest;
1212 err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
1215 err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
1223 if (isdotent(name, len))
1227 ddir = ffhp->fh_dentry;
1228 dirp = ddir->d_inode;
1230 dnew = lookup_one_len(name, ddir, len);
1231 err = PTR_ERR(dnew);
1235 dold = tfhp->fh_dentry;
1236 dest = dold->d_inode;
1238 err = vfs_link(dold, dirp, dnew);
1240 if (EX_ISSYNC(ffhp->fh_export)) {
1241 nfsd_sync_dir(ddir);
1242 write_inode_now(dest, 1);
1245 if (err == -EXDEV && rqstp->rq_vers == 2)
1248 err = nfserrno(err);
1257 err = nfserrno(err);
1263 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1266 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1267 struct svc_fh *tfhp, char *tname, int tlen)
1269 struct dentry *fdentry, *tdentry, *odentry, *ndentry;
1270 struct inode *fdir, *tdir;
1273 err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
1276 err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
1280 fdentry = ffhp->fh_dentry;
1281 fdir = fdentry->d_inode;
1283 tdentry = tfhp->fh_dentry;
1284 tdir = tdentry->d_inode;
1286 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1287 if (fdir->i_dev != tdir->i_dev)
1291 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1294 /* cannot use fh_lock as we need deadlock protective ordering
1295 * so do it by hand */
1296 double_down(&tdir->i_sem, &fdir->i_sem);
1297 ffhp->fh_locked = tfhp->fh_locked = 1;
1301 odentry = lookup_one_len(fname, fdentry, flen);
1302 err = PTR_ERR(odentry);
1303 if (IS_ERR(odentry))
1307 if (!odentry->d_inode)
1310 ndentry = lookup_one_len(tname, tdentry, tlen);
1311 err = PTR_ERR(ndentry);
1312 if (IS_ERR(ndentry))
1317 if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1318 ((atomic_read(&odentry->d_count) > 1)
1319 || (atomic_read(&ndentry->d_count) > 1))) {
1323 err = vfs_rename(fdir, odentry, tdir, ndentry);
1324 if (!err && EX_ISSYNC(tfhp->fh_export)) {
1325 nfsd_sync_dir(tdentry);
1326 nfsd_sync_dir(fdentry);
1334 err = nfserrno(err);
1336 /* we cannot reply on fh_unlock on the two filehandles,
1337 * as that would do the wrong thing if the two directories
1338 * were the same, so again we do it by hand
1340 fill_post_wcc(ffhp);
1341 fill_post_wcc(tfhp);
1342 double_up(&tdir->i_sem, &fdir->i_sem);
1343 ffhp->fh_locked = tfhp->fh_locked = 0;
1350 * Unlink a file or directory
1351 * N.B. After this call fhp needs an fh_put
1354 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1355 char *fname, int flen)
1357 struct dentry *dentry, *rdentry;
1362 if (!flen || isdotent(fname, flen))
1364 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
1369 dentry = fhp->fh_dentry;
1370 dirp = dentry->d_inode;
1372 rdentry = lookup_one_len(fname, dentry, flen);
1373 err = PTR_ERR(rdentry);
1374 if (IS_ERR(rdentry))
1377 if (!rdentry->d_inode) {
1383 if (type != S_IFDIR) { /* It's UNLINK */
1385 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1386 (atomic_read(&rdentry->d_count) > 1)) {
1390 err = vfs_unlink(dirp, rdentry);
1391 } else { /* It's RMDIR */
1392 err = vfs_rmdir(dirp, rdentry);
1399 if (EX_ISSYNC(fhp->fh_export))
1400 nfsd_sync_dir(dentry);
1406 err = nfserrno(err);
1411 * Read entries from a directory.
1412 * The verifier is an NFSv3 thing we ignore for now.
1415 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
1416 encode_dent_fn func, u32 *buffer, int *countp, u32 *verf)
1419 int oldlen, eof, err;
1421 struct readdir_cd cd;
1423 err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
1427 offset = llseek(&file, offset, 0);
1429 err = nfserrno((int)offset);
1433 /* Set up the readdir context */
1434 memset(&cd, 0, sizeof(cd));
1437 cd.buflen = *countp; /* count of words */
1441 * Read the directory entries. This silly loop is necessary because
1442 * readdir() is not guaranteed to fill up the entire buffer, but
1443 * may choose to do less.
1449 err = vfs_readdir(&file, (filldir_t) func, &cd);
1454 } while (oldlen != cd.buflen && !cd.eob);
1456 /* If we didn't fill the buffer completely, we're at EOF */
1460 offset = llseek(&file, 0LL, 1);
1462 if (rqstp->rq_vers == 3)
1463 (void)xdr_encode_hyper(cd.offset, offset);
1465 *cd.offset = htonl(offset);
1469 *p++ = 0; /* no more entries */
1470 *p++ = htonl(eof); /* end of directory */
1471 *countp = (caddr_t) p - (caddr_t) buffer;
1473 dprintk("nfsd: readdir result %d bytes, eof %d offset %d\n",
1475 cd.offset? ntohl(*cd.offset) : -1);
1483 err = nfserrno(err);
1488 * Get file system stats
1489 * N.B. After this call fhp needs an fh_put
1492 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct statfs *stat)
1494 int err = fh_verify(rqstp, fhp, 0, MAY_NOP);
1495 if (!err && vfs_statfs(fhp->fh_dentry->d_inode->i_sb,stat))
1501 * Check for a user's access permissions to this inode.
1504 nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
1506 struct inode *inode = dentry->d_inode;
1512 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1514 (acc & MAY_READ)? " read" : "",
1515 (acc & MAY_WRITE)? " write" : "",
1516 (acc & MAY_EXEC)? " exec" : "",
1517 (acc & MAY_SATTR)? " sattr" : "",
1518 (acc & MAY_TRUNC)? " trunc" : "",
1519 (acc & MAY_LOCK)? " lock" : "",
1520 (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
1522 IS_IMMUTABLE(inode)? " immut" : "",
1523 IS_APPEND(inode)? " append" : "",
1524 IS_RDONLY(inode)? " ro" : "");
1525 dprintk(" owner %d/%d user %d/%d\n",
1526 inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
1529 /* The following code is here to make IRIX happy, which
1530 * does a permission check every time a user does
1531 * echo yaddayadda > special-file
1532 * by sending a CREATE request.
1533 * The original code would check read-only export status
1534 * only for regular files and directories, allowing
1535 * clients to chown/chmod device files and fifos even
1536 * on volumes exported read-only. */
1537 if (!(acc & _NFSD_IRIX_BOGOSITY)
1538 && (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC))) {
1539 if (EX_RDONLY(exp) || IS_RDONLY(inode))
1541 if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
1544 if ((acc & MAY_TRUNC) && IS_APPEND(inode))
1547 if (acc & MAY_LOCK) {
1548 /* If we cannot rely on authentication in NLM requests,
1549 * just allow locks, otherwise require read permission, or
1552 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1555 acc = MAY_READ | MAY_OWNER_OVERRIDE;
1558 * The file owner always gets access permission for accesses that
1559 * would normally be checked at open time. This is to make
1560 * file access work even when the client has done a fchmod(fd, 0).
1562 * However, `cp foo bar' should fail nevertheless when bar is
1563 * readonly. A sensible way to do this might be to reject all
1564 * attempts to truncate a read-only file, because a creat() call
1565 * always implies file truncation.
1566 * ... but this isn't really fair. A process may reasonably call
1567 * ftruncate on an open file descriptor on a file with perm 000.
1568 * We must trust the client to do permission checking - using "ACCESS"
1571 if ((acc & MAY_OWNER_OVERRIDE) &&
1572 inode->i_uid == current->fsuid)
1575 err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
1577 /* Allow read access to binaries even when mode 111 */
1578 if (err == -EACCES && S_ISREG(inode->i_mode) &&
1579 acc == (MAY_READ | MAY_OWNER_OVERRIDE))
1580 err = permission(inode, MAY_EXEC);
1582 return err? nfserrno(err) : 0;
1586 nfsd_racache_shutdown(void)
1590 dprintk("nfsd: freeing readahead buffers.\n");
1592 raparm_cache = raparml = NULL;
1595 * Initialize readahead param cache
1598 nfsd_racache_init(int cache_size)
1604 raparml = kmalloc(sizeof(struct raparms) * cache_size, GFP_KERNEL);
1606 if (raparml != NULL) {
1607 dprintk("nfsd: allocating %d readahead buffers.\n",
1609 memset(raparml, 0, sizeof(struct raparms) * cache_size);
1610 for (i = 0; i < cache_size - 1; i++) {
1611 raparml[i].p_next = raparml + i + 1;
1613 raparm_cache = raparml;
1616 "nfsd: Could not allocate memory read-ahead cache.\n");
1619 nfsdstats.ra_size = cache_size;