xref: /linux/fs/nfsd/nfsfh.c (revision 1e123fd73deb16cb362ecefb55c90c9196f4a6c2)
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(&nop_mnt_idmap,
44 				       d_inode(parent), MAY_EXEC);
45 		if (err < 0) {
46 			dput(parent);
47 			break;
48 		}
49 		dput(tdentry);
50 		tdentry = parent;
51 	}
52 	if (tdentry != exp->ex_path.dentry)
53 		dprintk("nfsd_acceptable failed at %p %pd\n", tdentry, tdentry);
54 	rv = (tdentry == exp->ex_path.dentry);
55 	dput(tdentry);
56 	return rv;
57 }
58 
59 /* Type check. The correct error return for type mismatches does not seem to be
60  * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
61  * comment in the NFSv3 spec says this is incorrect (implementation notes for
62  * the write call).
63  */
64 static inline __be32
65 nfsd_mode_check(struct dentry *dentry, 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 	if (mode == S_IFLNK) {
79 		if (requested == S_IFDIR)
80 			return nfserr_symlink_not_dir;
81 		return nfserr_symlink;
82 	}
83 	if (requested == S_IFDIR)
84 		return nfserr_notdir;
85 	if (mode == S_IFDIR)
86 		return nfserr_isdir;
87 	return nfserr_wrong_type;
88 }
89 
90 static bool nfsd_originating_port_ok(struct svc_rqst *rqstp, int flags)
91 {
92 	if (flags & NFSEXP_INSECURE_PORT)
93 		return true;
94 	/* We don't require gss requests to use low ports: */
95 	if (rqstp->rq_cred.cr_flavor >= RPC_AUTH_GSS)
96 		return true;
97 	return test_bit(RQ_SECURE, &rqstp->rq_flags);
98 }
99 
100 static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
101 					  struct svc_export *exp)
102 {
103 	int flags = nfsexp_flags(&rqstp->rq_cred, exp);
104 
105 	/* Check if the request originated from a secure port. */
106 	if (!nfsd_originating_port_ok(rqstp, flags)) {
107 		RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
108 		dprintk("nfsd: request from insecure port %s!\n",
109 		        svc_print_addr(rqstp, buf, sizeof(buf)));
110 		return nfserr_perm;
111 	}
112 
113 	/* Set user creds for this exportpoint */
114 	return nfserrno(nfsd_setuser(&rqstp->rq_cred, exp));
115 }
116 
117 static inline __be32 check_pseudo_root(struct dentry *dentry,
118 				       struct svc_export *exp)
119 {
120 	if (!(exp->ex_flags & NFSEXP_V4ROOT))
121 		return nfs_ok;
122 	/*
123 	 * We're exposing only the directories and symlinks that have to be
124 	 * traversed on the way to real exports:
125 	 */
126 	if (unlikely(!d_is_dir(dentry) &&
127 		     !d_is_symlink(dentry)))
128 		return nfserr_stale;
129 	/*
130 	 * A pseudoroot export gives permission to access only one
131 	 * single directory; the kernel has to make another upcall
132 	 * before granting access to anything else under it:
133 	 */
134 	if (unlikely(dentry != exp->ex_path.dentry))
135 		return nfserr_stale;
136 	return nfs_ok;
137 }
138 
139 /*
140  * Use the given filehandle to look up the corresponding export and
141  * dentry.  On success, the results are used to set fh_export and
142  * fh_dentry.
143  */
144 static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
145 {
146 	struct knfsd_fh	*fh = &fhp->fh_handle;
147 	struct fid *fid = NULL;
148 	struct svc_export *exp;
149 	struct dentry *dentry;
150 	int fileid_type;
151 	int data_left = fh->fh_size/4;
152 	int len;
153 	__be32 error;
154 
155 	error = nfserr_badhandle;
156 	if (fh->fh_size == 0)
157 		return nfserr_nofilehandle;
158 
159 	if (fh->fh_version != 1)
160 		return error;
161 
162 	if (--data_left < 0)
163 		return error;
164 	if (fh->fh_auth_type != 0)
165 		return error;
166 	len = key_len(fh->fh_fsid_type) / 4;
167 	if (len == 0)
168 		return error;
169 	if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
170 		/* deprecated, convert to type 3 */
171 		len = key_len(FSID_ENCODE_DEV)/4;
172 		fh->fh_fsid_type = FSID_ENCODE_DEV;
173 		/*
174 		 * struct knfsd_fh uses host-endian fields, which are
175 		 * sometimes used to hold net-endian values. This
176 		 * confuses sparse, so we must use __force here to
177 		 * keep it from complaining.
178 		 */
179 		fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl((__force __be32)fh->fh_fsid[0]),
180 						      ntohl((__force __be32)fh->fh_fsid[1])));
181 		fh->fh_fsid[1] = fh->fh_fsid[2];
182 	}
183 	data_left -= len;
184 	if (data_left < 0)
185 		return error;
186 	exp = rqst_exp_find(&rqstp->rq_chandle, SVC_NET(rqstp),
187 			    rqstp->rq_client, rqstp->rq_gssclient,
188 			    fh->fh_fsid_type, fh->fh_fsid);
189 	fid = (struct fid *)(fh->fh_fsid + len);
190 
191 	error = nfserr_stale;
192 	if (IS_ERR(exp)) {
193 		trace_nfsd_set_fh_dentry_badexport(rqstp, fhp, PTR_ERR(exp));
194 
195 		if (PTR_ERR(exp) == -ENOENT)
196 			return error;
197 
198 		return nfserrno(PTR_ERR(exp));
199 	}
200 
201 	if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
202 		/* Elevate privileges so that the lack of 'r' or 'x'
203 		 * permission on some parent directory will
204 		 * not stop exportfs_decode_fh from being able
205 		 * to reconnect a directory into the dentry cache.
206 		 * The same problem can affect "SUBTREECHECK" exports,
207 		 * but as nfsd_acceptable depends on correct
208 		 * access control settings being in effect, we cannot
209 		 * fix that case easily.
210 		 */
211 		struct cred *new = prepare_creds();
212 		if (!new) {
213 			error =  nfserrno(-ENOMEM);
214 			goto out;
215 		}
216 		new->cap_effective =
217 			cap_raise_nfsd_set(new->cap_effective,
218 					   new->cap_permitted);
219 		put_cred(override_creds(new));
220 		put_cred(new);
221 	} else {
222 		error = nfsd_setuser_and_check_port(rqstp, exp);
223 		if (error)
224 			goto out;
225 	}
226 
227 	/*
228 	 * Look up the dentry using the NFS file handle.
229 	 */
230 	error = nfserr_badhandle;
231 
232 	fileid_type = fh->fh_fileid_type;
233 
234 	if (fileid_type == FILEID_ROOT)
235 		dentry = dget(exp->ex_path.dentry);
236 	else {
237 		dentry = exportfs_decode_fh_raw(exp->ex_path.mnt, fid,
238 						data_left, fileid_type, 0,
239 						nfsd_acceptable, exp);
240 		if (IS_ERR_OR_NULL(dentry)) {
241 			trace_nfsd_set_fh_dentry_badhandle(rqstp, fhp,
242 					dentry ?  PTR_ERR(dentry) : -ESTALE);
243 			switch (PTR_ERR(dentry)) {
244 			case -ENOMEM:
245 			case -ETIMEDOUT:
246 				break;
247 			default:
248 				dentry = ERR_PTR(-ESTALE);
249 			}
250 		}
251 	}
252 	if (dentry == NULL)
253 		goto out;
254 	if (IS_ERR(dentry)) {
255 		if (PTR_ERR(dentry) != -EINVAL)
256 			error = nfserrno(PTR_ERR(dentry));
257 		goto out;
258 	}
259 
260 	if (d_is_dir(dentry) &&
261 			(dentry->d_flags & DCACHE_DISCONNECTED)) {
262 		printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %pd2\n",
263 				dentry);
264 	}
265 
266 	fhp->fh_dentry = dentry;
267 	fhp->fh_export = exp;
268 
269 	switch (rqstp->rq_vers) {
270 	case 4:
271 		if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOATOMIC_ATTR)
272 			fhp->fh_no_atomic_attr = true;
273 		fhp->fh_64bit_cookies = true;
274 		break;
275 	case 3:
276 		if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOWCC)
277 			fhp->fh_no_wcc = true;
278 		fhp->fh_64bit_cookies = true;
279 		if (exp->ex_flags & NFSEXP_V4ROOT)
280 			goto out;
281 		break;
282 	case 2:
283 		fhp->fh_no_wcc = true;
284 		if (EX_WGATHER(exp))
285 			fhp->fh_use_wgather = true;
286 		if (exp->ex_flags & NFSEXP_V4ROOT)
287 			goto out;
288 	}
289 
290 	return 0;
291 out:
292 	exp_put(exp);
293 	return error;
294 }
295 
296 /**
297  * fh_verify - filehandle lookup and access checking
298  * @rqstp: pointer to current rpc request
299  * @fhp: filehandle to be verified
300  * @type: expected type of object pointed to by filehandle
301  * @access: type of access needed to object
302  *
303  * Look up a dentry from the on-the-wire filehandle, check the client's
304  * access to the export, and set the current task's credentials.
305  *
306  * Regardless of success or failure of fh_verify(), fh_put() should be
307  * called on @fhp when the caller is finished with the filehandle.
308  *
309  * fh_verify() may be called multiple times on a given filehandle, for
310  * example, when processing an NFSv4 compound.  The first call will look
311  * up a dentry using the on-the-wire filehandle.  Subsequent calls will
312  * skip the lookup and just perform the other checks and possibly change
313  * the current task's credentials.
314  *
315  * @type specifies the type of object expected using one of the S_IF*
316  * constants defined in include/linux/stat.h.  The caller may use zero
317  * to indicate that it doesn't care, or a negative integer to indicate
318  * that it expects something not of the given type.
319  *
320  * @access is formed from the NFSD_MAY_* constants defined in
321  * fs/nfsd/vfs.h.
322  */
323 __be32
324 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, int access)
325 {
326 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
327 	struct svc_export *exp = NULL;
328 	struct dentry	*dentry;
329 	__be32		error;
330 
331 	if (!fhp->fh_dentry) {
332 		error = nfsd_set_fh_dentry(rqstp, fhp);
333 		if (error)
334 			goto out;
335 	}
336 	dentry = fhp->fh_dentry;
337 	exp = fhp->fh_export;
338 
339 	trace_nfsd_fh_verify(rqstp, fhp, type, access);
340 
341 	/*
342 	 * We still have to do all these permission checks, even when
343 	 * fh_dentry is already set:
344 	 * 	- fh_verify may be called multiple times with different
345 	 * 	  "access" arguments (e.g. nfsd_proc_create calls
346 	 * 	  fh_verify(...,NFSD_MAY_EXEC) first, then later (in
347 	 * 	  nfsd_create) calls fh_verify(...,NFSD_MAY_CREATE).
348 	 *	- in the NFSv4 case, the filehandle may have been filled
349 	 *	  in by fh_compose, and given a dentry, but further
350 	 *	  compound operations performed with that filehandle
351 	 *	  still need permissions checks.  In the worst case, a
352 	 *	  mountpoint crossing may have changed the export
353 	 *	  options, and we may now need to use a different uid
354 	 *	  (for example, if different id-squashing options are in
355 	 *	  effect on the new filesystem).
356 	 */
357 	error = check_pseudo_root(dentry, exp);
358 	if (error)
359 		goto out;
360 
361 	error = nfsd_setuser_and_check_port(rqstp, exp);
362 	if (error)
363 		goto out;
364 
365 	error = nfsd_mode_check(dentry, type);
366 	if (error)
367 		goto out;
368 
369 	/*
370 	 * pseudoflavor restrictions are not enforced on NLM,
371 	 * which clients virtually always use auth_sys for,
372 	 * even while using RPCSEC_GSS for NFS.
373 	 */
374 	if (access & NFSD_MAY_LOCK || access & NFSD_MAY_BYPASS_GSS)
375 		goto skip_pseudoflavor_check;
376 	/*
377 	 * Clients may expect to be able to use auth_sys during mount,
378 	 * even if they use gss for everything else; see section 2.3.2
379 	 * of rfc 2623.
380 	 */
381 	if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
382 			&& exp->ex_path.dentry == dentry)
383 		goto skip_pseudoflavor_check;
384 
385 	error = check_nfsd_access(exp, rqstp);
386 	if (error)
387 		goto out;
388 
389 skip_pseudoflavor_check:
390 	/* Finally, check access permissions. */
391 	error = nfsd_permission(&rqstp->rq_cred, exp, dentry, access);
392 out:
393 	trace_nfsd_fh_verify_err(rqstp, fhp, type, access, error);
394 	if (error == nfserr_stale)
395 		nfsd_stats_fh_stale_inc(nn, exp);
396 	return error;
397 }
398 
399 
400 /*
401  * Compose a file handle for an NFS reply.
402  *
403  * Note that when first composed, the dentry may not yet have
404  * an inode.  In this case a call to fh_update should be made
405  * before the fh goes out on the wire ...
406  */
407 static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
408 		struct dentry *dentry)
409 {
410 	if (dentry != exp->ex_path.dentry) {
411 		struct fid *fid = (struct fid *)
412 			(fhp->fh_handle.fh_fsid + fhp->fh_handle.fh_size/4 - 1);
413 		int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
414 		int fh_flags = (exp->ex_flags & NFSEXP_NOSUBTREECHECK) ? 0 :
415 				EXPORT_FH_CONNECTABLE;
416 		int fileid_type =
417 			exportfs_encode_fh(dentry, fid, &maxsize, fh_flags);
418 
419 		fhp->fh_handle.fh_fileid_type =
420 			fileid_type > 0 ? fileid_type : FILEID_INVALID;
421 		fhp->fh_handle.fh_size += maxsize * 4;
422 	} else {
423 		fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
424 	}
425 }
426 
427 static bool is_root_export(struct svc_export *exp)
428 {
429 	return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root;
430 }
431 
432 static struct super_block *exp_sb(struct svc_export *exp)
433 {
434 	return exp->ex_path.dentry->d_sb;
435 }
436 
437 static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)
438 {
439 	switch (fsid_type) {
440 	case FSID_DEV:
441 		if (!old_valid_dev(exp_sb(exp)->s_dev))
442 			return false;
443 		fallthrough;
444 	case FSID_MAJOR_MINOR:
445 	case FSID_ENCODE_DEV:
446 		return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV;
447 	case FSID_NUM:
448 		return exp->ex_flags & NFSEXP_FSID;
449 	case FSID_UUID8:
450 	case FSID_UUID16:
451 		if (!is_root_export(exp))
452 			return false;
453 		fallthrough;
454 	case FSID_UUID4_INUM:
455 	case FSID_UUID16_INUM:
456 		return exp->ex_uuid != NULL;
457 	}
458 	return true;
459 }
460 
461 
462 static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh)
463 {
464 	u8 version;
465 	u8 fsid_type;
466 retry:
467 	version = 1;
468 	if (ref_fh && ref_fh->fh_export == exp) {
469 		version = ref_fh->fh_handle.fh_version;
470 		fsid_type = ref_fh->fh_handle.fh_fsid_type;
471 
472 		ref_fh = NULL;
473 
474 		switch (version) {
475 		case 0xca:
476 			fsid_type = FSID_DEV;
477 			break;
478 		case 1:
479 			break;
480 		default:
481 			goto retry;
482 		}
483 
484 		/*
485 		 * As the fsid -> filesystem mapping was guided by
486 		 * user-space, there is no guarantee that the filesystem
487 		 * actually supports that fsid type. If it doesn't we
488 		 * loop around again without ref_fh set.
489 		 */
490 		if (!fsid_type_ok_for_exp(fsid_type, exp))
491 			goto retry;
492 	} else if (exp->ex_flags & NFSEXP_FSID) {
493 		fsid_type = FSID_NUM;
494 	} else if (exp->ex_uuid) {
495 		if (fhp->fh_maxsize >= 64) {
496 			if (is_root_export(exp))
497 				fsid_type = FSID_UUID16;
498 			else
499 				fsid_type = FSID_UUID16_INUM;
500 		} else {
501 			if (is_root_export(exp))
502 				fsid_type = FSID_UUID8;
503 			else
504 				fsid_type = FSID_UUID4_INUM;
505 		}
506 	} else if (!old_valid_dev(exp_sb(exp)->s_dev))
507 		/* for newer device numbers, we must use a newer fsid format */
508 		fsid_type = FSID_ENCODE_DEV;
509 	else
510 		fsid_type = FSID_DEV;
511 	fhp->fh_handle.fh_version = version;
512 	if (version)
513 		fhp->fh_handle.fh_fsid_type = fsid_type;
514 }
515 
516 __be32
517 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
518 	   struct svc_fh *ref_fh)
519 {
520 	/* ref_fh is a reference file handle.
521 	 * if it is non-null and for the same filesystem, then we should compose
522 	 * a filehandle which is of the same version, where possible.
523 	 */
524 
525 	struct inode * inode = d_inode(dentry);
526 	dev_t ex_dev = exp_sb(exp)->s_dev;
527 
528 	dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %pd2, ino=%ld)\n",
529 		MAJOR(ex_dev), MINOR(ex_dev),
530 		(long) d_inode(exp->ex_path.dentry)->i_ino,
531 		dentry,
532 		(inode ? inode->i_ino : 0));
533 
534 	/* Choose filehandle version and fsid type based on
535 	 * the reference filehandle (if it is in the same export)
536 	 * or the export options.
537 	 */
538 	set_version_and_fsid_type(fhp, exp, ref_fh);
539 
540 	/* If we have a ref_fh, then copy the fh_no_wcc setting from it. */
541 	fhp->fh_no_wcc = ref_fh ? ref_fh->fh_no_wcc : false;
542 
543 	if (ref_fh == fhp)
544 		fh_put(ref_fh);
545 
546 	if (fhp->fh_dentry) {
547 		printk(KERN_ERR "fh_compose: fh %pd2 not initialized!\n",
548 		       dentry);
549 	}
550 	if (fhp->fh_maxsize < NFS_FHSIZE)
551 		printk(KERN_ERR "fh_compose: called with maxsize %d! %pd2\n",
552 		       fhp->fh_maxsize,
553 		       dentry);
554 
555 	fhp->fh_dentry = dget(dentry); /* our internal copy */
556 	fhp->fh_export = exp_get(exp);
557 
558 	fhp->fh_handle.fh_size =
559 		key_len(fhp->fh_handle.fh_fsid_type) + 4;
560 	fhp->fh_handle.fh_auth_type = 0;
561 
562 	mk_fsid(fhp->fh_handle.fh_fsid_type,
563 		fhp->fh_handle.fh_fsid,
564 		ex_dev,
565 		d_inode(exp->ex_path.dentry)->i_ino,
566 		exp->ex_fsid, exp->ex_uuid);
567 
568 	if (inode)
569 		_fh_update(fhp, exp, dentry);
570 	if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID) {
571 		fh_put(fhp);
572 		return nfserr_stale;
573 	}
574 
575 	return 0;
576 }
577 
578 /*
579  * Update file handle information after changing a dentry.
580  * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
581  */
582 __be32
583 fh_update(struct svc_fh *fhp)
584 {
585 	struct dentry *dentry;
586 
587 	if (!fhp->fh_dentry)
588 		goto out_bad;
589 
590 	dentry = fhp->fh_dentry;
591 	if (d_really_is_negative(dentry))
592 		goto out_negative;
593 	if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
594 		return 0;
595 
596 	_fh_update(fhp, fhp->fh_export, dentry);
597 	if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID)
598 		return nfserr_stale;
599 	return 0;
600 out_bad:
601 	printk(KERN_ERR "fh_update: fh not verified!\n");
602 	return nfserr_serverfault;
603 out_negative:
604 	printk(KERN_ERR "fh_update: %pd2 still negative!\n",
605 		dentry);
606 	return nfserr_serverfault;
607 }
608 
609 /**
610  * fh_fill_pre_attrs - Fill in pre-op attributes
611  * @fhp: file handle to be updated
612  *
613  */
614 __be32 __must_check fh_fill_pre_attrs(struct svc_fh *fhp)
615 {
616 	bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
617 	struct inode *inode;
618 	struct kstat stat;
619 	__be32 err;
620 
621 	if (fhp->fh_no_wcc || fhp->fh_pre_saved)
622 		return nfs_ok;
623 
624 	inode = d_inode(fhp->fh_dentry);
625 	err = fh_getattr(fhp, &stat);
626 	if (err)
627 		return err;
628 
629 	if (v4)
630 		fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
631 
632 	fhp->fh_pre_mtime = stat.mtime;
633 	fhp->fh_pre_ctime = stat.ctime;
634 	fhp->fh_pre_size  = stat.size;
635 	fhp->fh_pre_saved = true;
636 	return nfs_ok;
637 }
638 
639 /**
640  * fh_fill_post_attrs - Fill in post-op attributes
641  * @fhp: file handle to be updated
642  *
643  */
644 __be32 fh_fill_post_attrs(struct svc_fh *fhp)
645 {
646 	bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
647 	struct inode *inode = d_inode(fhp->fh_dentry);
648 	__be32 err;
649 
650 	if (fhp->fh_no_wcc)
651 		return nfs_ok;
652 
653 	if (fhp->fh_post_saved)
654 		printk("nfsd: inode locked twice during operation.\n");
655 
656 	err = fh_getattr(fhp, &fhp->fh_post_attr);
657 	if (err)
658 		return err;
659 
660 	fhp->fh_post_saved = true;
661 	if (v4)
662 		fhp->fh_post_change =
663 			nfsd4_change_attribute(&fhp->fh_post_attr, inode);
664 	return nfs_ok;
665 }
666 
667 /**
668  * fh_fill_both_attrs - Fill pre-op and post-op attributes
669  * @fhp: file handle to be updated
670  *
671  * This is used when the directory wasn't changed, but wcc attributes
672  * are needed anyway.
673  */
674 __be32 __must_check fh_fill_both_attrs(struct svc_fh *fhp)
675 {
676 	__be32 err;
677 
678 	err = fh_fill_post_attrs(fhp);
679 	if (err)
680 		return err;
681 
682 	fhp->fh_pre_change = fhp->fh_post_change;
683 	fhp->fh_pre_mtime = fhp->fh_post_attr.mtime;
684 	fhp->fh_pre_ctime = fhp->fh_post_attr.ctime;
685 	fhp->fh_pre_size = fhp->fh_post_attr.size;
686 	fhp->fh_pre_saved = true;
687 	return nfs_ok;
688 }
689 
690 /*
691  * Release a file handle.
692  */
693 void
694 fh_put(struct svc_fh *fhp)
695 {
696 	struct dentry * dentry = fhp->fh_dentry;
697 	struct svc_export * exp = fhp->fh_export;
698 	if (dentry) {
699 		fhp->fh_dentry = NULL;
700 		dput(dentry);
701 		fh_clear_pre_post_attrs(fhp);
702 	}
703 	fh_drop_write(fhp);
704 	if (exp) {
705 		exp_put(exp);
706 		fhp->fh_export = NULL;
707 	}
708 	fhp->fh_no_wcc = false;
709 	return;
710 }
711 
712 /*
713  * Shorthand for dprintk()'s
714  */
715 char * SVCFH_fmt(struct svc_fh *fhp)
716 {
717 	struct knfsd_fh *fh = &fhp->fh_handle;
718 	static char buf[2+1+1+64*3+1];
719 
720 	if (fh->fh_size < 0 || fh->fh_size> 64)
721 		return "bad-fh";
722 	sprintf(buf, "%d: %*ph", fh->fh_size, fh->fh_size, fh->fh_raw);
723 	return buf;
724 }
725 
726 enum fsid_source fsid_source(const struct svc_fh *fhp)
727 {
728 	if (fhp->fh_handle.fh_version != 1)
729 		return FSIDSOURCE_DEV;
730 	switch(fhp->fh_handle.fh_fsid_type) {
731 	case FSID_DEV:
732 	case FSID_ENCODE_DEV:
733 	case FSID_MAJOR_MINOR:
734 		if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV)
735 			return FSIDSOURCE_DEV;
736 		break;
737 	case FSID_NUM:
738 		if (fhp->fh_export->ex_flags & NFSEXP_FSID)
739 			return FSIDSOURCE_FSID;
740 		break;
741 	default:
742 		break;
743 	}
744 	/* either a UUID type filehandle, or the filehandle doesn't
745 	 * match the export.
746 	 */
747 	if (fhp->fh_export->ex_flags & NFSEXP_FSID)
748 		return FSIDSOURCE_FSID;
749 	if (fhp->fh_export->ex_uuid)
750 		return FSIDSOURCE_UUID;
751 	return FSIDSOURCE_DEV;
752 }
753 
754 /*
755  * We could use i_version alone as the change attribute.  However, i_version
756  * can go backwards on a regular file after an unclean shutdown.  On its own
757  * that doesn't necessarily cause a problem, but if i_version goes backwards
758  * and then is incremented again it could reuse a value that was previously
759  * used before boot, and a client who queried the two values might incorrectly
760  * assume nothing changed.
761  *
762  * By using both ctime and the i_version counter we guarantee that as long as
763  * time doesn't go backwards we never reuse an old value. If the filesystem
764  * advertises STATX_ATTR_CHANGE_MONOTONIC, then this mitigation is not
765  * needed.
766  *
767  * We only need to do this for regular files as well. For directories, we
768  * assume that the new change attr is always logged to stable storage in some
769  * fashion before the results can be seen.
770  */
771 u64 nfsd4_change_attribute(const struct kstat *stat, const struct inode *inode)
772 {
773 	u64 chattr;
774 
775 	if (stat->result_mask & STATX_CHANGE_COOKIE) {
776 		chattr = stat->change_cookie;
777 		if (S_ISREG(inode->i_mode) &&
778 		    !(stat->attributes & STATX_ATTR_CHANGE_MONOTONIC)) {
779 			chattr += (u64)stat->ctime.tv_sec << 30;
780 			chattr += stat->ctime.tv_nsec;
781 		}
782 	} else {
783 		chattr = time_to_chattr(&stat->ctime);
784 	}
785 	return chattr;
786 }
787