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