Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/nfs/nfs4state.c |
| 3 | * |
| 4 | * Client-side XDR for NFSv4. |
| 5 | * |
| 6 | * Copyright (c) 2002 The Regents of the University of Michigan. |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Kendrick Smith <kmsmith@umich.edu> |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer. |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * 3. Neither the name of the University nor the names of its |
| 21 | * contributors may be used to endorse or promote products derived |
| 22 | * from this software without specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 31 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 32 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 33 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | * |
| 36 | * Implementation of the NFSv4 state model. For the time being, |
| 37 | * this is minimal, but will be made much more complex in a |
| 38 | * subsequent patch. |
| 39 | */ |
| 40 | |
Trond Myklebust | 6f43ddc | 2007-07-08 16:49:11 -0400 | [diff] [blame] | 41 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/slab.h> |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 43 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/nfs_fs.h> |
| 45 | #include <linux/nfs_idmap.h> |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame] | 46 | #include <linux/kthread.h> |
| 47 | #include <linux/module.h> |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 48 | #include <linux/random.h> |
Randy Dunlap | 8c7597f | 2010-10-22 16:18:52 -0700 | [diff] [blame] | 49 | #include <linux/ratelimit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/workqueue.h> |
| 51 | #include <linux/bitops.h> |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 52 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 54 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include "callback.h" |
| 56 | #include "delegation.h" |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 57 | #include "internal.h" |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 58 | #include "pnfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Chuck Lever | e3c0fb7 | 2012-05-21 22:45:24 -0400 | [diff] [blame] | 60 | #define NFSDBG_FACILITY NFSDBG_STATE |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #define OPENOWNER_POOL_SIZE 8 |
| 63 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 64 | const nfs4_stateid zero_stateid; |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static LIST_HEAD(nfs4_clientid_list); |
| 67 | |
Andy Adamson | 591d71c | 2009-04-01 09:22:47 -0400 | [diff] [blame] | 68 | int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
Trond Myklebust | fd954ae | 2011-04-24 14:28:18 -0400 | [diff] [blame] | 70 | struct nfs4_setclientid_res clid = { |
| 71 | .clientid = clp->cl_clientid, |
| 72 | .confirm = clp->cl_confirm, |
| 73 | }; |
Chuck Lever | f738f51 | 2009-03-18 20:48:06 -0400 | [diff] [blame] | 74 | unsigned short port; |
| 75 | int status; |
| 76 | |
Trond Myklebust | fd954ae | 2011-04-24 14:28:18 -0400 | [diff] [blame] | 77 | if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state)) |
| 78 | goto do_confirm; |
Chuck Lever | f738f51 | 2009-03-18 20:48:06 -0400 | [diff] [blame] | 79 | port = nfs_callback_tcpport; |
| 80 | if (clp->cl_addr.ss_family == AF_INET6) |
| 81 | port = nfs_callback_tcpport6; |
| 82 | |
Trond Myklebust | bb8b27e | 2010-04-16 16:43:06 -0400 | [diff] [blame] | 83 | status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid); |
| 84 | if (status != 0) |
| 85 | goto out; |
Trond Myklebust | fd954ae | 2011-04-24 14:28:18 -0400 | [diff] [blame] | 86 | clp->cl_clientid = clid.clientid; |
| 87 | clp->cl_confirm = clid.confirm; |
| 88 | set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state); |
| 89 | do_confirm: |
Trond Myklebust | bb8b27e | 2010-04-16 16:43:06 -0400 | [diff] [blame] | 90 | status = nfs4_proc_setclientid_confirm(clp, &clid, cred); |
| 91 | if (status != 0) |
| 92 | goto out; |
Trond Myklebust | fd954ae | 2011-04-24 14:28:18 -0400 | [diff] [blame] | 93 | clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state); |
Trond Myklebust | bb8b27e | 2010-04-16 16:43:06 -0400 | [diff] [blame] | 94 | nfs4_schedule_state_renewal(clp); |
| 95 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return status; |
| 97 | } |
| 98 | |
Andy Adamson | a7b7210 | 2009-04-01 09:22:46 -0400 | [diff] [blame] | 99 | struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp) |
Trond Myklebust | a2b2bb8 | 2008-04-08 16:02:17 -0400 | [diff] [blame] | 100 | { |
| 101 | struct rpc_cred *cred = NULL; |
| 102 | |
Trond Myklebust | a2b2bb8 | 2008-04-08 16:02:17 -0400 | [diff] [blame] | 103 | if (clp->cl_machine_cred != NULL) |
| 104 | cred = get_rpccred(clp->cl_machine_cred); |
Trond Myklebust | a2b2bb8 | 2008-04-08 16:02:17 -0400 | [diff] [blame] | 105 | return cred; |
| 106 | } |
| 107 | |
| 108 | static void nfs4_clear_machine_cred(struct nfs_client *clp) |
| 109 | { |
| 110 | struct rpc_cred *cred; |
| 111 | |
| 112 | spin_lock(&clp->cl_lock); |
| 113 | cred = clp->cl_machine_cred; |
| 114 | clp->cl_machine_cred = NULL; |
| 115 | spin_unlock(&clp->cl_lock); |
| 116 | if (cred != NULL) |
| 117 | put_rpccred(cred); |
| 118 | } |
| 119 | |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 120 | static struct rpc_cred * |
| 121 | nfs4_get_renew_cred_server_locked(struct nfs_server *server) |
Trond Myklebust | b4454fe | 2006-01-03 09:55:25 +0100 | [diff] [blame] | 122 | { |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 123 | struct rpc_cred *cred = NULL; |
Trond Myklebust | b4454fe | 2006-01-03 09:55:25 +0100 | [diff] [blame] | 124 | struct nfs4_state_owner *sp; |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 125 | struct rb_node *pos; |
Trond Myklebust | b4454fe | 2006-01-03 09:55:25 +0100 | [diff] [blame] | 126 | |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 127 | for (pos = rb_first(&server->state_owners); |
| 128 | pos != NULL; |
| 129 | pos = rb_next(pos)) { |
| 130 | sp = rb_entry(pos, struct nfs4_state_owner, so_server_node); |
Trond Myklebust | b4454fe | 2006-01-03 09:55:25 +0100 | [diff] [blame] | 131 | if (list_empty(&sp->so_states)) |
| 132 | continue; |
| 133 | cred = get_rpccred(sp->so_cred); |
| 134 | break; |
| 135 | } |
| 136 | return cred; |
| 137 | } |
| 138 | |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 139 | /** |
| 140 | * nfs4_get_renew_cred_locked - Acquire credential for a renew operation |
| 141 | * @clp: client state handle |
| 142 | * |
| 143 | * Returns an rpc_cred with reference count bumped, or NULL. |
| 144 | * Caller must hold clp->cl_lock. |
| 145 | */ |
| 146 | struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp) |
| 147 | { |
| 148 | struct rpc_cred *cred = NULL; |
| 149 | struct nfs_server *server; |
| 150 | |
Sachin Prabhu | e49a29b | 2012-03-16 19:25:52 +0000 | [diff] [blame] | 151 | /* Use machine credentials if available */ |
| 152 | cred = nfs4_get_machine_cred_locked(clp); |
| 153 | if (cred != NULL) |
| 154 | goto out; |
| 155 | |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 156 | rcu_read_lock(); |
| 157 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { |
| 158 | cred = nfs4_get_renew_cred_server_locked(server); |
| 159 | if (cred != NULL) |
| 160 | break; |
| 161 | } |
| 162 | rcu_read_unlock(); |
Sachin Prabhu | e49a29b | 2012-03-16 19:25:52 +0000 | [diff] [blame] | 163 | |
| 164 | out: |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 165 | return cred; |
| 166 | } |
| 167 | |
Andy Adamson | b4b8260 | 2009-04-01 09:22:49 -0400 | [diff] [blame] | 168 | #if defined(CONFIG_NFS_V4_1) |
| 169 | |
Ricardo Labiaga | 9430fb6 | 2009-12-06 12:23:46 -0500 | [diff] [blame] | 170 | static int nfs41_setup_state_renewal(struct nfs_client *clp) |
| 171 | { |
| 172 | int status; |
| 173 | struct nfs_fsinfo fsinfo; |
| 174 | |
Andy Adamson | d6fb79d | 2011-03-01 01:34:11 +0000 | [diff] [blame] | 175 | if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) { |
| 176 | nfs4_schedule_state_renewal(clp); |
| 177 | return 0; |
| 178 | } |
| 179 | |
Ricardo Labiaga | 9430fb6 | 2009-12-06 12:23:46 -0500 | [diff] [blame] | 180 | status = nfs4_proc_get_lease_time(clp, &fsinfo); |
| 181 | if (status == 0) { |
| 182 | /* Update lease time and schedule renewal */ |
| 183 | spin_lock(&clp->cl_lock); |
| 184 | clp->cl_lease_time = fsinfo.lease_time * HZ; |
| 185 | clp->cl_last_renewal = jiffies; |
| 186 | spin_unlock(&clp->cl_lock); |
| 187 | |
| 188 | nfs4_schedule_state_renewal(clp); |
| 189 | } |
| 190 | |
| 191 | return status; |
| 192 | } |
| 193 | |
Andy Adamson | 42acd02 | 2011-01-06 02:04:34 +0000 | [diff] [blame] | 194 | /* |
| 195 | * Back channel returns NFS4ERR_DELAY for new requests when |
| 196 | * NFS4_SESSION_DRAINING is set so there is no work to be done when draining |
| 197 | * is ended. |
| 198 | */ |
Alexandros Batsakis | 5601a00 | 2009-12-14 21:27:58 -0800 | [diff] [blame] | 199 | static void nfs4_end_drain_session(struct nfs_client *clp) |
Ricardo Labiaga | 9dfdf40 | 2009-12-06 12:57:34 -0500 | [diff] [blame] | 200 | { |
Alexandros Batsakis | 5601a00 | 2009-12-14 21:27:58 -0800 | [diff] [blame] | 201 | struct nfs4_session *ses = clp->cl_session; |
Trond Myklebust | 961a828 | 2012-01-17 22:57:37 -0500 | [diff] [blame] | 202 | struct nfs4_slot_table *tbl; |
Alexandros Batsakis | 689cf5c | 2009-12-14 21:27:56 -0800 | [diff] [blame] | 203 | int max_slots; |
| 204 | |
Trond Myklebust | a2118c3 | 2010-06-16 09:52:26 -0400 | [diff] [blame] | 205 | if (ses == NULL) |
| 206 | return; |
Trond Myklebust | 961a828 | 2012-01-17 22:57:37 -0500 | [diff] [blame] | 207 | tbl = &ses->fc_slot_table; |
Trond Myklebust | a2118c3 | 2010-06-16 09:52:26 -0400 | [diff] [blame] | 208 | if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) { |
Trond Myklebust | 961a828 | 2012-01-17 22:57:37 -0500 | [diff] [blame] | 209 | spin_lock(&tbl->slot_tbl_lock); |
| 210 | max_slots = tbl->max_slots; |
Alexandros Batsakis | 689cf5c | 2009-12-14 21:27:56 -0800 | [diff] [blame] | 211 | while (max_slots--) { |
Trond Myklebust | 961a828 | 2012-01-17 22:57:37 -0500 | [diff] [blame] | 212 | if (rpc_wake_up_first(&tbl->slot_tbl_waitq, |
| 213 | nfs4_set_task_privileged, |
| 214 | NULL) == NULL) |
Alexandros Batsakis | 689cf5c | 2009-12-14 21:27:56 -0800 | [diff] [blame] | 215 | break; |
Alexandros Batsakis | 689cf5c | 2009-12-14 21:27:56 -0800 | [diff] [blame] | 216 | } |
Trond Myklebust | 961a828 | 2012-01-17 22:57:37 -0500 | [diff] [blame] | 217 | spin_unlock(&tbl->slot_tbl_lock); |
Alexandros Batsakis | 689cf5c | 2009-12-14 21:27:56 -0800 | [diff] [blame] | 218 | } |
Ricardo Labiaga | 9dfdf40 | 2009-12-06 12:57:34 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Andy Adamson | 42acd02 | 2011-01-06 02:04:34 +0000 | [diff] [blame] | 221 | static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl) |
Ricardo Labiaga | 9dfdf40 | 2009-12-06 12:57:34 -0500 | [diff] [blame] | 222 | { |
Ricardo Labiaga | 9dfdf40 | 2009-12-06 12:57:34 -0500 | [diff] [blame] | 223 | spin_lock(&tbl->slot_tbl_lock); |
Andy Adamson | b6bf6e7 | 2012-02-17 13:05:23 -0500 | [diff] [blame] | 224 | if (tbl->highest_used_slotid != NFS4_NO_SLOT) { |
Andy Adamson | 42acd02 | 2011-01-06 02:04:34 +0000 | [diff] [blame] | 225 | INIT_COMPLETION(tbl->complete); |
Ricardo Labiaga | 9dfdf40 | 2009-12-06 12:57:34 -0500 | [diff] [blame] | 226 | spin_unlock(&tbl->slot_tbl_lock); |
Andy Adamson | 42acd02 | 2011-01-06 02:04:34 +0000 | [diff] [blame] | 227 | return wait_for_completion_interruptible(&tbl->complete); |
Ricardo Labiaga | 9dfdf40 | 2009-12-06 12:57:34 -0500 | [diff] [blame] | 228 | } |
| 229 | spin_unlock(&tbl->slot_tbl_lock); |
| 230 | return 0; |
| 231 | } |
| 232 | |
Andy Adamson | 42acd02 | 2011-01-06 02:04:34 +0000 | [diff] [blame] | 233 | static int nfs4_begin_drain_session(struct nfs_client *clp) |
| 234 | { |
| 235 | struct nfs4_session *ses = clp->cl_session; |
| 236 | int ret = 0; |
| 237 | |
| 238 | set_bit(NFS4_SESSION_DRAINING, &ses->session_state); |
| 239 | /* back channel */ |
| 240 | ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table); |
| 241 | if (ret) |
| 242 | return ret; |
| 243 | /* fore channel */ |
| 244 | return nfs4_wait_on_slot_tbl(&ses->fc_slot_table); |
| 245 | } |
| 246 | |
Andy Adamson | 4d643d1 | 2009-12-04 15:52:24 -0500 | [diff] [blame] | 247 | int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred) |
| 248 | { |
| 249 | int status; |
| 250 | |
Trond Myklebust | fd954ae | 2011-04-24 14:28:18 -0400 | [diff] [blame] | 251 | if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state)) |
| 252 | goto do_confirm; |
Trond Myklebust | 3804541 | 2009-12-15 17:36:57 -0500 | [diff] [blame] | 253 | nfs4_begin_drain_session(clp); |
Andy Adamson | 4d643d1 | 2009-12-04 15:52:24 -0500 | [diff] [blame] | 254 | status = nfs4_proc_exchange_id(clp, cred); |
Ricardo Labiaga | 9430fb6 | 2009-12-06 12:23:46 -0500 | [diff] [blame] | 255 | if (status != 0) |
| 256 | goto out; |
Trond Myklebust | fd954ae | 2011-04-24 14:28:18 -0400 | [diff] [blame] | 257 | set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state); |
| 258 | do_confirm: |
Ricardo Labiaga | 9430fb6 | 2009-12-06 12:23:46 -0500 | [diff] [blame] | 259 | status = nfs4_proc_create_session(clp); |
| 260 | if (status != 0) |
| 261 | goto out; |
Trond Myklebust | fd954ae | 2011-04-24 14:28:18 -0400 | [diff] [blame] | 262 | clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state); |
Ricardo Labiaga | 9430fb6 | 2009-12-06 12:23:46 -0500 | [diff] [blame] | 263 | nfs41_setup_state_renewal(clp); |
| 264 | nfs_mark_client_ready(clp, NFS_CS_READY); |
| 265 | out: |
Andy Adamson | 4d643d1 | 2009-12-04 15:52:24 -0500 | [diff] [blame] | 266 | return status; |
| 267 | } |
| 268 | |
Andy Adamson | b4b8260 | 2009-04-01 09:22:49 -0400 | [diff] [blame] | 269 | struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp) |
| 270 | { |
| 271 | struct rpc_cred *cred; |
| 272 | |
| 273 | spin_lock(&clp->cl_lock); |
| 274 | cred = nfs4_get_machine_cred_locked(clp); |
| 275 | spin_unlock(&clp->cl_lock); |
| 276 | return cred; |
| 277 | } |
| 278 | |
| 279 | #endif /* CONFIG_NFS_V4_1 */ |
| 280 | |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 281 | static struct rpc_cred * |
| 282 | nfs4_get_setclientid_cred_server(struct nfs_server *server) |
Trond Myklebust | 286d7d6 | 2006-01-03 09:55:26 +0100 | [diff] [blame] | 283 | { |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 284 | struct nfs_client *clp = server->nfs_client; |
| 285 | struct rpc_cred *cred = NULL; |
Trond Myklebust | 286d7d6 | 2006-01-03 09:55:26 +0100 | [diff] [blame] | 286 | struct nfs4_state_owner *sp; |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 287 | struct rb_node *pos; |
Trond Myklebust | 286d7d6 | 2006-01-03 09:55:26 +0100 | [diff] [blame] | 288 | |
Trond Myklebust | 6dc9d57 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 289 | spin_lock(&clp->cl_lock); |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 290 | pos = rb_first(&server->state_owners); |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 291 | if (pos != NULL) { |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 292 | sp = rb_entry(pos, struct nfs4_state_owner, so_server_node); |
Trond Myklebust | a2b2bb8 | 2008-04-08 16:02:17 -0400 | [diff] [blame] | 293 | cred = get_rpccred(sp->so_cred); |
Trond Myklebust | 286d7d6 | 2006-01-03 09:55:26 +0100 | [diff] [blame] | 294 | } |
Trond Myklebust | 6dc9d57 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 295 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | a2b2bb8 | 2008-04-08 16:02:17 -0400 | [diff] [blame] | 296 | return cred; |
Trond Myklebust | 286d7d6 | 2006-01-03 09:55:26 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 299 | /** |
| 300 | * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation |
| 301 | * @clp: client state handle |
| 302 | * |
| 303 | * Returns an rpc_cred with reference count bumped, or NULL. |
| 304 | */ |
| 305 | struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp) |
| 306 | { |
| 307 | struct nfs_server *server; |
| 308 | struct rpc_cred *cred; |
| 309 | |
| 310 | spin_lock(&clp->cl_lock); |
| 311 | cred = nfs4_get_machine_cred_locked(clp); |
| 312 | spin_unlock(&clp->cl_lock); |
| 313 | if (cred != NULL) |
| 314 | goto out; |
| 315 | |
| 316 | rcu_read_lock(); |
| 317 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { |
| 318 | cred = nfs4_get_setclientid_cred_server(server); |
| 319 | if (cred != NULL) |
| 320 | break; |
| 321 | } |
| 322 | rcu_read_unlock(); |
| 323 | |
| 324 | out: |
| 325 | return cred; |
| 326 | } |
| 327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | static struct nfs4_state_owner * |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 329 | nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | { |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 331 | struct rb_node **p = &server->state_owners.rb_node, |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 332 | *parent = NULL; |
Chuck Lever | 414adf1 | 2011-12-06 16:13:39 -0500 | [diff] [blame] | 333 | struct nfs4_state_owner *sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 335 | while (*p != NULL) { |
| 336 | parent = *p; |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 337 | sp = rb_entry(parent, struct nfs4_state_owner, so_server_node); |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 338 | |
| 339 | if (cred < sp->so_cred) |
| 340 | p = &parent->rb_left; |
| 341 | else if (cred > sp->so_cred) |
| 342 | p = &parent->rb_right; |
| 343 | else { |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 344 | if (!list_empty(&sp->so_lru)) |
| 345 | list_del_init(&sp->so_lru); |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 346 | atomic_inc(&sp->so_count); |
Chuck Lever | 414adf1 | 2011-12-06 16:13:39 -0500 | [diff] [blame] | 347 | return sp; |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 348 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
Chuck Lever | 414adf1 | 2011-12-06 16:13:39 -0500 | [diff] [blame] | 350 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 353 | static struct nfs4_state_owner * |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 354 | nfs4_insert_state_owner_locked(struct nfs4_state_owner *new) |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 355 | { |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 356 | struct nfs_server *server = new->so_server; |
| 357 | struct rb_node **p = &server->state_owners.rb_node, |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 358 | *parent = NULL; |
| 359 | struct nfs4_state_owner *sp; |
Trond Myklebust | 9157c31 | 2012-01-17 22:04:24 -0500 | [diff] [blame] | 360 | int err; |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 361 | |
| 362 | while (*p != NULL) { |
| 363 | parent = *p; |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 364 | sp = rb_entry(parent, struct nfs4_state_owner, so_server_node); |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 365 | |
| 366 | if (new->so_cred < sp->so_cred) |
| 367 | p = &parent->rb_left; |
| 368 | else if (new->so_cred > sp->so_cred) |
| 369 | p = &parent->rb_right; |
| 370 | else { |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 371 | if (!list_empty(&sp->so_lru)) |
| 372 | list_del_init(&sp->so_lru); |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 373 | atomic_inc(&sp->so_count); |
| 374 | return sp; |
| 375 | } |
| 376 | } |
Trond Myklebust | 48c22eb | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 377 | err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id); |
Trond Myklebust | 9157c31 | 2012-01-17 22:04:24 -0500 | [diff] [blame] | 378 | if (err) |
| 379 | return ERR_PTR(err); |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 380 | rb_link_node(&new->so_server_node, parent, p); |
| 381 | rb_insert_color(&new->so_server_node, &server->state_owners); |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 382 | return new; |
| 383 | } |
| 384 | |
| 385 | static void |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 386 | nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp) |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 387 | { |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 388 | struct nfs_server *server = sp->so_server; |
| 389 | |
| 390 | if (!RB_EMPTY_NODE(&sp->so_server_node)) |
| 391 | rb_erase(&sp->so_server_node, &server->state_owners); |
Trond Myklebust | 48c22eb | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 392 | ida_remove(&server->openowner_id, sp->so_seqid.owner_id); |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 393 | } |
| 394 | |
Trond Myklebust | 7ba127a | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 395 | static void |
| 396 | nfs4_init_seqid_counter(struct nfs_seqid_counter *sc) |
| 397 | { |
Trond Myklebust | 95b72eb | 2012-04-20 19:24:51 -0400 | [diff] [blame] | 398 | sc->create_time = ktime_get(); |
Trond Myklebust | 7ba127a | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 399 | sc->flags = 0; |
| 400 | sc->counter = 0; |
| 401 | spin_lock_init(&sc->lock); |
| 402 | INIT_LIST_HEAD(&sc->list); |
| 403 | rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue"); |
| 404 | } |
| 405 | |
| 406 | static void |
| 407 | nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc) |
| 408 | { |
| 409 | rpc_destroy_wait_queue(&sc->wait); |
| 410 | } |
| 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | /* |
| 413 | * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to |
| 414 | * create a new state_owner. |
| 415 | * |
| 416 | */ |
| 417 | static struct nfs4_state_owner * |
Trond Myklebust | d1e284d | 2012-01-17 22:04:24 -0500 | [diff] [blame] | 418 | nfs4_alloc_state_owner(struct nfs_server *server, |
| 419 | struct rpc_cred *cred, |
| 420 | gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | { |
| 422 | struct nfs4_state_owner *sp; |
| 423 | |
Trond Myklebust | d1e284d | 2012-01-17 22:04:24 -0500 | [diff] [blame] | 424 | sp = kzalloc(sizeof(*sp), gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | if (!sp) |
| 426 | return NULL; |
Trond Myklebust | d1e284d | 2012-01-17 22:04:24 -0500 | [diff] [blame] | 427 | sp->so_server = server; |
| 428 | sp->so_cred = get_rpccred(cred); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 429 | spin_lock_init(&sp->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | INIT_LIST_HEAD(&sp->so_states); |
Trond Myklebust | 7ba127a | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 431 | nfs4_init_seqid_counter(&sp->so_seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | atomic_set(&sp->so_count, 1); |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 433 | INIT_LIST_HEAD(&sp->so_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | return sp; |
| 435 | } |
| 436 | |
Adrian Bunk | 1d2e88e | 2008-05-02 13:42:45 -0700 | [diff] [blame] | 437 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | nfs4_drop_state_owner(struct nfs4_state_owner *sp) |
| 439 | { |
Trond Myklebust | c77365c | 2012-04-21 12:31:05 -0400 | [diff] [blame] | 440 | struct rb_node *rb_node = &sp->so_server_node; |
| 441 | |
| 442 | if (!RB_EMPTY_NODE(rb_node)) { |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 443 | struct nfs_server *server = sp->so_server; |
| 444 | struct nfs_client *clp = server->nfs_client; |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 445 | |
| 446 | spin_lock(&clp->cl_lock); |
Trond Myklebust | c77365c | 2012-04-21 12:31:05 -0400 | [diff] [blame] | 447 | if (!RB_EMPTY_NODE(rb_node)) { |
| 448 | rb_erase(rb_node, &server->state_owners); |
| 449 | RB_CLEAR_NODE(rb_node); |
| 450 | } |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 451 | spin_unlock(&clp->cl_lock); |
| 452 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 455 | static void nfs4_free_state_owner(struct nfs4_state_owner *sp) |
| 456 | { |
Trond Myklebust | 7ba127a | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 457 | nfs4_destroy_seqid_counter(&sp->so_seqid); |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 458 | put_rpccred(sp->so_cred); |
| 459 | kfree(sp); |
| 460 | } |
| 461 | |
| 462 | static void nfs4_gc_state_owners(struct nfs_server *server) |
| 463 | { |
| 464 | struct nfs_client *clp = server->nfs_client; |
| 465 | struct nfs4_state_owner *sp, *tmp; |
| 466 | unsigned long time_min, time_max; |
| 467 | LIST_HEAD(doomed); |
| 468 | |
| 469 | spin_lock(&clp->cl_lock); |
| 470 | time_max = jiffies; |
| 471 | time_min = (long)time_max - (long)clp->cl_lease_time; |
| 472 | list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) { |
| 473 | /* NB: LRU is sorted so that oldest is at the head */ |
| 474 | if (time_in_range(sp->so_expires, time_min, time_max)) |
| 475 | break; |
| 476 | list_move(&sp->so_lru, &doomed); |
| 477 | nfs4_remove_state_owner_locked(sp); |
| 478 | } |
| 479 | spin_unlock(&clp->cl_lock); |
| 480 | |
| 481 | list_for_each_entry_safe(sp, tmp, &doomed, so_lru) { |
| 482 | list_del(&sp->so_lru); |
| 483 | nfs4_free_state_owner(sp); |
| 484 | } |
| 485 | } |
| 486 | |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 487 | /** |
| 488 | * nfs4_get_state_owner - Look up a state owner given a credential |
| 489 | * @server: nfs_server to search |
| 490 | * @cred: RPC credential to match |
| 491 | * |
| 492 | * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL. |
| 493 | */ |
| 494 | struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, |
Trond Myklebust | d1e284d | 2012-01-17 22:04:24 -0500 | [diff] [blame] | 495 | struct rpc_cred *cred, |
| 496 | gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 498 | struct nfs_client *clp = server->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | struct nfs4_state_owner *sp, *new; |
| 500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | spin_lock(&clp->cl_lock); |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 502 | sp = nfs4_find_state_owner_locked(server, cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | spin_unlock(&clp->cl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | if (sp != NULL) |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 505 | goto out; |
Trond Myklebust | d1e284d | 2012-01-17 22:04:24 -0500 | [diff] [blame] | 506 | new = nfs4_alloc_state_owner(server, cred, gfp_flags); |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 507 | if (new == NULL) |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 508 | goto out; |
Trond Myklebust | 9157c31 | 2012-01-17 22:04:24 -0500 | [diff] [blame] | 509 | do { |
| 510 | if (ida_pre_get(&server->openowner_id, gfp_flags) == 0) |
| 511 | break; |
| 512 | spin_lock(&clp->cl_lock); |
| 513 | sp = nfs4_insert_state_owner_locked(new); |
| 514 | spin_unlock(&clp->cl_lock); |
| 515 | } while (sp == ERR_PTR(-EAGAIN)); |
Trond Myklebust | d1e284d | 2012-01-17 22:04:24 -0500 | [diff] [blame] | 516 | if (sp != new) |
| 517 | nfs4_free_state_owner(new); |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 518 | out: |
| 519 | nfs4_gc_state_owners(server); |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 520 | return sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 523 | /** |
| 524 | * nfs4_put_state_owner - Release a nfs4_state_owner |
| 525 | * @sp: state owner data to release |
Trond Myklebust | 7bf97bc | 2012-04-21 12:36:19 -0400 | [diff] [blame] | 526 | * |
| 527 | * Note that we keep released state owners on an LRU |
| 528 | * list. |
| 529 | * This caches valid state owners so that they can be |
| 530 | * reused, to avoid the OPEN_CONFIRM on minor version 0. |
| 531 | * It also pins the uniquifier of dropped state owners for |
| 532 | * a while, to ensure that those state owner names are |
| 533 | * never reused. |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 534 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | void nfs4_put_state_owner(struct nfs4_state_owner *sp) |
| 536 | { |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 537 | struct nfs_server *server = sp->so_server; |
| 538 | struct nfs_client *clp = server->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
| 540 | if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock)) |
| 541 | return; |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 542 | |
Trond Myklebust | 7bf97bc | 2012-04-21 12:36:19 -0400 | [diff] [blame] | 543 | sp->so_expires = jiffies; |
| 544 | list_add_tail(&sp->so_lru, &server->state_owners_lru); |
| 545 | spin_unlock(&clp->cl_lock); |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /** |
| 549 | * nfs4_purge_state_owners - Release all cached state owners |
| 550 | * @server: nfs_server with cached state owners to release |
| 551 | * |
| 552 | * Called at umount time. Remaining state owners will be on |
| 553 | * the LRU with ref count of zero. |
| 554 | */ |
| 555 | void nfs4_purge_state_owners(struct nfs_server *server) |
| 556 | { |
| 557 | struct nfs_client *clp = server->nfs_client; |
| 558 | struct nfs4_state_owner *sp, *tmp; |
| 559 | LIST_HEAD(doomed); |
| 560 | |
| 561 | spin_lock(&clp->cl_lock); |
| 562 | list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) { |
| 563 | list_move(&sp->so_lru, &doomed); |
| 564 | nfs4_remove_state_owner_locked(sp); |
| 565 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | spin_unlock(&clp->cl_lock); |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 567 | |
| 568 | list_for_each_entry_safe(sp, tmp, &doomed, so_lru) { |
| 569 | list_del(&sp->so_lru); |
| 570 | nfs4_free_state_owner(sp); |
| 571 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | static struct nfs4_state * |
| 575 | nfs4_alloc_open_state(void) |
| 576 | { |
| 577 | struct nfs4_state *state; |
| 578 | |
Trond Myklebust | 8535b2b | 2010-05-13 12:51:01 -0400 | [diff] [blame] | 579 | state = kzalloc(sizeof(*state), GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | if (!state) |
| 581 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | atomic_set(&state->count, 1); |
| 583 | INIT_LIST_HEAD(&state->lock_states); |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 584 | spin_lock_init(&state->state_lock); |
Trond Myklebust | 8bda4e4 | 2007-07-09 10:45:42 -0400 | [diff] [blame] | 585 | seqlock_init(&state->seqlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | return state; |
| 587 | } |
| 588 | |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 589 | void |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 590 | nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode) |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 591 | { |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 592 | if (state->state == fmode) |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 593 | return; |
| 594 | /* NB! List reordering - see the reclaim code for why. */ |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 595 | if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) { |
| 596 | if (fmode & FMODE_WRITE) |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 597 | list_move(&state->open_states, &state->owner->so_states); |
| 598 | else |
| 599 | list_move_tail(&state->open_states, &state->owner->so_states); |
| 600 | } |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 601 | state->state = fmode; |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 602 | } |
| 603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | static struct nfs4_state * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner) |
| 606 | { |
| 607 | struct nfs_inode *nfsi = NFS_I(inode); |
| 608 | struct nfs4_state *state; |
| 609 | |
| 610 | list_for_each_entry(state, &nfsi->open_states, inode_states) { |
Trond Myklebust | 1c816ef | 2007-07-03 14:41:19 -0400 | [diff] [blame] | 611 | if (state->owner != owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | continue; |
Trond Myklebust | 1c816ef | 2007-07-03 14:41:19 -0400 | [diff] [blame] | 613 | if (atomic_inc_not_zero(&state->count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | return state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
| 616 | return NULL; |
| 617 | } |
| 618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | static void |
| 620 | nfs4_free_open_state(struct nfs4_state *state) |
| 621 | { |
| 622 | kfree(state); |
| 623 | } |
| 624 | |
| 625 | struct nfs4_state * |
| 626 | nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner) |
| 627 | { |
| 628 | struct nfs4_state *state, *new; |
| 629 | struct nfs_inode *nfsi = NFS_I(inode); |
| 630 | |
| 631 | spin_lock(&inode->i_lock); |
| 632 | state = __nfs4_find_state_byowner(inode, owner); |
| 633 | spin_unlock(&inode->i_lock); |
| 634 | if (state) |
| 635 | goto out; |
| 636 | new = nfs4_alloc_open_state(); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 637 | spin_lock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | spin_lock(&inode->i_lock); |
| 639 | state = __nfs4_find_state_byowner(inode, owner); |
| 640 | if (state == NULL && new != NULL) { |
| 641 | state = new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | state->owner = owner; |
| 643 | atomic_inc(&owner->so_count); |
| 644 | list_add(&state->inode_states, &nfsi->open_states); |
Dave Chinner | 0444d76 | 2011-03-29 18:08:50 +1100 | [diff] [blame] | 645 | ihold(inode); |
| 646 | state->inode = inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | spin_unlock(&inode->i_lock); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 648 | /* Note: The reclaim code dictates that we add stateless |
| 649 | * and read-only stateids to the end of the list */ |
| 650 | list_add_tail(&state->open_states, &owner->so_states); |
| 651 | spin_unlock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | } else { |
| 653 | spin_unlock(&inode->i_lock); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 654 | spin_unlock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | if (new) |
| 656 | nfs4_free_open_state(new); |
| 657 | } |
| 658 | out: |
| 659 | return state; |
| 660 | } |
| 661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | void nfs4_put_open_state(struct nfs4_state *state) |
| 663 | { |
| 664 | struct inode *inode = state->inode; |
| 665 | struct nfs4_state_owner *owner = state->owner; |
| 666 | |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 667 | if (!atomic_dec_and_lock(&state->count, &owner->so_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | return; |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 669 | spin_lock(&inode->i_lock); |
Trond Myklebust | ba683031 | 2007-07-27 10:23:05 -0400 | [diff] [blame] | 670 | list_del(&state->inode_states); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | list_del(&state->open_states); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 672 | spin_unlock(&inode->i_lock); |
| 673 | spin_unlock(&owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | iput(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | nfs4_free_open_state(state); |
| 676 | nfs4_put_state_owner(owner); |
| 677 | } |
| 678 | |
| 679 | /* |
Trond Myklebust | 83c9d41 | 2005-10-18 14:20:13 -0700 | [diff] [blame] | 680 | * Close the current file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | */ |
Al Viro | 643168c | 2011-06-22 18:20:23 -0400 | [diff] [blame] | 682 | static void __nfs4_close(struct nfs4_state *state, |
Trond Myklebust | 8535b2b | 2010-05-13 12:51:01 -0400 | [diff] [blame] | 683 | fmode_t fmode, gfp_t gfp_mask, int wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | struct nfs4_state_owner *owner = state->owner; |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 686 | int call_close = 0; |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 687 | fmode_t newstate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
| 689 | atomic_inc(&owner->so_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | /* Protect against nfs4_find_state() */ |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 691 | spin_lock(&owner->so_lock); |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 692 | switch (fmode & (FMODE_READ | FMODE_WRITE)) { |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 693 | case FMODE_READ: |
| 694 | state->n_rdonly--; |
| 695 | break; |
| 696 | case FMODE_WRITE: |
| 697 | state->n_wronly--; |
| 698 | break; |
| 699 | case FMODE_READ|FMODE_WRITE: |
| 700 | state->n_rdwr--; |
| 701 | } |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 702 | newstate = FMODE_READ|FMODE_WRITE; |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 703 | if (state->n_rdwr == 0) { |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 704 | if (state->n_rdonly == 0) { |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 705 | newstate &= ~FMODE_READ; |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 706 | call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags); |
| 707 | call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags); |
| 708 | } |
| 709 | if (state->n_wronly == 0) { |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 710 | newstate &= ~FMODE_WRITE; |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 711 | call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags); |
| 712 | call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags); |
| 713 | } |
| 714 | if (newstate == 0) |
| 715 | clear_bit(NFS_DELEGATED_STATE, &state->flags); |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 716 | } |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 717 | nfs4_state_set_mode_locked(state, newstate); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 718 | spin_unlock(&owner->so_lock); |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 719 | |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 720 | if (!call_close) { |
Trond Myklebust | b39e625 | 2007-06-11 23:05:07 -0400 | [diff] [blame] | 721 | nfs4_put_open_state(state); |
| 722 | nfs4_put_state_owner(owner); |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 723 | } else { |
| 724 | bool roc = pnfs_roc(state->inode); |
| 725 | |
Al Viro | 643168c | 2011-06-22 18:20:23 -0400 | [diff] [blame] | 726 | nfs4_do_close(state, gfp_mask, wait, roc); |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 727 | } |
Trond Myklebust | a49c3c7 | 2007-10-18 18:03:27 -0400 | [diff] [blame] | 728 | } |
| 729 | |
Al Viro | 643168c | 2011-06-22 18:20:23 -0400 | [diff] [blame] | 730 | void nfs4_close_state(struct nfs4_state *state, fmode_t fmode) |
Trond Myklebust | a49c3c7 | 2007-10-18 18:03:27 -0400 | [diff] [blame] | 731 | { |
Al Viro | 643168c | 2011-06-22 18:20:23 -0400 | [diff] [blame] | 732 | __nfs4_close(state, fmode, GFP_NOFS, 0); |
Trond Myklebust | a49c3c7 | 2007-10-18 18:03:27 -0400 | [diff] [blame] | 733 | } |
| 734 | |
Al Viro | 643168c | 2011-06-22 18:20:23 -0400 | [diff] [blame] | 735 | void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode) |
Trond Myklebust | a49c3c7 | 2007-10-18 18:03:27 -0400 | [diff] [blame] | 736 | { |
Al Viro | 643168c | 2011-06-22 18:20:23 -0400 | [diff] [blame] | 737 | __nfs4_close(state, fmode, GFP_KERNEL, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Search the state->lock_states for an existing lock_owner |
| 742 | * that is compatible with current->files |
| 743 | */ |
| 744 | static struct nfs4_lock_state * |
Trond Myklebust | 77041ed | 2010-07-01 12:49:11 -0400 | [diff] [blame] | 745 | __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | { |
| 747 | struct nfs4_lock_state *pos; |
| 748 | list_for_each_entry(pos, &state->lock_states, ls_locks) { |
Trond Myklebust | 77041ed | 2010-07-01 12:49:11 -0400 | [diff] [blame] | 749 | if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | continue; |
Trond Myklebust | 77041ed | 2010-07-01 12:49:11 -0400 | [diff] [blame] | 751 | switch (pos->ls_owner.lo_type) { |
| 752 | case NFS4_POSIX_LOCK_TYPE: |
| 753 | if (pos->ls_owner.lo_u.posix_owner != fl_owner) |
| 754 | continue; |
| 755 | break; |
| 756 | case NFS4_FLOCK_LOCK_TYPE: |
| 757 | if (pos->ls_owner.lo_u.flock_owner != fl_pid) |
| 758 | continue; |
| 759 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | atomic_inc(&pos->ls_count); |
| 761 | return pos; |
| 762 | } |
| 763 | return NULL; |
| 764 | } |
| 765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | /* |
| 767 | * Return a compatible lock_state. If no initialized lock_state structure |
| 768 | * exists, return an uninitialized one. |
| 769 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | */ |
Trond Myklebust | 77041ed | 2010-07-01 12:49:11 -0400 | [diff] [blame] | 771 | static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | { |
| 773 | struct nfs4_lock_state *lsp; |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 774 | struct nfs_server *server = state->owner->so_server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
Trond Myklebust | 8535b2b | 2010-05-13 12:51:01 -0400 | [diff] [blame] | 776 | lsp = kzalloc(sizeof(*lsp), GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | if (lsp == NULL) |
| 778 | return NULL; |
Trond Myklebust | 7ba127a | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 779 | nfs4_init_seqid_counter(&lsp->ls_seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | atomic_set(&lsp->ls_count, 1); |
Trond Myklebust | b64aec8 | 2009-07-21 16:47:46 -0400 | [diff] [blame] | 781 | lsp->ls_state = state; |
Trond Myklebust | 77041ed | 2010-07-01 12:49:11 -0400 | [diff] [blame] | 782 | lsp->ls_owner.lo_type = type; |
| 783 | switch (lsp->ls_owner.lo_type) { |
| 784 | case NFS4_FLOCK_LOCK_TYPE: |
| 785 | lsp->ls_owner.lo_u.flock_owner = fl_pid; |
| 786 | break; |
| 787 | case NFS4_POSIX_LOCK_TYPE: |
| 788 | lsp->ls_owner.lo_u.posix_owner = fl_owner; |
| 789 | break; |
| 790 | default: |
Trond Myklebust | d2d7ce2 | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 791 | goto out_free; |
Trond Myklebust | 77041ed | 2010-07-01 12:49:11 -0400 | [diff] [blame] | 792 | } |
Trond Myklebust | 48c22eb | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 793 | lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS); |
| 794 | if (lsp->ls_seqid.owner_id < 0) |
Trond Myklebust | d2d7ce2 | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 795 | goto out_free; |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 796 | INIT_LIST_HEAD(&lsp->ls_locks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | return lsp; |
Trond Myklebust | d2d7ce2 | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 798 | out_free: |
| 799 | kfree(lsp); |
| 800 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Trond Myklebust | 5ae67c4 | 2012-03-19 16:17:18 -0400 | [diff] [blame] | 803 | void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp) |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 804 | { |
Trond Myklebust | 48c22eb | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 805 | ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id); |
Trond Myklebust | 7ba127a | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 806 | nfs4_destroy_seqid_counter(&lsp->ls_seqid); |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 807 | kfree(lsp); |
| 808 | } |
| 809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | /* |
| 811 | * Return a compatible lock_state. If no initialized lock_state structure |
| 812 | * exists, return an uninitialized one. |
| 813 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | */ |
Trond Myklebust | 77041ed | 2010-07-01 12:49:11 -0400 | [diff] [blame] | 815 | static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | { |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 817 | struct nfs4_lock_state *lsp, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 819 | for(;;) { |
| 820 | spin_lock(&state->state_lock); |
Trond Myklebust | 77041ed | 2010-07-01 12:49:11 -0400 | [diff] [blame] | 821 | lsp = __nfs4_find_lock_state(state, owner, pid, type); |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 822 | if (lsp != NULL) |
| 823 | break; |
| 824 | if (new != NULL) { |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 825 | list_add(&new->ls_locks, &state->lock_states); |
| 826 | set_bit(LK_STATE_IN_USE, &state->flags); |
| 827 | lsp = new; |
| 828 | new = NULL; |
| 829 | break; |
| 830 | } |
| 831 | spin_unlock(&state->state_lock); |
Trond Myklebust | 77041ed | 2010-07-01 12:49:11 -0400 | [diff] [blame] | 832 | new = nfs4_alloc_lock_state(state, owner, pid, type); |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 833 | if (new == NULL) |
| 834 | return NULL; |
| 835 | } |
| 836 | spin_unlock(&state->state_lock); |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 837 | if (new != NULL) |
Trond Myklebust | 5ae67c4 | 2012-03-19 16:17:18 -0400 | [diff] [blame] | 838 | nfs4_free_lock_state(state->owner->so_server, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | return lsp; |
| 840 | } |
| 841 | |
| 842 | /* |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 843 | * Release reference to lock_state, and free it if we see that |
| 844 | * it is no longer in use |
| 845 | */ |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 846 | void nfs4_put_lock_state(struct nfs4_lock_state *lsp) |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 847 | { |
| 848 | struct nfs4_state *state; |
| 849 | |
| 850 | if (lsp == NULL) |
| 851 | return; |
| 852 | state = lsp->ls_state; |
| 853 | if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock)) |
| 854 | return; |
| 855 | list_del(&lsp->ls_locks); |
| 856 | if (list_empty(&state->lock_states)) |
| 857 | clear_bit(LK_STATE_IN_USE, &state->flags); |
| 858 | spin_unlock(&state->state_lock); |
Trond Myklebust | cf470c3 | 2012-03-07 13:49:12 -0500 | [diff] [blame] | 859 | if (lsp->ls_flags & NFS_LOCK_INITIALIZED) { |
| 860 | if (nfs4_release_lockowner(lsp) == 0) |
| 861 | return; |
| 862 | } |
Trond Myklebust | 5ae67c4 | 2012-03-19 16:17:18 -0400 | [diff] [blame] | 863 | nfs4_free_lock_state(lsp->ls_state->owner->so_server, lsp); |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src) |
| 867 | { |
| 868 | struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner; |
| 869 | |
| 870 | dst->fl_u.nfs4_fl.owner = lsp; |
| 871 | atomic_inc(&lsp->ls_count); |
| 872 | } |
| 873 | |
| 874 | static void nfs4_fl_release_lock(struct file_lock *fl) |
| 875 | { |
| 876 | nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner); |
| 877 | } |
| 878 | |
Alexey Dobriyan | 6aed628 | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 879 | static const struct file_lock_operations nfs4_fl_lock_ops = { |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 880 | .fl_copy_lock = nfs4_fl_copy_lock, |
| 881 | .fl_release_private = nfs4_fl_release_lock, |
| 882 | }; |
| 883 | |
| 884 | int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl) |
| 885 | { |
| 886 | struct nfs4_lock_state *lsp; |
| 887 | |
| 888 | if (fl->fl_ops != NULL) |
| 889 | return 0; |
Trond Myklebust | 77041ed | 2010-07-01 12:49:11 -0400 | [diff] [blame] | 890 | if (fl->fl_flags & FL_POSIX) |
| 891 | lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE); |
| 892 | else if (fl->fl_flags & FL_FLOCK) |
Trond Myklebust | 1728017 | 2012-03-11 13:11:00 -0400 | [diff] [blame] | 893 | lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid, |
| 894 | NFS4_FLOCK_LOCK_TYPE); |
Trond Myklebust | 77041ed | 2010-07-01 12:49:11 -0400 | [diff] [blame] | 895 | else |
| 896 | return -EINVAL; |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 897 | if (lsp == NULL) |
| 898 | return -ENOMEM; |
| 899 | fl->fl_u.nfs4_fl.owner = lsp; |
| 900 | fl->fl_ops = &nfs4_fl_lock_ops; |
| 901 | return 0; |
| 902 | } |
| 903 | |
Trond Myklebust | 4fc8796 | 2012-03-08 17:42:01 -0500 | [diff] [blame] | 904 | static bool nfs4_copy_lock_stateid(nfs4_stateid *dst, struct nfs4_state *state, |
| 905 | fl_owner_t fl_owner, pid_t fl_pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | { |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 907 | struct nfs4_lock_state *lsp; |
Trond Myklebust | 4fc8796 | 2012-03-08 17:42:01 -0500 | [diff] [blame] | 908 | bool ret = false; |
| 909 | |
| 910 | if (test_bit(LK_STATE_IN_USE, &state->flags) == 0) |
| 911 | goto out; |
| 912 | |
| 913 | spin_lock(&state->state_lock); |
| 914 | lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE); |
| 915 | if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0) { |
| 916 | nfs4_stateid_copy(dst, &lsp->ls_stateid); |
| 917 | ret = true; |
| 918 | } |
| 919 | spin_unlock(&state->state_lock); |
| 920 | nfs4_put_lock_state(lsp); |
| 921 | out: |
| 922 | return ret; |
| 923 | } |
| 924 | |
| 925 | static void nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state) |
| 926 | { |
Trond Myklebust | 8bda4e4 | 2007-07-09 10:45:42 -0400 | [diff] [blame] | 927 | int seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | |
Trond Myklebust | 8bda4e4 | 2007-07-09 10:45:42 -0400 | [diff] [blame] | 929 | do { |
| 930 | seq = read_seqbegin(&state->seqlock); |
Trond Myklebust | f597c53 | 2012-03-04 18:13:56 -0500 | [diff] [blame] | 931 | nfs4_stateid_copy(dst, &state->stateid); |
Trond Myklebust | 8bda4e4 | 2007-07-09 10:45:42 -0400 | [diff] [blame] | 932 | } while (read_seqretry(&state->seqlock, seq)); |
Trond Myklebust | 4fc8796 | 2012-03-08 17:42:01 -0500 | [diff] [blame] | 933 | } |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 934 | |
Trond Myklebust | 4fc8796 | 2012-03-08 17:42:01 -0500 | [diff] [blame] | 935 | /* |
| 936 | * Byte-range lock aware utility to initialize the stateid of read/write |
| 937 | * requests. |
| 938 | */ |
| 939 | void nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state, |
| 940 | fmode_t fmode, fl_owner_t fl_owner, pid_t fl_pid) |
| 941 | { |
| 942 | if (nfs4_copy_delegation_stateid(dst, state->inode, fmode)) |
| 943 | return; |
| 944 | if (nfs4_copy_lock_stateid(dst, state, fl_owner, fl_pid)) |
| 945 | return; |
| 946 | nfs4_copy_open_stateid(dst, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Trond Myklebust | 8535b2b | 2010-05-13 12:51:01 -0400 | [diff] [blame] | 949 | struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | { |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 951 | struct nfs_seqid *new; |
| 952 | |
Trond Myklebust | 8535b2b | 2010-05-13 12:51:01 -0400 | [diff] [blame] | 953 | new = kmalloc(sizeof(*new), gfp_mask); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 954 | if (new != NULL) { |
| 955 | new->sequence = counter; |
Trond Myklebust | 2f74c0a | 2008-01-08 17:56:07 -0500 | [diff] [blame] | 956 | INIT_LIST_HEAD(&new->list); |
Trond Myklebust | 4601df2 | 2012-01-20 18:47:05 -0500 | [diff] [blame] | 957 | new->task = NULL; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 958 | } |
| 959 | return new; |
| 960 | } |
| 961 | |
Trond Myklebust | 72211db | 2009-12-15 14:47:36 -0500 | [diff] [blame] | 962 | void nfs_release_seqid(struct nfs_seqid *seqid) |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 963 | { |
Trond Myklebust | 4601df2 | 2012-01-20 18:47:05 -0500 | [diff] [blame] | 964 | struct nfs_seqid_counter *sequence; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 965 | |
Trond Myklebust | 4601df2 | 2012-01-20 18:47:05 -0500 | [diff] [blame] | 966 | if (list_empty(&seqid->list)) |
| 967 | return; |
| 968 | sequence = seqid->sequence; |
| 969 | spin_lock(&sequence->lock); |
| 970 | list_del_init(&seqid->list); |
| 971 | if (!list_empty(&sequence->list)) { |
| 972 | struct nfs_seqid *next; |
| 973 | |
| 974 | next = list_first_entry(&sequence->list, |
| 975 | struct nfs_seqid, list); |
| 976 | rpc_wake_up_queued_task(&sequence->wait, next->task); |
Trond Myklebust | 2f74c0a | 2008-01-08 17:56:07 -0500 | [diff] [blame] | 977 | } |
Trond Myklebust | 4601df2 | 2012-01-20 18:47:05 -0500 | [diff] [blame] | 978 | spin_unlock(&sequence->lock); |
Trond Myklebust | 72211db | 2009-12-15 14:47:36 -0500 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | void nfs_free_seqid(struct nfs_seqid *seqid) |
| 982 | { |
| 983 | nfs_release_seqid(seqid); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 984 | kfree(seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | /* |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 988 | * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or |
| 989 | * failed with a seqid incrementing error - |
| 990 | * see comments nfs_fs.h:seqid_mutating_error() |
| 991 | */ |
Trond Myklebust | 88d9093 | 2007-07-02 14:03:03 -0400 | [diff] [blame] | 992 | static void nfs_increment_seqid(int status, struct nfs_seqid *seqid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | { |
Trond Myklebust | 7ba127a | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 994 | BUG_ON(list_first_entry(&seqid->sequence->list, struct nfs_seqid, list) != seqid); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 995 | switch (status) { |
| 996 | case 0: |
| 997 | break; |
| 998 | case -NFS4ERR_BAD_SEQID: |
Trond Myklebust | 6f43ddc | 2007-07-08 16:49:11 -0400 | [diff] [blame] | 999 | if (seqid->sequence->flags & NFS_SEQID_CONFIRMED) |
| 1000 | return; |
Trond Myklebust | 9a3ba43 | 2012-03-12 18:01:48 -0400 | [diff] [blame] | 1001 | pr_warn_ratelimited("NFS: v4 server returned a bad" |
Dan Muntz | 497799e | 2008-02-13 13:09:35 -0800 | [diff] [blame] | 1002 | " sequence-id error on an" |
| 1003 | " unconfirmed sequence %p!\n", |
Trond Myklebust | 6f43ddc | 2007-07-08 16:49:11 -0400 | [diff] [blame] | 1004 | seqid->sequence); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1005 | case -NFS4ERR_STALE_CLIENTID: |
| 1006 | case -NFS4ERR_STALE_STATEID: |
| 1007 | case -NFS4ERR_BAD_STATEID: |
| 1008 | case -NFS4ERR_BADXDR: |
| 1009 | case -NFS4ERR_RESOURCE: |
| 1010 | case -NFS4ERR_NOFILEHANDLE: |
| 1011 | /* Non-seqid mutating errors */ |
| 1012 | return; |
| 1013 | }; |
| 1014 | /* |
| 1015 | * Note: no locking needed as we are guaranteed to be first |
| 1016 | * on the sequence list |
| 1017 | */ |
| 1018 | seqid->sequence->counter++; |
| 1019 | } |
| 1020 | |
| 1021 | void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid) |
| 1022 | { |
Benny Halevy | 34dc1ad | 2009-04-01 09:22:52 -0400 | [diff] [blame] | 1023 | struct nfs4_state_owner *sp = container_of(seqid->sequence, |
| 1024 | struct nfs4_state_owner, so_seqid); |
| 1025 | struct nfs_server *server = sp->so_server; |
| 1026 | |
| 1027 | if (status == -NFS4ERR_BAD_SEQID) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | nfs4_drop_state_owner(sp); |
Benny Halevy | 34dc1ad | 2009-04-01 09:22:52 -0400 | [diff] [blame] | 1029 | if (!nfs4_has_session(server->nfs_client)) |
| 1030 | nfs_increment_seqid(status, seqid); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | /* |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1034 | * Increment the seqid if the LOCK/LOCKU succeeded, or |
| 1035 | * failed with a seqid incrementing error - |
| 1036 | * see comments nfs_fs.h:seqid_mutating_error() |
| 1037 | */ |
| 1038 | void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid) |
| 1039 | { |
Trond Myklebust | 88d9093 | 2007-07-02 14:03:03 -0400 | [diff] [blame] | 1040 | nfs_increment_seqid(status, seqid); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task) |
| 1044 | { |
Trond Myklebust | 7ba127a | 2012-01-17 22:04:25 -0500 | [diff] [blame] | 1045 | struct nfs_seqid_counter *sequence = seqid->sequence; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1046 | int status = 0; |
| 1047 | |
| 1048 | spin_lock(&sequence->lock); |
Trond Myklebust | 4601df2 | 2012-01-20 18:47:05 -0500 | [diff] [blame] | 1049 | seqid->task = task; |
Trond Myklebust | 2f74c0a | 2008-01-08 17:56:07 -0500 | [diff] [blame] | 1050 | if (list_empty(&seqid->list)) |
| 1051 | list_add_tail(&seqid->list, &sequence->list); |
| 1052 | if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid) |
| 1053 | goto unlock; |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 1054 | rpc_sleep_on(&sequence->wait, task, NULL); |
Trond Myklebust | 2f74c0a | 2008-01-08 17:56:07 -0500 | [diff] [blame] | 1055 | status = -EAGAIN; |
| 1056 | unlock: |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1057 | spin_unlock(&sequence->lock); |
| 1058 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Trond Myklebust | e005e80 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 1061 | static int nfs4_run_state_manager(void *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
Trond Myklebust | e005e80 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 1063 | static void nfs4_clear_state_manager_bit(struct nfs_client *clp) |
Trond Myklebust | 433fbe4 | 2006-01-03 09:55:22 +0100 | [diff] [blame] | 1064 | { |
| 1065 | smp_mb__before_clear_bit(); |
Trond Myklebust | e005e80 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 1066 | clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state); |
Trond Myklebust | 433fbe4 | 2006-01-03 09:55:22 +0100 | [diff] [blame] | 1067 | smp_mb__after_clear_bit(); |
Trond Myklebust | e005e80 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 1068 | wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING); |
Trond Myklebust | 433fbe4 | 2006-01-03 09:55:22 +0100 | [diff] [blame] | 1069 | rpc_wake_up(&clp->cl_rpcwaitq); |
| 1070 | } |
| 1071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | /* |
Trond Myklebust | e005e80 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 1073 | * Schedule the nfs_client asynchronous state management routine |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | */ |
Trond Myklebust | b0d3ded | 2008-12-23 15:21:50 -0500 | [diff] [blame] | 1075 | void nfs4_schedule_state_manager(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | { |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame] | 1077 | struct task_struct *task; |
Trond Myklebust | 2446ab6 | 2012-03-01 17:00:56 -0500 | [diff] [blame] | 1078 | char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
Trond Myklebust | e005e80 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 1080 | if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0) |
| 1081 | return; |
Trond Myklebust | 5043e90 | 2006-01-03 09:55:23 +0100 | [diff] [blame] | 1082 | __module_get(THIS_MODULE); |
| 1083 | atomic_inc(&clp->cl_count); |
Trond Myklebust | 2446ab6 | 2012-03-01 17:00:56 -0500 | [diff] [blame] | 1084 | |
| 1085 | /* The rcu_read_lock() is not strictly necessary, as the state |
| 1086 | * manager is the only thread that ever changes the rpc_xprt |
| 1087 | * after it's initialized. At this point, we're single threaded. */ |
| 1088 | rcu_read_lock(); |
| 1089 | snprintf(buf, sizeof(buf), "%s-manager", |
| 1090 | rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)); |
| 1091 | rcu_read_unlock(); |
| 1092 | task = kthread_run(nfs4_run_state_manager, clp, buf); |
| 1093 | if (IS_ERR(task)) { |
| 1094 | printk(KERN_ERR "%s: kthread_run: %ld\n", |
| 1095 | __func__, PTR_ERR(task)); |
| 1096 | nfs4_clear_state_manager_bit(clp); |
| 1097 | nfs_put_client(clp); |
| 1098 | module_put(THIS_MODULE); |
| 1099 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /* |
Trond Myklebust | 0400a6b | 2011-03-09 16:00:53 -0500 | [diff] [blame] | 1103 | * Schedule a lease recovery attempt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | */ |
Trond Myklebust | 0400a6b | 2011-03-09 16:00:53 -0500 | [diff] [blame] | 1105 | void nfs4_schedule_lease_recovery(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | { |
| 1107 | if (!clp) |
| 1108 | return; |
Trond Myklebust | e598d84 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1109 | if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) |
| 1110 | set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state); |
Trond Myklebust | e005e80 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 1111 | nfs4_schedule_state_manager(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | } |
Andy Adamson | 9cb8196 | 2012-03-07 10:49:41 -0500 | [diff] [blame] | 1113 | EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
Trond Myklebust | ad1e396 | 2012-03-10 11:23:15 -0500 | [diff] [blame] | 1115 | /* |
| 1116 | * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN |
| 1117 | * @clp: client to process |
| 1118 | * |
| 1119 | * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a |
| 1120 | * resend of the SETCLIENTID and hence re-establish the |
| 1121 | * callback channel. Then return all existing delegations. |
| 1122 | */ |
| 1123 | static void nfs40_handle_cb_pathdown(struct nfs_client *clp) |
| 1124 | { |
| 1125 | set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state); |
| 1126 | nfs_expire_all_delegations(clp); |
| 1127 | } |
| 1128 | |
Trond Myklebust | 042b60b | 2011-08-24 15:07:37 -0400 | [diff] [blame] | 1129 | void nfs4_schedule_path_down_recovery(struct nfs_client *clp) |
| 1130 | { |
Trond Myklebust | ad1e396 | 2012-03-10 11:23:15 -0500 | [diff] [blame] | 1131 | nfs40_handle_cb_pathdown(clp); |
Trond Myklebust | 042b60b | 2011-08-24 15:07:37 -0400 | [diff] [blame] | 1132 | nfs4_schedule_state_manager(clp); |
| 1133 | } |
| 1134 | |
Trond Myklebust | f9feab1 | 2011-03-09 16:12:46 -0500 | [diff] [blame] | 1135 | static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state) |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1136 | { |
| 1137 | |
| 1138 | set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags); |
| 1139 | /* Don't recover state that expired before the reboot */ |
| 1140 | if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) { |
| 1141 | clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags); |
| 1142 | return 0; |
| 1143 | } |
Trond Myklebust | 7eff03a | 2008-12-23 15:21:43 -0500 | [diff] [blame] | 1144 | set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags); |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1145 | set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state); |
| 1146 | return 1; |
| 1147 | } |
| 1148 | |
Trond Myklebust | f9feab1 | 2011-03-09 16:12:46 -0500 | [diff] [blame] | 1149 | static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state) |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1150 | { |
| 1151 | set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags); |
| 1152 | clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags); |
Trond Myklebust | 7eff03a | 2008-12-23 15:21:43 -0500 | [diff] [blame] | 1153 | set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags); |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1154 | set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state); |
| 1155 | return 1; |
| 1156 | } |
| 1157 | |
Trond Myklebust | 0400a6b | 2011-03-09 16:00:53 -0500 | [diff] [blame] | 1158 | void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state) |
| 1159 | { |
| 1160 | struct nfs_client *clp = server->nfs_client; |
| 1161 | |
| 1162 | nfs4_state_mark_reclaim_nograce(clp, state); |
| 1163 | nfs4_schedule_state_manager(clp); |
| 1164 | } |
Andy Adamson | 9cb8196 | 2012-03-07 10:49:41 -0500 | [diff] [blame] | 1165 | EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery); |
Trond Myklebust | 0400a6b | 2011-03-09 16:00:53 -0500 | [diff] [blame] | 1166 | |
Trond Myklebust | a1d0b5e | 2012-03-05 19:56:44 -0500 | [diff] [blame] | 1167 | void nfs_inode_find_state_and_recover(struct inode *inode, |
| 1168 | const nfs4_stateid *stateid) |
| 1169 | { |
| 1170 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
| 1171 | struct nfs_inode *nfsi = NFS_I(inode); |
| 1172 | struct nfs_open_context *ctx; |
| 1173 | struct nfs4_state *state; |
| 1174 | bool found = false; |
| 1175 | |
| 1176 | spin_lock(&inode->i_lock); |
| 1177 | list_for_each_entry(ctx, &nfsi->open_files, list) { |
| 1178 | state = ctx->state; |
| 1179 | if (state == NULL) |
| 1180 | continue; |
| 1181 | if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) |
| 1182 | continue; |
Trond Myklebust | f597c53 | 2012-03-04 18:13:56 -0500 | [diff] [blame] | 1183 | if (!nfs4_stateid_match(&state->stateid, stateid)) |
Trond Myklebust | a1d0b5e | 2012-03-05 19:56:44 -0500 | [diff] [blame] | 1184 | continue; |
| 1185 | nfs4_state_mark_reclaim_nograce(clp, state); |
| 1186 | found = true; |
| 1187 | } |
| 1188 | spin_unlock(&inode->i_lock); |
| 1189 | if (found) |
| 1190 | nfs4_schedule_state_manager(clp); |
| 1191 | } |
| 1192 | |
| 1193 | |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1194 | static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | { |
| 1196 | struct inode *inode = state->inode; |
Trond Myklebust | 19e03c5 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 1197 | struct nfs_inode *nfsi = NFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | struct file_lock *fl; |
| 1199 | int status = 0; |
| 1200 | |
Trond Myklebust | 3f09df7 | 2009-06-17 13:23:00 -0700 | [diff] [blame] | 1201 | if (inode->i_flock == NULL) |
| 1202 | return 0; |
| 1203 | |
| 1204 | /* Guard against delegation returns and new lock/unlock calls */ |
Trond Myklebust | 19e03c5 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 1205 | down_write(&nfsi->rwsem); |
Trond Myklebust | 3f09df7 | 2009-06-17 13:23:00 -0700 | [diff] [blame] | 1206 | /* Protect inode->i_flock using the BKL */ |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 1207 | lock_flocks(); |
Harvey Harrison | 90dc7d2 | 2008-02-20 13:03:05 -0800 | [diff] [blame] | 1208 | for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { |
Trond Myklebust | 43b2a33 | 2005-11-04 15:35:30 -0500 | [diff] [blame] | 1209 | if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | continue; |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 1211 | if (nfs_file_open_context(fl->fl_file)->state != state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | continue; |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 1213 | unlock_flocks(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | status = ops->recover_lock(state, fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | switch (status) { |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 1216 | case 0: |
| 1217 | break; |
| 1218 | case -ESTALE: |
| 1219 | case -NFS4ERR_ADMIN_REVOKED: |
| 1220 | case -NFS4ERR_STALE_STATEID: |
| 1221 | case -NFS4ERR_BAD_STATEID: |
| 1222 | case -NFS4ERR_EXPIRED: |
| 1223 | case -NFS4ERR_NO_GRACE: |
| 1224 | case -NFS4ERR_STALE_CLIENTID: |
Trond Myklebust | 9c4c761 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 1225 | case -NFS4ERR_BADSESSION: |
| 1226 | case -NFS4ERR_BADSLOT: |
| 1227 | case -NFS4ERR_BAD_HIGH_SLOT: |
| 1228 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 1229 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | default: |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 1231 | printk(KERN_ERR "NFS: %s: unhandled error %d. " |
| 1232 | "Zeroing state\n", __func__, status); |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 1233 | case -ENOMEM: |
| 1234 | case -NFS4ERR_DENIED: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | case -NFS4ERR_RECLAIM_BAD: |
| 1236 | case -NFS4ERR_RECLAIM_CONFLICT: |
Trond Myklebust | 43b2a33 | 2005-11-04 15:35:30 -0500 | [diff] [blame] | 1237 | /* kill_proc(fl->fl_pid, SIGLOST, 1); */ |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 1238 | status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 1240 | lock_flocks(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | } |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 1242 | unlock_flocks(); |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 1243 | out: |
Trond Myklebust | 19e03c5 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 1244 | up_write(&nfsi->rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | return status; |
| 1246 | } |
| 1247 | |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1248 | static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | { |
| 1250 | struct nfs4_state *state; |
| 1251 | struct nfs4_lock_state *lock; |
| 1252 | int status = 0; |
| 1253 | |
| 1254 | /* Note: we rely on the sp->so_states list being ordered |
| 1255 | * so that we always reclaim open(O_RDWR) and/or open(O_WRITE) |
| 1256 | * states first. |
| 1257 | * This is needed to ensure that the server won't give us any |
| 1258 | * read delegations that we have to return if, say, we are |
| 1259 | * recovering after a network partition or a reboot from a |
| 1260 | * server that doesn't support a grace period. |
| 1261 | */ |
Trond Myklebust | fe1d819 | 2008-12-23 15:21:43 -0500 | [diff] [blame] | 1262 | restart: |
| 1263 | spin_lock(&sp->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | list_for_each_entry(state, &sp->so_states, open_states) { |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1265 | if (!test_and_clear_bit(ops->state_flag_bit, &state->flags)) |
| 1266 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | if (state->state == 0) |
| 1268 | continue; |
Trond Myklebust | fe1d819 | 2008-12-23 15:21:43 -0500 | [diff] [blame] | 1269 | atomic_inc(&state->count); |
| 1270 | spin_unlock(&sp->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | status = ops->recover_open(sp, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | if (status >= 0) { |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1273 | status = nfs4_reclaim_locks(state, ops); |
| 1274 | if (status >= 0) { |
Trond Myklebust | 4b44b40 | 2011-12-09 16:31:52 -0500 | [diff] [blame] | 1275 | spin_lock(&state->state_lock); |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1276 | list_for_each_entry(lock, &state->lock_states, ls_locks) { |
| 1277 | if (!(lock->ls_flags & NFS_LOCK_INITIALIZED)) |
William Dauchy | 96dcadc | 2012-03-14 12:32:04 +0100 | [diff] [blame] | 1278 | pr_warn_ratelimited("NFS: " |
| 1279 | "%s: Lock reclaim " |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 1280 | "failed!\n", __func__); |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1281 | } |
Trond Myklebust | 4b44b40 | 2011-12-09 16:31:52 -0500 | [diff] [blame] | 1282 | spin_unlock(&state->state_lock); |
Trond Myklebust | fe1d819 | 2008-12-23 15:21:43 -0500 | [diff] [blame] | 1283 | nfs4_put_open_state(state); |
| 1284 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | } |
| 1287 | switch (status) { |
| 1288 | default: |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 1289 | printk(KERN_ERR "NFS: %s: unhandled error %d. " |
| 1290 | "Zeroing state\n", __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | case -ENOENT: |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 1292 | case -ENOMEM: |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1293 | case -ESTALE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | /* |
| 1295 | * Open state on this file cannot be recovered |
| 1296 | * All we can do is revert to using the zero stateid. |
| 1297 | */ |
Trond Myklebust | 2d2f24a | 2012-03-04 18:13:57 -0500 | [diff] [blame] | 1298 | memset(&state->stateid, 0, |
| 1299 | sizeof(state->stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | /* Mark the file as being 'closed' */ |
| 1301 | state->state = 0; |
| 1302 | break; |
Trond Myklebust | 168667c | 2010-10-19 19:47:49 -0400 | [diff] [blame] | 1303 | case -EKEYEXPIRED: |
| 1304 | /* |
| 1305 | * User RPCSEC_GSS context has expired. |
| 1306 | * We cannot recover this stateid now, so |
| 1307 | * skip it and allow recovery thread to |
| 1308 | * proceed. |
| 1309 | */ |
| 1310 | break; |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 1311 | case -NFS4ERR_ADMIN_REVOKED: |
| 1312 | case -NFS4ERR_STALE_STATEID: |
| 1313 | case -NFS4ERR_BAD_STATEID: |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1314 | case -NFS4ERR_RECLAIM_BAD: |
| 1315 | case -NFS4ERR_RECLAIM_CONFLICT: |
Trond Myklebust | 1f0e890 | 2010-06-24 15:11:43 -0400 | [diff] [blame] | 1316 | nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state); |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1317 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | case -NFS4ERR_EXPIRED: |
| 1319 | case -NFS4ERR_NO_GRACE: |
Trond Myklebust | 1f0e890 | 2010-06-24 15:11:43 -0400 | [diff] [blame] | 1320 | nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | case -NFS4ERR_STALE_CLIENTID: |
Trond Myklebust | 9c4c761 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 1322 | case -NFS4ERR_BADSESSION: |
| 1323 | case -NFS4ERR_BADSLOT: |
| 1324 | case -NFS4ERR_BAD_HIGH_SLOT: |
| 1325 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | goto out_err; |
| 1327 | } |
Trond Myklebust | fe1d819 | 2008-12-23 15:21:43 -0500 | [diff] [blame] | 1328 | nfs4_put_open_state(state); |
| 1329 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | } |
Trond Myklebust | fe1d819 | 2008-12-23 15:21:43 -0500 | [diff] [blame] | 1331 | spin_unlock(&sp->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | return 0; |
| 1333 | out_err: |
Trond Myklebust | fe1d819 | 2008-12-23 15:21:43 -0500 | [diff] [blame] | 1334 | nfs4_put_open_state(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | return status; |
| 1336 | } |
| 1337 | |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1338 | static void nfs4_clear_open_state(struct nfs4_state *state) |
| 1339 | { |
| 1340 | struct nfs4_lock_state *lock; |
| 1341 | |
| 1342 | clear_bit(NFS_DELEGATED_STATE, &state->flags); |
| 1343 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); |
| 1344 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); |
| 1345 | clear_bit(NFS_O_RDWR_STATE, &state->flags); |
Trond Myklebust | 4b44b40 | 2011-12-09 16:31:52 -0500 | [diff] [blame] | 1346 | spin_lock(&state->state_lock); |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1347 | list_for_each_entry(lock, &state->lock_states, ls_locks) { |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1348 | lock->ls_seqid.flags = 0; |
| 1349 | lock->ls_flags &= ~NFS_LOCK_INITIALIZED; |
| 1350 | } |
Trond Myklebust | 4b44b40 | 2011-12-09 16:31:52 -0500 | [diff] [blame] | 1351 | spin_unlock(&state->state_lock); |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1352 | } |
| 1353 | |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1354 | static void nfs4_reset_seqids(struct nfs_server *server, |
| 1355 | int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state)) |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1356 | { |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1357 | struct nfs_client *clp = server->nfs_client; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1358 | struct nfs4_state_owner *sp; |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 1359 | struct rb_node *pos; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1360 | struct nfs4_state *state; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1361 | |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1362 | spin_lock(&clp->cl_lock); |
| 1363 | for (pos = rb_first(&server->state_owners); |
| 1364 | pos != NULL; |
| 1365 | pos = rb_next(pos)) { |
| 1366 | sp = rb_entry(pos, struct nfs4_state_owner, so_server_node); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1367 | sp->so_seqid.flags = 0; |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 1368 | spin_lock(&sp->so_lock); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1369 | list_for_each_entry(state, &sp->so_states, open_states) { |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1370 | if (mark_reclaim(clp, state)) |
| 1371 | nfs4_clear_open_state(state); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1372 | } |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 1373 | spin_unlock(&sp->so_lock); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1374 | } |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1375 | spin_unlock(&clp->cl_lock); |
| 1376 | } |
| 1377 | |
| 1378 | static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp, |
| 1379 | int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state)) |
| 1380 | { |
| 1381 | struct nfs_server *server; |
| 1382 | |
| 1383 | rcu_read_lock(); |
| 1384 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) |
| 1385 | nfs4_reset_seqids(server, mark_reclaim); |
| 1386 | rcu_read_unlock(); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1389 | static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp) |
| 1390 | { |
| 1391 | /* Mark all delegations for reclaim */ |
| 1392 | nfs_delegation_mark_reclaim(clp); |
| 1393 | nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot); |
| 1394 | } |
| 1395 | |
Ricardo Labiaga | fce5c83 | 2009-12-05 16:08:41 -0500 | [diff] [blame] | 1396 | static void nfs4_reclaim_complete(struct nfs_client *clp, |
| 1397 | const struct nfs4_state_recovery_ops *ops) |
| 1398 | { |
| 1399 | /* Notify the server we're done reclaiming our state */ |
| 1400 | if (ops->reclaim_complete) |
| 1401 | (void)ops->reclaim_complete(clp); |
| 1402 | } |
| 1403 | |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1404 | static void nfs4_clear_reclaim_server(struct nfs_server *server) |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1405 | { |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1406 | struct nfs_client *clp = server->nfs_client; |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1407 | struct nfs4_state_owner *sp; |
| 1408 | struct rb_node *pos; |
| 1409 | struct nfs4_state *state; |
| 1410 | |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1411 | spin_lock(&clp->cl_lock); |
| 1412 | for (pos = rb_first(&server->state_owners); |
| 1413 | pos != NULL; |
| 1414 | pos = rb_next(pos)) { |
| 1415 | sp = rb_entry(pos, struct nfs4_state_owner, so_server_node); |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1416 | spin_lock(&sp->so_lock); |
| 1417 | list_for_each_entry(state, &sp->so_states, open_states) { |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1418 | if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT, |
| 1419 | &state->flags)) |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1420 | continue; |
| 1421 | nfs4_state_mark_reclaim_nograce(clp, state); |
| 1422 | } |
| 1423 | spin_unlock(&sp->so_lock); |
| 1424 | } |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1425 | spin_unlock(&clp->cl_lock); |
| 1426 | } |
| 1427 | |
| 1428 | static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp) |
| 1429 | { |
| 1430 | struct nfs_server *server; |
| 1431 | |
| 1432 | if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) |
| 1433 | return 0; |
| 1434 | |
| 1435 | rcu_read_lock(); |
| 1436 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) |
| 1437 | nfs4_clear_reclaim_server(server); |
| 1438 | rcu_read_unlock(); |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1439 | |
| 1440 | nfs_delegation_reap_unclaimed(clp); |
Trond Myklebust | 6eaa614 | 2010-10-04 17:59:08 -0400 | [diff] [blame] | 1441 | return 1; |
| 1442 | } |
| 1443 | |
| 1444 | static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp) |
| 1445 | { |
| 1446 | if (!nfs4_state_clear_reclaim_reboot(clp)) |
| 1447 | return; |
| 1448 | nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops); |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | static void nfs_delegation_clear_all(struct nfs_client *clp) |
| 1452 | { |
| 1453 | nfs_delegation_mark_reclaim(clp); |
| 1454 | nfs_delegation_reap_unclaimed(clp); |
| 1455 | } |
| 1456 | |
| 1457 | static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp) |
| 1458 | { |
| 1459 | nfs_delegation_clear_all(clp); |
| 1460 | nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce); |
| 1461 | } |
| 1462 | |
Trond Myklebust | 168667c | 2010-10-19 19:47:49 -0400 | [diff] [blame] | 1463 | static void nfs4_warn_keyexpired(const char *s) |
| 1464 | { |
| 1465 | printk_ratelimited(KERN_WARNING "Error: state manager" |
| 1466 | " encountered RPCSEC_GSS session" |
| 1467 | " expired against NFSv4 server %s.\n", |
| 1468 | s); |
| 1469 | } |
| 1470 | |
Trond Myklebust | 4f7cdf1 | 2009-12-03 15:53:20 -0500 | [diff] [blame] | 1471 | static int nfs4_recovery_handle_error(struct nfs_client *clp, int error) |
Trond Myklebust | e598d84 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1472 | { |
| 1473 | switch (error) { |
Trond Myklebust | 4f38e4a | 2011-12-01 16:31:34 -0500 | [diff] [blame] | 1474 | case 0: |
| 1475 | break; |
Trond Myklebust | e598d84 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1476 | case -NFS4ERR_CB_PATH_DOWN: |
Trond Myklebust | ad1e396 | 2012-03-10 11:23:15 -0500 | [diff] [blame] | 1477 | nfs40_handle_cb_pathdown(clp); |
Trond Myklebust | 4f38e4a | 2011-12-01 16:31:34 -0500 | [diff] [blame] | 1478 | break; |
Trond Myklebust | c8b7ae3d | 2009-12-03 15:53:21 -0500 | [diff] [blame] | 1479 | case -NFS4ERR_NO_GRACE: |
| 1480 | nfs4_state_end_reclaim_reboot(clp); |
Trond Myklebust | 4f38e4a | 2011-12-01 16:31:34 -0500 | [diff] [blame] | 1481 | break; |
Trond Myklebust | e598d84 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1482 | case -NFS4ERR_STALE_CLIENTID: |
| 1483 | case -NFS4ERR_LEASE_MOVED: |
| 1484 | set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state); |
Trond Myklebust | 6eaa614 | 2010-10-04 17:59:08 -0400 | [diff] [blame] | 1485 | nfs4_state_clear_reclaim_reboot(clp); |
Trond Myklebust | e598d84 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1486 | nfs4_state_start_reclaim_reboot(clp); |
| 1487 | break; |
| 1488 | case -NFS4ERR_EXPIRED: |
| 1489 | set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state); |
| 1490 | nfs4_state_start_reclaim_nograce(clp); |
Andy Adamson | 8ba9bf8 | 2009-12-04 15:55:34 -0500 | [diff] [blame] | 1491 | break; |
Andy Adamson | c3fad1b | 2009-04-01 09:22:39 -0400 | [diff] [blame] | 1492 | case -NFS4ERR_BADSESSION: |
| 1493 | case -NFS4ERR_BADSLOT: |
| 1494 | case -NFS4ERR_BAD_HIGH_SLOT: |
| 1495 | case -NFS4ERR_DEADSESSION: |
| 1496 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: |
| 1497 | case -NFS4ERR_SEQ_FALSE_RETRY: |
| 1498 | case -NFS4ERR_SEQ_MISORDERED: |
Andy Adamson | 6df0818 | 2009-12-04 15:55:05 -0500 | [diff] [blame] | 1499 | set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state); |
Andy Adamson | 0b9e2d4 | 2009-12-04 16:02:14 -0500 | [diff] [blame] | 1500 | /* Zero session reset errors */ |
Trond Myklebust | 4f38e4a | 2011-12-01 16:31:34 -0500 | [diff] [blame] | 1501 | break; |
Trond Myklebust | 168667c | 2010-10-19 19:47:49 -0400 | [diff] [blame] | 1502 | case -EKEYEXPIRED: |
| 1503 | /* Nothing we can do */ |
| 1504 | nfs4_warn_keyexpired(clp->cl_hostname); |
Trond Myklebust | 4f38e4a | 2011-12-01 16:31:34 -0500 | [diff] [blame] | 1505 | break; |
| 1506 | default: |
| 1507 | return error; |
Trond Myklebust | e598d84 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1508 | } |
Trond Myklebust | 4f38e4a | 2011-12-01 16:31:34 -0500 | [diff] [blame] | 1509 | return 0; |
Trond Myklebust | e598d84 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1510 | } |
| 1511 | |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1512 | static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | { |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1514 | struct nfs4_state_owner *sp; |
| 1515 | struct nfs_server *server; |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 1516 | struct rb_node *pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | int status = 0; |
| 1518 | |
Trond Myklebust | 7eff03a | 2008-12-23 15:21:43 -0500 | [diff] [blame] | 1519 | restart: |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1520 | rcu_read_lock(); |
| 1521 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { |
Chuck Lever | 0aaaf5c | 2011-12-06 16:13:48 -0500 | [diff] [blame] | 1522 | nfs4_purge_state_owners(server); |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1523 | spin_lock(&clp->cl_lock); |
| 1524 | for (pos = rb_first(&server->state_owners); |
| 1525 | pos != NULL; |
| 1526 | pos = rb_next(pos)) { |
| 1527 | sp = rb_entry(pos, |
| 1528 | struct nfs4_state_owner, so_server_node); |
| 1529 | if (!test_and_clear_bit(ops->owner_flag_bit, |
| 1530 | &sp->so_flags)) |
| 1531 | continue; |
| 1532 | atomic_inc(&sp->so_count); |
| 1533 | spin_unlock(&clp->cl_lock); |
| 1534 | rcu_read_unlock(); |
| 1535 | |
| 1536 | status = nfs4_reclaim_open_state(sp, ops); |
| 1537 | if (status < 0) { |
| 1538 | set_bit(ops->owner_flag_bit, &sp->so_flags); |
| 1539 | nfs4_put_state_owner(sp); |
| 1540 | return nfs4_recovery_handle_error(clp, status); |
| 1541 | } |
| 1542 | |
Trond Myklebust | 7eff03a | 2008-12-23 15:21:43 -0500 | [diff] [blame] | 1543 | nfs4_put_state_owner(sp); |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1544 | goto restart; |
Trond Myklebust | 7eff03a | 2008-12-23 15:21:43 -0500 | [diff] [blame] | 1545 | } |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1546 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1547 | } |
Chuck Lever | 24d292b | 2010-12-24 01:32:43 +0000 | [diff] [blame] | 1548 | rcu_read_unlock(); |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1549 | return status; |
| 1550 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1552 | static int nfs4_check_lease(struct nfs_client *clp) |
| 1553 | { |
| 1554 | struct rpc_cred *cred; |
Trond Myklebust | c48f4f3 | 2010-06-16 09:52:27 -0400 | [diff] [blame] | 1555 | const struct nfs4_state_maintenance_ops *ops = |
| 1556 | clp->cl_mvops->state_renewal_ops; |
Trond Myklebust | 4f38e4a | 2011-12-01 16:31:34 -0500 | [diff] [blame] | 1557 | int status; |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1558 | |
Trond Myklebust | 0f605b5 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1559 | /* Is the client already known to have an expired lease? */ |
| 1560 | if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) |
| 1561 | return 0; |
Andy Adamson | a7b7210 | 2009-04-01 09:22:46 -0400 | [diff] [blame] | 1562 | spin_lock(&clp->cl_lock); |
| 1563 | cred = ops->get_state_renewal_cred_locked(clp); |
| 1564 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 0f605b5 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1565 | if (cred == NULL) { |
| 1566 | cred = nfs4_get_setclientid_cred(clp); |
Trond Myklebust | 4f38e4a | 2011-12-01 16:31:34 -0500 | [diff] [blame] | 1567 | status = -ENOKEY; |
Trond Myklebust | 0f605b5 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1568 | if (cred == NULL) |
| 1569 | goto out; |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1570 | } |
Benny Halevy | 8e69514f | 2009-04-01 09:22:45 -0400 | [diff] [blame] | 1571 | status = ops->renew_lease(clp, cred); |
Trond Myklebust | 0f605b5 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1572 | put_rpccred(cred); |
| 1573 | out: |
Trond Myklebust | 4f7cdf1 | 2009-12-03 15:53:20 -0500 | [diff] [blame] | 1574 | return nfs4_recovery_handle_error(clp, status); |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1575 | } |
| 1576 | |
Trond Myklebust | 2a6ee6a | 2012-05-25 15:00:06 -0400 | [diff] [blame] | 1577 | /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors |
| 1578 | * on EXCHANGE_ID for v4.1 |
| 1579 | */ |
| 1580 | static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status) |
| 1581 | { |
| 1582 | switch (status) { |
| 1583 | case -NFS4ERR_CLID_INUSE: |
| 1584 | case -NFS4ERR_STALE_CLIENTID: |
| 1585 | clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state); |
| 1586 | break; |
| 1587 | case -EACCES: |
| 1588 | if (clp->cl_machine_cred == NULL) |
| 1589 | return -EACCES; |
| 1590 | /* Handle case where the user hasn't set up machine creds */ |
| 1591 | nfs4_clear_machine_cred(clp); |
| 1592 | case -NFS4ERR_DELAY: |
| 1593 | case -ETIMEDOUT: |
| 1594 | case -EAGAIN: |
| 1595 | ssleep(1); |
| 1596 | break; |
| 1597 | |
| 1598 | case -NFS4ERR_MINOR_VERS_MISMATCH: |
| 1599 | if (clp->cl_cons_state == NFS_CS_SESSION_INITING) |
| 1600 | nfs_mark_client_ready(clp, -EPROTONOSUPPORT); |
| 1601 | return -EPROTONOSUPPORT; |
| 1602 | case -EKEYEXPIRED: |
| 1603 | nfs4_warn_keyexpired(clp->cl_hostname); |
| 1604 | case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery |
| 1605 | * in nfs4_exchange_id */ |
| 1606 | default: |
| 1607 | return status; |
| 1608 | } |
| 1609 | set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state); |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1613 | static int nfs4_reclaim_lease(struct nfs_client *clp) |
| 1614 | { |
| 1615 | struct rpc_cred *cred; |
Trond Myklebust | c48f4f3 | 2010-06-16 09:52:27 -0400 | [diff] [blame] | 1616 | const struct nfs4_state_recovery_ops *ops = |
| 1617 | clp->cl_mvops->reboot_recovery_ops; |
Trond Myklebust | 2a6ee6a | 2012-05-25 15:00:06 -0400 | [diff] [blame] | 1618 | int status; |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1619 | |
Andy Adamson | 90a1661 | 2009-04-01 09:22:48 -0400 | [diff] [blame] | 1620 | cred = ops->get_clid_cred(clp); |
Trond Myklebust | 2a6ee6a | 2012-05-25 15:00:06 -0400 | [diff] [blame] | 1621 | if (cred == NULL) |
| 1622 | return -ENOENT; |
| 1623 | status = ops->establish_clid(clp, cred); |
| 1624 | put_rpccred(cred); |
| 1625 | if (status != 0) |
| 1626 | return nfs4_handle_reclaim_lease_error(clp, status); |
| 1627 | return 0; |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1628 | } |
| 1629 | |
Andy Adamson | 76db6d9 | 2009-04-01 09:22:38 -0400 | [diff] [blame] | 1630 | #ifdef CONFIG_NFS_V4_1 |
Trond Myklebust | 0400a6b | 2011-03-09 16:00:53 -0500 | [diff] [blame] | 1631 | void nfs4_schedule_session_recovery(struct nfs4_session *session) |
| 1632 | { |
Trond Myklebust | 444f72f | 2011-05-26 14:26:35 -0400 | [diff] [blame] | 1633 | struct nfs_client *clp = session->clp; |
| 1634 | |
| 1635 | set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state); |
| 1636 | nfs4_schedule_lease_recovery(clp); |
Trond Myklebust | 0400a6b | 2011-03-09 16:00:53 -0500 | [diff] [blame] | 1637 | } |
Andy Adamson | cbdabc7 | 2011-03-01 01:34:20 +0000 | [diff] [blame] | 1638 | EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery); |
Trond Myklebust | 0400a6b | 2011-03-09 16:00:53 -0500 | [diff] [blame] | 1639 | |
Andy Adamson | b9efa1b | 2010-01-20 16:06:27 -0500 | [diff] [blame] | 1640 | void nfs41_handle_recall_slot(struct nfs_client *clp) |
| 1641 | { |
| 1642 | set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state); |
Trond Myklebust | 0400a6b | 2011-03-09 16:00:53 -0500 | [diff] [blame] | 1643 | nfs4_schedule_state_manager(clp); |
Andy Adamson | b9efa1b | 2010-01-20 16:06:27 -0500 | [diff] [blame] | 1644 | } |
| 1645 | |
Trond Myklebust | 0f79fd6 | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 1646 | static void nfs4_reset_all_state(struct nfs_client *clp) |
| 1647 | { |
| 1648 | if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) { |
Chuck Lever | 2c820d9 | 2012-05-21 22:45:33 -0400 | [diff] [blame] | 1649 | set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state); |
Trond Myklebust | be0bfed | 2012-05-25 16:02:15 -0400 | [diff] [blame^] | 1650 | clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state); |
Trond Myklebust | 0f79fd6 | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 1651 | nfs4_state_start_reclaim_nograce(clp); |
Trond Myklebust | 0400a6b | 2011-03-09 16:00:53 -0500 | [diff] [blame] | 1652 | nfs4_schedule_state_manager(clp); |
Trond Myklebust | 0f79fd6 | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | static void nfs41_handle_server_reboot(struct nfs_client *clp) |
| 1657 | { |
| 1658 | if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) { |
| 1659 | nfs4_state_start_reclaim_reboot(clp); |
Trond Myklebust | 0400a6b | 2011-03-09 16:00:53 -0500 | [diff] [blame] | 1660 | nfs4_schedule_state_manager(clp); |
Trond Myklebust | 0f79fd6 | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | static void nfs41_handle_state_revoked(struct nfs_client *clp) |
| 1665 | { |
Trond Myklebust | 0f79fd6 | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 1666 | nfs4_reset_all_state(clp); |
| 1667 | } |
| 1668 | |
| 1669 | static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp) |
| 1670 | { |
| 1671 | /* This will need to handle layouts too */ |
| 1672 | nfs_expire_all_delegations(clp); |
| 1673 | } |
| 1674 | |
Weston Andros Adamson | a9e6444 | 2012-05-24 12:26:37 -0400 | [diff] [blame] | 1675 | static void nfs41_handle_backchannel_fault(struct nfs_client *clp) |
Trond Myklebust | 0f79fd6 | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 1676 | { |
| 1677 | nfs_expire_all_delegations(clp); |
| 1678 | if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0) |
Trond Myklebust | 0400a6b | 2011-03-09 16:00:53 -0500 | [diff] [blame] | 1679 | nfs4_schedule_state_manager(clp); |
Trond Myklebust | 0f79fd6 | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 1680 | } |
| 1681 | |
Weston Andros Adamson | a9e6444 | 2012-05-24 12:26:37 -0400 | [diff] [blame] | 1682 | static void nfs41_handle_cb_path_down(struct nfs_client *clp) |
| 1683 | { |
| 1684 | if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, |
| 1685 | &clp->cl_state) == 0) |
| 1686 | nfs4_schedule_state_manager(clp); |
| 1687 | } |
| 1688 | |
Alexandros Batsakis | 0629e37 | 2009-12-05 13:46:14 -0500 | [diff] [blame] | 1689 | void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags) |
| 1690 | { |
| 1691 | if (!flags) |
| 1692 | return; |
Chuck Lever | 2c820d9 | 2012-05-21 22:45:33 -0400 | [diff] [blame] | 1693 | |
| 1694 | dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n", |
| 1695 | __func__, clp->cl_hostname, clp->cl_clientid, flags); |
| 1696 | |
Trond Myklebust | 111d489 | 2011-12-01 16:37:42 -0500 | [diff] [blame] | 1697 | if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED) |
Trond Myklebust | 0f79fd6 | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 1698 | nfs41_handle_server_reboot(clp); |
Trond Myklebust | 111d489 | 2011-12-01 16:37:42 -0500 | [diff] [blame] | 1699 | if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED | |
Alexandros Batsakis | 0629e37 | 2009-12-05 13:46:14 -0500 | [diff] [blame] | 1700 | SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED | |
| 1701 | SEQ4_STATUS_ADMIN_STATE_REVOKED | |
Trond Myklebust | 0f79fd6 | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 1702 | SEQ4_STATUS_LEASE_MOVED)) |
| 1703 | nfs41_handle_state_revoked(clp); |
Trond Myklebust | 111d489 | 2011-12-01 16:37:42 -0500 | [diff] [blame] | 1704 | if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED) |
Trond Myklebust | 0f79fd6 | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 1705 | nfs41_handle_recallable_state_revoked(clp); |
Weston Andros Adamson | a9e6444 | 2012-05-24 12:26:37 -0400 | [diff] [blame] | 1706 | if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT) |
| 1707 | nfs41_handle_backchannel_fault(clp); |
| 1708 | else if (flags & (SEQ4_STATUS_CB_PATH_DOWN | |
| 1709 | SEQ4_STATUS_CB_PATH_DOWN_SESSION)) |
Trond Myklebust | 0f79fd6 | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 1710 | nfs41_handle_cb_path_down(clp); |
Alexandros Batsakis | 0629e37 | 2009-12-05 13:46:14 -0500 | [diff] [blame] | 1711 | } |
| 1712 | |
Andy Adamson | c3fad1b | 2009-04-01 09:22:39 -0400 | [diff] [blame] | 1713 | static int nfs4_reset_session(struct nfs_client *clp) |
| 1714 | { |
| 1715 | int status; |
| 1716 | |
Trond Myklebust | 3804541 | 2009-12-15 17:36:57 -0500 | [diff] [blame] | 1717 | nfs4_begin_drain_session(clp); |
Andy Adamson | c3fad1b | 2009-04-01 09:22:39 -0400 | [diff] [blame] | 1718 | status = nfs4_proc_destroy_session(clp->cl_session); |
| 1719 | if (status && status != -NFS4ERR_BADSESSION && |
| 1720 | status != -NFS4ERR_DEADSESSION) { |
Ricardo Labiaga | f455848 | 2009-12-07 09:16:09 -0500 | [diff] [blame] | 1721 | status = nfs4_recovery_handle_error(clp, status); |
Andy Adamson | c3fad1b | 2009-04-01 09:22:39 -0400 | [diff] [blame] | 1722 | goto out; |
| 1723 | } |
| 1724 | |
| 1725 | memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN); |
Trond Myklebust | f26468f | 2009-12-05 19:32:11 -0500 | [diff] [blame] | 1726 | status = nfs4_proc_create_session(clp); |
Andy Adamson | 41f54a5 | 2010-01-21 14:54:13 -0500 | [diff] [blame] | 1727 | if (status) { |
Ricardo Labiaga | f455848 | 2009-12-07 09:16:09 -0500 | [diff] [blame] | 1728 | status = nfs4_recovery_handle_error(clp, status); |
Andy Adamson | 41f54a5 | 2010-01-21 14:54:13 -0500 | [diff] [blame] | 1729 | goto out; |
| 1730 | } |
Trond Myklebust | 444f72f | 2011-05-26 14:26:35 -0400 | [diff] [blame] | 1731 | clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state); |
Andy Adamson | 41f54a5 | 2010-01-21 14:54:13 -0500 | [diff] [blame] | 1732 | /* create_session negotiated new slot table */ |
| 1733 | clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state); |
Weston Andros Adamson | a9e6444 | 2012-05-24 12:26:37 -0400 | [diff] [blame] | 1734 | clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state); |
Ricardo Labiaga | 9dfdf40 | 2009-12-06 12:57:34 -0500 | [diff] [blame] | 1735 | |
Andy Adamson | 41f54a5 | 2010-01-21 14:54:13 -0500 | [diff] [blame] | 1736 | /* Let the state manager reestablish state */ |
| 1737 | if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) |
Alexandros Batsakis | 5601a00 | 2009-12-14 21:27:58 -0800 | [diff] [blame] | 1738 | nfs41_setup_state_renewal(clp); |
Andy Adamson | 41f54a5 | 2010-01-21 14:54:13 -0500 | [diff] [blame] | 1739 | out: |
Andy Adamson | c3fad1b | 2009-04-01 09:22:39 -0400 | [diff] [blame] | 1740 | return status; |
| 1741 | } |
Andy Adamson | 76db6d9 | 2009-04-01 09:22:38 -0400 | [diff] [blame] | 1742 | |
Andy Adamson | b9efa1b | 2010-01-20 16:06:27 -0500 | [diff] [blame] | 1743 | static int nfs4_recall_slot(struct nfs_client *clp) |
| 1744 | { |
| 1745 | struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table; |
| 1746 | struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs; |
| 1747 | struct nfs4_slot *new, *old; |
| 1748 | int i; |
| 1749 | |
| 1750 | nfs4_begin_drain_session(clp); |
| 1751 | new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot), |
Trond Myklebust | 8535b2b | 2010-05-13 12:51:01 -0400 | [diff] [blame] | 1752 | GFP_NOFS); |
Andy Adamson | b9efa1b | 2010-01-20 16:06:27 -0500 | [diff] [blame] | 1753 | if (!new) |
| 1754 | return -ENOMEM; |
| 1755 | |
| 1756 | spin_lock(&fc_tbl->slot_tbl_lock); |
| 1757 | for (i = 0; i < fc_tbl->target_max_slots; i++) |
| 1758 | new[i].seq_nr = fc_tbl->slots[i].seq_nr; |
| 1759 | old = fc_tbl->slots; |
| 1760 | fc_tbl->slots = new; |
| 1761 | fc_tbl->max_slots = fc_tbl->target_max_slots; |
| 1762 | fc_tbl->target_max_slots = 0; |
| 1763 | fc_attrs->max_reqs = fc_tbl->max_slots; |
| 1764 | spin_unlock(&fc_tbl->slot_tbl_lock); |
| 1765 | |
| 1766 | kfree(old); |
| 1767 | nfs4_end_drain_session(clp); |
| 1768 | return 0; |
| 1769 | } |
| 1770 | |
Weston Andros Adamson | a9e6444 | 2012-05-24 12:26:37 -0400 | [diff] [blame] | 1771 | static int nfs4_bind_conn_to_session(struct nfs_client *clp) |
| 1772 | { |
| 1773 | return nfs4_proc_bind_conn_to_session(clp); |
| 1774 | } |
Andy Adamson | 76db6d9 | 2009-04-01 09:22:38 -0400 | [diff] [blame] | 1775 | #else /* CONFIG_NFS_V4_1 */ |
Andy Adamson | c3fad1b | 2009-04-01 09:22:39 -0400 | [diff] [blame] | 1776 | static int nfs4_reset_session(struct nfs_client *clp) { return 0; } |
Alexandros Batsakis | 5601a00 | 2009-12-14 21:27:58 -0800 | [diff] [blame] | 1777 | static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; } |
Andy Adamson | b9efa1b | 2010-01-20 16:06:27 -0500 | [diff] [blame] | 1778 | static int nfs4_recall_slot(struct nfs_client *clp) { return 0; } |
Weston Andros Adamson | a9e6444 | 2012-05-24 12:26:37 -0400 | [diff] [blame] | 1779 | |
| 1780 | static int nfs4_bind_conn_to_session(struct nfs_client *clp) |
| 1781 | { |
| 1782 | return 0; |
| 1783 | } |
Andy Adamson | 76db6d9 | 2009-04-01 09:22:38 -0400 | [diff] [blame] | 1784 | #endif /* CONFIG_NFS_V4_1 */ |
| 1785 | |
Trond Myklebust | e005e80 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 1786 | static void nfs4_state_manager(struct nfs_client *clp) |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1787 | { |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1788 | int status = 0; |
| 1789 | |
Trond Myklebust | 0286001 | 2008-12-23 15:21:40 -0500 | [diff] [blame] | 1790 | /* Ensure exclusive access to NFSv4 state */ |
Trond Myklebust | 47c2199 | 2011-04-15 17:34:18 -0400 | [diff] [blame] | 1791 | do { |
Chuck Lever | 2c820d9 | 2012-05-21 22:45:33 -0400 | [diff] [blame] | 1792 | if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) { |
Trond Myklebust | 2a6ee6a | 2012-05-25 15:00:06 -0400 | [diff] [blame] | 1793 | status = nfs4_reclaim_lease(clp); |
| 1794 | if (status < 0) |
| 1795 | goto out_error; |
Chuck Lever | 2c820d9 | 2012-05-21 22:45:33 -0400 | [diff] [blame] | 1796 | clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state); |
| 1797 | set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state); |
| 1798 | } |
| 1799 | |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1800 | if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) { |
| 1801 | /* We're going to have to re-establish a clientid */ |
| 1802 | status = nfs4_reclaim_lease(clp); |
Trond Myklebust | 2a6ee6a | 2012-05-25 15:00:06 -0400 | [diff] [blame] | 1803 | if (status < 0) |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1804 | goto out_error; |
Trond Myklebust | 2a6ee6a | 2012-05-25 15:00:06 -0400 | [diff] [blame] | 1805 | if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) |
| 1806 | continue; |
Trond Myklebust | e598d84 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1807 | clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state); |
Weston Andros Adamson | 78fe0f4 | 2011-05-31 19:05:47 -0400 | [diff] [blame] | 1808 | |
| 1809 | if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, |
| 1810 | &clp->cl_state)) |
| 1811 | nfs4_state_start_reclaim_nograce(clp); |
| 1812 | else |
| 1813 | set_bit(NFS4CLNT_RECLAIM_REBOOT, |
| 1814 | &clp->cl_state); |
| 1815 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 1816 | pnfs_destroy_all_layouts(clp); |
Trond Myklebust | e598d84 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) { |
| 1820 | status = nfs4_check_lease(clp); |
Trond Myklebust | 4f38e4a | 2011-12-01 16:31:34 -0500 | [diff] [blame] | 1821 | if (status < 0) |
| 1822 | goto out_error; |
Trond Myklebust | b6d408b | 2009-12-03 15:53:22 -0500 | [diff] [blame] | 1823 | if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) |
Trond Myklebust | e598d84 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1824 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | } |
Trond Myklebust | b6d408b | 2009-12-03 15:53:22 -0500 | [diff] [blame] | 1826 | |
Andy Adamson | c3fad1b | 2009-04-01 09:22:39 -0400 | [diff] [blame] | 1827 | /* Initialize or reset the session */ |
Andy Adamson | 6df0818 | 2009-12-04 15:55:05 -0500 | [diff] [blame] | 1828 | if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) |
Trond Myklebust | 7111dc7 | 2009-08-24 19:21:29 -0400 | [diff] [blame] | 1829 | && nfs4_has_session(clp)) { |
Andy Adamson | 4d643d1 | 2009-12-04 15:52:24 -0500 | [diff] [blame] | 1830 | status = nfs4_reset_session(clp); |
Trond Myklebust | b6d408b | 2009-12-03 15:53:22 -0500 | [diff] [blame] | 1831 | if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) |
| 1832 | continue; |
| 1833 | if (status < 0) |
Andy Adamson | 76db6d9 | 2009-04-01 09:22:38 -0400 | [diff] [blame] | 1834 | goto out_error; |
Andy Adamson | 76db6d9 | 2009-04-01 09:22:38 -0400 | [diff] [blame] | 1835 | } |
Trond Myklebust | b6d408b | 2009-12-03 15:53:22 -0500 | [diff] [blame] | 1836 | |
Weston Andros Adamson | a9e6444 | 2012-05-24 12:26:37 -0400 | [diff] [blame] | 1837 | /* Send BIND_CONN_TO_SESSION */ |
| 1838 | if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, |
| 1839 | &clp->cl_state) && nfs4_has_session(clp)) { |
| 1840 | status = nfs4_bind_conn_to_session(clp); |
| 1841 | if (status < 0) |
| 1842 | goto out_error; |
| 1843 | } |
| 1844 | |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1845 | /* First recover reboot state... */ |
Trond Myklebust | e345e88 | 2009-12-03 15:52:41 -0500 | [diff] [blame] | 1846 | if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) { |
Andy Adamson | 591d71c | 2009-04-01 09:22:47 -0400 | [diff] [blame] | 1847 | status = nfs4_do_reclaim(clp, |
Trond Myklebust | c48f4f3 | 2010-06-16 09:52:27 -0400 | [diff] [blame] | 1848 | clp->cl_mvops->reboot_recovery_ops); |
Trond Myklebust | b6d408b | 2009-12-03 15:53:22 -0500 | [diff] [blame] | 1849 | if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) || |
Andy Adamson | 6df0818 | 2009-12-04 15:55:05 -0500 | [diff] [blame] | 1850 | test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) |
Andy Adamson | c3fad1b | 2009-04-01 09:22:39 -0400 | [diff] [blame] | 1851 | continue; |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1852 | nfs4_state_end_reclaim_reboot(clp); |
Trond Myklebust | b6d408b | 2009-12-03 15:53:22 -0500 | [diff] [blame] | 1853 | if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) |
| 1854 | continue; |
| 1855 | if (status < 0) |
| 1856 | goto out_error; |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | /* Now recover expired state... */ |
| 1860 | if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) { |
Andy Adamson | 591d71c | 2009-04-01 09:22:47 -0400 | [diff] [blame] | 1861 | status = nfs4_do_reclaim(clp, |
Trond Myklebust | c48f4f3 | 2010-06-16 09:52:27 -0400 | [diff] [blame] | 1862 | clp->cl_mvops->nograce_recovery_ops); |
Trond Myklebust | b6d408b | 2009-12-03 15:53:22 -0500 | [diff] [blame] | 1863 | if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) || |
Andy Adamson | 6df0818 | 2009-12-04 15:55:05 -0500 | [diff] [blame] | 1864 | test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) || |
Trond Myklebust | b6d408b | 2009-12-03 15:53:22 -0500 | [diff] [blame] | 1865 | test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) |
| 1866 | continue; |
| 1867 | if (status < 0) |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1868 | goto out_error; |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 1869 | } |
Trond Myklebust | 707fb4b | 2008-12-23 15:21:47 -0500 | [diff] [blame] | 1870 | |
Alexandros Batsakis | 5601a00 | 2009-12-14 21:27:58 -0800 | [diff] [blame] | 1871 | nfs4_end_drain_session(clp); |
Trond Myklebust | 707fb4b | 2008-12-23 15:21:47 -0500 | [diff] [blame] | 1872 | if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) { |
| 1873 | nfs_client_return_marked_delegations(clp); |
| 1874 | continue; |
| 1875 | } |
Andy Adamson | b9efa1b | 2010-01-20 16:06:27 -0500 | [diff] [blame] | 1876 | /* Recall session slots */ |
| 1877 | if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state) |
| 1878 | && nfs4_has_session(clp)) { |
| 1879 | status = nfs4_recall_slot(clp); |
| 1880 | if (status < 0) |
| 1881 | goto out_error; |
| 1882 | continue; |
| 1883 | } |
| 1884 | |
Trond Myklebust | e005e80 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 1885 | |
| 1886 | nfs4_clear_state_manager_bit(clp); |
Trond Myklebust | f3c7649 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 1887 | /* Did we race with an attempt to give us more work? */ |
| 1888 | if (clp->cl_state == 0) |
| 1889 | break; |
| 1890 | if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0) |
| 1891 | break; |
Trond Myklebust | 47c2199 | 2011-04-15 17:34:18 -0400 | [diff] [blame] | 1892 | } while (atomic_read(&clp->cl_count) > 1); |
Trond Myklebust | e005e80 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 1893 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | out_error: |
Trond Myklebust | 9a3ba43 | 2012-03-12 18:01:48 -0400 | [diff] [blame] | 1895 | pr_warn_ratelimited("NFS: state manager failed on NFSv4 server %s" |
Chuck Lever | 5d8515c | 2007-12-10 14:57:16 -0500 | [diff] [blame] | 1896 | " with error %d\n", clp->cl_hostname, -status); |
Alexandros Batsakis | 5601a00 | 2009-12-14 21:27:58 -0800 | [diff] [blame] | 1897 | nfs4_end_drain_session(clp); |
Trond Myklebust | e005e80 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 1898 | nfs4_clear_state_manager_bit(clp); |
| 1899 | } |
| 1900 | |
| 1901 | static int nfs4_run_state_manager(void *ptr) |
| 1902 | { |
| 1903 | struct nfs_client *clp = ptr; |
| 1904 | |
| 1905 | allow_signal(SIGKILL); |
| 1906 | nfs4_state_manager(clp); |
| 1907 | nfs_put_client(clp); |
| 1908 | module_put_and_exit(0); |
| 1909 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | /* |
| 1913 | * Local variables: |
| 1914 | * c-basic-offset: 8 |
| 1915 | * End: |
| 1916 | */ |