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