xref: /openbmc/linux/fs/nfsd/nfsfh.c (revision 4c9608b2f2f51e84eba72f4bf061d360259ce656)
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/sched.h>
131da177e4SLinus Torvalds #include <linux/slab.h>
141da177e4SLinus Torvalds #include <linux/smp_lock.h>
151da177e4SLinus Torvalds #include <linux/fs.h>
161da177e4SLinus Torvalds #include <linux/unistd.h>
171da177e4SLinus Torvalds #include <linux/string.h>
181da177e4SLinus Torvalds #include <linux/stat.h>
191da177e4SLinus Torvalds #include <linux/dcache.h>
201da177e4SLinus Torvalds #include <linux/mount.h>
211da177e4SLinus Torvalds #include <asm/pgtable.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
241da177e4SLinus Torvalds #include <linux/nfsd/nfsd.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #define NFSDDBG_FACILITY		NFSDDBG_FH
271da177e4SLinus Torvalds #define NFSD_PARANOIA 1
281da177e4SLinus Torvalds /* #define NFSD_DEBUG_VERBOSE 1 */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds static int nfsd_nr_verified;
321da177e4SLinus Torvalds static int nfsd_nr_put;
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds extern struct export_operations export_op_default;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #define	CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /*
391da177e4SLinus Torvalds  * our acceptability function.
401da177e4SLinus Torvalds  * if NOSUBTREECHECK, accept anything
411da177e4SLinus Torvalds  * if not, require that we can walk up to exp->ex_dentry
421da177e4SLinus Torvalds  * doing some checks on the 'x' bits
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds static int nfsd_acceptable(void *expv, struct dentry *dentry)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	struct svc_export *exp = expv;
471da177e4SLinus Torvalds 	int rv;
481da177e4SLinus Torvalds 	struct dentry *tdentry;
491da177e4SLinus Torvalds 	struct dentry *parent;
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
521da177e4SLinus Torvalds 		return 1;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	tdentry = dget(dentry);
551da177e4SLinus Torvalds 	while (tdentry != exp->ex_dentry && ! IS_ROOT(tdentry)) {
561da177e4SLinus Torvalds 		/* make sure parents give x permission to user */
571da177e4SLinus Torvalds 		int err;
581da177e4SLinus Torvalds 		parent = dget_parent(tdentry);
591da177e4SLinus Torvalds 		err = permission(parent->d_inode, MAY_EXEC, NULL);
601da177e4SLinus Torvalds 		if (err < 0) {
611da177e4SLinus Torvalds 			dput(parent);
621da177e4SLinus Torvalds 			break;
631da177e4SLinus Torvalds 		}
641da177e4SLinus Torvalds 		dput(tdentry);
651da177e4SLinus Torvalds 		tdentry = parent;
661da177e4SLinus Torvalds 	}
671da177e4SLinus Torvalds 	if (tdentry != exp->ex_dentry)
681da177e4SLinus Torvalds 		dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
691da177e4SLinus Torvalds 	rv = (tdentry == exp->ex_dentry);
701da177e4SLinus Torvalds 	dput(tdentry);
711da177e4SLinus Torvalds 	return rv;
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* Type check. The correct error return for type mismatches does not seem to be
751da177e4SLinus Torvalds  * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
761da177e4SLinus Torvalds  * comment in the NFSv3 spec says this is incorrect (implementation notes for
771da177e4SLinus Torvalds  * the write call).
781da177e4SLinus Torvalds  */
791da177e4SLinus Torvalds static inline int
801da177e4SLinus Torvalds nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	/* Type can be negative when creating hardlinks - not to a dir */
831da177e4SLinus Torvalds 	if (type > 0 && (mode & S_IFMT) != type) {
841da177e4SLinus Torvalds 		if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
851da177e4SLinus Torvalds 			return nfserr_symlink;
861da177e4SLinus Torvalds 		else if (type == S_IFDIR)
871da177e4SLinus Torvalds 			return nfserr_notdir;
881da177e4SLinus Torvalds 		else if ((mode & S_IFMT) == S_IFDIR)
891da177e4SLinus Torvalds 			return nfserr_isdir;
901da177e4SLinus Torvalds 		else
911da177e4SLinus Torvalds 			return nfserr_inval;
921da177e4SLinus Torvalds 	}
931da177e4SLinus Torvalds 	if (type < 0 && (mode & S_IFMT) == -type) {
941da177e4SLinus Torvalds 		if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
951da177e4SLinus Torvalds 			return nfserr_symlink;
961da177e4SLinus Torvalds 		else if (type == -S_IFDIR)
971da177e4SLinus Torvalds 			return nfserr_isdir;
981da177e4SLinus Torvalds 		else
991da177e4SLinus Torvalds 			return nfserr_notdir;
1001da177e4SLinus Torvalds 	}
1011da177e4SLinus Torvalds 	return 0;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * Perform sanity checks on the dentry in a client's file handle.
1061da177e4SLinus Torvalds  *
1071da177e4SLinus Torvalds  * Note that the file handle dentry may need to be freed even after
1081da177e4SLinus Torvalds  * an error return.
1091da177e4SLinus Torvalds  *
1101da177e4SLinus Torvalds  * This is only called at the start of an nfsproc call, so fhp points to
1111da177e4SLinus Torvalds  * a svc_fh which is all 0 except for the over-the-wire file handle.
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds u32
1141da177e4SLinus Torvalds fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	struct knfsd_fh	*fh = &fhp->fh_handle;
1171da177e4SLinus Torvalds 	struct svc_export *exp = NULL;
1181da177e4SLinus Torvalds 	struct dentry	*dentry;
1191da177e4SLinus Torvalds 	u32		error = 0;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	/* keep this filehandle for possible reference  when encoding attributes */
1241da177e4SLinus Torvalds 	rqstp->rq_reffh = fh;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	if (!fhp->fh_dentry) {
1271da177e4SLinus Torvalds 		__u32 *datap=NULL;
1281da177e4SLinus Torvalds 		__u32 tfh[3];		/* filehandle fragment for oldstyle filehandles */
1291da177e4SLinus Torvalds 		int fileid_type;
1301da177e4SLinus Torvalds 		int data_left = fh->fh_size/4;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 		error = nfserr_stale;
1331da177e4SLinus Torvalds 		if (rqstp->rq_client == NULL)
1341da177e4SLinus Torvalds 			goto out;
1351da177e4SLinus Torvalds 		if (rqstp->rq_vers > 2)
1361da177e4SLinus Torvalds 			error = nfserr_badhandle;
1371da177e4SLinus Torvalds 		if (rqstp->rq_vers == 4 && fh->fh_size == 0)
1381da177e4SLinus Torvalds 			return nfserr_nofilehandle;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 		if (fh->fh_version == 1) {
1411da177e4SLinus Torvalds 			int len;
1421da177e4SLinus Torvalds 			datap = fh->fh_auth;
1431da177e4SLinus Torvalds 			if (--data_left<0) goto out;
1441da177e4SLinus Torvalds 			switch (fh->fh_auth_type) {
1451da177e4SLinus Torvalds 			case 0: break;
1461da177e4SLinus Torvalds 			default: goto out;
1471da177e4SLinus Torvalds 			}
1481da177e4SLinus Torvalds 			len = key_len(fh->fh_fsid_type) / 4;
1491da177e4SLinus Torvalds 			if (len == 0) goto out;
1501da177e4SLinus Torvalds 			if  (fh->fh_fsid_type == 2) {
1511da177e4SLinus Torvalds 				/* deprecated, convert to type 3 */
1521da177e4SLinus Torvalds 				len = 3;
1531da177e4SLinus Torvalds 				fh->fh_fsid_type = 3;
1541da177e4SLinus Torvalds 				fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
1551da177e4SLinus Torvalds 				fh->fh_fsid[1] = fh->fh_fsid[2];
1561da177e4SLinus Torvalds 			}
1571da177e4SLinus Torvalds 			if ((data_left -= len)<0) goto out;
1581da177e4SLinus Torvalds 			exp = exp_find(rqstp->rq_client, fh->fh_fsid_type, datap, &rqstp->rq_chandle);
1591da177e4SLinus Torvalds 			datap += len;
1601da177e4SLinus Torvalds 		} else {
1611da177e4SLinus Torvalds 			dev_t xdev;
1621da177e4SLinus Torvalds 			ino_t xino;
1631da177e4SLinus Torvalds 			if (fh->fh_size != NFS_FHSIZE)
1641da177e4SLinus Torvalds 				goto out;
1651da177e4SLinus Torvalds 			/* assume old filehandle format */
1661da177e4SLinus Torvalds 			xdev = old_decode_dev(fh->ofh_xdev);
1671da177e4SLinus Torvalds 			xino = u32_to_ino_t(fh->ofh_xino);
1681da177e4SLinus Torvalds 			mk_fsid_v0(tfh, xdev, xino);
1691da177e4SLinus Torvalds 			exp = exp_find(rqstp->rq_client, 0, tfh, &rqstp->rq_chandle);
1701da177e4SLinus Torvalds 		}
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 		error = nfserr_dropit;
1731da177e4SLinus Torvalds 		if (IS_ERR(exp) && PTR_ERR(exp) == -EAGAIN)
1741da177e4SLinus Torvalds 			goto out;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 		error = nfserr_stale;
1771da177e4SLinus Torvalds 		if (!exp || IS_ERR(exp))
1781da177e4SLinus Torvalds 			goto out;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 		/* Check if the request originated from a secure port. */
1811da177e4SLinus Torvalds 		error = nfserr_perm;
1821da177e4SLinus Torvalds 		if (!rqstp->rq_secure && EX_SECURE(exp)) {
1831da177e4SLinus Torvalds 			printk(KERN_WARNING
1841da177e4SLinus Torvalds 			       "nfsd: request from insecure port (%u.%u.%u.%u:%d)!\n",
1851da177e4SLinus Torvalds 			       NIPQUAD(rqstp->rq_addr.sin_addr.s_addr),
1861da177e4SLinus Torvalds 			       ntohs(rqstp->rq_addr.sin_port));
1871da177e4SLinus Torvalds 			goto out;
1881da177e4SLinus Torvalds 		}
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 		/* Set user creds for this exportpoint */
1911da177e4SLinus Torvalds 		error = nfsd_setuser(rqstp, exp);
1921da177e4SLinus Torvalds 		if (error) {
1931da177e4SLinus Torvalds 			error = nfserrno(error);
1941da177e4SLinus Torvalds 			goto out;
1951da177e4SLinus Torvalds 		}
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 		/*
1981da177e4SLinus Torvalds 		 * Look up the dentry using the NFS file handle.
1991da177e4SLinus Torvalds 		 */
2001da177e4SLinus Torvalds 		error = nfserr_stale;
2011da177e4SLinus Torvalds 		if (rqstp->rq_vers > 2)
2021da177e4SLinus Torvalds 			error = nfserr_badhandle;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 		if (fh->fh_version != 1) {
2051da177e4SLinus Torvalds 			tfh[0] = fh->ofh_ino;
2061da177e4SLinus Torvalds 			tfh[1] = fh->ofh_generation;
2071da177e4SLinus Torvalds 			tfh[2] = fh->ofh_dirino;
2081da177e4SLinus Torvalds 			datap = tfh;
2091da177e4SLinus Torvalds 			data_left = 3;
2101da177e4SLinus Torvalds 			if (fh->ofh_dirino == 0)
2111da177e4SLinus Torvalds 				fileid_type = 1;
2121da177e4SLinus Torvalds 			else
2131da177e4SLinus Torvalds 				fileid_type = 2;
2141da177e4SLinus Torvalds 		} else
2151da177e4SLinus Torvalds 			fileid_type = fh->fh_fileid_type;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 		if (fileid_type == 0)
2181da177e4SLinus Torvalds 			dentry = dget(exp->ex_dentry);
2191da177e4SLinus Torvalds 		else {
2201da177e4SLinus Torvalds 			struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
2211da177e4SLinus Torvalds 			dentry = CALL(nop,decode_fh)(exp->ex_mnt->mnt_sb,
2221da177e4SLinus Torvalds 						     datap, data_left,
2231da177e4SLinus Torvalds 						     fileid_type,
2241da177e4SLinus Torvalds 						     nfsd_acceptable, exp);
2251da177e4SLinus Torvalds 		}
2261da177e4SLinus Torvalds 		if (dentry == NULL)
2271da177e4SLinus Torvalds 			goto out;
2281da177e4SLinus Torvalds 		if (IS_ERR(dentry)) {
2291da177e4SLinus Torvalds 			if (PTR_ERR(dentry) != -EINVAL)
2301da177e4SLinus Torvalds 				error = nfserrno(PTR_ERR(dentry));
2311da177e4SLinus Torvalds 			goto out;
2321da177e4SLinus Torvalds 		}
2331da177e4SLinus Torvalds #ifdef NFSD_PARANOIA
2341da177e4SLinus Torvalds 		if (S_ISDIR(dentry->d_inode->i_mode) &&
2351da177e4SLinus Torvalds 		    (dentry->d_flags & DCACHE_DISCONNECTED)) {
2361da177e4SLinus Torvalds 			printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
2371da177e4SLinus Torvalds 			       dentry->d_parent->d_name.name, dentry->d_name.name);
2381da177e4SLinus Torvalds 		}
2391da177e4SLinus Torvalds #endif
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 		fhp->fh_dentry = dentry;
2421da177e4SLinus Torvalds 		fhp->fh_export = exp;
2431da177e4SLinus Torvalds 		nfsd_nr_verified++;
2441da177e4SLinus Torvalds 	} else {
2451da177e4SLinus Torvalds 		/* just rechecking permissions
2461da177e4SLinus Torvalds 		 * (e.g. nfsproc_create calls fh_verify, then nfsd_create does as well)
2471da177e4SLinus Torvalds 		 */
2481da177e4SLinus Torvalds 		dprintk("nfsd: fh_verify - just checking\n");
2491da177e4SLinus Torvalds 		dentry = fhp->fh_dentry;
2501da177e4SLinus Torvalds 		exp = fhp->fh_export;
2511da177e4SLinus Torvalds 	}
2521da177e4SLinus Torvalds 	cache_get(&exp->h);
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
2551da177e4SLinus Torvalds 	if (error)
2561da177e4SLinus Torvalds 		goto out;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	/* Finally, check access permissions. */
2591da177e4SLinus Torvalds 	error = nfsd_permission(exp, dentry, access);
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds #ifdef NFSD_PARANOIA_EXTREME
2621da177e4SLinus Torvalds 	if (error) {
2631da177e4SLinus Torvalds 		printk("fh_verify: %s/%s permission failure, acc=%x, error=%d\n",
2641da177e4SLinus Torvalds 		       dentry->d_parent->d_name.name, dentry->d_name.name, access, (error >> 24));
2651da177e4SLinus Torvalds 	}
2661da177e4SLinus Torvalds #endif
2671da177e4SLinus Torvalds out:
2681da177e4SLinus Torvalds 	if (exp && !IS_ERR(exp))
2691da177e4SLinus Torvalds 		exp_put(exp);
2701da177e4SLinus Torvalds 	if (error == nfserr_stale)
2711da177e4SLinus Torvalds 		nfsdstats.fh_stale++;
2721da177e4SLinus Torvalds 	return error;
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds /*
2771da177e4SLinus Torvalds  * Compose a file handle for an NFS reply.
2781da177e4SLinus Torvalds  *
2791da177e4SLinus Torvalds  * Note that when first composed, the dentry may not yet have
2801da177e4SLinus Torvalds  * an inode.  In this case a call to fh_update should be made
2811da177e4SLinus Torvalds  * before the fh goes out on the wire ...
2821da177e4SLinus Torvalds  */
2831da177e4SLinus Torvalds static inline int _fh_update(struct dentry *dentry, struct svc_export *exp,
2841da177e4SLinus Torvalds 			     __u32 *datap, int *maxsize)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	if (dentry == exp->ex_dentry) {
2891da177e4SLinus Torvalds 		*maxsize = 0;
2901da177e4SLinus Torvalds 		return 0;
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	return CALL(nop,encode_fh)(dentry, datap, maxsize,
2941da177e4SLinus Torvalds 			  !(exp->ex_flags&NFSEXP_NOSUBTREECHECK));
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds /*
2981da177e4SLinus Torvalds  * for composing old style file handles
2991da177e4SLinus Torvalds  */
3001da177e4SLinus Torvalds static inline void _fh_update_old(struct dentry *dentry,
3011da177e4SLinus Torvalds 				  struct svc_export *exp,
3021da177e4SLinus Torvalds 				  struct knfsd_fh *fh)
3031da177e4SLinus Torvalds {
3041da177e4SLinus Torvalds 	fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
3051da177e4SLinus Torvalds 	fh->ofh_generation = dentry->d_inode->i_generation;
3061da177e4SLinus Torvalds 	if (S_ISDIR(dentry->d_inode->i_mode) ||
3071da177e4SLinus Torvalds 	    (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
3081da177e4SLinus Torvalds 		fh->ofh_dirino = 0;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds int
3121da177e4SLinus Torvalds fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry, struct svc_fh *ref_fh)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	/* ref_fh is a reference file handle.
3151da177e4SLinus Torvalds 	 * if it is non-null, then we should compose a filehandle which is
3161da177e4SLinus Torvalds 	 * of the same version, where possible.
3171da177e4SLinus Torvalds 	 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
3181da177e4SLinus Torvalds 	 * Then create a 32byte filehandle using nfs_fhbase_old
3191da177e4SLinus Torvalds 	 *
3201da177e4SLinus Torvalds 	 */
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	u8 ref_fh_version = 0;
3231da177e4SLinus Torvalds 	u8 ref_fh_fsid_type = 0;
3241da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
3251da177e4SLinus Torvalds 	struct dentry *parent = dentry->d_parent;
3261da177e4SLinus Torvalds 	__u32 *datap;
3271da177e4SLinus Torvalds 	dev_t ex_dev = exp->ex_dentry->d_inode->i_sb->s_dev;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
3301da177e4SLinus Torvalds 		MAJOR(ex_dev), MINOR(ex_dev),
3311da177e4SLinus Torvalds 		(long) exp->ex_dentry->d_inode->i_ino,
3321da177e4SLinus Torvalds 		parent->d_name.name, dentry->d_name.name,
3331da177e4SLinus Torvalds 		(inode ? inode->i_ino : 0));
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	if (ref_fh) {
3361da177e4SLinus Torvalds 		ref_fh_version = ref_fh->fh_handle.fh_version;
3371da177e4SLinus Torvalds 		if (ref_fh_version == 0xca)
3381da177e4SLinus Torvalds 			ref_fh_fsid_type = 0;
3391da177e4SLinus Torvalds 		else
3401da177e4SLinus Torvalds 			ref_fh_fsid_type = ref_fh->fh_handle.fh_fsid_type;
3411da177e4SLinus Torvalds 		if (ref_fh_fsid_type > 3)
3421da177e4SLinus Torvalds 			ref_fh_fsid_type = 0;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 		/* make sure ref_fh type works for given export */
3451da177e4SLinus Torvalds 		if (ref_fh_fsid_type == 1 &&
3461da177e4SLinus Torvalds 		    !(exp->ex_flags & NFSEXP_FSID)) {
3471da177e4SLinus Torvalds 			/* if we don't have an fsid, we cannot provide one... */
3481da177e4SLinus Torvalds 			ref_fh_fsid_type = 0;
3491da177e4SLinus Torvalds 		}
3501da177e4SLinus Torvalds 	} else if (exp->ex_flags & NFSEXP_FSID)
3511da177e4SLinus Torvalds 		ref_fh_fsid_type = 1;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	if (!old_valid_dev(ex_dev) && ref_fh_fsid_type == 0) {
3541da177e4SLinus Torvalds 		/* for newer device numbers, we must use a newer fsid format */
3551da177e4SLinus Torvalds 		ref_fh_version = 1;
3561da177e4SLinus Torvalds 		ref_fh_fsid_type = 3;
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 	if (old_valid_dev(ex_dev) &&
3591da177e4SLinus Torvalds 	    (ref_fh_fsid_type == 2 || ref_fh_fsid_type == 3))
3601da177e4SLinus Torvalds 		/* must use type1 for smaller device numbers */
3611da177e4SLinus Torvalds 		ref_fh_fsid_type = 0;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	if (ref_fh == fhp)
3641da177e4SLinus Torvalds 		fh_put(ref_fh);
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	if (fhp->fh_locked || fhp->fh_dentry) {
3671da177e4SLinus Torvalds 		printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
3681da177e4SLinus Torvalds 			parent->d_name.name, dentry->d_name.name);
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 	if (fhp->fh_maxsize < NFS_FHSIZE)
3711da177e4SLinus Torvalds 		printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
3721da177e4SLinus Torvalds 		       fhp->fh_maxsize, parent->d_name.name, dentry->d_name.name);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	fhp->fh_dentry = dget(dentry); /* our internal copy */
3751da177e4SLinus Torvalds 	fhp->fh_export = exp;
3761da177e4SLinus Torvalds 	cache_get(&exp->h);
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	if (ref_fh_version == 0xca) {
3791da177e4SLinus Torvalds 		/* old style filehandle please */
3801da177e4SLinus Torvalds 		memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
3811da177e4SLinus Torvalds 		fhp->fh_handle.fh_size = NFS_FHSIZE;
3821da177e4SLinus Torvalds 		fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
3831da177e4SLinus Torvalds 		fhp->fh_handle.ofh_dev =  old_encode_dev(ex_dev);
3841da177e4SLinus Torvalds 		fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
3851da177e4SLinus Torvalds 		fhp->fh_handle.ofh_xino = ino_t_to_u32(exp->ex_dentry->d_inode->i_ino);
3861da177e4SLinus Torvalds 		fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
3871da177e4SLinus Torvalds 		if (inode)
3881da177e4SLinus Torvalds 			_fh_update_old(dentry, exp, &fhp->fh_handle);
3891da177e4SLinus Torvalds 	} else {
3901da177e4SLinus Torvalds 		int len;
3911da177e4SLinus Torvalds 		fhp->fh_handle.fh_version = 1;
3921da177e4SLinus Torvalds 		fhp->fh_handle.fh_auth_type = 0;
3931da177e4SLinus Torvalds 		datap = fhp->fh_handle.fh_auth+0;
3941da177e4SLinus Torvalds 		fhp->fh_handle.fh_fsid_type = ref_fh_fsid_type;
3951da177e4SLinus Torvalds 		switch (ref_fh_fsid_type) {
3961da177e4SLinus Torvalds 			case 0:
3971da177e4SLinus Torvalds 				/*
3981da177e4SLinus Torvalds 				 * fsid_type 0:
3991da177e4SLinus Torvalds 				 * 2byte major, 2byte minor, 4byte inode
4001da177e4SLinus Torvalds 				 */
4011da177e4SLinus Torvalds 				mk_fsid_v0(datap, ex_dev,
4021da177e4SLinus Torvalds 					exp->ex_dentry->d_inode->i_ino);
4031da177e4SLinus Torvalds 				break;
4041da177e4SLinus Torvalds 			case 1:
4051da177e4SLinus Torvalds 				/* fsid_type 1 == 4 bytes filesystem id */
4061da177e4SLinus Torvalds 				mk_fsid_v1(datap, exp->ex_fsid);
4071da177e4SLinus Torvalds 				break;
4081da177e4SLinus Torvalds 			case 2:
4091da177e4SLinus Torvalds 				/*
4101da177e4SLinus Torvalds 				 * fsid_type 2:
4111da177e4SLinus Torvalds 				 * 4byte major, 4byte minor, 4byte inode
4121da177e4SLinus Torvalds 				 */
4131da177e4SLinus Torvalds 				mk_fsid_v2(datap, ex_dev,
4141da177e4SLinus Torvalds 					exp->ex_dentry->d_inode->i_ino);
4151da177e4SLinus Torvalds 				break;
4161da177e4SLinus Torvalds 			case 3:
4171da177e4SLinus Torvalds 				/*
4181da177e4SLinus Torvalds 				 * fsid_type 3:
4191da177e4SLinus Torvalds 				 * 4byte devicenumber, 4byte inode
4201da177e4SLinus Torvalds 				 */
4211da177e4SLinus Torvalds 				mk_fsid_v3(datap, ex_dev,
4221da177e4SLinus Torvalds 					exp->ex_dentry->d_inode->i_ino);
4231da177e4SLinus Torvalds 				break;
4241da177e4SLinus Torvalds 		}
4251da177e4SLinus Torvalds 		len = key_len(ref_fh_fsid_type);
4261da177e4SLinus Torvalds 		datap += len/4;
4271da177e4SLinus Torvalds 		fhp->fh_handle.fh_size = 4 + len;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 		if (inode) {
4301da177e4SLinus Torvalds 			int size = (fhp->fh_maxsize-len-4)/4;
4311da177e4SLinus Torvalds 			fhp->fh_handle.fh_fileid_type =
4321da177e4SLinus Torvalds 				_fh_update(dentry, exp, datap, &size);
4331da177e4SLinus Torvalds 			fhp->fh_handle.fh_size += size*4;
4341da177e4SLinus Torvalds 		}
4351da177e4SLinus Torvalds 		if (fhp->fh_handle.fh_fileid_type == 255)
4361da177e4SLinus Torvalds 			return nfserr_opnotsupp;
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	nfsd_nr_verified++;
4401da177e4SLinus Torvalds 	return 0;
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds /*
4441da177e4SLinus Torvalds  * Update file handle information after changing a dentry.
4451da177e4SLinus Torvalds  * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
4461da177e4SLinus Torvalds  */
4471da177e4SLinus Torvalds int
4481da177e4SLinus Torvalds fh_update(struct svc_fh *fhp)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds 	struct dentry *dentry;
4511da177e4SLinus Torvalds 	__u32 *datap;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	if (!fhp->fh_dentry)
4541da177e4SLinus Torvalds 		goto out_bad;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
4571da177e4SLinus Torvalds 	if (!dentry->d_inode)
4581da177e4SLinus Torvalds 		goto out_negative;
4591da177e4SLinus Torvalds 	if (fhp->fh_handle.fh_version != 1) {
4601da177e4SLinus Torvalds 		_fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
4611da177e4SLinus Torvalds 	} else {
4621da177e4SLinus Torvalds 		int size;
4631da177e4SLinus Torvalds 		if (fhp->fh_handle.fh_fileid_type != 0)
464*4c9608b2SNeilBrown 			goto out;
4651da177e4SLinus Torvalds 		datap = fhp->fh_handle.fh_auth+
4661da177e4SLinus Torvalds 			fhp->fh_handle.fh_size/4 -1;
4671da177e4SLinus Torvalds 		size = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
4681da177e4SLinus Torvalds 		fhp->fh_handle.fh_fileid_type =
4691da177e4SLinus Torvalds 			_fh_update(dentry, fhp->fh_export, datap, &size);
4701da177e4SLinus Torvalds 		fhp->fh_handle.fh_size += size*4;
4711da177e4SLinus Torvalds 		if (fhp->fh_handle.fh_fileid_type == 255)
4721da177e4SLinus Torvalds 			return nfserr_opnotsupp;
4731da177e4SLinus Torvalds 	}
4741da177e4SLinus Torvalds out:
4751da177e4SLinus Torvalds 	return 0;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds out_bad:
4781da177e4SLinus Torvalds 	printk(KERN_ERR "fh_update: fh not verified!\n");
4791da177e4SLinus Torvalds 	goto out;
4801da177e4SLinus Torvalds out_negative:
4811da177e4SLinus Torvalds 	printk(KERN_ERR "fh_update: %s/%s still negative!\n",
4821da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
4831da177e4SLinus Torvalds 	goto out;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds /*
4871da177e4SLinus Torvalds  * Release a file handle.
4881da177e4SLinus Torvalds  */
4891da177e4SLinus Torvalds void
4901da177e4SLinus Torvalds fh_put(struct svc_fh *fhp)
4911da177e4SLinus Torvalds {
4921da177e4SLinus Torvalds 	struct dentry * dentry = fhp->fh_dentry;
4931da177e4SLinus Torvalds 	struct svc_export * exp = fhp->fh_export;
4941da177e4SLinus Torvalds 	if (dentry) {
4951da177e4SLinus Torvalds 		fh_unlock(fhp);
4961da177e4SLinus Torvalds 		fhp->fh_dentry = NULL;
4971da177e4SLinus Torvalds 		dput(dentry);
4981da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
4991da177e4SLinus Torvalds 		fhp->fh_pre_saved = 0;
5001da177e4SLinus Torvalds 		fhp->fh_post_saved = 0;
5011da177e4SLinus Torvalds #endif
5021da177e4SLinus Torvalds 		nfsd_nr_put++;
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 	if (exp) {
505baab935fSNeilBrown 		cache_put(&exp->h, &svc_export_cache);
5061da177e4SLinus Torvalds 		fhp->fh_export = NULL;
5071da177e4SLinus Torvalds 	}
5081da177e4SLinus Torvalds 	return;
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds /*
5121da177e4SLinus Torvalds  * Shorthand for dprintk()'s
5131da177e4SLinus Torvalds  */
5141da177e4SLinus Torvalds char * SVCFH_fmt(struct svc_fh *fhp)
5151da177e4SLinus Torvalds {
5161da177e4SLinus Torvalds 	struct knfsd_fh *fh = &fhp->fh_handle;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	static char buf[80];
5191da177e4SLinus Torvalds 	sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
5201da177e4SLinus Torvalds 		fh->fh_size,
5211da177e4SLinus Torvalds 		fh->fh_base.fh_pad[0],
5221da177e4SLinus Torvalds 		fh->fh_base.fh_pad[1],
5231da177e4SLinus Torvalds 		fh->fh_base.fh_pad[2],
5241da177e4SLinus Torvalds 		fh->fh_base.fh_pad[3],
5251da177e4SLinus Torvalds 		fh->fh_base.fh_pad[4],
5261da177e4SLinus Torvalds 		fh->fh_base.fh_pad[5]);
5271da177e4SLinus Torvalds 	return buf;
5281da177e4SLinus Torvalds }
529