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 <crypto/hash.h>
36 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.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 nfsd_net
*);
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
;
69 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
71 static char user_recovery_dirname
[PATH_MAX
] = "/var/lib/nfs/v4recovery";
74 nfs4_save_creds(const struct cred
**original_creds
)
78 new = prepare_creds();
82 new->fsuid
= GLOBAL_ROOT_UID
;
83 new->fsgid
= GLOBAL_ROOT_GID
;
84 *original_creds
= override_creds(new);
90 nfs4_reset_creds(const struct cred
*original
)
92 revert_creds(original
);
96 md5_to_hex(char *out
, char *md5
)
100 for (i
=0; i
<16; i
++) {
101 unsigned char c
= md5
[i
];
103 *out
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
104 *out
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
110 nfs4_make_rec_clidname(char *dname
, const struct xdr_netobj
*clname
)
112 struct xdr_netobj cksum
;
113 struct crypto_shash
*tfm
;
116 dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
117 clname
->len
, clname
->data
);
118 tfm
= crypto_alloc_shash("md5", 0, 0);
120 status
= PTR_ERR(tfm
);
124 cksum
.len
= crypto_shash_digestsize(tfm
);
125 cksum
.data
= kmalloc(cksum
.len
, GFP_KERNEL
);
126 if (cksum
.data
== NULL
) {
131 status
= crypto_shash_tfm_digest(tfm
, clname
->data
, clname
->len
,
136 md5_to_hex(dname
, cksum
.data
);
141 crypto_free_shash(tfm
);
147 * If we had an error generating the recdir name for the legacy tracker
148 * then warn the admin. If the error doesn't appear to be transient,
149 * then disable recovery tracking.
152 legacy_recdir_name_error(struct nfs4_client
*clp
, int error
)
154 printk(KERN_ERR
"NFSD: unable to generate recoverydir "
155 "name (%d).\n", error
);
158 * if the algorithm just doesn't exist, then disable the recovery
159 * tracker altogether. The crypto libs will generally return this if
160 * FIPS is enabled as well.
162 if (error
== -ENOENT
) {
163 printk(KERN_ERR
"NFSD: disabling legacy clientid tracking. "
164 "Reboot recovery will not function correctly!\n");
165 nfsd4_client_tracking_exit(clp
->net
);
170 __nfsd4_create_reclaim_record_grace(struct nfs4_client
*clp
,
171 const char *dname
, int len
, struct nfsd_net
*nn
)
173 struct xdr_netobj name
;
174 struct xdr_netobj princhash
= { .len
= 0, .data
= NULL
};
175 struct nfs4_client_reclaim
*crp
;
177 name
.data
= kmemdup(dname
, len
, GFP_KERNEL
);
179 dprintk("%s: failed to allocate memory for name.data!\n",
184 crp
= nfs4_client_to_reclaim(name
, princhash
, nn
);
193 nfsd4_create_clid_dir(struct nfs4_client
*clp
)
195 const struct cred
*original_cred
;
196 char dname
[HEXDIR_LEN
];
197 struct dentry
*dir
, *dentry
;
199 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
201 if (test_and_set_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
))
206 status
= nfs4_make_rec_clidname(dname
, &clp
->cl_name
);
208 return legacy_recdir_name_error(clp
, status
);
210 status
= nfs4_save_creds(&original_cred
);
214 status
= mnt_want_write_file(nn
->rec_file
);
218 dir
= nn
->rec_file
->f_path
.dentry
;
219 /* lock the parent */
220 inode_lock(d_inode(dir
));
222 dentry
= lookup_one_len(dname
, dir
, HEXDIR_LEN
-1);
223 if (IS_ERR(dentry
)) {
224 status
= PTR_ERR(dentry
);
227 if (d_really_is_positive(dentry
))
229 * In the 4.1 case, where we're called from
230 * reclaim_complete(), records from the previous reboot
231 * may still be left, so this is OK.
233 * In the 4.0 case, we should never get here; but we may
234 * as well be forgiving and just succeed silently.
237 status
= vfs_mkdir(&nop_mnt_idmap
, d_inode(dir
), dentry
, S_IRWXU
);
241 inode_unlock(d_inode(dir
));
244 __nfsd4_create_reclaim_record_grace(clp
, dname
,
246 vfs_fsync(nn
->rec_file
, 0);
248 printk(KERN_ERR
"NFSD: failed to write recovery record"
249 " (err %d); please check that %s exists"
250 " and is writeable", status
,
251 user_recovery_dirname
);
253 mnt_drop_write_file(nn
->rec_file
);
255 nfs4_reset_creds(original_cred
);
258 typedef int (recdir_func
)(struct dentry
*, struct dentry
*, struct nfsd_net
*);
261 char name
[HEXDIR_LEN
];
262 struct list_head list
;
265 struct nfs4_dir_ctx
{
266 struct dir_context ctx
;
267 struct list_head names
;
271 nfsd4_build_namelist(struct dir_context
*__ctx
, const char *name
, int namlen
,
272 loff_t offset
, u64 ino
, unsigned int d_type
)
274 struct nfs4_dir_ctx
*ctx
=
275 container_of(__ctx
, struct nfs4_dir_ctx
, ctx
);
276 struct name_list
*entry
;
278 if (namlen
!= HEXDIR_LEN
- 1)
280 entry
= kmalloc(sizeof(struct name_list
), GFP_KERNEL
);
283 memcpy(entry
->name
, name
, HEXDIR_LEN
- 1);
284 entry
->name
[HEXDIR_LEN
- 1] = '\0';
285 list_add(&entry
->list
, &ctx
->names
);
290 nfsd4_list_rec_dir(recdir_func
*f
, struct nfsd_net
*nn
)
292 const struct cred
*original_cred
;
293 struct dentry
*dir
= nn
->rec_file
->f_path
.dentry
;
294 struct nfs4_dir_ctx ctx
= {
295 .ctx
.actor
= nfsd4_build_namelist
,
296 .names
= LIST_HEAD_INIT(ctx
.names
)
298 struct name_list
*entry
, *tmp
;
301 status
= nfs4_save_creds(&original_cred
);
305 status
= vfs_llseek(nn
->rec_file
, 0, SEEK_SET
);
307 nfs4_reset_creds(original_cred
);
311 status
= iterate_dir(nn
->rec_file
, &ctx
.ctx
);
312 inode_lock_nested(d_inode(dir
), I_MUTEX_PARENT
);
314 list_for_each_entry_safe(entry
, tmp
, &ctx
.names
, list
) {
316 struct dentry
*dentry
;
317 dentry
= lookup_one_len(entry
->name
, dir
, HEXDIR_LEN
-1);
318 if (IS_ERR(dentry
)) {
319 status
= PTR_ERR(dentry
);
322 status
= f(dir
, dentry
, nn
);
325 list_del(&entry
->list
);
328 inode_unlock(d_inode(dir
));
329 nfs4_reset_creds(original_cred
);
331 list_for_each_entry_safe(entry
, tmp
, &ctx
.names
, list
) {
332 dprintk("NFSD: %s. Left entry %s\n", __func__
, entry
->name
);
333 list_del(&entry
->list
);
340 nfsd4_unlink_clid_dir(char *name
, int namlen
, struct nfsd_net
*nn
)
342 struct dentry
*dir
, *dentry
;
345 dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen
, name
);
347 dir
= nn
->rec_file
->f_path
.dentry
;
348 inode_lock_nested(d_inode(dir
), I_MUTEX_PARENT
);
349 dentry
= lookup_one_len(name
, dir
, namlen
);
350 if (IS_ERR(dentry
)) {
351 status
= PTR_ERR(dentry
);
355 if (d_really_is_negative(dentry
))
357 status
= vfs_rmdir(&nop_mnt_idmap
, d_inode(dir
), dentry
);
361 inode_unlock(d_inode(dir
));
366 __nfsd4_remove_reclaim_record_grace(const char *dname
, int len
,
369 struct xdr_netobj name
;
370 struct nfs4_client_reclaim
*crp
;
372 name
.data
= kmemdup(dname
, len
, GFP_KERNEL
);
374 dprintk("%s: failed to allocate memory for name.data!\n",
379 crp
= nfsd4_find_reclaim_client(name
, nn
);
382 nfs4_remove_reclaim_record(crp
, nn
);
386 nfsd4_remove_clid_dir(struct nfs4_client
*clp
)
388 const struct cred
*original_cred
;
389 char dname
[HEXDIR_LEN
];
391 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
393 if (!nn
->rec_file
|| !test_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
))
396 status
= nfs4_make_rec_clidname(dname
, &clp
->cl_name
);
398 return legacy_recdir_name_error(clp
, status
);
400 status
= mnt_want_write_file(nn
->rec_file
);
403 clear_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
);
405 status
= nfs4_save_creds(&original_cred
);
409 status
= nfsd4_unlink_clid_dir(dname
, HEXDIR_LEN
-1, nn
);
410 nfs4_reset_creds(original_cred
);
412 vfs_fsync(nn
->rec_file
, 0);
414 __nfsd4_remove_reclaim_record_grace(dname
,
418 mnt_drop_write_file(nn
->rec_file
);
421 printk("NFSD: Failed to remove expired client state directory"
422 " %.*s\n", HEXDIR_LEN
, dname
);
426 purge_old(struct dentry
*parent
, struct dentry
*child
, struct nfsd_net
*nn
)
429 struct xdr_netobj name
;
431 if (child
->d_name
.len
!= HEXDIR_LEN
- 1) {
432 printk("%s: illegal name %pd in recovery directory\n",
434 /* Keep trying; maybe the others are OK: */
437 name
.data
= kmemdup_nul(child
->d_name
.name
, child
->d_name
.len
, GFP_KERNEL
);
439 dprintk("%s: failed to allocate memory for name.data!\n",
443 name
.len
= HEXDIR_LEN
;
444 if (nfs4_has_reclaimed_state(name
, nn
))
447 status
= vfs_rmdir(&nop_mnt_idmap
, d_inode(parent
), child
);
449 printk("failed to remove client recovery directory %pd\n",
454 /* Keep trying, success or failure: */
459 nfsd4_recdir_purge_old(struct nfsd_net
*nn
)
463 nn
->in_grace
= false;
466 status
= mnt_want_write_file(nn
->rec_file
);
469 status
= nfsd4_list_rec_dir(purge_old
, nn
);
471 vfs_fsync(nn
->rec_file
, 0);
472 mnt_drop_write_file(nn
->rec_file
);
474 nfs4_release_reclaim(nn
);
476 printk("nfsd4: failed to purge old clients from recovery"
477 " directory %pD\n", nn
->rec_file
);
481 load_recdir(struct dentry
*parent
, struct dentry
*child
, struct nfsd_net
*nn
)
483 struct xdr_netobj name
;
484 struct xdr_netobj princhash
= { .len
= 0, .data
= NULL
};
486 if (child
->d_name
.len
!= HEXDIR_LEN
- 1) {
487 printk("%s: illegal name %pd in recovery directory\n",
489 /* Keep trying; maybe the others are OK: */
492 name
.data
= kmemdup_nul(child
->d_name
.name
, child
->d_name
.len
, GFP_KERNEL
);
494 dprintk("%s: failed to allocate memory for name.data!\n",
498 name
.len
= HEXDIR_LEN
;
499 if (!nfs4_client_to_reclaim(name
, princhash
, nn
))
506 nfsd4_recdir_load(struct net
*net
) {
508 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
513 status
= nfsd4_list_rec_dir(load_recdir
, nn
);
515 printk("nfsd4: failed loading clients from recovery"
516 " directory %pD\n", nn
->rec_file
);
521 * Hold reference to the recovery directory.
525 nfsd4_init_recdir(struct net
*net
)
527 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
528 const struct cred
*original_cred
;
531 printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
532 user_recovery_dirname
);
534 BUG_ON(nn
->rec_file
);
536 status
= nfs4_save_creds(&original_cred
);
538 printk("NFSD: Unable to change credentials to find recovery"
539 " directory: error %d\n",
544 nn
->rec_file
= filp_open(user_recovery_dirname
, O_RDONLY
| O_DIRECTORY
, 0);
545 if (IS_ERR(nn
->rec_file
)) {
546 printk("NFSD: unable to find recovery directory %s\n",
547 user_recovery_dirname
);
548 status
= PTR_ERR(nn
->rec_file
);
552 nfs4_reset_creds(original_cred
);
559 nfsd4_shutdown_recdir(struct net
*net
)
561 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
570 nfs4_legacy_state_init(struct net
*net
)
572 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
575 nn
->reclaim_str_hashtbl
= kmalloc_array(CLIENT_HASH_SIZE
,
576 sizeof(struct list_head
),
578 if (!nn
->reclaim_str_hashtbl
)
581 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++)
582 INIT_LIST_HEAD(&nn
->reclaim_str_hashtbl
[i
]);
583 nn
->reclaim_str_hashtbl_size
= 0;
589 nfs4_legacy_state_shutdown(struct net
*net
)
591 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
593 kfree(nn
->reclaim_str_hashtbl
);
597 nfsd4_load_reboot_recovery_data(struct net
*net
)
601 status
= nfsd4_init_recdir(net
);
605 status
= nfsd4_recdir_load(net
);
607 nfsd4_shutdown_recdir(net
);
613 nfsd4_legacy_tracking_init(struct net
*net
)
617 /* XXX: The legacy code won't work in a container */
618 if (net
!= &init_net
) {
619 pr_warn("NFSD: attempt to initialize legacy client tracking in a container ignored.\n");
623 status
= nfs4_legacy_state_init(net
);
627 status
= nfsd4_load_reboot_recovery_data(net
);
630 pr_info("NFSD: Using legacy client tracking operations.\n");
634 nfs4_legacy_state_shutdown(net
);
639 nfsd4_legacy_tracking_exit(struct net
*net
)
641 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
643 nfs4_release_reclaim(nn
);
644 nfsd4_shutdown_recdir(net
);
645 nfs4_legacy_state_shutdown(net
);
649 * Change the NFSv4 recovery directory to recdir.
652 nfs4_reset_recoverydir(char *recdir
)
657 status
= kern_path(recdir
, LOOKUP_FOLLOW
, &path
);
661 if (d_is_dir(path
.dentry
)) {
662 strcpy(user_recovery_dirname
, recdir
);
670 nfs4_recoverydir(void)
672 return user_recovery_dirname
;
676 nfsd4_check_legacy_client(struct nfs4_client
*clp
)
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
;
684 /* did we already find that this client is stable? */
685 if (test_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
))
688 status
= nfs4_make_rec_clidname(dname
, &clp
->cl_name
);
690 legacy_recdir_name_error(clp
, status
);
694 /* look for it in the reclaim hashtable otherwise */
695 name
.data
= kmemdup(dname
, HEXDIR_LEN
, GFP_KERNEL
);
697 dprintk("%s: failed to allocate memory for name.data!\n",
701 name
.len
= HEXDIR_LEN
;
702 crp
= nfsd4_find_reclaim_client(name
, nn
);
705 set_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
);
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
,
724 #endif /* CONFIG_NFSD_LEGACY_CLIENT_TRACKING */
727 #define NFSD_PIPE_DIR "nfsd"
728 #define NFSD_CLD_PIPE "cld"
730 /* per-net-ns structure for holding cld upcall info */
732 struct rpc_pipe
*cn_pipe
;
734 struct list_head cn_list
;
736 struct crypto_shash
*cn_tfm
;
737 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
743 struct list_head cu_list
;
744 struct cld_net
*cu_net
;
745 struct completion cu_done
;
747 struct cld_msg_hdr cu_hdr
;
748 struct cld_msg cu_msg
;
749 struct cld_msg_v2 cu_msg_v2
;
754 __cld_pipe_upcall(struct rpc_pipe
*pipe
, void *cmsg
, struct nfsd_net
*nn
)
757 struct rpc_pipe_msg msg
;
758 struct cld_upcall
*cup
= container_of(cmsg
, struct cld_upcall
, cu_u
);
760 memset(&msg
, 0, sizeof(msg
));
762 msg
.len
= nn
->client_tracking_ops
->msglen
;
764 ret
= rpc_queue_upcall(pipe
, &msg
);
769 wait_for_completion(&cup
->cu_done
);
778 cld_pipe_upcall(struct rpc_pipe
*pipe
, void *cmsg
, struct nfsd_net
*nn
)
783 * -EAGAIN occurs when pipe is closed and reopened while there are
787 ret
= __cld_pipe_upcall(pipe
, cmsg
, nn
);
788 } while (ret
== -EAGAIN
);
794 __cld_pipe_inprogress_downcall(const struct cld_msg_v2 __user
*cmsg
,
797 uint8_t cmd
, princhashlen
;
798 struct xdr_netobj name
, princhash
= { .len
= 0, .data
= NULL
};
801 if (get_user(cmd
, &cmsg
->cm_cmd
)) {
802 dprintk("%s: error when copying cmd from userspace", __func__
);
805 if (cmd
== Cld_GraceStart
) {
806 if (nn
->client_tracking_ops
->version
>= 2) {
807 const struct cld_clntinfo __user
*ci
;
809 ci
= &cmsg
->cm_u
.cm_clntinfo
;
810 if (get_user(namelen
, &ci
->cc_name
.cn_len
))
812 if (namelen
== 0 || namelen
> NFS4_OPAQUE_LIMIT
) {
813 dprintk("%s: invalid namelen (%u)", __func__
, namelen
);
816 name
.data
= memdup_user(&ci
->cc_name
.cn_id
, namelen
);
817 if (IS_ERR(name
.data
))
818 return PTR_ERR(name
.data
);
820 get_user(princhashlen
, &ci
->cc_princhash
.cp_len
);
821 if (princhashlen
> 0) {
822 princhash
.data
= memdup_user(
823 &ci
->cc_princhash
.cp_data
,
825 if (IS_ERR(princhash
.data
)) {
827 return PTR_ERR(princhash
.data
);
829 princhash
.len
= princhashlen
;
833 const struct cld_name __user
*cnm
;
835 cnm
= &cmsg
->cm_u
.cm_name
;
836 if (get_user(namelen
, &cnm
->cn_len
))
838 if (namelen
== 0 || namelen
> NFS4_OPAQUE_LIMIT
) {
839 dprintk("%s: invalid namelen (%u)", __func__
, namelen
);
842 name
.data
= memdup_user(&cnm
->cn_id
, namelen
);
843 if (IS_ERR(name
.data
))
844 return PTR_ERR(name
.data
);
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
;
851 name
.len
= name
.len
- 5;
852 memmove(name
.data
, name
.data
+ 5, name
.len
);
853 cn
->cn_has_legacy
= true;
856 if (!nfs4_client_to_reclaim(name
, princhash
, nn
)) {
858 kfree(princhash
.data
);
861 return nn
->client_tracking_ops
->msglen
;
867 cld_pipe_downcall(struct file
*filp
, const char __user
*src
, size_t mlen
)
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
;
873 struct nfsd_net
*nn
= net_generic(file_inode(filp
)->i_sb
->s_fs_info
,
875 struct cld_net
*cn
= nn
->cld_net
;
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
);
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__
);
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)
895 if (get_user(status
, &hdr
->cm_status
)) {
896 dprintk("%s: error when copying status from userspace", __func__
);
900 /* walk the list and find corresponding xid */
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
) {
906 if (status
!= -EINPROGRESS
)
907 list_del_init(&cup
->cu_list
);
911 spin_unlock(&cn
->cn_lock
);
913 /* couldn't find upcall? */
915 dprintk("%s: couldn't find upcall -- xid=%u\n", __func__
, xid
);
919 if (status
== -EINPROGRESS
)
920 return __cld_pipe_inprogress_downcall(cmsg
, nn
);
922 if (copy_from_user(&cup
->cu_u
.cu_msg_v2
, src
, mlen
) != 0)
925 complete(&cup
->cu_done
);
930 cld_pipe_destroy_msg(struct rpc_pipe_msg
*msg
)
932 struct cld_msg
*cmsg
= msg
->data
;
933 struct cld_upcall
*cup
= container_of(cmsg
, struct cld_upcall
,
936 /* errno >= 0 means we got a downcall */
940 complete(&cup
->cu_done
);
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
,
949 static struct dentry
*
950 nfsd4_cld_register_sb(struct super_block
*sb
, struct rpc_pipe
*pipe
)
952 struct dentry
*dir
, *dentry
;
954 dir
= rpc_d_lookup_sb(sb
, NFSD_PIPE_DIR
);
956 return ERR_PTR(-ENOENT
);
957 dentry
= rpc_mkpipe_dentry(dir
, NFSD_CLD_PIPE
, NULL
, pipe
);
963 nfsd4_cld_unregister_sb(struct rpc_pipe
*pipe
)
966 rpc_unlink(pipe
->dentry
);
969 static struct dentry
*
970 nfsd4_cld_register_net(struct net
*net
, struct rpc_pipe
*pipe
)
972 struct super_block
*sb
;
973 struct dentry
*dentry
;
975 sb
= rpc_get_sb_net(net
);
978 dentry
= nfsd4_cld_register_sb(sb
, pipe
);
984 nfsd4_cld_unregister_net(struct net
*net
, struct rpc_pipe
*pipe
)
986 struct super_block
*sb
;
988 sb
= rpc_get_sb_net(net
);
990 nfsd4_cld_unregister_sb(pipe
);
995 /* Initialize rpc_pipefs pipe for communication with client tracking daemon */
997 __nfsd4_init_cld_pipe(struct net
*net
)
1000 struct dentry
*dentry
;
1001 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1007 cn
= kzalloc(sizeof(*cn
), GFP_KERNEL
);
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
);
1018 spin_lock_init(&cn
->cn_lock
);
1019 INIT_LIST_HEAD(&cn
->cn_list
);
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
;
1027 cn
->cn_pipe
->dentry
= dentry
;
1028 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1029 cn
->cn_has_legacy
= false;
1035 rpc_destroy_pipe_data(cn
->cn_pipe
);
1038 printk(KERN_ERR
"NFSD: unable to create nfsdcld upcall pipe (%d)\n",
1044 nfsd4_init_cld_pipe(struct net
*net
)
1048 status
= __nfsd4_init_cld_pipe(net
);
1050 pr_info("NFSD: Using old nfsdcld client tracking operations.\n");
1055 nfsd4_remove_cld_pipe(struct net
*net
)
1057 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1058 struct cld_net
*cn
= nn
->cld_net
;
1060 nfsd4_cld_unregister_net(net
, cn
->cn_pipe
);
1061 rpc_destroy_pipe_data(cn
->cn_pipe
);
1063 crypto_free_shash(cn
->cn_tfm
);
1068 static struct cld_upcall
*
1069 alloc_cld_upcall(struct nfsd_net
*nn
)
1071 struct cld_upcall
*new, *tmp
;
1072 struct cld_net
*cn
= nn
->cld_net
;
1074 new = kzalloc(sizeof(*new), GFP_KERNEL
);
1078 /* FIXME: hard cap on number in flight? */
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
) {
1084 spin_unlock(&cn
->cn_lock
);
1085 goto restart_search
;
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
);
1092 list_add(&new->cu_list
, &cn
->cn_list
);
1093 spin_unlock(&cn
->cn_lock
);
1095 dprintk("%s: allocated xid %u\n", __func__
, new->cu_u
.cu_msg
.cm_xid
);
1101 free_cld_upcall(struct cld_upcall
*victim
)
1103 struct cld_net
*cn
= victim
->cu_net
;
1105 spin_lock(&cn
->cn_lock
);
1106 list_del(&victim
->cu_list
);
1107 spin_unlock(&cn
->cn_lock
);
1111 /* Ask daemon to create a new record */
1113 nfsd4_cld_create(struct nfs4_client
*clp
)
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
;
1120 /* Don't upcall if it's already stored */
1121 if (test_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
))
1124 cup
= alloc_cld_upcall(nn
);
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
,
1135 ret
= cld_pipe_upcall(cn
->cn_pipe
, &cup
->cu_u
.cu_msg
, nn
);
1137 ret
= cup
->cu_u
.cu_msg
.cm_status
;
1138 set_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
);
1141 free_cld_upcall(cup
);
1144 printk(KERN_ERR
"NFSD: Unable to create client "
1145 "record on stable storage: %d\n", ret
);
1148 /* Ask daemon to create a new record */
1150 nfsd4_cld_create_v2(struct nfs4_client
*clp
)
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
;
1161 /* Don't upcall if it's already stored */
1162 if (test_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
))
1165 cup
= alloc_cld_upcall(nn
);
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
,
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
;
1181 cksum
.len
= crypto_shash_digestsize(tfm
);
1182 cksum
.data
= kmalloc(cksum
.len
, GFP_KERNEL
);
1183 if (cksum
.data
== NULL
) {
1187 ret
= crypto_shash_tfm_digest(tfm
, principal
, strlen(principal
),
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
);
1198 cmsg
->cm_u
.cm_clntinfo
.cc_princhash
.cp_len
= 0;
1200 ret
= cld_pipe_upcall(cn
->cn_pipe
, cmsg
, nn
);
1202 ret
= cmsg
->cm_status
;
1203 set_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
);
1207 free_cld_upcall(cup
);
1210 pr_err("NFSD: Unable to create client record on stable storage: %d\n",
1214 /* Ask daemon to create a new record */
1216 nfsd4_cld_remove(struct nfs4_client
*clp
)
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
;
1223 /* Don't upcall if it's already removed */
1224 if (!test_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
))
1227 cup
= alloc_cld_upcall(nn
);
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
,
1238 ret
= cld_pipe_upcall(cn
->cn_pipe
, &cup
->cu_u
.cu_msg
, nn
);
1240 ret
= cup
->cu_u
.cu_msg
.cm_status
;
1241 clear_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
);
1244 free_cld_upcall(cup
);
1247 printk(KERN_ERR
"NFSD: Unable to remove client "
1248 "record from stable storage: %d\n", ret
);
1252 * For older nfsdcld's that do not allow us to "slurp" the clients
1253 * from the tracking database during startup.
1255 * Check for presence of a record, and update its timestamp
1258 nfsd4_cld_check_v0(struct nfs4_client
*clp
)
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
;
1265 /* Don't upcall if one was already stored during this grace pd */
1266 if (test_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
))
1269 cup
= alloc_cld_upcall(nn
);
1271 printk(KERN_ERR
"NFSD: Unable to check client record on "
1272 "stable storage: %d\n", -ENOMEM
);
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
,
1281 ret
= cld_pipe_upcall(cn
->cn_pipe
, &cup
->cu_u
.cu_msg
, nn
);
1283 ret
= cup
->cu_u
.cu_msg
.cm_status
;
1284 set_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
);
1287 free_cld_upcall(cup
);
1292 * For newer nfsdcld's that allow us to "slurp" the clients
1293 * from the tracking database during startup.
1295 * Check for presence of a record in the reclaim_str_hashtbl
1298 nfsd4_cld_check(struct nfs4_client
*clp
)
1300 struct nfs4_client_reclaim
*crp
;
1301 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1303 /* did we already find that this client is stable? */
1304 if (test_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
))
1307 /* look for it in the reclaim hashtable otherwise */
1308 crp
= nfsd4_find_reclaim_client(clp
->cl_name
, nn
);
1312 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1313 if (nn
->cld_net
->cn_has_legacy
) {
1315 char dname
[HEXDIR_LEN
];
1316 struct xdr_netobj name
;
1318 status
= nfs4_make_rec_clidname(dname
, &clp
->cl_name
);
1322 name
.data
= kmemdup(dname
, HEXDIR_LEN
, GFP_KERNEL
);
1324 dprintk("%s: failed to allocate memory for name.data!\n",
1328 name
.len
= HEXDIR_LEN
;
1329 crp
= nfsd4_find_reclaim_client(name
, nn
);
1343 nfsd4_cld_check_v2(struct nfs4_client
*clp
)
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
;
1349 struct crypto_shash
*tfm
= cn
->cn_tfm
;
1350 struct xdr_netobj cksum
;
1351 char *principal
= NULL
;
1353 /* did we already find that this client is stable? */
1354 if (test_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
))
1357 /* look for it in the reclaim hashtable otherwise */
1358 crp
= nfsd4_find_reclaim_client(clp
->cl_name
, nn
);
1362 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1363 if (cn
->cn_has_legacy
) {
1364 struct xdr_netobj name
;
1365 char dname
[HEXDIR_LEN
];
1367 status
= nfs4_make_rec_clidname(dname
, &clp
->cl_name
);
1371 name
.data
= kmemdup(dname
, HEXDIR_LEN
, GFP_KERNEL
);
1373 dprintk("%s: failed to allocate memory for name.data\n",
1377 name
.len
= HEXDIR_LEN
;
1378 crp
= nfsd4_find_reclaim_client(name
, nn
);
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
)
1394 cksum
.len
= crypto_shash_digestsize(tfm
);
1395 cksum
.data
= kmalloc(cksum
.len
, GFP_KERNEL
);
1396 if (cksum
.data
== NULL
)
1398 status
= crypto_shash_tfm_digest(tfm
, principal
,
1399 strlen(principal
), cksum
.data
);
1404 if (memcmp(crp
->cr_princhash
.data
, cksum
.data
,
1405 crp
->cr_princhash
.len
)) {
1416 nfsd4_cld_grace_start(struct nfsd_net
*nn
)
1419 struct cld_upcall
*cup
;
1420 struct cld_net
*cn
= nn
->cld_net
;
1422 cup
= alloc_cld_upcall(nn
);
1428 cup
->cu_u
.cu_msg
.cm_cmd
= Cld_GraceStart
;
1429 ret
= cld_pipe_upcall(cn
->cn_pipe
, &cup
->cu_u
.cu_msg
, nn
);
1431 ret
= cup
->cu_u
.cu_msg
.cm_status
;
1433 free_cld_upcall(cup
);
1436 dprintk("%s: Unable to get clients from userspace: %d\n",
1441 /* For older nfsdcld's that need cm_gracetime */
1443 nfsd4_cld_grace_done_v0(struct nfsd_net
*nn
)
1446 struct cld_upcall
*cup
;
1447 struct cld_net
*cn
= nn
->cld_net
;
1449 cup
= alloc_cld_upcall(nn
);
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
);
1459 ret
= cup
->cu_u
.cu_msg
.cm_status
;
1461 free_cld_upcall(cup
);
1464 printk(KERN_ERR
"NFSD: Unable to end grace period: %d\n", ret
);
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.
1472 nfsd4_cld_grace_done(struct nfsd_net
*nn
)
1475 struct cld_upcall
*cup
;
1476 struct cld_net
*cn
= nn
->cld_net
;
1478 cup
= alloc_cld_upcall(nn
);
1484 cup
->cu_u
.cu_msg
.cm_cmd
= Cld_GraceDone
;
1485 ret
= cld_pipe_upcall(cn
->cn_pipe
, &cup
->cu_u
.cu_msg
, nn
);
1487 ret
= cup
->cu_u
.cu_msg
.cm_status
;
1489 free_cld_upcall(cup
);
1491 nfs4_release_reclaim(nn
);
1493 printk(KERN_ERR
"NFSD: Unable to end grace period: %d\n", ret
);
1497 nfs4_cld_state_init(struct net
*net
)
1499 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1502 nn
->reclaim_str_hashtbl
= kmalloc_array(CLIENT_HASH_SIZE
,
1503 sizeof(struct list_head
),
1505 if (!nn
->reclaim_str_hashtbl
)
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);
1518 nfs4_cld_state_shutdown(struct net
*net
)
1520 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1522 nn
->track_reclaim_completes
= false;
1523 kfree(nn
->reclaim_str_hashtbl
);
1527 cld_running(struct nfsd_net
*nn
)
1529 struct cld_net
*cn
= nn
->cld_net
;
1530 struct rpc_pipe
*pipe
= cn
->cn_pipe
;
1532 return pipe
->nreaders
|| pipe
->nwriters
;
1536 nfsd4_cld_get_version(struct nfsd_net
*nn
)
1539 struct cld_upcall
*cup
;
1540 struct cld_net
*cn
= nn
->cld_net
;
1543 cup
= alloc_cld_upcall(nn
);
1548 cup
->cu_u
.cu_msg
.cm_cmd
= Cld_GetVersion
;
1549 ret
= cld_pipe_upcall(cn
->cn_pipe
, &cup
->cu_u
.cu_msg
, nn
);
1551 ret
= cup
->cu_u
.cu_msg
.cm_status
;
1554 version
= cup
->cu_u
.cu_msg
.cm_u
.cm_version
;
1555 dprintk("%s: userspace returned version %u\n",
1559 else if (version
> CLD_UPCALL_VERSION
)
1560 version
= CLD_UPCALL_VERSION
;
1564 nn
->client_tracking_ops
= &nfsd4_cld_tracking_ops
;
1567 nn
->client_tracking_ops
= &nfsd4_cld_tracking_ops_v2
;
1574 free_cld_upcall(cup
);
1577 dprintk("%s: Unable to get version from userspace: %d\n",
1583 nfsd4_cld_tracking_init(struct net
*net
)
1586 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1589 struct crypto_shash
*tfm
;
1591 status
= nfs4_cld_state_init(net
);
1595 status
= __nfsd4_init_cld_pipe(net
);
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.
1607 while (!(running
= cld_running(nn
)) && retries
--)
1611 status
= -ETIMEDOUT
;
1614 tfm
= crypto_alloc_shash("sha256", 0, 0);
1616 status
= PTR_ERR(tfm
);
1619 nn
->cld_net
->cn_tfm
= tfm
;
1621 status
= nfsd4_cld_get_version(nn
);
1622 if (status
== -EOPNOTSUPP
)
1623 pr_warn("NFSD: nfsdcld GetVersion upcall failed. Please upgrade nfsdcld.\n");
1625 status
= nfsd4_cld_grace_start(nn
);
1627 if (status
== -EOPNOTSUPP
)
1628 pr_warn("NFSD: nfsdcld GraceStart upcall failed. Please upgrade nfsdcld.\n");
1629 nfs4_release_reclaim(nn
);
1632 pr_info("NFSD: Using nfsdcld client tracking operations.\n");
1636 nfsd4_remove_cld_pipe(net
);
1638 nfs4_cld_state_shutdown(net
);
1643 nfsd4_cld_tracking_exit(struct net
*net
)
1645 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1647 nfs4_release_reclaim(nn
);
1648 nfsd4_remove_cld_pipe(net
);
1649 nfs4_cld_state_shutdown(net
);
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
,
1661 .msglen
= sizeof(struct cld_msg
),
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
,
1673 .msglen
= sizeof(struct cld_msg
),
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
,
1685 .msglen
= sizeof(struct cld_msg_v2
),
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
),
1693 MODULE_PARM_DESC(cltrack_prog
, "Path to the nfsdcltrack upcall program");
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");
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="
1706 nfsd4_cltrack_legacy_topdir(void)
1712 if (cltrack_legacy_disable
)
1715 len
= strlen(LEGACY_TOPDIR_ENV_PREFIX
) +
1716 strlen(nfs4_recoverydir()) + 1;
1718 result
= kmalloc(len
, GFP_KERNEL
);
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 */
1734 nfsd4_cltrack_legacy_recdir(const struct xdr_netobj
*name
)
1740 if (cltrack_legacy_disable
)
1743 /* +1 is for '/' between "topdir" and "recdir" */
1744 len
= strlen(LEGACY_RECDIR_ENV_PREFIX
) +
1745 strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN
;
1747 result
= kmalloc(len
, GFP_KERNEL
);
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 */
1759 copied
= nfs4_make_rec_clidname(result
+ copied
, name
);
1769 nfsd4_cltrack_client_has_session(struct nfs4_client
*clp
)
1775 /* prefix + Y/N character + terminating NULL */
1776 len
= strlen(HAS_SESSION_ENV_PREFIX
) + 1 + 1;
1778 result
= kmalloc(len
, GFP_KERNEL
);
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 */
1794 nfsd4_cltrack_grace_start(time64_t grace_start
)
1800 /* prefix + max width of int64_t string + terminating NULL */
1801 len
= strlen(GRACE_START_ENV_PREFIX
) + 22 + 1;
1803 result
= kmalloc(len
, GFP_KERNEL
);
1807 copied
= snprintf(result
, len
, GRACE_START_ENV_PREFIX
"%lld",
1809 if (copied
>= len
) {
1810 /* just return nothing if output was truncated */
1819 nfsd4_umh_cltrack_upcall(char *cmd
, char *arg
, char *env0
, char *env1
)
1825 if (unlikely(!cltrack_prog
[0])) {
1826 dprintk("%s: cltrack_prog is disabled\n", __func__
);
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)");
1839 argv
[0] = (char *)cltrack_prog
;
1844 ret
= call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_PROC
);
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.
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!",
1854 cltrack_prog
[0] = '\0';
1856 dprintk("%s: %s return value: %d\n", __func__
, cltrack_prog
, ret
);
1862 bin_to_hex_dup(const unsigned char *src
, int srclen
)
1866 /* +1 for terminating NULL */
1867 buf
= kzalloc((srclen
* 2) + 1, GFP_KERNEL
);
1871 bin2hex(buf
, src
, srclen
);
1876 nfsd4_umh_cltrack_init(struct net
*net
)
1879 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1880 char *grace_start
= nfsd4_cltrack_grace_start(nn
->boot_time
);
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");
1889 ret
= nfsd4_umh_cltrack_upcall("init", NULL
, grace_start
, NULL
);
1892 pr_info("NFSD: Using UMH upcall client tracking operations.\n");
1897 nfsd4_cltrack_upcall_lock(struct nfs4_client
*clp
)
1899 wait_on_bit_lock(&clp
->cl_flags
, NFSD4_CLIENT_UPCALL_LOCK
,
1900 TASK_UNINTERRUPTIBLE
);
1904 nfsd4_cltrack_upcall_unlock(struct nfs4_client
*clp
)
1906 clear_and_wake_up_bit(NFSD4_CLIENT_UPCALL_LOCK
, &clp
->cl_flags
);
1910 nfsd4_umh_cltrack_create(struct nfs4_client
*clp
)
1912 char *hexid
, *has_session
, *grace_start
;
1913 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
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.
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.
1927 if (clp
->cl_minorversion
== 0 &&
1928 test_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
))
1931 hexid
= bin_to_hex_dup(clp
->cl_name
.data
, clp
->cl_name
.len
);
1933 dprintk("%s: can't allocate memory for upcall!\n", __func__
);
1937 has_session
= nfsd4_cltrack_client_has_session(clp
);
1938 grace_start
= nfsd4_cltrack_grace_start(nn
->boot_time
);
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
);
1951 nfsd4_umh_cltrack_remove(struct nfs4_client
*clp
)
1955 if (!test_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
))
1958 hexid
= bin_to_hex_dup(clp
->cl_name
.data
, clp
->cl_name
.len
);
1960 dprintk("%s: can't allocate memory for upcall!\n", __func__
);
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
);
1974 nfsd4_umh_cltrack_check(struct nfs4_client
*clp
)
1977 char *hexid
, *has_session
, *legacy
;
1979 if (test_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
))
1982 hexid
= bin_to_hex_dup(clp
->cl_name
.data
, clp
->cl_name
.len
);
1984 dprintk("%s: can't allocate memory for upcall!\n", __func__
);
1988 has_session
= nfsd4_cltrack_client_has_session(clp
);
1989 legacy
= nfsd4_cltrack_legacy_recdir(&clp
->cl_name
);
1991 nfsd4_cltrack_upcall_lock(clp
);
1992 if (test_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
)) {
1995 ret
= nfsd4_umh_cltrack_upcall("check", hexid
, has_session
, legacy
);
1997 set_bit(NFSD4_CLIENT_STABLE
, &clp
->cl_flags
);
1999 nfsd4_cltrack_upcall_unlock(clp
);
2008 nfsd4_umh_cltrack_grace_done(struct nfsd_net
*nn
)
2011 char timestr
[22]; /* FIXME: better way to determine max size? */
2013 sprintf(timestr
, "%lld", nn
->boot_time
);
2014 legacy
= nfsd4_cltrack_legacy_topdir();
2015 nfsd4_umh_cltrack_upcall("gracedone", timestr
, legacy
, NULL
);
2019 static const struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops
= {
2020 .init
= nfsd4_umh_cltrack_init
,
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
,
2030 static inline int check_for_legacy_methods(int status
, struct net
*net
)
2032 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2036 * Next, try the UMH upcall.
2038 nn
->client_tracking_ops
= &nfsd4_umh_tracking_ops
;
2039 status
= nn
->client_tracking_ops
->init(net
);
2044 * Finally, See if the recoverydir exists and is a directory.
2045 * If it is, then use the legacy ops.
2047 nn
->client_tracking_ops
= &nfsd4_legacy_tracking_ops
;
2048 status
= kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW
, &path
);
2050 status
= !d_is_dir(path
.dentry
);
2054 status
= nn
->client_tracking_ops
->init(net
);
2059 static inline int check_for_legacy_methods(int status
, struct net
*net
)
2063 #endif /* CONFIG_LEGACY_NFSD_CLIENT_TRACKING */
2066 nfsd4_client_tracking_init(struct net
*net
)
2068 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2071 /* just run the init if it the method is already decided */
2072 if (nn
->client_tracking_ops
)
2075 /* First, try to use nfsdcld */
2076 nn
->client_tracking_ops
= &nfsd4_cld_tracking_ops
;
2077 status
= nn
->client_tracking_ops
->init(net
);
2080 if (status
!= -ETIMEDOUT
) {
2081 nn
->client_tracking_ops
= &nfsd4_cld_tracking_ops_v0
;
2082 status
= nn
->client_tracking_ops
->init(net
);
2087 status
= check_for_legacy_methods(status
, net
);
2091 status
= nn
->client_tracking_ops
->init(net
);
2094 pr_warn("NFSD: Unable to initialize client recovery tracking! (%d)\n", status
);
2095 pr_warn("NFSD: Is nfsdcld running? If not, enable CONFIG_NFSD_LEGACY_CLIENT_TRACKING.\n");
2096 nn
->client_tracking_ops
= NULL
;
2102 nfsd4_client_tracking_exit(struct net
*net
)
2104 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2106 if (nn
->client_tracking_ops
) {
2107 if (nn
->client_tracking_ops
->exit
)
2108 nn
->client_tracking_ops
->exit(net
);
2109 nn
->client_tracking_ops
= NULL
;
2114 nfsd4_client_record_create(struct nfs4_client
*clp
)
2116 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2118 if (nn
->client_tracking_ops
)
2119 nn
->client_tracking_ops
->create(clp
);
2123 nfsd4_client_record_remove(struct nfs4_client
*clp
)
2125 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2127 if (nn
->client_tracking_ops
)
2128 nn
->client_tracking_ops
->remove(clp
);
2132 nfsd4_client_record_check(struct nfs4_client
*clp
)
2134 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2136 if (nn
->client_tracking_ops
)
2137 return nn
->client_tracking_ops
->check(clp
);
2143 nfsd4_record_grace_done(struct nfsd_net
*nn
)
2145 if (nn
->client_tracking_ops
)
2146 nn
->client_tracking_ops
->grace_done(nn
);
2150 rpc_pipefs_event(struct notifier_block
*nb
, unsigned long event
, void *ptr
)
2152 struct super_block
*sb
= ptr
;
2153 struct net
*net
= sb
->s_fs_info
;
2154 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2155 struct cld_net
*cn
= nn
->cld_net
;
2156 struct dentry
*dentry
;
2159 if (!try_module_get(THIS_MODULE
))
2163 module_put(THIS_MODULE
);
2168 case RPC_PIPEFS_MOUNT
:
2169 dentry
= nfsd4_cld_register_sb(sb
, cn
->cn_pipe
);
2170 if (IS_ERR(dentry
)) {
2171 ret
= PTR_ERR(dentry
);
2174 cn
->cn_pipe
->dentry
= dentry
;
2176 case RPC_PIPEFS_UMOUNT
:
2177 if (cn
->cn_pipe
->dentry
)
2178 nfsd4_cld_unregister_sb(cn
->cn_pipe
);
2184 module_put(THIS_MODULE
);
2188 static struct notifier_block nfsd4_cld_block
= {
2189 .notifier_call
= rpc_pipefs_event
,
2193 register_cld_notifier(void)
2195 WARN_ON(!nfsd_net_id
);
2196 return rpc_pipefs_notifier_register(&nfsd4_cld_block
);
2200 unregister_cld_notifier(void)
2202 rpc_pipefs_notifier_unregister(&nfsd4_cld_block
);