2 * File operations used by nfsd. Some of these have been ripped from
3 * other parts of the kernel because they weren't exported, others
4 * are partial duplicates with added or changed functionality.
6 * Note that several functions dget() the dentry upon which they want
7 * to act, most notably those that create directory entries. Response
8 * dentry's are dput()'d if necessary in the release callback.
9 * So if you notice code paths that apparently fail to dput() the
10 * dentry, don't worry--they have been taken care of.
12 * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
13 * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
17 #include <linux/file.h>
18 #include <linux/splice.h>
19 #include <linux/fcntl.h>
20 #include <linux/namei.h>
21 #include <linux/delay.h>
22 #include <linux/fsnotify.h>
23 #include <linux/posix_acl_xattr.h>
24 #include <linux/xattr.h>
25 #include <linux/jhash.h>
26 #include <linux/ima.h>
27 #include <linux/slab.h>
28 #include <asm/uaccess.h>
29 #include <linux/exportfs.h>
30 #include <linux/writeback.h>
31 #include <linux/security.h>
35 #endif /* CONFIG_NFSD_V3 */
40 #endif /* CONFIG_NFSD_V4 */
45 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
49 * This is a cache of readahead params that help us choose the proper
50 * readahead strategy. Initially, we set all readahead parameters to 0
51 * and let the VFS handle things.
52 * If you increase the number of cached files very much, you'll need to
53 * add a hash table here.
56 struct raparms
*p_next
;
61 struct file_ra_state p_ra
;
62 unsigned int p_hindex
;
65 struct raparm_hbucket
{
66 struct raparms
*pb_head
;
68 } ____cacheline_aligned_in_smp
;
70 #define RAPARM_HASH_BITS 4
71 #define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
72 #define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
73 static struct raparm_hbucket raparm_hash
[RAPARM_HASH_SIZE
];
76 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
78 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
79 * or nfs_ok having possibly changed *dpp and *expp
82 nfsd_cross_mnt(struct svc_rqst
*rqstp
, struct dentry
**dpp
,
83 struct svc_export
**expp
)
85 struct svc_export
*exp
= *expp
, *exp2
= NULL
;
86 struct dentry
*dentry
= *dpp
;
87 struct path path
= {.mnt
= mntget(exp
->ex_path
.mnt
),
88 .dentry
= dget(dentry
)};
91 err
= follow_down(&path
);
95 exp2
= rqst_exp_get_by_name(rqstp
, &path
);
99 * We normally allow NFS clients to continue
100 * "underneath" a mountpoint that is not exported.
101 * The exception is V4ROOT, where no traversal is ever
102 * allowed without an explicit export of the new
105 if (err
== -ENOENT
&& !(exp
->ex_flags
& NFSEXP_V4ROOT
))
110 if (nfsd_v4client(rqstp
) ||
111 (exp
->ex_flags
& NFSEXP_CROSSMOUNT
) || EX_NOHIDE(exp2
)) {
112 /* successfully crossed mount point */
114 * This is subtle: path.dentry is *not* on path.mnt
115 * at this point. The only reason we are safe is that
116 * original mnt is pinned down by exp, so we should
117 * put path *before* putting exp
120 path
.dentry
= dentry
;
130 static void follow_to_parent(struct path
*path
)
134 while (path
->dentry
== path
->mnt
->mnt_root
&& follow_up(path
))
136 dp
= dget_parent(path
->dentry
);
141 static int nfsd_lookup_parent(struct svc_rqst
*rqstp
, struct dentry
*dparent
, struct svc_export
**exp
, struct dentry
**dentryp
)
143 struct svc_export
*exp2
;
144 struct path path
= {.mnt
= mntget((*exp
)->ex_path
.mnt
),
145 .dentry
= dget(dparent
)};
147 follow_to_parent(&path
);
149 exp2
= rqst_exp_parent(rqstp
, &path
);
150 if (PTR_ERR(exp2
) == -ENOENT
) {
151 *dentryp
= dget(dparent
);
152 } else if (IS_ERR(exp2
)) {
154 return PTR_ERR(exp2
);
156 *dentryp
= dget(path
.dentry
);
165 * For nfsd purposes, we treat V4ROOT exports as though there was an
166 * export at *every* directory.
168 int nfsd_mountpoint(struct dentry
*dentry
, struct svc_export
*exp
)
170 if (d_mountpoint(dentry
))
172 if (nfsd4_is_junction(dentry
))
174 if (!(exp
->ex_flags
& NFSEXP_V4ROOT
))
176 return dentry
->d_inode
!= NULL
;
180 nfsd_lookup_dentry(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
181 const char *name
, unsigned int len
,
182 struct svc_export
**exp_ret
, struct dentry
**dentry_ret
)
184 struct svc_export
*exp
;
185 struct dentry
*dparent
;
186 struct dentry
*dentry
;
189 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp
), len
,name
);
191 dparent
= fhp
->fh_dentry
;
192 exp
= exp_get(fhp
->fh_export
);
194 /* Lookup the name, but don't follow links */
195 if (isdotent(name
, len
)) {
197 dentry
= dget(dparent
);
198 else if (dparent
!= exp
->ex_path
.dentry
)
199 dentry
= dget_parent(dparent
);
200 else if (!EX_NOHIDE(exp
) && !nfsd_v4client(rqstp
))
201 dentry
= dget(dparent
); /* .. == . just like at / */
203 /* checking mountpoint crossing is very different when stepping up */
204 host_err
= nfsd_lookup_parent(rqstp
, dparent
, &exp
, &dentry
);
210 * In the nfsd4_open() case, this may be held across
211 * subsequent open and delegation acquisition which may
212 * need to take the child's i_mutex:
214 fh_lock_nested(fhp
, I_MUTEX_PARENT
);
215 dentry
= lookup_one_len(name
, dparent
, len
);
216 host_err
= PTR_ERR(dentry
);
220 * check if we have crossed a mount point ...
222 if (nfsd_mountpoint(dentry
, exp
)) {
223 if ((host_err
= nfsd_cross_mnt(rqstp
, &dentry
, &exp
))) {
229 *dentry_ret
= dentry
;
235 return nfserrno(host_err
);
239 * Look up one component of a pathname.
240 * N.B. After this call _both_ fhp and resfh need an fh_put
242 * If the lookup would cross a mountpoint, and the mounted filesystem
243 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
244 * accepted as it stands and the mounted directory is
245 * returned. Otherwise the covered directory is returned.
246 * NOTE: this mountpoint crossing is not supported properly by all
247 * clients and is explicitly disallowed for NFSv3
248 * NeilBrown <neilb@cse.unsw.edu.au>
251 nfsd_lookup(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, const char *name
,
252 unsigned int len
, struct svc_fh
*resfh
)
254 struct svc_export
*exp
;
255 struct dentry
*dentry
;
258 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_EXEC
);
261 err
= nfsd_lookup_dentry(rqstp
, fhp
, name
, len
, &exp
, &dentry
);
264 err
= check_nfsd_access(exp
, rqstp
);
268 * Note: we compose the file handle now, but as the
269 * dentry may be negative, it may need to be updated.
271 err
= fh_compose(resfh
, exp
, dentry
, fhp
);
272 if (!err
&& !dentry
->d_inode
)
281 * Commit metadata changes to stable storage.
284 commit_metadata(struct svc_fh
*fhp
)
286 struct inode
*inode
= fhp
->fh_dentry
->d_inode
;
287 const struct export_operations
*export_ops
= inode
->i_sb
->s_export_op
;
289 if (!EX_ISSYNC(fhp
->fh_export
))
292 if (export_ops
->commit_metadata
)
293 return export_ops
->commit_metadata(inode
);
294 return sync_inode_metadata(inode
, 1);
298 * Go over the attributes and take care of the small differences between
299 * NFS semantics and what Linux expects.
302 nfsd_sanitize_attrs(struct inode
*inode
, struct iattr
*iap
)
305 * NFSv2 does not differentiate between "set-[ac]time-to-now"
306 * which only requires access, and "set-[ac]time-to-X" which
307 * requires ownership.
308 * So if it looks like it might be "set both to the same time which
309 * is close to now", and if inode_change_ok fails, then we
310 * convert to "set to now" instead of "set to explicit time"
312 * We only call inode_change_ok as the last test as technically
313 * it is not an interface that we should be using.
315 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
316 #define MAX_TOUCH_TIME_ERROR (30*60)
317 if ((iap
->ia_valid
& BOTH_TIME_SET
) == BOTH_TIME_SET
&&
318 iap
->ia_mtime
.tv_sec
== iap
->ia_atime
.tv_sec
) {
322 * Now just make sure time is in the right ballpark.
323 * Solaris, at least, doesn't seem to care what the time
324 * request is. We require it be within 30 minutes of now.
326 time_t delta
= iap
->ia_atime
.tv_sec
- get_seconds();
329 if (delta
< MAX_TOUCH_TIME_ERROR
&&
330 inode_change_ok(inode
, iap
) != 0) {
332 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
333 * This will cause notify_change to set these times
336 iap
->ia_valid
&= ~BOTH_TIME_SET
;
340 /* sanitize the mode change */
341 if (iap
->ia_valid
& ATTR_MODE
) {
342 iap
->ia_mode
&= S_IALLUGO
;
343 iap
->ia_mode
|= (inode
->i_mode
& ~S_IALLUGO
);
346 /* Revoke setuid/setgid on chown */
347 if (!S_ISDIR(inode
->i_mode
) &&
348 ((iap
->ia_valid
& ATTR_UID
) || (iap
->ia_valid
& ATTR_GID
))) {
349 iap
->ia_valid
|= ATTR_KILL_PRIV
;
350 if (iap
->ia_valid
& ATTR_MODE
) {
351 /* we're setting mode too, just clear the s*id bits */
352 iap
->ia_mode
&= ~S_ISUID
;
353 if (iap
->ia_mode
& S_IXGRP
)
354 iap
->ia_mode
&= ~S_ISGID
;
356 /* set ATTR_KILL_* bits and let VFS handle it */
357 iap
->ia_valid
|= (ATTR_KILL_SUID
| ATTR_KILL_SGID
);
363 nfsd_get_write_access(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
366 struct inode
*inode
= fhp
->fh_dentry
->d_inode
;
369 if (iap
->ia_size
< inode
->i_size
) {
372 err
= nfsd_permission(rqstp
, fhp
->fh_export
, fhp
->fh_dentry
,
373 NFSD_MAY_TRUNC
| NFSD_MAY_OWNER_OVERRIDE
);
378 host_err
= get_write_access(inode
);
382 host_err
= locks_verify_truncate(inode
, NULL
, iap
->ia_size
);
384 goto out_put_write_access
;
387 out_put_write_access
:
388 put_write_access(inode
);
390 return nfserrno(host_err
);
394 * Set various file attributes. After this call fhp needs an fh_put.
397 nfsd_setattr(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct iattr
*iap
,
398 int check_guard
, time_t guardtime
)
400 struct dentry
*dentry
;
402 int accmode
= NFSD_MAY_SATTR
;
406 bool get_write_count
;
409 if (iap
->ia_valid
& (ATTR_ATIME
| ATTR_MTIME
| ATTR_SIZE
))
410 accmode
|= NFSD_MAY_WRITE
|NFSD_MAY_OWNER_OVERRIDE
;
411 if (iap
->ia_valid
& ATTR_SIZE
)
414 /* Callers that do fh_verify should do the fh_want_write: */
415 get_write_count
= !fhp
->fh_dentry
;
418 err
= fh_verify(rqstp
, fhp
, ftype
, accmode
);
421 if (get_write_count
) {
422 host_err
= fh_want_write(fhp
);
424 return nfserrno(host_err
);
427 dentry
= fhp
->fh_dentry
;
428 inode
= dentry
->d_inode
;
430 /* Ignore any mode updates on symlinks */
431 if (S_ISLNK(inode
->i_mode
))
432 iap
->ia_valid
&= ~ATTR_MODE
;
437 nfsd_sanitize_attrs(inode
, iap
);
440 * The size case is special, it changes the file in addition to the
443 if (iap
->ia_valid
& ATTR_SIZE
) {
444 err
= nfsd_get_write_access(rqstp
, fhp
, iap
);
450 * RFC5661, Section 18.30.4:
451 * Changing the size of a file with SETATTR indirectly
452 * changes the time_modify and change attributes.
454 * (and similar for the older RFCs)
456 if (iap
->ia_size
!= i_size_read(inode
))
457 iap
->ia_valid
|= ATTR_MTIME
;
460 iap
->ia_valid
|= ATTR_CTIME
;
462 if (check_guard
&& guardtime
!= inode
->i_ctime
.tv_sec
) {
463 err
= nfserr_notsync
;
464 goto out_put_write_access
;
468 host_err
= notify_change(dentry
, iap
, NULL
);
470 err
= nfserrno(host_err
);
472 out_put_write_access
:
474 put_write_access(inode
);
476 err
= nfserrno(commit_metadata(fhp
));
481 #if defined(CONFIG_NFSD_V4)
483 * NFS junction information is stored in an extended attribute.
485 #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
488 * nfsd4_is_junction - Test if an object could be an NFS junction
490 * @dentry: object to test
492 * Returns 1 if "dentry" appears to contain NFS junction information.
493 * Otherwise 0 is returned.
495 int nfsd4_is_junction(struct dentry
*dentry
)
497 struct inode
*inode
= dentry
->d_inode
;
501 if (inode
->i_mode
& S_IXUGO
)
503 if (!(inode
->i_mode
& S_ISVTX
))
505 if (vfs_getxattr(dentry
, NFSD_JUNCTION_XATTR_NAME
, NULL
, 0) <= 0)
509 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
510 __be32
nfsd4_set_nfs4_label(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
511 struct xdr_netobj
*label
)
515 struct dentry
*dentry
;
517 error
= fh_verify(rqstp
, fhp
, 0 /* S_IFREG */, NFSD_MAY_SATTR
);
521 dentry
= fhp
->fh_dentry
;
523 mutex_lock(&dentry
->d_inode
->i_mutex
);
524 host_error
= security_inode_setsecctx(dentry
, label
->data
, label
->len
);
525 mutex_unlock(&dentry
->d_inode
->i_mutex
);
526 return nfserrno(host_error
);
529 __be32
nfsd4_set_nfs4_label(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
530 struct xdr_netobj
*label
)
532 return nfserr_notsupp
;
536 #endif /* defined(CONFIG_NFSD_V4) */
538 #ifdef CONFIG_NFSD_V3
540 * Check server access rights to a file system object
546 static struct accessmap nfs3_regaccess
[] = {
547 { NFS3_ACCESS_READ
, NFSD_MAY_READ
},
548 { NFS3_ACCESS_EXECUTE
, NFSD_MAY_EXEC
},
549 { NFS3_ACCESS_MODIFY
, NFSD_MAY_WRITE
|NFSD_MAY_TRUNC
},
550 { NFS3_ACCESS_EXTEND
, NFSD_MAY_WRITE
},
555 static struct accessmap nfs3_diraccess
[] = {
556 { NFS3_ACCESS_READ
, NFSD_MAY_READ
},
557 { NFS3_ACCESS_LOOKUP
, NFSD_MAY_EXEC
},
558 { NFS3_ACCESS_MODIFY
, NFSD_MAY_EXEC
|NFSD_MAY_WRITE
|NFSD_MAY_TRUNC
},
559 { NFS3_ACCESS_EXTEND
, NFSD_MAY_EXEC
|NFSD_MAY_WRITE
},
560 { NFS3_ACCESS_DELETE
, NFSD_MAY_REMOVE
},
565 static struct accessmap nfs3_anyaccess
[] = {
566 /* Some clients - Solaris 2.6 at least, make an access call
567 * to the server to check for access for things like /dev/null
568 * (which really, the server doesn't care about). So
569 * We provide simple access checking for them, looking
570 * mainly at mode bits, and we make sure to ignore read-only
573 { NFS3_ACCESS_READ
, NFSD_MAY_READ
},
574 { NFS3_ACCESS_EXECUTE
, NFSD_MAY_EXEC
},
575 { NFS3_ACCESS_MODIFY
, NFSD_MAY_WRITE
|NFSD_MAY_LOCAL_ACCESS
},
576 { NFS3_ACCESS_EXTEND
, NFSD_MAY_WRITE
|NFSD_MAY_LOCAL_ACCESS
},
582 nfsd_access(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, u32
*access
, u32
*supported
)
584 struct accessmap
*map
;
585 struct svc_export
*export
;
586 struct dentry
*dentry
;
587 u32 query
, result
= 0, sresult
= 0;
590 error
= fh_verify(rqstp
, fhp
, 0, NFSD_MAY_NOP
);
594 export
= fhp
->fh_export
;
595 dentry
= fhp
->fh_dentry
;
597 if (S_ISREG(dentry
->d_inode
->i_mode
))
598 map
= nfs3_regaccess
;
599 else if (S_ISDIR(dentry
->d_inode
->i_mode
))
600 map
= nfs3_diraccess
;
602 map
= nfs3_anyaccess
;
606 for (; map
->access
; map
++) {
607 if (map
->access
& query
) {
610 sresult
|= map
->access
;
612 err2
= nfsd_permission(rqstp
, export
, dentry
, map
->how
);
615 result
|= map
->access
;
618 /* the following error codes just mean the access was not allowed,
619 * rather than an error occurred */
623 /* simply don't "or" in the access bit. */
633 *supported
= sresult
;
638 #endif /* CONFIG_NFSD_V3 */
640 static int nfsd_open_break_lease(struct inode
*inode
, int access
)
644 if (access
& NFSD_MAY_NOT_BREAK_LEASE
)
646 mode
= (access
& NFSD_MAY_WRITE
) ? O_WRONLY
: O_RDONLY
;
647 return break_lease(inode
, mode
| O_NONBLOCK
);
651 * Open an existing file or directory.
652 * The may_flags argument indicates the type of open (read/write/lock)
653 * and additional flags.
654 * N.B. After this call fhp needs an fh_put
657 nfsd_open(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, umode_t type
,
658 int may_flags
, struct file
**filp
)
663 int flags
= O_RDONLY
|O_LARGEFILE
;
667 validate_process_creds();
670 * If we get here, then the client has already done an "open",
671 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
672 * in case a chmod has now revoked permission.
674 * Arguably we should also allow the owner override for
675 * directories, but we never have and it doesn't seem to have
676 * caused anyone a problem. If we were to change this, note
677 * also that our filldir callbacks would need a variant of
678 * lookup_one_len that doesn't check permissions.
681 may_flags
|= NFSD_MAY_OWNER_OVERRIDE
;
682 err
= fh_verify(rqstp
, fhp
, type
, may_flags
);
686 path
.mnt
= fhp
->fh_export
->ex_path
.mnt
;
687 path
.dentry
= fhp
->fh_dentry
;
688 inode
= path
.dentry
->d_inode
;
690 /* Disallow write access to files with the append-only bit set
691 * or any access when mandatory locking enabled
694 if (IS_APPEND(inode
) && (may_flags
& NFSD_MAY_WRITE
))
697 * We must ignore files (but only files) which might have mandatory
698 * locks on them because there is no way to know if the accesser has
701 if (S_ISREG((inode
)->i_mode
) && mandatory_lock(inode
))
707 host_err
= nfsd_open_break_lease(inode
, may_flags
);
708 if (host_err
) /* NOMEM or WOULDBLOCK */
711 if (may_flags
& NFSD_MAY_WRITE
) {
712 if (may_flags
& NFSD_MAY_READ
)
713 flags
= O_RDWR
|O_LARGEFILE
;
715 flags
= O_WRONLY
|O_LARGEFILE
;
718 file
= dentry_open(&path
, flags
, current_cred());
720 host_err
= PTR_ERR(file
);
724 host_err
= ima_file_check(file
, may_flags
, 0);
730 if (may_flags
& NFSD_MAY_64BIT_COOKIE
)
731 file
->f_mode
|= FMODE_64BITHASH
;
733 file
->f_mode
|= FMODE_32BITHASH
;
737 err
= nfserrno(host_err
);
739 validate_process_creds();
747 nfsd_close(struct file
*filp
)
753 * Obtain the readahead parameters for the file
754 * specified by (dev, ino).
757 static inline struct raparms
*
758 nfsd_get_raparms(dev_t dev
, ino_t ino
)
760 struct raparms
*ra
, **rap
, **frap
= NULL
;
763 struct raparm_hbucket
*rab
;
765 hash
= jhash_2words(dev
, ino
, 0xfeedbeef) & RAPARM_HASH_MASK
;
766 rab
= &raparm_hash
[hash
];
768 spin_lock(&rab
->pb_lock
);
769 for (rap
= &rab
->pb_head
; (ra
= *rap
); rap
= &ra
->p_next
) {
770 if (ra
->p_ino
== ino
&& ra
->p_dev
== dev
)
773 if (ra
->p_count
== 0)
776 depth
= nfsdstats
.ra_size
;
778 spin_unlock(&rab
->pb_lock
);
788 if (rap
!= &rab
->pb_head
) {
790 ra
->p_next
= rab
->pb_head
;
794 nfsdstats
.ra_depth
[depth
*10/nfsdstats
.ra_size
]++;
795 spin_unlock(&rab
->pb_lock
);
800 * Grab and keep cached pages associated with a file in the svc_rqst
801 * so that they can be passed to the network sendmsg/sendpage routines
802 * directly. They will be released after the sending has completed.
805 nfsd_splice_actor(struct pipe_inode_info
*pipe
, struct pipe_buffer
*buf
,
806 struct splice_desc
*sd
)
808 struct svc_rqst
*rqstp
= sd
->u
.data
;
809 struct page
**pp
= rqstp
->rq_next_page
;
810 struct page
*page
= buf
->page
;
815 if (rqstp
->rq_res
.page_len
== 0) {
817 put_page(*rqstp
->rq_next_page
);
818 *(rqstp
->rq_next_page
++) = page
;
819 rqstp
->rq_res
.page_base
= buf
->offset
;
820 rqstp
->rq_res
.page_len
= size
;
821 } else if (page
!= pp
[-1]) {
823 if (*rqstp
->rq_next_page
)
824 put_page(*rqstp
->rq_next_page
);
825 *(rqstp
->rq_next_page
++) = page
;
826 rqstp
->rq_res
.page_len
+= size
;
828 rqstp
->rq_res
.page_len
+= size
;
833 static int nfsd_direct_splice_actor(struct pipe_inode_info
*pipe
,
834 struct splice_desc
*sd
)
836 return __splice_from_pipe(pipe
, sd
, nfsd_splice_actor
);
840 nfsd_finish_read(struct file
*file
, unsigned long *count
, int host_err
)
843 nfsdstats
.io_read
+= host_err
;
845 fsnotify_access(file
);
848 return nfserrno(host_err
);
851 __be32
nfsd_splice_read(struct svc_rqst
*rqstp
,
852 struct file
*file
, loff_t offset
, unsigned long *count
)
854 struct splice_desc sd
= {
862 rqstp
->rq_next_page
= rqstp
->rq_respages
+ 1;
863 host_err
= splice_direct_to_actor(file
, &sd
, nfsd_direct_splice_actor
);
864 return nfsd_finish_read(file
, count
, host_err
);
867 __be32
nfsd_readv(struct file
*file
, loff_t offset
, struct kvec
*vec
, int vlen
,
868 unsigned long *count
)
875 host_err
= vfs_readv(file
, (struct iovec __user
*)vec
, vlen
, &offset
);
877 return nfsd_finish_read(file
, count
, host_err
);
881 nfsd_vfs_read(struct svc_rqst
*rqstp
, struct file
*file
,
882 loff_t offset
, struct kvec
*vec
, int vlen
, unsigned long *count
)
884 if (file
->f_op
->splice_read
&& rqstp
->rq_splice_ok
)
885 return nfsd_splice_read(rqstp
, file
, offset
, count
);
887 return nfsd_readv(file
, offset
, vec
, vlen
, count
);
891 * Gathered writes: If another process is currently writing to the file,
892 * there's a high chance this is another nfsd (triggered by a bulk write
893 * from a client's biod). Rather than syncing the file with each write
894 * request, we sleep for 10 msec.
896 * I don't know if this roughly approximates C. Juszak's idea of
897 * gathered writes, but it's a nice and simple solution (IMHO), and it
900 * Note: we do this only in the NFSv2 case, since v3 and higher have a
901 * better tool (separate unstable writes and commits) for solving this
904 static int wait_for_concurrent_writes(struct file
*file
)
906 struct inode
*inode
= file_inode(file
);
907 static ino_t last_ino
;
908 static dev_t last_dev
;
911 if (atomic_read(&inode
->i_writecount
) > 1
912 || (last_ino
== inode
->i_ino
&& last_dev
== inode
->i_sb
->s_dev
)) {
913 dprintk("nfsd: write defer %d\n", task_pid_nr(current
));
915 dprintk("nfsd: write resume %d\n", task_pid_nr(current
));
918 if (inode
->i_state
& I_DIRTY
) {
919 dprintk("nfsd: write sync %d\n", task_pid_nr(current
));
920 err
= vfs_fsync(file
, 0);
922 last_ino
= inode
->i_ino
;
923 last_dev
= inode
->i_sb
->s_dev
;
928 nfsd_vfs_write(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file
*file
,
929 loff_t offset
, struct kvec
*vec
, int vlen
,
930 unsigned long *cnt
, int *stablep
)
932 struct svc_export
*exp
;
933 struct dentry
*dentry
;
938 int stable
= *stablep
;
941 unsigned int pflags
= current
->flags
;
945 * We want less throttling in balance_dirty_pages()
946 * and shrink_inactive_list() so that nfs to
947 * localhost doesn't cause nfsd to lock up due to all
948 * the client's dirty pages or its congested queue.
950 current
->flags
|= PF_LESS_THROTTLE
;
952 dentry
= file
->f_path
.dentry
;
953 inode
= dentry
->d_inode
;
954 exp
= fhp
->fh_export
;
956 use_wgather
= (rqstp
->rq_vers
== 2) && EX_WGATHER(exp
);
961 /* Write the data. */
962 oldfs
= get_fs(); set_fs(KERNEL_DS
);
963 host_err
= vfs_writev(file
, (struct iovec __user
*)vec
, vlen
, &pos
);
968 nfsdstats
.io_write
+= host_err
;
969 fsnotify_modify(file
);
973 host_err
= wait_for_concurrent_writes(file
);
975 host_err
= vfs_fsync_range(file
, offset
, offset
+*cnt
, 0);
979 dprintk("nfsd: write complete host_err=%d\n", host_err
);
983 err
= nfserrno(host_err
);
985 tsk_restore_flags(current
, pflags
, PF_LESS_THROTTLE
);
989 __be32
nfsd_get_tmp_read_open(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
990 struct file
**file
, struct raparms
**ra
)
995 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, file
);
999 inode
= file_inode(*file
);
1001 /* Get readahead parameters */
1002 *ra
= nfsd_get_raparms(inode
->i_sb
->s_dev
, inode
->i_ino
);
1004 if (*ra
&& (*ra
)->p_set
)
1005 (*file
)->f_ra
= (*ra
)->p_ra
;
1009 void nfsd_put_tmp_read_open(struct file
*file
, struct raparms
*ra
)
1011 /* Write back readahead params */
1013 struct raparm_hbucket
*rab
= &raparm_hash
[ra
->p_hindex
];
1014 spin_lock(&rab
->pb_lock
);
1015 ra
->p_ra
= file
->f_ra
;
1018 spin_unlock(&rab
->pb_lock
);
1024 * Read data from a file. count must contain the requested read count
1025 * on entry. On return, *count contains the number of bytes actually read.
1026 * N.B. After this call fhp needs an fh_put
1028 __be32
nfsd_read(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1029 loff_t offset
, struct kvec
*vec
, int vlen
, unsigned long *count
)
1035 err
= nfsd_get_tmp_read_open(rqstp
, fhp
, &file
, &ra
);
1039 err
= nfsd_vfs_read(rqstp
, file
, offset
, vec
, vlen
, count
);
1041 nfsd_put_tmp_read_open(file
, ra
);
1047 * Write data to a file.
1048 * The stable flag requests synchronous writes.
1049 * N.B. After this call fhp needs an fh_put
1052 nfsd_write(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file
*file
,
1053 loff_t offset
, struct kvec
*vec
, int vlen
, unsigned long *cnt
,
1059 err
= nfsd_permission(rqstp
, fhp
->fh_export
, fhp
->fh_dentry
,
1060 NFSD_MAY_WRITE
|NFSD_MAY_OWNER_OVERRIDE
);
1063 err
= nfsd_vfs_write(rqstp
, fhp
, file
, offset
, vec
, vlen
, cnt
,
1066 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_WRITE
, &file
);
1071 err
= nfsd_vfs_write(rqstp
, fhp
, file
, offset
, vec
, vlen
,
1079 #ifdef CONFIG_NFSD_V3
1081 * Commit all pending writes to stable storage.
1083 * Note: we only guarantee that data that lies within the range specified
1084 * by the 'offset' and 'count' parameters will be synced.
1086 * Unfortunately we cannot lock the file to make sure we return full WCC
1087 * data to the client, as locking happens lower down in the filesystem.
1090 nfsd_commit(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1091 loff_t offset
, unsigned long count
)
1094 loff_t end
= LLONG_MAX
;
1095 __be32 err
= nfserr_inval
;
1100 end
= offset
+ (loff_t
)count
- 1;
1105 err
= nfsd_open(rqstp
, fhp
, S_IFREG
,
1106 NFSD_MAY_WRITE
|NFSD_MAY_NOT_BREAK_LEASE
, &file
);
1109 if (EX_ISSYNC(fhp
->fh_export
)) {
1110 int err2
= vfs_fsync_range(file
, offset
, end
, 0);
1112 if (err2
!= -EINVAL
)
1113 err
= nfserrno(err2
);
1115 err
= nfserr_notsupp
;
1122 #endif /* CONFIG_NFSD_V3 */
1125 nfsd_create_setattr(struct svc_rqst
*rqstp
, struct svc_fh
*resfhp
,
1129 * Mode has already been set earlier in create:
1131 iap
->ia_valid
&= ~ATTR_MODE
;
1133 * Setting uid/gid works only for root. Irix appears to
1134 * send along the gid on create when it tries to implement
1135 * setgid directories via NFS:
1137 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID
))
1138 iap
->ia_valid
&= ~(ATTR_UID
|ATTR_GID
);
1140 return nfsd_setattr(rqstp
, resfhp
, iap
, 0, (time_t)0);
1141 /* Callers expect file metadata to be committed here */
1142 return nfserrno(commit_metadata(resfhp
));
1145 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1146 * setting size to 0 may fail for some specific file systems by the permission
1147 * checking which requires WRITE permission but the mode is 000.
1148 * we ignore the resizing(to 0) on the just new created file, since the size is
1149 * 0 after file created.
1151 * call this only after vfs_create() is called.
1154 nfsd_check_ignore_resizing(struct iattr
*iap
)
1156 if ((iap
->ia_valid
& ATTR_SIZE
) && (iap
->ia_size
== 0))
1157 iap
->ia_valid
&= ~ATTR_SIZE
;
1161 * Create a file (regular, directory, device, fifo); UNIX sockets
1162 * not yet implemented.
1163 * If the response fh has been verified, the parent directory should
1164 * already be locked. Note that the parent directory is left locked.
1166 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1169 nfsd_create(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1170 char *fname
, int flen
, struct iattr
*iap
,
1171 int type
, dev_t rdev
, struct svc_fh
*resfhp
)
1173 struct dentry
*dentry
, *dchild
= NULL
;
1183 if (isdotent(fname
, flen
))
1186 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1190 dentry
= fhp
->fh_dentry
;
1191 dirp
= dentry
->d_inode
;
1193 err
= nfserr_notdir
;
1194 if (!dirp
->i_op
->lookup
)
1197 * Check whether the response file handle has been verified yet.
1198 * If it has, the parent directory should already be locked.
1200 if (!resfhp
->fh_dentry
) {
1201 host_err
= fh_want_write(fhp
);
1205 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
1206 fh_lock_nested(fhp
, I_MUTEX_PARENT
);
1207 dchild
= lookup_one_len(fname
, dentry
, flen
);
1208 host_err
= PTR_ERR(dchild
);
1211 err
= fh_compose(resfhp
, fhp
->fh_export
, dchild
, fhp
);
1215 /* called from nfsd_proc_create */
1216 dchild
= dget(resfhp
->fh_dentry
);
1217 if (!fhp
->fh_locked
) {
1218 /* not actually possible */
1220 "nfsd_create: parent %pd2 not locked!\n",
1227 * Make sure the child dentry is still negative ...
1230 if (dchild
->d_inode
) {
1231 dprintk("nfsd_create: dentry %pd/%pd not negative!\n",
1236 if (!(iap
->ia_valid
& ATTR_MODE
))
1238 iap
->ia_mode
= (iap
->ia_mode
& S_IALLUGO
) | type
;
1241 if (!S_ISREG(type
) && !S_ISDIR(type
) && !special_file(type
)) {
1242 printk(KERN_WARNING
"nfsd: bad file type %o in nfsd_create\n",
1248 * Get the dir op function pointer.
1254 host_err
= vfs_create(dirp
, dchild
, iap
->ia_mode
, true);
1256 nfsd_check_ignore_resizing(iap
);
1259 host_err
= vfs_mkdir(dirp
, dchild
, iap
->ia_mode
);
1265 host_err
= vfs_mknod(dirp
, dchild
, iap
->ia_mode
, rdev
);
1271 err
= nfsd_create_setattr(rqstp
, resfhp
, iap
);
1274 * nfsd_create_setattr already committed the child. Transactional
1275 * filesystems had a chance to commit changes for both parent and
1276 * child * simultaneously making the following commit_metadata a
1279 err2
= nfserrno(commit_metadata(fhp
));
1283 * Update the file handle to get the new inode info.
1286 err
= fh_update(resfhp
);
1288 if (dchild
&& !IS_ERR(dchild
))
1293 err
= nfserrno(host_err
);
1297 #ifdef CONFIG_NFSD_V3
1299 static inline int nfsd_create_is_exclusive(int createmode
)
1301 return createmode
== NFS3_CREATE_EXCLUSIVE
1302 || createmode
== NFS4_CREATE_EXCLUSIVE4_1
;
1306 * NFSv3 and NFSv4 version of nfsd_create
1309 do_nfsd_create(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1310 char *fname
, int flen
, struct iattr
*iap
,
1311 struct svc_fh
*resfhp
, int createmode
, u32
*verifier
,
1312 bool *truncp
, bool *created
)
1314 struct dentry
*dentry
, *dchild
= NULL
;
1318 __u32 v_mtime
=0, v_atime
=0;
1324 if (isdotent(fname
, flen
))
1326 if (!(iap
->ia_valid
& ATTR_MODE
))
1328 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_EXEC
);
1332 dentry
= fhp
->fh_dentry
;
1333 dirp
= dentry
->d_inode
;
1335 /* Get all the sanity checks out of the way before
1336 * we lock the parent. */
1337 err
= nfserr_notdir
;
1338 if (!dirp
->i_op
->lookup
)
1341 host_err
= fh_want_write(fhp
);
1345 fh_lock_nested(fhp
, I_MUTEX_PARENT
);
1348 * Compose the response file handle.
1350 dchild
= lookup_one_len(fname
, dentry
, flen
);
1351 host_err
= PTR_ERR(dchild
);
1355 /* If file doesn't exist, check for permissions to create one */
1356 if (!dchild
->d_inode
) {
1357 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1362 err
= fh_compose(resfhp
, fhp
->fh_export
, dchild
, fhp
);
1366 if (nfsd_create_is_exclusive(createmode
)) {
1367 /* solaris7 gets confused (bugid 4218508) if these have
1368 * the high bit set, so just clear the high bits. If this is
1369 * ever changed to use different attrs for storing the
1370 * verifier, then do_open_lookup() will also need to be fixed
1373 v_mtime
= verifier
[0]&0x7fffffff;
1374 v_atime
= verifier
[1]&0x7fffffff;
1377 if (dchild
->d_inode
) {
1380 switch (createmode
) {
1381 case NFS3_CREATE_UNCHECKED
:
1382 if (! S_ISREG(dchild
->d_inode
->i_mode
))
1385 /* in nfsv4, we need to treat this case a little
1386 * differently. we don't want to truncate the
1387 * file now; this would be wrong if the OPEN
1388 * fails for some other reason. furthermore,
1389 * if the size is nonzero, we should ignore it
1390 * according to spec!
1392 *truncp
= (iap
->ia_valid
& ATTR_SIZE
) && !iap
->ia_size
;
1395 iap
->ia_valid
&= ATTR_SIZE
;
1399 case NFS3_CREATE_EXCLUSIVE
:
1400 if ( dchild
->d_inode
->i_mtime
.tv_sec
== v_mtime
1401 && dchild
->d_inode
->i_atime
.tv_sec
== v_atime
1402 && dchild
->d_inode
->i_size
== 0 ) {
1407 case NFS4_CREATE_EXCLUSIVE4_1
:
1408 if ( dchild
->d_inode
->i_mtime
.tv_sec
== v_mtime
1409 && dchild
->d_inode
->i_atime
.tv_sec
== v_atime
1410 && dchild
->d_inode
->i_size
== 0 ) {
1416 case NFS3_CREATE_GUARDED
:
1423 host_err
= vfs_create(dirp
, dchild
, iap
->ia_mode
, true);
1431 nfsd_check_ignore_resizing(iap
);
1433 if (nfsd_create_is_exclusive(createmode
)) {
1434 /* Cram the verifier into atime/mtime */
1435 iap
->ia_valid
= ATTR_MTIME
|ATTR_ATIME
1436 | ATTR_MTIME_SET
|ATTR_ATIME_SET
;
1437 /* XXX someone who knows this better please fix it for nsec */
1438 iap
->ia_mtime
.tv_sec
= v_mtime
;
1439 iap
->ia_atime
.tv_sec
= v_atime
;
1440 iap
->ia_mtime
.tv_nsec
= 0;
1441 iap
->ia_atime
.tv_nsec
= 0;
1445 err
= nfsd_create_setattr(rqstp
, resfhp
, iap
);
1448 * nfsd_create_setattr already committed the child
1449 * (and possibly also the parent).
1452 err
= nfserrno(commit_metadata(fhp
));
1455 * Update the filehandle to get the new inode info.
1458 err
= fh_update(resfhp
);
1462 if (dchild
&& !IS_ERR(dchild
))
1468 err
= nfserrno(host_err
);
1471 #endif /* CONFIG_NFSD_V3 */
1474 * Read a symlink. On entry, *lenp must contain the maximum path length that
1475 * fits into the buffer. On return, it contains the true length.
1476 * N.B. After this call fhp needs an fh_put
1479 nfsd_readlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, char *buf
, int *lenp
)
1481 struct inode
*inode
;
1487 err
= fh_verify(rqstp
, fhp
, S_IFLNK
, NFSD_MAY_NOP
);
1491 path
.mnt
= fhp
->fh_export
->ex_path
.mnt
;
1492 path
.dentry
= fhp
->fh_dentry
;
1493 inode
= path
.dentry
->d_inode
;
1496 if (!inode
->i_op
->readlink
)
1500 /* N.B. Why does this call need a get_fs()??
1501 * Remove the set_fs and watch the fireworks:-) --okir
1504 oldfs
= get_fs(); set_fs(KERNEL_DS
);
1505 host_err
= inode
->i_op
->readlink(path
.dentry
, (char __user
*)buf
, *lenp
);
1516 err
= nfserrno(host_err
);
1521 * Create a symlink and look up its inode
1522 * N.B. After this call _both_ fhp and resfhp need an fh_put
1525 nfsd_symlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1526 char *fname
, int flen
,
1528 struct svc_fh
*resfhp
)
1530 struct dentry
*dentry
, *dnew
;
1535 if (!flen
|| path
[0] == '\0')
1538 if (isdotent(fname
, flen
))
1541 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1545 host_err
= fh_want_write(fhp
);
1550 dentry
= fhp
->fh_dentry
;
1551 dnew
= lookup_one_len(fname
, dentry
, flen
);
1552 host_err
= PTR_ERR(dnew
);
1556 host_err
= vfs_symlink(dentry
->d_inode
, dnew
, path
);
1557 err
= nfserrno(host_err
);
1559 err
= nfserrno(commit_metadata(fhp
));
1564 cerr
= fh_compose(resfhp
, fhp
->fh_export
, dnew
, fhp
);
1566 if (err
==0) err
= cerr
;
1571 err
= nfserrno(host_err
);
1577 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1580 nfsd_link(struct svc_rqst
*rqstp
, struct svc_fh
*ffhp
,
1581 char *name
, int len
, struct svc_fh
*tfhp
)
1583 struct dentry
*ddir
, *dnew
, *dold
;
1588 err
= fh_verify(rqstp
, ffhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1591 err
= fh_verify(rqstp
, tfhp
, 0, NFSD_MAY_NOP
);
1595 if (S_ISDIR(tfhp
->fh_dentry
->d_inode
->i_mode
))
1601 if (isdotent(name
, len
))
1604 host_err
= fh_want_write(tfhp
);
1606 err
= nfserrno(host_err
);
1610 fh_lock_nested(ffhp
, I_MUTEX_PARENT
);
1611 ddir
= ffhp
->fh_dentry
;
1612 dirp
= ddir
->d_inode
;
1614 dnew
= lookup_one_len(name
, ddir
, len
);
1615 host_err
= PTR_ERR(dnew
);
1619 dold
= tfhp
->fh_dentry
;
1624 host_err
= vfs_link(dold
, dirp
, dnew
, NULL
);
1626 err
= nfserrno(commit_metadata(ffhp
));
1628 err
= nfserrno(commit_metadata(tfhp
));
1630 if (host_err
== -EXDEV
&& rqstp
->rq_vers
== 2)
1633 err
= nfserrno(host_err
);
1639 fh_drop_write(tfhp
);
1644 err
= nfserrno(host_err
);
1650 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1653 nfsd_rename(struct svc_rqst
*rqstp
, struct svc_fh
*ffhp
, char *fname
, int flen
,
1654 struct svc_fh
*tfhp
, char *tname
, int tlen
)
1656 struct dentry
*fdentry
, *tdentry
, *odentry
, *ndentry
, *trap
;
1657 struct inode
*fdir
, *tdir
;
1661 err
= fh_verify(rqstp
, ffhp
, S_IFDIR
, NFSD_MAY_REMOVE
);
1664 err
= fh_verify(rqstp
, tfhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1668 fdentry
= ffhp
->fh_dentry
;
1669 fdir
= fdentry
->d_inode
;
1671 tdentry
= tfhp
->fh_dentry
;
1672 tdir
= tdentry
->d_inode
;
1675 if (!flen
|| isdotent(fname
, flen
) || !tlen
|| isdotent(tname
, tlen
))
1678 host_err
= fh_want_write(ffhp
);
1680 err
= nfserrno(host_err
);
1684 /* cannot use fh_lock as we need deadlock protective ordering
1685 * so do it by hand */
1686 trap
= lock_rename(tdentry
, fdentry
);
1687 ffhp
->fh_locked
= tfhp
->fh_locked
= 1;
1691 odentry
= lookup_one_len(fname
, fdentry
, flen
);
1692 host_err
= PTR_ERR(odentry
);
1693 if (IS_ERR(odentry
))
1697 if (!odentry
->d_inode
)
1700 if (odentry
== trap
)
1703 ndentry
= lookup_one_len(tname
, tdentry
, tlen
);
1704 host_err
= PTR_ERR(ndentry
);
1705 if (IS_ERR(ndentry
))
1707 host_err
= -ENOTEMPTY
;
1708 if (ndentry
== trap
)
1712 if (ffhp
->fh_export
->ex_path
.mnt
!= tfhp
->fh_export
->ex_path
.mnt
)
1714 if (ffhp
->fh_export
->ex_path
.dentry
!= tfhp
->fh_export
->ex_path
.dentry
)
1717 host_err
= vfs_rename(fdir
, odentry
, tdir
, ndentry
, NULL
, 0);
1719 host_err
= commit_metadata(tfhp
);
1721 host_err
= commit_metadata(ffhp
);
1728 err
= nfserrno(host_err
);
1730 * We cannot rely on fh_unlock on the two filehandles,
1731 * as that would do the wrong thing if the two directories
1732 * were the same, so again we do it by hand.
1734 fill_post_wcc(ffhp
);
1735 fill_post_wcc(tfhp
);
1736 unlock_rename(tdentry
, fdentry
);
1737 ffhp
->fh_locked
= tfhp
->fh_locked
= 0;
1738 fh_drop_write(ffhp
);
1745 * Unlink a file or directory
1746 * N.B. After this call fhp needs an fh_put
1749 nfsd_unlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, int type
,
1750 char *fname
, int flen
)
1752 struct dentry
*dentry
, *rdentry
;
1758 if (!flen
|| isdotent(fname
, flen
))
1760 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_REMOVE
);
1764 host_err
= fh_want_write(fhp
);
1768 fh_lock_nested(fhp
, I_MUTEX_PARENT
);
1769 dentry
= fhp
->fh_dentry
;
1770 dirp
= dentry
->d_inode
;
1772 rdentry
= lookup_one_len(fname
, dentry
, flen
);
1773 host_err
= PTR_ERR(rdentry
);
1774 if (IS_ERR(rdentry
))
1777 if (!rdentry
->d_inode
) {
1784 type
= rdentry
->d_inode
->i_mode
& S_IFMT
;
1786 if (type
!= S_IFDIR
)
1787 host_err
= vfs_unlink(dirp
, rdentry
, NULL
);
1789 host_err
= vfs_rmdir(dirp
, rdentry
);
1791 host_err
= commit_metadata(fhp
);
1795 err
= nfserrno(host_err
);
1801 * We do this buffering because we must not call back into the file
1802 * system's ->lookup() method from the filldir callback. That may well
1803 * deadlock a number of file systems.
1805 * This is based heavily on the implementation of same in XFS.
1807 struct buffered_dirent
{
1811 unsigned int d_type
;
1815 struct readdir_data
{
1816 struct dir_context ctx
;
1822 static int nfsd_buffered_filldir(void *__buf
, const char *name
, int namlen
,
1823 loff_t offset
, u64 ino
, unsigned int d_type
)
1825 struct readdir_data
*buf
= __buf
;
1826 struct buffered_dirent
*de
= (void *)(buf
->dirent
+ buf
->used
);
1827 unsigned int reclen
;
1829 reclen
= ALIGN(sizeof(struct buffered_dirent
) + namlen
, sizeof(u64
));
1830 if (buf
->used
+ reclen
> PAGE_SIZE
) {
1835 de
->namlen
= namlen
;
1836 de
->offset
= offset
;
1838 de
->d_type
= d_type
;
1839 memcpy(de
->name
, name
, namlen
);
1840 buf
->used
+= reclen
;
1845 static __be32
nfsd_buffered_readdir(struct file
*file
, filldir_t func
,
1846 struct readdir_cd
*cdp
, loff_t
*offsetp
)
1848 struct buffered_dirent
*de
;
1852 struct readdir_data buf
= {
1853 .ctx
.actor
= nfsd_buffered_filldir
,
1854 .dirent
= (void *)__get_free_page(GFP_KERNEL
)
1858 return nfserrno(-ENOMEM
);
1863 struct inode
*dir_inode
= file_inode(file
);
1864 unsigned int reclen
;
1866 cdp
->err
= nfserr_eof
; /* will be cleared on successful read */
1870 host_err
= iterate_dir(file
, &buf
.ctx
);
1883 * Various filldir functions may end up calling back into
1884 * lookup_one_len() and the file system's ->lookup() method.
1885 * These expect i_mutex to be held, as it would within readdir.
1887 host_err
= mutex_lock_killable(&dir_inode
->i_mutex
);
1891 de
= (struct buffered_dirent
*)buf
.dirent
;
1893 offset
= de
->offset
;
1895 if (func(cdp
, de
->name
, de
->namlen
, de
->offset
,
1896 de
->ino
, de
->d_type
))
1899 if (cdp
->err
!= nfs_ok
)
1902 reclen
= ALIGN(sizeof(*de
) + de
->namlen
,
1905 de
= (struct buffered_dirent
*)((char *)de
+ reclen
);
1907 mutex_unlock(&dir_inode
->i_mutex
);
1908 if (size
> 0) /* We bailed out early */
1911 offset
= vfs_llseek(file
, 0, SEEK_CUR
);
1914 free_page((unsigned long)(buf
.dirent
));
1917 return nfserrno(host_err
);
1924 * Read entries from a directory.
1925 * The NFSv3/4 verifier we ignore for now.
1928 nfsd_readdir(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, loff_t
*offsetp
,
1929 struct readdir_cd
*cdp
, filldir_t func
)
1933 loff_t offset
= *offsetp
;
1934 int may_flags
= NFSD_MAY_READ
;
1936 /* NFSv2 only supports 32 bit cookies */
1937 if (rqstp
->rq_vers
> 2)
1938 may_flags
|= NFSD_MAY_64BIT_COOKIE
;
1940 err
= nfsd_open(rqstp
, fhp
, S_IFDIR
, may_flags
, &file
);
1944 offset
= vfs_llseek(file
, offset
, SEEK_SET
);
1946 err
= nfserrno((int)offset
);
1950 err
= nfsd_buffered_readdir(file
, func
, cdp
, offsetp
);
1952 if (err
== nfserr_eof
|| err
== nfserr_toosmall
)
1953 err
= nfs_ok
; /* can still be found in ->err */
1961 * Get file system stats
1962 * N.B. After this call fhp needs an fh_put
1965 nfsd_statfs(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct kstatfs
*stat
, int access
)
1969 err
= fh_verify(rqstp
, fhp
, 0, NFSD_MAY_NOP
| access
);
1971 struct path path
= {
1972 .mnt
= fhp
->fh_export
->ex_path
.mnt
,
1973 .dentry
= fhp
->fh_dentry
,
1975 if (vfs_statfs(&path
, stat
))
1981 static int exp_rdonly(struct svc_rqst
*rqstp
, struct svc_export
*exp
)
1983 return nfsexp_flags(rqstp
, exp
) & NFSEXP_READONLY
;
1987 * Check for a user's access permissions to this inode.
1990 nfsd_permission(struct svc_rqst
*rqstp
, struct svc_export
*exp
,
1991 struct dentry
*dentry
, int acc
)
1993 struct inode
*inode
= dentry
->d_inode
;
1996 if ((acc
& NFSD_MAY_MASK
) == NFSD_MAY_NOP
)
1999 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2001 (acc
& NFSD_MAY_READ
)? " read" : "",
2002 (acc
& NFSD_MAY_WRITE
)? " write" : "",
2003 (acc
& NFSD_MAY_EXEC
)? " exec" : "",
2004 (acc
& NFSD_MAY_SATTR
)? " sattr" : "",
2005 (acc
& NFSD_MAY_TRUNC
)? " trunc" : "",
2006 (acc
& NFSD_MAY_LOCK
)? " lock" : "",
2007 (acc
& NFSD_MAY_OWNER_OVERRIDE
)? " owneroverride" : "",
2009 IS_IMMUTABLE(inode
)? " immut" : "",
2010 IS_APPEND(inode
)? " append" : "",
2011 __mnt_is_readonly(exp
->ex_path
.mnt
)? " ro" : "");
2012 dprintk(" owner %d/%d user %d/%d\n",
2013 inode
->i_uid
, inode
->i_gid
, current_fsuid(), current_fsgid());
2016 /* Normally we reject any write/sattr etc access on a read-only file
2017 * system. But if it is IRIX doing check on write-access for a
2018 * device special file, we ignore rofs.
2020 if (!(acc
& NFSD_MAY_LOCAL_ACCESS
))
2021 if (acc
& (NFSD_MAY_WRITE
| NFSD_MAY_SATTR
| NFSD_MAY_TRUNC
)) {
2022 if (exp_rdonly(rqstp
, exp
) ||
2023 __mnt_is_readonly(exp
->ex_path
.mnt
))
2025 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode
))
2028 if ((acc
& NFSD_MAY_TRUNC
) && IS_APPEND(inode
))
2031 if (acc
& NFSD_MAY_LOCK
) {
2032 /* If we cannot rely on authentication in NLM requests,
2033 * just allow locks, otherwise require read permission, or
2036 if (exp
->ex_flags
& NFSEXP_NOAUTHNLM
)
2039 acc
= NFSD_MAY_READ
| NFSD_MAY_OWNER_OVERRIDE
;
2042 * The file owner always gets access permission for accesses that
2043 * would normally be checked at open time. This is to make
2044 * file access work even when the client has done a fchmod(fd, 0).
2046 * However, `cp foo bar' should fail nevertheless when bar is
2047 * readonly. A sensible way to do this might be to reject all
2048 * attempts to truncate a read-only file, because a creat() call
2049 * always implies file truncation.
2050 * ... but this isn't really fair. A process may reasonably call
2051 * ftruncate on an open file descriptor on a file with perm 000.
2052 * We must trust the client to do permission checking - using "ACCESS"
2055 if ((acc
& NFSD_MAY_OWNER_OVERRIDE
) &&
2056 uid_eq(inode
->i_uid
, current_fsuid()))
2059 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2060 err
= inode_permission(inode
, acc
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
));
2062 /* Allow read access to binaries even when mode 111 */
2063 if (err
== -EACCES
&& S_ISREG(inode
->i_mode
) &&
2064 (acc
== (NFSD_MAY_READ
| NFSD_MAY_OWNER_OVERRIDE
) ||
2065 acc
== (NFSD_MAY_READ
| NFSD_MAY_READ_IF_EXEC
)))
2066 err
= inode_permission(inode
, MAY_EXEC
);
2068 return err
? nfserrno(err
) : 0;
2072 nfsd_racache_shutdown(void)
2074 struct raparms
*raparm
, *last_raparm
;
2077 dprintk("nfsd: freeing readahead buffers.\n");
2079 for (i
= 0; i
< RAPARM_HASH_SIZE
; i
++) {
2080 raparm
= raparm_hash
[i
].pb_head
;
2082 last_raparm
= raparm
;
2083 raparm
= raparm
->p_next
;
2086 raparm_hash
[i
].pb_head
= NULL
;
2090 * Initialize readahead param cache
2093 nfsd_racache_init(int cache_size
)
2098 struct raparms
**raparm
= NULL
;
2101 if (raparm_hash
[0].pb_head
)
2103 nperbucket
= DIV_ROUND_UP(cache_size
, RAPARM_HASH_SIZE
);
2104 nperbucket
= max(2, nperbucket
);
2105 cache_size
= nperbucket
* RAPARM_HASH_SIZE
;
2107 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size
);
2109 for (i
= 0; i
< RAPARM_HASH_SIZE
; i
++) {
2110 spin_lock_init(&raparm_hash
[i
].pb_lock
);
2112 raparm
= &raparm_hash
[i
].pb_head
;
2113 for (j
= 0; j
< nperbucket
; j
++) {
2114 *raparm
= kzalloc(sizeof(struct raparms
), GFP_KERNEL
);
2117 raparm
= &(*raparm
)->p_next
;
2122 nfsdstats
.ra_size
= cache_size
;
2126 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2127 nfsd_racache_shutdown();