Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/nfs/nfs4state.c |
| 3 | * |
| 4 | * Client-side XDR for NFSv4. |
| 5 | * |
| 6 | * Copyright (c) 2002 The Regents of the University of Michigan. |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Kendrick Smith <kmsmith@umich.edu> |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer. |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * 3. Neither the name of the University nor the names of its |
| 21 | * contributors may be used to endorse or promote products derived |
| 22 | * from this software without specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 31 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 32 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 33 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | * |
| 36 | * Implementation of the NFSv4 state model. For the time being, |
| 37 | * this is minimal, but will be made much more complex in a |
| 38 | * subsequent patch. |
| 39 | */ |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/slab.h> |
| 42 | #include <linux/smp_lock.h> |
| 43 | #include <linux/nfs_fs.h> |
| 44 | #include <linux/nfs_idmap.h> |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame] | 45 | #include <linux/kthread.h> |
| 46 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/workqueue.h> |
| 48 | #include <linux/bitops.h> |
| 49 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 50 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include "callback.h" |
| 52 | #include "delegation.h" |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 53 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | #define OPENOWNER_POOL_SIZE 8 |
| 56 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 57 | const nfs4_stateid zero_stateid; |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | static LIST_HEAD(nfs4_clientid_list); |
| 60 | |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 61 | static int nfs4_init_client(struct nfs_client *clp, struct rpc_cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
Trond Myklebust | 286d7d6 | 2006-01-03 09:55:26 +0100 | [diff] [blame] | 63 | int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, |
| 64 | nfs_callback_tcpport, cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | if (status == 0) |
Trond Myklebust | 286d7d6 | 2006-01-03 09:55:26 +0100 | [diff] [blame] | 66 | status = nfs4_proc_setclientid_confirm(clp, cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | if (status == 0) |
| 68 | nfs4_schedule_state_renewal(clp); |
| 69 | return status; |
| 70 | } |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | u32 |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 73 | nfs4_alloc_lockowner_id(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
| 75 | return clp->cl_lockowner_id ++; |
| 76 | } |
| 77 | |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 78 | struct rpc_cred *nfs4_get_renew_cred(struct nfs_client *clp) |
Trond Myklebust | b4454fe | 2006-01-03 09:55:25 +0100 | [diff] [blame] | 79 | { |
| 80 | struct nfs4_state_owner *sp; |
| 81 | struct rpc_cred *cred = NULL; |
| 82 | |
| 83 | list_for_each_entry(sp, &clp->cl_state_owners, so_list) { |
| 84 | if (list_empty(&sp->so_states)) |
| 85 | continue; |
| 86 | cred = get_rpccred(sp->so_cred); |
| 87 | break; |
| 88 | } |
| 89 | return cred; |
| 90 | } |
| 91 | |
Trond Myklebust | 10afec9 | 2007-05-14 17:16:04 -0400 | [diff] [blame] | 92 | static struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp) |
Trond Myklebust | 286d7d6 | 2006-01-03 09:55:26 +0100 | [diff] [blame] | 93 | { |
| 94 | struct nfs4_state_owner *sp; |
| 95 | |
| 96 | if (!list_empty(&clp->cl_state_owners)) { |
| 97 | sp = list_entry(clp->cl_state_owners.next, |
| 98 | struct nfs4_state_owner, so_list); |
| 99 | return get_rpccred(sp->so_cred); |
| 100 | } |
| 101 | return NULL; |
| 102 | } |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | static struct nfs4_state_owner * |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 105 | nfs4_find_state_owner(struct nfs_client *clp, struct rpc_cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
| 107 | struct nfs4_state_owner *sp, *res = NULL; |
| 108 | |
| 109 | list_for_each_entry(sp, &clp->cl_state_owners, so_list) { |
| 110 | if (sp->so_cred != cred) |
| 111 | continue; |
| 112 | atomic_inc(&sp->so_count); |
| 113 | /* Move to the head of the list */ |
| 114 | list_move(&sp->so_list, &clp->cl_state_owners); |
| 115 | res = sp; |
| 116 | break; |
| 117 | } |
| 118 | return res; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to |
| 123 | * create a new state_owner. |
| 124 | * |
| 125 | */ |
| 126 | static struct nfs4_state_owner * |
| 127 | nfs4_alloc_state_owner(void) |
| 128 | { |
| 129 | struct nfs4_state_owner *sp; |
| 130 | |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 131 | sp = kzalloc(sizeof(*sp),GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | if (!sp) |
| 133 | return NULL; |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 134 | spin_lock_init(&sp->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | INIT_LIST_HEAD(&sp->so_states); |
| 136 | INIT_LIST_HEAD(&sp->so_delegations); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 137 | rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue"); |
| 138 | sp->so_seqid.sequence = &sp->so_sequence; |
| 139 | spin_lock_init(&sp->so_sequence.lock); |
| 140 | INIT_LIST_HEAD(&sp->so_sequence.list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | atomic_set(&sp->so_count, 1); |
| 142 | return sp; |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | nfs4_drop_state_owner(struct nfs4_state_owner *sp) |
| 147 | { |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 148 | struct nfs_client *clp = sp->so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | spin_lock(&clp->cl_lock); |
| 150 | list_del_init(&sp->so_list); |
| 151 | spin_unlock(&clp->cl_lock); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Note: must be called with clp->cl_sem held in order to prevent races |
| 156 | * with reboot recovery! |
| 157 | */ |
| 158 | struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred) |
| 159 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 160 | struct nfs_client *clp = server->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | struct nfs4_state_owner *sp, *new; |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | new = nfs4_alloc_state_owner(); |
| 164 | spin_lock(&clp->cl_lock); |
| 165 | sp = nfs4_find_state_owner(clp, cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | if (sp == NULL && new != NULL) { |
| 167 | list_add(&new->so_list, &clp->cl_state_owners); |
| 168 | new->so_client = clp; |
| 169 | new->so_id = nfs4_alloc_lockowner_id(clp); |
Trond Myklebust | 27b3f94 | 2007-07-02 10:24:56 -0400 | [diff] [blame] | 170 | new->so_cred = get_rpccred(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | sp = new; |
| 172 | new = NULL; |
| 173 | } |
| 174 | spin_unlock(&clp->cl_lock); |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 175 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | if (sp != NULL) |
| 177 | return sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Must be called with clp->cl_sem held in order to avoid races |
| 183 | * with state recovery... |
| 184 | */ |
| 185 | void nfs4_put_state_owner(struct nfs4_state_owner *sp) |
| 186 | { |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 187 | struct nfs_client *clp = sp->so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | struct rpc_cred *cred = sp->so_cred; |
| 189 | |
| 190 | if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock)) |
| 191 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | list_del(&sp->so_list); |
| 193 | spin_unlock(&clp->cl_lock); |
| 194 | put_rpccred(cred); |
| 195 | kfree(sp); |
| 196 | } |
| 197 | |
| 198 | static struct nfs4_state * |
| 199 | nfs4_alloc_open_state(void) |
| 200 | { |
| 201 | struct nfs4_state *state; |
| 202 | |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 203 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | if (!state) |
| 205 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | atomic_set(&state->count, 1); |
| 207 | INIT_LIST_HEAD(&state->lock_states); |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 208 | spin_lock_init(&state->state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return state; |
| 210 | } |
| 211 | |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 212 | void |
| 213 | nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode) |
| 214 | { |
| 215 | if (state->state == mode) |
| 216 | return; |
| 217 | /* NB! List reordering - see the reclaim code for why. */ |
| 218 | if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) { |
| 219 | if (mode & FMODE_WRITE) |
| 220 | list_move(&state->open_states, &state->owner->so_states); |
| 221 | else |
| 222 | list_move_tail(&state->open_states, &state->owner->so_states); |
| 223 | } |
| 224 | if (mode == 0) |
| 225 | list_del_init(&state->inode_states); |
| 226 | state->state = mode; |
| 227 | } |
| 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | static struct nfs4_state * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner) |
| 231 | { |
| 232 | struct nfs_inode *nfsi = NFS_I(inode); |
| 233 | struct nfs4_state *state; |
| 234 | |
| 235 | list_for_each_entry(state, &nfsi->open_states, inode_states) { |
| 236 | /* Is this in the process of being freed? */ |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 237 | if (state->state == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | continue; |
| 239 | if (state->owner == owner) { |
| 240 | atomic_inc(&state->count); |
| 241 | return state; |
| 242 | } |
| 243 | } |
| 244 | return NULL; |
| 245 | } |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | static void |
| 248 | nfs4_free_open_state(struct nfs4_state *state) |
| 249 | { |
| 250 | kfree(state); |
| 251 | } |
| 252 | |
| 253 | struct nfs4_state * |
| 254 | nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner) |
| 255 | { |
| 256 | struct nfs4_state *state, *new; |
| 257 | struct nfs_inode *nfsi = NFS_I(inode); |
| 258 | |
| 259 | spin_lock(&inode->i_lock); |
| 260 | state = __nfs4_find_state_byowner(inode, owner); |
| 261 | spin_unlock(&inode->i_lock); |
| 262 | if (state) |
| 263 | goto out; |
| 264 | new = nfs4_alloc_open_state(); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 265 | spin_lock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | spin_lock(&inode->i_lock); |
| 267 | state = __nfs4_find_state_byowner(inode, owner); |
| 268 | if (state == NULL && new != NULL) { |
| 269 | state = new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | state->owner = owner; |
| 271 | atomic_inc(&owner->so_count); |
| 272 | list_add(&state->inode_states, &nfsi->open_states); |
| 273 | state->inode = igrab(inode); |
| 274 | spin_unlock(&inode->i_lock); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 275 | /* Note: The reclaim code dictates that we add stateless |
| 276 | * and read-only stateids to the end of the list */ |
| 277 | list_add_tail(&state->open_states, &owner->so_states); |
| 278 | spin_unlock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } else { |
| 280 | spin_unlock(&inode->i_lock); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 281 | spin_unlock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | if (new) |
| 283 | nfs4_free_open_state(new); |
| 284 | } |
| 285 | out: |
| 286 | return state; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Beware! Caller must be holding exactly one |
Trond Myklebust | e6dfa55 | 2005-10-18 14:20:13 -0700 | [diff] [blame] | 291 | * reference to clp->cl_sem! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | */ |
| 293 | void nfs4_put_open_state(struct nfs4_state *state) |
| 294 | { |
| 295 | struct inode *inode = state->inode; |
| 296 | struct nfs4_state_owner *owner = state->owner; |
| 297 | |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 298 | if (!atomic_dec_and_lock(&state->count, &owner->so_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | return; |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 300 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | if (!list_empty(&state->inode_states)) |
| 302 | list_del(&state->inode_states); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | list_del(&state->open_states); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 304 | spin_unlock(&inode->i_lock); |
| 305 | spin_unlock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | iput(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | nfs4_free_open_state(state); |
| 308 | nfs4_put_state_owner(owner); |
| 309 | } |
| 310 | |
| 311 | /* |
Trond Myklebust | 83c9d41 | 2005-10-18 14:20:13 -0700 | [diff] [blame] | 312 | * Close the current file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | */ |
Trond Myklebust | 4a35bd4 | 2007-06-05 10:31:33 -0400 | [diff] [blame] | 314 | void nfs4_close_state(struct path *path, struct nfs4_state *state, mode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
| 316 | struct inode *inode = state->inode; |
| 317 | struct nfs4_state_owner *owner = state->owner; |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 318 | int oldstate, newstate = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | atomic_inc(&owner->so_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | /* Protect against nfs4_find_state() */ |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 322 | spin_lock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | spin_lock(&inode->i_lock); |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 324 | switch (mode & (FMODE_READ | FMODE_WRITE)) { |
| 325 | case FMODE_READ: |
| 326 | state->n_rdonly--; |
| 327 | break; |
| 328 | case FMODE_WRITE: |
| 329 | state->n_wronly--; |
| 330 | break; |
| 331 | case FMODE_READ|FMODE_WRITE: |
| 332 | state->n_rdwr--; |
| 333 | } |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 334 | oldstate = newstate = state->state; |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 335 | if (state->n_rdwr == 0) { |
| 336 | if (state->n_rdonly == 0) |
| 337 | newstate &= ~FMODE_READ; |
| 338 | if (state->n_wronly == 0) |
| 339 | newstate &= ~FMODE_WRITE; |
| 340 | } |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 341 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { |
| 342 | nfs4_state_set_mode_locked(state, newstate); |
| 343 | oldstate = newstate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | spin_unlock(&inode->i_lock); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 346 | spin_unlock(&owner->so_lock); |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 347 | |
Trond Myklebust | b39e625 | 2007-06-11 23:05:07 -0400 | [diff] [blame] | 348 | if (oldstate == newstate) { |
| 349 | nfs4_put_open_state(state); |
| 350 | nfs4_put_state_owner(owner); |
| 351 | } else |
| 352 | nfs4_do_close(path, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Search the state->lock_states for an existing lock_owner |
| 357 | * that is compatible with current->files |
| 358 | */ |
| 359 | static struct nfs4_lock_state * |
| 360 | __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner) |
| 361 | { |
| 362 | struct nfs4_lock_state *pos; |
| 363 | list_for_each_entry(pos, &state->lock_states, ls_locks) { |
| 364 | if (pos->ls_owner != fl_owner) |
| 365 | continue; |
| 366 | atomic_inc(&pos->ls_count); |
| 367 | return pos; |
| 368 | } |
| 369 | return NULL; |
| 370 | } |
| 371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | /* |
| 373 | * Return a compatible lock_state. If no initialized lock_state structure |
| 374 | * exists, return an uninitialized one. |
| 375 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | */ |
| 377 | static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner) |
| 378 | { |
| 379 | struct nfs4_lock_state *lsp; |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 380 | struct nfs_client *clp = state->owner->so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 382 | lsp = kzalloc(sizeof(*lsp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | if (lsp == NULL) |
| 384 | return NULL; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 385 | lsp->ls_seqid.sequence = &state->owner->so_sequence; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | atomic_set(&lsp->ls_count, 1); |
| 387 | lsp->ls_owner = fl_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | spin_lock(&clp->cl_lock); |
| 389 | lsp->ls_id = nfs4_alloc_lockowner_id(clp); |
| 390 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 391 | INIT_LIST_HEAD(&lsp->ls_locks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | return lsp; |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Return a compatible lock_state. If no initialized lock_state structure |
| 397 | * exists, return an uninitialized one. |
| 398 | * |
Trond Myklebust | e6dfa55 | 2005-10-18 14:20:13 -0700 | [diff] [blame] | 399 | * The caller must be holding clp->cl_sem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | */ |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 401 | static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 403 | struct nfs4_lock_state *lsp, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 405 | for(;;) { |
| 406 | spin_lock(&state->state_lock); |
| 407 | lsp = __nfs4_find_lock_state(state, owner); |
| 408 | if (lsp != NULL) |
| 409 | break; |
| 410 | if (new != NULL) { |
| 411 | new->ls_state = state; |
| 412 | list_add(&new->ls_locks, &state->lock_states); |
| 413 | set_bit(LK_STATE_IN_USE, &state->flags); |
| 414 | lsp = new; |
| 415 | new = NULL; |
| 416 | break; |
| 417 | } |
| 418 | spin_unlock(&state->state_lock); |
| 419 | new = nfs4_alloc_lock_state(state, owner); |
| 420 | if (new == NULL) |
| 421 | return NULL; |
| 422 | } |
| 423 | spin_unlock(&state->state_lock); |
| 424 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return lsp; |
| 426 | } |
| 427 | |
| 428 | /* |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 429 | * Release reference to lock_state, and free it if we see that |
| 430 | * it is no longer in use |
| 431 | */ |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 432 | void nfs4_put_lock_state(struct nfs4_lock_state *lsp) |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 433 | { |
| 434 | struct nfs4_state *state; |
| 435 | |
| 436 | if (lsp == NULL) |
| 437 | return; |
| 438 | state = lsp->ls_state; |
| 439 | if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock)) |
| 440 | return; |
| 441 | list_del(&lsp->ls_locks); |
| 442 | if (list_empty(&state->lock_states)) |
| 443 | clear_bit(LK_STATE_IN_USE, &state->flags); |
| 444 | spin_unlock(&state->state_lock); |
| 445 | kfree(lsp); |
| 446 | } |
| 447 | |
| 448 | static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src) |
| 449 | { |
| 450 | struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner; |
| 451 | |
| 452 | dst->fl_u.nfs4_fl.owner = lsp; |
| 453 | atomic_inc(&lsp->ls_count); |
| 454 | } |
| 455 | |
| 456 | static void nfs4_fl_release_lock(struct file_lock *fl) |
| 457 | { |
| 458 | nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner); |
| 459 | } |
| 460 | |
| 461 | static struct file_lock_operations nfs4_fl_lock_ops = { |
| 462 | .fl_copy_lock = nfs4_fl_copy_lock, |
| 463 | .fl_release_private = nfs4_fl_release_lock, |
| 464 | }; |
| 465 | |
| 466 | int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl) |
| 467 | { |
| 468 | struct nfs4_lock_state *lsp; |
| 469 | |
| 470 | if (fl->fl_ops != NULL) |
| 471 | return 0; |
| 472 | lsp = nfs4_get_lock_state(state, fl->fl_owner); |
| 473 | if (lsp == NULL) |
| 474 | return -ENOMEM; |
| 475 | fl->fl_u.nfs4_fl.owner = lsp; |
| 476 | fl->fl_ops = &nfs4_fl_lock_ops; |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | * Byte-range lock aware utility to initialize the stateid of read/write |
| 482 | * requests. |
| 483 | */ |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 484 | void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 486 | struct nfs4_lock_state *lsp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | memcpy(dst, &state->stateid, sizeof(*dst)); |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 489 | if (test_bit(LK_STATE_IN_USE, &state->flags) == 0) |
| 490 | return; |
| 491 | |
| 492 | spin_lock(&state->state_lock); |
| 493 | lsp = __nfs4_find_lock_state(state, fl_owner); |
| 494 | if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0) |
| 495 | memcpy(dst, &lsp->ls_stateid, sizeof(*dst)); |
| 496 | spin_unlock(&state->state_lock); |
| 497 | nfs4_put_lock_state(lsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 500 | struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
Trond Myklebust | 36f20c6 | 2005-11-25 17:09:57 -0500 | [diff] [blame] | 502 | struct rpc_sequence *sequence = counter->sequence; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 503 | struct nfs_seqid *new; |
| 504 | |
| 505 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
| 506 | if (new != NULL) { |
| 507 | new->sequence = counter; |
Trond Myklebust | 36f20c6 | 2005-11-25 17:09:57 -0500 | [diff] [blame] | 508 | spin_lock(&sequence->lock); |
| 509 | list_add_tail(&new->list, &sequence->list); |
| 510 | spin_unlock(&sequence->lock); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 511 | } |
| 512 | return new; |
| 513 | } |
| 514 | |
| 515 | void nfs_free_seqid(struct nfs_seqid *seqid) |
| 516 | { |
| 517 | struct rpc_sequence *sequence = seqid->sequence->sequence; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 518 | |
Trond Myklebust | 36f20c6 | 2005-11-25 17:09:57 -0500 | [diff] [blame] | 519 | spin_lock(&sequence->lock); |
| 520 | list_del(&seqid->list); |
| 521 | spin_unlock(&sequence->lock); |
| 522 | rpc_wake_up(&sequence->wait); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 523 | kfree(seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | /* |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 527 | * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or |
| 528 | * failed with a seqid incrementing error - |
| 529 | * see comments nfs_fs.h:seqid_mutating_error() |
| 530 | */ |
Trond Myklebust | 88d9093 | 2007-07-02 14:03:03 -0400 | [diff] [blame^] | 531 | static void nfs_increment_seqid(int status, struct nfs_seqid *seqid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 533 | switch (status) { |
| 534 | case 0: |
| 535 | break; |
| 536 | case -NFS4ERR_BAD_SEQID: |
| 537 | case -NFS4ERR_STALE_CLIENTID: |
| 538 | case -NFS4ERR_STALE_STATEID: |
| 539 | case -NFS4ERR_BAD_STATEID: |
| 540 | case -NFS4ERR_BADXDR: |
| 541 | case -NFS4ERR_RESOURCE: |
| 542 | case -NFS4ERR_NOFILEHANDLE: |
| 543 | /* Non-seqid mutating errors */ |
| 544 | return; |
| 545 | }; |
| 546 | /* |
| 547 | * Note: no locking needed as we are guaranteed to be first |
| 548 | * on the sequence list |
| 549 | */ |
| 550 | seqid->sequence->counter++; |
| 551 | } |
| 552 | |
| 553 | void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid) |
| 554 | { |
| 555 | if (status == -NFS4ERR_BAD_SEQID) { |
| 556 | struct nfs4_state_owner *sp = container_of(seqid->sequence, |
| 557 | struct nfs4_state_owner, so_seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | nfs4_drop_state_owner(sp); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 559 | } |
Trond Myklebust | 88d9093 | 2007-07-02 14:03:03 -0400 | [diff] [blame^] | 560 | nfs_increment_seqid(status, seqid); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /* |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 564 | * Increment the seqid if the LOCK/LOCKU succeeded, or |
| 565 | * failed with a seqid incrementing error - |
| 566 | * see comments nfs_fs.h:seqid_mutating_error() |
| 567 | */ |
| 568 | void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid) |
| 569 | { |
Trond Myklebust | 88d9093 | 2007-07-02 14:03:03 -0400 | [diff] [blame^] | 570 | nfs_increment_seqid(status, seqid); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task) |
| 574 | { |
| 575 | struct rpc_sequence *sequence = seqid->sequence->sequence; |
| 576 | int status = 0; |
| 577 | |
Trond Myklebust | 4e51336 | 2005-10-20 14:22:41 -0700 | [diff] [blame] | 578 | if (sequence->list.next == &seqid->list) |
| 579 | goto out; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 580 | spin_lock(&sequence->lock); |
Trond Myklebust | 36f20c6 | 2005-11-25 17:09:57 -0500 | [diff] [blame] | 581 | if (sequence->list.next != &seqid->list) { |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 582 | rpc_sleep_on(&sequence->wait, task, NULL, NULL); |
| 583 | status = -EAGAIN; |
Trond Myklebust | 36f20c6 | 2005-11-25 17:09:57 -0500 | [diff] [blame] | 584 | } |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 585 | spin_unlock(&sequence->lock); |
Trond Myklebust | 4e51336 | 2005-10-20 14:22:41 -0700 | [diff] [blame] | 586 | out: |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 587 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | static int reclaimer(void *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 592 | static inline void nfs4_clear_recover_bit(struct nfs_client *clp) |
Trond Myklebust | 433fbe4 | 2006-01-03 09:55:22 +0100 | [diff] [blame] | 593 | { |
| 594 | smp_mb__before_clear_bit(); |
| 595 | clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state); |
| 596 | smp_mb__after_clear_bit(); |
| 597 | wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER); |
| 598 | rpc_wake_up(&clp->cl_rpcwaitq); |
| 599 | } |
| 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | /* |
| 602 | * State recovery routine |
| 603 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 604 | static void nfs4_recover_state(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame] | 606 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame] | 608 | __module_get(THIS_MODULE); |
| 609 | atomic_inc(&clp->cl_count); |
| 610 | task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim", |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 611 | NIPQUAD(clp->cl_addr.sin_addr)); |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame] | 612 | if (!IS_ERR(task)) |
| 613 | return; |
Trond Myklebust | 433fbe4 | 2006-01-03 09:55:22 +0100 | [diff] [blame] | 614 | nfs4_clear_recover_bit(clp); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 615 | nfs_put_client(clp); |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame] | 616 | module_put(THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /* |
| 620 | * Schedule a state recovery attempt |
| 621 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 622 | void nfs4_schedule_state_recovery(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | { |
| 624 | if (!clp) |
| 625 | return; |
Trond Myklebust | 433fbe4 | 2006-01-03 09:55:22 +0100 | [diff] [blame] | 626 | if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0) |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame] | 627 | nfs4_recover_state(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state) |
| 631 | { |
| 632 | struct inode *inode = state->inode; |
| 633 | struct file_lock *fl; |
| 634 | int status = 0; |
| 635 | |
| 636 | for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) { |
Trond Myklebust | 43b2a33 | 2005-11-04 15:35:30 -0500 | [diff] [blame] | 637 | if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | continue; |
| 639 | if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state) |
| 640 | continue; |
| 641 | status = ops->recover_lock(state, fl); |
| 642 | if (status >= 0) |
| 643 | continue; |
| 644 | switch (status) { |
| 645 | default: |
| 646 | printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n", |
| 647 | __FUNCTION__, status); |
| 648 | case -NFS4ERR_EXPIRED: |
| 649 | case -NFS4ERR_NO_GRACE: |
| 650 | case -NFS4ERR_RECLAIM_BAD: |
| 651 | case -NFS4ERR_RECLAIM_CONFLICT: |
Trond Myklebust | 43b2a33 | 2005-11-04 15:35:30 -0500 | [diff] [blame] | 652 | /* kill_proc(fl->fl_pid, SIGLOST, 1); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | break; |
| 654 | case -NFS4ERR_STALE_CLIENTID: |
| 655 | goto out_err; |
| 656 | } |
| 657 | } |
| 658 | return 0; |
| 659 | out_err: |
| 660 | return status; |
| 661 | } |
| 662 | |
| 663 | static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp) |
| 664 | { |
| 665 | struct nfs4_state *state; |
| 666 | struct nfs4_lock_state *lock; |
| 667 | int status = 0; |
| 668 | |
| 669 | /* Note: we rely on the sp->so_states list being ordered |
| 670 | * so that we always reclaim open(O_RDWR) and/or open(O_WRITE) |
| 671 | * states first. |
| 672 | * This is needed to ensure that the server won't give us any |
| 673 | * read delegations that we have to return if, say, we are |
| 674 | * recovering after a network partition or a reboot from a |
| 675 | * server that doesn't support a grace period. |
| 676 | */ |
| 677 | list_for_each_entry(state, &sp->so_states, open_states) { |
| 678 | if (state->state == 0) |
| 679 | continue; |
| 680 | status = ops->recover_open(sp, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | if (status >= 0) { |
| 682 | status = nfs4_reclaim_locks(ops, state); |
| 683 | if (status < 0) |
| 684 | goto out_err; |
| 685 | list_for_each_entry(lock, &state->lock_states, ls_locks) { |
| 686 | if (!(lock->ls_flags & NFS_LOCK_INITIALIZED)) |
| 687 | printk("%s: Lock reclaim failed!\n", |
| 688 | __FUNCTION__); |
| 689 | } |
| 690 | continue; |
| 691 | } |
| 692 | switch (status) { |
| 693 | default: |
| 694 | printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n", |
| 695 | __FUNCTION__, status); |
| 696 | case -ENOENT: |
| 697 | case -NFS4ERR_RECLAIM_BAD: |
| 698 | case -NFS4ERR_RECLAIM_CONFLICT: |
| 699 | /* |
| 700 | * Open state on this file cannot be recovered |
| 701 | * All we can do is revert to using the zero stateid. |
| 702 | */ |
| 703 | memset(state->stateid.data, 0, |
| 704 | sizeof(state->stateid.data)); |
| 705 | /* Mark the file as being 'closed' */ |
| 706 | state->state = 0; |
| 707 | break; |
| 708 | case -NFS4ERR_EXPIRED: |
| 709 | case -NFS4ERR_NO_GRACE: |
| 710 | case -NFS4ERR_STALE_CLIENTID: |
| 711 | goto out_err; |
| 712 | } |
| 713 | } |
| 714 | return 0; |
| 715 | out_err: |
| 716 | return status; |
| 717 | } |
| 718 | |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 719 | static void nfs4_state_mark_reclaim(struct nfs_client *clp) |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 720 | { |
| 721 | struct nfs4_state_owner *sp; |
| 722 | struct nfs4_state *state; |
| 723 | struct nfs4_lock_state *lock; |
| 724 | |
| 725 | /* Reset all sequence ids to zero */ |
| 726 | list_for_each_entry(sp, &clp->cl_state_owners, so_list) { |
| 727 | sp->so_seqid.counter = 0; |
| 728 | sp->so_seqid.flags = 0; |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 729 | spin_lock(&sp->so_lock); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 730 | list_for_each_entry(state, &sp->so_states, open_states) { |
| 731 | list_for_each_entry(lock, &state->lock_states, ls_locks) { |
| 732 | lock->ls_seqid.counter = 0; |
| 733 | lock->ls_seqid.flags = 0; |
| 734 | lock->ls_flags &= ~NFS_LOCK_INITIALIZED; |
| 735 | } |
| 736 | } |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 737 | spin_unlock(&sp->so_lock); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | static int reclaimer(void *ptr) |
| 742 | { |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 743 | struct nfs_client *clp = ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | struct nfs4_state_owner *sp; |
| 745 | struct nfs4_state_recovery_ops *ops; |
Trond Myklebust | 286d7d6 | 2006-01-03 09:55:26 +0100 | [diff] [blame] | 746 | struct rpc_cred *cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | int status = 0; |
| 748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | allow_signal(SIGKILL); |
| 750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | /* Ensure exclusive access to NFSv4 state */ |
| 752 | lock_kernel(); |
| 753 | down_write(&clp->cl_sem); |
| 754 | /* Are there any NFS mounts out there? */ |
| 755 | if (list_empty(&clp->cl_superblocks)) |
| 756 | goto out; |
| 757 | restart_loop: |
Trond Myklebust | 286d7d6 | 2006-01-03 09:55:26 +0100 | [diff] [blame] | 758 | ops = &nfs4_network_partition_recovery_ops; |
| 759 | /* Are there any open files on this volume? */ |
| 760 | cred = nfs4_get_renew_cred(clp); |
| 761 | if (cred != NULL) { |
| 762 | /* Yes there are: try to renew the old lease */ |
| 763 | status = nfs4_proc_renew(clp, cred); |
| 764 | switch (status) { |
| 765 | case 0: |
| 766 | case -NFS4ERR_CB_PATH_DOWN: |
| 767 | put_rpccred(cred); |
| 768 | goto out; |
| 769 | case -NFS4ERR_STALE_CLIENTID: |
| 770 | case -NFS4ERR_LEASE_MOVED: |
| 771 | ops = &nfs4_reboot_recovery_ops; |
| 772 | } |
| 773 | } else { |
| 774 | /* "reboot" to ensure we clear all state on the server */ |
| 775 | clp->cl_boot_time = CURRENT_TIME; |
| 776 | cred = nfs4_get_setclientid_cred(clp); |
| 777 | } |
| 778 | /* We're going to have to re-establish a clientid */ |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 779 | nfs4_state_mark_reclaim(clp); |
Trond Myklebust | 286d7d6 | 2006-01-03 09:55:26 +0100 | [diff] [blame] | 780 | status = -ENOENT; |
| 781 | if (cred != NULL) { |
| 782 | status = nfs4_init_client(clp, cred); |
| 783 | put_rpccred(cred); |
| 784 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | if (status) |
| 786 | goto out_error; |
| 787 | /* Mark all delegations for reclaim */ |
| 788 | nfs_delegation_mark_reclaim(clp); |
| 789 | /* Note: list is protected by exclusive lock on cl->cl_sem */ |
| 790 | list_for_each_entry(sp, &clp->cl_state_owners, so_list) { |
| 791 | status = nfs4_reclaim_open_state(ops, sp); |
| 792 | if (status < 0) { |
| 793 | if (status == -NFS4ERR_NO_GRACE) { |
| 794 | ops = &nfs4_network_partition_recovery_ops; |
| 795 | status = nfs4_reclaim_open_state(ops, sp); |
| 796 | } |
| 797 | if (status == -NFS4ERR_STALE_CLIENTID) |
| 798 | goto restart_loop; |
| 799 | if (status == -NFS4ERR_EXPIRED) |
| 800 | goto restart_loop; |
| 801 | } |
| 802 | } |
| 803 | nfs_delegation_reap_unclaimed(clp); |
| 804 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | up_write(&clp->cl_sem); |
| 806 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | if (status == -NFS4ERR_CB_PATH_DOWN) |
| 808 | nfs_handle_cb_pathdown(clp); |
Trond Myklebust | 433fbe4 | 2006-01-03 09:55:22 +0100 | [diff] [blame] | 809 | nfs4_clear_recover_bit(clp); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 810 | nfs_put_client(clp); |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame] | 811 | module_put_and_exit(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | return 0; |
| 813 | out_error: |
| 814 | printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n", |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 815 | NIPQUAD(clp->cl_addr.sin_addr), -status); |
Trond Myklebust | 51581f3 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 816 | set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | goto out; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * Local variables: |
| 822 | * c-basic-offset: 8 |
| 823 | * End: |
| 824 | */ |