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