xref: /openbmc/linux/fs/nfsd/nfsfh.c (revision 20ad856e47323e208ae8d6a9ecfe5bf0be6f505e)
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  */
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);
432b0143b5SDavid Howells 		err = inode_permission(d_inode(parent), MAY_EXEC);
441da177e4SLinus Torvalds 		if (err < 0) {
451da177e4SLinus Torvalds 			dput(parent);
461da177e4SLinus Torvalds 			break;
471da177e4SLinus Torvalds 		}
481da177e4SLinus Torvalds 		dput(tdentry);
491da177e4SLinus Torvalds 		tdentry = parent;
501da177e4SLinus Torvalds 	}
5154775491SJan Blunck 	if (tdentry != exp->ex_path.dentry)
5297e47fa1SAl Viro 		dprintk("nfsd_acceptable failed at %p %pd\n", tdentry, tdentry);
5354775491SJan Blunck 	rv = (tdentry == exp->ex_path.dentry);
541da177e4SLinus Torvalds 	dput(tdentry);
551da177e4SLinus Torvalds 	return rv;
561da177e4SLinus Torvalds }
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds /* Type check. The correct error return for type mismatches does not seem to be
591da177e4SLinus Torvalds  * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
601da177e4SLinus Torvalds  * comment in the NFSv3 spec says this is incorrect (implementation notes for
611da177e4SLinus Torvalds  * the write call).
621da177e4SLinus Torvalds  */
6383b11340SAl Viro static inline __be32
64e75b23f9SJ. Bruce Fields nfsd_mode_check(struct svc_rqst *rqstp, struct dentry *dentry,
65e75b23f9SJ. Bruce Fields 		umode_t requested)
661da177e4SLinus Torvalds {
67e75b23f9SJ. Bruce Fields 	umode_t mode = d_inode(dentry)->i_mode & S_IFMT;
68e10f9e14SJ. Bruce Fields 
69e10f9e14SJ. Bruce Fields 	if (requested == 0) /* the caller doesn't care */
70e10f9e14SJ. Bruce Fields 		return nfs_ok;
71e75b23f9SJ. Bruce Fields 	if (mode == requested) {
72e75b23f9SJ. Bruce Fields 		if (mode == S_IFDIR && !d_can_lookup(dentry)) {
73e75b23f9SJ. Bruce Fields 			WARN_ON_ONCE(1);
74e75b23f9SJ. Bruce Fields 			return nfserr_notdir;
75e75b23f9SJ. Bruce Fields 		}
76e10f9e14SJ. Bruce Fields 		return nfs_ok;
77e75b23f9SJ. Bruce Fields 	}
78e10f9e14SJ. Bruce Fields 	/*
79e10f9e14SJ. Bruce Fields 	 * v4 has an error more specific than err_notdir which we should
80e10f9e14SJ. Bruce Fields 	 * return in preference to err_notdir:
81e10f9e14SJ. Bruce Fields 	 */
82e10f9e14SJ. Bruce Fields 	if (rqstp->rq_vers == 4 && mode == S_IFLNK)
831da177e4SLinus Torvalds 		return nfserr_symlink;
84e10f9e14SJ. Bruce Fields 	if (requested == S_IFDIR)
851da177e4SLinus Torvalds 		return nfserr_notdir;
86e10f9e14SJ. Bruce Fields 	if (mode == S_IFDIR)
871da177e4SLinus Torvalds 		return nfserr_isdir;
881da177e4SLinus Torvalds 	return nfserr_inval;
891da177e4SLinus Torvalds }
901da177e4SLinus Torvalds 
919d7ed135SJ. Bruce Fields static bool nfsd_originating_port_ok(struct svc_rqst *rqstp, int flags)
929d7ed135SJ. Bruce Fields {
939d7ed135SJ. Bruce Fields 	if (flags & NFSEXP_INSECURE_PORT)
949d7ed135SJ. Bruce Fields 		return true;
959d7ed135SJ. Bruce Fields 	/* We don't require gss requests to use low ports: */
969d7ed135SJ. Bruce Fields 	if (rqstp->rq_cred.cr_flavor >= RPC_AUTH_GSS)
979d7ed135SJ. Bruce Fields 		return true;
989d7ed135SJ. Bruce Fields 	return test_bit(RQ_SECURE, &rqstp->rq_flags);
999d7ed135SJ. Bruce Fields }
1009d7ed135SJ. Bruce Fields 
1016fa02839SJ. Bruce Fields static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
1026fa02839SJ. Bruce Fields 					  struct svc_export *exp)
1036fa02839SJ. Bruce Fields {
10412045a6eSJ. Bruce Fields 	int flags = nfsexp_flags(rqstp, exp);
10512045a6eSJ. Bruce Fields 
1066fa02839SJ. Bruce Fields 	/* Check if the request originated from a secure port. */
1079d7ed135SJ. Bruce Fields 	if (!nfsd_originating_port_ok(rqstp, flags)) {
1085216a8e7SPavel Emelyanov 		RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
109a48fd0f9SKinglong Mee 		dprintk("nfsd: request from insecure port %s!\n",
1106fa02839SJ. Bruce Fields 		        svc_print_addr(rqstp, buf, sizeof(buf)));
1116fa02839SJ. Bruce Fields 		return nfserr_perm;
1126fa02839SJ. Bruce Fields 	}
1136fa02839SJ. Bruce Fields 
1146fa02839SJ. Bruce Fields 	/* Set user creds for this exportpoint */
1156fa02839SJ. Bruce Fields 	return nfserrno(nfsd_setuser(rqstp, exp));
1166fa02839SJ. Bruce Fields }
1176fa02839SJ. Bruce Fields 
11803a816b4SSteve Dickson static inline __be32 check_pseudo_root(struct svc_rqst *rqstp,
11903a816b4SSteve Dickson 	struct dentry *dentry, struct svc_export *exp)
12003a816b4SSteve Dickson {
12103a816b4SSteve Dickson 	if (!(exp->ex_flags & NFSEXP_V4ROOT))
12203a816b4SSteve Dickson 		return nfs_ok;
12303a816b4SSteve Dickson 	/*
12403a816b4SSteve Dickson 	 * v2/v3 clients have no need for the V4ROOT export--they use
12503a816b4SSteve Dickson 	 * the mount protocl instead; also, further V4ROOT checks may be
12603a816b4SSteve Dickson 	 * in v4-specific code, in which case v2/v3 clients could bypass
12703a816b4SSteve Dickson 	 * them.
12803a816b4SSteve Dickson 	 */
12903a816b4SSteve Dickson 	if (!nfsd_v4client(rqstp))
13003a816b4SSteve Dickson 		return nfserr_stale;
13103a816b4SSteve Dickson 	/*
13203a816b4SSteve Dickson 	 * We're exposing only the directories and symlinks that have to be
13303a816b4SSteve Dickson 	 * traversed on the way to real exports:
13403a816b4SSteve Dickson 	 */
135e36cb0b8SDavid Howells 	if (unlikely(!d_is_dir(dentry) &&
136e36cb0b8SDavid Howells 		     !d_is_symlink(dentry)))
13703a816b4SSteve Dickson 		return nfserr_stale;
13803a816b4SSteve Dickson 	/*
13903a816b4SSteve Dickson 	 * A pseudoroot export gives permission to access only one
14003a816b4SSteve Dickson 	 * single directory; the kernel has to make another upcall
14103a816b4SSteve Dickson 	 * before granting access to anything else under it:
14203a816b4SSteve Dickson 	 */
14303a816b4SSteve Dickson 	if (unlikely(dentry != exp->ex_path.dentry))
14403a816b4SSteve Dickson 		return nfserr_stale;
14503a816b4SSteve Dickson 	return nfs_ok;
14603a816b4SSteve Dickson }
14703a816b4SSteve Dickson 
1481da177e4SLinus Torvalds /*
14903550facSJ. Bruce Fields  * Use the given filehandle to look up the corresponding export and
15003550facSJ. Bruce Fields  * dentry.  On success, the results are used to set fh_export and
15103550facSJ. Bruce Fields  * fh_dentry.
1521da177e4SLinus Torvalds  */
15303550facSJ. Bruce Fields static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds 	struct knfsd_fh	*fh = &fhp->fh_handle;
1566e91ea2bSChristoph Hellwig 	struct fid *fid = NULL, sfid;
15703550facSJ. Bruce Fields 	struct svc_export *exp;
15803550facSJ. Bruce Fields 	struct dentry *dentry;
1591da177e4SLinus Torvalds 	int fileid_type;
1601da177e4SLinus Torvalds 	int data_left = fh->fh_size/4;
16103550facSJ. Bruce Fields 	__be32 error;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	error = nfserr_stale;
1641da177e4SLinus Torvalds 	if (rqstp->rq_vers > 2)
1651da177e4SLinus Torvalds 		error = nfserr_badhandle;
1661da177e4SLinus Torvalds 	if (rqstp->rq_vers == 4 && fh->fh_size == 0)
1671da177e4SLinus Torvalds 		return nfserr_nofilehandle;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	if (fh->fh_version == 1) {
1701da177e4SLinus Torvalds 		int len;
17103550facSJ. Bruce Fields 
17203550facSJ. Bruce Fields 		if (--data_left < 0)
17303550facSJ. Bruce Fields 			return error;
17403550facSJ. Bruce Fields 		if (fh->fh_auth_type != 0)
17503550facSJ. Bruce Fields 			return error;
1761da177e4SLinus Torvalds 		len = key_len(fh->fh_fsid_type) / 4;
17703550facSJ. Bruce Fields 		if (len == 0)
17803550facSJ. Bruce Fields 			return error;
179af6a4e28SNeilBrown 		if  (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
1801da177e4SLinus Torvalds 			/* deprecated, convert to type 3 */
181af6a4e28SNeilBrown 			len = key_len(FSID_ENCODE_DEV)/4;
182af6a4e28SNeilBrown 			fh->fh_fsid_type = FSID_ENCODE_DEV;
18394ec938bSJeff Layton 			/*
18494ec938bSJeff Layton 			 * struct knfsd_fh uses host-endian fields, which are
18594ec938bSJeff Layton 			 * sometimes used to hold net-endian values. This
18694ec938bSJeff Layton 			 * confuses sparse, so we must use __force here to
18794ec938bSJeff Layton 			 * keep it from complaining.
18894ec938bSJeff Layton 			 */
18994ec938bSJeff Layton 			fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl((__force __be32)fh->fh_fsid[0]),
19094ec938bSJeff Layton 							ntohl((__force __be32)fh->fh_fsid[1])));
1911da177e4SLinus Torvalds 			fh->fh_fsid[1] = fh->fh_fsid[2];
1921da177e4SLinus Torvalds 		}
19303550facSJ. Bruce Fields 		data_left -= len;
19403550facSJ. Bruce Fields 		if (data_left < 0)
19503550facSJ. Bruce Fields 			return error;
1965409e46fSChristoph Hellwig 		exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_fsid);
1975409e46fSChristoph Hellwig 		fid = (struct fid *)(fh->fh_fsid + len);
1981da177e4SLinus Torvalds 	} else {
1996e91ea2bSChristoph Hellwig 		__u32 tfh[2];
2001da177e4SLinus Torvalds 		dev_t xdev;
2011da177e4SLinus Torvalds 		ino_t xino;
20203550facSJ. Bruce Fields 
2031da177e4SLinus Torvalds 		if (fh->fh_size != NFS_FHSIZE)
20403550facSJ. Bruce Fields 			return error;
2051da177e4SLinus Torvalds 		/* assume old filehandle format */
2061da177e4SLinus Torvalds 		xdev = old_decode_dev(fh->ofh_xdev);
2071da177e4SLinus Torvalds 		xino = u32_to_ino_t(fh->ofh_xino);
208af6a4e28SNeilBrown 		mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
2090989a788SJ. Bruce Fields 		exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
2101da177e4SLinus Torvalds 	}
2111da177e4SLinus Torvalds 
2122d3bb252SJ. Bruce Fields 	error = nfserr_stale;
213f01274a9STrond Myklebust 	if (IS_ERR(exp)) {
214f01274a9STrond Myklebust 		trace_nfsd_set_fh_dentry_badexport(rqstp, fhp, PTR_ERR(exp));
215f01274a9STrond Myklebust 
2162d3bb252SJ. Bruce Fields 		if (PTR_ERR(exp) == -ENOENT)
21703550facSJ. Bruce Fields 			return error;
2182d3bb252SJ. Bruce Fields 
21903550facSJ. Bruce Fields 		return nfserrno(PTR_ERR(exp));
220f01274a9STrond Myklebust 	}
2211da177e4SLinus Torvalds 
222496d6c32SNeil Brown 	if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
223496d6c32SNeil Brown 		/* Elevate privileges so that the lack of 'r' or 'x'
224496d6c32SNeil Brown 		 * permission on some parent directory will
225496d6c32SNeil Brown 		 * not stop exportfs_decode_fh from being able
226496d6c32SNeil Brown 		 * to reconnect a directory into the dentry cache.
227496d6c32SNeil Brown 		 * The same problem can affect "SUBTREECHECK" exports,
228496d6c32SNeil Brown 		 * but as nfsd_acceptable depends on correct
229496d6c32SNeil Brown 		 * access control settings being in effect, we cannot
230496d6c32SNeil Brown 		 * fix that case easily.
231496d6c32SNeil Brown 		 */
232d84f4f99SDavid Howells 		struct cred *new = prepare_creds();
233027bc41aSKinglong Mee 		if (!new) {
234027bc41aSKinglong Mee 			error =  nfserrno(-ENOMEM);
235027bc41aSKinglong Mee 			goto out;
236027bc41aSKinglong Mee 		}
237d84f4f99SDavid Howells 		new->cap_effective =
238d84f4f99SDavid Howells 			cap_raise_nfsd_set(new->cap_effective,
239d84f4f99SDavid Howells 					   new->cap_permitted);
240d84f4f99SDavid Howells 		put_cred(override_creds(new));
241d84f4f99SDavid Howells 		put_cred(new);
242496d6c32SNeil Brown 	} else {
2436fa02839SJ. Bruce Fields 		error = nfsd_setuser_and_check_port(rqstp, exp);
244d1bbf14fSNeilBrown 		if (error)
245d1bbf14fSNeilBrown 			goto out;
246496d6c32SNeil Brown 	}
247d1bbf14fSNeilBrown 
2481da177e4SLinus Torvalds 	/*
2491da177e4SLinus Torvalds 	 * Look up the dentry using the NFS file handle.
2501da177e4SLinus Torvalds 	 */
2511da177e4SLinus Torvalds 	error = nfserr_stale;
2521da177e4SLinus Torvalds 	if (rqstp->rq_vers > 2)
2531da177e4SLinus Torvalds 		error = nfserr_badhandle;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	if (fh->fh_version != 1) {
2566e91ea2bSChristoph Hellwig 		sfid.i32.ino = fh->ofh_ino;
2576e91ea2bSChristoph Hellwig 		sfid.i32.gen = fh->ofh_generation;
2586e91ea2bSChristoph Hellwig 		sfid.i32.parent_ino = fh->ofh_dirino;
2596e91ea2bSChristoph Hellwig 		fid = &sfid;
2601da177e4SLinus Torvalds 		data_left = 3;
2611da177e4SLinus Torvalds 		if (fh->ofh_dirino == 0)
2626e91ea2bSChristoph Hellwig 			fileid_type = FILEID_INO32_GEN;
2631da177e4SLinus Torvalds 		else
2646e91ea2bSChristoph Hellwig 			fileid_type = FILEID_INO32_GEN_PARENT;
2651da177e4SLinus Torvalds 	} else
2661da177e4SLinus Torvalds 		fileid_type = fh->fh_fileid_type;
2671da177e4SLinus Torvalds 
2686e91ea2bSChristoph Hellwig 	if (fileid_type == FILEID_ROOT)
26954775491SJan Blunck 		dentry = dget(exp->ex_path.dentry);
2701da177e4SLinus Torvalds 	else {
2712e19d10cSTrond Myklebust 		dentry = exportfs_decode_fh_raw(exp->ex_path.mnt, fid,
272d37065cdSChristoph Hellwig 						data_left, fileid_type,
2731da177e4SLinus Torvalds 						nfsd_acceptable, exp);
2742e19d10cSTrond Myklebust 		if (IS_ERR_OR_NULL(dentry)) {
275f01274a9STrond Myklebust 			trace_nfsd_set_fh_dentry_badhandle(rqstp, fhp,
276f01274a9STrond Myklebust 					dentry ?  PTR_ERR(dentry) : -ESTALE);
2772e19d10cSTrond Myklebust 			switch (PTR_ERR(dentry)) {
2782e19d10cSTrond Myklebust 			case -ENOMEM:
2792e19d10cSTrond Myklebust 			case -ETIMEDOUT:
2802e19d10cSTrond Myklebust 				break;
2812e19d10cSTrond Myklebust 			default:
2822e19d10cSTrond Myklebust 				dentry = ERR_PTR(-ESTALE);
2832e19d10cSTrond Myklebust 			}
2842e19d10cSTrond Myklebust 		}
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 	if (dentry == NULL)
2871da177e4SLinus Torvalds 		goto out;
2881da177e4SLinus Torvalds 	if (IS_ERR(dentry)) {
2891da177e4SLinus Torvalds 		if (PTR_ERR(dentry) != -EINVAL)
2901da177e4SLinus Torvalds 			error = nfserrno(PTR_ERR(dentry));
2911da177e4SLinus Torvalds 		goto out;
2921da177e4SLinus Torvalds 	}
29334e9a63bSNeilBrown 
294e36cb0b8SDavid Howells 	if (d_is_dir(dentry) &&
2951da177e4SLinus Torvalds 			(dentry->d_flags & DCACHE_DISCONNECTED)) {
29697e47fa1SAl Viro 		printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %pd2\n",
29797e47fa1SAl Viro 				dentry);
2981da177e4SLinus Torvalds 	}
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	fhp->fh_dentry = dentry;
3011da177e4SLinus Torvalds 	fhp->fh_export = exp;
302daab110eSJeff Layton 
303daab110eSJeff Layton 	switch (rqstp->rq_vers) {
304716a8bc7STrond Myklebust 	case 4:
305716a8bc7STrond Myklebust 		if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOATOMIC_ATTR)
306716a8bc7STrond Myklebust 			fhp->fh_no_atomic_attr = true;
307716a8bc7STrond Myklebust 		break;
308daab110eSJeff Layton 	case 3:
309daab110eSJeff Layton 		if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOWCC)
310daab110eSJeff Layton 			fhp->fh_no_wcc = true;
311daab110eSJeff Layton 		break;
312daab110eSJeff Layton 	case 2:
313daab110eSJeff Layton 		fhp->fh_no_wcc = true;
314daab110eSJeff Layton 	}
315daab110eSJeff Layton 
31603550facSJ. Bruce Fields 	return 0;
31703550facSJ. Bruce Fields out:
31803550facSJ. Bruce Fields 	exp_put(exp);
31903550facSJ. Bruce Fields 	return error;
32003550facSJ. Bruce Fields }
32103550facSJ. Bruce Fields 
322b3d47676SJ. Bruce Fields /**
323b3d47676SJ. Bruce Fields  * fh_verify - filehandle lookup and access checking
324b3d47676SJ. Bruce Fields  * @rqstp: pointer to current rpc request
325b3d47676SJ. Bruce Fields  * @fhp: filehandle to be verified
326b3d47676SJ. Bruce Fields  * @type: expected type of object pointed to by filehandle
327b3d47676SJ. Bruce Fields  * @access: type of access needed to object
32803550facSJ. Bruce Fields  *
329b3d47676SJ. Bruce Fields  * Look up a dentry from the on-the-wire filehandle, check the client's
330b3d47676SJ. Bruce Fields  * access to the export, and set the current task's credentials.
33103550facSJ. Bruce Fields  *
332b3d47676SJ. Bruce Fields  * Regardless of success or failure of fh_verify(), fh_put() should be
333b3d47676SJ. Bruce Fields  * called on @fhp when the caller is finished with the filehandle.
334b3d47676SJ. Bruce Fields  *
335b3d47676SJ. Bruce Fields  * fh_verify() may be called multiple times on a given filehandle, for
336b3d47676SJ. Bruce Fields  * example, when processing an NFSv4 compound.  The first call will look
337b3d47676SJ. Bruce Fields  * up a dentry using the on-the-wire filehandle.  Subsequent calls will
338b3d47676SJ. Bruce Fields  * skip the lookup and just perform the other checks and possibly change
339b3d47676SJ. Bruce Fields  * the current task's credentials.
340b3d47676SJ. Bruce Fields  *
341b3d47676SJ. Bruce Fields  * @type specifies the type of object expected using one of the S_IF*
342b3d47676SJ. Bruce Fields  * constants defined in include/linux/stat.h.  The caller may use zero
343b3d47676SJ. Bruce Fields  * to indicate that it doesn't care, or a negative integer to indicate
344b3d47676SJ. Bruce Fields  * that it expects something not of the given type.
345b3d47676SJ. Bruce Fields  *
346b3d47676SJ. Bruce Fields  * @access is formed from the NFSD_MAY_* constants defined in
34793f580a9SOleg Drokin  * fs/nfsd/vfs.h.
34803550facSJ. Bruce Fields  */
34903550facSJ. Bruce Fields __be32
350175a4eb7SAl Viro fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, int access)
35103550facSJ. Bruce Fields {
352*20ad856eSAmir Goldstein 	struct svc_export *exp = NULL;
35303550facSJ. Bruce Fields 	struct dentry	*dentry;
35403550facSJ. Bruce Fields 	__be32		error;
35503550facSJ. Bruce Fields 
35603550facSJ. Bruce Fields 	dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
35703550facSJ. Bruce Fields 
35803550facSJ. Bruce Fields 	if (!fhp->fh_dentry) {
35903550facSJ. Bruce Fields 		error = nfsd_set_fh_dentry(rqstp, fhp);
36003550facSJ. Bruce Fields 		if (error)
36103550facSJ. Bruce Fields 			goto out;
362864f0f61SJ. Bruce Fields 	}
3631da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
3641da177e4SLinus Torvalds 	exp = fhp->fh_export;
3656fa02839SJ. Bruce Fields 	/*
366864f0f61SJ. Bruce Fields 	 * We still have to do all these permission checks, even when
367864f0f61SJ. Bruce Fields 	 * fh_dentry is already set:
368864f0f61SJ. Bruce Fields 	 * 	- fh_verify may be called multiple times with different
369864f0f61SJ. Bruce Fields 	 * 	  "access" arguments (e.g. nfsd_proc_create calls
370864f0f61SJ. Bruce Fields 	 * 	  fh_verify(...,NFSD_MAY_EXEC) first, then later (in
371864f0f61SJ. Bruce Fields 	 * 	  nfsd_create) calls fh_verify(...,NFSD_MAY_CREATE).
372864f0f61SJ. Bruce Fields 	 *	- in the NFSv4 case, the filehandle may have been filled
373864f0f61SJ. Bruce Fields 	 *	  in by fh_compose, and given a dentry, but further
374864f0f61SJ. Bruce Fields 	 *	  compound operations performed with that filehandle
375864f0f61SJ. Bruce Fields 	 *	  still need permissions checks.  In the worst case, a
376864f0f61SJ. Bruce Fields 	 *	  mountpoint crossing may have changed the export
377864f0f61SJ. Bruce Fields 	 *	  options, and we may now need to use a different uid
378864f0f61SJ. Bruce Fields 	 *	  (for example, if different id-squashing options are in
379864f0f61SJ. Bruce Fields 	 *	  effect on the new filesystem).
3806fa02839SJ. Bruce Fields 	 */
38103a816b4SSteve Dickson 	error = check_pseudo_root(rqstp, dentry, exp);
38203a816b4SSteve Dickson 	if (error)
38303a816b4SSteve Dickson 		goto out;
38403a816b4SSteve Dickson 
3856fa02839SJ. Bruce Fields 	error = nfsd_setuser_and_check_port(rqstp, exp);
3867fc90ec9SJ. Bruce Fields 	if (error)
3877fc90ec9SJ. Bruce Fields 		goto out;
3887fc90ec9SJ. Bruce Fields 
389e75b23f9SJ. Bruce Fields 	error = nfsd_mode_check(rqstp, dentry, type);
3901da177e4SLinus Torvalds 	if (error)
3911da177e4SLinus Torvalds 		goto out;
3921da177e4SLinus Torvalds 
3939091224fSJ. Bruce Fields 	/*
3949091224fSJ. Bruce Fields 	 * pseudoflavor restrictions are not enforced on NLM,
3959091224fSJ. Bruce Fields 	 * which clients virtually always use auth_sys for,
3969091224fSJ. Bruce Fields 	 * even while using RPCSEC_GSS for NFS.
3979091224fSJ. Bruce Fields 	 */
398204f4ce7SJ. Bruce Fields 	if (access & NFSD_MAY_LOCK || access & NFSD_MAY_BYPASS_GSS)
39904716e66SJ. Bruce Fields 		goto skip_pseudoflavor_check;
40004716e66SJ. Bruce Fields 	/*
40104716e66SJ. Bruce Fields 	 * Clients may expect to be able to use auth_sys during mount,
40204716e66SJ. Bruce Fields 	 * even if they use gss for everything else; see section 2.3.2
40304716e66SJ. Bruce Fields 	 * of rfc 2623.
40404716e66SJ. Bruce Fields 	 */
40504716e66SJ. Bruce Fields 	if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
40604716e66SJ. Bruce Fields 			&& exp->ex_path.dentry == dentry)
40704716e66SJ. Bruce Fields 		goto skip_pseudoflavor_check;
40804716e66SJ. Bruce Fields 
40932c1eb0cSAndy Adamson 	error = check_nfsd_access(exp, rqstp);
41032c1eb0cSAndy Adamson 	if (error)
41132c1eb0cSAndy Adamson 		goto out;
41232c1eb0cSAndy Adamson 
41304716e66SJ. Bruce Fields skip_pseudoflavor_check:
4141da177e4SLinus Torvalds 	/* Finally, check access permissions. */
4150ec757dfSJ. Bruce Fields 	error = nfsd_permission(rqstp, exp, dentry, access);
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	if (error) {
41897e47fa1SAl Viro 		dprintk("fh_verify: %pd2 permission failure, "
41934e9a63bSNeilBrown 			"acc=%x, error=%d\n",
42097e47fa1SAl Viro 			dentry,
421fc2dd2e5SAl Viro 			access, ntohl(error));
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds out:
4241da177e4SLinus Torvalds 	if (error == nfserr_stale)
425*20ad856eSAmir Goldstein 		nfsd_stats_fh_stale_inc(exp);
4261da177e4SLinus Torvalds 	return error;
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds /*
4311da177e4SLinus Torvalds  * Compose a file handle for an NFS reply.
4321da177e4SLinus Torvalds  *
4331da177e4SLinus Torvalds  * Note that when first composed, the dentry may not yet have
4341da177e4SLinus Torvalds  * an inode.  In this case a call to fh_update should be made
4351da177e4SLinus Torvalds  * before the fh goes out on the wire ...
4361da177e4SLinus Torvalds  */
4376e91ea2bSChristoph Hellwig static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
4386e91ea2bSChristoph Hellwig 		struct dentry *dentry)
4391da177e4SLinus Torvalds {
44054775491SJan Blunck 	if (dentry != exp->ex_path.dentry) {
4416e91ea2bSChristoph Hellwig 		struct fid *fid = (struct fid *)
4425409e46fSChristoph Hellwig 			(fhp->fh_handle.fh_fsid + fhp->fh_handle.fh_size/4 - 1);
4436e91ea2bSChristoph Hellwig 		int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
4446e91ea2bSChristoph Hellwig 		int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
4451da177e4SLinus Torvalds 
4466e91ea2bSChristoph Hellwig 		fhp->fh_handle.fh_fileid_type =
4476e91ea2bSChristoph Hellwig 			exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
4486e91ea2bSChristoph Hellwig 		fhp->fh_handle.fh_size += maxsize * 4;
4496e91ea2bSChristoph Hellwig 	} else {
4506e91ea2bSChristoph Hellwig 		fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
4516e91ea2bSChristoph Hellwig 	}
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds /*
4551da177e4SLinus Torvalds  * for composing old style file handles
4561da177e4SLinus Torvalds  */
4571da177e4SLinus Torvalds static inline void _fh_update_old(struct dentry *dentry,
4581da177e4SLinus Torvalds 				  struct svc_export *exp,
4591da177e4SLinus Torvalds 				  struct knfsd_fh *fh)
4601da177e4SLinus Torvalds {
4612b0143b5SDavid Howells 	fh->ofh_ino = ino_t_to_u32(d_inode(dentry)->i_ino);
4622b0143b5SDavid Howells 	fh->ofh_generation = d_inode(dentry)->i_generation;
463e36cb0b8SDavid Howells 	if (d_is_dir(dentry) ||
4641da177e4SLinus Torvalds 	    (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
4651da177e4SLinus Torvalds 		fh->ofh_dirino = 0;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4688e498751SJ. Bruce Fields static bool is_root_export(struct svc_export *exp)
4698e498751SJ. Bruce Fields {
4708e498751SJ. Bruce Fields 	return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root;
4718e498751SJ. Bruce Fields }
4728e498751SJ. Bruce Fields 
4738e498751SJ. Bruce Fields static struct super_block *exp_sb(struct svc_export *exp)
4748e498751SJ. Bruce Fields {
475fc64005cSAl Viro 	return exp->ex_path.dentry->d_sb;
4768e498751SJ. Bruce Fields }
4778e498751SJ. Bruce Fields 
4788e498751SJ. Bruce Fields static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)
4798e498751SJ. Bruce Fields {
4808e498751SJ. Bruce Fields 	switch (fsid_type) {
4818e498751SJ. Bruce Fields 	case FSID_DEV:
4828e498751SJ. Bruce Fields 		if (!old_valid_dev(exp_sb(exp)->s_dev))
483a677a783SGustavo A. R. Silva 			return false;
484df561f66SGustavo A. R. Silva 		fallthrough;
4858e498751SJ. Bruce Fields 	case FSID_MAJOR_MINOR:
4868e498751SJ. Bruce Fields 	case FSID_ENCODE_DEV:
4878e498751SJ. Bruce Fields 		return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV;
4888e498751SJ. Bruce Fields 	case FSID_NUM:
4898e498751SJ. Bruce Fields 		return exp->ex_flags & NFSEXP_FSID;
4908e498751SJ. Bruce Fields 	case FSID_UUID8:
4918e498751SJ. Bruce Fields 	case FSID_UUID16:
4928e498751SJ. Bruce Fields 		if (!is_root_export(exp))
493a677a783SGustavo A. R. Silva 			return false;
494df561f66SGustavo A. R. Silva 		fallthrough;
4958e498751SJ. Bruce Fields 	case FSID_UUID4_INUM:
4968e498751SJ. Bruce Fields 	case FSID_UUID16_INUM:
4978e498751SJ. Bruce Fields 		return exp->ex_uuid != NULL;
4988e498751SJ. Bruce Fields 	}
499a677a783SGustavo A. R. Silva 	return true;
5008e498751SJ. Bruce Fields }
5018e498751SJ. Bruce Fields 
502bc6c53d5SJ. Bruce Fields 
503bc6c53d5SJ. Bruce Fields static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh)
5041da177e4SLinus Torvalds {
505b41eeef1SNeilBrown 	u8 version;
506bc6c53d5SJ. Bruce Fields 	u8 fsid_type;
507b41eeef1SNeilBrown retry:
508b41eeef1SNeilBrown 	version = 1;
5097e405364SNeilBrown 	if (ref_fh && ref_fh->fh_export == exp) {
510982aedfdSNeilBrown 		version = ref_fh->fh_handle.fh_version;
511982aedfdSNeilBrown 		fsid_type = ref_fh->fh_handle.fh_fsid_type;
512b41eeef1SNeilBrown 
513b41eeef1SNeilBrown 		ref_fh = NULL;
514b41eeef1SNeilBrown 
515b41eeef1SNeilBrown 		switch (version) {
516b41eeef1SNeilBrown 		case 0xca:
517b41eeef1SNeilBrown 			fsid_type = FSID_DEV;
518b41eeef1SNeilBrown 			break;
519b41eeef1SNeilBrown 		case 1:
520b41eeef1SNeilBrown 			break;
521b41eeef1SNeilBrown 		default:
522b41eeef1SNeilBrown 			goto retry;
523b41eeef1SNeilBrown 		}
524b41eeef1SNeilBrown 
5258e498751SJ. Bruce Fields 		/*
5268e498751SJ. Bruce Fields 		 * As the fsid -> filesystem mapping was guided by
5278e498751SJ. Bruce Fields 		 * user-space, there is no guarantee that the filesystem
5288e498751SJ. Bruce Fields 		 * actually supports that fsid type. If it doesn't we
5298e498751SJ. Bruce Fields 		 * loop around again without ref_fh set.
530982aedfdSNeilBrown 		 */
5318e498751SJ. Bruce Fields 		if (!fsid_type_ok_for_exp(fsid_type, exp))
532b41eeef1SNeilBrown 			goto retry;
53330fa8c01SSteve Dickson 	} else if (exp->ex_flags & NFSEXP_FSID) {
53430fa8c01SSteve Dickson 		fsid_type = FSID_NUM;
535af6a4e28SNeilBrown 	} else if (exp->ex_uuid) {
536af6a4e28SNeilBrown 		if (fhp->fh_maxsize >= 64) {
5378e498751SJ. Bruce Fields 			if (is_root_export(exp))
538af6a4e28SNeilBrown 				fsid_type = FSID_UUID16;
539af6a4e28SNeilBrown 			else
540af6a4e28SNeilBrown 				fsid_type = FSID_UUID16_INUM;
541af6a4e28SNeilBrown 		} else {
5428e498751SJ. Bruce Fields 			if (is_root_export(exp))
543af6a4e28SNeilBrown 				fsid_type = FSID_UUID8;
544af6a4e28SNeilBrown 			else
545af6a4e28SNeilBrown 				fsid_type = FSID_UUID4_INUM;
546af6a4e28SNeilBrown 		}
547bc6c53d5SJ. Bruce Fields 	} else if (!old_valid_dev(exp_sb(exp)->s_dev))
5481da177e4SLinus Torvalds 		/* for newer device numbers, we must use a newer fsid format */
549af6a4e28SNeilBrown 		fsid_type = FSID_ENCODE_DEV;
550982aedfdSNeilBrown 	else
551af6a4e28SNeilBrown 		fsid_type = FSID_DEV;
552bc6c53d5SJ. Bruce Fields 	fhp->fh_handle.fh_version = version;
553bc6c53d5SJ. Bruce Fields 	if (version)
554bc6c53d5SJ. Bruce Fields 		fhp->fh_handle.fh_fsid_type = fsid_type;
555bc6c53d5SJ. Bruce Fields }
556bc6c53d5SJ. Bruce Fields 
557bc6c53d5SJ. Bruce Fields __be32
558bc6c53d5SJ. Bruce Fields fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
559bc6c53d5SJ. Bruce Fields 	   struct svc_fh *ref_fh)
560bc6c53d5SJ. Bruce Fields {
561bc6c53d5SJ. Bruce Fields 	/* ref_fh is a reference file handle.
562bc6c53d5SJ. Bruce Fields 	 * if it is non-null and for the same filesystem, then we should compose
563bc6c53d5SJ. Bruce Fields 	 * a filehandle which is of the same version, where possible.
564bc6c53d5SJ. Bruce Fields 	 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
565bc6c53d5SJ. Bruce Fields 	 * Then create a 32byte filehandle using nfs_fhbase_old
566bc6c53d5SJ. Bruce Fields 	 *
567bc6c53d5SJ. Bruce Fields 	 */
568bc6c53d5SJ. Bruce Fields 
5692b0143b5SDavid Howells 	struct inode * inode = d_inode(dentry);
570bc6c53d5SJ. Bruce Fields 	dev_t ex_dev = exp_sb(exp)->s_dev;
571bc6c53d5SJ. Bruce Fields 
57297e47fa1SAl Viro 	dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %pd2, ino=%ld)\n",
573bc6c53d5SJ. Bruce Fields 		MAJOR(ex_dev), MINOR(ex_dev),
5742b0143b5SDavid Howells 		(long) d_inode(exp->ex_path.dentry)->i_ino,
57597e47fa1SAl Viro 		dentry,
576bc6c53d5SJ. Bruce Fields 		(inode ? inode->i_ino : 0));
577bc6c53d5SJ. Bruce Fields 
578bc6c53d5SJ. Bruce Fields 	/* Choose filehandle version and fsid type based on
579bc6c53d5SJ. Bruce Fields 	 * the reference filehandle (if it is in the same export)
580bc6c53d5SJ. Bruce Fields 	 * or the export options.
581bc6c53d5SJ. Bruce Fields 	 */
582bc6c53d5SJ. Bruce Fields 	set_version_and_fsid_type(fhp, exp, ref_fh);
5831da177e4SLinus Torvalds 
584daab110eSJeff Layton 	/* If we have a ref_fh, then copy the fh_no_wcc setting from it. */
585daab110eSJeff Layton 	fhp->fh_no_wcc = ref_fh ? ref_fh->fh_no_wcc : false;
586daab110eSJeff Layton 
5871da177e4SLinus Torvalds 	if (ref_fh == fhp)
5881da177e4SLinus Torvalds 		fh_put(ref_fh);
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	if (fhp->fh_locked || fhp->fh_dentry) {
59197e47fa1SAl Viro 		printk(KERN_ERR "fh_compose: fh %pd2 not initialized!\n",
59297e47fa1SAl Viro 		       dentry);
5931da177e4SLinus Torvalds 	}
5941da177e4SLinus Torvalds 	if (fhp->fh_maxsize < NFS_FHSIZE)
59597e47fa1SAl Viro 		printk(KERN_ERR "fh_compose: called with maxsize %d! %pd2\n",
596982aedfdSNeilBrown 		       fhp->fh_maxsize,
59797e47fa1SAl Viro 		       dentry);
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	fhp->fh_dentry = dget(dentry); /* our internal copy */
600bf18f163SKinglong Mee 	fhp->fh_export = exp_get(exp);
6011da177e4SLinus Torvalds 
602bc6c53d5SJ. Bruce Fields 	if (fhp->fh_handle.fh_version == 0xca) {
6031da177e4SLinus Torvalds 		/* old style filehandle please */
6041da177e4SLinus Torvalds 		memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
6051da177e4SLinus Torvalds 		fhp->fh_handle.fh_size = NFS_FHSIZE;
6061da177e4SLinus Torvalds 		fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
6071da177e4SLinus Torvalds 		fhp->fh_handle.ofh_dev =  old_encode_dev(ex_dev);
6081da177e4SLinus Torvalds 		fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
609982aedfdSNeilBrown 		fhp->fh_handle.ofh_xino =
6102b0143b5SDavid Howells 			ino_t_to_u32(d_inode(exp->ex_path.dentry)->i_ino);
6111da177e4SLinus Torvalds 		fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
6121da177e4SLinus Torvalds 		if (inode)
6131da177e4SLinus Torvalds 			_fh_update_old(dentry, exp, &fhp->fh_handle);
6141da177e4SLinus Torvalds 	} else {
6155409e46fSChristoph Hellwig 		fhp->fh_handle.fh_size =
6165409e46fSChristoph Hellwig 			key_len(fhp->fh_handle.fh_fsid_type) + 4;
6171da177e4SLinus Torvalds 		fhp->fh_handle.fh_auth_type = 0;
6185409e46fSChristoph Hellwig 
6195409e46fSChristoph Hellwig 		mk_fsid(fhp->fh_handle.fh_fsid_type,
6205409e46fSChristoph Hellwig 			fhp->fh_handle.fh_fsid,
6215409e46fSChristoph Hellwig 			ex_dev,
6222b0143b5SDavid Howells 			d_inode(exp->ex_path.dentry)->i_ino,
623af6a4e28SNeilBrown 			exp->ex_fsid, exp->ex_uuid);
624af6a4e28SNeilBrown 
6256e91ea2bSChristoph Hellwig 		if (inode)
6266e91ea2bSChristoph Hellwig 			_fh_update(fhp, exp, dentry);
627216b6cbdSNamjae Jeon 		if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID) {
6281be10a88SJ. Bruce Fields 			fh_put(fhp);
6291da177e4SLinus Torvalds 			return nfserr_opnotsupp;
6301da177e4SLinus Torvalds 		}
6311be10a88SJ. Bruce Fields 	}
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	return 0;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds /*
6371da177e4SLinus Torvalds  * Update file handle information after changing a dentry.
6381da177e4SLinus Torvalds  * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
6391da177e4SLinus Torvalds  */
64083b11340SAl Viro __be32
6411da177e4SLinus Torvalds fh_update(struct svc_fh *fhp)
6421da177e4SLinus Torvalds {
6431da177e4SLinus Torvalds 	struct dentry *dentry;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	if (!fhp->fh_dentry)
6461da177e4SLinus Torvalds 		goto out_bad;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
6492b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
6501da177e4SLinus Torvalds 		goto out_negative;
6511da177e4SLinus Torvalds 	if (fhp->fh_handle.fh_version != 1) {
6521da177e4SLinus Torvalds 		_fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
6531da177e4SLinus Torvalds 	} else {
6546e91ea2bSChristoph Hellwig 		if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
65549e73720SJ. Bruce Fields 			return 0;
6566e91ea2bSChristoph Hellwig 
6576e91ea2bSChristoph Hellwig 		_fh_update(fhp, fhp->fh_export, dentry);
658216b6cbdSNamjae Jeon 		if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID)
6591da177e4SLinus Torvalds 			return nfserr_opnotsupp;
6601da177e4SLinus Torvalds 	}
6611da177e4SLinus Torvalds 	return 0;
6621da177e4SLinus Torvalds out_bad:
6631da177e4SLinus Torvalds 	printk(KERN_ERR "fh_update: fh not verified!\n");
66449e73720SJ. Bruce Fields 	return nfserr_serverfault;
6651da177e4SLinus Torvalds out_negative:
66697e47fa1SAl Viro 	printk(KERN_ERR "fh_update: %pd2 still negative!\n",
66797e47fa1SAl Viro 		dentry);
66849e73720SJ. Bruce Fields 	return nfserr_serverfault;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds /*
6721da177e4SLinus Torvalds  * Release a file handle.
6731da177e4SLinus Torvalds  */
6741da177e4SLinus Torvalds void
6751da177e4SLinus Torvalds fh_put(struct svc_fh *fhp)
6761da177e4SLinus Torvalds {
6771da177e4SLinus Torvalds 	struct dentry * dentry = fhp->fh_dentry;
6781da177e4SLinus Torvalds 	struct svc_export * exp = fhp->fh_export;
6791da177e4SLinus Torvalds 	if (dentry) {
6801da177e4SLinus Torvalds 		fh_unlock(fhp);
6811da177e4SLinus Torvalds 		fhp->fh_dentry = NULL;
6821da177e4SLinus Torvalds 		dput(dentry);
683aaf91ec1SJeff Layton 		fh_clear_wcc(fhp);
6841da177e4SLinus Torvalds 	}
6854a55c101SJan Kara 	fh_drop_write(fhp);
6861da177e4SLinus Torvalds 	if (exp) {
687a09581f2SStanislav Kinsbursky 		exp_put(exp);
6881da177e4SLinus Torvalds 		fhp->fh_export = NULL;
6891da177e4SLinus Torvalds 	}
690daab110eSJeff Layton 	fhp->fh_no_wcc = false;
6911da177e4SLinus Torvalds 	return;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds /*
6951da177e4SLinus Torvalds  * Shorthand for dprintk()'s
6961da177e4SLinus Torvalds  */
6971da177e4SLinus Torvalds char * SVCFH_fmt(struct svc_fh *fhp)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	struct knfsd_fh *fh = &fhp->fh_handle;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	static char buf[80];
7021da177e4SLinus Torvalds 	sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
7031da177e4SLinus Torvalds 		fh->fh_size,
7041da177e4SLinus Torvalds 		fh->fh_base.fh_pad[0],
7051da177e4SLinus Torvalds 		fh->fh_base.fh_pad[1],
7061da177e4SLinus Torvalds 		fh->fh_base.fh_pad[2],
7071da177e4SLinus Torvalds 		fh->fh_base.fh_pad[3],
7081da177e4SLinus Torvalds 		fh->fh_base.fh_pad[4],
7091da177e4SLinus Torvalds 		fh->fh_base.fh_pad[5]);
7101da177e4SLinus Torvalds 	return buf;
7111da177e4SLinus Torvalds }
712af6a4e28SNeilBrown 
713af6a4e28SNeilBrown enum fsid_source fsid_source(struct svc_fh *fhp)
714af6a4e28SNeilBrown {
715af6a4e28SNeilBrown 	if (fhp->fh_handle.fh_version != 1)
716af6a4e28SNeilBrown 		return FSIDSOURCE_DEV;
717af6a4e28SNeilBrown 	switch(fhp->fh_handle.fh_fsid_type) {
718af6a4e28SNeilBrown 	case FSID_DEV:
719af6a4e28SNeilBrown 	case FSID_ENCODE_DEV:
720af6a4e28SNeilBrown 	case FSID_MAJOR_MINOR:
7218e498751SJ. Bruce Fields 		if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV)
722af6a4e28SNeilBrown 			return FSIDSOURCE_DEV;
723b8da0d1cSNeil Brown 		break;
724af6a4e28SNeilBrown 	case FSID_NUM:
725af6a4e28SNeilBrown 		if (fhp->fh_export->ex_flags & NFSEXP_FSID)
726af6a4e28SNeilBrown 			return FSIDSOURCE_FSID;
727b8da0d1cSNeil Brown 		break;
728b8da0d1cSNeil Brown 	default:
729b8da0d1cSNeil Brown 		break;
730af6a4e28SNeilBrown 	}
731b8da0d1cSNeil Brown 	/* either a UUID type filehandle, or the filehandle doesn't
732b8da0d1cSNeil Brown 	 * match the export.
733b8da0d1cSNeil Brown 	 */
734b8da0d1cSNeil Brown 	if (fhp->fh_export->ex_flags & NFSEXP_FSID)
735b8da0d1cSNeil Brown 		return FSIDSOURCE_FSID;
736b8da0d1cSNeil Brown 	if (fhp->fh_export->ex_uuid)
737b8da0d1cSNeil Brown 		return FSIDSOURCE_UUID;
738b8da0d1cSNeil Brown 	return FSIDSOURCE_DEV;
739af6a4e28SNeilBrown }
740