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.
36 #include <linux/err.h>
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 <linux/scatterlist.h>
47 #include <linux/crypto.h>
48 #include <linux/sched.h>
49 #include <linux/mount.h>
52 #define NFSDDBG_FACILITY NFSDDBG_PROC
55 static struct path rec_dir;
56 static int rec_dir_init = 0;
59 nfs4_save_creds(const struct cred **original_creds)
63 new = prepare_creds();
69 *original_creds = override_creds(new);
75 nfs4_reset_creds(const struct cred *original)
77 revert_creds(original);
81 md5_to_hex(char *out, char *md5)
85 for (i=0; i<16; i++) {
86 unsigned char c = md5[i];
88 *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
89 *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
95 nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
97 struct xdr_netobj cksum;
98 struct hash_desc desc;
99 struct scatterlist sg;
100 __be32 status = nfserr_resource;
102 dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
103 clname->len, clname->data);
104 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
105 desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
106 if (IS_ERR(desc.tfm))
108 cksum.len = crypto_hash_digestsize(desc.tfm);
109 cksum.data = kmalloc(cksum.len, GFP_KERNEL);
110 if (cksum.data == NULL)
113 sg_init_one(&sg, clname->data, clname->len);
115 if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data))
118 md5_to_hex(dname, cksum.data);
123 crypto_free_hash(desc.tfm);
129 nfsd4_sync_rec_dir(void)
131 mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
132 nfsd_sync_dir(rec_dir.dentry);
133 mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
137 nfsd4_create_clid_dir(struct nfs4_client *clp)
139 const struct cred *original_cred;
140 char *dname = clp->cl_recdir;
141 struct dentry *dentry;
144 dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
146 if (!rec_dir_init || clp->cl_firststate)
149 status = nfs4_save_creds(&original_cred);
153 /* lock the parent */
154 mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
156 dentry = lookup_one_len(dname, rec_dir.dentry, HEXDIR_LEN-1);
157 if (IS_ERR(dentry)) {
158 status = PTR_ERR(dentry);
162 if (dentry->d_inode) {
163 dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
166 status = mnt_want_write(rec_dir.mnt);
169 status = vfs_mkdir(rec_dir.dentry->d_inode, dentry, S_IRWXU);
170 mnt_drop_write(rec_dir.mnt);
174 mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
176 clp->cl_firststate = 1;
177 nfsd4_sync_rec_dir();
179 nfs4_reset_creds(original_cred);
180 dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status);
184 typedef int (recdir_func)(struct dentry *, struct dentry *);
187 char name[HEXDIR_LEN];
188 struct list_head list;
192 nfsd4_build_namelist(void *arg, const char *name, int namlen,
193 loff_t offset, u64 ino, unsigned int d_type)
195 struct list_head *names = arg;
196 struct name_list *entry;
198 if (namlen != HEXDIR_LEN - 1)
200 entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
203 memcpy(entry->name, name, HEXDIR_LEN - 1);
204 entry->name[HEXDIR_LEN - 1] = '\0';
205 list_add(&entry->list, names);
210 nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
212 const struct cred *original_cred;
215 struct name_list *entry;
216 struct dentry *dentry;
222 status = nfs4_save_creds(&original_cred);
226 filp = dentry_open(dget(dir), mntget(rec_dir.mnt), O_RDONLY,
228 status = PTR_ERR(filp);
231 status = vfs_readdir(filp, nfsd4_build_namelist, &names);
233 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
234 while (!list_empty(&names)) {
235 entry = list_entry(names.next, struct name_list, list);
237 dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
238 if (IS_ERR(dentry)) {
239 status = PTR_ERR(dentry);
242 status = f(dir, dentry);
246 list_del(&entry->list);
249 mutex_unlock(&dir->d_inode->i_mutex);
251 while (!list_empty(&names)) {
252 entry = list_entry(names.next, struct name_list, list);
253 list_del(&entry->list);
256 nfs4_reset_creds(original_cred);
261 nfsd4_unlink_clid_dir(char *name, int namlen)
263 struct dentry *dentry;
266 dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
268 mutex_lock_nested(&rec_dir.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
269 dentry = lookup_one_len(name, rec_dir.dentry, namlen);
270 if (IS_ERR(dentry)) {
271 status = PTR_ERR(dentry);
275 if (!dentry->d_inode)
277 status = vfs_rmdir(rec_dir.dentry->d_inode, dentry);
281 mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
286 nfsd4_remove_clid_dir(struct nfs4_client *clp)
288 const struct cred *original_cred;
291 if (!rec_dir_init || !clp->cl_firststate)
294 status = mnt_want_write(rec_dir.mnt);
297 clp->cl_firststate = 0;
299 status = nfs4_save_creds(&original_cred);
303 status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
304 nfs4_reset_creds(original_cred);
306 nfsd4_sync_rec_dir();
307 mnt_drop_write(rec_dir.mnt);
310 printk("NFSD: Failed to remove expired client state directory"
311 " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
316 purge_old(struct dentry *parent, struct dentry *child)
320 /* note: we currently use this path only for minorversion 0 */
321 if (nfs4_has_reclaimed_state(child->d_name.name, false))
324 status = vfs_rmdir(parent->d_inode, child);
326 printk("failed to remove client recovery directory %s\n",
328 /* Keep trying, success or failure: */
333 nfsd4_recdir_purge_old(void) {
338 status = mnt_want_write(rec_dir.mnt);
341 status = nfsd4_list_rec_dir(rec_dir.dentry, purge_old);
343 nfsd4_sync_rec_dir();
344 mnt_drop_write(rec_dir.mnt);
347 printk("nfsd4: failed to purge old clients from recovery"
348 " directory %s\n", rec_dir.dentry->d_name.name);
352 load_recdir(struct dentry *parent, struct dentry *child)
354 if (child->d_name.len != HEXDIR_LEN - 1) {
355 printk("nfsd4: illegal name %s in recovery directory\n",
357 /* Keep trying; maybe the others are OK: */
360 nfs4_client_to_reclaim(child->d_name.name);
365 nfsd4_recdir_load(void) {
368 status = nfsd4_list_rec_dir(rec_dir.dentry, load_recdir);
370 printk("nfsd4: failed loading clients from recovery"
371 " directory %s\n", rec_dir.dentry->d_name.name);
376 * Hold reference to the recovery directory.
380 nfsd4_init_recdir(char *rec_dirname)
382 const struct cred *original_cred;
385 printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
388 BUG_ON(rec_dir_init);
390 status = nfs4_save_creds(&original_cred);
392 printk("NFSD: Unable to change credentials to find recovery"
393 " directory: error %d\n",
398 status = kern_path(rec_dirname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
401 printk("NFSD: unable to find recovery directory %s\n",
406 nfs4_reset_creds(original_cred);
410 nfsd4_shutdown_recdir(void)