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