nfs4recover.c 9.4 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

D
David Howells 已提交
49 50
static int
nfs4_save_creds(const struct cred **original_creds)
51
{
D
David Howells 已提交
52 53 54 55 56 57 58 59 60 61 62
	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;
63 64 65
}

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

N
NeilBrown 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84
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';
}

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

	dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
			clname->len, clname->data);
95 96 97 98 99
	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 已提交
100 101 102 103
	cksum.data = kmalloc(cksum.len, GFP_KERNEL);
	if (cksum.data == NULL)
 		goto out;

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

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

	md5_to_hex(dname, cksum.data);

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

119 120 121
int
nfsd4_create_clid_dir(struct nfs4_client *clp)
{
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 (!rec_file || clp->cl_firststate)
130 131
		return 0;

D
David Howells 已提交
132 133 134
	status = nfs4_save_creds(&original_cred);
	if (status < 0)
		return status;
135

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

140
	dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
141 142 143 144 145 146 147 148 149
	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;
	}
150
	status = mnt_want_write(rec_file->f_path.mnt);
151 152
	if (status)
		goto out_put;
153 154
	status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU);
	mnt_drop_write(rec_file->f_path.mnt);
155 156 157
out_put:
	dput(dentry);
out_unlock:
158
	mutex_unlock(&dir->d_inode->i_mutex);
159 160
	if (status == 0) {
		clp->cl_firststate = 1;
161
		vfs_fsync(rec_file, rec_file->f_path.dentry, 0);
162
	}
D
David Howells 已提交
163
	nfs4_reset_creds(original_cred);
164 165 166 167
	dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status);
	return status;
}

168 169
typedef int (recdir_func)(struct dentry *, struct dentry *);

170 171
struct name_list {
	char name[HEXDIR_LEN];
172 173 174 175
	struct list_head list;
};

static int
176
nfsd4_build_namelist(void *arg, const char *name, int namlen,
177
		loff_t offset, u64 ino, unsigned int d_type)
178
{
179 180
	struct list_head *names = arg;
	struct name_list *entry;
181

182
	if (namlen != HEXDIR_LEN - 1)
183
		return 0;
184 185
	entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
	if (entry == NULL)
186
		return -ENOMEM;
187 188 189
	memcpy(entry->name, name, HEXDIR_LEN - 1);
	entry->name[HEXDIR_LEN - 1] = '\0';
	list_add(&entry->list, names);
190 191 192 193 194 195
	return 0;
}

static int
nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
{
D
David Howells 已提交
196
	const struct cred *original_cred;
197
	struct file *filp;
198 199 200
	LIST_HEAD(names);
	struct name_list *entry;
	struct dentry *dentry;
201 202
	int status;

203
	if (!rec_file)
204 205
		return 0;

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

210
	filp = dentry_open(dget(dir), mntget(rec_file->f_path.mnt), O_RDONLY,
211
			   current_cred());
212 213 214
	status = PTR_ERR(filp);
	if (IS_ERR(filp))
		goto out;
215
	status = vfs_readdir(filp, nfsd4_build_namelist, &names);
216
	fput(filp);
J
J. Bruce Fields 已提交
217
	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
218 219 220 221 222 223
	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);
224
			break;
225 226 227
		}
		status = f(dir, dentry);
		dput(dentry);
228
		if (status)
229
			break;
230 231
		list_del(&entry->list);
		kfree(entry);
232
	}
233
	mutex_unlock(&dir->d_inode->i_mutex);
234
out:
235 236 237 238
	while (!list_empty(&names)) {
		entry = list_entry(names.next, struct name_list, list);
		list_del(&entry->list);
		kfree(entry);
239
	}
D
David Howells 已提交
240
	nfs4_reset_creds(original_cred);
241 242 243
	return status;
}

244 245 246
static int
nfsd4_unlink_clid_dir(char *name, int namlen)
{
247
	struct dentry *dir, *dentry;
248 249 250 251
	int status;

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

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

void
nfsd4_remove_clid_dir(struct nfs4_client *clp)
{
D
David Howells 已提交
273
	const struct cred *original_cred;
274 275
	int status;

276
	if (!rec_file || !clp->cl_firststate)
277 278
		return;

279
	status = mnt_want_write(rec_file->f_path.mnt);
280 281
	if (status)
		goto out;
282
	clp->cl_firststate = 0;
D
David Howells 已提交
283 284 285 286 287

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

288
	status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
D
David Howells 已提交
289
	nfs4_reset_creds(original_cred);
290
	if (status == 0)
291 292
		vfs_fsync(rec_file, rec_file->f_path.dentry, 0);
	mnt_drop_write(rec_file->f_path.mnt);
293
out:
294 295 296 297 298 299 300 301 302 303 304
	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;

305 306
	/* note: we currently use this path only for minorversion 0 */
	if (nfs4_has_reclaimed_state(child->d_name.name, false))
307
		return 0;
308

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

void
nfsd4_recdir_purge_old(void) {
	int status;

321
	if (!rec_file)
322
		return;
323
	status = mnt_want_write(rec_file->f_path.mnt);
324 325
	if (status)
		goto out;
326
	status = nfsd4_list_rec_dir(rec_file->f_path.dentry, purge_old);
327
	if (status == 0)
328 329
		vfs_fsync(rec_file, rec_file->f_path.dentry, 0);
	mnt_drop_write(rec_file->f_path.mnt);
330
out:
331 332
	if (status)
		printk("nfsd4: failed to purge old clients from recovery"
333
			" directory %s\n", rec_file->f_path.dentry->d_name.name);
334 335
}

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

int
nfsd4_recdir_load(void) {
	int status;

353 354 355 356
	if (!rec_file)
		return 0;

	status = nfsd4_list_rec_dir(rec_file->f_path.dentry, load_recdir);
357 358
	if (status)
		printk("nfsd4: failed loading clients from recovery"
359
			" directory %s\n", rec_file->f_path.dentry->d_name.name);
360 361 362 363 364 365 366 367 368 369
	return status;
}

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

void
nfsd4_init_recdir(char *rec_dirname)
{
D
David Howells 已提交
370 371
	const struct cred *original_cred;
	int status;
372 373 374 375

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

376
	BUG_ON(rec_file);
377

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

386 387
	rec_file = filp_open(rec_dirname, O_RDONLY | O_DIRECTORY, 0);
	if (IS_ERR(rec_file)) {
388
		printk("NFSD: unable to find recovery directory %s\n",
389
				rec_dirname);
390 391
		rec_file = NULL;
	}
392

D
David Howells 已提交
393
	nfs4_reset_creds(original_cred);
394 395 396 397 398
}

void
nfsd4_shutdown_recdir(void)
{
399
	if (!rec_file)
400
		return;
401 402
	fput(rec_file);
	rec_file = NULL;
403
}