nfs4recover.c 9.5 KB
Newer Older
N
NeilBrown 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
*  linux/fs/nfsd/nfs4recover.c
*
*  Copyright (c) 2004 The Regents of the University of Michigan.
*  All rights reserved.
*
*  Andy Adamson <andros@citi.umich.edu>
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*  1. Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*  2. Redistributions in binary form must reproduce the above copyright
*     notice, this list of conditions and the following disclaimer in the
*     documentation and/or other materials provided with the distribution.
*  3. Neither the name of the University nor the names of its
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
*  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
*  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
*  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
*  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
*  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
*  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

#include <linux/nfsd/nfsd.h>
#include <linux/nfsd/state.h>
38 39
#include <linux/file.h>
#include <linux/namei.h>
N
NeilBrown 已提交
40
#include <linux/crypto.h>
A
Alexey Dobriyan 已提交
41
#include <linux/sched.h>
42
#include "vfs.h"
N
NeilBrown 已提交
43 44 45

#define NFSDDBG_FACILITY                NFSDDBG_PROC

46
/* Globals */
A
Al Viro 已提交
47
static struct path rec_dir;
48 49
static int rec_dir_init = 0;

D
David Howells 已提交
50 51
static int
nfs4_save_creds(const struct cred **original_creds)
52
{
D
David Howells 已提交
53 54 55 56 57 58 59 60 61 62 63
	struct cred *new;

	new = prepare_creds();
	if (!new)
		return -ENOMEM;

	new->fsuid = 0;
	new->fsgid = 0;
	*original_creds = override_creds(new);
	put_cred(new);
	return 0;
64 65 66
}

static void
D
David Howells 已提交
67
nfs4_reset_creds(const struct cred *original)
68
{
D
David Howells 已提交
69
	revert_creds(original);
70 71
}

N
NeilBrown 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85
static void
md5_to_hex(char *out, char *md5)
{
	int i;

	for (i=0; i<16; i++) {
		unsigned char c = md5[i];

		*out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
		*out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
	}
	*out = '\0';
}

86
__be32
N
NeilBrown 已提交
87 88 89
nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
{
	struct xdr_netobj cksum;
90
	struct hash_desc desc;
J
Jens Axboe 已提交
91
	struct scatterlist sg;
92
	__be32 status = nfserr_resource;
N
NeilBrown 已提交
93 94 95

	dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
			clname->len, clname->data);
96 97 98 99 100
	desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
	desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
	if (IS_ERR(desc.tfm))
		goto out_no_tfm;
	cksum.len = crypto_hash_digestsize(desc.tfm);
N
NeilBrown 已提交
101 102 103 104
	cksum.data = kmalloc(cksum.len, GFP_KERNEL);
	if (cksum.data == NULL)
 		goto out;

J
Jens Axboe 已提交
105
	sg_init_one(&sg, clname->data, clname->len);
N
NeilBrown 已提交
106

J
Jens Axboe 已提交
107
	if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data))
108
		goto out;
N
NeilBrown 已提交
109 110 111 112 113

	md5_to_hex(dname, cksum.data);

	status = nfs_ok;
out:
114
	kfree(cksum.data);
115 116
	crypto_free_hash(desc.tfm);
out_no_tfm:
N
NeilBrown 已提交
117 118
	return status;
}
119

120 121
static void
nfsd4_sync_rec_dir(void)
122
{
A
Al Viro 已提交
123 124 125
	mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
	nfsd_sync_dir(rec_dir.dentry);
	mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
126 127 128 129 130
}

int
nfsd4_create_clid_dir(struct nfs4_client *clp)
{
D
David Howells 已提交
131
	const struct cred *original_cred;
132 133 134 135 136 137 138 139 140
	char *dname = clp->cl_recdir;
	struct dentry *dentry;
	int status;

	dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);

	if (!rec_dir_init || clp->cl_firststate)
		return 0;

D
David Howells 已提交
141 142 143
	status = nfs4_save_creds(&original_cred);
	if (status < 0)
		return status;
144 145

	/* lock the parent */
A
Al Viro 已提交
146
	mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
147

A
Al Viro 已提交
148
	dentry = lookup_one_len(dname, rec_dir.dentry, HEXDIR_LEN-1);
