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