2 * Copyright (c) 2004 The Regents of the University of Michigan.
3 * Copyright (c) 2012 Jeff Layton <jlayton@redhat.com>
6 * Andy Adamson <andros@citi.umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
35 #include <linux/file.h>
36 #include <linux/slab.h>
37 #include <linux/namei.h>
38 #include <linux/crypto.h>
39 #include <linux/sched.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>
52 #define NFSDDBG_FACILITY NFSDDBG_PROC
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 net *, time_t);
65 static struct file *rec_file;
66 static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
67 static struct nfsd4_client_tracking_ops *client_tracking_ops;
70 nfs4_save_creds(const struct cred **original_creds)
74 new = prepare_creds();
80 *original_creds = override_creds(new);
86 nfs4_reset_creds(const struct cred *original)
88 revert_creds(original);
92 md5_to_hex(char *out, char *md5)
96 for (i=0; i<16; i++) {
97 unsigned char c = md5[i];
99 *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
100 *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
106 nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
108 struct xdr_netobj cksum;
109 struct hash_desc desc;
110 struct scatterlist sg;
111 __be32 status = nfserr_jukebox;
113 dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
114 clname->len, clname->data);
115 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
116 desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
117 if (IS_ERR(desc.tfm))
119 cksum.len = crypto_hash_digestsize(desc.tfm);
120 cksum.data = kmalloc(cksum.len, GFP_KERNEL);
121 if (cksum.data == NULL)
124 sg_init_one(&sg, clname->data, clname->len);
126 if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data))
129 md5_to_hex(dname, cksum.data);
134 crypto_free_hash(desc.tfm);
140 nfsd4_create_clid_dir(struct nfs4_client *clp)
142 const struct cred *original_cred;
143 char *dname = clp->cl_recdir;
144 struct dentry *dir, *dentry;
147 dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
149 if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
153 status = nfs4_save_creds(&original_cred);
157 status = mnt_want_write_file(rec_file);
161 dir = rec_file->f_path.dentry;
162 /* lock the parent */
163 mutex_lock(&dir->d_inode->i_mutex);
165 dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
166 if (IS_ERR(dentry)) {
167 status = PTR_ERR(dentry);
172 * In the 4.1 case, where we're called from
173 * reclaim_complete(), records from the previous reboot
174 * may still be left, so this is OK.
176 * In the 4.0 case, we should never get here; but we may
177 * as well be forgiving and just succeed silently.
180 status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU);
184 mutex_unlock(&dir->d_inode->i_mutex);
186 vfs_fsync(rec_file, 0);
188 printk(KERN_ERR "NFSD: failed to write recovery record"
189 " (err %d); please check that %s exists"
190 " and is writeable", status,
191 user_recovery_dirname);
192 mnt_drop_write_file(rec_file);
193 nfs4_reset_creds(original_cred);
196 typedef int (recdir_func)(struct dentry *, struct dentry *);
199 char name[HEXDIR_LEN];
200 struct list_head list;
204 nfsd4_build_namelist(void *arg, const char *name, int namlen,
205 loff_t offset, u64 ino, unsigned int d_type)
207 struct list_head *names = arg;
208 struct name_list *entry;
210 if (namlen != HEXDIR_LEN - 1)
212 entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
215 memcpy(entry->name, name, HEXDIR_LEN - 1);
216 entry->name[HEXDIR_LEN - 1] = '\0';
217 list_add(&entry->list, names);
222 nfsd4_list_rec_dir(recdir_func *f)
224 const struct cred *original_cred;
225 struct dentry *dir = rec_file->f_path.dentry;
229 status = nfs4_save_creds(&original_cred);
233 status = vfs_llseek(rec_file, 0, SEEK_SET);
235 nfs4_reset_creds(original_cred);
239 status = vfs_readdir(rec_file, nfsd4_build_namelist, &names);
240 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
241 while (!list_empty(&names)) {
242 struct name_list *entry;
243 entry = list_entry(names.next, struct name_list, list);
245 struct dentry *dentry;
246 dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
247 if (IS_ERR(dentry)) {
248 status = PTR_ERR(dentry);
251 status = f(dir, dentry);
254 list_del(&entry->list);
257 mutex_unlock(&dir->d_inode->i_mutex);
258 nfs4_reset_creds(original_cred);
263 nfsd4_unlink_clid_dir(char *name, int namlen)
265 struct dentry *dir, *dentry;
268 dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
270 dir = rec_file->f_path.dentry;
271 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
272 dentry = lookup_one_len(name, dir, namlen);
273 if (IS_ERR(dentry)) {
274 status = PTR_ERR(dentry);
278 if (!dentry->d_inode)
280 status = vfs_rmdir(dir->d_inode, dentry);
284 mutex_unlock(&dir->d_inode->i_mutex);
289 nfsd4_remove_clid_dir(struct nfs4_client *clp)
291 const struct cred *original_cred;
294 if (!rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
297 status = mnt_want_write_file(rec_file);
300 clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
302 status = nfs4_save_creds(&original_cred);
306 status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
307 nfs4_reset_creds(original_cred);
309 vfs_fsync(rec_file, 0);
311 mnt_drop_write_file(rec_file);
314 printk("NFSD: Failed to remove expired client state directory"
315 " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
319 purge_old(struct dentry *parent, struct dentry *child)
323 if (nfs4_has_reclaimed_state(child->d_name.name))
326 status = vfs_rmdir(parent->d_inode, child);
328 printk("failed to remove client recovery directory %s\n",
330 /* Keep trying, success or failure: */
335 nfsd4_recdir_purge_old(struct net *net, time_t boot_time)
341 status = mnt_want_write_file(rec_file);
344 status = nfsd4_list_rec_dir(purge_old);
346 vfs_fsync(rec_file, 0);
347 mnt_drop_write_file(rec_file);
350 printk("nfsd4: failed to purge old clients from recovery"
351 " directory %s\n", rec_file->f_path.dentry->d_name.name);
355 load_recdir(struct dentry *parent, struct dentry *child)
357 if (child->d_name.len != HEXDIR_LEN - 1) {
358 printk("nfsd4: illegal name %s in recovery directory\n",
360 /* Keep trying; maybe the others are OK: */
363 nfs4_client_to_reclaim(child->d_name.name);
368 nfsd4_recdir_load(void) {
374 status = nfsd4_list_rec_dir(load_recdir);
376 printk("nfsd4: failed loading clients from recovery"
377 " directory %s\n", rec_file->f_path.dentry->d_name.name);
382 * Hold reference to the recovery directory.
386 nfsd4_init_recdir(void)
388 const struct cred *original_cred;
391 printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
392 user_recovery_dirname);
396 status = nfs4_save_creds(&original_cred);
398 printk("NFSD: Unable to change credentials to find recovery"
399 " directory: error %d\n",
404 rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0);
405 if (IS_ERR(rec_file)) {
406 printk("NFSD: unable to find recovery directory %s\n",
407 user_recovery_dirname);
408 status = PTR_ERR(rec_file);
412 nfs4_reset_creds(original_cred);
417 nfsd4_load_reboot_recovery_data(struct net *net)
421 /* XXX: The legacy code won't work in a container */
422 if (net != &init_net) {
423 WARN(1, KERN_ERR "NFSD: attempt to initialize legacy client "
424 "tracking in a container!\n");
429 status = nfsd4_init_recdir();
431 status = nfsd4_recdir_load();
434 printk(KERN_ERR "NFSD: Failure reading reboot recovery data\n");
439 nfsd4_shutdown_recdir(void)
448 nfsd4_legacy_tracking_exit(struct net *net)
450 nfs4_release_reclaim();
451 nfsd4_shutdown_recdir();
455 * Change the NFSv4 recovery directory to recdir.
458 nfs4_reset_recoverydir(char *recdir)
463 status = kern_path(recdir, LOOKUP_FOLLOW, &path);
467 if (S_ISDIR(path.dentry->d_inode->i_mode)) {
468 strcpy(user_recovery_dirname, recdir);
476 nfs4_recoverydir(void)
478 return user_recovery_dirname;
482 nfsd4_check_legacy_client(struct nfs4_client *clp)
484 /* did we already find that this client is stable? */
485 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
488 /* look for it in the reclaim hashtable otherwise */
489 if (nfsd4_find_reclaim_client(clp->cl_recdir)) {
490 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
497 static struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = {
498 .init = nfsd4_load_reboot_recovery_data,
499 .exit = nfsd4_legacy_tracking_exit,
500 .create = nfsd4_create_clid_dir,
501 .remove = nfsd4_remove_clid_dir,
502 .check = nfsd4_check_legacy_client,
503 .grace_done = nfsd4_recdir_purge_old,
507 #define NFSD_PIPE_DIR "nfsd"
508 #define NFSD_CLD_PIPE "cld"
510 /* per-net-ns structure for holding cld upcall info */
512 struct rpc_pipe *cn_pipe;
514 struct list_head cn_list;
519 struct list_head cu_list;
520 struct cld_net *cu_net;
521 struct task_struct *cu_task;
522 struct cld_msg cu_msg;
526 __cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
529 struct rpc_pipe_msg msg;
531 memset(&msg, 0, sizeof(msg));
533 msg.len = sizeof(*cmsg);
536 * Set task state before we queue the upcall. That prevents
537 * wake_up_process in the downcall from racing with schedule.
539 set_current_state(TASK_UNINTERRUPTIBLE);
540 ret = rpc_queue_upcall(pipe, &msg);
542 set_current_state(TASK_RUNNING);
547 set_current_state(TASK_RUNNING);
556 cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
561 * -EAGAIN occurs when pipe is closed and reopened while there are
565 ret = __cld_pipe_upcall(pipe, cmsg);
566 } while (ret == -EAGAIN);
572 cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
574 struct cld_upcall *tmp, *cup;
575 struct cld_msg __user *cmsg = (struct cld_msg __user *)src;
577 struct nfsd_net *nn = net_generic(filp->f_dentry->d_sb->s_fs_info,
579 struct cld_net *cn = nn->cld_net;
581 if (mlen != sizeof(*cmsg)) {
582 dprintk("%s: got %zu bytes, expected %zu\n", __func__, mlen,
587 /* copy just the xid so we can try to find that */
588 if (copy_from_user(&xid, &cmsg->cm_xid, sizeof(xid)) != 0) {
589 dprintk("%s: error when copying xid from userspace", __func__);
593 /* walk the list and find corresponding xid */
595 spin_lock(&cn->cn_lock);
596 list_for_each_entry(tmp, &cn->cn_list, cu_list) {
597 if (get_unaligned(&tmp->cu_msg.cm_xid) == xid) {
599 list_del_init(&cup->cu_list);
603 spin_unlock(&cn->cn_lock);
605 /* couldn't find upcall? */
607 dprintk("%s: couldn't find upcall -- xid=%u\n", __func__, xid);
611 if (copy_from_user(&cup->cu_msg, src, mlen) != 0)
614 wake_up_process(cup->cu_task);
619 cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)
621 struct cld_msg *cmsg = msg->data;
622 struct cld_upcall *cup = container_of(cmsg, struct cld_upcall,
625 /* errno >= 0 means we got a downcall */
629 wake_up_process(cup->cu_task);
632 static const struct rpc_pipe_ops cld_upcall_ops = {
633 .upcall = rpc_pipe_generic_upcall,
634 .downcall = cld_pipe_downcall,
635 .destroy_msg = cld_pipe_destroy_msg,
638 static struct dentry *
639 nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe)
641 struct dentry *dir, *dentry;
643 dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR);
645 return ERR_PTR(-ENOENT);
646 dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe);
652 nfsd4_cld_unregister_sb(struct rpc_pipe *pipe)
655 rpc_unlink(pipe->dentry);
658 static struct dentry *
659 nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe)
661 struct super_block *sb;
662 struct dentry *dentry;
664 sb = rpc_get_sb_net(net);
667 dentry = nfsd4_cld_register_sb(sb, pipe);
673 nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe)
675 struct super_block *sb;
677 sb = rpc_get_sb_net(net);
679 nfsd4_cld_unregister_sb(pipe);
684 /* Initialize rpc_pipefs pipe for communication with client tracking daemon */
686 nfsd4_init_cld_pipe(struct net *net)
689 struct dentry *dentry;
690 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
696 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
702 cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
703 if (IS_ERR(cn->cn_pipe)) {
704 ret = PTR_ERR(cn->cn_pipe);
707 spin_lock_init(&cn->cn_lock);
708 INIT_LIST_HEAD(&cn->cn_list);
710 dentry = nfsd4_cld_register_net(net, cn->cn_pipe);
711 if (IS_ERR(dentry)) {
712 ret = PTR_ERR(dentry);
713 goto err_destroy_data;
716 cn->cn_pipe->dentry = dentry;
721 rpc_destroy_pipe_data(cn->cn_pipe);
724 printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n",
730 nfsd4_remove_cld_pipe(struct net *net)
732 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
733 struct cld_net *cn = nn->cld_net;
735 nfsd4_cld_unregister_net(net, cn->cn_pipe);
736 rpc_destroy_pipe_data(cn->cn_pipe);
741 static struct cld_upcall *
742 alloc_cld_upcall(struct cld_net *cn)
744 struct cld_upcall *new, *tmp;
746 new = kzalloc(sizeof(*new), GFP_KERNEL);
750 /* FIXME: hard cap on number in flight? */
752 spin_lock(&cn->cn_lock);
753 list_for_each_entry(tmp, &cn->cn_list, cu_list) {
754 if (tmp->cu_msg.cm_xid == cn->cn_xid) {
756 spin_unlock(&cn->cn_lock);
760 new->cu_task = current;
761 new->cu_msg.cm_vers = CLD_UPCALL_VERSION;
762 put_unaligned(cn->cn_xid++, &new->cu_msg.cm_xid);
764 list_add(&new->cu_list, &cn->cn_list);
765 spin_unlock(&cn->cn_lock);
767 dprintk("%s: allocated xid %u\n", __func__, new->cu_msg.cm_xid);
773 free_cld_upcall(struct cld_upcall *victim)
775 struct cld_net *cn = victim->cu_net;
777 spin_lock(&cn->cn_lock);
778 list_del(&victim->cu_list);
779 spin_unlock(&cn->cn_lock);
783 /* Ask daemon to create a new record */
785 nfsd4_cld_create(struct nfs4_client *clp)
788 struct cld_upcall *cup;
789 /* FIXME: determine net from clp */
790 struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
791 struct cld_net *cn = nn->cld_net;
793 /* Don't upcall if it's already stored */
794 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
797 cup = alloc_cld_upcall(cn);
803 cup->cu_msg.cm_cmd = Cld_Create;
804 cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
805 memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
808 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
810 ret = cup->cu_msg.cm_status;
811 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
814 free_cld_upcall(cup);
817 printk(KERN_ERR "NFSD: Unable to create client "
818 "record on stable storage: %d\n", ret);
821 /* Ask daemon to create a new record */
823 nfsd4_cld_remove(struct nfs4_client *clp)
826 struct cld_upcall *cup;
827 /* FIXME: determine net from clp */
828 struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
829 struct cld_net *cn = nn->cld_net;
831 /* Don't upcall if it's already removed */
832 if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
835 cup = alloc_cld_upcall(cn);
841 cup->cu_msg.cm_cmd = Cld_Remove;
842 cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
843 memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
846 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
848 ret = cup->cu_msg.cm_status;
849 clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
852 free_cld_upcall(cup);
855 printk(KERN_ERR "NFSD: Unable to remove client "
856 "record from stable storage: %d\n", ret);
859 /* Check for presence of a record, and update its timestamp */
861 nfsd4_cld_check(struct nfs4_client *clp)
864 struct cld_upcall *cup;
865 /* FIXME: determine net from clp */
866 struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
867 struct cld_net *cn = nn->cld_net;
869 /* Don't upcall if one was already stored during this grace pd */
870 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
873 cup = alloc_cld_upcall(cn);
875 printk(KERN_ERR "NFSD: Unable to check client record on "
876 "stable storage: %d\n", -ENOMEM);
880 cup->cu_msg.cm_cmd = Cld_Check;
881 cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
882 memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
885 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
887 ret = cup->cu_msg.cm_status;
888 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
891 free_cld_upcall(cup);
896 nfsd4_cld_grace_done(struct net *net, time_t boot_time)
899 struct cld_upcall *cup;
900 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
901 struct cld_net *cn = nn->cld_net;
903 cup = alloc_cld_upcall(cn);
909 cup->cu_msg.cm_cmd = Cld_GraceDone;
910 cup->cu_msg.cm_u.cm_gracetime = (int64_t)boot_time;
911 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
913 ret = cup->cu_msg.cm_status;
915 free_cld_upcall(cup);
918 printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
921 static struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = {
922 .init = nfsd4_init_cld_pipe,
923 .exit = nfsd4_remove_cld_pipe,
924 .create = nfsd4_cld_create,
925 .remove = nfsd4_cld_remove,
926 .check = nfsd4_cld_check,
927 .grace_done = nfsd4_cld_grace_done,
930 /* upcall via usermodehelper */
931 static char cltrack_prog[PATH_MAX] = "/sbin/nfsdcltrack";
932 module_param_string(cltrack_prog, cltrack_prog, sizeof(cltrack_prog),
934 MODULE_PARM_DESC(cltrack_prog, "Path to the nfsdcltrack upcall program");
936 static bool cltrack_legacy_disable;
937 module_param(cltrack_legacy_disable, bool, S_IRUGO|S_IWUSR);
938 MODULE_PARM_DESC(cltrack_legacy_disable,
939 "Disable legacy recoverydir conversion. Default: false");
941 #define LEGACY_TOPDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_TOPDIR="
942 #define LEGACY_RECDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_RECDIR="
945 nfsd4_cltrack_legacy_topdir(void)
951 if (cltrack_legacy_disable)
954 len = strlen(LEGACY_TOPDIR_ENV_PREFIX) +
955 strlen(nfs4_recoverydir()) + 1;
957 result = kmalloc(len, GFP_KERNEL);
961 copied = snprintf(result, len, LEGACY_TOPDIR_ENV_PREFIX "%s",
964 /* just return nothing if output was truncated */
973 nfsd4_cltrack_legacy_recdir(const char *recdir)
979 if (cltrack_legacy_disable)
982 /* +1 is for '/' between "topdir" and "recdir" */
983 len = strlen(LEGACY_RECDIR_ENV_PREFIX) +
984 strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN;
986 result = kmalloc(len, GFP_KERNEL);
990 copied = snprintf(result, len, LEGACY_RECDIR_ENV_PREFIX "%s/%s",
991 nfs4_recoverydir(), recdir);
993 /* just return nothing if output was truncated */
1002 nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *legacy)
1008 if (unlikely(!cltrack_prog[0])) {
1009 dprintk("%s: cltrack_prog is disabled\n", __func__);
1013 dprintk("%s: cmd: %s\n", __func__, cmd);
1014 dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)");
1015 dprintk("%s: legacy: %s\n", __func__, legacy ? legacy : "(null)");
1020 argv[0] = (char *)cltrack_prog;
1025 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
1027 * Disable the upcall mechanism if we're getting an ENOENT or EACCES
1028 * error. The admin can re-enable it on the fly by using sysfs
1029 * once the problem has been fixed.
1031 if (ret == -ENOENT || ret == -EACCES) {
1032 dprintk("NFSD: %s was not found or isn't executable (%d). "
1033 "Setting cltrack_prog to blank string!",
1035 cltrack_prog[0] = '\0';
1037 dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret);
1043 bin_to_hex_dup(const unsigned char *src, int srclen)
1048 /* +1 for terminating NULL */
1049 buf = kmalloc((srclen * 2) + 1, GFP_KERNEL);
1054 for (i = 0; i < srclen; i++) {
1055 sprintf(hex, "%2.2x", *src++);
1062 nfsd4_umh_cltrack_init(struct net __attribute__((unused)) *net)
1064 return nfsd4_umh_cltrack_upcall("init", NULL, NULL);
1068 nfsd4_umh_cltrack_create(struct nfs4_client *clp)
1072 hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
1074 dprintk("%s: can't allocate memory for upcall!\n", __func__);
1077 nfsd4_umh_cltrack_upcall("create", hexid, NULL);
1082 nfsd4_umh_cltrack_remove(struct nfs4_client *clp)
1086 hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
1088 dprintk("%s: can't allocate memory for upcall!\n", __func__);
1091 nfsd4_umh_cltrack_upcall("remove", hexid, NULL);
1096 nfsd4_umh_cltrack_check(struct nfs4_client *clp)
1099 char *hexid, *legacy;
1101 hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
1103 dprintk("%s: can't allocate memory for upcall!\n", __func__);
1106 legacy = nfsd4_cltrack_legacy_recdir(clp->cl_recdir);
1107 ret = nfsd4_umh_cltrack_upcall("check", hexid, legacy);
1114 nfsd4_umh_cltrack_grace_done(struct net __attribute__((unused)) *net,
1118 char timestr[22]; /* FIXME: better way to determine max size? */
1120 sprintf(timestr, "%ld", boot_time);
1121 legacy = nfsd4_cltrack_legacy_topdir();
1122 nfsd4_umh_cltrack_upcall("gracedone", timestr, legacy);
1126 static struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = {
1127 .init = nfsd4_umh_cltrack_init,
1129 .create = nfsd4_umh_cltrack_create,
1130 .remove = nfsd4_umh_cltrack_remove,
1131 .check = nfsd4_umh_cltrack_check,
1132 .grace_done = nfsd4_umh_cltrack_grace_done,
1136 nfsd4_client_tracking_init(struct net *net)
1141 /* just run the init if it the method is already decided */
1142 if (client_tracking_ops)
1146 * First, try a UMH upcall. It should succeed or fail quickly, so
1147 * there's little harm in trying that first.
1149 client_tracking_ops = &nfsd4_umh_tracking_ops;
1150 status = client_tracking_ops->init(net);
1155 * See if the recoverydir exists and is a directory. If it is,
1156 * then use the legacy ops.
1158 client_tracking_ops = &nfsd4_legacy_tracking_ops;
1159 status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path);
1161 status = S_ISDIR(path.dentry->d_inode->i_mode);
1167 /* Finally, try to use nfsdcld */
1168 client_tracking_ops = &nfsd4_cld_tracking_ops;
1169 printk(KERN_WARNING "NFSD: the nfsdcld client tracking upcall will be "
1170 "removed in 3.10. Please transition to using "
1173 status = client_tracking_ops->init(net);
1175 printk(KERN_WARNING "NFSD: Unable to initialize client "
1176 "recovery tracking! (%d)\n", status);
1177 client_tracking_ops = NULL;
1183 nfsd4_client_tracking_exit(struct net *net)
1185 if (client_tracking_ops) {
1186 if (client_tracking_ops->exit)
1187 client_tracking_ops->exit(net);
1188 client_tracking_ops = NULL;
1193 nfsd4_client_record_create(struct nfs4_client *clp)
1195 if (client_tracking_ops)
1196 client_tracking_ops->create(clp);
1200 nfsd4_client_record_remove(struct nfs4_client *clp)
1202 if (client_tracking_ops)
1203 client_tracking_ops->remove(clp);
1207 nfsd4_client_record_check(struct nfs4_client *clp)
1209 if (client_tracking_ops)
1210 return client_tracking_ops->check(clp);
1216 nfsd4_record_grace_done(struct net *net, time_t boot_time)
1218 if (client_tracking_ops)
1219 client_tracking_ops->grace_done(net, boot_time);
1223 rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr)
1225 struct super_block *sb = ptr;
1226 struct net *net = sb->s_fs_info;
1227 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1228 struct cld_net *cn = nn->cld_net;
1229 struct dentry *dentry;
1232 if (!try_module_get(THIS_MODULE))
1236 module_put(THIS_MODULE);
1241 case RPC_PIPEFS_MOUNT:
1242 dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe);
1243 if (IS_ERR(dentry)) {
1244 ret = PTR_ERR(dentry);
1247 cn->cn_pipe->dentry = dentry;
1249 case RPC_PIPEFS_UMOUNT:
1250 if (cn->cn_pipe->dentry)
1251 nfsd4_cld_unregister_sb(cn->cn_pipe);
1257 module_put(THIS_MODULE);
1261 static struct notifier_block nfsd4_cld_block = {
1262 .notifier_call = rpc_pipefs_event,
1266 register_cld_notifier(void)
1268 return rpc_pipefs_notifier_register(&nfsd4_cld_block);
1272 unregister_cld_notifier(void)
1274 rpc_pipefs_notifier_unregister(&nfsd4_cld_block);