]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/nfsd/nfs4recover.c
2 * linux/fs/nfsd/nfs4recover.c
4 * Copyright (c) 2004 The Regents of the University of Michigan.
7 * Andy Adamson <andros@citi.umich.edu>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
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>
50 #define NFSDDBG_FACILITY NFSDDBG_PROC
53 char recovery_dirname
[PATH_MAX
] = "/var/lib/nfs/v4recovery";
54 static struct nameidata rec_dir
;
55 static int rec_dir_init
= 0;
58 nfs4_save_user(uid_t
*saveuid
, gid_t
*savegid
)
60 *saveuid
= current
->fsuid
;
61 *savegid
= current
->fsgid
;
67 nfs4_reset_user(uid_t saveuid
, gid_t savegid
)
69 current
->fsuid
= saveuid
;
70 current
->fsgid
= savegid
;
74 md5_to_hex(char *out
, char *md5
)
78 for (i
=0; i
<16; i
++) {
79 unsigned char c
= md5
[i
];
81 *out
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
82 *out
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
88 nfs4_make_rec_clidname(char *dname
, struct xdr_netobj
*clname
)
90 struct xdr_netobj cksum
;
91 struct crypto_tfm
*tfm
;
92 struct scatterlist sg
[1];
93 int status
= nfserr_resource
;
95 dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
96 clname
->len
, clname
->data
);
97 tfm
= crypto_alloc_tfm("md5", 0);
100 cksum
.len
= crypto_tfm_alg_digestsize(tfm
);
101 cksum
.data
= kmalloc(cksum
.len
, GFP_KERNEL
);
102 if (cksum
.data
== NULL
)
104 crypto_digest_init(tfm
);
106 sg
[0].page
= virt_to_page(clname
->data
);
107 sg
[0].offset
= offset_in_page(clname
->data
);
108 sg
[0].length
= clname
->len
;
110 crypto_digest_update(tfm
, sg
, 1);
111 crypto_digest_final(tfm
, cksum
.data
);
113 md5_to_hex(dname
, cksum
.data
);
119 crypto_free_tfm(tfm
);
124 nfsd4_rec_fsync(struct dentry
*dentry
)
129 dprintk("NFSD: nfs4_fsync_rec_dir\n");
130 filp
= dentry_open(dget(dentry
), mntget(rec_dir
.mnt
), O_RDWR
);
132 status
= PTR_ERR(filp
);
135 if (filp
->f_op
&& filp
->f_op
->fsync
)
136 status
= filp
->f_op
->fsync(filp
, filp
->f_dentry
, 0);
140 printk("nfsd4: unable to sync recovery directory\n");
145 nfsd4_create_clid_dir(struct nfs4_client
*clp
)
147 char *dname
= clp
->cl_recdir
;
148 struct dentry
*dentry
;
153 dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname
);
155 if (!rec_dir_init
|| clp
->cl_firststate
)
158 nfs4_save_user(&uid
, &gid
);
160 /* lock the parent */
161 down(&rec_dir
.dentry
->d_inode
->i_sem
);
163 dentry
= lookup_one_len(dname
, rec_dir
.dentry
, HEXDIR_LEN
-1);
164 if (IS_ERR(dentry
)) {
165 status
= PTR_ERR(dentry
);
169 if (dentry
->d_inode
) {
170 dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
173 status
= vfs_mkdir(rec_dir
.dentry
->d_inode
, dentry
, S_IRWXU
);
177 up(&rec_dir
.dentry
->d_inode
->i_sem
);
179 clp
->cl_firststate
= 1;
180 status
= nfsd4_rec_fsync(rec_dir
.dentry
);
182 nfs4_reset_user(uid
, gid
);
183 dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status
);
187 typedef int (recdir_func
)(struct dentry
*, struct dentry
*);
190 struct dentry
*dentry
;
191 struct list_head list
;
194 struct dentry_list_arg
{
195 struct list_head dentries
;
196 struct dentry
*parent
;
200 nfsd4_build_dentrylist(void *arg
, const char *name
, int namlen
,
201 loff_t offset
, ino_t ino
, unsigned int d_type
)
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
;
209 if (name
&& isdotent(name
, namlen
))
211 dentry
= lookup_one_len(name
, parent
, namlen
);
213 return PTR_ERR(dentry
);
214 child
= kmalloc(sizeof(*child
), GFP_KERNEL
);
217 child
->dentry
= dentry
;
218 list_add(&child
->list
, dentries
);
223 nfsd4_list_rec_dir(struct dentry
*dir
, recdir_func
*f
)
226 struct dentry_list_arg dla
= {
229 struct list_head
*dentries
= &dla
.dentries
;
230 struct dentry_list
*child
;
238 nfs4_save_user(&uid
, &gid
);
240 filp
= dentry_open(dget(dir
), mntget(rec_dir
.mnt
),
242 status
= PTR_ERR(filp
);
245 INIT_LIST_HEAD(dentries
);
246 status
= vfs_readdir(filp
, nfsd4_build_dentrylist
, &dla
);
248 while (!list_empty(dentries
)) {
249 child
= list_entry(dentries
->next
, struct dentry_list
, list
);
250 status
= f(dir
, child
->dentry
);
253 list_del(&child
->list
);
258 while (!list_empty(dentries
)) {
259 child
= list_entry(dentries
->next
, struct dentry_list
, list
);
260 list_del(&child
->list
);
264 nfs4_reset_user(uid
, gid
);
269 nfsd4_remove_clid_file(struct dentry
*dir
, struct dentry
*dentry
)
273 if (!S_ISREG(dir
->d_inode
->i_mode
)) {
274 printk("nfsd4: non-file found in client recovery directory\n");
277 down(&dir
->d_inode
->i_sem
);
278 status
= vfs_unlink(dir
->d_inode
, dentry
);
279 up(&dir
->d_inode
->i_sem
);
284 nfsd4_clear_clid_dir(struct dentry
*dir
, struct dentry
*dentry
)
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
);
299 nfsd4_unlink_clid_dir(char *name
, int namlen
)
301 struct dentry
*dentry
;
304 dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen
, name
);
306 dentry
= lookup_one_len(name
, rec_dir
.dentry
, namlen
);
307 if (IS_ERR(dentry
)) {
308 status
= PTR_ERR(dentry
);
312 if (!dentry
->d_inode
)
315 status
= nfsd4_clear_clid_dir(rec_dir
.dentry
, dentry
);
322 nfsd4_remove_clid_dir(struct nfs4_client
*clp
)
328 if (!rec_dir_init
|| !clp
->cl_firststate
)
331 nfs4_save_user(&uid
, &gid
);
332 status
= nfsd4_unlink_clid_dir(clp
->cl_recdir
, HEXDIR_LEN
-1);
333 nfs4_reset_user(uid
, gid
);
335 status
= nfsd4_rec_fsync(rec_dir
.dentry
);
337 printk("NFSD: Failed to remove expired client state directory"
338 " %.*s\n", HEXDIR_LEN
, clp
->cl_recdir
);
343 purge_old(struct dentry
*parent
, struct dentry
*child
)
347 if (nfs4_has_reclaimed_state(child
->d_name
.name
))
350 status
= nfsd4_clear_clid_dir(parent
, child
);
352 printk("failed to remove client recovery directory %s\n",
354 /* Keep trying, success or failure: */
359 nfsd4_recdir_purge_old(void) {
364 status
= nfsd4_list_rec_dir(rec_dir
.dentry
, purge_old
);
366 status
= nfsd4_rec_fsync(rec_dir
.dentry
);
368 printk("nfsd4: failed to purge old clients from recovery"
369 " directory %s\n", rec_dir
.dentry
->d_name
.name
);
374 load_recdir(struct dentry
*parent
, struct dentry
*child
)
376 if (child
->d_name
.len
!= HEXDIR_LEN
- 1) {
377 printk("nfsd4: illegal name %s in recovery directory\n",
379 /* Keep trying; maybe the others are OK: */
382 nfs4_client_to_reclaim(child
->d_name
.name
);
387 nfsd4_recdir_load(void) {
390 status
= nfsd4_list_rec_dir(rec_dir
.dentry
, load_recdir
);
392 printk("nfsd4: failed loading clients from recovery"
393 " directory %s\n", rec_dir
.dentry
->d_name
.name
);
398 * Hold reference to the recovery directory.
402 nfsd4_init_recdir(char *rec_dirname
)
408 printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
411 BUG_ON(rec_dir_init
);
413 nfs4_save_user(&uid
, &gid
);
415 status
= path_lookup(rec_dirname
, LOOKUP_FOLLOW
, &rec_dir
);
416 if (status
== -ENOENT
)
417 printk("NFSD: recovery directory %s doesn't exist\n",
422 nfs4_reset_user(uid
, gid
);
426 nfsd4_shutdown_recdir(void)
431 path_release(&rec_dir
);