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