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 */
171 * Note: we compose the file handle now, but as the
172 * dentry may be negative, it may need to be updated.
174 err
= fh_compose(resfh
, exp
, dentry
);
175 if (!err
&& !dentry
->d_inode
)
186 * Set various file attributes.
187 * N.B. After this call fhp needs an fh_put
190 nfsd_setattr(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct iattr
*iap
)
192 struct dentry
*dentry
;
194 int accmode
= MAY_SATTR
;
198 kernel_cap_t saved_cap
= 0;
201 if (iap
->ia_valid
& (ATTR_ATIME
| ATTR_MTIME
| ATTR_SIZE
))
202 accmode
|= MAY_WRITE
|MAY_OWNER_OVERRIDE
;
203 if (iap
->ia_valid
& ATTR_SIZE
)
207 err
= fh_verify(rqstp
, fhp
, ftype
, accmode
);
208 if (err
|| !iap
->ia_valid
)
211 dentry
= fhp
->fh_dentry
;
212 inode
= dentry
->d_inode
;
214 err
= inode_change_ok(inode
, iap
);
215 /* could be a "touch" (utimes) request where the user is not the owner but does
216 * have write permission. In this case the user should be allowed to set
217 * both times to the current time. We could just assume any such SETATTR
218 * is intended to set the times to "now", but we do a couple of simple tests
219 * to increase our confidence.
221 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
222 #define MAX_TOUCH_TIME_ERROR (30*60)
224 && (iap
->ia_valid
& BOTH_TIME_SET
) == BOTH_TIME_SET
225 && iap
->ia_mtime
== iap
->ia_ctime
227 /* looks good. now just make sure time is in the right ballpark.
228 * solaris, at least, doesn't seem to care what the time request is
230 time_t delta
= iap
->ia_atime
- CURRENT_TIME
;
231 if (delta
<0) delta
= -delta
;
232 if (delta
< MAX_TOUCH_TIME_ERROR
) {
233 /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
234 * this will cause notify_change to set these times to "now"
236 iap
->ia_valid
&= ~BOTH_TIME_SET
;
237 err
= inode_change_ok(inode
, iap
);
244 /* The size case is special. It changes the file as well as the attributes. */
245 if (iap
->ia_valid
& ATTR_SIZE
) {
246 if (iap
->ia_size
< inode
->i_size
) {
247 err
= nfsd_permission(fhp
->fh_export
, dentry
, MAY_TRUNC
|MAY_OWNER_OVERRIDE
);
251 err
= get_write_access(inode
);
255 err
= locks_verify_truncate(inode
, NULL
, iap
->ia_size
);
261 imode
= inode
->i_mode
;
262 if (iap
->ia_valid
& ATTR_MODE
) {
263 iap
->ia_mode
&= S_IALLUGO
;
264 imode
= iap
->ia_mode
|= (imode
& ~S_IALLUGO
);
267 /* Revoke setuid/setgid bit on chown/chgrp */
268 if ((iap
->ia_valid
& ATTR_UID
) && (imode
& S_ISUID
)
269 && iap
->ia_uid
!= inode
->i_uid
) {
270 iap
->ia_valid
|= ATTR_MODE
;
271 iap
->ia_mode
= imode
&= ~S_ISUID
;
273 if ((iap
->ia_valid
& ATTR_GID
) && (imode
& S_ISGID
)
274 && iap
->ia_gid
!= inode
->i_gid
) {
275 iap
->ia_valid
|= ATTR_MODE
;
276 iap
->ia_mode
= imode
&= ~S_ISGID
;
279 /* Change the attributes. */
282 iap
->ia_valid
|= ATTR_CTIME
;
283 if (current
->fsuid
!= 0) {
284 saved_cap
= current
->cap_effective
;
285 cap_clear(current
->cap_effective
);
288 /* DQUOT_TRANSFER needs both ia_uid and ia_gid defined */
289 if (iap
->ia_valid
& (ATTR_UID
|ATTR_GID
)) {
290 if (! (iap
->ia_valid
& ATTR_UID
))
291 iap
->ia_uid
= inode
->i_uid
;
292 if (! (iap
->ia_valid
& ATTR_GID
))
293 iap
->ia_gid
= inode
->i_gid
;
294 iap
->ia_valid
|= ATTR_UID
|ATTR_GID
;
296 #endif /* CONFIG_QUOTA */
298 if (iap
->ia_valid
& ATTR_SIZE
) {
303 if (iap
->ia_valid
& (ATTR_UID
|ATTR_GID
))
304 err
= DQUOT_TRANSFER(dentry
, iap
);
307 err
= notify_change(dentry
, iap
);
310 put_write_access(inode
);
312 if (current
->fsuid
!= 0)
313 current
->cap_effective
= saved_cap
;
316 if (EX_ISSYNC(fhp
->fh_export
))
317 write_inode_now(inode
);
320 /* Don't unlock inode; the nfssvc_release functions are supposed
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. */
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
);
601 nfsdstats
.io_read
+= *count
;
603 /* Write back readahead params */
605 dprintk("nfsd: raparms %ld %ld %ld %ld %ld\n",
606 file
.f_reada
, file
.f_ramax
, file
.f_raend
,
607 file
.f_ralen
, file
.f_rawin
);
608 ra
->p_reada
= file
.f_reada
;
609 ra
->p_ramax
= file
.f_ramax
;
610 ra
->p_raend
= file
.f_raend
;
611 ra
->p_ralen
= file
.f_ralen
;
612 ra
->p_rawin
= file
.f_rawin
;
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.
668 #ifdef CONFIG_NFSD_V3
669 if (file
.f_op
->fsync
== 0) {/* COMMIT3 cannot work */
671 *stablep
= 2; /* FILE_SYNC */
675 if (stable
&& !EX_WGATHER(exp
))
676 file
.f_flags
|= O_SYNC
;
678 if ((stable
|| (stable
= EX_ISSYNC(exp
))) && !EX_WGATHER(exp
))
679 file
.f_flags
|= O_SYNC
;
680 #endif /* CONFIG_NFSD_V3 */
682 file
.f_pos
= offset
; /* set write offset */
684 /* Write the data. */
685 oldfs
= get_fs(); set_fs(KERNEL_DS
);
687 /* This is for disk quota. */
688 saved_euid
= current
->euid
;
689 current
->euid
= current
->fsuid
;
690 err
= file
.f_op
->write(&file
, buf
, cnt
, &file
.f_pos
);
691 current
->euid
= saved_euid
;
693 err
= file
.f_op
->write(&file
, buf
, cnt
, &file
.f_pos
);
695 nfsdstats
.io_write
+= cnt
;
698 /* clear setuid/setgid flag after write */
699 if (err
>= 0 && (inode
->i_mode
& (S_ISUID
| S_ISGID
))) {
701 kernel_cap_t saved_cap
= 0;
703 ia
.ia_valid
= ATTR_MODE
;
704 ia
.ia_mode
= inode
->i_mode
& ~(S_ISUID
| S_ISGID
);
705 if (current
->fsuid
!= 0) {
706 saved_cap
= current
->cap_effective
;
707 cap_clear(current
->cap_effective
);
709 notify_change(dentry
, &ia
);
710 if (current
->fsuid
!= 0)
711 current
->cap_effective
= saved_cap
;
714 if (err
>= 0 && stable
) {
715 static unsigned long last_ino
= 0;
716 static kdev_t last_dev
= NODEV
;
719 * Gathered writes: If another process is currently
720 * writing to the file, there's a high chance
721 * this is another nfsd (triggered by a bulk write
722 * from a client's biod). Rather than syncing the
723 * file with each write request, we sleep for 10 msec.
725 * I don't know if this roughly approximates
726 * C. Juszak's idea of gathered writes, but it's a
727 * nice and simple solution (IMHO), and it seems to
730 if (EX_WGATHER(exp
) && (atomic_read(&inode
->i_writecount
) > 1
731 || (last_ino
== inode
->i_ino
&& last_dev
== inode
->i_dev
))) {
733 interruptible_sleep_on_timeout(&inode
->i_wait
, 10 * HZ
/ 1000);
735 dprintk("nfsd: write defer %d\n", current
->pid
);
736 /* FIXME: Olaf commented this out [gam3] */
737 schedule_timeout((HZ
+99)/100);
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
);
746 wake_up(&inode
->i_wait
);
747 last_ino
= inode
->i_ino
;
748 last_dev
= inode
->i_dev
;
751 dprintk("nfsd: write complete err=%d\n", err
);
763 #ifdef CONFIG_NFSD_V3
765 * Commit all pendig writes to stable storage.
766 * Strictly speaking, we could sync just indicated the file region here,
767 * but there's currently no way we can ask the VFS to do so.
769 * We lock the file to make sure we return full WCC data to the client.
772 nfsd_commit(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
773 off_t offset
, unsigned long count
)
778 if ((err
= nfsd_open(rqstp
, fhp
, S_IFREG
, MAY_WRITE
, &file
)) != 0)
780 if (EX_ISSYNC(fhp
->fh_export
)) {
781 if (file
.f_op
&& file
.f_op
->fsync
) {
784 err
= nfserr_notsupp
;
791 #endif /* CONFIG_NFSD_V3 */
794 * Create a file (regular, directory, device, fifo); UNIX sockets
795 * not yet implemented.
796 * If the response fh has been verified, the parent directory should
797 * already be locked. Note that the parent directory is left locked.
799 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
802 nfsd_create(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
803 char *fname
, int flen
, struct iattr
*iap
,
804 int type
, dev_t rdev
, struct svc_fh
*resfhp
)
806 struct dentry
*dentry
, *dchild
;
814 if (isdotent(fname
, flen
))
817 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, MAY_CREATE
);
821 dentry
= fhp
->fh_dentry
;
822 dirp
= dentry
->d_inode
;
825 if(!dirp
->i_op
|| !dirp
->i_op
->lookup
)
828 * Check whether the response file handle has been verified yet.
829 * If it has, the parent directory should already be locked.
831 if (!resfhp
->fh_dverified
) {
832 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
834 dchild
= lookup_one(fname
, dentry
);
835 err
= PTR_ERR(dchild
);
838 err
= fh_compose(resfhp
, fhp
->fh_export
, dchild
);
842 /* called from nfsd_proc_create */
843 dchild
= resfhp
->fh_dentry
;
844 if (!fhp
->fh_locked
) {
845 /* not actually possible */
847 "nfsd_create: parent %s/%s not locked!\n",
848 dentry
->d_parent
->d_name
.name
,
849 dentry
->d_name
.name
);
855 * Make sure the child dentry is still negative ...
858 if (dchild
->d_inode
) {
859 dprintk("nfsd_create: dentry %s/%s not negative!\n",
860 dentry
->d_name
.name
, dchild
->d_name
.name
);
864 if (!(iap
->ia_valid
& ATTR_MODE
))
866 iap
->ia_mode
= (iap
->ia_mode
& S_IALLUGO
) | type
;
869 * Get the dir op function pointer.
874 err
= vfs_create(dirp
, dchild
, iap
->ia_mode
);
877 err
= vfs_mkdir(dirp
, dchild
, iap
->ia_mode
);
883 err
= vfs_mknod(dirp
, dchild
, iap
->ia_mode
, rdev
);
886 printk("nfsd: bad file type %o in nfsd_create\n", type
);
892 if (EX_ISSYNC(fhp
->fh_export
)) {
893 nfsd_sync_dir(dentry
);
894 write_inode_now(dchild
->d_inode
);
898 /* Set file attributes. Mode has already been set and
899 * setting uid/gid works only for root. Irix appears to
900 * send along the gid when it tries to implement setgid
901 * directories via NFS.
904 if ((iap
->ia_valid
&= ~(ATTR_UID
|ATTR_GID
|ATTR_MODE
)) != 0)
905 err
= nfsd_setattr(rqstp
, resfhp
, iap
);
907 * Update the file handle to get the new inode info.
910 err
= fh_update(resfhp
);
919 #ifdef CONFIG_NFSD_V3
921 * NFSv3 version of nfsd_create
924 nfsd_create_v3(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
925 char *fname
, int flen
, struct iattr
*iap
,
926 struct svc_fh
*resfhp
, int createmode
, u32
*verifier
)
928 struct dentry
*dentry
, *dchild
;
936 if (isdotent(fname
, flen
))
938 if (!(iap
->ia_valid
& ATTR_MODE
))
940 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, MAY_CREATE
);
944 dentry
= fhp
->fh_dentry
;
945 dirp
= dentry
->d_inode
;
947 /* Get all the sanity checks out of the way before
948 * we lock the parent. */
950 if(!dirp
->i_op
|| !dirp
->i_op
->lookup
)
955 * Compose the response file handle.
957 dchild
= lookup_one(fname
, dentry
);
958 err
= PTR_ERR(dchild
);
962 err
= fh_compose(resfhp
, fhp
->fh_export
, dchild
);
966 if (dchild
->d_inode
) {
969 switch (createmode
) {
970 case NFS3_CREATE_UNCHECKED
:
971 if (! S_ISREG(dchild
->d_inode
->i_mode
))
974 iap
->ia_valid
&= ATTR_SIZE
;
978 case NFS3_CREATE_EXCLUSIVE
:
979 if ( dchild
->d_inode
->i_mtime
== verifier
[0]
980 && dchild
->d_inode
->i_atime
== verifier
[1]
981 && dchild
->d_inode
->i_mode
== S_IFREG
982 && dchild
->d_inode
->i_size
== 0 )
985 case NFS3_CREATE_GUARDED
:
991 err
= vfs_create(dirp
, dchild
, iap
->ia_mode
);
995 if (EX_ISSYNC(fhp
->fh_export
)) {
996 nfsd_sync_dir(dentry
);
997 /* setattr will sync the child (or not) */
1001 * Update the filehandle to get the new inode info.
1003 err
= fh_update(resfhp
);
1007 if (createmode
== NFS3_CREATE_EXCLUSIVE
) {
1008 /* Cram the verifier into atime/mtime */
1009 iap
->ia_valid
= ATTR_MTIME
|ATTR_ATIME
|ATTR_MTIME_SET
|ATTR_ATIME_SET
;
1010 iap
->ia_mtime
= verifier
[0];
1011 iap
->ia_atime
= verifier
[1];
1014 /* Set file attributes. Mode has already been set and
1015 * setting uid/gid works only for root. Irix appears to
1016 * send along the gid when it tries to implement setgid
1017 * directories via NFS. Clear out all that cruft.
1020 if ((iap
->ia_valid
&= ~(ATTR_UID
|ATTR_GID
|ATTR_MODE
)) != 0)
1021 err
= nfsd_setattr(rqstp
, resfhp
, iap
);
1028 err
= nfserrno(err
);
1031 #endif /* CONFIG_NFSD_V3 */
1034 * Read a symlink. On entry, *lenp must contain the maximum path length that
1035 * fits into the buffer. On return, it contains the true length.
1036 * N.B. After this call fhp needs an fh_put
1039 nfsd_readlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, char *buf
, int *lenp
)
1041 struct dentry
*dentry
;
1042 struct inode
*inode
;
1046 err
= fh_verify(rqstp
, fhp
, S_IFLNK
, MAY_NOP
);
1050 dentry
= fhp
->fh_dentry
;
1051 inode
= dentry
->d_inode
;
1054 if (!inode
->i_op
|| !inode
->i_op
->readlink
)
1057 UPDATE_ATIME(inode
);
1058 /* N.B. Why does this call need a get_fs()??
1059 * Remove the set_fs and watch the fireworks:-) --okir
1062 oldfs
= get_fs(); set_fs(KERNEL_DS
);
1063 err
= inode
->i_op
->readlink(dentry
, buf
, *lenp
);
1074 err
= nfserrno(err
);
1079 * Create a symlink and look up its inode
1080 * N.B. After this call _both_ fhp and resfhp need an fh_put
1083 nfsd_symlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1084 char *fname
, int flen
,
1085 char *path
, int plen
,
1086 struct svc_fh
*resfhp
,
1089 struct dentry
*dentry
, *dnew
;
1096 if (isdotent(fname
, flen
))
1099 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, MAY_CREATE
);
1103 dentry
= fhp
->fh_dentry
;
1104 dnew
= lookup_one(fname
, dentry
);
1105 err
= PTR_ERR(dnew
);
1109 err
= vfs_symlink(dentry
->d_inode
, dnew
, path
);
1111 if (EX_ISSYNC(fhp
->fh_export
))
1112 nfsd_sync_dir(dentry
);
1114 iap
->ia_valid
&= ATTR_MODE
/* ~(ATTR_MODE|ATTR_UID|ATTR_GID)*/;
1115 if (iap
->ia_valid
) {
1116 iap
->ia_valid
|= ATTR_CTIME
;
1117 iap
->ia_mode
= (iap
->ia_mode
&S_IALLUGO
)
1119 err
= notify_change(dnew
, iap
);
1120 if (!err
&& EX_ISSYNC(fhp
->fh_export
))
1121 write_inode_now(dentry
->d_inode
);
1125 err
= nfserrno(err
);
1128 /* Compose the fh so the dentry will be freed ... */
1129 cerr
= fh_compose(resfhp
, fhp
->fh_export
, dnew
);
1130 if (err
==0) err
= cerr
;
1135 err
= nfserrno(err
);
1141 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1144 nfsd_link(struct svc_rqst
*rqstp
, struct svc_fh
*ffhp
,
1145 char *fname
, int len
, struct svc_fh
*tfhp
)
1147 struct dentry
*ddir
, *dnew
, *dold
;
1148 struct inode
*dirp
, *dest
;
1151 err
= fh_verify(rqstp
, ffhp
, S_IFDIR
, MAY_CREATE
);
1154 err
= fh_verify(rqstp
, tfhp
, -S_IFDIR
, MAY_NOP
);
1162 if (isdotent(fname
, len
))
1166 ddir
= ffhp
->fh_dentry
;
1167 dirp
= ddir
->d_inode
;
1169 dnew
= lookup_one(fname
, ddir
);
1170 err
= PTR_ERR(dnew
);
1174 dold
= tfhp
->fh_dentry
;
1175 dest
= dold
->d_inode
;
1177 err
= vfs_link(dold
, dirp
, dnew
);
1179 if (EX_ISSYNC(ffhp
->fh_export
)) {
1180 nfsd_sync_dir(ddir
);
1181 write_inode_now(dest
);
1184 if (err
== -EXDEV
&& rqstp
->rq_vers
== 2)
1187 err
= nfserrno(err
);
1196 err
= nfserrno(err
);
1202 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1205 nfsd_rename(struct svc_rqst
*rqstp
, struct svc_fh
*ffhp
, char *fname
, int flen
,
1206 struct svc_fh
*tfhp
, char *tname
, int tlen
)
1208 struct dentry
*fdentry
, *tdentry
, *odentry
, *ndentry
;
1209 struct inode
*fdir
, *tdir
;
1212 err
= fh_verify(rqstp
, ffhp
, S_IFDIR
, MAY_REMOVE
);
1215 err
= fh_verify(rqstp
, tfhp
, S_IFDIR
, MAY_CREATE
);
1219 fdentry
= ffhp
->fh_dentry
;
1220 fdir
= fdentry
->d_inode
;
1222 tdentry
= tfhp
->fh_dentry
;
1223 tdir
= tdentry
->d_inode
;
1225 err
= (rqstp
->rq_vers
== 2) ? nfserr_acces
: nfserr_xdev
;
1226 if (fdir
->i_dev
!= tdir
->i_dev
)
1230 if (!flen
|| isdotent(fname
, flen
) || !tlen
|| isdotent(tname
, tlen
))
1233 double_down(&tdir
->i_sem
, &fdir
->i_sem
);
1234 odentry
= lookup_one(fname
, fdentry
);
1235 err
= PTR_ERR(odentry
);
1236 if (IS_ERR(odentry
))
1240 if (!odentry
->d_inode
)
1243 ndentry
= lookup_one(tname
, tdentry
);
1244 err
= PTR_ERR(ndentry
);
1245 if (IS_ERR(ndentry
))
1248 #ifdef CONFIG_NFSD_V3
1249 /* Fill in the pre-op attr for the wcc data for both
1254 #endif /* CONFIG_NFSD_V3 */
1256 err
= vfs_rename(fdir
, odentry
, tdir
, ndentry
);
1257 if (!err
&& EX_ISSYNC(tfhp
->fh_export
)) {
1258 nfsd_sync_dir(tdentry
);
1259 nfsd_sync_dir(fdentry
);
1261 #ifdef CONFIG_NFSD_V3
1262 /* Fill in the post-op attr for the wcc data for both
1265 fill_post_wcc(ffhp
);
1266 fill_post_wcc(tfhp
);
1267 #endif /* CONFIG_NFSD_V3 */
1268 double_up(&tdir
->i_sem
, &fdir
->i_sem
);
1279 err
= nfserrno(err
);
1284 * Unlink a file or directory
1285 * N.B. After this call fhp needs an fh_put
1288 nfsd_unlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, int type
,
1289 char *fname
, int flen
)
1291 struct dentry
*dentry
, *rdentry
;
1296 if (!flen
|| isdotent(fname
, flen
))
1298 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, MAY_REMOVE
);
1303 dentry
= fhp
->fh_dentry
;
1304 dirp
= dentry
->d_inode
;
1306 rdentry
= lookup_one(fname
, dentry
);
1307 err
= PTR_ERR(rdentry
);
1308 if (IS_ERR(rdentry
))
1311 if (!rdentry
->d_inode
) {
1317 if (type
!= S_IFDIR
) { /* It's UNLINK */
1318 err
= vfs_unlink(dirp
, rdentry
);
1319 } else { /* It's RMDIR */
1320 err
= vfs_rmdir(dirp
, rdentry
);
1329 if (EX_ISSYNC(fhp
->fh_export
)) {
1330 down(&dentry
->d_inode
->i_sem
);
1331 nfsd_sync_dir(dentry
);
1332 up(&dentry
->d_inode
->i_sem
);
1338 err
= nfserrno(err
);
1343 * Read entries from a directory.
1344 * The verifier is an NFSv3 thing we ignore for now.
1347 nfsd_readdir(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, loff_t offset
,
1348 encode_dent_fn func
, u32
*buffer
, int *countp
, u32
*verf
)
1350 struct inode
*inode
;
1352 int oldlen
, eof
, err
;
1354 struct readdir_cd cd
;
1357 if (offset
> ~(u32
) 0)
1360 err
= nfsd_open(rqstp
, fhp
, S_IFDIR
, MAY_READ
, &file
);
1364 err
= nfserr_notdir
;
1365 if (!file
.f_op
->readdir
)
1367 file
.f_pos
= offset
;
1369 /* Set up the readdir context */
1370 memset(&cd
, 0, sizeof(cd
));
1373 cd
.buflen
= *countp
; /* count of words */
1377 * Read the directory entries. This silly loop is necessary because
1378 * readdir() is not guaranteed to fill up the entire buffer, but
1379 * may choose to do less.
1381 inode
= file
.f_dentry
->d_inode
;
1382 down(&inode
->i_sem
);
1387 dprintk("nfsd: f_op->readdir(%x/%ld @ %d) buflen = %d (%d)\n",
1388 file.f_inode->i_dev, file.f_inode->i_ino,
1389 (int) file.f_pos, (int) oldlen, (int) cd.buflen);
1391 err
= file
.f_op
->readdir(&file
, &cd
, (filldir_t
) func
);
1394 if (oldlen
== cd
.buflen
)
1401 /* If we didn't fill the buffer completely, we're at EOF */
1405 #ifdef CONFIG_NFSD_V3
1406 if (rqstp
->rq_vers
== 3)
1407 (void)xdr_encode_hyper(cd
.offset
, file
.f_pos
);
1409 #endif /* CONFIG_NFSD_V3 */
1410 *cd
.offset
= htonl(file
.f_pos
);
1414 *p
++ = 0; /* no more entries */
1415 *p
++ = htonl(eof
); /* end of directory */
1416 *countp
= (caddr_t
) p
- (caddr_t
) buffer
;
1418 dprintk("nfsd: readdir result %d bytes, eof %d offset %d\n",
1420 cd
.offset
? ntohl(*cd
.offset
) : -1);
1429 err
= nfserrno(err
);
1434 * Get file system stats
1435 * N.B. After this call fhp needs an fh_put
1438 nfsd_statfs(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct statfs
*stat
)
1440 int err
= fh_verify(rqstp
, fhp
, 0, MAY_NOP
);
1441 if (!err
&& vfs_statfs(fhp
->fh_dentry
->d_inode
->i_sb
,stat
))
1447 * Check for a user's access permissions to this inode.
1450 nfsd_permission(struct svc_export
*exp
, struct dentry
*dentry
, int acc
)
1452 struct inode
*inode
= dentry
->d_inode
;
1454 kernel_cap_t saved_cap
= 0;
1459 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1461 (acc
& MAY_READ
)? " read" : "",
1462 (acc
& MAY_WRITE
)? " write" : "",
1463 (acc
& MAY_EXEC
)? " exec" : "",
1464 (acc
& MAY_SATTR
)? " sattr" : "",
1465 (acc
& MAY_TRUNC
)? " trunc" : "",
1466 (acc
& MAY_LOCK
)? " lock" : "",
1467 (acc
& MAY_OWNER_OVERRIDE
)? " owneroverride" : "",
1469 IS_IMMUTABLE(inode
)? " immut" : "",
1470 IS_APPEND(inode
)? " append" : "",
1471 IS_RDONLY(inode
)? " ro" : "");
1472 dprintk(" owner %d/%d user %d/%d\n",
1473 inode
->i_uid
, inode
->i_gid
, current
->fsuid
, current
->fsgid
);
1476 if (acc
& (MAY_WRITE
| MAY_SATTR
| MAY_TRUNC
)) {
1477 if (EX_RDONLY(exp
) || IS_RDONLY(inode
))
1479 if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode
))
1482 if ((acc
& MAY_TRUNC
) && IS_APPEND(inode
))
1485 if (acc
& MAY_LOCK
) {
1486 /* If we cannot rely on authentication in NLM requests,
1487 * just allow locks, otherwise require read permission, or
1490 if (exp
->ex_flags
& NFSEXP_NOAUTHNLM
)
1493 acc
= MAY_READ
| MAY_OWNER_OVERRIDE
;
1496 * The file owner always gets access permission for accesses that
1497 * would normally be checked at open time. This is to make
1498 * file access work even when the client has done a fchmod(fd, 0).
1500 * However, `cp foo bar' should fail nevertheless when bar is
1501 * readonly. A sensible way to do this might be to reject all
1502 * attempts to truncate a read-only file, because a creat() call
1503 * always implies file truncation.
1504 * ... but this isn't really fair. A process may reasonably call
1505 * ftruncate on an open file descriptor on a file with perm 000.
1506 * We must trust the client to do permission checking - using "ACCESS"
1509 if ((acc
& MAY_OWNER_OVERRIDE
) &&
1510 inode
->i_uid
== current
->fsuid
)
1513 if (current
->fsuid
!= 0) {
1514 saved_cap
= current
->cap_effective
;
1515 cap_clear(current
->cap_effective
);
1518 err
= permission(inode
, acc
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
));
1520 /* Allow read access to binaries even when mode 111 */
1521 if (err
== -EACCES
&& S_ISREG(inode
->i_mode
) && acc
== MAY_READ
)
1522 err
= permission(inode
, MAY_EXEC
);
1524 if (current
->fsuid
!= 0)
1525 current
->cap_effective
= saved_cap
;
1527 return err
? nfserrno(err
) : 0;
1531 nfsd_racache_shutdown(void)
1535 dprintk("nfsd: freeing readahead buffers.\n");
1537 raparm_cache
= raparml
= NULL
;
1540 * Initialize readahead param cache
1543 nfsd_racache_init(int cache_size
)
1549 raparml
= kmalloc(sizeof(struct raparms
) * cache_size
, GFP_KERNEL
);
1551 if (raparml
!= NULL
) {
1552 dprintk("nfsd: allocating %d readahead buffers.\n",
1554 memset(raparml
, 0, sizeof(struct raparms
) * cache_size
);
1555 for (i
= 0; i
< cache_size
- 1; i
++) {
1556 raparml
[i
].p_next
= raparml
+ i
+ 1;
1558 raparm_cache
= raparml
;
1561 "nfsd: Could not allocate memory read-ahead cache.\n");
1564 nfsdstats
.ra_size
= cache_size
;