149 150 151 152 153 154 155 156 157
	if (IS_ERR(dentry)) {
		status = PTR_ERR(dentry);
		goto out_unlock;
	}
	status = -EEXIST;
	if (dentry->d_inode) {
		dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
		goto out_put;
	}
A
Al Viro 已提交
158
	status = mnt_want_write(rec_dir.mnt);
159 160
	if (status)
		goto out_put;
A
Al Viro 已提交
161 162
	status = vfs_mkdir(rec_dir.dentry->d_inode, dentry, S_IRWXU);
	mnt_drop_write(rec_dir.mnt);
163 164 165
out_put:
	dput(dentry);
out_unlock:
A
Al Viro 已提交
166
	mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
167 168
	if (status == 0) {
		clp->cl_firststate = 1;
169
		nfsd4_sync_rec_dir();
170
	}
D
David Howells 已提交
171
	nfs4_reset_creds(original_cred);
172 173 174 175
	dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status);
	return status;
}

176 177
typedef int (recdir_func)(struct dentry *, struct dentry *);

178 179
struct name_list {
	char name[HEXDIR_LEN];
180 181 182 183
	struct list_head list;
};

static int
184
nfsd4_build_namelist(void *arg, const char *name, int namlen,
185
		loff_t offset, u64 ino, unsigned int d_type)
186
{
187 188
	struct list_head *names = arg;
	struct name_list *entry;
189

190
	if (namlen != HEXDIR_LEN - 1)
191
		return 0;
192 193
	entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
	if (entry == NULL)
194
		return -ENOMEM;
195 196 197
	memcpy(entry->name, name, HEXDIR_LEN - 1);
	entry->name[HEXDIR_LEN - 1] = '\0';
	list_add(&entry->list, names);
198 199 200 201 202 203
	return 0;
}

static int
nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
{
D
David Howells 已提交
204
	const struct cred *original_cred;
205
	struct file *filp;
206 207 208
	LIST_HEAD(names);
	struct name_list *entry;
	struct dentry *dentry;
209 210 211 212 213
	int status;

	if (!rec_dir_init)
		return 0;

D
David Howells 已提交
214 215 216
	status = nfs4_save_creds(&original_cred);
	if (status < 0)
		return status;
217

218 219
	filp = dentry_open(dget(dir), mntget(rec_dir.mnt), O_RDONLY,
			   current_cred());
220 221 222
	status = PTR_ERR(filp);
	if (IS_ERR(filp))
		goto out;
223
	status = vfs_readdir(filp, nfsd4_build_namelist, &names);
224
	fput(filp);
J
J. Bruce Fields 已提交
225
	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
226 227 228 229 230 231
	while (!list_empty(&names)) {
		entry = list_entry(names.next, struct name_list, list);

		dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
		if (IS_ERR(dentry)) {
			status = PTR_ERR(dentry);
232
			break;
233 234 235
		}
		status = f(dir, dentry);
		dput(dentry);
236
		if (status)
237
			break;
238 239
		list_del(&entry->list);
		kfree(entry);
240
	}
241
	mutex_unlock(&dir->d_inode->i_mutex);
242
out:
243 244 245 246
	while (!list_empty(&names)) {
		entry = list_entry(names.next, struct name_list, list);
		list_del(&entry->list);
		kfree(entry);
247
	}
D
David Howells 已提交
248
	nfs4_reset_creds(original_cred);
249 250 251
	return status;
}

252 253 254 255 256 257 258 259
static int
nfsd4_unlink_clid_dir(char *name, int namlen)
{
	struct dentry *dentry;
	int status;

	dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);

J
J. Bruce Fields 已提交
260
	mutex_lock_nested(&rec_dir.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
A
Al Viro 已提交
261
	dentry = lookup_one_len(name, rec_dir.dentry, namlen);
262 263
	if (IS_ERR(dentry)) {
		status = PTR_ERR(dentry);
264
		goto out_unlock;
265 266 267 268
	}
	status = -ENOENT;
	if (!dentry->d_inode)
		goto out;
269
	status = vfs_rmdir(rec_dir.dentry->d_inode, dentry);
270 271
out:
	dput(dentry);
272 273
out_unlock:
	mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
274 275 276 277 278 279
	return status;
}

