]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/nfsd/vfs.c
nfsd: minor nfsd_setattr cleanup
[mirror_ubuntu-zesty-kernel.git] / fs / nfsd / vfs.c
CommitLineData
1da177e4 1/*
1da177e4
LT
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.
5 *
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.
11 *
12 * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
13 * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
14 */
15
1da177e4
LT
16#include <linux/fs.h>
17#include <linux/file.h>
d6b29d7c 18#include <linux/splice.h>
95d871f0 19#include <linux/falloc.h>
1da177e4 20#include <linux/fcntl.h>
1da177e4 21#include <linux/namei.h>
1da177e4 22#include <linux/delay.h>
0eeca283 23#include <linux/fsnotify.h>
1da177e4 24#include <linux/posix_acl_xattr.h>
1da177e4 25#include <linux/xattr.h>
9a74af21
BH
26#include <linux/jhash.h>
27#include <linux/ima.h>
5a0e3ad6 28#include <linux/slab.h>
7c0f6ba6 29#include <linux/uaccess.h>
f501912a
BM
30#include <linux/exportfs.h>
31#include <linux/writeback.h>
18032ca0 32#include <linux/security.h>
9a74af21
BH
33
34#ifdef CONFIG_NFSD_V3
35#include "xdr3.h"
36#endif /* CONFIG_NFSD_V3 */
37
5be196e5 38#ifdef CONFIG_NFSD_V4
ffa0160a 39#include "../internal.h"
2ca72e17
BF
40#include "acl.h"
41#include "idmap.h"
1da177e4
LT
42#endif /* CONFIG_NFSD_V4 */
43
9a74af21
BH
44#include "nfsd.h"
45#include "vfs.h"
6e8b50d1 46#include "trace.h"
1da177e4
LT
47
48#define NFSDDBG_FACILITY NFSDDBG_FILEOP
1da177e4
LT
49
50
1da177e4
LT
51/*
52 * This is a cache of readahead params that help us choose the proper
53 * readahead strategy. Initially, we set all readahead parameters to 0
54 * and let the VFS handle things.
55 * If you increase the number of cached files very much, you'll need to
56 * add a hash table here.
57 */
58struct raparms {
59 struct raparms *p_next;
60 unsigned int p_count;
61 ino_t p_ino;
62 dev_t p_dev;
63 int p_set;
64 struct file_ra_state p_ra;
fce1456a 65 unsigned int p_hindex;
1da177e4
LT
66};
67
fce1456a
GB
68struct raparm_hbucket {
69 struct raparms *pb_head;
70 spinlock_t pb_lock;
71} ____cacheline_aligned_in_smp;
72
fce1456a
GB
73#define RAPARM_HASH_BITS 4
74#define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
75#define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
76static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
1da177e4
LT
77
78/*
79 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
80 * a mount point.
e0bb89ef 81 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
1da177e4
LT
82 * or nfs_ok having possibly changed *dpp and *expp
83 */
84int
85nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
86 struct svc_export **expp)
87{
88 struct svc_export *exp = *expp, *exp2 = NULL;
89 struct dentry *dentry = *dpp;
91c9fa8f
AV
90 struct path path = {.mnt = mntget(exp->ex_path.mnt),
91 .dentry = dget(dentry)};
6264d69d 92 int err = 0;
1da177e4 93
7cc90cc3 94 err = follow_down(&path);
cc53ce53
DH
95 if (err < 0)
96 goto out;
1da177e4 97
91c9fa8f 98 exp2 = rqst_exp_get_by_name(rqstp, &path);
1da177e4 99 if (IS_ERR(exp2)) {
3b6cee7b
BF
100 err = PTR_ERR(exp2);
101 /*
102 * We normally allow NFS clients to continue
103 * "underneath" a mountpoint that is not exported.
104 * The exception is V4ROOT, where no traversal is ever
105 * allowed without an explicit export of the new
106 * directory.
107 */
108 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
109 err = 0;
91c9fa8f 110 path_put(&path);
1da177e4
LT
111 goto out;
112 }
3c394dda
SD
113 if (nfsd_v4client(rqstp) ||
114 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
1da177e4 115 /* successfully crossed mount point */
1644ccc8 116 /*
91c9fa8f
AV
117 * This is subtle: path.dentry is *not* on path.mnt
118 * at this point. The only reason we are safe is that
119 * original mnt is pinned down by exp, so we should
120 * put path *before* putting exp
1644ccc8 121 */
91c9fa8f
AV
122 *dpp = path.dentry;
123 path.dentry = dentry;
1644ccc8 124 *expp = exp2;
91c9fa8f 125 exp2 = exp;
1da177e4 126 }
91c9fa8f
AV
127 path_put(&path);
128 exp_put(exp2);
1da177e4
LT
129out:
130 return err;
131}
132
289ede45
BF
133static void follow_to_parent(struct path *path)
134{
135 struct dentry *dp;
136
137 while (path->dentry == path->mnt->mnt_root && follow_up(path))
138 ;
139 dp = dget_parent(path->dentry);
140 dput(path->dentry);
141 path->dentry = dp;
142}
143
144static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
145{
146 struct svc_export *exp2;
147 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
148 .dentry = dget(dparent)};
149
150 follow_to_parent(&path);
151
152 exp2 = rqst_exp_parent(rqstp, &path);
153 if (PTR_ERR(exp2) == -ENOENT) {
154 *dentryp = dget(dparent);
155 } else if (IS_ERR(exp2)) {
156 path_put(&path);
157 return PTR_ERR(exp2);
158 } else {
159 *dentryp = dget(path.dentry);
160 exp_put(*exp);
161 *exp = exp2;
162 }
163 path_put(&path);
164 return 0;
165}
166
82ead7fe
BF
167/*
168 * For nfsd purposes, we treat V4ROOT exports as though there was an
169 * export at *every* directory.
170 */
3227fa41 171int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
82ead7fe
BF
172{
173 if (d_mountpoint(dentry))
174 return 1;
11fcee02
TM
175 if (nfsd4_is_junction(dentry))
176 return 1;
82ead7fe
BF
177 if (!(exp->ex_flags & NFSEXP_V4ROOT))
178 return 0;
2b0143b5 179 return d_inode(dentry) != NULL;
82ead7fe
BF
180}
181
6264d69d 182__be32
6c0a654d 183nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
5a022fc8 184 const char *name, unsigned int len,
6c0a654d 185 struct svc_export **exp_ret, struct dentry **dentry_ret)
1da177e4
LT
186{
187 struct svc_export *exp;
188 struct dentry *dparent;
189 struct dentry *dentry;
6264d69d 190 int host_err;
1da177e4
LT
191
192 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
193
1da177e4 194 dparent = fhp->fh_dentry;
bf18f163 195 exp = exp_get(fhp->fh_export);
1da177e4 196
1da177e4
LT
197 /* Lookup the name, but don't follow links */
198 if (isdotent(name, len)) {
199 if (len==1)
200 dentry = dget(dparent);
54775491 201 else if (dparent != exp->ex_path.dentry)
1da177e4 202 dentry = dget_parent(dparent);
fed83811 203 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
1da177e4
LT
204 dentry = dget(dparent); /* .. == . just like at / */
205 else {
206 /* checking mountpoint crossing is very different when stepping up */
289ede45
BF
207 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
208 if (host_err)
1da177e4 209 goto out_nfserr;
1da177e4
LT
210 }
211 } else {
4335723e
BF
212 /*
213 * In the nfsd4_open() case, this may be held across
214 * subsequent open and delegation acquisition which may
215 * need to take the child's i_mutex:
216 */
217 fh_lock_nested(fhp, I_MUTEX_PARENT);
1da177e4 218 dentry = lookup_one_len(name, dparent, len);
6264d69d 219 host_err = PTR_ERR(dentry);
1da177e4
LT
220 if (IS_ERR(dentry))
221 goto out_nfserr;
82ead7fe 222 if (nfsd_mountpoint(dentry, exp)) {
bbddca8e
N
223 /*
224 * We don't need the i_mutex after all. It's
225 * still possible we could open this (regular
226 * files can be mountpoints too), but the
227 * i_mutex is just there to prevent renames of
228 * something that we might be about to delegate,
229 * and a mountpoint won't be renamed:
230 */
231 fh_unlock(fhp);
6264d69d 232 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
1da177e4
LT
233 dput(dentry);
234 goto out_nfserr;
235 }
236 }
237 }
6c0a654d
BF
238 *dentry_ret = dentry;
239 *exp_ret = exp;
240 return 0;
241
242out_nfserr:
243 exp_put(exp);
244 return nfserrno(host_err);
245}
246
247/*
248 * Look up one component of a pathname.
249 * N.B. After this call _both_ fhp and resfh need an fh_put
250 *
251 * If the lookup would cross a mountpoint, and the mounted filesystem
252 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
253 * accepted as it stands and the mounted directory is
254 * returned. Otherwise the covered directory is returned.
255 * NOTE: this mountpoint crossing is not supported properly by all
256 * clients and is explicitly disallowed for NFSv3
257 * NeilBrown <neilb@cse.unsw.edu.au>
258 */
259__be32
260nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
5a022fc8 261 unsigned int len, struct svc_fh *resfh)
6c0a654d
BF
262{
263 struct svc_export *exp;
264 struct dentry *dentry;
265 __be32 err;
266
29a78a3e
BF
267 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
268 if (err)
269 return err;
6c0a654d
BF
270 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
271 if (err)
272 return err;
32c1eb0c
AA
273 err = check_nfsd_access(exp, rqstp);
274 if (err)
275 goto out;
1da177e4
LT
276 /*
277 * Note: we compose the file handle now, but as the
278 * dentry may be negative, it may need to be updated.
279 */
280 err = fh_compose(resfh, exp, dentry, fhp);
2b0143b5 281 if (!err && d_really_is_negative(dentry))
1da177e4 282 err = nfserr_noent;
32c1eb0c 283out:
1da177e4 284 dput(dentry);
1da177e4
LT
285 exp_put(exp);
286 return err;
1da177e4
LT
287}
288
f501912a
BM
289/*
290 * Commit metadata changes to stable storage.
291 */
292static int
293commit_metadata(struct svc_fh *fhp)
294{
2b0143b5 295 struct inode *inode = d_inode(fhp->fh_dentry);
f501912a 296 const struct export_operations *export_ops = inode->i_sb->s_export_op;
f501912a
BM
297
298 if (!EX_ISSYNC(fhp->fh_export))
299 return 0;
300
c3765016
CH
301 if (export_ops->commit_metadata)
302 return export_ops->commit_metadata(inode);
303 return sync_inode_metadata(inode, 1);
f501912a 304}
6c0a654d 305
1da177e4 306/*
818e5a22
CH
307 * Go over the attributes and take care of the small differences between
308 * NFS semantics and what Linux expects.
1da177e4 309 */
818e5a22
CH
310static void
311nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
1da177e4 312{
ca456252 313 /* sanitize the mode change */
1da177e4
LT
314 if (iap->ia_valid & ATTR_MODE) {
315 iap->ia_mode &= S_IALLUGO;
dee3209d 316 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
ca456252
JL
317 }
318
319 /* Revoke setuid/setgid on chown */
0953e620 320 if (!S_ISDIR(inode->i_mode) &&
c4fa6d7c 321 ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
ca456252
JL
322 iap->ia_valid |= ATTR_KILL_PRIV;
323 if (iap->ia_valid & ATTR_MODE) {
324 /* we're setting mode too, just clear the s*id bits */
8a0ce7d9 325 iap->ia_mode &= ~S_ISUID;
ca456252
JL
326 if (iap->ia_mode & S_IXGRP)
327 iap->ia_mode &= ~S_ISGID;
328 } else {
329 /* set ATTR_KILL_* bits and let VFS handle it */
330 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
8a0ce7d9 331 }
1da177e4 332 }
818e5a22
CH
333}
334
0839ffb8
BF
335static __be32
336nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
337 struct iattr *iap)
338{
339 struct inode *inode = d_inode(fhp->fh_dentry);
340 int host_err;
341
342 if (iap->ia_size < inode->i_size) {
343 __be32 err;
344
345 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
346 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
347 if (err)
348 return err;
349 }
350
351 host_err = get_write_access(inode);
352 if (host_err)
353 goto out_nfserrno;
354
355 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
356 if (host_err)
357 goto out_put_write_access;
358 return 0;
359
360out_put_write_access:
361 put_write_access(inode);
362out_nfserrno:
363 return nfserrno(host_err);
364}
365
818e5a22
CH
366/*
367 * Set various file attributes. After this call fhp needs an fh_put.
368 */
369__be32
370nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
371 int check_guard, time_t guardtime)
372{
373 struct dentry *dentry;
374 struct inode *inode;
375 int accmode = NFSD_MAY_SATTR;
376 umode_t ftype = 0;
377 __be32 err;
378 int host_err;
9f67f189 379 bool get_write_count;
d8d5a74c 380 bool size_change = (iap->ia_valid & ATTR_SIZE);
818e5a22
CH
381
382 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
383 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
384 if (iap->ia_valid & ATTR_SIZE)
385 ftype = S_IFREG;
386
9f67f189
BF
387 /* Callers that do fh_verify should do the fh_want_write: */
388 get_write_count = !fhp->fh_dentry;
389
818e5a22
CH
390 /* Get inode */
391 err = fh_verify(rqstp, fhp, ftype, accmode);
392 if (err)
d8d5a74c 393 return err;
9f67f189
BF
394 if (get_write_count) {
395 host_err = fh_want_write(fhp);
396 if (host_err)
d8d5a74c 397 goto out;
9f67f189 398 }
818e5a22
CH
399
400 dentry = fhp->fh_dentry;
2b0143b5 401 inode = d_inode(dentry);
818e5a22
CH
402
403 /* Ignore any mode updates on symlinks */
404 if (S_ISLNK(inode->i_mode))
405 iap->ia_valid &= ~ATTR_MODE;
406
407 if (!iap->ia_valid)
d8d5a74c 408 return 0;
1da177e4 409
818e5a22
CH
410 nfsd_sanitize_attrs(inode, iap);
411
d8d5a74c
CH
412 if (check_guard && guardtime != inode->i_ctime.tv_sec)
413 return nfserr_notsync;
414
818e5a22
CH
415 /*
416 * The size case is special, it changes the file in addition to the
0839ffb8 417 * attributes.
818e5a22 418 */
d8d5a74c 419 if (size_change) {
0839ffb8
BF
420 err = nfsd_get_write_access(rqstp, fhp, iap);
421 if (err)
d8d5a74c 422 return err;
f0c63124
CH
423
424 /*
0839ffb8
BF
425 * RFC5661, Section 18.30.4:
426 * Changing the size of a file with SETATTR indirectly
427 * changes the time_modify and change attributes.
428 *
429 * (and similar for the older RFCs)
f0c63124 430 */
0839ffb8
BF
431 if (iap->ia_size != i_size_read(inode))
432 iap->ia_valid |= ATTR_MTIME;
1da177e4 433 }
987da479 434
41f53350
CH
435 iap->ia_valid |= ATTR_CTIME;
436
987da479
CH
437 fh_lock(fhp);
438 host_err = notify_change(dentry, iap, NULL);
439 fh_unlock(fhp);
440
0839ffb8
BF
441 if (size_change)
442 put_write_access(inode);
0839ffb8 443out:
d8d5a74c
CH
444 if (!host_err)
445 host_err = commit_metadata(fhp);
446 return nfserrno(host_err);
1da177e4
LT
447}
448
5be196e5 449#if defined(CONFIG_NFSD_V4)
9b4146e8
CL
450/*
451 * NFS junction information is stored in an extended attribute.
452 */
453#define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
454
455/**
456 * nfsd4_is_junction - Test if an object could be an NFS junction
457 *
458 * @dentry: object to test
459 *
460 * Returns 1 if "dentry" appears to contain NFS junction information.
461 * Otherwise 0 is returned.
462 */
11fcee02
TM
463int nfsd4_is_junction(struct dentry *dentry)
464{
2b0143b5 465 struct inode *inode = d_inode(dentry);
11fcee02
TM
466
467 if (inode == NULL)
468 return 0;
469 if (inode->i_mode & S_IXUGO)
470 return 0;
471 if (!(inode->i_mode & S_ISVTX))
472 return 0;
9b4146e8 473 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
11fcee02
TM
474 return 0;
475 return 1;
476}
18032ca0
DQ
477#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
478__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
479 struct xdr_netobj *label)
480{
481 __be32 error;
482 int host_error;
483 struct dentry *dentry;
484
485 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
486 if (error)
487 return error;
488
489 dentry = fhp->fh_dentry;
490
5955102c 491 inode_lock(d_inode(dentry));
18032ca0 492 host_error = security_inode_setsecctx(dentry, label->data, label->len);
5955102c 493 inode_unlock(d_inode(dentry));
18032ca0
DQ
494 return nfserrno(host_error);
495}
496#else
497__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
498 struct xdr_netobj *label)
499{
500 return nfserr_notsupp;
501}
502#endif
503
ffa0160a
CH
504__be32 nfsd4_clone_file_range(struct file *src, u64 src_pos, struct file *dst,
505 u64 dst_pos, u64 count)
506{
031a072a 507 return nfserrno(do_clone_file_range(src, src_pos, dst, dst_pos, count));
ffa0160a
CH
508}
509
29ae7f9d
AS
510ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
511 u64 dst_pos, u64 count)
512{
513
514 /*
515 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
516 * thread and client rpc slot. The choice of 4MB is somewhat
517 * arbitrary. We might instead base this on r/wsize, or make it
518 * tunable, or use a time instead of a byte limit, or implement
519 * asynchronous copy. In theory a client could also recognize a
520 * limit like this and pipeline multiple COPY requests.
521 */
522 count = min_t(u64, count, 1 << 22);
523 return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
524}
525
95d871f0
AS
526__be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
527 struct file *file, loff_t offset, loff_t len,
528 int flags)
529{
95d871f0
AS
530 int error;
531
532 if (!S_ISREG(file_inode(file)->i_mode))
533 return nfserr_inval;
534
95d871f0
AS
535 error = vfs_fallocate(file, flags, offset, len);
536 if (!error)
537 error = commit_metadata(fhp);
538
539 return nfserrno(error);
540}
6a85d6c7 541#endif /* defined(CONFIG_NFSD_V4) */
1da177e4
LT
542
543#ifdef CONFIG_NFSD_V3
544/*
545 * Check server access rights to a file system object
546 */
547struct accessmap {
548 u32 access;
549 int how;
550};
551static struct accessmap nfs3_regaccess[] = {
8837abca
MS
552 { NFS3_ACCESS_READ, NFSD_MAY_READ },
553 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
554 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
555 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
1da177e4
LT
556
557 { 0, 0 }
558};
559
560static struct accessmap nfs3_diraccess[] = {
8837abca
MS
561 { NFS3_ACCESS_READ, NFSD_MAY_READ },
562 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
563 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
564 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
565 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
1da177e4
LT
566
567 { 0, 0 }
568};
569
570static struct accessmap nfs3_anyaccess[] = {
571 /* Some clients - Solaris 2.6 at least, make an access call
572 * to the server to check for access for things like /dev/null
573 * (which really, the server doesn't care about). So
574 * We provide simple access checking for them, looking
575 * mainly at mode bits, and we make sure to ignore read-only
576 * filesystem checks
577 */
8837abca
MS
578 { NFS3_ACCESS_READ, NFSD_MAY_READ },
579 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
580 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
581 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
1da177e4
LT
582
583 { 0, 0 }
584};
585
6264d69d 586__be32
1da177e4
LT
587nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
588{
589 struct accessmap *map;
590 struct svc_export *export;
591 struct dentry *dentry;
592 u32 query, result = 0, sresult = 0;
6264d69d 593 __be32 error;
1da177e4 594
8837abca 595 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
1da177e4
LT
596 if (error)
597 goto out;
598
599 export = fhp->fh_export;
600 dentry = fhp->fh_dentry;
601
e36cb0b8 602 if (d_is_reg(dentry))
1da177e4 603 map = nfs3_regaccess;
e36cb0b8 604 else if (d_is_dir(dentry))
1da177e4
LT
605 map = nfs3_diraccess;
606 else
607 map = nfs3_anyaccess;
608
609
610 query = *access;
611 for (; map->access; map++) {
612 if (map->access & query) {
6264d69d 613 __be32 err2;
1da177e4
LT
614
615 sresult |= map->access;
616
0ec757df 617 err2 = nfsd_permission(rqstp, export, dentry, map->how);
1da177e4
LT
618 switch (err2) {
619 case nfs_ok:
620 result |= map->access;
621 break;
622
623 /* the following error codes just mean the access was not allowed,
624 * rather than an error occurred */
625 case nfserr_rofs:
626 case nfserr_acces:
627 case nfserr_perm:
628 /* simply don't "or" in the access bit. */
629 break;
630 default:
631 error = err2;
632 goto out;
633 }
634 }
635 }
636 *access = result;
637 if (supported)
638 *supported = sresult;
639
640 out:
641 return error;
642}
643#endif /* CONFIG_NFSD_V3 */
644
105f4622
BF
645static int nfsd_open_break_lease(struct inode *inode, int access)
646{
647 unsigned int mode;
1da177e4 648
105f4622
BF
649 if (access & NFSD_MAY_NOT_BREAK_LEASE)
650 return 0;
651 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
652 return break_lease(inode, mode | O_NONBLOCK);
653}
1da177e4
LT
654
655/*
656 * Open an existing file or directory.
999448a8
BS
657 * The may_flags argument indicates the type of open (read/write/lock)
658 * and additional flags.
1da177e4
LT
659 * N.B. After this call fhp needs an fh_put
660 */
6264d69d 661__be32
175a4eb7 662nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
999448a8 663 int may_flags, struct file **filp)
1da177e4 664{
765927b2 665 struct path path;
1da177e4 666 struct inode *inode;
8519f994 667 struct file *file;
6264d69d
AV
668 int flags = O_RDONLY|O_LARGEFILE;
669 __be32 err;
91885258 670 int host_err = 0;
1da177e4 671
e0e81739
DH
672 validate_process_creds();
673
1da177e4
LT
674 /*
675 * If we get here, then the client has already done an "open",
676 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
677 * in case a chmod has now revoked permission.
d91d0b56
BF
678 *
679 * Arguably we should also allow the owner override for
680 * directories, but we never have and it doesn't seem to have
681 * caused anyone a problem. If we were to change this, note
682 * also that our filldir callbacks would need a variant of
683 * lookup_one_len that doesn't check permissions.
1da177e4 684 */
d91d0b56
BF
685 if (type == S_IFREG)
686 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
687 err = fh_verify(rqstp, fhp, type, may_flags);
1da177e4
LT
688 if (err)
689 goto out;
690
765927b2
AV
691 path.mnt = fhp->fh_export->ex_path.mnt;
692 path.dentry = fhp->fh_dentry;
2b0143b5 693 inode = d_inode(path.dentry);
1da177e4
LT
694
695 /* Disallow write access to files with the append-only bit set
696 * or any access when mandatory locking enabled
697 */
698 err = nfserr_perm;
999448a8 699 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
1da177e4 700 goto out;
5e7fc436
BF
701 /*
702 * We must ignore files (but only files) which might have mandatory
703 * locks on them because there is no way to know if the accesser has
704 * the lock.
705 */
706 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
1da177e4
LT
707 goto out;
708
709 if (!inode->i_fop)
710 goto out;
711
999448a8 712 host_err = nfsd_open_break_lease(inode, may_flags);
6264d69d 713 if (host_err) /* NOMEM or WOULDBLOCK */
1da177e4
LT
714 goto out_nfserr;
715
999448a8
BS
716 if (may_flags & NFSD_MAY_WRITE) {
717 if (may_flags & NFSD_MAY_READ)
9ecb6a08
BF
718 flags = O_RDWR|O_LARGEFILE;
719 else
720 flags = O_WRONLY|O_LARGEFILE;
1da177e4 721 }
999448a8 722
8519f994
KM
723 file = dentry_open(&path, flags, current_cred());
724 if (IS_ERR(file)) {
725 host_err = PTR_ERR(file);
726 goto out_nfserr;
727 }
728
5e40d331 729 host_err = ima_file_check(file, may_flags, 0);
8519f994 730 if (host_err) {
fd891454 731 fput(file);
8519f994 732 goto out_nfserr;
06effdbb
BS
733 }
734
8519f994
KM
735 if (may_flags & NFSD_MAY_64BIT_COOKIE)
736 file->f_mode |= FMODE_64BITHASH;
737 else
738 file->f_mode |= FMODE_32BITHASH;
739
740 *filp = file;
1da177e4 741out_nfserr:
6264d69d 742 err = nfserrno(host_err);
1da177e4 743out:
e0e81739 744 validate_process_creds();
1da177e4
LT
745 return err;
746}
747
e749a462
CH
748struct raparms *
749nfsd_init_raparms(struct file *file)
1da177e4 750{
e749a462
CH
751 struct inode *inode = file_inode(file);
752 dev_t dev = inode->i_sb->s_dev;
753 ino_t ino = inode->i_ino;
1da177e4
LT
754 struct raparms *ra, **rap, **frap = NULL;
755 int depth = 0;
fce1456a
GB
756 unsigned int hash;
757 struct raparm_hbucket *rab;
758
759 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
760 rab = &raparm_hash[hash];
1da177e4 761
fce1456a
GB
762 spin_lock(&rab->pb_lock);
763 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
1da177e4
LT
764 if (ra->p_ino == ino && ra->p_dev == dev)
765 goto found;
766 depth++;
767 if (ra->p_count == 0)
768 frap = rap;
769 }
3aa6e0aa 770 depth = nfsdstats.ra_size;
1da177e4 771 if (!frap) {
fce1456a 772 spin_unlock(&rab->pb_lock);
1da177e4
LT
773 return NULL;
774 }
775 rap = frap;
776 ra = *frap;
777 ra->p_dev = dev;
778 ra->p_ino = ino;
779 ra->p_set = 0;
fce1456a 780 ra->p_hindex = hash;
1da177e4 781found:
fce1456a 782 if (rap != &rab->pb_head) {
1da177e4 783 *rap = ra->p_next;
fce1456a
GB
784 ra->p_next = rab->pb_head;
785 rab->pb_head = ra;
1da177e4
LT
786 }
787 ra->p_count++;
788 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
fce1456a 789 spin_unlock(&rab->pb_lock);
e749a462
CH
790
791 if (ra->p_set)
792 file->f_ra = ra->p_ra;
1da177e4
LT
793 return ra;
794}
795
e749a462
CH
796void nfsd_put_raparams(struct file *file, struct raparms *ra)
797{
798 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
799
800 spin_lock(&rab->pb_lock);
801 ra->p_ra = file->f_ra;
802 ra->p_set = 1;
803 ra->p_count--;
804 spin_unlock(&rab->pb_lock);
805}
806
1da177e4 807/*
cf8208d0
JA
808 * Grab and keep cached pages associated with a file in the svc_rqst
809 * so that they can be passed to the network sendmsg/sendpage routines
810 * directly. They will be released after the sending has completed.
1da177e4
LT
811 */
812static int
cf8208d0
JA
813nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
814 struct splice_desc *sd)
1da177e4 815{
cf8208d0 816 struct svc_rqst *rqstp = sd->u.data;
afc59400 817 struct page **pp = rqstp->rq_next_page;
cf8208d0
JA
818 struct page *page = buf->page;
819 size_t size;
cf8208d0
JA
820
821 size = sd->len;
1da177e4
LT
822
823 if (rqstp->rq_res.page_len == 0) {
824 get_page(page);
afc59400
BF
825 put_page(*rqstp->rq_next_page);
826 *(rqstp->rq_next_page++) = page;
cf8208d0 827 rqstp->rq_res.page_base = buf->offset;
1da177e4 828 rqstp->rq_res.page_len = size;
44524359 829 } else if (page != pp[-1]) {
1da177e4 830 get_page(page);
afc59400
BF
831 if (*rqstp->rq_next_page)
832 put_page(*rqstp->rq_next_page);
833 *(rqstp->rq_next_page++) = page;
1da177e4 834 rqstp->rq_res.page_len += size;
44524359 835 } else
1da177e4 836 rqstp->rq_res.page_len += size;
1da177e4 837
1da177e4
LT
838 return size;
839}
840
cf8208d0
JA
841static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
842 struct splice_desc *sd)
843{
844 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
845}
846
e2afc819
JL
847static __be32
848nfsd_finish_read(struct file *file, unsigned long *count, int host_err)
1da177e4 849{
6264d69d
AV
850 if (host_err >= 0) {
851 nfsdstats.io_read += host_err;
852 *count = host_err;
2a12a9d7 853 fsnotify_access(file);
dc97618d 854 return 0;
1da177e4 855 } else
dc97618d
BF
856 return nfserrno(host_err);
857}
858
e2afc819 859__be32 nfsd_splice_read(struct svc_rqst *rqstp,
dc97618d
BF
860 struct file *file, loff_t offset, unsigned long *count)
861{
862 struct splice_desc sd = {
863 .len = 0,
864 .total_len = *count,
865 .pos = offset,
866 .u.data = rqstp,
867 };
868 int host_err;
869
870 rqstp->rq_next_page = rqstp->rq_respages + 1;
871 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
872 return nfsd_finish_read(file, count, host_err);
873}
874
e2afc819 875__be32 nfsd_readv(struct file *file, loff_t offset, struct kvec *vec, int vlen,
dc97618d
BF
876 unsigned long *count)
877{
878 mm_segment_t oldfs;
879 int host_err;
880
881 oldfs = get_fs();
882 set_fs(KERNEL_DS);
793b80ef 883 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset, 0);
dc97618d
BF
884 set_fs(oldfs);
885 return nfsd_finish_read(file, count, host_err);
886}
887
888static __be32
889nfsd_vfs_read(struct svc_rqst *rqstp, struct file *file,
890 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
891{
779fb0f3 892 if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
dc97618d
BF
893 return nfsd_splice_read(rqstp, file, offset, count);
894 else
895 return nfsd_readv(file, offset, vec, vlen, count);
1da177e4
LT
896}
897
d911df7b
BF
898/*
899 * Gathered writes: If another process is currently writing to the file,
900 * there's a high chance this is another nfsd (triggered by a bulk write
901 * from a client's biod). Rather than syncing the file with each write
902 * request, we sleep for 10 msec.
903 *
904 * I don't know if this roughly approximates C. Juszak's idea of
905 * gathered writes, but it's a nice and simple solution (IMHO), and it
906 * seems to work:-)
907 *
908 * Note: we do this only in the NFSv2 case, since v3 and higher have a
909 * better tool (separate unstable writes and commits) for solving this
910 * problem.
911 */
912static int wait_for_concurrent_writes(struct file *file)
913{
496ad9aa 914 struct inode *inode = file_inode(file);
d911df7b
BF
915 static ino_t last_ino;
916 static dev_t last_dev;
917 int err = 0;
918
919 if (atomic_read(&inode->i_writecount) > 1
920 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
921 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
922 msleep(10);
923 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
924 }
925
926 if (inode->i_state & I_DIRTY) {
927 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
8018ab05 928 err = vfs_fsync(file, 0);
d911df7b
BF
929 }
930 last_ino = inode->i_ino;
931 last_dev = inode->i_sb->s_dev;
932 return err;
933}
934
af90f707 935__be32
1da177e4
LT
936nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
937 loff_t offset, struct kvec *vec, int vlen,
31dec253 938 unsigned long *cnt, int *stablep)
1da177e4
LT
939{
940 struct svc_export *exp;
1da177e4
LT
941 struct inode *inode;
942 mm_segment_t oldfs;
6264d69d
AV
943 __be32 err = 0;
944 int host_err;
1da177e4 945 int stable = *stablep;
48e03bc5 946 int use_wgather;
e49dbbf3 947 loff_t pos = offset;
8658452e 948 unsigned int pflags = current->flags;
24368aad 949 int flags = 0;
8658452e 950
7501cc2b 951 if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
8658452e
N
952 /*
953 * We want less throttling in balance_dirty_pages()
954 * and shrink_inactive_list() so that nfs to
955 * localhost doesn't cause nfsd to lock up due to all
956 * the client's dirty pages or its congested queue.
957 */
958 current->flags |= PF_LESS_THROTTLE;
1da177e4 959
6f4e0d5a 960 inode = file_inode(file);
1da177e4
LT
961 exp = fhp->fh_export;
962
48e03bc5 963 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1da177e4 964
1da177e4
LT
965 if (!EX_ISSYNC(exp))
966 stable = 0;
1da177e4 967
24368aad
CH
968 if (stable && !use_wgather)
969 flags |= RWF_SYNC;
970
1da177e4
LT
971 /* Write the data. */
972 oldfs = get_fs(); set_fs(KERNEL_DS);
24368aad 973 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos, flags);
1da177e4 974 set_fs(oldfs);
e4636d53
BF
975 if (host_err < 0)
976 goto out_nfserr;
977 *cnt = host_err;
978 nfsdstats.io_write += host_err;
2a12a9d7 979 fsnotify_modify(file);
1da177e4 980
24368aad
CH
981 if (stable && use_wgather)
982 host_err = wait_for_concurrent_writes(file);
1da177e4 983
e4636d53 984out_nfserr:
6264d69d 985 dprintk("nfsd: write complete host_err=%d\n", host_err);
a0d24b29 986 if (host_err >= 0)
1da177e4 987 err = 0;
a0d24b29 988 else
6264d69d 989 err = nfserrno(host_err);
7501cc2b 990 if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
8658452e 991 tsk_restore_flags(current, pflags, PF_LESS_THROTTLE);
1da177e4
LT
992 return err;
993}
994
dc97618d
BF
995/*
996 * Read data from a file. count must contain the requested read count
997 * on entry. On return, *count contains the number of bytes actually read.
998 * N.B. After this call fhp needs an fh_put
999 */
1000__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1001 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1002{
1003 struct file *file;
1004 struct raparms *ra;
1005 __be32 err;
1006
6e8b50d1 1007 trace_read_start(rqstp, fhp, offset, vlen);
e749a462 1008 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
dc97618d
BF
1009 if (err)
1010 return err;
1011
e749a462 1012 ra = nfsd_init_raparms(file);
6e8b50d1
JL
1013
1014 trace_read_opened(rqstp, fhp, offset, vlen);
dc97618d 1015 err = nfsd_vfs_read(rqstp, file, offset, vec, vlen, count);
6e8b50d1
JL
1016 trace_read_io_done(rqstp, fhp, offset, vlen);
1017
e749a462
CH
1018 if (ra)
1019 nfsd_put_raparams(file, ra);
1020 fput(file);
dc97618d 1021
6e8b50d1
JL
1022 trace_read_done(rqstp, fhp, offset, vlen);
1023
fa0a2126
BF
1024 return err;
1025}
1026
1da177e4
LT
1027/*
1028 * Write data to a file.
1029 * The stable flag requests synchronous writes.
1030 * N.B. After this call fhp needs an fh_put
1031 */
6264d69d 1032__be32
1da177e4 1033nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
31dec253 1034 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
1da177e4
LT
1035 int *stablep)
1036{
6264d69d 1037 __be32 err = 0;
1da177e4 1038
6e8b50d1
JL
1039 trace_write_start(rqstp, fhp, offset, vlen);
1040
1da177e4 1041 if (file) {
0ec757df 1042 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
8837abca 1043 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
1da177e4
LT
1044 if (err)
1045 goto out;
6e8b50d1 1046 trace_write_opened(rqstp, fhp, offset, vlen);
1da177e4
LT
1047 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1048 stablep);
6e8b50d1 1049 trace_write_io_done(rqstp, fhp, offset, vlen);
1da177e4 1050 } else {
8837abca 1051 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1da177e4
LT
1052 if (err)
1053 goto out;
1054
6e8b50d1 1055 trace_write_opened(rqstp, fhp, offset, vlen);
1da177e4
LT
1056 if (cnt)
1057 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1058 cnt, stablep);
6e8b50d1 1059 trace_write_io_done(rqstp, fhp, offset, vlen);
fd891454 1060 fput(file);
1da177e4
LT
1061 }
1062out:
6e8b50d1 1063 trace_write_done(rqstp, fhp, offset, vlen);
1da177e4
LT
1064 return err;
1065}
1066
1067#ifdef CONFIG_NFSD_V3
1068/*
1069 * Commit all pending writes to stable storage.
aa696a6f
TM
1070 *
1071 * Note: we only guarantee that data that lies within the range specified
1072 * by the 'offset' and 'count' parameters will be synced.
1da177e4
LT
1073 *
1074 * Unfortunately we cannot lock the file to make sure we return full WCC
1075 * data to the client, as locking happens lower down in the filesystem.
1076 */
6264d69d 1077__be32
1da177e4
LT
1078nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1079 loff_t offset, unsigned long count)
1080{
1081 struct file *file;
aa696a6f
TM
1082 loff_t end = LLONG_MAX;
1083 __be32 err = nfserr_inval;
1da177e4 1084
aa696a6f
TM
1085 if (offset < 0)
1086 goto out;
1087 if (count != 0) {
1088 end = offset + (loff_t)count - 1;
1089 if (end < offset)
1090 goto out;
1091 }
1da177e4 1092
91885258
JL
1093 err = nfsd_open(rqstp, fhp, S_IFREG,
1094 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
8837abca 1095 if (err)
aa696a6f 1096 goto out;
1da177e4 1097 if (EX_ISSYNC(fhp->fh_export)) {
8018ab05 1098 int err2 = vfs_fsync_range(file, offset, end, 0);
aa696a6f
TM
1099
1100 if (err2 != -EINVAL)
1101 err = nfserrno(err2);
1102 else
1da177e4 1103 err = nfserr_notsupp;
1da177e4
LT
1104 }
1105
fd891454 1106 fput(file);
aa696a6f 1107out:
1da177e4
LT
1108 return err;
1109}
1110#endif /* CONFIG_NFSD_V3 */
1111
f2b0dee2 1112static __be32
5c002b3b
BF
1113nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1114 struct iattr *iap)
1115{
1116 /*
1117 * Mode has already been set earlier in create:
1118 */
1119 iap->ia_valid &= ~ATTR_MODE;
1120 /*
1121 * Setting uid/gid works only for root. Irix appears to
1122 * send along the gid on create when it tries to implement
1123 * setgid directories via NFS:
1124 */
6fab8779 1125 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
5c002b3b
BF
1126 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1127 if (iap->ia_valid)
1128 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
0f3a24b4 1129 /* Callers expect file metadata to be committed here */
722b620d 1130 return nfserrno(commit_metadata(resfhp));
5c002b3b
BF
1131}
1132
4ac35c2f 1133/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1134 * setting size to 0 may fail for some specific file systems by the permission
1135 * checking which requires WRITE permission but the mode is 000.
1136 * we ignore the resizing(to 0) on the just new created file, since the size is
1137 * 0 after file created.
1138 *
1139 * call this only after vfs_create() is called.
1140 * */
1141static void
1142nfsd_check_ignore_resizing(struct iattr *iap)
1143{
1144 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1145 iap->ia_valid &= ~ATTR_SIZE;
1146}
1147
b44061d0 1148/* The parent directory should already be locked: */
6264d69d 1149__be32
b44061d0 1150nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
1da177e4
LT
1151 char *fname, int flen, struct iattr *iap,
1152 int type, dev_t rdev, struct svc_fh *resfhp)
1153{
2b118859 1154 struct dentry *dentry, *dchild;
1da177e4 1155 struct inode *dirp;
6264d69d 1156 __be32 err;
5c002b3b 1157 __be32 err2;
6264d69d 1158 int host_err;
1da177e4 1159
1da177e4 1160 dentry = fhp->fh_dentry;
2b0143b5 1161 dirp = d_inode(dentry);
1da177e4 1162
b44061d0
BF
1163 dchild = dget(resfhp->fh_dentry);
1164 if (!fhp->fh_locked) {
1165 WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
a6a9f18f 1166 dentry);
b44061d0
BF
1167 err = nfserr_io;
1168 goto out;
1da177e4 1169 }
1da177e4 1170
7eed34f1
OD
1171 err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
1172 if (err)
1173 goto out;
1174
1da177e4
LT
1175 if (!(iap->ia_valid & ATTR_MODE))
1176 iap->ia_mode = 0;
1177 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1178
088406bc 1179 err = 0;
4a55c101 1180 host_err = 0;
1da177e4
LT
1181 switch (type) {
1182 case S_IFREG:
312b63fb 1183 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
4ac35c2f 1184 if (!host_err)
1185 nfsd_check_ignore_resizing(iap);
1da177e4
LT
1186 break;
1187 case S_IFDIR:
6264d69d 1188 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
1da177e4
LT
1189 break;
1190 case S_IFCHR:
1191 case S_IFBLK:
1192 case S_IFIFO:
1193 case S_IFSOCK:
6264d69d 1194 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
1da177e4 1195 break;
71423274
BF
1196 default:
1197 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1198 type);
1199 host_err = -EINVAL;
1da177e4 1200 }
4a55c101 1201 if (host_err < 0)
1da177e4
LT
1202 goto out_nfserr;
1203
f501912a 1204 err = nfsd_create_setattr(rqstp, resfhp, iap);
1da177e4 1205
f501912a 1206 /*
0f3a24b4
TM
1207 * nfsd_create_setattr already committed the child. Transactional
1208 * filesystems had a chance to commit changes for both parent and
b44061d0 1209 * child simultaneously making the following commit_metadata a
0f3a24b4 1210 * noop.
f501912a
BM
1211 */
1212 err2 = nfserrno(commit_metadata(fhp));
5c002b3b
BF
1213 if (err2)
1214 err = err2;
1da177e4
LT
1215 /*
1216 * Update the file handle to get the new inode info.
1217 */
1218 if (!err)
1219 err = fh_update(resfhp);
1220out:
2b118859 1221 dput(dchild);
1da177e4
LT
1222 return err;
1223
1224out_nfserr:
6264d69d 1225 err = nfserrno(host_err);
1da177e4
LT
1226 goto out;
1227}
1228
b44061d0
BF
1229/*
1230 * Create a filesystem object (regular, directory, special).
1231 * Note that the parent directory is left locked.
1232 *
1233 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1234 */
1235__be32
1236nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1237 char *fname, int flen, struct iattr *iap,
1238 int type, dev_t rdev, struct svc_fh *resfhp)
1239{
1240 struct dentry *dentry, *dchild = NULL;
1241 struct inode *dirp;
1242 __be32 err;
1243 int host_err;
1244
1245 if (isdotent(fname, flen))
1246 return nfserr_exist;
1247
fa08139d 1248 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
b44061d0
BF
1249 if (err)
1250 return err;
1251
1252 dentry = fhp->fh_dentry;
1253 dirp = d_inode(dentry);
1254
1255 host_err = fh_want_write(fhp);
1256 if (host_err)
1257 return nfserrno(host_err);
1258
1259 fh_lock_nested(fhp, I_MUTEX_PARENT);
1260 dchild = lookup_one_len(fname, dentry, flen);
1261 host_err = PTR_ERR(dchild);
1262 if (IS_ERR(dchild))
1263 return nfserrno(host_err);
1264 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
502aa0a5
JB
1265 /*
1266 * We unconditionally drop our ref to dchild as fh_compose will have
1267 * already grabbed its own ref for it.
1268 */
1269 dput(dchild);
1270 if (err)
b44061d0 1271 return err;
b44061d0
BF
1272 return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
1273 rdev, resfhp);
1274}
1275
1da177e4 1276#ifdef CONFIG_NFSD_V3
ac6721a1 1277
1da177e4 1278/*
ac6721a1 1279 * NFSv3 and NFSv4 version of nfsd_create
1da177e4 1280 */
6264d69d 1281__be32
ac6721a1 1282do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1da177e4
LT
1283 char *fname, int flen, struct iattr *iap,
1284 struct svc_fh *resfhp, int createmode, u32 *verifier,
856121b2 1285 bool *truncp, bool *created)
1da177e4
LT
1286{
1287 struct dentry *dentry, *dchild = NULL;
1288 struct inode *dirp;
6264d69d
AV
1289 __be32 err;
1290 int host_err;
1da177e4 1291 __u32 v_mtime=0, v_atime=0;
1da177e4
LT
1292
1293 err = nfserr_perm;
1294 if (!flen)
1295 goto out;
1296 err = nfserr_exist;
1297 if (isdotent(fname, flen))
1298 goto out;
1299 if (!(iap->ia_valid & ATTR_MODE))
1300 iap->ia_mode = 0;
1574dff8 1301 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
1da177e4
LT
1302 if (err)
1303 goto out;
1304
1305 dentry = fhp->fh_dentry;
2b0143b5 1306 dirp = d_inode(dentry);
1da177e4 1307
4a55c101
JK
1308 host_err = fh_want_write(fhp);
1309 if (host_err)
1310 goto out_nfserr;
1311
12fd3520 1312 fh_lock_nested(fhp, I_MUTEX_PARENT);
1da177e4
LT
1313
1314 /*
1315 * Compose the response file handle.
1316 */
1317 dchild = lookup_one_len(fname, dentry, flen);
6264d69d 1318 host_err = PTR_ERR(dchild);
1da177e4
LT
1319 if (IS_ERR(dchild))
1320 goto out_nfserr;
1321
1574dff8 1322 /* If file doesn't exist, check for permissions to create one */
2b0143b5 1323 if (d_really_is_negative(dchild)) {
1574dff8
SP
1324 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1325 if (err)
1326 goto out;
1327 }
1328
1da177e4
LT
1329 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1330 if (err)
1331 goto out;
1332
ac6721a1 1333 if (nfsd_create_is_exclusive(createmode)) {
c397852c 1334 /* solaris7 gets confused (bugid 4218508) if these have
749997e5
JL
1335 * the high bit set, so just clear the high bits. If this is
1336 * ever changed to use different attrs for storing the
1337 * verifier, then do_open_lookup() will also need to be fixed
1338 * accordingly.
1da177e4
LT
1339 */
1340 v_mtime = verifier[0]&0x7fffffff;
1341 v_atime = verifier[1]&0x7fffffff;
1da177e4
LT
1342 }
1343
2b0143b5 1344 if (d_really_is_positive(dchild)) {
1da177e4
LT
1345 err = 0;
1346
1347 switch (createmode) {
1348 case NFS3_CREATE_UNCHECKED:
e36cb0b8 1349 if (! d_is_reg(dchild))
9dc4e6c4 1350 goto out;
1da177e4
LT
1351 else if (truncp) {
1352 /* in nfsv4, we need to treat this case a little
1353 * differently. we don't want to truncate the
1354 * file now; this would be wrong if the OPEN
1355 * fails for some other reason. furthermore,
1356 * if the size is nonzero, we should ignore it
1357 * according to spec!
1358 */
1359 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1360 }
1361 else {
1362 iap->ia_valid &= ATTR_SIZE;
1363 goto set_attr;
1364 }
1365 break;
1366 case NFS3_CREATE_EXCLUSIVE:
2b0143b5
DH
1367 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1368 && d_inode(dchild)->i_atime.tv_sec == v_atime
1369 && d_inode(dchild)->i_size == 0 ) {
7007c90f
NB
1370 if (created)
1371 *created = 1;
1da177e4 1372 break;
7007c90f 1373 }
ac6721a1 1374 case NFS4_CREATE_EXCLUSIVE4_1:
2b0143b5
DH
1375 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1376 && d_inode(dchild)->i_atime.tv_sec == v_atime
1377 && d_inode(dchild)->i_size == 0 ) {
7007c90f
NB
1378 if (created)
1379 *created = 1;
ac6721a1 1380 goto set_attr;
7007c90f 1381 }
1da177e4
LT
1382 /* fallthru */
1383 case NFS3_CREATE_GUARDED:
1384 err = nfserr_exist;
1385 }
bad0dcff 1386 fh_drop_write(fhp);
1da177e4
LT
1387 goto out;
1388 }
1389
312b63fb 1390 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
463c3197 1391 if (host_err < 0) {
bad0dcff 1392 fh_drop_write(fhp);
1da177e4 1393 goto out_nfserr;
463c3197 1394 }
81ac95c5
BF
1395 if (created)
1396 *created = 1;
1da177e4 1397
4ac35c2f 1398 nfsd_check_ignore_resizing(iap);
1399
ac6721a1 1400 if (nfsd_create_is_exclusive(createmode)) {
c397852c 1401 /* Cram the verifier into atime/mtime */
1da177e4 1402 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
c397852c 1403 | ATTR_MTIME_SET|ATTR_ATIME_SET;
1da177e4
LT
1404 /* XXX someone who knows this better please fix it for nsec */
1405 iap->ia_mtime.tv_sec = v_mtime;
1406 iap->ia_atime.tv_sec = v_atime;
1407 iap->ia_mtime.tv_nsec = 0;
1408 iap->ia_atime.tv_nsec = 0;
1da177e4
LT
1409 }
1410
1da177e4 1411 set_attr:
f501912a
BM
1412 err = nfsd_create_setattr(rqstp, resfhp, iap);
1413
1414 /*
0f3a24b4
TM
1415 * nfsd_create_setattr already committed the child
1416 * (and possibly also the parent).
f501912a
BM
1417 */
1418 if (!err)
1419 err = nfserrno(commit_metadata(fhp));
f193fbab
YT
1420
1421 /*
1422 * Update the filehandle to get the new inode info.
1423 */
1424 if (!err)
1425 err = fh_update(resfhp);
1da177e4
LT
1426
1427 out:
1428 fh_unlock(fhp);
1429 if (dchild && !IS_ERR(dchild))
1430 dput(dchild);
4a55c101 1431 fh_drop_write(fhp);
1da177e4
LT
1432 return err;
1433
1434 out_nfserr:
6264d69d 1435 err = nfserrno(host_err);
1da177e4
LT
1436 goto out;
1437}
1438#endif /* CONFIG_NFSD_V3 */
1439
1440/*
1441 * Read a symlink. On entry, *lenp must contain the maximum path length that
1442 * fits into the buffer. On return, it contains the true length.
1443 * N.B. After this call fhp needs an fh_put
1444 */
6264d69d 1445__be32
1da177e4
LT
1446nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1447{
1da177e4 1448 mm_segment_t oldfs;
6264d69d
AV
1449 __be32 err;
1450 int host_err;
68ac1234 1451 struct path path;
1da177e4 1452
8837abca 1453 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1da177e4
LT
1454 if (err)
1455 goto out;
1456
68ac1234
AV
1457 path.mnt = fhp->fh_export->ex_path.mnt;
1458 path.dentry = fhp->fh_dentry;
1da177e4
LT
1459
1460 err = nfserr_inval;
fd4a0edf 1461 if (!d_is_symlink(path.dentry))
1da177e4
LT
1462 goto out;
1463
68ac1234 1464 touch_atime(&path);
1da177e4
LT
1465 /* N.B. Why does this call need a get_fs()??
1466 * Remove the set_fs and watch the fireworks:-) --okir
1467 */
1468
1469 oldfs = get_fs(); set_fs(KERNEL_DS);
fd4a0edf 1470 host_err = vfs_readlink(path.dentry, (char __user *)buf, *lenp);
1da177e4
LT
1471 set_fs(oldfs);
1472
6264d69d 1473 if (host_err < 0)
1da177e4 1474 goto out_nfserr;
6264d69d 1475 *lenp = host_err;
1da177e4
LT
1476 err = 0;
1477out:
1478 return err;
1479
1480out_nfserr:
6264d69d 1481 err = nfserrno(host_err);
1da177e4
LT
1482 goto out;
1483}
1484
1485/*
1486 * Create a symlink and look up its inode
1487 * N.B. After this call _both_ fhp and resfhp need an fh_put
1488 */
6264d69d 1489__be32
1da177e4
LT
1490nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1491 char *fname, int flen,
52ee0433 1492 char *path,
1e444f5b 1493 struct svc_fh *resfhp)
1da177e4
LT
1494{
1495 struct dentry *dentry, *dnew;
6264d69d
AV
1496 __be32 err, cerr;
1497 int host_err;
1da177e4
LT
1498
1499 err = nfserr_noent;
52ee0433 1500 if (!flen || path[0] == '\0')
1da177e4
LT
1501 goto out;
1502 err = nfserr_exist;
1503 if (isdotent(fname, flen))
1504 goto out;
1505
8837abca 1506 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1da177e4
LT
1507 if (err)
1508 goto out;
4a55c101
JK
1509
1510 host_err = fh_want_write(fhp);
1511 if (host_err)
1512 goto out_nfserr;
1513
1da177e4
LT
1514 fh_lock(fhp);
1515 dentry = fhp->fh_dentry;
1516 dnew = lookup_one_len(fname, dentry, flen);
6264d69d 1517 host_err = PTR_ERR(dnew);
1da177e4
LT
1518 if (IS_ERR(dnew))
1519 goto out_nfserr;
1520
2b0143b5 1521 host_err = vfs_symlink(d_inode(dentry), dnew, path);
6264d69d 1522 err = nfserrno(host_err);
f501912a
BM
1523 if (!err)
1524 err = nfserrno(commit_metadata(fhp));
1da177e4
LT
1525 fh_unlock(fhp);
1526
bad0dcff 1527 fh_drop_write(fhp);
75c3f29d 1528
1da177e4
LT
1529 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1530 dput(dnew);
1531 if (err==0) err = cerr;
1532out:
1533 return err;
1534
1535out_nfserr:
6264d69d 1536 err = nfserrno(host_err);
1da177e4
LT
1537 goto out;
1538}
1539
1540/*
1541 * Create a hardlink
1542 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1543 */
6264d69d 1544__be32
1da177e4
LT
1545nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1546 char *name, int len, struct svc_fh *tfhp)
1547{
1548 struct dentry *ddir, *dnew, *dold;
55b13354 1549 struct inode *dirp;
6264d69d
AV
1550 __be32 err;
1551 int host_err;
1da177e4 1552
8837abca 1553 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1da177e4
LT
1554 if (err)
1555 goto out;
7d818a7b 1556 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
1da177e4
LT
1557 if (err)
1558 goto out;
7d818a7b 1559 err = nfserr_isdir;
e36cb0b8 1560 if (d_is_dir(tfhp->fh_dentry))
7d818a7b 1561 goto out;
1da177e4
LT
1562 err = nfserr_perm;
1563 if (!len)
1564 goto out;
1565 err = nfserr_exist;
1566 if (isdotent(name, len))
1567 goto out;
1568
4a55c101
JK
1569 host_err = fh_want_write(tfhp);
1570 if (host_err) {
1571 err = nfserrno(host_err);
1572 goto out;
1573 }
1574
12fd3520 1575 fh_lock_nested(ffhp, I_MUTEX_PARENT);
1da177e4 1576 ddir = ffhp->fh_dentry;
2b0143b5 1577 dirp = d_inode(ddir);
1da177e4
LT
1578
1579 dnew = lookup_one_len(name, ddir, len);
6264d69d 1580 host_err = PTR_ERR(dnew);
1da177e4
LT
1581 if (IS_ERR(dnew))
1582 goto out_nfserr;
1583
1584 dold = tfhp->fh_dentry;
1da177e4 1585
4795bb37 1586 err = nfserr_noent;
2b0143b5 1587 if (d_really_is_negative(dold))
4a55c101 1588 goto out_dput;
146a8595 1589 host_err = vfs_link(dold, dirp, dnew, NULL);
6264d69d 1590 if (!host_err) {
f501912a
BM
1591 err = nfserrno(commit_metadata(ffhp));
1592 if (!err)
1593 err = nfserrno(commit_metadata(tfhp));
1da177e4 1594 } else {
6264d69d 1595 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1da177e4
LT
1596 err = nfserr_acces;
1597 else
6264d69d 1598 err = nfserrno(host_err);
1da177e4 1599 }
75c3f29d 1600out_dput:
1da177e4 1601 dput(dnew);
270d56e5
DR
1602out_unlock:
1603 fh_unlock(ffhp);
4a55c101 1604 fh_drop_write(tfhp);
1da177e4
LT
1605out:
1606 return err;
1607
1608out_nfserr:
6264d69d 1609 err = nfserrno(host_err);
270d56e5 1610 goto out_unlock;
1da177e4
LT
1611}
1612
1613/*
1614 * Rename a file
1615 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1616 */
6264d69d 1617__be32
1da177e4
LT
1618nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1619 struct svc_fh *tfhp, char *tname, int tlen)
1620{
1621 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1622 struct inode *fdir, *tdir;
6264d69d
AV
1623 __be32 err;
1624 int host_err;
1da177e4 1625
8837abca 1626 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1da177e4
LT
1627 if (err)
1628 goto out;
8837abca 1629 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1da177e4
LT
1630 if (err)
1631 goto out;
1632
1633 fdentry = ffhp->fh_dentry;
2b0143b5 1634 fdir = d_inode(fdentry);
1da177e4
LT
1635
1636 tdentry = tfhp->fh_dentry;
2b0143b5 1637 tdir = d_inode(tdentry);
1da177e4 1638
1da177e4
LT
1639 err = nfserr_perm;
1640 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1641 goto out;
1642
4a55c101
JK
1643 host_err = fh_want_write(ffhp);
1644 if (host_err) {
1645 err = nfserrno(host_err);
1646 goto out;
1647 }
1648
1da177e4
LT
1649 /* cannot use fh_lock as we need deadlock protective ordering
1650 * so do it by hand */
1651 trap = lock_rename(tdentry, fdentry);
aaf91ec1 1652 ffhp->fh_locked = tfhp->fh_locked = true;
1da177e4
LT
1653 fill_pre_wcc(ffhp);
1654 fill_pre_wcc(tfhp);
1655
1656 odentry = lookup_one_len(fname, fdentry, flen);
6264d69d 1657 host_err = PTR_ERR(odentry);
1da177e4
LT
1658 if (IS_ERR(odentry))
1659 goto out_nfserr;
1660
6264d69d 1661 host_err = -ENOENT;
2b0143b5 1662 if (d_really_is_negative(odentry))
1da177e4 1663 goto out_dput_old;
6264d69d 1664 host_err = -EINVAL;
1da177e4
LT
1665 if (odentry == trap)
1666 goto out_dput_old;
1667
1668 ndentry = lookup_one_len(tname, tdentry, tlen);
6264d69d 1669 host_err = PTR_ERR(ndentry);
1da177e4
LT
1670 if (IS_ERR(ndentry))
1671 goto out_dput_old;
6264d69d 1672 host_err = -ENOTEMPTY;
1da177e4
LT
1673 if (ndentry == trap)
1674 goto out_dput_new;
1675
9079b1eb
DH
1676 host_err = -EXDEV;
1677 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1678 goto out_dput_new;
aa387d6c
BF
1679 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1680 goto out_dput_new;
9079b1eb 1681
520c8b16 1682 host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
f501912a
BM
1683 if (!host_err) {
1684 host_err = commit_metadata(tfhp);
6264d69d 1685 if (!host_err)
f501912a 1686 host_err = commit_metadata(ffhp);
1da177e4 1687 }
1da177e4
LT
1688 out_dput_new:
1689 dput(ndentry);
1690 out_dput_old:
1691 dput(odentry);
1692 out_nfserr:
6264d69d 1693 err = nfserrno(host_err);
fbb74a34
BF
1694 /*
1695 * We cannot rely on fh_unlock on the two filehandles,
1da177e4 1696 * as that would do the wrong thing if the two directories
fbb74a34 1697 * were the same, so again we do it by hand.
1da177e4
LT
1698 */
1699 fill_post_wcc(ffhp);
1700 fill_post_wcc(tfhp);
1701 unlock_rename(tdentry, fdentry);
aaf91ec1 1702 ffhp->fh_locked = tfhp->fh_locked = false;
4a55c101 1703 fh_drop_write(ffhp);
1da177e4
LT
1704
1705out:
1706 return err;
1707}
1708
1709/*
1710 * Unlink a file or directory
1711 * N.B. After this call fhp needs an fh_put
1712 */
6264d69d 1713__be32
1da177e4
LT
1714nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1715 char *fname, int flen)
1716{
1717 struct dentry *dentry, *rdentry;
1718 struct inode *dirp;
6264d69d
AV
1719 __be32 err;
1720 int host_err;
1da177e4
LT
1721
1722 err = nfserr_acces;
1723 if (!flen || isdotent(fname, flen))
1724 goto out;
8837abca 1725 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1da177e4
LT
1726 if (err)
1727 goto out;
1728
4a55c101
JK
1729 host_err = fh_want_write(fhp);
1730 if (host_err)
1731 goto out_nfserr;
1732
12fd3520 1733 fh_lock_nested(fhp, I_MUTEX_PARENT);
1da177e4 1734 dentry = fhp->fh_dentry;
2b0143b5 1735 dirp = d_inode(dentry);
1da177e4
LT
1736
1737 rdentry = lookup_one_len(fname, dentry, flen);
6264d69d 1738 host_err = PTR_ERR(rdentry);
1da177e4
LT
1739 if (IS_ERR(rdentry))
1740 goto out_nfserr;
1741
2b0143b5 1742 if (d_really_is_negative(rdentry)) {
1da177e4
LT
1743 dput(rdentry);
1744 err = nfserr_noent;
1745 goto out;
1746 }
1747
1748 if (!type)
2b0143b5 1749 type = d_inode(rdentry)->i_mode & S_IFMT;
1da177e4 1750
9ce137ee 1751 if (type != S_IFDIR)
b21996e3 1752 host_err = vfs_unlink(dirp, rdentry, NULL);
9ce137ee 1753 else
6264d69d 1754 host_err = vfs_rmdir(dirp, rdentry);
f501912a
BM
1755 if (!host_err)
1756 host_err = commit_metadata(fhp);
541ce98c
BF
1757 dput(rdentry);
1758
1da177e4 1759out_nfserr:
6264d69d 1760 err = nfserrno(host_err);
f193fbab
YT
1761out:
1762 return err;
1da177e4
LT
1763}
1764
14f7dd63
DW
1765/*
1766 * We do this buffering because we must not call back into the file
1767 * system's ->lookup() method from the filldir callback. That may well
1768 * deadlock a number of file systems.
1769 *
1770 * This is based heavily on the implementation of same in XFS.
1771 */
1772struct buffered_dirent {
1773 u64 ino;
1774 loff_t offset;
1775 int namlen;
1776 unsigned int d_type;
1777 char name[];
1778};
1779
1780struct readdir_data {
5c0ba4e0 1781 struct dir_context ctx;
14f7dd63
DW
1782 char *dirent;
1783 size_t used;
53c9c5c0 1784 int full;
14f7dd63
DW
1785};
1786
ac7576f4
MS
1787static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1788 int namlen, loff_t offset, u64 ino,
1789 unsigned int d_type)
14f7dd63 1790{
ac7576f4
MS
1791 struct readdir_data *buf =
1792 container_of(ctx, struct readdir_data, ctx);
14f7dd63
DW
1793 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1794 unsigned int reclen;
1795
1796 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
53c9c5c0
AV
1797 if (buf->used + reclen > PAGE_SIZE) {
1798 buf->full = 1;
14f7dd63 1799 return -EINVAL;
53c9c5c0 1800 }
14f7dd63
DW
1801
1802 de->namlen = namlen;
1803 de->offset = offset;
1804 de->ino = ino;
1805 de->d_type = d_type;
1806 memcpy(de->name, name, namlen);
1807 buf->used += reclen;
1808
1809 return 0;
1810}
1811
ac7576f4 1812static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
2f9092e1 1813 struct readdir_cd *cdp, loff_t *offsetp)
2628b766 1814{
14f7dd63 1815 struct buffered_dirent *de;
2628b766 1816 int host_err;
14f7dd63
DW
1817 int size;
1818 loff_t offset;
ac6614b7
AV
1819 struct readdir_data buf = {
1820 .ctx.actor = nfsd_buffered_filldir,
1821 .dirent = (void *)__get_free_page(GFP_KERNEL)
1822 };
2628b766 1823
14f7dd63 1824 if (!buf.dirent)
2f9092e1 1825 return nfserrno(-ENOMEM);
14f7dd63
DW
1826
1827 offset = *offsetp;
2628b766 1828
14f7dd63
DW
1829 while (1) {
1830 unsigned int reclen;
1831
b726e923 1832 cdp->err = nfserr_eof; /* will be cleared on successful read */
14f7dd63 1833 buf.used = 0;
53c9c5c0 1834 buf.full = 0;
14f7dd63 1835
5c0ba4e0 1836 host_err = iterate_dir(file, &buf.ctx);
53c9c5c0
AV
1837 if (buf.full)
1838 host_err = 0;
1839
1840 if (host_err < 0)
14f7dd63
DW
1841 break;
1842
1843 size = buf.used;
1844
1845 if (!size)
1846 break;
1847
14f7dd63
DW
1848 de = (struct buffered_dirent *)buf.dirent;
1849 while (size > 0) {
1850 offset = de->offset;
1851
1852 if (func(cdp, de->name, de->namlen, de->offset,
1853 de->ino, de->d_type))
2f9092e1 1854 break;
14f7dd63
DW
1855
1856 if (cdp->err != nfs_ok)
2f9092e1 1857 break;
14f7dd63
DW
1858
1859 reclen = ALIGN(sizeof(*de) + de->namlen,
1860 sizeof(u64));
1861 size -= reclen;
1862 de = (struct buffered_dirent *)((char *)de + reclen);
1863 }
2f9092e1
DW
1864 if (size > 0) /* We bailed out early */
1865 break;
1866
c002a6c7 1867 offset = vfs_llseek(file, 0, SEEK_CUR);
14f7dd63
DW
1868 }
1869
14f7dd63 1870 free_page((unsigned long)(buf.dirent));
2628b766
DW
1871
1872 if (host_err)
1873 return nfserrno(host_err);
14f7dd63
DW
1874
1875 *offsetp = offset;
1876 return cdp->err;
2628b766
DW
1877}
1878
1da177e4
LT
1879/*
1880 * Read entries from a directory.
1881 * The NFSv3/4 verifier we ignore for now.
1882 */
6264d69d 1883__be32
1da177e4 1884nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
ac7576f4 1885 struct readdir_cd *cdp, nfsd_filldir_t func)
1da177e4 1886{
6264d69d 1887 __be32 err;
1da177e4
LT
1888 struct file *file;
1889 loff_t offset = *offsetp;
06effdbb
BS
1890 int may_flags = NFSD_MAY_READ;
1891
1892 /* NFSv2 only supports 32 bit cookies */
1893 if (rqstp->rq_vers > 2)
1894 may_flags |= NFSD_MAY_64BIT_COOKIE;
1da177e4 1895
06effdbb 1896 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
1da177e4
LT
1897 if (err)
1898 goto out;
1899
b108fe6b 1900 offset = vfs_llseek(file, offset, SEEK_SET);
1da177e4
LT
1901 if (offset < 0) {
1902 err = nfserrno((int)offset);
1903 goto out_close;
1904 }
1905
14f7dd63 1906 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
1da177e4
LT
1907
1908 if (err == nfserr_eof || err == nfserr_toosmall)
1909 err = nfs_ok; /* can still be found in ->err */
1910out_close:
fd891454 1911 fput(file);
1da177e4
LT
1912out:
1913 return err;
1914}
1915
1916/*
1917 * Get file system stats
1918 * N.B. After this call fhp needs an fh_put
1919 */
6264d69d 1920__be32
04716e66 1921nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
1da177e4 1922{
ebabe9a9
CH
1923 __be32 err;
1924
1925 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
f6360efb
TI
1926 if (!err) {
1927 struct path path = {
1928 .mnt = fhp->fh_export->ex_path.mnt,
1929 .dentry = fhp->fh_dentry,
1930 };
1931 if (vfs_statfs(&path, stat))
1932 err = nfserr_io;
1933 }
1da177e4
LT
1934 return err;
1935}
1936
c7d51402 1937static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
e22841c6 1938{
c7d51402 1939 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
e22841c6
BF
1940}
1941
1da177e4
LT
1942/*
1943 * Check for a user's access permissions to this inode.
1944 */
6264d69d 1945__be32
0ec757df
BF
1946nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
1947 struct dentry *dentry, int acc)
1da177e4 1948{
2b0143b5 1949 struct inode *inode = d_inode(dentry);
1da177e4
LT
1950 int err;
1951
aea93397 1952 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
1da177e4
LT
1953 return 0;
1954#if 0
1955 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1956 acc,
8837abca
MS
1957 (acc & NFSD_MAY_READ)? " read" : "",
1958 (acc & NFSD_MAY_WRITE)? " write" : "",
1959 (acc & NFSD_MAY_EXEC)? " exec" : "",
1960 (acc & NFSD_MAY_SATTR)? " sattr" : "",
1961 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
1962 (acc & NFSD_MAY_LOCK)? " lock" : "",
1963 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
1da177e4
LT
1964 inode->i_mode,
1965 IS_IMMUTABLE(inode)? " immut" : "",
1966 IS_APPEND(inode)? " append" : "",
2c463e95 1967 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
1da177e4 1968 dprintk(" owner %d/%d user %d/%d\n",
5cc0a840 1969 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
1da177e4
LT
1970#endif
1971
1972 /* Normally we reject any write/sattr etc access on a read-only file
1973 * system. But if it is IRIX doing check on write-access for a
1974 * device special file, we ignore rofs.
1975 */
8837abca
MS
1976 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
1977 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2c463e95
DH
1978 if (exp_rdonly(rqstp, exp) ||
1979 __mnt_is_readonly(exp->ex_path.mnt))
1da177e4 1980 return nfserr_rofs;
8837abca 1981 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
1da177e4
LT
1982 return nfserr_perm;
1983 }
8837abca 1984 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
1da177e4
LT
1985 return nfserr_perm;
1986
8837abca 1987 if (acc & NFSD_MAY_LOCK) {
1da177e4
LT
1988 /* If we cannot rely on authentication in NLM requests,
1989 * just allow locks, otherwise require read permission, or
1990 * ownership
1991 */
1992 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1993 return 0;
1994 else
8837abca 1995 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
1da177e4
LT
1996 }
1997 /*
1998 * The file owner always gets access permission for accesses that
1999 * would normally be checked at open time. This is to make
2000 * file access work even when the client has done a fchmod(fd, 0).
2001 *
2002 * However, `cp foo bar' should fail nevertheless when bar is
2003 * readonly. A sensible way to do this might be to reject all
2004 * attempts to truncate a read-only file, because a creat() call
2005 * always implies file truncation.
2006 * ... but this isn't really fair. A process may reasonably call
2007 * ftruncate on an open file descriptor on a file with perm 000.
2008 * We must trust the client to do permission checking - using "ACCESS"
2009 * with NFSv3.
2010 */
8837abca 2011 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
6fab8779 2012 uid_eq(inode->i_uid, current_fsuid()))
1da177e4
LT
2013 return 0;
2014
8837abca 2015 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
f419a2e3 2016 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
1da177e4
LT
2017
2018 /* Allow read access to binaries even when mode 111 */
2019 if (err == -EACCES && S_ISREG(inode->i_mode) &&
a043226b
BF
2020 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2021 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
f419a2e3 2022 err = inode_permission(inode, MAY_EXEC);
1da177e4
LT
2023
2024 return err? nfserrno(err) : 0;
2025}
2026
2027void
2028nfsd_racache_shutdown(void)
2029{
54a66e54
JL
2030 struct raparms *raparm, *last_raparm;
2031 unsigned int i;
2032
1da177e4 2033 dprintk("nfsd: freeing readahead buffers.\n");
54a66e54
JL
2034
2035 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2036 raparm = raparm_hash[i].pb_head;
2037 while(raparm) {
2038 last_raparm = raparm;
2039 raparm = raparm->p_next;
2040 kfree(last_raparm);
2041 }
2042 raparm_hash[i].pb_head = NULL;
2043 }
1da177e4
LT
2044}
2045/*
2046 * Initialize readahead param cache
2047 */
2048int
2049nfsd_racache_init(int cache_size)
2050{
2051 int i;
fce1456a
GB
2052 int j = 0;
2053 int nperbucket;
54a66e54 2054 struct raparms **raparm = NULL;
1da177e4 2055
fce1456a 2056
54a66e54 2057 if (raparm_hash[0].pb_head)
1da177e4 2058 return 0;
54a66e54 2059 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
3c7aa15d 2060 nperbucket = max(2, nperbucket);
54a66e54 2061 cache_size = nperbucket * RAPARM_HASH_SIZE;
4b3bb06b
YB
2062
2063 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
54a66e54
JL
2064
2065 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
4b3bb06b 2066 spin_lock_init(&raparm_hash[i].pb_lock);
54a66e54
JL
2067
2068 raparm = &raparm_hash[i].pb_head;
2069 for (j = 0; j < nperbucket; j++) {
2070 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2071 if (!*raparm)
2072 goto out_nomem;
2073 raparm = &(*raparm)->p_next;
2074 }
2075 *raparm = NULL;
4b3bb06b
YB
2076 }
2077
1da177e4
LT
2078 nfsdstats.ra_size = cache_size;
2079 return 0;
54a66e54
JL
2080
2081out_nomem:
2082 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2083 nfsd_racache_shutdown();
2084 return -ENOMEM;
1da177e4 2085}