Commit | Line | Data |
---|---|---|
a55370a3 | 1 | /* |
a55370a3 | 2 | * Copyright (c) 2004 The Regents of the University of Michigan. |
f3f80148 | 3 | * Copyright (c) 2012 Jeff Layton <jlayton@redhat.com> |
a55370a3 N |
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 | ||
190e4fbf | 35 | #include <linux/file.h> |
5a0e3ad6 | 36 | #include <linux/slab.h> |
190e4fbf | 37 | #include <linux/namei.h> |
a55370a3 | 38 | #include <linux/crypto.h> |
e8edc6e0 | 39 | #include <linux/sched.h> |
f3f80148 | 40 | #include <linux/fs.h> |
813fd320 | 41 | #include <linux/module.h> |
f3f80148 JL |
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> | |
9a74af21 BH |
46 | |
47 | #include "nfsd.h" | |
48 | #include "state.h" | |
0a3adade | 49 | #include "vfs.h" |
f3f80148 | 50 | #include "netns.h" |
a55370a3 N |
51 | |
52 | #define NFSDDBG_FACILITY NFSDDBG_PROC | |
53 | ||
2a4317c5 JL |
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 *); | |
12760c66 | 61 | void (*grace_done)(struct nfsd_net *, time_t); |
2a4317c5 JL |
62 | }; |
63 | ||
190e4fbf | 64 | /* Globals */ |
48483bf2 | 65 | static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery"; |
2a4317c5 | 66 | static struct nfsd4_client_tracking_ops *client_tracking_ops; |
190e4fbf | 67 | |
d84f4f99 DH |
68 | static int |
69 | nfs4_save_creds(const struct cred **original_creds) | |
190e4fbf | 70 | { |
d84f4f99 DH |
71 | struct cred *new; |
72 | ||
73 | new = prepare_creds(); | |
74 | if (!new) | |
75 | return -ENOMEM; | |
76 | ||
77 | new->fsuid = 0; | |
78 | new->fsgid = 0; | |
79 | *original_creds = override_creds(new); | |
80 | put_cred(new); | |
81 | return 0; | |
190e4fbf N |
82 | } |
83 | ||
84 | static void | |
d84f4f99 | 85 | nfs4_reset_creds(const struct cred *original) |
190e4fbf | 86 | { |
d84f4f99 | 87 | revert_creds(original); |
190e4fbf N |
88 | } |
89 | ||
a55370a3 N |
90 | static void |
91 | md5_to_hex(char *out, char *md5) | |
92 | { | |
93 | int i; | |
94 | ||
95 | for (i=0; i<16; i++) { | |
96 | unsigned char c = md5[i]; | |
97 | ||
98 | *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1); | |
99 | *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1); | |
100 | } | |
101 | *out = '\0'; | |
102 | } | |
103 | ||
2216d449 JL |
104 | static int |
105 | nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname) | |
a55370a3 N |
106 | { |
107 | struct xdr_netobj cksum; | |
35058687 | 108 | struct hash_desc desc; |
60c74f81 | 109 | struct scatterlist sg; |
2216d449 | 110 | int status; |
a55370a3 N |
111 | |
112 | dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n", | |
113 | clname->len, clname->data); | |
35058687 HX |
114 | desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
115 | desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC); | |
2216d449 JL |
116 | if (IS_ERR(desc.tfm)) { |
117 | status = PTR_ERR(desc.tfm); | |
35058687 | 118 | goto out_no_tfm; |
2216d449 JL |
119 | } |
120 | ||
35058687 | 121 | cksum.len = crypto_hash_digestsize(desc.tfm); |
a55370a3 | 122 | cksum.data = kmalloc(cksum.len, GFP_KERNEL); |
2216d449 JL |
123 | if (cksum.data == NULL) { |
124 | status = -ENOMEM; | |
a55370a3 | 125 | goto out; |
2216d449 | 126 | } |
a55370a3 | 127 | |
60c74f81 | 128 | sg_init_one(&sg, clname->data, clname->len); |
a55370a3 | 129 | |
2216d449 JL |
130 | status = crypto_hash_digest(&desc, &sg, sg.length, cksum.data); |
131 | if (status) | |
35058687 | 132 | goto out; |
a55370a3 N |
133 | |
134 | md5_to_hex(dname, cksum.data); | |
135 | ||
2216d449 | 136 | status = 0; |
a55370a3 | 137 | out: |
2bd9e7b6 | 138 | kfree(cksum.data); |
35058687 HX |
139 | crypto_free_hash(desc.tfm); |
140 | out_no_tfm: | |
a55370a3 N |
141 | return status; |
142 | } | |
190e4fbf | 143 | |
2216d449 JL |
144 | /* |
145 | * If we had an error generating the recdir name for the legacy tracker | |
146 | * then warn the admin. If the error doesn't appear to be transient, | |
147 | * then disable recovery tracking. | |
148 | */ | |
149 | static void | |
150 | legacy_recdir_name_error(int error) | |
151 | { | |
152 | printk(KERN_ERR "NFSD: unable to generate recoverydir " | |
153 | "name (%d).\n", error); | |
154 | ||
155 | /* | |
156 | * if the algorithm just doesn't exist, then disable the recovery | |
157 | * tracker altogether. The crypto libs will generally return this if | |
158 | * FIPS is enabled as well. | |
159 | */ | |
160 | if (error == -ENOENT) { | |
161 | printk(KERN_ERR "NFSD: disabling legacy clientid tracking. " | |
162 | "Reboot recovery will not function correctly!\n"); | |
163 | ||
164 | /* the argument is ignored by the legacy exit function */ | |
165 | nfsd4_client_tracking_exit(NULL); | |
166 | } | |
167 | } | |
168 | ||
2a4317c5 JL |
169 | static void |
170 | nfsd4_create_clid_dir(struct nfs4_client *clp) | |
c7b9a459 | 171 | { |
d84f4f99 | 172 | const struct cred *original_cred; |
2216d449 | 173 | char dname[HEXDIR_LEN]; |
e970a573 | 174 | struct dentry *dir, *dentry; |
0ce0c2b5 | 175 | struct nfs4_client_reclaim *crp; |
c7b9a459 | 176 | int status; |
52e19c09 | 177 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
c7b9a459 N |
178 | |
179 | dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname); | |
180 | ||
a52d726b | 181 | if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) |
7a6ef8c7 | 182 | return; |
3a073369 | 183 | if (!nn->rec_file) |
7a6ef8c7 | 184 | return; |
2216d449 JL |
185 | |
186 | status = nfs4_make_rec_clidname(dname, &clp->cl_name); | |
187 | if (status) | |
188 | return legacy_recdir_name_error(status); | |
189 | ||
d84f4f99 DH |
190 | status = nfs4_save_creds(&original_cred); |
191 | if (status < 0) | |
7a6ef8c7 | 192 | return; |
c7b9a459 | 193 | |
3a073369 | 194 | status = mnt_want_write_file(nn->rec_file); |
4a55c101 JK |
195 | if (status) |
196 | return; | |
197 | ||
3a073369 | 198 | dir = nn->rec_file->f_path.dentry; |
c7b9a459 | 199 | /* lock the parent */ |
e970a573 | 200 | mutex_lock(&dir->d_inode->i_mutex); |
c7b9a459 | 201 | |
e970a573 | 202 | dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1); |
c7b9a459 N |
203 | if (IS_ERR(dentry)) { |
204 | status = PTR_ERR(dentry); | |
205 | goto out_unlock; | |
206 | } | |
6577aac0 | 207 | if (dentry->d_inode) |
aec39680 BF |
208 | /* |
209 | * In the 4.1 case, where we're called from | |
210 | * reclaim_complete(), records from the previous reboot | |
211 | * may still be left, so this is OK. | |
212 | * | |
213 | * In the 4.0 case, we should never get here; but we may | |
214 | * as well be forgiving and just succeed silently. | |
215 | */ | |
c7b9a459 | 216 | goto out_put; |
e970a573 | 217 | status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU); |
c7b9a459 N |
218 | out_put: |
219 | dput(dentry); | |
220 | out_unlock: | |
e970a573 | 221 | mutex_unlock(&dir->d_inode->i_mutex); |
0ce0c2b5 | 222 | if (status == 0) { |
f141f79d | 223 | if (nn->in_grace) { |
52e19c09 | 224 | crp = nfs4_client_to_reclaim(dname, nn); |
0ce0c2b5 JL |
225 | if (crp) |
226 | crp->cr_clp = clp; | |
227 | } | |
3a073369 | 228 | vfs_fsync(nn->rec_file, 0); |
0ce0c2b5 | 229 | } else { |
6577aac0 BH |
230 | printk(KERN_ERR "NFSD: failed to write recovery record" |
231 | " (err %d); please check that %s exists" | |
232 | " and is writeable", status, | |
233 | user_recovery_dirname); | |
0ce0c2b5 | 234 | } |
3a073369 | 235 | mnt_drop_write_file(nn->rec_file); |
d84f4f99 | 236 | nfs4_reset_creds(original_cred); |
c7b9a459 N |
237 | } |
238 | ||
52e19c09 | 239 | typedef int (recdir_func)(struct dentry *, struct dentry *, struct nfsd_net *); |
190e4fbf | 240 | |
05f4f678 BF |
241 | struct name_list { |
242 | char name[HEXDIR_LEN]; | |
190e4fbf N |
243 | struct list_head list; |
244 | }; | |
245 | ||
190e4fbf | 246 | static int |
05f4f678 | 247 | nfsd4_build_namelist(void *arg, const char *name, int namlen, |
afefdbb2 | 248 | loff_t offset, u64 ino, unsigned int d_type) |
190e4fbf | 249 | { |
05f4f678 BF |
250 | struct list_head *names = arg; |
251 | struct name_list *entry; | |
190e4fbf | 252 | |
05f4f678 | 253 | if (namlen != HEXDIR_LEN - 1) |
b37ad28b | 254 | return 0; |
05f4f678 BF |
255 | entry = kmalloc(sizeof(struct name_list), GFP_KERNEL); |
256 | if (entry == NULL) | |
190e4fbf | 257 | return -ENOMEM; |
05f4f678 BF |
258 | memcpy(entry->name, name, HEXDIR_LEN - 1); |
259 | entry->name[HEXDIR_LEN - 1] = '\0'; | |
260 | list_add(&entry->list, names); | |
190e4fbf N |
261 | return 0; |
262 | } | |
263 | ||
264 | static int | |
52e19c09 | 265 | nfsd4_list_rec_dir(recdir_func *f, struct nfsd_net *nn) |
190e4fbf | 266 | { |
d84f4f99 | 267 | const struct cred *original_cred; |
3a073369 | 268 | struct dentry *dir = nn->rec_file->f_path.dentry; |
05f4f678 | 269 | LIST_HEAD(names); |
190e4fbf N |
270 | int status; |
271 | ||
d84f4f99 DH |
272 | status = nfs4_save_creds(&original_cred); |
273 | if (status < 0) | |
274 | return status; | |
190e4fbf | 275 | |
3a073369 | 276 | status = vfs_llseek(nn->rec_file, 0, SEEK_SET); |
5b4b299c AV |
277 | if (status < 0) { |
278 | nfs4_reset_creds(original_cred); | |
279 | return status; | |
280 | } | |
281 | ||
3a073369 | 282 | status = vfs_readdir(nn->rec_file, nfsd4_build_namelist, &names); |
8daed1e5 | 283 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT); |
05f4f678 | 284 | while (!list_empty(&names)) { |
5b4b299c | 285 | struct name_list *entry; |
05f4f678 | 286 | entry = list_entry(names.next, struct name_list, list); |
5b4b299c AV |
287 | if (!status) { |
288 | struct dentry *dentry; | |
289 | dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1); | |
290 | if (IS_ERR(dentry)) { | |
291 | status = PTR_ERR(dentry); | |
292 | break; | |
293 | } | |
52e19c09 | 294 | status = f(dir, dentry, nn); |
5b4b299c | 295 | dput(dentry); |
05f4f678 | 296 | } |
05f4f678 BF |
297 | list_del(&entry->list); |
298 | kfree(entry); | |
190e4fbf | 299 | } |
2f9092e1 | 300 | mutex_unlock(&dir->d_inode->i_mutex); |
d84f4f99 | 301 | nfs4_reset_creds(original_cred); |
190e4fbf N |
302 | return status; |
303 | } | |
304 | ||
c7b9a459 | 305 | static int |
3a073369 | 306 | nfsd4_unlink_clid_dir(char *name, int namlen, struct nfsd_net *nn) |
c7b9a459 | 307 | { |
e970a573 | 308 | struct dentry *dir, *dentry; |
c7b9a459 N |
309 | int status; |
310 | ||
311 | dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name); | |
312 | ||
3a073369 | 313 | dir = nn->rec_file->f_path.dentry; |
e970a573 CH |
314 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT); |
315 | dentry = lookup_one_len(name, dir, namlen); | |
c7b9a459 N |
316 | if (IS_ERR(dentry)) { |
317 | status = PTR_ERR(dentry); | |
2f9092e1 | 318 | goto out_unlock; |
c7b9a459 N |
319 | } |
320 | status = -ENOENT; | |
321 | if (!dentry->d_inode) | |
322 | goto out; | |
e970a573 | 323 | status = vfs_rmdir(dir->d_inode, dentry); |
c7b9a459 N |
324 | out: |
325 | dput(dentry); | |
2f9092e1 | 326 | out_unlock: |
e970a573 | 327 | mutex_unlock(&dir->d_inode->i_mutex); |
c7b9a459 N |
328 | return status; |
329 | } | |
330 | ||
2a4317c5 | 331 | static void |
c7b9a459 N |
332 | nfsd4_remove_clid_dir(struct nfs4_client *clp) |
333 | { | |
d84f4f99 | 334 | const struct cred *original_cred; |
0ce0c2b5 | 335 | struct nfs4_client_reclaim *crp; |
2216d449 | 336 | char dname[HEXDIR_LEN]; |
c7b9a459 | 337 | int status; |
52e19c09 | 338 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
c7b9a459 | 339 | |
3a073369 | 340 | if (!nn->rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) |
c7b9a459 N |
341 | return; |
342 | ||
2216d449 JL |
343 | status = nfs4_make_rec_clidname(dname, &clp->cl_name); |
344 | if (status) | |
345 | return legacy_recdir_name_error(status); | |
346 | ||
3a073369 | 347 | status = mnt_want_write_file(nn->rec_file); |
0622753b DH |
348 | if (status) |
349 | goto out; | |
a52d726b | 350 | clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); |
d84f4f99 DH |
351 | |
352 | status = nfs4_save_creds(&original_cred); | |
353 | if (status < 0) | |
698d8d87 | 354 | goto out_drop_write; |
d84f4f99 | 355 | |
3a073369 | 356 | status = nfsd4_unlink_clid_dir(dname, HEXDIR_LEN-1, nn); |
d84f4f99 | 357 | nfs4_reset_creds(original_cred); |
0ce0c2b5 | 358 | if (status == 0) { |
3a073369 | 359 | vfs_fsync(nn->rec_file, 0); |
f141f79d | 360 | if (nn->in_grace) { |
0ce0c2b5 | 361 | /* remove reclaim record */ |
52e19c09 | 362 | crp = nfsd4_find_reclaim_client(dname, nn); |
0ce0c2b5 | 363 | if (crp) |
52e19c09 | 364 | nfs4_remove_reclaim_record(crp, nn); |
0ce0c2b5 JL |
365 | } |
366 | } | |
698d8d87 | 367 | out_drop_write: |
3a073369 | 368 | mnt_drop_write_file(nn->rec_file); |
0622753b | 369 | out: |
c7b9a459 N |
370 | if (status) |
371 | printk("NFSD: Failed to remove expired client state directory" | |
2216d449 | 372 | " %.*s\n", HEXDIR_LEN, dname); |
c7b9a459 N |
373 | } |
374 | ||
375 | static int | |
52e19c09 | 376 | purge_old(struct dentry *parent, struct dentry *child, struct nfsd_net *nn) |
c7b9a459 N |
377 | { |
378 | int status; | |
379 | ||
52e19c09 | 380 | if (nfs4_has_reclaimed_state(child->d_name.name, nn)) |
b37ad28b | 381 | return 0; |
c7b9a459 | 382 | |
2f9092e1 | 383 | status = vfs_rmdir(parent->d_inode, child); |
c7b9a459 N |
384 | if (status) |
385 | printk("failed to remove client recovery directory %s\n", | |
386 | child->d_name.name); | |
387 | /* Keep trying, success or failure: */ | |
b37ad28b | 388 | return 0; |
c7b9a459 N |
389 | } |
390 | ||
2a4317c5 | 391 | static void |
12760c66 | 392 | nfsd4_recdir_purge_old(struct nfsd_net *nn, time_t boot_time) |
2a4317c5 | 393 | { |
c7b9a459 N |
394 | int status; |
395 | ||
f141f79d | 396 | nn->in_grace = false; |
3a073369 | 397 | if (!nn->rec_file) |
c7b9a459 | 398 | return; |
3a073369 | 399 | status = mnt_want_write_file(nn->rec_file); |
0622753b DH |
400 | if (status) |
401 | goto out; | |
52e19c09 | 402 | status = nfsd4_list_rec_dir(purge_old, nn); |
c7b9a459 | 403 | if (status == 0) |
3a073369 SK |
404 | vfs_fsync(nn->rec_file, 0); |
405 | mnt_drop_write_file(nn->rec_file); | |
0622753b | 406 | out: |
52e19c09 | 407 | nfs4_release_reclaim(nn); |
c7b9a459 N |
408 | if (status) |
409 | printk("nfsd4: failed to purge old clients from recovery" | |
3a073369 | 410 | " directory %s\n", nn->rec_file->f_path.dentry->d_name.name); |
c7b9a459 N |
411 | } |
412 | ||
190e4fbf | 413 | static int |
52e19c09 | 414 | load_recdir(struct dentry *parent, struct dentry *child, struct nfsd_net *nn) |
190e4fbf N |
415 | { |
416 | if (child->d_name.len != HEXDIR_LEN - 1) { | |
417 | printk("nfsd4: illegal name %s in recovery directory\n", | |
418 | child->d_name.name); | |
419 | /* Keep trying; maybe the others are OK: */ | |
b37ad28b | 420 | return 0; |
190e4fbf | 421 | } |
52e19c09 | 422 | nfs4_client_to_reclaim(child->d_name.name, nn); |
b37ad28b | 423 | return 0; |
190e4fbf N |
424 | } |
425 | ||
2a4317c5 | 426 | static int |
52e19c09 | 427 | nfsd4_recdir_load(struct net *net) { |
190e4fbf | 428 | int status; |
52e19c09 | 429 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
190e4fbf | 430 | |
3a073369 | 431 | if (!nn->rec_file) |
e970a573 CH |
432 | return 0; |
433 | ||
52e19c09 | 434 | status = nfsd4_list_rec_dir(load_recdir, nn); |
190e4fbf N |
435 | if (status) |
436 | printk("nfsd4: failed loading clients from recovery" | |
3a073369 | 437 | " directory %s\n", nn->rec_file->f_path.dentry->d_name.name); |
190e4fbf N |
438 | return status; |
439 | } | |
440 | ||
441 | /* | |
442 | * Hold reference to the recovery directory. | |
443 | */ | |
444 | ||
2a4317c5 | 445 | static int |
3a073369 | 446 | nfsd4_init_recdir(struct net *net) |
190e4fbf | 447 | { |
3a073369 | 448 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
d84f4f99 DH |
449 | const struct cred *original_cred; |
450 | int status; | |
190e4fbf N |
451 | |
452 | printk("NFSD: Using %s as the NFSv4 state recovery directory\n", | |
48483bf2 | 453 | user_recovery_dirname); |
190e4fbf | 454 | |
3a073369 | 455 | BUG_ON(nn->rec_file); |
190e4fbf | 456 | |
d84f4f99 DH |
457 | status = nfs4_save_creds(&original_cred); |
458 | if (status < 0) { | |
459 | printk("NFSD: Unable to change credentials to find recovery" | |
460 | " directory: error %d\n", | |
461 | status); | |
2a4317c5 | 462 | return status; |
d84f4f99 | 463 | } |
190e4fbf | 464 | |
3a073369 SK |
465 | nn->rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0); |
466 | if (IS_ERR(nn->rec_file)) { | |
c2642ab0 | 467 | printk("NFSD: unable to find recovery directory %s\n", |
48483bf2 | 468 | user_recovery_dirname); |
3a073369 SK |
469 | status = PTR_ERR(nn->rec_file); |
470 | nn->rec_file = NULL; | |
e970a573 | 471 | } |
190e4fbf | 472 | |
d84f4f99 | 473 | nfs4_reset_creds(original_cred); |
0ce0c2b5 | 474 | if (!status) |
f141f79d | 475 | nn->in_grace = true; |
2a4317c5 | 476 | return status; |
190e4fbf N |
477 | } |
478 | ||
52e19c09 SK |
479 | |
480 | static int | |
481 | nfs4_legacy_state_init(struct net *net) | |
482 | { | |
483 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
484 | int i; | |
485 | ||
486 | nn->reclaim_str_hashtbl = kmalloc(sizeof(struct list_head) * | |
487 | CLIENT_HASH_SIZE, GFP_KERNEL); | |
488 | if (!nn->reclaim_str_hashtbl) | |
489 | return -ENOMEM; | |
490 | ||
491 | for (i = 0; i < CLIENT_HASH_SIZE; i++) | |
492 | INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]); | |
493 | nn->reclaim_str_hashtbl_size = 0; | |
494 | ||
495 | return 0; | |
496 | } | |
497 | ||
498 | static void | |
499 | nfs4_legacy_state_shutdown(struct net *net) | |
500 | { | |
501 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
502 | ||
503 | kfree(nn->reclaim_str_hashtbl); | |
504 | } | |
505 | ||
2a4317c5 JL |
506 | static int |
507 | nfsd4_load_reboot_recovery_data(struct net *net) | |
508 | { | |
509 | int status; | |
510 | ||
3a073369 | 511 | status = nfsd4_init_recdir(net); |
52e19c09 SK |
512 | if (!status) |
513 | status = nfsd4_recdir_load(net); | |
52e19c09 SK |
514 | if (status) |
515 | printk(KERN_ERR "NFSD: Failure reading reboot recovery data\n"); | |
516 | return status; | |
517 | } | |
518 | ||
519 | static int | |
520 | nfsd4_legacy_tracking_init(struct net *net) | |
521 | { | |
522 | int status; | |
523 | ||
cc27e0d4 JL |
524 | /* XXX: The legacy code won't work in a container */ |
525 | if (net != &init_net) { | |
526 | WARN(1, KERN_ERR "NFSD: attempt to initialize legacy client " | |
527 | "tracking in a container!\n"); | |
528 | return -EINVAL; | |
529 | } | |
530 | ||
52e19c09 | 531 | status = nfs4_legacy_state_init(net); |
2a4317c5 | 532 | if (status) |
52e19c09 SK |
533 | return status; |
534 | ||
535 | status = nfsd4_load_reboot_recovery_data(net); | |
536 | if (status) | |
537 | goto err; | |
538 | return 0; | |
539 | ||
540 | err: | |
541 | nfs4_legacy_state_shutdown(net); | |
2a4317c5 JL |
542 | return status; |
543 | } | |
544 | ||
545 | static void | |
3a073369 | 546 | nfsd4_shutdown_recdir(struct nfsd_net *nn) |
190e4fbf | 547 | { |
3a073369 | 548 | if (!nn->rec_file) |
190e4fbf | 549 | return; |
3a073369 SK |
550 | fput(nn->rec_file); |
551 | nn->rec_file = NULL; | |
190e4fbf | 552 | } |
48483bf2 | 553 | |
2a4317c5 JL |
554 | static void |
555 | nfsd4_legacy_tracking_exit(struct net *net) | |
556 | { | |
52e19c09 SK |
557 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
558 | ||
559 | nfs4_release_reclaim(nn); | |
3a073369 | 560 | nfsd4_shutdown_recdir(nn); |
52e19c09 | 561 | nfs4_legacy_state_shutdown(net); |
2a4317c5 JL |
562 | } |
563 | ||
48483bf2 BF |
564 | /* |
565 | * Change the NFSv4 recovery directory to recdir. | |
566 | */ | |
567 | int | |
568 | nfs4_reset_recoverydir(char *recdir) | |
569 | { | |
570 | int status; | |
571 | struct path path; | |
572 | ||
573 | status = kern_path(recdir, LOOKUP_FOLLOW, &path); | |
574 | if (status) | |
575 | return status; | |
576 | status = -ENOTDIR; | |
577 | if (S_ISDIR(path.dentry->d_inode->i_mode)) { | |
578 | strcpy(user_recovery_dirname, recdir); | |
579 | status = 0; | |
580 | } | |
581 | path_put(&path); | |
582 | return status; | |
583 | } | |
584 | ||
585 | char * | |
586 | nfs4_recoverydir(void) | |
587 | { | |
588 | return user_recovery_dirname; | |
589 | } | |
2a4317c5 JL |
590 | |
591 | static int | |
592 | nfsd4_check_legacy_client(struct nfs4_client *clp) | |
593 | { | |
2216d449 JL |
594 | int status; |
595 | char dname[HEXDIR_LEN]; | |
0ce0c2b5 | 596 | struct nfs4_client_reclaim *crp; |
52e19c09 | 597 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
0ce0c2b5 | 598 | |
2a4317c5 JL |
599 | /* did we already find that this client is stable? */ |
600 | if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) | |
601 | return 0; | |
602 | ||
2216d449 JL |
603 | status = nfs4_make_rec_clidname(dname, &clp->cl_name); |
604 | if (status) { | |
605 | legacy_recdir_name_error(status); | |
606 | return status; | |
607 | } | |
608 | ||
2a4317c5 | 609 | /* look for it in the reclaim hashtable otherwise */ |
52e19c09 | 610 | crp = nfsd4_find_reclaim_client(dname, nn); |
0ce0c2b5 | 611 | if (crp) { |
2a4317c5 | 612 | set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); |
0ce0c2b5 | 613 | crp->cr_clp = clp; |
2a4317c5 JL |
614 | return 0; |
615 | } | |
616 | ||
617 | return -ENOENT; | |
618 | } | |
619 | ||
620 | static struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = { | |
52e19c09 | 621 | .init = nfsd4_legacy_tracking_init, |
2a4317c5 JL |
622 | .exit = nfsd4_legacy_tracking_exit, |
623 | .create = nfsd4_create_clid_dir, | |
624 | .remove = nfsd4_remove_clid_dir, | |
625 | .check = nfsd4_check_legacy_client, | |
626 | .grace_done = nfsd4_recdir_purge_old, | |
627 | }; | |
628 | ||
f3f80148 JL |
629 | /* Globals */ |
630 | #define NFSD_PIPE_DIR "nfsd" | |
631 | #define NFSD_CLD_PIPE "cld" | |
632 | ||
633 | /* per-net-ns structure for holding cld upcall info */ | |
634 | struct cld_net { | |
635 | struct rpc_pipe *cn_pipe; | |
636 | spinlock_t cn_lock; | |
637 | struct list_head cn_list; | |
638 | unsigned int cn_xid; | |
639 | }; | |
640 | ||
641 | struct cld_upcall { | |
642 | struct list_head cu_list; | |
643 | struct cld_net *cu_net; | |
644 | struct task_struct *cu_task; | |
645 | struct cld_msg cu_msg; | |
646 | }; | |
647 | ||
648 | static int | |
649 | __cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg) | |
650 | { | |
651 | int ret; | |
652 | struct rpc_pipe_msg msg; | |
653 | ||
654 | memset(&msg, 0, sizeof(msg)); | |
655 | msg.data = cmsg; | |
656 | msg.len = sizeof(*cmsg); | |
657 | ||
658 | /* | |
659 | * Set task state before we queue the upcall. That prevents | |
660 | * wake_up_process in the downcall from racing with schedule. | |
661 | */ | |
662 | set_current_state(TASK_UNINTERRUPTIBLE); | |
663 | ret = rpc_queue_upcall(pipe, &msg); | |
664 | if (ret < 0) { | |
665 | set_current_state(TASK_RUNNING); | |
666 | goto out; | |
667 | } | |
668 | ||
669 | schedule(); | |
670 | set_current_state(TASK_RUNNING); | |
671 | ||
672 | if (msg.errno < 0) | |
673 | ret = msg.errno; | |
674 | out: | |
675 | return ret; | |
676 | } | |
677 | ||
678 | static int | |
679 | cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg) | |
680 | { | |
681 | int ret; | |
682 | ||
683 | /* | |
684 | * -EAGAIN occurs when pipe is closed and reopened while there are | |
685 | * upcalls queued. | |
686 | */ | |
687 | do { | |
688 | ret = __cld_pipe_upcall(pipe, cmsg); | |
689 | } while (ret == -EAGAIN); | |
690 | ||
691 | return ret; | |
692 | } | |
693 | ||
694 | static ssize_t | |
695 | cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen) | |
696 | { | |
697 | struct cld_upcall *tmp, *cup; | |
bc1b542b | 698 | struct cld_msg __user *cmsg = (struct cld_msg __user *)src; |
f3f80148 JL |
699 | uint32_t xid; |
700 | struct nfsd_net *nn = net_generic(filp->f_dentry->d_sb->s_fs_info, | |
701 | nfsd_net_id); | |
702 | struct cld_net *cn = nn->cld_net; | |
703 | ||
704 | if (mlen != sizeof(*cmsg)) { | |
8a7dc4b0 | 705 | dprintk("%s: got %zu bytes, expected %zu\n", __func__, mlen, |
f3f80148 JL |
706 | sizeof(*cmsg)); |
707 | return -EINVAL; | |
708 | } | |
709 | ||
710 | /* copy just the xid so we can try to find that */ | |
711 | if (copy_from_user(&xid, &cmsg->cm_xid, sizeof(xid)) != 0) { | |
712 | dprintk("%s: error when copying xid from userspace", __func__); | |
713 | return -EFAULT; | |
714 | } | |
715 | ||
716 | /* walk the list and find corresponding xid */ | |
717 | cup = NULL; | |
718 | spin_lock(&cn->cn_lock); | |
719 | list_for_each_entry(tmp, &cn->cn_list, cu_list) { | |
720 | if (get_unaligned(&tmp->cu_msg.cm_xid) == xid) { | |
721 | cup = tmp; | |
722 | list_del_init(&cup->cu_list); | |
723 | break; | |
724 | } | |
725 | } | |
726 | spin_unlock(&cn->cn_lock); | |
727 | ||
728 | /* couldn't find upcall? */ | |
729 | if (!cup) { | |
21f72c9f | 730 | dprintk("%s: couldn't find upcall -- xid=%u\n", __func__, xid); |
f3f80148 JL |
731 | return -EINVAL; |
732 | } | |
733 | ||
734 | if (copy_from_user(&cup->cu_msg, src, mlen) != 0) | |
735 | return -EFAULT; | |
736 | ||
737 | wake_up_process(cup->cu_task); | |
738 | return mlen; | |
739 | } | |
740 | ||
741 | static void | |
742 | cld_pipe_destroy_msg(struct rpc_pipe_msg *msg) | |
743 | { | |
744 | struct cld_msg *cmsg = msg->data; | |
745 | struct cld_upcall *cup = container_of(cmsg, struct cld_upcall, | |
746 | cu_msg); | |
747 | ||
748 | /* errno >= 0 means we got a downcall */ | |
749 | if (msg->errno >= 0) | |
750 | return; | |
751 | ||
752 | wake_up_process(cup->cu_task); | |
753 | } | |
754 | ||
755 | static const struct rpc_pipe_ops cld_upcall_ops = { | |
756 | .upcall = rpc_pipe_generic_upcall, | |
757 | .downcall = cld_pipe_downcall, | |
758 | .destroy_msg = cld_pipe_destroy_msg, | |
759 | }; | |
760 | ||
761 | static struct dentry * | |
762 | nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe) | |
763 | { | |
764 | struct dentry *dir, *dentry; | |
765 | ||
766 | dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR); | |
767 | if (dir == NULL) | |
768 | return ERR_PTR(-ENOENT); | |
769 | dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe); | |
770 | dput(dir); | |
771 | return dentry; | |
772 | } | |
773 | ||
774 | static void | |
775 | nfsd4_cld_unregister_sb(struct rpc_pipe *pipe) | |
776 | { | |
777 | if (pipe->dentry) | |
778 | rpc_unlink(pipe->dentry); | |
779 | } | |
780 | ||
781 | static struct dentry * | |
782 | nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe) | |
783 | { | |
784 | struct super_block *sb; | |
785 | struct dentry *dentry; | |
786 | ||
787 | sb = rpc_get_sb_net(net); | |
788 | if (!sb) | |
789 | return NULL; | |
790 | dentry = nfsd4_cld_register_sb(sb, pipe); | |
791 | rpc_put_sb_net(net); | |
792 | return dentry; | |
793 | } | |
794 | ||
795 | static void | |
796 | nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe) | |
797 | { | |
798 | struct super_block *sb; | |
799 | ||
800 | sb = rpc_get_sb_net(net); | |
801 | if (sb) { | |
802 | nfsd4_cld_unregister_sb(pipe); | |
803 | rpc_put_sb_net(net); | |
804 | } | |
805 | } | |
806 | ||
807 | /* Initialize rpc_pipefs pipe for communication with client tracking daemon */ | |
808 | static int | |
809 | nfsd4_init_cld_pipe(struct net *net) | |
810 | { | |
811 | int ret; | |
812 | struct dentry *dentry; | |
813 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
814 | struct cld_net *cn; | |
815 | ||
816 | if (nn->cld_net) | |
817 | return 0; | |
818 | ||
819 | cn = kzalloc(sizeof(*cn), GFP_KERNEL); | |
820 | if (!cn) { | |
821 | ret = -ENOMEM; | |
822 | goto err; | |
823 | } | |
824 | ||
825 | cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN); | |
826 | if (IS_ERR(cn->cn_pipe)) { | |
827 | ret = PTR_ERR(cn->cn_pipe); | |
828 | goto err; | |
829 | } | |
830 | spin_lock_init(&cn->cn_lock); | |
831 | INIT_LIST_HEAD(&cn->cn_list); | |
832 | ||
833 | dentry = nfsd4_cld_register_net(net, cn->cn_pipe); | |
834 | if (IS_ERR(dentry)) { | |
835 | ret = PTR_ERR(dentry); | |
836 | goto err_destroy_data; | |
837 | } | |
838 | ||
839 | cn->cn_pipe->dentry = dentry; | |
840 | nn->cld_net = cn; | |
841 | return 0; | |
842 | ||
843 | err_destroy_data: | |
844 | rpc_destroy_pipe_data(cn->cn_pipe); | |
845 | err: | |
846 | kfree(cn); | |
847 | printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n", | |
848 | ret); | |
849 | return ret; | |
850 | } | |
851 | ||
852 | static void | |
853 | nfsd4_remove_cld_pipe(struct net *net) | |
854 | { | |
855 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
856 | struct cld_net *cn = nn->cld_net; | |
857 | ||
858 | nfsd4_cld_unregister_net(net, cn->cn_pipe); | |
859 | rpc_destroy_pipe_data(cn->cn_pipe); | |
860 | kfree(nn->cld_net); | |
861 | nn->cld_net = NULL; | |
862 | } | |
863 | ||
864 | static struct cld_upcall * | |
865 | alloc_cld_upcall(struct cld_net *cn) | |
866 | { | |
867 | struct cld_upcall *new, *tmp; | |
868 | ||
869 | new = kzalloc(sizeof(*new), GFP_KERNEL); | |
870 | if (!new) | |
871 | return new; | |
872 | ||
873 | /* FIXME: hard cap on number in flight? */ | |
874 | restart_search: | |
875 | spin_lock(&cn->cn_lock); | |
876 | list_for_each_entry(tmp, &cn->cn_list, cu_list) { | |
877 | if (tmp->cu_msg.cm_xid == cn->cn_xid) { | |
878 | cn->cn_xid++; | |
879 | spin_unlock(&cn->cn_lock); | |
880 | goto restart_search; | |
881 | } | |
882 | } | |
883 | new->cu_task = current; | |
884 | new->cu_msg.cm_vers = CLD_UPCALL_VERSION; | |
885 | put_unaligned(cn->cn_xid++, &new->cu_msg.cm_xid); | |
886 | new->cu_net = cn; | |
887 | list_add(&new->cu_list, &cn->cn_list); | |
888 | spin_unlock(&cn->cn_lock); | |
889 | ||
890 | dprintk("%s: allocated xid %u\n", __func__, new->cu_msg.cm_xid); | |
891 | ||
892 | return new; | |
893 | } | |
894 | ||
895 | static void | |
896 | free_cld_upcall(struct cld_upcall *victim) | |
897 | { | |
898 | struct cld_net *cn = victim->cu_net; | |
899 | ||
900 | spin_lock(&cn->cn_lock); | |
901 | list_del(&victim->cu_list); | |
902 | spin_unlock(&cn->cn_lock); | |
903 | kfree(victim); | |
904 | } | |
905 | ||
906 | /* Ask daemon to create a new record */ | |
907 | static void | |
908 | nfsd4_cld_create(struct nfs4_client *clp) | |
909 | { | |
910 | int ret; | |
911 | struct cld_upcall *cup; | |
c212cecf | 912 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
f3f80148 JL |
913 | struct cld_net *cn = nn->cld_net; |
914 | ||
915 | /* Don't upcall if it's already stored */ | |
916 | if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) | |
917 | return; | |
918 | ||
919 | cup = alloc_cld_upcall(cn); | |
920 | if (!cup) { | |
921 | ret = -ENOMEM; | |
922 | goto out_err; | |
923 | } | |
924 | ||
925 | cup->cu_msg.cm_cmd = Cld_Create; | |
926 | cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len; | |
927 | memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data, | |
928 | clp->cl_name.len); | |
929 | ||
930 | ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg); | |
931 | if (!ret) { | |
932 | ret = cup->cu_msg.cm_status; | |
933 | set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); | |
934 | } | |
935 | ||
936 | free_cld_upcall(cup); | |
937 | out_err: | |
938 | if (ret) | |
939 | printk(KERN_ERR "NFSD: Unable to create client " | |
940 | "record on stable storage: %d\n", ret); | |
941 | } | |
942 | ||
943 | /* Ask daemon to create a new record */ | |
944 | static void | |
945 | nfsd4_cld_remove(struct nfs4_client *clp) | |
946 | { | |
947 | int ret; | |
948 | struct cld_upcall *cup; | |
c212cecf | 949 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
f3f80148 JL |
950 | struct cld_net *cn = nn->cld_net; |
951 | ||
952 | /* Don't upcall if it's already removed */ | |
953 | if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) | |
954 | return; | |
955 | ||
956 | cup = alloc_cld_upcall(cn); | |
957 | if (!cup) { | |
958 | ret = -ENOMEM; | |
959 | goto out_err; | |
960 | } | |
961 | ||
962 | cup->cu_msg.cm_cmd = Cld_Remove; | |
963 | cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len; | |
964 | memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data, | |
965 | clp->cl_name.len); | |
966 | ||
967 | ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg); | |
968 | if (!ret) { | |
969 | ret = cup->cu_msg.cm_status; | |
970 | clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); | |
971 | } | |
972 | ||
973 | free_cld_upcall(cup); | |
974 | out_err: | |
975 | if (ret) | |
976 | printk(KERN_ERR "NFSD: Unable to remove client " | |
977 | "record from stable storage: %d\n", ret); | |
978 | } | |
979 | ||
980 | /* Check for presence of a record, and update its timestamp */ | |
981 | static int | |
982 | nfsd4_cld_check(struct nfs4_client *clp) | |
983 | { | |
984 | int ret; | |
985 | struct cld_upcall *cup; | |
c212cecf | 986 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
f3f80148 JL |
987 | struct cld_net *cn = nn->cld_net; |
988 | ||
989 | /* Don't upcall if one was already stored during this grace pd */ | |
990 | if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) | |
991 | return 0; | |
992 | ||
993 | cup = alloc_cld_upcall(cn); | |
994 | if (!cup) { | |
995 | printk(KERN_ERR "NFSD: Unable to check client record on " | |
996 | "stable storage: %d\n", -ENOMEM); | |
997 | return -ENOMEM; | |
998 | } | |
999 | ||
1000 | cup->cu_msg.cm_cmd = Cld_Check; | |
1001 | cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len; | |
1002 | memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data, | |
1003 | clp->cl_name.len); | |
1004 | ||
1005 | ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg); | |
1006 | if (!ret) { | |
1007 | ret = cup->cu_msg.cm_status; | |
1008 | set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); | |
1009 | } | |
1010 | ||
1011 | free_cld_upcall(cup); | |
1012 | return ret; | |
1013 | } | |
1014 | ||
1015 | static void | |
12760c66 | 1016 | nfsd4_cld_grace_done(struct nfsd_net *nn, time_t boot_time) |
f3f80148 JL |
1017 | { |
1018 | int ret; | |
1019 | struct cld_upcall *cup; | |
f3f80148 JL |
1020 | struct cld_net *cn = nn->cld_net; |
1021 | ||
1022 | cup = alloc_cld_upcall(cn); | |
1023 | if (!cup) { | |
1024 | ret = -ENOMEM; | |
1025 | goto out_err; | |
1026 | } | |
1027 | ||
1028 | cup->cu_msg.cm_cmd = Cld_GraceDone; | |
1029 | cup->cu_msg.cm_u.cm_gracetime = (int64_t)boot_time; | |
1030 | ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg); | |
1031 | if (!ret) | |
1032 | ret = cup->cu_msg.cm_status; | |
1033 | ||
1034 | free_cld_upcall(cup); | |
1035 | out_err: | |
1036 | if (ret) | |
1037 | printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret); | |
1038 | } | |
1039 | ||
1040 | static struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = { | |
1041 | .init = nfsd4_init_cld_pipe, | |
1042 | .exit = nfsd4_remove_cld_pipe, | |
1043 | .create = nfsd4_cld_create, | |
1044 | .remove = nfsd4_cld_remove, | |
1045 | .check = nfsd4_cld_check, | |
1046 | .grace_done = nfsd4_cld_grace_done, | |
1047 | }; | |
1048 | ||
2873d214 JL |
1049 | /* upcall via usermodehelper */ |
1050 | static char cltrack_prog[PATH_MAX] = "/sbin/nfsdcltrack"; | |
1051 | module_param_string(cltrack_prog, cltrack_prog, sizeof(cltrack_prog), | |
1052 | S_IRUGO|S_IWUSR); | |
1053 | MODULE_PARM_DESC(cltrack_prog, "Path to the nfsdcltrack upcall program"); | |
1054 | ||
f3aa7e24 JL |
1055 | static bool cltrack_legacy_disable; |
1056 | module_param(cltrack_legacy_disable, bool, S_IRUGO|S_IWUSR); | |
1057 | MODULE_PARM_DESC(cltrack_legacy_disable, | |
1058 | "Disable legacy recoverydir conversion. Default: false"); | |
1059 | ||
1060 | #define LEGACY_TOPDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_TOPDIR=" | |
1061 | #define LEGACY_RECDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_RECDIR=" | |
1062 | ||
1063 | static char * | |
1064 | nfsd4_cltrack_legacy_topdir(void) | |
1065 | { | |
1066 | int copied; | |
1067 | size_t len; | |
1068 | char *result; | |
1069 | ||
1070 | if (cltrack_legacy_disable) | |
1071 | return NULL; | |
1072 | ||
1073 | len = strlen(LEGACY_TOPDIR_ENV_PREFIX) + | |
1074 | strlen(nfs4_recoverydir()) + 1; | |
1075 | ||
1076 | result = kmalloc(len, GFP_KERNEL); | |
1077 | if (!result) | |
1078 | return result; | |
1079 | ||
1080 | copied = snprintf(result, len, LEGACY_TOPDIR_ENV_PREFIX "%s", | |
1081 | nfs4_recoverydir()); | |
1082 | if (copied >= len) { | |
1083 | /* just return nothing if output was truncated */ | |
1084 | kfree(result); | |
1085 | return NULL; | |
1086 | } | |
1087 | ||
1088 | return result; | |
1089 | } | |
1090 | ||
1091 | static char * | |
2216d449 | 1092 | nfsd4_cltrack_legacy_recdir(const struct xdr_netobj *name) |
f3aa7e24 JL |
1093 | { |
1094 | int copied; | |
1095 | size_t len; | |
1096 | char *result; | |
1097 | ||
1098 | if (cltrack_legacy_disable) | |
1099 | return NULL; | |
1100 | ||
1101 | /* +1 is for '/' between "topdir" and "recdir" */ | |
1102 | len = strlen(LEGACY_RECDIR_ENV_PREFIX) + | |
1103 | strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN; | |
1104 | ||
1105 | result = kmalloc(len, GFP_KERNEL); | |
1106 | if (!result) | |
1107 | return result; | |
1108 | ||
2216d449 JL |
1109 | copied = snprintf(result, len, LEGACY_RECDIR_ENV_PREFIX "%s/", |
1110 | nfs4_recoverydir()); | |
1111 | if (copied > (len - HEXDIR_LEN)) { | |
1112 | /* just return nothing if output will be truncated */ | |
1113 | kfree(result); | |
1114 | return NULL; | |
1115 | } | |
1116 | ||
1117 | copied = nfs4_make_rec_clidname(result + copied, name); | |
1118 | if (copied) { | |
f3aa7e24 JL |
1119 | kfree(result); |
1120 | return NULL; | |
1121 | } | |
1122 | ||
1123 | return result; | |
1124 | } | |
1125 | ||
2873d214 | 1126 | static int |
f3aa7e24 | 1127 | nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *legacy) |
2873d214 | 1128 | { |
f3aa7e24 | 1129 | char *envp[2]; |
2873d214 JL |
1130 | char *argv[4]; |
1131 | int ret; | |
1132 | ||
1133 | if (unlikely(!cltrack_prog[0])) { | |
1134 | dprintk("%s: cltrack_prog is disabled\n", __func__); | |
1135 | return -EACCES; | |
1136 | } | |
1137 | ||
1138 | dprintk("%s: cmd: %s\n", __func__, cmd); | |
1139 | dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)"); | |
f3aa7e24 JL |
1140 | dprintk("%s: legacy: %s\n", __func__, legacy ? legacy : "(null)"); |
1141 | ||
1142 | envp[0] = legacy; | |
1143 | envp[1] = NULL; | |
2873d214 JL |
1144 | |
1145 | argv[0] = (char *)cltrack_prog; | |
1146 | argv[1] = cmd; | |
1147 | argv[2] = arg; | |
1148 | argv[3] = NULL; | |
1149 | ||
1150 | ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); | |
1151 | /* | |
1152 | * Disable the upcall mechanism if we're getting an ENOENT or EACCES | |
1153 | * error. The admin can re-enable it on the fly by using sysfs | |
1154 | * once the problem has been fixed. | |
1155 | */ | |
1156 | if (ret == -ENOENT || ret == -EACCES) { | |
1157 | dprintk("NFSD: %s was not found or isn't executable (%d). " | |
1158 | "Setting cltrack_prog to blank string!", | |
1159 | cltrack_prog, ret); | |
1160 | cltrack_prog[0] = '\0'; | |
1161 | } | |
1162 | dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret); | |
1163 | ||
1164 | return ret; | |
1165 | } | |
1166 | ||
1167 | static char * | |
1168 | bin_to_hex_dup(const unsigned char *src, int srclen) | |
1169 | { | |
1170 | int i; | |
1171 | char *buf, *hex; | |
1172 | ||
1173 | /* +1 for terminating NULL */ | |
1174 | buf = kmalloc((srclen * 2) + 1, GFP_KERNEL); | |
1175 | if (!buf) | |
1176 | return buf; | |
1177 | ||
1178 | hex = buf; | |
1179 | for (i = 0; i < srclen; i++) { | |
1180 | sprintf(hex, "%2.2x", *src++); | |
1181 | hex += 2; | |
1182 | } | |
1183 | return buf; | |
1184 | } | |
1185 | ||
1186 | static int | |
1187 | nfsd4_umh_cltrack_init(struct net __attribute__((unused)) *net) | |
1188 | { | |
f3aa7e24 | 1189 | return nfsd4_umh_cltrack_upcall("init", NULL, NULL); |
2873d214 JL |
1190 | } |
1191 | ||
1192 | static void | |
1193 | nfsd4_umh_cltrack_create(struct nfs4_client *clp) | |
1194 | { | |
1195 | char *hexid; | |
1196 | ||
1197 | hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len); | |
1198 | if (!hexid) { | |
1199 | dprintk("%s: can't allocate memory for upcall!\n", __func__); | |
1200 | return; | |
1201 | } | |
f3aa7e24 | 1202 | nfsd4_umh_cltrack_upcall("create", hexid, NULL); |
2873d214 JL |
1203 | kfree(hexid); |
1204 | } | |
1205 | ||
1206 | static void | |
1207 | nfsd4_umh_cltrack_remove(struct nfs4_client *clp) | |
1208 | { | |
1209 | char *hexid; | |
1210 | ||
1211 | hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len); | |
1212 | if (!hexid) { | |
1213 | dprintk("%s: can't allocate memory for upcall!\n", __func__); | |
1214 | return; | |
1215 | } | |
f3aa7e24 | 1216 | nfsd4_umh_cltrack_upcall("remove", hexid, NULL); |
2873d214 JL |
1217 | kfree(hexid); |
1218 | } | |
1219 | ||
1220 | static int | |
1221 | nfsd4_umh_cltrack_check(struct nfs4_client *clp) | |
1222 | { | |
1223 | int ret; | |
f3aa7e24 | 1224 | char *hexid, *legacy; |
2873d214 JL |
1225 | |
1226 | hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len); | |
1227 | if (!hexid) { | |
1228 | dprintk("%s: can't allocate memory for upcall!\n", __func__); | |
1229 | return -ENOMEM; | |
1230 | } | |
2216d449 | 1231 | legacy = nfsd4_cltrack_legacy_recdir(&clp->cl_name); |
f3aa7e24 JL |
1232 | ret = nfsd4_umh_cltrack_upcall("check", hexid, legacy); |
1233 | kfree(legacy); | |
2873d214 JL |
1234 | kfree(hexid); |
1235 | return ret; | |
1236 | } | |
1237 | ||
1238 | static void | |
12760c66 | 1239 | nfsd4_umh_cltrack_grace_done(struct nfsd_net __attribute__((unused)) *nn, |
2873d214 JL |
1240 | time_t boot_time) |
1241 | { | |
f3aa7e24 | 1242 | char *legacy; |
2873d214 JL |
1243 | char timestr[22]; /* FIXME: better way to determine max size? */ |
1244 | ||
1245 | sprintf(timestr, "%ld", boot_time); | |
f3aa7e24 JL |
1246 | legacy = nfsd4_cltrack_legacy_topdir(); |
1247 | nfsd4_umh_cltrack_upcall("gracedone", timestr, legacy); | |
1248 | kfree(legacy); | |
2873d214 JL |
1249 | } |
1250 | ||
1251 | static struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = { | |
1252 | .init = nfsd4_umh_cltrack_init, | |
1253 | .exit = NULL, | |
1254 | .create = nfsd4_umh_cltrack_create, | |
1255 | .remove = nfsd4_umh_cltrack_remove, | |
1256 | .check = nfsd4_umh_cltrack_check, | |
1257 | .grace_done = nfsd4_umh_cltrack_grace_done, | |
1258 | }; | |
1259 | ||
2a4317c5 JL |
1260 | int |
1261 | nfsd4_client_tracking_init(struct net *net) | |
1262 | { | |
1263 | int status; | |
f3f80148 | 1264 | struct path path; |
2a4317c5 | 1265 | |
2d77bf0a JL |
1266 | /* just run the init if it the method is already decided */ |
1267 | if (client_tracking_ops) | |
1268 | goto do_init; | |
1269 | ||
1270 | /* | |
1271 | * First, try a UMH upcall. It should succeed or fail quickly, so | |
1272 | * there's little harm in trying that first. | |
1273 | */ | |
1274 | client_tracking_ops = &nfsd4_umh_tracking_ops; | |
1275 | status = client_tracking_ops->init(net); | |
1276 | if (!status) | |
1277 | return status; | |
1278 | ||
1279 | /* | |
1280 | * See if the recoverydir exists and is a directory. If it is, | |
1281 | * then use the legacy ops. | |
1282 | */ | |
1283 | client_tracking_ops = &nfsd4_legacy_tracking_ops; | |
1284 | status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path); | |
1285 | if (!status) { | |
1286 | status = S_ISDIR(path.dentry->d_inode->i_mode); | |
1287 | path_put(&path); | |
1288 | if (status) | |
1289 | goto do_init; | |
f3f80148 | 1290 | } |
2a4317c5 | 1291 | |
2d77bf0a JL |
1292 | /* Finally, try to use nfsdcld */ |
1293 | client_tracking_ops = &nfsd4_cld_tracking_ops; | |
8b0554e9 JL |
1294 | printk(KERN_WARNING "NFSD: the nfsdcld client tracking upcall will be " |
1295 | "removed in 3.10. Please transition to using " | |
1296 | "nfsdcltrack.\n"); | |
2d77bf0a | 1297 | do_init: |
2a4317c5 JL |
1298 | status = client_tracking_ops->init(net); |
1299 | if (status) { | |
1300 | printk(KERN_WARNING "NFSD: Unable to initialize client " | |
1301 | "recovery tracking! (%d)\n", status); | |
1302 | client_tracking_ops = NULL; | |
1303 | } | |
1304 | return status; | |
1305 | } | |
1306 | ||
1307 | void | |
1308 | nfsd4_client_tracking_exit(struct net *net) | |
1309 | { | |
1310 | if (client_tracking_ops) { | |
2873d214 JL |
1311 | if (client_tracking_ops->exit) |
1312 | client_tracking_ops->exit(net); | |
2a4317c5 JL |
1313 | client_tracking_ops = NULL; |
1314 | } | |
1315 | } | |
1316 | ||
1317 | void | |
1318 | nfsd4_client_record_create(struct nfs4_client *clp) | |
1319 | { | |
1320 | if (client_tracking_ops) | |
1321 | client_tracking_ops->create(clp); | |
1322 | } | |
1323 | ||
1324 | void | |
1325 | nfsd4_client_record_remove(struct nfs4_client *clp) | |
1326 | { | |
1327 | if (client_tracking_ops) | |
1328 | client_tracking_ops->remove(clp); | |
1329 | } | |
1330 | ||
1331 | int | |
1332 | nfsd4_client_record_check(struct nfs4_client *clp) | |
1333 | { | |
1334 | if (client_tracking_ops) | |
1335 | return client_tracking_ops->check(clp); | |
1336 | ||
1337 | return -EOPNOTSUPP; | |
1338 | } | |
1339 | ||
1340 | void | |
12760c66 | 1341 | nfsd4_record_grace_done(struct nfsd_net *nn, time_t boot_time) |
2a4317c5 JL |
1342 | { |
1343 | if (client_tracking_ops) | |
12760c66 | 1344 | client_tracking_ops->grace_done(nn, boot_time); |
2a4317c5 | 1345 | } |
813fd320 JL |
1346 | |
1347 | static int | |
1348 | rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr) | |
1349 | { | |
1350 | struct super_block *sb = ptr; | |
1351 | struct net *net = sb->s_fs_info; | |
1352 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
1353 | struct cld_net *cn = nn->cld_net; | |
1354 | struct dentry *dentry; | |
1355 | int ret = 0; | |
1356 | ||
1357 | if (!try_module_get(THIS_MODULE)) | |
1358 | return 0; | |
1359 | ||
1360 | if (!cn) { | |
1361 | module_put(THIS_MODULE); | |
1362 | return 0; | |
1363 | } | |
1364 | ||
1365 | switch (event) { | |
1366 | case RPC_PIPEFS_MOUNT: | |
1367 | dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe); | |
1368 | if (IS_ERR(dentry)) { | |
1369 | ret = PTR_ERR(dentry); | |
1370 | break; | |
1371 | } | |
1372 | cn->cn_pipe->dentry = dentry; | |
1373 | break; | |
1374 | case RPC_PIPEFS_UMOUNT: | |
1375 | if (cn->cn_pipe->dentry) | |
1376 | nfsd4_cld_unregister_sb(cn->cn_pipe); | |
1377 | break; | |
1378 | default: | |
1379 | ret = -ENOTSUPP; | |
1380 | break; | |
1381 | } | |
1382 | module_put(THIS_MODULE); | |
1383 | return ret; | |
1384 | } | |
1385 | ||
2355c596 | 1386 | static struct notifier_block nfsd4_cld_block = { |
813fd320 JL |
1387 | .notifier_call = rpc_pipefs_event, |
1388 | }; | |
797a9d79 JL |
1389 | |
1390 | int | |
1391 | register_cld_notifier(void) | |
1392 | { | |
1393 | return rpc_pipefs_notifier_register(&nfsd4_cld_block); | |
1394 | } | |
1395 | ||
1396 | void | |
1397 | unregister_cld_notifier(void) | |
1398 | { | |
1399 | rpc_pipefs_notifier_unregister(&nfsd4_cld_block); | |
1400 | } |