void
nfsd4_remove_clid_dir(struct nfs4_client *clp)
{
D
David Howells 已提交
280
	const struct cred *original_cred;
281 282 283 284 285
	int status;

	if (!rec_dir_init || !clp->cl_firststate)
		return;

A
Al Viro 已提交
286
	status = mnt_want_write(rec_dir.mnt);
287 288
	if (status)
		goto out;
289
	clp->cl_firststate = 0;
D
David Howells 已提交
290 291 292 293 294

	status = nfs4_save_creds(&original_cred);
	if (status < 0)
		goto out;

295
	status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
D
David Howells 已提交
296
	nfs4_reset_creds(original_cred);
297
	if (status == 0)
298
		nfsd4_sync_rec_dir();
A
Al Viro 已提交
299
	mnt_drop_write(rec_dir.mnt);
300
out:
301 302 303 304 305 306 307 308 309 310 311
	if (status)
		printk("NFSD: Failed to remove expired client state directory"
				" %.*s\n", HEXDIR_LEN, clp->cl_recdir);
	return;
}

static int
purge_old(struct dentry *parent, struct dentry *child)
{
	int status;

312 313
	/* note: we currently use this path only for minorversion 0 */
	if (nfs4_has_reclaimed_state(child->d_name.name, false))
314
		return 0;
315

316
	status = vfs_rmdir(parent->d_inode, child);
317 318 319 320
	if (status)
		printk("failed to remove client recovery directory %s\n",
				child->d_name.name);
	/* Keep trying, success or failure: */
321
	return 0;
322 323 324 325 326 327 328 329
}

void
nfsd4_recdir_purge_old(void) {
	int status;

	if (!rec_dir_init)
		return;
A
Al Viro 已提交
330
	status = mnt_want_write(rec_dir.mnt);
331 332
	if (status)
		goto out;
A
Al Viro 已提交
333
	status = nfsd4_list_rec_dir(rec_dir.dentry, purge_old);
334
	if (status == 0)
335
		nfsd4_sync_rec_dir();
A
Al Viro 已提交
336
	mnt_drop_write(rec_dir.mnt);
337
out:
338 339
	if (status)
		printk("nfsd4: failed to purge old clients from recovery"
A
Al Viro 已提交
340
			" directory %s\n", rec_dir.dentry->d_name.name);
341 342
}

343 344 345 346 347 348 349
static int
load_recdir(struct dentry *parent, struct dentry *child)
{
	if (child->d_name.len != HEXDIR_LEN - 1) {
		printk("nfsd4: illegal name %s in recovery directory\n",
				child->d_name.name);
		/* Keep trying; maybe the others are OK: */
350
		return 0;
351 352
	}
	nfs4_client_to_reclaim(child->d_name.name);
353
	return 0;
354 355 356 357 358 359
}

int
nfsd4_recdir_load(void) {
	int status;

A
Al Viro 已提交
360
	status = nfsd4_list_rec_dir(rec_dir.dentry, load_recdir);
361 362
	if (status)
		printk("nfsd4: failed loading clients from recovery"
A
Al Viro 已提交
363
			" directory %s\n", rec_dir.dentry->d_name.name);
364 365 366 367 368 369 370 371 372 373
	return status;
}

/*
 * Hold reference to the recovery directory.
 */

void
nfsd4_init_recdir(char *rec_dirname)
{
D
David Howells 已提交
374 375
	const struct cred *original_cred;
	int status;
376 377 378 379 380 381

	printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
			rec_dirname);

	BUG_ON(rec_dir_init);

D
David Howells 已提交
382 383 384 385 386 387 388
	status = nfs4_save_creds(&original_cred);
	if (status < 0) {
		printk("NFSD: Unable to change credentials to find recovery"
		       " directory: error %d\n",
		       status);
		return;
	}
389

A
Al Viro 已提交
390
	status = kern_path(rec_dirname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
391 392 393
			&rec_dir);
	if (status)
		printk("NFSD: unable to find recovery directory %s\n",
394 395 396 397
				rec_dirname);

	if (!status)
		rec_dir_init = 1;
D
David Howells 已提交
398
	nfs4_reset_creds(original_cred);
399 400 401 402 403 404 405 406
}

void
nfsd4_shutdown_recdir(void)
{
	if (!rec_dir_init)
		return;
	rec_dir_init = 0;
A
Al Viro 已提交
407
	path_put(&rec_dir);
408
}