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