xref: /linux/fs/nfsd/nfsfh.c (revision 067012974c8ae31a8886046df082aeba93592972)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NFS server file handle treatment.
4  *
5  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
6  * Portions Copyright (C) 1999 G. Allen Morris III <gam3@acm.org>
7  * Extensive rewrite by Neil Brown <neilb@cse.unsw.edu.au> Southern-Spring 1999
8  * ... and again Southern-Winter 2001 to support export_operations
9  */
10 
11 #include <linux/exportfs.h>
12 
13 #include <linux/sunrpc/svcauth_gss.h>
14 #include "nfsd.h"
15 #include "vfs.h"
16 #include "auth.h"
17 #include "trace.h"
18 
19 #define NFSDDBG_FACILITY		NFSDDBG_FH
20 
21 
22 /*
23  * our acceptability function.
24  * if NOSUBTREECHECK, accept anything
25  * if not, require that we can walk up to exp->ex_dentry
26  * doing some checks on the 'x' bits
27  */
28 static int nfsd_acceptable(void *expv, struct dentry *dentry)
29 {
30 	struct svc_export *exp = expv;
31 	int rv;
32 	struct dentry *tdentry;
33 	struct dentry *parent;
34 
35 	if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
36 		return 1;
37 
38 	tdentry = dget(dentry);
39 	while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
40 		/* make sure parents give x permission to user */
41 		int err;
42 		parent = dget_parent(tdentry);
43 		err = inode_permission(d_inode(parent), MAY_EXEC);
44 		if (err < 0) {
45 			dput(parent);
46 			break;
47 		}
48 		dput(tdentry);
49 		tdentry = parent;
50 	}
51 	if (tdentry != exp->ex_path.dentry)
52 		dprintk("nfsd_acceptable failed at %p %pd\n", tdentry, tdentry);
53 	rv = (tdentry == exp->ex_path.dentry);
54 	dput(tdentry);
55 	return rv;
56 }
57 
58 /* Type check. The correct error return for type mismatches does not seem to be
59  * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
60  * comment in the NFSv3 spec says this is incorrect (implementation notes for
61  * the write call).
62  */
63 static inline __be32
64 nfsd_mode_check(struct svc_rqst *rqstp, struct dentry *dentry,
65 		umode_t requested)
66 {
67 	umode_t mode = d_inode(dentry)->i_mode & S_IFMT;
68 
69 	if (requested == 0) /* the caller doesn't care */
70 		return nfs_ok;
71 	if (mode == requested) {
72 		if (mode == S_IFDIR && !d_can_lookup(dentry)) {
73 			WARN_ON_ONCE(1);
74 			return nfserr_notdir;
75 		}
76 		return nfs_ok;
77 	}
78 	/*
79 	 * v4 has an error more specific than err_notdir which we should
80 	 * return in preference to err_notdir:
81 	 */
82 	if (rqstp->rq_vers == 4 && mode == S_IFLNK)
83 		return nfserr_symlink;
84 	if (requested == S_IFDIR)
85 		return nfserr_notdir;
86 	if (mode == S_IFDIR)
87 		return nfserr_isdir;
88 	return nfserr_inval;
89 }
90 
91 static bool nfsd_originating_port_ok(struct svc_rqst *rqstp, int flags)
92 {
93 	if (flags & NFSEXP_INSECURE_PORT)
94 		return true;
95 	/* We don't require gss requests to use low ports: */
96 	if (rqstp->rq_cred.cr_flavor >= RPC_AUTH_GSS)
97 		return true;
98 	return test_bit(RQ_SECURE, &rqstp->rq_flags);
99 }
100 
101 static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
102 					  struct svc_export *exp)
103 {
104 	int flags = nfsexp_flags(rqstp, exp);
105 
106 	/* Check if the request originated from a secure port. */
107 	if (!nfsd_originating_port_ok(rqstp, flags)) {
108 		RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
109 		dprintk("nfsd: request from insecure port %s!\n",
110 		        svc_print_addr(rqstp, buf, sizeof(buf)));
111 		return nfserr_perm;
112 	}
113 
114 	/* Set user creds for this exportpoint */
115 	return nfserrno(nfsd_setuser(rqstp, exp));
116 }
117 
118 static inline __be32 check_pseudo_root(struct svc_rqst *rqstp,
119 	struct dentry *dentry, struct svc_export *exp)
120 {
121 	if (!(exp->ex_flags & NFSEXP_V4ROOT))
122 		return nfs_ok;
123 	/*
124 	 * v2/v3 clients have no need for the V4ROOT export--they use
125 	 * the mount protocl instead; also, further V4ROOT checks may be
126 	 * in v4-specific code, in which case v2/v3 clients could bypass
127 	 * them.
128 	 */
129 	if (!nfsd_v4client(rqstp))
130 		return nfserr_stale;
131 	/*
132 	 * We're exposing only the directories and symlinks that have to be
133 	 * traversed on the way to real exports:
134 	 */
135 	if (unlikely(!d_is_dir(dentry) &&
136 		     !d_is_symlink(dentry)))
137 		return nfserr_stale;
138 	/*
139 	 * A pseudoroot export gives permission to access only one
140 	 * single directory; the kernel has to make another upcall
141 	 * before granting access to anything else under it:
142 	 */
143 	if (unlikely(dentry != exp->ex_path.dentry))
144 		return nfserr_stale;
145 	return nfs_ok;
146 }
147 
148 /*
149  * Use the given filehandle to look up the corresponding export and
150  * dentry.  On success, the results are used to set fh_export and
151  * fh_dentry.
152  */
153 static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
154 {
155 	struct knfsd_fh	*fh = &fhp->fh_handle;
156 	struct fid *fid = NULL, sfid;
157 	struct svc_export *exp;
158 	struct dentry *dentry;
159 	int fileid_type;
160 	int data_left = fh->fh_size/4;
161 	__be32 error;
162 
163 	error = nfserr_stale;
164 	if (rqstp->rq_vers > 2)
165 		error = nfserr_badhandle;
166 	if (rqstp->rq_vers == 4 && fh->fh_size == 0)
167 		return nfserr_nofilehandle;
168 
169 	if (fh->fh_version == 1) {
170 		int len;
171 
172 		if (--data_left < 0)
173 			return error;
174 		if (fh->fh_auth_type != 0)
175 			return error;
176 		len = key_len(fh->fh_fsid_type) / 4;
177 		if (len == 0)
178 			return error;
179 		if  (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
180 			/* deprecated, convert to type 3 */
181 			len = key_len(FSID_ENCODE_DEV)/4;
182 			fh->fh_fsid_type = FSID_ENCODE_DEV;
183 			/*
184 			 * struct knfsd_fh uses host-endian fields, which are
185 			 * sometimes used to hold net-endian values. This
186 			 * confuses sparse, so we must use __force here to
187 			 * keep it from complaining.
188 			 */
189 			fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl((__force __be32)fh->fh_fsid[0]),
190 							ntohl((__force __be32)fh->fh_fsid[1])));
191 			fh->fh_fsid[1] = fh->fh_fsid[2];
192 		}
193 		data_left -= len;
194 		if (data_left < 0)
195 			return error;
196 		exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_fsid);
197 		fid = (struct fid *)(fh->fh_fsid + len);
198 	} else {
199 		__u32 tfh[2];
200 		dev_t xdev;
201 		ino_t xino;
202 
203 		if (fh->fh_size != NFS_FHSIZE)
204 			return error;
205 		/* assume old filehandle format */
206 		xdev = old_decode_dev(fh->ofh_xdev);
207 		xino = u32_to_ino_t(fh->ofh_xino);
208 		mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
209 		exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
210 	}
211 
212 	error = nfserr_stale;
213 	if (IS_ERR(exp)) {
214 		trace_nfsd_set_fh_dentry_badexport(rqstp, fhp, PTR_ERR(exp));
215 
216 		if (PTR_ERR(exp) == -ENOENT)
217 			return error;
218 
219 		return nfserrno(PTR_ERR(exp));
220 	}
221 
222 	if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
223 		/* Elevate privileges so that the lack of 'r' or 'x'
224 		 * permission on some parent directory will
225 		 * not stop exportfs_decode_fh from being able
226 		 * to reconnect a directory into the dentry cache.
227 		 * The same problem can affect "SUBTREECHECK" exports,
228 		 * but as nfsd_acceptable depends on correct
229 		 * access control settings being in effect, we cannot
230 		 * fix that case easily.
231 		 */
232 		struct cred *new = prepare_creds();
233 		if (!new) {
234 			error =  nfserrno(-ENOMEM);
235 			goto out;
236 		}
237 		new->cap_effective =
238 			cap_raise_nfsd_set(new->cap_effective,
239 					   new->cap_permitted);
240 		put_cred(override_creds(new));
241 		put_cred(new);
242 	} else {
243 		error = nfsd_setuser_and_check_port(rqstp, exp);
244 		if (error)
245 			goto out;
246 	}
247 
248 	/*
249 	 * Look up the dentry using the NFS file handle.
250 	 */
251 	error = nfserr_stale;
252 	if (rqstp->rq_vers > 2)
253 		error = nfserr_badhandle;
254 
255 	if (fh->fh_version != 1) {
256 		sfid.i32.ino = fh->ofh_ino;
257 		sfid.i32.gen = fh->ofh_generation;
258 		sfid.i32.parent_ino = fh->ofh_dirino;
259 		fid = &sfid;
260 		data_left = 3;
261 		if (fh->ofh_dirino == 0)
262 			fileid_type = FILEID_INO32_GEN;
263 		else
264 			fileid_type = FILEID_INO32_GEN_PARENT;
265 	} else
266 		fileid_type = fh->fh_fileid_type;
267 
268 	if (fileid_type == FILEID_ROOT)
269 		dentry = dget(exp->ex_path.dentry);
270 	else {
271 		dentry = exportfs_decode_fh_raw(exp->ex_path.mnt, fid,
272 						data_left, fileid_type,
273 						nfsd_acceptable, exp);
274 		if (IS_ERR_OR_NULL(dentry)) {
275 			trace_nfsd_set_fh_dentry_badhandle(rqstp, fhp,
276 					dentry ?  PTR_ERR(dentry) : -ESTALE);
277 			switch (PTR_ERR(dentry)) {
278 			case -ENOMEM:
279 			case -ETIMEDOUT:
280 				break;
281 			default:
282 				dentry = ERR_PTR(-ESTALE);
283 			}
284 		}
285 	}
286 	if (dentry == NULL)
287 		goto out;
288 	if (IS_ERR(dentry)) {
289 		if (PTR_ERR(dentry) != -EINVAL)
290 			error = nfserrno(PTR_ERR(dentry));
291 		goto out;
292 	}
293 
294 	if (d_is_dir(dentry) &&
295 			(dentry->d_flags & DCACHE_DISCONNECTED)) {
296 		printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %pd2\n",
297 				dentry);
298 	}
299 
300 	fhp->fh_dentry = dentry;
301 	fhp->fh_export = exp;
302 
303 	switch (rqstp->rq_vers) {
304 	case 4:
305 		if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOATOMIC_ATTR)
306 			fhp->fh_no_atomic_attr = true;
307 		break;
308 	case 3:
309 		if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOWCC)
310 			fhp->fh_no_wcc = true;
311 		break;
312 	case 2:
313 		fhp->fh_no_wcc = true;
314 	}
315 
316 	return 0;
317 out:
318 	exp_put(exp);
319 	return error;
320 }
321 
322 /**
323  * fh_verify - filehandle lookup and access checking
324  * @rqstp: pointer to current rpc request
325  * @fhp: filehandle to be verified
326  * @type: expected type of object pointed to by filehandle
327  * @access: type of access needed to object
328  *
329  * Look up a dentry from the on-the-wire filehandle, check the client's
330  * access to the export, and set the current task's credentials.
331  *
332  * Regardless of success or failure of fh_verify(), fh_put() should be
333  * called on @fhp when the caller is finished with the filehandle.
334  *
335  * fh_verify() may be called multiple times on a given filehandle, for
336  * example, when processing an NFSv4 compound.  The first call will look
337  * up a dentry using the on-the-wire filehandle.  Subsequent calls will
338  * skip the lookup and just perform the other checks and possibly change
339  * the current task's credentials.
340  *
341  * @type specifies the type of object expected using one of the S_IF*
342  * constants defined in include/linux/stat.h.  The caller may use zero
343  * to indicate that it doesn't care, or a negative integer to indicate
344  * that it expects something not of the given type.
345  *
346  * @access is formed from the NFSD_MAY_* constants defined in
347  * fs/nfsd/vfs.h.
348  */
349 __be32
350 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, int access)
351 {
352 	struct svc_export *exp;
353 	struct dentry	*dentry;
354 	__be32		error;
355 
356 	dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
357 
358 	if (!fhp->fh_dentry) {
359 		error = nfsd_set_fh_dentry(rqstp, fhp);
360 		if (error)
361 			goto out;
362 	}
363 	dentry = fhp->fh_dentry;
364 	exp = fhp->fh_export;
365 	/*
366 	 * We still have to do all these permission checks, even when
367 	 * fh_dentry is already set:
368 	 * 	- fh_verify may be called multiple times with different
369 	 * 	  "access" arguments (e.g. nfsd_proc_create calls
370 	 * 	  fh_verify(...,NFSD_MAY_EXEC) first, then later (in
371 	 * 	  nfsd_create) calls fh_verify(...,NFSD_MAY_CREATE).
372 	 *	- in the NFSv4 case, the filehandle may have been filled
373 	 *	  in by fh_compose, and given a dentry, but further
374 	 *	  compound operations performed with that filehandle
375 	 *	  still need permissions checks.  In the worst case, a
376 	 *	  mountpoint crossing may have changed the export
377 	 *	  options, and we may now need to use a different uid
378 	 *	  (for example, if different id-squashing options are in
379 	 *	  effect on the new filesystem).
380 	 */
381 	error = check_pseudo_root(rqstp, dentry, exp);
382 	if (error)
383 		goto out;
384 
385 	error = nfsd_setuser_and_check_port(rqstp, exp);
386 	if (error)
387 		goto out;
388 
389 	error = nfsd_mode_check(rqstp, dentry, type);
390 	if (error)
391 		goto out;
392 
393 	/*
394 	 * pseudoflavor restrictions are not enforced on NLM,
395 	 * which clients virtually always use auth_sys for,
396 	 * even while using RPCSEC_GSS for NFS.
397 	 */
398 	if (access & NFSD_MAY_LOCK || access & NFSD_MAY_BYPASS_GSS)
399 		goto skip_pseudoflavor_check;
400 	/*
401 	 * Clients may expect to be able to use auth_sys during mount,
402 	 * even if they use gss for everything else; see section 2.3.2
403 	 * of rfc 2623.
404 	 */
405 	if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
406 			&& exp->ex_path.dentry == dentry)
407 		goto skip_pseudoflavor_check;
408 
409 	error = check_nfsd_access(exp, rqstp);
410 	if (error)
411 		goto out;
412 
413 skip_pseudoflavor_check:
414 	/* Finally, check access permissions. */
415 	error = nfsd_permission(rqstp, exp, dentry, access);
416 
417 	if (error) {
418 		dprintk("fh_verify: %pd2 permission failure, "
419 			"acc=%x, error=%d\n",
420 			dentry,
421 			access, ntohl(error));
422 	}
423 out:
424 	if (error == nfserr_stale)
425 		nfsdstats.fh_stale++;
426 	return error;
427 }
428 
429 
430 /*
431  * Compose a file handle for an NFS reply.
432  *
433  * Note that when first composed, the dentry may not yet have
434  * an inode.  In this case a call to fh_update should be made
435  * before the fh goes out on the wire ...
436  */
437 static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
438 		struct dentry *dentry)
439 {
440 	if (dentry != exp->ex_path.dentry) {
441 		struct fid *fid = (struct fid *)
442 			(fhp->fh_handle.fh_fsid + fhp->fh_handle.fh_size/4 - 1);
443 		int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
444 		int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
445 
446 		fhp->fh_handle.fh_fileid_type =
447 			exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
448 		fhp->fh_handle.fh_size += maxsize * 4;
449 	} else {
450 		fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
451 	}
452 }
453 
454 /*
455  * for composing old style file handles
456  */
457 static inline void _fh_update_old(struct dentry *dentry,
458 				  struct svc_export *exp,
459 				  struct knfsd_fh *fh)
460 {
461 	fh->ofh_ino = ino_t_to_u32(d_inode(dentry)->i_ino);
462 	fh->ofh_generation = d_inode(dentry)->i_generation;
463 	if (d_is_dir(dentry) ||
464 	    (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
465 		fh->ofh_dirino = 0;
466 }
467 
468 static bool is_root_export(struct svc_export *exp)
469 {
470 	return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root;
471 }
472 
473 static struct super_block *exp_sb(struct svc_export *exp)
474 {
475 	return exp->ex_path.dentry->d_sb;
476 }
477 
478 static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)
479 {
480 	switch (fsid_type) {
481 	case FSID_DEV:
482 		if (!old_valid_dev(exp_sb(exp)->s_dev))
483 			return false;
484 		fallthrough;
485 	case FSID_MAJOR_MINOR:
486 	case FSID_ENCODE_DEV:
487 		return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV;
488 	case FSID_NUM:
489 		return exp->ex_flags & NFSEXP_FSID;
490 	case FSID_UUID8:
491 	case FSID_UUID16:
492 		if (!is_root_export(exp))
493 			return false;
494 		fallthrough;
495 	case FSID_UUID4_INUM:
496 	case FSID_UUID16_INUM:
497 		return exp->ex_uuid != NULL;
498 	}
499 	return true;
500 }
501 
502 
503 static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh)
504 {
505 	u8 version;
506 	u8 fsid_type;
507 retry:
508 	version = 1;
509 	if (ref_fh && ref_fh->fh_export == exp) {
510 		version = ref_fh->fh_handle.fh_version;
511 		fsid_type = ref_fh->fh_handle.fh_fsid_type;
512 
513 		ref_fh = NULL;
514 
515 		switch (version) {
516 		case 0xca:
517 			fsid_type = FSID_DEV;
518 			break;
519 		case 1:
520 			break;
521 		default:
522 			goto retry;
523 		}
524 
525 		/*
526 		 * As the fsid -> filesystem mapping was guided by
527 		 * user-space, there is no guarantee that the filesystem
528 		 * actually supports that fsid type. If it doesn't we
529 		 * loop around again without ref_fh set.
530 		 */
531 		if (!fsid_type_ok_for_exp(fsid_type, exp))
532 			goto retry;
533 	} else if (exp->ex_flags & NFSEXP_FSID) {
534 		fsid_type = FSID_NUM;
535 	} else if (exp->ex_uuid) {
536 		if (fhp->fh_maxsize >= 64) {
537 			if (is_root_export(exp))
538 				fsid_type = FSID_UUID16;
539 			else
540 				fsid_type = FSID_UUID16_INUM;
541 		} else {
542 			if (is_root_export(exp))
543 				fsid_type = FSID_UUID8;
544 			else
545 				fsid_type = FSID_UUID4_INUM;
546 		}
547 	} else if (!old_valid_dev(exp_sb(exp)->s_dev))
548 		/* for newer device numbers, we must use a newer fsid format */
549 		fsid_type = FSID_ENCODE_DEV;
550 	else
551 		fsid_type = FSID_DEV;
552 	fhp->fh_handle.fh_version = version;
553 	if (version)
554 		fhp->fh_handle.fh_fsid_type = fsid_type;
555 }
556 
557 __be32
558 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
559 	   struct svc_fh *ref_fh)
560 {
561 	/* ref_fh is a reference file handle.
562 	 * if it is non-null and for the same filesystem, then we should compose
563 	 * a filehandle which is of the same version, where possible.
564 	 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
565 	 * Then create a 32byte filehandle using nfs_fhbase_old
566 	 *
567 	 */
568 
569 	struct inode * inode = d_inode(dentry);
570 	dev_t ex_dev = exp_sb(exp)->s_dev;
571 
572 	dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %pd2, ino=%ld)\n",
573 		MAJOR(ex_dev), MINOR(ex_dev),
574 		(long) d_inode(exp->ex_path.dentry)->i_ino,
575 		dentry,
576 		(inode ? inode->i_ino : 0));
577 
578 	/* Choose filehandle version and fsid type based on
579 	 * the reference filehandle (if it is in the same export)
580 	 * or the export options.
581 	 */
582 	set_version_and_fsid_type(fhp, exp, ref_fh);
583 
584 	/* If we have a ref_fh, then copy the fh_no_wcc setting from it. */
585 	fhp->fh_no_wcc = ref_fh ? ref_fh->fh_no_wcc : false;
586 
587 	if (ref_fh == fhp)
588 		fh_put(ref_fh);
589 
590 	if (fhp->fh_locked || fhp->fh_dentry) {
591 		printk(KERN_ERR "fh_compose: fh %pd2 not initialized!\n",
592 		       dentry);
593 	}
594 	if (fhp->fh_maxsize < NFS_FHSIZE)
595 		printk(KERN_ERR "fh_compose: called with maxsize %d! %pd2\n",
596 		       fhp->fh_maxsize,
597 		       dentry);
598 
599 	fhp->fh_dentry = dget(dentry); /* our internal copy */
600 	fhp->fh_export = exp_get(exp);
601 
602 	if (fhp->fh_handle.fh_version == 0xca) {
603 		/* old style filehandle please */
604 		memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
605 		fhp->fh_handle.fh_size = NFS_FHSIZE;
606 		fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
607 		fhp->fh_handle.ofh_dev =  old_encode_dev(ex_dev);
608 		fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
609 		fhp->fh_handle.ofh_xino =
610 			ino_t_to_u32(d_inode(exp->ex_path.dentry)->i_ino);
611 		fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
612 		if (inode)
613 			_fh_update_old(dentry, exp, &fhp->fh_handle);
614 	} else {
615 		fhp->fh_handle.fh_size =
616 			key_len(fhp->fh_handle.fh_fsid_type) + 4;
617 		fhp->fh_handle.fh_auth_type = 0;
618 
619 		mk_fsid(fhp->fh_handle.fh_fsid_type,
620 			fhp->fh_handle.fh_fsid,
621 			ex_dev,
622 			d_inode(exp->ex_path.dentry)->i_ino,
623 			exp->ex_fsid, exp->ex_uuid);
624 
625 		if (inode)
626 			_fh_update(fhp, exp, dentry);
627 		if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID) {
628 			fh_put(fhp);
629 			return nfserr_opnotsupp;
630 		}
631 	}
632 
633 	return 0;
634 }
635 
636 /*
637  * Update file handle information after changing a dentry.
638  * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
639  */
640 __be32
641 fh_update(struct svc_fh *fhp)
642 {
643 	struct dentry *dentry;
644 
645 	if (!fhp->fh_dentry)
646 		goto out_bad;
647 
648 	dentry = fhp->fh_dentry;
649 	if (d_really_is_negative(dentry))
650 		goto out_negative;
651 	if (fhp->fh_handle.fh_version != 1) {
652 		_fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
653 	} else {
654 		if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
655 			return 0;
656 
657 		_fh_update(fhp, fhp->fh_export, dentry);
658 		if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID)
659 			return nfserr_opnotsupp;
660 	}
661 	return 0;
662 out_bad:
663 	printk(KERN_ERR "fh_update: fh not verified!\n");
664 	return nfserr_serverfault;
665 out_negative:
666 	printk(KERN_ERR "fh_update: %pd2 still negative!\n",
667 		dentry);
668 	return nfserr_serverfault;
669 }
670 
671 /*
672  * Release a file handle.
673  */
674 void
675 fh_put(struct svc_fh *fhp)
676 {
677 	struct dentry * dentry = fhp->fh_dentry;
678 	struct svc_export * exp = fhp->fh_export;
679 	if (dentry) {
680 		fh_unlock(fhp);
681 		fhp->fh_dentry = NULL;
682 		dput(dentry);
683 		fh_clear_wcc(fhp);
684 	}
685 	fh_drop_write(fhp);
686 	if (exp) {
687 		exp_put(exp);
688 		fhp->fh_export = NULL;
689 	}
690 	fhp->fh_no_wcc = false;
691 	return;
692 }
693 
694 /*
695  * Shorthand for dprintk()'s
696  */
697 char * SVCFH_fmt(struct svc_fh *fhp)
698 {
699 	struct knfsd_fh *fh = &fhp->fh_handle;
700 
701 	static char buf[80];
702 	sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
703 		fh->fh_size,
704 		fh->fh_base.fh_pad[0],
705 		fh->fh_base.fh_pad[1],
706 		fh->fh_base.fh_pad[2],
707 		fh->fh_base.fh_pad[3],
708 		fh->fh_base.fh_pad[4],
709 		fh->fh_base.fh_pad[5]);
710 	return buf;
711 }
712 
713 enum fsid_source fsid_source(struct svc_fh *fhp)
714 {
715 	if (fhp->fh_handle.fh_version != 1)
716 		return FSIDSOURCE_DEV;
717 	switch(fhp->fh_handle.fh_fsid_type) {
718 	case FSID_DEV:
719 	case FSID_ENCODE_DEV:
720 	case FSID_MAJOR_MINOR:
721 		if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV)
722 			return FSIDSOURCE_DEV;
723 		break;
724 	case FSID_NUM:
725 		if (fhp->fh_export->ex_flags & NFSEXP_FSID)
726 			return FSIDSOURCE_FSID;
727 		break;
728 	default:
729 		break;
730 	}
731 	/* either a UUID type filehandle, or the filehandle doesn't
732 	 * match the export.
733 	 */
734 	if (fhp->fh_export->ex_flags & NFSEXP_FSID)
735 		return FSIDSOURCE_FSID;
736 	if (fhp->fh_export->ex_uuid)
737 		return FSIDSOURCE_UUID;
738 	return FSIDSOURCE_DEV;
739 }
740