xref: /openbmc/linux/fs/nfsd/nfsfh.c (revision a332018a)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * NFS server file handle treatment.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
61da177e4SLinus Torvalds  * Portions Copyright (C) 1999 G. Allen Morris III <gam3@acm.org>
71da177e4SLinus Torvalds  * Extensive rewrite by Neil Brown <neilb@cse.unsw.edu.au> Southern-Spring 1999
81da177e4SLinus Torvalds  * ... and again Southern-Winter 2001 to support export_operations
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
11a5694255SChristoph Hellwig #include <linux/exportfs.h>
121da177e4SLinus Torvalds 
1332c1eb0cSAndy Adamson #include <linux/sunrpc/svcauth_gss.h>
149a74af21SBoaz Harrosh #include "nfsd.h"
150a3adadeSJ. Bruce Fields #include "vfs.h"
162e8138a2SJ. Bruce Fields #include "auth.h"
17f01274a9STrond Myklebust #include "trace.h"
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #define NFSDDBG_FACILITY		NFSDDBG_FH
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds /*
231da177e4SLinus Torvalds  * our acceptability function.
241da177e4SLinus Torvalds  * if NOSUBTREECHECK, accept anything
251da177e4SLinus Torvalds  * if not, require that we can walk up to exp->ex_dentry
261da177e4SLinus Torvalds  * doing some checks on the 'x' bits
271da177e4SLinus Torvalds  */
nfsd_acceptable(void * expv,struct dentry * dentry)281da177e4SLinus Torvalds static int nfsd_acceptable(void *expv, struct dentry *dentry)
291da177e4SLinus Torvalds {
301da177e4SLinus Torvalds 	struct svc_export *exp = expv;
311da177e4SLinus Torvalds 	int rv;
321da177e4SLinus Torvalds 	struct dentry *tdentry;
331da177e4SLinus Torvalds 	struct dentry *parent;
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds 	if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
361da177e4SLinus Torvalds 		return 1;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 	tdentry = dget(dentry);
3954775491SJan Blunck 	while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
401da177e4SLinus Torvalds 		/* make sure parents give x permission to user */
411da177e4SLinus Torvalds 		int err;
421da177e4SLinus Torvalds 		parent = dget_parent(tdentry);
434609e1f1SChristian Brauner 		err = inode_permission(&nop_mnt_idmap,
4447291baaSChristian Brauner 				       d_inode(parent), MAY_EXEC);
451da177e4SLinus Torvalds 		if (err < 0) {
461da177e4SLinus Torvalds 			dput(parent);
471da177e4SLinus Torvalds 			break;
481da177e4SLinus Torvalds 		}
491da177e4SLinus Torvalds 		dput(tdentry);
501da177e4SLinus Torvalds 		tdentry = parent;
511da177e4SLinus Torvalds 	}
5254775491SJan Blunck 	if (tdentry != exp->ex_path.dentry)
5397e47fa1SAl Viro 		dprintk("nfsd_acceptable failed at %p %pd\n", tdentry, tdentry);
5454775491SJan Blunck 	rv = (tdentry == exp->ex_path.dentry);
551da177e4SLinus Torvalds 	dput(tdentry);
561da177e4SLinus Torvalds 	return rv;
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /* Type check. The correct error return for type mismatches does not seem to be
601da177e4SLinus Torvalds  * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
611da177e4SLinus Torvalds  * comment in the NFSv3 spec says this is incorrect (implementation notes for
621da177e4SLinus Torvalds  * the write call).
631da177e4SLinus Torvalds  */
6483b11340SAl Viro static inline __be32
nfsd_mode_check(struct svc_rqst * rqstp,struct dentry * dentry,umode_t requested)65e75b23f9SJ. Bruce Fields nfsd_mode_check(struct svc_rqst *rqstp, struct dentry *dentry,
66e75b23f9SJ. Bruce Fields 		umode_t requested)
671da177e4SLinus Torvalds {
68e75b23f9SJ. Bruce Fields 	umode_t mode = d_inode(dentry)->i_mode & S_IFMT;
69e10f9e14SJ. Bruce Fields 
70e10f9e14SJ. Bruce Fields 	if (requested == 0) /* the caller doesn't care */
71e10f9e14SJ. Bruce Fields 		return nfs_ok;
72e75b23f9SJ. Bruce Fields 	if (mode == requested) {
73e75b23f9SJ. Bruce Fields 		if (mode == S_IFDIR && !d_can_lookup(dentry)) {
74e75b23f9SJ. Bruce Fields 			WARN_ON_ONCE(1);
75e75b23f9SJ. Bruce Fields 			return nfserr_notdir;
76e75b23f9SJ. Bruce Fields 		}
77e10f9e14SJ. Bruce Fields 		return nfs_ok;
78e75b23f9SJ. Bruce Fields 	}
79e10f9e14SJ. Bruce Fields 	/*
80e10f9e14SJ. Bruce Fields 	 * v4 has an error more specific than err_notdir which we should
81e10f9e14SJ. Bruce Fields 	 * return in preference to err_notdir:
82e10f9e14SJ. Bruce Fields 	 */
83e10f9e14SJ. Bruce Fields 	if (rqstp->rq_vers == 4 && mode == S_IFLNK)
841da177e4SLinus Torvalds 		return nfserr_symlink;
85e10f9e14SJ. Bruce Fields 	if (requested == S_IFDIR)
861da177e4SLinus Torvalds 		return nfserr_notdir;
87e10f9e14SJ. Bruce Fields 	if (mode == S_IFDIR)
881da177e4SLinus Torvalds 		return nfserr_isdir;
891da177e4SLinus Torvalds 	return nfserr_inval;
901da177e4SLinus Torvalds }
911da177e4SLinus Torvalds 
nfsd_originating_port_ok(struct svc_rqst * rqstp,int flags)929d7ed135SJ. Bruce Fields static bool nfsd_originating_port_ok(struct svc_rqst *rqstp, int flags)
939d7ed135SJ. Bruce Fields {
949d7ed135SJ. Bruce Fields 	if (flags & NFSEXP_INSECURE_PORT)
959d7ed135SJ. Bruce Fields 		return true;
969d7ed135SJ. Bruce Fields 	/* We don't require gss requests to use low ports: */
979d7ed135SJ. Bruce Fields 	if (rqstp->rq_cred.cr_flavor >= RPC_AUTH_GSS)
989d7ed135SJ. Bruce Fields 		return true;
999d7ed135SJ. Bruce Fields 	return test_bit(RQ_SECURE, &rqstp->rq_flags);
1009d7ed135SJ. Bruce Fields }
1019d7ed135SJ. Bruce Fields 
nfsd_setuser_and_check_port(struct svc_rqst * rqstp,struct svc_export * exp)1026fa02839SJ. Bruce Fields static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
1036fa02839SJ. Bruce Fields 					  struct svc_export *exp)
1046fa02839SJ. Bruce Fields {
10512045a6eSJ. Bruce Fields 	int flags = nfsexp_flags(rqstp, exp);
10612045a6eSJ. Bruce Fields 
1076fa02839SJ. Bruce Fields 	/* Check if the request originated from a secure port. */
1089d7ed135SJ. Bruce Fields 	if (!nfsd_originating_port_ok(rqstp, flags)) {
1095216a8e7SPavel Emelyanov 		RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
110a48fd0f9SKinglong Mee 		dprintk("nfsd: request from insecure port %s!\n",
1116fa02839SJ. Bruce Fields 		        svc_print_addr(rqstp, buf, sizeof(buf)));
1126fa02839SJ. Bruce Fields 		return nfserr_perm;
1136fa02839SJ. Bruce Fields 	}
1146fa02839SJ. Bruce Fields 
1156fa02839SJ. Bruce Fields 	/* Set user creds for this exportpoint */
1166fa02839SJ. Bruce Fields 	return nfserrno(nfsd_setuser(rqstp, exp));
1176fa02839SJ. Bruce Fields }
1186fa02839SJ. Bruce Fields 
check_pseudo_root(struct svc_rqst * rqstp,struct dentry * dentry,struct svc_export * exp)11903a816b4SSteve Dickson static inline __be32 check_pseudo_root(struct svc_rqst *rqstp,
12003a816b4SSteve Dickson 	struct dentry *dentry, struct svc_export *exp)
12103a816b4SSteve Dickson {
12203a816b4SSteve Dickson 	if (!(exp->ex_flags & NFSEXP_V4ROOT))
12303a816b4SSteve Dickson 		return nfs_ok;
12403a816b4SSteve Dickson 	/*
12503a816b4SSteve Dickson 	 * v2/v3 clients have no need for the V4ROOT export--they use
12603a816b4SSteve Dickson 	 * the mount protocl instead; also, further V4ROOT checks may be
12703a816b4SSteve Dickson 	 * in v4-specific code, in which case v2/v3 clients could bypass
12803a816b4SSteve Dickson 	 * them.
12903a816b4SSteve Dickson 	 */
13003a816b4SSteve Dickson 	if (!nfsd_v4client(rqstp))
13103a816b4SSteve Dickson 		return nfserr_stale;
13203a816b4SSteve Dickson 	/*
13303a816b4SSteve Dickson 	 * We're exposing only the directories and symlinks that have to be
13403a816b4SSteve Dickson 	 * traversed on the way to real exports:
13503a816b4SSteve Dickson 	 */
136e36cb0b8SDavid Howells 	if (unlikely(!d_is_dir(dentry) &&
137e36cb0b8SDavid Howells 		     !d_is_symlink(dentry)))
13803a816b4SSteve Dickson 		return nfserr_stale;
13903a816b4SSteve Dickson 	/*
14003a816b4SSteve Dickson 	 * A pseudoroot export gives permission to access only one
14103a816b4SSteve Dickson 	 * single directory; the kernel has to make another upcall
14203a816b4SSteve Dickson 	 * before granting access to anything else under it:
14303a816b4SSteve Dickson 	 */
14403a816b4SSteve Dickson 	if (unlikely(dentry != exp->ex_path.dentry))
14503a816b4SSteve Dickson 		return nfserr_stale;
14603a816b4SSteve Dickson 	return nfs_ok;
14703a816b4SSteve Dickson }
14803a816b4SSteve Dickson 
1491da177e4SLinus Torvalds /*
15003550facSJ. Bruce Fields  * Use the given filehandle to look up the corresponding export and
15103550facSJ. Bruce Fields  * dentry.  On success, the results are used to set fh_export and
15203550facSJ. Bruce Fields  * fh_dentry.
1531da177e4SLinus Torvalds  */
nfsd_set_fh_dentry(struct svc_rqst * rqstp,struct svc_fh * fhp)15403550facSJ. Bruce Fields static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	struct knfsd_fh	*fh = &fhp->fh_handle;
157c645a883SNeilBrown 	struct fid *fid = NULL;
15803550facSJ. Bruce Fields 	struct svc_export *exp;
15903550facSJ. Bruce Fields 	struct dentry *dentry;
1601da177e4SLinus Torvalds 	int fileid_type;
1611da177e4SLinus Torvalds 	int data_left = fh->fh_size/4;
162c645a883SNeilBrown 	int len;
16303550facSJ. Bruce Fields 	__be32 error;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	error = nfserr_stale;
1661da177e4SLinus Torvalds 	if (rqstp->rq_vers > 2)
1671da177e4SLinus Torvalds 		error = nfserr_badhandle;
1681da177e4SLinus Torvalds 	if (rqstp->rq_vers == 4 && fh->fh_size == 0)
1691da177e4SLinus Torvalds 		return nfserr_nofilehandle;
1701da177e4SLinus Torvalds 
171c645a883SNeilBrown 	if (fh->fh_version != 1)
172c645a883SNeilBrown 		return error;
17303550facSJ. Bruce Fields 
17403550facSJ. Bruce Fields 	if (--data_left < 0)
17503550facSJ. Bruce Fields 		return error;
17603550facSJ. Bruce Fields 	if (fh->fh_auth_type != 0)
17703550facSJ. Bruce Fields 		return error;
1781da177e4SLinus Torvalds 	len = key_len(fh->fh_fsid_type) / 4;
17903550facSJ. Bruce Fields 	if (len == 0)
18003550facSJ. Bruce Fields 		return error;
181af6a4e28SNeilBrown 	if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
1821da177e4SLinus Torvalds 		/* deprecated, convert to type 3 */
183af6a4e28SNeilBrown 		len = key_len(FSID_ENCODE_DEV)/4;
184af6a4e28SNeilBrown 		fh->fh_fsid_type = FSID_ENCODE_DEV;
18594ec938bSJeff Layton 		/*
18694ec938bSJeff Layton 		 * struct knfsd_fh uses host-endian fields, which are
18794ec938bSJeff Layton 		 * sometimes used to hold net-endian values. This
18894ec938bSJeff Layton 		 * confuses sparse, so we must use __force here to
18994ec938bSJeff Layton 		 * keep it from complaining.
19094ec938bSJeff Layton 		 */
19194ec938bSJeff Layton 		fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl((__force __be32)fh->fh_fsid[0]),
19294ec938bSJeff Layton 						      ntohl((__force __be32)fh->fh_fsid[1])));
1931da177e4SLinus Torvalds 		fh->fh_fsid[1] = fh->fh_fsid[2];
1941da177e4SLinus Torvalds 	}
19503550facSJ. Bruce Fields 	data_left -= len;
19603550facSJ. Bruce Fields 	if (data_left < 0)
19703550facSJ. Bruce Fields 		return error;
1985409e46fSChristoph Hellwig 	exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_fsid);
1995409e46fSChristoph Hellwig 	fid = (struct fid *)(fh->fh_fsid + len);
2001da177e4SLinus Torvalds 
2012d3bb252SJ. Bruce Fields 	error = nfserr_stale;
202f01274a9STrond Myklebust 	if (IS_ERR(exp)) {
203f01274a9STrond Myklebust 		trace_nfsd_set_fh_dentry_badexport(rqstp, fhp, PTR_ERR(exp));
204f01274a9STrond Myklebust 
2052d3bb252SJ. Bruce Fields 		if (PTR_ERR(exp) == -ENOENT)
20603550facSJ. Bruce Fields 			return error;
2072d3bb252SJ. Bruce Fields 
20803550facSJ. Bruce Fields 		return nfserrno(PTR_ERR(exp));
209f01274a9STrond Myklebust 	}
2101da177e4SLinus Torvalds 
211496d6c32SNeil Brown 	if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
212496d6c32SNeil Brown 		/* Elevate privileges so that the lack of 'r' or 'x'
213496d6c32SNeil Brown 		 * permission on some parent directory will
214496d6c32SNeil Brown 		 * not stop exportfs_decode_fh from being able
215496d6c32SNeil Brown 		 * to reconnect a directory into the dentry cache.
216496d6c32SNeil Brown 		 * The same problem can affect "SUBTREECHECK" exports,
217496d6c32SNeil Brown 		 * but as nfsd_acceptable depends on correct
218496d6c32SNeil Brown 		 * access control settings being in effect, we cannot
219496d6c32SNeil Brown 		 * fix that case easily.
220496d6c32SNeil Brown 		 */
221d84f4f99SDavid Howells 		struct cred *new = prepare_creds();
222027bc41aSKinglong Mee 		if (!new) {
223027bc41aSKinglong Mee 			error =  nfserrno(-ENOMEM);
224027bc41aSKinglong Mee 			goto out;
225027bc41aSKinglong Mee 		}
226d84f4f99SDavid Howells 		new->cap_effective =
227d84f4f99SDavid Howells 			cap_raise_nfsd_set(new->cap_effective,
228d84f4f99SDavid Howells 					   new->cap_permitted);
229d84f4f99SDavid Howells 		put_cred(override_creds(new));
230d84f4f99SDavid Howells 		put_cred(new);
231496d6c32SNeil Brown 	} else {
2326fa02839SJ. Bruce Fields 		error = nfsd_setuser_and_check_port(rqstp, exp);
233d1bbf14fSNeilBrown 		if (error)
234d1bbf14fSNeilBrown 			goto out;
235496d6c32SNeil Brown 	}
236d1bbf14fSNeilBrown 
2371da177e4SLinus Torvalds 	/*
2381da177e4SLinus Torvalds 	 * Look up the dentry using the NFS file handle.
2391da177e4SLinus Torvalds 	 */
2401da177e4SLinus Torvalds 	error = nfserr_stale;
2411da177e4SLinus Torvalds 	if (rqstp->rq_vers > 2)
2421da177e4SLinus Torvalds 		error = nfserr_badhandle;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	fileid_type = fh->fh_fileid_type;
2451da177e4SLinus Torvalds 
2466e91ea2bSChristoph Hellwig 	if (fileid_type == FILEID_ROOT)
24754775491SJan Blunck 		dentry = dget(exp->ex_path.dentry);
2481da177e4SLinus Torvalds 	else {
2492e19d10cSTrond Myklebust 		dentry = exportfs_decode_fh_raw(exp->ex_path.mnt, fid,
250d37065cdSChristoph Hellwig 						data_left, fileid_type,
2511da177e4SLinus Torvalds 						nfsd_acceptable, exp);
2522e19d10cSTrond Myklebust 		if (IS_ERR_OR_NULL(dentry)) {
253f01274a9STrond Myklebust 			trace_nfsd_set_fh_dentry_badhandle(rqstp, fhp,
254f01274a9STrond Myklebust 					dentry ?  PTR_ERR(dentry) : -ESTALE);
2552e19d10cSTrond Myklebust 			switch (PTR_ERR(dentry)) {
2562e19d10cSTrond Myklebust 			case -ENOMEM:
2572e19d10cSTrond Myklebust 			case -ETIMEDOUT:
2582e19d10cSTrond Myklebust 				break;
2592e19d10cSTrond Myklebust 			default:
2602e19d10cSTrond Myklebust 				dentry = ERR_PTR(-ESTALE);
2612e19d10cSTrond Myklebust 			}
2622e19d10cSTrond Myklebust 		}
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds 	if (dentry == NULL)
2651da177e4SLinus Torvalds 		goto out;
2661da177e4SLinus Torvalds 	if (IS_ERR(dentry)) {
2671da177e4SLinus Torvalds 		if (PTR_ERR(dentry) != -EINVAL)
2681da177e4SLinus Torvalds 			error = nfserrno(PTR_ERR(dentry));
2691da177e4SLinus Torvalds 		goto out;
2701da177e4SLinus Torvalds 	}
27134e9a63bSNeilBrown 
272e36cb0b8SDavid Howells 	if (d_is_dir(dentry) &&
2731da177e4SLinus Torvalds 			(dentry->d_flags & DCACHE_DISCONNECTED)) {
27497e47fa1SAl Viro 		printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %pd2\n",
27597e47fa1SAl Viro 				dentry);
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	fhp->fh_dentry = dentry;
2791da177e4SLinus Torvalds 	fhp->fh_export = exp;
280daab110eSJeff Layton 
281daab110eSJeff Layton 	switch (rqstp->rq_vers) {
282716a8bc7STrond Myklebust 	case 4:
283716a8bc7STrond Myklebust 		if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOATOMIC_ATTR)
284716a8bc7STrond Myklebust 			fhp->fh_no_atomic_attr = true;
285716a8bc7STrond Myklebust 		break;
286daab110eSJeff Layton 	case 3:
287daab110eSJeff Layton 		if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOWCC)
288daab110eSJeff Layton 			fhp->fh_no_wcc = true;
289daab110eSJeff Layton 		break;
290daab110eSJeff Layton 	case 2:
291daab110eSJeff Layton 		fhp->fh_no_wcc = true;
292daab110eSJeff Layton 	}
293daab110eSJeff Layton 
29403550facSJ. Bruce Fields 	return 0;
29503550facSJ. Bruce Fields out:
29603550facSJ. Bruce Fields 	exp_put(exp);
29703550facSJ. Bruce Fields 	return error;
29803550facSJ. Bruce Fields }
29903550facSJ. Bruce Fields 
300b3d47676SJ. Bruce Fields /**
301b3d47676SJ. Bruce Fields  * fh_verify - filehandle lookup and access checking
302b3d47676SJ. Bruce Fields  * @rqstp: pointer to current rpc request
303b3d47676SJ. Bruce Fields  * @fhp: filehandle to be verified
304b3d47676SJ. Bruce Fields  * @type: expected type of object pointed to by filehandle
305b3d47676SJ. Bruce Fields  * @access: type of access needed to object
30603550facSJ. Bruce Fields  *
307b3d47676SJ. Bruce Fields  * Look up a dentry from the on-the-wire filehandle, check the client's
308b3d47676SJ. Bruce Fields  * access to the export, and set the current task's credentials.
30903550facSJ. Bruce Fields  *
310b3d47676SJ. Bruce Fields  * Regardless of success or failure of fh_verify(), fh_put() should be
311b3d47676SJ. Bruce Fields  * called on @fhp when the caller is finished with the filehandle.
312b3d47676SJ. Bruce Fields  *
313b3d47676SJ. Bruce Fields  * fh_verify() may be called multiple times on a given filehandle, for
314b3d47676SJ. Bruce Fields  * example, when processing an NFSv4 compound.  The first call will look
315b3d47676SJ. Bruce Fields  * up a dentry using the on-the-wire filehandle.  Subsequent calls will
316b3d47676SJ. Bruce Fields  * skip the lookup and just perform the other checks and possibly change
317b3d47676SJ. Bruce Fields  * the current task's credentials.
318b3d47676SJ. Bruce Fields  *
319b3d47676SJ. Bruce Fields  * @type specifies the type of object expected using one of the S_IF*
320b3d47676SJ. Bruce Fields  * constants defined in include/linux/stat.h.  The caller may use zero
321b3d47676SJ. Bruce Fields  * to indicate that it doesn't care, or a negative integer to indicate
322b3d47676SJ. Bruce Fields  * that it expects something not of the given type.
323b3d47676SJ. Bruce Fields  *
324b3d47676SJ. Bruce Fields  * @access is formed from the NFSD_MAY_* constants defined in
32593f580a9SOleg Drokin  * fs/nfsd/vfs.h.
32603550facSJ. Bruce Fields  */
32703550facSJ. Bruce Fields __be32
fh_verify(struct svc_rqst * rqstp,struct svc_fh * fhp,umode_t type,int access)328175a4eb7SAl Viro fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, int access)
32903550facSJ. Bruce Fields {
33020ad856eSAmir Goldstein 	struct svc_export *exp = NULL;
33103550facSJ. Bruce Fields 	struct dentry	*dentry;
33203550facSJ. Bruce Fields 	__be32		error;
33303550facSJ. Bruce Fields 
33403550facSJ. Bruce Fields 	if (!fhp->fh_dentry) {
33503550facSJ. Bruce Fields 		error = nfsd_set_fh_dentry(rqstp, fhp);
33603550facSJ. Bruce Fields 		if (error)
33703550facSJ. Bruce Fields 			goto out;
338864f0f61SJ. Bruce Fields 	}
3391da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
3401da177e4SLinus Torvalds 	exp = fhp->fh_export;
34105138288SChuck Lever 
34205138288SChuck Lever 	trace_nfsd_fh_verify(rqstp, fhp, type, access);
34305138288SChuck Lever 
3446fa02839SJ. Bruce Fields 	/*
345864f0f61SJ. Bruce Fields 	 * We still have to do all these permission checks, even when
346864f0f61SJ. Bruce Fields 	 * fh_dentry is already set:
347864f0f61SJ. Bruce Fields 	 * 	- fh_verify may be called multiple times with different
348864f0f61SJ. Bruce Fields 	 * 	  "access" arguments (e.g. nfsd_proc_create calls
349864f0f61SJ. Bruce Fields 	 * 	  fh_verify(...,NFSD_MAY_EXEC) first, then later (in
350864f0f61SJ. Bruce Fields 	 * 	  nfsd_create) calls fh_verify(...,NFSD_MAY_CREATE).
351864f0f61SJ. Bruce Fields 	 *	- in the NFSv4 case, the filehandle may have been filled
352864f0f61SJ. Bruce Fields 	 *	  in by fh_compose, and given a dentry, but further
353864f0f61SJ. Bruce Fields 	 *	  compound operations performed with that filehandle
354864f0f61SJ. Bruce Fields 	 *	  still need permissions checks.  In the worst case, a
355864f0f61SJ. Bruce Fields 	 *	  mountpoint crossing may have changed the export
356864f0f61SJ. Bruce Fields 	 *	  options, and we may now need to use a different uid
357864f0f61SJ. Bruce Fields 	 *	  (for example, if different id-squashing options are in
358864f0f61SJ. Bruce Fields 	 *	  effect on the new filesystem).
3596fa02839SJ. Bruce Fields 	 */
36003a816b4SSteve Dickson 	error = check_pseudo_root(rqstp, dentry, exp);
36103a816b4SSteve Dickson 	if (error)
36203a816b4SSteve Dickson 		goto out;
36303a816b4SSteve Dickson 
3646fa02839SJ. Bruce Fields 	error = nfsd_setuser_and_check_port(rqstp, exp);
3657fc90ec9SJ. Bruce Fields 	if (error)
3667fc90ec9SJ. Bruce Fields 		goto out;
3677fc90ec9SJ. Bruce Fields 
368e75b23f9SJ. Bruce Fields 	error = nfsd_mode_check(rqstp, dentry, type);
3691da177e4SLinus Torvalds 	if (error)
3701da177e4SLinus Torvalds 		goto out;
3711da177e4SLinus Torvalds 
3729091224fSJ. Bruce Fields 	/*
3739091224fSJ. Bruce Fields 	 * pseudoflavor restrictions are not enforced on NLM,
3749091224fSJ. Bruce Fields 	 * which clients virtually always use auth_sys for,
3759091224fSJ. Bruce Fields 	 * even while using RPCSEC_GSS for NFS.
3769091224fSJ. Bruce Fields 	 */
377204f4ce7SJ. Bruce Fields 	if (access & NFSD_MAY_LOCK || access & NFSD_MAY_BYPASS_GSS)
37804716e66SJ. Bruce Fields 		goto skip_pseudoflavor_check;
37904716e66SJ. Bruce Fields 	/*
38004716e66SJ. Bruce Fields 	 * Clients may expect to be able to use auth_sys during mount,
38104716e66SJ. Bruce Fields 	 * even if they use gss for everything else; see section 2.3.2
38204716e66SJ. Bruce Fields 	 * of rfc 2623.
38304716e66SJ. Bruce Fields 	 */
38404716e66SJ. Bruce Fields 	if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
38504716e66SJ. Bruce Fields 			&& exp->ex_path.dentry == dentry)
38604716e66SJ. Bruce Fields 		goto skip_pseudoflavor_check;
38704716e66SJ. Bruce Fields 
38832c1eb0cSAndy Adamson 	error = check_nfsd_access(exp, rqstp);
38932c1eb0cSAndy Adamson 	if (error)
39032c1eb0cSAndy Adamson 		goto out;
39132c1eb0cSAndy Adamson 
39204716e66SJ. Bruce Fields skip_pseudoflavor_check:
3931da177e4SLinus Torvalds 	/* Finally, check access permissions. */
3940ec757dfSJ. Bruce Fields 	error = nfsd_permission(rqstp, exp, dentry, access);
3951da177e4SLinus Torvalds out:
39693c128e7SJeff Layton 	trace_nfsd_fh_verify_err(rqstp, fhp, type, access, error);
3971da177e4SLinus Torvalds 	if (error == nfserr_stale)
39820ad856eSAmir Goldstein 		nfsd_stats_fh_stale_inc(exp);
3991da177e4SLinus Torvalds 	return error;
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds /*
4041da177e4SLinus Torvalds  * Compose a file handle for an NFS reply.
4051da177e4SLinus Torvalds  *
4061da177e4SLinus Torvalds  * Note that when first composed, the dentry may not yet have
4071da177e4SLinus Torvalds  * an inode.  In this case a call to fh_update should be made
4081da177e4SLinus Torvalds  * before the fh goes out on the wire ...
4091da177e4SLinus Torvalds  */
_fh_update(struct svc_fh * fhp,struct svc_export * exp,struct dentry * dentry)4106e91ea2bSChristoph Hellwig static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
4116e91ea2bSChristoph Hellwig 		struct dentry *dentry)
4121da177e4SLinus Torvalds {
41354775491SJan Blunck 	if (dentry != exp->ex_path.dentry) {
4146e91ea2bSChristoph Hellwig 		struct fid *fid = (struct fid *)
4155409e46fSChristoph Hellwig 			(fhp->fh_handle.fh_fsid + fhp->fh_handle.fh_size/4 - 1);
4166e91ea2bSChristoph Hellwig 		int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
417b5287827SAmir Goldstein 		int fh_flags = (exp->ex_flags & NFSEXP_NOSUBTREECHECK) ? 0 :
418b5287827SAmir Goldstein 				EXPORT_FH_CONNECTABLE;
4197cdafe6cSAmir Goldstein 		int fileid_type =
4207cdafe6cSAmir Goldstein 			exportfs_encode_fh(dentry, fid, &maxsize, fh_flags);
4211da177e4SLinus Torvalds 
4226e91ea2bSChristoph Hellwig 		fhp->fh_handle.fh_fileid_type =
4237cdafe6cSAmir Goldstein 			fileid_type > 0 ? fileid_type : FILEID_INVALID;
4246e91ea2bSChristoph Hellwig 		fhp->fh_handle.fh_size += maxsize * 4;
4256e91ea2bSChristoph Hellwig 	} else {
4266e91ea2bSChristoph Hellwig 		fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
4276e91ea2bSChristoph Hellwig 	}
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
is_root_export(struct svc_export * exp)4308e498751SJ. Bruce Fields static bool is_root_export(struct svc_export *exp)
4318e498751SJ. Bruce Fields {
4328e498751SJ. Bruce Fields 	return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root;
4338e498751SJ. Bruce Fields }
4348e498751SJ. Bruce Fields 
exp_sb(struct svc_export * exp)4358e498751SJ. Bruce Fields static struct super_block *exp_sb(struct svc_export *exp)
4368e498751SJ. Bruce Fields {
437fc64005cSAl Viro 	return exp->ex_path.dentry->d_sb;
4388e498751SJ. Bruce Fields }
4398e498751SJ. Bruce Fields 
fsid_type_ok_for_exp(u8 fsid_type,struct svc_export * exp)4408e498751SJ. Bruce Fields static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)
4418e498751SJ. Bruce Fields {
4428e498751SJ. Bruce Fields 	switch (fsid_type) {
4438e498751SJ. Bruce Fields 	case FSID_DEV:
4448e498751SJ. Bruce Fields 		if (!old_valid_dev(exp_sb(exp)->s_dev))
445a677a783SGustavo A. R. Silva 			return false;
446df561f66SGustavo A. R. Silva 		fallthrough;
4478e498751SJ. Bruce Fields 	case FSID_MAJOR_MINOR:
4488e498751SJ. Bruce Fields 	case FSID_ENCODE_DEV:
4498e498751SJ. Bruce Fields 		return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV;
4508e498751SJ. Bruce Fields 	case FSID_NUM:
4518e498751SJ. Bruce Fields 		return exp->ex_flags & NFSEXP_FSID;
4528e498751SJ. Bruce Fields 	case FSID_UUID8:
4538e498751SJ. Bruce Fields 	case FSID_UUID16:
4548e498751SJ. Bruce Fields 		if (!is_root_export(exp))
455a677a783SGustavo A. R. Silva 			return false;
456df561f66SGustavo A. R. Silva 		fallthrough;
4578e498751SJ. Bruce Fields 	case FSID_UUID4_INUM:
4588e498751SJ. Bruce Fields 	case FSID_UUID16_INUM:
4598e498751SJ. Bruce Fields 		return exp->ex_uuid != NULL;
4608e498751SJ. Bruce Fields 	}
461a677a783SGustavo A. R. Silva 	return true;
4628e498751SJ. Bruce Fields }
4638e498751SJ. Bruce Fields 
464bc6c53d5SJ. Bruce Fields 
set_version_and_fsid_type(struct svc_fh * fhp,struct svc_export * exp,struct svc_fh * ref_fh)465bc6c53d5SJ. Bruce Fields static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh)
4661da177e4SLinus Torvalds {
467b41eeef1SNeilBrown 	u8 version;
468bc6c53d5SJ. Bruce Fields 	u8 fsid_type;
469b41eeef1SNeilBrown retry:
470b41eeef1SNeilBrown 	version = 1;
4717e405364SNeilBrown 	if (ref_fh && ref_fh->fh_export == exp) {
472982aedfdSNeilBrown 		version = ref_fh->fh_handle.fh_version;
473982aedfdSNeilBrown 		fsid_type = ref_fh->fh_handle.fh_fsid_type;
474b41eeef1SNeilBrown 
475b41eeef1SNeilBrown 		ref_fh = NULL;
476b41eeef1SNeilBrown 
477b41eeef1SNeilBrown 		switch (version) {
478b41eeef1SNeilBrown 		case 0xca:
479b41eeef1SNeilBrown 			fsid_type = FSID_DEV;
480b41eeef1SNeilBrown 			break;
481b41eeef1SNeilBrown 		case 1:
482b41eeef1SNeilBrown 			break;
483b41eeef1SNeilBrown 		default:
484b41eeef1SNeilBrown 			goto retry;
485b41eeef1SNeilBrown 		}
486b41eeef1SNeilBrown 
4878e498751SJ. Bruce Fields 		/*
4888e498751SJ. Bruce Fields 		 * As the fsid -> filesystem mapping was guided by
4898e498751SJ. Bruce Fields 		 * user-space, there is no guarantee that the filesystem
4908e498751SJ. Bruce Fields 		 * actually supports that fsid type. If it doesn't we
4918e498751SJ. Bruce Fields 		 * loop around again without ref_fh set.
492982aedfdSNeilBrown 		 */
4938e498751SJ. Bruce Fields 		if (!fsid_type_ok_for_exp(fsid_type, exp))
494b41eeef1SNeilBrown 			goto retry;
49530fa8c01SSteve Dickson 	} else if (exp->ex_flags & NFSEXP_FSID) {
49630fa8c01SSteve Dickson 		fsid_type = FSID_NUM;
497af6a4e28SNeilBrown 	} else if (exp->ex_uuid) {
498af6a4e28SNeilBrown 		if (fhp->fh_maxsize >= 64) {
4998e498751SJ. Bruce Fields 			if (is_root_export(exp))
500af6a4e28SNeilBrown 				fsid_type = FSID_UUID16;
501af6a4e28SNeilBrown 			else
502af6a4e28SNeilBrown 				fsid_type = FSID_UUID16_INUM;
503af6a4e28SNeilBrown 		} else {
5048e498751SJ. Bruce Fields 			if (is_root_export(exp))
505af6a4e28SNeilBrown 				fsid_type = FSID_UUID8;
506af6a4e28SNeilBrown 			else
507af6a4e28SNeilBrown 				fsid_type = FSID_UUID4_INUM;
508af6a4e28SNeilBrown 		}
509bc6c53d5SJ. Bruce Fields 	} else if (!old_valid_dev(exp_sb(exp)->s_dev))
5101da177e4SLinus Torvalds 		/* for newer device numbers, we must use a newer fsid format */
511af6a4e28SNeilBrown 		fsid_type = FSID_ENCODE_DEV;
512982aedfdSNeilBrown 	else
513af6a4e28SNeilBrown 		fsid_type = FSID_DEV;
514bc6c53d5SJ. Bruce Fields 	fhp->fh_handle.fh_version = version;
515bc6c53d5SJ. Bruce Fields 	if (version)
516bc6c53d5SJ. Bruce Fields 		fhp->fh_handle.fh_fsid_type = fsid_type;
517bc6c53d5SJ. Bruce Fields }
518bc6c53d5SJ. Bruce Fields 
519bc6c53d5SJ. Bruce Fields __be32
fh_compose(struct svc_fh * fhp,struct svc_export * exp,struct dentry * dentry,struct svc_fh * ref_fh)520bc6c53d5SJ. Bruce Fields fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
521bc6c53d5SJ. Bruce Fields 	   struct svc_fh *ref_fh)
522bc6c53d5SJ. Bruce Fields {
523bc6c53d5SJ. Bruce Fields 	/* ref_fh is a reference file handle.
524bc6c53d5SJ. Bruce Fields 	 * if it is non-null and for the same filesystem, then we should compose
525bc6c53d5SJ. Bruce Fields 	 * a filehandle which is of the same version, where possible.
526bc6c53d5SJ. Bruce Fields 	 */
527bc6c53d5SJ. Bruce Fields 
5282b0143b5SDavid Howells 	struct inode * inode = d_inode(dentry);
529bc6c53d5SJ. Bruce Fields 	dev_t ex_dev = exp_sb(exp)->s_dev;
530bc6c53d5SJ. Bruce Fields 
53197e47fa1SAl Viro 	dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %pd2, ino=%ld)\n",
532bc6c53d5SJ. Bruce Fields 		MAJOR(ex_dev), MINOR(ex_dev),
5332b0143b5SDavid Howells 		(long) d_inode(exp->ex_path.dentry)->i_ino,
53497e47fa1SAl Viro 		dentry,
535bc6c53d5SJ. Bruce Fields 		(inode ? inode->i_ino : 0));
536bc6c53d5SJ. Bruce Fields 
537bc6c53d5SJ. Bruce Fields 	/* Choose filehandle version and fsid type based on
538bc6c53d5SJ. Bruce Fields 	 * the reference filehandle (if it is in the same export)
539bc6c53d5SJ. Bruce Fields 	 * or the export options.
540bc6c53d5SJ. Bruce Fields 	 */
541bc6c53d5SJ. Bruce Fields 	set_version_and_fsid_type(fhp, exp, ref_fh);
5421da177e4SLinus Torvalds 
543daab110eSJeff Layton 	/* If we have a ref_fh, then copy the fh_no_wcc setting from it. */
544daab110eSJeff Layton 	fhp->fh_no_wcc = ref_fh ? ref_fh->fh_no_wcc : false;
545daab110eSJeff Layton 
5461da177e4SLinus Torvalds 	if (ref_fh == fhp)
5471da177e4SLinus Torvalds 		fh_put(ref_fh);
5481da177e4SLinus Torvalds 
549dd8dd403SNeilBrown 	if (fhp->fh_dentry) {
55097e47fa1SAl Viro 		printk(KERN_ERR "fh_compose: fh %pd2 not initialized!\n",
55197e47fa1SAl Viro 		       dentry);
5521da177e4SLinus Torvalds 	}
5531da177e4SLinus Torvalds 	if (fhp->fh_maxsize < NFS_FHSIZE)
55497e47fa1SAl Viro 		printk(KERN_ERR "fh_compose: called with maxsize %d! %pd2\n",
555982aedfdSNeilBrown 		       fhp->fh_maxsize,
55697e47fa1SAl Viro 		       dentry);
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	fhp->fh_dentry = dget(dentry); /* our internal copy */
559bf18f163SKinglong Mee 	fhp->fh_export = exp_get(exp);
5601da177e4SLinus Torvalds 
5615409e46fSChristoph Hellwig 	fhp->fh_handle.fh_size =
5625409e46fSChristoph Hellwig 		key_len(fhp->fh_handle.fh_fsid_type) + 4;
5631da177e4SLinus Torvalds 	fhp->fh_handle.fh_auth_type = 0;
5645409e46fSChristoph Hellwig 
5655409e46fSChristoph Hellwig 	mk_fsid(fhp->fh_handle.fh_fsid_type,
5665409e46fSChristoph Hellwig 		fhp->fh_handle.fh_fsid,
5675409e46fSChristoph Hellwig 		ex_dev,
5682b0143b5SDavid Howells 		d_inode(exp->ex_path.dentry)->i_ino,
569af6a4e28SNeilBrown 		exp->ex_fsid, exp->ex_uuid);
570af6a4e28SNeilBrown 
5716e91ea2bSChristoph Hellwig 	if (inode)
5726e91ea2bSChristoph Hellwig 		_fh_update(fhp, exp, dentry);
573216b6cbdSNamjae Jeon 	if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID) {
5741be10a88SJ. Bruce Fields 		fh_put(fhp);
5751da177e4SLinus Torvalds 		return nfserr_opnotsupp;
5761da177e4SLinus Torvalds 	}
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	return 0;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds /*
5821da177e4SLinus Torvalds  * Update file handle information after changing a dentry.
5831da177e4SLinus Torvalds  * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
5841da177e4SLinus Torvalds  */
58583b11340SAl Viro __be32
fh_update(struct svc_fh * fhp)5861da177e4SLinus Torvalds fh_update(struct svc_fh *fhp)
5871da177e4SLinus Torvalds {
5881da177e4SLinus Torvalds 	struct dentry *dentry;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	if (!fhp->fh_dentry)
5911da177e4SLinus Torvalds 		goto out_bad;
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
5942b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
5951da177e4SLinus Torvalds 		goto out_negative;
5966e91ea2bSChristoph Hellwig 	if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
59749e73720SJ. Bruce Fields 		return 0;
5986e91ea2bSChristoph Hellwig 
5996e91ea2bSChristoph Hellwig 	_fh_update(fhp, fhp->fh_export, dentry);
600216b6cbdSNamjae Jeon 	if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID)
6011da177e4SLinus Torvalds 		return nfserr_opnotsupp;
6021da177e4SLinus Torvalds 	return 0;
6031da177e4SLinus Torvalds out_bad:
6041da177e4SLinus Torvalds 	printk(KERN_ERR "fh_update: fh not verified!\n");
60549e73720SJ. Bruce Fields 	return nfserr_serverfault;
6061da177e4SLinus Torvalds out_negative:
60797e47fa1SAl Viro 	printk(KERN_ERR "fh_update: %pd2 still negative!\n",
60897e47fa1SAl Viro 		dentry);
60949e73720SJ. Bruce Fields 	return nfserr_serverfault;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
612fcb5e3faSChuck Lever /**
613fcb5e3faSChuck Lever  * fh_fill_pre_attrs - Fill in pre-op attributes
614fcb5e3faSChuck Lever  * @fhp: file handle to be updated
615fcb5e3faSChuck Lever  *
616fcb5e3faSChuck Lever  */
fh_fill_pre_attrs(struct svc_fh * fhp)617*a332018aSJeff Layton __be32 __must_check fh_fill_pre_attrs(struct svc_fh *fhp)
618fcb5e3faSChuck Lever {
619fcb5e3faSChuck Lever 	bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
620fcb5e3faSChuck Lever 	struct inode *inode;
621fcb5e3faSChuck Lever 	struct kstat stat;
622fcb5e3faSChuck Lever 	__be32 err;
623fcb5e3faSChuck Lever 
624fcb5e3faSChuck Lever 	if (fhp->fh_no_wcc || fhp->fh_pre_saved)
625*a332018aSJeff Layton 		return nfs_ok;
626fcb5e3faSChuck Lever 
627fcb5e3faSChuck Lever 	inode = d_inode(fhp->fh_dentry);
628fcb5e3faSChuck Lever 	err = fh_getattr(fhp, &stat);
629518f375cSJeff Layton 	if (err)
630*a332018aSJeff Layton 		return err;
631518f375cSJeff Layton 
632fcb5e3faSChuck Lever 	if (v4)
633fcb5e3faSChuck Lever 		fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
634fcb5e3faSChuck Lever 
635fcb5e3faSChuck Lever 	fhp->fh_pre_mtime = stat.mtime;
636fcb5e3faSChuck Lever 	fhp->fh_pre_ctime = stat.ctime;
637fcb5e3faSChuck Lever 	fhp->fh_pre_size  = stat.size;
638fcb5e3faSChuck Lever 	fhp->fh_pre_saved = true;
639*a332018aSJeff Layton 	return nfs_ok;
640fcb5e3faSChuck Lever }
641fcb5e3faSChuck Lever 
642fcb5e3faSChuck Lever /**
643fcb5e3faSChuck Lever  * fh_fill_post_attrs - Fill in post-op attributes
644fcb5e3faSChuck Lever  * @fhp: file handle to be updated
645fcb5e3faSChuck Lever  *
646fcb5e3faSChuck Lever  */
fh_fill_post_attrs(struct svc_fh * fhp)647*a332018aSJeff Layton __be32 fh_fill_post_attrs(struct svc_fh *fhp)
648fcb5e3faSChuck Lever {
649fcb5e3faSChuck Lever 	bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
650fcb5e3faSChuck Lever 	struct inode *inode = d_inode(fhp->fh_dentry);
651fcb5e3faSChuck Lever 	__be32 err;
652fcb5e3faSChuck Lever 
653fcb5e3faSChuck Lever 	if (fhp->fh_no_wcc)
654*a332018aSJeff Layton 		return nfs_ok;
655fcb5e3faSChuck Lever 
656fcb5e3faSChuck Lever 	if (fhp->fh_post_saved)
657fcb5e3faSChuck Lever 		printk("nfsd: inode locked twice during operation.\n");
658fcb5e3faSChuck Lever 
659fcb5e3faSChuck Lever 	err = fh_getattr(fhp, &fhp->fh_post_attr);
660518f375cSJeff Layton 	if (err)
661*a332018aSJeff Layton 		return err;
662518f375cSJeff Layton 
663fcb5e3faSChuck Lever 	fhp->fh_post_saved = true;
664fcb5e3faSChuck Lever 	if (v4)
665fcb5e3faSChuck Lever 		fhp->fh_post_change =
666fcb5e3faSChuck Lever 			nfsd4_change_attribute(&fhp->fh_post_attr, inode);
667*a332018aSJeff Layton 	return nfs_ok;
668fcb5e3faSChuck Lever }
669fcb5e3faSChuck Lever 
67019d008b4SNeilBrown /**
67119d008b4SNeilBrown  * fh_fill_both_attrs - Fill pre-op and post-op attributes
67219d008b4SNeilBrown  * @fhp: file handle to be updated
67319d008b4SNeilBrown  *
67419d008b4SNeilBrown  * This is used when the directory wasn't changed, but wcc attributes
67519d008b4SNeilBrown  * are needed anyway.
67619d008b4SNeilBrown  */
fh_fill_both_attrs(struct svc_fh * fhp)677*a332018aSJeff Layton __be32 __must_check fh_fill_both_attrs(struct svc_fh *fhp)
67819d008b4SNeilBrown {
679*a332018aSJeff Layton 	__be32 err;
680*a332018aSJeff Layton 
681*a332018aSJeff Layton 	err = fh_fill_post_attrs(fhp);
682*a332018aSJeff Layton 	if (err)
683*a332018aSJeff Layton 		return err;
684*a332018aSJeff Layton 
68519d008b4SNeilBrown 	fhp->fh_pre_change = fhp->fh_post_change;
68619d008b4SNeilBrown 	fhp->fh_pre_mtime = fhp->fh_post_attr.mtime;
68719d008b4SNeilBrown 	fhp->fh_pre_ctime = fhp->fh_post_attr.ctime;
68819d008b4SNeilBrown 	fhp->fh_pre_size = fhp->fh_post_attr.size;
68919d008b4SNeilBrown 	fhp->fh_pre_saved = true;
690*a332018aSJeff Layton 	return nfs_ok;
69119d008b4SNeilBrown }
69219d008b4SNeilBrown 
6931da177e4SLinus Torvalds /*
6941da177e4SLinus Torvalds  * Release a file handle.
6951da177e4SLinus Torvalds  */
6961da177e4SLinus Torvalds void
fh_put(struct svc_fh * fhp)6971da177e4SLinus Torvalds fh_put(struct svc_fh *fhp)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	struct dentry * dentry = fhp->fh_dentry;
7001da177e4SLinus Torvalds 	struct svc_export * exp = fhp->fh_export;
7011da177e4SLinus Torvalds 	if (dentry) {
7021da177e4SLinus Torvalds 		fhp->fh_dentry = NULL;
7031da177e4SLinus Torvalds 		dput(dentry);
704fcb5e3faSChuck Lever 		fh_clear_pre_post_attrs(fhp);
7051da177e4SLinus Torvalds 	}
7064a55c101SJan Kara 	fh_drop_write(fhp);
7071da177e4SLinus Torvalds 	if (exp) {
708a09581f2SStanislav Kinsbursky 		exp_put(exp);
7091da177e4SLinus Torvalds 		fhp->fh_export = NULL;
7101da177e4SLinus Torvalds 	}
711daab110eSJeff Layton 	fhp->fh_no_wcc = false;
7121da177e4SLinus Torvalds 	return;
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds /*
7161da177e4SLinus Torvalds  * Shorthand for dprintk()'s
7171da177e4SLinus Torvalds  */
SVCFH_fmt(struct svc_fh * fhp)7181da177e4SLinus Torvalds char * SVCFH_fmt(struct svc_fh *fhp)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	struct knfsd_fh *fh = &fhp->fh_handle;
721d8b26071SNeilBrown 	static char buf[2+1+1+64*3+1];
7221da177e4SLinus Torvalds 
723d8b26071SNeilBrown 	if (fh->fh_size < 0 || fh->fh_size> 64)
724d8b26071SNeilBrown 		return "bad-fh";
725d8b26071SNeilBrown 	sprintf(buf, "%d: %*ph", fh->fh_size, fh->fh_size, fh->fh_raw);
7261da177e4SLinus Torvalds 	return buf;
7271da177e4SLinus Torvalds }
728af6a4e28SNeilBrown 
fsid_source(const struct svc_fh * fhp)7292c42f804SChuck Lever enum fsid_source fsid_source(const struct svc_fh *fhp)
730af6a4e28SNeilBrown {
731af6a4e28SNeilBrown 	if (fhp->fh_handle.fh_version != 1)
732af6a4e28SNeilBrown 		return FSIDSOURCE_DEV;
733af6a4e28SNeilBrown 	switch(fhp->fh_handle.fh_fsid_type) {
734af6a4e28SNeilBrown 	case FSID_DEV:
735af6a4e28SNeilBrown 	case FSID_ENCODE_DEV:
736af6a4e28SNeilBrown 	case FSID_MAJOR_MINOR:
7378e498751SJ. Bruce Fields 		if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV)
738af6a4e28SNeilBrown 			return FSIDSOURCE_DEV;
739b8da0d1cSNeil Brown 		break;
740af6a4e28SNeilBrown 	case FSID_NUM:
741af6a4e28SNeilBrown 		if (fhp->fh_export->ex_flags & NFSEXP_FSID)
742af6a4e28SNeilBrown 			return FSIDSOURCE_FSID;
743b8da0d1cSNeil Brown 		break;
744b8da0d1cSNeil Brown 	default:
745b8da0d1cSNeil Brown 		break;
746af6a4e28SNeilBrown 	}
747b8da0d1cSNeil Brown 	/* either a UUID type filehandle, or the filehandle doesn't
748b8da0d1cSNeil Brown 	 * match the export.
749b8da0d1cSNeil Brown 	 */
750b8da0d1cSNeil Brown 	if (fhp->fh_export->ex_flags & NFSEXP_FSID)
751b8da0d1cSNeil Brown 		return FSIDSOURCE_FSID;
752b8da0d1cSNeil Brown 	if (fhp->fh_export->ex_uuid)
753b8da0d1cSNeil Brown 		return FSIDSOURCE_UUID;
754b8da0d1cSNeil Brown 	return FSIDSOURCE_DEV;
755af6a4e28SNeilBrown }
7563139b1d7SJeff Layton 
7573139b1d7SJeff Layton /*
758638e3e7dSJeff Layton  * We could use i_version alone as the change attribute.  However, i_version
759638e3e7dSJeff Layton  * can go backwards on a regular file after an unclean shutdown.  On its own
760638e3e7dSJeff Layton  * that doesn't necessarily cause a problem, but if i_version goes backwards
761638e3e7dSJeff Layton  * and then is incremented again it could reuse a value that was previously
762638e3e7dSJeff Layton  * used before boot, and a client who queried the two values might incorrectly
763638e3e7dSJeff Layton  * assume nothing changed.
7643139b1d7SJeff Layton  *
765638e3e7dSJeff Layton  * By using both ctime and the i_version counter we guarantee that as long as
766638e3e7dSJeff Layton  * time doesn't go backwards we never reuse an old value. If the filesystem
767638e3e7dSJeff Layton  * advertises STATX_ATTR_CHANGE_MONOTONIC, then this mitigation is not
768638e3e7dSJeff Layton  * needed.
769638e3e7dSJeff Layton  *
770638e3e7dSJeff Layton  * We only need to do this for regular files as well. For directories, we
771638e3e7dSJeff Layton  * assume that the new change attr is always logged to stable storage in some
772638e3e7dSJeff Layton  * fashion before the results can be seen.
7733139b1d7SJeff Layton  */
nfsd4_change_attribute(struct kstat * stat,struct inode * inode)7743139b1d7SJeff Layton u64 nfsd4_change_attribute(struct kstat *stat, struct inode *inode)
7753139b1d7SJeff Layton {
7763139b1d7SJeff Layton 	u64 chattr;
7773139b1d7SJeff Layton 
778638e3e7dSJeff Layton 	if (stat->result_mask & STATX_CHANGE_COOKIE) {
779638e3e7dSJeff Layton 		chattr = stat->change_cookie;
780638e3e7dSJeff Layton 		if (S_ISREG(inode->i_mode) &&
781638e3e7dSJeff Layton 		    !(stat->attributes & STATX_ATTR_CHANGE_MONOTONIC)) {
782638e3e7dSJeff Layton 			chattr += (u64)stat->ctime.tv_sec << 30;
7833139b1d7SJeff Layton 			chattr += stat->ctime.tv_nsec;
784638e3e7dSJeff Layton 		}
785638e3e7dSJeff Layton 	} else {
786638e3e7dSJeff Layton 		chattr = time_to_chattr(&stat->ctime);
787638e3e7dSJeff Layton 	}
7883139b1d7SJeff Layton 	return chattr;
7893139b1d7SJeff Layton }
790