| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * linux/fs/nfsd/nfsfh.c | 
 | 3 |  * | 
 | 4 |  * NFS server file handle treatment. | 
 | 5 |  * | 
 | 6 |  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> | 
 | 7 |  * Portions Copyright (C) 1999 G. Allen Morris III <gam3@acm.org> | 
 | 8 |  * Extensive rewrite by Neil Brown <neilb@cse.unsw.edu.au> Southern-Spring 1999 | 
 | 9 |  * ... and again Southern-Winter 2001 to support export_operations | 
 | 10 |  */ | 
 | 11 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/slab.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/fs.h> | 
 | 14 | #include <linux/unistd.h> | 
 | 15 | #include <linux/string.h> | 
 | 16 | #include <linux/stat.h> | 
 | 17 | #include <linux/dcache.h> | 
| Christoph Hellwig | a569425 | 2007-07-17 04:04:28 -0700 | [diff] [blame] | 18 | #include <linux/exportfs.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/mount.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 |  | 
| Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 21 | #include <linux/sunrpc/clnt.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/sunrpc/svc.h> | 
| Andy Adamson | 32c1eb0 | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 23 | #include <linux/sunrpc/svcauth_gss.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/nfsd/nfsd.h> | 
| J. Bruce Fields | 2e8138a | 2007-11-15 17:05:43 -0500 | [diff] [blame] | 25 | #include "auth.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 |  | 
 | 27 | #define NFSDDBG_FACILITY		NFSDDBG_FH | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 |  | 
 | 29 |  | 
 | 30 | static int nfsd_nr_verified; | 
 | 31 | static int nfsd_nr_put; | 
 | 32 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | /* | 
 | 34 |  * our acceptability function. | 
 | 35 |  * if NOSUBTREECHECK, accept anything | 
 | 36 |  * if not, require that we can walk up to exp->ex_dentry | 
 | 37 |  * doing some checks on the 'x' bits | 
 | 38 |  */ | 
 | 39 | static int nfsd_acceptable(void *expv, struct dentry *dentry) | 
 | 40 | { | 
 | 41 | 	struct svc_export *exp = expv; | 
 | 42 | 	int rv; | 
 | 43 | 	struct dentry *tdentry; | 
 | 44 | 	struct dentry *parent; | 
 | 45 |  | 
 | 46 | 	if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) | 
 | 47 | 		return 1; | 
 | 48 |  | 
 | 49 | 	tdentry = dget(dentry); | 
 | 50 | 	while (tdentry != exp->ex_dentry && ! IS_ROOT(tdentry)) { | 
 | 51 | 		/* make sure parents give x permission to user */ | 
 | 52 | 		int err; | 
 | 53 | 		parent = dget_parent(tdentry); | 
 | 54 | 		err = permission(parent->d_inode, MAY_EXEC, NULL); | 
 | 55 | 		if (err < 0) { | 
 | 56 | 			dput(parent); | 
 | 57 | 			break; | 
 | 58 | 		} | 
 | 59 | 		dput(tdentry); | 
 | 60 | 		tdentry = parent; | 
 | 61 | 	} | 
 | 62 | 	if (tdentry != exp->ex_dentry) | 
 | 63 | 		dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name); | 
 | 64 | 	rv = (tdentry == exp->ex_dentry); | 
 | 65 | 	dput(tdentry); | 
 | 66 | 	return rv; | 
 | 67 | } | 
 | 68 |  | 
 | 69 | /* Type check. The correct error return for type mismatches does not seem to be | 
 | 70 |  * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a | 
 | 71 |  * comment in the NFSv3 spec says this is incorrect (implementation notes for | 
 | 72 |  * the write call). | 
 | 73 |  */ | 
