Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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