Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * fs/nfs/nfs4session.c |
| 3 | * |
| 4 | * Copyright (c) 2012 Trond Myklebust <Trond.Myklebust@netapp.com> |
| 5 | * |
| 6 | */ |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/errno.h> |
| 9 | #include <linux/string.h> |
| 10 | #include <linux/printk.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/sunrpc/sched.h> |
| 13 | #include <linux/sunrpc/bc_xprt.h> |
| 14 | #include <linux/nfs.h> |
| 15 | #include <linux/nfs4.h> |
| 16 | #include <linux/nfs_fs.h> |
| 17 | #include <linux/module.h> |
| 18 | |
| 19 | #include "nfs4_fs.h" |
| 20 | #include "internal.h" |
| 21 | #include "nfs4session.h" |
| 22 | #include "callback.h" |
| 23 | |
| 24 | #define NFSDBG_FACILITY NFSDBG_STATE |
| 25 | |
Chuck Lever | 744aa52 | 2013-08-09 12:48:53 -0400 | [diff] [blame] | 26 | static void nfs4_init_slot_table(struct nfs4_slot_table *tbl, const char *queue) |
| 27 | { |
| 28 | tbl->highest_used_slotid = NFS4_NO_SLOT; |
| 29 | spin_lock_init(&tbl->slot_tbl_lock); |
| 30 | rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, queue); |
Trond Myklebust | 045d2a6 | 2016-08-28 13:25:43 -0400 | [diff] [blame] | 31 | init_waitqueue_head(&tbl->slot_waitq); |
Chuck Lever | 744aa52 | 2013-08-09 12:48:53 -0400 | [diff] [blame] | 32 | init_completion(&tbl->complete); |
| 33 | } |
| 34 | |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 35 | /* |
| 36 | * nfs4_shrink_slot_table - free retired slots from the slot table |
| 37 | */ |
| 38 | static void nfs4_shrink_slot_table(struct nfs4_slot_table *tbl, u32 newsize) |
| 39 | { |
| 40 | struct nfs4_slot **p; |
| 41 | if (newsize >= tbl->max_slots) |
| 42 | return; |
| 43 | |
| 44 | p = &tbl->slots; |
| 45 | while (newsize--) |
| 46 | p = &(*p)->next; |
| 47 | while (*p) { |
| 48 | struct nfs4_slot *slot = *p; |
| 49 | |
| 50 | *p = slot->next; |
| 51 | kfree(slot); |
| 52 | tbl->max_slots--; |
| 53 | } |
| 54 | } |
| 55 | |
Chuck Lever | 9d33059 | 2013-08-09 12:48:44 -0400 | [diff] [blame] | 56 | /** |
| 57 | * nfs4_slot_tbl_drain_complete - wake waiters when drain is complete |
Trond Myklebust | 302fad7 | 2019-02-18 13:32:38 -0500 | [diff] [blame] | 58 | * @tbl: controlling slot table |
Chuck Lever | 9d33059 | 2013-08-09 12:48:44 -0400 | [diff] [blame] | 59 | * |
| 60 | */ |
| 61 | void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table *tbl) |
| 62 | { |
| 63 | if (nfs4_slot_tbl_draining(tbl)) |
| 64 | complete(&tbl->complete); |
| 65 | } |
| 66 | |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 67 | /* |
| 68 | * nfs4_free_slot - free a slot and efficiently update slot table. |
| 69 | * |
| 70 | * freeing a slot is trivially done by clearing its respective bit |
| 71 | * in the bitmap. |
| 72 | * If the freed slotid equals highest_used_slotid we want to update it |
| 73 | * so that the server would be able to size down the slot table if needed, |
| 74 | * otherwise we know that the highest_used_slotid is still in use. |
| 75 | * When updating highest_used_slotid there may be "holes" in the bitmap |
| 76 | * so we need to scan down from highest_used_slotid to 0 looking for the now |
| 77 | * highest slotid in use. |
| 78 | * If none found, highest_used_slotid is set to NFS4_NO_SLOT. |
| 79 | * |
| 80 | * Must be called while holding tbl->slot_tbl_lock |
| 81 | */ |
| 82 | void nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot) |
| 83 | { |
| 84 | u32 slotid = slot->slot_nr; |
| 85 | |
| 86 | /* clear used bit in bitmap */ |
| 87 | __clear_bit(slotid, tbl->used_slots); |
| 88 | |
| 89 | /* update highest_used_slotid when it is freed */ |
| 90 | if (slotid == tbl->highest_used_slotid) { |
| 91 | u32 new_max = find_last_bit(tbl->used_slots, slotid); |
| 92 | if (new_max < slotid) |
| 93 | tbl->highest_used_slotid = new_max; |
| 94 | else { |
| 95 | tbl->highest_used_slotid = NFS4_NO_SLOT; |
Andy Adamson | 774d5f1 | 2013-05-20 14:13:50 -0400 | [diff] [blame] | 96 | nfs4_slot_tbl_drain_complete(tbl); |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 97 | } |
| 98 | } |
Chuck Lever | e8d9238 | 2013-08-09 12:47:51 -0400 | [diff] [blame] | 99 | dprintk("%s: slotid %u highest_used_slotid %u\n", __func__, |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 100 | slotid, tbl->highest_used_slotid); |
| 101 | } |
| 102 | |
| 103 | static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl, |
| 104 | u32 slotid, u32 seq_init, gfp_t gfp_mask) |
| 105 | { |
| 106 | struct nfs4_slot *slot; |
| 107 | |
| 108 | slot = kzalloc(sizeof(*slot), gfp_mask); |
| 109 | if (slot) { |
| 110 | slot->table = tbl; |
| 111 | slot->slot_nr = slotid; |
| 112 | slot->seq_nr = seq_init; |
Trond Myklebust | 3453d57 | 2018-06-20 17:53:34 -0400 | [diff] [blame^] | 113 | slot->seq_nr_highest_sent = seq_init; |
| 114 | slot->seq_nr_last_acked = seq_init - 1; |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 115 | } |
| 116 | return slot; |
| 117 | } |
| 118 | |
| 119 | static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl, |
| 120 | u32 slotid, u32 seq_init, gfp_t gfp_mask) |
| 121 | { |
| 122 | struct nfs4_slot **p, *slot; |
| 123 | |
| 124 | p = &tbl->slots; |
| 125 | for (;;) { |
| 126 | if (*p == NULL) { |
| 127 | *p = nfs4_new_slot(tbl, tbl->max_slots, |
| 128 | seq_init, gfp_mask); |
| 129 | if (*p == NULL) |
| 130 | break; |
| 131 | tbl->max_slots++; |
| 132 | } |
| 133 | slot = *p; |
| 134 | if (slot->slot_nr == slotid) |
| 135 | return slot; |
| 136 | p = &slot->next; |
| 137 | } |
| 138 | return ERR_PTR(-ENOMEM); |
| 139 | } |
| 140 | |
Trond Myklebust | 810d82e | 2016-01-23 15:18:18 -0500 | [diff] [blame] | 141 | static void nfs4_lock_slot(struct nfs4_slot_table *tbl, |
| 142 | struct nfs4_slot *slot) |
| 143 | { |
| 144 | u32 slotid = slot->slot_nr; |
| 145 | |
| 146 | __set_bit(slotid, tbl->used_slots); |
| 147 | if (slotid > tbl->highest_used_slotid || |
| 148 | tbl->highest_used_slotid == NFS4_NO_SLOT) |
| 149 | tbl->highest_used_slotid = slotid; |
| 150 | slot->generation = tbl->generation; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * nfs4_try_to_lock_slot - Given a slot try to allocate it |
| 155 | * |
| 156 | * Note: must be called with the slot_tbl_lock held. |
| 157 | */ |
| 158 | bool nfs4_try_to_lock_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot) |
| 159 | { |
| 160 | if (nfs4_test_locked_slot(tbl, slot->slot_nr)) |
| 161 | return false; |
| 162 | nfs4_lock_slot(tbl, slot); |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * nfs4_lookup_slot - Find a slot but don't allocate it |
| 168 | * |
| 169 | * Note: must be called with the slot_tbl_lock held. |
| 170 | */ |
| 171 | struct nfs4_slot *nfs4_lookup_slot(struct nfs4_slot_table *tbl, u32 slotid) |
| 172 | { |
| 173 | if (slotid <= tbl->max_slotid) |
Fred Isaman | 9a83785 | 2016-09-27 15:37:11 -0400 | [diff] [blame] | 174 | return nfs4_find_or_create_slot(tbl, slotid, 0, GFP_NOWAIT); |
Trond Myklebust | 810d82e | 2016-01-23 15:18:18 -0500 | [diff] [blame] | 175 | return ERR_PTR(-E2BIG); |
| 176 | } |
| 177 | |
Trond Myklebust | e09c978 | 2016-08-27 23:44:04 -0400 | [diff] [blame] | 178 | static int nfs4_slot_get_seqid(struct nfs4_slot_table *tbl, u32 slotid, |
| 179 | u32 *seq_nr) |
| 180 | __must_hold(&tbl->slot_tbl_lock) |
| 181 | { |
| 182 | struct nfs4_slot *slot; |
Arnd Bergmann | 68a5640 | 2016-10-18 00:05:35 +0200 | [diff] [blame] | 183 | int ret; |
Trond Myklebust | e09c978 | 2016-08-27 23:44:04 -0400 | [diff] [blame] | 184 | |
| 185 | slot = nfs4_lookup_slot(tbl, slotid); |
Arnd Bergmann | 68a5640 | 2016-10-18 00:05:35 +0200 | [diff] [blame] | 186 | ret = PTR_ERR_OR_ZERO(slot); |
| 187 | if (!ret) |
| 188 | *seq_nr = slot->seq_nr; |
| 189 | |
| 190 | return ret; |
Trond Myklebust | e09c978 | 2016-08-27 23:44:04 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* |
| 194 | * nfs4_slot_seqid_in_use - test if a slot sequence id is still in use |
| 195 | * |
| 196 | * Given a slot table, slot id and sequence number, determine if the |
| 197 | * RPC call in question is still in flight. This function is mainly |
| 198 | * intended for use by the callback channel. |
| 199 | */ |
Trond Myklebust | 045d2a6 | 2016-08-28 13:25:43 -0400 | [diff] [blame] | 200 | static bool nfs4_slot_seqid_in_use(struct nfs4_slot_table *tbl, |
| 201 | u32 slotid, u32 seq_nr) |
Trond Myklebust | e09c978 | 2016-08-27 23:44:04 -0400 | [diff] [blame] | 202 | { |
Shuah Khan | 0ac84b7 | 2016-11-07 10:48:16 -0700 | [diff] [blame] | 203 | u32 cur_seq = 0; |
Trond Myklebust | e09c978 | 2016-08-27 23:44:04 -0400 | [diff] [blame] | 204 | bool ret = false; |
| 205 | |
| 206 | spin_lock(&tbl->slot_tbl_lock); |
| 207 | if (nfs4_slot_get_seqid(tbl, slotid, &cur_seq) == 0 && |
| 208 | cur_seq == seq_nr && test_bit(slotid, tbl->used_slots)) |
| 209 | ret = true; |
| 210 | spin_unlock(&tbl->slot_tbl_lock); |
| 211 | return ret; |
| 212 | } |
| 213 | |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 214 | /* |
Trond Myklebust | 045d2a6 | 2016-08-28 13:25:43 -0400 | [diff] [blame] | 215 | * nfs4_slot_wait_on_seqid - wait until a slot sequence id is complete |
| 216 | * |
| 217 | * Given a slot table, slot id and sequence number, wait until the |
| 218 | * corresponding RPC call completes. This function is mainly |
| 219 | * intended for use by the callback channel. |
| 220 | */ |
| 221 | int nfs4_slot_wait_on_seqid(struct nfs4_slot_table *tbl, |
| 222 | u32 slotid, u32 seq_nr, |
| 223 | unsigned long timeout) |
| 224 | { |
| 225 | if (wait_event_timeout(tbl->slot_waitq, |
| 226 | !nfs4_slot_seqid_in_use(tbl, slotid, seq_nr), |
| 227 | timeout) == 0) |
| 228 | return -ETIMEDOUT; |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | /* |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 233 | * nfs4_alloc_slot - efficiently look for a free slot |
| 234 | * |
| 235 | * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap. |
| 236 | * If found, we mark the slot as used, update the highest_used_slotid, |
| 237 | * and respectively set up the sequence operation args. |
| 238 | * |
| 239 | * Note: must be called with under the slot_tbl_lock. |
| 240 | */ |
| 241 | struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl) |
| 242 | { |
| 243 | struct nfs4_slot *ret = ERR_PTR(-EBUSY); |
| 244 | u32 slotid; |
| 245 | |
| 246 | dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n", |
| 247 | __func__, tbl->used_slots[0], tbl->highest_used_slotid, |
| 248 | tbl->max_slotid + 1); |
| 249 | slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1); |
Trond Myklebust | 810d82e | 2016-01-23 15:18:18 -0500 | [diff] [blame] | 250 | if (slotid <= tbl->max_slotid) { |
| 251 | ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT); |
| 252 | if (!IS_ERR(ret)) |
| 253 | nfs4_lock_slot(tbl, ret); |
| 254 | } |
Chuck Lever | e8d9238 | 2013-08-09 12:47:51 -0400 | [diff] [blame] | 255 | dprintk("<-- %s used_slots=%04lx highest_used=%u slotid=%u\n", |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 256 | __func__, tbl->used_slots[0], tbl->highest_used_slotid, |
Chuck Lever | e8d9238 | 2013-08-09 12:47:51 -0400 | [diff] [blame] | 257 | !IS_ERR(ret) ? ret->slot_nr : NFS4_NO_SLOT); |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl, |
| 262 | u32 max_reqs, u32 ivalue) |
| 263 | { |
| 264 | if (max_reqs <= tbl->max_slots) |
| 265 | return 0; |
| 266 | if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS))) |
| 267 | return 0; |
| 268 | return -ENOMEM; |
| 269 | } |
| 270 | |
| 271 | static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl, |
| 272 | u32 server_highest_slotid, |
| 273 | u32 ivalue) |
| 274 | { |
| 275 | struct nfs4_slot **p; |
| 276 | |
| 277 | nfs4_shrink_slot_table(tbl, server_highest_slotid + 1); |
| 278 | p = &tbl->slots; |
| 279 | while (*p) { |
| 280 | (*p)->seq_nr = ivalue; |
Trond Myklebust | 3453d57 | 2018-06-20 17:53:34 -0400 | [diff] [blame^] | 281 | (*p)->seq_nr_highest_sent = ivalue; |
| 282 | (*p)->seq_nr_last_acked = ivalue - 1; |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 283 | p = &(*p)->next; |
| 284 | } |
| 285 | tbl->highest_used_slotid = NFS4_NO_SLOT; |
| 286 | tbl->target_highest_slotid = server_highest_slotid; |
| 287 | tbl->server_highest_slotid = server_highest_slotid; |
Trond Myklebust | 1fa8064 | 2012-12-02 13:54:59 -0500 | [diff] [blame] | 288 | tbl->d_target_highest_slotid = 0; |
| 289 | tbl->d2_target_highest_slotid = 0; |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 290 | tbl->max_slotid = server_highest_slotid; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * (re)Initialise a slot table |
| 295 | */ |
| 296 | static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, |
| 297 | u32 max_reqs, u32 ivalue) |
| 298 | { |
| 299 | int ret; |
| 300 | |
Chuck Lever | e8d9238 | 2013-08-09 12:47:51 -0400 | [diff] [blame] | 301 | dprintk("--> %s: max_reqs=%u, tbl->max_slots %u\n", __func__, |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 302 | max_reqs, tbl->max_slots); |
| 303 | |
| 304 | if (max_reqs > NFS4_MAX_SLOT_TABLE) |
| 305 | max_reqs = NFS4_MAX_SLOT_TABLE; |
| 306 | |
| 307 | ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue); |
| 308 | if (ret) |
| 309 | goto out; |
| 310 | |
| 311 | spin_lock(&tbl->slot_tbl_lock); |
| 312 | nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue); |
| 313 | spin_unlock(&tbl->slot_tbl_lock); |
| 314 | |
Chuck Lever | e8d9238 | 2013-08-09 12:47:51 -0400 | [diff] [blame] | 315 | dprintk("%s: tbl=%p slots=%p max_slots=%u\n", __func__, |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 316 | tbl, tbl->slots, tbl->max_slots); |
| 317 | out: |
| 318 | dprintk("<-- %s: return %d\n", __func__, ret); |
| 319 | return ret; |
| 320 | } |
| 321 | |
Trond Myklebust | 20b9a90 | 2014-02-01 13:47:06 -0500 | [diff] [blame] | 322 | /* |
| 323 | * nfs4_release_slot_table - release all slot table entries |
| 324 | */ |
| 325 | static void nfs4_release_slot_table(struct nfs4_slot_table *tbl) |
| 326 | { |
| 327 | nfs4_shrink_slot_table(tbl, 0); |
| 328 | } |
| 329 | |
Chuck Lever | 744aa52 | 2013-08-09 12:48:53 -0400 | [diff] [blame] | 330 | /** |
Trond Myklebust | 20b9a90 | 2014-02-01 13:47:06 -0500 | [diff] [blame] | 331 | * nfs4_shutdown_slot_table - release resources attached to a slot table |
Chuck Lever | eb2a1cd | 2013-08-09 12:49:02 -0400 | [diff] [blame] | 332 | * @tbl: slot table to shut down |
| 333 | * |
| 334 | */ |
Trond Myklebust | 20b9a90 | 2014-02-01 13:47:06 -0500 | [diff] [blame] | 335 | void nfs4_shutdown_slot_table(struct nfs4_slot_table *tbl) |
Chuck Lever | eb2a1cd | 2013-08-09 12:49:02 -0400 | [diff] [blame] | 336 | { |
Trond Myklebust | 20b9a90 | 2014-02-01 13:47:06 -0500 | [diff] [blame] | 337 | nfs4_release_slot_table(tbl); |
| 338 | rpc_destroy_wait_queue(&tbl->slot_tbl_waitq); |
Chuck Lever | eb2a1cd | 2013-08-09 12:49:02 -0400 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /** |
Chuck Lever | 744aa52 | 2013-08-09 12:48:53 -0400 | [diff] [blame] | 342 | * nfs4_setup_slot_table - prepare a stand-alone slot table for use |
| 343 | * @tbl: slot table to set up |
| 344 | * @max_reqs: maximum number of requests allowed |
| 345 | * @queue: name to give RPC wait queue |
| 346 | * |
| 347 | * Returns zero on success, or a negative errno. |
| 348 | */ |
| 349 | int nfs4_setup_slot_table(struct nfs4_slot_table *tbl, unsigned int max_reqs, |
| 350 | const char *queue) |
| 351 | { |
| 352 | nfs4_init_slot_table(tbl, queue); |
| 353 | return nfs4_realloc_slot_table(tbl, max_reqs, 0); |
| 354 | } |
| 355 | |
Trond Myklebust | b75ad4c | 2012-11-29 17:27:47 -0500 | [diff] [blame] | 356 | static bool nfs41_assign_slot(struct rpc_task *task, void *pslot) |
| 357 | { |
| 358 | struct nfs4_sequence_args *args = task->tk_msg.rpc_argp; |
| 359 | struct nfs4_sequence_res *res = task->tk_msg.rpc_resp; |
| 360 | struct nfs4_slot *slot = pslot; |
| 361 | struct nfs4_slot_table *tbl = slot->table; |
| 362 | |
Andy Adamson | 774d5f1 | 2013-05-20 14:13:50 -0400 | [diff] [blame] | 363 | if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged) |
Trond Myklebust | b75ad4c | 2012-11-29 17:27:47 -0500 | [diff] [blame] | 364 | return false; |
Trond Myklebust | b75ad4c | 2012-11-29 17:27:47 -0500 | [diff] [blame] | 365 | slot->generation = tbl->generation; |
| 366 | args->sa_slot = slot; |
Trond Myklebust | 8e63b6a | 2012-12-15 15:21:52 -0500 | [diff] [blame] | 367 | res->sr_timestamp = jiffies; |
Trond Myklebust | b75ad4c | 2012-11-29 17:27:47 -0500 | [diff] [blame] | 368 | res->sr_slot = slot; |
| 369 | res->sr_status_flags = 0; |
| 370 | res->sr_status = 1; |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl, |
| 375 | struct nfs4_slot *slot) |
| 376 | { |
| 377 | if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot)) |
| 378 | return true; |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl, |
| 383 | struct nfs4_slot *slot) |
| 384 | { |
| 385 | if (slot->slot_nr > tbl->max_slotid) |
| 386 | return false; |
| 387 | return __nfs41_wake_and_assign_slot(tbl, slot); |
| 388 | } |
| 389 | |
| 390 | static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl) |
| 391 | { |
| 392 | struct nfs4_slot *slot = nfs4_alloc_slot(tbl); |
| 393 | if (!IS_ERR(slot)) { |
| 394 | bool ret = __nfs41_wake_and_assign_slot(tbl, slot); |
| 395 | if (ret) |
| 396 | return ret; |
| 397 | nfs4_free_slot(tbl, slot); |
| 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | void nfs41_wake_slot_table(struct nfs4_slot_table *tbl) |
| 403 | { |
| 404 | for (;;) { |
| 405 | if (!nfs41_try_wake_next_slot_table_entry(tbl)) |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | |
Chuck Lever | 1cec16a | 2013-09-04 12:26:03 -0400 | [diff] [blame] | 410 | #if defined(CONFIG_NFS_V4_1) |
| 411 | |
Trond Myklebust | b0ef964 | 2012-12-11 12:10:14 -0500 | [diff] [blame] | 412 | static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl, |
| 413 | u32 target_highest_slotid) |
| 414 | { |
| 415 | u32 max_slotid; |
| 416 | |
| 417 | max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid); |
| 418 | if (max_slotid > tbl->server_highest_slotid) |
| 419 | max_slotid = tbl->server_highest_slotid; |
| 420 | if (max_slotid > tbl->target_highest_slotid) |
| 421 | max_slotid = tbl->target_highest_slotid; |
| 422 | tbl->max_slotid = max_slotid; |
| 423 | nfs41_wake_slot_table(tbl); |
| 424 | } |
| 425 | |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 426 | /* Update the client's idea of target_highest_slotid */ |
| 427 | static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl, |
| 428 | u32 target_highest_slotid) |
| 429 | { |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 430 | if (tbl->target_highest_slotid == target_highest_slotid) |
| 431 | return; |
| 432 | tbl->target_highest_slotid = target_highest_slotid; |
| 433 | tbl->generation++; |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | void nfs41_set_target_slotid(struct nfs4_slot_table *tbl, |
| 437 | u32 target_highest_slotid) |
| 438 | { |
| 439 | spin_lock(&tbl->slot_tbl_lock); |
| 440 | nfs41_set_target_slotid_locked(tbl, target_highest_slotid); |
Trond Myklebust | 1fa8064 | 2012-12-02 13:54:59 -0500 | [diff] [blame] | 441 | tbl->d_target_highest_slotid = 0; |
| 442 | tbl->d2_target_highest_slotid = 0; |
Trond Myklebust | b0ef964 | 2012-12-11 12:10:14 -0500 | [diff] [blame] | 443 | nfs41_set_max_slotid_locked(tbl, target_highest_slotid); |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 444 | spin_unlock(&tbl->slot_tbl_lock); |
| 445 | } |
| 446 | |
| 447 | static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl, |
| 448 | u32 highest_slotid) |
| 449 | { |
| 450 | if (tbl->server_highest_slotid == highest_slotid) |
| 451 | return; |
| 452 | if (tbl->highest_used_slotid > highest_slotid) |
| 453 | return; |
| 454 | /* Deallocate slots */ |
| 455 | nfs4_shrink_slot_table(tbl, highest_slotid + 1); |
| 456 | tbl->server_highest_slotid = highest_slotid; |
| 457 | } |
| 458 | |
Trond Myklebust | 1fa8064 | 2012-12-02 13:54:59 -0500 | [diff] [blame] | 459 | static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2) |
| 460 | { |
| 461 | s1 -= s2; |
| 462 | if (s1 == 0) |
| 463 | return 0; |
| 464 | if (s1 < 0) |
| 465 | return (s1 - 1) >> 1; |
| 466 | return (s1 + 1) >> 1; |
| 467 | } |
| 468 | |
| 469 | static int nfs41_sign_s32(s32 s1) |
| 470 | { |
| 471 | if (s1 > 0) |
| 472 | return 1; |
| 473 | if (s1 < 0) |
| 474 | return -1; |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2) |
| 479 | { |
| 480 | if (!s1 || !s2) |
| 481 | return true; |
| 482 | return nfs41_sign_s32(s1) == nfs41_sign_s32(s2); |
| 483 | } |
| 484 | |
| 485 | /* Try to eliminate outliers by checking for sharp changes in the |
| 486 | * derivatives and second derivatives |
| 487 | */ |
| 488 | static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl, |
| 489 | u32 new_target) |
| 490 | { |
| 491 | s32 d_target, d2_target; |
| 492 | bool ret = true; |
| 493 | |
| 494 | d_target = nfs41_derivative_target_slotid(new_target, |
| 495 | tbl->target_highest_slotid); |
| 496 | d2_target = nfs41_derivative_target_slotid(d_target, |
| 497 | tbl->d_target_highest_slotid); |
| 498 | /* Is first derivative same sign? */ |
| 499 | if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid)) |
| 500 | ret = false; |
| 501 | /* Is second derivative same sign? */ |
| 502 | if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid)) |
| 503 | ret = false; |
| 504 | tbl->d_target_highest_slotid = d_target; |
| 505 | tbl->d2_target_highest_slotid = d2_target; |
| 506 | return ret; |
| 507 | } |
| 508 | |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 509 | void nfs41_update_target_slotid(struct nfs4_slot_table *tbl, |
| 510 | struct nfs4_slot *slot, |
| 511 | struct nfs4_sequence_res *res) |
| 512 | { |
| 513 | spin_lock(&tbl->slot_tbl_lock); |
Trond Myklebust | 1fa8064 | 2012-12-02 13:54:59 -0500 | [diff] [blame] | 514 | if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid)) |
| 515 | nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid); |
| 516 | if (tbl->generation == slot->generation) |
| 517 | nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid); |
Trond Myklebust | b0ef964 | 2012-12-11 12:10:14 -0500 | [diff] [blame] | 518 | nfs41_set_max_slotid_locked(tbl, res->sr_target_highest_slotid); |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 519 | spin_unlock(&tbl->slot_tbl_lock); |
| 520 | } |
| 521 | |
Trond Myklebust | 20b9a90 | 2014-02-01 13:47:06 -0500 | [diff] [blame] | 522 | static void nfs4_release_session_slot_tables(struct nfs4_session *session) |
Chuck Lever | 9d33059 | 2013-08-09 12:48:44 -0400 | [diff] [blame] | 523 | { |
Chuck Lever | eb2a1cd | 2013-08-09 12:49:02 -0400 | [diff] [blame] | 524 | nfs4_release_slot_table(&session->fc_slot_table); |
| 525 | nfs4_release_slot_table(&session->bc_slot_table); |
Chuck Lever | 9d33059 | 2013-08-09 12:48:44 -0400 | [diff] [blame] | 526 | } |
| 527 | |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 528 | /* |
| 529 | * Initialize or reset the forechannel and backchannel tables |
| 530 | */ |
| 531 | int nfs4_setup_session_slot_tables(struct nfs4_session *ses) |
| 532 | { |
| 533 | struct nfs4_slot_table *tbl; |
| 534 | int status; |
| 535 | |
| 536 | dprintk("--> %s\n", __func__); |
| 537 | /* Fore channel */ |
| 538 | tbl = &ses->fc_slot_table; |
| 539 | tbl->session = ses; |
| 540 | status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1); |
Trond Myklebust | b1c0df5 | 2015-02-18 11:34:58 -0800 | [diff] [blame] | 541 | if (status || !(ses->flags & SESSION4_BACK_CHAN)) /* -ENOMEM */ |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 542 | return status; |
| 543 | /* Back channel */ |
| 544 | tbl = &ses->bc_slot_table; |
| 545 | tbl->session = ses; |
| 546 | status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0); |
| 547 | if (status && tbl->slots == NULL) |
| 548 | /* Fore and back channel share a connection so get |
| 549 | * both slot tables or neither */ |
Trond Myklebust | 20b9a90 | 2014-02-01 13:47:06 -0500 | [diff] [blame] | 550 | nfs4_release_session_slot_tables(ses); |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 551 | return status; |
| 552 | } |
| 553 | |
| 554 | struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp) |
| 555 | { |
| 556 | struct nfs4_session *session; |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 557 | |
| 558 | session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS); |
| 559 | if (!session) |
| 560 | return NULL; |
| 561 | |
Chuck Lever | 744aa52 | 2013-08-09 12:48:53 -0400 | [diff] [blame] | 562 | nfs4_init_slot_table(&session->fc_slot_table, "ForeChannel Slot table"); |
| 563 | nfs4_init_slot_table(&session->bc_slot_table, "BackChannel Slot table"); |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 564 | session->session_state = 1<<NFS4_SESSION_INITING; |
| 565 | |
| 566 | session->clp = clp; |
| 567 | return session; |
| 568 | } |
| 569 | |
Trond Myklebust | 20b9a90 | 2014-02-01 13:47:06 -0500 | [diff] [blame] | 570 | static void nfs4_destroy_session_slot_tables(struct nfs4_session *session) |
| 571 | { |
| 572 | nfs4_shutdown_slot_table(&session->fc_slot_table); |
| 573 | nfs4_shutdown_slot_table(&session->bc_slot_table); |
| 574 | } |
| 575 | |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 576 | void nfs4_destroy_session(struct nfs4_session *session) |
| 577 | { |
| 578 | struct rpc_xprt *xprt; |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 579 | const struct cred *cred; |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 580 | |
Chuck Lever | 73d8bde | 2013-07-24 12:28:37 -0400 | [diff] [blame] | 581 | cred = nfs4_get_clid_cred(session->clp); |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 582 | nfs4_proc_destroy_session(session, cred); |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 583 | put_cred(cred); |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 584 | |
| 585 | rcu_read_lock(); |
| 586 | xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt); |
| 587 | rcu_read_unlock(); |
| 588 | dprintk("%s Destroy backchannel for xprt %p\n", |
| 589 | __func__, xprt); |
| 590 | xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS); |
Chuck Lever | eb2a1cd | 2013-08-09 12:49:02 -0400 | [diff] [blame] | 591 | nfs4_destroy_session_slot_tables(session); |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 592 | kfree(session); |
| 593 | } |
| 594 | |
| 595 | /* |
| 596 | * With sessions, the client is not marked ready until after a |
| 597 | * successful EXCHANGE_ID and CREATE_SESSION. |
| 598 | * |
| 599 | * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate |
| 600 | * other versions of NFS can be tried. |
| 601 | */ |
| 602 | static int nfs41_check_session_ready(struct nfs_client *clp) |
| 603 | { |
| 604 | int ret; |
| 605 | |
| 606 | if (clp->cl_cons_state == NFS_CS_SESSION_INITING) { |
| 607 | ret = nfs4_client_recover_expired_lease(clp); |
| 608 | if (ret) |
| 609 | return ret; |
| 610 | } |
| 611 | if (clp->cl_cons_state < NFS_CS_READY) |
| 612 | return -EPROTONOSUPPORT; |
| 613 | smp_rmb(); |
| 614 | return 0; |
| 615 | } |
| 616 | |
Andy Adamson | 18aad3d | 2013-06-26 12:21:49 -0400 | [diff] [blame] | 617 | int nfs4_init_session(struct nfs_client *clp) |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 618 | { |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 619 | if (!nfs4_has_session(clp)) |
| 620 | return 0; |
| 621 | |
Andy Adamson | 18aad3d | 2013-06-26 12:21:49 -0400 | [diff] [blame] | 622 | clear_bit(NFS4_SESSION_INITING, &clp->cl_session->session_state); |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 623 | return nfs41_check_session_ready(clp); |
| 624 | } |
| 625 | |
| 626 | int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time) |
| 627 | { |
| 628 | struct nfs4_session *session = clp->cl_session; |
| 629 | int ret; |
| 630 | |
| 631 | spin_lock(&clp->cl_lock); |
| 632 | if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) { |
| 633 | /* |
| 634 | * Do not set NFS_CS_CHECK_LEASE_TIME instead set the |
| 635 | * DS lease to be equal to the MDS lease. |
| 636 | */ |
| 637 | clp->cl_lease_time = lease_time; |
| 638 | clp->cl_last_renewal = jiffies; |
| 639 | } |
| 640 | spin_unlock(&clp->cl_lock); |
| 641 | |
| 642 | ret = nfs41_check_session_ready(clp); |
| 643 | if (ret) |
| 644 | return ret; |
| 645 | /* Test for the DS role */ |
| 646 | if (!is_ds_client(clp)) |
| 647 | return -ENODEV; |
| 648 | return 0; |
| 649 | } |
| 650 | EXPORT_SYMBOL_GPL(nfs4_init_ds_session); |
| 651 | |
Chuck Lever | 9d33059 | 2013-08-09 12:48:44 -0400 | [diff] [blame] | 652 | #endif /* defined(CONFIG_NFS_V4_1) */ |