| Al Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 74 | static inline __be32 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type) | 
 | 76 | { | 
 | 77 | 	/* Type can be negative when creating hardlinks - not to a dir */ | 
 | 78 | 	if (type > 0 && (mode & S_IFMT) != type) { | 
 | 79 | 		if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK) | 
 | 80 | 			return nfserr_symlink; | 
 | 81 | 		else if (type == S_IFDIR) | 
 | 82 | 			return nfserr_notdir; | 
 | 83 | 		else if ((mode & S_IFMT) == S_IFDIR) | 
 | 84 | 			return nfserr_isdir; | 
 | 85 | 		else | 
 | 86 | 			return nfserr_inval; | 
 | 87 | 	} | 
 | 88 | 	if (type < 0 && (mode & S_IFMT) == -type) { | 
 | 89 | 		if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK) | 
 | 90 | 			return nfserr_symlink; | 
 | 91 | 		else if (type == -S_IFDIR) | 
 | 92 | 			return nfserr_isdir; | 
 | 93 | 		else | 
 | 94 | 			return nfserr_notdir; | 
 | 95 | 	} | 
 | 96 | 	return 0; | 
 | 97 | } | 
 | 98 |  | 
| J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 99 | static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp, | 
 | 100 | 					  struct svc_export *exp) | 
 | 101 | { | 
 | 102 | 	/* Check if the request originated from a secure port. */ | 
 | 103 | 	if (!rqstp->rq_secure && EX_SECURE(exp)) { | 
 | 104 | 		char buf[RPC_MAX_ADDRBUFLEN]; | 
 | 105 | 		dprintk(KERN_WARNING | 
 | 106 | 		       "nfsd: request from insecure port %s!\n", | 
 | 107 | 		       svc_print_addr(rqstp, buf, sizeof(buf))); | 
 | 108 | 		return nfserr_perm; | 
 | 109 | 	} | 
 | 110 |  | 
 | 111 | 	/* Set user creds for this exportpoint */ | 
 | 112 | 	return nfserrno(nfsd_setuser(rqstp, exp)); | 
 | 113 | } | 
 | 114 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | /* | 
 | 116 |  * Perform sanity checks on the dentry in a client's file handle. | 
 | 117 |  * | 
 | 118 |  * Note that the file handle dentry may need to be freed even after | 
 | 119 |  * an error return. | 
 | 120 |  * | 
 | 121 |  * This is only called at the start of an nfsproc call, so fhp points to | 
 | 122 |  * a svc_fh which is all 0 except for the over-the-wire file handle. | 
 | 123 |  */ | 
| Al Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 124 | __be32 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access) | 
 | 126 | { | 
 | 127 | 	struct knfsd_fh	*fh = &fhp->fh_handle; | 
 | 128 | 	struct svc_export *exp = NULL; | 
 | 129 | 	struct dentry	*dentry; | 
| Al Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 130 | 	__be32		error = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 |  | 
 | 132 | 	dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp)); | 
 | 133 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | 	if (!fhp->fh_dentry) { | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 135 | 		struct fid *fid = NULL, sfid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | 		int fileid_type; | 
 | 137 | 		int data_left = fh->fh_size/4; | 
 | 138 |  | 
 | 139 | 		error = nfserr_stale; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | 		if (rqstp->rq_vers > 2) | 
 | 141 | 			error = nfserr_badhandle; | 
 | 142 | 		if (rqstp->rq_vers == 4 && fh->fh_size == 0) | 
 | 143 | 			return nfserr_nofilehandle; | 
 | 144 |  | 
 | 145 | 		if (fh->fh_version == 1) { | 
 | 146 | 			int len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | 			if (--data_left<0) goto out; | 
 | 148 | 			switch (fh->fh_auth_type) { | 
 | 149 | 			case 0: break; | 
 | 150 | 			default: goto out; | 
 | 151 | 			} | 
 | 152 | 			len = key_len(fh->fh_fsid_type) / 4; | 
 | 153 | 			if (len == 0) goto out; | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 154 | 			if  (fh->fh_fsid_type == FSID_MAJOR_MINOR) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | 				/* deprecated, convert to type 3 */ | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 156 | 				len = key_len(FSID_ENCODE_DEV)/4; | 
 | 157 | 				fh->fh_fsid_type = FSID_ENCODE_DEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | 				fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1]))); | 
 | 159 | 				fh->fh_fsid[1] = fh->fh_fsid[2]; | 
 | 160 | 			} | 
 | 161 | 			if ((data_left -= len)<0) goto out; | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 162 | 			exp = rqst_exp_find(rqstp, fh->fh_fsid_type, | 
 | 163 | 					    fh->fh_auth); | 
 | 164 | 			fid = (struct fid *)(fh->fh_auth + len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | 		} else { | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 166 | 			__u32 tfh[2]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | 			dev_t xdev; | 
 | 168 | 			ino_t xino; | 
 | 169 | 			if (fh->fh_size != NFS_FHSIZE) | 
 | 170 | 				goto out; | 
 | 171 | 			/* assume old filehandle format */ | 
 | 172 | 			xdev = old_decode_dev(fh->ofh_xdev); | 
 | 173 | 			xino = u32_to_ino_t(fh->ofh_xino); | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 174 | 			mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL); | 
