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. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Andy Adamson <andros@citi.umich.edu> |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the name of the University nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived |
| 18 | * from this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 21 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 34 | #include <linux/file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 36 | #include <linux/namei.h> |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 37 | #include <linux/crypto.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 38 | #include <linux/sched.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 39 | |
| 40 | #include "nfsd.h" |
| 41 | #include "state.h" |
J. Bruce Fields | 0a3adad | 2009-11-04 18:12:35 -0500 | [diff] [blame] | 42 | #include "vfs.h" |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 43 | |
| 44 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 45 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame^] | 46 | /* Declarations */ |
| 47 | struct nfsd4_client_tracking_ops { |
| 48 | int (*init)(struct net *); |
| 49 | void (*exit)(struct net *); |
| 50 | void (*create)(struct nfs4_client *); |
| 51 | void (*remove)(struct nfs4_client *); |
| 52 | int (*check)(struct nfs4_client *); |
| 53 | void (*grace_done)(struct net *, time_t); |
| 54 | }; |
| 55 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 56 | /* Globals */ |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 57 | static struct file *rec_file; |
J. Bruce Fields | 48483bf | 2011-08-26 20:40:28 -0400 | [diff] [blame] | 58 | static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery"; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame^] | 59 | static struct nfsd4_client_tracking_ops *client_tracking_ops; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 60 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 61 | static int |
| 62 | nfs4_save_creds(const struct cred **original_creds) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 63 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 64 | struct cred *new; |
| 65 | |
| 66 | new = prepare_creds(); |
| 67 | if (!new) |
| 68 | return -ENOMEM; |
| 69 | |
| 70 | new->fsuid = 0; |
| 71 | new->fsgid = 0; |
| 72 | *original_creds = override_creds(new); |
| 73 | put_cred(new); |
| 74 | return 0; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 78 | nfs4_reset_creds(const struct cred *original) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 79 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 80 | revert_creds(original); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 81 | } |
| 82 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 83 | static void |
| 84 | md5_to_hex(char *out, char *md5) |
| 85 | { |
| 86 | int i; |
| 87 | |
| 88 | for (i=0; i<16; i++) { |
| 89 | unsigned char c = md5[i]; |
| 90 | |
| 91 | *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1); |
| 92 | *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1); |
| 93 | } |
| 94 | *out = '\0'; |
| 95 | } |
| 96 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 97 | __be32 |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 98 | nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname) |
| 99 | { |
| 100 | struct xdr_netobj cksum; |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 101 | struct hash_desc desc; |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 102 | struct scatterlist sg; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 103 | __be32 status = nfserr_jukebox; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 104 | |
| 105 | dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n", |
| 106 | clname->len, clname->data); |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 107 | desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 108 | desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC); |
| 109 | if (IS_ERR(desc.tfm)) |
| 110 | goto out_no_tfm; |
| 111 | cksum.len = crypto_hash_digestsize(desc.tfm); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 112 | cksum.data = kmalloc(cksum.len, GFP_KERNEL); |
| 113 | if (cksum.data == NULL) |
| 114 | goto out; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 115 | |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 116 | sg_init_one(&sg, clname->data, clname->len); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 117 | |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 118 | if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data)) |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 119 | goto out; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 120 | |
| 121 | md5_to_hex(dname, cksum.data); |
| 122 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 123 | status = nfs_ok; |
| 124 | out: |
Krishna Kumar | 2bd9e7b | 2008-10-20 11:47:09 +0530 | [diff] [blame] | 125 | kfree(cksum.data); |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 126 | crypto_free_hash(desc.tfm); |
| 127 | out_no_tfm: |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 128 | return status; |
| 129 | } |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 130 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame^] | 131 | static void |
| 132 | nfsd4_create_clid_dir(struct nfs4_client *clp) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 133 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 134 | const struct cred *original_cred; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 135 | char *dname = clp->cl_recdir; |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 136 | struct dentry *dir, *dentry; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 137 | int status; |
| 138 | |
| 139 | dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname); |
| 140 | |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 141 | if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) |
J. Bruce Fields | 7a6ef8c | 2012-01-05 15:38:41 -0500 | [diff] [blame] | 142 | return; |
J. Bruce Fields | b854889 | 2012-01-02 17:49:12 -0500 | [diff] [blame] | 143 | if (!rec_file) |
J. Bruce Fields | 7a6ef8c | 2012-01-05 15:38:41 -0500 | [diff] [blame] | 144 | return; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 145 | status = nfs4_save_creds(&original_cred); |
| 146 | if (status < 0) |
J. Bruce Fields | 7a6ef8c | 2012-01-05 15:38:41 -0500 | [diff] [blame] | 147 | return; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 148 | |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 149 | dir = rec_file->f_path.dentry; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 150 | /* lock the parent */ |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 151 | mutex_lock(&dir->d_inode->i_mutex); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 152 | |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 153 | dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 154 | if (IS_ERR(dentry)) { |
| 155 | status = PTR_ERR(dentry); |
| 156 | goto out_unlock; |
| 157 | } |
Boaz Harrosh | 6577aac | 2011-08-12 17:30:12 -0700 | [diff] [blame] | 158 | if (dentry->d_inode) |
J. Bruce Fields | aec3968 | 2012-01-02 17:30:05 -0500 | [diff] [blame] | 159 | /* |
| 160 | * In the 4.1 case, where we're called from |
| 161 | * reclaim_complete(), records from the previous reboot |
| 162 | * may still be left, so this is OK. |
| 163 | * |
| 164 | * In the 4.0 case, we should never get here; but we may |
| 165 | * as well be forgiving and just succeed silently. |
| 166 | */ |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 167 | goto out_put; |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 168 | status = mnt_want_write_file(rec_file); |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 169 | if (status) |
| 170 | goto out_put; |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 171 | status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 172 | mnt_drop_write_file(rec_file); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 173 | out_put: |
| 174 | dput(dentry); |
| 175 | out_unlock: |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 176 | mutex_unlock(&dir->d_inode->i_mutex); |
Boaz Harrosh | 6577aac | 2011-08-12 17:30:12 -0700 | [diff] [blame] | 177 | if (status == 0) |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 178 | vfs_fsync(rec_file, 0); |
Boaz Harrosh | 6577aac | 2011-08-12 17:30:12 -0700 | [diff] [blame] | 179 | else |
| 180 | printk(KERN_ERR "NFSD: failed to write recovery record" |
| 181 | " (err %d); please check that %s exists" |
| 182 | " and is writeable", status, |
| 183 | user_recovery_dirname); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 184 | nfs4_reset_creds(original_cred); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 185 | } |
| 186 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 187 | typedef int (recdir_func)(struct dentry *, struct dentry *); |
| 188 | |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 189 | struct name_list { |
| 190 | char name[HEXDIR_LEN]; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 191 | struct list_head list; |
| 192 | }; |
| 193 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 194 | static int |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 195 | nfsd4_build_namelist(void *arg, const char *name, int namlen, |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 196 | loff_t offset, u64 ino, unsigned int d_type) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 197 | { |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 198 | struct list_head *names = arg; |
| 199 | struct name_list *entry; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 200 | |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 201 | if (namlen != HEXDIR_LEN - 1) |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 202 | return 0; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 203 | entry = kmalloc(sizeof(struct name_list), GFP_KERNEL); |
| 204 | if (entry == NULL) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 205 | return -ENOMEM; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 206 | memcpy(entry->name, name, HEXDIR_LEN - 1); |
| 207 | entry->name[HEXDIR_LEN - 1] = '\0'; |
| 208 | list_add(&entry->list, names); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 213 | nfsd4_list_rec_dir(recdir_func *f) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 214 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 215 | const struct cred *original_cred; |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 216 | struct dentry *dir = rec_file->f_path.dentry; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 217 | LIST_HEAD(names); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 218 | int status; |
| 219 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 220 | status = nfs4_save_creds(&original_cred); |
| 221 | if (status < 0) |
| 222 | return status; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 223 | |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 224 | status = vfs_llseek(rec_file, 0, SEEK_SET); |
| 225 | if (status < 0) { |
| 226 | nfs4_reset_creds(original_cred); |
| 227 | return status; |
| 228 | } |
| 229 | |
| 230 | status = vfs_readdir(rec_file, nfsd4_build_namelist, &names); |
J. Bruce Fields | 8daed1e | 2009-05-11 16:10:19 -0400 | [diff] [blame] | 231 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT); |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 232 | while (!list_empty(&names)) { |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 233 | struct name_list *entry; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 234 | entry = list_entry(names.next, struct name_list, list); |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 235 | if (!status) { |
| 236 | struct dentry *dentry; |
| 237 | dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1); |
| 238 | if (IS_ERR(dentry)) { |
| 239 | status = PTR_ERR(dentry); |
| 240 | break; |
| 241 | } |
| 242 | status = f(dir, dentry); |
| 243 | dput(dentry); |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 244 | } |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 245 | list_del(&entry->list); |
| 246 | kfree(entry); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 247 | } |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 248 | mutex_unlock(&dir->d_inode->i_mutex); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 249 | nfs4_reset_creds(original_cred); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 250 | return status; |
| 251 | } |
| 252 | |
| 253 | static int |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 254 | nfsd4_unlink_clid_dir(char *name, int namlen) |
| 255 | { |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 256 | struct dentry *dir, *dentry; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 257 | int status; |
| 258 | |
| 259 | dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name); |
| 260 | |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 261 | dir = rec_file->f_path.dentry; |
| 262 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT); |
| 263 | dentry = lookup_one_len(name, dir, namlen); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 264 | if (IS_ERR(dentry)) { |
| 265 | status = PTR_ERR(dentry); |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 266 | goto out_unlock; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 267 | } |
| 268 | status = -ENOENT; |
| 269 | if (!dentry->d_inode) |
| 270 | goto out; |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 271 | status = vfs_rmdir(dir->d_inode, dentry); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 272 | out: |
| 273 | dput(dentry); |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 274 | out_unlock: |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 275 | mutex_unlock(&dir->d_inode->i_mutex); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 276 | return status; |
| 277 | } |
| 278 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame^] | 279 | static void |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 280 | nfsd4_remove_clid_dir(struct nfs4_client *clp) |
| 281 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 282 | const struct cred *original_cred; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 283 | int status; |
| 284 | |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 285 | if (!rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 286 | return; |
| 287 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 288 | status = mnt_want_write_file(rec_file); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 289 | if (status) |
| 290 | goto out; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 291 | clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 292 | |
| 293 | status = nfs4_save_creds(&original_cred); |
| 294 | if (status < 0) |
| 295 | goto out; |
| 296 | |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 297 | status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 298 | nfs4_reset_creds(original_cred); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 299 | if (status == 0) |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 300 | vfs_fsync(rec_file, 0); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 301 | mnt_drop_write_file(rec_file); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 302 | out: |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 303 | if (status) |
| 304 | printk("NFSD: Failed to remove expired client state directory" |
| 305 | " %.*s\n", HEXDIR_LEN, clp->cl_recdir); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static int |
| 309 | purge_old(struct dentry *parent, struct dentry *child) |
| 310 | { |
| 311 | int status; |
| 312 | |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 313 | if (nfs4_has_reclaimed_state(child->d_name.name, false)) |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 314 | return 0; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 315 | |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 316 | status = vfs_rmdir(parent->d_inode, child); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 317 | if (status) |
| 318 | printk("failed to remove client recovery directory %s\n", |
| 319 | child->d_name.name); |
| 320 | /* Keep trying, success or failure: */ |
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 | } |
| 323 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame^] | 324 | static void |
| 325 | nfsd4_recdir_purge_old(struct net *net, time_t boot_time) |
| 326 | { |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 327 | int status; |
| 328 | |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 329 | if (!rec_file) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 330 | return; |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 331 | status = mnt_want_write_file(rec_file); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 332 | if (status) |
| 333 | goto out; |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 334 | status = nfsd4_list_rec_dir(purge_old); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 335 | if (status == 0) |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 336 | vfs_fsync(rec_file, 0); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 337 | mnt_drop_write_file(rec_file); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 338 | out: |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 339 | if (status) |
| 340 | printk("nfsd4: failed to purge old clients from recovery" |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 341 | " directory %s\n", rec_file->f_path.dentry->d_name.name); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static int |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 345 | load_recdir(struct dentry *parent, struct dentry *child) |
| 346 | { |
| 347 | if (child->d_name.len != HEXDIR_LEN - 1) { |
| 348 | printk("nfsd4: illegal name %s in recovery directory\n", |
| 349 | child->d_name.name); |
| 350 | /* Keep trying; maybe the others are OK: */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 351 | return 0; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 352 | } |
| 353 | nfs4_client_to_reclaim(child->d_name.name); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 354 | return 0; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame^] | 357 | static int |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 358 | nfsd4_recdir_load(void) { |
| 359 | int status; |
| 360 | |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 361 | if (!rec_file) |
| 362 | return 0; |
| 363 | |
Al Viro | 5b4b299 | 2011-07-07 18:43:21 -0400 | [diff] [blame] | 364 | status = nfsd4_list_rec_dir(load_recdir); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 365 | if (status) |
| 366 | printk("nfsd4: failed loading clients from recovery" |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 367 | " directory %s\n", rec_file->f_path.dentry->d_name.name); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 368 | return status; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Hold reference to the recovery directory. |
| 373 | */ |
| 374 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame^] | 375 | static int |
| 376 | nfsd4_init_recdir(void) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 377 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 378 | const struct cred *original_cred; |
| 379 | int status; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 380 | |
| 381 | printk("NFSD: Using %s as the NFSv4 state recovery directory\n", |
J. Bruce Fields | 48483bf | 2011-08-26 20:40:28 -0400 | [diff] [blame] | 382 | user_recovery_dirname); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 383 | |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 384 | BUG_ON(rec_file); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 385 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 386 | status = nfs4_save_creds(&original_cred); |
| 387 | if (status < 0) { |
| 388 | printk("NFSD: Unable to change credentials to find recovery" |
| 389 | " directory: error %d\n", |
| 390 | status); |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame^] | 391 | return status; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 392 | } |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 393 | |
J. Bruce Fields | 48483bf | 2011-08-26 20:40:28 -0400 | [diff] [blame] | 394 | rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0); |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 395 | if (IS_ERR(rec_file)) { |
J. Bruce Fields | c2642ab | 2006-01-18 17:43:29 -0800 | [diff] [blame] | 396 | printk("NFSD: unable to find recovery directory %s\n", |
J. Bruce Fields | 48483bf | 2011-08-26 20:40:28 -0400 | [diff] [blame] | 397 | user_recovery_dirname); |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame^] | 398 | status = PTR_ERR(rec_file); |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 399 | rec_file = NULL; |
| 400 | } |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 401 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 402 | nfs4_reset_creds(original_cred); |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame^] | 403 | return status; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame^] | 406 | static int |
| 407 | nfsd4_load_reboot_recovery_data(struct net *net) |
| 408 | { |
| 409 | int status; |
| 410 | |
| 411 | nfs4_lock_state(); |
| 412 | status = nfsd4_init_recdir(); |
| 413 | if (!status) |
| 414 | status = nfsd4_recdir_load(); |
| 415 | nfs4_unlock_state(); |
| 416 | if (status) |
| 417 | printk(KERN_ERR "NFSD: Failure reading reboot recovery data\n"); |
| 418 | return status; |
| 419 | } |
| 420 | |
| 421 | static void |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 422 | nfsd4_shutdown_recdir(void) |
| 423 | { |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 424 | if (!rec_file) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 425 | return; |
Christoph Hellwig | e970a57 | 2010-03-22 17:32:14 +0100 | [diff] [blame] | 426 | fput(rec_file); |
| 427 | rec_file = NULL; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 428 | } |
J. Bruce Fields | 48483bf | 2011-08-26 20:40:28 -0400 | [diff] [blame] | 429 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame^] | 430 | static void |
| 431 | nfsd4_legacy_tracking_exit(struct net *net) |
| 432 | { |
| 433 | nfs4_release_reclaim(); |
| 434 | nfsd4_shutdown_recdir(); |
| 435 | } |
| 436 | |
J. Bruce Fields | 48483bf | 2011-08-26 20:40:28 -0400 | [diff] [blame] | 437 | /* |
| 438 | * Change the NFSv4 recovery directory to recdir. |
| 439 | */ |
| 440 | int |
| 441 | nfs4_reset_recoverydir(char *recdir) |
| 442 | { |
| 443 | int status; |
| 444 | struct path path; |
| 445 | |
| 446 | status = kern_path(recdir, LOOKUP_FOLLOW, &path); |
| 447 | if (status) |
| 448 | return status; |
| 449 | status = -ENOTDIR; |
| 450 | if (S_ISDIR(path.dentry->d_inode->i_mode)) { |
| 451 | strcpy(user_recovery_dirname, recdir); |
| 452 | status = 0; |
| 453 | } |
| 454 | path_put(&path); |
| 455 | return status; |
| 456 | } |
| 457 | |
| 458 | char * |
| 459 | nfs4_recoverydir(void) |
| 460 | { |
| 461 | return user_recovery_dirname; |
| 462 | } |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame^] | 463 | |
| 464 | static int |
| 465 | nfsd4_check_legacy_client(struct nfs4_client *clp) |
| 466 | { |
| 467 | /* did we already find that this client is stable? */ |
| 468 | if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) |
| 469 | return 0; |
| 470 | |
| 471 | /* look for it in the reclaim hashtable otherwise */ |
| 472 | if (nfsd4_find_reclaim_client(clp)) { |
| 473 | set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | return -ENOENT; |
| 478 | } |
| 479 | |
| 480 | static struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = { |
| 481 | .init = nfsd4_load_reboot_recovery_data, |
| 482 | .exit = nfsd4_legacy_tracking_exit, |
| 483 | .create = nfsd4_create_clid_dir, |
| 484 | .remove = nfsd4_remove_clid_dir, |
| 485 | .check = nfsd4_check_legacy_client, |
| 486 | .grace_done = nfsd4_recdir_purge_old, |
| 487 | }; |
| 488 | |
| 489 | int |
| 490 | nfsd4_client_tracking_init(struct net *net) |
| 491 | { |
| 492 | int status; |
| 493 | |
| 494 | client_tracking_ops = &nfsd4_legacy_tracking_ops; |
| 495 | |
| 496 | status = client_tracking_ops->init(net); |
| 497 | if (status) { |
| 498 | printk(KERN_WARNING "NFSD: Unable to initialize client " |
| 499 | "recovery tracking! (%d)\n", status); |
| 500 | client_tracking_ops = NULL; |
| 501 | } |
| 502 | return status; |
| 503 | } |
| 504 | |
| 505 | void |
| 506 | nfsd4_client_tracking_exit(struct net *net) |
| 507 | { |
| 508 | if (client_tracking_ops) { |
| 509 | client_tracking_ops->exit(net); |
| 510 | client_tracking_ops = NULL; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | void |
| 515 | nfsd4_client_record_create(struct nfs4_client *clp) |
| 516 | { |
| 517 | if (client_tracking_ops) |
| 518 | client_tracking_ops->create(clp); |
| 519 | } |
| 520 | |
| 521 | void |
| 522 | nfsd4_client_record_remove(struct nfs4_client *clp) |
| 523 | { |
| 524 | if (client_tracking_ops) |
| 525 | client_tracking_ops->remove(clp); |
| 526 | } |
| 527 | |
| 528 | int |
| 529 | nfsd4_client_record_check(struct nfs4_client *clp) |
| 530 | { |
| 531 | if (client_tracking_ops) |
| 532 | return client_tracking_ops->check(clp); |
| 533 | |
| 534 | return -EOPNOTSUPP; |
| 535 | } |
| 536 | |
| 537 | void |
| 538 | nfsd4_record_grace_done(struct net *net, time_t boot_time) |
| 539 | { |
| 540 | if (client_tracking_ops) |
| 541 | client_tracking_ops->grace_done(net, boot_time); |
| 542 | } |