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