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