nfs4recover.c 9.8 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
/*
*  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.
*
*/

34
#include <linux/file.h>
35
#include <linux/slab.h>
36
#include <linux/namei.h>
N
NeilBrown 已提交
37
#include <linux/crypto.h>
A
Alexey Dobriyan 已提交
38
#include <linux/sched.h>
39 40 41

#include "nfsd.h"
#include "state.h"
42
#include "vfs.h"
N
NeilBrown 已提交
43 44 45

#define NFSDDBG_FACILITY                NFSDDBG_PROC

46
/* Globals */
47
static struct file *rec_file;
48
static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
49

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_jukebox;
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
void nfsd4_create_clid_dir(struct nfs4_client *clp)
121
{
D
David Howells 已提交
122
	const struct cred *original_cred;
123
	char *dname = clp->cl_recdir;
124
	struct dentry *dir, *dentry;
125 126 127 128
	int status;

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

129
	if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
130
		return;
131
	if (!rec_file)
132
		return;
D
David Howells 已提交
133 134
	status = nfs4_save_creds(&original_cred);
	if (status < 0)
135
		return;
136

137
	dir = rec_file->f_path.dentry;
138
	/* lock the parent */
139
	mutex_lock(&dir->d_inode->i_mutex);
140

141
	dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
142 143 144 145
	if (IS_ERR(dentry)) {
		status = PTR_ERR(dentry);
		goto out_unlock;
	}
146
	if (dentry->d_inode)
147 148 149 150 151 152 153 154
		/*
		 * In the 4.1 case, where we're called from
		 * reclaim_complete(), records from the previous reboot
		 * may still be left, so this is OK.
		 *
		 * In the 4.0 case, we should never get here; but we may
		 * as well be forgiving and just succeed silently.
		 */
155
		goto out_put;
156
	status = mnt_want_write_file(rec_file);
157 158
	if (status)
		goto out_put;
159
	status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU);
A
Al Viro 已提交
160
	mnt_drop_write_file(rec_file);
161 162 163
out_put:
	dput(dentry);
out_unlock:
164
	mutex_unlock(&dir->d_inode->i_mutex);
165
	if (status == 0)
166
		vfs_fsync(rec_file, 0);
167 168 169 170 171
	else
		printk(KERN_ERR "NFSD: failed to write recovery record"
				" (err %d); please check that %s exists"
				" and is writeable", status,
				user_recovery_dirname);
D
David Howells 已提交
172
	nfs4_reset_creds(original_cred);
173 174
}

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

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

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

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

static int
201
nfsd4_list_rec_dir(recdir_func *f)
202
{
D
David Howells 已提交
203
	const struct cred *original_cred;
204
	struct dentry *dir = rec_file->f_path.dentry;
205
	LIST_HEAD(names);
206 207
	int status;

D
David Howells 已提交
208 209 210
	status = nfs4_save_creds(&original_cred);
	if (status < 0)
		return status;
211

212 213 214 215 216 217 218
	status = vfs_llseek(rec_file, 0, SEEK_SET);
	if (status < 0) {
		nfs4_reset_creds(original_cred);
		return status;
	}

	status = vfs_readdir(rec_file, nfsd4_build_namelist, &names);
J
J. Bruce Fields 已提交
219
	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
220
	while (!list_empty(&names)) {
221
		struct name_list *entry;
222
		entry = list_entry(names.next, struct name_list, list);
223 224 225 226 227 228 229 230 231
		if (!status) {
			struct dentry *dentry;
			dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
			if (IS_ERR(dentry)) {
				status = PTR_ERR(dentry);
				break;
			}
			status = f(dir, dentry);
			dput(dentry);
232 233 234
		}
		list_del(&entry->list);
		kfree(entry);
235
	}
236
	mutex_unlock(&dir->d_inode->i_mutex);
D
David Howells 已提交
237
	nfs4_reset_creds(original_cred);
238 239 240
	return status;
}

