1/*
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
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 <asm/scatterlist.h>
47#include <linux/crypto.h>
48#include <linux/sched.h>
49
50#define NFSDDBG_FACILITY                NFSDDBG_PROC
51
52/* Globals */
53static struct nameidata rec_dir;
54static int rec_dir_init = 0;
55
56static void
57nfs4_save_user(uid_t *saveuid, gid_t *savegid)
58{
59	*saveuid = current->fsuid;
60	*savegid = current->fsgid;
61	current->fsuid = 0;
62	current->fsgid = 0;
63}
64
65static void
66nfs4_reset_user(uid_t saveuid, gid_t savegid)
67{
68	current->fsuid = saveuid;
69	current->fsgid = savegid;
70}
71
72static void
73md5_to_hex(char *out, char *md5)
74{
75	int i;
76
77	for (i=0; i<16; i++) {
78		unsigned char c = md5[i];
79
80		*out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
81		*out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
82	}
83	*out = '\0';
84}
85
86__be32
87nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
88{
89	struct xdr_netobj cksum;
90	struct hash_desc desc;
91	struct scatterlist sg[1];
92	__be32 status = nfserr_resource;
93
94	dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
95			clname->len, clname->data);
96	desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
97	desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
98	if (IS_ERR(desc.tfm))
99		goto out_no_tfm;
100	cksum.len = crypto_hash_digestsize(desc.tfm);
101	cksum.data = kmalloc(cksum.len, GFP_KERNEL);
102	if (cksum.data == NULL)
103 		goto out;
104
105	sg[0].page = virt_to_page(clname->data);
106	sg[0].offset = offset_in_page(clname->data);
107	sg[0].length = clname->len;
108
109	if (crypto_hash_digest(&desc, sg, sg->length, cksum.data))
110		goto out;
111
112	md5_to_hex(dname, cksum.data);
113
114	kfree(cksum.data);
115	status = nfs_ok;
116out:
117	crypto_free_hash(desc.tfm);
118out_no_tfm:
119	return status;
120}
121
122static void
123nfsd4_sync_rec_dir(void)
124{
125	mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
126	nfsd_sync_dir(rec_dir.dentry);
127	mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
128}
129
130int
131nfsd4_create_clid_dir(struct nfs4_client *clp)
132{
133	char *dname = clp->cl_recdir;
134	struct dentry *dentry;
135	uid_t uid;
136	gid_t gid;
137	int status;
138
139	dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
140
141	if (!rec_dir_init || clp->cl_firststate)
142		return 0;
143
144	nfs4_save_user(&uid, &gid);
145
146	/* lock the parent */
147	mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
148
149	dentry = lookup_one_len(dname, rec_dir.dentry, HEXDIR_LEN-1);
150	if (IS_ERR(dentry)) {
151		status = PTR_ERR(dentry);
152		goto out_unlock;
153	}
154	status = -EEXIST;
155	if (dentry->d_inode) {
156		dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
157		goto out_put;
158	}
159	status = vfs_mkdir(rec_dir.dentry->d_inode, dentry, S_IRWXU);
160out_put:
161	dput(dentry);
162out_unlock:
163	mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
164	if (status == 0) {
165		clp->cl_firststate = 1;
166		nfsd4_sync_rec_dir();
167	}
168	nfs4_reset_user(uid, gid);
169	dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status);
170	return status;
171}
172
173typedef int (recdir_func)(struct dentry *, struct dentry *);
174
175struct dentry_list {
176	struct dentry *dentry;
177	struct list_head list;
178};
179
180struct dentry_list_arg {
181	struct list_head dentries;
182	struct dentry *parent;
183};
184
185static int
186nfsd4_build_dentrylist(void *arg, const char *name, int namlen,
187		loff_t offset, u64 ino, unsigned int d_type)
188{
189	struct dentry_list_arg *dla = arg;
190	struct list_head *dentries = &dla->dentries;
191	struct dentry *parent = dla->parent;
192	struct dentry *dentry;
193	struct dentry_list *child;
194
195	if (name && isdotent(name, namlen))
196		return 0;
197	dentry = lookup_one_len(name, parent, namlen);
198	if (IS_ERR(dentry))
199		return PTR_ERR(dentry);
200	child = kmalloc(sizeof(*child), GFP_KERNEL);
201	if (child == NULL)
202		return -ENOMEM;
203	child->dentry = dentry;
204	list_add(&child->list, dentries);
205	return 0;
206}
207
208static int
209nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
210{
211	struct file *filp;
212	struct dentry_list_arg dla = {
213		.parent = dir,
214	};
215	struct list_head *dentries = &dla.dentries;
216	struct dentry_list *child;
217	uid_t uid;
218	gid_t gid;
219	int status;
220
221	if (!rec_dir_init)
222		return 0;
223
224	nfs4_save_user(&uid, &gid);
225
226	filp = dentry_open(dget(dir), mntget(rec_dir.mnt), O_RDONLY);
227	status = PTR_ERR(filp);
228	if (IS_ERR(filp))
229		goto out;
230	INIT_LIST_HEAD(dentries);
231	status = vfs_readdir(filp, nfsd4_build_dentrylist, &dla);
232	fput(filp);
233	while (!list_empty(dentries)) {
234		child = list_entry(dentries->next, struct dentry_list, list);
235		status = f(dir, child->dentry);
236		if (status)
237			goto out;
238		list_del(&child->list);
239		dput(child->dentry);
240		kfree(child);
241	}
242out:
243	while (!list_empty(dentries)) {
244		child = list_entry(dentries->next, struct dentry_list, list);
245		list_del(&child->list);
246		dput(child->dentry);
247		kfree(child);
248	}
249	nfs4_reset_user(uid, gid);
250	return status;
251}
252
253static int
254nfsd4_remove_clid_file(struct dentry *dir, struct dentry *dentry)
255{
256	int status;
257
258	if (!S_ISREG(dir->d_inode->i_mode)) {
259		printk("nfsd4: non-file found in client recovery directory\n");
260		return -EINVAL;
261	}
262	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
263	status = vfs_unlink(dir->d_inode, dentry);
264	mutex_unlock(&dir->d_inode->i_mutex);
265	return status;
266}
267
268static int
269nfsd4_clear_clid_dir(struct dentry *dir, struct dentry *dentry)
270{
271	int status;
272
273	/* For now this directory should already be empty, but we empty it of
274	 * any regular files anyway, just in case the directory was created by
275	 * a kernel from the future.... */
276	nfsd4_list_rec_dir(dentry, nfsd4_remove_clid_file);
277	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
278	status = vfs_rmdir(dir->d_inode, dentry);
279	mutex_unlock(&dir->d_inode->i_mutex);
280	return status;
281}
282
283static int
284nfsd4_unlink_clid_dir(char *name, int namlen)
285{
286	struct dentry *dentry;
287	int status;
288
289	dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
290
291	mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
292	dentry = lookup_one_len(name, rec_dir.dentry, namlen);
293	mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
294	if (IS_ERR(dentry)) {
295		status = PTR_ERR(dentry);
296		return status;
297	}
298	status = -ENOENT;
299	if (!dentry->d_inode)
300		goto out;
301
302	status = nfsd4_clear_clid_dir(rec_dir.dentry, dentry);
303out:
304	dput(dentry);
305	return status;
306}
307
308void
309nfsd4_remove_clid_dir(struct nfs4_client *clp)
310{
311	uid_t uid;
312	gid_t gid;
313	int status;
314
315	if (!rec_dir_init || !clp->cl_firststate)
316		return;
317
318	clp->cl_firststate = 0;
319	nfs4_save_user(&uid, &gid);
320	status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
321	nfs4_reset_user(uid, gid);
322	if (status == 0)
323		nfsd4_sync_rec_dir();
324	if (status)
325		printk("NFSD: Failed to remove expired client state directory"
326				" %.*s\n", HEXDIR_LEN, clp->cl_recdir);
327	return;
328}
329
330static int
331purge_old(struct dentry *parent, struct dentry *child)
332{
333	int status;
334
335	if (nfs4_has_reclaimed_state(child->d_name.name))
336		return 0;
337
338	status = nfsd4_clear_clid_dir(parent, child);
339	if (status)
340		printk("failed to remove client recovery directory %s\n",
341				child->d_name.name);
342	/* Keep trying, success or failure: */
343	return 0;
344}
345
346void
347nfsd4_recdir_purge_old(void) {
348	int status;
349
350	if (!rec_dir_init)
351		return;
352	status = nfsd4_list_rec_dir(rec_dir.dentry, purge_old);
353	if (status == 0)
354		nfsd4_sync_rec_dir();
355	if (status)
356		printk("nfsd4: failed to purge old clients from recovery"
357			" directory %s\n", rec_dir.dentry->d_name.name);
358	return;
359}
360
361static int
362load_recdir(struct dentry *parent, struct dentry *child)
363{
364	if (child->d_name.len != HEXDIR_LEN - 1) {
365		printk("nfsd4: illegal name %s in recovery directory\n",
366				child->d_name.name);
367		/* Keep trying; maybe the others are OK: */
368		return 0;
369	}
370	nfs4_client_to_reclaim(child->d_name.name);
371	return 0;
372}
373
374int
375nfsd4_recdir_load(void) {
376	int status;
377
378	status = nfsd4_list_rec_dir(rec_dir.dentry, load_recdir);
379	if (status)
380		printk("nfsd4: failed loading clients from recovery"
381			" directory %s\n", rec_dir.dentry->d_name.name);
382	return status;
383}
384
385/*
386 * Hold reference to the recovery directory.
387 */
388
389void
390nfsd4_init_recdir(char *rec_dirname)
391{
392	uid_t			uid = 0;
393	gid_t			gid = 0;
394	int 			status;
395
396	printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
397			rec_dirname);
398
399	BUG_ON(rec_dir_init);
400
401	nfs4_save_user(&uid, &gid);
402
403	status = path_lookup(rec_dirname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
404			&rec_dir);
405	if (status)
406		printk("NFSD: unable to find recovery directory %s\n",
407				rec_dirname);
408
409	if (!status)
410		rec_dir_init = 1;
411	nfs4_reset_user(uid, gid);
412}
413
414void
415nfsd4_shutdown_recdir(void)
416{
417	if (!rec_dir_init)
418		return;
419	rec_dir_init = 0;
420	path_release(&rec_dir);
421}
422