xref: /linux/fs/nfsd/nfs4recover.c (revision 15ecd83dc06277385ad71dc7ea26911d9a79acaf)
1 /*
2 *  Copyright (c) 2004 The Regents of the University of Michigan.
3 *  Copyright (c) 2012 Jeff Layton <jlayton@redhat.com>
4 *  All rights reserved.
5 *
6 *  Andy Adamson <andros@citi.umich.edu>
7 *
8 *  Redistribution and use in source and binary forms, with or without
9 *  modification, are permitted provided that the following conditions
10 *  are met:
11 *
12 *  1. Redistributions of source code must retain the above copyright
13 *     notice, this list of conditions and the following disclaimer.
14 *  2. Redistributions in binary form must reproduce the above copyright
15 *     notice, this list of conditions and the following disclaimer in the
16 *     documentation and/or other materials provided with the distribution.
17 *  3. Neither the name of the University nor the names of its
18 *     contributors may be used to endorse or promote products derived
19 *     from this software without specific prior written permission.
20 *
21 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34 
35 #include <crypto/hash.h>
36 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/sched.h>
40 #include <linux/fs.h>
41 #include <linux/module.h>
42 #include <net/net_namespace.h>
43 #include <linux/sunrpc/rpc_pipe_fs.h>
44 #include <linux/sunrpc/clnt.h>
45 #include <linux/nfsd/cld.h>
46 
47 #include "nfsd.h"
48 #include "state.h"
49 #include "vfs.h"
50 #include "netns.h"
51 
52 #define NFSDDBG_FACILITY                NFSDDBG_PROC
53 
54 /* Declarations */
55 struct nfsd4_client_tracking_ops {
56 	int (*init)(struct net *);
57 	void (*exit)(struct net *);
58 	void (*create)(struct nfs4_client *);
59 	void (*remove)(struct nfs4_client *);
60 	int (*check)(struct nfs4_client *);
61 	void (*grace_done)(struct nfsd_net *);
62 	uint8_t version;
63 	size_t msglen;
64 };
65 
66 static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops;
67 static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v2;
68 
69 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
70 /* Globals */
71 static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
72 
73 static int
74 nfs4_save_creds(const struct cred **original_creds)
75 {
76 	struct cred *new;
77 
78 	new = prepare_creds();
79 	if (!new)
80 		return -ENOMEM;
81 
82 	new->fsuid = GLOBAL_ROOT_UID;
83 	new->fsgid = GLOBAL_ROOT_GID;
84 	*original_creds = override_creds(new);
85 	return 0;
86 }
87 
88 static void
89 nfs4_reset_creds(const struct cred *original)
90 {
91 	put_cred(revert_creds(original));
92 }
93 
94 static void
95 md5_to_hex(char *out, char *md5)
96 {
97 	int i;
98 
99 	for (i=0; i<16; i++) {
100 		unsigned char c = md5[i];
101 
102 		*out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
103 		*out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
104 	}
105 	*out = '\0';
106 }
107 
108 static int
109 nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
110 {
111 	struct xdr_netobj cksum;
112 	struct crypto_shash *tfm;
113 	int status;
114 
115 	dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
116 			clname->len, clname->data);
117 	tfm = crypto_alloc_shash("md5", 0, 0);
118 	if (IS_ERR(tfm)) {
119 		status = PTR_ERR(tfm);
120 		goto out_no_tfm;
121 	}
122 
123 	cksum.len = crypto_shash_digestsize(tfm);
124 	cksum.data = kmalloc(cksum.len, GFP_KERNEL);
125 	if (cksum.data == NULL) {
126 		status = -ENOMEM;
127  		goto out;
128 	}
129 
130 	status = crypto_shash_tfm_digest(tfm, clname->data, clname->len,
131 					 cksum.data);
132 	if (status)
133 		goto out;
134 
135 	md5_to_hex(dname, cksum.data);
136 
137 	status = 0;
138 out:
139 	kfree(cksum.data);
140 	crypto_free_shash(tfm);
141 out_no_tfm:
142 	return status;
143 }
144 
145 /*
146  * If we had an error generating the recdir name for the legacy tracker
147  * then warn the admin. If the error doesn't appear to be transient,
148  * then disable recovery tracking.
149  */
150 static void
151 legacy_recdir_name_error(struct nfs4_client *clp, int error)
152 {
153 	printk(KERN_ERR "NFSD: unable to generate recoverydir "
154 			"name (%d).\n", error);
155 
156 	/*
157 	 * if the algorithm just doesn't exist, then disable the recovery
158 	 * tracker altogether. The crypto libs will generally return this if
159 	 * FIPS is enabled as well.
160 	 */
161 	if (error == -ENOENT) {
162 		printk(KERN_ERR "NFSD: disabling legacy clientid tracking. "
163 			"Reboot recovery will not function correctly!\n");
164 		nfsd4_client_tracking_exit(clp->net);
165 	}
166 }
167 
168 static void
169 __nfsd4_create_reclaim_record_grace(struct nfs4_client *clp,
170 		const char *dname, int len, struct nfsd_net *nn)
171 {
172 	struct xdr_netobj name;
173 	struct xdr_netobj princhash = { .len = 0, .data = NULL };
174 	struct nfs4_client_reclaim *crp;
175 
176 	name.data = kmemdup(dname, len, GFP_KERNEL);
177 	if (!name.data) {
178 		dprintk("%s: failed to allocate memory for name.data!\n",
179 			__func__);
180 		return;
181 	}
182 	name.len = len;
183 	crp = nfs4_client_to_reclaim(name, princhash, nn);
184 	if (!crp) {
185 		kfree(name.data);
186 		return;
187 	}
188 	crp->cr_clp = clp;
189 }
190 
191 static void
192 nfsd4_create_clid_dir(struct nfs4_client *clp)
193 {
194 	const struct cred *original_cred;
195 	char dname[HEXDIR_LEN];
196 	struct dentry *dir, *dentry;
197 	int status;
198 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
199 
200 	if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
201 		return;
202 	if (!nn->rec_file)
203 		return;
204 
205 	status = nfs4_make_rec_clidname(dname, &clp->cl_name);
206 	if (status)
207 		return legacy_recdir_name_error(clp, status);
208 
209 	status = nfs4_save_creds(&original_cred);
210 	if (status < 0)
211 		return;
212 
213 	status = mnt_want_write_file(nn->rec_file);
214 	if (status)
215 		goto out_creds;
216 
217 	dir = nn->rec_file->f_path.dentry;
218 	/* lock the parent */
219 	inode_lock(d_inode(dir));
220 
221 	dentry = lookup_one(&nop_mnt_idmap, &QSTR(dname), dir);
222 	if (IS_ERR(dentry)) {
223 		status = PTR_ERR(dentry);
224 		goto out_unlock;
225 	}
226 	if (d_really_is_positive(dentry))
227 		/*
228 		 * In the 4.1 case, where we're called from
229 		 * reclaim_complete(), records from the previous reboot
230 		 * may still be left, so this is OK.
231 		 *
232 		 * In the 4.0 case, we should never get here; but we may
233 		 * as well be forgiving and just succeed silently.
234 		 */
235 		goto out_put;
236 	dentry = vfs_mkdir(&nop_mnt_idmap, d_inode(dir), dentry, S_IRWXU);
237 	if (IS_ERR(dentry))
238 		status = PTR_ERR(dentry);
239 out_put:
240 	if (!status)
241 		dput(dentry);
242 out_unlock:
243 	inode_unlock(d_inode(dir));
244 	if (status == 0) {
245 		if (nn->in_grace)
246 			__nfsd4_create_reclaim_record_grace(clp, dname,
247 					HEXDIR_LEN, nn);
248 		vfs_fsync(nn->rec_file, 0);
249 	} else {
250 		printk(KERN_ERR "NFSD: failed to write recovery record"
251 				" (err %d); please check that %s exists"
252 				" and is writeable", status,
253 				user_recovery_dirname);
254 	}
255 	mnt_drop_write_file(nn->rec_file);
256 out_creds:
257 	nfs4_reset_creds(original_cred);
258 }
259 
260 typedef int (recdir_func)(struct dentry *, struct dentry *, struct nfsd_net *);
261 
262 struct name_list {
263 	char name[HEXDIR_LEN];
264 	struct list_head list;
265 };
266 
267 struct nfs4_dir_ctx {
268 	struct dir_context ctx;
269 	struct list_head names;
270 };
271 
272 static bool
273 nfsd4_build_namelist(struct dir_context *__ctx, const char *name, int namlen,
274 		loff_t offset, u64 ino, unsigned int d_type)
275 {
276 	struct nfs4_dir_ctx *ctx =
277 		container_of(__ctx, struct nfs4_dir_ctx, ctx);
278 	struct name_list *entry;
279 
280 	if (namlen != HEXDIR_LEN - 1)
281 		return true;
282 	entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
283 	if (entry == NULL)
284 		return false;
285 	memcpy(entry->name, name, HEXDIR_LEN - 1);
286 	entry->name[HEXDIR_LEN - 1] = '\0';
287 	list_add(&entry->list, &ctx->names);
288 	return true;
289 }
290 
291 static int
292 nfsd4_list_rec_dir(recdir_func *f, struct nfsd_net *nn)
293 {
294 	const struct cred *original_cred;
295 	struct dentry *dir = nn->rec_file->f_path.dentry;
296 	struct nfs4_dir_ctx ctx = {
297 		.ctx.actor = nfsd4_build_namelist,
298 		.names = LIST_HEAD_INIT(ctx.names)
299 	};
300 	struct name_list *entry, *tmp;
301 	int status;
302 
303 	status = nfs4_save_creds(&original_cred);
304 	if (status < 0)
305 		return status;
306 
307 	status = vfs_llseek(nn->rec_file, 0, SEEK_SET);
308 	if (status < 0) {
309 		nfs4_reset_creds(original_cred);
310 		return status;
311 	}
312 
313 	status = iterate_dir(nn->rec_file, &ctx.ctx);
314 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
315 
316 	list_for_each_entry_safe(entry, tmp, &ctx.names, list) {
317 		if (!status) {
318 			struct dentry *dentry;
319 			dentry = lookup_one(&nop_mnt_idmap,
320 					    &QSTR(entry->name), dir);
321 			if (IS_ERR(dentry)) {
322 				status = PTR_ERR(dentry);
323 				break;
324 			}
325 			status = f(dir, dentry, nn);
326 			dput(dentry);
327 		}
328 		list_del(&entry->list);
329 		kfree(entry);
330 	}
331 	inode_unlock(d_inode(dir));
332 	nfs4_reset_creds(original_cred);
333 
334 	list_for_each_entry_safe(entry, tmp, &ctx.names, list) {
335 		dprintk("NFSD: %s. Left entry %s\n", __func__, entry->name);
336 		list_del(&entry->list);
337 		kfree(entry);
338 	}
339 	return status;
340 }
341 
342 static int
343 nfsd4_unlink_clid_dir(char *name, struct nfsd_net *nn)
344 {
345 	struct dentry *dir, *dentry;
346 	int status;
347 
348 	dprintk("NFSD: nfsd4_unlink_clid_dir. name %s\n", name);
349 
350 	dir = nn->rec_file->f_path.dentry;
351 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
352 	dentry = lookup_one(&nop_mnt_idmap, &QSTR(name), dir);
353 	if (IS_ERR(dentry)) {
354 		status = PTR_ERR(dentry);
355 		goto out_unlock;
356 	}
357 	status = -ENOENT;
358 	if (d_really_is_negative(dentry))
359 		goto out;
360 	status = vfs_rmdir(&nop_mnt_idmap, d_inode(dir), dentry);
361 out:
362 	dput(dentry);
363 out_unlock:
364 	inode_unlock(d_inode(dir));
365 	return status;
366 }
367 
368 static void
369 __nfsd4_remove_reclaim_record_grace(const char *dname, int len,
370 		struct nfsd_net *nn)
371 {
372 	struct xdr_netobj name;
373 	struct nfs4_client_reclaim *crp;
374 
375 	name.data = kmemdup(dname, len, GFP_KERNEL);
376 	if (!name.data) {
377 		dprintk("%s: failed to allocate memory for name.data!\n",
378 			__func__);
379 		return;
380 	}
381 	name.len = len;
382 	crp = nfsd4_find_reclaim_client(name, nn);
383 	kfree(name.data);
384 	if (crp)
385 		nfs4_remove_reclaim_record(crp, nn);
386 }
387 
388 static void
389 nfsd4_remove_clid_dir(struct nfs4_client *clp)
390 {
391 	const struct cred *original_cred;
392 	char dname[HEXDIR_LEN];
393 	int status;
394 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
395 
396 	if (!nn->rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
397 		return;
398 
399 	status = nfs4_make_rec_clidname(dname, &clp->cl_name);
400 	if (status)
401 		return legacy_recdir_name_error(clp, status);
402 
403 	status = mnt_want_write_file(nn->rec_file);
404 	if (status)
405 		goto out;
406 	clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
407 
408 	status = nfs4_save_creds(&original_cred);
409 	if (status < 0)
410 		goto out_drop_write;
411 
412 	status = nfsd4_unlink_clid_dir(dname, nn);
413 	nfs4_reset_creds(original_cred);
414 	if (status == 0) {
415 		vfs_fsync(nn->rec_file, 0);
416 		if (nn->in_grace)
417 			__nfsd4_remove_reclaim_record_grace(dname,
418 					HEXDIR_LEN, nn);
419 	}
420 out_drop_write:
421 	mnt_drop_write_file(nn->rec_file);
422 out:
423 	if (status)
424 		printk("NFSD: Failed to remove expired client state directory"
425 				" %.*s\n", HEXDIR_LEN, dname);
426 }
427 
428 static int
429 purge_old(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
430 {
431 	int status;
432 	struct xdr_netobj name;
433 
434 	if (child->d_name.len != HEXDIR_LEN - 1) {
435 		printk("%s: illegal name %pd in recovery directory\n",
436 				__func__, child);
437 		/* Keep trying; maybe the others are OK: */
438 		return 0;
439 	}
440 	name.data = kmemdup_nul(child->d_name.name, child->d_name.len, GFP_KERNEL);
441 	if (!name.data) {
442 		dprintk("%s: failed to allocate memory for name.data!\n",
443 			__func__);
444 		goto out;
445 	}
446 	name.len = HEXDIR_LEN;
447 	if (nfs4_has_reclaimed_state(name, nn))
448 		goto out_free;
449 
450 	status = vfs_rmdir(&nop_mnt_idmap, d_inode(parent), child);
451 	if (status)
452 		printk("failed to remove client recovery directory %pd\n",
453 				child);
454 out_free:
455 	kfree(name.data);
456 out:
457 	/* Keep trying, success or failure: */
458 	return 0;
459 }
460 
461 static void
462 nfsd4_recdir_purge_old(struct nfsd_net *nn)
463 {
464 	int status;
465 
466 	nn->in_grace = false;
467 	if (!nn->rec_file)
468 		return;
469 	status = mnt_want_write_file(nn->rec_file);
470 	if (status)
471 		goto out;
472 	status = nfsd4_list_rec_dir(purge_old, nn);
473 	if (status == 0)
474 		vfs_fsync(nn->rec_file, 0);
475 	mnt_drop_write_file(nn->rec_file);
476 out:
477 	nfs4_release_reclaim(nn);
478 	if (status)
479 		printk("nfsd4: failed to purge old clients from recovery"
480 			" directory %pD\n", nn->rec_file);
481 }
482 
483 static int
484 load_recdir(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
485 {
486 	struct xdr_netobj name;
487 	struct xdr_netobj princhash = { .len = 0, .data = NULL };
488 
489 	if (child->d_name.len != HEXDIR_LEN - 1) {
490 		printk("%s: illegal name %pd in recovery directory\n",
491 				__func__, child);
492 		/* Keep trying; maybe the others are OK: */
493 		return 0;
494 	}
495 	name.data = kmemdup_nul(child->d_name.name, child->d_name.len, GFP_KERNEL);
496 	if (!name.data) {
497 		dprintk("%s: failed to allocate memory for name.data!\n",
498 			__func__);
499 		goto out;
500 	}
501 	name.len = HEXDIR_LEN;
502 	if (!nfs4_client_to_reclaim(name, princhash, nn))
503 		kfree(name.data);
504 out:
505 	return 0;
506 }
507 
508 static int
509 nfsd4_recdir_load(struct net *net) {
510 	int status;
511 	struct nfsd_net *nn =  net_generic(net, nfsd_net_id);
512 
513 	if (!nn->rec_file)
514 		return 0;
515 
516 	status = nfsd4_list_rec_dir(load_recdir, nn);
517 	if (status)
518 		printk("nfsd4: failed loading clients from recovery"
519 			" directory %pD\n", nn->rec_file);
520 	return status;
521 }
522 
523 /*
524  * Hold reference to the recovery directory.
525  */
526 
527 static int
528 nfsd4_init_recdir(struct net *net)
529 {
530 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
531 	const struct cred *original_cred;
532 	int status;
533 
534 	printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
535 			user_recovery_dirname);
536 
537 	BUG_ON(nn->rec_file);
538 
539 	status = nfs4_save_creds(&original_cred);
540 	if (status < 0) {
541 		printk("NFSD: Unable to change credentials to find recovery"
542 		       " directory: error %d\n",
543 		       status);
544 		return status;
545 	}
546 
547 	nn->rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0);
548 	if (IS_ERR(nn->rec_file)) {
549 		printk("NFSD: unable to find recovery directory %s\n",
550 				user_recovery_dirname);
551 		status = PTR_ERR(nn->rec_file);
552 		nn->rec_file = NULL;
553 	}
554 
555 	nfs4_reset_creds(original_cred);
556 	if (!status)
557 		nn->in_grace = true;
558 	return status;
559 }
560 
561 static void
562 nfsd4_shutdown_recdir(struct net *net)
563 {
564 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
565 
566 	if (!nn->rec_file)
567 		return;
568 	fput(nn->rec_file);
569 	nn->rec_file = NULL;
570 }
571 
572 static int
573 nfs4_legacy_state_init(struct net *net)
574 {
575 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
576 	int i;
577 
578 	nn->reclaim_str_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
579 						sizeof(struct list_head),
580 						GFP_KERNEL);
581 	if (!nn->reclaim_str_hashtbl)
582 		return -ENOMEM;
583 
584 	for (i = 0; i < CLIENT_HASH_SIZE; i++)
585 		INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]);
586 	nn->reclaim_str_hashtbl_size = 0;
587 
588 	return 0;
589 }
590 
591 static void
592 nfs4_legacy_state_shutdown(struct net *net)
593 {
594 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
595 
596 	kfree(nn->reclaim_str_hashtbl);
597 }
598 
599 static int
600 nfsd4_load_reboot_recovery_data(struct net *net)
601 {
602 	int status;
603 
604 	status = nfsd4_init_recdir(net);
605 	if (status)
606 		return status;
607 
608 	status = nfsd4_recdir_load(net);
609 	if (status)
610 		nfsd4_shutdown_recdir(net);
611 
612 	return status;
613 }
614 
615 static int
616 nfsd4_legacy_tracking_init(struct net *net)
617 {
618 	int status;
619 
620 	/* XXX: The legacy code won't work in a container */
621 	if (net != &init_net) {
622 		pr_warn("NFSD: attempt to initialize legacy client tracking in a container ignored.\n");
623 		return -EINVAL;
624 	}
625 
626 	status = nfs4_legacy_state_init(net);
627 	if (status)
628 		return status;
629 
630 	status = nfsd4_load_reboot_recovery_data(net);
631 	if (status)
632 		goto err;
633 	pr_info("NFSD: Using legacy client tracking operations.\n");
634 	return 0;
635 
636 err:
637 	nfs4_legacy_state_shutdown(net);
638 	return status;
639 }
640 
641 static void
642 nfsd4_legacy_tracking_exit(struct net *net)
643 {
644 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
645 
646 	nfs4_release_reclaim(nn);
647 	nfsd4_shutdown_recdir(net);
648 	nfs4_legacy_state_shutdown(net);
649 }
650 
651 /*
652  * Change the NFSv4 recovery directory to recdir.
653  */
654 int
655 nfs4_reset_recoverydir(char *recdir)
656 {
657 	int status;
658 	struct path path;
659 
660 	status = kern_path(recdir, LOOKUP_FOLLOW, &path);
661 	if (status)
662 		return status;
663 	status = -ENOTDIR;
664 	if (d_is_dir(path.dentry)) {
665 		strscpy(user_recovery_dirname, recdir,
666 			sizeof(user_recovery_dirname));
667 		status = 0;
668 	}
669 	path_put(&path);
670 	return status;
671 }
672 
673 char *
674 nfs4_recoverydir(void)
675 {
676 	return user_recovery_dirname;
677 }
678 
679 static int
680 nfsd4_check_legacy_client(struct nfs4_client *clp)
681 {
682 	int status;
683 	char dname[HEXDIR_LEN];
684 	struct nfs4_client_reclaim *crp;
685 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
686 	struct xdr_netobj name;
687 
688 	/* did we already find that this client is stable? */
689 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
690 		return 0;
691 
692 	status = nfs4_make_rec_clidname(dname, &clp->cl_name);
693 	if (status) {
694 		legacy_recdir_name_error(clp, status);
695 		return status;
696 	}
697 
698 	/* look for it in the reclaim hashtable otherwise */
699 	name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL);
700 	if (!name.data) {
701 		dprintk("%s: failed to allocate memory for name.data!\n",
702 			__func__);
703 		goto out_enoent;
704 	}
705 	name.len = HEXDIR_LEN;
706 	crp = nfsd4_find_reclaim_client(name, nn);
707 	kfree(name.data);
708 	if (crp) {
709 		set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
710 		crp->cr_clp = clp;
711 		return 0;
712 	}
713 
714 out_enoent:
715 	return -ENOENT;
716 }
717 
718 static const struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = {
719 	.init		= nfsd4_legacy_tracking_init,
720 	.exit		= nfsd4_legacy_tracking_exit,
721 	.create		= nfsd4_create_clid_dir,
722 	.remove		= nfsd4_remove_clid_dir,
723 	.check		= nfsd4_check_legacy_client,
724 	.grace_done	= nfsd4_recdir_purge_old,
725 	.version	= 1,
726 	.msglen		= 0,
727 };
728 #endif /* CONFIG_NFSD_LEGACY_CLIENT_TRACKING */
729 
730 /* Globals */
731 #define NFSD_PIPE_DIR		"nfsd"
732 #define NFSD_CLD_PIPE		"cld"
733 
734 /* per-net-ns structure for holding cld upcall info */
735 struct cld_net {
736 	struct rpc_pipe		*cn_pipe;
737 	spinlock_t		 cn_lock;
738 	struct list_head	 cn_list;
739 	unsigned int		 cn_xid;
740 	struct crypto_shash	*cn_tfm;
741 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
742 	bool			 cn_has_legacy;
743 #endif
744 };
745 
746 struct cld_upcall {
747 	struct list_head	 cu_list;
748 	struct cld_net		*cu_net;
749 	struct completion	 cu_done;
750 	union {
751 		struct cld_msg_hdr	 cu_hdr;
752 		struct cld_msg		 cu_msg;
753 		struct cld_msg_v2	 cu_msg_v2;
754 	} cu_u;
755 };
756 
757 static int
758 __cld_pipe_upcall(struct rpc_pipe *pipe, void *cmsg, struct nfsd_net *nn)
759 {
760 	int ret;
761 	struct rpc_pipe_msg msg;
762 	struct cld_upcall *cup = container_of(cmsg, struct cld_upcall, cu_u);
763 
764 	memset(&msg, 0, sizeof(msg));
765 	msg.data = cmsg;
766 	msg.len = nn->client_tracking_ops->msglen;
767 
768 	ret = rpc_queue_upcall(pipe, &msg);
769 	if (ret < 0) {
770 		goto out;
771 	}
772 
773 	wait_for_completion(&cup->cu_done);
774 
775 	if (msg.errno < 0)
776 		ret = msg.errno;
777 out:
778 	return ret;
779 }
780 
781 static int
782 cld_pipe_upcall(struct rpc_pipe *pipe, void *cmsg, struct nfsd_net *nn)
783 {
784 	int ret;
785 
786 	/*
787 	 * -EAGAIN occurs when pipe is closed and reopened while there are
788 	 *  upcalls queued.
789 	 */
790 	do {
791 		ret = __cld_pipe_upcall(pipe, cmsg, nn);
792 	} while (ret == -EAGAIN);
793 
794 	return ret;
795 }
796 
797 static ssize_t
798 __cld_pipe_inprogress_downcall(const struct cld_msg_v2 __user *cmsg,
799 		struct nfsd_net *nn)
800 {
801 	uint8_t cmd, princhashlen;
802 	struct xdr_netobj name, princhash = { .len = 0, .data = NULL };
803 	uint16_t namelen;
804 
805 	if (get_user(cmd, &cmsg->cm_cmd)) {
806 		dprintk("%s: error when copying cmd from userspace", __func__);
807 		return -EFAULT;
808 	}
809 	if (cmd == Cld_GraceStart) {
810 		if (nn->client_tracking_ops->version >= 2) {
811 			const struct cld_clntinfo __user *ci;
812 
813 			ci = &cmsg->cm_u.cm_clntinfo;
814 			if (get_user(namelen, &ci->cc_name.cn_len))
815 				return -EFAULT;
816 			if (namelen == 0 || namelen > NFS4_OPAQUE_LIMIT) {
817 				dprintk("%s: invalid namelen (%u)", __func__, namelen);
818 				return -EINVAL;
819 			}
820 			name.data = memdup_user(&ci->cc_name.cn_id, namelen);
821 			if (IS_ERR(name.data))
822 				return PTR_ERR(name.data);
823 			name.len = namelen;
824 			get_user(princhashlen, &ci->cc_princhash.cp_len);
825 			if (princhashlen > 0) {
826 				princhash.data = memdup_user(
827 						&ci->cc_princhash.cp_data,
828 						princhashlen);
829 				if (IS_ERR(princhash.data)) {
830 					kfree(name.data);
831 					return PTR_ERR(princhash.data);
832 				}
833 				princhash.len = princhashlen;
834 			} else
835 				princhash.len = 0;
836 		} else {
837 			const struct cld_name __user *cnm;
838 
839 			cnm = &cmsg->cm_u.cm_name;
840 			if (get_user(namelen, &cnm->cn_len))
841 				return -EFAULT;
842 			if (namelen == 0 || namelen > NFS4_OPAQUE_LIMIT) {
843 				dprintk("%s: invalid namelen (%u)", __func__, namelen);
844 				return -EINVAL;
845 			}
846 			name.data = memdup_user(&cnm->cn_id, namelen);
847 			if (IS_ERR(name.data))
848 				return PTR_ERR(name.data);
849 			name.len = namelen;
850 		}
851 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
852 		if (name.len > 5 && memcmp(name.data, "hash:", 5) == 0) {
853 			struct cld_net *cn = nn->cld_net;
854 
855 			name.len = name.len - 5;
856 			memmove(name.data, name.data + 5, name.len);
857 			cn->cn_has_legacy = true;
858 		}
859 #endif
860 		if (!nfs4_client_to_reclaim(name, princhash, nn)) {
861 			kfree(name.data);
862 			kfree(princhash.data);
863 			return -EFAULT;
864 		}
865 		return nn->client_tracking_ops->msglen;
866 	}
867 	return -EFAULT;
868 }
869 
870 static ssize_t
871 cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
872 {
873 	struct cld_upcall *tmp, *cup;
874 	struct cld_msg_hdr __user *hdr = (struct cld_msg_hdr __user *)src;
875 	struct cld_msg_v2 __user *cmsg = (struct cld_msg_v2 __user *)src;
876 	uint32_t xid;
877 	struct nfsd_net *nn = net_generic(file_inode(filp)->i_sb->s_fs_info,
878 						nfsd_net_id);
879 	struct cld_net *cn = nn->cld_net;
880 	int16_t status;
881 
882 	if (mlen != nn->client_tracking_ops->msglen) {
883 		dprintk("%s: got %zu bytes, expected %zu\n", __func__, mlen,
884 			nn->client_tracking_ops->msglen);
885 		return -EINVAL;
886 	}
887 
888 	/* copy just the xid so we can try to find that */
889 	if (copy_from_user(&xid, &hdr->cm_xid, sizeof(xid)) != 0) {
890 		dprintk("%s: error when copying xid from userspace", __func__);
891 		return -EFAULT;
892 	}
893 
894 	/*
895 	 * copy the status so we know whether to remove the upcall from the
896 	 * list (for -EINPROGRESS, we just want to make sure the xid is
897 	 * valid, not remove the upcall from the list)
898 	 */
899 	if (get_user(status, &hdr->cm_status)) {
900 		dprintk("%s: error when copying status from userspace", __func__);
901 		return -EFAULT;
902 	}
903 
904 	/* walk the list and find corresponding xid */
905 	cup = NULL;
906 	spin_lock(&cn->cn_lock);
907 	list_for_each_entry(tmp, &cn->cn_list, cu_list) {
908 		if (get_unaligned(&tmp->cu_u.cu_hdr.cm_xid) == xid) {
909 			cup = tmp;
910 			if (status != -EINPROGRESS)
911 				list_del_init(&cup->cu_list);
912 			break;
913 		}
914 	}
915 	spin_unlock(&cn->cn_lock);
916 
917 	/* couldn't find upcall? */
918 	if (!cup) {
919 		dprintk("%s: couldn't find upcall -- xid=%u\n", __func__, xid);
920 		return -EINVAL;
921 	}
922 
923 	if (status == -EINPROGRESS)
924 		return __cld_pipe_inprogress_downcall(cmsg, nn);
925 
926 	if (copy_from_user(&cup->cu_u.cu_msg_v2, src, mlen) != 0)
927 		return -EFAULT;
928 
929 	complete(&cup->cu_done);
930 	return mlen;
931 }
932 
933 static void
934 cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)
935 {
936 	struct cld_msg *cmsg = msg->data;
937 	struct cld_upcall *cup = container_of(cmsg, struct cld_upcall,
938 						 cu_u.cu_msg);
939 
940 	/* errno >= 0 means we got a downcall */
941 	if (msg->errno >= 0)
942 		return;
943 
944 	complete(&cup->cu_done);
945 }
946 
947 static const struct rpc_pipe_ops cld_upcall_ops = {
948 	.upcall		= rpc_pipe_generic_upcall,
949 	.downcall	= cld_pipe_downcall,
950 	.destroy_msg	= cld_pipe_destroy_msg,
951 };
952 
953 static struct dentry *
954 nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe)
955 {
956 	struct dentry *dir, *dentry;
957 
958 	dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR);
959 	if (dir == NULL)
960 		return ERR_PTR(-ENOENT);
961 	dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe);
962 	dput(dir);
963 	return dentry;
964 }
965 
966 static void
967 nfsd4_cld_unregister_sb(struct rpc_pipe *pipe)
968 {
969 	if (pipe->dentry)
970 		rpc_unlink(pipe->dentry);
971 }
972 
973 static struct dentry *
974 nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe)
975 {
976 	struct super_block *sb;
977 	struct dentry *dentry;
978 
979 	sb = rpc_get_sb_net(net);
980 	if (!sb)
981 		return NULL;
982 	dentry = nfsd4_cld_register_sb(sb, pipe);
983 	rpc_put_sb_net(net);
984 	return dentry;
985 }
986 
987 static void
988 nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe)
989 {
990 	struct super_block *sb;
991 
992 	sb = rpc_get_sb_net(net);
993 	if (sb) {
994 		nfsd4_cld_unregister_sb(pipe);
995 		rpc_put_sb_net(net);
996 	}
997 }
998 
999 /* Initialize rpc_pipefs pipe for communication with client tracking daemon */
1000 static int
1001 __nfsd4_init_cld_pipe(struct net *net)
1002 {
1003 	int ret;
1004 	struct dentry *dentry;
1005 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1006 	struct cld_net *cn;
1007 
1008 	if (nn->cld_net)
1009 		return 0;
1010 
1011 	cn = kzalloc(sizeof(*cn), GFP_KERNEL);
1012 	if (!cn) {
1013 		ret = -ENOMEM;
1014 		goto err;
1015 	}
1016 
1017 	cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
1018 	if (IS_ERR(cn->cn_pipe)) {
1019 		ret = PTR_ERR(cn->cn_pipe);
1020 		goto err;
1021 	}
1022 	spin_lock_init(&cn->cn_lock);
1023 	INIT_LIST_HEAD(&cn->cn_list);
1024 
1025 	dentry = nfsd4_cld_register_net(net, cn->cn_pipe);
1026 	if (IS_ERR(dentry)) {
1027 		ret = PTR_ERR(dentry);
1028 		goto err_destroy_data;
1029 	}
1030 
1031 	cn->cn_pipe->dentry = dentry;
1032 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1033 	cn->cn_has_legacy = false;
1034 #endif
1035 	nn->cld_net = cn;
1036 	return 0;
1037 
1038 err_destroy_data:
1039 	rpc_destroy_pipe_data(cn->cn_pipe);
1040 err:
1041 	kfree(cn);
1042 	printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n",
1043 			ret);
1044 	return ret;
1045 }
1046 
1047 static int
1048 nfsd4_init_cld_pipe(struct net *net)
1049 {
1050 	int status;
1051 
1052 	status = __nfsd4_init_cld_pipe(net);
1053 	if (!status)
1054 		pr_info("NFSD: Using old nfsdcld client tracking operations.\n");
1055 	return status;
1056 }
1057 
1058 static void
1059 nfsd4_remove_cld_pipe(struct net *net)
1060 {
1061 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1062 	struct cld_net *cn = nn->cld_net;
1063 
1064 	nfsd4_cld_unregister_net(net, cn->cn_pipe);
1065 	rpc_destroy_pipe_data(cn->cn_pipe);
1066 	if (cn->cn_tfm)
1067 		crypto_free_shash(cn->cn_tfm);
1068 	kfree(nn->cld_net);
1069 	nn->cld_net = NULL;
1070 }
1071 
1072 static struct cld_upcall *
1073 alloc_cld_upcall(struct nfsd_net *nn)
1074 {
1075 	struct cld_upcall *new, *tmp;
1076 	struct cld_net *cn = nn->cld_net;
1077 
1078 	new = kzalloc(sizeof(*new), GFP_KERNEL);
1079 	if (!new)
1080 		return new;
1081 
1082 	/* FIXME: hard cap on number in flight? */
1083 restart_search:
1084 	spin_lock(&cn->cn_lock);
1085 	list_for_each_entry(tmp, &cn->cn_list, cu_list) {
1086 		if (tmp->cu_u.cu_msg.cm_xid == cn->cn_xid) {
1087 			cn->cn_xid++;
1088 			spin_unlock(&cn->cn_lock);
1089 			goto restart_search;
1090 		}
1091 	}
1092 	init_completion(&new->cu_done);
1093 	new->cu_u.cu_msg.cm_vers = nn->client_tracking_ops->version;
1094 	put_unaligned(cn->cn_xid++, &new->cu_u.cu_msg.cm_xid);
1095 	new->cu_net = cn;
1096 	list_add(&new->cu_list, &cn->cn_list);
1097 	spin_unlock(&cn->cn_lock);
1098 
1099 	dprintk("%s: allocated xid %u\n", __func__, new->cu_u.cu_msg.cm_xid);
1100 
1101 	return new;
1102 }
1103 
1104 static void
1105 free_cld_upcall(struct cld_upcall *victim)
1106 {
1107 	struct cld_net *cn = victim->cu_net;
1108 
1109 	spin_lock(&cn->cn_lock);
1110 	list_del(&victim->cu_list);
1111 	spin_unlock(&cn->cn_lock);
1112 	kfree(victim);
1113 }
1114 
1115 /* Ask daemon to create a new record */
1116 static void
1117 nfsd4_cld_create(struct nfs4_client *clp)
1118 {
1119 	int ret;
1120 	struct cld_upcall *cup;
1121 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1122 	struct cld_net *cn = nn->cld_net;
1123 
1124 	/* Don't upcall if it's already stored */
1125 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
1126 		return;
1127 
1128 	cup = alloc_cld_upcall(nn);
1129 	if (!cup) {
1130 		ret = -ENOMEM;
1131 		goto out_err;
1132 	}
1133 
1134 	cup->cu_u.cu_msg.cm_cmd = Cld_Create;
1135 	cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
1136 	memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
1137 			clp->cl_name.len);
1138 
1139 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
1140 	if (!ret) {
1141 		ret = cup->cu_u.cu_msg.cm_status;
1142 		set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
1143 	}
1144 
1145 	free_cld_upcall(cup);
1146 out_err:
1147 	if (ret)
1148 		printk(KERN_ERR "NFSD: Unable to create client "
1149 				"record on stable storage: %d\n", ret);
1150 }
1151 
1152 /* Ask daemon to create a new record */
1153 static void
1154 nfsd4_cld_create_v2(struct nfs4_client *clp)
1155 {
1156 	int ret;
1157 	struct cld_upcall *cup;
1158 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1159 	struct cld_net *cn = nn->cld_net;
1160 	struct cld_msg_v2 *cmsg;
1161 	struct crypto_shash *tfm = cn->cn_tfm;
1162 	struct xdr_netobj cksum;
1163 	char *principal = NULL;
1164 
1165 	/* Don't upcall if it's already stored */
1166 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
1167 		return;
1168 
1169 	cup = alloc_cld_upcall(nn);
1170 	if (!cup) {
1171 		ret = -ENOMEM;
1172 		goto out_err;
1173 	}
1174 
1175 	cmsg = &cup->cu_u.cu_msg_v2;
1176 	cmsg->cm_cmd = Cld_Create;
1177 	cmsg->cm_u.cm_clntinfo.cc_name.cn_len = clp->cl_name.len;
1178 	memcpy(cmsg->cm_u.cm_clntinfo.cc_name.cn_id, clp->cl_name.data,
1179 			clp->cl_name.len);
1180 	if (clp->cl_cred.cr_raw_principal)
1181 		principal = clp->cl_cred.cr_raw_principal;
1182 	else if (clp->cl_cred.cr_principal)
1183 		principal = clp->cl_cred.cr_principal;
1184 	if (principal) {
1185 		cksum.len = crypto_shash_digestsize(tfm);
1186 		cksum.data = kmalloc(cksum.len, GFP_KERNEL);
1187 		if (cksum.data == NULL) {
1188 			ret = -ENOMEM;
1189 			goto out;
1190 		}
1191 		ret = crypto_shash_tfm_digest(tfm, principal, strlen(principal),
1192 					      cksum.data);
1193 		if (ret) {
1194 			kfree(cksum.data);
1195 			goto out;
1196 		}
1197 		cmsg->cm_u.cm_clntinfo.cc_princhash.cp_len = cksum.len;
1198 		memcpy(cmsg->cm_u.cm_clntinfo.cc_princhash.cp_data,
1199 		       cksum.data, cksum.len);
1200 		kfree(cksum.data);
1201 	} else
1202 		cmsg->cm_u.cm_clntinfo.cc_princhash.cp_len = 0;
1203 
1204 	ret = cld_pipe_upcall(cn->cn_pipe, cmsg, nn);
1205 	if (!ret) {
1206 		ret = cmsg->cm_status;
1207 		set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
1208 	}
1209 
1210 out:
1211 	free_cld_upcall(cup);
1212 out_err:
1213 	if (ret)
1214 		pr_err("NFSD: Unable to create client record on stable storage: %d\n",
1215 				ret);
1216 }
1217 
1218 /* Ask daemon to create a new record */
1219 static void
1220 nfsd4_cld_remove(struct nfs4_client *clp)
1221 {
1222 	int ret;
1223 	struct cld_upcall *cup;
1224 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1225 	struct cld_net *cn = nn->cld_net;
1226 
1227 	/* Don't upcall if it's already removed */
1228 	if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
1229 		return;
1230 
1231 	cup = alloc_cld_upcall(nn);
1232 	if (!cup) {
1233 		ret = -ENOMEM;
1234 		goto out_err;
1235 	}
1236 
1237 	cup->cu_u.cu_msg.cm_cmd = Cld_Remove;
1238 	cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
1239 	memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
1240 			clp->cl_name.len);
1241 
1242 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
1243 	if (!ret) {
1244 		ret = cup->cu_u.cu_msg.cm_status;
1245 		clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
1246 	}
1247 
1248 	free_cld_upcall(cup);
1249 out_err:
1250 	if (ret)
1251 		printk(KERN_ERR "NFSD: Unable to remove client "
1252 				"record from stable storage: %d\n", ret);
1253 }
1254 
1255 /*
1256  * For older nfsdcld's that do not allow us to "slurp" the clients
1257  * from the tracking database during startup.
1258  *
1259  * Check for presence of a record, and update its timestamp
1260  */
1261 static int
1262 nfsd4_cld_check_v0(struct nfs4_client *clp)
1263 {
1264 	int ret;
1265 	struct cld_upcall *cup;
1266 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1267 	struct cld_net *cn = nn->cld_net;
1268 
1269 	/* Don't upcall if one was already stored during this grace pd */
1270 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
1271 		return 0;
1272 
1273 	cup = alloc_cld_upcall(nn);
1274 	if (!cup) {
1275 		printk(KERN_ERR "NFSD: Unable to check client record on "
1276 				"stable storage: %d\n", -ENOMEM);
1277 		return -ENOMEM;
1278 	}
1279 
1280 	cup->cu_u.cu_msg.cm_cmd = Cld_Check;
1281 	cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
1282 	memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
1283 			clp->cl_name.len);
1284 
1285 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
1286 	if (!ret) {
1287 		ret = cup->cu_u.cu_msg.cm_status;
1288 		set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
1289 	}
1290 
1291 	free_cld_upcall(cup);
1292 	return ret;
1293 }
1294 
1295 /*
1296  * For newer nfsdcld's that allow us to "slurp" the clients
1297  * from the tracking database during startup.
1298  *
1299  * Check for presence of a record in the reclaim_str_hashtbl
1300  */
1301 static int
1302 nfsd4_cld_check(struct nfs4_client *clp)
1303 {
1304 	struct nfs4_client_reclaim *crp;
1305 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1306 
1307 	/* did we already find that this client is stable? */
1308 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
1309 		return 0;
1310 
1311 	/* look for it in the reclaim hashtable otherwise */
1312 	crp = nfsd4_find_reclaim_client(clp->cl_name, nn);
1313 	if (crp)
1314 		goto found;
1315 
1316 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1317 	if (nn->cld_net->cn_has_legacy) {
1318 		int status;
1319 		char dname[HEXDIR_LEN];
1320 		struct xdr_netobj name;
1321 
1322 		status = nfs4_make_rec_clidname(dname, &clp->cl_name);
1323 		if (status)
1324 			return -ENOENT;
1325 
1326 		name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL);
1327 		if (!name.data) {
1328 			dprintk("%s: failed to allocate memory for name.data!\n",
1329 				__func__);
1330 			return -ENOENT;
1331 		}
1332 		name.len = HEXDIR_LEN;
1333 		crp = nfsd4_find_reclaim_client(name, nn);
1334 		kfree(name.data);
1335 		if (crp)
1336 			goto found;
1337 
1338 	}
1339 #endif
1340 	return -ENOENT;
1341 found:
1342 	crp->cr_clp = clp;
1343 	return 0;
1344 }
1345 
1346 static int
1347 nfsd4_cld_check_v2(struct nfs4_client *clp)
1348 {
1349 	struct nfs4_client_reclaim *crp;
1350 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1351 	struct cld_net *cn = nn->cld_net;
1352 	int status;
1353 	struct crypto_shash *tfm = cn->cn_tfm;
1354 	struct xdr_netobj cksum;
1355 	char *principal = NULL;
1356 
1357 	/* did we already find that this client is stable? */
1358 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
1359 		return 0;
1360 
1361 	/* look for it in the reclaim hashtable otherwise */
1362 	crp = nfsd4_find_reclaim_client(clp->cl_name, nn);
1363 	if (crp)
1364 		goto found;
1365 
1366 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1367 	if (cn->cn_has_legacy) {
1368 		struct xdr_netobj name;
1369 		char dname[HEXDIR_LEN];
1370 
1371 		status = nfs4_make_rec_clidname(dname, &clp->cl_name);
1372 		if (status)
1373 			return -ENOENT;
1374 
1375 		name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL);
1376 		if (!name.data) {
1377 			dprintk("%s: failed to allocate memory for name.data\n",
1378 					__func__);
1379 			return -ENOENT;
1380 		}
1381 		name.len = HEXDIR_LEN;
1382 		crp = nfsd4_find_reclaim_client(name, nn);
1383 		kfree(name.data);
1384 		if (crp)
1385 			goto found;
1386 
1387 	}
1388 #endif
1389 	return -ENOENT;
1390 found:
1391 	if (crp->cr_princhash.len) {
1392 		if (clp->cl_cred.cr_raw_principal)
1393 			principal = clp->cl_cred.cr_raw_principal;
1394 		else if (clp->cl_cred.cr_principal)
1395 			principal = clp->cl_cred.cr_principal;
1396 		if (principal == NULL)
1397 			return -ENOENT;
1398 		cksum.len = crypto_shash_digestsize(tfm);
1399 		cksum.data = kmalloc(cksum.len, GFP_KERNEL);
1400 		if (cksum.data == NULL)
1401 			return -ENOENT;
1402 		status = crypto_shash_tfm_digest(tfm, principal,
1403 						 strlen(principal), cksum.data);
1404 		if (status) {
1405 			kfree(cksum.data);
1406 			return -ENOENT;
1407 		}
1408 		if (memcmp(crp->cr_princhash.data, cksum.data,
1409 				crp->cr_princhash.len)) {
1410 			kfree(cksum.data);
1411 			return -ENOENT;
1412 		}
1413 		kfree(cksum.data);
1414 	}
1415 	crp->cr_clp = clp;
1416 	return 0;
1417 }
1418 
1419 static int
1420 nfsd4_cld_grace_start(struct nfsd_net *nn)
1421 {
1422 	int ret;
1423 	struct cld_upcall *cup;
1424 	struct cld_net *cn = nn->cld_net;
1425 
1426 	cup = alloc_cld_upcall(nn);
1427 	if (!cup) {
1428 		ret = -ENOMEM;
1429 		goto out_err;
1430 	}
1431 
1432 	cup->cu_u.cu_msg.cm_cmd = Cld_GraceStart;
1433 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
1434 	if (!ret)
1435 		ret = cup->cu_u.cu_msg.cm_status;
1436 
1437 	free_cld_upcall(cup);
1438 out_err:
1439 	if (ret)
1440 		dprintk("%s: Unable to get clients from userspace: %d\n",
1441 			__func__, ret);
1442 	return ret;
1443 }
1444 
1445 /* For older nfsdcld's that need cm_gracetime */
1446 static void
1447 nfsd4_cld_grace_done_v0(struct nfsd_net *nn)
1448 {
1449 	int ret;
1450 	struct cld_upcall *cup;
1451 	struct cld_net *cn = nn->cld_net;
1452 
1453 	cup = alloc_cld_upcall(nn);
1454 	if (!cup) {
1455 		ret = -ENOMEM;
1456 		goto out_err;
1457 	}
1458 
1459 	cup->cu_u.cu_msg.cm_cmd = Cld_GraceDone;
1460 	cup->cu_u.cu_msg.cm_u.cm_gracetime = nn->boot_time;
1461 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
1462 	if (!ret)
1463 		ret = cup->cu_u.cu_msg.cm_status;
1464 
1465 	free_cld_upcall(cup);
1466 out_err:
1467 	if (ret)
1468 		printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
1469 }
1470 
1471 /*
1472  * For newer nfsdcld's that do not need cm_gracetime.  We also need to call
1473  * nfs4_release_reclaim() to clear out the reclaim_str_hashtbl.
1474  */
1475 static void
1476 nfsd4_cld_grace_done(struct nfsd_net *nn)
1477 {
1478 	int ret;
1479 	struct cld_upcall *cup;
1480 	struct cld_net *cn = nn->cld_net;
1481 
1482 	cup = alloc_cld_upcall(nn);
1483 	if (!cup) {
1484 		ret = -ENOMEM;
1485 		goto out_err;
1486 	}
1487 
1488 	cup->cu_u.cu_msg.cm_cmd = Cld_GraceDone;
1489 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
1490 	if (!ret)
1491 		ret = cup->cu_u.cu_msg.cm_status;
1492 
1493 	free_cld_upcall(cup);
1494 out_err:
1495 	nfs4_release_reclaim(nn);
1496 	if (ret)
1497 		printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
1498 }
1499 
1500 static int
1501 nfs4_cld_state_init(struct net *net)
1502 {
1503 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1504 	int i;
1505 
1506 	nn->reclaim_str_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
1507 						sizeof(struct list_head),
1508 						GFP_KERNEL);
1509 	if (!nn->reclaim_str_hashtbl)
1510 		return -ENOMEM;
1511 
1512 	for (i = 0; i < CLIENT_HASH_SIZE; i++)
1513 		INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]);
1514 	nn->reclaim_str_hashtbl_size = 0;
1515 	nn->track_reclaim_completes = true;
1516 	atomic_set(&nn->nr_reclaim_complete, 0);
1517 
1518 	return 0;
1519 }
1520 
1521 static void
1522 nfs4_cld_state_shutdown(struct net *net)
1523 {
1524 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1525 
1526 	nn->track_reclaim_completes = false;
1527 	kfree(nn->reclaim_str_hashtbl);
1528 }
1529 
1530 static bool
1531 cld_running(struct nfsd_net *nn)
1532 {
1533 	struct cld_net *cn = nn->cld_net;
1534 	struct rpc_pipe *pipe = cn->cn_pipe;
1535 
1536 	return pipe->nreaders || pipe->nwriters;
1537 }
1538 
1539 static int
1540 nfsd4_cld_get_version(struct nfsd_net *nn)
1541 {
1542 	int ret = 0;
1543 	struct cld_upcall *cup;
1544 	struct cld_net *cn = nn->cld_net;
1545 	uint8_t version;
1546 
1547 	cup = alloc_cld_upcall(nn);
1548 	if (!cup) {
1549 		ret = -ENOMEM;
1550 		goto out_err;
1551 	}
1552 	cup->cu_u.cu_msg.cm_cmd = Cld_GetVersion;
1553 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
1554 	if (!ret) {
1555 		ret = cup->cu_u.cu_msg.cm_status;
1556 		if (ret)
1557 			goto out_free;
1558 		version = cup->cu_u.cu_msg.cm_u.cm_version;
1559 		dprintk("%s: userspace returned version %u\n",
1560 				__func__, version);
1561 		if (version < 1)
1562 			version = 1;
1563 		else if (version > CLD_UPCALL_VERSION)
1564 			version = CLD_UPCALL_VERSION;
1565 
1566 		switch (version) {
1567 		case 1:
1568 			nn->client_tracking_ops = &nfsd4_cld_tracking_ops;
1569 			break;
1570 		case 2:
1571 			nn->client_tracking_ops = &nfsd4_cld_tracking_ops_v2;
1572 			break;
1573 		default:
1574 			break;
1575 		}
1576 	}
1577 out_free:
1578 	free_cld_upcall(cup);
1579 out_err:
1580 	if (ret)
1581 		dprintk("%s: Unable to get version from userspace: %d\n",
1582 			__func__, ret);
1583 	return ret;
1584 }
1585 
1586 static int
1587 nfsd4_cld_tracking_init(struct net *net)
1588 {
1589 	int status;
1590 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1591 	bool running;
1592 	int retries = 10;
1593 	struct crypto_shash *tfm;
1594 
1595 	status = nfs4_cld_state_init(net);
1596 	if (status)
1597 		return status;
1598 
1599 	status = __nfsd4_init_cld_pipe(net);
1600 	if (status)
1601 		goto err_shutdown;
1602 
1603 	/*
1604 	 * rpc pipe upcalls take 30 seconds to time out, so we don't want to
1605 	 * queue an upcall unless we know that nfsdcld is running (because we
1606 	 * want this to fail fast so that nfsd4_client_tracking_init() can try
1607 	 * the next client tracking method).  nfsdcld should already be running
1608 	 * before nfsd is started, so the wait here is for nfsdcld to open the
1609 	 * pipefs file we just created.
1610 	 */
1611 	while (!(running = cld_running(nn)) && retries--)
1612 		msleep(100);
1613 
1614 	if (!running) {
1615 		status = -ETIMEDOUT;
1616 		goto err_remove;
1617 	}
1618 	tfm = crypto_alloc_shash("sha256", 0, 0);
1619 	if (IS_ERR(tfm)) {
1620 		status = PTR_ERR(tfm);
1621 		goto err_remove;
1622 	}
1623 	nn->cld_net->cn_tfm = tfm;
1624 
1625 	status = nfsd4_cld_get_version(nn);
1626 	if (status == -EOPNOTSUPP)
1627 		pr_warn("NFSD: nfsdcld GetVersion upcall failed. Please upgrade nfsdcld.\n");
1628 
1629 	status = nfsd4_cld_grace_start(nn);
1630 	if (status) {
1631 		if (status == -EOPNOTSUPP)
1632 			pr_warn("NFSD: nfsdcld GraceStart upcall failed. Please upgrade nfsdcld.\n");
1633 		nfs4_release_reclaim(nn);
1634 		goto err_remove;
1635 	} else
1636 		pr_info("NFSD: Using nfsdcld client tracking operations.\n");
1637 	return 0;
1638 
1639 err_remove:
1640 	nfsd4_remove_cld_pipe(net);
1641 err_shutdown:
1642 	nfs4_cld_state_shutdown(net);
1643 	return status;
1644 }
1645 
1646 static void
1647 nfsd4_cld_tracking_exit(struct net *net)
1648 {
1649 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1650 
1651 	nfs4_release_reclaim(nn);
1652 	nfsd4_remove_cld_pipe(net);
1653 	nfs4_cld_state_shutdown(net);
1654 }
1655 
1656 /* For older nfsdcld's */
1657 static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v0 = {
1658 	.init		= nfsd4_init_cld_pipe,
1659 	.exit		= nfsd4_remove_cld_pipe,
1660 	.create		= nfsd4_cld_create,
1661 	.remove		= nfsd4_cld_remove,
1662 	.check		= nfsd4_cld_check_v0,
1663 	.grace_done	= nfsd4_cld_grace_done_v0,
1664 	.version	= 1,
1665 	.msglen		= sizeof(struct cld_msg),
1666 };
1667 
1668 /* For newer nfsdcld's */
1669 static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = {
1670 	.init		= nfsd4_cld_tracking_init,
1671 	.exit		= nfsd4_cld_tracking_exit,
1672 	.create		= nfsd4_cld_create,
1673 	.remove		= nfsd4_cld_remove,
1674 	.check		= nfsd4_cld_check,
1675 	.grace_done	= nfsd4_cld_grace_done,
1676 	.version	= 1,
1677 	.msglen		= sizeof(struct cld_msg),
1678 };
1679 
1680 /* v2 create/check ops include the principal, if available */
1681 static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v2 = {
1682 	.init		= nfsd4_cld_tracking_init,
1683 	.exit		= nfsd4_cld_tracking_exit,
1684 	.create		= nfsd4_cld_create_v2,
1685 	.remove		= nfsd4_cld_remove,
1686 	.check		= nfsd4_cld_check_v2,
1687 	.grace_done	= nfsd4_cld_grace_done,
1688 	.version	= 2,
1689 	.msglen		= sizeof(struct cld_msg_v2),
1690 };
1691 
1692 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1693 /* upcall via usermodehelper */
1694 static char cltrack_prog[PATH_MAX] = "/sbin/nfsdcltrack";
1695 module_param_string(cltrack_prog, cltrack_prog, sizeof(cltrack_prog),
1696 			S_IRUGO|S_IWUSR);
1697 MODULE_PARM_DESC(cltrack_prog, "Path to the nfsdcltrack upcall program");
1698 
1699 static bool cltrack_legacy_disable;
1700 module_param(cltrack_legacy_disable, bool, S_IRUGO|S_IWUSR);
1701 MODULE_PARM_DESC(cltrack_legacy_disable,
1702 		"Disable legacy recoverydir conversion. Default: false");
1703 
1704 #define LEGACY_TOPDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_TOPDIR="
1705 #define LEGACY_RECDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_RECDIR="
1706 #define HAS_SESSION_ENV_PREFIX "NFSDCLTRACK_CLIENT_HAS_SESSION="
1707 #define GRACE_START_ENV_PREFIX "NFSDCLTRACK_GRACE_START="
1708 
1709 static char *
1710 nfsd4_cltrack_legacy_topdir(void)
1711 {
1712 	int copied;
1713 	size_t len;
1714 	char *result;
1715 
1716 	if (cltrack_legacy_disable)
1717 		return NULL;
1718 
1719 	len = strlen(LEGACY_TOPDIR_ENV_PREFIX) +
1720 		strlen(nfs4_recoverydir()) + 1;
1721 
1722 	result = kmalloc(len, GFP_KERNEL);
1723 	if (!result)
1724 		return result;
1725 
1726 	copied = snprintf(result, len, LEGACY_TOPDIR_ENV_PREFIX "%s",
1727 				nfs4_recoverydir());
1728 	if (copied >= len) {
1729 		/* just return nothing if output was truncated */
1730 		kfree(result);
1731 		return NULL;
1732 	}
1733 
1734 	return result;
1735 }
1736 
1737 static char *
1738 nfsd4_cltrack_legacy_recdir(const struct xdr_netobj *name)
1739 {
1740 	int copied;
1741 	size_t len;
1742 	char *result;
1743 
1744 	if (cltrack_legacy_disable)
1745 		return NULL;
1746 
1747 	/* +1 is for '/' between "topdir" and "recdir" */
1748 	len = strlen(LEGACY_RECDIR_ENV_PREFIX) +
1749 		strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN;
1750 
1751 	result = kmalloc(len, GFP_KERNEL);
1752 	if (!result)
1753 		return result;
1754 
1755 	copied = snprintf(result, len, LEGACY_RECDIR_ENV_PREFIX "%s/",
1756 				nfs4_recoverydir());
1757 	if (copied > (len - HEXDIR_LEN)) {
1758 		/* just return nothing if output will be truncated */
1759 		kfree(result);
1760 		return NULL;
1761 	}
1762 
1763 	copied = nfs4_make_rec_clidname(result + copied, name);
1764 	if (copied) {
1765 		kfree(result);
1766 		return NULL;
1767 	}
1768 
1769 	return result;
1770 }
1771 
1772 static char *
1773 nfsd4_cltrack_client_has_session(struct nfs4_client *clp)
1774 {
1775 	int copied;
1776 	size_t len;
1777 	char *result;
1778 
1779 	/* prefix + Y/N character + terminating NULL */
1780 	len = strlen(HAS_SESSION_ENV_PREFIX) + 1 + 1;
1781 
1782 	result = kmalloc(len, GFP_KERNEL);
1783 	if (!result)
1784 		return result;
1785 
1786 	copied = snprintf(result, len, HAS_SESSION_ENV_PREFIX "%c",
1787 				clp->cl_minorversion ? 'Y' : 'N');
1788 	if (copied >= len) {
1789 		/* just return nothing if output was truncated */
1790 		kfree(result);
1791 		return NULL;
1792 	}
1793 
1794 	return result;
1795 }
1796 
1797 static char *
1798 nfsd4_cltrack_grace_start(time64_t grace_start)
1799 {
1800 	int copied;
1801 	size_t len;
1802 	char *result;
1803 
1804 	/* prefix + max width of int64_t string + terminating NULL */
1805 	len = strlen(GRACE_START_ENV_PREFIX) + 22 + 1;
1806 
1807 	result = kmalloc(len, GFP_KERNEL);
1808 	if (!result)
1809 		return result;
1810 
1811 	copied = snprintf(result, len, GRACE_START_ENV_PREFIX "%lld",
1812 				grace_start);
1813 	if (copied >= len) {
1814 		/* just return nothing if output was truncated */
1815 		kfree(result);
1816 		return NULL;
1817 	}
1818 
1819 	return result;
1820 }
1821 
1822 static int
1823 nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *env0, char *env1)
1824 {
1825 	char *envp[3];
1826 	char *argv[4];
1827 	int ret;
1828 
1829 	if (unlikely(!cltrack_prog[0])) {
1830 		dprintk("%s: cltrack_prog is disabled\n", __func__);
1831 		return -EACCES;
1832 	}
1833 
1834 	dprintk("%s: cmd: %s\n", __func__, cmd);
1835 	dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)");
1836 	dprintk("%s: env0: %s\n", __func__, env0 ? env0 : "(null)");
1837 	dprintk("%s: env1: %s\n", __func__, env1 ? env1 : "(null)");
1838 
1839 	envp[0] = env0;
1840 	envp[1] = env1;
1841 	envp[2] = NULL;
1842 
1843 	argv[0] = (char *)cltrack_prog;
1844 	argv[1] = cmd;
1845 	argv[2] = arg;
1846 	argv[3] = NULL;
1847 
1848 	ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
1849 	/*
1850 	 * Disable the upcall mechanism if we're getting an ENOENT or EACCES
1851 	 * error. The admin can re-enable it on the fly by using sysfs
1852 	 * once the problem has been fixed.
1853 	 */
1854 	if (ret == -ENOENT || ret == -EACCES) {
1855 		dprintk("NFSD: %s was not found or isn't executable (%d). "
1856 			"Setting cltrack_prog to blank string!",
1857 			cltrack_prog, ret);
1858 		cltrack_prog[0] = '\0';
1859 	}
1860 	dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret);
1861 
1862 	return ret;
1863 }
1864 
1865 static char *
1866 bin_to_hex_dup(const unsigned char *src, int srclen)
1867 {
1868 	char *buf;
1869 
1870 	/* +1 for terminating NULL */
1871 	buf = kzalloc((srclen * 2) + 1, GFP_KERNEL);
1872 	if (!buf)
1873 		return buf;
1874 
1875 	bin2hex(buf, src, srclen);
1876 	return buf;
1877 }
1878 
1879 static int
1880 nfsd4_umh_cltrack_init(struct net *net)
1881 {
1882 	int ret;
1883 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1884 	char *grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
1885 
1886 	/* XXX: The usermode helper s not working in container yet. */
1887 	if (net != &init_net) {
1888 		pr_warn("NFSD: attempt to initialize umh client tracking in a container ignored.\n");
1889 		kfree(grace_start);
1890 		return -EINVAL;
1891 	}
1892 
1893 	ret = nfsd4_umh_cltrack_upcall("init", NULL, grace_start, NULL);
1894 	kfree(grace_start);
1895 	if (!ret)
1896 		pr_info("NFSD: Using UMH upcall client tracking operations.\n");
1897 	return ret;
1898 }
1899 
1900 static void
1901 nfsd4_cltrack_upcall_lock(struct nfs4_client *clp)
1902 {
1903 	wait_on_bit_lock(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK,
1904 			 TASK_UNINTERRUPTIBLE);
1905 }
1906 
1907 static void
1908 nfsd4_cltrack_upcall_unlock(struct nfs4_client *clp)
1909 {
1910 	clear_and_wake_up_bit(NFSD4_CLIENT_UPCALL_LOCK, &clp->cl_flags);
1911 }
1912 
1913 static void
1914 nfsd4_umh_cltrack_create(struct nfs4_client *clp)
1915 {
1916 	char *hexid, *has_session, *grace_start;
1917 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1918 
1919 	/*
1920 	 * With v4.0 clients, there's little difference in outcome between a
1921 	 * create and check operation, and we can end up calling into this
1922 	 * function multiple times per client (once for each openowner). So,
1923 	 * for v4.0 clients skip upcalling once the client has been recorded
1924 	 * on stable storage.
1925 	 *
1926 	 * For v4.1+ clients, the outcome of the two operations is different,
1927 	 * so we must ensure that we upcall for the create operation. v4.1+
1928 	 * clients call this on RECLAIM_COMPLETE though, so we should only end
1929 	 * up doing a single create upcall per client.
1930 	 */
1931 	if (clp->cl_minorversion == 0 &&
1932 	    test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
1933 		return;
1934 
1935 	hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
1936 	if (!hexid) {
1937 		dprintk("%s: can't allocate memory for upcall!\n", __func__);
1938 		return;
1939 	}
1940 
1941 	has_session = nfsd4_cltrack_client_has_session(clp);
1942 	grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
1943 
1944 	nfsd4_cltrack_upcall_lock(clp);
1945 	if (!nfsd4_umh_cltrack_upcall("create", hexid, has_session, grace_start))
1946 		set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
1947 	nfsd4_cltrack_upcall_unlock(clp);
1948 
1949 	kfree(has_session);
1950 	kfree(grace_start);
1951 	kfree(hexid);
1952 }
1953 
1954 static void
1955 nfsd4_umh_cltrack_remove(struct nfs4_client *clp)
1956 {
1957 	char *hexid;
1958 
1959 	if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
1960 		return;
1961 
1962 	hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
1963 	if (!hexid) {
1964 		dprintk("%s: can't allocate memory for upcall!\n", __func__);
1965 		return;
1966 	}
1967 
1968 	nfsd4_cltrack_upcall_lock(clp);
1969 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags) &&
1970 	    nfsd4_umh_cltrack_upcall("remove", hexid, NULL, NULL) == 0)
1971 		clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
1972 	nfsd4_cltrack_upcall_unlock(clp);
1973 
1974 	kfree(hexid);
1975 }
1976 
1977 static int
1978 nfsd4_umh_cltrack_check(struct nfs4_client *clp)
1979 {
1980 	int ret;
1981 	char *hexid, *has_session, *legacy;
1982 
1983 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
1984 		return 0;
1985 
1986 	hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
1987 	if (!hexid) {
1988 		dprintk("%s: can't allocate memory for upcall!\n", __func__);
1989 		return -ENOMEM;
1990 	}
1991 
1992 	has_session = nfsd4_cltrack_client_has_session(clp);
1993 	legacy = nfsd4_cltrack_legacy_recdir(&clp->cl_name);
1994 
1995 	nfsd4_cltrack_upcall_lock(clp);
1996 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) {
1997 		ret = 0;
1998 	} else {
1999 		ret = nfsd4_umh_cltrack_upcall("check", hexid, has_session, legacy);
2000 		if (ret == 0)
2001 			set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
2002 	}
2003 	nfsd4_cltrack_upcall_unlock(clp);
2004 	kfree(has_session);
2005 	kfree(legacy);
2006 	kfree(hexid);
2007 
2008 	return ret;
2009 }
2010 
2011 static void
2012 nfsd4_umh_cltrack_grace_done(struct nfsd_net *nn)
2013 {
2014 	char *legacy;
2015 	char timestr[22]; /* FIXME: better way to determine max size? */
2016 
2017 	sprintf(timestr, "%lld", nn->boot_time);
2018 	legacy = nfsd4_cltrack_legacy_topdir();
2019 	nfsd4_umh_cltrack_upcall("gracedone", timestr, legacy, NULL);
2020 	kfree(legacy);
2021 }
2022 
2023 static const struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = {
2024 	.init		= nfsd4_umh_cltrack_init,
2025 	.exit		= NULL,
2026 	.create		= nfsd4_umh_cltrack_create,
2027 	.remove		= nfsd4_umh_cltrack_remove,
2028 	.check		= nfsd4_umh_cltrack_check,
2029 	.grace_done	= nfsd4_umh_cltrack_grace_done,
2030 	.version	= 1,
2031 	.msglen		= 0,
2032 };
2033 
2034 static inline int check_for_legacy_methods(int status, struct net *net)
2035 {
2036 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2037 	struct path path;
2038 
2039 	/*
2040 	 * Next, try the UMH upcall.
2041 	 */
2042 	nn->client_tracking_ops = &nfsd4_umh_tracking_ops;
2043 	status = nn->client_tracking_ops->init(net);
2044 	if (!status)
2045 		return status;
2046 
2047 	/*
2048 	 * Finally, See if the recoverydir exists and is a directory.
2049 	 * If it is, then use the legacy ops.
2050 	 */
2051 	nn->client_tracking_ops = &nfsd4_legacy_tracking_ops;
2052 	status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path);
2053 	if (!status) {
2054 		status = !d_is_dir(path.dentry);
2055 		path_put(&path);
2056 		if (status)
2057 			return -ENOTDIR;
2058 	}
2059 	return status;
2060 }
2061 #else
2062 static inline int check_for_legacy_methods(int status, struct net *net)
2063 {
2064 	return status;
2065 }
2066 #endif /* CONFIG_LEGACY_NFSD_CLIENT_TRACKING */
2067 
2068 int
2069 nfsd4_client_tracking_init(struct net *net)
2070 {
2071 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2072 	int status;
2073 
2074 	/* just run the init if it the method is already decided */
2075 	if (nn->client_tracking_ops)
2076 		goto do_init;
2077 
2078 	/* First, try to use nfsdcld */
2079 	nn->client_tracking_ops = &nfsd4_cld_tracking_ops;
2080 	status = nn->client_tracking_ops->init(net);
2081 	if (!status)
2082 		return status;
2083 	if (status != -ETIMEDOUT) {
2084 		nn->client_tracking_ops = &nfsd4_cld_tracking_ops_v0;
2085 		status = nn->client_tracking_ops->init(net);
2086 		if (!status)
2087 			return status;
2088 	}
2089 
2090 	status = check_for_legacy_methods(status, net);
2091 	if (status)
2092 		goto out;
2093 do_init:
2094 	status = nn->client_tracking_ops->init(net);
2095 out:
2096 	if (status) {
2097 		pr_warn("NFSD: Unable to initialize client recovery tracking! (%d)\n", status);
2098 		pr_warn("NFSD: Is nfsdcld running? If not, enable CONFIG_NFSD_LEGACY_CLIENT_TRACKING.\n");
2099 		nn->client_tracking_ops = NULL;
2100 	}
2101 	return status;
2102 }
2103 
2104 void
2105 nfsd4_client_tracking_exit(struct net *net)
2106 {
2107 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2108 
2109 	if (nn->client_tracking_ops) {
2110 		if (nn->client_tracking_ops->exit)
2111 			nn->client_tracking_ops->exit(net);
2112 		nn->client_tracking_ops = NULL;
2113 	}
2114 }
2115 
2116 void
2117 nfsd4_client_record_create(struct nfs4_client *clp)
2118 {
2119 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2120 
2121 	if (nn->client_tracking_ops)
2122 		nn->client_tracking_ops->create(clp);
2123 }
2124 
2125 void
2126 nfsd4_client_record_remove(struct nfs4_client *clp)
2127 {
2128 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2129 
2130 	if (nn->client_tracking_ops)
2131 		nn->client_tracking_ops->remove(clp);
2132 }
2133 
2134 int
2135 nfsd4_client_record_check(struct nfs4_client *clp)
2136 {
2137 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2138 
2139 	if (nn->client_tracking_ops)
2140 		return nn->client_tracking_ops->check(clp);
2141 
2142 	return -EOPNOTSUPP;
2143 }
2144 
2145 void
2146 nfsd4_record_grace_done(struct nfsd_net *nn)
2147 {
2148 	if (nn->client_tracking_ops)
2149 		nn->client_tracking_ops->grace_done(nn);
2150 }
2151 
2152 static int
2153 rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr)
2154 {
2155 	struct super_block *sb = ptr;
2156 	struct net *net = sb->s_fs_info;
2157 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2158 	struct cld_net *cn = nn->cld_net;
2159 	struct dentry *dentry;
2160 	int ret = 0;
2161 
2162 	if (!try_module_get(THIS_MODULE))
2163 		return 0;
2164 
2165 	if (!cn) {
2166 		module_put(THIS_MODULE);
2167 		return 0;
2168 	}
2169 
2170 	switch (event) {
2171 	case RPC_PIPEFS_MOUNT:
2172 		dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe);
2173 		if (IS_ERR(dentry)) {
2174 			ret = PTR_ERR(dentry);
2175 			break;
2176 		}
2177 		cn->cn_pipe->dentry = dentry;
2178 		break;
2179 	case RPC_PIPEFS_UMOUNT:
2180 		if (cn->cn_pipe->dentry)
2181 			nfsd4_cld_unregister_sb(cn->cn_pipe);
2182 		break;
2183 	default:
2184 		ret = -ENOTSUPP;
2185 		break;
2186 	}
2187 	module_put(THIS_MODULE);
2188 	return ret;
2189 }
2190 
2191 static struct notifier_block nfsd4_cld_block = {
2192 	.notifier_call = rpc_pipefs_event,
2193 };
2194 
2195 int
2196 register_cld_notifier(void)
2197 {
2198 	WARN_ON(!nfsd_net_id);
2199 	return rpc_pipefs_notifier_register(&nfsd4_cld_block);
2200 }
2201 
2202 void
2203 unregister_cld_notifier(void)
2204 {
2205 	rpc_pipefs_notifier_unregister(&nfsd4_cld_block);
2206 }
2207