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/falloc.h>
20 #include <linux/fcntl.h>
21 #include <linux/namei.h>
22 #include <linux/delay.h>
23 #include <linux/fsnotify.h>
24 #include <linux/posix_acl_xattr.h>
25 #include <linux/xattr.h>
26 #include <linux/jhash.h>
27 #include <linux/ima.h>
28 #include <linux/slab.h>
29 #include <asm/uaccess.h>
30 #include <linux/exportfs.h>
31 #include <linux/writeback.h>
32 #include <linux/security.h>
36 #endif /* CONFIG_NFSD_V3 */
39 #include "../internal.h"
42 #endif /* CONFIG_NFSD_V4 */
47 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
51 * This is a cache of readahead params that help us choose the proper
52 * readahead strategy. Initially, we set all readahead parameters to 0
53 * and let the VFS handle things.
54 * If you increase the number of cached files very much, you'll need to
55 * add a hash table here.
58 struct raparms
*p_next
;
63 struct file_ra_state p_ra
;
64 unsigned int p_hindex
;
67 struct raparm_hbucket
{
68 struct raparms
*pb_head
;
70 } ____cacheline_aligned_in_smp
;
72 #define RAPARM_HASH_BITS 4
73 #define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
74 #define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
75 static struct raparm_hbucket raparm_hash
[RAPARM_HASH_SIZE
];
78 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
80 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
81 * or nfs_ok having possibly changed *dpp and *expp
84 nfsd_cross_mnt(struct svc_rqst
*rqstp
, struct dentry
**dpp
,
85 struct svc_export
**expp
)
87 struct svc_export
*exp
= *expp
, *exp2
= NULL
;
88 struct dentry
*dentry
= *dpp
;
89 struct path path
= {.mnt
= mntget(exp
->ex_path
.mnt
),
90 .dentry
= dget(dentry
)};
93 err
= follow_down(&path
);
97 exp2
= rqst_exp_get_by_name(rqstp
, &path
);
101 * We normally allow NFS clients to continue
102 * "underneath" a mountpoint that is not exported.
103 * The exception is V4ROOT, where no traversal is ever
104 * allowed without an explicit export of the new
107 if (err
== -ENOENT
&& !(exp
->ex_flags
& NFSEXP_V4ROOT
))
112 if (nfsd_v4client(rqstp
) ||
113 (exp
->ex_flags
& NFSEXP_CROSSMOUNT
) || EX_NOHIDE(exp2
)) {
114 /* successfully crossed mount point */
116 * This is subtle: path.dentry is *not* on path.mnt
117 * at this point. The only reason we are safe is that
118 * original mnt is pinned down by exp, so we should
119 * put path *before* putting exp
122 path
.dentry
= dentry
;
132 static void follow_to_parent(struct path
*path
)
136 while (path
->dentry
== path
->mnt
->mnt_root
&& follow_up(path
))
138 dp
= dget_parent(path
->dentry
);
143 static int nfsd_lookup_parent(struct svc_rqst
*rqstp
, struct dentry
*dparent
, struct svc_export
**exp
, struct dentry
**dentryp
)
145 struct svc_export
*exp2
;
146 struct path path
= {.mnt
= mntget((*exp
)->ex_path
.mnt
),
147 .dentry
= dget(dparent
)};
149 follow_to_parent(&path
);
151 exp2
= rqst_exp_parent(rqstp
, &path
);
152 if (PTR_ERR(exp2
) == -ENOENT
) {
153 *dentryp
= dget(dparent
);
154 } else if (IS_ERR(exp2
)) {
156 return PTR_ERR(exp2
);
158 *dentryp
= dget(path
.dentry
);
167 * For nfsd purposes, we treat V4ROOT exports as though there was an
168 * export at *every* directory.
170 int nfsd_mountpoint(struct dentry
*dentry
, struct svc_export
*exp
)
172 if (d_mountpoint(dentry
))
174 if (nfsd4_is_junction(dentry
))
176 if (!(exp
->ex_flags
& NFSEXP_V4ROOT
))
178 return d_inode(dentry
) != NULL
;
182 nfsd_lookup_dentry(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
183 const char *name
, unsigned int len
,
184 struct svc_export
**exp_ret
, struct dentry
**dentry_ret
)
186 struct svc_export
*exp
;
187 struct dentry
*dparent
;
188 struct dentry
*dentry
;
191 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp
), len
,name
);
193 dparent
= fhp
->fh_dentry
;
194 exp
= exp_get(fhp
->fh_export
);
196 /* Lookup the name, but don't follow links */
197 if (isdotent(name
, len
)) {
199 dentry
= dget(dparent
);
200 else if (dparent
!= exp
->ex_path
.dentry
)
201 dentry
= dget_parent(dparent
);
202 else if (!EX_NOHIDE(exp
) && !nfsd_v4client(rqstp
))
203 dentry
= dget(dparent
); /* .. == . just like at / */
205 /* checking mountpoint crossing is very different when stepping up */
206 host_err
= nfsd_lookup_parent(rqstp
, dparent
, &exp
, &dentry
);
212 * In the nfsd4_open() case, this may be held across
213 * subsequent open and delegation acquisition which may
214 * need to take the child's i_mutex:
216 fh_lock_nested(fhp
, I_MUTEX_PARENT
);
217 dentry
= lookup_one_len(name
, dparent
, len
);
218 host_err
= PTR_ERR(dentry
);
221 if (nfsd_mountpoint(dentry
, exp
)) {
223 * We don't need the i_mutex after all. It's
224 * still possible we could open this (regular
225 * files can be mountpoints too), but the
226 * i_mutex is just there to prevent renames of
227 * something that we might be about to delegate,
228 * and a mountpoint won't be renamed:
231 if ((host_err
= nfsd_cross_mnt(rqstp
, &dentry
, &exp
))) {
237 *dentry_ret
= dentry
;
243 return nfserrno(host_err
);
247 * Look up one component of a pathname.
248 * N.B. After this call _both_ fhp and resfh need an fh_put
250 * If the lookup would cross a mountpoint, and the mounted filesystem
251 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
252 * accepted as it stands and the mounted directory is
253 * returned. Otherwise the covered directory is returned.
254 * NOTE: this mountpoint crossing is not supported properly by all
255 * clients and is explicitly disallowed for NFSv3
256 * NeilBrown <neilb@cse.unsw.edu.au>
259 nfsd_lookup(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, const char *name
,
260 unsigned int len
, struct svc_fh
*resfh
)
262 struct svc_export
*exp
;
263 struct dentry
*dentry
;
266 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_EXEC
);
269 err
= nfsd_lookup_dentry(rqstp
, fhp
, name
, len
, &exp
, &dentry
);
272 err
= check_nfsd_access(exp
, rqstp
);
276 * Note: we compose the file handle now, but as the
277 * dentry may be negative, it may need to be updated.
279 err
= fh_compose(resfh
, exp
, dentry
, fhp
);
280 if (!err
&& d_really_is_negative(dentry
))
289 * Commit metadata changes to stable storage.
292 commit_metadata(struct svc_fh
*fhp
)
294 struct inode
*inode
= d_inode(fhp
->fh_dentry
);
295 const struct export_operations
*export_ops
= inode
->i_sb
->s_export_op
;
297 if (!EX_ISSYNC(fhp
->fh_export
))
300 if (export_ops
->commit_metadata
)
301 return export_ops
->commit_metadata(inode
);
302 return sync_inode_metadata(inode
, 1);
306 * Go over the attributes and take care of the small differences between
307 * NFS semantics and what Linux expects.
310 nfsd_sanitize_attrs(struct inode
*inode
, struct iattr
*iap
)
312 /* sanitize the mode change */
313 if (iap
->ia_valid
& ATTR_MODE
) {
314 iap
->ia_mode
&= S_IALLUGO
;
315 iap
->ia_mode
|= (inode
->i_mode
& ~S_IALLUGO
);
318 /* Revoke setuid/setgid on chown */
319 if (!S_ISDIR(inode
->i_mode
) &&
320 ((iap
->ia_valid
& ATTR_UID
) || (iap
->ia_valid
& ATTR_GID
))) {
321 iap
->ia_valid
|= ATTR_KILL_PRIV
;
322 if (iap
->ia_valid
& ATTR_MODE
) {
323 /* we're setting mode too, just clear the s*id bits */
324 iap
->ia_mode
&= ~S_ISUID
;
325 if (iap
->ia_mode
& S_IXGRP
)
326 iap
->ia_mode
&= ~S_ISGID
;
328 /* set ATTR_KILL_* bits and let VFS handle it */
329 iap
->ia_valid
|= (ATTR_KILL_SUID
| ATTR_KILL_SGID
);
335 nfsd_get_write_access(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
338 struct inode
*inode
= d_inode(fhp
->fh_dentry
);
341 if (iap
->ia_size
< inode
->i_size
) {
344 err
= nfsd_permission(rqstp
, fhp
->fh_export
, fhp
->fh_dentry
,
345 NFSD_MAY_TRUNC
| NFSD_MAY_OWNER_OVERRIDE
);
350 host_err
= get_write_access(inode
);
354 host_err
= locks_verify_truncate(inode
, NULL
, iap
->ia_size
);
356 goto out_put_write_access
;
359 out_put_write_access
:
360 put_write_access(inode
);
362 return nfserrno(host_err
);
366 * Set various file attributes. After this call fhp needs an fh_put.
369 nfsd_setattr(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct iattr
*iap
,
370 int check_guard
, time_t guardtime
)
372 struct dentry
*dentry
;
374 int accmode
= NFSD_MAY_SATTR
;
378 bool get_write_count
;
381 if (iap
->ia_valid
& (ATTR_ATIME
| ATTR_MTIME
| ATTR_SIZE
))
382 accmode
|= NFSD_MAY_WRITE
|NFSD_MAY_OWNER_OVERRIDE
;
383 if (iap
->ia_valid
& ATTR_SIZE
)
386 /* Callers that do fh_verify should do the fh_want_write: */
387 get_write_count
= !fhp
->fh_dentry
;
390 err
= fh_verify(rqstp
, fhp
, ftype
, accmode
);
393 if (get_write_count
) {
394 host_err
= fh_want_write(fhp
);
396 return nfserrno(host_err
);
399 dentry
= fhp
->fh_dentry
;
400 inode
= d_inode(dentry
);
402 /* Ignore any mode updates on symlinks */
403 if (S_ISLNK(inode
->i_mode
))
404 iap
->ia_valid
&= ~ATTR_MODE
;
409 nfsd_sanitize_attrs(inode
, iap
);
412 * The size case is special, it changes the file in addition to the
415 if (iap
->ia_valid
& ATTR_SIZE
) {
416 err
= nfsd_get_write_access(rqstp
, fhp
, iap
);
422 * RFC5661, Section 18.30.4:
423 * Changing the size of a file with SETATTR indirectly
424 * changes the time_modify and change attributes.
426 * (and similar for the older RFCs)
428 if (iap
->ia_size
!= i_size_read(inode
))
429 iap
->ia_valid
|= ATTR_MTIME
;
432 iap
->ia_valid
|= ATTR_CTIME
;
434 if (check_guard
&& guardtime
!= inode
->i_ctime
.tv_sec
) {
435 err
= nfserr_notsync
;
436 goto out_put_write_access
;
440 host_err
= notify_change(dentry
, iap
, NULL
);
442 err
= nfserrno(host_err
);
444 out_put_write_access
:
446 put_write_access(inode
);
448 err
= nfserrno(commit_metadata(fhp
));
453 #if defined(CONFIG_NFSD_V4)
455 * NFS junction information is stored in an extended attribute.
457 #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
460 * nfsd4_is_junction - Test if an object could be an NFS junction
462 * @dentry: object to test
464 * Returns 1 if "dentry" appears to contain NFS junction information.
465 * Otherwise 0 is returned.
467 int nfsd4_is_junction(struct dentry
*dentry
)
469 struct inode
*inode
= d_inode(dentry
);
473 if (inode
->i_mode
& S_IXUGO
)
475 if (!(inode
->i_mode
& S_ISVTX
))
477 if (vfs_getxattr(dentry
, NFSD_JUNCTION_XATTR_NAME
, NULL
, 0) <= 0)
481 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
482 __be32
nfsd4_set_nfs4_label(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
483 struct xdr_netobj
*label
)
487 struct dentry
*dentry
;
489 error
= fh_verify(rqstp
, fhp
, 0 /* S_IFREG */, NFSD_MAY_SATTR
);
493 dentry
= fhp
->fh_dentry
;
495 mutex_lock(&d_inode(dentry
)->i_mutex
);
496 host_error
= security_inode_setsecctx(dentry
, label
->data
, label
->len
);
497 mutex_unlock(&d_inode(dentry
)->i_mutex
);
498 return nfserrno(host_error
);
501 __be32
nfsd4_set_nfs4_label(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
502 struct xdr_netobj
*label
)
504 return nfserr_notsupp
;
508 __be32
nfsd4_clone_file_range(struct file
*src
, u64 src_pos
, struct file
*dst
,
509 u64 dst_pos
, u64 count
)
511 return nfserrno(vfs_clone_file_range(src
, src_pos
, dst
, dst_pos
,
515 __be32
nfsd4_vfs_fallocate(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
516 struct file
*file
, loff_t offset
, loff_t len
,
521 if (!S_ISREG(file_inode(file
)->i_mode
))
524 error
= vfs_fallocate(file
, flags
, offset
, len
);
526 error
= commit_metadata(fhp
);
528 return nfserrno(error
);
530 #endif /* defined(CONFIG_NFSD_V4) */
532 #ifdef CONFIG_NFSD_V3
534 * Check server access rights to a file system object
540 static struct accessmap nfs3_regaccess
[] = {
541 { NFS3_ACCESS_READ
, NFSD_MAY_READ
},
542 { NFS3_ACCESS_EXECUTE
, NFSD_MAY_EXEC
},
543 { NFS3_ACCESS_MODIFY
, NFSD_MAY_WRITE
|NFSD_MAY_TRUNC
},
544 { NFS3_ACCESS_EXTEND
, NFSD_MAY_WRITE
},
549 static struct accessmap nfs3_diraccess
[] = {
550 { NFS3_ACCESS_READ
, NFSD_MAY_READ
},
551 { NFS3_ACCESS_LOOKUP
, NFSD_MAY_EXEC
},
552 { NFS3_ACCESS_MODIFY
, NFSD_MAY_EXEC
|NFSD_MAY_WRITE
|NFSD_MAY_TRUNC
},
553 { NFS3_ACCESS_EXTEND
, NFSD_MAY_EXEC
|NFSD_MAY_WRITE
},
554 { NFS3_ACCESS_DELETE
, NFSD_MAY_REMOVE
},
559 static struct accessmap nfs3_anyaccess
[] = {
560 /* Some clients - Solaris 2.6 at least, make an access call
561 * to the server to check for access for things like /dev/null
562 * (which really, the server doesn't care about). So
563 * We provide simple access checking for them, looking
564 * mainly at mode bits, and we make sure to ignore read-only
567 { NFS3_ACCESS_READ
, NFSD_MAY_READ
},
568 { NFS3_ACCESS_EXECUTE
, NFSD_MAY_EXEC
},
569 { NFS3_ACCESS_MODIFY
, NFSD_MAY_WRITE
|NFSD_MAY_LOCAL_ACCESS
},
570 { NFS3_ACCESS_EXTEND
, NFSD_MAY_WRITE
|NFSD_MAY_LOCAL_ACCESS
},
576 nfsd_access(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, u32
*access
, u32
*supported
)
578 struct accessmap
*map
;
579 struct svc_export
*export
;
580 struct dentry
*dentry
;
581 u32 query
, result
= 0, sresult
= 0;
584 error
= fh_verify(rqstp
, fhp
, 0, NFSD_MAY_NOP
);
588 export
= fhp
->fh_export
;
589 dentry
= fhp
->fh_dentry
;
591 if (d_is_reg(dentry
))
592 map
= nfs3_regaccess
;
593 else if (d_is_dir(dentry
))
594 map
= nfs3_diraccess
;
596 map
= nfs3_anyaccess
;
600 for (; map
->access
; map
++) {
601 if (map
->access
& query
) {
604 sresult
|= map
->access
;
606 err2
= nfsd_permission(rqstp
, export
, dentry
, map
->how
);
609 result
|= map
->access
;
612 /* the following error codes just mean the access was not allowed,
613 * rather than an error occurred */
617 /* simply don't "or" in the access bit. */
627 *supported
= sresult
;
632 #endif /* CONFIG_NFSD_V3 */
634 static int nfsd_open_break_lease(struct inode
*inode
, int access
)
638 if (access
& NFSD_MAY_NOT_BREAK_LEASE
)
640 mode
= (access
& NFSD_MAY_WRITE
) ? O_WRONLY
: O_RDONLY
;
641 return break_lease(inode
, mode
| O_NONBLOCK
);
645 * Open an existing file or directory.
646 * The may_flags argument indicates the type of open (read/write/lock)
647 * and additional flags.
648 * N.B. After this call fhp needs an fh_put
651 nfsd_open(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, umode_t type
,
652 int may_flags
, struct file
**filp
)
657 int flags
= O_RDONLY
|O_LARGEFILE
;
661 validate_process_creds();
664 * If we get here, then the client has already done an "open",
665 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
666 * in case a chmod has now revoked permission.
668 * Arguably we should also allow the owner override for
669 * directories, but we never have and it doesn't seem to have
670 * caused anyone a problem. If we were to change this, note
671 * also that our filldir callbacks would need a variant of
672 * lookup_one_len that doesn't check permissions.
675 may_flags
|= NFSD_MAY_OWNER_OVERRIDE
;
676 err
= fh_verify(rqstp
, fhp
, type
, may_flags
);
680 path
.mnt
= fhp
->fh_export
->ex_path
.mnt
;
681 path
.dentry
= fhp
->fh_dentry
;
682 inode
= d_inode(path
.dentry
);
684 /* Disallow write access to files with the append-only bit set
685 * or any access when mandatory locking enabled
688 if (IS_APPEND(inode
) && (may_flags
& NFSD_MAY_WRITE
))
691 * We must ignore files (but only files) which might have mandatory
692 * locks on them because there is no way to know if the accesser has
695 if (S_ISREG((inode
)->i_mode
) && mandatory_lock(inode
))
701 host_err
= nfsd_open_break_lease(inode
, may_flags
);
702 if (host_err
) /* NOMEM or WOULDBLOCK */
705 if (may_flags
& NFSD_MAY_WRITE
) {
706 if (may_flags
& NFSD_MAY_READ
)
707 flags
= O_RDWR
|O_LARGEFILE
;
709 flags
= O_WRONLY
|O_LARGEFILE
;
712 file
= dentry_open(&path
, flags
, current_cred());
714 host_err
= PTR_ERR(file
);
718 host_err
= ima_file_check(file
, may_flags
, 0);
724 if (may_flags
& NFSD_MAY_64BIT_COOKIE
)
725 file
->f_mode
|= FMODE_64BITHASH
;
727 file
->f_mode
|= FMODE_32BITHASH
;
731 err
= nfserrno(host_err
);
733 validate_process_creds();
738 nfsd_init_raparms(struct file
*file
)
740 struct inode
*inode
= file_inode(file
);
741 dev_t dev
= inode
->i_sb
->s_dev
;
742 ino_t ino
= inode
->i_ino
;
743 struct raparms
*ra
, **rap
, **frap
= NULL
;
746 struct raparm_hbucket
*rab
;
748 hash
= jhash_2words(dev
, ino
, 0xfeedbeef) & RAPARM_HASH_MASK
;
749 rab
= &raparm_hash
[hash
];
751 spin_lock(&rab
->pb_lock
);
752 for (rap
= &rab
->pb_head
; (ra
= *rap
); rap
= &ra
->p_next
) {
753 if (ra
->p_ino
== ino
&& ra
->p_dev
== dev
)
756 if (ra
->p_count
== 0)
759 depth
= nfsdstats
.ra_size
;
761 spin_unlock(&rab
->pb_lock
);
771 if (rap
!= &rab
->pb_head
) {
773 ra
->p_next
= rab
->pb_head
;
777 nfsdstats
.ra_depth
[depth
*10/nfsdstats
.ra_size
]++;
778 spin_unlock(&rab
->pb_lock
);
781 file
->f_ra
= ra
->p_ra
;
785 void nfsd_put_raparams(struct file
*file
, struct raparms
*ra
)
787 struct raparm_hbucket
*rab
= &raparm_hash
[ra
->p_hindex
];
789 spin_lock(&rab
->pb_lock
);
790 ra
->p_ra
= file
->f_ra
;
793 spin_unlock(&rab
->pb_lock
);
797 * Grab and keep cached pages associated with a file in the svc_rqst
798 * so that they can be passed to the network sendmsg/sendpage routines
799 * directly. They will be released after the sending has completed.
802 nfsd_splice_actor(struct pipe_inode_info
*pipe
, struct pipe_buffer
*buf
,
803 struct splice_desc
*sd
)
805 struct svc_rqst
*rqstp
= sd
->u
.data
;
806 struct page
**pp
= rqstp
->rq_next_page
;
807 struct page
*page
= buf
->page
;
812 if (rqstp
->rq_res
.page_len
== 0) {
814 put_page(*rqstp
->rq_next_page
);
815 *(rqstp
->rq_next_page
++) = page
;
816 rqstp
->rq_res
.page_base
= buf
->offset
;
817 rqstp
->rq_res
.page_len
= size
;
818 } else if (page
!= pp
[-1]) {
820 if (*rqstp
->rq_next_page
)
821 put_page(*rqstp
->rq_next_page
);
822 *(rqstp
->rq_next_page
++) = page
;
823 rqstp
->rq_res
.page_len
+= size
;
825 rqstp
->rq_res
.page_len
+= size
;
830 static int nfsd_direct_splice_actor(struct pipe_inode_info
*pipe
,
831 struct splice_desc
*sd
)
833 return __splice_from_pipe(pipe
, sd
, nfsd_splice_actor
);
837 nfsd_finish_read(struct file
*file
, unsigned long *count
, int host_err
)
840 nfsdstats
.io_read
+= host_err
;
842 fsnotify_access(file
);
845 return nfserrno(host_err
);
848 __be32
nfsd_splice_read(struct svc_rqst
*rqstp
,
849 struct file
*file
, loff_t offset
, unsigned long *count
)
851 struct splice_desc sd
= {
859 rqstp
->rq_next_page
= rqstp
->rq_respages
+ 1;
860 host_err
= splice_direct_to_actor(file
, &sd
, nfsd_direct_splice_actor
);
861 return nfsd_finish_read(file
, count
, host_err
);
864 __be32
nfsd_readv(struct file
*file
, loff_t offset
, struct kvec
*vec
, int vlen
,
865 unsigned long *count
)
872 host_err
= vfs_readv(file
, (struct iovec __user
*)vec
, vlen
, &offset
);
874 return nfsd_finish_read(file
, count
, host_err
);
878 nfsd_vfs_read(struct svc_rqst
*rqstp
, struct file
*file
,
879 loff_t offset
, struct kvec
*vec
, int vlen
, unsigned long *count
)
881 if (file
->f_op
->splice_read
&& test_bit(RQ_SPLICE_OK
, &rqstp
->rq_flags
))
882 return nfsd_splice_read(rqstp
, file
, offset
, count
);
884 return nfsd_readv(file
, offset
, vec
, vlen
, count
);
888 * Gathered writes: If another process is currently writing to the file,
889 * there's a high chance this is another nfsd (triggered by a bulk write
890 * from a client's biod). Rather than syncing the file with each write
891 * request, we sleep for 10 msec.
893 * I don't know if this roughly approximates C. Juszak's idea of
894 * gathered writes, but it's a nice and simple solution (IMHO), and it
897 * Note: we do this only in the NFSv2 case, since v3 and higher have a
898 * better tool (separate unstable writes and commits) for solving this
901 static int wait_for_concurrent_writes(struct file
*file
)
903 struct inode
*inode
= file_inode(file
);
904 static ino_t last_ino
;
905 static dev_t last_dev
;
908 if (atomic_read(&inode
->i_writecount
) > 1
909 || (last_ino
== inode
->i_ino
&& last_dev
== inode
->i_sb
->s_dev
)) {
910 dprintk("nfsd: write defer %d\n", task_pid_nr(current
));
912 dprintk("nfsd: write resume %d\n", task_pid_nr(current
));
915 if (inode
->i_state
& I_DIRTY
) {
916 dprintk("nfsd: write sync %d\n", task_pid_nr(current
));
917 err
= vfs_fsync(file
, 0);
919 last_ino
= inode
->i_ino
;
920 last_dev
= inode
->i_sb
->s_dev
;
925 nfsd_vfs_write(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file
*file
,
926 loff_t offset
, struct kvec
*vec
, int vlen
,
927 unsigned long *cnt
, int *stablep
)
929 struct svc_export
*exp
;
934 int stable
= *stablep
;
937 loff_t end
= LLONG_MAX
;
938 unsigned int pflags
= current
->flags
;
940 if (test_bit(RQ_LOCAL
, &rqstp
->rq_flags
))
942 * We want less throttling in balance_dirty_pages()
943 * and shrink_inactive_list() so that nfs to
944 * localhost doesn't cause nfsd to lock up due to all
945 * the client's dirty pages or its congested queue.
947 current
->flags
|= PF_LESS_THROTTLE
;
949 inode
= file_inode(file
);
950 exp
= fhp
->fh_export
;
952 use_wgather
= (rqstp
->rq_vers
== 2) && EX_WGATHER(exp
);
957 /* Write the data. */
958 oldfs
= get_fs(); set_fs(KERNEL_DS
);
959 host_err
= vfs_writev(file
, (struct iovec __user
*)vec
, vlen
, &pos
);
964 nfsdstats
.io_write
+= host_err
;
965 fsnotify_modify(file
);
969 host_err
= wait_for_concurrent_writes(file
);
972 end
= offset
+ *cnt
- 1;
973 host_err
= vfs_fsync_range(file
, offset
, end
, 0);
978 dprintk("nfsd: write complete host_err=%d\n", host_err
);
982 err
= nfserrno(host_err
);
983 if (test_bit(RQ_LOCAL
, &rqstp
->rq_flags
))
984 tsk_restore_flags(current
, pflags
, PF_LESS_THROTTLE
);
989 * Read data from a file. count must contain the requested read count
990 * on entry. On return, *count contains the number of bytes actually read.
991 * N.B. After this call fhp needs an fh_put
993 __be32
nfsd_read(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
994 loff_t offset
, struct kvec
*vec
, int vlen
, unsigned long *count
)
1000 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
1004 ra
= nfsd_init_raparms(file
);
1005 err
= nfsd_vfs_read(rqstp
, file
, offset
, vec
, vlen
, count
);
1007 nfsd_put_raparams(file
, ra
);
1014 * Write data to a file.
1015 * The stable flag requests synchronous writes.
1016 * N.B. After this call fhp needs an fh_put
1019 nfsd_write(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file
*file
,
1020 loff_t offset
, struct kvec
*vec
, int vlen
, unsigned long *cnt
,
1026 err
= nfsd_permission(rqstp
, fhp
->fh_export
, fhp
->fh_dentry
,
1027 NFSD_MAY_WRITE
|NFSD_MAY_OWNER_OVERRIDE
);
1030 err
= nfsd_vfs_write(rqstp
, fhp
, file
, offset
, vec
, vlen
, cnt
,
1033 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_WRITE
, &file
);
1038 err
= nfsd_vfs_write(rqstp
, fhp
, file
, offset
, vec
, vlen
,
1046 #ifdef CONFIG_NFSD_V3
1048 * Commit all pending writes to stable storage.
1050 * Note: we only guarantee that data that lies within the range specified
1051 * by the 'offset' and 'count' parameters will be synced.
1053 * Unfortunately we cannot lock the file to make sure we return full WCC
1054 * data to the client, as locking happens lower down in the filesystem.
1057 nfsd_commit(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1058 loff_t offset
, unsigned long count
)
1061 loff_t end
= LLONG_MAX
;
1062 __be32 err
= nfserr_inval
;
1067 end
= offset
+ (loff_t
)count
- 1;
1072 err
= nfsd_open(rqstp
, fhp
, S_IFREG
,
1073 NFSD_MAY_WRITE
|NFSD_MAY_NOT_BREAK_LEASE
, &file
);
1076 if (EX_ISSYNC(fhp
->fh_export
)) {
1077 int err2
= vfs_fsync_range(file
, offset
, end
, 0);
1079 if (err2
!= -EINVAL
)
1080 err
= nfserrno(err2
);
1082 err
= nfserr_notsupp
;
1089 #endif /* CONFIG_NFSD_V3 */
1092 nfsd_create_setattr(struct svc_rqst
*rqstp
, struct svc_fh
*resfhp
,
1096 * Mode has already been set earlier in create:
1098 iap
->ia_valid
&= ~ATTR_MODE
;
1100 * Setting uid/gid works only for root. Irix appears to
1101 * send along the gid on create when it tries to implement
1102 * setgid directories via NFS:
1104 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID
))
1105 iap
->ia_valid
&= ~(ATTR_UID
|ATTR_GID
);
1107 return nfsd_setattr(rqstp
, resfhp
, iap
, 0, (time_t)0);
1108 /* Callers expect file metadata to be committed here */
1109 return nfserrno(commit_metadata(resfhp
));
1112 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1113 * setting size to 0 may fail for some specific file systems by the permission
1114 * checking which requires WRITE permission but the mode is 000.
1115 * we ignore the resizing(to 0) on the just new created file, since the size is
1116 * 0 after file created.
1118 * call this only after vfs_create() is called.
1121 nfsd_check_ignore_resizing(struct iattr
*iap
)
1123 if ((iap
->ia_valid
& ATTR_SIZE
) && (iap
->ia_size
== 0))
1124 iap
->ia_valid
&= ~ATTR_SIZE
;
1128 * Create a file (regular, directory, device, fifo); UNIX sockets
1129 * not yet implemented.
1130 * If the response fh has been verified, the parent directory should
1131 * already be locked. Note that the parent directory is left locked.
1133 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1136 nfsd_create(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1137 char *fname
, int flen
, struct iattr
*iap
,
1138 int type
, dev_t rdev
, struct svc_fh
*resfhp
)
1140 struct dentry
*dentry
, *dchild
= NULL
;
1150 if (isdotent(fname
, flen
))
1153 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1157 dentry
= fhp
->fh_dentry
;
1158 dirp
= d_inode(dentry
);
1160 err
= nfserr_notdir
;
1161 if (!dirp
->i_op
->lookup
)
1164 * Check whether the response file handle has been verified yet.
1165 * If it has, the parent directory should already be locked.
1167 if (!resfhp
->fh_dentry
) {
1168 host_err
= fh_want_write(fhp
);
1172 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
1173 fh_lock_nested(fhp
, I_MUTEX_PARENT
);
1174 dchild
= lookup_one_len(fname
, dentry
, flen
);
1175 host_err
= PTR_ERR(dchild
);
1178 err
= fh_compose(resfhp
, fhp
->fh_export
, dchild
, fhp
);
1182 /* called from nfsd_proc_create */
1183 dchild
= dget(resfhp
->fh_dentry
);
1184 if (!fhp
->fh_locked
) {
1185 /* not actually possible */
1187 "nfsd_create: parent %pd2 not locked!\n",
1194 * Make sure the child dentry is still negative ...
1197 if (d_really_is_positive(dchild
)) {
1198 dprintk("nfsd_create: dentry %pd/%pd not negative!\n",
1203 if (!(iap
->ia_valid
& ATTR_MODE
))
1205 iap
->ia_mode
= (iap
->ia_mode
& S_IALLUGO
) | type
;
1208 if (!S_ISREG(type
) && !S_ISDIR(type
) && !special_file(type
)) {
1209 printk(KERN_WARNING
"nfsd: bad file type %o in nfsd_create\n",
1215 * Get the dir op function pointer.
1221 host_err
= vfs_create(dirp
, dchild
, iap
->ia_mode
, true);
1223 nfsd_check_ignore_resizing(iap
);
1226 host_err
= vfs_mkdir(dirp
, dchild
, iap
->ia_mode
);
1232 host_err
= vfs_mknod(dirp
, dchild
, iap
->ia_mode
, rdev
);
1238 err
= nfsd_create_setattr(rqstp
, resfhp
, iap
);
1241 * nfsd_create_setattr already committed the child. Transactional
1242 * filesystems had a chance to commit changes for both parent and
1243 * child * simultaneously making the following commit_metadata a
1246 err2
= nfserrno(commit_metadata(fhp
));
1250 * Update the file handle to get the new inode info.
1253 err
= fh_update(resfhp
);
1255 if (dchild
&& !IS_ERR(dchild
))
1260 err
= nfserrno(host_err
);
1264 #ifdef CONFIG_NFSD_V3
1267 * NFSv3 and NFSv4 version of nfsd_create
1270 do_nfsd_create(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1271 char *fname
, int flen
, struct iattr
*iap
,
1272 struct svc_fh
*resfhp
, int createmode
, u32
*verifier
,
1273 bool *truncp
, bool *created
)
1275 struct dentry
*dentry
, *dchild
= NULL
;
1279 __u32 v_mtime
=0, v_atime
=0;
1285 if (isdotent(fname
, flen
))
1287 if (!(iap
->ia_valid
& ATTR_MODE
))
1289 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_EXEC
);
1293 dentry
= fhp
->fh_dentry
;
1294 dirp
= d_inode(dentry
);
1296 /* Get all the sanity checks out of the way before
1297 * we lock the parent. */
1298 err
= nfserr_notdir
;
1299 if (!dirp
->i_op
->lookup
)
1302 host_err
= fh_want_write(fhp
);
1306 fh_lock_nested(fhp
, I_MUTEX_PARENT
);
1309 * Compose the response file handle.
1311 dchild
= lookup_one_len(fname
, dentry
, flen
);
1312 host_err
= PTR_ERR(dchild
);
1316 /* If file doesn't exist, check for permissions to create one */
1317 if (d_really_is_negative(dchild
)) {
1318 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1323 err
= fh_compose(resfhp
, fhp
->fh_export
, dchild
, fhp
);
1327 if (nfsd_create_is_exclusive(createmode
)) {
1328 /* solaris7 gets confused (bugid 4218508) if these have
1329 * the high bit set, so just clear the high bits. If this is
1330 * ever changed to use different attrs for storing the
1331 * verifier, then do_open_lookup() will also need to be fixed
1334 v_mtime
= verifier
[0]&0x7fffffff;
1335 v_atime
= verifier
[1]&0x7fffffff;
1338 if (d_really_is_positive(dchild
)) {
1341 switch (createmode
) {
1342 case NFS3_CREATE_UNCHECKED
:
1343 if (! d_is_reg(dchild
))
1346 /* in nfsv4, we need to treat this case a little
1347 * differently. we don't want to truncate the
1348 * file now; this would be wrong if the OPEN
1349 * fails for some other reason. furthermore,
1350 * if the size is nonzero, we should ignore it
1351 * according to spec!
1353 *truncp
= (iap
->ia_valid
& ATTR_SIZE
) && !iap
->ia_size
;
1356 iap
->ia_valid
&= ATTR_SIZE
;
1360 case NFS3_CREATE_EXCLUSIVE
:
1361 if ( d_inode(dchild
)->i_mtime
.tv_sec
== v_mtime
1362 && d_inode(dchild
)->i_atime
.tv_sec
== v_atime
1363 && d_inode(dchild
)->i_size
== 0 ) {
1368 case NFS4_CREATE_EXCLUSIVE4_1
:
1369 if ( d_inode(dchild
)->i_mtime
.tv_sec
== v_mtime
1370 && d_inode(dchild
)->i_atime
.tv_sec
== v_atime
1371 && d_inode(dchild
)->i_size
== 0 ) {
1377 case NFS3_CREATE_GUARDED
:
1384 host_err
= vfs_create(dirp
, dchild
, iap
->ia_mode
, true);
1392 nfsd_check_ignore_resizing(iap
);
1394 if (nfsd_create_is_exclusive(createmode
)) {
1395 /* Cram the verifier into atime/mtime */
1396 iap
->ia_valid
= ATTR_MTIME
|ATTR_ATIME
1397 | ATTR_MTIME_SET
|ATTR_ATIME_SET
;
1398 /* XXX someone who knows this better please fix it for nsec */
1399 iap
->ia_mtime
.tv_sec
= v_mtime
;
1400 iap
->ia_atime
.tv_sec
= v_atime
;
1401 iap
->ia_mtime
.tv_nsec
= 0;
1402 iap
->ia_atime
.tv_nsec
= 0;
1406 err
= nfsd_create_setattr(rqstp
, resfhp
, iap
);
1409 * nfsd_create_setattr already committed the child
1410 * (and possibly also the parent).
1413 err
= nfserrno(commit_metadata(fhp
));
1416 * Update the filehandle to get the new inode info.
1419 err
= fh_update(resfhp
);
1423 if (dchild
&& !IS_ERR(dchild
))
1429 err
= nfserrno(host_err
);
1432 #endif /* CONFIG_NFSD_V3 */
1435 * Read a symlink. On entry, *lenp must contain the maximum path length that
1436 * fits into the buffer. On return, it contains the true length.
1437 * N.B. After this call fhp needs an fh_put
1440 nfsd_readlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, char *buf
, int *lenp
)
1442 struct inode
*inode
;
1448 err
= fh_verify(rqstp
, fhp
, S_IFLNK
, NFSD_MAY_NOP
);
1452 path
.mnt
= fhp
->fh_export
->ex_path
.mnt
;
1453 path
.dentry
= fhp
->fh_dentry
;
1454 inode
= d_inode(path
.dentry
);
1457 if (!inode
->i_op
->readlink
)
1461 /* N.B. Why does this call need a get_fs()??
1462 * Remove the set_fs and watch the fireworks:-) --okir
1465 oldfs
= get_fs(); set_fs(KERNEL_DS
);
1466 host_err
= inode
->i_op
->readlink(path
.dentry
, (char __user
*)buf
, *lenp
);
1477 err
= nfserrno(host_err
);
1482 * Create a symlink and look up its inode
1483 * N.B. After this call _both_ fhp and resfhp need an fh_put
1486 nfsd_symlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1487 char *fname
, int flen
,
1489 struct svc_fh
*resfhp
)
1491 struct dentry
*dentry
, *dnew
;
1496 if (!flen
|| path
[0] == '\0')
1499 if (isdotent(fname
, flen
))
1502 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1506 host_err
= fh_want_write(fhp
);
1511 dentry
= fhp
->fh_dentry
;
1512 dnew
= lookup_one_len(fname
, dentry
, flen
);
1513 host_err
= PTR_ERR(dnew
);
1517 host_err
= vfs_symlink(d_inode(dentry
), dnew
, path
);
1518 err
= nfserrno(host_err
);
1520 err
= nfserrno(commit_metadata(fhp
));
1525 cerr
= fh_compose(resfhp
, fhp
->fh_export
, dnew
, fhp
);
1527 if (err
==0) err
= cerr
;
1532 err
= nfserrno(host_err
);
1538 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1541 nfsd_link(struct svc_rqst
*rqstp
, struct svc_fh
*ffhp
,
1542 char *name
, int len
, struct svc_fh
*tfhp
)
1544 struct dentry
*ddir
, *dnew
, *dold
;
1549 err
= fh_verify(rqstp
, ffhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1552 err
= fh_verify(rqstp
, tfhp
, 0, NFSD_MAY_NOP
);
1556 if (d_is_dir(tfhp
->fh_dentry
))
1562 if (isdotent(name
, len
))
1565 host_err
= fh_want_write(tfhp
);
1567 err
= nfserrno(host_err
);
1571 fh_lock_nested(ffhp
, I_MUTEX_PARENT
);
1572 ddir
= ffhp
->fh_dentry
;
1573 dirp
= d_inode(ddir
);
1575 dnew
= lookup_one_len(name
, ddir
, len
);
1576 host_err
= PTR_ERR(dnew
);
1580 dold
= tfhp
->fh_dentry
;
1583 if (d_really_is_negative(dold
))
1585 host_err
= vfs_link(dold
, dirp
, dnew
, NULL
);
1587 err
= nfserrno(commit_metadata(ffhp
));
1589 err
= nfserrno(commit_metadata(tfhp
));
1591 if (host_err
== -EXDEV
&& rqstp
->rq_vers
== 2)
1594 err
= nfserrno(host_err
);
1600 fh_drop_write(tfhp
);
1605 err
= nfserrno(host_err
);
1611 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1614 nfsd_rename(struct svc_rqst
*rqstp
, struct svc_fh
*ffhp
, char *fname
, int flen
,
1615 struct svc_fh
*tfhp
, char *tname
, int tlen
)
1617 struct dentry
*fdentry
, *tdentry
, *odentry
, *ndentry
, *trap
;
1618 struct inode
*fdir
, *tdir
;
1622 err
= fh_verify(rqstp
, ffhp
, S_IFDIR
, NFSD_MAY_REMOVE
);
1625 err
= fh_verify(rqstp
, tfhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1629 fdentry
= ffhp
->fh_dentry
;
1630 fdir
= d_inode(fdentry
);
1632 tdentry
= tfhp
->fh_dentry
;
1633 tdir
= d_inode(tdentry
);
1636 if (!flen
|| isdotent(fname
, flen
) || !tlen
|| isdotent(tname
, tlen
))
1639 host_err
= fh_want_write(ffhp
);
1641 err
= nfserrno(host_err
);
1645 /* cannot use fh_lock as we need deadlock protective ordering
1646 * so do it by hand */
1647 trap
= lock_rename(tdentry
, fdentry
);
1648 ffhp
->fh_locked
= tfhp
->fh_locked
= true;
1652 odentry
= lookup_one_len(fname
, fdentry
, flen
);
1653 host_err
= PTR_ERR(odentry
);
1654 if (IS_ERR(odentry
))
1658 if (d_really_is_negative(odentry
))
1661 if (odentry
== trap
)
1664 ndentry
= lookup_one_len(tname
, tdentry
, tlen
);
1665 host_err
= PTR_ERR(ndentry
);
1666 if (IS_ERR(ndentry
))
1668 host_err
= -ENOTEMPTY
;
1669 if (ndentry
== trap
)
1673 if (ffhp
->fh_export
->ex_path
.mnt
!= tfhp
->fh_export
->ex_path
.mnt
)
1675 if (ffhp
->fh_export
->ex_path
.dentry
!= tfhp
->fh_export
->ex_path
.dentry
)
1678 host_err
= vfs_rename(fdir
, odentry
, tdir
, ndentry
, NULL
, 0);
1680 host_err
= commit_metadata(tfhp
);
1682 host_err
= commit_metadata(ffhp
);
1689 err
= nfserrno(host_err
);
1691 * We cannot rely on fh_unlock on the two filehandles,
1692 * as that would do the wrong thing if the two directories
1693 * were the same, so again we do it by hand.
1695 fill_post_wcc(ffhp
);
1696 fill_post_wcc(tfhp
);
1697 unlock_rename(tdentry
, fdentry
);
1698 ffhp
->fh_locked
= tfhp
->fh_locked
= false;
1699 fh_drop_write(ffhp
);
1706 * Unlink a file or directory
1707 * N.B. After this call fhp needs an fh_put
1710 nfsd_unlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, int type
,
1711 char *fname
, int flen
)
1713 struct dentry
*dentry
, *rdentry
;
1719 if (!flen
|| isdotent(fname
, flen
))
1721 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_REMOVE
);
1725 host_err
= fh_want_write(fhp
);
1729 fh_lock_nested(fhp
, I_MUTEX_PARENT
);
1730 dentry
= fhp
->fh_dentry
;
1731 dirp
= d_inode(dentry
);
1733 rdentry
= lookup_one_len(fname
, dentry
, flen
);
1734 host_err
= PTR_ERR(rdentry
);
1735 if (IS_ERR(rdentry
))
1738 if (d_really_is_negative(rdentry
)) {
1745 type
= d_inode(rdentry
)->i_mode
& S_IFMT
;
1747 if (type
!= S_IFDIR
)
1748 host_err
= vfs_unlink(dirp
, rdentry
, NULL
);
1750 host_err
= vfs_rmdir(dirp
, rdentry
);
1752 host_err
= commit_metadata(fhp
);
1756 err
= nfserrno(host_err
);
1762 * We do this buffering because we must not call back into the file
1763 * system's ->lookup() method from the filldir callback. That may well
1764 * deadlock a number of file systems.
1766 * This is based heavily on the implementation of same in XFS.
1768 struct buffered_dirent
{
1772 unsigned int d_type
;
1776 struct readdir_data
{
1777 struct dir_context ctx
;
1783 static int nfsd_buffered_filldir(struct dir_context
*ctx
, const char *name
,
1784 int namlen
, loff_t offset
, u64 ino
,
1785 unsigned int d_type
)
1787 struct readdir_data
*buf
=
1788 container_of(ctx
, struct readdir_data
, ctx
);
1789 struct buffered_dirent
*de
= (void *)(buf
->dirent
+ buf
->used
);
1790 unsigned int reclen
;
1792 reclen
= ALIGN(sizeof(struct buffered_dirent
) + namlen
, sizeof(u64
));
1793 if (buf
->used
+ reclen
> PAGE_SIZE
) {
1798 de
->namlen
= namlen
;
1799 de
->offset
= offset
;
1801 de
->d_type
= d_type
;
1802 memcpy(de
->name
, name
, namlen
);
1803 buf
->used
+= reclen
;
1808 static __be32
nfsd_buffered_readdir(struct file
*file
, nfsd_filldir_t func
,
1809 struct readdir_cd
*cdp
, loff_t
*offsetp
)
1811 struct buffered_dirent
*de
;
1815 struct readdir_data buf
= {
1816 .ctx
.actor
= nfsd_buffered_filldir
,
1817 .dirent
= (void *)__get_free_page(GFP_KERNEL
)
1821 return nfserrno(-ENOMEM
);
1826 unsigned int reclen
;
1828 cdp
->err
= nfserr_eof
; /* will be cleared on successful read */
1832 host_err
= iterate_dir(file
, &buf
.ctx
);
1844 de
= (struct buffered_dirent
*)buf
.dirent
;
1846 offset
= de
->offset
;
1848 if (func(cdp
, de
->name
, de
->namlen
, de
->offset
,
1849 de
->ino
, de
->d_type
))
1852 if (cdp
->err
!= nfs_ok
)
1855 reclen
= ALIGN(sizeof(*de
) + de
->namlen
,
1858 de
= (struct buffered_dirent
*)((char *)de
+ reclen
);
1860 if (size
> 0) /* We bailed out early */
1863 offset
= vfs_llseek(file
, 0, SEEK_CUR
);
1866 free_page((unsigned long)(buf
.dirent
));
1869 return nfserrno(host_err
);
1876 * Read entries from a directory.
1877 * The NFSv3/4 verifier we ignore for now.
1880 nfsd_readdir(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, loff_t
*offsetp
,
1881 struct readdir_cd
*cdp
, nfsd_filldir_t func
)
1885 loff_t offset
= *offsetp
;
1886 int may_flags
= NFSD_MAY_READ
;
1888 /* NFSv2 only supports 32 bit cookies */
1889 if (rqstp
->rq_vers
> 2)
1890 may_flags
|= NFSD_MAY_64BIT_COOKIE
;
1892 err
= nfsd_open(rqstp
, fhp
, S_IFDIR
, may_flags
, &file
);
1896 offset
= vfs_llseek(file
, offset
, SEEK_SET
);
1898 err
= nfserrno((int)offset
);
1902 err
= nfsd_buffered_readdir(file
, func
, cdp
, offsetp
);
1904 if (err
== nfserr_eof
|| err
== nfserr_toosmall
)
1905 err
= nfs_ok
; /* can still be found in ->err */
1913 * Get file system stats
1914 * N.B. After this call fhp needs an fh_put
1917 nfsd_statfs(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct kstatfs
*stat
, int access
)
1921 err
= fh_verify(rqstp
, fhp
, 0, NFSD_MAY_NOP
| access
);
1923 struct path path
= {
1924 .mnt
= fhp
->fh_export
->ex_path
.mnt
,
1925 .dentry
= fhp
->fh_dentry
,
1927 if (vfs_statfs(&path
, stat
))
1933 static int exp_rdonly(struct svc_rqst
*rqstp
, struct svc_export
*exp
)
1935 return nfsexp_flags(rqstp
, exp
) & NFSEXP_READONLY
;
1939 * Check for a user's access permissions to this inode.
1942 nfsd_permission(struct svc_rqst
*rqstp
, struct svc_export
*exp
,
1943 struct dentry
*dentry
, int acc
)
1945 struct inode
*inode
= d_inode(dentry
);
1948 if ((acc
& NFSD_MAY_MASK
) == NFSD_MAY_NOP
)
1951 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1953 (acc
& NFSD_MAY_READ
)? " read" : "",
1954 (acc
& NFSD_MAY_WRITE
)? " write" : "",
1955 (acc
& NFSD_MAY_EXEC
)? " exec" : "",
1956 (acc
& NFSD_MAY_SATTR
)? " sattr" : "",
1957 (acc
& NFSD_MAY_TRUNC
)? " trunc" : "",
1958 (acc
& NFSD_MAY_LOCK
)? " lock" : "",
1959 (acc
& NFSD_MAY_OWNER_OVERRIDE
)? " owneroverride" : "",
1961 IS_IMMUTABLE(inode
)? " immut" : "",
1962 IS_APPEND(inode
)? " append" : "",
1963 __mnt_is_readonly(exp
->ex_path
.mnt
)? " ro" : "");
1964 dprintk(" owner %d/%d user %d/%d\n",
1965 inode
->i_uid
, inode
->i_gid
, current_fsuid(), current_fsgid());
1968 /* Normally we reject any write/sattr etc access on a read-only file
1969 * system. But if it is IRIX doing check on write-access for a
1970 * device special file, we ignore rofs.
1972 if (!(acc
& NFSD_MAY_LOCAL_ACCESS
))
1973 if (acc
& (NFSD_MAY_WRITE
| NFSD_MAY_SATTR
| NFSD_MAY_TRUNC
)) {
1974 if (exp_rdonly(rqstp
, exp
) ||
1975 __mnt_is_readonly(exp
->ex_path
.mnt
))
1977 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode
))
1980 if ((acc
& NFSD_MAY_TRUNC
) && IS_APPEND(inode
))
1983 if (acc
& NFSD_MAY_LOCK
) {
1984 /* If we cannot rely on authentication in NLM requests,
1985 * just allow locks, otherwise require read permission, or
1988 if (exp
->ex_flags
& NFSEXP_NOAUTHNLM
)
1991 acc
= NFSD_MAY_READ
| NFSD_MAY_OWNER_OVERRIDE
;
1994 * The file owner always gets access permission for accesses that
1995 * would normally be checked at open time. This is to make
1996 * file access work even when the client has done a fchmod(fd, 0).
1998 * However, `cp foo bar' should fail nevertheless when bar is
1999 * readonly. A sensible way to do this might be to reject all
2000 * attempts to truncate a read-only file, because a creat() call
2001 * always implies file truncation.
2002 * ... but this isn't really fair. A process may reasonably call
2003 * ftruncate on an open file descriptor on a file with perm 000.
2004 * We must trust the client to do permission checking - using "ACCESS"
2007 if ((acc
& NFSD_MAY_OWNER_OVERRIDE
) &&
2008 uid_eq(inode
->i_uid
, current_fsuid()))
2011 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2012 err
= inode_permission(inode
, acc
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
));
2014 /* Allow read access to binaries even when mode 111 */
2015 if (err
== -EACCES
&& S_ISREG(inode
->i_mode
) &&
2016 (acc
== (NFSD_MAY_READ
| NFSD_MAY_OWNER_OVERRIDE
) ||
2017 acc
== (NFSD_MAY_READ
| NFSD_MAY_READ_IF_EXEC
)))
2018 err
= inode_permission(inode
, MAY_EXEC
);
2020 return err
? nfserrno(err
) : 0;
2024 nfsd_racache_shutdown(void)
2026 struct raparms
*raparm
, *last_raparm
;
2029 dprintk("nfsd: freeing readahead buffers.\n");
2031 for (i
= 0; i
< RAPARM_HASH_SIZE
; i
++) {
2032 raparm
= raparm_hash
[i
].pb_head
;
2034 last_raparm
= raparm
;
2035 raparm
= raparm
->p_next
;
2038 raparm_hash
[i
].pb_head
= NULL
;
2042 * Initialize readahead param cache
2045 nfsd_racache_init(int cache_size
)
2050 struct raparms
**raparm
= NULL
;
2053 if (raparm_hash
[0].pb_head
)
2055 nperbucket
= DIV_ROUND_UP(cache_size
, RAPARM_HASH_SIZE
);
2056 nperbucket
= max(2, nperbucket
);
2057 cache_size
= nperbucket
* RAPARM_HASH_SIZE
;
2059 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size
);
2061 for (i
= 0; i
< RAPARM_HASH_SIZE
; i
++) {
2062 spin_lock_init(&raparm_hash
[i
].pb_lock
);
2064 raparm
= &raparm_hash
[i
].pb_head
;
2065 for (j
= 0; j
< nperbucket
; j
++) {
2066 *raparm
= kzalloc(sizeof(struct raparms
), GFP_KERNEL
);
2069 raparm
= &(*raparm
)->p_next
;
2074 nfsdstats
.ra_size
= cache_size
;
2078 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2079 nfsd_racache_shutdown();