xref: /openbmc/linux/fs/nfsd/nfsfh.c (revision 1be10a88cac5e589cdd2bcb0cf6a13ed30bcc233)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfsd/nfsfh.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * NFS server file handle treatment.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
71da177e4SLinus Torvalds  * Portions Copyright (C) 1999 G. Allen Morris III <gam3@acm.org>
81da177e4SLinus Torvalds  * Extensive rewrite by Neil Brown <neilb@cse.unsw.edu.au> Southern-Spring 1999
91da177e4SLinus Torvalds  * ... and again Southern-Winter 2001 to support export_operations
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/slab.h>
131da177e4SLinus Torvalds #include <linux/fs.h>
141da177e4SLinus Torvalds #include <linux/unistd.h>
151da177e4SLinus Torvalds #include <linux/string.h>
161da177e4SLinus Torvalds #include <linux/stat.h>
171da177e4SLinus Torvalds #include <linux/dcache.h>
18a5694255SChristoph Hellwig #include <linux/exportfs.h>
191da177e4SLinus Torvalds #include <linux/mount.h>
201da177e4SLinus Torvalds 
21ad06e4bdSChuck Lever #include <linux/sunrpc/clnt.h>
221da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
2332c1eb0cSAndy Adamson #include <linux/sunrpc/svcauth_gss.h>
241da177e4SLinus Torvalds #include <linux/nfsd/nfsd.h>
252e8138a2SJ. Bruce Fields #include "auth.h"
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #define NFSDDBG_FACILITY		NFSDDBG_FH
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /*
311da177e4SLinus Torvalds  * our acceptability function.
321da177e4SLinus Torvalds  * if NOSUBTREECHECK, accept anything
331da177e4SLinus Torvalds  * if not, require that we can walk up to exp->ex_dentry
341da177e4SLinus Torvalds  * doing some checks on the 'x' bits
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds static int nfsd_acceptable(void *expv, struct dentry *dentry)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds 	struct svc_export *exp = expv;
391da177e4SLinus Torvalds 	int rv;
401da177e4SLinus Torvalds 	struct dentry *tdentry;
411da177e4SLinus Torvalds 	struct dentry *parent;
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
441da177e4SLinus Torvalds 		return 1;
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 	tdentry = dget(dentry);
4754775491SJan Blunck 	while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
481da177e4SLinus Torvalds 		/* make sure parents give x permission to user */
491da177e4SLinus Torvalds 		int err;
501da177e4SLinus Torvalds 		parent = dget_parent(tdentry);
51f419a2e3SAl Viro 		err = inode_permission(parent->d_inode, MAY_EXEC);
521da177e4SLinus Torvalds 		if (err < 0) {
531da177e4SLinus Torvalds 			dput(parent);
541da177e4SLinus Torvalds 			break;
551da177e4SLinus Torvalds 		}
561da177e4SLinus Torvalds 		dput(tdentry);
571da177e4SLinus Torvalds 		tdentry = parent;
581da177e4SLinus Torvalds 	}
5954775491SJan Blunck 	if (tdentry != exp->ex_path.dentry)
601da177e4SLinus Torvalds 		dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
6154775491SJan Blunck 	rv = (tdentry == exp->ex_path.dentry);
621da177e4SLinus Torvalds 	dput(tdentry);
631da177e4SLinus Torvalds 	return rv;
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /* Type check. The correct error return for type mismatches does not seem to be
671da177e4SLinus Torvalds  * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
681da177e4SLinus Torvalds  * comment in the NFSv3 spec says this is incorrect (implementation notes for
691da177e4SLinus Torvalds  * the write call).
701da177e4SLinus Torvalds  */
7183b11340SAl Viro static inline __be32
721da177e4SLinus Torvalds nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds 	/* Type can be negative when creating hardlinks - not to a dir */
751da177e4SLinus Torvalds 	if (type > 0 && (mode & S_IFMT) != type) {
761da177e4SLinus Torvalds 		if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
771da177e4SLinus Torvalds 			return nfserr_symlink;
781da177e4SLinus Torvalds 		else if (type == S_IFDIR)
791da177e4SLinus Torvalds 			return nfserr_notdir;
801da177e4SLinus Torvalds 		else if ((mode & S_IFMT) == S_IFDIR)
811da177e4SLinus Torvalds 			return nfserr_isdir;
821da177e4SLinus Torvalds 		else
831da177e4SLinus Torvalds 			return nfserr_inval;
841da177e4SLinus Torvalds 	}
851da177e4SLinus Torvalds 	if (type < 0 && (mode & S_IFMT) == -type) {
861da177e4SLinus Torvalds 		if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
871da177e4SLinus Torvalds 			return nfserr_symlink;
881da177e4SLinus Torvalds 		else if (type == -S_IFDIR)
891da177e4SLinus Torvalds 			return nfserr_isdir;
901da177e4SLinus Torvalds 		else
911da177e4SLinus Torvalds 			return nfserr_notdir;
921da177e4SLinus Torvalds 	}
931da177e4SLinus Torvalds 	return 0;
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds 
966fa02839SJ. Bruce Fields static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
976fa02839SJ. Bruce Fields 					  struct svc_export *exp)
986fa02839SJ. Bruce Fields {
996fa02839SJ. Bruce Fields 	/* Check if the request originated from a secure port. */
1006fa02839SJ. Bruce Fields 	if (!rqstp->rq_secure && EX_SECURE(exp)) {
1015216a8e7SPavel Emelyanov 		RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
1026fa02839SJ. Bruce Fields 		dprintk(KERN_WARNING
1036fa02839SJ. Bruce Fields 		       "nfsd: request from insecure port %s!\n",
1046fa02839SJ. Bruce Fields 		       svc_print_addr(rqstp, buf, sizeof(buf)));
1056fa02839SJ. Bruce Fields 		return nfserr_perm;
1066fa02839SJ. Bruce Fields 	}
1076fa02839SJ. Bruce Fields 
1086fa02839SJ. Bruce Fields 	/* Set user creds for this exportpoint */
1096fa02839SJ. Bruce Fields 	return nfserrno(nfsd_setuser(rqstp, exp));
1106fa02839SJ. Bruce Fields }
1116fa02839SJ. Bruce Fields 
1121da177e4SLinus Torvalds /*
11303550facSJ. Bruce Fields  * Use the given filehandle to look up the corresponding export and
11403550facSJ. Bruce Fields  * dentry.  On success, the results are used to set fh_export and
11503550facSJ. Bruce Fields  * fh_dentry.
1161da177e4SLinus Torvalds  */
11703550facSJ. Bruce Fields static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	struct knfsd_fh	*fh = &fhp->fh_handle;
1206e91ea2bSChristoph Hellwig 	struct fid *fid = NULL, sfid;
12103550facSJ. Bruce Fields 	struct svc_export *exp;
12203550facSJ. Bruce Fields 	struct dentry *dentry;
1231da177e4SLinus Torvalds 	int fileid_type;
1241da177e4SLinus Torvalds 	int data_left = fh->fh_size/4;
12503550facSJ. Bruce Fields 	__be32 error;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	error = nfserr_stale;
1281da177e4SLinus Torvalds 	if (rqstp->rq_vers > 2)
1291da177e4SLinus Torvalds 		error = nfserr_badhandle;
1301da177e4SLinus Torvalds 	if (rqstp->rq_vers == 4 && fh->fh_size == 0)
1311da177e4SLinus Torvalds 		return nfserr_nofilehandle;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	if (fh->fh_version == 1) {
1341da177e4SLinus Torvalds 		int len;
13503550facSJ. Bruce Fields 
13603550facSJ. Bruce Fields 		if (--data_left < 0)
13703550facSJ. Bruce Fields 			return error;
13803550facSJ. Bruce Fields 		if (fh->fh_auth_type != 0)
13903550facSJ. Bruce Fields 			return error;
1401da177e4SLinus Torvalds 		len = key_len(fh->fh_fsid_type) / 4;
14103550facSJ. Bruce Fields 		if (len == 0)
14203550facSJ. Bruce Fields 			return error;
143af6a4e28SNeilBrown 		if  (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
1441da177e4SLinus Torvalds 			/* deprecated, convert to type 3 */
145af6a4e28SNeilBrown 			len = key_len(FSID_ENCODE_DEV)/4;
146af6a4e28SNeilBrown 			fh->fh_fsid_type = FSID_ENCODE_DEV;
1471da177e4SLinus Torvalds 			fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
1481da177e4SLinus Torvalds 			fh->fh_fsid[1] = fh->fh_fsid[2];
1491da177e4SLinus Torvalds 		}
15003550facSJ. Bruce Fields 		data_left -= len;
15103550facSJ. Bruce Fields 		if (data_left < 0)
15203550facSJ. Bruce Fields 			return error;
15303550facSJ. Bruce Fields 		exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_auth);
1546e91ea2bSChristoph Hellwig 		fid = (struct fid *)(fh->fh_auth + len);
1551da177e4SLinus Torvalds 	} else {
1566e91ea2bSChristoph Hellwig 		__u32 tfh[2];
1571da177e4SLinus Torvalds 		dev_t xdev;
1581da177e4SLinus Torvalds 		ino_t xino;
15903550facSJ. Bruce Fields 
1601da177e4SLinus Torvalds 		if (fh->fh_size != NFS_FHSIZE)
16103550facSJ. Bruce Fields 			return error;
1621da177e4SLinus Torvalds 		/* assume old filehandle format */
1631da177e4SLinus Torvalds 		xdev = old_decode_dev(fh->ofh_xdev);
1641da177e4SLinus Torvalds 		xino = u32_to_ino_t(fh->ofh_xino);
165af6a4e28SNeilBrown 		mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
1660989a788SJ. Bruce Fields 		exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
1671da177e4SLinus Torvalds 	}
1681da177e4SLinus Torvalds 
1692d3bb252SJ. Bruce Fields 	error = nfserr_stale;
1702d3bb252SJ. Bruce Fields 	if (PTR_ERR(exp) == -ENOENT)
17103550facSJ. Bruce Fields 		return error;
1722d3bb252SJ. Bruce Fields 
17303550facSJ. Bruce Fields 	if (IS_ERR(exp))
17403550facSJ. Bruce Fields 		return nfserrno(PTR_ERR(exp));
1751da177e4SLinus Torvalds 
176496d6c32SNeil Brown 	if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
177496d6c32SNeil Brown 		/* Elevate privileges so that the lack of 'r' or 'x'
178496d6c32SNeil Brown 		 * permission on some parent directory will
179496d6c32SNeil Brown 		 * not stop exportfs_decode_fh from being able
180496d6c32SNeil Brown 		 * to reconnect a directory into the dentry cache.
181496d6c32SNeil Brown 		 * The same problem can affect "SUBTREECHECK" exports,
182496d6c32SNeil Brown 		 * but as nfsd_acceptable depends on correct
183496d6c32SNeil Brown 		 * access control settings being in effect, we cannot
184496d6c32SNeil Brown 		 * fix that case easily.
185496d6c32SNeil Brown 		 */
186d84f4f99SDavid Howells 		struct cred *new = prepare_creds();
187d84f4f99SDavid Howells 		if (!new)
188d84f4f99SDavid Howells 			return nfserrno(-ENOMEM);
189d84f4f99SDavid Howells 		new->cap_effective =
190d84f4f99SDavid Howells 			cap_raise_nfsd_set(new->cap_effective,
191d84f4f99SDavid Howells 					   new->cap_permitted);
192d84f4f99SDavid Howells 		put_cred(override_creds(new));
193d84f4f99SDavid Howells 		put_cred(new);
194496d6c32SNeil Brown 	} else {
1956fa02839SJ. Bruce Fields 		error = nfsd_setuser_and_check_port(rqstp, exp);
196d1bbf14fSNeilBrown 		if (error)
197d1bbf14fSNeilBrown 			goto out;
198496d6c32SNeil Brown 	}
199d1bbf14fSNeilBrown 
2001da177e4SLinus Torvalds 	/*
2011da177e4SLinus Torvalds 	 * Look up the dentry using the NFS file handle.
2021da177e4SLinus Torvalds 	 */
2031da177e4SLinus Torvalds 	error = nfserr_stale;
2041da177e4SLinus Torvalds 	if (rqstp->rq_vers > 2)
2051da177e4SLinus Torvalds 		error = nfserr_badhandle;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	if (fh->fh_version != 1) {
2086e91ea2bSChristoph Hellwig 		sfid.i32.ino = fh->ofh_ino;
2096e91ea2bSChristoph Hellwig 		sfid.i32.gen = fh->ofh_generation;
2106e91ea2bSChristoph Hellwig 		sfid.i32.parent_ino = fh->ofh_dirino;
2116e91ea2bSChristoph Hellwig 		fid = &sfid;
2121da177e4SLinus Torvalds 		data_left = 3;
2131da177e4SLinus Torvalds 		if (fh->ofh_dirino == 0)
2146e91ea2bSChristoph Hellwig 			fileid_type = FILEID_INO32_GEN;
2151da177e4SLinus Torvalds 		else
2166e91ea2bSChristoph Hellwig 			fileid_type = FILEID_INO32_GEN_PARENT;
2171da177e4SLinus Torvalds 	} else
2181da177e4SLinus Torvalds 		fileid_type = fh->fh_fileid_type;
2191da177e4SLinus Torvalds 
2206e91ea2bSChristoph Hellwig 	if (fileid_type == FILEID_ROOT)
22154775491SJan Blunck 		dentry = dget(exp->ex_path.dentry);
2221da177e4SLinus Torvalds 	else {
22354775491SJan Blunck 		dentry = exportfs_decode_fh(exp->ex_path.mnt, fid,
224d37065cdSChristoph Hellwig 				data_left, fileid_type,
2251da177e4SLinus Torvalds 				nfsd_acceptable, exp);
2261da177e4SLinus Torvalds 	}
2271da177e4SLinus Torvalds 	if (dentry == NULL)
2281da177e4SLinus Torvalds 		goto out;
2291da177e4SLinus Torvalds 	if (IS_ERR(dentry)) {
2301da177e4SLinus Torvalds 		if (PTR_ERR(dentry) != -EINVAL)
2311da177e4SLinus Torvalds 			error = nfserrno(PTR_ERR(dentry));
2321da177e4SLinus Torvalds 		goto out;
2331da177e4SLinus Torvalds 	}
23434e9a63bSNeilBrown 
235496d6c32SNeil Brown 	if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
236496d6c32SNeil Brown 		error = nfsd_setuser_and_check_port(rqstp, exp);
237496d6c32SNeil Brown 		if (error) {
238496d6c32SNeil Brown 			dput(dentry);
239496d6c32SNeil Brown 			goto out;
240496d6c32SNeil Brown 		}
241496d6c32SNeil Brown 	}
242496d6c32SNeil Brown 
2431da177e4SLinus Torvalds 	if (S_ISDIR(dentry->d_inode->i_mode) &&
2441da177e4SLinus Torvalds 			(dentry->d_flags & DCACHE_DISCONNECTED)) {
2451da177e4SLinus Torvalds 		printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
2461da177e4SLinus Torvalds 				dentry->d_parent->d_name.name, dentry->d_name.name);
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	fhp->fh_dentry = dentry;
2501da177e4SLinus Torvalds 	fhp->fh_export = exp;
25103550facSJ. Bruce Fields 	return 0;
25203550facSJ. Bruce Fields out:
25303550facSJ. Bruce Fields 	exp_put(exp);
25403550facSJ. Bruce Fields 	return error;
25503550facSJ. Bruce Fields }
25603550facSJ. Bruce Fields 
257b3d47676SJ. Bruce Fields /**
258b3d47676SJ. Bruce Fields  * fh_verify - filehandle lookup and access checking
259b3d47676SJ. Bruce Fields  * @rqstp: pointer to current rpc request
260b3d47676SJ. Bruce Fields  * @fhp: filehandle to be verified
261b3d47676SJ. Bruce Fields  * @type: expected type of object pointed to by filehandle
262b3d47676SJ. Bruce Fields  * @access: type of access needed to object
26303550facSJ. Bruce Fields  *
264b3d47676SJ. Bruce Fields  * Look up a dentry from the on-the-wire filehandle, check the client's
265b3d47676SJ. Bruce Fields  * access to the export, and set the current task's credentials.
26603550facSJ. Bruce Fields  *
267b3d47676SJ. Bruce Fields  * Regardless of success or failure of fh_verify(), fh_put() should be
268b3d47676SJ. Bruce Fields  * called on @fhp when the caller is finished with the filehandle.
269b3d47676SJ. Bruce Fields  *
270b3d47676SJ. Bruce Fields  * fh_verify() may be called multiple times on a given filehandle, for
271b3d47676SJ. Bruce Fields  * example, when processing an NFSv4 compound.  The first call will look
272b3d47676SJ. Bruce Fields  * up a dentry using the on-the-wire filehandle.  Subsequent calls will
273b3d47676SJ. Bruce Fields  * skip the lookup and just perform the other checks and possibly change
274b3d47676SJ. Bruce Fields  * the current task's credentials.
275b3d47676SJ. Bruce Fields  *
276b3d47676SJ. Bruce Fields  * @type specifies the type of object expected using one of the S_IF*
277b3d47676SJ. Bruce Fields  * constants defined in include/linux/stat.h.  The caller may use zero
278b3d47676SJ. Bruce Fields  * to indicate that it doesn't care, or a negative integer to indicate
279b3d47676SJ. Bruce Fields  * that it expects something not of the given type.
280b3d47676SJ. Bruce Fields  *
281b3d47676SJ. Bruce Fields  * @access is formed from the NFSD_MAY_* constants defined in
282b3d47676SJ. Bruce Fields  * include/linux/nfsd/nfsd.h.
28303550facSJ. Bruce Fields  */
28403550facSJ. Bruce Fields __be32
28503550facSJ. Bruce Fields fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
28603550facSJ. Bruce Fields {
28703550facSJ. Bruce Fields 	struct svc_export *exp;
28803550facSJ. Bruce Fields 	struct dentry	*dentry;
28903550facSJ. Bruce Fields 	__be32		error;
29003550facSJ. Bruce Fields 
29103550facSJ. Bruce Fields 	dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
29203550facSJ. Bruce Fields 
29303550facSJ. Bruce Fields 	if (!fhp->fh_dentry) {
29403550facSJ. Bruce Fields 		error = nfsd_set_fh_dentry(rqstp, fhp);
29503550facSJ. Bruce Fields 		if (error)
29603550facSJ. Bruce Fields 			goto out;
29703550facSJ. Bruce Fields 		dentry = fhp->fh_dentry;
29803550facSJ. Bruce Fields 		exp = fhp->fh_export;
2991da177e4SLinus Torvalds 	} else {
3006fa02839SJ. Bruce Fields 		/*
3016fa02839SJ. Bruce Fields 		 * just rechecking permissions
3026fa02839SJ. Bruce Fields 		 * (e.g. nfsproc_create calls fh_verify, then nfsd_create
3036fa02839SJ. Bruce Fields 		 * does as well)
3041da177e4SLinus Torvalds 		 */
3051da177e4SLinus Torvalds 		dprintk("nfsd: fh_verify - just checking\n");
3061da177e4SLinus Torvalds 		dentry = fhp->fh_dentry;
3071da177e4SLinus Torvalds 		exp = fhp->fh_export;
3086fa02839SJ. Bruce Fields 		/*
3096fa02839SJ. Bruce Fields 		 * Set user creds for this exportpoint; necessary even
310d1bbf14fSNeilBrown 		 * in the "just checking" case because this may be a
311d1bbf14fSNeilBrown 		 * filehandle that was created by fh_compose, and that
312d1bbf14fSNeilBrown 		 * is about to be used in another nfsv4 compound
3136fa02839SJ. Bruce Fields 		 * operation.
3146fa02839SJ. Bruce Fields 		 */
3156fa02839SJ. Bruce Fields 		error = nfsd_setuser_and_check_port(rqstp, exp);
3167fc90ec9SJ. Bruce Fields 		if (error)
3177fc90ec9SJ. Bruce Fields 			goto out;
318d1bbf14fSNeilBrown 	}
3197fc90ec9SJ. Bruce Fields 
3201da177e4SLinus Torvalds 	error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
3211da177e4SLinus Torvalds 	if (error)
3221da177e4SLinus Torvalds 		goto out;
3231da177e4SLinus Torvalds 
3249091224fSJ. Bruce Fields 	/*
3259091224fSJ. Bruce Fields 	 * pseudoflavor restrictions are not enforced on NLM,
3269091224fSJ. Bruce Fields 	 * which clients virtually always use auth_sys for,
3279091224fSJ. Bruce Fields 	 * even while using RPCSEC_GSS for NFS.
3289091224fSJ. Bruce Fields 	 */
32904716e66SJ. Bruce Fields 	if (access & NFSD_MAY_LOCK)
33004716e66SJ. Bruce Fields 		goto skip_pseudoflavor_check;
33104716e66SJ. Bruce Fields 	/*
33204716e66SJ. Bruce Fields 	 * Clients may expect to be able to use auth_sys during mount,
33304716e66SJ. Bruce Fields 	 * even if they use gss for everything else; see section 2.3.2
33404716e66SJ. Bruce Fields 	 * of rfc 2623.
33504716e66SJ. Bruce Fields 	 */
33604716e66SJ. Bruce Fields 	if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
33704716e66SJ. Bruce Fields 			&& exp->ex_path.dentry == dentry)
33804716e66SJ. Bruce Fields 		goto skip_pseudoflavor_check;
33904716e66SJ. Bruce Fields 
34032c1eb0cSAndy Adamson 	error = check_nfsd_access(exp, rqstp);
34132c1eb0cSAndy Adamson 	if (error)
34232c1eb0cSAndy Adamson 		goto out;
34332c1eb0cSAndy Adamson 
34404716e66SJ. Bruce Fields skip_pseudoflavor_check:
3451da177e4SLinus Torvalds 	/* Finally, check access permissions. */
3460ec757dfSJ. Bruce Fields 	error = nfsd_permission(rqstp, exp, dentry, access);
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	if (error) {
34934e9a63bSNeilBrown 		dprintk("fh_verify: %s/%s permission failure, "
35034e9a63bSNeilBrown 			"acc=%x, error=%d\n",
35134e9a63bSNeilBrown 			dentry->d_parent->d_name.name,
35234e9a63bSNeilBrown 			dentry->d_name.name,
353fc2dd2e5SAl Viro 			access, ntohl(error));
3541da177e4SLinus Torvalds 	}
3551da177e4SLinus Torvalds out:
3561da177e4SLinus Torvalds 	if (error == nfserr_stale)
3571da177e4SLinus Torvalds 		nfsdstats.fh_stale++;
3581da177e4SLinus Torvalds 	return error;
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds /*
3631da177e4SLinus Torvalds  * Compose a file handle for an NFS reply.
3641da177e4SLinus Torvalds  *
3651da177e4SLinus Torvalds  * Note that when first composed, the dentry may not yet have
3661da177e4SLinus Torvalds  * an inode.  In this case a call to fh_update should be made
3671da177e4SLinus Torvalds  * before the fh goes out on the wire ...
3681da177e4SLinus Torvalds  */
3696e91ea2bSChristoph Hellwig static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
3706e91ea2bSChristoph Hellwig 		struct dentry *dentry)
3711da177e4SLinus Torvalds {
37254775491SJan Blunck 	if (dentry != exp->ex_path.dentry) {
3736e91ea2bSChristoph Hellwig 		struct fid *fid = (struct fid *)
3746e91ea2bSChristoph Hellwig 			(fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1);
3756e91ea2bSChristoph Hellwig 		int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
3766e91ea2bSChristoph Hellwig 		int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
3771da177e4SLinus Torvalds 
3786e91ea2bSChristoph Hellwig 		fhp->fh_handle.fh_fileid_type =
3796e91ea2bSChristoph Hellwig 			exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
3806e91ea2bSChristoph Hellwig 		fhp->fh_handle.fh_size += maxsize * 4;
3816e91ea2bSChristoph Hellwig 	} else {
3826e91ea2bSChristoph Hellwig 		fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
3836e91ea2bSChristoph Hellwig 	}
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds /*
3871da177e4SLinus Torvalds  * for composing old style file handles
3881da177e4SLinus Torvalds  */
3891da177e4SLinus Torvalds static inline void _fh_update_old(struct dentry *dentry,
3901da177e4SLinus Torvalds 				  struct svc_export *exp,
3911da177e4SLinus Torvalds 				  struct knfsd_fh *fh)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
3941da177e4SLinus Torvalds 	fh->ofh_generation = dentry->d_inode->i_generation;
3951da177e4SLinus Torvalds 	if (S_ISDIR(dentry->d_inode->i_mode) ||
3961da177e4SLinus Torvalds 	    (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
3971da177e4SLinus Torvalds 		fh->ofh_dirino = 0;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4008e498751SJ. Bruce Fields static bool is_root_export(struct svc_export *exp)
4018e498751SJ. Bruce Fields {
4028e498751SJ. Bruce Fields 	return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root;
4038e498751SJ. Bruce Fields }
4048e498751SJ. Bruce Fields 
4058e498751SJ. Bruce Fields static struct super_block *exp_sb(struct svc_export *exp)
4068e498751SJ. Bruce Fields {
4078e498751SJ. Bruce Fields 	return exp->ex_path.dentry->d_inode->i_sb;
4088e498751SJ. Bruce Fields }
4098e498751SJ. Bruce Fields 
4108e498751SJ. Bruce Fields static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)
4118e498751SJ. Bruce Fields {
4128e498751SJ. Bruce Fields 	switch (fsid_type) {
4138e498751SJ. Bruce Fields 	case FSID_DEV:
4148e498751SJ. Bruce Fields 		if (!old_valid_dev(exp_sb(exp)->s_dev))
4158e498751SJ. Bruce Fields 			return 0;
4168e498751SJ. Bruce Fields 		/* FALL THROUGH */
4178e498751SJ. Bruce Fields 	case FSID_MAJOR_MINOR:
4188e498751SJ. Bruce Fields 	case FSID_ENCODE_DEV:
4198e498751SJ. Bruce Fields 		return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV;
4208e498751SJ. Bruce Fields 	case FSID_NUM:
4218e498751SJ. Bruce Fields 		return exp->ex_flags & NFSEXP_FSID;
4228e498751SJ. Bruce Fields 	case FSID_UUID8:
4238e498751SJ. Bruce Fields 	case FSID_UUID16:
4248e498751SJ. Bruce Fields 		if (!is_root_export(exp))
4258e498751SJ. Bruce Fields 			return 0;
4268e498751SJ. Bruce Fields 		/* fall through */
4278e498751SJ. Bruce Fields 	case FSID_UUID4_INUM:
4288e498751SJ. Bruce Fields 	case FSID_UUID16_INUM:
4298e498751SJ. Bruce Fields 		return exp->ex_uuid != NULL;
4308e498751SJ. Bruce Fields 	}
4318e498751SJ. Bruce Fields 	return 1;
4328e498751SJ. Bruce Fields }
4338e498751SJ. Bruce Fields 
434bc6c53d5SJ. Bruce Fields 
435bc6c53d5SJ. Bruce Fields static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh)
4361da177e4SLinus Torvalds {
437b41eeef1SNeilBrown 	u8 version;
438bc6c53d5SJ. Bruce Fields 	u8 fsid_type;
439b41eeef1SNeilBrown retry:
440b41eeef1SNeilBrown 	version = 1;
4417e405364SNeilBrown 	if (ref_fh && ref_fh->fh_export == exp) {
442982aedfdSNeilBrown 		version = ref_fh->fh_handle.fh_version;
443982aedfdSNeilBrown 		fsid_type = ref_fh->fh_handle.fh_fsid_type;
444b41eeef1SNeilBrown 
445b41eeef1SNeilBrown 		ref_fh = NULL;
446b41eeef1SNeilBrown 
447b41eeef1SNeilBrown 		switch (version) {
448b41eeef1SNeilBrown 		case 0xca:
449b41eeef1SNeilBrown 			fsid_type = FSID_DEV;
450b41eeef1SNeilBrown 			break;
451b41eeef1SNeilBrown 		case 1:
452b41eeef1SNeilBrown 			break;
453b41eeef1SNeilBrown 		default:
454b41eeef1SNeilBrown 			goto retry;
455b41eeef1SNeilBrown 		}
456b41eeef1SNeilBrown 
4578e498751SJ. Bruce Fields 		/*
4588e498751SJ. Bruce Fields 		 * As the fsid -> filesystem mapping was guided by
4598e498751SJ. Bruce Fields 		 * user-space, there is no guarantee that the filesystem
4608e498751SJ. Bruce Fields 		 * actually supports that fsid type. If it doesn't we
4618e498751SJ. Bruce Fields 		 * loop around again without ref_fh set.
462982aedfdSNeilBrown 		 */
4638e498751SJ. Bruce Fields 		if (!fsid_type_ok_for_exp(fsid_type, exp))
464b41eeef1SNeilBrown 			goto retry;
46530fa8c01SSteve Dickson 	} else if (exp->ex_flags & NFSEXP_FSID) {
46630fa8c01SSteve Dickson 		fsid_type = FSID_NUM;
467af6a4e28SNeilBrown 	} else if (exp->ex_uuid) {
468af6a4e28SNeilBrown 		if (fhp->fh_maxsize >= 64) {
4698e498751SJ. Bruce Fields 			if (is_root_export(exp))
470af6a4e28SNeilBrown 				fsid_type = FSID_UUID16;
471af6a4e28SNeilBrown 			else
472af6a4e28SNeilBrown 				fsid_type = FSID_UUID16_INUM;
473af6a4e28SNeilBrown 		} else {
4748e498751SJ. Bruce Fields 			if (is_root_export(exp))
475af6a4e28SNeilBrown 				fsid_type = FSID_UUID8;
476af6a4e28SNeilBrown 			else
477af6a4e28SNeilBrown 				fsid_type = FSID_UUID4_INUM;
478af6a4e28SNeilBrown 		}
479bc6c53d5SJ. Bruce Fields 	} else if (!old_valid_dev(exp_sb(exp)->s_dev))
4801da177e4SLinus Torvalds 		/* for newer device numbers, we must use a newer fsid format */
481af6a4e28SNeilBrown 		fsid_type = FSID_ENCODE_DEV;
482982aedfdSNeilBrown 	else
483af6a4e28SNeilBrown 		fsid_type = FSID_DEV;
484bc6c53d5SJ. Bruce Fields 	fhp->fh_handle.fh_version = version;
485bc6c53d5SJ. Bruce Fields 	if (version)
486bc6c53d5SJ. Bruce Fields 		fhp->fh_handle.fh_fsid_type = fsid_type;
487bc6c53d5SJ. Bruce Fields }
488bc6c53d5SJ. Bruce Fields 
489bc6c53d5SJ. Bruce Fields __be32
490bc6c53d5SJ. Bruce Fields fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
491bc6c53d5SJ. Bruce Fields 	   struct svc_fh *ref_fh)
492bc6c53d5SJ. Bruce Fields {
493bc6c53d5SJ. Bruce Fields 	/* ref_fh is a reference file handle.
494bc6c53d5SJ. Bruce Fields 	 * if it is non-null and for the same filesystem, then we should compose
495bc6c53d5SJ. Bruce Fields 	 * a filehandle which is of the same version, where possible.
496bc6c53d5SJ. Bruce Fields 	 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
497bc6c53d5SJ. Bruce Fields 	 * Then create a 32byte filehandle using nfs_fhbase_old
498bc6c53d5SJ. Bruce Fields 	 *
499bc6c53d5SJ. Bruce Fields 	 */
500bc6c53d5SJ. Bruce Fields 
501bc6c53d5SJ. Bruce Fields 	struct inode * inode = dentry->d_inode;
502bc6c53d5SJ. Bruce Fields 	struct dentry *parent = dentry->d_parent;
503bc6c53d5SJ. Bruce Fields 	__u32 *datap;
504bc6c53d5SJ. Bruce Fields 	dev_t ex_dev = exp_sb(exp)->s_dev;
505bc6c53d5SJ. Bruce Fields 
506bc6c53d5SJ. Bruce Fields 	dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
507bc6c53d5SJ. Bruce Fields 		MAJOR(ex_dev), MINOR(ex_dev),
508bc6c53d5SJ. Bruce Fields 		(long) exp->ex_path.dentry->d_inode->i_ino,
509bc6c53d5SJ. Bruce Fields 		parent->d_name.name, dentry->d_name.name,
510bc6c53d5SJ. Bruce Fields 		(inode ? inode->i_ino : 0));
511bc6c53d5SJ. Bruce Fields 
512bc6c53d5SJ. Bruce Fields 	/* Choose filehandle version and fsid type based on
513bc6c53d5SJ. Bruce Fields 	 * the reference filehandle (if it is in the same export)
514bc6c53d5SJ. Bruce Fields 	 * or the export options.
515bc6c53d5SJ. Bruce Fields 	 */
516bc6c53d5SJ. Bruce Fields 	 set_version_and_fsid_type(fhp, exp, ref_fh);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	if (ref_fh == fhp)
5191da177e4SLinus Torvalds 		fh_put(ref_fh);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	if (fhp->fh_locked || fhp->fh_dentry) {
5221da177e4SLinus Torvalds 		printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
5231da177e4SLinus Torvalds 		       parent->d_name.name, dentry->d_name.name);
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds 	if (fhp->fh_maxsize < NFS_FHSIZE)
5261da177e4SLinus Torvalds 		printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
527982aedfdSNeilBrown 		       fhp->fh_maxsize,
528982aedfdSNeilBrown 		       parent->d_name.name, dentry->d_name.name);
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	fhp->fh_dentry = dget(dentry); /* our internal copy */
5311da177e4SLinus Torvalds 	fhp->fh_export = exp;
5321da177e4SLinus Torvalds 	cache_get(&exp->h);
5331da177e4SLinus Torvalds 
534bc6c53d5SJ. Bruce Fields 	if (fhp->fh_handle.fh_version == 0xca) {
5351da177e4SLinus Torvalds 		/* old style filehandle please */
5361da177e4SLinus Torvalds 		memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
5371da177e4SLinus Torvalds 		fhp->fh_handle.fh_size = NFS_FHSIZE;
5381da177e4SLinus Torvalds 		fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
5391da177e4SLinus Torvalds 		fhp->fh_handle.ofh_dev =  old_encode_dev(ex_dev);
5401da177e4SLinus Torvalds 		fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
541982aedfdSNeilBrown 		fhp->fh_handle.ofh_xino =
54254775491SJan Blunck 			ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino);
5431da177e4SLinus Torvalds 		fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
5441da177e4SLinus Torvalds 		if (inode)
5451da177e4SLinus Torvalds 			_fh_update_old(dentry, exp, &fhp->fh_handle);
5461da177e4SLinus Torvalds 	} else {
5471da177e4SLinus Torvalds 		int len;
5481da177e4SLinus Torvalds 		fhp->fh_handle.fh_auth_type = 0;
5491da177e4SLinus Torvalds 		datap = fhp->fh_handle.fh_auth+0;
550bc6c53d5SJ. Bruce Fields 		mk_fsid(fhp->fh_handle.fh_fsid_type, datap, ex_dev,
55154775491SJan Blunck 			exp->ex_path.dentry->d_inode->i_ino,
552af6a4e28SNeilBrown 			exp->ex_fsid, exp->ex_uuid);
553af6a4e28SNeilBrown 
554bc6c53d5SJ. Bruce Fields 		len = key_len(fhp->fh_handle.fh_fsid_type);
5551da177e4SLinus Torvalds 		datap += len/4;
5561da177e4SLinus Torvalds 		fhp->fh_handle.fh_size = 4 + len;
5571da177e4SLinus Torvalds 
5586e91ea2bSChristoph Hellwig 		if (inode)
5596e91ea2bSChristoph Hellwig 			_fh_update(fhp, exp, dentry);
560*1be10a88SJ. Bruce Fields 		if (fhp->fh_handle.fh_fileid_type == 255) {
561*1be10a88SJ. Bruce Fields 			fh_put(fhp);
5621da177e4SLinus Torvalds 			return nfserr_opnotsupp;
5631da177e4SLinus Torvalds 		}
564*1be10a88SJ. Bruce Fields 	}
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	return 0;
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds /*
5701da177e4SLinus Torvalds  * Update file handle information after changing a dentry.
5711da177e4SLinus Torvalds  * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
5721da177e4SLinus Torvalds  */
57383b11340SAl Viro __be32
5741da177e4SLinus Torvalds fh_update(struct svc_fh *fhp)
5751da177e4SLinus Torvalds {
5761da177e4SLinus Torvalds 	struct dentry *dentry;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	if (!fhp->fh_dentry)
5791da177e4SLinus Torvalds 		goto out_bad;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
5821da177e4SLinus Torvalds 	if (!dentry->d_inode)
5831da177e4SLinus Torvalds 		goto out_negative;
5841da177e4SLinus Torvalds 	if (fhp->fh_handle.fh_version != 1) {
5851da177e4SLinus Torvalds 		_fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
5861da177e4SLinus Torvalds 	} else {
5876e91ea2bSChristoph Hellwig 		if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
5884c9608b2SNeilBrown 			goto out;
5896e91ea2bSChristoph Hellwig 
5906e91ea2bSChristoph Hellwig 		_fh_update(fhp, fhp->fh_export, dentry);
5911da177e4SLinus Torvalds 		if (fhp->fh_handle.fh_fileid_type == 255)
5921da177e4SLinus Torvalds 			return nfserr_opnotsupp;
5931da177e4SLinus Torvalds 	}
5941da177e4SLinus Torvalds out:
5951da177e4SLinus Torvalds 	return 0;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds out_bad:
5981da177e4SLinus Torvalds 	printk(KERN_ERR "fh_update: fh not verified!\n");
5991da177e4SLinus Torvalds 	goto out;
6001da177e4SLinus Torvalds out_negative:
6011da177e4SLinus Torvalds 	printk(KERN_ERR "fh_update: %s/%s still negative!\n",
6021da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
6031da177e4SLinus Torvalds 	goto out;
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds /*
6071da177e4SLinus Torvalds  * Release a file handle.
6081da177e4SLinus Torvalds  */
6091da177e4SLinus Torvalds void
6101da177e4SLinus Torvalds fh_put(struct svc_fh *fhp)
6111da177e4SLinus Torvalds {
6121da177e4SLinus Torvalds 	struct dentry * dentry = fhp->fh_dentry;
6131da177e4SLinus Torvalds 	struct svc_export * exp = fhp->fh_export;
6141da177e4SLinus Torvalds 	if (dentry) {
6151da177e4SLinus Torvalds 		fh_unlock(fhp);
6161da177e4SLinus Torvalds 		fhp->fh_dentry = NULL;
6171da177e4SLinus Torvalds 		dput(dentry);
6181da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
6191da177e4SLinus Torvalds 		fhp->fh_pre_saved = 0;
6201da177e4SLinus Torvalds 		fhp->fh_post_saved = 0;
6211da177e4SLinus Torvalds #endif
6221da177e4SLinus Torvalds 	}
6231da177e4SLinus Torvalds 	if (exp) {
624baab935fSNeilBrown 		cache_put(&exp->h, &svc_export_cache);
6251da177e4SLinus Torvalds 		fhp->fh_export = NULL;
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds 	return;
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds /*
6311da177e4SLinus Torvalds  * Shorthand for dprintk()'s
6321da177e4SLinus Torvalds  */
6331da177e4SLinus Torvalds char * SVCFH_fmt(struct svc_fh *fhp)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	struct knfsd_fh *fh = &fhp->fh_handle;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	static char buf[80];
6381da177e4SLinus Torvalds 	sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
6391da177e4SLinus Torvalds 		fh->fh_size,
6401da177e4SLinus Torvalds 		fh->fh_base.fh_pad[0],
6411da177e4SLinus Torvalds 		fh->fh_base.fh_pad[1],
6421da177e4SLinus Torvalds 		fh->fh_base.fh_pad[2],
6431da177e4SLinus Torvalds 		fh->fh_base.fh_pad[3],
6441da177e4SLinus Torvalds 		fh->fh_base.fh_pad[4],
6451da177e4SLinus Torvalds 		fh->fh_base.fh_pad[5]);
6461da177e4SLinus Torvalds 	return buf;
6471da177e4SLinus Torvalds }
648af6a4e28SNeilBrown 
649af6a4e28SNeilBrown enum fsid_source fsid_source(struct svc_fh *fhp)
650af6a4e28SNeilBrown {
651af6a4e28SNeilBrown 	if (fhp->fh_handle.fh_version != 1)
652af6a4e28SNeilBrown 		return FSIDSOURCE_DEV;
653af6a4e28SNeilBrown 	switch(fhp->fh_handle.fh_fsid_type) {
654af6a4e28SNeilBrown 	case FSID_DEV:
655af6a4e28SNeilBrown 	case FSID_ENCODE_DEV:
656af6a4e28SNeilBrown 	case FSID_MAJOR_MINOR:
6578e498751SJ. Bruce Fields 		if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV)
658af6a4e28SNeilBrown 			return FSIDSOURCE_DEV;
659b8da0d1cSNeil Brown 		break;
660af6a4e28SNeilBrown 	case FSID_NUM:
661af6a4e28SNeilBrown 		if (fhp->fh_export->ex_flags & NFSEXP_FSID)
662af6a4e28SNeilBrown 			return FSIDSOURCE_FSID;
663b8da0d1cSNeil Brown 		break;
664b8da0d1cSNeil Brown 	default:
665b8da0d1cSNeil Brown 		break;
666af6a4e28SNeilBrown 	}
667b8da0d1cSNeil Brown 	/* either a UUID type filehandle, or the filehandle doesn't
668b8da0d1cSNeil Brown 	 * match the export.
669b8da0d1cSNeil Brown 	 */
670b8da0d1cSNeil Brown 	if (fhp->fh_export->ex_flags & NFSEXP_FSID)
671b8da0d1cSNeil Brown 		return FSIDSOURCE_FSID;
672b8da0d1cSNeil Brown 	if (fhp->fh_export->ex_uuid)
673b8da0d1cSNeil Brown 		return FSIDSOURCE_UUID;
674b8da0d1cSNeil Brown 	return FSIDSOURCE_DEV;
675af6a4e28SNeilBrown }
676