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;
152 /* Lookup the name, but don't follow links */
153 if (isdotent(name, len)) {
155 dentry = dget(dparent);
156 else if (dparent != exp->ex_dentry) {
157 dentry = dget_parent(dparent);
158 } else if (!EX_NOHIDE(exp))
159 dentry = dget(dparent); /* .. == . just like at / */
161 /* checking mountpoint crossing is very different when stepping up */
162 struct svc_export *exp2 = NULL;
164 struct vfsmount *mnt = mntget(exp->ex_mnt);
165 dentry = dget(dparent);
166 while(dentry == mnt->mnt_root && follow_up(&mnt, &dentry))
168 dp = dget_parent(dentry);
172 exp2 = exp_parent(exp->ex_client, mnt, dentry,
182 dentry = dget(dparent);
191 dentry = lookup_one_len(name, dparent, len);
192 err = PTR_ERR(dentry);
196 * check if we have crossed a mount point ...
198 if (d_mountpoint(dentry)) {
199 if ((err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
206 * Note: we compose the file handle now, but as the
207 * dentry may be negative, it may need to be updated.
209 err = fh_compose(resfh, exp, dentry, fhp);
210 if (!err && !dentry->d_inode)
223 * Set various file attributes.
224 * N.B. After this call fhp needs an fh_put
227 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
228 int check_guard, time_t guardtime)
230 struct dentry *dentry;
232 int accmode = MAY_SATTR;
238 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
239 accmode |= MAY_WRITE|MAY_OWNER_OVERRIDE;
240 if (iap->ia_valid & ATTR_SIZE)
244 err = fh_verify(rqstp, fhp, ftype, accmode);
245 if (err || !iap->ia_valid)
248 dentry = fhp->fh_dentry;
249 inode = dentry->d_inode;
251 /* NFSv2 does not differentiate between "set-[ac]time-to-now"
252 * which only requires access, and "set-[ac]time-to-X" which
253 * requires ownership.
254 * So if it looks like it might be "set both to the same time which
255 * is close to now", and if inode_change_ok fails, then we
256 * convert to "set to now" instead of "set to explicit time"
258 * We only call inode_change_ok as the last test as technically
259 * it is not an interface that we should be using. It is only
260 * valid if the filesystem does not define it's own i_op->setattr.
262 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
263 #define MAX_TOUCH_TIME_ERROR (30*60)
264 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET
265 && iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec
267 /* Looks probable. Now just make sure time is in the right ballpark.
268 * Solaris, at least, doesn't seem to care what the time request is.
269 * We require it be within 30 minutes of now.
271 time_t delta = iap->ia_atime.tv_sec - get_seconds();
272 if (delta<0) delta = -delta;
273 if (delta < MAX_TOUCH_TIME_ERROR &&
274 inode_change_ok(inode, iap) != 0) {
275 /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
276 * this will cause notify_change to set these times to "now"
278 iap->ia_valid &= ~BOTH_TIME_SET;
282 /* The size case is special. It changes the file as well as the attributes. */
283 if (iap->ia_valid & ATTR_SIZE) {
284 if (iap->ia_size < inode->i_size) {
285 err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
291 * If we are changing the size of the file, then
292 * we need to break all leases.
294 err = break_lease(inode, FMODE_WRITE);
298 err = get_write_access(inode);
303 err = locks_verify_truncate(inode, NULL, iap->ia_size);
305 put_write_access(inode);
311 imode = inode->i_mode;
312 if (iap->ia_valid & ATTR_MODE) {
313 iap->ia_mode &= S_IALLUGO;
314 imode = iap->ia_mode |= (imode & ~S_IALLUGO);
317 /* Revoke setuid/setgid bit on chown/chgrp */
318 if ((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid)
319 iap->ia_valid |= ATTR_KILL_SUID;
320 if ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)
321 iap->ia_valid |= ATTR_KILL_SGID;
323 /* Change the attributes. */
325 iap->ia_valid |= ATTR_CTIME;
327 err = nfserr_notsync;
328 if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
330 err = notify_change(dentry, iap);
335 put_write_access(inode);
337 if (EX_ISSYNC(fhp->fh_export))
338 write_inode_now(inode, 1);
347 #ifdef CONFIG_NFSD_V3
349 * Check server access rights to a file system object
355 static struct accessmap nfs3_regaccess[] = {
356 { NFS3_ACCESS_READ, MAY_READ },
357 { NFS3_ACCESS_EXECUTE, MAY_EXEC },
358 { NFS3_ACCESS_MODIFY, MAY_WRITE|MAY_TRUNC },
359 { NFS3_ACCESS_EXTEND, MAY_WRITE },
364 static struct accessmap nfs3_diraccess[] = {
365 { NFS3_ACCESS_READ, MAY_READ },
366 { NFS3_ACCESS_LOOKUP, MAY_EXEC },
367 { NFS3_ACCESS_MODIFY, MAY_EXEC|MAY_WRITE|MAY_TRUNC },
368 { NFS3_ACCESS_EXTEND, MAY_EXEC|MAY_WRITE },
369 { NFS3_ACCESS_DELETE, MAY_REMOVE },
374 static struct accessmap nfs3_anyaccess[] = {
375 /* Some clients - Solaris 2.6 at least, make an access call
376 * to the server to check for access for things like /dev/null
377 * (which really, the server doesn't care about). So
378 * We provide simple access checking for them, looking
379 * mainly at mode bits
381 { NFS3_ACCESS_READ, MAY_READ },
382 { NFS3_ACCESS_EXECUTE, MAY_EXEC },
383 { NFS3_ACCESS_MODIFY, MAY_WRITE },
384 { NFS3_ACCESS_EXTEND, MAY_WRITE },
390 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
392 struct accessmap *map;
393 struct svc_export *export;
394 struct dentry *dentry;
395 u32 query, result = 0, sresult = 0;
398 error = fh_verify(rqstp, fhp, 0, MAY_NOP);
402 export = fhp->fh_export;
403 dentry = fhp->fh_dentry;
405 if (S_ISREG(dentry->d_inode->i_mode))
406 map = nfs3_regaccess;
407 else if (S_ISDIR(dentry->d_inode->i_mode))
408 map = nfs3_diraccess;
410 map = nfs3_anyaccess;
414 for (; map->access; map++) {
415 if (map->access & query) {
418 sresult |= map->access;
420 err2 = nfsd_permission(export, dentry, map->how);
423 result |= map->access;
426 /* the following error codes just mean the access was not allowed,
427 * rather than an error occurred */
431 /* simply don't "or" in the access bit. */
441 *supported = sresult;
446 #endif /* CONFIG_NFSD_V3 */
451 * Open an existing file or directory.
452 * The access argument indicates the type of open (read/write/lock)
453 * N.B. After this call fhp needs an fh_put
456 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
457 int access, struct file *filp)
459 struct dentry *dentry;
461 int flags = O_RDONLY|O_LARGEFILE, err;
464 * If we get here, then the client has already done an "open",
465 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
466 * in case a chmod has now revoked permission.
468 err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE);
472 dentry = fhp->fh_dentry;
473 inode = dentry->d_inode;
475 /* Disallow access to files with the append-only bit set or
476 * with mandatory locking enabled
479 if (IS_APPEND(inode) || IS_ISMNDLK(inode))
485 * Check to see if there are any leases on this file.
486 * This may block while leases are broken.
488 err = break_lease(inode, (access & MAY_WRITE) ? FMODE_WRITE : 0);
492 if (access & MAY_WRITE) {
493 err = get_write_access(inode);
497 flags = O_WRONLY|O_LARGEFILE;
502 err = open_private_file(filp, dentry, flags);
504 filp->f_vfsmnt = fhp->fh_export->ex_mnt;
505 } else if (access & MAY_WRITE)
506 put_write_access(inode);
519 nfsd_close(struct file *filp)
521 struct dentry *dentry = filp->f_dentry;
522 struct inode *inode = dentry->d_inode;
524 close_private_file(filp);
525 if (filp->f_mode & FMODE_WRITE)
526 put_write_access(inode);
531 * As this calls fsync (not fdatasync) there is no need for a write_inode
534 inline void nfsd_dosync(struct file *filp, struct dentry *dp,
535 struct file_operations *fop)
537 struct inode *inode = dp->d_inode;
538 int (*fsync) (struct file *, struct dentry *, int);
540 filemap_fdatawrite(inode->i_mapping);
541 if (fop && (fsync = fop->fsync))
543 filemap_fdatawait(inode->i_mapping);
548 nfsd_sync(struct file *filp)
550 struct inode *inode = filp->f_dentry->d_inode;
551 dprintk("nfsd: sync file %s\n", filp->f_dentry->d_name.name);
553 nfsd_dosync(filp, filp->f_dentry, filp->f_op);
558 nfsd_sync_dir(struct dentry *dp)
560 nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
564 * Obtain the readahead parameters for the file
565 * specified by (dev, ino).
567 static spinlock_t ra_lock = SPIN_LOCK_UNLOCKED;
569 static inline struct raparms *
570 nfsd_get_raparms(dev_t dev, ino_t ino, struct address_space *mapping)
572 struct raparms *ra, **rap, **frap = NULL;
576 for (rap = &raparm_cache; (ra = *rap); rap = &ra->p_next) {
577 if (ra->p_ino == ino && ra->p_dev == dev)
580 if (ra->p_count == 0)
583 depth = nfsdstats.ra_size*11/10;
585 spin_unlock(&ra_lock);
592 file_ra_state_init(&ra->p_ra, mapping);
594 if (rap != &raparm_cache) {
596 ra->p_next = raparm_cache;
600 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
601 spin_unlock(&ra_lock);
606 * Grab and keep cached pages assosiated with a file in the svc_rqst
607 * so that they can be passed to the netowork sendmsg/sendpage routines
608 * directrly. They will be released after the sending has completed.
611 nfsd_read_actor(read_descriptor_t *desc, struct page *page, unsigned long offset , unsigned long size)
613 unsigned long count = desc->count;
614 struct svc_rqst *rqstp = (struct svc_rqst *)desc->buf;
619 if (rqstp->rq_res.page_len == 0) {
621 rqstp->rq_respages[rqstp->rq_resused++] = page;
622 rqstp->rq_res.page_base = offset;
623 rqstp->rq_res.page_len = size;
624 } else if (page != rqstp->rq_respages[rqstp->rq_resused-1]) {
626 rqstp->rq_respages[rqstp->rq_resused++] = page;
627 rqstp->rq_res.page_len += size;
629 rqstp->rq_res.page_len += size;
632 desc->count = count - size;
633 desc->written += size;
638 * Read data from a file. count must contain the requested read count
639 * on entry. On return, *count contains the number of bytes actually read.
640 * N.B. After this call fhp needs an fh_put
643 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
644 struct iovec *vec, int vlen, unsigned long *count)
652 err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file);
656 inode = file.f_dentry->d_inode;
658 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
659 (!lock_may_read(inode, offset, *count)))
663 /* Get readahead parameters */
664 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino,
665 inode->i_mapping->host->i_mapping);
667 file.f_ra = ra->p_ra;
669 if (file.f_op->sendfile) {
670 svc_pushback_unused_pages(rqstp);
671 err = file.f_op->sendfile(&file, &offset, *count,
672 nfsd_read_actor, rqstp);
676 err = vfs_readv(&file, vec, vlen, &offset);
680 /* Write back readahead params */
683 ra->p_ra = file.f_ra;
685 spin_unlock(&ra_lock);
688 nfsdstats.io_read += err;
691 dnotify_parent(file.f_dentry, DN_ACCESS);
701 * Write data to a file.
702 * The stable flag requests synchronous writes.
703 * N.B. After this call fhp needs an fh_put
706 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
707 struct iovec *vec, int vlen,
708 unsigned long cnt, int *stablep)
710 struct svc_export *exp;
712 struct dentry *dentry;
716 int stable = *stablep;
718 err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
726 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
727 (!lock_may_write(file.f_dentry->d_inode, offset, cnt)))
731 dentry = file.f_dentry;
732 inode = dentry->d_inode;
733 exp = fhp->fh_export;
736 * Request sync writes if
737 * - the sync export option has been set, or
738 * - the client requested O_SYNC behavior (NFSv3 feature).
739 * - The file system doesn't support fsync().
740 * When gathered writes have been configured for this volume,
741 * flushing the data to disk is handled separately below.
744 if (file.f_op->fsync == 0) {/* COMMIT3 cannot work */
746 *stablep = 2; /* FILE_SYNC */
751 if (stable && !EX_WGATHER(exp))
752 file.f_flags |= O_SYNC;
754 /* Write the data. */
755 oldfs = get_fs(); set_fs(KERNEL_DS);
756 err = vfs_writev(&file, vec, vlen, &offset);
759 nfsdstats.io_write += cnt;
760 dnotify_parent(file.f_dentry, DN_MODIFY);
763 /* clear setuid/setgid flag after write */
764 if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) {
766 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
769 notify_change(dentry, &ia);
773 if (err >= 0 && stable) {
774 static ino_t last_ino;
775 static dev_t last_dev;
778 * Gathered writes: If another process is currently
779 * writing to the file, there's a high chance
780 * this is another nfsd (triggered by a bulk write
781 * from a client's biod). Rather than syncing the
782 * file with each write request, we sleep for 10 msec.
784 * I don't know if this roughly approximates
785 * C. Juszak's idea of gathered writes, but it's a
786 * nice and simple solution (IMHO), and it seems to
789 if (EX_WGATHER(exp)) {
790 if (atomic_read(&inode->i_writecount) > 1
791 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
792 dprintk("nfsd: write defer %d\n", current->pid);
793 set_current_state(TASK_UNINTERRUPTIBLE);
794 schedule_timeout((HZ+99)/100);
795 dprintk("nfsd: write resume %d\n", current->pid);
798 if (inode->i_state & I_DIRTY) {
799 dprintk("nfsd: write sync %d\n", current->pid);
803 wake_up(&inode->i_wait);
806 last_ino = inode->i_ino;
807 last_dev = inode->i_sb->s_dev;
810 dprintk("nfsd: write complete err=%d\n", err);
822 #ifdef CONFIG_NFSD_V3
824 * Commit all pending writes to stable storage.
825 * Strictly speaking, we could sync just the indicated file region here,
826 * but there's currently no way we can ask the VFS to do so.
828 * Unfortunately we cannot lock the file to make sure we return full WCC
829 * data to the client, as locking happens lower down in the filesystem.
832 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
833 loff_t offset, unsigned long count)
838 if ((u64)count > ~(u64)offset)
841 if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
843 if (EX_ISSYNC(fhp->fh_export)) {
844 if (file.f_op && file.f_op->fsync) {
847 err = nfserr_notsupp;
854 #endif /* CONFIG_NFSD_V3 */
857 * Create a file (regular, directory, device, fifo); UNIX sockets
858 * not yet implemented.
859 * If the response fh has been verified, the parent directory should
860 * already be locked. Note that the parent directory is left locked.
862 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
865 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
866 char *fname, int flen, struct iattr *iap,
867 int type, dev_t rdev, struct svc_fh *resfhp)
869 struct dentry *dentry, *dchild = NULL;
877 if (isdotent(fname, flen))
880 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
884 dentry = fhp->fh_dentry;
885 dirp = dentry->d_inode;
888 if(!dirp->i_op || !dirp->i_op->lookup)
891 * Check whether the response file handle has been verified yet.
892 * If it has, the parent directory should already be locked.
894 if (!resfhp->fh_dentry) {
895 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
897 dchild = lookup_one_len(fname, dentry, flen);
898 err = PTR_ERR(dchild);
901 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
905 /* called from nfsd_proc_create */
906 dchild = dget(resfhp->fh_dentry);
907 if (!fhp->fh_locked) {
908 /* not actually possible */
910 "nfsd_create: parent %s/%s not locked!\n",
911 dentry->d_parent->d_name.name,
912 dentry->d_name.name);
918 * Make sure the child dentry is still negative ...
921 if (dchild->d_inode) {
922 dprintk("nfsd_create: dentry %s/%s not negative!\n",
923 dentry->d_name.name, dchild->d_name.name);
927 if (!(iap->ia_valid & ATTR_MODE))
929 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
932 * Get the dir op function pointer.
937 err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
940 err = vfs_mkdir(dirp, dchild, iap->ia_mode);
946 err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
949 printk("nfsd: bad file type %o in nfsd_create\n", type);
955 if (EX_ISSYNC(fhp->fh_export)) {
956 nfsd_sync_dir(dentry);
957 write_inode_now(dchild->d_inode, 1);
961 /* Set file attributes. Mode has already been set and
962 * setting uid/gid works only for root. Irix appears to
963 * send along the gid when it tries to implement setgid
964 * directories via NFS.
967 if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0)
968 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
970 * Update the file handle to get the new inode info.
973 err = fh_update(resfhp);
975 if (dchild && !IS_ERR(dchild))
984 #ifdef CONFIG_NFSD_V3
986 * NFSv3 version of nfsd_create
989 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
990 char *fname, int flen, struct iattr *iap,
991 struct svc_fh *resfhp, int createmode, u32 *verifier,
994 struct dentry *dentry, *dchild = NULL;
997 __u32 v_mtime=0, v_atime=0;
1004 if (isdotent(fname, flen))
1006 if (!(iap->ia_valid & ATTR_MODE))
1008 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1012 dentry = fhp->fh_dentry;
1013 dirp = dentry->d_inode;
1015 /* Get all the sanity checks out of the way before
1016 * we lock the parent. */
1017 err = nfserr_notdir;
1018 if(!dirp->i_op || !dirp->i_op->lookup)
1023 * Compose the response file handle.
1025 dchild = lookup_one_len(fname, dentry, flen);
1026 err = PTR_ERR(dchild);
1030 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1034 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1035 /* while the verifier would fit in mtime+atime,
1036 * solaris7 gets confused (bugid 4218508) if these have
1037 * the high bit set, so we use the mode as well
1039 v_mtime = verifier[0]&0x7fffffff;
1040 v_atime = verifier[1]&0x7fffffff;
1042 | ((verifier[0]&0x80000000) >> (32-7)) /* u+x */
1043 | ((verifier[1]&0x80000000) >> (32-9)) /* u+r */
1047 if (dchild->d_inode) {
1050 switch (createmode) {
1051 case NFS3_CREATE_UNCHECKED:
1052 if (! S_ISREG(dchild->d_inode->i_mode))
1055 /* in nfsv4, we need to treat this case a little
1056 * differently. we don't want to truncate the
1057 * file now; this would be wrong if the OPEN
1058 * fails for some other reason. furthermore,
1059 * if the size is nonzero, we should ignore it
1060 * according to spec!
1062 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1065 iap->ia_valid &= ATTR_SIZE;
1069 case NFS3_CREATE_EXCLUSIVE:
1070 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1071 && dchild->d_inode->i_atime.tv_sec == v_atime
1072 && dchild->d_inode->i_mode == v_mode
1073 && dchild->d_inode->i_size == 0 )
1076 case NFS3_CREATE_GUARDED:
1082 err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1086 if (EX_ISSYNC(fhp->fh_export)) {
1087 nfsd_sync_dir(dentry);
1088 /* setattr will sync the child (or not) */
1092 * Update the filehandle to get the new inode info.
1094 err = fh_update(resfhp);
1098 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1099 /* Cram the verifier into atime/mtime/mode */
1100 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1101 | ATTR_MTIME_SET|ATTR_ATIME_SET
1103 /* XXX someone who knows this better please fix it for nsec */
1104 iap->ia_mtime.tv_sec = v_mtime;
1105 iap->ia_atime.tv_sec = v_atime;
1106 iap->ia_mtime.tv_nsec = 0;
1107 iap->ia_atime.tv_nsec = 0;
1108 iap->ia_mode = v_mode;
1111 /* Set file attributes.
1112 * Mode has already been set but we might need to reset it
1113 * for CREATE_EXCLUSIVE
1114 * Irix appears to send along the gid when it tries to
1115 * implement setgid directories via NFS. Clear out all that cruft.
1118 if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0)
1119 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1123 if (dchild && !IS_ERR(dchild))
1128 err = nfserrno(err);
1131 #endif /* CONFIG_NFSD_V3 */
1134 * Read a symlink. On entry, *lenp must contain the maximum path length that
1135 * fits into the buffer. On return, it contains the true length.
1136 * N.B. After this call fhp needs an fh_put
1139 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1141 struct dentry *dentry;
1142 struct inode *inode;
1146 err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
1150 dentry = fhp->fh_dentry;
1151 inode = dentry->d_inode;
1154 if (!inode->i_op || !inode->i_op->readlink)
1157 touch_atime(fhp->fh_export->ex_mnt, dentry);
1158 /* N.B. Why does this call need a get_fs()??
1159 * Remove the set_fs and watch the fireworks:-) --okir
1162 oldfs = get_fs(); set_fs(KERNEL_DS);
1163 err = inode->i_op->readlink(dentry, buf, *lenp);
1174 err = nfserrno(err);
1179 * Create a symlink and look up its inode
1180 * N.B. After this call _both_ fhp and resfhp need an fh_put
1183 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1184 char *fname, int flen,
1185 char *path, int plen,
1186 struct svc_fh *resfhp,
1189 struct dentry *dentry, *dnew;
1197 if (isdotent(fname, flen))
1200 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1204 dentry = fhp->fh_dentry;
1205 dnew = lookup_one_len(fname, dentry, flen);
1206 err = PTR_ERR(dnew);
1211 /* Only the MODE ATTRibute is even vaguely meaningful */
1212 if (iap && (iap->ia_valid & ATTR_MODE))
1213 mode = iap->ia_mode & S_IALLUGO;
1215 if (unlikely(path[plen] != 0)) {
1216 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1217 if (path_alloced == NULL)
1220 strncpy(path_alloced, path, plen);
1221 path_alloced[plen] = 0;
1222 err = vfs_symlink(dentry->d_inode, dnew, path_alloced, mode);
1223 kfree(path_alloced);
1226 err = vfs_symlink(dentry->d_inode, dnew, path, mode);
1229 if (EX_ISSYNC(fhp->fh_export))
1230 nfsd_sync_dir(dentry);
1232 err = nfserrno(err);
1235 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1237 if (err==0) err = cerr;
1242 err = nfserrno(err);
1248 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1251 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1252 char *name, int len, struct svc_fh *tfhp)
1254 struct dentry *ddir, *dnew, *dold;
1255 struct inode *dirp, *dest;
1258 err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
1261 err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
1269 if (isdotent(name, len))
1273 ddir = ffhp->fh_dentry;
1274 dirp = ddir->d_inode;
1276 dnew = lookup_one_len(name, ddir, len);
1277 err = PTR_ERR(dnew);
1281 dold = tfhp->fh_dentry;
1282 dest = dold->d_inode;
1284 err = vfs_link(dold, dirp, dnew);
1286 if (EX_ISSYNC(ffhp->fh_export)) {
1287 nfsd_sync_dir(ddir);
1288 write_inode_now(dest, 1);
1291 if (err == -EXDEV && rqstp->rq_vers == 2)
1294 err = nfserrno(err);
1303 err = nfserrno(err);
1309 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1312 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1313 struct svc_fh *tfhp, char *tname, int tlen)
1315 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1316 struct inode *fdir, *tdir;
1319 err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
1322 err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
1326 fdentry = ffhp->fh_dentry;
1327 fdir = fdentry->d_inode;
1329 tdentry = tfhp->fh_dentry;
1330 tdir = tdentry->d_inode;
1332 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1333 if (fdir->i_sb != tdir->i_sb)
1337 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1340 /* cannot use fh_lock as we need deadlock protective ordering
1341 * so do it by hand */
1342 trap = lock_rename(tdentry, fdentry);
1343 ffhp->fh_locked = tfhp->fh_locked = 1;
1347 odentry = lookup_one_len(fname, fdentry, flen);
1348 err = PTR_ERR(odentry);
1349 if (IS_ERR(odentry))
1353 if (!odentry->d_inode)
1356 if (odentry == trap)
1359 ndentry = lookup_one_len(tname, tdentry, tlen);
1360 err = PTR_ERR(ndentry);
1361 if (IS_ERR(ndentry))
1364 if (ndentry == trap)
1368 if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1369 ((atomic_read(&odentry->d_count) > 1)
1370 || (atomic_read(&ndentry->d_count) > 1))) {
1374 err = vfs_rename(fdir, odentry, tdir, ndentry);
1375 if (!err && EX_ISSYNC(tfhp->fh_export)) {
1376 nfsd_sync_dir(tdentry);
1377 nfsd_sync_dir(fdentry);
1386 err = nfserrno(err);
1388 /* we cannot reply on fh_unlock on the two filehandles,
1389 * as that would do the wrong thing if the two directories
1390 * were the same, so again we do it by hand
1392 fill_post_wcc(ffhp);
1393 fill_post_wcc(tfhp);
1394 unlock_rename(tdentry, fdentry);
1395 ffhp->fh_locked = tfhp->fh_locked = 0;
1402 * Unlink a file or directory
1403 * N.B. After this call fhp needs an fh_put
1406 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1407 char *fname, int flen)
1409 struct dentry *dentry, *rdentry;
1414 if (!flen || isdotent(fname, flen))
1416 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
1421 dentry = fhp->fh_dentry;
1422 dirp = dentry->d_inode;
1424 rdentry = lookup_one_len(fname, dentry, flen);
1425 err = PTR_ERR(rdentry);
1426 if (IS_ERR(rdentry))
1429 if (!rdentry->d_inode) {
1436 type = rdentry->d_inode->i_mode & S_IFMT;
1438 if (type != S_IFDIR) { /* It's UNLINK */
1440 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1441 (atomic_read(&rdentry->d_count) > 1)) {
1445 err = vfs_unlink(dirp, rdentry);
1446 } else { /* It's RMDIR */
1447 err = vfs_rmdir(dirp, rdentry);
1454 if (EX_ISSYNC(fhp->fh_export))
1455 nfsd_sync_dir(dentry);
1461 err = nfserrno(err);
1466 * Read entries from a directory.
1467 * The NFSv3/4 verifier we ignore for now.
1470 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
1471 struct readdir_cd *cdp, encode_dent_fn func)
1475 loff_t offset = *offsetp;
1477 err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
1480 if (offset > ~(u32) 0)
1483 file.f_pos = offset;
1486 * Read the directory entries. This silly loop is necessary because
1487 * readdir() is not guaranteed to fill up the entire buffer, but
1488 * may choose to do less.
1492 cdp->err = nfserr_eof; /* will be cleared on successful read */
1493 err = vfs_readdir(&file, (filldir_t) func, cdp);
1494 } while (err >=0 && cdp->err == nfs_ok);
1496 err = nfserrno(err);
1499 *offsetp = file.f_pos;
1501 if (err == nfserr_eof || err == nfserr_toosmall)
1502 err = nfs_ok; /* can still be found in ->err */
1510 * Get file system stats
1511 * N.B. After this call fhp needs an fh_put
1514 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat)
1516 int err = fh_verify(rqstp, fhp, 0, MAY_NOP);
1518 if (!err && vfs_statfs(fhp->fh_dentry->d_inode->i_sb,stat))
1524 * Check for a user's access permissions to this inode.
1527 nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
1529 struct inode *inode = dentry->d_inode;
1535 printk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1537 (acc & MAY_READ)? " read" : "",
1538 (acc & MAY_WRITE)? " write" : "",
1539 (acc & MAY_EXEC)? " exec" : "",
1540 (acc & MAY_SATTR)? " sattr" : "",
1541 (acc & MAY_TRUNC)? " trunc" : "",
1542 (acc & MAY_LOCK)? " lock" : "",
1543 (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
1545 IS_IMMUTABLE(inode)? " immut" : "",
1546 IS_APPEND(inode)? " append" : "",
1547 IS_RDONLY(inode)? " ro" : "");
1548 printk(" owner %d/%d user %d/%d\n",
1549 inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
1552 /* Normally we reject any write/sattr etc access on a read-only file
1553 * system. But if it is IRIX doing check on write-access for a
1554 * device special file, we ignore rofs.
1556 if (!(acc & MAY_LOCAL_ACCESS))
1557 if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
1558 if (EX_RDONLY(exp) || IS_RDONLY(inode))
1560 if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
1563 if ((acc & MAY_TRUNC) && IS_APPEND(inode))
1566 if (acc & MAY_LOCK) {
1567 /* If we cannot rely on authentication in NLM requests,
1568 * just allow locks, otherwise require read permission, or
1571 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1574 acc = MAY_READ | MAY_OWNER_OVERRIDE;
1577 * The file owner always gets access permission for accesses that
1578 * would normally be checked at open time. This is to make
1579 * file access work even when the client has done a fchmod(fd, 0).
1581 * However, `cp foo bar' should fail nevertheless when bar is
1582 * readonly. A sensible way to do this might be to reject all
1583 * attempts to truncate a read-only file, because a creat() call
1584 * always implies file truncation.
1585 * ... but this isn't really fair. A process may reasonably call
1586 * ftruncate on an open file descriptor on a file with perm 000.
1587 * We must trust the client to do permission checking - using "ACCESS"
1590 if ((acc & MAY_OWNER_OVERRIDE) &&
1591 inode->i_uid == current->fsuid)
1594 err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC), NULL);
1596 /* Allow read access to binaries even when mode 111 */
1597 if (err == -EACCES && S_ISREG(inode->i_mode) &&
1598 acc == (MAY_READ | MAY_OWNER_OVERRIDE))
1599 err = permission(inode, MAY_EXEC, NULL);
1601 return err? nfserrno(err) : 0;
1605 nfsd_racache_shutdown(void)
1609 dprintk("nfsd: freeing readahead buffers.\n");
1611 raparm_cache = raparml = NULL;
1614 * Initialize readahead param cache
1617 nfsd_racache_init(int cache_size)
1623 raparml = kmalloc(sizeof(struct raparms) * cache_size, GFP_KERNEL);
1625 if (raparml != NULL) {
1626 dprintk("nfsd: allocating %d readahead buffers.\n",
1628 memset(raparml, 0, sizeof(struct raparms) * cache_size);
1629 for (i = 0; i < cache_size - 1; i++) {
1630 raparml[i].p_next = raparml + i + 1;
1632 raparm_cache = raparml;
1635 "nfsd: Could not allocate memory read-ahead cache.\n");
1638 nfsdstats.ra_size = cache_size;