blob: cec62ed2a064cf5a3ef046d80edbf3905377c930 [file] [log] [blame]
NeilBrowna55370a2005-06-23 22:03:52 -07001/*
NeilBrowna55370a2005-06-23 22:03:52 -07002* Copyright (c) 2004 The Regents of the University of Michigan.
Jeff Laytonf3f80142012-03-21 09:52:07 -04003* Copyright (c) 2012 Jeff Layton <jlayton@redhat.com>
NeilBrowna55370a2005-06-23 22:03:52 -07004* All rights reserved.
5*
6* Andy Adamson <andros@citi.umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
NeilBrown190e4fb2005-06-23 22:04:25 -070035#include <linux/file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
NeilBrown190e4fb2005-06-23 22:04:25 -070037#include <linux/namei.h>
NeilBrowna55370a2005-06-23 22:03:52 -070038#include <linux/crypto.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040039#include <linux/sched.h>
Jeff Laytonf3f80142012-03-21 09:52:07 -040040#include <linux/fs.h>
41#include <net/net_namespace.h>
42#include <linux/sunrpc/rpc_pipe_fs.h>
43#include <linux/sunrpc/clnt.h>
44#include <linux/nfsd/cld.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045
46#include "nfsd.h"
47#include "state.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050048#include "vfs.h"
Jeff Laytonf3f80142012-03-21 09:52:07 -040049#include "netns.h"
NeilBrowna55370a2005-06-23 22:03:52 -070050
51#define NFSDDBG_FACILITY NFSDDBG_PROC
52
Jeff Layton2a4317c2012-03-21 16:42:43 -040053/* Declarations */
54struct nfsd4_client_tracking_ops {
55 int (*init)(struct net *);
56 void (*exit)(struct net *);
57 void (*create)(struct nfs4_client *);
58 void (*remove)(struct nfs4_client *);
59 int (*check)(struct nfs4_client *);
60 void (*grace_done)(struct net *, time_t);
61};
62
NeilBrown190e4fb2005-06-23 22:04:25 -070063/* Globals */
Christoph Hellwige970a572010-03-22 17:32:14 +010064static struct file *rec_file;
J. Bruce Fields48483bf2011-08-26 20:40:28 -040065static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
Jeff Layton2a4317c2012-03-21 16:42:43 -040066static struct nfsd4_client_tracking_ops *client_tracking_ops;
NeilBrown190e4fb2005-06-23 22:04:25 -070067
David Howellsd84f4f92008-11-14 10:39:23 +110068static int
69nfs4_save_creds(const struct cred **original_creds)
NeilBrown190e4fb2005-06-23 22:04:25 -070070{
David Howellsd84f4f92008-11-14 10:39:23 +110071 struct cred *new;
72
73 new = prepare_creds();
74 if (!new)
75 return -ENOMEM;
76
77 new->fsuid = 0;
78 new->fsgid = 0;
79 *original_creds = override_creds(new);
80 put_cred(new);
81 return 0;
NeilBrown190e4fb2005-06-23 22:04:25 -070082}
83
84static void
David Howellsd84f4f92008-11-14 10:39:23 +110085nfs4_reset_creds(const struct cred *original)
NeilBrown190e4fb2005-06-23 22:04:25 -070086{
David Howellsd84f4f92008-11-14 10:39:23 +110087 revert_creds(original);
NeilBrown190e4fb2005-06-23 22:04:25 -070088}
89
NeilBrowna55370a2005-06-23 22:03:52 -070090static void
91md5_to_hex(char *out, char *md5)
92{
93 int i;
94
95 for (i=0; i<16; i++) {
96 unsigned char c = md5[i];
97
98 *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
99 *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
100 }
101 *out = '\0';
102}
103
Al Virob37ad282006-10-19 23:28:59 -0700104__be32
NeilBrowna55370a2005-06-23 22:03:52 -0700105nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
106{
107 struct xdr_netobj cksum;
Herbert Xu35058682006-08-24 19:10:20 +1000108 struct hash_desc desc;
Jens Axboe60c74f82007-10-22 19:43:30 +0200109 struct scatterlist sg;
J. Bruce Fields3e772462011-08-10 19:07:33 -0400110 __be32 status = nfserr_jukebox;
NeilBrowna55370a2005-06-23 22:03:52 -0700111
112 dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
113 clname->len, clname->data);
Herbert Xu35058682006-08-24 19:10:20 +1000114 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
115 desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
116 if (IS_ERR(desc.tfm))
117 goto out_no_tfm;
118 cksum.len = crypto_hash_digestsize(desc.tfm);
NeilBrowna55370a2005-06-23 22:03:52 -0700119 cksum.data = kmalloc(cksum.len, GFP_KERNEL);
120 if (cksum.data == NULL)
121 goto out;
NeilBrowna55370a2005-06-23 22:03:52 -0700122
Jens Axboe60c74f82007-10-22 19:43:30 +0200123 sg_init_one(&sg, clname->data, clname->len);
NeilBrowna55370a2005-06-23 22:03:52 -0700124
Jens Axboe60c74f82007-10-22 19:43:30 +0200125 if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data))
Herbert Xu35058682006-08-24 19:10:20 +1000126 goto out;
NeilBrowna55370a2005-06-23 22:03:52 -0700127
128 md5_to_hex(dname, cksum.data);
129
NeilBrowna55370a2005-06-23 22:03:52 -0700130 status = nfs_ok;
131out:
Krishna Kumar2bd9e7b2008-10-20 11:47:09 +0530132 kfree(cksum.data);
Herbert Xu35058682006-08-24 19:10:20 +1000133 crypto_free_hash(desc.tfm);
134out_no_tfm:
NeilBrowna55370a2005-06-23 22:03:52 -0700135 return status;
136}
NeilBrown190e4fb2005-06-23 22:04:25 -0700137
Jeff Layton2a4317c2012-03-21 16:42:43 -0400138static void
139nfsd4_create_clid_dir(struct nfs4_client *clp)
NeilBrownc7b9a452005-06-23 22:04:30 -0700140{
David Howellsd84f4f92008-11-14 10:39:23 +1100141 const struct cred *original_cred;
NeilBrownc7b9a452005-06-23 22:04:30 -0700142 char *dname = clp->cl_recdir;
Christoph Hellwige970a572010-03-22 17:32:14 +0100143 struct dentry *dir, *dentry;
NeilBrownc7b9a452005-06-23 22:04:30 -0700144 int status;
145
146 dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
147
Jeff Laytona52d7262012-03-21 09:52:02 -0400148 if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
J. Bruce Fields7a6ef8c2012-01-05 15:38:41 -0500149 return;
J. Bruce Fieldsb8548892012-01-02 17:49:12 -0500150 if (!rec_file)
J. Bruce Fields7a6ef8c2012-01-05 15:38:41 -0500151 return;
David Howellsd84f4f92008-11-14 10:39:23 +1100152 status = nfs4_save_creds(&original_cred);
153 if (status < 0)
J. Bruce Fields7a6ef8c2012-01-05 15:38:41 -0500154 return;
NeilBrownc7b9a452005-06-23 22:04:30 -0700155
Christoph Hellwige970a572010-03-22 17:32:14 +0100156 dir = rec_file->f_path.dentry;
NeilBrownc7b9a452005-06-23 22:04:30 -0700157 /* lock the parent */
Christoph Hellwige970a572010-03-22 17:32:14 +0100158 mutex_lock(&dir->d_inode->i_mutex);
NeilBrownc7b9a452005-06-23 22:04:30 -0700159
Christoph Hellwige970a572010-03-22 17:32:14 +0100160 dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
NeilBrownc7b9a452005-06-23 22:04:30 -0700161 if (IS_ERR(dentry)) {
162 status = PTR_ERR(dentry);
163 goto out_unlock;
164 }
Boaz Harrosh6577aac2011-08-12 17:30:12 -0700165 if (dentry->d_inode)
J. Bruce Fieldsaec39682012-01-02 17:30:05 -0500166 /*
167 * In the 4.1 case, where we're called from
168 * reclaim_complete(), records from the previous reboot
169 * may still be left, so this is OK.
170 *
171 * In the 4.0 case, we should never get here; but we may
172 * as well be forgiving and just succeed silently.
173 */
NeilBrownc7b9a452005-06-23 22:04:30 -0700174 goto out_put;
Al Viroa561be72011-11-23 11:57:51 -0500175 status = mnt_want_write_file(rec_file);
Dave Hansen463c3192008-02-15 14:37:57 -0800176 if (status)
177 goto out_put;
Christoph Hellwige970a572010-03-22 17:32:14 +0100178 status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU);
Al Viro2a79f172011-12-09 08:06:57 -0500179 mnt_drop_write_file(rec_file);
NeilBrownc7b9a452005-06-23 22:04:30 -0700180out_put:
181 dput(dentry);
182out_unlock:
Christoph Hellwige970a572010-03-22 17:32:14 +0100183 mutex_unlock(&dir->d_inode->i_mutex);
Boaz Harrosh6577aac2011-08-12 17:30:12 -0700184 if (status == 0)
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100185 vfs_fsync(rec_file, 0);
Boaz Harrosh6577aac2011-08-12 17:30:12 -0700186 else
187 printk(KERN_ERR "NFSD: failed to write recovery record"
188 " (err %d); please check that %s exists"
189 " and is writeable", status,
190 user_recovery_dirname);
David Howellsd84f4f92008-11-14 10:39:23 +1100191 nfs4_reset_creds(original_cred);
NeilBrownc7b9a452005-06-23 22:04:30 -0700192}
193
NeilBrown190e4fb2005-06-23 22:04:25 -0700194typedef int (recdir_func)(struct dentry *, struct dentry *);
195
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400196struct name_list {
197 char name[HEXDIR_LEN];
NeilBrown190e4fb2005-06-23 22:04:25 -0700198 struct list_head list;
199};
200
NeilBrown190e4fb2005-06-23 22:04:25 -0700201static int
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400202nfsd4_build_namelist(void *arg, const char *name, int namlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700203 loff_t offset, u64 ino, unsigned int d_type)
NeilBrown190e4fb2005-06-23 22:04:25 -0700204{
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400205 struct list_head *names = arg;
206 struct name_list *entry;
NeilBrown190e4fb2005-06-23 22:04:25 -0700207
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400208 if (namlen != HEXDIR_LEN - 1)
Al Virob37ad282006-10-19 23:28:59 -0700209 return 0;
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400210 entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
211 if (entry == NULL)
NeilBrown190e4fb2005-06-23 22:04:25 -0700212 return -ENOMEM;
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400213 memcpy(entry->name, name, HEXDIR_LEN - 1);
214 entry->name[HEXDIR_LEN - 1] = '\0';
215 list_add(&entry->list, names);
NeilBrown190e4fb2005-06-23 22:04:25 -0700216 return 0;
217}
218
219static int
Al Viro5b4b2992011-07-07 18:43:21 -0400220nfsd4_list_rec_dir(recdir_func *f)
NeilBrown190e4fb2005-06-23 22:04:25 -0700221{
David Howellsd84f4f92008-11-14 10:39:23 +1100222 const struct cred *original_cred;
Al Viro5b4b2992011-07-07 18:43:21 -0400223 struct dentry *dir = rec_file->f_path.dentry;
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400224 LIST_HEAD(names);
NeilBrown190e4fb2005-06-23 22:04:25 -0700225 int status;
226
David Howellsd84f4f92008-11-14 10:39:23 +1100227 status = nfs4_save_creds(&original_cred);
228 if (status < 0)
229 return status;
NeilBrown190e4fb2005-06-23 22:04:25 -0700230
Al Viro5b4b2992011-07-07 18:43:21 -0400231 status = vfs_llseek(rec_file, 0, SEEK_SET);
232 if (status < 0) {
233 nfs4_reset_creds(original_cred);
234 return status;
235 }
236
237 status = vfs_readdir(rec_file, nfsd4_build_namelist, &names);
J. Bruce Fields8daed1e2009-05-11 16:10:19 -0400238 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400239 while (!list_empty(&names)) {
Al Viro5b4b2992011-07-07 18:43:21 -0400240 struct name_list *entry;
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400241 entry = list_entry(names.next, struct name_list, list);
Al Viro5b4b2992011-07-07 18:43:21 -0400242 if (!status) {
243 struct dentry *dentry;
244 dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
245 if (IS_ERR(dentry)) {
246 status = PTR_ERR(dentry);
247 break;
248 }
249 status = f(dir, dentry);
250 dput(dentry);
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400251 }
J. Bruce Fields05f4f672009-03-13 16:02:59 -0400252 list_del(&entry->list);
253 kfree(entry);
NeilBrown190e4fb2005-06-23 22:04:25 -0700254 }
David Woodhouse2f9092e2009-04-20 23:18:37 +0100255 mutex_unlock(&dir->d_inode->i_mutex);
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{
Christoph Hellwige970a572010-03-22 17:32:14 +0100263 struct dentry *dir, *dentry;
NeilBrownc7b9a452005-06-23 22:04:30 -0700264 int status;
265
266 dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
267
Christoph Hellwige970a572010-03-22 17:32:14 +0100268 dir = rec_file->f_path.dentry;
269 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
270 dentry = lookup_one_len(name, dir, namlen);
NeilBrownc7b9a452005-06-23 22:04:30 -0700271 if (IS_ERR(dentry)) {
272 status = PTR_ERR(dentry);
David Woodhouse2f9092e2009-04-20 23:18:37 +0100273 goto out_unlock;
NeilBrownc7b9a452005-06-23 22:04:30 -0700274 }
275 status = -ENOENT;
276 if (!dentry->d_inode)
277 goto out;
Christoph Hellwige970a572010-03-22 17:32:14 +0100278 status = vfs_rmdir(dir->d_inode, dentry);
NeilBrownc7b9a452005-06-23 22:04:30 -0700279out:
280 dput(dentry);
David Woodhouse2f9092e2009-04-20 23:18:37 +0100281out_unlock:
Christoph Hellwige970a572010-03-22 17:32:14 +0100282 mutex_unlock(&dir->d_inode->i_mutex);
NeilBrownc7b9a452005-06-23 22:04:30 -0700283 return status;
284}
285
Jeff Layton2a4317c2012-03-21 16:42:43 -0400286static void
NeilBrownc7b9a452005-06-23 22:04:30 -0700287nfsd4_remove_clid_dir(struct nfs4_client *clp)
288{
David Howellsd84f4f92008-11-14 10:39:23 +1100289 const struct cred *original_cred;
NeilBrownc7b9a452005-06-23 22:04:30 -0700290 int status;
291
Jeff Laytona52d7262012-03-21 09:52:02 -0400292 if (!rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
NeilBrownc7b9a452005-06-23 22:04:30 -0700293 return;
294
Al Viroa561be72011-11-23 11:57:51 -0500295 status = mnt_want_write_file(rec_file);
Dave Hansen06227532008-02-15 14:37:34 -0800296 if (status)
297 goto out;
Jeff Laytona52d7262012-03-21 09:52:02 -0400298 clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
David Howellsd84f4f92008-11-14 10:39:23 +1100299
300 status = nfs4_save_creds(&original_cred);
301 if (status < 0)
302 goto out;
303
NeilBrownc7b9a452005-06-23 22:04:30 -0700304 status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
David Howellsd84f4f92008-11-14 10:39:23 +1100305 nfs4_reset_creds(original_cred);
NeilBrownc7b9a452005-06-23 22:04:30 -0700306 if (status == 0)
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100307 vfs_fsync(rec_file, 0);
Al Viro2a79f172011-12-09 08:06:57 -0500308 mnt_drop_write_file(rec_file);
Dave Hansen06227532008-02-15 14:37:34 -0800309out:
NeilBrownc7b9a452005-06-23 22:04:30 -0700310 if (status)
311 printk("NFSD: Failed to remove expired client state directory"
312 " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
NeilBrownc7b9a452005-06-23 22:04:30 -0700313}
314
315static int
316purge_old(struct dentry *parent, struct dentry *child)
317{
318 int status;
319
Andy Adamsona1bcecd2009-04-03 08:28:05 +0300320 if (nfs4_has_reclaimed_state(child->d_name.name, false))
Al Virob37ad282006-10-19 23:28:59 -0700321 return 0;
NeilBrownc7b9a452005-06-23 22:04:30 -0700322
David Woodhouse2f9092e2009-04-20 23:18:37 +0100323 status = vfs_rmdir(parent->d_inode, child);
NeilBrownc7b9a452005-06-23 22:04:30 -0700324 if (status)
325 printk("failed to remove client recovery directory %s\n",
326 child->d_name.name);
327 /* Keep trying, success or failure: */
Al Virob37ad282006-10-19 23:28:59 -0700328 return 0;
NeilBrownc7b9a452005-06-23 22:04:30 -0700329}
330
Jeff Layton2a4317c2012-03-21 16:42:43 -0400331static void
332nfsd4_recdir_purge_old(struct net *net, time_t boot_time)
333{
NeilBrownc7b9a452005-06-23 22:04:30 -0700334 int status;
335
Christoph Hellwige970a572010-03-22 17:32:14 +0100336 if (!rec_file)
NeilBrownc7b9a452005-06-23 22:04:30 -0700337 return;
Al Viroa561be72011-11-23 11:57:51 -0500338 status = mnt_want_write_file(rec_file);
Dave Hansen06227532008-02-15 14:37:34 -0800339 if (status)
340 goto out;
Al Viro5b4b2992011-07-07 18:43:21 -0400341 status = nfsd4_list_rec_dir(purge_old);
NeilBrownc7b9a452005-06-23 22:04:30 -0700342 if (status == 0)
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100343 vfs_fsync(rec_file, 0);
Al Viro2a79f172011-12-09 08:06:57 -0500344 mnt_drop_write_file(rec_file);
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"
Christoph Hellwige970a572010-03-22 17:32:14 +0100348 " directory %s\n", rec_file->f_path.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
Jeff Layton2a4317c2012-03-21 16:42:43 -0400364static int
NeilBrown190e4fb2005-06-23 22:04:25 -0700365nfsd4_recdir_load(void) {
366 int status;
367
Christoph Hellwige970a572010-03-22 17:32:14 +0100368 if (!rec_file)
369 return 0;
370
Al Viro5b4b2992011-07-07 18:43:21 -0400371 status = nfsd4_list_rec_dir(load_recdir);
NeilBrown190e4fb2005-06-23 22:04:25 -0700372 if (status)
373 printk("nfsd4: failed loading clients from recovery"
Christoph Hellwige970a572010-03-22 17:32:14 +0100374 " directory %s\n", rec_file->f_path.dentry->d_name.name);
NeilBrown190e4fb2005-06-23 22:04:25 -0700375 return status;
376}
377
378/*
379 * Hold reference to the recovery directory.
380 */
381
Jeff Layton2a4317c2012-03-21 16:42:43 -0400382static int
383nfsd4_init_recdir(void)
NeilBrown190e4fb2005-06-23 22:04:25 -0700384{
David Howellsd84f4f92008-11-14 10:39:23 +1100385 const struct cred *original_cred;
386 int status;
NeilBrown190e4fb2005-06-23 22:04:25 -0700387
388 printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
J. Bruce Fields48483bf2011-08-26 20:40:28 -0400389 user_recovery_dirname);
NeilBrown190e4fb2005-06-23 22:04:25 -0700390
Christoph Hellwige970a572010-03-22 17:32:14 +0100391 BUG_ON(rec_file);
NeilBrown190e4fb2005-06-23 22:04:25 -0700392
David Howellsd84f4f92008-11-14 10:39:23 +1100393 status = nfs4_save_creds(&original_cred);
394 if (status < 0) {
395 printk("NFSD: Unable to change credentials to find recovery"
396 " directory: error %d\n",
397 status);
Jeff Layton2a4317c2012-03-21 16:42:43 -0400398 return status;
David Howellsd84f4f92008-11-14 10:39:23 +1100399 }
NeilBrown190e4fb2005-06-23 22:04:25 -0700400
J. Bruce Fields48483bf2011-08-26 20:40:28 -0400401 rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0);
Christoph Hellwige970a572010-03-22 17:32:14 +0100402 if (IS_ERR(rec_file)) {
J. Bruce Fieldsc2642ab2006-01-18 17:43:29 -0800403 printk("NFSD: unable to find recovery directory %s\n",
J. Bruce Fields48483bf2011-08-26 20:40:28 -0400404 user_recovery_dirname);
Jeff Layton2a4317c2012-03-21 16:42:43 -0400405 status = PTR_ERR(rec_file);
Christoph Hellwige970a572010-03-22 17:32:14 +0100406 rec_file = NULL;
407 }
NeilBrown190e4fb2005-06-23 22:04:25 -0700408
David Howellsd84f4f92008-11-14 10:39:23 +1100409 nfs4_reset_creds(original_cred);
Jeff Layton2a4317c2012-03-21 16:42:43 -0400410 return status;
NeilBrown190e4fb2005-06-23 22:04:25 -0700411}
412
Jeff Layton2a4317c2012-03-21 16:42:43 -0400413static int
414nfsd4_load_reboot_recovery_data(struct net *net)
415{
416 int status;
417
418 nfs4_lock_state();
419 status = nfsd4_init_recdir();
420 if (!status)
421 status = nfsd4_recdir_load();
422 nfs4_unlock_state();
423 if (status)
424 printk(KERN_ERR "NFSD: Failure reading reboot recovery data\n");
425 return status;
426}
427
428static void
NeilBrown190e4fb2005-06-23 22:04:25 -0700429nfsd4_shutdown_recdir(void)
430{
Christoph Hellwige970a572010-03-22 17:32:14 +0100431 if (!rec_file)
NeilBrown190e4fb2005-06-23 22:04:25 -0700432 return;
Christoph Hellwige970a572010-03-22 17:32:14 +0100433 fput(rec_file);
434 rec_file = NULL;
NeilBrown190e4fb2005-06-23 22:04:25 -0700435}
J. Bruce Fields48483bf2011-08-26 20:40:28 -0400436
Jeff Layton2a4317c2012-03-21 16:42:43 -0400437static void
438nfsd4_legacy_tracking_exit(struct net *net)
439{
440 nfs4_release_reclaim();
441 nfsd4_shutdown_recdir();
442}
443
J. Bruce Fields48483bf2011-08-26 20:40:28 -0400444/*
445 * Change the NFSv4 recovery directory to recdir.
446 */
447int
448nfs4_reset_recoverydir(char *recdir)
449{
450 int status;
451 struct path path;
452
453 status = kern_path(recdir, LOOKUP_FOLLOW, &path);
454 if (status)
455 return status;
456 status = -ENOTDIR;
457 if (S_ISDIR(path.dentry->d_inode->i_mode)) {
458 strcpy(user_recovery_dirname, recdir);
459 status = 0;
460 }
461 path_put(&path);
462 return status;
463}
464
465char *
466nfs4_recoverydir(void)
467{
468 return user_recovery_dirname;
469}
Jeff Layton2a4317c2012-03-21 16:42:43 -0400470
471static int
472nfsd4_check_legacy_client(struct nfs4_client *clp)
473{
474 /* did we already find that this client is stable? */
475 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
476 return 0;
477
478 /* look for it in the reclaim hashtable otherwise */
479 if (nfsd4_find_reclaim_client(clp)) {
480 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
481 return 0;
482 }
483
484 return -ENOENT;
485}
486
487static struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = {
488 .init = nfsd4_load_reboot_recovery_data,
489 .exit = nfsd4_legacy_tracking_exit,
490 .create = nfsd4_create_clid_dir,
491 .remove = nfsd4_remove_clid_dir,
492 .check = nfsd4_check_legacy_client,
493 .grace_done = nfsd4_recdir_purge_old,
494};
495
Jeff Laytonf3f80142012-03-21 09:52:07 -0400496/* Globals */
497#define NFSD_PIPE_DIR "nfsd"
498#define NFSD_CLD_PIPE "cld"
499
500/* per-net-ns structure for holding cld upcall info */
501struct cld_net {
502 struct rpc_pipe *cn_pipe;
503 spinlock_t cn_lock;
504 struct list_head cn_list;
505 unsigned int cn_xid;
506};
507
508struct cld_upcall {
509 struct list_head cu_list;
510 struct cld_net *cu_net;
511 struct task_struct *cu_task;
512 struct cld_msg cu_msg;
513};
514
515static int
516__cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
517{
518 int ret;
519 struct rpc_pipe_msg msg;
520
521 memset(&msg, 0, sizeof(msg));
522 msg.data = cmsg;
523 msg.len = sizeof(*cmsg);
524
525 /*
526 * Set task state before we queue the upcall. That prevents
527 * wake_up_process in the downcall from racing with schedule.
528 */
529 set_current_state(TASK_UNINTERRUPTIBLE);
530 ret = rpc_queue_upcall(pipe, &msg);
531 if (ret < 0) {
532 set_current_state(TASK_RUNNING);
533 goto out;
534 }
535
536 schedule();
537 set_current_state(TASK_RUNNING);
538
539 if (msg.errno < 0)
540 ret = msg.errno;
541out:
542 return ret;
543}
544
545static int
546cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
547{
548 int ret;
549
550 /*
551 * -EAGAIN occurs when pipe is closed and reopened while there are
552 * upcalls queued.
553 */
554 do {
555 ret = __cld_pipe_upcall(pipe, cmsg);
556 } while (ret == -EAGAIN);
557
558 return ret;
559}
560
561static ssize_t
562cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
563{
564 struct cld_upcall *tmp, *cup;
565 struct cld_msg *cmsg = (struct cld_msg *)src;
566 uint32_t xid;
567 struct nfsd_net *nn = net_generic(filp->f_dentry->d_sb->s_fs_info,
568 nfsd_net_id);
569 struct cld_net *cn = nn->cld_net;
570
571 if (mlen != sizeof(*cmsg)) {
572 dprintk("%s: got %lu bytes, expected %lu\n", __func__, mlen,
573 sizeof(*cmsg));
574 return -EINVAL;
575 }
576
577 /* copy just the xid so we can try to find that */
578 if (copy_from_user(&xid, &cmsg->cm_xid, sizeof(xid)) != 0) {
579 dprintk("%s: error when copying xid from userspace", __func__);
580 return -EFAULT;
581 }
582
583 /* walk the list and find corresponding xid */
584 cup = NULL;
585 spin_lock(&cn->cn_lock);
586 list_for_each_entry(tmp, &cn->cn_list, cu_list) {
587 if (get_unaligned(&tmp->cu_msg.cm_xid) == xid) {
588 cup = tmp;
589 list_del_init(&cup->cu_list);
590 break;
591 }
592 }
593 spin_unlock(&cn->cn_lock);
594
595 /* couldn't find upcall? */
596 if (!cup) {
597 dprintk("%s: couldn't find upcall -- xid=%u\n", __func__,
598 cup->cu_msg.cm_xid);
599 return -EINVAL;
600 }
601
602 if (copy_from_user(&cup->cu_msg, src, mlen) != 0)
603 return -EFAULT;
604
605 wake_up_process(cup->cu_task);
606 return mlen;
607}
608
609static void
610cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)
611{
612 struct cld_msg *cmsg = msg->data;
613 struct cld_upcall *cup = container_of(cmsg, struct cld_upcall,
614 cu_msg);
615
616 /* errno >= 0 means we got a downcall */
617 if (msg->errno >= 0)
618 return;
619
620 wake_up_process(cup->cu_task);
621}
622
623static const struct rpc_pipe_ops cld_upcall_ops = {
624 .upcall = rpc_pipe_generic_upcall,
625 .downcall = cld_pipe_downcall,
626 .destroy_msg = cld_pipe_destroy_msg,
627};
628
629static struct dentry *
630nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe)
631{
632 struct dentry *dir, *dentry;
633
634 dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR);
635 if (dir == NULL)
636 return ERR_PTR(-ENOENT);
637 dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe);
638 dput(dir);
639 return dentry;
640}
641
642static void
643nfsd4_cld_unregister_sb(struct rpc_pipe *pipe)
644{
645 if (pipe->dentry)
646 rpc_unlink(pipe->dentry);
647}
648
649static struct dentry *
650nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe)
651{
652 struct super_block *sb;
653 struct dentry *dentry;
654
655 sb = rpc_get_sb_net(net);
656 if (!sb)
657 return NULL;
658 dentry = nfsd4_cld_register_sb(sb, pipe);
659 rpc_put_sb_net(net);
660 return dentry;
661}
662
663static void
664nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe)
665{
666 struct super_block *sb;
667
668 sb = rpc_get_sb_net(net);
669 if (sb) {
670 nfsd4_cld_unregister_sb(pipe);
671 rpc_put_sb_net(net);
672 }
673}
674
675/* Initialize rpc_pipefs pipe for communication with client tracking daemon */
676static int
677nfsd4_init_cld_pipe(struct net *net)
678{
679 int ret;
680 struct dentry *dentry;
681 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
682 struct cld_net *cn;
683
684 if (nn->cld_net)
685 return 0;
686
687 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
688 if (!cn) {
689 ret = -ENOMEM;
690 goto err;
691 }
692
693 cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
694 if (IS_ERR(cn->cn_pipe)) {
695 ret = PTR_ERR(cn->cn_pipe);
696 goto err;
697 }
698 spin_lock_init(&cn->cn_lock);
699 INIT_LIST_HEAD(&cn->cn_list);
700
701 dentry = nfsd4_cld_register_net(net, cn->cn_pipe);
702 if (IS_ERR(dentry)) {
703 ret = PTR_ERR(dentry);
704 goto err_destroy_data;
705 }
706
707 cn->cn_pipe->dentry = dentry;
708 nn->cld_net = cn;
709 return 0;
710
711err_destroy_data:
712 rpc_destroy_pipe_data(cn->cn_pipe);
713err:
714 kfree(cn);
715 printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n",
716 ret);
717 return ret;
718}
719
720static void
721nfsd4_remove_cld_pipe(struct net *net)
722{
723 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
724 struct cld_net *cn = nn->cld_net;
725
726 nfsd4_cld_unregister_net(net, cn->cn_pipe);
727 rpc_destroy_pipe_data(cn->cn_pipe);
728 kfree(nn->cld_net);
729 nn->cld_net = NULL;
730}
731
732static struct cld_upcall *
733alloc_cld_upcall(struct cld_net *cn)
734{
735 struct cld_upcall *new, *tmp;
736
737 new = kzalloc(sizeof(*new), GFP_KERNEL);
738 if (!new)
739 return new;
740
741 /* FIXME: hard cap on number in flight? */
742restart_search:
743 spin_lock(&cn->cn_lock);
744 list_for_each_entry(tmp, &cn->cn_list, cu_list) {
745 if (tmp->cu_msg.cm_xid == cn->cn_xid) {
746 cn->cn_xid++;
747 spin_unlock(&cn->cn_lock);
748 goto restart_search;
749 }
750 }
751 new->cu_task = current;
752 new->cu_msg.cm_vers = CLD_UPCALL_VERSION;
753 put_unaligned(cn->cn_xid++, &new->cu_msg.cm_xid);
754 new->cu_net = cn;
755 list_add(&new->cu_list, &cn->cn_list);
756 spin_unlock(&cn->cn_lock);
757
758 dprintk("%s: allocated xid %u\n", __func__, new->cu_msg.cm_xid);
759
760 return new;
761}
762
763static void
764free_cld_upcall(struct cld_upcall *victim)
765{
766 struct cld_net *cn = victim->cu_net;
767
768 spin_lock(&cn->cn_lock);
769 list_del(&victim->cu_list);
770 spin_unlock(&cn->cn_lock);
771 kfree(victim);
772}
773
774/* Ask daemon to create a new record */
775static void
776nfsd4_cld_create(struct nfs4_client *clp)
777{
778 int ret;
779 struct cld_upcall *cup;
780 /* FIXME: determine net from clp */
781 struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
782 struct cld_net *cn = nn->cld_net;
783
784 /* Don't upcall if it's already stored */
785 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
786 return;
787
788 cup = alloc_cld_upcall(cn);
789 if (!cup) {
790 ret = -ENOMEM;
791 goto out_err;
792 }
793
794 cup->cu_msg.cm_cmd = Cld_Create;
795 cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
796 memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
797 clp->cl_name.len);
798
799 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
800 if (!ret) {
801 ret = cup->cu_msg.cm_status;
802 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
803 }
804
805 free_cld_upcall(cup);
806out_err:
807 if (ret)
808 printk(KERN_ERR "NFSD: Unable to create client "
809 "record on stable storage: %d\n", ret);
810}
811
812/* Ask daemon to create a new record */
813static void
814nfsd4_cld_remove(struct nfs4_client *clp)
815{
816 int ret;
817 struct cld_upcall *cup;
818 /* FIXME: determine net from clp */
819 struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
820 struct cld_net *cn = nn->cld_net;
821
822 /* Don't upcall if it's already removed */
823 if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
824 return;
825
826 cup = alloc_cld_upcall(cn);
827 if (!cup) {
828 ret = -ENOMEM;
829 goto out_err;
830 }
831
832 cup->cu_msg.cm_cmd = Cld_Remove;
833 cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
834 memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
835 clp->cl_name.len);
836
837 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
838 if (!ret) {
839 ret = cup->cu_msg.cm_status;
840 clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
841 }
842
843 free_cld_upcall(cup);
844out_err:
845 if (ret)
846 printk(KERN_ERR "NFSD: Unable to remove client "
847 "record from stable storage: %d\n", ret);
848}
849
850/* Check for presence of a record, and update its timestamp */
851static int
852nfsd4_cld_check(struct nfs4_client *clp)
853{
854 int ret;
855 struct cld_upcall *cup;
856 /* FIXME: determine net from clp */
857 struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
858 struct cld_net *cn = nn->cld_net;
859
860 /* Don't upcall if one was already stored during this grace pd */
861 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
862 return 0;
863
864 cup = alloc_cld_upcall(cn);
865 if (!cup) {
866 printk(KERN_ERR "NFSD: Unable to check client record on "
867 "stable storage: %d\n", -ENOMEM);
868 return -ENOMEM;
869 }
870
871 cup->cu_msg.cm_cmd = Cld_Check;
872 cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
873 memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
874 clp->cl_name.len);
875
876 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
877 if (!ret) {
878 ret = cup->cu_msg.cm_status;
879 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
880 }
881
882 free_cld_upcall(cup);
883 return ret;
884}
885
886static void
887nfsd4_cld_grace_done(struct net *net, time_t boot_time)
888{
889 int ret;
890 struct cld_upcall *cup;
891 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
892 struct cld_net *cn = nn->cld_net;
893
894 cup = alloc_cld_upcall(cn);
895 if (!cup) {
896 ret = -ENOMEM;
897 goto out_err;
898 }
899
900 cup->cu_msg.cm_cmd = Cld_GraceDone;
901 cup->cu_msg.cm_u.cm_gracetime = (int64_t)boot_time;
902 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
903 if (!ret)
904 ret = cup->cu_msg.cm_status;
905
906 free_cld_upcall(cup);
907out_err:
908 if (ret)
909 printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
910}
911
912static struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = {
913 .init = nfsd4_init_cld_pipe,
914 .exit = nfsd4_remove_cld_pipe,
915 .create = nfsd4_cld_create,
916 .remove = nfsd4_cld_remove,
917 .check = nfsd4_cld_check,
918 .grace_done = nfsd4_cld_grace_done,
919};
920
Jeff Layton2a4317c2012-03-21 16:42:43 -0400921int
922nfsd4_client_tracking_init(struct net *net)
923{
924 int status;
Jeff Laytonf3f80142012-03-21 09:52:07 -0400925 struct path path;
Jeff Layton2a4317c2012-03-21 16:42:43 -0400926
Jeff Laytonf3f80142012-03-21 09:52:07 -0400927 if (!client_tracking_ops) {
928 client_tracking_ops = &nfsd4_cld_tracking_ops;
929 status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path);
930 if (!status) {
931 if (S_ISDIR(path.dentry->d_inode->i_mode))
932 client_tracking_ops =
933 &nfsd4_legacy_tracking_ops;
934 path_put(&path);
935 }
936 }
Jeff Layton2a4317c2012-03-21 16:42:43 -0400937
938 status = client_tracking_ops->init(net);
939 if (status) {
940 printk(KERN_WARNING "NFSD: Unable to initialize client "
941 "recovery tracking! (%d)\n", status);
942 client_tracking_ops = NULL;
943 }
944 return status;
945}
946
947void
948nfsd4_client_tracking_exit(struct net *net)
949{
950 if (client_tracking_ops) {
951 client_tracking_ops->exit(net);
952 client_tracking_ops = NULL;
953 }
954}
955
956void
957nfsd4_client_record_create(struct nfs4_client *clp)
958{
959 if (client_tracking_ops)
960 client_tracking_ops->create(clp);
961}
962
963void
964nfsd4_client_record_remove(struct nfs4_client *clp)
965{
966 if (client_tracking_ops)
967 client_tracking_ops->remove(clp);
968}
969
970int
971nfsd4_client_record_check(struct nfs4_client *clp)
972{
973 if (client_tracking_ops)
974 return client_tracking_ops->check(clp);
975
976 return -EOPNOTSUPP;
977}
978
979void
980nfsd4_record_grace_done(struct net *net, time_t boot_time)
981{
982 if (client_tracking_ops)
983 client_tracking_ops->grace_done(net, boot_time);
984}