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