NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfsd/nfs4recover.c |
| 3 | * |
| 4 | * Copyright (c) 2004 The Regents of the University of Michigan. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Andy Adamson <andros@citi.umich.edu> |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 3. Neither the name of the University nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 29 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 31 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 32 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | * |
| 34 | */ |
| 35 | |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 36 | #include <linux/err.h> |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 37 | #include <linux/sunrpc/svc.h> |
| 38 | #include <linux/nfsd/nfsd.h> |
| 39 | #include <linux/nfs4.h> |
| 40 | #include <linux/nfsd/state.h> |
| 41 | #include <linux/nfsd/xdr4.h> |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 42 | #include <linux/param.h> |
| 43 | #include <linux/file.h> |
| 44 | #include <linux/namei.h> |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 45 | #include <asm/uaccess.h> |
Adrian Bunk | 87ae9af | 2007-10-30 10:35:04 +0100 | [diff] [blame] | 46 | #include <linux/scatterlist.h> |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 47 | #include <linux/crypto.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 48 | #include <linux/sched.h> |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 49 | #include <linux/mount.h> |
J. Bruce Fields | 0a3adad | 2009-11-04 18:12:35 -0500 | [diff] [blame^] | 50 | #include "vfs.h" |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 51 | |
| 52 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 53 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 54 | /* Globals */ |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 55 | static struct path rec_dir; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 56 | static int rec_dir_init = 0; |
| 57 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 58 | static int |
| 59 | nfs4_save_creds(const struct cred **original_creds) |
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 | struct cred *new; |
| 62 | |
| 63 | new = prepare_creds(); |
| 64 | if (!new) |
| 65 | return -ENOMEM; |
| 66 | |
| 67 | new->fsuid = 0; |
| 68 | new->fsgid = 0; |
| 69 | *original_creds = override_creds(new); |
| 70 | put_cred(new); |
| 71 | return 0; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 75 | nfs4_reset_creds(const struct cred *original) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 76 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 77 | revert_creds(original); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 78 | } |
| 79 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 80 | static void |
| 81 | md5_to_hex(char *out, char *md5) |
| 82 | { |
| 83 | int i; |
| 84 | |
| 85 | for (i=0; i<16; i++) { |
| 86 | unsigned char c = md5[i]; |
| 87 | |
| 88 | *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1); |
| 89 | *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1); |
| 90 | } |
| 91 | *out = '\0'; |
| 92 | } |
| 93 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 94 | __be32 |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 95 | nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname) |
| 96 | { |
| 97 | struct xdr_netobj cksum; |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 98 | struct hash_desc desc; |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 99 | struct scatterlist sg; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 100 | __be32 status = nfserr_resource; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 101 | |
| 102 | dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n", |
| 103 | clname->len, clname->data); |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 104 | desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 105 | desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC); |
| 106 | if (IS_ERR(desc.tfm)) |
| 107 | goto out_no_tfm; |
| 108 | cksum.len = crypto_hash_digestsize(desc.tfm); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 109 | cksum.data = kmalloc(cksum.len, GFP_KERNEL); |
| 110 | if (cksum.data == NULL) |
| 111 | goto out; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 112 | |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 113 | sg_init_one(&sg, clname->data, clname->len); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 114 | |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 115 | if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data)) |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 116 | goto out; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 117 | |
| 118 | md5_to_hex(dname, cksum.data); |
| 119 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 120 | status = nfs_ok; |
| 121 | out: |
Krishna Kumar | 2bd9e7b | 2008-10-20 11:47:09 +0530 | [diff] [blame] | 122 | kfree(cksum.data); |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 123 | crypto_free_hash(desc.tfm); |
| 124 | out_no_tfm: |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 125 | return status; |
| 126 | } |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 127 | |
NeilBrown | a6ccbbb | 2005-07-07 17:59:11 -0700 | [diff] [blame] | 128 | static void |
| 129 | nfsd4_sync_rec_dir(void) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 130 | { |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 131 | mutex_lock(&rec_dir.dentry->d_inode->i_mutex); |
| 132 | nfsd_sync_dir(rec_dir.dentry); |
| 133 | mutex_unlock(&rec_dir.dentry->d_inode->i_mutex); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | int |
| 137 | nfsd4_create_clid_dir(struct nfs4_client *clp) |
| 138 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 139 | const struct cred *original_cred; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 140 | char *dname = clp->cl_recdir; |
| 141 | struct dentry *dentry; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 142 | int status; |
| 143 | |
| 144 | dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname); |
| 145 | |
| 146 | if (!rec_dir_init || clp->cl_firststate) |
| 147 | return 0; |
| 148 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 149 | status = nfs4_save_creds(&original_cred); |
| 150 | if (status < 0) |
| 151 | return status; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 152 | |
| 153 | /* lock the parent */ |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 154 | mutex_lock(&rec_dir.dentry->d_inode->i_mutex); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 155 | |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 156 | dentry = lookup_one_len(dname, rec_dir.dentry, HEXDIR_LEN-1); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 157 | if (IS_ERR(dentry)) { |
| 158 | status = PTR_ERR(dentry); |
| 159 | goto out_unlock; |
| 160 | } |
| 161 | status = -EEXIST; |
| 162 | if (dentry->d_inode) { |
| 163 | dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n"); |
| 164 | goto out_put; |
| 165 | } |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 166 | status = mnt_want_write(rec_dir.mnt); |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 167 | if (status) |
| 168 | goto out_put; |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 169 | status = vfs_mkdir(rec_dir.dentry->d_inode, dentry, S_IRWXU); |
| 170 | mnt_drop_write(rec_dir.mnt); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 171 | out_put: |
| 172 | dput(dentry); |
| 173 | out_unlock: |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 174 | mutex_unlock(&rec_dir.dentry->d_inode->i_mutex); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 175 | if (status == 0) { |
| 176 | clp->cl_firststate = 1; |
NeilBrown | a6ccbbb | 2005-07-07 17:59:11 -0700 | [diff] [blame] | 177 | nfsd4_sync_rec_dir(); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 178 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 179 | nfs4_reset_creds(original_cred); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 180 | dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status); |
| 181 | return status; |
| 182 | } |
| 183 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 184 | typedef int (recdir_func)(struct dentry *, struct dentry *); |
| 185 | |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 186 | struct name_list { |
| 187 | char name[HEXDIR_LEN]; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 188 | struct list_head list; |
| 189 | }; |
| 190 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 191 | static int |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 192 | nfsd4_build_namelist(void *arg, const char *name, int namlen, |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 193 | loff_t offset, u64 ino, unsigned int d_type) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 194 | { |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 195 | struct list_head *names = arg; |
| 196 | struct name_list *entry; |
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 | if (namlen != HEXDIR_LEN - 1) |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 199 | return 0; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 200 | entry = kmalloc(sizeof(struct name_list), GFP_KERNEL); |
| 201 | if (entry == NULL) |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 202 | return -ENOMEM; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 203 | memcpy(entry->name, name, HEXDIR_LEN - 1); |
| 204 | entry->name[HEXDIR_LEN - 1] = '\0'; |
| 205 | list_add(&entry->list, names); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static int |
| 210 | nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f) |
| 211 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 212 | const struct cred *original_cred; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 213 | struct file *filp; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 214 | LIST_HEAD(names); |
| 215 | struct name_list *entry; |
| 216 | struct dentry *dentry; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 217 | int status; |
| 218 | |
| 219 | if (!rec_dir_init) |
| 220 | return 0; |
| 221 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 222 | status = nfs4_save_creds(&original_cred); |
| 223 | if (status < 0) |
| 224 | return status; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 225 | |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 226 | filp = dentry_open(dget(dir), mntget(rec_dir.mnt), O_RDONLY, |
| 227 | current_cred()); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 228 | status = PTR_ERR(filp); |
| 229 | if (IS_ERR(filp)) |
| 230 | goto out; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 231 | status = vfs_readdir(filp, nfsd4_build_namelist, &names); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 232 | fput(filp); |
J. Bruce Fields | 8daed1e | 2009-05-11 16:10:19 -0400 | [diff] [blame] | 233 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT); |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 234 | while (!list_empty(&names)) { |
| 235 | entry = list_entry(names.next, struct name_list, list); |
| 236 | |
| 237 | dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1); |
| 238 | if (IS_ERR(dentry)) { |
| 239 | status = PTR_ERR(dentry); |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 240 | break; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 241 | } |
| 242 | status = f(dir, dentry); |
| 243 | dput(dentry); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 244 | if (status) |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 245 | break; |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 246 | list_del(&entry->list); |
| 247 | kfree(entry); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 248 | } |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 249 | mutex_unlock(&dir->d_inode->i_mutex); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 250 | out: |
J. Bruce Fields | 05f4f67 | 2009-03-13 16:02:59 -0400 | [diff] [blame] | 251 | while (!list_empty(&names)) { |
| 252 | entry = list_entry(names.next, struct name_list, list); |
| 253 | list_del(&entry->list); |
| 254 | kfree(entry); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 255 | } |
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 | { |
| 263 | struct dentry *dentry; |
| 264 | int status; |
| 265 | |
| 266 | dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name); |
| 267 | |
J. Bruce Fields | 8daed1e | 2009-05-11 16:10:19 -0400 | [diff] [blame] | 268 | mutex_lock_nested(&rec_dir.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 269 | dentry = lookup_one_len(name, rec_dir.dentry, namlen); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 270 | if (IS_ERR(dentry)) { |
| 271 | status = PTR_ERR(dentry); |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 272 | goto out_unlock; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 273 | } |
| 274 | status = -ENOENT; |
| 275 | if (!dentry->d_inode) |
| 276 | goto out; |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 277 | status = vfs_rmdir(rec_dir.dentry->d_inode, dentry); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 278 | out: |
| 279 | dput(dentry); |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 280 | out_unlock: |
| 281 | mutex_unlock(&rec_dir.dentry->d_inode->i_mutex); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 282 | return status; |
| 283 | } |
| 284 | |
| 285 | void |
| 286 | nfsd4_remove_clid_dir(struct nfs4_client *clp) |
| 287 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 288 | const struct cred *original_cred; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 289 | int status; |
| 290 | |
| 291 | if (!rec_dir_init || !clp->cl_firststate) |
| 292 | return; |
| 293 | |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 294 | status = mnt_want_write(rec_dir.mnt); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 295 | if (status) |
| 296 | goto out; |
NeilBrown | 67be431 | 2005-07-07 17:59:13 -0700 | [diff] [blame] | 297 | clp->cl_firststate = 0; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 298 | |
| 299 | status = nfs4_save_creds(&original_cred); |
| 300 | if (status < 0) |
| 301 | goto out; |
| 302 | |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 303 | status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 304 | nfs4_reset_creds(original_cred); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 305 | if (status == 0) |
NeilBrown | a6ccbbb | 2005-07-07 17:59:11 -0700 | [diff] [blame] | 306 | nfsd4_sync_rec_dir(); |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 307 | mnt_drop_write(rec_dir.mnt); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 308 | out: |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 309 | if (status) |
| 310 | printk("NFSD: Failed to remove expired client state directory" |
| 311 | " %.*s\n", HEXDIR_LEN, clp->cl_recdir); |
| 312 | return; |
| 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 | /* note: we currently use this path only for minorversion 0 */ |
| 321 | if (nfs4_has_reclaimed_state(child->d_name.name, false)) |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 322 | return 0; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 323 | |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 324 | status = vfs_rmdir(parent->d_inode, child); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 325 | if (status) |
| 326 | printk("failed to remove client recovery directory %s\n", |
| 327 | child->d_name.name); |
| 328 | /* Keep trying, success or failure: */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 329 | return 0; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | void |
| 333 | nfsd4_recdir_purge_old(void) { |
| 334 | int status; |
| 335 | |
| 336 | if (!rec_dir_init) |
| 337 | return; |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 338 | status = mnt_want_write(rec_dir.mnt); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 339 | if (status) |
| 340 | goto out; |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 341 | status = nfsd4_list_rec_dir(rec_dir.dentry, purge_old); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 342 | if (status == 0) |
NeilBrown | a6ccbbb | 2005-07-07 17:59:11 -0700 | [diff] [blame] | 343 | nfsd4_sync_rec_dir(); |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 344 | mnt_drop_write(rec_dir.mnt); |
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" |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 348 | " directory %s\n", rec_dir.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 | |
| 364 | int |
| 365 | nfsd4_recdir_load(void) { |
| 366 | int status; |
| 367 | |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 368 | status = nfsd4_list_rec_dir(rec_dir.dentry, load_recdir); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 369 | if (status) |
| 370 | printk("nfsd4: failed loading clients from recovery" |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 371 | " directory %s\n", rec_dir.dentry->d_name.name); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 372 | return status; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Hold reference to the recovery directory. |
| 377 | */ |
| 378 | |
| 379 | void |
| 380 | nfsd4_init_recdir(char *rec_dirname) |
| 381 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 382 | const struct cred *original_cred; |
| 383 | int status; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 384 | |
| 385 | printk("NFSD: Using %s as the NFSv4 state recovery directory\n", |
| 386 | rec_dirname); |
| 387 | |
| 388 | BUG_ON(rec_dir_init); |
| 389 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 390 | status = nfs4_save_creds(&original_cred); |
| 391 | if (status < 0) { |
| 392 | printk("NFSD: Unable to change credentials to find recovery" |
| 393 | " directory: error %d\n", |
| 394 | status); |
| 395 | return; |
| 396 | } |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 397 | |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 398 | status = kern_path(rec_dirname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, |
J. Bruce Fields | c2642ab | 2006-01-18 17:43:29 -0800 | [diff] [blame] | 399 | &rec_dir); |
| 400 | if (status) |
| 401 | printk("NFSD: unable to find recovery directory %s\n", |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 402 | rec_dirname); |
| 403 | |
| 404 | if (!status) |
| 405 | rec_dir_init = 1; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 406 | nfs4_reset_creds(original_cred); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | void |
| 410 | nfsd4_shutdown_recdir(void) |
| 411 | { |
| 412 | if (!rec_dir_init) |
| 413 | return; |
| 414 | rec_dir_init = 0; |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 415 | path_put(&rec_dir); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 416 | } |