VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   33 Branches   55 Tags
b24413180f560 (Greg Kroah-Hartman     2017-11-01 15:07:57 +0100    1) // SPDX-License-Identifier: GPL-2.0
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700    2) /*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700    3)  * File operations used by nfsd. Some of these have been ripped from
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700    4)  * other parts of the kernel because they weren't exported, others
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700    5)  * are partial duplicates with added or changed functionality.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700    6)  *
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700    7)  * Note that several functions dget() the dentry upon which they want
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700    8)  * to act, most notably those that create directory entries. Response
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700    9)  * dentry's are dput()'d if necessary in the release callback.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   10)  * So if you notice code paths that apparently fail to dput() the
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   11)  * dentry, don't worry--they have been taken care of.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   12)  *
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   13)  * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   14)  * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   15)  */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   16) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   17) #include <linux/fs.h>
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   18) #include <linux/file.h>
d6b29d7cee064 (Jens Axboe             2007-06-04 09:59:47 +0200   19) #include <linux/splice.h>
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500   20) #include <linux/falloc.h>
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   21) #include <linux/fcntl.h>
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   22) #include <linux/namei.h>
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   23) #include <linux/delay.h>
0eeca28300df1 (Robert Love            2005-07-12 17:06:03 -0400   24) #include <linux/fsnotify.h>
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   25) #include <linux/posix_acl_xattr.h>
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   26) #include <linux/xattr.h>
9a74af21330c8 (Boaz Harrosh           2009-12-03 20:30:56 +0200   27) #include <linux/jhash.h>
9a74af21330c8 (Boaz Harrosh           2009-12-03 20:30:56 +0200   28) #include <linux/ima.h>
5a0e3ad6af866 (Tejun Heo              2010-03-24 17:04:11 +0900   29) #include <linux/slab.h>
7c0f6ba682b9c (Linus Torvalds         2016-12-24 11:46:01 -0800   30) #include <linux/uaccess.h>
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600   31) #include <linux/exportfs.h>
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600   32) #include <linux/writeback.h>
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400   33) #include <linux/security.h>
9a74af21330c8 (Boaz Harrosh           2009-12-03 20:30:56 +0200   34) 
9a74af21330c8 (Boaz Harrosh           2009-12-03 20:30:56 +0200   35) #ifdef CONFIG_NFSD_V3
9a74af21330c8 (Boaz Harrosh           2009-12-03 20:30:56 +0200   36) #include "xdr3.h"
9a74af21330c8 (Boaz Harrosh           2009-12-03 20:30:56 +0200   37) #endif /* CONFIG_NFSD_V3 */
9a74af21330c8 (Boaz Harrosh           2009-12-03 20:30:56 +0200   38) 
5be196e5f925d (Christoph Hellwig      2006-01-09 20:51:55 -0800   39) #ifdef CONFIG_NFSD_V4
ffa0160a10391 (Christoph Hellwig      2015-12-03 12:59:52 +0100   40) #include "../internal.h"
2ca72e17e5acb (J. Bruce Fields        2011-01-04 17:37:15 -0500   41) #include "acl.h"
2ca72e17e5acb (J. Bruce Fields        2011-01-04 17:37:15 -0500   42) #include "idmap.h"
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   43) #endif /* CONFIG_NFSD_V4 */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   44) 
9a74af21330c8 (Boaz Harrosh           2009-12-03 20:30:56 +0200   45) #include "nfsd.h"
9a74af21330c8 (Boaz Harrosh           2009-12-03 20:30:56 +0200   46) #include "vfs.h"
b493523926f9b (Jeff Layton            2019-08-18 14:18:49 -0400   47) #include "filecache.h"
6e8b50d16a757 (Jeff Layton            2015-11-17 06:52:23 -0500   48) #include "trace.h"
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   49) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   50) #define NFSDDBG_FACILITY		NFSDDBG_FILEOP
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   51) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   52) /* 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   53)  * Called from nfsd_lookup and encode_dirent. Check if we have crossed 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   54)  * a mount point.
e0bb89ef031f7 (J.Bruce Fields         2006-12-13 00:35:25 -0800   55)  * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   56)  *  or nfs_ok having possibly changed *dpp and *expp
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   57)  */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   58) int
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   59) nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp, 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   60) 		        struct svc_export **expp)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   61) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   62) 	struct svc_export *exp = *expp, *exp2 = NULL;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   63) 	struct dentry *dentry = *dpp;
91c9fa8f75877 (Al Viro                2009-04-18 02:42:05 -0400   64) 	struct path path = {.mnt = mntget(exp->ex_path.mnt),
91c9fa8f75877 (Al Viro                2009-04-18 02:42:05 -0400   65) 			    .dentry = dget(dentry)};
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700   66) 	int err = 0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   67) 
7cc90cc3ffe22 (Al Viro                2011-03-18 09:04:20 -0400   68) 	err = follow_down(&path);
cc53ce53c8692 (David Howells          2011-01-14 18:45:26 +0000   69) 	if (err < 0)
cc53ce53c8692 (David Howells          2011-01-14 18:45:26 +0000   70) 		goto out;
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100   71) 	if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100   72) 	    nfsd_mountpoint(dentry, exp) == 2) {
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100   73) 		/* This is only a mountpoint in some other namespace */
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100   74) 		path_put(&path);
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100   75) 		goto out;
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100   76) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   77) 
91c9fa8f75877 (Al Viro                2009-04-18 02:42:05 -0400   78) 	exp2 = rqst_exp_get_by_name(rqstp, &path);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   79) 	if (IS_ERR(exp2)) {
3b6cee7bc4b2f (J. Bruce Fields        2009-10-25 21:18:19 -0400   80) 		err = PTR_ERR(exp2);
3b6cee7bc4b2f (J. Bruce Fields        2009-10-25 21:18:19 -0400   81) 		/*
3b6cee7bc4b2f (J. Bruce Fields        2009-10-25 21:18:19 -0400   82) 		 * We normally allow NFS clients to continue
3b6cee7bc4b2f (J. Bruce Fields        2009-10-25 21:18:19 -0400   83) 		 * "underneath" a mountpoint that is not exported.
3b6cee7bc4b2f (J. Bruce Fields        2009-10-25 21:18:19 -0400   84) 		 * The exception is V4ROOT, where no traversal is ever
3b6cee7bc4b2f (J. Bruce Fields        2009-10-25 21:18:19 -0400   85) 		 * allowed without an explicit export of the new
3b6cee7bc4b2f (J. Bruce Fields        2009-10-25 21:18:19 -0400   86) 		 * directory.
3b6cee7bc4b2f (J. Bruce Fields        2009-10-25 21:18:19 -0400   87) 		 */
3b6cee7bc4b2f (J. Bruce Fields        2009-10-25 21:18:19 -0400   88) 		if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
3b6cee7bc4b2f (J. Bruce Fields        2009-10-25 21:18:19 -0400   89) 			err = 0;
91c9fa8f75877 (Al Viro                2009-04-18 02:42:05 -0400   90) 		path_put(&path);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   91) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   92) 	}
3c394ddaa7ea4 (Steve Dickson          2009-09-09 15:02:40 -0400   93) 	if (nfsd_v4client(rqstp) ||
3c394ddaa7ea4 (Steve Dickson          2009-09-09 15:02:40 -0400   94) 		(exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700   95) 		/* successfully crossed mount point */
1644ccc8a99ae (Al Viro                2009-04-18 02:32:31 -0400   96) 		/*
91c9fa8f75877 (Al Viro                2009-04-18 02:42:05 -0400   97) 		 * This is subtle: path.dentry is *not* on path.mnt
91c9fa8f75877 (Al Viro                2009-04-18 02:42:05 -0400   98) 		 * at this point.  The only reason we are safe is that
91c9fa8f75877 (Al Viro                2009-04-18 02:42:05 -0400   99) 		 * original mnt is pinned down by exp, so we should
91c9fa8f75877 (Al Viro                2009-04-18 02:42:05 -0400  100) 		 * put path *before* putting exp
1644ccc8a99ae (Al Viro                2009-04-18 02:32:31 -0400  101) 		 */
91c9fa8f75877 (Al Viro                2009-04-18 02:42:05 -0400  102) 		*dpp = path.dentry;
91c9fa8f75877 (Al Viro                2009-04-18 02:42:05 -0400  103) 		path.dentry = dentry;
1644ccc8a99ae (Al Viro                2009-04-18 02:32:31 -0400  104) 		*expp = exp2;
91c9fa8f75877 (Al Viro                2009-04-18 02:42:05 -0400  105) 		exp2 = exp;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  106) 	}
91c9fa8f75877 (Al Viro                2009-04-18 02:42:05 -0400  107) 	path_put(&path);
91c9fa8f75877 (Al Viro                2009-04-18 02:42:05 -0400  108) 	exp_put(exp2);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  109) out:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  110) 	return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  111) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  112) 
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  113) static void follow_to_parent(struct path *path)
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  114) {
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  115) 	struct dentry *dp;
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  116) 
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  117) 	while (path->dentry == path->mnt->mnt_root && follow_up(path))
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  118) 		;
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  119) 	dp = dget_parent(path->dentry);
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  120) 	dput(path->dentry);
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  121) 	path->dentry = dp;
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  122) }
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  123) 
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  124) static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  125) {
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  126) 	struct svc_export *exp2;
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  127) 	struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  128) 			    .dentry = dget(dparent)};
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  129) 
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  130) 	follow_to_parent(&path);
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  131) 
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  132) 	exp2 = rqst_exp_parent(rqstp, &path);
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  133) 	if (PTR_ERR(exp2) == -ENOENT) {
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  134) 		*dentryp = dget(dparent);
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  135) 	} else if (IS_ERR(exp2)) {
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  136) 		path_put(&path);
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  137) 		return PTR_ERR(exp2);
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  138) 	} else {
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  139) 		*dentryp = dget(path.dentry);
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  140) 		exp_put(*exp);
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  141) 		*exp = exp2;
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  142) 	}
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  143) 	path_put(&path);
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  144) 	return 0;
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  145) }
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  146) 
82ead7fe41da9 (J. Bruce Fields        2009-10-25 21:33:15 -0400  147) /*
82ead7fe41da9 (J. Bruce Fields        2009-10-25 21:33:15 -0400  148)  * For nfsd purposes, we treat V4ROOT exports as though there was an
82ead7fe41da9 (J. Bruce Fields        2009-10-25 21:33:15 -0400  149)  * export at *every* directory.
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  150)  * We return:
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  151)  * '1' if this dentry *must* be an export point,
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  152)  * '2' if it might be, if there is really a mount here, and
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  153)  * '0' if there is no chance of an export point here.
82ead7fe41da9 (J. Bruce Fields        2009-10-25 21:33:15 -0400  154)  */
3227fa41abc19 (J. Bruce Fields        2009-10-25 21:43:01 -0400  155) int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
82ead7fe41da9 (J. Bruce Fields        2009-10-25 21:33:15 -0400  156) {
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  157) 	if (!d_inode(dentry))
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  158) 		return 0;
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  159) 	if (exp->ex_flags & NFSEXP_V4ROOT)
82ead7fe41da9 (J. Bruce Fields        2009-10-25 21:33:15 -0400  160) 		return 1;
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  161) 	if (nfsd4_is_junction(dentry))
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  162) 		return 1;
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  163) 	if (d_mountpoint(dentry))
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  164) 		/*
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  165) 		 * Might only be a mountpoint in a different namespace,
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  166) 		 * but we need to check.
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  167) 		 */
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  168) 		return 2;
99bbf6ecc694d (NeilBrown              2017-03-15 12:40:44 +1100  169) 	return 0;
82ead7fe41da9 (J. Bruce Fields        2009-10-25 21:33:15 -0400  170) }
82ead7fe41da9 (J. Bruce Fields        2009-10-25 21:33:15 -0400  171) 
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  172) __be32
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  173) nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
5a022fc8700ca (Chuck Lever            2007-11-01 16:57:09 -0400  174) 		   const char *name, unsigned int len,
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  175) 		   struct svc_export **exp_ret, struct dentry **dentry_ret)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  176) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  177) 	struct svc_export	*exp;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  178) 	struct dentry		*dparent;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  179) 	struct dentry		*dentry;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  180) 	int			host_err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  181) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  182) 	dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  183) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  184) 	dparent = fhp->fh_dentry;
bf18f163e89c5 (Kinglong Mee           2014-06-10 22:06:44 +0800  185) 	exp = exp_get(fhp->fh_export);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  186) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  187) 	/* Lookup the name, but don't follow links */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  188) 	if (isdotent(name, len)) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  189) 		if (len==1)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  190) 			dentry = dget(dparent);
5477549161480 (Jan Blunck             2008-02-14 19:38:39 -0800  191) 		else if (dparent != exp->ex_path.dentry)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  192) 			dentry = dget_parent(dparent);
fed83811269d0 (J. Bruce Fields        2009-09-26 16:53:01 -0400  193) 		else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  194) 			dentry = dget(dparent); /* .. == . just like at / */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  195) 		else {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  196) 			/* checking mountpoint crossing is very different when stepping up */
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  197) 			host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
289ede453e5a6 (J. Bruce Fields        2009-09-26 20:32:24 -0400  198) 			if (host_err)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  199) 				goto out_nfserr;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  200) 		}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  201) 	} else {
4335723e8e9fd (J. Bruce Fields        2014-01-24 18:04:40 -0500  202) 		/*
4335723e8e9fd (J. Bruce Fields        2014-01-24 18:04:40 -0500  203) 		 * In the nfsd4_open() case, this may be held across
4335723e8e9fd (J. Bruce Fields        2014-01-24 18:04:40 -0500  204) 		 * subsequent open and delegation acquisition which may
4335723e8e9fd (J. Bruce Fields        2014-01-24 18:04:40 -0500  205) 		 * need to take the child's i_mutex:
4335723e8e9fd (J. Bruce Fields        2014-01-24 18:04:40 -0500  206) 		 */
4335723e8e9fd (J. Bruce Fields        2014-01-24 18:04:40 -0500  207) 		fh_lock_nested(fhp, I_MUTEX_PARENT);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  208) 		dentry = lookup_one_len(name, dparent, len);
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  209) 		host_err = PTR_ERR(dentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  210) 		if (IS_ERR(dentry))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  211) 			goto out_nfserr;
82ead7fe41da9 (J. Bruce Fields        2009-10-25 21:33:15 -0400  212) 		if (nfsd_mountpoint(dentry, exp)) {
bbddca8e8fac0 (NeilBrown              2016-01-07 16:08:20 -0500  213) 			/*
bbddca8e8fac0 (NeilBrown              2016-01-07 16:08:20 -0500  214) 			 * We don't need the i_mutex after all.  It's
bbddca8e8fac0 (NeilBrown              2016-01-07 16:08:20 -0500  215) 			 * still possible we could open this (regular
bbddca8e8fac0 (NeilBrown              2016-01-07 16:08:20 -0500  216) 			 * files can be mountpoints too), but the
bbddca8e8fac0 (NeilBrown              2016-01-07 16:08:20 -0500  217) 			 * i_mutex is just there to prevent renames of
bbddca8e8fac0 (NeilBrown              2016-01-07 16:08:20 -0500  218) 			 * something that we might be about to delegate,
bbddca8e8fac0 (NeilBrown              2016-01-07 16:08:20 -0500  219) 			 * and a mountpoint won't be renamed:
bbddca8e8fac0 (NeilBrown              2016-01-07 16:08:20 -0500  220) 			 */
bbddca8e8fac0 (NeilBrown              2016-01-07 16:08:20 -0500  221) 			fh_unlock(fhp);
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  222) 			if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  223) 				dput(dentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  224) 				goto out_nfserr;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  225) 			}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  226) 		}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  227) 	}
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  228) 	*dentry_ret = dentry;
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  229) 	*exp_ret = exp;
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  230) 	return 0;
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  231) 
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  232) out_nfserr:
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  233) 	exp_put(exp);
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  234) 	return nfserrno(host_err);
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  235) }
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  236) 
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  237) /*
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  238)  * Look up one component of a pathname.
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  239)  * N.B. After this call _both_ fhp and resfh need an fh_put
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  240)  *
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  241)  * If the lookup would cross a mountpoint, and the mounted filesystem
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  242)  * is exported to the client with NFSEXP_NOHIDE, then the lookup is
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  243)  * accepted as it stands and the mounted directory is
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  244)  * returned. Otherwise the covered directory is returned.
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  245)  * NOTE: this mountpoint crossing is not supported properly by all
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  246)  *   clients and is explicitly disallowed for NFSv3
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  247)  *      NeilBrown <neilb@cse.unsw.edu.au>
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  248)  */
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  249) __be32
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  250) nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
5a022fc8700ca (Chuck Lever            2007-11-01 16:57:09 -0400  251) 				unsigned int len, struct svc_fh *resfh)
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  252) {
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  253) 	struct svc_export	*exp;
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  254) 	struct dentry		*dentry;
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  255) 	__be32 err;
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  256) 
29a78a3ed7fc9 (J. Bruce Fields        2011-04-09 11:28:53 -0400  257) 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
29a78a3ed7fc9 (J. Bruce Fields        2011-04-09 11:28:53 -0400  258) 	if (err)
29a78a3ed7fc9 (J. Bruce Fields        2011-04-09 11:28:53 -0400  259) 		return err;
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  260) 	err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  261) 	if (err)
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  262) 		return err;
32c1eb0cd7ee0 (Andy Adamson           2007-07-17 04:04:48 -0700  263) 	err = check_nfsd_access(exp, rqstp);
32c1eb0cd7ee0 (Andy Adamson           2007-07-17 04:04:48 -0700  264) 	if (err)
32c1eb0cd7ee0 (Andy Adamson           2007-07-17 04:04:48 -0700  265) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  266) 	/*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  267) 	 * Note: we compose the file handle now, but as the
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  268) 	 * dentry may be negative, it may need to be updated.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  269) 	 */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  270) 	err = fh_compose(resfh, exp, dentry, fhp);
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000  271) 	if (!err && d_really_is_negative(dentry))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  272) 		err = nfserr_noent;
32c1eb0cd7ee0 (Andy Adamson           2007-07-17 04:04:48 -0700  273) out:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  274) 	dput(dentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  275) 	exp_put(exp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  276) 	return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  277) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  278) 
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600  279) /*
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600  280)  * Commit metadata changes to stable storage.
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600  281)  */
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600  282) static int
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  283) commit_inode_metadata(struct inode *inode)
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600  284) {
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600  285) 	const struct export_operations *export_ops = inode->i_sb->s_export_op;
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600  286) 
c37650161a53c (Christoph Hellwig      2010-10-06 10:48:20 +0200  287) 	if (export_ops->commit_metadata)
c37650161a53c (Christoph Hellwig      2010-10-06 10:48:20 +0200  288) 		return export_ops->commit_metadata(inode);
c37650161a53c (Christoph Hellwig      2010-10-06 10:48:20 +0200  289) 	return sync_inode_metadata(inode, 1);
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600  290) }
6c0a654dceaa4 (J. Bruce Fields        2007-07-17 04:04:47 -0700  291) 
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  292) static int
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  293) commit_metadata(struct svc_fh *fhp)
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  294) {
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  295) 	struct inode *inode = d_inode(fhp->fh_dentry);
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  296) 
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  297) 	if (!EX_ISSYNC(fhp->fh_export))
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  298) 		return 0;
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  299) 	return commit_inode_metadata(inode);
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  300) }
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  301) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  302) /*
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  303)  * Go over the attributes and take care of the small differences between
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  304)  * NFS semantics and what Linux expects.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  305)  */
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  306) static void
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  307) nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  308) {
ca456252db052 (Jeff Layton            2008-04-16 16:28:47 -0400  309) 	/* sanitize the mode change */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  310) 	if (iap->ia_valid & ATTR_MODE) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  311) 		iap->ia_mode &= S_IALLUGO;
dee3209d993f1 (Jeff Layton            2008-04-16 16:28:46 -0400  312) 		iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
ca456252db052 (Jeff Layton            2008-04-16 16:28:47 -0400  313) 	}
ca456252db052 (Jeff Layton            2008-04-16 16:28:47 -0400  314) 
ca456252db052 (Jeff Layton            2008-04-16 16:28:47 -0400  315) 	/* Revoke setuid/setgid on chown */
0953e620de053 (Sachin S. Prabhu       2009-02-23 16:22:03 +0000  316) 	if (!S_ISDIR(inode->i_mode) &&
c4fa6d7c5971b (Stanislav Kholmanskikh 2013-12-11 14:16:36 +0400  317) 	    ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
ca456252db052 (Jeff Layton            2008-04-16 16:28:47 -0400  318) 		iap->ia_valid |= ATTR_KILL_PRIV;
ca456252db052 (Jeff Layton            2008-04-16 16:28:47 -0400  319) 		if (iap->ia_valid & ATTR_MODE) {
ca456252db052 (Jeff Layton            2008-04-16 16:28:47 -0400  320) 			/* we're setting mode too, just clear the s*id bits */
8a0ce7d99a4d1 (Jeff Layton            2007-10-18 03:05:19 -0700  321) 			iap->ia_mode &= ~S_ISUID;
ca456252db052 (Jeff Layton            2008-04-16 16:28:47 -0400  322) 			if (iap->ia_mode & S_IXGRP)
ca456252db052 (Jeff Layton            2008-04-16 16:28:47 -0400  323) 				iap->ia_mode &= ~S_ISGID;
ca456252db052 (Jeff Layton            2008-04-16 16:28:47 -0400  324) 		} else {
ca456252db052 (Jeff Layton            2008-04-16 16:28:47 -0400  325) 			/* set ATTR_KILL_* bits and let VFS handle it */
ca456252db052 (Jeff Layton            2008-04-16 16:28:47 -0400  326) 			iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
8a0ce7d99a4d1 (Jeff Layton            2007-10-18 03:05:19 -0700  327) 		}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  328) 	}
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  329) }
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  330) 
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  331) static __be32
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  332) nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  333) 		struct iattr *iap)
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  334) {
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  335) 	struct inode *inode = d_inode(fhp->fh_dentry);
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  336) 	int host_err;
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  337) 
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  338) 	if (iap->ia_size < inode->i_size) {
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  339) 		__be32 err;
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  340) 
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  341) 		err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  342) 				NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  343) 		if (err)
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  344) 			return err;
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  345) 	}
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  346) 
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  347) 	host_err = get_write_access(inode);
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  348) 	if (host_err)
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  349) 		goto out_nfserrno;
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  350) 
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  351) 	host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  352) 	if (host_err)
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  353) 		goto out_put_write_access;
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  354) 	return 0;
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  355) 
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  356) out_put_write_access:
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  357) 	put_write_access(inode);
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  358) out_nfserrno:
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  359) 	return nfserrno(host_err);
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  360) }
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  361) 
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  362) /*
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  363)  * Set various file attributes.  After this call fhp needs an fh_put.
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  364)  */
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  365) __be32
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  366) nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
2a1aa48929af7 (Arnd Bergmann          2019-11-03 17:50:18 +0100  367) 	     int check_guard, time64_t guardtime)
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  368) {
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  369) 	struct dentry	*dentry;
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  370) 	struct inode	*inode;
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  371) 	int		accmode = NFSD_MAY_SATTR;
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  372) 	umode_t		ftype = 0;
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  373) 	__be32		err;
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  374) 	int		host_err;
9f67f189939ec (J. Bruce Fields        2014-02-24 14:59:47 -0500  375) 	bool		get_write_count;
758e99fefe1d9 (Christoph Hellwig      2017-02-20 17:04:42 -0500  376) 	bool		size_change = (iap->ia_valid & ATTR_SIZE);
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  377) 
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  378) 	if (iap->ia_valid & ATTR_SIZE) {
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  379) 		accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  380) 		ftype = S_IFREG;
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  381) 	}
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  382) 
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  383) 	/*
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  384) 	 * If utimes(2) and friends are called with times not NULL, we should
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  385) 	 * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
e977cc8308620 (Geert Uytterhoeven     2019-05-27 14:21:32 +0200  386) 	 * will return EACCES, when the caller's effective UID does not match
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  387) 	 * the owner of the file, and the caller is not privileged. In this
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  388) 	 * situation, we should return EPERM(notify_change will return this).
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  389) 	 */
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  390) 	if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  391) 		accmode |= NFSD_MAY_OWNER_OVERRIDE;
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  392) 		if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  393) 			accmode |= NFSD_MAY_WRITE;
255fbca65137e (zhengbin               2018-11-30 16:04:25 +0800  394) 	}
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  395) 
9f67f189939ec (J. Bruce Fields        2014-02-24 14:59:47 -0500  396) 	/* Callers that do fh_verify should do the fh_want_write: */
9f67f189939ec (J. Bruce Fields        2014-02-24 14:59:47 -0500  397) 	get_write_count = !fhp->fh_dentry;
9f67f189939ec (J. Bruce Fields        2014-02-24 14:59:47 -0500  398) 
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  399) 	/* Get inode */
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  400) 	err = fh_verify(rqstp, fhp, ftype, accmode);
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  401) 	if (err)
758e99fefe1d9 (Christoph Hellwig      2017-02-20 17:04:42 -0500  402) 		return err;
9f67f189939ec (J. Bruce Fields        2014-02-24 14:59:47 -0500  403) 	if (get_write_count) {
9f67f189939ec (J. Bruce Fields        2014-02-24 14:59:47 -0500  404) 		host_err = fh_want_write(fhp);
9f67f189939ec (J. Bruce Fields        2014-02-24 14:59:47 -0500  405) 		if (host_err)
758e99fefe1d9 (Christoph Hellwig      2017-02-20 17:04:42 -0500  406) 			goto out;
9f67f189939ec (J. Bruce Fields        2014-02-24 14:59:47 -0500  407) 	}
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  408) 
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  409) 	dentry = fhp->fh_dentry;
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000  410) 	inode = d_inode(dentry);
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  411) 
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  412) 	/* Ignore any mode updates on symlinks */
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  413) 	if (S_ISLNK(inode->i_mode))
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  414) 		iap->ia_valid &= ~ATTR_MODE;
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  415) 
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  416) 	if (!iap->ia_valid)
758e99fefe1d9 (Christoph Hellwig      2017-02-20 17:04:42 -0500  417) 		return 0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  418) 
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  419) 	nfsd_sanitize_attrs(inode, iap);
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  420) 
758e99fefe1d9 (Christoph Hellwig      2017-02-20 17:04:42 -0500  421) 	if (check_guard && guardtime != inode->i_ctime.tv_sec)
758e99fefe1d9 (Christoph Hellwig      2017-02-20 17:04:42 -0500  422) 		return nfserr_notsync;
758e99fefe1d9 (Christoph Hellwig      2017-02-20 17:04:42 -0500  423) 
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  424) 	/*
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  425) 	 * The size case is special, it changes the file in addition to the
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  426) 	 * attributes, and file systems don't expect it to be mixed with
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  427) 	 * "random" attribute changes.  We thus split out the size change
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  428) 	 * into a separate call to ->setattr, and do the rest as a separate
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  429) 	 * setattr call.
818e5a22e907f (Christoph Hellwig      2013-11-18 05:07:30 -0800  430) 	 */
758e99fefe1d9 (Christoph Hellwig      2017-02-20 17:04:42 -0500  431) 	if (size_change) {
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  432) 		err = nfsd_get_write_access(rqstp, fhp, iap);
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  433) 		if (err)
758e99fefe1d9 (Christoph Hellwig      2017-02-20 17:04:42 -0500  434) 			return err;
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  435) 	}
f0c63124a6165 (Christoph Hellwig      2014-09-07 12:15:52 -0700  436) 
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  437) 	fh_lock(fhp);
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  438) 	if (size_change) {
f0c63124a6165 (Christoph Hellwig      2014-09-07 12:15:52 -0700  439) 		/*
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  440) 		 * RFC5661, Section 18.30.4:
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  441) 		 *   Changing the size of a file with SETATTR indirectly
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  442) 		 *   changes the time_modify and change attributes.
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  443) 		 *
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  444) 		 * (and similar for the older RFCs)
f0c63124a6165 (Christoph Hellwig      2014-09-07 12:15:52 -0700  445) 		 */
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  446) 		struct iattr size_attr = {
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  447) 			.ia_valid	= ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  448) 			.ia_size	= iap->ia_size,
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  449) 		};
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  450) 
2f221d6f7b881 (Christian Brauner      2021-01-21 14:19:26 +0100  451) 		host_err = notify_change(&init_user_ns, dentry, &size_attr, NULL);
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  452) 		if (host_err)
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  453) 			goto out_unlock;
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  454) 		iap->ia_valid &= ~ATTR_SIZE;
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  455) 
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  456) 		/*
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  457) 		 * Avoid the additional setattr call below if the only other
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  458) 		 * attribute that the client sends is the mtime, as we update
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  459) 		 * it as part of the size change above.
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  460) 		 */
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  461) 		if ((iap->ia_valid & ~ATTR_MTIME) == 0)
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  462) 			goto out_unlock;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  463) 	}
987da4791052f (Christoph Hellwig      2013-11-18 05:07:47 -0800  464) 
41f53350a0f36 (Christoph Hellwig      2017-01-24 09:22:41 +0100  465) 	iap->ia_valid |= ATTR_CTIME;
2f221d6f7b881 (Christian Brauner      2021-01-21 14:19:26 +0100  466) 	host_err = notify_change(&init_user_ns, dentry, iap, NULL);
987da4791052f (Christoph Hellwig      2013-11-18 05:07:47 -0800  467) 
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  468) out_unlock:
783112f7401ff (Christoph Hellwig      2017-02-20 07:21:33 +0100  469) 	fh_unlock(fhp);
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  470) 	if (size_change)
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  471) 		put_write_access(inode);
0839ffb83e44e (J. Bruce Fields        2017-02-09 14:20:42 -0500  472) out:
758e99fefe1d9 (Christoph Hellwig      2017-02-20 17:04:42 -0500  473) 	if (!host_err)
758e99fefe1d9 (Christoph Hellwig      2017-02-20 17:04:42 -0500  474) 		host_err = commit_metadata(fhp);
758e99fefe1d9 (Christoph Hellwig      2017-02-20 17:04:42 -0500  475) 	return nfserrno(host_err);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  476) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  477) 
5be196e5f925d (Christoph Hellwig      2006-01-09 20:51:55 -0800  478) #if defined(CONFIG_NFSD_V4)
9b4146e85536e (Chuck Lever            2012-01-04 16:26:43 -0500  479) /*
9b4146e85536e (Chuck Lever            2012-01-04 16:26:43 -0500  480)  * NFS junction information is stored in an extended attribute.
9b4146e85536e (Chuck Lever            2012-01-04 16:26:43 -0500  481)  */
9b4146e85536e (Chuck Lever            2012-01-04 16:26:43 -0500  482) #define NFSD_JUNCTION_XATTR_NAME	XATTR_TRUSTED_PREFIX "junction.nfs"
9b4146e85536e (Chuck Lever            2012-01-04 16:26:43 -0500  483) 
9b4146e85536e (Chuck Lever            2012-01-04 16:26:43 -0500  484) /**
9b4146e85536e (Chuck Lever            2012-01-04 16:26:43 -0500  485)  * nfsd4_is_junction - Test if an object could be an NFS junction
9b4146e85536e (Chuck Lever            2012-01-04 16:26:43 -0500  486)  *
9b4146e85536e (Chuck Lever            2012-01-04 16:26:43 -0500  487)  * @dentry: object to test
9b4146e85536e (Chuck Lever            2012-01-04 16:26:43 -0500  488)  *
9b4146e85536e (Chuck Lever            2012-01-04 16:26:43 -0500  489)  * Returns 1 if "dentry" appears to contain NFS junction information.
9b4146e85536e (Chuck Lever            2012-01-04 16:26:43 -0500  490)  * Otherwise 0 is returned.
9b4146e85536e (Chuck Lever            2012-01-04 16:26:43 -0500  491)  */
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  492) int nfsd4_is_junction(struct dentry *dentry)
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  493) {
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000  494) 	struct inode *inode = d_inode(dentry);
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  495) 
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  496) 	if (inode == NULL)
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  497) 		return 0;
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  498) 	if (inode->i_mode & S_IXUGO)
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  499) 		return 0;
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  500) 	if (!(inode->i_mode & S_ISVTX))
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  501) 		return 0;
c7c7a1a18af4c (Tycho Andersen         2021-01-21 14:19:28 +0100  502) 	if (vfs_getxattr(&init_user_ns, dentry, NFSD_JUNCTION_XATTR_NAME,
c7c7a1a18af4c (Tycho Andersen         2021-01-21 14:19:28 +0100  503) 			 NULL, 0) <= 0)
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  504) 		return 0;
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  505) 	return 1;
11fcee0293a6d (Trond Myklebust        2011-09-12 19:37:26 -0400  506) }
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  507) #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  508) __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  509) 		struct xdr_netobj *label)
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  510) {
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  511) 	__be32 error;
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  512) 	int host_error;
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  513) 	struct dentry *dentry;
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  514) 
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  515) 	error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  516) 	if (error)
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  517) 		return error;
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  518) 
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  519) 	dentry = fhp->fh_dentry;
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  520) 
5955102c9984f (Al Viro                2016-01-22 15:40:57 -0500  521) 	inode_lock(d_inode(dentry));
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  522) 	host_error = security_inode_setsecctx(dentry, label->data, label->len);
5955102c9984f (Al Viro                2016-01-22 15:40:57 -0500  523) 	inode_unlock(d_inode(dentry));
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  524) 	return nfserrno(host_error);
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  525) }
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  526) #else
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  527) __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  528) 		struct xdr_netobj *label)
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  529) {
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  530) 	return nfserr_notsupp;
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  531) }
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  532) #endif
18032ca062e62 (David Quigley          2013-05-02 13:19:10 -0400  533) 
b66ae6dd0c30c (Trond Myklebust        2020-01-06 13:40:32 -0500  534) __be32 nfsd4_clone_file_range(struct nfsd_file *nf_src, u64 src_pos,
b66ae6dd0c30c (Trond Myklebust        2020-01-06 13:40:32 -0500  535) 		struct nfsd_file *nf_dst, u64 dst_pos, u64 count, bool sync)
ffa0160a10391 (Christoph Hellwig      2015-12-03 12:59:52 +0100  536) {
b66ae6dd0c30c (Trond Myklebust        2020-01-06 13:40:32 -0500  537) 	struct file *src = nf_src->nf_file;
b66ae6dd0c30c (Trond Myklebust        2020-01-06 13:40:32 -0500  538) 	struct file *dst = nf_dst->nf_file;
42ec3d4c02187 (Darrick J. Wong        2018-10-30 10:41:49 +1100  539) 	loff_t cloned;
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  540) 	__be32 ret = 0;
42ec3d4c02187 (Darrick J. Wong        2018-10-30 10:41:49 +1100  541) 
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  542) 	down_write(&nf_dst->nf_rwsem);
452ce65951a2f (Darrick J. Wong        2018-10-30 10:41:56 +1100  543) 	cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  544) 	if (cloned < 0) {
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  545) 		ret = nfserrno(cloned);
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  546) 		goto out_err;
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  547) 	}
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  548) 	if (count && cloned != count) {
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  549) 		ret = nfserrno(-EINVAL);
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  550) 		goto out_err;
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  551) 	}
a25e3726b32c7 (Trond Myklebust        2019-11-27 17:05:51 -0500  552) 	if (sync) {
a25e3726b32c7 (Trond Myklebust        2019-11-27 17:05:51 -0500  553) 		loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX;
a25e3726b32c7 (Trond Myklebust        2019-11-27 17:05:51 -0500  554) 		int status = vfs_fsync_range(dst, dst_pos, dst_end, 0);
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  555) 
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  556) 		if (!status)
57f64034966fb (Trond Myklebust        2019-12-18 14:57:23 -0500  557) 			status = commit_inode_metadata(file_inode(src));
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  558) 		if (status < 0) {
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  559) 			nfsd_reset_boot_verifier(net_generic(nf_dst->nf_net,
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  560) 						 nfsd_net_id));
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  561) 			ret = nfserrno(status);
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  562) 		}
a25e3726b32c7 (Trond Myklebust        2019-11-27 17:05:51 -0500  563) 	}
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  564) out_err:
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  565) 	up_write(&nf_dst->nf_rwsem);
1b28d756b2eae (Trond Myklebust        2020-01-06 13:40:33 -0500  566) 	return ret;
ffa0160a10391 (Christoph Hellwig      2015-12-03 12:59:52 +0100  567) }
ffa0160a10391 (Christoph Hellwig      2015-12-03 12:59:52 +0100  568) 
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  569) ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  570) 			     u64 dst_pos, u64 count)
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  571) {
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  572) 
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  573) 	/*
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  574) 	 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  575) 	 * thread and client rpc slot.  The choice of 4MB is somewhat
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  576) 	 * arbitrary.  We might instead base this on r/wsize, or make it
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  577) 	 * tunable, or use a time instead of a byte limit, or implement
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  578) 	 * asynchronous copy.  In theory a client could also recognize a
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  579) 	 * limit like this and pipeline multiple COPY requests.
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  580) 	 */
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  581) 	count = min_t(u64, count, 1 << 22);
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  582) 	return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  583) }
29ae7f9dc21a7 (Anna Schumaker         2016-09-07 15:57:30 -0400  584) 
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  585) __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  586) 			   struct file *file, loff_t offset, loff_t len,
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  587) 			   int flags)
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  588) {
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  589) 	int error;
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  590) 
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  591) 	if (!S_ISREG(file_inode(file)->i_mode))
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  592) 		return nfserr_inval;
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  593) 
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  594) 	error = vfs_fallocate(file, flags, offset, len);
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  595) 	if (!error)
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  596) 		error = commit_metadata(fhp);
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  597) 
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  598) 	return nfserrno(error);
95d871f03cae6 (Anna Schumaker         2014-11-07 14:44:26 -0500  599) }
6a85d6c76962d (J. Bruce Fields        2010-07-06 12:39:12 -0400  600) #endif /* defined(CONFIG_NFSD_V4) */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  601) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  602) #ifdef CONFIG_NFSD_V3
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  603) /*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  604)  * Check server access rights to a file system object
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  605)  */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  606) struct accessmap {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  607) 	u32		access;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  608) 	int		how;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  609) };
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  610) static struct accessmap	nfs3_regaccess[] = {
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  611)     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  612)     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  613)     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_TRUNC	},
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  614)     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE			},
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  615) 
c11d7fd1b3178 (Frank van der Linden   2020-06-23 22:39:24 +0000  616) #ifdef CONFIG_NFSD_V4
c11d7fd1b3178 (Frank van der Linden   2020-06-23 22:39:24 +0000  617)     {	NFS4_ACCESS_XAREAD,	NFSD_MAY_READ			},
c11d7fd1b3178 (Frank van der Linden   2020-06-23 22:39:24 +0000  618)     {	NFS4_ACCESS_XAWRITE,	NFSD_MAY_WRITE			},
c11d7fd1b3178 (Frank van der Linden   2020-06-23 22:39:24 +0000  619)     {	NFS4_ACCESS_XALIST,	NFSD_MAY_READ			},
c11d7fd1b3178 (Frank van der Linden   2020-06-23 22:39:24 +0000  620) #endif
c11d7fd1b3178 (Frank van der Linden   2020-06-23 22:39:24 +0000  621) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  622)     {	0,			0				}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  623) };
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  624) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  625) static struct accessmap	nfs3_diraccess[] = {
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  626)     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  627)     {	NFS3_ACCESS_LOOKUP,	NFSD_MAY_EXEC			},
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  628)     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  629)     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_EXEC|NFSD_MAY_WRITE	},
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  630)     {	NFS3_ACCESS_DELETE,	NFSD_MAY_REMOVE			},
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  631) 
c11d7fd1b3178 (Frank van der Linden   2020-06-23 22:39:24 +0000  632) #ifdef CONFIG_NFSD_V4
c11d7fd1b3178 (Frank van der Linden   2020-06-23 22:39:24 +0000  633)     {	NFS4_ACCESS_XAREAD,	NFSD_MAY_READ			},
c11d7fd1b3178 (Frank van der Linden   2020-06-23 22:39:24 +0000  634)     {	NFS4_ACCESS_XAWRITE,	NFSD_MAY_WRITE			},
c11d7fd1b3178 (Frank van der Linden   2020-06-23 22:39:24 +0000  635)     {	NFS4_ACCESS_XALIST,	NFSD_MAY_READ			},
c11d7fd1b3178 (Frank van der Linden   2020-06-23 22:39:24 +0000  636) #endif
c11d7fd1b3178 (Frank van der Linden   2020-06-23 22:39:24 +0000  637) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  638)     {	0,			0				}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  639) };
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  640) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  641) static struct accessmap	nfs3_anyaccess[] = {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  642) 	/* Some clients - Solaris 2.6 at least, make an access call
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  643) 	 * to the server to check for access for things like /dev/null
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  644) 	 * (which really, the server doesn't care about).  So
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  645) 	 * We provide simple access checking for them, looking
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  646) 	 * mainly at mode bits, and we make sure to ignore read-only
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  647) 	 * filesystem checks
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  648) 	 */
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  649)     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  650)     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  651)     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  652)     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  653) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  654)     {	0,			0				}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  655) };
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  656) 
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  657) __be32
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  658) nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  659) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  660) 	struct accessmap	*map;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  661) 	struct svc_export	*export;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  662) 	struct dentry		*dentry;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  663) 	u32			query, result = 0, sresult = 0;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  664) 	__be32			error;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  665) 
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200  666) 	error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  667) 	if (error)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  668) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  669) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  670) 	export = fhp->fh_export;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  671) 	dentry = fhp->fh_dentry;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  672) 
e36cb0b89ce20 (David Howells          2015-01-29 12:02:35 +0000  673) 	if (d_is_reg(dentry))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  674) 		map = nfs3_regaccess;
e36cb0b89ce20 (David Howells          2015-01-29 12:02:35 +0000  675) 	else if (d_is_dir(dentry))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  676) 		map = nfs3_diraccess;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  677) 	else
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  678) 		map = nfs3_anyaccess;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  679) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  680) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  681) 	query = *access;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  682) 	for  (; map->access; map++) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  683) 		if (map->access & query) {
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  684) 			__be32 err2;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  685) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  686) 			sresult |= map->access;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  687) 
0ec757df97430 (J. Bruce Fields        2007-07-17 04:04:48 -0700  688) 			err2 = nfsd_permission(rqstp, export, dentry, map->how);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  689) 			switch (err2) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  690) 			case nfs_ok:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  691) 				result |= map->access;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  692) 				break;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  693) 				
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  694) 			/* the following error codes just mean the access was not allowed,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  695) 			 * rather than an error occurred */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  696) 			case nfserr_rofs:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  697) 			case nfserr_acces:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  698) 			case nfserr_perm:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  699) 				/* simply don't "or" in the access bit. */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  700) 				break;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  701) 			default:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  702) 				error = err2;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  703) 				goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  704) 			}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  705) 		}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  706) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  707) 	*access = result;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  708) 	if (supported)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  709) 		*supported = sresult;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  710) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  711)  out:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  712) 	return error;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  713) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  714) #endif /* CONFIG_NFSD_V3 */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  715) 
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  716) int nfsd_open_break_lease(struct inode *inode, int access)
105f462210484 (J. Bruce Fields        2011-06-07 11:50:23 -0400  717) {
105f462210484 (J. Bruce Fields        2011-06-07 11:50:23 -0400  718) 	unsigned int mode;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  719) 
105f462210484 (J. Bruce Fields        2011-06-07 11:50:23 -0400  720) 	if (access & NFSD_MAY_NOT_BREAK_LEASE)
105f462210484 (J. Bruce Fields        2011-06-07 11:50:23 -0400  721) 		return 0;
105f462210484 (J. Bruce Fields        2011-06-07 11:50:23 -0400  722) 	mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
105f462210484 (J. Bruce Fields        2011-06-07 11:50:23 -0400  723) 	return break_lease(inode, mode | O_NONBLOCK);
105f462210484 (J. Bruce Fields        2011-06-07 11:50:23 -0400  724) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  725) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  726) /*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  727)  * Open an existing file or directory.
999448a8c0202 (Bernd Schubert         2012-03-18 22:44:49 -0400  728)  * The may_flags argument indicates the type of open (read/write/lock)
999448a8c0202 (Bernd Schubert         2012-03-18 22:44:49 -0400  729)  * and additional flags.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  730)  * N.B. After this call fhp needs an fh_put
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  731)  */
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  732) static __be32
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  733) __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
999448a8c0202 (Bernd Schubert         2012-03-18 22:44:49 -0400  734) 			int may_flags, struct file **filp)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  735) {
765927b2d5087 (Al Viro                2012-06-26 21:58:53 +0400  736) 	struct path	path;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  737) 	struct inode	*inode;
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  738) 	struct file	*file;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  739) 	int		flags = O_RDONLY|O_LARGEFILE;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  740) 	__be32		err;
91885258e8343 (Jeff Layton            2010-03-19 08:06:28 -0400  741) 	int		host_err = 0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  742) 
765927b2d5087 (Al Viro                2012-06-26 21:58:53 +0400  743) 	path.mnt = fhp->fh_export->ex_path.mnt;
765927b2d5087 (Al Viro                2012-06-26 21:58:53 +0400  744) 	path.dentry = fhp->fh_dentry;
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000  745) 	inode = d_inode(path.dentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  746) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  747) 	/* Disallow write access to files with the append-only bit set
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  748) 	 * or any access when mandatory locking enabled
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  749) 	 */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  750) 	err = nfserr_perm;
999448a8c0202 (Bernd Schubert         2012-03-18 22:44:49 -0400  751) 	if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  752) 		goto out;
5e7fc436426b1 (J. Bruce Fields        2007-10-02 14:18:12 -0400  753) 	/*
5e7fc436426b1 (J. Bruce Fields        2007-10-02 14:18:12 -0400  754) 	 * We must ignore files (but only files) which might have mandatory
5e7fc436426b1 (J. Bruce Fields        2007-10-02 14:18:12 -0400  755) 	 * locks on them because there is no way to know if the accesser has
5e7fc436426b1 (J. Bruce Fields        2007-10-02 14:18:12 -0400  756) 	 * the lock.
5e7fc436426b1 (J. Bruce Fields        2007-10-02 14:18:12 -0400  757) 	 */
5e7fc436426b1 (J. Bruce Fields        2007-10-02 14:18:12 -0400  758) 	if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  759) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  760) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  761) 	if (!inode->i_fop)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  762) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  763) 
999448a8c0202 (Bernd Schubert         2012-03-18 22:44:49 -0400  764) 	host_err = nfsd_open_break_lease(inode, may_flags);
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  765) 	if (host_err) /* NOMEM or WOULDBLOCK */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  766) 		goto out_nfserr;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  767) 
999448a8c0202 (Bernd Schubert         2012-03-18 22:44:49 -0400  768) 	if (may_flags & NFSD_MAY_WRITE) {
999448a8c0202 (Bernd Schubert         2012-03-18 22:44:49 -0400  769) 		if (may_flags & NFSD_MAY_READ)
9ecb6a08d84d0 (J. Bruce Fields        2006-06-30 01:56:17 -0700  770) 			flags = O_RDWR|O_LARGEFILE;
9ecb6a08d84d0 (J. Bruce Fields        2006-06-30 01:56:17 -0700  771) 		else
9ecb6a08d84d0 (J. Bruce Fields        2006-06-30 01:56:17 -0700  772) 			flags = O_WRONLY|O_LARGEFILE;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  773) 	}
999448a8c0202 (Bernd Schubert         2012-03-18 22:44:49 -0400  774) 
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  775) 	file = dentry_open(&path, flags, current_cred());
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  776) 	if (IS_ERR(file)) {
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  777) 		host_err = PTR_ERR(file);
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  778) 		goto out_nfserr;
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  779) 	}
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  780) 
6035a27b25ab9 (Al Viro                2018-06-08 13:40:10 -0400  781) 	host_err = ima_file_check(file, may_flags);
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  782) 	if (host_err) {
fd891454609ec (Christoph Hellwig      2015-04-28 15:41:16 +0200  783) 		fput(file);
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  784) 		goto out_nfserr;
06effdbb49af5 (Bernd Schubert         2012-03-18 22:44:50 -0400  785) 	}
06effdbb49af5 (Bernd Schubert         2012-03-18 22:44:50 -0400  786) 
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  787) 	if (may_flags & NFSD_MAY_64BIT_COOKIE)
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  788) 		file->f_mode |= FMODE_64BITHASH;
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  789) 	else
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  790) 		file->f_mode |= FMODE_32BITHASH;
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  791) 
8519f994e5cf2 (Kinglong Mee           2014-09-03 08:14:06 +0800  792) 	*filp = file;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  793) out_nfserr:
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  794) 	err = nfserrno(host_err);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  795) out:
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  796) 	return err;
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  797) }
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  798) 
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  799) __be32
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  800) nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  801) 		int may_flags, struct file **filp)
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  802) {
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  803) 	__be32 err;
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  804) 
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  805) 	validate_process_creds();
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  806) 	/*
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  807) 	 * If we get here, then the client has already done an "open",
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  808) 	 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  809) 	 * in case a chmod has now revoked permission.
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  810) 	 *
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  811) 	 * Arguably we should also allow the owner override for
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  812) 	 * directories, but we never have and it doesn't seem to have
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  813) 	 * caused anyone a problem.  If we were to change this, note
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  814) 	 * also that our filldir callbacks would need a variant of
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  815) 	 * lookup_one_len that doesn't check permissions.
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  816) 	 */
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  817) 	if (type == S_IFREG)
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  818) 		may_flags |= NFSD_MAY_OWNER_OVERRIDE;
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  819) 	err = fh_verify(rqstp, fhp, type, may_flags);
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  820) 	if (!err)
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  821) 		err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  822) 	validate_process_creds();
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  823) 	return err;
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  824) }
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  825) 
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  826) __be32
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  827) nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  828) 		int may_flags, struct file **filp)
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  829) {
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  830) 	__be32 err;
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  831) 
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  832) 	validate_process_creds();
65294c1f2c5e7 (Jeff Layton            2019-08-18 14:18:48 -0400  833) 	err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
e0e817392b9ac (David Howells          2009-09-02 09:13:40 +0100  834) 	validate_process_creds();
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  835) 	return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  836) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  837) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  838) /*
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  839)  * Grab and keep cached pages associated with a file in the svc_rqst
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  840)  * so that they can be passed to the network sendmsg/sendpage routines
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  841)  * directly. They will be released after the sending has completed.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  842)  */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  843) static int
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  844) nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  845) 		  struct splice_desc *sd)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  846) {
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  847) 	struct svc_rqst *rqstp = sd->u.data;
afc59400d6c65 (J. Bruce Fields        2012-12-10 18:01:37 -0500  848) 	struct page **pp = rqstp->rq_next_page;
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  849) 	struct page *page = buf->page;
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  850) 	size_t size;
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  851) 
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  852) 	size = sd->len;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  853) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  854) 	if (rqstp->rq_res.page_len == 0) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  855) 		get_page(page);
afc59400d6c65 (J. Bruce Fields        2012-12-10 18:01:37 -0500  856) 		put_page(*rqstp->rq_next_page);
afc59400d6c65 (J. Bruce Fields        2012-12-10 18:01:37 -0500  857) 		*(rqstp->rq_next_page++) = page;
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  858) 		rqstp->rq_res.page_base = buf->offset;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  859) 		rqstp->rq_res.page_len = size;
4452435948424 (NeilBrown              2006-10-04 02:15:46 -0700  860) 	} else if (page != pp[-1]) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  861) 		get_page(page);
afc59400d6c65 (J. Bruce Fields        2012-12-10 18:01:37 -0500  862) 		if (*rqstp->rq_next_page)
afc59400d6c65 (J. Bruce Fields        2012-12-10 18:01:37 -0500  863) 			put_page(*rqstp->rq_next_page);
afc59400d6c65 (J. Bruce Fields        2012-12-10 18:01:37 -0500  864) 		*(rqstp->rq_next_page++) = page;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  865) 		rqstp->rq_res.page_len += size;
4452435948424 (NeilBrown              2006-10-04 02:15:46 -0700  866) 	} else
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  867) 		rqstp->rq_res.page_len += size;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  868) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  869) 	return size;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  870) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  871) 
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  872) static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  873) 				    struct splice_desc *sd)
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  874) {
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  875) 	return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  876) }
cf8208d0eabd1 (Jens Axboe             2007-06-12 21:22:14 +0200  877) 
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  878) static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len,
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  879) 		size_t expected)
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  880) {
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  881) 	if (expected != 0 && len == 0)
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  882) 		return 1;
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  883) 	if (offset+len >= i_size_read(file_inode(file)))
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  884) 		return 1;
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  885) 	return 0;
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  886) }
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  887) 
87c5942e8fae8 (Chuck Lever            2018-03-28 13:29:11 -0400  888) static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
87c5942e8fae8 (Chuck Lever            2018-03-28 13:29:11 -0400  889) 			       struct file *file, loff_t offset,
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  890) 			       unsigned long *count, u32 *eof, ssize_t host_err)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  891) {
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  892) 	if (host_err >= 0) {
20ad856e47323 (Amir Goldstein         2021-01-06 09:52:36 +0200  893) 		nfsd_stats_io_read_add(fhp->fh_export, host_err);
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  894) 		*eof = nfsd_eof_on_read(file, offset, host_err, *count);
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  895) 		*count = host_err;
2a12a9d781463 (Eric Paris             2009-12-17 21:24:21 -0500  896) 		fsnotify_access(file);
87c5942e8fae8 (Chuck Lever            2018-03-28 13:29:11 -0400  897) 		trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  898) 		return 0;
87c5942e8fae8 (Chuck Lever            2018-03-28 13:29:11 -0400  899) 	} else {
87c5942e8fae8 (Chuck Lever            2018-03-28 13:29:11 -0400  900) 		trace_nfsd_read_err(rqstp, fhp, offset, host_err);
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  901) 		return nfserrno(host_err);
87c5942e8fae8 (Chuck Lever            2018-03-28 13:29:11 -0400  902) 	}
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  903) }
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  904) 
87c5942e8fae8 (Chuck Lever            2018-03-28 13:29:11 -0400  905) __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  906) 			struct file *file, loff_t offset, unsigned long *count,
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  907) 			u32 *eof)
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  908) {
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  909) 	struct splice_desc sd = {
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  910) 		.len		= 0,
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  911) 		.total_len	= *count,
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  912) 		.pos		= offset,
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  913) 		.u.data		= rqstp,
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  914) 	};
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  915) 	ssize_t host_err;
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  916) 
87c5942e8fae8 (Chuck Lever            2018-03-28 13:29:11 -0400  917) 	trace_nfsd_read_splice(rqstp, fhp, offset, *count);
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  918) 	rqstp->rq_next_page = rqstp->rq_respages + 1;
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  919) 	host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  920) 	return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  921) }
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  922) 
87c5942e8fae8 (Chuck Lever            2018-03-28 13:29:11 -0400  923) __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
87c5942e8fae8 (Chuck Lever            2018-03-28 13:29:11 -0400  924) 		  struct file *file, loff_t offset,
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  925) 		  struct kvec *vec, int vlen, unsigned long *count,
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  926) 		  u32 *eof)
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  927) {
73da852e3831b (Christoph Hellwig      2017-05-27 11:16:53 +0300  928) 	struct iov_iter iter;
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  929) 	loff_t ppos = offset;
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  930) 	ssize_t host_err;
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400  931) 
87c5942e8fae8 (Chuck Lever            2018-03-28 13:29:11 -0400  932) 	trace_nfsd_read_vector(rqstp, fhp, offset, *count);
aa563d7bca6e8 (David Howells          2018-10-20 00:57:56 +0100  933) 	iov_iter_kvec(&iter, READ, vec, vlen, *count);
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  934) 	host_err = vfs_iter_read(file, &iter, &ppos, 0);
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400  935) 	return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  936) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  937) 
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  938) /*
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  939)  * Gathered writes: If another process is currently writing to the file,
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  940)  * there's a high chance this is another nfsd (triggered by a bulk write
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  941)  * from a client's biod). Rather than syncing the file with each write
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  942)  * request, we sleep for 10 msec.
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  943)  *
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  944)  * I don't know if this roughly approximates C. Juszak's idea of
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  945)  * gathered writes, but it's a nice and simple solution (IMHO), and it
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  946)  * seems to work:-)
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  947)  *
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  948)  * Note: we do this only in the NFSv2 case, since v3 and higher have a
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  949)  * better tool (separate unstable writes and commits) for solving this
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  950)  * problem.
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  951)  */
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  952) static int wait_for_concurrent_writes(struct file *file)
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  953) {
496ad9aa8ef44 (Al Viro                2013-01-23 17:07:38 -0500  954) 	struct inode *inode = file_inode(file);
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  955) 	static ino_t last_ino;
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  956) 	static dev_t last_dev;
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  957) 	int err = 0;
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  958) 
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  959) 	if (atomic_read(&inode->i_writecount) > 1
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  960) 	    || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  961) 		dprintk("nfsd: write defer %d\n", task_pid_nr(current));
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  962) 		msleep(10);
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  963) 		dprintk("nfsd: write resume %d\n", task_pid_nr(current));
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  964) 	}
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  965) 
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  966) 	if (inode->i_state & I_DIRTY) {
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  967) 		dprintk("nfsd: write sync %d\n", task_pid_nr(current));
8018ab0574809 (Christoph Hellwig      2010-03-22 17:32:25 +0100  968) 		err = vfs_fsync(file, 0);
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  969) 	}
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  970) 	last_ino = inode->i_ino;
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  971) 	last_dev = inode->i_sb->s_dev;
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  972) 	return err;
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  973) }
d911df7b8d44d (J. Bruce Fields        2009-06-15 16:03:53 -0700  974) 
af90f707fa6d5 (Christoph Hellwig      2015-06-18 16:45:00 +0200  975) __be32
16f8f89410827 (Trond Myklebust        2020-01-06 13:40:29 -0500  976) nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  977) 				loff_t offset, struct kvec *vec, int vlen,
19e0663ff9bce (Trond Myklebust        2020-01-06 13:40:37 -0500  978) 				unsigned long *cnt, int stable,
19e0663ff9bce (Trond Myklebust        2020-01-06 13:40:37 -0500  979) 				__be32 *verf)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  980) {
16f8f89410827 (Trond Myklebust        2020-01-06 13:40:29 -0500  981) 	struct file		*file = nf->nf_file;
01cbf3853959f (Trond Myklebust        2020-11-30 17:03:19 -0500  982) 	struct super_block	*sb = file_inode(file)->i_sb;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700  983) 	struct svc_export	*exp;
73da852e3831b (Christoph Hellwig      2017-05-27 11:16:53 +0300  984) 	struct iov_iter		iter;
d890be159a713 (Chuck Lever            2018-03-27 10:53:27 -0400  985) 	__be32			nfserr;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700  986) 	int			host_err;
48e03bc515cff (Trond Myklebust        2009-06-05 12:35:15 -0400  987) 	int			use_wgather;
e49dbbf3e770a (Kent Overstreet        2013-03-22 11:18:24 -0700  988) 	loff_t			pos = offset;
01cbf3853959f (Trond Myklebust        2020-11-30 17:03:19 -0500  989) 	unsigned long		exp_op_flags = 0;
8658452e4a588 (NeilBrown              2014-05-12 11:22:47 +1000  990) 	unsigned int		pflags = current->flags;
ddef7ed2b5cba (Christoph Hellwig      2017-07-06 18:58:37 +0200  991) 	rwf_t			flags = 0;
01cbf3853959f (Trond Myklebust        2020-11-30 17:03:19 -0500  992) 	bool			restore_flags = false;
8658452e4a588 (NeilBrown              2014-05-12 11:22:47 +1000  993) 
d890be159a713 (Chuck Lever            2018-03-27 10:53:27 -0400  994) 	trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
d890be159a713 (Chuck Lever            2018-03-27 10:53:27 -0400  995) 
01cbf3853959f (Trond Myklebust        2020-11-30 17:03:19 -0500  996) 	if (sb->s_export_op)
01cbf3853959f (Trond Myklebust        2020-11-30 17:03:19 -0500  997) 		exp_op_flags = sb->s_export_op->flags;
01cbf3853959f (Trond Myklebust        2020-11-30 17:03:19 -0500  998) 
01cbf3853959f (Trond Myklebust        2020-11-30 17:03:19 -0500  999) 	if (test_bit(RQ_LOCAL, &rqstp->rq_flags) &&
01cbf3853959f (Trond Myklebust        2020-11-30 17:03:19 -0500 1000) 	    !(exp_op_flags & EXPORT_OP_REMOTE_FS)) {
8658452e4a588 (NeilBrown              2014-05-12 11:22:47 +1000 1001) 		/*
a37b0715ddf30 (NeilBrown              2020-06-01 21:48:18 -0700 1002) 		 * We want throttling in balance_dirty_pages()
a37b0715ddf30 (NeilBrown              2020-06-01 21:48:18 -0700 1003) 		 * and shrink_inactive_list() to only consider
a37b0715ddf30 (NeilBrown              2020-06-01 21:48:18 -0700 1004) 		 * the backingdev we are writing to, so that nfs to
8658452e4a588 (NeilBrown              2014-05-12 11:22:47 +1000 1005) 		 * localhost doesn't cause nfsd to lock up due to all
8658452e4a588 (NeilBrown              2014-05-12 11:22:47 +1000 1006) 		 * the client's dirty pages or its congested queue.
8658452e4a588 (NeilBrown              2014-05-12 11:22:47 +1000 1007) 		 */
a37b0715ddf30 (NeilBrown              2020-06-01 21:48:18 -0700 1008) 		current->flags |= PF_LOCAL_THROTTLE;
01cbf3853959f (Trond Myklebust        2020-11-30 17:03:19 -0500 1009) 		restore_flags = true;
01cbf3853959f (Trond Myklebust        2020-11-30 17:03:19 -0500 1010) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1011) 
865d50b23e4d2 (Kinglong Mee           2016-12-31 21:00:21 +0800 1012) 	exp = fhp->fh_export;
48e03bc515cff (Trond Myklebust        2009-06-05 12:35:15 -0400 1013) 	use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1014) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1015) 	if (!EX_ISSYNC(exp))
54bbb7d206db7 (Kinglong Mee           2016-12-31 20:59:53 +0800 1016) 		stable = NFS_UNSTABLE;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1017) 
24368aad47dc6 (Christoph Hellwig      2016-04-07 08:52:04 -0700 1018) 	if (stable && !use_wgather)
24368aad47dc6 (Christoph Hellwig      2016-04-07 08:52:04 -0700 1019) 		flags |= RWF_SYNC;
24368aad47dc6 (Christoph Hellwig      2016-04-07 08:52:04 -0700 1020) 
aa563d7bca6e8 (David Howells          2018-10-20 00:57:56 +0100 1021) 	iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt);
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1022) 	if (flags & RWF_SYNC) {
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1023) 		down_write(&nf->nf_rwsem);
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1024) 		host_err = vfs_iter_write(file, &iter, &pos, flags);
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1025) 		if (host_err < 0)
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1026) 			nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1027) 						 nfsd_net_id));
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1028) 		up_write(&nf->nf_rwsem);
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1029) 	} else {
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1030) 		down_read(&nf->nf_rwsem);
19e0663ff9bce (Trond Myklebust        2020-01-06 13:40:37 -0500 1031) 		if (verf)
19e0663ff9bce (Trond Myklebust        2020-01-06 13:40:37 -0500 1032) 			nfsd_copy_boot_verifier(verf,
19e0663ff9bce (Trond Myklebust        2020-01-06 13:40:37 -0500 1033) 					net_generic(SVC_NET(rqstp),
19e0663ff9bce (Trond Myklebust        2020-01-06 13:40:37 -0500 1034) 					nfsd_net_id));
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1035) 		host_err = vfs_iter_write(file, &iter, &pos, flags);
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1036) 		up_read(&nf->nf_rwsem);
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1037) 	}
7bf94c6ba9fb2 (Trond Myklebust        2020-01-06 13:40:31 -0500 1038) 	if (host_err < 0) {
7bf94c6ba9fb2 (Trond Myklebust        2020-01-06 13:40:31 -0500 1039) 		nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
7bf94c6ba9fb2 (Trond Myklebust        2020-01-06 13:40:31 -0500 1040) 					 nfsd_net_id));
e4636d535e327 (J. Bruce Fields        2009-06-15 19:07:13 -0700 1041) 		goto out_nfserr;
7bf94c6ba9fb2 (Trond Myklebust        2020-01-06 13:40:31 -0500 1042) 	}
09a80f2aef06b (Trond Myklebust        2019-12-17 12:33:33 -0500 1043) 	*cnt = host_err;
20ad856e47323 (Amir Goldstein         2021-01-06 09:52:36 +0200 1044) 	nfsd_stats_io_write_add(exp, *cnt);
2a12a9d781463 (Eric Paris             2009-12-17 21:24:21 -0500 1045) 	fsnotify_modify(file);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1046) 
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1047) 	if (stable && use_wgather) {
24368aad47dc6 (Christoph Hellwig      2016-04-07 08:52:04 -0700 1048) 		host_err = wait_for_concurrent_writes(file);
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1049) 		if (host_err < 0)
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1050) 			nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1051) 						 nfsd_net_id));
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1052) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1053) 
e4636d535e327 (J. Bruce Fields        2009-06-15 19:07:13 -0700 1054) out_nfserr:
d890be159a713 (Chuck Lever            2018-03-27 10:53:27 -0400 1055) 	if (host_err >= 0) {
d890be159a713 (Chuck Lever            2018-03-27 10:53:27 -0400 1056) 		trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
d890be159a713 (Chuck Lever            2018-03-27 10:53:27 -0400 1057) 		nfserr = nfs_ok;
d890be159a713 (Chuck Lever            2018-03-27 10:53:27 -0400 1058) 	} else {
d890be159a713 (Chuck Lever            2018-03-27 10:53:27 -0400 1059) 		trace_nfsd_write_err(rqstp, fhp, offset, host_err);
d890be159a713 (Chuck Lever            2018-03-27 10:53:27 -0400 1060) 		nfserr = nfserrno(host_err);
d890be159a713 (Chuck Lever            2018-03-27 10:53:27 -0400 1061) 	}
01cbf3853959f (Trond Myklebust        2020-11-30 17:03:19 -0500 1062) 	if (restore_flags)
a37b0715ddf30 (NeilBrown              2020-06-01 21:48:18 -0700 1063) 		current_restore_flags(pflags, PF_LOCAL_THROTTLE);
d890be159a713 (Chuck Lever            2018-03-27 10:53:27 -0400 1064) 	return nfserr;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1065) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1066) 
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1067) /*
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1068)  * Read data from a file. count must contain the requested read count
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1069)  * on entry. On return, *count contains the number of bytes actually read.
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1070)  * N.B. After this call fhp needs an fh_put
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1071)  */
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1072) __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400 1073) 	loff_t offset, struct kvec *vec, int vlen, unsigned long *count,
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400 1074) 	u32 *eof)
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1075) {
48cd7b51258c1 (Jeff Layton            2019-08-18 14:18:50 -0400 1076) 	struct nfsd_file	*nf;
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1077) 	struct file *file;
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1078) 	__be32 err;
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1079) 
f394b62b7b0dd (Chuck Lever            2018-03-27 10:53:11 -0400 1080) 	trace_nfsd_read_start(rqstp, fhp, offset, *count);
48cd7b51258c1 (Jeff Layton            2019-08-18 14:18:50 -0400 1081) 	err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1082) 	if (err)
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1083) 		return err;
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1084) 
48cd7b51258c1 (Jeff Layton            2019-08-18 14:18:50 -0400 1085) 	file = nf->nf_file;
a4058c5bce8ad (Christoph Hellwig      2017-05-27 11:16:54 +0300 1086) 	if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400 1087) 		err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof);
a4058c5bce8ad (Christoph Hellwig      2017-05-27 11:16:54 +0300 1088) 	else
83a63072c815e (Trond Myklebust        2019-08-26 13:03:11 -0400 1089) 		err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof);
6e8b50d16a757 (Jeff Layton            2015-11-17 06:52:23 -0500 1090) 
48cd7b51258c1 (Jeff Layton            2019-08-18 14:18:50 -0400 1091) 	nfsd_file_put(nf);
dc97618ddda9a (J. Bruce Fields        2014-03-18 17:01:51 -0400 1092) 
f394b62b7b0dd (Chuck Lever            2018-03-27 10:53:11 -0400 1093) 	trace_nfsd_read_done(rqstp, fhp, offset, *count);
6e8b50d16a757 (Jeff Layton            2015-11-17 06:52:23 -0500 1094) 
fa0a21269f807 (J. Bruce Fields        2010-07-27 16:48:54 -0400 1095) 	return err;
fa0a21269f807 (J. Bruce Fields        2010-07-27 16:48:54 -0400 1096) }
fa0a21269f807 (J. Bruce Fields        2010-07-27 16:48:54 -0400 1097) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1098) /*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1099)  * Write data to a file.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1100)  * The stable flag requests synchronous writes.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1101)  * N.B. After this call fhp needs an fh_put
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1102)  */
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1103) __be32
52e380e049d28 (Kinglong Mee           2016-12-31 21:00:13 +0800 1104) nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
19e0663ff9bce (Trond Myklebust        2020-01-06 13:40:37 -0500 1105) 	   struct kvec *vec, int vlen, unsigned long *cnt, int stable,
19e0663ff9bce (Trond Myklebust        2020-01-06 13:40:37 -0500 1106) 	   __be32 *verf)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1107) {
b493523926f9b (Jeff Layton            2019-08-18 14:18:49 -0400 1108) 	struct nfsd_file *nf;
b493523926f9b (Jeff Layton            2019-08-18 14:18:49 -0400 1109) 	__be32 err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1110) 
f394b62b7b0dd (Chuck Lever            2018-03-27 10:53:11 -0400 1111) 	trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
6e8b50d16a757 (Jeff Layton            2015-11-17 06:52:23 -0500 1112) 
b493523926f9b (Jeff Layton            2019-08-18 14:18:49 -0400 1113) 	err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf);
52e380e049d28 (Kinglong Mee           2016-12-31 21:00:13 +0800 1114) 	if (err)
52e380e049d28 (Kinglong Mee           2016-12-31 21:00:13 +0800 1115) 		goto out;
52e380e049d28 (Kinglong Mee           2016-12-31 21:00:13 +0800 1116) 
16f8f89410827 (Trond Myklebust        2020-01-06 13:40:29 -0500 1117) 	err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec,
19e0663ff9bce (Trond Myklebust        2020-01-06 13:40:37 -0500 1118) 			vlen, cnt, stable, verf);
b493523926f9b (Jeff Layton            2019-08-18 14:18:49 -0400 1119) 	nfsd_file_put(nf);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1120) out:
f394b62b7b0dd (Chuck Lever            2018-03-27 10:53:11 -0400 1121) 	trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1122) 	return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1123) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1124) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1125) #ifdef CONFIG_NFSD_V3
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1126) static int
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1127) nfsd_filemap_write_and_wait_range(struct nfsd_file *nf, loff_t offset,
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1128) 				  loff_t end)
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1129) {
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1130) 	struct address_space *mapping = nf->nf_file->f_mapping;
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1131) 	int ret = filemap_fdatawrite_range(mapping, offset, end);
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1132) 
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1133) 	if (ret)
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1134) 		return ret;
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1135) 	filemap_fdatawait_range_keep_errors(mapping, offset, end);
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1136) 	return 0;
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1137) }
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1138) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1139) /*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1140)  * Commit all pending writes to stable storage.
aa696a6f34963 (Trond Myklebust        2010-01-29 16:44:25 -0500 1141)  *
aa696a6f34963 (Trond Myklebust        2010-01-29 16:44:25 -0500 1142)  * Note: we only guarantee that data that lies within the range specified
aa696a6f34963 (Trond Myklebust        2010-01-29 16:44:25 -0500 1143)  * by the 'offset' and 'count' parameters will be synced.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1144)  *
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1145)  * Unfortunately we cannot lock the file to make sure we return full WCC
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1146)  * data to the client, as locking happens lower down in the filesystem.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1147)  */
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1148) __be32
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1149) nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
524ff1af226ce (Trond Myklebust        2020-01-06 13:40:36 -0500 1150)                loff_t offset, unsigned long count, __be32 *verf)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1151) {
5920afa3c85ff (Jeff Layton            2019-08-18 14:18:51 -0400 1152) 	struct nfsd_file	*nf;
5920afa3c85ff (Jeff Layton            2019-08-18 14:18:51 -0400 1153) 	loff_t			end = LLONG_MAX;
5920afa3c85ff (Jeff Layton            2019-08-18 14:18:51 -0400 1154) 	__be32			err = nfserr_inval;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1155) 
aa696a6f34963 (Trond Myklebust        2010-01-29 16:44:25 -0500 1156) 	if (offset < 0)
aa696a6f34963 (Trond Myklebust        2010-01-29 16:44:25 -0500 1157) 		goto out;
aa696a6f34963 (Trond Myklebust        2010-01-29 16:44:25 -0500 1158) 	if (count != 0) {
aa696a6f34963 (Trond Myklebust        2010-01-29 16:44:25 -0500 1159) 		end = offset + (loff_t)count - 1;
aa696a6f34963 (Trond Myklebust        2010-01-29 16:44:25 -0500 1160) 		if (end < offset)
aa696a6f34963 (Trond Myklebust        2010-01-29 16:44:25 -0500 1161) 			goto out;
aa696a6f34963 (Trond Myklebust        2010-01-29 16:44:25 -0500 1162) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1163) 
5920afa3c85ff (Jeff Layton            2019-08-18 14:18:51 -0400 1164) 	err = nfsd_file_acquire(rqstp, fhp,
5920afa3c85ff (Jeff Layton            2019-08-18 14:18:51 -0400 1165) 			NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &nf);
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 1166) 	if (err)
aa696a6f34963 (Trond Myklebust        2010-01-29 16:44:25 -0500 1167) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1168) 	if (EX_ISSYNC(fhp->fh_export)) {
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1169) 		int err2 = nfsd_filemap_write_and_wait_range(nf, offset, end);
aa696a6f34963 (Trond Myklebust        2010-01-29 16:44:25 -0500 1170) 
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1171) 		down_write(&nf->nf_rwsem);
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1172) 		if (!err2)
e8a8bb8dca84a (Trond Myklebust        2021-06-17 19:26:52 -0400 1173) 			err2 = vfs_fsync_range(nf->nf_file, offset, end, 0);
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1174) 		switch (err2) {
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1175) 		case 0:
524ff1af226ce (Trond Myklebust        2020-01-06 13:40:36 -0500 1176) 			nfsd_copy_boot_verifier(verf, net_generic(nf->nf_net,
524ff1af226ce (Trond Myklebust        2020-01-06 13:40:36 -0500 1177) 						nfsd_net_id));
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1178) 			break;
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1179) 		case -EINVAL:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1180) 			err = nfserr_notsupp;
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1181) 			break;
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1182) 		default:
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1183) 			err = nfserrno(err2);
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1184) 			nfsd_reset_boot_verifier(net_generic(nf->nf_net,
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1185) 						 nfsd_net_id));
bbf2f098838aa (Trond Myklebust        2019-09-02 13:02:58 -0400 1186) 		}
5011af4c698a9 (Trond Myklebust        2020-01-06 13:40:30 -0500 1187) 		up_write(&nf->nf_rwsem);
524ff1af226ce (Trond Myklebust        2020-01-06 13:40:36 -0500 1188) 	} else
524ff1af226ce (Trond Myklebust        2020-01-06 13:40:36 -0500 1189) 		nfsd_copy_boot_verifier(verf, net_generic(nf->nf_net,
524ff1af226ce (Trond Myklebust        2020-01-06 13:40:36 -0500 1190) 					nfsd_net_id));
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1191) 
5920afa3c85ff (Jeff Layton            2019-08-18 14:18:51 -0400 1192) 	nfsd_file_put(nf);
aa696a6f34963 (Trond Myklebust        2010-01-29 16:44:25 -0500 1193) out:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1194) 	return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1195) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1196) #endif /* CONFIG_NFSD_V3 */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1197) 
f2b0dee2ec8d5 (Adrian Bunk            2008-02-13 23:30:26 +0200 1198) static __be32
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1199) nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1200) 			struct iattr *iap)
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1201) {
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1202) 	/*
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1203) 	 * Mode has already been set earlier in create:
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1204) 	 */
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1205) 	iap->ia_valid &= ~ATTR_MODE;
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1206) 	/*
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1207) 	 * Setting uid/gid works only for root.  Irix appears to
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1208) 	 * send along the gid on create when it tries to implement
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1209) 	 * setgid directories via NFS:
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1210) 	 */
6fab877900030 (Eric W. Biederman      2013-02-02 06:53:11 -0800 1211) 	if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1212) 		iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1213) 	if (iap->ia_valid)
2a1aa48929af7 (Arnd Bergmann          2019-11-03 17:50:18 +0100 1214) 		return nfsd_setattr(rqstp, resfhp, iap, 0, (time64_t)0);
0f3a24b43bf75 (Trond Myklebust        2014-07-01 18:27:53 -0400 1215) 	/* Callers expect file metadata to be committed here */
722b620d1830f (Jeff Layton            2014-07-03 07:54:19 -0400 1216) 	return nfserrno(commit_metadata(resfhp));
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1217) }
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1218) 
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1219) /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1220)  * setting size to 0 may fail for some specific file systems by the permission
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1221)  * checking which requires WRITE permission but the mode is 000.
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1222)  * we ignore the resizing(to 0) on the just new created file, since the size is
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1223)  * 0 after file created.
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1224)  *
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1225)  * call this only after vfs_create() is called.
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1226)  * */
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1227) static void
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1228) nfsd_check_ignore_resizing(struct iattr *iap)
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1229) {
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1230) 	if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1231) 		iap->ia_valid &= ~ATTR_SIZE;
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1232) }
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1233) 
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1234) /* The parent directory should already be locked: */
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1235) __be32
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1236) nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1237) 		char *fname, int flen, struct iattr *iap,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1238) 		int type, dev_t rdev, struct svc_fh *resfhp)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1239) {
2b11885921a48 (Dan Carpenter          2016-08-03 22:05:00 +0300 1240) 	struct dentry	*dentry, *dchild;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1241) 	struct inode	*dirp;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1242) 	__be32		err;
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1243) 	__be32		err2;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1244) 	int		host_err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1245) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1246) 	dentry = fhp->fh_dentry;
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1247) 	dirp = d_inode(dentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1248) 
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1249) 	dchild = dget(resfhp->fh_dentry);
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1250) 	if (!fhp->fh_locked) {
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1251) 		WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
a6a9f18f0a9f9 (Al Viro                2013-09-16 10:57:01 -0400 1252) 				dentry);
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1253) 		err = nfserr_io;
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1254) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1255) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1256) 
7eed34f18d9f0 (Oleg Drokin            2016-07-14 23:20:22 -0400 1257) 	err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
7eed34f18d9f0 (Oleg Drokin            2016-07-14 23:20:22 -0400 1258) 	if (err)
7eed34f18d9f0 (Oleg Drokin            2016-07-14 23:20:22 -0400 1259) 		goto out;
7eed34f18d9f0 (Oleg Drokin            2016-07-14 23:20:22 -0400 1260) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1261) 	if (!(iap->ia_valid & ATTR_MODE))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1262) 		iap->ia_mode = 0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1263) 	iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1264) 
22cf8419f1319 (J. Bruce Fields        2020-06-16 16:43:18 -0400 1265) 	if (!IS_POSIXACL(dirp))
22cf8419f1319 (J. Bruce Fields        2020-06-16 16:43:18 -0400 1266) 		iap->ia_mode &= ~current_umask();
22cf8419f1319 (J. Bruce Fields        2020-06-16 16:43:18 -0400 1267) 
088406bcf66d6 (J. Bruce Fields        2006-11-08 17:44:59 -0800 1268) 	err = 0;
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1269) 	host_err = 0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1270) 	switch (type) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1271) 	case S_IFREG:
6521f89170829 (Christian Brauner      2021-01-21 14:19:33 +0100 1272) 		host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true);
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1273) 		if (!host_err)
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1274) 			nfsd_check_ignore_resizing(iap);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1275) 		break;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1276) 	case S_IFDIR:
6521f89170829 (Christian Brauner      2021-01-21 14:19:33 +0100 1277) 		host_err = vfs_mkdir(&init_user_ns, dirp, dchild, iap->ia_mode);
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1278) 		if (!host_err && unlikely(d_unhashed(dchild))) {
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1279) 			struct dentry *d;
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1280) 			d = lookup_one_len(dchild->d_name.name,
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1281) 					   dchild->d_parent,
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1282) 					   dchild->d_name.len);
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1283) 			if (IS_ERR(d)) {
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1284) 				host_err = PTR_ERR(d);
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1285) 				break;
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1286) 			}
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1287) 			if (unlikely(d_is_negative(d))) {
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1288) 				dput(d);
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1289) 				err = nfserr_serverfault;
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1290) 				goto out;
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1291) 			}
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1292) 			dput(resfhp->fh_dentry);
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1293) 			resfhp->fh_dentry = dget(d);
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1294) 			err = fh_update(resfhp);
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1295) 			dput(dchild);
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1296) 			dchild = d;
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1297) 			if (err)
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1298) 				goto out;
3819bb0d79f50 (Al Viro                2018-05-11 17:03:19 -0400 1299) 		}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1300) 		break;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1301) 	case S_IFCHR:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1302) 	case S_IFBLK:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1303) 	case S_IFIFO:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1304) 	case S_IFSOCK:
6521f89170829 (Christian Brauner      2021-01-21 14:19:33 +0100 1305) 		host_err = vfs_mknod(&init_user_ns, dirp, dchild,
6521f89170829 (Christian Brauner      2021-01-21 14:19:33 +0100 1306) 				     iap->ia_mode, rdev);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1307) 		break;
7142327449816 (J. Bruce Fields        2016-07-22 12:03:46 -0400 1308) 	default:
7142327449816 (J. Bruce Fields        2016-07-22 12:03:46 -0400 1309) 		printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
7142327449816 (J. Bruce Fields        2016-07-22 12:03:46 -0400 1310) 		       type);
7142327449816 (J. Bruce Fields        2016-07-22 12:03:46 -0400 1311) 		host_err = -EINVAL;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1312) 	}
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1313) 	if (host_err < 0)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1314) 		goto out_nfserr;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1315) 
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1316) 	err = nfsd_create_setattr(rqstp, resfhp, iap);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1317) 
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1318) 	/*
0f3a24b43bf75 (Trond Myklebust        2014-07-01 18:27:53 -0400 1319) 	 * nfsd_create_setattr already committed the child.  Transactional
0f3a24b43bf75 (Trond Myklebust        2014-07-01 18:27:53 -0400 1320) 	 * filesystems had a chance to commit changes for both parent and
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1321) 	 * child simultaneously making the following commit_metadata a
0f3a24b43bf75 (Trond Myklebust        2014-07-01 18:27:53 -0400 1322) 	 * noop.
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1323) 	 */
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1324) 	err2 = nfserrno(commit_metadata(fhp));
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1325) 	if (err2)
5c002b3bb294a (J. Bruce Fields        2007-11-30 16:55:23 -0500 1326) 		err = err2;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1327) 	/*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1328) 	 * Update the file handle to get the new inode info.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1329) 	 */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1330) 	if (!err)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1331) 		err = fh_update(resfhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1332) out:
2b11885921a48 (Dan Carpenter          2016-08-03 22:05:00 +0300 1333) 	dput(dchild);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1334) 	return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1335) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1336) out_nfserr:
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1337) 	err = nfserrno(host_err);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1338) 	goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1339) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1340) 
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1341) /*
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1342)  * Create a filesystem object (regular, directory, special).
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1343)  * Note that the parent directory is left locked.
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1344)  *
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1345)  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1346)  */
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1347) __be32
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1348) nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1349) 		char *fname, int flen, struct iattr *iap,
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1350) 		int type, dev_t rdev, struct svc_fh *resfhp)
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1351) {
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1352) 	struct dentry	*dentry, *dchild = NULL;
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1353) 	__be32		err;
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1354) 	int		host_err;
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1355) 
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1356) 	if (isdotent(fname, flen))
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1357) 		return nfserr_exist;
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1358) 
fa08139d5e028 (J. Bruce Fields        2016-07-21 16:00:12 -0400 1359) 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1360) 	if (err)
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1361) 		return err;
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1362) 
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1363) 	dentry = fhp->fh_dentry;
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1364) 
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1365) 	host_err = fh_want_write(fhp);
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1366) 	if (host_err)
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1367) 		return nfserrno(host_err);
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1368) 
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1369) 	fh_lock_nested(fhp, I_MUTEX_PARENT);
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1370) 	dchild = lookup_one_len(fname, dentry, flen);
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1371) 	host_err = PTR_ERR(dchild);
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1372) 	if (IS_ERR(dchild))
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1373) 		return nfserrno(host_err);
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1374) 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
502aa0a5be633 (Josef Bacik            2016-08-10 14:46:27 -0400 1375) 	/*
502aa0a5be633 (Josef Bacik            2016-08-10 14:46:27 -0400 1376) 	 * We unconditionally drop our ref to dchild as fh_compose will have
502aa0a5be633 (Josef Bacik            2016-08-10 14:46:27 -0400 1377) 	 * already grabbed its own ref for it.
502aa0a5be633 (Josef Bacik            2016-08-10 14:46:27 -0400 1378) 	 */
502aa0a5be633 (Josef Bacik            2016-08-10 14:46:27 -0400 1379) 	dput(dchild);
502aa0a5be633 (Josef Bacik            2016-08-10 14:46:27 -0400 1380) 	if (err)
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1381) 		return err;
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1382) 	return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1383) 					rdev, resfhp);
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1384) }
b44061d0b914c (J. Bruce Fields        2016-07-20 16:16:06 -0400 1385) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1386) #ifdef CONFIG_NFSD_V3
ac6721a13e5b1 (Mi Jinlong             2011-04-20 17:06:25 +0800 1387) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1388) /*
ac6721a13e5b1 (Mi Jinlong             2011-04-20 17:06:25 +0800 1389)  * NFSv3 and NFSv4 version of nfsd_create
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1390)  */
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1391) __be32
ac6721a13e5b1 (Mi Jinlong             2011-04-20 17:06:25 +0800 1392) do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1393) 		char *fname, int flen, struct iattr *iap,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1394) 		struct svc_fh *resfhp, int createmode, u32 *verifier,
856121b2e83bd (J. Bruce Fields        2011-10-13 11:37:11 -0400 1395) 	        bool *truncp, bool *created)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1396) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1397) 	struct dentry	*dentry, *dchild = NULL;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1398) 	struct inode	*dirp;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1399) 	__be32		err;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1400) 	int		host_err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1401) 	__u32		v_mtime=0, v_atime=0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1402) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1403) 	err = nfserr_perm;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1404) 	if (!flen)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1405) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1406) 	err = nfserr_exist;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1407) 	if (isdotent(fname, flen))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1408) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1409) 	if (!(iap->ia_valid & ATTR_MODE))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1410) 		iap->ia_mode = 0;
1574dff8996ab (Sachin Prabhu          2011-04-20 13:09:35 +0100 1411) 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1412) 	if (err)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1413) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1414) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1415) 	dentry = fhp->fh_dentry;
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1416) 	dirp = d_inode(dentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1417) 
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1418) 	host_err = fh_want_write(fhp);
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1419) 	if (host_err)
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1420) 		goto out_nfserr;
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1421) 
12fd352038c03 (Peter Zijlstra         2006-10-02 02:18:03 -0700 1422) 	fh_lock_nested(fhp, I_MUTEX_PARENT);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1423) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1424) 	/*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1425) 	 * Compose the response file handle.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1426) 	 */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1427) 	dchild = lookup_one_len(fname, dentry, flen);
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1428) 	host_err = PTR_ERR(dchild);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1429) 	if (IS_ERR(dchild))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1430) 		goto out_nfserr;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1431) 
1574dff8996ab (Sachin Prabhu          2011-04-20 13:09:35 +0100 1432) 	/* If file doesn't exist, check for permissions to create one */
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1433) 	if (d_really_is_negative(dchild)) {
1574dff8996ab (Sachin Prabhu          2011-04-20 13:09:35 +0100 1434) 		err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1574dff8996ab (Sachin Prabhu          2011-04-20 13:09:35 +0100 1435) 		if (err)
1574dff8996ab (Sachin Prabhu          2011-04-20 13:09:35 +0100 1436) 			goto out;
1574dff8996ab (Sachin Prabhu          2011-04-20 13:09:35 +0100 1437) 	}
1574dff8996ab (Sachin Prabhu          2011-04-20 13:09:35 +0100 1438) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1439) 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1440) 	if (err)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1441) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1442) 
ac6721a13e5b1 (Mi Jinlong             2011-04-20 17:06:25 +0800 1443) 	if (nfsd_create_is_exclusive(createmode)) {
c397852c3ddad (Peter Staubach         2007-01-26 00:57:00 -0800 1444) 		/* solaris7 gets confused (bugid 4218508) if these have
749997e5127a1 (Jeff Layton            2007-07-31 00:37:51 -0700 1445) 		 * the high bit set, so just clear the high bits. If this is
749997e5127a1 (Jeff Layton            2007-07-31 00:37:51 -0700 1446) 		 * ever changed to use different attrs for storing the
749997e5127a1 (Jeff Layton            2007-07-31 00:37:51 -0700 1447) 		 * verifier, then do_open_lookup() will also need to be fixed
749997e5127a1 (Jeff Layton            2007-07-31 00:37:51 -0700 1448) 		 * accordingly.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1449) 		 */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1450) 		v_mtime = verifier[0]&0x7fffffff;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1451) 		v_atime = verifier[1]&0x7fffffff;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1452) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1453) 	
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1454) 	if (d_really_is_positive(dchild)) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1455) 		err = 0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1456) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1457) 		switch (createmode) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1458) 		case NFS3_CREATE_UNCHECKED:
e36cb0b89ce20 (David Howells          2015-01-29 12:02:35 +0000 1459) 			if (! d_is_reg(dchild))
9dc4e6c4d1182 (J. Bruce Fields        2012-04-09 18:06:49 -0400 1460) 				goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1461) 			else if (truncp) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1462) 				/* in nfsv4, we need to treat this case a little
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1463) 				 * differently.  we don't want to truncate the
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1464) 				 * file now; this would be wrong if the OPEN
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1465) 				 * fails for some other reason.  furthermore,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1466) 				 * if the size is nonzero, we should ignore it
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1467) 				 * according to spec!
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1468) 				 */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1469) 				*truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1470) 			}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1471) 			else {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1472) 				iap->ia_valid &= ATTR_SIZE;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1473) 				goto set_attr;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1474) 			}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1475) 			break;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1476) 		case NFS3_CREATE_EXCLUSIVE:
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1477) 			if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1478) 			    && d_inode(dchild)->i_atime.tv_sec == v_atime
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1479) 			    && d_inode(dchild)->i_size  == 0 ) {
7007c90fb9fef (Neil Brown             2012-12-07 15:40:55 -0500 1480) 				if (created)
384a7ccaa37df (zhengbin               2019-12-25 11:19:34 +0800 1481) 					*created = true;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1482) 				break;
7007c90fb9fef (Neil Brown             2012-12-07 15:40:55 -0500 1483) 			}
df561f6688fef (Gustavo A. R. Silva    2020-08-23 17:36:59 -0500 1484) 			fallthrough;
ac6721a13e5b1 (Mi Jinlong             2011-04-20 17:06:25 +0800 1485) 		case NFS4_CREATE_EXCLUSIVE4_1:
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1486) 			if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1487) 			    && d_inode(dchild)->i_atime.tv_sec == v_atime
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1488) 			    && d_inode(dchild)->i_size  == 0 ) {
7007c90fb9fef (Neil Brown             2012-12-07 15:40:55 -0500 1489) 				if (created)
384a7ccaa37df (zhengbin               2019-12-25 11:19:34 +0800 1490) 					*created = true;
ac6721a13e5b1 (Mi Jinlong             2011-04-20 17:06:25 +0800 1491) 				goto set_attr;
7007c90fb9fef (Neil Brown             2012-12-07 15:40:55 -0500 1492) 			}
df561f6688fef (Gustavo A. R. Silva    2020-08-23 17:36:59 -0500 1493) 			fallthrough;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1494) 		case NFS3_CREATE_GUARDED:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1495) 			err = nfserr_exist;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1496) 		}
bad0dcffc21d1 (Al Viro                2011-11-23 12:03:18 -0500 1497) 		fh_drop_write(fhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1498) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1499) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1500) 
22cf8419f1319 (J. Bruce Fields        2020-06-16 16:43:18 -0400 1501) 	if (!IS_POSIXACL(dirp))
22cf8419f1319 (J. Bruce Fields        2020-06-16 16:43:18 -0400 1502) 		iap->ia_mode &= ~current_umask();
22cf8419f1319 (J. Bruce Fields        2020-06-16 16:43:18 -0400 1503) 
6521f89170829 (Christian Brauner      2021-01-21 14:19:33 +0100 1504) 	host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true);
463c3197263bd (Dave Hansen            2008-02-15 14:37:57 -0800 1505) 	if (host_err < 0) {
bad0dcffc21d1 (Al Viro                2011-11-23 12:03:18 -0500 1506) 		fh_drop_write(fhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1507) 		goto out_nfserr;
463c3197263bd (Dave Hansen            2008-02-15 14:37:57 -0800 1508) 	}
81ac95c5569d7 (J. Bruce Fields        2006-11-08 17:44:40 -0800 1509) 	if (created)
384a7ccaa37df (zhengbin               2019-12-25 11:19:34 +0800 1510) 		*created = true;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1511) 
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1512) 	nfsd_check_ignore_resizing(iap);
4ac35c2f79450 (wengang wang           2009-02-10 11:27:51 +0800 1513) 
ac6721a13e5b1 (Mi Jinlong             2011-04-20 17:06:25 +0800 1514) 	if (nfsd_create_is_exclusive(createmode)) {
c397852c3ddad (Peter Staubach         2007-01-26 00:57:00 -0800 1515) 		/* Cram the verifier into atime/mtime */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1516) 		iap->ia_valid = ATTR_MTIME|ATTR_ATIME
c397852c3ddad (Peter Staubach         2007-01-26 00:57:00 -0800 1517) 			| ATTR_MTIME_SET|ATTR_ATIME_SET;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1518) 		/* XXX someone who knows this better please fix it for nsec */ 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1519) 		iap->ia_mtime.tv_sec = v_mtime;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1520) 		iap->ia_atime.tv_sec = v_atime;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1521) 		iap->ia_mtime.tv_nsec = 0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1522) 		iap->ia_atime.tv_nsec = 0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1523) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1524) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1525)  set_attr:
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1526) 	err = nfsd_create_setattr(rqstp, resfhp, iap);
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1527) 
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1528) 	/*
0f3a24b43bf75 (Trond Myklebust        2014-07-01 18:27:53 -0400 1529) 	 * nfsd_create_setattr already committed the child
0f3a24b43bf75 (Trond Myklebust        2014-07-01 18:27:53 -0400 1530) 	 * (and possibly also the parent).
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1531) 	 */
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1532) 	if (!err)
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1533) 		err = nfserrno(commit_metadata(fhp));
f193fbab2e471 (YAMAMOTO Takashi       2006-01-18 17:43:13 -0800 1534) 
f193fbab2e471 (YAMAMOTO Takashi       2006-01-18 17:43:13 -0800 1535) 	/*
f193fbab2e471 (YAMAMOTO Takashi       2006-01-18 17:43:13 -0800 1536) 	 * Update the filehandle to get the new inode info.
f193fbab2e471 (YAMAMOTO Takashi       2006-01-18 17:43:13 -0800 1537) 	 */
f193fbab2e471 (YAMAMOTO Takashi       2006-01-18 17:43:13 -0800 1538) 	if (!err)
f193fbab2e471 (YAMAMOTO Takashi       2006-01-18 17:43:13 -0800 1539) 		err = fh_update(resfhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1540) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1541)  out:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1542) 	fh_unlock(fhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1543) 	if (dchild && !IS_ERR(dchild))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1544) 		dput(dchild);
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1545) 	fh_drop_write(fhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1546)  	return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1547)  
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1548)  out_nfserr:
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1549) 	err = nfserrno(host_err);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1550) 	goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1551) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1552) #endif /* CONFIG_NFSD_V3 */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1553) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1554) /*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1555)  * Read a symlink. On entry, *lenp must contain the maximum path length that
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1556)  * fits into the buffer. On return, it contains the true length.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1557)  * N.B. After this call fhp needs an fh_put
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1558)  */
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1559) __be32
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1560) nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1561) {
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1562) 	__be32		err;
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1563) 	const char *link;
68ac1234fb949 (Al Viro                2012-03-15 08:21:57 -0400 1564) 	struct path path;
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1565) 	DEFINE_DELAYED_CALL(done);
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1566) 	int len;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1567) 
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 1568) 	err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1569) 	if (unlikely(err))
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1570) 		return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1571) 
68ac1234fb949 (Al Viro                2012-03-15 08:21:57 -0400 1572) 	path.mnt = fhp->fh_export->ex_path.mnt;
68ac1234fb949 (Al Viro                2012-03-15 08:21:57 -0400 1573) 	path.dentry = fhp->fh_dentry;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1574) 
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1575) 	if (unlikely(!d_is_symlink(path.dentry)))
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1576) 		return nfserr_inval;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1577) 
68ac1234fb949 (Al Viro                2012-03-15 08:21:57 -0400 1578) 	touch_atime(&path);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1579) 
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1580) 	link = vfs_get_link(path.dentry, &done);
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1581) 	if (IS_ERR(link))
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1582) 		return nfserrno(PTR_ERR(link));
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1583) 
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1584) 	len = strlen(link);
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1585) 	if (len < *lenp)
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1586) 		*lenp = len;
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1587) 	memcpy(buf, link, *lenp);
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1588) 	do_delayed_call(&done);
4d7edbc34cccf (Al Viro                2017-05-27 16:11:23 -0400 1589) 	return 0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1590) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1591) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1592) /*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1593)  * Create a symlink and look up its inode
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1594)  * N.B. After this call _both_ fhp and resfhp need an fh_put
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1595)  */
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1596) __be32
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1597) nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1598) 				char *fname, int flen,
52ee04330f585 (J. Bruce Fields        2014-06-20 11:52:21 -0400 1599) 				char *path,
1e444f5bc0c46 (Kinglong Mee           2014-07-01 17:48:02 +0800 1600) 				struct svc_fh *resfhp)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1601) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1602) 	struct dentry	*dentry, *dnew;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1603) 	__be32		err, cerr;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1604) 	int		host_err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1605) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1606) 	err = nfserr_noent;
52ee04330f585 (J. Bruce Fields        2014-06-20 11:52:21 -0400 1607) 	if (!flen || path[0] == '\0')
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1608) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1609) 	err = nfserr_exist;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1610) 	if (isdotent(fname, flen))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1611) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1612) 
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 1613) 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1614) 	if (err)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1615) 		goto out;
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1616) 
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1617) 	host_err = fh_want_write(fhp);
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1618) 	if (host_err)
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1619) 		goto out_nfserr;
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1620) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1621) 	fh_lock(fhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1622) 	dentry = fhp->fh_dentry;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1623) 	dnew = lookup_one_len(fname, dentry, flen);
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1624) 	host_err = PTR_ERR(dnew);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1625) 	if (IS_ERR(dnew))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1626) 		goto out_nfserr;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1627) 
6521f89170829 (Christian Brauner      2021-01-21 14:19:33 +0100 1628) 	host_err = vfs_symlink(&init_user_ns, d_inode(dentry), dnew, path);
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1629) 	err = nfserrno(host_err);
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1630) 	if (!err)
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1631) 		err = nfserrno(commit_metadata(fhp));
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1632) 	fh_unlock(fhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1633) 
bad0dcffc21d1 (Al Viro                2011-11-23 12:03:18 -0500 1634) 	fh_drop_write(fhp);
75c3f29de7451 (Dave Hansen            2008-02-15 14:37:45 -0800 1635) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1636) 	cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1637) 	dput(dnew);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1638) 	if (err==0) err = cerr;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1639) out:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1640) 	return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1641) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1642) out_nfserr:
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1643) 	err = nfserrno(host_err);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1644) 	goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1645) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1646) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1647) /*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1648)  * Create a hardlink
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1649)  * N.B. After this call _both_ ffhp and tfhp need an fh_put
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1650)  */
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1651) __be32
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1652) nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1653) 				char *name, int len, struct svc_fh *tfhp)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1654) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1655) 	struct dentry	*ddir, *dnew, *dold;
55b13354d789d (J. Bruce Fields        2010-07-19 16:38:24 -0400 1656) 	struct inode	*dirp;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1657) 	__be32		err;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1658) 	int		host_err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1659) 
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 1660) 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1661) 	if (err)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1662) 		goto out;
7d818a7b8fc8d (J. Bruce Fields        2011-08-15 16:59:55 -0400 1663) 	err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1664) 	if (err)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1665) 		goto out;
7d818a7b8fc8d (J. Bruce Fields        2011-08-15 16:59:55 -0400 1666) 	err = nfserr_isdir;
e36cb0b89ce20 (David Howells          2015-01-29 12:02:35 +0000 1667) 	if (d_is_dir(tfhp->fh_dentry))
7d818a7b8fc8d (J. Bruce Fields        2011-08-15 16:59:55 -0400 1668) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1669) 	err = nfserr_perm;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1670) 	if (!len)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1671) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1672) 	err = nfserr_exist;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1673) 	if (isdotent(name, len))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1674) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1675) 
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1676) 	host_err = fh_want_write(tfhp);
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1677) 	if (host_err) {
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1678) 		err = nfserrno(host_err);
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1679) 		goto out;
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1680) 	}
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1681) 
12fd352038c03 (Peter Zijlstra         2006-10-02 02:18:03 -0700 1682) 	fh_lock_nested(ffhp, I_MUTEX_PARENT);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1683) 	ddir = ffhp->fh_dentry;
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1684) 	dirp = d_inode(ddir);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1685) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1686) 	dnew = lookup_one_len(name, ddir, len);
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1687) 	host_err = PTR_ERR(dnew);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1688) 	if (IS_ERR(dnew))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1689) 		goto out_nfserr;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1690) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1691) 	dold = tfhp->fh_dentry;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1692) 
4795bb37effb7 (J. Bruce Fields        2011-01-11 13:55:46 -0500 1693) 	err = nfserr_noent;
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1694) 	if (d_really_is_negative(dold))
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1695) 		goto out_dput;
6521f89170829 (Christian Brauner      2021-01-21 14:19:33 +0100 1696) 	host_err = vfs_link(dold, &init_user_ns, dirp, dnew, NULL);
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1697) 	if (!host_err) {
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1698) 		err = nfserrno(commit_metadata(ffhp));
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1699) 		if (!err)
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1700) 			err = nfserrno(commit_metadata(tfhp));
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1701) 	} else {
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1702) 		if (host_err == -EXDEV && rqstp->rq_vers == 2)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1703) 			err = nfserr_acces;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1704) 		else
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1705) 			err = nfserrno(host_err);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1706) 	}
75c3f29de7451 (Dave Hansen            2008-02-15 14:37:45 -0800 1707) out_dput:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1708) 	dput(dnew);
270d56e536dcd (David M. Richter       2006-06-30 01:56:15 -0700 1709) out_unlock:
270d56e536dcd (David M. Richter       2006-06-30 01:56:15 -0700 1710) 	fh_unlock(ffhp);
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1711) 	fh_drop_write(tfhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1712) out:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1713) 	return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1714) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1715) out_nfserr:
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1716) 	err = nfserrno(host_err);
270d56e536dcd (David M. Richter       2006-06-30 01:56:15 -0700 1717) 	goto out_unlock;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1718) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1719) 
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1720) static void
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1721) nfsd_close_cached_files(struct dentry *dentry)
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1722) {
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1723) 	struct inode *inode = d_inode(dentry);
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1724) 
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1725) 	if (inode && S_ISREG(inode->i_mode))
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1726) 		nfsd_file_close_inode_sync(inode);
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1727) }
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1728) 
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1729) static bool
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1730) nfsd_has_cached_files(struct dentry *dentry)
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1731) {
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1732) 	bool		ret = false;
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1733) 	struct inode *inode = d_inode(dentry);
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1734) 
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1735) 	if (inode && S_ISREG(inode->i_mode))
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1736) 		ret = nfsd_file_is_cached(inode);
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1737) 	return ret;
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1738) }
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1739) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1740) /*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1741)  * Rename a file
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1742)  * N.B. After this call _both_ ffhp and tfhp need an fh_put
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1743)  */
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1744) __be32
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1745) nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1746) 			    struct svc_fh *tfhp, char *tname, int tlen)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1747) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1748) 	struct dentry	*fdentry, *tdentry, *odentry, *ndentry, *trap;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1749) 	struct inode	*fdir, *tdir;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1750) 	__be32		err;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1751) 	int		host_err;
7f84b488f9add (Jeff Layton            2020-11-30 17:03:16 -0500 1752) 	bool		close_cached = false;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1753) 
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 1754) 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1755) 	if (err)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1756) 		goto out;
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 1757) 	err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1758) 	if (err)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1759) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1760) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1761) 	fdentry = ffhp->fh_dentry;
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1762) 	fdir = d_inode(fdentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1763) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1764) 	tdentry = tfhp->fh_dentry;
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1765) 	tdir = d_inode(tdentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1766) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1767) 	err = nfserr_perm;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1768) 	if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1769) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1770) 
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1771) retry:
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1772) 	host_err = fh_want_write(ffhp);
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1773) 	if (host_err) {
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1774) 		err = nfserrno(host_err);
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1775) 		goto out;
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1776) 	}
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1777) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1778) 	/* cannot use fh_lock as we need deadlock protective ordering
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1779) 	 * so do it by hand */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1780) 	trap = lock_rename(tdentry, fdentry);
aaf91ec148910 (Jeff Layton            2015-09-17 08:28:39 -0400 1781) 	ffhp->fh_locked = tfhp->fh_locked = true;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1782) 	fill_pre_wcc(ffhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1783) 	fill_pre_wcc(tfhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1784) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1785) 	odentry = lookup_one_len(fname, fdentry, flen);
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1786) 	host_err = PTR_ERR(odentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1787) 	if (IS_ERR(odentry))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1788) 		goto out_nfserr;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1789) 
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1790) 	host_err = -ENOENT;
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1791) 	if (d_really_is_negative(odentry))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1792) 		goto out_dput_old;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1793) 	host_err = -EINVAL;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1794) 	if (odentry == trap)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1795) 		goto out_dput_old;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1796) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1797) 	ndentry = lookup_one_len(tname, tdentry, tlen);
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1798) 	host_err = PTR_ERR(ndentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1799) 	if (IS_ERR(ndentry))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1800) 		goto out_dput_old;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1801) 	host_err = -ENOTEMPTY;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1802) 	if (ndentry == trap)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1803) 		goto out_dput_new;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1804) 
9079b1eb1753f (Dave Hansen            2008-02-15 14:37:49 -0800 1805) 	host_err = -EXDEV;
9079b1eb1753f (Dave Hansen            2008-02-15 14:37:49 -0800 1806) 	if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
9079b1eb1753f (Dave Hansen            2008-02-15 14:37:49 -0800 1807) 		goto out_dput_new;
aa387d6ce1533 (J. Bruce Fields        2013-04-15 16:03:46 -0400 1808) 	if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
aa387d6ce1533 (J. Bruce Fields        2013-04-15 16:03:46 -0400 1809) 		goto out_dput_new;
9079b1eb1753f (Dave Hansen            2008-02-15 14:37:49 -0800 1810) 
7f84b488f9add (Jeff Layton            2020-11-30 17:03:16 -0500 1811) 	if ((ndentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK) &&
7f84b488f9add (Jeff Layton            2020-11-30 17:03:16 -0500 1812) 	    nfsd_has_cached_files(ndentry)) {
7f84b488f9add (Jeff Layton            2020-11-30 17:03:16 -0500 1813) 		close_cached = true;
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1814) 		goto out_dput_old;
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1815) 	} else {
9fe61450972d3 (Christian Brauner      2021-01-21 14:19:32 +0100 1816) 		struct renamedata rd = {
6521f89170829 (Christian Brauner      2021-01-21 14:19:33 +0100 1817) 			.old_mnt_userns	= &init_user_ns,
9fe61450972d3 (Christian Brauner      2021-01-21 14:19:32 +0100 1818) 			.old_dir	= fdir,
9fe61450972d3 (Christian Brauner      2021-01-21 14:19:32 +0100 1819) 			.old_dentry	= odentry,
6521f89170829 (Christian Brauner      2021-01-21 14:19:33 +0100 1820) 			.new_mnt_userns	= &init_user_ns,
9fe61450972d3 (Christian Brauner      2021-01-21 14:19:32 +0100 1821) 			.new_dir	= tdir,
9fe61450972d3 (Christian Brauner      2021-01-21 14:19:32 +0100 1822) 			.new_dentry	= ndentry,
9fe61450972d3 (Christian Brauner      2021-01-21 14:19:32 +0100 1823) 		};
9fe61450972d3 (Christian Brauner      2021-01-21 14:19:32 +0100 1824) 		host_err = vfs_rename(&rd);
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1825) 		if (!host_err) {
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1826) 			host_err = commit_metadata(tfhp);
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1827) 			if (!host_err)
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1828) 				host_err = commit_metadata(ffhp);
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1829) 		}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1830) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1831)  out_dput_new:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1832) 	dput(ndentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1833)  out_dput_old:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1834) 	dput(odentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1835)  out_nfserr:
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1836) 	err = nfserrno(host_err);
fbb74a34a5b04 (J. Bruce Fields        2014-03-28 16:43:17 -0400 1837) 	/*
fbb74a34a5b04 (J. Bruce Fields        2014-03-28 16:43:17 -0400 1838) 	 * We cannot rely on fh_unlock on the two filehandles,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1839) 	 * as that would do the wrong thing if the two directories
fbb74a34a5b04 (J. Bruce Fields        2014-03-28 16:43:17 -0400 1840) 	 * were the same, so again we do it by hand.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1841) 	 */
7f84b488f9add (Jeff Layton            2020-11-30 17:03:16 -0500 1842) 	if (!close_cached) {
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1843) 		fill_post_wcc(ffhp);
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1844) 		fill_post_wcc(tfhp);
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1845) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1846) 	unlock_rename(tdentry, fdentry);
aaf91ec148910 (Jeff Layton            2015-09-17 08:28:39 -0400 1847) 	ffhp->fh_locked = tfhp->fh_locked = false;
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1848) 	fh_drop_write(ffhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1849) 
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1850) 	/*
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1851) 	 * If the target dentry has cached open files, then we need to try to
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1852) 	 * close them prior to doing the rename. Flushing delayed fput
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1853) 	 * shouldn't be done with locks held however, so we delay it until this
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1854) 	 * point and then reattempt the whole shebang.
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1855) 	 */
7f84b488f9add (Jeff Layton            2020-11-30 17:03:16 -0500 1856) 	if (close_cached) {
7f84b488f9add (Jeff Layton            2020-11-30 17:03:16 -0500 1857) 		close_cached = false;
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1858) 		nfsd_close_cached_files(ndentry);
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1859) 		dput(ndentry);
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1860) 		goto retry;
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1861) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1862) out:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1863) 	return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1864) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1865) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1866) /*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1867)  * Unlink a file or directory
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1868)  * N.B. After this call fhp needs an fh_put
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1869)  */
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1870) __be32
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1871) nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1872) 				char *fname, int flen)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1873) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1874) 	struct dentry	*dentry, *rdentry;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1875) 	struct inode	*dirp;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1876) 	__be32		err;
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1877) 	int		host_err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1878) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1879) 	err = nfserr_acces;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1880) 	if (!flen || isdotent(fname, flen))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1881) 		goto out;
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 1882) 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1883) 	if (err)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1884) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1885) 
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1886) 	host_err = fh_want_write(fhp);
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1887) 	if (host_err)
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1888) 		goto out_nfserr;
4a55c1017b8dc (Jan Kara               2012-06-12 16:20:33 +0200 1889) 
12fd352038c03 (Peter Zijlstra         2006-10-02 02:18:03 -0700 1890) 	fh_lock_nested(fhp, I_MUTEX_PARENT);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1891) 	dentry = fhp->fh_dentry;
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1892) 	dirp = d_inode(dentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1893) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1894) 	rdentry = lookup_one_len(fname, dentry, flen);
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 1895) 	host_err = PTR_ERR(rdentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1896) 	if (IS_ERR(rdentry))
0ca0c9d7edcf4 (J. Bruce Fields        2019-04-12 16:26:30 -0400 1897) 		goto out_drop_write;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1898) 
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1899) 	if (d_really_is_negative(rdentry)) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1900) 		dput(rdentry);
0ca0c9d7edcf4 (J. Bruce Fields        2019-04-12 16:26:30 -0400 1901) 		host_err = -ENOENT;
0ca0c9d7edcf4 (J. Bruce Fields        2019-04-12 16:26:30 -0400 1902) 		goto out_drop_write;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1903) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1904) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1905) 	if (!type)
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 1906) 		type = d_inode(rdentry)->i_mode & S_IFMT;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1907) 
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1908) 	if (type != S_IFDIR) {
7f84b488f9add (Jeff Layton            2020-11-30 17:03:16 -0500 1909) 		if (rdentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK)
7f84b488f9add (Jeff Layton            2020-11-30 17:03:16 -0500 1910) 			nfsd_close_cached_files(rdentry);
6521f89170829 (Christian Brauner      2021-01-21 14:19:33 +0100 1911) 		host_err = vfs_unlink(&init_user_ns, dirp, rdentry, NULL);
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1912) 	} else {
6521f89170829 (Christian Brauner      2021-01-21 14:19:33 +0100 1913) 		host_err = vfs_rmdir(&init_user_ns, dirp, rdentry);
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1914) 	}
7775ec57f4c71 (Jeff Layton            2019-08-18 14:18:57 -0400 1915) 
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1916) 	if (!host_err)
f501912a35c02 (Ben Myers              2010-02-17 14:05:11 -0600 1917) 		host_err = commit_metadata(fhp);
541ce98c10111 (J. Bruce Fields        2011-01-14 20:00:02 -0500 1918) 	dput(rdentry);
541ce98c10111 (J. Bruce Fields        2011-01-14 20:00:02 -0500 1919) 
0ca0c9d7edcf4 (J. Bruce Fields        2019-04-12 16:26:30 -0400 1920) out_drop_write:
0ca0c9d7edcf4 (J. Bruce Fields        2019-04-12 16:26:30 -0400 1921) 	fh_drop_write(fhp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1922) out_nfserr:
466e16f0920f3 (NeilBrown              2019-11-28 13:56:43 +1100 1923) 	if (host_err == -EBUSY) {
466e16f0920f3 (NeilBrown              2019-11-28 13:56:43 +1100 1924) 		/* name is mounted-on. There is no perfect
466e16f0920f3 (NeilBrown              2019-11-28 13:56:43 +1100 1925) 		 * error status.
466e16f0920f3 (NeilBrown              2019-11-28 13:56:43 +1100 1926) 		 */
466e16f0920f3 (NeilBrown              2019-11-28 13:56:43 +1100 1927) 		if (nfsd_v4client(rqstp))
466e16f0920f3 (NeilBrown              2019-11-28 13:56:43 +1100 1928) 			err = nfserr_file_open;
466e16f0920f3 (NeilBrown              2019-11-28 13:56:43 +1100 1929) 		else
466e16f0920f3 (NeilBrown              2019-11-28 13:56:43 +1100 1930) 			err = nfserr_acces;
466e16f0920f3 (NeilBrown              2019-11-28 13:56:43 +1100 1931) 	} else {
466e16f0920f3 (NeilBrown              2019-11-28 13:56:43 +1100 1932) 		err = nfserrno(host_err);
466e16f0920f3 (NeilBrown              2019-11-28 13:56:43 +1100 1933) 	}
f193fbab2e471 (YAMAMOTO Takashi       2006-01-18 17:43:13 -0800 1934) out:
f193fbab2e471 (YAMAMOTO Takashi       2006-01-18 17:43:13 -0800 1935) 	return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1936) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 1937) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1938) /*
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1939)  * We do this buffering because we must not call back into the file
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1940)  * system's ->lookup() method from the filldir callback. That may well
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1941)  * deadlock a number of file systems.
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1942)  *
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1943)  * This is based heavily on the implementation of same in XFS.
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1944)  */
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1945) struct buffered_dirent {
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1946) 	u64		ino;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1947) 	loff_t		offset;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1948) 	int		namlen;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1949) 	unsigned int	d_type;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1950) 	char		name[];
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1951) };
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1952) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1953) struct readdir_data {
5c0ba4e0762e6 (Al Viro                2013-05-15 13:52:59 -0400 1954) 	struct dir_context ctx;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1955) 	char		*dirent;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1956) 	size_t		used;
53c9c5c0e32c6 (Al Viro                2008-08-24 07:29:52 -0400 1957) 	int		full;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1958) };
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1959) 
ac7576f4b1da8 (Miklos Szeredi         2014-10-30 17:37:34 +0100 1960) static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
ac7576f4b1da8 (Miklos Szeredi         2014-10-30 17:37:34 +0100 1961) 				 int namlen, loff_t offset, u64 ino,
ac7576f4b1da8 (Miklos Szeredi         2014-10-30 17:37:34 +0100 1962) 				 unsigned int d_type)
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1963) {
ac7576f4b1da8 (Miklos Szeredi         2014-10-30 17:37:34 +0100 1964) 	struct readdir_data *buf =
ac7576f4b1da8 (Miklos Szeredi         2014-10-30 17:37:34 +0100 1965) 		container_of(ctx, struct readdir_data, ctx);
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1966) 	struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1967) 	unsigned int reclen;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1968) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1969) 	reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
53c9c5c0e32c6 (Al Viro                2008-08-24 07:29:52 -0400 1970) 	if (buf->used + reclen > PAGE_SIZE) {
53c9c5c0e32c6 (Al Viro                2008-08-24 07:29:52 -0400 1971) 		buf->full = 1;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1972) 		return -EINVAL;
53c9c5c0e32c6 (Al Viro                2008-08-24 07:29:52 -0400 1973) 	}
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1974) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1975) 	de->namlen = namlen;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1976) 	de->offset = offset;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1977) 	de->ino = ino;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1978) 	de->d_type = d_type;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1979) 	memcpy(de->name, name, namlen);
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1980) 	buf->used += reclen;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1981) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1982) 	return 0;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1983) }
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1984) 
6019ce0742ca5 (Chuck Lever            2021-03-05 13:57:40 -0500 1985) static __be32 nfsd_buffered_readdir(struct file *file, struct svc_fh *fhp,
6019ce0742ca5 (Chuck Lever            2021-03-05 13:57:40 -0500 1986) 				    nfsd_filldir_t func, struct readdir_cd *cdp,
6019ce0742ca5 (Chuck Lever            2021-03-05 13:57:40 -0500 1987) 				    loff_t *offsetp)
2628b766363aa (David Woodhouse        2008-07-31 17:16:51 +0100 1988) {
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1989) 	struct buffered_dirent *de;
2628b766363aa (David Woodhouse        2008-07-31 17:16:51 +0100 1990) 	int host_err;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1991) 	int size;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1992) 	loff_t offset;
ac6614b76478e (Al Viro                2013-05-22 22:22:04 -0400 1993) 	struct readdir_data buf = {
ac6614b76478e (Al Viro                2013-05-22 22:22:04 -0400 1994) 		.ctx.actor = nfsd_buffered_filldir,
ac6614b76478e (Al Viro                2013-05-22 22:22:04 -0400 1995) 		.dirent = (void *)__get_free_page(GFP_KERNEL)
ac6614b76478e (Al Viro                2013-05-22 22:22:04 -0400 1996) 	};
2628b766363aa (David Woodhouse        2008-07-31 17:16:51 +0100 1997) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 1998) 	if (!buf.dirent)
2f9092e102024 (David Woodhouse        2009-04-20 23:18:37 +0100 1999) 		return nfserrno(-ENOMEM);
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2000) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2001) 	offset = *offsetp;
2628b766363aa (David Woodhouse        2008-07-31 17:16:51 +0100 2002) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2003) 	while (1) {
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2004) 		unsigned int reclen;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2005) 
b726e923ea4d2 (Doug Nazar             2008-11-05 06:16:28 -0500 2006) 		cdp->err = nfserr_eof; /* will be cleared on successful read */
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2007) 		buf.used = 0;
53c9c5c0e32c6 (Al Viro                2008-08-24 07:29:52 -0400 2008) 		buf.full = 0;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2009) 
5c0ba4e0762e6 (Al Viro                2013-05-15 13:52:59 -0400 2010) 		host_err = iterate_dir(file, &buf.ctx);
53c9c5c0e32c6 (Al Viro                2008-08-24 07:29:52 -0400 2011) 		if (buf.full)
53c9c5c0e32c6 (Al Viro                2008-08-24 07:29:52 -0400 2012) 			host_err = 0;
53c9c5c0e32c6 (Al Viro                2008-08-24 07:29:52 -0400 2013) 
53c9c5c0e32c6 (Al Viro                2008-08-24 07:29:52 -0400 2014) 		if (host_err < 0)
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2015) 			break;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2016) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2017) 		size = buf.used;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2018) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2019) 		if (!size)
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2020) 			break;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2021) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2022) 		de = (struct buffered_dirent *)buf.dirent;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2023) 		while (size > 0) {
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2024) 			offset = de->offset;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2025) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2026) 			if (func(cdp, de->name, de->namlen, de->offset,
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2027) 				 de->ino, de->d_type))
2f9092e102024 (David Woodhouse        2009-04-20 23:18:37 +0100 2028) 				break;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2029) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2030) 			if (cdp->err != nfs_ok)
2f9092e102024 (David Woodhouse        2009-04-20 23:18:37 +0100 2031) 				break;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2032) 
6019ce0742ca5 (Chuck Lever            2021-03-05 13:57:40 -0500 2033) 			trace_nfsd_dirent(fhp, de->ino, de->name, de->namlen);
6019ce0742ca5 (Chuck Lever            2021-03-05 13:57:40 -0500 2034) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2035) 			reclen = ALIGN(sizeof(*de) + de->namlen,
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2036) 				       sizeof(u64));
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2037) 			size -= reclen;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2038) 			de = (struct buffered_dirent *)((char *)de + reclen);
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2039) 		}
2f9092e102024 (David Woodhouse        2009-04-20 23:18:37 +0100 2040) 		if (size > 0) /* We bailed out early */
2f9092e102024 (David Woodhouse        2009-04-20 23:18:37 +0100 2041) 			break;
2f9092e102024 (David Woodhouse        2009-04-20 23:18:37 +0100 2042) 
c002a6c797732 (David Woodhouse        2008-08-17 17:21:18 +0100 2043) 		offset = vfs_llseek(file, 0, SEEK_CUR);
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2044) 	}
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2045) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2046) 	free_page((unsigned long)(buf.dirent));
2628b766363aa (David Woodhouse        2008-07-31 17:16:51 +0100 2047) 
2628b766363aa (David Woodhouse        2008-07-31 17:16:51 +0100 2048) 	if (host_err)
2628b766363aa (David Woodhouse        2008-07-31 17:16:51 +0100 2049) 		return nfserrno(host_err);
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2050) 
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2051) 	*offsetp = offset;
14f7dd632011b (David Woodhouse        2008-07-31 20:29:12 +0100 2052) 	return cdp->err;
2628b766363aa (David Woodhouse        2008-07-31 17:16:51 +0100 2053) }
2628b766363aa (David Woodhouse        2008-07-31 17:16:51 +0100 2054) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2055) /*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2056)  * Read entries from a directory.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2057)  * The  NFSv3/4 verifier we ignore for now.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2058)  */
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 2059) __be32
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2060) nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp, 
ac7576f4b1da8 (Miklos Szeredi         2014-10-30 17:37:34 +0100 2061) 	     struct readdir_cd *cdp, nfsd_filldir_t func)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2062) {
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 2063) 	__be32		err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2064) 	struct file	*file;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2065) 	loff_t		offset = *offsetp;
06effdbb49af5 (Bernd Schubert         2012-03-18 22:44:50 -0400 2066) 	int             may_flags = NFSD_MAY_READ;
06effdbb49af5 (Bernd Schubert         2012-03-18 22:44:50 -0400 2067) 
06effdbb49af5 (Bernd Schubert         2012-03-18 22:44:50 -0400 2068) 	/* NFSv2 only supports 32 bit cookies */
06effdbb49af5 (Bernd Schubert         2012-03-18 22:44:50 -0400 2069) 	if (rqstp->rq_vers > 2)
06effdbb49af5 (Bernd Schubert         2012-03-18 22:44:50 -0400 2070) 		may_flags |= NFSD_MAY_64BIT_COOKIE;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2071) 
06effdbb49af5 (Bernd Schubert         2012-03-18 22:44:50 -0400 2072) 	err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2073) 	if (err)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2074) 		goto out;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2075) 
b108fe6b08f3f (Jeff Layton            2012-04-25 15:30:00 -0400 2076) 	offset = vfs_llseek(file, offset, SEEK_SET);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2077) 	if (offset < 0) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2078) 		err = nfserrno((int)offset);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2079) 		goto out_close;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2080) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2081) 
6019ce0742ca5 (Chuck Lever            2021-03-05 13:57:40 -0500 2082) 	err = nfsd_buffered_readdir(file, fhp, func, cdp, offsetp);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2083) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2084) 	if (err == nfserr_eof || err == nfserr_toosmall)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2085) 		err = nfs_ok; /* can still be found in ->err */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2086) out_close:
fd891454609ec (Christoph Hellwig      2015-04-28 15:41:16 +0200 2087) 	fput(file);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2088) out:
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2089) 	return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2090) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2091) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2092) /*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2093)  * Get file system stats
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2094)  * N.B. After this call fhp needs an fh_put
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2095)  */
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 2096) __be32
04716e6621ff4 (J. Bruce Fields        2008-08-07 13:00:20 -0400 2097) nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2098) {
ebabe9a9001af (Christoph Hellwig      2010-07-07 18:53:11 +0200 2099) 	__be32 err;
ebabe9a9001af (Christoph Hellwig      2010-07-07 18:53:11 +0200 2100) 
ebabe9a9001af (Christoph Hellwig      2010-07-07 18:53:11 +0200 2101) 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
f6360efb83cd6 (Takashi Iwai           2010-08-13 15:53:49 +0200 2102) 	if (!err) {
f6360efb83cd6 (Takashi Iwai           2010-08-13 15:53:49 +0200 2103) 		struct path path = {
f6360efb83cd6 (Takashi Iwai           2010-08-13 15:53:49 +0200 2104) 			.mnt	= fhp->fh_export->ex_path.mnt,
f6360efb83cd6 (Takashi Iwai           2010-08-13 15:53:49 +0200 2105) 			.dentry	= fhp->fh_dentry,
f6360efb83cd6 (Takashi Iwai           2010-08-13 15:53:49 +0200 2106) 		};
f6360efb83cd6 (Takashi Iwai           2010-08-13 15:53:49 +0200 2107) 		if (vfs_statfs(&path, stat))
f6360efb83cd6 (Takashi Iwai           2010-08-13 15:53:49 +0200 2108) 			err = nfserr_io;
f6360efb83cd6 (Takashi Iwai           2010-08-13 15:53:49 +0200 2109) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2110) 	return err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2111) }
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2112) 
c7d51402d2a64 (J. Bruce Fields        2007-07-19 01:49:20 -0700 2113) static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
e22841c637dc8 (J. Bruce Fields        2007-07-19 01:49:20 -0700 2114) {
c7d51402d2a64 (J. Bruce Fields        2007-07-19 01:49:20 -0700 2115) 	return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
e22841c637dc8 (J. Bruce Fields        2007-07-19 01:49:20 -0700 2116) }
e22841c637dc8 (J. Bruce Fields        2007-07-19 01:49:20 -0700 2117) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2118) #ifdef CONFIG_NFSD_V4
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2119) /*
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2120)  * Helper function to translate error numbers. In the case of xattr operations,
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2121)  * some error codes need to be translated outside of the standard translations.
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2122)  *
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2123)  * ENODATA needs to be translated to nfserr_noxattr.
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2124)  * E2BIG to nfserr_xattr2big.
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2125)  *
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2126)  * Additionally, vfs_listxattr can return -ERANGE. This means that the
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2127)  * file has too many extended attributes to retrieve inside an
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2128)  * XATTR_LIST_MAX sized buffer. This is a bug in the xattr implementation:
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2129)  * filesystems will allow the adding of extended attributes until they hit
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2130)  * their own internal limit. This limit may be larger than XATTR_LIST_MAX.
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2131)  * So, at that point, the attributes are present and valid, but can't
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2132)  * be retrieved using listxattr, since the upper level xattr code enforces
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2133)  * the XATTR_LIST_MAX limit.
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2134)  *
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2135)  * This bug means that we need to deal with listxattr returning -ERANGE. The
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2136)  * best mapping is to return TOOSMALL.
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2137)  */
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2138) static __be32
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2139) nfsd_xattr_errno(int err)
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2140) {
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2141) 	switch (err) {
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2142) 	case -ENODATA:
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2143) 		return nfserr_noxattr;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2144) 	case -E2BIG:
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2145) 		return nfserr_xattr2big;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2146) 	case -ERANGE:
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2147) 		return nfserr_toosmall;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2148) 	}
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2149) 	return nfserrno(err);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2150) }
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2151) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2152) /*
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2153)  * Retrieve the specified user extended attribute. To avoid always
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2154)  * having to allocate the maximum size (since we are not getting
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2155)  * a maximum size from the RPC), do a probe + alloc. Hold a reader
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2156)  * lock on i_rwsem to prevent the extended attribute from changing
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2157)  * size while we're doing this.
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2158)  */
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2159) __be32
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2160) nfsd_getxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2161) 	      void **bufp, int *lenp)
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2162) {
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2163) 	ssize_t len;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2164) 	__be32 err;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2165) 	char *buf;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2166) 	struct inode *inode;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2167) 	struct dentry *dentry;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2168) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2169) 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2170) 	if (err)
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2171) 		return err;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2172) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2173) 	err = nfs_ok;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2174) 	dentry = fhp->fh_dentry;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2175) 	inode = d_inode(dentry);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2176) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2177) 	inode_lock_shared(inode);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2178) 
c7c7a1a18af4c (Tycho Andersen         2021-01-21 14:19:28 +0100 2179) 	len = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2180) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2181) 	/*
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2182) 	 * Zero-length attribute, just return.
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2183) 	 */
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2184) 	if (len == 0) {
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2185) 		*bufp = NULL;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2186) 		*lenp = 0;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2187) 		goto out;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2188) 	}
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2189) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2190) 	if (len < 0) {
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2191) 		err = nfsd_xattr_errno(len);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2192) 		goto out;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2193) 	}
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2194) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2195) 	if (len > *lenp) {
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2196) 		err = nfserr_toosmall;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2197) 		goto out;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2198) 	}
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2199) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2200) 	buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2201) 	if (buf == NULL) {
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2202) 		err = nfserr_jukebox;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2203) 		goto out;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2204) 	}
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2205) 
c7c7a1a18af4c (Tycho Andersen         2021-01-21 14:19:28 +0100 2206) 	len = vfs_getxattr(&init_user_ns, dentry, name, buf, len);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2207) 	if (len <= 0) {
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2208) 		kvfree(buf);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2209) 		buf = NULL;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2210) 		err = nfsd_xattr_errno(len);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2211) 	}
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2212) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2213) 	*lenp = len;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2214) 	*bufp = buf;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2215) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2216) out:
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2217) 	inode_unlock_shared(inode);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2218) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2219) 	return err;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2220) }
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2221) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2222) /*
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2223)  * Retrieve the xattr names. Since we can't know how many are
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2224)  * user extended attributes, we must get all attributes here,
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2225)  * and have the XDR encode filter out the "user." ones.
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2226)  *
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2227)  * While this could always just allocate an XATTR_LIST_MAX
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2228)  * buffer, that's a waste, so do a probe + allocate. To
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2229)  * avoid any changes between the probe and allocate, wrap
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2230)  * this in inode_lock.
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2231)  */
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2232) __be32
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2233) nfsd_listxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char **bufp,
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2234) 	       int *lenp)
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2235) {
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2236) 	ssize_t len;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2237) 	__be32 err;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2238) 	char *buf;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2239) 	struct inode *inode;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2240) 	struct dentry *dentry;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2241) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2242) 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2243) 	if (err)
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2244) 		return err;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2245) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2246) 	dentry = fhp->fh_dentry;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2247) 	inode = d_inode(dentry);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2248) 	*lenp = 0;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2249) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2250) 	inode_lock_shared(inode);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2251) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2252) 	len = vfs_listxattr(dentry, NULL, 0);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2253) 	if (len <= 0) {
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2254) 		err = nfsd_xattr_errno(len);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2255) 		goto out;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2256) 	}
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2257) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2258) 	if (len > XATTR_LIST_MAX) {
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2259) 		err = nfserr_xattr2big;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2260) 		goto out;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2261) 	}
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2262) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2263) 	/*
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2264) 	 * We're holding i_rwsem - use GFP_NOFS.
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2265) 	 */
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2266) 	buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2267) 	if (buf == NULL) {
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2268) 		err = nfserr_jukebox;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2269) 		goto out;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2270) 	}
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2271) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2272) 	len = vfs_listxattr(dentry, buf, len);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2273) 	if (len <= 0) {
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2274) 		kvfree(buf);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2275) 		err = nfsd_xattr_errno(len);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2276) 		goto out;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2277) 	}
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2278) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2279) 	*lenp = len;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2280) 	*bufp = buf;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2281) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2282) 	err = nfs_ok;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2283) out:
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2284) 	inode_unlock_shared(inode);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2285) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2286) 	return err;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2287) }
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2288) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2289) /*
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2290)  * Removexattr and setxattr need to call fh_lock to both lock the inode
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2291)  * and set the change attribute. Since the top-level vfs_removexattr
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2292)  * and vfs_setxattr calls already do their own inode_lock calls, call
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2293)  * the _locked variant. Pass in a NULL pointer for delegated_inode,
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2294)  * and let the client deal with NFS4ERR_DELAY (same as with e.g.
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2295)  * setattr and remove).
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2296)  */
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2297) __be32
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2298) nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name)
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2299) {
8237284a00d99 (Chuck Lever            2020-09-11 14:47:48 -0400 2300) 	__be32 err;
8237284a00d99 (Chuck Lever            2020-09-11 14:47:48 -0400 2301) 	int ret;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2302) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2303) 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2304) 	if (err)
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2305) 		return err;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2306) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2307) 	ret = fh_want_write(fhp);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2308) 	if (ret)
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2309) 		return nfserrno(ret);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2310) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2311) 	fh_lock(fhp);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2312) 
c7c7a1a18af4c (Tycho Andersen         2021-01-21 14:19:28 +0100 2313) 	ret = __vfs_removexattr_locked(&init_user_ns, fhp->fh_dentry,
c7c7a1a18af4c (Tycho Andersen         2021-01-21 14:19:28 +0100 2314) 				       name, NULL);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2315) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2316) 	fh_unlock(fhp);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2317) 	fh_drop_write(fhp);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2318) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2319) 	return nfsd_xattr_errno(ret);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2320) }
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2321) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2322) __be32
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2323) nfsd_setxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2324) 	      void *buf, u32 len, u32 flags)
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2325) {
8237284a00d99 (Chuck Lever            2020-09-11 14:47:48 -0400 2326) 	__be32 err;
8237284a00d99 (Chuck Lever            2020-09-11 14:47:48 -0400 2327) 	int ret;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2328) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2329) 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2330) 	if (err)
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2331) 		return err;
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2332) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2333) 	ret = fh_want_write(fhp);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2334) 	if (ret)
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2335) 		return nfserrno(ret);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2336) 	fh_lock(fhp);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2337) 
c7c7a1a18af4c (Tycho Andersen         2021-01-21 14:19:28 +0100 2338) 	ret = __vfs_setxattr_locked(&init_user_ns, fhp->fh_dentry, name, buf,
c7c7a1a18af4c (Tycho Andersen         2021-01-21 14:19:28 +0100 2339) 				    len, flags, NULL);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2340) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2341) 	fh_unlock(fhp);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2342) 	fh_drop_write(fhp);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2343) 
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2344) 	return nfsd_xattr_errno(ret);
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2345) }
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2346) #endif
32119446bb65d (Frank van der Linden   2020-06-23 22:39:23 +0000 2347) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2348) /*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2349)  * Check for a user's access permissions to this inode.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2350)  */
6264d69d7df65 (Al Viro                2006-10-19 23:28:58 -0700 2351) __be32
0ec757df97430 (J. Bruce Fields        2007-07-17 04:04:48 -0700 2352) nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
0ec757df97430 (J. Bruce Fields        2007-07-17 04:04:48 -0700 2353) 					struct dentry *dentry, int acc)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2354) {
2b0143b5c986b (David Howells          2015-03-17 22:25:59 +0000 2355) 	struct inode	*inode = d_inode(dentry);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2356) 	int		err;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2357) 
aea93397db4b3 (J. Bruce Fields        2011-04-10 10:35:12 -0400 2358) 	if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2359) 		return 0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2360) #if 0
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2361) 	dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2362) 		acc,
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2363) 		(acc & NFSD_MAY_READ)?	" read"  : "",
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2364) 		(acc & NFSD_MAY_WRITE)?	" write" : "",
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2365) 		(acc & NFSD_MAY_EXEC)?	" exec"  : "",
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2366) 		(acc & NFSD_MAY_SATTR)?	" sattr" : "",
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2367) 		(acc & NFSD_MAY_TRUNC)?	" trunc" : "",
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2368) 		(acc & NFSD_MAY_LOCK)?	" lock"  : "",
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2369) 		(acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2370) 		inode->i_mode,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2371) 		IS_IMMUTABLE(inode)?	" immut" : "",
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2372) 		IS_APPEND(inode)?	" append" : "",
2c463e9548082 (Dave Hansen            2008-02-15 14:37:56 -0800 2373) 		__mnt_is_readonly(exp->ex_path.mnt)?	" ro" : "");
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2374) 	dprintk("      owner %d/%d user %d/%d\n",
5cc0a84076e17 (David Howells          2008-11-14 10:38:58 +1100 2375) 		inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2376) #endif
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2377) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2378) 	/* Normally we reject any write/sattr etc access on a read-only file
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2379) 	 * system.  But if it is IRIX doing check on write-access for a 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2380) 	 * device special file, we ignore rofs.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2381) 	 */
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2382) 	if (!(acc & NFSD_MAY_LOCAL_ACCESS))
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2383) 		if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2c463e9548082 (Dave Hansen            2008-02-15 14:37:56 -0800 2384) 			if (exp_rdonly(rqstp, exp) ||
2c463e9548082 (Dave Hansen            2008-02-15 14:37:56 -0800 2385) 			    __mnt_is_readonly(exp->ex_path.mnt))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2386) 				return nfserr_rofs;
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2387) 			if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2388) 				return nfserr_perm;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2389) 		}
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2390) 	if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2391) 		return nfserr_perm;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2392) 
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2393) 	if (acc & NFSD_MAY_LOCK) {
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2394) 		/* If we cannot rely on authentication in NLM requests,
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2395) 		 * just allow locks, otherwise require read permission, or
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2396) 		 * ownership
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2397) 		 */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2398) 		if (exp->ex_flags & NFSEXP_NOAUTHNLM)
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2399) 			return 0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2400) 		else
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2401) 			acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2402) 	}
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2403) 	/*
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2404) 	 * The file owner always gets access permission for accesses that
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2405) 	 * would normally be checked at open time. This is to make
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2406) 	 * file access work even when the client has done a fchmod(fd, 0).
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2407) 	 *
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2408) 	 * However, `cp foo bar' should fail nevertheless when bar is
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2409) 	 * readonly. A sensible way to do this might be to reject all
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2410) 	 * attempts to truncate a read-only file, because a creat() call
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2411) 	 * always implies file truncation.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2412) 	 * ... but this isn't really fair.  A process may reasonably call
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2413) 	 * ftruncate on an open file descriptor on a file with perm 000.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2414) 	 * We must trust the client to do permission checking - using "ACCESS"
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2415) 	 * with NFSv3.
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2416) 	 */
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2417) 	if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
6fab877900030 (Eric W. Biederman      2013-02-02 06:53:11 -0800 2418) 	    uid_eq(inode->i_uid, current_fsuid()))
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2419) 		return 0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2420) 
8837abcab3d16 (Miklos Szeredi         2008-06-16 13:20:29 +0200 2421) 	/* This assumes  NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
47291baa8ddfd (Christian Brauner      2021-01-21 14:19:24 +0100 2422) 	err = inode_permission(&init_user_ns, inode,
47291baa8ddfd (Christian Brauner      2021-01-21 14:19:24 +0100 2423) 			       acc & (MAY_READ | MAY_WRITE | MAY_EXEC));
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2424) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2425) 	/* Allow read access to binaries even when mode 111 */
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2426) 	if (err == -EACCES && S_ISREG(inode->i_mode) &&
a043226bc140a (J. Bruce Fields        2011-08-25 10:48:39 -0400 2427) 	     (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
a043226bc140a (J. Bruce Fields        2011-08-25 10:48:39 -0400 2428) 	      acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
47291baa8ddfd (Christian Brauner      2021-01-21 14:19:24 +0100 2429) 		err = inode_permission(&init_user_ns, inode, MAY_EXEC);
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2430) 
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2431) 	return err? nfserrno(err) : 0;
^1da177e4c3f4 (Linus Torvalds         2005-04-16 15:20:36 -0700 2432) }