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 | |
| 235 | static struct nfs4_state_owner * |
| 236 | nfs4_find_state_owner(struct nfs4_client *clp, struct rpc_cred *cred) |
| 237 | { |
| 238 | struct nfs4_state_owner *sp, *res = NULL; |
| 239 | |
| 240 | list_for_each_entry(sp, &clp->cl_state_owners, so_list) { |
| 241 | if (sp->so_cred != cred) |
| 242 | continue; |
| 243 | atomic_inc(&sp->so_count); |
| 244 | /* Move to the head of the list */ |
| 245 | list_move(&sp->so_list, &clp->cl_state_owners); |
| 246 | res = sp; |
| 247 | break; |
| 248 | } |
| 249 | return res; |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to |
| 254 | * create a new state_owner. |
| 255 | * |
| 256 | */ |
| 257 | static struct nfs4_state_owner * |
| 258 | nfs4_alloc_state_owner(void) |
| 259 | { |
| 260 | struct nfs4_state_owner *sp; |
| 261 | |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 262 | sp = kzalloc(sizeof(*sp),GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (!sp) |
| 264 | return NULL; |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 265 | spin_lock_init(&sp->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | INIT_LIST_HEAD(&sp->so_states); |
| 267 | INIT_LIST_HEAD(&sp->so_delegations); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 268 | rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue"); |
| 269 | sp->so_seqid.sequence = &sp->so_sequence; |
| 270 | spin_lock_init(&sp->so_sequence.lock); |
| 271 | INIT_LIST_HEAD(&sp->so_sequence.list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | atomic_set(&sp->so_count, 1); |
| 273 | return sp; |
| 274 | } |
| 275 | |
| 276 | void |
| 277 | nfs4_drop_state_owner(struct nfs4_state_owner *sp) |
| 278 | { |
| 279 | struct nfs4_client *clp = sp->so_client; |
| 280 | spin_lock(&clp->cl_lock); |
| 281 | list_del_init(&sp->so_list); |
| 282 | spin_unlock(&clp->cl_lock); |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Note: must be called with clp->cl_sem held in order to prevent races |
| 287 | * with reboot recovery! |
| 288 | */ |
| 289 | struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred) |
| 290 | { |
| 291 | struct nfs4_client *clp = server->nfs4_state; |
| 292 | struct nfs4_state_owner *sp, *new; |
| 293 | |
| 294 | get_rpccred(cred); |
| 295 | new = nfs4_alloc_state_owner(); |
| 296 | spin_lock(&clp->cl_lock); |
| 297 | sp = nfs4_find_state_owner(clp, cred); |
| 298 | if (sp == NULL) |
| 299 | sp = nfs4_client_grab_unused(clp, cred); |
| 300 | if (sp == NULL && new != NULL) { |
| 301 | list_add(&new->so_list, &clp->cl_state_owners); |
| 302 | new->so_client = clp; |
| 303 | new->so_id = nfs4_alloc_lockowner_id(clp); |
| 304 | new->so_cred = cred; |
| 305 | sp = new; |
| 306 | new = NULL; |
| 307 | } |
| 308 | spin_unlock(&clp->cl_lock); |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 309 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | if (sp != NULL) |
| 311 | return sp; |
| 312 | put_rpccred(cred); |
| 313 | return NULL; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Must be called with clp->cl_sem held in order to avoid races |
| 318 | * with state recovery... |
| 319 | */ |
| 320 | void nfs4_put_state_owner(struct nfs4_state_owner *sp) |
| 321 | { |
| 322 | struct nfs4_client *clp = sp->so_client; |
| 323 | struct rpc_cred *cred = sp->so_cred; |
| 324 | |
| 325 | if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock)) |
| 326 | return; |
| 327 | if (clp->cl_nunused >= OPENOWNER_POOL_SIZE) |
| 328 | goto out_free; |
| 329 | if (list_empty(&sp->so_list)) |
| 330 | goto out_free; |
| 331 | list_move(&sp->so_list, &clp->cl_unused); |
| 332 | clp->cl_nunused++; |
| 333 | spin_unlock(&clp->cl_lock); |
| 334 | put_rpccred(cred); |
| 335 | cred = NULL; |
| 336 | return; |
| 337 | out_free: |
| 338 | list_del(&sp->so_list); |
| 339 | spin_unlock(&clp->cl_lock); |
| 340 | put_rpccred(cred); |
| 341 | kfree(sp); |
| 342 | } |
| 343 | |
| 344 | static struct nfs4_state * |
| 345 | nfs4_alloc_open_state(void) |
| 346 | { |
| 347 | struct nfs4_state *state; |
| 348 | |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 349 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | if (!state) |
| 351 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | atomic_set(&state->count, 1); |
| 353 | INIT_LIST_HEAD(&state->lock_states); |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 354 | spin_lock_init(&state->state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | return state; |
| 356 | } |
| 357 | |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 358 | void |
| 359 | nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode) |
| 360 | { |
| 361 | if (state->state == mode) |
| 362 | return; |
| 363 | /* NB! List reordering - see the reclaim code for why. */ |
| 364 | if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) { |
| 365 | if (mode & FMODE_WRITE) |
| 366 | list_move(&state->open_states, &state->owner->so_states); |
| 367 | else |
| 368 | list_move_tail(&state->open_states, &state->owner->so_states); |
| 369 | } |
| 370 | if (mode == 0) |
| 371 | list_del_init(&state->inode_states); |
| 372 | state->state = mode; |
| 373 | } |
| 374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | static struct nfs4_state * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner) |
| 377 | { |
| 378 | struct nfs_inode *nfsi = NFS_I(inode); |
| 379 | struct nfs4_state *state; |
| 380 | |
| 381 | list_for_each_entry(state, &nfsi->open_states, inode_states) { |
| 382 | /* Is this in the process of being freed? */ |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 383 | if (state->state == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | continue; |
| 385 | if (state->owner == owner) { |
| 386 | atomic_inc(&state->count); |
| 387 | return state; |
| 388 | } |
| 389 | } |
| 390 | return NULL; |
| 391 | } |
| 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | static void |
| 394 | nfs4_free_open_state(struct nfs4_state *state) |
| 395 | { |
| 396 | kfree(state); |
| 397 | } |
| 398 | |
| 399 | struct nfs4_state * |
| 400 | nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner) |
| 401 | { |
| 402 | struct nfs4_state *state, *new; |
| 403 | struct nfs_inode *nfsi = NFS_I(inode); |
| 404 | |
| 405 | spin_lock(&inode->i_lock); |
| 406 | state = __nfs4_find_state_byowner(inode, owner); |
| 407 | spin_unlock(&inode->i_lock); |
| 408 | if (state) |
| 409 | goto out; |
| 410 | new = nfs4_alloc_open_state(); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 411 | spin_lock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | spin_lock(&inode->i_lock); |
| 413 | state = __nfs4_find_state_byowner(inode, owner); |
| 414 | if (state == NULL && new != NULL) { |
| 415 | state = new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | state->owner = owner; |
| 417 | atomic_inc(&owner->so_count); |
| 418 | list_add(&state->inode_states, &nfsi->open_states); |
| 419 | state->inode = igrab(inode); |
| 420 | spin_unlock(&inode->i_lock); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 421 | /* Note: The reclaim code dictates that we add stateless |
| 422 | * and read-only stateids to the end of the list */ |
| 423 | list_add_tail(&state->open_states, &owner->so_states); |
| 424 | spin_unlock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } else { |
| 426 | spin_unlock(&inode->i_lock); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 427 | spin_unlock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | if (new) |
| 429 | nfs4_free_open_state(new); |
| 430 | } |
| 431 | out: |
| 432 | return state; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Beware! Caller must be holding exactly one |
Trond Myklebust | e6dfa55 | 2005-10-18 14:20:13 -0700 | [diff] [blame] | 437 | * reference to clp->cl_sem! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | */ |
| 439 | void nfs4_put_open_state(struct nfs4_state *state) |
| 440 | { |
| 441 | struct inode *inode = state->inode; |
| 442 | struct nfs4_state_owner *owner = state->owner; |
| 443 | |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 444 | if (!atomic_dec_and_lock(&state->count, &owner->so_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | return; |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 446 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | if (!list_empty(&state->inode_states)) |
| 448 | list_del(&state->inode_states); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | list_del(&state->open_states); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 450 | spin_unlock(&inode->i_lock); |
| 451 | spin_unlock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | iput(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | nfs4_free_open_state(state); |
| 454 | nfs4_put_state_owner(owner); |
| 455 | } |
| 456 | |
| 457 | /* |
Trond Myklebust | 83c9d41 | 2005-10-18 14:20:13 -0700 | [diff] [blame] | 458 | * Close the current file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | */ |
| 460 | void nfs4_close_state(struct nfs4_state *state, mode_t mode) |
| 461 | { |
| 462 | struct inode *inode = state->inode; |
| 463 | struct nfs4_state_owner *owner = state->owner; |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 464 | int oldstate, newstate = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
| 466 | atomic_inc(&owner->so_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | /* Protect against nfs4_find_state() */ |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 468 | spin_lock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | spin_lock(&inode->i_lock); |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 470 | switch (mode & (FMODE_READ | FMODE_WRITE)) { |
| 471 | case FMODE_READ: |
| 472 | state->n_rdonly--; |
| 473 | break; |
| 474 | case FMODE_WRITE: |
| 475 | state->n_wronly--; |
| 476 | break; |
| 477 | case FMODE_READ|FMODE_WRITE: |
| 478 | state->n_rdwr--; |
| 479 | } |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 480 | oldstate = newstate = state->state; |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 481 | if (state->n_rdwr == 0) { |
| 482 | if (state->n_rdonly == 0) |
| 483 | newstate &= ~FMODE_READ; |
| 484 | if (state->n_wronly == 0) |
| 485 | newstate &= ~FMODE_WRITE; |
| 486 | } |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 487 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { |
| 488 | nfs4_state_set_mode_locked(state, newstate); |
| 489 | oldstate = newstate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | spin_unlock(&inode->i_lock); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 492 | spin_unlock(&owner->so_lock); |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 493 | |
| 494 | if (oldstate != newstate && nfs4_do_close(inode, state) == 0) |
| 495 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | nfs4_put_open_state(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | nfs4_put_state_owner(owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /* |
| 501 | * Search the state->lock_states for an existing lock_owner |
| 502 | * that is compatible with current->files |
| 503 | */ |
| 504 | static struct nfs4_lock_state * |
| 505 | __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner) |
| 506 | { |
| 507 | struct nfs4_lock_state *pos; |
| 508 | list_for_each_entry(pos, &state->lock_states, ls_locks) { |
| 509 | if (pos->ls_owner != fl_owner) |
| 510 | continue; |
| 511 | atomic_inc(&pos->ls_count); |
| 512 | return pos; |
| 513 | } |
| 514 | return NULL; |
| 515 | } |
| 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | /* |
| 518 | * Return a compatible lock_state. If no initialized lock_state structure |
| 519 | * exists, return an uninitialized one. |
| 520 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | */ |
| 522 | static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner) |
| 523 | { |
| 524 | struct nfs4_lock_state *lsp; |
| 525 | struct nfs4_client *clp = state->owner->so_client; |
| 526 | |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 527 | lsp = kzalloc(sizeof(*lsp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | if (lsp == NULL) |
| 529 | return NULL; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 530 | lsp->ls_seqid.sequence = &state->owner->so_sequence; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | atomic_set(&lsp->ls_count, 1); |
| 532 | lsp->ls_owner = fl_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | spin_lock(&clp->cl_lock); |
| 534 | lsp->ls_id = nfs4_alloc_lockowner_id(clp); |
| 535 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 536 | INIT_LIST_HEAD(&lsp->ls_locks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | return lsp; |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | * Return a compatible lock_state. If no initialized lock_state structure |
| 542 | * exists, return an uninitialized one. |
| 543 | * |
Trond Myklebust | e6dfa55 | 2005-10-18 14:20:13 -0700 | [diff] [blame] | 544 | * The caller must be holding clp->cl_sem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | */ |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 546 | 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] | 547 | { |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 548 | struct nfs4_lock_state *lsp, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 550 | for(;;) { |
| 551 | spin_lock(&state->state_lock); |
| 552 | lsp = __nfs4_find_lock_state(state, owner); |
| 553 | if (lsp != NULL) |
| 554 | break; |
| 555 | if (new != NULL) { |
| 556 | new->ls_state = state; |
| 557 | list_add(&new->ls_locks, &state->lock_states); |
| 558 | set_bit(LK_STATE_IN_USE, &state->flags); |
| 559 | lsp = new; |
| 560 | new = NULL; |
| 561 | break; |
| 562 | } |
| 563 | spin_unlock(&state->state_lock); |
| 564 | new = nfs4_alloc_lock_state(state, owner); |
| 565 | if (new == NULL) |
| 566 | return NULL; |
| 567 | } |
| 568 | spin_unlock(&state->state_lock); |
| 569 | kfree(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); |
| 590 | kfree(lsp); |
| 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | memcpy(dst, &state->stateid, sizeof(*dst)); |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 634 | if (test_bit(LK_STATE_IN_USE, &state->flags) == 0) |
| 635 | return; |
| 636 | |
| 637 | spin_lock(&state->state_lock); |
| 638 | lsp = __nfs4_find_lock_state(state, fl_owner); |
| 639 | if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0) |
| 640 | memcpy(dst, &lsp->ls_stateid, sizeof(*dst)); |
| 641 | spin_unlock(&state->state_lock); |
| 642 | nfs4_put_lock_state(lsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 645 | struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | { |
Trond Myklebust | 36f20c6 | 2005-11-25 17:09:57 -0500 | [diff] [blame] | 647 | struct rpc_sequence *sequence = counter->sequence; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 648 | struct nfs_seqid *new; |
| 649 | |
| 650 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
| 651 | if (new != NULL) { |
| 652 | new->sequence = counter; |
Trond Myklebust | 36f20c6 | 2005-11-25 17:09:57 -0500 | [diff] [blame] | 653 | spin_lock(&sequence->lock); |
| 654 | list_add_tail(&new->list, &sequence->list); |
| 655 | spin_unlock(&sequence->lock); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 656 | } |
| 657 | return new; |
| 658 | } |
| 659 | |
| 660 | void nfs_free_seqid(struct nfs_seqid *seqid) |
| 661 | { |
| 662 | struct rpc_sequence *sequence = seqid->sequence->sequence; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 663 | |
Trond Myklebust | 36f20c6 | 2005-11-25 17:09:57 -0500 | [diff] [blame] | 664 | spin_lock(&sequence->lock); |
| 665 | list_del(&seqid->list); |
| 666 | spin_unlock(&sequence->lock); |
| 667 | rpc_wake_up(&sequence->wait); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 668 | kfree(seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | /* |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 672 | * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or |
| 673 | * failed with a seqid incrementing error - |
| 674 | * see comments nfs_fs.h:seqid_mutating_error() |
| 675 | */ |
| 676 | static inline void nfs_increment_seqid(int status, struct nfs_seqid *seqid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 678 | switch (status) { |
| 679 | case 0: |
| 680 | break; |
| 681 | case -NFS4ERR_BAD_SEQID: |
| 682 | case -NFS4ERR_STALE_CLIENTID: |
| 683 | case -NFS4ERR_STALE_STATEID: |
| 684 | case -NFS4ERR_BAD_STATEID: |
| 685 | case -NFS4ERR_BADXDR: |
| 686 | case -NFS4ERR_RESOURCE: |
| 687 | case -NFS4ERR_NOFILEHANDLE: |
| 688 | /* Non-seqid mutating errors */ |
| 689 | return; |
| 690 | }; |
| 691 | /* |
| 692 | * Note: no locking needed as we are guaranteed to be first |
| 693 | * on the sequence list |
| 694 | */ |
| 695 | seqid->sequence->counter++; |
| 696 | } |
| 697 | |
| 698 | void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid) |
| 699 | { |
| 700 | if (status == -NFS4ERR_BAD_SEQID) { |
| 701 | struct nfs4_state_owner *sp = container_of(seqid->sequence, |
| 702 | struct nfs4_state_owner, so_seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | nfs4_drop_state_owner(sp); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 704 | } |
| 705 | return nfs_increment_seqid(status, seqid); |
| 706 | } |
| 707 | |
| 708 | /* |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 709 | * Increment the seqid if the LOCK/LOCKU succeeded, or |
| 710 | * failed with a seqid incrementing error - |
| 711 | * see comments nfs_fs.h:seqid_mutating_error() |
| 712 | */ |
| 713 | void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid) |
| 714 | { |
| 715 | return nfs_increment_seqid(status, seqid); |
| 716 | } |
| 717 | |
| 718 | int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task) |
| 719 | { |
| 720 | struct rpc_sequence *sequence = seqid->sequence->sequence; |
| 721 | int status = 0; |
| 722 | |
Trond Myklebust | 4e51336 | 2005-10-20 14:22:41 -0700 | [diff] [blame] | 723 | if (sequence->list.next == &seqid->list) |
| 724 | goto out; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 725 | spin_lock(&sequence->lock); |
Trond Myklebust | 36f20c6 | 2005-11-25 17:09:57 -0500 | [diff] [blame] | 726 | if (sequence->list.next != &seqid->list) { |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 727 | rpc_sleep_on(&sequence->wait, task, NULL, NULL); |
| 728 | status = -EAGAIN; |
Trond Myklebust | 36f20c6 | 2005-11-25 17:09:57 -0500 | [diff] [blame] | 729 | } |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 730 | spin_unlock(&sequence->lock); |
Trond Myklebust | 4e51336 | 2005-10-20 14:22:41 -0700 | [diff] [blame] | 731 | out: |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 732 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | static int reclaimer(void *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
Trond Myklebust | 433fbe4 | 2006-01-03 09:55:22 +0100 | [diff] [blame] | 737 | static inline void nfs4_clear_recover_bit(struct nfs4_client *clp) |
| 738 | { |
| 739 | smp_mb__before_clear_bit(); |
| 740 | clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state); |
| 741 | smp_mb__after_clear_bit(); |
| 742 | wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER); |
| 743 | rpc_wake_up(&clp->cl_rpcwaitq); |
| 744 | } |
| 745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | /* |
| 747 | * State recovery routine |
| 748 | */ |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame^] | 749 | static void nfs4_recover_state(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | { |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame^] | 751 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame^] | 753 | __module_get(THIS_MODULE); |
| 754 | atomic_inc(&clp->cl_count); |
| 755 | task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim", |
| 756 | NIPQUAD(clp->cl_addr)); |
| 757 | if (!IS_ERR(task)) |
| 758 | return; |
Trond Myklebust | 433fbe4 | 2006-01-03 09:55:22 +0100 | [diff] [blame] | 759 | nfs4_clear_recover_bit(clp); |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame^] | 760 | nfs4_put_client(clp); |
| 761 | module_put(THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /* |
| 765 | * Schedule a state recovery attempt |
| 766 | */ |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame^] | 767 | void nfs4_schedule_state_recovery(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | { |
| 769 | if (!clp) |
| 770 | return; |
Trond Myklebust | 433fbe4 | 2006-01-03 09:55:22 +0100 | [diff] [blame] | 771 | if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0) |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame^] | 772 | nfs4_recover_state(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state) |
| 776 | { |
| 777 | struct inode *inode = state->inode; |
| 778 | struct file_lock *fl; |
| 779 | int status = 0; |
| 780 | |
| 781 | for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) { |
Trond Myklebust | 43b2a33 | 2005-11-04 15:35:30 -0500 | [diff] [blame] | 782 | if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | continue; |
| 784 | if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state) |
| 785 | continue; |
| 786 | status = ops->recover_lock(state, fl); |
| 787 | if (status >= 0) |
| 788 | continue; |
| 789 | switch (status) { |
| 790 | default: |
| 791 | printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n", |
| 792 | __FUNCTION__, status); |
| 793 | case -NFS4ERR_EXPIRED: |
| 794 | case -NFS4ERR_NO_GRACE: |
| 795 | case -NFS4ERR_RECLAIM_BAD: |
| 796 | case -NFS4ERR_RECLAIM_CONFLICT: |
Trond Myklebust | 43b2a33 | 2005-11-04 15:35:30 -0500 | [diff] [blame] | 797 | /* kill_proc(fl->fl_pid, SIGLOST, 1); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | break; |
| 799 | case -NFS4ERR_STALE_CLIENTID: |
| 800 | goto out_err; |
| 801 | } |
| 802 | } |
| 803 | return 0; |
| 804 | out_err: |
| 805 | return status; |
| 806 | } |
| 807 | |
| 808 | static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp) |
| 809 | { |
| 810 | struct nfs4_state *state; |
| 811 | struct nfs4_lock_state *lock; |
| 812 | int status = 0; |
| 813 | |
| 814 | /* Note: we rely on the sp->so_states list being ordered |
| 815 | * so that we always reclaim open(O_RDWR) and/or open(O_WRITE) |
| 816 | * states first. |
| 817 | * This is needed to ensure that the server won't give us any |
| 818 | * read delegations that we have to return if, say, we are |
| 819 | * recovering after a network partition or a reboot from a |
| 820 | * server that doesn't support a grace period. |
| 821 | */ |
| 822 | list_for_each_entry(state, &sp->so_states, open_states) { |
| 823 | if (state->state == 0) |
| 824 | continue; |
| 825 | status = ops->recover_open(sp, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | if (status >= 0) { |
| 827 | status = nfs4_reclaim_locks(ops, state); |
| 828 | if (status < 0) |
| 829 | goto out_err; |
| 830 | list_for_each_entry(lock, &state->lock_states, ls_locks) { |
| 831 | if (!(lock->ls_flags & NFS_LOCK_INITIALIZED)) |
| 832 | printk("%s: Lock reclaim failed!\n", |
| 833 | __FUNCTION__); |
| 834 | } |
| 835 | continue; |
| 836 | } |
| 837 | switch (status) { |
| 838 | default: |
| 839 | printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n", |
| 840 | __FUNCTION__, status); |
| 841 | case -ENOENT: |
| 842 | case -NFS4ERR_RECLAIM_BAD: |
| 843 | case -NFS4ERR_RECLAIM_CONFLICT: |
| 844 | /* |
| 845 | * Open state on this file cannot be recovered |
| 846 | * All we can do is revert to using the zero stateid. |
| 847 | */ |
| 848 | memset(state->stateid.data, 0, |
| 849 | sizeof(state->stateid.data)); |
| 850 | /* Mark the file as being 'closed' */ |
| 851 | state->state = 0; |
| 852 | break; |
| 853 | case -NFS4ERR_EXPIRED: |
| 854 | case -NFS4ERR_NO_GRACE: |
| 855 | case -NFS4ERR_STALE_CLIENTID: |
| 856 | goto out_err; |
| 857 | } |
| 858 | } |
| 859 | return 0; |
| 860 | out_err: |
| 861 | return status; |
| 862 | } |
| 863 | |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 864 | static void nfs4_state_mark_reclaim(struct nfs4_client *clp) |
| 865 | { |
| 866 | struct nfs4_state_owner *sp; |
| 867 | struct nfs4_state *state; |
| 868 | struct nfs4_lock_state *lock; |
| 869 | |
| 870 | /* Reset all sequence ids to zero */ |
| 871 | list_for_each_entry(sp, &clp->cl_state_owners, so_list) { |
| 872 | sp->so_seqid.counter = 0; |
| 873 | sp->so_seqid.flags = 0; |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 874 | spin_lock(&sp->so_lock); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 875 | list_for_each_entry(state, &sp->so_states, open_states) { |
| 876 | list_for_each_entry(lock, &state->lock_states, ls_locks) { |
| 877 | lock->ls_seqid.counter = 0; |
| 878 | lock->ls_seqid.flags = 0; |
| 879 | lock->ls_flags &= ~NFS_LOCK_INITIALIZED; |
| 880 | } |
| 881 | } |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 882 | spin_unlock(&sp->so_lock); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | static int reclaimer(void *ptr) |
| 887 | { |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame^] | 888 | struct nfs4_client *clp = ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | struct nfs4_state_owner *sp; |
| 890 | struct nfs4_state_recovery_ops *ops; |
| 891 | int status = 0; |
| 892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | allow_signal(SIGKILL); |
| 894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | /* Ensure exclusive access to NFSv4 state */ |
| 896 | lock_kernel(); |
| 897 | down_write(&clp->cl_sem); |
| 898 | /* Are there any NFS mounts out there? */ |
| 899 | if (list_empty(&clp->cl_superblocks)) |
| 900 | goto out; |
| 901 | restart_loop: |
| 902 | status = nfs4_proc_renew(clp); |
| 903 | switch (status) { |
| 904 | case 0: |
| 905 | case -NFS4ERR_CB_PATH_DOWN: |
| 906 | goto out; |
| 907 | case -NFS4ERR_STALE_CLIENTID: |
| 908 | case -NFS4ERR_LEASE_MOVED: |
| 909 | ops = &nfs4_reboot_recovery_ops; |
| 910 | break; |
| 911 | default: |
| 912 | ops = &nfs4_network_partition_recovery_ops; |
| 913 | }; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 914 | nfs4_state_mark_reclaim(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | status = __nfs4_init_client(clp); |
| 916 | if (status) |
| 917 | goto out_error; |
| 918 | /* Mark all delegations for reclaim */ |
| 919 | nfs_delegation_mark_reclaim(clp); |
| 920 | /* Note: list is protected by exclusive lock on cl->cl_sem */ |
| 921 | list_for_each_entry(sp, &clp->cl_state_owners, so_list) { |
| 922 | status = nfs4_reclaim_open_state(ops, sp); |
| 923 | if (status < 0) { |
| 924 | if (status == -NFS4ERR_NO_GRACE) { |
| 925 | ops = &nfs4_network_partition_recovery_ops; |
| 926 | status = nfs4_reclaim_open_state(ops, sp); |
| 927 | } |
| 928 | if (status == -NFS4ERR_STALE_CLIENTID) |
| 929 | goto restart_loop; |
| 930 | if (status == -NFS4ERR_EXPIRED) |
| 931 | goto restart_loop; |
| 932 | } |
| 933 | } |
| 934 | nfs_delegation_reap_unclaimed(clp); |
| 935 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | up_write(&clp->cl_sem); |
| 937 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | if (status == -NFS4ERR_CB_PATH_DOWN) |
| 939 | nfs_handle_cb_pathdown(clp); |
Trond Myklebust | 433fbe4 | 2006-01-03 09:55:22 +0100 | [diff] [blame] | 940 | nfs4_clear_recover_bit(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | nfs4_put_client(clp); |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame^] | 942 | module_put_and_exit(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | return 0; |
| 944 | out_error: |
| 945 | printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n", |
| 946 | NIPQUAD(clp->cl_addr.s_addr), -status); |
| 947 | goto out; |
| 948 | } |
| 949 | |
| 950 | /* |
| 951 | * Local variables: |
| 952 | * c-basic-offset: 8 |
| 953 | * End: |
| 954 | */ |