| J. Bruce Fields | 0989a78 | 2007-07-17 04:04:44 -0700 | [diff] [blame] | 175 | 			exp = rqst_exp_find(rqstp, FSID_DEV, tfh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | 		} | 
 | 177 |  | 
| J. Bruce Fields | 2d3bb25 | 2007-07-17 04:04:40 -0700 | [diff] [blame] | 178 | 		error = nfserr_stale; | 
 | 179 | 		if (PTR_ERR(exp) == -ENOENT) | 
 | 180 | 			goto out; | 
 | 181 |  | 
 | 182 | 		if (IS_ERR(exp)) { | 
| J.Bruce Fields | e0bb89e | 2006-12-13 00:35:25 -0800 | [diff] [blame] | 183 | 			error = nfserrno(PTR_ERR(exp)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | 			goto out; | 
| J.Bruce Fields | e0bb89e | 2006-12-13 00:35:25 -0800 | [diff] [blame] | 185 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 |  | 
| J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 187 | 		error = nfsd_setuser_and_check_port(rqstp, exp); | 
| NeilBrown | d1bbf14 | 2006-07-30 03:03:16 -0700 | [diff] [blame] | 188 | 		if (error) | 
 | 189 | 			goto out; | 
 | 190 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | 		/* | 
 | 192 | 		 * Look up the dentry using the NFS file handle. | 
 | 193 | 		 */ | 
 | 194 | 		error = nfserr_stale; | 
 | 195 | 		if (rqstp->rq_vers > 2) | 
 | 196 | 			error = nfserr_badhandle; | 
 | 197 |  | 
 | 198 | 		if (fh->fh_version != 1) { | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 199 | 			sfid.i32.ino = fh->ofh_ino; | 
 | 200 | 			sfid.i32.gen = fh->ofh_generation; | 
 | 201 | 			sfid.i32.parent_ino = fh->ofh_dirino; | 
 | 202 | 			fid = &sfid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | 			data_left = 3; | 
 | 204 | 			if (fh->ofh_dirino == 0) | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 205 | 				fileid_type = FILEID_INO32_GEN; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | 			else | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 207 | 				fileid_type = FILEID_INO32_GEN_PARENT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | 		} else | 
 | 209 | 			fileid_type = fh->fh_fileid_type; | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 210 |  | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 211 | 		if (fileid_type == FILEID_ROOT) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | 			dentry = dget(exp->ex_dentry); | 
 | 213 | 		else { | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 214 | 			dentry = exportfs_decode_fh(exp->ex_mnt, fid, | 
| Christoph Hellwig | d37065c | 2007-07-17 04:04:30 -0700 | [diff] [blame] | 215 | 					data_left, fileid_type, | 
 | 216 | 					nfsd_acceptable, exp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | 		} | 
 | 218 | 		if (dentry == NULL) | 
 | 219 | 			goto out; | 
 | 220 | 		if (IS_ERR(dentry)) { | 
 | 221 | 			if (PTR_ERR(dentry) != -EINVAL) | 
 | 222 | 				error = nfserrno(PTR_ERR(dentry)); | 
 | 223 | 			goto out; | 
 | 224 | 		} | 
| NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 225 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | 		if (S_ISDIR(dentry->d_inode->i_mode) && | 
 | 227 | 		    (dentry->d_flags & DCACHE_DISCONNECTED)) { | 
 | 228 | 			printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n", | 
 | 229 | 			       dentry->d_parent->d_name.name, dentry->d_name.name); | 
 | 230 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 |  | 
 | 232 | 		fhp->fh_dentry = dentry; | 
 | 233 | 		fhp->fh_export = exp; | 
 | 234 | 		nfsd_nr_verified++; | 
 | 235 | 	} else { | 
| J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 236 | 		/* | 
 | 237 | 		 * just rechecking permissions | 
 | 238 | 		 * (e.g. nfsproc_create calls fh_verify, then nfsd_create | 
 | 239 | 		 * does as well) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | 		 */ | 
 | 241 | 		dprintk("nfsd: fh_verify - just checking\n"); | 
 | 242 | 		dentry = fhp->fh_dentry; | 
 | 243 | 		exp = fhp->fh_export; | 
| J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 244 | 		/* | 
 | 245 | 		 * Set user creds for this exportpoint; necessary even | 
| NeilBrown | d1bbf14 | 2006-07-30 03:03:16 -0700 | [diff] [blame] | 246 | 		 * in the "just checking" case because this may be a | 
 | 247 | 		 * filehandle that was created by fh_compose, and that | 
 | 248 | 		 * is about to be used in another nfsv4 compound | 
| J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 249 | 		 * operation. | 
 | 250 | 		 */ | 
 | 251 | 		error = nfsd_setuser_and_check_port(rqstp, exp); | 
| NeilBrown | d1bbf14 | 2006-07-30 03:03:16 -0700 | [diff] [blame] | 252 | 		if (error) | 
 | 253 | 			goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | 	} | 
 | 255 | 	cache_get(&exp->h); | 
 | 256 |  | 
| J. Bruce Fields | 7fc90ec | 2006-06-30 01:56:14 -0700 | [diff] [blame] | 257 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | 	error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type); | 
 | 259 | 	if (error) | 
 | 260 | 		goto out; | 
 | 261 |  | 
| J. Bruce Fields | 9091224 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 262 | 	if (!(access & MAY_LOCK)) { | 
 | 263 | 		/* | 
 | 264 | 		 * pseudoflavor restrictions are not enforced on NLM, | 
 | 265 | 		 * which clients virtually always use auth_sys for, | 
 | 266 | 		 * even while using RPCSEC_GSS for NFS. | 
 | 267 | 		 */ | 
 | 268 | 		error = check_nfsd_access(exp, rqstp); | 
 | 269 | 		if (error) | 
 | 270 | 			goto out; | 
 | 271 | 	} | 
| Andy Adamson | 32c1eb0 | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 272 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | 	/* Finally, check access permissions. */ | 
| J. Bruce Fields | 0ec757d | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 274 | 	error = nfsd_permission(rqstp, exp, dentry, access); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | 	if (error) { | 
| NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 277 | 		dprintk("fh_verify: %s/%s permission failure, " | 
 | 278 | 			"acc=%x, error=%d\n", | 
 | 279 | 			dentry->d_parent->d_name.name, | 
 | 280 | 			dentry->d_name.name, | 
| Al Viro | fc2dd2e | 2007-02-01 13:52:43 +0000 | [diff] [blame] | 281 | 			access, ntohl(error)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | out: | 
 | 284 | 	if (exp && !IS_ERR(exp)) | 
 | 285 | 		exp_put(exp); | 
 | 286 | 	if (error == nfserr_stale) | 
 | 287 | 		nfsdstats.fh_stale++; | 
 | 288 | 	return error; | 
 | 289 | } | 
 | 290 |  | 
 | 291 |  | 
 | 292 | /* | 
 | 293 |  * Compose a file handle for an NFS reply. | 
 | 294 |  * | 
 | 295 |  * Note that when first composed, the dentry may not yet have | 
 | 296 |  * an inode.  In this case a call to fh_update should be made | 
 | 297 |  * before the fh goes out on the wire ... | 
 | 298 |  */ | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 299 | static void _fh_update(struct svc_fh *fhp, struct svc_export *exp, | 
 | 300 | 		struct dentry *dentry) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 302 | 	if (dentry != exp->ex_dentry) { | 
 | 303 | 		struct fid *fid = (struct fid *) | 
 | 304 | 			(fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1); | 
 | 305 | 		int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4; | 
 | 306 | 		int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 |  | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 308 | 		fhp->fh_handle.fh_fileid_type = | 
 | 309 | 			exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck); | 
 | 310 | 		fhp->fh_handle.fh_size += maxsize * 4; | 
 | 311 | 	} else { | 
 | 312 | 		fhp->fh_handle.fh_fileid_type = FILEID_ROOT; | 
 | 313 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } | 
 | 315 |  | 
 | 316 | /* | 
 | 317 |  * for composing old style file handles | 
 | 318 |  */ | 
 | 319 | static inline void _fh_update_old(struct dentry *dentry, | 
 | 320 | 				  struct svc_export *exp, | 
 | 321 | 				  struct knfsd_fh *fh) | 
 | 322 | { | 
 | 323 | 	fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino); | 
 | 324 | 	fh->ofh_generation = dentry->d_inode->i_generation; | 
 | 325 | 	if (S_ISDIR(dentry->d_inode->i_mode) || | 
 | 326 | 	    (exp->ex_flags & NFSEXP_NOSUBTREECHECK)) | 
 | 327 | 		fh->ofh_dirino = 0; | 
 | 328 | } | 
 | 329 |  | 
