blob: c7a6b245c7ad8486f2580091656d4d852b1c3fe6 [file] [log] [blame]
NeilBrowna55370a2005-06-23 22:03:52 -07001/*
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
Herbert Xu35058682006-08-24 19:10:20 +100036#include <linux/err.h>
NeilBrowna55370a2005-06-23 22:03:52 -070037#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>
NeilBrown190e4fb2005-06-23 22:04:25 -070042#include <linux/param.h>
43#include <linux/file.h>
44#include <linux/namei.h>
NeilBrowna55370a2005-06-23 22:03:52 -070045#include <asm/uaccess.h>
Adrian Bunk87ae9af2007-10-30 10:35:04 +010046#include <linux/scatterlist.h>
NeilBrowna55370a2005-06-23 22:03:52 -070047#include <linux/crypto.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040048#include <linux/sched.h>
Dave Hansen06227532008-02-15 14:37:34 -080049#include <linux/mount.h>
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050050#include "vfs.h"
NeilBrowna55370a2005-06-23 22:03:52 -070051
52#define NFSDDBG_FACILITY NFSDDBG_PROC
53
NeilBrown190e4fb2005-06-23 22:04:25 -070054/* Globals */
Al Viroa63bb992008-08-02 01:03:36 -040055static struct path rec_dir;
NeilBrown190e4fb2005-06-23 22:04:25 -070056static int rec_dir_init = 0;
57
David Howellsd84f4f92008-11-14 10:39:23 +110058static int
59nfs4_save_creds(const struct cred **original_creds)
NeilBrown190e4fb2005-06-23 22:04:25 -070060{
David Howellsd84f4f92008-11-14 10:39:23 +110061 struct cred *new;
62
63 new = prepare_creds();
64 if (!new)
65 return -ENOMEM;
66
67 new->fsuid = 0;
68 new->fsgid = 0;
69 *original_creds = override_creds(new);
70 put_cred(new);
71 return 0;
NeilBrown190e4fb2005-06-23 22:04:25 -070072}
73
74static void
David Howellsd84f4f92008-11-14 10:39:23 +110075nfs4_reset_creds(const struct cred *original)
NeilBrown190e4fb2005-06-23 22:04:25 -070076{
David Howellsd84f4f92008-11-14 10:39:23 +110077 revert_creds(original);
NeilBrown190e4fb2005-06-23 22:04:25 -070078}
79
NeilBrowna55370a2005-06-23 22:03:52 -070080static void
81md5_to_hex(char *out, char *md5)
82{
83 int i;
84
85 for (i=0; i<16; i++) {
86 unsigned char c = md5[i];
87
88 *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
89 *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
90 }
91 *out = '\0';
92}
93
Al Virob37ad282006-10-19 23:28:59 -070094__be32
NeilBrowna55370a2005-06-23 22:03:52 -070095nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
96{
97 struct xdr_netobj cksum;
Herbert Xu35058682006-08-24 19:10:20 +100098 struct hash_desc desc;
Jens Axboe60c74f82007-10-22 19:43:30 +020099 struct scatterlist sg;
Al Virob37ad282006-10-19 23:28:59 -0700100 __be32 status = nfserr_resource;
NeilBrowna55370a2005-06-23 22:03:52 -0700101
102 dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
103 clname->len, clname->data);
Herbert Xu35058682006-08-24 19:10:20 +1000104 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
105 desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
106 if (IS_ERR(desc.tfm))
107 goto out_no_tfm;
108 cksum.len = crypto_hash_digestsize(desc.tfm);
NeilBrowna55370a2005-06-23 22:03:52 -0700109 cksum.data = kmalloc(cksum.len, GFP_KERNEL);
110 if (cksum.data == NULL)
111 goto out;
NeilBrowna55370a2005-06-23 22:03:52 -0700112
Jens Axboe60c74f82007-10-22 19:43:30 +0200113 sg_init_one(&sg, clname->data, clname->len);
NeilBrowna55370a2005-06-23 22:03:52 -0700114
Jens Axboe60c74f82007-10-22 19:43:30 +0200115 if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data))
Herbert Xu35058682006-08-24 19:10:20 +1000116 goto out;
NeilBrowna55370a2005-06-23 22:03:52 -0700117
118 md5_to_hex(dname, cksum.data);
119
NeilBrowna55370a2005-06-23 22:03:52 -0700120 status = nfs_ok;
121out:
Krishna Kumar2bd9e7b2008-10-20 11:47:09 +0530122 kfree(cksum.data);
Herbert Xu35058682006-08-24 19:10:20 +1000123 crypto_free_hash(desc.tfm);
124out_no_tfm:
NeilBrowna55370a2005-06-23 22:03:52 -0700125 return status;
126}
NeilBrown190e4fb2005-06-23 22:04:25 -0700127
NeilBrowna6ccbbb2005-07-07 17:59:11 -0700128static void
129nfsd4_sync_rec_dir(void)
NeilBrownc7b9a452005-06-23 22:04:30 -0700130{
Al Viroa63bb992008-08-02 01:03:36 -0400131 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);
NeilBrownc7b9a452005-06-23 22:04:30 -0700134}
135
136int
137nfsd4_create_clid_dir(struct nfs4_client *clp)
138{
David Howellsd84f4f92008-11-14 10:39:23 +1100139 const struct cred *original_cred;
NeilBrownc7b9a452005-06-23 22:04:30 -0700140 char *dname = clp->cl_recdir;
141 struct dentry *dentry;
NeilBrownc7b9a452005-06-23 22:04:30 -0700142 int status;
143
144 dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
145
146 if (!rec_dir_init || clp->cl_firststate)
147 return 0;
148
David Howellsd84f4f92008-11-14 10:39:23 +1100149 status = nfs4_save_creds(&original_cred);
150 if (status < 0)
151 return status;
NeilBrownc7b9a452005-06-23 22:04:30 -0700152
153 /* lock the parent */
Al Viroa63bb992008-08-02 01:03:36 -0400154 mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
NeilBrownc7b9a452005-06-23 22:04:30 -0700155
Al Viroa63bb992008-08-02 01:03:36 -0400156 dentry = lookup_one_len(dname, rec_dir.dentry, HEXDIR_LEN-1);
NeilBrownc7b9a452005-06-23 22:04:30 -0700157 if (IS_ERR(dentry)) {
158 status = PTR_ERR(dentry);
159 goto out_unlock;
160 }
161 status = -EEXIST;
162 if (dentry->d_inode) {
163 dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
164 goto out_put;
165 }
Al Viroa63bb992008-08-02 01:03:36 -0400166 status = mnt_want_write(rec_dir.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -0800167 if (status)
168 goto out_put;
Al Viroa63bb992008-08-02 01:03:36 -0400169 status = vfs_mkdir(rec_dir.dentry->d_inode, dentry, S_IRWXU);
170 mnt_drop_write(rec_dir.mnt);
NeilBrownc7b9a452005-06-23 22:04:30 -0700171out_put:
172 dput(dentry);
173out_unlock:
Al Viroa63bb992008-08-02 01:03:36 -0400174 mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
NeilBrownc7b9a452005-06-23 22:04:30 -0700175 if (status == 0) {
176 clp->cl_firststate = 1;
NeilBrowna6ccbbb2005-07-07 17:59:11 -0700177 nfsd4_sync_rec_dir();
NeilBrownc7b9a452005-06-23 22:04:30 -0700178 }
David Howellsd84f4f92008-11-14 10:39:23 +1100179 nfs4_reset_creds(original_cred);
NeilBrownc7b9a452005-06-23 22:04:30 -0700180 dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status);
181 return status;
182}
183
NeilBrown190e4fb2005-06-23 22:04:25 -0700184typedef int (recdir_func)(struct dentry *, struct dentry *);
185
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400186struct name_list {
187 char name[HEXDIR_LEN];
NeilBrown190e4fb2005-06-23 22:04:25 -0700188 struct list_head list;
189};
190
NeilBrown190e4fb2005-06-23 22:04:25 -0700191static int
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400192nfsd4_build_namelist(void *arg, const char *name, int namlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700193 loff_t offset, u64 ino, unsigned int d_type)
NeilBrown190e4fb2005-06-23 22:04:25 -0700194{
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400195 struct list_head *names = arg;
196 struct name_list *entry;
NeilBrown190e4fb2005-06-23 22:04:25 -0700197
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400198 if (namlen != HEXDIR_LEN - 1)
Al Virob37ad282006-10-19 23:28:59 -0700199 return 0;
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400200 entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
201 if (entry == NULL)
NeilBrown190e4fb2005-06-23 22:04:25 -0700202 return -ENOMEM;
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400203 memcpy(entry->name, name, HEXDIR_LEN - 1);
204 entry->name[HEXDIR_LEN - 1] = '\0';
205 list_add(&entry->list, names);
NeilBrown190e4fb2005-06-23 22:04:25 -0700206 return 0;
207}
208
209static int
210nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
211{
David Howellsd84f4f92008-11-14 10:39:23 +1100212 const struct cred *original_cred;
NeilBrown190e4fb2005-06-23 22:04:25 -0700213 struct file *filp;
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400214 LIST_HEAD(names);
215 struct name_list *entry;
216 struct dentry *dentry;
NeilBrown190e4fb2005-06-23 22:04:25 -0700217 int status;
218
219 if (!rec_dir_init)
220 return 0;
221
David Howellsd84f4f92008-11-14 10:39:23 +1100222 status = nfs4_save_creds(&original_cred);
223 if (status < 0)
224 return status;
NeilBrown190e4fb2005-06-23 22:04:25 -0700225
David Howells745ca242008-11-14 10:39:22 +1100226 filp = dentry_open(dget(dir), mntget(rec_dir.mnt), O_RDONLY,
227 current_cred());
NeilBrown190e4fb2005-06-23 22:04:25 -0700228 status = PTR_ERR(filp);
229 if (IS_ERR(filp))
230 goto out;
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400231 status = vfs_readdir(filp, nfsd4_build_namelist, &names);
NeilBrown190e4fb2005-06-23 22:04:25 -0700232 fput(filp);
J. Bruce Fields8daed1e2009-05-11 16:10:19 -0400233 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400234 while (!list_empty(&names)) {
235 entry = list_entry(names.next, struct name_list, list);
236
237 dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
238 if (IS_ERR(dentry)) {
239 status = PTR_ERR(dentry);
David Woodhouse2f9092e2009-04-20 23:18:37 +0100240 break;
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400241 }
242 status = f(dir, dentry);
243 dput(dentry);
NeilBrown190e4fb2005-06-23 22:04:25 -0700244 if (status)
David Woodhouse2f9092e2009-04-20 23:18:37 +0100245 break;
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400246 list_del(&entry->list);
247 kfree(entry);
NeilBrown190e4fb2005-06-23 22:04:25 -0700248 }
David Woodhouse2f9092e2009-04-20 23:18:37 +0100249 mutex_unlock(&dir->d_inode->i_mutex);
NeilBrown190e4fb2005-06-23 22:04:25 -0700250out:
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400251 while (!list_empty(&names)) {
252 entry = list_entry(names.next, struct name_list, list);
253 list_del(&entry->list);
254 kfree(entry);
NeilBrown190e4fb2005-06-23 22:04:25 -0700255 }
David Howellsd84f4f92008-11-14 10:39:23 +1100256 nfs4_reset_creds(original_cred);
NeilBrown190e4fb2005-06-23 22:04:25 -0700257 return status;
258}
259
260static int
NeilBrownc7b9a452005-06-23 22:04:30 -0700261nfsd4_unlink_clid_dir(char *name, int namlen)
262{
263 struct dentry *dentry;
264 int status;
265
266 dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
267
J. Bruce Fields8daed1e2009-05-11 16:10:19 -0400268 mutex_lock_nested(&rec_dir.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viroa63bb992008-08-02 01:03:36 -0400269 dentry = lookup_one_len(name, rec_dir.dentry, namlen);
NeilBrownc7b9a452005-06-23 22:04:30 -0700270 if (IS_ERR(dentry)) {
271 status = PTR_ERR(dentry);
David Woodhouse2f9092e2009-04-20 23:18:37 +0100272 goto out_unlock;
NeilBrownc7b9a452005-06-23 22:04:30 -0700273 }
274 status = -ENOENT;
275 if (!dentry->d_inode)
276 goto out;
David Woodhouse2f9092e2009-04-20 23:18:37 +0100277 status = vfs_rmdir(rec_dir.dentry->d_inode, dentry);
NeilBrownc7b9a452005-06-23 22:04:30 -0700278out:
279 dput(dentry);
David Woodhouse2f9092e2009-04-20 23:18:37 +0100280out_unlock:
281 mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
NeilBrownc7b9a452005-06-23 22:04:30 -0700282 return status;
283}
284
285void
286nfsd4_remove_clid_dir(struct nfs4_client *clp)
287{
David Howellsd84f4f92008-11-14 10:39:23 +1100288 const struct cred *original_cred;
NeilBrownc7b9a452005-06-23 22:04:30 -0700289 int status;
290
291 if (!rec_dir_init || !clp->cl_firststate)
292 return;
293
Al Viroa63bb992008-08-02 01:03:36 -0400294 status = mnt_want_write(rec_dir.mnt);
Dave Hansen06227532008-02-15 14:37:34 -0800295 if (status)
296 goto out;
NeilBrown67be4312005-07-07 17:59:13 -0700297 clp->cl_firststate = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100298
299 status = nfs4_save_creds(&original_cred);
300 if (status < 0)
301 goto out;
302
NeilBrownc7b9a452005-06-23 22:04:30 -0700303 status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
David Howellsd84f4f92008-11-14 10:39:23 +1100304 nfs4_reset_creds(original_cred);
NeilBrownc7b9a452005-06-23 22:04:30 -0700305 if (status == 0)
NeilBrowna6ccbbb2005-07-07 17:59:11 -0700306 nfsd4_sync_rec_dir();
Al Viroa63bb992008-08-02 01:03:36 -0400307 mnt_drop_write(rec_dir.mnt);
Dave Hansen06227532008-02-15 14:37:34 -0800308out:
NeilBrownc7b9a452005-06-23 22:04:30 -0700309 if (status)
310 printk("NFSD: Failed to remove expired client state directory"
311 " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
312 return;
313}
314
315static int
316purge_old(struct dentry *parent, struct dentry *child)
317{
318 int status;
319
Andy Adamsona1bcecd2009-04-03 08:28:05 +0300320 /* note: we currently use this path only for minorversion 0 */
321 if (nfs4_has_reclaimed_state(child->d_name.name, false))
Al Virob37ad282006-10-19 23:28:59 -0700322 return 0;
NeilBrownc7b9a452005-06-23 22:04:30 -0700323
David Woodhouse2f9092e2009-04-20 23:18:37 +0100324 status = vfs_rmdir(parent->d_inode, child);
NeilBrownc7b9a452005-06-23 22:04:30 -0700325 if (status)
326 printk("failed to remove client recovery directory %s\n",
327 child->d_name.name);
328 /* Keep trying, success or failure: */
Al Virob37ad282006-10-19 23:28:59 -0700329 return 0;
NeilBrownc7b9a452005-06-23 22:04:30 -0700330}
331
332void
333nfsd4_recdir_purge_old(void) {
334 int status;
335
336 if (!rec_dir_init)
337 return;
Al Viroa63bb992008-08-02 01:03:36 -0400338 status = mnt_want_write(rec_dir.mnt);
Dave Hansen06227532008-02-15 14:37:34 -0800339 if (status)
340 goto out;
Al Viroa63bb992008-08-02 01:03:36 -0400341 status = nfsd4_list_rec_dir(rec_dir.dentry, purge_old);
NeilBrownc7b9a452005-06-23 22:04:30 -0700342 if (status == 0)
NeilBrowna6ccbbb2005-07-07 17:59:11 -0700343 nfsd4_sync_rec_dir();
Al Viroa63bb992008-08-02 01:03:36 -0400344 mnt_drop_write(rec_dir.mnt);
Dave Hansen06227532008-02-15 14:37:34 -0800345out:
NeilBrownc7b9a452005-06-23 22:04:30 -0700346 if (status)
347 printk("nfsd4: failed to purge old clients from recovery"
Al Viroa63bb992008-08-02 01:03:36 -0400348 " directory %s\n", rec_dir.dentry->d_name.name);
NeilBrownc7b9a452005-06-23 22:04:30 -0700349}
350
351static int
NeilBrown190e4fb2005-06-23 22:04:25 -0700352load_recdir(struct dentry *parent, struct dentry *child)
353{
354 if (child->d_name.len != HEXDIR_LEN - 1) {
355 printk("nfsd4: illegal name %s in recovery directory\n",
356 child->d_name.name);
357 /* Keep trying; maybe the others are OK: */
Al Virob37ad282006-10-19 23:28:59 -0700358 return 0;
NeilBrown190e4fb2005-06-23 22:04:25 -0700359 }
360 nfs4_client_to_reclaim(child->d_name.name);
Al Virob37ad282006-10-19 23:28:59 -0700361 return 0;
NeilBrown190e4fb2005-06-23 22:04:25 -0700362}
363
364int
365nfsd4_recdir_load(void) {
366 int status;
367
Al Viroa63bb992008-08-02 01:03:36 -0400368 status = nfsd4_list_rec_dir(rec_dir.dentry, load_recdir);
NeilBrown190e4fb2005-06-23 22:04:25 -0700369 if (status)
370 printk("nfsd4: failed loading clients from recovery"
Al Viroa63bb992008-08-02 01:03:36 -0400371 " directory %s\n", rec_dir.dentry->d_name.name);
NeilBrown190e4fb2005-06-23 22:04:25 -0700372 return status;
373}
374
375/*
376 * Hold reference to the recovery directory.
377 */
378
379void
380nfsd4_init_recdir(char *rec_dirname)
381{
David Howellsd84f4f92008-11-14 10:39:23 +1100382 const struct cred *original_cred;
383 int status;
NeilBrown190e4fb2005-06-23 22:04:25 -0700384
385 printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
386 rec_dirname);
387
388 BUG_ON(rec_dir_init);
389
David Howellsd84f4f92008-11-14 10:39:23 +1100390 status = nfs4_save_creds(&original_cred);
391 if (status < 0) {
392 printk("NFSD: Unable to change credentials to find recovery"
393 " directory: error %d\n",
394 status);
395 return;
396 }
NeilBrown190e4fb2005-06-23 22:04:25 -0700397
Al Viroa63bb992008-08-02 01:03:36 -0400398 status = kern_path(rec_dirname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
J. Bruce Fieldsc2642ab2006-01-18 17:43:29 -0800399 &rec_dir);
400 if (status)
401 printk("NFSD: unable to find recovery directory %s\n",
NeilBrown190e4fb2005-06-23 22:04:25 -0700402 rec_dirname);
403
404 if (!status)
405 rec_dir_init = 1;
David Howellsd84f4f92008-11-14 10:39:23 +1100406 nfs4_reset_creds(original_cred);
NeilBrown190e4fb2005-06-23 22:04:25 -0700407}
408
409void
410nfsd4_shutdown_recdir(void)
411{
412 if (!rec_dir_init)
413 return;
414 rec_dir_init = 0;
Al Viroa63bb992008-08-02 01:03:36 -0400415 path_put(&rec_dir);
NeilBrown190e4fb2005-06-23 22:04:25 -0700416}