241 242 243
static int
nfsd4_unlink_clid_dir(char *name, int namlen)
{
244
	struct dentry *dir, *dentry;
245 246 247 248
	int status;

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

249 250 251
	dir = rec_file->f_path.dentry;
	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
	dentry = lookup_one_len(name, dir, namlen);
252 253
	if (IS_ERR(dentry)) {
		status = PTR_ERR(dentry);
254
		goto out_unlock;
255 256 257 258
	}
	status = -ENOENT;
	if (!dentry->d_inode)
		goto out;
259
	status = vfs_rmdir(dir->d_inode, dentry);
260 261
out:
	dput(dentry);
262
out_unlock:
263
	mutex_unlock(&dir->d_inode->i_mutex);
264 265 266 267 268 269
	return status;
}

void
nfsd4_remove_clid_dir(struct nfs4_client *clp)
{
D
David Howells 已提交
270
	const struct cred *original_cred;
271 272
	int status;

273
	if (!rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
274 275
		return;

276
	status = mnt_want_write_file(rec_file);
277 278
	if (status)
		goto out;
279
	clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
D
David Howells 已提交
280 281 282 283 284

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

285
	status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
D
David Howells 已提交
286
	nfs4_reset_creds(original_cred);
287
	if (status == 0)
288
		vfs_fsync(rec_file, 0);
A
Al Viro 已提交
289
	mnt_drop_write_file(rec_file);
290
out:
291 292 293 294 295 296 297 298 299 300 301
	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;

302
	if (nfs4_has_reclaimed_state(child->d_name.name, false))
303
		return 0;
304

305
	status = vfs_rmdir(parent->d_inode, child);
306 307 308 309
	if (status)
		printk("failed to remove client recovery directory %s\n",
				child->d_name.name);
	/* Keep trying, success or failure: */
310
	return 0;
311 312 313 314 315 316
}

void
nfsd4_recdir_purge_old(void) {
	int status;

317
	if (!rec_file)
318
		return;
319
	status = mnt_want_write_file(rec_file);
320 321
	if (status)
		goto out;
322
	status = nfsd4_list_rec_dir(purge_old);
323
	if (status == 0)
324
		vfs_fsync(rec_file, 0);
A
Al Viro 已提交
325
	mnt_drop_write_file(rec_file);
326
out:
327 328
	if (status)
		printk("nfsd4: failed to purge old clients from recovery"
329
			" directory %s\n", rec_file->f_path.dentry->d_name.name);
330 331
}

332 333 334 335 336 337 338
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: */
339
		return 0;
340 341
	}
	nfs4_client_to_reclaim(child->d_name.name);
342
	return 0;
343 344 345 346 347 348
}

int
nfsd4_recdir_load(void) {
	int status;

349 350 351
	if (!rec_file)
		return 0;

352
	status = nfsd4_list_rec_dir(load_recdir);
353 354
	if (status)
		printk("nfsd4: failed loading clients from recovery"
355
			" directory %s\n", rec_file->f_path.dentry->d_name.name);
356 357 358 359 360 361 362 363
	return status;
}

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

void
364
nfsd4_init_recdir()
365
{
D
David Howells 已提交
366 367
	const struct cred *original_cred;
	int status;
368 369

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

372
	BUG_ON(rec_file);
373

D
David Howells 已提交
374 375 376 377 378 379 380
	status = nfs4_save_creds(&original_cred);
	if (status < 0) {
		printk("NFSD: Unable to change credentials to find recovery"
		       " directory: error %d\n",
		       status);
		return;
	}
381

382
	rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0);
383
	if (IS_ERR(rec_file)) {
384
		printk("NFSD: unable to find recovery directory %s\n",
385
				user_recovery_dirname);
386 387
		rec_file = NULL;
	}
388

D
David Howells 已提交
389
	nfs4_reset_creds(original_cred);
390 391 392 393 394
}

void
nfsd4_shutdown_recdir(void)
{
395
	if (!rec_file)
396
		return;
397 398
	fput(rec_file);
	rec_file = NULL;
399
}
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

/*
 * Change the NFSv4 recovery directory to recdir.
 */
int
nfs4_reset_recoverydir(char *recdir)
{
	int status;
	struct path path;

	status = kern_path(recdir, LOOKUP_FOLLOW, &path);
	if (status)
		return status;
	status = -ENOTDIR;
	if (S_ISDIR(path.dentry->d_inode->i_mode)) {
		strcpy(user_recovery_dirname, recdir);
		status = 0;
	}
	path_put(&path);
	return status;
}

char *
nfs4_recoverydir(void)
{
	return user_recovery_dirname;
}