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