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/mount.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>
33 #include <linux/pagemap.h>
35 #include <linux/module.h>
36 #include <linux/namei.h>
37 #include <linux/vfs.h>
38 #include <linux/sunrpc/svc.h>
39 #include <linux/nfsd/nfsd.h>
41 #include <linux/nfs3.h>
42 #include <linux/nfsd/xdr3.h>
43 #endif /* CONFIG_NFSD_V3 */
44 #include <linux/nfsd/nfsfh.h>
45 #include <linux/quotaops.h>
46 #include <linux/dnotify.h>
48 #include <asm/uaccess.h>
50 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
54 /* We must ignore files (but only files) which might have mandatory
55 * locks on them because there is no way to know if the accesser has
58 #define IS_ISMNDLK(i) (S_ISREG((i)->i_mode) && MANDATORY_LOCK(i))
61 * This is a cache of readahead params that help us choose the proper
62 * readahead strategy. Initially, we set all readahead parameters to 0
63 * and let the VFS handle things.
64 * If you increase the number of cached files very much, you'll need to
65 * add a hash table here.
68 struct raparms *p_next;
72 struct file_ra_state p_ra;
75 static struct raparms * raparml;
76 static struct raparms * raparm_cache;
79 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
81 * Returns -EAGAIN leaving *dpp and *expp unchanged,
82 * or nfs_ok having possibly changed *dpp and *expp
85 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
86 struct svc_export **expp)
88 struct svc_export *exp = *expp, *exp2 = NULL;
89 struct dentry *dentry = *dpp;
90 struct vfsmount *mnt = mntget(exp->ex_mnt);
91 struct dentry *mounts = dget(dentry);
94 while (follow_down(&mnt,&mounts)&&d_mountpoint(mounts));
96 exp2 = exp_get_by_name(exp->ex_client, mnt, mounts, &rqstp->rq_chandle);
103 if (exp2 && ((exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2))) {
104 /* successfully crossed mount point */
110 if (exp2) exp_put(exp2);
119 * Look up one component of a pathname.
120 * N.B. After this call _both_ fhp and resfh need an fh_put
122 * If the lookup would cross a mountpoint, and the mounted filesystem
123 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
124 * accepted as it stands and the mounted directory is
125 * returned. Otherwise the covered directory is returned.
126 * NOTE: this mountpoint crossing is not supported properly by all
127 * clients and is explicitly disallowed for NFSv3
128 * NeilBrown <neilb@cse.unsw.edu.au>
131 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
132 int len, struct svc_fh *resfh)
134 struct svc_export *exp;
135 struct dentry *dparent;
136 struct dentry *dentry;
139 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
141 /* Obtain dentry and export. */
142 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_EXEC);
146 dparent = fhp->fh_dentry;
147 exp = fhp->fh_export;
151 /* Lookup the name, but don't follow links */
152 if (isdotent(name, len)) {
154 dentry = dget(dparent);
155 else if (dparent != exp->ex_dentry) {
156 dentry = dget_parent(dparent);
157 } else if (!EX_NOHIDE(exp))
158 dentry = dget(dparent); /* .. == . just like at / */
160 /* checking mountpoint crossing is very different when stepping up */
161 struct svc_export *exp2 = NULL;
163 struct vfsmount *mnt = mntget(exp->ex_mnt);
164 dentry = dget(dparent);
165 while(dentry == mnt->mnt_root && follow_up(&mnt, &dentry))
167 dp = dget_parent(dentry);
171 exp2 = exp_parent(exp->ex_client, mnt, dentry,
181 dentry = dget(dparent);
190 dentry = lookup_one_len(name, dparent, len);
191 err = PTR_ERR(dentry);
195 * check if we have crossed a mount point ...
197 if (d_mountpoint(dentry)) {
198 if ((err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
205 * Note: we compose the file handle now, but as the
206 * dentry may be negative, it may need to be updated.
208 err = fh_compose(resfh, exp, dentry, fhp);
209 if (!err && !dentry->d_inode)
220 * Set various file attributes.
221 * N.B. After this call fhp needs an fh_put
224 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
225 int check_guard, time_t guardtime)
227 struct dentry *dentry;
229 int accmode = MAY_SATTR;
235 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
236 accmode |= MAY_WRITE|MAY_OWNER_OVERRIDE;
237 if (iap->ia_valid & ATTR_SIZE)
241 err = fh_verify(rqstp, fhp, ftype, accmode);
242 if (err || !iap->ia_valid)
245 dentry = fhp->fh_dentry;
246 inode = dentry->d_inode;
248 /* NFSv2 does not differentiate between "set-[ac]time-to-now"
249 * which only requires access, and "set-[ac]time-to-X" which
250 * requires ownership.
251 * So if it looks like it might be "set both to the same time which
252 * is close to now", and if inode_change_ok fails, then we
253 * convert to "set to now" instead of "set to explicit time"
255 * We only call inode_change_ok as the last test as technically
256 * it is not an interface that we should be using. It is only
257 * valid if the filesystem does not define it's own i_op->setattr.
259 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
260 #define MAX_TOUCH_TIME_ERROR (30*60)
261 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET
262 && iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec
264 /* Looks probable. Now just make sure time is in the right ballpark.
265 * Solaris, at least, doesn't seem to care what the time request is.
266 * We require it be within 30 minutes of now.
268 time_t delta = iap->ia_atime.tv_sec - get_seconds();
269 if (delta<0) delta = -delta;
270 if (delta < MAX_TOUCH_TIME_ERROR &&
271 inode_change_ok(inode, iap) != 0) {
272 /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
273 * this will cause notify_change to set these times to "now"
275 iap->ia_valid &= ~BOTH_TIME_SET;
279 /* The size case is special. It changes the file as well as the attributes. */
280 if (iap->ia_valid & ATTR_SIZE) {
281 if (iap->ia_size < inode->i_size) {
282 err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
288 * If we are changing the size of the file, then
289 * we need to break all leases.
291 err = break_lease(inode, FMODE_WRITE);
295 err = get_write_access(inode);
300 err = locks_verify_truncate(inode, NULL, iap->ia_size);
302 put_write_access(inode);
308 imode = inode->i_mode;
309 if (iap->ia_valid & ATTR_MODE) {
310 iap->ia_mode &= S_IALLUGO;
311 imode = iap->ia_mode |= (imode & ~S_IALLUGO);
314 /* Revoke setuid/setgid bit on chown/chgrp */
315 if ((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid)
316 iap->ia_valid |= ATTR_KILL_SUID;
317 if ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)
318 iap->ia_valid |= ATTR_KILL_SGID;
320 /* Change the attributes. */
322 iap->ia_valid |= ATTR_CTIME;
324 err = nfserr_notsync;
325 if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
327 err = notify_change(dentry, iap);
332 put_write_access(inode);
334 if (EX_ISSYNC(fhp->fh_export))
335 write_inode_now(inode, 1);
344 #ifdef CONFIG_NFSD_V3
346 * Check server access rights to a file system object
352 static struct accessmap nfs3_regaccess[] = {
353 { NFS3_ACCESS_READ, MAY_READ },
354 { NFS3_ACCESS_EXECUTE, MAY_EXEC },
355 { NFS3_ACCESS_MODIFY, MAY_WRITE|MAY_TRUNC },
356 { NFS3_ACCESS_EXTEND, MAY_WRITE },
361 static struct accessmap nfs3_diraccess[] = {
362 { NFS3_ACCESS_READ, MAY_READ },
363 { NFS3_ACCESS_LOOKUP, MAY_EXEC },
364 { NFS3_ACCESS_MODIFY, MAY_EXEC|MAY_WRITE|MAY_TRUNC },
365 { NFS3_ACCESS_EXTEND, MAY_EXEC|MAY_WRITE },
366 { NFS3_ACCESS_DELETE, MAY_REMOVE },
371 static struct accessmap nfs3_anyaccess[] = {
372 /* Some clients - Solaris 2.6 at least, make an access call
373 * to the server to check for access for things like /dev/null
374 * (which really, the server doesn't care about). So
375 * We provide simple access checking for them, looking
376 * mainly at mode bits
378 { NFS3_ACCESS_READ, MAY_READ },
379 { NFS3_ACCESS_EXECUTE, MAY_EXEC },
380 { NFS3_ACCESS_MODIFY, MAY_WRITE },
381 { NFS3_ACCESS_EXTEND, MAY_WRITE },
387 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
389 struct accessmap *map;
390 struct svc_export *export;
391 struct dentry *dentry;
392 u32 query, result = 0, sresult = 0;
395 error = fh_verify(rqstp, fhp, 0, MAY_NOP);
399 export = fhp->fh_export;
400 dentry = fhp->fh_dentry;
402 if (S_ISREG(dentry->d_inode->i_mode))
403 map = nfs3_regaccess;
404 else if (S_ISDIR(dentry->d_inode->i_mode))
405 map = nfs3_diraccess;
407 map = nfs3_anyaccess;
411 for (; map->access; map++) {
412 if (map->access & query) {
415 sresult |= map->access;
417 err2 = nfsd_permission(export, dentry, map->how);
420 result |= map->access;
423 /* the following error codes just mean the access was not allowed,
424 * rather than an error occurred */
428 /* simply don't "or" in the access bit. */
438 *supported = sresult;
443 #endif /* CONFIG_NFSD_V3 */
448 * Open an existing file or directory.
449 * The access argument indicates the type of open (read/write/lock)
450 * N.B. After this call fhp needs an fh_put
453 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
454 int access, struct file *filp)
456 struct dentry *dentry;
458 int flags = O_RDONLY|O_LARGEFILE, err;
461 * If we get here, then the client has already done an "open",
462 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
463 * in case a chmod has now revoked permission.
465 err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE);
469 dentry = fhp->fh_dentry;
470 inode = dentry->d_inode;
472 /* Disallow access to files with the append-only bit set or
473 * with mandatory locking enabled
476 if (IS_APPEND(inode) || IS_ISMNDLK(inode))
482 * Check to see if there are any leases on this file.
483 * This may block while leases are broken.
485 err = break_lease(inode, (access & MAY_WRITE) ? FMODE_WRITE : 0);
489 if (access & MAY_WRITE) {
490 err = get_write_access(inode);
494 flags = O_WRONLY|O_LARGEFILE;
499 err = open_private_file(filp, dentry, flags);
501 filp->f_vfsmnt = fhp->fh_export->ex_mnt;
502 } else if (access & MAY_WRITE)
503 put_write_access(inode);
516 nfsd_close(struct file *filp)
518 struct dentry *dentry = filp->f_dentry;
519 struct inode *inode = dentry->d_inode;
521 close_private_file(filp);
522 if (filp->f_mode & FMODE_WRITE)
523 put_write_access(inode);
528 * As this calls fsync (not fdatasync) there is no need for a write_inode
531 inline void nfsd_dosync(struct file *filp, struct dentry *dp,
532 struct file_operations *fop)
534 struct inode *inode = dp->d_inode;
535 int (*fsync) (struct file *, struct dentry *, int);
537 filemap_fdatawrite(inode->i_mapping);
538 if (fop && (fsync = fop->fsync))
540 filemap_fdatawait(inode->i_mapping);
545 nfsd_sync(struct file *filp)
547 struct inode *inode = filp->f_dentry->d_inode;
548 dprintk("nfsd: sync file %s\n", filp->f_dentry->d_name.name);
550 nfsd_dosync(filp, filp->f_dentry, filp->f_op);
555 nfsd_sync_dir(struct dentry *dp)
557 nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
561 * Obtain the readahead parameters for the file
562 * specified by (dev, ino).
564 static spinlock_t ra_lock = SPIN_LOCK_UNLOCKED;
566 static inline struct raparms *
567 nfsd_get_raparms(dev_t dev, ino_t ino)
569 struct raparms *ra, **rap, **frap = NULL;
573 for (rap = &raparm_cache; (ra = *rap); rap = &ra->p_next) {
574 if (ra->p_ino == ino && ra->p_dev == dev)
577 if (ra->p_count == 0)
580 depth = nfsdstats.ra_size*11/10;
582 spin_unlock(&ra_lock);
589 memset(&ra->p_ra, 0, sizeof(ra->p_ra));
591 if (rap != &raparm_cache) {
593 ra->p_next = raparm_cache;
597 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
598 spin_unlock(&ra_lock);
603 * Grab and keep cached pages assosiated with a file in the svc_rqst
604 * so that they can be passed to the netowork sendmsg/sendpage routines
605 * directrly. They will be released after the sending has completed.
608 nfsd_read_actor(read_descriptor_t *desc, struct page *page, unsigned long offset , unsigned long size)
610 unsigned long count = desc->count;
611 struct svc_rqst *rqstp = (struct svc_rqst *)desc->buf;
616 if (rqstp->rq_res.page_len == 0) {
618 rqstp->rq_respages[rqstp->rq_resused++] = page;
619 rqstp->rq_res.page_base = offset;
620 rqstp->rq_res.page_len = size;
621 } else if (page != rqstp->rq_respages[rqstp->rq_resused-1]) {
623 rqstp->rq_respages[rqstp->rq_resused++] = page;
624 rqstp->rq_res.page_len += size;
626 rqstp->rq_res.page_len += size;
629 desc->count = count - size;
630 desc->written += size;
635 * Read data from a file. count must contain the requested read count
636 * on entry. On return, *count contains the number of bytes actually read.
637 * N.B. After this call fhp needs an fh_put
640 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
641 struct iovec *vec, int vlen, unsigned long *count)
649 err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file);
653 inode = file.f_dentry->d_inode;
655 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
656 (!lock_may_read(inode, offset, *count)))
660 /* Get readahead parameters */
661 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
663 file.f_ra = ra->p_ra;
665 if (file.f_op->sendfile) {
666 svc_pushback_unused_pages(rqstp);
667 err = file.f_op->sendfile(&file, &offset, *count,
668 nfsd_read_actor, rqstp);
672 err = vfs_readv(&file, vec, vlen, &offset);
676 /* Write back readahead params */
678 ra->p_ra = file.f_ra;
681 nfsdstats.io_read += err;
684 dnotify_parent(file.f_dentry, DN_ACCESS);
694 * Write data to a file.
695 * The stable flag requests synchronous writes.
696 * N.B. After this call fhp needs an fh_put
699 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
700 struct iovec *vec, int vlen,
701 unsigned long cnt, int *stablep)
703 struct svc_export *exp;
705 struct dentry *dentry;
709 int stable = *stablep;
711 err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
719 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
720 (!lock_may_write(file.f_dentry->d_inode, offset, cnt)))
724 dentry = file.f_dentry;
725 inode = dentry->d_inode;
726 exp = fhp->fh_export;
729 * Request sync writes if
730 * - the sync export option has been set, or
731 * - the client requested O_SYNC behavior (NFSv3 feature).
732 * - The file system doesn't support fsync().
733 * When gathered writes have been configured for this volume,
734 * flushing the data to disk is handled separately below.
737 if (file.f_op->fsync == 0) {/* COMMIT3 cannot work */
739 *stablep = 2; /* FILE_SYNC */
744 if (stable && !EX_WGATHER(exp))
745 file.f_flags |= O_SYNC;
747 /* Write the data. */
748 oldfs = get_fs(); set_fs(KERNEL_DS);
749 err = vfs_writev(&file, vec, vlen, &offset);
752 nfsdstats.io_write += cnt;
753 dnotify_parent(file.f_dentry, DN_MODIFY);
756 /* clear setuid/setgid flag after write */
757 if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) {
759 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
762 notify_change(dentry, &ia);
766 if (err >= 0 && stable) {
767 static ino_t last_ino;
768 static dev_t last_dev;
771 * Gathered writes: If another process is currently
772 * writing to the file, there's a high chance
773 * this is another nfsd (triggered by a bulk write
774 * from a client's biod). Rather than syncing the
775 * file with each write request, we sleep for 10 msec.
777 * I don't know if this roughly approximates
778 * C. Juszak's idea of gathered writes, but it's a
779 * nice and simple solution (IMHO), and it seems to
782 if (EX_WGATHER(exp)) {
783 if (atomic_read(&inode->i_writecount) > 1
784 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
785 dprintk("nfsd: write defer %d\n", current->pid);
786 set_current_state(TASK_UNINTERRUPTIBLE);
787 schedule_timeout((HZ+99)/100);
788 dprintk("nfsd: write resume %d\n", current->pid);
791 if (inode->i_state & I_DIRTY) {
792 dprintk("nfsd: write sync %d\n", current->pid);
796 wake_up(&inode->i_wait);
799 last_ino = inode->i_ino;
800 last_dev = inode->i_sb->s_dev;
803 dprintk("nfsd: write complete err=%d\n", err);
815 #ifdef CONFIG_NFSD_V3
817 * Commit all pending writes to stable storage.
818 * Strictly speaking, we could sync just the indicated file region here,
819 * but there's currently no way we can ask the VFS to do so.
821 * Unfortunately we cannot lock the file to make sure we return full WCC
822 * data to the client, as locking happens lower down in the filesystem.
825 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
826 loff_t offset, unsigned long count)
831 if ((u64)count > ~(u64)offset)
834 if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
836 if (EX_ISSYNC(fhp->fh_export)) {
837 if (file.f_op && file.f_op->fsync) {
840 err = nfserr_notsupp;
847 #endif /* CONFIG_NFSD_V3 */
850 * Create a file (regular, directory, device, fifo); UNIX sockets
851 * not yet implemented.
852 * If the response fh has been verified, the parent directory should
853 * already be locked. Note that the parent directory is left locked.
855 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
858 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
859 char *fname, int flen, struct iattr *iap,
860 int type, dev_t rdev, struct svc_fh *resfhp)
862 struct dentry *dentry, *dchild;
870 if (isdotent(fname, flen))
873 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
877 dentry = fhp->fh_dentry;
878 dirp = dentry->d_inode;
881 if(!dirp->i_op || !dirp->i_op->lookup)
884 * Check whether the response file handle has been verified yet.
885 * If it has, the parent directory should already be locked.
887 if (!resfhp->fh_dentry) {
888 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
890 dchild = lookup_one_len(fname, dentry, flen);
891 err = PTR_ERR(dchild);
894 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
898 /* called from nfsd_proc_create */
899 dchild = resfhp->fh_dentry;
900 if (!fhp->fh_locked) {
901 /* not actually possible */
903 "nfsd_create: parent %s/%s not locked!\n",
904 dentry->d_parent->d_name.name,
905 dentry->d_name.name);
911 * Make sure the child dentry is still negative ...
914 if (dchild->d_inode) {
915 dprintk("nfsd_create: dentry %s/%s not negative!\n",
916 dentry->d_name.name, dchild->d_name.name);
920 if (!(iap->ia_valid & ATTR_MODE))
922 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
925 * Get the dir op function pointer.
930 err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
933 err = vfs_mkdir(dirp, dchild, iap->ia_mode);
939 err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
942 printk("nfsd: bad file type %o in nfsd_create\n", type);
948 if (EX_ISSYNC(fhp->fh_export)) {
949 nfsd_sync_dir(dentry);
950 write_inode_now(dchild->d_inode, 1);
954 /* Set file attributes. Mode has already been set and
955 * setting uid/gid works only for root. Irix appears to
956 * send along the gid when it tries to implement setgid
957 * directories via NFS.
960 if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0)
961 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
963 * Update the file handle to get the new inode info.
966 err = fh_update(resfhp);
975 #ifdef CONFIG_NFSD_V3
977 * NFSv3 version of nfsd_create
980 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
981 char *fname, int flen, struct iattr *iap,
982 struct svc_fh *resfhp, int createmode, u32 *verifier,
985 struct dentry *dentry, *dchild;
988 __u32 v_mtime=0, v_atime=0;
995 if (isdotent(fname, flen))
997 if (!(iap->ia_valid & ATTR_MODE))
999 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1003 dentry = fhp->fh_dentry;
1004 dirp = dentry->d_inode;
1006 /* Get all the sanity checks out of the way before
1007 * we lock the parent. */
1008 err = nfserr_notdir;
1009 if(!dirp->i_op || !dirp->i_op->lookup)
1014 * Compose the response file handle.
1016 dchild = lookup_one_len(fname, dentry, flen);
1017 err = PTR_ERR(dchild);
1021 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1025 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1026 /* while the verifier would fit in mtime+atime,
1027 * solaris7 gets confused (bugid 4218508) if these have
1028 * the high bit set, so we use the mode as well
1030 v_mtime = verifier[0]&0x7fffffff;
1031 v_atime = verifier[1]&0x7fffffff;
1033 | ((verifier[0]&0x80000000) >> (32-7)) /* u+x */
1034 | ((verifier[1]&0x80000000) >> (32-9)) /* u+r */
1038 if (dchild->d_inode) {
1041 switch (createmode) {
1042 case NFS3_CREATE_UNCHECKED:
1043 if (! S_ISREG(dchild->d_inode->i_mode))
1046 /* in nfsv4, we need to treat this case a little
1047 * differently. we don't want to truncate the
1048 * file now; this would be wrong if the OPEN
1049 * fails for some other reason. furthermore,
1050 * if the size is nonzero, we should ignore it
1051 * according to spec!
1053 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1056 iap->ia_valid &= ATTR_SIZE;
1060 case NFS3_CREATE_EXCLUSIVE:
1061 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1062 && dchild->d_inode->i_atime.tv_sec == v_atime
1063 && dchild->d_inode->i_mode == v_mode
1064 && dchild->d_inode->i_size == 0 )
1067 case NFS3_CREATE_GUARDED:
1073 err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1077 if (EX_ISSYNC(fhp->fh_export)) {
1078 nfsd_sync_dir(dentry);
1079 /* setattr will sync the child (or not) */
1083 * Update the filehandle to get the new inode info.
1085 err = fh_update(resfhp);
1089 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1090 /* Cram the verifier into atime/mtime/mode */
1091 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1092 | ATTR_MTIME_SET|ATTR_ATIME_SET
1094 /* XXX someone who knows this better please fix it for nsec */
1095 iap->ia_mtime.tv_sec = v_mtime;
1096 iap->ia_atime.tv_sec = v_atime;
1097 iap->ia_mtime.tv_nsec = 0;
1098 iap->ia_atime.tv_nsec = 0;
1099 iap->ia_mode = v_mode;
1102 /* Set file attributes.
1103 * Mode has already been set but we might need to reset it
1104 * for CREATE_EXCLUSIVE
1105 * Irix appears to send along the gid when it tries to
1106 * implement setgid directories via NFS. Clear out all that cruft.
1109 if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0)
1110 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1117 err = nfserrno(err);
1120 #endif /* CONFIG_NFSD_V3 */
1123 * Read a symlink. On entry, *lenp must contain the maximum path length that
1124 * fits into the buffer. On return, it contains the true length.
1125 * N.B. After this call fhp needs an fh_put
1128 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1130 struct dentry *dentry;
1131 struct inode *inode;
1135 err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
1139 dentry = fhp->fh_dentry;
1140 inode = dentry->d_inode;
1143 if (!inode->i_op || !inode->i_op->readlink)
1146 touch_atime(fhp->fh_export->ex_mnt, dentry);
1147 /* N.B. Why does this call need a get_fs()??
1148 * Remove the set_fs and watch the fireworks:-) --okir
1151 oldfs = get_fs(); set_fs(KERNEL_DS);
1152 err = inode->i_op->readlink(dentry, buf, *lenp);
1163 err = nfserrno(err);
1168 * Create a symlink and look up its inode
1169 * N.B. After this call _both_ fhp and resfhp need an fh_put
1172 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1173 char *fname, int flen,
1174 char *path, int plen,
1175 struct svc_fh *resfhp,
1178 struct dentry *dentry, *dnew;
1185 if (isdotent(fname, flen))
1188 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1192 dentry = fhp->fh_dentry;
1193 dnew = lookup_one_len(fname, dentry, flen);
1194 err = PTR_ERR(dnew);
1198 if (unlikely(path[plen] != 0)) {
1199 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1200 if (path_alloced == NULL)
1203 strncpy(path_alloced, path, plen);
1204 path_alloced[plen] = 0;
1205 err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
1206 kfree(path_alloced);
1209 err = vfs_symlink(dentry->d_inode, dnew, path);
1212 if (EX_ISSYNC(fhp->fh_export))
1213 nfsd_sync_dir(dentry);
1215 iap->ia_valid &= ATTR_MODE /* ~(ATTR_MODE|ATTR_UID|ATTR_GID)*/;
1216 if (iap->ia_valid) {
1217 iap->ia_valid |= ATTR_CTIME;
1218 iap->ia_mode = (iap->ia_mode&S_IALLUGO)
1220 err = notify_change(dnew, iap);
1222 err = nfserrno(err);
1223 else if (EX_ISSYNC(fhp->fh_export))
1224 write_inode_now(dentry->d_inode, 1);
1228 err = nfserrno(err);
1231 /* Compose the fh so the dentry will be freed ... */
1232 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1233 if (err==0) err = cerr;
1238 err = nfserrno(err);
1244 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1247 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1248 char *name, int len, struct svc_fh *tfhp)
1250 struct dentry *ddir, *dnew, *dold;
1251 struct inode *dirp, *dest;
1254 err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
1257 err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
1265 if (isdotent(name, len))
1269 ddir = ffhp->fh_dentry;
1270 dirp = ddir->d_inode;
1272 dnew = lookup_one_len(name, ddir, len);
1273 err = PTR_ERR(dnew);
1277 dold = tfhp->fh_dentry;
1278 dest = dold->d_inode;
1280 err = vfs_link(dold, dirp, dnew);
1282 if (EX_ISSYNC(ffhp->fh_export)) {
1283 nfsd_sync_dir(ddir);
1284 write_inode_now(dest, 1);
1287 if (err == -EXDEV && rqstp->rq_vers == 2)
1290 err = nfserrno(err);
1299 err = nfserrno(err);
1305 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1308 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1309 struct svc_fh *tfhp, char *tname, int tlen)
1311 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1312 struct inode *fdir, *tdir;
1315 err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
1318 err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
1322 fdentry = ffhp->fh_dentry;
1323 fdir = fdentry->d_inode;
1325 tdentry = tfhp->fh_dentry;
1326 tdir = tdentry->d_inode;
1328 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1329 if (fdir->i_sb != tdir->i_sb)
1333 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1336 /* cannot use fh_lock as we need deadlock protective ordering
1337 * so do it by hand */
1338 trap = lock_rename(tdentry, fdentry);
1339 ffhp->fh_locked = tfhp->fh_locked = 1;
1343 odentry = lookup_one_len(fname, fdentry, flen);
1344 err = PTR_ERR(odentry);
1345 if (IS_ERR(odentry))
1349 if (!odentry->d_inode)
1352 if (odentry == trap)
1355 ndentry = lookup_one_len(tname, tdentry, tlen);
1356 err = PTR_ERR(ndentry);
1357 if (IS_ERR(ndentry))
1360 if (ndentry == trap)
1364 if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1365 ((atomic_read(&odentry->d_count) > 1)
1366 || (atomic_read(&ndentry->d_count) > 1))) {
1370 err = vfs_rename(fdir, odentry, tdir, ndentry);
1371 if (!err && EX_ISSYNC(tfhp->fh_export)) {
1372 nfsd_sync_dir(tdentry);
1373 nfsd_sync_dir(fdentry);
1382 err = nfserrno(err);
1384 /* we cannot reply on fh_unlock on the two filehandles,
1385 * as that would do the wrong thing if the two directories
1386 * were the same, so again we do it by hand
1388 fill_post_wcc(ffhp);
1389 fill_post_wcc(tfhp);
1390 unlock_rename(tdentry, fdentry);
1391 ffhp->fh_locked = tfhp->fh_locked = 0;
1398 * Unlink a file or directory
1399 * N.B. After this call fhp needs an fh_put
1402 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1403 char *fname, int flen)
1405 struct dentry *dentry, *rdentry;
1410 if (!flen || isdotent(fname, flen))
1412 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
1417 dentry = fhp->fh_dentry;
1418 dirp = dentry->d_inode;
1420 rdentry = lookup_one_len(fname, dentry, flen);
1421 err = PTR_ERR(rdentry);
1422 if (IS_ERR(rdentry))
1425 if (!rdentry->d_inode) {
1432 type = rdentry->d_inode->i_mode & S_IFMT;
1434 if (type != S_IFDIR) { /* It's UNLINK */
1436 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1437 (atomic_read(&rdentry->d_count) > 1)) {
1441 err = vfs_unlink(dirp, rdentry);
1442 } else { /* It's RMDIR */
1443 err = vfs_rmdir(dirp, rdentry);
1450 if (EX_ISSYNC(fhp->fh_export))
1451 nfsd_sync_dir(dentry);
1457 err = nfserrno(err);
1462 * Read entries from a directory.
1463 * The NFSv3/4 verifier we ignore for now.
1466 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
1467 struct readdir_cd *cdp, encode_dent_fn func)
1471 loff_t offset = *offsetp;
1473 err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
1476 if (offset > ~(u32) 0)
1479 file.f_pos = offset;
1482 * Read the directory entries. This silly loop is necessary because
1483 * readdir() is not guaranteed to fill up the entire buffer, but
1484 * may choose to do less.
1488 cdp->err = nfserr_eof; /* will be cleared on successful read */
1489 err = vfs_readdir(&file, (filldir_t) func, cdp);
1490 } while (err >=0 && cdp->err == nfs_ok);
1492 err = nfserrno(err);
1495 *offsetp = file.f_pos;
1497 if (err == nfserr_eof || err == nfserr_toosmall)
1498 err = nfs_ok; /* can still be found in ->err */
1506 * Get file system stats
1507 * N.B. After this call fhp needs an fh_put
1510 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat)
1512 int err = fh_verify(rqstp, fhp, 0, MAY_NOP);
1513 if (!err && vfs_statfs(fhp->fh_dentry->d_inode->i_sb,stat))
1519 * Check for a user's access permissions to this inode.
1522 nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
1524 struct inode *inode = dentry->d_inode;
1530 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1532 (acc & MAY_READ)? " read" : "",
1533 (acc & MAY_WRITE)? " write" : "",
1534 (acc & MAY_EXEC)? " exec" : "",
1535 (acc & MAY_SATTR)? " sattr" : "",
1536 (acc & MAY_TRUNC)? " trunc" : "",
1537 (acc & MAY_LOCK)? " lock" : "",
1538 (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
1540 IS_IMMUTABLE(inode)? " immut" : "",
1541 IS_APPEND(inode)? " append" : "",
1542 IS_RDONLY(inode)? " ro" : "");
1543 dprintk(" owner %d/%d user %d/%d\n",
1544 inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
1547 /* Normally we reject any write/sattr etc access on a read-only file
1548 * system. But if it is IRIX doing check on write-access for a
1549 * device special file, we ignore rofs.
1551 if (!(acc & MAY_LOCAL_ACCESS))
1552 if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
1553 if (EX_RDONLY(exp) || IS_RDONLY(inode))
1555 if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
1558 if ((acc & MAY_TRUNC) && IS_APPEND(inode))
1561 if (acc & MAY_LOCK) {
1562 /* If we cannot rely on authentication in NLM requests,
1563 * just allow locks, otherwise require read permission, or
1566 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1569 acc = MAY_READ | MAY_OWNER_OVERRIDE;
1572 * The file owner always gets access permission for accesses that
1573 * would normally be checked at open time. This is to make
1574 * file access work even when the client has done a fchmod(fd, 0).
1576 * However, `cp foo bar' should fail nevertheless when bar is
1577 * readonly. A sensible way to do this might be to reject all
1578 * attempts to truncate a read-only file, because a creat() call
1579 * always implies file truncation.
1580 * ... but this isn't really fair. A process may reasonably call
1581 * ftruncate on an open file descriptor on a file with perm 000.
1582 * We must trust the client to do permission checking - using "ACCESS"
1585 if ((acc & MAY_OWNER_OVERRIDE) &&
1586 inode->i_uid == current->fsuid)
1589 err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC), NULL);
1591 /* Allow read access to binaries even when mode 111 */
1592 if (err == -EACCES && S_ISREG(inode->i_mode) &&
1593 acc == (MAY_READ | MAY_OWNER_OVERRIDE))
1594 err = permission(inode, MAY_EXEC, NULL);
1596 return err? nfserrno(err) : 0;
1600 nfsd_racache_shutdown(void)
1604 dprintk("nfsd: freeing readahead buffers.\n");
1606 raparm_cache = raparml = NULL;
1609 * Initialize readahead param cache
1612 nfsd_racache_init(int cache_size)
1618 raparml = kmalloc(sizeof(struct raparms) * cache_size, GFP_KERNEL);
1620 if (raparml != NULL) {
1621 dprintk("nfsd: allocating %d readahead buffers.\n",
1623 memset(raparml, 0, sizeof(struct raparms) * cache_size);
1624 for (i = 0; i < cache_size - 1; i++) {
1625 raparml[i].p_next = raparml + i + 1;
1627 raparm_cache = raparml;
1630 "nfsd: Could not allocate memory read-ahead cache.\n");
1633 nfsdstats.ra_size = cache_size;