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