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