]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/nfsd/nfs4recover.c
[PATCH] knfsd: nfsd4: reboot recovery
[mirror_ubuntu-bionic-kernel.git] / fs / nfsd / nfs4recover.c
1 /*
2 * linux/fs/nfsd/nfs4recover.c
3 *
4 * Copyright (c) 2004 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Andy Adamson <andros@citi.umich.edu>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36
37 #include <linux/sunrpc/svc.h>
38 #include <linux/nfsd/nfsd.h>
39 #include <linux/nfs4.h>
40 #include <linux/nfsd/state.h>
41 #include <linux/nfsd/xdr4.h>
42 #include <linux/param.h>
43 #include <linux/file.h>
44 #include <linux/namei.h>
45 #include <asm/uaccess.h>
46 #include <asm/scatterlist.h>
47 #include <linux/crypto.h>
48
49
50 #define NFSDDBG_FACILITY NFSDDBG_PROC
51
52 /* Globals */
53 char recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
54 static struct nameidata rec_dir;
55 static int rec_dir_init = 0;
56
57 static void
58 nfs4_save_user(uid_t *saveuid, gid_t *savegid)
59 {
60 *saveuid = current->fsuid;
61 *savegid = current->fsgid;
62 current->fsuid = 0;
63 current->fsgid = 0;
64 }
65
66 static void
67 nfs4_reset_user(uid_t saveuid, gid_t savegid)
68 {
69 current->fsuid = saveuid;
70 current->fsgid = savegid;
71 }
72
73 static void
74 md5_to_hex(char *out, char *md5)
75 {
76 int i;
77
78 for (i=0; i<16; i++) {
79 unsigned char c = md5[i];
80
81 *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
82 *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
83 }
84 *out = '\0';
85 }
86
87 int
88 nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
89 {
90 struct xdr_netobj cksum;
91 struct crypto_tfm *tfm;
92 struct scatterlist sg[1];
93 int status = nfserr_resource;
94
95 dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
96 clname->len, clname->data);
97 tfm = crypto_alloc_tfm("md5", 0);
98 if (tfm == NULL)
99 goto out;
100 cksum.len = crypto_tfm_alg_digestsize(tfm);
101 cksum.data = kmalloc(cksum.len, GFP_KERNEL);
102 if (cksum.data == NULL)
103 goto out;
104 crypto_digest_init(tfm);
105
106 sg[0].page = virt_to_page(clname->data);
107 sg[0].offset = offset_in_page(clname->data);
108 sg[0].length = clname->len;
109
110 crypto_digest_update(tfm, sg, 1);
111 crypto_digest_final(tfm, cksum.data);
112
113 md5_to_hex(dname, cksum.data);
114
115 kfree(cksum.data);
116 status = nfs_ok;
117 out:
118 if (tfm)
119 crypto_free_tfm(tfm);
120 return status;
121 }
122
123 static int
124 nfsd4_rec_fsync(struct dentry *dentry)
125 {
126 struct file *filp;
127 int status = nfs_ok;
128
129 dprintk("NFSD: nfs4_fsync_rec_dir\n");
130 filp = dentry_open(dget(dentry), mntget(rec_dir.mnt), O_RDWR);
131 if (IS_ERR(filp)) {
132 status = PTR_ERR(filp);
133 goto out;
134 }
135 if (filp->f_op && filp->f_op->fsync)
136 status = filp->f_op->fsync(filp, filp->f_dentry, 0);
137 fput(filp);
138 out:
139 if (status)
140 printk("nfsd4: unable to sync recovery directory\n");
141 return status;
142 }
143
144 int
145 nfsd4_create_clid_dir(struct nfs4_client *clp)
146 {
147 char *dname = clp->cl_recdir;
148 struct dentry *dentry;
149 uid_t uid;
150 gid_t gid;
151 int status;
152
153 dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
154
155 if (!rec_dir_init || clp->cl_firststate)
156 return 0;
157
158 nfs4_save_user(&uid, &gid);
159
160 /* lock the parent */
161 down(&rec_dir.dentry->d_inode->i_sem);
162
163 dentry = lookup_one_len(dname, rec_dir.dentry, HEXDIR_LEN-1);
164 if (IS_ERR(dentry)) {
165 status = PTR_ERR(dentry);
166 goto out_unlock;
167 }
168 status = -EEXIST;
169 if (dentry->d_inode) {
170 dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
171 goto out_put;
172 }
173 status = vfs_mkdir(rec_dir.dentry->d_inode, dentry, S_IRWXU);
174 out_put:
175 dput(dentry);
176 out_unlock:
177 up(&rec_dir.dentry->d_inode->i_sem);
178 if (status == 0) {
179 clp->cl_firststate = 1;
180 status = nfsd4_rec_fsync(rec_dir.dentry);
181 }
182 nfs4_reset_user(uid, gid);
183 dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status);
184 return status;
185 }
186
187 typedef int (recdir_func)(struct dentry *, struct dentry *);
188
189 struct dentry_list {
190 struct dentry *dentry;
191 struct list_head list;
192 };
193
194 struct dentry_list_arg {
195 struct list_head dentries;
196 struct dentry *parent;
197 };
198
199 static int
200 nfsd4_build_dentrylist(void *arg, const char *name, int namlen,
201 loff_t offset, ino_t ino, unsigned int d_type)
202 {
203 struct dentry_list_arg *dla = arg;
204 struct list_head *dentries = &dla->dentries;
205 struct dentry *parent = dla->parent;
206 struct dentry *dentry;
207 struct dentry_list *child;
208
209 if (name && isdotent(name, namlen))
210 return nfs_ok;
211 dentry = lookup_one_len(name, parent, namlen);
212 if (IS_ERR(dentry))
213 return PTR_ERR(dentry);
214 child = kmalloc(sizeof(*child), GFP_KERNEL);
215 if (child == NULL)
216 return -ENOMEM;
217 child->dentry = dentry;
218 list_add(&child->list, dentries);
219 return 0;
220 }
221
222 static int
223 nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
224 {
225 struct file *filp;
226 struct dentry_list_arg dla = {
227 .parent = dir,
228 };
229 struct list_head *dentries = &dla.dentries;
230 struct dentry_list *child;
231 uid_t uid;
232 gid_t gid;
233 int status;
234
235 if (!rec_dir_init)
236 return 0;
237
238 nfs4_save_user(&uid, &gid);
239
240 filp = dentry_open(dget(dir), mntget(rec_dir.mnt),
241 O_RDWR);
242 status = PTR_ERR(filp);
243 if (IS_ERR(filp))
244 goto out;
245 INIT_LIST_HEAD(dentries);
246 status = vfs_readdir(filp, nfsd4_build_dentrylist, &dla);
247 fput(filp);
248 while (!list_empty(dentries)) {
249 child = list_entry(dentries->next, struct dentry_list, list);
250 status = f(dir, child->dentry);
251 if (status)
252 goto out;
253 list_del(&child->list);
254 dput(child->dentry);
255 kfree(child);
256 }
257 out:
258 while (!list_empty(dentries)) {
259 child = list_entry(dentries->next, struct dentry_list, list);
260 list_del(&child->list);
261 dput(child->dentry);
262 kfree(child);
263 }
264 nfs4_reset_user(uid, gid);
265 return status;
266 }
267
268 static int
269 nfsd4_remove_clid_file(struct dentry *dir, struct dentry *dentry)
270 {
271 int status;
272
273 if (!S_ISREG(dir->d_inode->i_mode)) {
274 printk("nfsd4: non-file found in client recovery directory\n");
275 return -EINVAL;
276 }
277 down(&dir->d_inode->i_sem);
278 status = vfs_unlink(dir->d_inode, dentry);
279 up(&dir->d_inode->i_sem);
280 return status;
281 }
282
283 static int
284 nfsd4_clear_clid_dir(struct dentry *dir, struct dentry *dentry)
285 {
286 int status;
287
288 /* For now this directory should already be empty, but we empty it of
289 * any regular files anyway, just in case the directory was created by
290 * a kernel from the future.... */
291 nfsd4_list_rec_dir(dentry, nfsd4_remove_clid_file);
292 down(&dir->d_inode->i_sem);
293 status = vfs_rmdir(dir->d_inode, dentry);
294 up(&dir->d_inode->i_sem);
295 return status;
296 }
297
298 static int
299 nfsd4_unlink_clid_dir(char *name, int namlen)
300 {
301 struct dentry *dentry;
302 int status;
303
304 dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
305
306 dentry = lookup_one_len(name, rec_dir.dentry, namlen);
307 if (IS_ERR(dentry)) {
308 status = PTR_ERR(dentry);
309 return status;
310 }
311 status = -ENOENT;
312 if (!dentry->d_inode)
313 goto out;
314
315 status = nfsd4_clear_clid_dir(rec_dir.dentry, dentry);
316 out:
317 dput(dentry);
318 return status;
319 }
320
321 void
322 nfsd4_remove_clid_dir(struct nfs4_client *clp)
323 {
324 uid_t uid;
325 gid_t gid;
326 int status;
327
328 if (!rec_dir_init || !clp->cl_firststate)
329 return;
330
331 nfs4_save_user(&uid, &gid);
332 status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
333 nfs4_reset_user(uid, gid);
334 if (status == 0)
335 status = nfsd4_rec_fsync(rec_dir.dentry);
336 if (status)
337 printk("NFSD: Failed to remove expired client state directory"
338 " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
339 return;
340 }
341
342 static int
343 purge_old(struct dentry *parent, struct dentry *child)
344 {
345 int status;
346
347 if (nfs4_has_reclaimed_state(child->d_name.name))
348 return nfs_ok;
349
350 status = nfsd4_clear_clid_dir(parent, child);
351 if (status)
352 printk("failed to remove client recovery directory %s\n",
353 child->d_name.name);
354 /* Keep trying, success or failure: */
355 return nfs_ok;
356 }
357
358 void
359 nfsd4_recdir_purge_old(void) {
360 int status;
361
362 if (!rec_dir_init)
363 return;
364 status = nfsd4_list_rec_dir(rec_dir.dentry, purge_old);
365 if (status == 0)
366 status = nfsd4_rec_fsync(rec_dir.dentry);
367 if (status)
368 printk("nfsd4: failed to purge old clients from recovery"
369 " directory %s\n", rec_dir.dentry->d_name.name);
370 return;
371 }
372
373 static int
374 load_recdir(struct dentry *parent, struct dentry *child)
375 {
376 if (child->d_name.len != HEXDIR_LEN - 1) {
377 printk("nfsd4: illegal name %s in recovery directory\n",
378 child->d_name.name);
379 /* Keep trying; maybe the others are OK: */
380 return nfs_ok;
381 }
382 nfs4_client_to_reclaim(child->d_name.name);
383 return nfs_ok;
384 }
385
386 int
387 nfsd4_recdir_load(void) {
388 int status;
389
390 status = nfsd4_list_rec_dir(rec_dir.dentry, load_recdir);
391 if (status)
392 printk("nfsd4: failed loading clients from recovery"
393 " directory %s\n", rec_dir.dentry->d_name.name);
394 return status;
395 }
396
397 /*
398 * Hold reference to the recovery directory.
399 */
400
401 void
402 nfsd4_init_recdir(char *rec_dirname)
403 {
404 uid_t uid = 0;
405 gid_t gid = 0;
406 int status;
407
408 printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
409 rec_dirname);
410
411 BUG_ON(rec_dir_init);
412
413 nfs4_save_user(&uid, &gid);
414
415 status = path_lookup(rec_dirname, LOOKUP_FOLLOW, &rec_dir);
416 if (status == -ENOENT)
417 printk("NFSD: recovery directory %s doesn't exist\n",
418 rec_dirname);
419
420 if (!status)
421 rec_dir_init = 1;
422 nfs4_reset_user(uid, gid);
423 }
424
425 void
426 nfsd4_shutdown_recdir(void)
427 {
428 if (!rec_dir_init)
429 return;
430 rec_dir_init = 0;
431 path_release(&rec_dir);
432 }