NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1 | /* |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 2 | * Copyright (c) 2004 The Regents of the University of Michigan. |
Jeff Layton | f3f8014 | 2012-03-21 09:52:07 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2012 Jeff Layton <jlayton@redhat.com> |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 4 | * 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 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 35 | #include <linux/file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 37 | #include <linux/namei.h> |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 38 | #include <linux/crypto.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 39 | #include <linux/sched.h> |
Jeff Layton | f3f8014 | 2012-03-21 09:52:07 -0400 | [diff] [blame^] | 40 | #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 Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 45 | |
| 46 | #include "nfsd.h" |
| 47 | #include "state.h" |
J. Bruce Fields | 0a3adad | 2009-11-04 18:12:35 -0500 | [diff] [blame] | 48 | #include "vfs.h" |
Jeff Layton | f3f8014 | 2012-03-21 09:52:07 -0400 | [diff] [blame^] | 49 | #include "netns.h" |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 50 | |
| 51 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 52 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 53 | /* Declarations */ |
| 54 | struct 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 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 63 | /* Globals */ |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 64 | static struct file *rec_file; |
J. Bruce Fields | 48483bf | 2011-08-26 20:40:28 -0400 | [diff] [blame] | 65 | static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery"; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 66 | static struct nfsd4_client_tracking_ops *client_tracking_ops; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 67 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 68 | static int |
| 69 | nfs4_save_creds(const struct cred **original_creds) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 70 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 71 | 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; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 85 | nfs4_reset_creds(const struct cred *original) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 86 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 87 | revert_creds(original); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 88 | } |
| 89 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 90 | static void |
| 91 | md5_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 Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 104 | __be32 |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 105 | nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname) |
| 106 | { |
| 107 | struct xdr_netobj cksum; |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 108 | struct hash_desc desc; |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 109 | struct scatterlist sg; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 110 | __be32 status = nfserr_jukebox; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 111 | |
| 112 | dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n", |
| 113 | clname->len, clname->data); |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 114 | 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); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 119 | cksum.data = kmalloc(cksum.len, GFP_KERNEL); |
| 120 | if (cksum.data == NULL) |
| 121 | goto out; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 122 | |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 123 | sg_init_one(&sg, clname->data, clname->len); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 124 | |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 125 | if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data)) |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 126 | goto out; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 127 | |
| 128 | md5_to_hex(dname, cksum.data); |
| 129 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 130 | status = nfs_ok; |
| 131 | out: |
Krishna Kumar | 2bd9e7b | 2008-10-20 11:47:09 +0530 | [diff] [blame] | 132 | kfree(cksum.data); |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 133 | crypto_free_hash(desc.tfm); |
| 134 | out_no_tfm: |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 135 | return status; |
| 136 | } |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 137 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 138 | static void |
| 139 | nfsd4_create_clid_dir(struct nfs4_client *clp) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 140 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 141 | const struct cred *original_cred; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 142 | char *dname = clp->cl_recdir; |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 143 | struct dentry *dir, *dentry; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 144 | int status; |
| 145 | |
| 146 | dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname); |
| 147 | |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 148 | if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) |
J. Bruce Fields | 7a6ef8c | 2012-01-05 15:38:41 -0500 | [diff] [blame] | 149 | return; |
J. Bruce Fields | b854889 | 2012-01-02 17:49:12 -0500 | [diff] [blame] | 150 | if (!rec_file) |
J. Bruce Fields | 7a6ef8c | 2012-01-05 15:38:41 -0500 | [diff] [blame] | 151 | return; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 152 | status = nfs4_save_creds(&original_cred); |
| 153 | if (status < 0) |
J. Bruce Fields | 7a6ef8c | 2012-01-05 15:38:41 -0500 | [diff] [blame] | 154 | return; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 155 | |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 156 | dir = rec_file->f_path.dentry; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 157 | /* lock the parent */ |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 158 | mutex_lock(&dir->d_inode->i_mutex); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 159 | |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 160 | dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 161 | if (IS_ERR(dentry)) { |
| 162 | status = PTR_ERR(dentry); |
| 163 | goto out_unlock; |
| 164 | } |
Boaz Harrosh | 6577aac | 2011-08-12 17:30:12 -0700 | [diff] [blame] | 165 | if (dentry->d_inode) |
J. Bruce Fields | aec3968 | 2012-01-02 17:30:05 -0500 | [diff] [blame] | 166 | /* |
| 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 | */ |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 174 | goto out_put; |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 175 | status = mnt_want_write_file(rec_file); |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 176 | if (status) |
| 177 | goto out_put; |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 178 | status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 179 | mnt_drop_write_file(rec_file); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 180 | out_put: |
| 181 | dput(dentry); |
| 182 | out_unlock: |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 183 | mutex_unlock(&dir->d_inode->i_mutex); |
Boaz Harrosh | 6577aac | 2011-08-12 17:30:12 -0700 | [diff] [blame] | 184 | if (status == 0) |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 185 | vfs_fsync(rec_file, 0); |
Boaz Harrosh | 6577aac | 2011-08-12 17:30:12 -0700 | [diff] [blame] | 186 | 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 Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 191 | nfs4_reset_creds(original_cred); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 192 | } |
| 193 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 194 | typedef int (recdir_func)(struct dentry *, struct dentry *); |
| 195 | |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 196 | struct name_list { |
| 197 | char name[HEXDIR_LEN]; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 198 | struct list_head list; |
| 199 | }; |
| 200 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 201 | static int |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 202 | nfsd4_build_namelist(void *arg, const char *name, int namlen, |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 203 | loff_t offset, u64 ino, unsigned int d_type) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 204 | { |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 205 | struct list_head *names = arg; |
| 206 | struct name_list *entry; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 207 | |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 208 | if (namlen != HEXDIR_LEN - 1) |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 209 | return 0; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 210 | entry = kmalloc(sizeof(struct name_list), GFP_KERNEL); |
| 211 | if (entry == NULL) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 212 | return -ENOMEM; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 213 | memcpy(entry->name, name, HEXDIR_LEN - 1); |
| 214 | entry->name[HEXDIR_LEN - 1] = '\0'; |
| 215 | list_add(&entry->list, names); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 220 | nfsd4_list_rec_dir(recdir_func *f) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 221 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 222 | const struct cred *original_cred; |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 223 | struct dentry *dir = rec_file->f_path.dentry; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 224 | LIST_HEAD(names); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 225 | int status; |
| 226 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 227 | status = nfs4_save_creds(&original_cred); |
| 228 | if (status < 0) |
| 229 | return status; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 230 | |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 231 | 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 Fields | 8daed1e | 2009-05-11 16:10:19 -0400 | [diff] [blame] | 238 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT); |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 239 | while (!list_empty(&names)) { |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 240 | struct name_list *entry; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 241 | entry = list_entry(names.next, struct name_list, list); |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 242 | 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 Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 251 | } |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 252 | list_del(&entry->list); |
| 253 | kfree(entry); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 254 | } |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 255 | mutex_unlock(&dir->d_inode->i_mutex); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 256 | nfs4_reset_creds(original_cred); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 257 | return status; |
| 258 | } |
| 259 | |
| 260 | static int |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 261 | nfsd4_unlink_clid_dir(char *name, int namlen) |
| 262 | { |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 263 | struct dentry *dir, *dentry; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 264 | int status; |
| 265 | |
| 266 | dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name); |
| 267 | |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 268 | 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); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 271 | if (IS_ERR(dentry)) { |
| 272 | status = PTR_ERR(dentry); |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 273 | goto out_unlock; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 274 | } |
| 275 | status = -ENOENT; |
| 276 | if (!dentry->d_inode) |
| 277 | goto out; |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 278 | status = vfs_rmdir(dir->d_inode, dentry); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 279 | out: |
| 280 | dput(dentry); |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 281 | out_unlock: |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 282 | mutex_unlock(&dir->d_inode->i_mutex); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 283 | return status; |
| 284 | } |
| 285 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 286 | static void |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 287 | nfsd4_remove_clid_dir(struct nfs4_client *clp) |
| 288 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 289 | const struct cred *original_cred; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 290 | int status; |
| 291 | |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 292 | if (!rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 293 | return; |
| 294 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 295 | status = mnt_want_write_file(rec_file); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 296 | if (status) |
| 297 | goto out; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 298 | clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 299 | |
| 300 | status = nfs4_save_creds(&original_cred); |
| 301 | if (status < 0) |
| 302 | goto out; |
| 303 | |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 304 | status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 305 | nfs4_reset_creds(original_cred); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 306 | if (status == 0) |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 307 | vfs_fsync(rec_file, 0); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 308 | mnt_drop_write_file(rec_file); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 309 | out: |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 310 | if (status) |
| 311 | printk("NFSD: Failed to remove expired client state directory" |
| 312 | " %.*s\n", HEXDIR_LEN, clp->cl_recdir); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static int |
| 316 | purge_old(struct dentry *parent, struct dentry *child) |
| 317 | { |
| 318 | int status; |
| 319 | |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 320 | if (nfs4_has_reclaimed_state(child->d_name.name, false)) |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 321 | return 0; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 322 | |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 323 | status = vfs_rmdir(parent->d_inode, child); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 324 | if (status) |
| 325 | printk("failed to remove client recovery directory %s\n", |
| 326 | child->d_name.name); |
| 327 | /* Keep trying, success or failure: */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 328 | return 0; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 331 | static void |
| 332 | nfsd4_recdir_purge_old(struct net *net, time_t boot_time) |
| 333 | { |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 334 | int status; |
| 335 | |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 336 | if (!rec_file) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 337 | return; |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 338 | status = mnt_want_write_file(rec_file); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 339 | if (status) |
| 340 | goto out; |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 341 | status = nfsd4_list_rec_dir(purge_old); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 342 | if (status == 0) |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 343 | vfs_fsync(rec_file, 0); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 344 | mnt_drop_write_file(rec_file); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 345 | out: |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 346 | if (status) |
| 347 | printk("nfsd4: failed to purge old clients from recovery" |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 348 | " directory %s\n", rec_file->f_path.dentry->d_name.name); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static int |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 352 | load_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 Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 358 | return 0; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 359 | } |
| 360 | nfs4_client_to_reclaim(child->d_name.name); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 361 | return 0; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 364 | static int |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 365 | nfsd4_recdir_load(void) { |
| 366 | int status; |
| 367 | |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 368 | if (!rec_file) |
| 369 | return 0; |
| 370 | |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 371 | status = nfsd4_list_rec_dir(load_recdir); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 372 | if (status) |
| 373 | printk("nfsd4: failed loading clients from recovery" |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 374 | " directory %s\n", rec_file->f_path.dentry->d_name.name); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 375 | return status; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Hold reference to the recovery directory. |
| 380 | */ |
| 381 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 382 | static int |
| 383 | nfsd4_init_recdir(void) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 384 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 385 | const struct cred *original_cred; |
| 386 | int status; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 387 | |
| 388 | printk("NFSD: Using %s as the NFSv4 state recovery directory\n", |
J. Bruce Fields | 48483bf | 2011-08-26 20:40:28 -0400 | [diff] [blame] | 389 | user_recovery_dirname); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 390 | |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 391 | BUG_ON(rec_file); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 392 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 393 | 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 Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 398 | return status; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 399 | } |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 400 | |
J. Bruce Fields | 48483bf | 2011-08-26 20:40:28 -0400 | [diff] [blame] | 401 | rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0); |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 402 | if (IS_ERR(rec_file)) { |
J. Bruce Fields | c2642ab | 2006-01-18 17:43:29 -0800 | [diff] [blame] | 403 | printk("NFSD: unable to find recovery directory %s\n", |
J. Bruce Fields | 48483bf | 2011-08-26 20:40:28 -0400 | [diff] [blame] | 404 | user_recovery_dirname); |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 405 | status = PTR_ERR(rec_file); |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 406 | rec_file = NULL; |
| 407 | } |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 408 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 409 | nfs4_reset_creds(original_cred); |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 410 | return status; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 413 | static int |
| 414 | nfsd4_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 | |
| 428 | static void |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 429 | nfsd4_shutdown_recdir(void) |
| 430 | { |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 431 | if (!rec_file) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 432 | return; |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 433 | fput(rec_file); |
| 434 | rec_file = NULL; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 435 | } |
J. Bruce Fields | 48483bf | 2011-08-26 20:40:28 -0400 | [diff] [blame] | 436 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 437 | static void |
| 438 | nfsd4_legacy_tracking_exit(struct net *net) |
| 439 | { |
| 440 | nfs4_release_reclaim(); |
| 441 | nfsd4_shutdown_recdir(); |
| 442 | } |
| 443 | |
J. Bruce Fields | 48483bf | 2011-08-26 20:40:28 -0400 | [diff] [blame] | 444 | /* |
| 445 | * Change the NFSv4 recovery directory to recdir. |
| 446 | */ |
| 447 | int |
| 448 | nfs4_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 | |
| 465 | char * |
| 466 | nfs4_recoverydir(void) |
| 467 | { |
| 468 | return user_recovery_dirname; |
| 469 | } |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 470 | |
| 471 | static int |
| 472 | nfsd4_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 | |
| 487 | static 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 Layton | f3f8014 | 2012-03-21 09:52:07 -0400 | [diff] [blame^] | 496 | /* Globals */ |
| 497 | #define NFSD_PIPE_DIR "nfsd" |
| 498 | #define NFSD_CLD_PIPE "cld" |
| 499 | |
| 500 | /* per-net-ns structure for holding cld upcall info */ |
| 501 | struct 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 | |
| 508 | struct 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 | |
| 515 | static 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; |
| 541 | out: |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | static int |
| 546 | cld_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 | |
| 561 | static ssize_t |
| 562 | cld_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 | |
| 609 | static void |
| 610 | cld_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 | |
| 623 | static 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 | |
| 629 | static struct dentry * |
| 630 | nfsd4_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 | |
| 642 | static void |
| 643 | nfsd4_cld_unregister_sb(struct rpc_pipe *pipe) |
| 644 | { |
| 645 | if (pipe->dentry) |
| 646 | rpc_unlink(pipe->dentry); |
| 647 | } |
| 648 | |
| 649 | static struct dentry * |
| 650 | nfsd4_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 | |
| 663 | static void |
| 664 | nfsd4_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 */ |
| 676 | static int |
| 677 | nfsd4_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 | |
| 711 | err_destroy_data: |
| 712 | rpc_destroy_pipe_data(cn->cn_pipe); |
| 713 | err: |
| 714 | kfree(cn); |
| 715 | printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n", |
| 716 | ret); |
| 717 | return ret; |
| 718 | } |
| 719 | |
| 720 | static void |
| 721 | nfsd4_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 | |
| 732 | static struct cld_upcall * |
| 733 | alloc_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? */ |
| 742 | restart_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 | |
| 763 | static void |
| 764 | free_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 */ |
| 775 | static void |
| 776 | nfsd4_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); |
| 806 | out_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 */ |
| 813 | static void |
| 814 | nfsd4_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); |
| 844 | out_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 */ |
| 851 | static int |
| 852 | nfsd4_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 | |
| 886 | static void |
| 887 | nfsd4_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); |
| 907 | out_err: |
| 908 | if (ret) |
| 909 | printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret); |
| 910 | } |
| 911 | |
| 912 | static 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 Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 921 | int |
| 922 | nfsd4_client_tracking_init(struct net *net) |
| 923 | { |
| 924 | int status; |
Jeff Layton | f3f8014 | 2012-03-21 09:52:07 -0400 | [diff] [blame^] | 925 | struct path path; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 926 | |
Jeff Layton | f3f8014 | 2012-03-21 09:52:07 -0400 | [diff] [blame^] | 927 | 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 Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 937 | |
| 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 | |
| 947 | void |
| 948 | nfsd4_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 | |
| 956 | void |
| 957 | nfsd4_client_record_create(struct nfs4_client *clp) |
| 958 | { |
| 959 | if (client_tracking_ops) |
| 960 | client_tracking_ops->create(clp); |
| 961 | } |
| 962 | |
| 963 | void |
| 964 | nfsd4_client_record_remove(struct nfs4_client *clp) |
| 965 | { |
| 966 | if (client_tracking_ops) |
| 967 | client_tracking_ops->remove(clp); |
| 968 | } |
| 969 | |
| 970 | int |
| 971 | nfsd4_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 | |
| 979 | void |
| 980 | nfsd4_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 | } |