| Al Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 330 | __be32 | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 331 | fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry, | 
 | 332 | 	   struct svc_fh *ref_fh) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { | 
 | 334 | 	/* ref_fh is a reference file handle. | 
| NeilBrown | 7e40536 | 2006-06-30 01:56:12 -0700 | [diff] [blame] | 335 | 	 * if it is non-null and for the same filesystem, then we should compose | 
 | 336 | 	 * a filehandle which is of the same version, where possible. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | 	 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca | 
 | 338 | 	 * Then create a 32byte filehandle using nfs_fhbase_old | 
 | 339 | 	 * | 
 | 340 | 	 */ | 
 | 341 |  | 
| NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 342 | 	u8 version; | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 343 | 	u8 fsid_type = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | 	struct inode * inode = dentry->d_inode; | 
 | 345 | 	struct dentry *parent = dentry->d_parent; | 
 | 346 | 	__u32 *datap; | 
 | 347 | 	dev_t ex_dev = exp->ex_dentry->d_inode->i_sb->s_dev; | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 348 | 	int root_export = (exp->ex_dentry == exp->ex_dentry->d_sb->s_root); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 |  | 
 | 350 | 	dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n", | 
 | 351 | 		MAJOR(ex_dev), MINOR(ex_dev), | 
 | 352 | 		(long) exp->ex_dentry->d_inode->i_ino, | 
 | 353 | 		parent->d_name.name, dentry->d_name.name, | 
 | 354 | 		(inode ? inode->i_ino : 0)); | 
 | 355 |  | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 356 | 	/* Choose filehandle version and fsid type based on | 
 | 357 | 	 * the reference filehandle (if it is in the same export) | 
 | 358 | 	 * or the export options. | 
 | 359 | 	 */ | 
| NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 360 |  retry: | 
 | 361 | 	version = 1; | 
| NeilBrown | 7e40536 | 2006-06-30 01:56:12 -0700 | [diff] [blame] | 362 | 	if (ref_fh && ref_fh->fh_export == exp) { | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 363 | 		version = ref_fh->fh_handle.fh_version; | 
| NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 364 | 		fsid_type = ref_fh->fh_handle.fh_fsid_type; | 
 | 365 |  | 
 | 366 | 		if (ref_fh == fhp) | 
 | 367 | 			fh_put(ref_fh); | 
 | 368 | 		ref_fh = NULL; | 
 | 369 |  | 
 | 370 | 		switch (version) { | 
 | 371 | 		case 0xca: | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 372 | 			fsid_type = FSID_DEV; | 
| NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 373 | 			break; | 
 | 374 | 		case 1: | 
 | 375 | 			break; | 
 | 376 | 		default: | 
 | 377 | 			goto retry; | 
 | 378 | 		} | 
 | 379 |  | 
 | 380 | 		/* Need to check that this type works for this | 
 | 381 | 		 * export point.  As the fsid -> filesystem mapping | 
 | 382 | 		 * was guided by user-space, there is no guarantee | 
 | 383 | 		 * that the filesystem actually supports that fsid | 
 | 384 | 		 * type. If it doesn't we loop around again without | 
 | 385 | 		 * ref_fh set. | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 386 | 		 */ | 
| NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 387 | 		switch(fsid_type) { | 
 | 388 | 		case FSID_DEV: | 
 | 389 | 			if (!old_valid_dev(ex_dev)) | 
 | 390 | 				goto retry; | 
 | 391 | 			/* FALL THROUGH */ | 
 | 392 | 		case FSID_MAJOR_MINOR: | 
 | 393 | 		case FSID_ENCODE_DEV: | 
 | 394 | 			if (!(exp->ex_dentry->d_inode->i_sb->s_type->fs_flags | 
 | 395 | 			      & FS_REQUIRES_DEV)) | 
 | 396 | 				goto retry; | 
 | 397 | 			break; | 
 | 398 | 		case FSID_NUM: | 
 | 399 | 			if (! (exp->ex_flags & NFSEXP_FSID)) | 
 | 400 | 				goto retry; | 
 | 401 | 			break; | 
 | 402 | 		case FSID_UUID8: | 
 | 403 | 		case FSID_UUID16: | 
 | 404 | 			if (!root_export) | 
 | 405 | 				goto retry; | 
 | 406 | 			/* fall through */ | 
 | 407 | 		case FSID_UUID4_INUM: | 
 | 408 | 		case FSID_UUID16_INUM: | 
 | 409 | 			if (exp->ex_uuid == NULL) | 
 | 410 | 				goto retry; | 
 | 411 | 			break; | 
 | 412 | 		} | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 413 | 	} else if (exp->ex_uuid) { | 
 | 414 | 		if (fhp->fh_maxsize >= 64) { | 
 | 415 | 			if (root_export) | 
 | 416 | 				fsid_type = FSID_UUID16; | 
 | 417 | 			else | 
 | 418 | 				fsid_type = FSID_UUID16_INUM; | 
 | 419 | 		} else { | 
 | 420 | 			if (root_export) | 
 | 421 | 				fsid_type = FSID_UUID8; | 
 | 422 | 			else | 
 | 423 | 				fsid_type = FSID_UUID4_INUM; | 
 | 424 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | 	} else if (exp->ex_flags & NFSEXP_FSID) | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 426 | 		fsid_type = FSID_NUM; | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 427 | 	else if (!old_valid_dev(ex_dev)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | 		/* for newer device numbers, we must use a newer fsid format */ | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 429 | 		fsid_type = FSID_ENCODE_DEV; | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 430 | 	else | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 431 | 		fsid_type = FSID_DEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 |  | 
 | 433 | 	if (ref_fh == fhp) | 
 | 434 | 		fh_put(ref_fh); | 
 | 435 |  | 
 | 436 | 	if (fhp->fh_locked || fhp->fh_dentry) { | 
 | 437 | 		printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n", | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 438 | 		       parent->d_name.name, dentry->d_name.name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | 	} | 
 | 440 | 	if (fhp->fh_maxsize < NFS_FHSIZE) | 
 | 441 | 		printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n", | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 442 | 		       fhp->fh_maxsize, | 
 | 443 | 		       parent->d_name.name, dentry->d_name.name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 |  | 
 | 445 | 	fhp->fh_dentry = dget(dentry); /* our internal copy */ | 
 | 446 | 	fhp->fh_export = exp; | 
 | 447 | 	cache_get(&exp->h); | 
 | 448 |  | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 449 | 	if (version == 0xca) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | 		/* old style filehandle please */ | 
 | 451 | 		memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE); | 
 | 452 | 		fhp->fh_handle.fh_size = NFS_FHSIZE; | 
 | 453 | 		fhp->fh_handle.ofh_dcookie = 0xfeebbaca; | 
 | 454 | 		fhp->fh_handle.ofh_dev =  old_encode_dev(ex_dev); | 
 | 455 | 		fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev; | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 456 | 		fhp->fh_handle.ofh_xino = | 
 | 457 | 			ino_t_to_u32(exp->ex_dentry->d_inode->i_ino); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | 		fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry)); | 
 | 459 | 		if (inode) | 
 | 460 | 			_fh_update_old(dentry, exp, &fhp->fh_handle); | 
 | 461 | 	} else { | 
 | 462 | 		int len; | 
 | 463 | 		fhp->fh_handle.fh_version = 1; | 
 | 464 | 		fhp->fh_handle.fh_auth_type = 0; | 
 | 465 | 		datap = fhp->fh_handle.fh_auth+0; | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 466 | 		fhp->fh_handle.fh_fsid_type = fsid_type; | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 467 | 		mk_fsid(fsid_type, datap, ex_dev, | 
 | 468 | 			exp->ex_dentry->d_inode->i_ino, | 
 | 469 | 			exp->ex_fsid, exp->ex_uuid); | 
 | 470 |  | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 471 | 		len = key_len(fsid_type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | 		datap += len/4; | 
 | 473 | 		fhp->fh_handle.fh_size = 4 + len; | 
 | 474 |  | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 475 | 		if (inode) | 
 | 476 | 			_fh_update(fhp, exp, dentry); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | 		if (fhp->fh_handle.fh_fileid_type == 255) | 
 | 478 | 			return nfserr_opnotsupp; | 
 | 479 | 	} | 
 | 480 |  | 
 | 481 | 	nfsd_nr_verified++; | 
 | 482 | 	return 0; | 
 | 483 | } | 
 | 484 |  | 
 | 485 | /* | 
 | 486 |  * Update file handle information after changing a dentry. | 
 | 487 |  * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create | 
 | 488 |  */ | 
