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