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 static struct nameidata rec_dir
;
54 static int rec_dir_init
= 0;
57 nfs4_save_user(uid_t
*saveuid
, gid_t
*savegid
)
59 *saveuid
= current
->fsuid
;
60 *savegid
= current
->fsgid
;
66 nfs4_reset_user(uid_t saveuid
, gid_t savegid
)
68 current
->fsuid
= saveuid
;
69 current
->fsgid
= savegid
;
73 md5_to_hex(char *out
, char *md5
)
77 for (i
=0; i
<16; i
++) {
78 unsigned char c
= md5
[i
];
80 *out
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
81 *out
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
87 nfs4_make_rec_clidname(char *dname
, struct xdr_netobj
*clname
)
89 struct xdr_netobj cksum
;
90 struct crypto_tfm
*tfm
;
91 struct scatterlist sg
[1];
92 int status
= nfserr_resource
;
94 dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
95 clname
->len
, clname
->data
);
96 tfm
= crypto_alloc_tfm("md5", 0);
99 cksum
.len
= crypto_tfm_alg_digestsize(tfm
);
100 cksum
.data
= kmalloc(cksum
.len
, GFP_KERNEL
);
101 if (cksum
.data
== NULL
)
103 crypto_digest_init(tfm
);
105 sg
[0].page
= virt_to_page(clname
->data
);
106 sg
[0].offset
= offset_in_page(clname
->data
);
107 sg
[0].length
= clname
->len
;
109 crypto_digest_update(tfm
, sg
, 1);
110 crypto_digest_final(tfm
, cksum
.data
);
112 md5_to_hex(dname
, cksum
.data
);
118 crypto_free_tfm(tfm
);
123 nfsd4_rec_fsync(struct dentry
*dentry
)
128 dprintk("NFSD: nfs4_fsync_rec_dir\n");
129 filp
= dentry_open(dget(dentry
), mntget(rec_dir
.mnt
), O_RDWR
);
131 status
= PTR_ERR(filp
);
134 if (filp
->f_op
&& filp
->f_op
->fsync
)
135 status
= filp
->f_op
->fsync(filp
, filp
->f_dentry
, 0);
139 printk("nfsd4: unable to sync recovery directory\n");
144 nfsd4_create_clid_dir(struct nfs4_client
*clp
)
146 char *dname
= clp
->cl_recdir
;
147 struct dentry
*dentry
;
152 dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname
);
154 if (!rec_dir_init
|| clp
->cl_firststate
)
157 nfs4_save_user(&uid
, &gid
);
159 /* lock the parent */
160 down(&rec_dir
.dentry
->d_inode
->i_sem
);
162 dentry
= lookup_one_len(dname
, rec_dir
.dentry
, HEXDIR_LEN
-1);
163 if (IS_ERR(dentry
)) {
164 status
= PTR_ERR(dentry
);
168 if (dentry
->d_inode
) {
169 dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
172 status
= vfs_mkdir(rec_dir
.dentry
->d_inode
, dentry
, S_IRWXU
);
176 up(&rec_dir
.dentry
->d_inode
->i_sem
);
178 clp
->cl_firststate
= 1;
179 status
= nfsd4_rec_fsync(rec_dir
.dentry
);
181 nfs4_reset_user(uid
, gid
);
182 dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status
);
186 typedef int (recdir_func
)(struct dentry
*, struct dentry
*);
189 struct dentry
*dentry
;
190 struct list_head list
;
193 struct dentry_list_arg
{
194 struct list_head dentries
;
195 struct dentry
*parent
;
199 nfsd4_build_dentrylist(void *arg
, const char *name
, int namlen
,
200 loff_t offset
, ino_t ino
, unsigned int d_type
)
202 struct dentry_list_arg
*dla
= arg
;
203 struct list_head
*dentries
= &dla
->dentries
;
204 struct dentry
*parent
= dla
->parent
;
205 struct dentry
*dentry
;
206 struct dentry_list
*child
;
208 if (name
&& isdotent(name
, namlen
))
210 dentry
= lookup_one_len(name
, parent
, namlen
);
212 return PTR_ERR(dentry
);
213 child
= kmalloc(sizeof(*child
), GFP_KERNEL
);
216 child
->dentry
= dentry
;
217 list_add(&child
->list
, dentries
);
222 nfsd4_list_rec_dir(struct dentry
*dir
, recdir_func
*f
)
225 struct dentry_list_arg dla
= {
228 struct list_head
*dentries
= &dla
.dentries
;
229 struct dentry_list
*child
;
237 nfs4_save_user(&uid
, &gid
);
239 filp
= dentry_open(dget(dir
), mntget(rec_dir
.mnt
),
241 status
= PTR_ERR(filp
);
244 INIT_LIST_HEAD(dentries
);
245 status
= vfs_readdir(filp
, nfsd4_build_dentrylist
, &dla
);
247 while (!list_empty(dentries
)) {
248 child
= list_entry(dentries
->next
, struct dentry_list
, list
);
249 status
= f(dir
, child
->dentry
);
252 list_del(&child
->list
);
257 while (!list_empty(dentries
)) {
258 child
= list_entry(dentries
->next
, struct dentry_list
, list
);
259 list_del(&child
->list
);
263 nfs4_reset_user(uid
, gid
);
268 nfsd4_remove_clid_file(struct dentry
*dir
, struct dentry
*dentry
)
272 if (!S_ISREG(dir
->d_inode
->i_mode
)) {
273 printk("nfsd4: non-file found in client recovery directory\n");
276 down(&dir
->d_inode
->i_sem
);
277 status
= vfs_unlink(dir
->d_inode
, dentry
);
278 up(&dir
->d_inode
->i_sem
);
283 nfsd4_clear_clid_dir(struct dentry
*dir
, struct dentry
*dentry
)
287 /* For now this directory should already be empty, but we empty it of
288 * any regular files anyway, just in case the directory was created by
289 * a kernel from the future.... */
290 nfsd4_list_rec_dir(dentry
, nfsd4_remove_clid_file
);
291 down(&dir
->d_inode
->i_sem
);
292 status
= vfs_rmdir(dir
->d_inode
, dentry
);
293 up(&dir
->d_inode
->i_sem
);
298 nfsd4_unlink_clid_dir(char *name
, int namlen
)
300 struct dentry
*dentry
;
303 dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen
, name
);
305 dentry
= lookup_one_len(name
, rec_dir
.dentry
, namlen
);
306 if (IS_ERR(dentry
)) {
307 status
= PTR_ERR(dentry
);
311 if (!dentry
->d_inode
)
314 status
= nfsd4_clear_clid_dir(rec_dir
.dentry
, dentry
);
321 nfsd4_remove_clid_dir(struct nfs4_client
*clp
)
327 if (!rec_dir_init
|| !clp
->cl_firststate
)
330 nfs4_save_user(&uid
, &gid
);
331 status
= nfsd4_unlink_clid_dir(clp
->cl_recdir
, HEXDIR_LEN
-1);
332 nfs4_reset_user(uid
, gid
);
334 status
= nfsd4_rec_fsync(rec_dir
.dentry
);
336 printk("NFSD: Failed to remove expired client state directory"
337 " %.*s\n", HEXDIR_LEN
, clp
->cl_recdir
);
342 purge_old(struct dentry
*parent
, struct dentry
*child
)
346 if (nfs4_has_reclaimed_state(child
->d_name
.name
))
349 status
= nfsd4_clear_clid_dir(parent
, child
);
351 printk("failed to remove client recovery directory %s\n",
353 /* Keep trying, success or failure: */
358 nfsd4_recdir_purge_old(void) {
363 status
= nfsd4_list_rec_dir(rec_dir
.dentry
, purge_old
);
365 status
= nfsd4_rec_fsync(rec_dir
.dentry
);
367 printk("nfsd4: failed to purge old clients from recovery"
368 " directory %s\n", rec_dir
.dentry
->d_name
.name
);
373 load_recdir(struct dentry
*parent
, struct dentry
*child
)
375 if (child
->d_name
.len
!= HEXDIR_LEN
- 1) {
376 printk("nfsd4: illegal name %s in recovery directory\n",
378 /* Keep trying; maybe the others are OK: */
381 nfs4_client_to_reclaim(child
->d_name
.name
);
386 nfsd4_recdir_load(void) {
389 status
= nfsd4_list_rec_dir(rec_dir
.dentry
, load_recdir
);
391 printk("nfsd4: failed loading clients from recovery"
392 " directory %s\n", rec_dir
.dentry
->d_name
.name
);
397 * Hold reference to the recovery directory.
401 nfsd4_init_recdir(char *rec_dirname
)
407 printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
410 BUG_ON(rec_dir_init
);
412 nfs4_save_user(&uid
, &gid
);
414 status
= path_lookup(rec_dirname
, LOOKUP_FOLLOW
, &rec_dir
);
415 if (status
== -ENOENT
)
416 printk("NFSD: recovery directory %s doesn't exist\n",
421 nfs4_reset_user(uid
, gid
);
425 nfsd4_shutdown_recdir(void)
430 path_release(&rec_dir
);