| Al Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 489 | __be32 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | fh_update(struct svc_fh *fhp) | 
 | 491 | { | 
 | 492 | 	struct dentry *dentry; | 
| NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 493 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | 	if (!fhp->fh_dentry) | 
 | 495 | 		goto out_bad; | 
 | 496 |  | 
 | 497 | 	dentry = fhp->fh_dentry; | 
 | 498 | 	if (!dentry->d_inode) | 
 | 499 | 		goto out_negative; | 
 | 500 | 	if (fhp->fh_handle.fh_version != 1) { | 
 | 501 | 		_fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle); | 
 | 502 | 	} else { | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 503 | 		if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT) | 
| NeilBrown | 4c9608b | 2006-06-30 01:56:11 -0700 | [diff] [blame] | 504 | 			goto out; | 
| Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 505 |  | 
 | 506 | 		_fh_update(fhp, fhp->fh_export, dentry); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | 		if (fhp->fh_handle.fh_fileid_type == 255) | 
 | 508 | 			return nfserr_opnotsupp; | 
 | 509 | 	} | 
 | 510 | out: | 
 | 511 | 	return 0; | 
 | 512 |  | 
 | 513 | out_bad: | 
 | 514 | 	printk(KERN_ERR "fh_update: fh not verified!\n"); | 
 | 515 | 	goto out; | 
 | 516 | out_negative: | 
 | 517 | 	printk(KERN_ERR "fh_update: %s/%s still negative!\n", | 
 | 518 | 		dentry->d_parent->d_name.name, dentry->d_name.name); | 
 | 519 | 	goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } | 
 | 521 |  | 
 | 522 | /* | 
 | 523 |  * Release a file handle. | 
 | 524 |  */ | 
 | 525 | void | 
 | 526 | fh_put(struct svc_fh *fhp) | 
 | 527 | { | 
 | 528 | 	struct dentry * dentry = fhp->fh_dentry; | 
 | 529 | 	struct svc_export * exp = fhp->fh_export; | 
 | 530 | 	if (dentry) { | 
 | 531 | 		fh_unlock(fhp); | 
 | 532 | 		fhp->fh_dentry = NULL; | 
 | 533 | 		dput(dentry); | 
 | 534 | #ifdef CONFIG_NFSD_V3 | 
 | 535 | 		fhp->fh_pre_saved = 0; | 
 | 536 | 		fhp->fh_post_saved = 0; | 
 | 537 | #endif | 
 | 538 | 		nfsd_nr_put++; | 
 | 539 | 	} | 
 | 540 | 	if (exp) { | 
| NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 541 | 		cache_put(&exp->h, &svc_export_cache); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | 		fhp->fh_export = NULL; | 
 | 543 | 	} | 
 | 544 | 	return; | 
 | 545 | } | 
 | 546 |  | 
 | 547 | /* | 
 | 548 |  * Shorthand for dprintk()'s | 
 | 549 |  */ | 
 | 550 | char * SVCFH_fmt(struct svc_fh *fhp) | 
 | 551 | { | 
 | 552 | 	struct knfsd_fh *fh = &fhp->fh_handle; | 
 | 553 |  | 
 | 554 | 	static char buf[80]; | 
 | 555 | 	sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x", | 
 | 556 | 		fh->fh_size, | 
 | 557 | 		fh->fh_base.fh_pad[0], | 
 | 558 | 		fh->fh_base.fh_pad[1], | 
 | 559 | 		fh->fh_base.fh_pad[2], | 
 | 560 | 		fh->fh_base.fh_pad[3], | 
 | 561 | 		fh->fh_base.fh_pad[4], | 
 | 562 | 		fh->fh_base.fh_pad[5]); | 
 | 563 | 	return buf; | 
 | 564 | } | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 565 |  | 
 | 566 | enum fsid_source fsid_source(struct svc_fh *fhp) | 
 | 567 | { | 
 | 568 | 	if (fhp->fh_handle.fh_version != 1) | 
 | 569 | 		return FSIDSOURCE_DEV; | 
 | 570 | 	switch(fhp->fh_handle.fh_fsid_type) { | 
 | 571 | 	case FSID_DEV: | 
 | 572 | 	case FSID_ENCODE_DEV: | 
 | 573 | 	case FSID_MAJOR_MINOR: | 
| Neil Brown | b8da0d1 | 2007-09-05 17:22:13 -0400 | [diff] [blame] | 574 | 		if (fhp->fh_export->ex_dentry->d_inode->i_sb->s_type->fs_flags | 
 | 575 | 		    & FS_REQUIRES_DEV) | 
 | 576 | 			return FSIDSOURCE_DEV; | 
 | 577 | 		break; | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 578 | 	case FSID_NUM: | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 579 | 		if (fhp->fh_export->ex_flags & NFSEXP_FSID) | 
 | 580 | 			return FSIDSOURCE_FSID; | 
| Neil Brown | b8da0d1 | 2007-09-05 17:22:13 -0400 | [diff] [blame] | 581 | 		break; | 
 | 582 | 	default: | 
 | 583 | 		break; | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 584 | 	} | 
| Neil Brown | b8da0d1 | 2007-09-05 17:22:13 -0400 | [diff] [blame] | 585 | 	/* either a UUID type filehandle, or the filehandle doesn't | 
 | 586 | 	 * match the export. | 
 | 587 | 	 */ | 
 | 588 | 	if (fhp->fh_export->ex_flags & NFSEXP_FSID) | 
 | 589 | 		return FSIDSOURCE_FSID; | 
 | 590 | 	if (fhp->fh_export->ex_uuid) | 
 | 591 | 		return FSIDSOURCE_UUID; | 
 | 592 | 	return FSIDSOURCE_DEV; | 
| NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 593 | } |