4 * File operations used by nfsd. Some of these have been ripped from
5 * other parts of the kernel because they weren't in ksyms.c, others
6 * are partial duplicates with added or changed functionality.
8 * Note that several functions dget() the dentry upon which they want
9 * to act, most notably those that create directory entries. Response
10 * dentry's are dput()'d if necessary in the release callback.
11 * So if you notice code paths that apparently fail to dput() the
12 * dentry, don't worry--they have been taken care of.
14 * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
17 #include <linux/config.h>
18 #include <linux/version.h>
19 #include <linux/string.h>
20 #include <linux/sched.h>
21 #include <linux/errno.h>
22 #include <linux/locks.h>
24 #include <linux/major.h>
25 #include <linux/ext2_fs.h>
26 #include <linux/proc_fs.h>
27 #include <linux/stat.h>
28 #include <linux/fcntl.h>
29 #include <linux/net.h>
30 #include <linux/unistd.h>
31 #include <linux/malloc.h>
33 #define __NO_VERSION__
34 #include <linux/module.h>
36 #include <linux/sunrpc/svc.h>
37 #include <linux/nfsd/nfsd.h>
39 #include <linux/nfs3.h>
40 #include <linux/nfsd/xdr3.h>
41 #endif /* CONFIG_NFSD_V3 */
42 #include <linux/nfsd/nfsfh.h>
43 #include <linux/quotaops.h>
45 #include <asm/uaccess.h>
47 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
51 /* We must ignore files (but only files) which might have mandatory
52 * locks on them because there is no way to know if the accesser has
55 #define IS_ISMNDLK(i) (S_ISREG((i)->i_mode) && MANDATORY_LOCK(i))
58 * This is a cache of readahead params that help us choose the proper
59 * readahead strategy. Initially, we set all readahead parameters to 0
60 * and let the VFS handle things.
61 * If you increase the number of cached files very much, you'll need to
62 * add a hash table here.
65 struct raparms
*p_next
;
69 unsigned long p_reada
,
76 static struct raparms
* raparml
= NULL
;
77 static struct raparms
* raparm_cache
= NULL
;
80 * Look up one component of a pathname.
81 * N.B. After this call _both_ fhp and resfh need an fh_put
83 * If the lookup would cross a mountpoint, and the mounted filesystem
84 * is exported to the client with NFSEXP_CROSSMNT, then the lookup is
85 * accepted as it stands and the mounted directory is
86 * returned. Otherwise the covered directory is returned.
87 * NOTE: this mountpoint crossing is not supported properly by all
88 * clients and is explicitly disallowed for NFSv3
89 * NeilBrown <neilb@cse.unsw.edu.au>
92 nfsd_lookup(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, const char *name
,
93 int len
, struct svc_fh
*resfh
)
95 struct svc_export
*exp
;
96 struct dentry
*dparent
;
97 struct dentry
*dentry
;
100 dprintk("nfsd: nfsd_lookup(fh %s, %s)\n", SVCFH_fmt(fhp
), name
);
102 /* Obtain dentry and export. */
103 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, MAY_EXEC
);
107 dparent
= fhp
->fh_dentry
;
108 exp
= fhp
->fh_export
;
112 /* Lookup the name, but don't follow links */
113 if (strcmp(name
, ".")==0) {
114 dentry
= dget(dparent
);
115 } else if (strcmp(name
, "..")==0) {
116 /* checking mountpoint crossing is very different when stepping up */
117 if (dparent
== exp
->ex_dentry
) {
118 if (!EX_CROSSMNT(exp
))
119 dentry
= dget(dparent
); /* .. == . just like at / */
122 struct svc_export
*exp2
= NULL
;
124 struct vfsmount
*mnt
= mntget(exp
->ex_mnt
);
125 dentry
= dget(dparent
);
126 while(follow_up(&mnt
, &dentry
))
128 dp
= dget(dentry
->d_parent
);
131 for ( ; exp2
== NULL
&& dp
->d_parent
!= dp
;
133 exp2
= exp_get(exp
->ex_client
, dp
->d_inode
->i_dev
, dp
->d_inode
->i_ino
);
136 dentry
= dget(dparent
);
143 dentry
= dget(dparent
->d_parent
);
145 dentry
= lookup_one(name
, dparent
);
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_CROSSMNT(exp2
)) {
162 /* successfully crossed mount point */
172 * Note: we compose the file handle now, but as the
173 * dentry may be negative, it may need to be updated.
175 err
= fh_compose(resfh
, exp
, dentry
);
176 if (!err
&& !dentry
->d_inode
)
187 * Set various file attributes.
188 * N.B. After this call fhp needs an fh_put
191 nfsd_setattr(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct iattr
*iap
)
193 struct dentry
*dentry
;
195 int accmode
= MAY_SATTR
;
199 kernel_cap_t saved_cap
= 0;
202 if (iap
->ia_valid
& (ATTR_ATIME
| ATTR_MTIME
| ATTR_SIZE
))
203 accmode
|= MAY_WRITE
|MAY_OWNER_OVERRIDE
;
204 if (iap
->ia_valid
& ATTR_SIZE
)
208 err
= fh_verify(rqstp
, fhp
, ftype
, accmode
);
209 if (err
|| !iap
->ia_valid
)
212 dentry
= fhp
->fh_dentry
;
213 inode
= dentry
->d_inode
;
215 err
= inode_change_ok(inode
, iap
);
216 /* could be a "touch" (utimes) request where the user is not the owner but does
217 * have write permission. In this case the user should be allowed to set
218 * both times to the current time. We could just assume any such SETATTR
219 * is intended to set the times to "now", but we do a couple of simple tests
220 * to increase our confidence.
222 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
223 #define MAX_TOUCH_TIME_ERROR (30*60)
225 && (iap
->ia_valid
& BOTH_TIME_SET
) == BOTH_TIME_SET
226 && iap
->ia_mtime
== iap
->ia_ctime
228 /* looks good. now just make sure time is in the right ballpark.
229 * solaris, at least, doesn't seem to care what the time request is
231 time_t delta
= iap
->ia_atime
- CURRENT_TIME
;
232 if (delta
<0) delta
= -delta
;
233 if (delta
< MAX_TOUCH_TIME_ERROR
) {
234 /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
235 * this will cause notify_change to set these times to "now"
237 iap
->ia_valid
&= ~BOTH_TIME_SET
;
238 err
= inode_change_ok(inode
, iap
);
245 /* The size case is special. It changes the file as well as the attributes. */
246 if (iap
->ia_valid
& ATTR_SIZE
) {
247 if (iap
->ia_size
< inode
->i_size
) {
248 err
= nfsd_permission(fhp
->fh_export
, dentry
, MAY_TRUNC
|MAY_OWNER_OVERRIDE
);
252 err
= get_write_access(inode
);
256 err
= locks_verify_truncate(inode
, NULL
, iap
->ia_size
);
258 put_write_access(inode
);
264 imode
= inode
->i_mode
;
265 if (iap
->ia_valid
& ATTR_MODE
) {
266 iap
->ia_mode
&= S_IALLUGO
;
267 imode
= iap
->ia_mode
|= (imode
& ~S_IALLUGO
);
270 /* Revoke setuid/setgid bit on chown/chgrp */
271 if ((iap
->ia_valid
& ATTR_UID
) && (imode
& S_ISUID
)
272 && iap
->ia_uid
!= inode
->i_uid
) {
273 iap
->ia_valid
|= ATTR_MODE
;
274 iap
->ia_mode
= imode
&= ~S_ISUID
;
276 if ((iap
->ia_valid
& ATTR_GID
) && (imode
& S_ISGID
)
277 && iap
->ia_gid
!= inode
->i_gid
) {
278 iap
->ia_valid
|= ATTR_MODE
;
279 iap
->ia_mode
= imode
&= ~S_ISGID
;
282 /* Change the attributes. */
285 iap
->ia_valid
|= ATTR_CTIME
;
286 if (current
->fsuid
!= 0) {
287 saved_cap
= current
->cap_effective
;
288 cap_clear(current
->cap_effective
);
291 /* DQUOT_TRANSFER needs both ia_uid and ia_gid defined */
292 if (iap
->ia_valid
& (ATTR_UID
|ATTR_GID
)) {
293 if (! (iap
->ia_valid
& ATTR_UID
))
294 iap
->ia_uid
= inode
->i_uid
;
295 if (! (iap
->ia_valid
& ATTR_GID
))
296 iap
->ia_gid
= inode
->i_gid
;
297 iap
->ia_valid
|= ATTR_UID
|ATTR_GID
;
299 #endif /* CONFIG_QUOTA */
301 if (iap
->ia_valid
& ATTR_SIZE
) {
306 if (iap
->ia_valid
& (ATTR_UID
|ATTR_GID
))
307 err
= DQUOT_TRANSFER(dentry
, iap
);
310 err
= notify_change(dentry
, iap
);
313 put_write_access(inode
);
315 if (current
->fsuid
!= 0)
316 current
->cap_effective
= saved_cap
;
319 if (EX_ISSYNC(fhp
->fh_export
))
320 write_inode_now(inode
);
330 #ifdef CONFIG_NFSD_V3
332 * Check server access rights to a file system object
338 static struct accessmap nfs3_regaccess
[] = {
339 { NFS3_ACCESS_READ
, MAY_READ
},
340 { NFS3_ACCESS_EXECUTE
, MAY_EXEC
},
341 { NFS3_ACCESS_MODIFY
, MAY_WRITE
|MAY_TRUNC
},
342 { NFS3_ACCESS_EXTEND
, MAY_WRITE
},
347 static struct accessmap nfs3_diraccess
[] = {
348 { NFS3_ACCESS_READ
, MAY_READ
},
349 { NFS3_ACCESS_LOOKUP
, MAY_EXEC
},
350 { NFS3_ACCESS_MODIFY
, MAY_EXEC
|MAY_WRITE
|MAY_TRUNC
},
351 { NFS3_ACCESS_EXTEND
, MAY_EXEC
|MAY_WRITE
},
352 { NFS3_ACCESS_DELETE
, MAY_REMOVE
},
357 static struct accessmap nfs3_anyaccess
[] = {
358 /* XXX: should we try to cover read/write here for clients that
359 * rely on us to do their access checking for special files? */
365 nfsd_access(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, u32
*access
)
367 struct accessmap
*map
;
368 struct svc_export
*export
;
369 struct dentry
*dentry
;
370 u32 query
, result
= 0;
373 error
= fh_verify(rqstp
, fhp
, 0, MAY_NOP
);
377 export
= fhp
->fh_export
;
378 dentry
= fhp
->fh_dentry
;
380 if (S_ISREG(dentry
->d_inode
->i_mode
))
381 map
= nfs3_regaccess
;
382 else if (S_ISDIR(dentry
->d_inode
->i_mode
))
383 map
= nfs3_diraccess
;
385 map
= nfs3_anyaccess
;
389 for (; map
->access
; map
++) {
390 if (map
->access
& query
) {
392 err2
= nfsd_permission(export
, dentry
, map
->how
);
395 result
|= map
->access
;
398 /* the following error codes just mean the access was not allowed,
399 * rather than an error occurred */
403 /* simply don't "or" in the access bit. */
416 #endif /* CONFIG_NFSD_V3 */
421 * Open an existing file or directory.
422 * The access argument indicates the type of open (read/write/lock)
423 * N.B. After this call fhp needs an fh_put
426 nfsd_open(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, int type
,
427 int access
, struct file
*filp
)
429 struct dentry
*dentry
;
433 /* If we get here, then the client has already done an "open", and (hopefully)
434 * checked permission - so allow OWNER_OVERRIDE in case a chmod has now revoked
436 err
= fh_verify(rqstp
, fhp
, type
, access
| MAY_OWNER_OVERRIDE
);
440 dentry
= fhp
->fh_dentry
;
441 inode
= dentry
->d_inode
;
443 /* Disallow access to files with the append-only bit set or
444 * with mandatory locking enabled
447 if (IS_APPEND(inode
) || IS_ISMNDLK(inode
))
452 if ((access
& MAY_WRITE
) && (err
= get_write_access(inode
)) != 0)
455 memset(filp
, 0, sizeof(*filp
));
456 filp
->f_op
= fops_get(inode
->i_fop
);
457 atomic_set(&filp
->f_count
, 1);
458 filp
->f_dentry
= dentry
;
459 if (access
& MAY_WRITE
) {
460 filp
->f_flags
= O_WRONLY
;
461 filp
->f_mode
= FMODE_WRITE
;
464 filp
->f_flags
= O_RDONLY
;
465 filp
->f_mode
= FMODE_READ
;
469 if (filp
->f_op
&& filp
->f_op
->open
) {
470 err
= filp
->f_op
->open(inode
, filp
);
472 fops_put(filp
->f_op
);
473 if (access
& MAY_WRITE
)
474 put_write_access(inode
);
476 /* I nearly added put_filp() call here, but this filp
477 * is really on callers stack frame. -DaveM
479 atomic_dec(&filp
->f_count
);
493 nfsd_close(struct file
*filp
)
495 struct dentry
*dentry
= filp
->f_dentry
;
496 struct inode
*inode
= dentry
->d_inode
;
498 if (filp
->f_op
&& filp
->f_op
->release
)
499 filp
->f_op
->release(inode
, filp
);
500 fops_put(filp
->f_op
);
501 if (filp
->f_mode
& FMODE_WRITE
)
502 put_write_access(inode
);
507 * As this calls fsync (not fdatasync) there is no need for a write_inode
511 nfsd_sync(struct file
*filp
)
513 dprintk("nfsd: sync file %s\n", filp
->f_dentry
->d_name
.name
);
514 down(&filp
->f_dentry
->d_inode
->i_sem
);
515 filp
->f_op
->fsync(filp
, filp
->f_dentry
);
516 up(&filp
->f_dentry
->d_inode
->i_sem
);
520 nfsd_sync_dir(struct dentry
*dp
)
522 struct inode
*inode
= dp
->d_inode
;
523 int (*fsync
) (struct file
*, struct dentry
*);
525 if (inode
->i_fop
&& (fsync
= inode
->i_fop
->fsync
)) {
531 * Obtain the readahead parameters for the file
532 * specified by (dev, ino).
534 static inline struct raparms
*
535 nfsd_get_raparms(dev_t dev
, ino_t ino
)
537 struct raparms
*ra
, **rap
, **frap
= NULL
;
540 for (rap
= &raparm_cache
; (ra
= *rap
); rap
= &ra
->p_next
) {
541 if (ra
->p_ino
== ino
&& ra
->p_dev
== dev
)
544 if (ra
->p_count
== 0)
547 depth
= nfsdstats
.ra_size
*11/10;
552 memset(ra
, 0, sizeof(*ra
));
556 if (rap
!= &raparm_cache
) {
558 ra
->p_next
= raparm_cache
;
562 nfsdstats
.ra_depth
[depth
*10/nfsdstats
.ra_size
]++;
567 * Read data from a file. count must contain the requested read count
568 * on entry. On return, *count contains the number of bytes actually read.
569 * N.B. After this call fhp needs an fh_put
572 nfsd_read(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, loff_t offset
,
573 char *buf
, unsigned long *count
)
580 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, MAY_READ
, &file
);
584 if (!file
.f_op
->read
)
587 /* Get readahead parameters */
588 ra
= nfsd_get_raparms(fhp
->fh_export
->ex_dev
, fhp
->fh_dentry
->d_inode
->i_ino
);
590 file
.f_reada
= ra
->p_reada
;
591 file
.f_ramax
= ra
->p_ramax
;
592 file
.f_raend
= ra
->p_raend
;
593 file
.f_ralen
= ra
->p_ralen
;
594 file
.f_rawin
= ra
->p_rawin
;
598 oldfs
= get_fs(); set_fs(KERNEL_DS
);
599 err
= file
.f_op
->read(&file
, buf
, *count
, &file
.f_pos
);
602 /* Write back readahead params */
604 dprintk("nfsd: raparms %ld %ld %ld %ld %ld\n",
605 file
.f_reada
, file
.f_ramax
, file
.f_raend
,
606 file
.f_ralen
, file
.f_rawin
);
607 ra
->p_reada
= file
.f_reada
;
608 ra
->p_ramax
= file
.f_ramax
;
609 ra
->p_raend
= file
.f_raend
;
610 ra
->p_ralen
= file
.f_ralen
;
611 ra
->p_rawin
= file
.f_rawin
;
616 nfsdstats
.io_read
+= err
;
628 * Write data to a file.
629 * The stable flag requests synchronous writes.
630 * N.B. After this call fhp needs an fh_put
633 nfsd_write(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, loff_t offset
,
634 char *buf
, unsigned long cnt
, int *stablep
)
636 struct svc_export
*exp
;
638 struct dentry
*dentry
;
642 int stable
= *stablep
;
647 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, MAY_WRITE
, &file
);
653 if (!file
.f_op
->write
)
656 dentry
= file
.f_dentry
;
657 inode
= dentry
->d_inode
;
658 exp
= fhp
->fh_export
;
661 * Request sync writes if
662 * - the sync export option has been set, or
663 * - the client requested O_SYNC behavior (NFSv3 feature).
664 * - The file system doesn't support fsync().
665 * When gathered writes have been configured for this volume,
666 * flushing the data to disk is handled separately below.
669 if (file
.f_op
->fsync
== 0) {/* COMMIT3 cannot work */
671 *stablep
= 2; /* FILE_SYNC */
676 if (stable
&& !EX_WGATHER(exp
))
677 file
.f_flags
|= O_SYNC
;
679 file
.f_pos
= offset
; /* set write offset */
681 /* Write the data. */
682 oldfs
= get_fs(); set_fs(KERNEL_DS
);
684 /* This is for disk quota. */
685 saved_euid
= current
->euid
;
686 current
->euid
= current
->fsuid
;
687 err
= file
.f_op
->write(&file
, buf
, cnt
, &file
.f_pos
);
688 current
->euid
= saved_euid
;
690 err
= file
.f_op
->write(&file
, buf
, cnt
, &file
.f_pos
);
693 nfsdstats
.io_write
+= cnt
;
696 /* clear setuid/setgid flag after write */
697 if (err
>= 0 && (inode
->i_mode
& (S_ISUID
| S_ISGID
))) {
699 kernel_cap_t saved_cap
= 0;
701 ia
.ia_valid
= ATTR_MODE
;
702 ia
.ia_mode
= inode
->i_mode
& ~(S_ISUID
| S_ISGID
);
703 if (current
->fsuid
!= 0) {
704 saved_cap
= current
->cap_effective
;
705 cap_clear(current
->cap_effective
);
707 notify_change(dentry
, &ia
);
708 if (current
->fsuid
!= 0)
709 current
->cap_effective
= saved_cap
;
712 if (err
>= 0 && stable
) {
713 static unsigned long last_ino
= 0;
714 static kdev_t last_dev
= NODEV
;
717 * Gathered writes: If another process is currently
718 * writing to the file, there's a high chance
719 * this is another nfsd (triggered by a bulk write
720 * from a client's biod). Rather than syncing the
721 * file with each write request, we sleep for 10 msec.
723 * I don't know if this roughly approximates
724 * C. Juszak's idea of gathered writes, but it's a
725 * nice and simple solution (IMHO), and it seems to
728 if (EX_WGATHER(exp
) && (atomic_read(&inode
->i_writecount
) > 1
729 || (last_ino
== inode
->i_ino
&& last_dev
== inode
->i_dev
))) {
731 interruptible_sleep_on_timeout(&inode
->i_wait
, 10 * HZ
/ 1000);
733 dprintk("nfsd: write defer %d\n", current
->pid
);
734 /* FIXME: Olaf commented this out [gam3] */
735 set_current_state(TASK_UNINTERRUPTIBLE
);
736 schedule_timeout((HZ
+99)/100);
737 current
->state
= TASK_RUNNING
;
738 dprintk("nfsd: write resume %d\n", current
->pid
);
742 if (inode
->i_state
& I_DIRTY
) {
743 dprintk("nfsd: write sync %d\n", current
->pid
);
747 wake_up(&inode
->i_wait
);
749 last_ino
= inode
->i_ino
;
750 last_dev
= inode
->i_dev
;
753 dprintk("nfsd: write complete err=%d\n", err
);
765 #ifdef CONFIG_NFSD_V3
767 * Commit all pending writes to stable storage.
768 * Strictly speaking, we could sync just the indicated file region here,
769 * but there's currently no way we can ask the VFS to do so.
771 * Unfortunately we cannot lock the file to make sure we return full WCC
772 * data to the client, as locking happens lower down in the filesystem.
775 nfsd_commit(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
776 off_t offset
, unsigned long count
)
781 if ((err
= nfsd_open(rqstp
, fhp
, S_IFREG
, MAY_WRITE
, &file
)) != 0)
783 if (EX_ISSYNC(fhp
->fh_export
)) {
784 if (file
.f_op
&& file
.f_op
->fsync
) {
787 err
= nfserr_notsupp
;
794 #endif /* CONFIG_NFSD_V3 */
797 * Create a file (regular, directory, device, fifo); UNIX sockets
798 * not yet implemented.
799 * If the response fh has been verified, the parent directory should
800 * already be locked. Note that the parent directory is left locked.
802 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
805 nfsd_create(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
806 char *fname
, int flen
, struct iattr
*iap
,
807 int type
, dev_t rdev
, struct svc_fh
*resfhp
)
809 struct dentry
*dentry
, *dchild
;
817 if (isdotent(fname
, flen
))
820 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, MAY_CREATE
);
824 dentry
= fhp
->fh_dentry
;
825 dirp
= dentry
->d_inode
;
828 if(!dirp
->i_op
|| !dirp
->i_op
->lookup
)
831 * Check whether the response file handle has been verified yet.
832 * If it has, the parent directory should already be locked.
834 if (!resfhp
->fh_dentry
) {
835 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
837 dchild
= lookup_one(fname
, dentry
);
838 err
= PTR_ERR(dchild
);
841 err
= fh_compose(resfhp
, fhp
->fh_export
, dchild
);
845 /* called from nfsd_proc_create */
846 dchild
= resfhp
->fh_dentry
;
847 if (!fhp
->fh_locked
) {
848 /* not actually possible */
850 "nfsd_create: parent %s/%s not locked!\n",
851 dentry
->d_parent
->d_name
.name
,
852 dentry
->d_name
.name
);
858 * Make sure the child dentry is still negative ...
861 if (dchild
->d_inode
) {
862 dprintk("nfsd_create: dentry %s/%s not negative!\n",
863 dentry
->d_name
.name
, dchild
->d_name
.name
);
867 if (!(iap
->ia_valid
& ATTR_MODE
))
869 iap
->ia_mode
= (iap
->ia_mode
& S_IALLUGO
) | type
;
872 * Get the dir op function pointer.
877 err
= vfs_create(dirp
, dchild
, iap
->ia_mode
);
880 err
= vfs_mkdir(dirp
, dchild
, iap
->ia_mode
);
886 err
= vfs_mknod(dirp
, dchild
, iap
->ia_mode
, rdev
);
889 printk("nfsd: bad file type %o in nfsd_create\n", type
);
895 if (EX_ISSYNC(fhp
->fh_export
)) {
896 nfsd_sync_dir(dentry
);
897 write_inode_now(dchild
->d_inode
);
901 /* Set file attributes. Mode has already been set and
902 * setting uid/gid works only for root. Irix appears to
903 * send along the gid when it tries to implement setgid
904 * directories via NFS.
907 if ((iap
->ia_valid
&= ~(ATTR_UID
|ATTR_GID
|ATTR_MODE
)) != 0)
908 err
= nfsd_setattr(rqstp
, resfhp
, iap
);
910 * Update the file handle to get the new inode info.
913 err
= fh_update(resfhp
);
922 #ifdef CONFIG_NFSD_V3
924 * NFSv3 version of nfsd_create
927 nfsd_create_v3(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
928 char *fname
, int flen
, struct iattr
*iap
,
929 struct svc_fh
*resfhp
, int createmode
, u32
*verifier
)
931 struct dentry
*dentry
, *dchild
;
934 __u32 v_mtime
=0, v_atime
=0;
941 if (isdotent(fname
, flen
))
943 if (!(iap
->ia_valid
& ATTR_MODE
))
945 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, MAY_CREATE
);
949 dentry
= fhp
->fh_dentry
;
950 dirp
= dentry
->d_inode
;
952 /* Get all the sanity checks out of the way before
953 * we lock the parent. */
955 if(!dirp
->i_op
|| !dirp
->i_op
->lookup
)
960 * Compose the response file handle.
962 dchild
= lookup_one(fname
, dentry
);
963 err
= PTR_ERR(dchild
);
967 err
= fh_compose(resfhp
, fhp
->fh_export
, dchild
);
971 if (createmode
== NFS3_CREATE_EXCLUSIVE
) {
972 /* while the verifier would fit in mtime+atime,
973 * solaris7 gets confused (bugid 4218508) if these have
974 * the high bit set, so we use the mode as well
976 v_mtime
= verifier
[0]&0x7fffffff;
977 v_atime
= verifier
[1]&0x7fffffff;
979 | ((verifier
[0]&0x80000000) >> (32-7)) /* u+x */
980 | ((verifier
[1]&0x80000000) >> (32-9)) /* u+r */
984 if (dchild
->d_inode
) {
987 switch (createmode
) {
988 case NFS3_CREATE_UNCHECKED
:
989 if (! S_ISREG(dchild
->d_inode
->i_mode
))
992 iap
->ia_valid
&= ATTR_SIZE
;
996 case NFS3_CREATE_EXCLUSIVE
:
997 if ( dchild
->d_inode
->i_mtime
== v_mtime
998 && dchild
->d_inode
->i_atime
== v_atime
999 && dchild
->d_inode
->i_mode
== v_mode
1000 && dchild
->d_inode
->i_size
== 0 )
1003 case NFS3_CREATE_GUARDED
:
1009 err
= vfs_create(dirp
, dchild
, iap
->ia_mode
);
1013 if (EX_ISSYNC(fhp
->fh_export
)) {
1014 nfsd_sync_dir(dentry
);
1015 /* setattr will sync the child (or not) */
1019 * Update the filehandle to get the new inode info.
1021 err
= fh_update(resfhp
);
1025 if (createmode
== NFS3_CREATE_EXCLUSIVE
) {
1026 /* Cram the verifier into atime/mtime/mode */
1027 iap
->ia_valid
= ATTR_MTIME
|ATTR_ATIME
1028 | ATTR_MTIME_SET
|ATTR_ATIME_SET
1030 iap
->ia_mtime
= v_mtime
;
1031 iap
->ia_atime
= v_atime
;
1032 iap
->ia_mode
= v_mode
;
1035 /* Set file attributes.
1036 * Mode has already been set but we might need to reset it
1037 * for CREATE_EXCLUSIVE
1038 * Irix appears to send along the gid when it tries to
1039 * implement setgid directories via NFS. Clear out all that cruft.
1042 if ((iap
->ia_valid
&= ~(ATTR_UID
|ATTR_GID
)) != 0)
1043 err
= nfsd_setattr(rqstp
, resfhp
, iap
);
1050 err
= nfserrno(err
);
1053 #endif /* CONFIG_NFSD_V3 */
1056 * Read a symlink. On entry, *lenp must contain the maximum path length that
1057 * fits into the buffer. On return, it contains the true length.
1058 * N.B. After this call fhp needs an fh_put
1061 nfsd_readlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, char *buf
, int *lenp
)
1063 struct dentry
*dentry
;
1064 struct inode
*inode
;
1068 err
= fh_verify(rqstp
, fhp
, S_IFLNK
, MAY_NOP
);
1072 dentry
= fhp
->fh_dentry
;
1073 inode
= dentry
->d_inode
;
1076 if (!inode
->i_op
|| !inode
->i_op
->readlink
)
1079 UPDATE_ATIME(inode
);
1080 /* N.B. Why does this call need a get_fs()??
1081 * Remove the set_fs and watch the fireworks:-) --okir
1084 oldfs
= get_fs(); set_fs(KERNEL_DS
);
1085 err
= inode
->i_op
->readlink(dentry
, buf
, *lenp
);
1096 err
= nfserrno(err
);
1101 * Create a symlink and look up its inode
1102 * N.B. After this call _both_ fhp and resfhp need an fh_put
1105 nfsd_symlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1106 char *fname
, int flen
,
1107 char *path
, int plen
,
1108 struct svc_fh
*resfhp
,
1111 struct dentry
*dentry
, *dnew
;
1118 if (isdotent(fname
, flen
))
1121 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, MAY_CREATE
);
1125 dentry
= fhp
->fh_dentry
;
1126 dnew
= lookup_one(fname
, dentry
);
1127 err
= PTR_ERR(dnew
);
1131 err
= vfs_symlink(dentry
->d_inode
, dnew
, path
);
1133 if (EX_ISSYNC(fhp
->fh_export
))
1134 nfsd_sync_dir(dentry
);
1136 iap
->ia_valid
&= ATTR_MODE
/* ~(ATTR_MODE|ATTR_UID|ATTR_GID)*/;
1137 if (iap
->ia_valid
) {
1138 iap
->ia_valid
|= ATTR_CTIME
;
1139 iap
->ia_mode
= (iap
->ia_mode
&S_IALLUGO
)
1141 err
= notify_change(dnew
, iap
);
1142 if (!err
&& EX_ISSYNC(fhp
->fh_export
))
1143 write_inode_now(dentry
->d_inode
);
1147 err
= nfserrno(err
);
1150 /* Compose the fh so the dentry will be freed ... */
1151 cerr
= fh_compose(resfhp
, fhp
->fh_export
, dnew
);
1152 if (err
==0) err
= cerr
;
1157 err
= nfserrno(err
);
1163 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1166 nfsd_link(struct svc_rqst
*rqstp
, struct svc_fh
*ffhp
,
1167 char *fname
, int len
, struct svc_fh
*tfhp
)
1169 struct dentry
*ddir
, *dnew
, *dold
;
1170 struct inode
*dirp
, *dest
;
1173 err
= fh_verify(rqstp
, ffhp
, S_IFDIR
, MAY_CREATE
);
1176 err
= fh_verify(rqstp
, tfhp
, -S_IFDIR
, MAY_NOP
);
1184 if (isdotent(fname
, len
))
1188 ddir
= ffhp
->fh_dentry
;
1189 dirp
= ddir
->d_inode
;
1191 dnew
= lookup_one(fname
, ddir
);
1192 err
= PTR_ERR(dnew
);
1196 dold
= tfhp
->fh_dentry
;
1197 dest
= dold
->d_inode
;
1199 err
= vfs_link(dold
, dirp
, dnew
);
1201 if (EX_ISSYNC(ffhp
->fh_export
)) {
1202 nfsd_sync_dir(ddir
);
1203 write_inode_now(dest
);
1206 if (err
== -EXDEV
&& rqstp
->rq_vers
== 2)
1209 err
= nfserrno(err
);
1218 err
= nfserrno(err
);
1224 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1227 nfsd_rename(struct svc_rqst
*rqstp
, struct svc_fh
*ffhp
, char *fname
, int flen
,
1228 struct svc_fh
*tfhp
, char *tname
, int tlen
)
1230 struct dentry
*fdentry
, *tdentry
, *odentry
, *ndentry
;
1231 struct inode
*fdir
, *tdir
;
1234 err
= fh_verify(rqstp
, ffhp
, S_IFDIR
, MAY_REMOVE
);
1237 err
= fh_verify(rqstp
, tfhp
, S_IFDIR
, MAY_CREATE
);
1241 fdentry
= ffhp
->fh_dentry
;
1242 fdir
= fdentry
->d_inode
;
1244 tdentry
= tfhp
->fh_dentry
;
1245 tdir
= tdentry
->d_inode
;
1247 err
= (rqstp
->rq_vers
== 2) ? nfserr_acces
: nfserr_xdev
;
1248 if (fdir
->i_dev
!= tdir
->i_dev
)
1252 if (!flen
|| isdotent(fname
, flen
) || !tlen
|| isdotent(tname
, tlen
))
1255 /* cannot use fh_lock as we need deadlock protective ordering
1256 * so do it by hand */
1257 double_down(&tdir
->i_sem
, &fdir
->i_sem
);
1258 ffhp
->fh_locked
= tfhp
->fh_locked
= 1;
1262 odentry
= lookup_one(fname
, fdentry
);
1263 err
= PTR_ERR(odentry
);
1264 if (IS_ERR(odentry
))
1268 if (!odentry
->d_inode
)
1271 ndentry
= lookup_one(tname
, tdentry
);
1272 err
= PTR_ERR(ndentry
);
1273 if (IS_ERR(ndentry
))
1277 err
= vfs_rename(fdir
, odentry
, tdir
, ndentry
);
1278 if (!err
&& EX_ISSYNC(tfhp
->fh_export
)) {
1279 nfsd_sync_dir(tdentry
);
1280 nfsd_sync_dir(fdentry
);
1288 err
= nfserrno(err
);
1290 /* we cannot reply on fh_unlock on the two filehandles,
1291 * as that would do the wrong thing if the two directories
1292 * were the same, so again we do it by hand
1294 fill_post_wcc(ffhp
);
1295 fill_post_wcc(tfhp
);
1296 double_up(&tdir
->i_sem
, &fdir
->i_sem
);
1297 ffhp
->fh_locked
= tfhp
->fh_locked
= 0;
1304 * Unlink a file or directory
1305 * N.B. After this call fhp needs an fh_put
1308 nfsd_unlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, int type
,
1309 char *fname
, int flen
)
1311 struct dentry
*dentry
, *rdentry
;
1316 if (!flen
|| isdotent(fname
, flen
))
1318 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, MAY_REMOVE
);
1323 dentry
= fhp
->fh_dentry
;
1324 dirp
= dentry
->d_inode
;
1326 rdentry
= lookup_one(fname
, dentry
);
1327 err
= PTR_ERR(rdentry
);
1328 if (IS_ERR(rdentry
))
1331 if (!rdentry
->d_inode
) {
1337 if (type
!= S_IFDIR
) { /* It's UNLINK */
1338 err
= vfs_unlink(dirp
, rdentry
);
1339 } else { /* It's RMDIR */
1340 err
= vfs_rmdir(dirp
, rdentry
);
1347 if (EX_ISSYNC(fhp
->fh_export
))
1348 nfsd_sync_dir(dentry
);
1354 err
= nfserrno(err
);
1359 * Read entries from a directory.
1360 * The verifier is an NFSv3 thing we ignore for now.
1363 nfsd_readdir(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, loff_t offset
,
1364 encode_dent_fn func
, u32
*buffer
, int *countp
, u32
*verf
)
1366 struct inode
*inode
;
1368 int oldlen
, eof
, err
;
1370 struct readdir_cd cd
;
1372 err
= nfsd_open(rqstp
, fhp
, S_IFDIR
, MAY_READ
, &file
);
1375 if (offset
> ~(u32
) 0)
1378 err
= nfserr_notdir
;
1379 if (!file
.f_op
->readdir
)
1381 file
.f_pos
= offset
;
1383 /* Set up the readdir context */
1384 memset(&cd
, 0, sizeof(cd
));
1387 cd
.buflen
= *countp
; /* count of words */
1391 * Read the directory entries. This silly loop is necessary because
1392 * readdir() is not guaranteed to fill up the entire buffer, but
1393 * may choose to do less.
1395 inode
= file
.f_dentry
->d_inode
;
1396 down(&inode
->i_sem
);
1401 dprintk("nfsd: f_op->readdir(%x/%ld @ %d) buflen = %d (%d)\n",
1402 file.f_inode->i_dev, file.f_inode->i_ino,
1403 (int) file.f_pos, (int) oldlen, (int) cd.buflen);
1405 err
= file
.f_op
->readdir(&file
, &cd
, (filldir_t
) func
);
1408 if (oldlen
== cd
.buflen
)
1415 /* If we didn't fill the buffer completely, we're at EOF */
1419 if (rqstp
->rq_vers
== 3)
1420 (void)xdr_encode_hyper(cd
.offset
, file
.f_pos
);
1422 *cd
.offset
= htonl(file
.f_pos
);
1426 *p
++ = 0; /* no more entries */
1427 *p
++ = htonl(eof
); /* end of directory */
1428 *countp
= (caddr_t
) p
- (caddr_t
) buffer
;
1430 dprintk("nfsd: readdir result %d bytes, eof %d offset %d\n",
1432 cd
.offset
? ntohl(*cd
.offset
) : -1);
1441 err
= nfserrno(err
);
1446 * Get file system stats
1447 * N.B. After this call fhp needs an fh_put
1450 nfsd_statfs(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct statfs
*stat
)
1452 int err
= fh_verify(rqstp
, fhp
, 0, MAY_NOP
);
1453 if (!err
&& vfs_statfs(fhp
->fh_dentry
->d_inode
->i_sb
,stat
))
1459 * Check for a user's access permissions to this inode.
1462 nfsd_permission(struct svc_export
*exp
, struct dentry
*dentry
, int acc
)
1464 struct inode
*inode
= dentry
->d_inode
;
1466 kernel_cap_t saved_cap
= 0;
1471 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1473 (acc
& MAY_READ
)? " read" : "",
1474 (acc
& MAY_WRITE
)? " write" : "",
1475 (acc
& MAY_EXEC
)? " exec" : "",
1476 (acc
& MAY_SATTR
)? " sattr" : "",
1477 (acc
& MAY_TRUNC
)? " trunc" : "",
1478 (acc
& MAY_LOCK
)? " lock" : "",
1479 (acc
& MAY_OWNER_OVERRIDE
)? " owneroverride" : "",
1481 IS_IMMUTABLE(inode
)? " immut" : "",
1482 IS_APPEND(inode
)? " append" : "",
1483 IS_RDONLY(inode
)? " ro" : "");
1484 dprintk(" owner %d/%d user %d/%d\n",
1485 inode
->i_uid
, inode
->i_gid
, current
->fsuid
, current
->fsgid
);
1488 if (acc
& (MAY_WRITE
| MAY_SATTR
| MAY_TRUNC
)) {
1489 if (EX_RDONLY(exp
) || IS_RDONLY(inode
))
1491 if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode
))
1494 if ((acc
& MAY_TRUNC
) && IS_APPEND(inode
))
1497 if (acc
& MAY_LOCK
) {
1498 /* If we cannot rely on authentication in NLM requests,
1499 * just allow locks, otherwise require read permission, or
1502 if (exp
->ex_flags
& NFSEXP_NOAUTHNLM
)
1505 acc
= MAY_READ
| MAY_OWNER_OVERRIDE
;
1508 * The file owner always gets access permission for accesses that
1509 * would normally be checked at open time. This is to make
1510 * file access work even when the client has done a fchmod(fd, 0).
1512 * However, `cp foo bar' should fail nevertheless when bar is
1513 * readonly. A sensible way to do this might be to reject all
1514 * attempts to truncate a read-only file, because a creat() call
1515 * always implies file truncation.
1516 * ... but this isn't really fair. A process may reasonably call
1517 * ftruncate on an open file descriptor on a file with perm 000.
1518 * We must trust the client to do permission checking - using "ACCESS"
1521 if ((acc
& MAY_OWNER_OVERRIDE
) &&
1522 inode
->i_uid
== current
->fsuid
)
1525 if (current
->fsuid
!= 0) {
1526 saved_cap
= current
->cap_effective
;
1527 cap_clear(current
->cap_effective
);
1530 err
= permission(inode
, acc
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
));
1532 /* Allow read access to binaries even when mode 111 */
1533 if (err
== -EACCES
&& S_ISREG(inode
->i_mode
) && acc
== MAY_READ
)
1534 err
= permission(inode
, MAY_EXEC
);
1536 if (current
->fsuid
!= 0)
1537 current
->cap_effective
= saved_cap
;
1539 return err
? nfserrno(err
) : 0;
1543 nfsd_racache_shutdown(void)
1547 dprintk("nfsd: freeing readahead buffers.\n");
1549 raparm_cache
= raparml
= NULL
;
1552 * Initialize readahead param cache
1555 nfsd_racache_init(int cache_size
)
1561 raparml
= kmalloc(sizeof(struct raparms
) * cache_size
, GFP_KERNEL
);
1563 if (raparml
!= NULL
) {
1564 dprintk("nfsd: allocating %d readahead buffers.\n",
1566 memset(raparml
, 0, sizeof(struct raparms
) * cache_size
);
1567 for (i
= 0; i
< cache_size
- 1; i
++) {
1568 raparml
[i
].p_next
= raparml
+ i
+ 1;
1570 raparm_cache
= raparml
;
1573 "nfsd: Could not allocate memory read-ahead cache.\n");
1576 nfsdstats
.ra_size
= cache_size
;