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