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