Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 1 | /* |
| 2 | * pNFS functions to call and manage layout drivers. |
| 3 | * |
| 4 | * Copyright (c) 2002 [year of first publication] |
| 5 | * The Regents of the University of Michigan |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * Dean Hildebrand <dhildebz@umich.edu> |
| 9 | * |
| 10 | * Permission is granted to use, copy, create derivative works, and |
| 11 | * redistribute this software and such derivative works for any purpose, |
| 12 | * so long as the name of the University of Michigan is not used in |
| 13 | * any advertising or publicity pertaining to the use or distribution |
| 14 | * of this software without specific, written prior authorization. If |
| 15 | * the above copyright notice or any other identification of the |
| 16 | * University of Michigan is included in any copy of any portion of |
| 17 | * this software, then the disclaimer below must also be included. |
| 18 | * |
| 19 | * This software is provided as is, without representation or warranty |
| 20 | * of any kind either express or implied, including without limitation |
| 21 | * the implied warranties of merchantability, fitness for a particular |
| 22 | * purpose, or noninfringement. The Regents of the University of |
| 23 | * Michigan shall not be liable for any damages, including special, |
| 24 | * indirect, incidental, or consequential damages, with respect to any |
| 25 | * claim arising out of or in connection with the use of the software, |
| 26 | * even if it has been or is hereafter advised of the possibility of |
| 27 | * such damages. |
| 28 | */ |
| 29 | |
| 30 | #include <linux/nfs_fs.h> |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 31 | #include "internal.h" |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 32 | #include "pnfs.h" |
| 33 | |
| 34 | #define NFSDBG_FACILITY NFSDBG_PNFS |
| 35 | |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 36 | /* Locking: |
| 37 | * |
| 38 | * pnfs_spinlock: |
| 39 | * protects pnfs_modules_tbl. |
| 40 | */ |
| 41 | static DEFINE_SPINLOCK(pnfs_spinlock); |
| 42 | |
| 43 | /* |
| 44 | * pnfs_modules_tbl holds all pnfs modules |
| 45 | */ |
| 46 | static LIST_HEAD(pnfs_modules_tbl); |
| 47 | |
| 48 | /* Return the registered pnfs layout driver module matching given id */ |
| 49 | static struct pnfs_layoutdriver_type * |
| 50 | find_pnfs_driver_locked(u32 id) |
| 51 | { |
| 52 | struct pnfs_layoutdriver_type *local; |
| 53 | |
| 54 | list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid) |
| 55 | if (local->id == id) |
| 56 | goto out; |
| 57 | local = NULL; |
| 58 | out: |
| 59 | dprintk("%s: Searching for id %u, found %p\n", __func__, id, local); |
| 60 | return local; |
| 61 | } |
| 62 | |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 63 | static struct pnfs_layoutdriver_type * |
| 64 | find_pnfs_driver(u32 id) |
| 65 | { |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 66 | struct pnfs_layoutdriver_type *local; |
| 67 | |
| 68 | spin_lock(&pnfs_spinlock); |
| 69 | local = find_pnfs_driver_locked(id); |
| 70 | spin_unlock(&pnfs_spinlock); |
| 71 | return local; |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void |
| 75 | unset_pnfs_layoutdriver(struct nfs_server *nfss) |
| 76 | { |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 77 | if (nfss->pnfs_curr_ld) { |
Trond Myklebust | 1c78709 | 2010-10-21 16:56:48 -0400 | [diff] [blame] | 78 | nfss->pnfs_curr_ld->clear_layoutdriver(nfss); |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 79 | module_put(nfss->pnfs_curr_ld->owner); |
| 80 | } |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 81 | nfss->pnfs_curr_ld = NULL; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Try to set the server's pnfs module to the pnfs layout type specified by id. |
| 86 | * Currently only one pNFS layout driver per filesystem is supported. |
| 87 | * |
| 88 | * @id layout type. Zero (illegal layout type) indicates pNFS not in use. |
| 89 | */ |
| 90 | void |
| 91 | set_pnfs_layoutdriver(struct nfs_server *server, u32 id) |
| 92 | { |
| 93 | struct pnfs_layoutdriver_type *ld_type = NULL; |
| 94 | |
| 95 | if (id == 0) |
| 96 | goto out_no_driver; |
| 97 | if (!(server->nfs_client->cl_exchange_flags & |
| 98 | (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) { |
| 99 | printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__, |
| 100 | id, server->nfs_client->cl_exchange_flags); |
| 101 | goto out_no_driver; |
| 102 | } |
| 103 | ld_type = find_pnfs_driver(id); |
| 104 | if (!ld_type) { |
| 105 | request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id); |
| 106 | ld_type = find_pnfs_driver(id); |
| 107 | if (!ld_type) { |
| 108 | dprintk("%s: No pNFS module found for %u.\n", |
| 109 | __func__, id); |
| 110 | goto out_no_driver; |
| 111 | } |
| 112 | } |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 113 | if (!try_module_get(ld_type->owner)) { |
| 114 | dprintk("%s: Could not grab reference on module\n", __func__); |
| 115 | goto out_no_driver; |
| 116 | } |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 117 | server->pnfs_curr_ld = ld_type; |
Trond Myklebust | 1c78709 | 2010-10-21 16:56:48 -0400 | [diff] [blame] | 118 | if (ld_type->set_layoutdriver(server)) { |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 119 | printk(KERN_ERR |
| 120 | "%s: Error initializing mount point for layout driver %u.\n", |
| 121 | __func__, id); |
| 122 | module_put(ld_type->owner); |
| 123 | goto out_no_driver; |
| 124 | } |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 125 | dprintk("%s: pNFS module for %u set\n", __func__, id); |
| 126 | return; |
| 127 | |
| 128 | out_no_driver: |
| 129 | dprintk("%s: Using NFSv4 I/O\n", __func__); |
| 130 | server->pnfs_curr_ld = NULL; |
| 131 | } |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 132 | |
| 133 | int |
| 134 | pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type) |
| 135 | { |
| 136 | int status = -EINVAL; |
| 137 | struct pnfs_layoutdriver_type *tmp; |
| 138 | |
| 139 | if (ld_type->id == 0) { |
| 140 | printk(KERN_ERR "%s id 0 is reserved\n", __func__); |
| 141 | return status; |
| 142 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 143 | if (!ld_type->alloc_lseg || !ld_type->free_lseg) { |
| 144 | printk(KERN_ERR "%s Layout driver must provide " |
| 145 | "alloc_lseg and free_lseg.\n", __func__); |
| 146 | return status; |
| 147 | } |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 148 | |
| 149 | spin_lock(&pnfs_spinlock); |
| 150 | tmp = find_pnfs_driver_locked(ld_type->id); |
| 151 | if (!tmp) { |
| 152 | list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl); |
| 153 | status = 0; |
| 154 | dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id, |
| 155 | ld_type->name); |
| 156 | } else { |
| 157 | printk(KERN_ERR "%s Module with id %d already loaded!\n", |
| 158 | __func__, ld_type->id); |
| 159 | } |
| 160 | spin_unlock(&pnfs_spinlock); |
| 161 | |
| 162 | return status; |
| 163 | } |
| 164 | EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver); |
| 165 | |
| 166 | void |
| 167 | pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type) |
| 168 | { |
| 169 | dprintk("%s Deregistering id:%u\n", __func__, ld_type->id); |
| 170 | spin_lock(&pnfs_spinlock); |
| 171 | list_del(&ld_type->pnfs_tblid); |
| 172 | spin_unlock(&pnfs_spinlock); |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 175 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 176 | /* |
| 177 | * pNFS client layout cache |
| 178 | */ |
| 179 | |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 180 | /* Need to hold i_lock if caller does not already hold reference */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 181 | void |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 182 | get_layout_hdr(struct pnfs_layout_hdr *lo) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 183 | { |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 184 | atomic_inc(&lo->plh_refcount); |
| 185 | } |
| 186 | |
| 187 | static void |
| 188 | destroy_layout_hdr(struct pnfs_layout_hdr *lo) |
| 189 | { |
| 190 | dprintk("%s: freeing layout cache %p\n", __func__, lo); |
| 191 | BUG_ON(!list_empty(&lo->plh_layouts)); |
| 192 | NFS_I(lo->plh_inode)->layout = NULL; |
| 193 | kfree(lo); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static void |
| 197 | put_layout_hdr_locked(struct pnfs_layout_hdr *lo) |
| 198 | { |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 199 | if (atomic_dec_and_test(&lo->plh_refcount)) |
| 200 | destroy_layout_hdr(lo); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 203 | void |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 204 | put_layout_hdr(struct pnfs_layout_hdr *lo) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 205 | { |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 206 | struct inode *inode = lo->plh_inode; |
| 207 | |
| 208 | if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) { |
| 209 | destroy_layout_hdr(lo); |
| 210 | spin_unlock(&inode->i_lock); |
| 211 | } |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void |
| 215 | init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg) |
| 216 | { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 217 | INIT_LIST_HEAD(&lseg->pls_list); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 218 | atomic_set(&lseg->pls_refcount, 1); |
| 219 | smp_mb(); |
| 220 | set_bit(NFS_LSEG_VALID, &lseg->pls_flags); |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 221 | lseg->pls_layout = lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 222 | } |
| 223 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 224 | static void free_lseg(struct pnfs_layout_segment *lseg) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 225 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 226 | struct inode *ino = lseg->pls_layout->plh_inode; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 227 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 228 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
Fred Isaman | 52fabd7 | 2011-01-06 11:36:18 +0000 | [diff] [blame] | 229 | /* Matched by get_layout_hdr in pnfs_insert_layout */ |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 230 | put_layout_hdr(NFS_I(ino)->layout); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 233 | /* The use of tmp_list is necessary because pnfs_curr_ld->free_lseg |
| 234 | * could sleep, so must be called outside of the lock. |
| 235 | * Returns 1 if object was removed, otherwise return 0. |
| 236 | */ |
| 237 | static int |
| 238 | put_lseg_locked(struct pnfs_layout_segment *lseg, |
| 239 | struct list_head *tmp_list) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 240 | { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 241 | dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg, |
| 242 | atomic_read(&lseg->pls_refcount), |
| 243 | test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
| 244 | if (atomic_dec_and_test(&lseg->pls_refcount)) { |
| 245 | struct inode *ino = lseg->pls_layout->plh_inode; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 246 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 247 | BUG_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
| 248 | list_del(&lseg->pls_list); |
| 249 | if (list_empty(&lseg->pls_layout->plh_segs)) { |
| 250 | struct nfs_client *clp; |
| 251 | |
| 252 | clp = NFS_SERVER(ino)->nfs_client; |
| 253 | spin_lock(&clp->cl_lock); |
| 254 | /* List does not take a reference, so no need for put here */ |
| 255 | list_del_init(&lseg->pls_layout->plh_layouts); |
| 256 | spin_unlock(&clp->cl_lock); |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 257 | set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags); |
| 258 | /* Matched by initial refcount set in alloc_init_layout_hdr */ |
| 259 | put_layout_hdr_locked(lseg->pls_layout); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 260 | } |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 261 | rpc_wake_up(&NFS_SERVER(ino)->roc_rpcwaitq); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 262 | list_add(&lseg->pls_list, tmp_list); |
| 263 | return 1; |
| 264 | } |
| 265 | return 0; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 266 | } |
| 267 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 268 | static bool |
| 269 | should_free_lseg(u32 lseg_iomode, u32 recall_iomode) |
| 270 | { |
| 271 | return (recall_iomode == IOMODE_ANY || |
| 272 | lseg_iomode == recall_iomode); |
| 273 | } |
| 274 | |
| 275 | /* Returns 1 if lseg is removed from list, 0 otherwise */ |
| 276 | static int mark_lseg_invalid(struct pnfs_layout_segment *lseg, |
| 277 | struct list_head *tmp_list) |
| 278 | { |
| 279 | int rv = 0; |
| 280 | |
| 281 | if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) { |
| 282 | /* Remove the reference keeping the lseg in the |
| 283 | * list. It will now be removed when all |
| 284 | * outstanding io is finished. |
| 285 | */ |
| 286 | rv = put_lseg_locked(lseg, tmp_list); |
| 287 | } |
| 288 | return rv; |
| 289 | } |
| 290 | |
| 291 | /* Returns count of number of matching invalid lsegs remaining in list |
| 292 | * after call. |
| 293 | */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 294 | int |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 295 | mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo, |
| 296 | struct list_head *tmp_list, |
| 297 | u32 iomode) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 298 | { |
| 299 | struct pnfs_layout_segment *lseg, *next; |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 300 | int invalid = 0, removed = 0; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 301 | |
| 302 | dprintk("%s:Begin lo %p\n", __func__, lo); |
| 303 | |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 304 | if (list_empty(&lo->plh_segs)) { |
| 305 | if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) |
| 306 | put_layout_hdr_locked(lo); |
| 307 | return 0; |
| 308 | } |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 309 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) |
| 310 | if (should_free_lseg(lseg->pls_range.iomode, iomode)) { |
| 311 | dprintk("%s: freeing lseg %p iomode %d " |
| 312 | "offset %llu length %llu\n", __func__, |
| 313 | lseg, lseg->pls_range.iomode, lseg->pls_range.offset, |
| 314 | lseg->pls_range.length); |
| 315 | invalid++; |
| 316 | removed += mark_lseg_invalid(lseg, tmp_list); |
| 317 | } |
| 318 | dprintk("%s:Return %i\n", __func__, invalid - removed); |
| 319 | return invalid - removed; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 320 | } |
| 321 | |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 322 | void |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 323 | pnfs_free_lseg_list(struct list_head *free_me) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 324 | { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 325 | struct pnfs_layout_segment *lseg, *tmp; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 326 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 327 | list_for_each_entry_safe(lseg, tmp, free_me, pls_list) { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 328 | list_del(&lseg->pls_list); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 329 | free_lseg(lseg); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 333 | void |
| 334 | pnfs_destroy_layout(struct nfs_inode *nfsi) |
| 335 | { |
| 336 | struct pnfs_layout_hdr *lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 337 | LIST_HEAD(tmp_list); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 338 | |
| 339 | spin_lock(&nfsi->vfs_inode.i_lock); |
| 340 | lo = nfsi->layout; |
| 341 | if (lo) { |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 342 | lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */ |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 343 | mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 344 | } |
| 345 | spin_unlock(&nfsi->vfs_inode.i_lock); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 346 | pnfs_free_lseg_list(&tmp_list); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 347 | } |
| 348 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 349 | /* |
| 350 | * Called by the state manger to remove all layouts established under an |
| 351 | * expired lease. |
| 352 | */ |
| 353 | void |
| 354 | pnfs_destroy_all_layouts(struct nfs_client *clp) |
| 355 | { |
| 356 | struct pnfs_layout_hdr *lo; |
| 357 | LIST_HEAD(tmp_list); |
| 358 | |
| 359 | spin_lock(&clp->cl_lock); |
| 360 | list_splice_init(&clp->cl_layouts, &tmp_list); |
| 361 | spin_unlock(&clp->cl_lock); |
| 362 | |
| 363 | while (!list_empty(&tmp_list)) { |
| 364 | lo = list_entry(tmp_list.next, struct pnfs_layout_hdr, |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 365 | plh_layouts); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 366 | dprintk("%s freeing layout for inode %lu\n", __func__, |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 367 | lo->plh_inode->i_ino); |
| 368 | pnfs_destroy_layout(NFS_I(lo->plh_inode)); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 372 | /* update lo->plh_stateid with new if is more recent */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 373 | void |
| 374 | pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new, |
| 375 | bool update_barrier) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 376 | { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 377 | u32 oldseq, newseq; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 378 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 379 | oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid); |
| 380 | newseq = be32_to_cpu(new->stateid.seqid); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 381 | if ((int)(newseq - oldseq) > 0) { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 382 | memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid)); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 383 | if (update_barrier) { |
| 384 | u32 new_barrier = be32_to_cpu(new->stateid.seqid); |
| 385 | |
| 386 | if ((int)(new_barrier - lo->plh_barrier)) |
| 387 | lo->plh_barrier = new_barrier; |
| 388 | } else { |
| 389 | /* Because of wraparound, we want to keep the barrier |
| 390 | * "close" to the current seqids. It needs to be |
| 391 | * within 2**31 to count as "behind", so if it |
| 392 | * gets too near that limit, give us a litle leeway |
| 393 | * and bring it to within 2**30. |
| 394 | * NOTE - and yes, this is all unsigned arithmetic. |
| 395 | */ |
| 396 | if (unlikely((newseq - lo->plh_barrier) > (3 << 29))) |
| 397 | lo->plh_barrier = newseq - (1 << 30); |
| 398 | } |
| 399 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 400 | } |
| 401 | |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 402 | /* lget is set to 1 if called from inside send_layoutget call chain */ |
| 403 | static bool |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 404 | pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid, |
| 405 | int lget) |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 406 | { |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 407 | if ((stateid) && |
| 408 | (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0) |
| 409 | return true; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 410 | return lo->plh_block_lgets || |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 411 | test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) || |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 412 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) || |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 413 | (list_empty(&lo->plh_segs) && |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 414 | (atomic_read(&lo->plh_outstanding) > lget)); |
| 415 | } |
| 416 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 417 | int |
| 418 | pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo, |
| 419 | struct nfs4_state *open_state) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 420 | { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 421 | int status = 0; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 422 | |
| 423 | dprintk("--> %s\n", __func__); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 424 | spin_lock(&lo->plh_inode->i_lock); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 425 | if (pnfs_layoutgets_blocked(lo, NULL, 1)) { |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 426 | status = -EAGAIN; |
| 427 | } else if (list_empty(&lo->plh_segs)) { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 428 | int seq; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 429 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 430 | do { |
| 431 | seq = read_seqbegin(&open_state->seqlock); |
| 432 | memcpy(dst->data, open_state->stateid.data, |
| 433 | sizeof(open_state->stateid.data)); |
| 434 | } while (read_seqretry(&open_state->seqlock, seq)); |
| 435 | } else |
| 436 | memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data)); |
| 437 | spin_unlock(&lo->plh_inode->i_lock); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 438 | dprintk("<-- %s\n", __func__); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 439 | return status; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Get layout from server. |
| 444 | * for now, assume that whole file layouts are requested. |
| 445 | * arg->offset: 0 |
| 446 | * arg->length: all ones |
| 447 | */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 448 | static struct pnfs_layout_segment * |
| 449 | send_layoutget(struct pnfs_layout_hdr *lo, |
| 450 | struct nfs_open_context *ctx, |
| 451 | u32 iomode) |
| 452 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 453 | struct inode *ino = lo->plh_inode; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 454 | struct nfs_server *server = NFS_SERVER(ino); |
| 455 | struct nfs4_layoutget *lgp; |
| 456 | struct pnfs_layout_segment *lseg = NULL; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 457 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 458 | dprintk("--> %s\n", __func__); |
| 459 | |
| 460 | BUG_ON(ctx == NULL); |
| 461 | lgp = kzalloc(sizeof(*lgp), GFP_KERNEL); |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 462 | if (lgp == NULL) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 463 | return NULL; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 464 | lgp->args.minlength = NFS4_MAX_UINT64; |
| 465 | lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE; |
| 466 | lgp->args.range.iomode = iomode; |
| 467 | lgp->args.range.offset = 0; |
| 468 | lgp->args.range.length = NFS4_MAX_UINT64; |
| 469 | lgp->args.type = server->pnfs_curr_ld->id; |
| 470 | lgp->args.inode = ino; |
| 471 | lgp->args.ctx = get_nfs_open_context(ctx); |
| 472 | lgp->lsegpp = &lseg; |
| 473 | |
| 474 | /* Synchronously retrieve layout information from server and |
| 475 | * store in lseg. |
| 476 | */ |
| 477 | nfs4_proc_layoutget(lgp); |
| 478 | if (!lseg) { |
| 479 | /* remember that LAYOUTGET failed and suspend trying */ |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 480 | set_bit(lo_fail_bit(iomode), &lo->plh_flags); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 481 | } |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 482 | return lseg; |
| 483 | } |
| 484 | |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 485 | bool pnfs_roc(struct inode *ino) |
| 486 | { |
| 487 | struct pnfs_layout_hdr *lo; |
| 488 | struct pnfs_layout_segment *lseg, *tmp; |
| 489 | LIST_HEAD(tmp_list); |
| 490 | bool found = false; |
| 491 | |
| 492 | spin_lock(&ino->i_lock); |
| 493 | lo = NFS_I(ino)->layout; |
| 494 | if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) || |
| 495 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) |
| 496 | goto out_nolayout; |
| 497 | list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list) |
| 498 | if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { |
| 499 | mark_lseg_invalid(lseg, &tmp_list); |
| 500 | found = true; |
| 501 | } |
| 502 | if (!found) |
| 503 | goto out_nolayout; |
| 504 | lo->plh_block_lgets++; |
| 505 | get_layout_hdr(lo); /* matched in pnfs_roc_release */ |
| 506 | spin_unlock(&ino->i_lock); |
| 507 | pnfs_free_lseg_list(&tmp_list); |
| 508 | return true; |
| 509 | |
| 510 | out_nolayout: |
| 511 | spin_unlock(&ino->i_lock); |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | void pnfs_roc_release(struct inode *ino) |
| 516 | { |
| 517 | struct pnfs_layout_hdr *lo; |
| 518 | |
| 519 | spin_lock(&ino->i_lock); |
| 520 | lo = NFS_I(ino)->layout; |
| 521 | lo->plh_block_lgets--; |
| 522 | put_layout_hdr_locked(lo); |
| 523 | spin_unlock(&ino->i_lock); |
| 524 | } |
| 525 | |
| 526 | void pnfs_roc_set_barrier(struct inode *ino, u32 barrier) |
| 527 | { |
| 528 | struct pnfs_layout_hdr *lo; |
| 529 | |
| 530 | spin_lock(&ino->i_lock); |
| 531 | lo = NFS_I(ino)->layout; |
| 532 | if ((int)(barrier - lo->plh_barrier) > 0) |
| 533 | lo->plh_barrier = barrier; |
| 534 | spin_unlock(&ino->i_lock); |
| 535 | } |
| 536 | |
| 537 | bool pnfs_roc_drain(struct inode *ino, u32 *barrier) |
| 538 | { |
| 539 | struct nfs_inode *nfsi = NFS_I(ino); |
| 540 | struct pnfs_layout_segment *lseg; |
| 541 | bool found = false; |
| 542 | |
| 543 | spin_lock(&ino->i_lock); |
| 544 | list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list) |
| 545 | if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { |
| 546 | found = true; |
| 547 | break; |
| 548 | } |
| 549 | if (!found) { |
| 550 | struct pnfs_layout_hdr *lo = nfsi->layout; |
| 551 | u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid); |
| 552 | |
| 553 | /* Since close does not return a layout stateid for use as |
| 554 | * a barrier, we choose the worst-case barrier. |
| 555 | */ |
| 556 | *barrier = current_seqid + atomic_read(&lo->plh_outstanding); |
| 557 | } |
| 558 | spin_unlock(&ino->i_lock); |
| 559 | return found; |
| 560 | } |
| 561 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 562 | /* |
| 563 | * Compare two layout segments for sorting into layout cache. |
| 564 | * We want to preferentially return RW over RO layouts, so ensure those |
| 565 | * are seen first. |
| 566 | */ |
| 567 | static s64 |
| 568 | cmp_layout(u32 iomode1, u32 iomode2) |
| 569 | { |
| 570 | /* read > read/write */ |
| 571 | return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ); |
| 572 | } |
| 573 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 574 | static void |
| 575 | pnfs_insert_layout(struct pnfs_layout_hdr *lo, |
| 576 | struct pnfs_layout_segment *lseg) |
| 577 | { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 578 | struct pnfs_layout_segment *lp; |
| 579 | int found = 0; |
| 580 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 581 | dprintk("%s:Begin\n", __func__); |
| 582 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 583 | assert_spin_locked(&lo->plh_inode->i_lock); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 584 | list_for_each_entry(lp, &lo->plh_segs, pls_list) { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 585 | if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 586 | continue; |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 587 | list_add_tail(&lseg->pls_list, &lp->pls_list); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 588 | dprintk("%s: inserted lseg %p " |
| 589 | "iomode %d offset %llu length %llu before " |
| 590 | "lp %p iomode %d offset %llu length %llu\n", |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 591 | __func__, lseg, lseg->pls_range.iomode, |
| 592 | lseg->pls_range.offset, lseg->pls_range.length, |
| 593 | lp, lp->pls_range.iomode, lp->pls_range.offset, |
| 594 | lp->pls_range.length); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 595 | found = 1; |
| 596 | break; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 597 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 598 | if (!found) { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 599 | list_add_tail(&lseg->pls_list, &lo->plh_segs); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 600 | dprintk("%s: inserted lseg %p " |
| 601 | "iomode %d offset %llu length %llu at tail\n", |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 602 | __func__, lseg, lseg->pls_range.iomode, |
| 603 | lseg->pls_range.offset, lseg->pls_range.length); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 604 | } |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 605 | get_layout_hdr(lo); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 606 | |
| 607 | dprintk("%s:Return\n", __func__); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static struct pnfs_layout_hdr * |
| 611 | alloc_init_layout_hdr(struct inode *ino) |
| 612 | { |
| 613 | struct pnfs_layout_hdr *lo; |
| 614 | |
| 615 | lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL); |
| 616 | if (!lo) |
| 617 | return NULL; |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 618 | atomic_set(&lo->plh_refcount, 1); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 619 | INIT_LIST_HEAD(&lo->plh_layouts); |
| 620 | INIT_LIST_HEAD(&lo->plh_segs); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 621 | INIT_LIST_HEAD(&lo->plh_bulk_recall); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 622 | lo->plh_inode = ino; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 623 | return lo; |
| 624 | } |
| 625 | |
| 626 | static struct pnfs_layout_hdr * |
| 627 | pnfs_find_alloc_layout(struct inode *ino) |
| 628 | { |
| 629 | struct nfs_inode *nfsi = NFS_I(ino); |
| 630 | struct pnfs_layout_hdr *new = NULL; |
| 631 | |
| 632 | dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout); |
| 633 | |
| 634 | assert_spin_locked(&ino->i_lock); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 635 | if (nfsi->layout) { |
| 636 | if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags)) |
| 637 | return NULL; |
| 638 | else |
| 639 | return nfsi->layout; |
| 640 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 641 | spin_unlock(&ino->i_lock); |
| 642 | new = alloc_init_layout_hdr(ino); |
| 643 | spin_lock(&ino->i_lock); |
| 644 | |
| 645 | if (likely(nfsi->layout == NULL)) /* Won the race? */ |
| 646 | nfsi->layout = new; |
| 647 | else |
| 648 | kfree(new); |
| 649 | return nfsi->layout; |
| 650 | } |
| 651 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 652 | /* |
| 653 | * iomode matching rules: |
| 654 | * iomode lseg match |
| 655 | * ----- ----- ----- |
| 656 | * ANY READ true |
| 657 | * ANY RW true |
| 658 | * RW READ false |
| 659 | * RW RW true |
| 660 | * READ READ true |
| 661 | * READ RW true |
| 662 | */ |
| 663 | static int |
| 664 | is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode) |
| 665 | { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 666 | return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /* |
| 670 | * lookup range in layout |
| 671 | */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 672 | static struct pnfs_layout_segment * |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 673 | pnfs_find_lseg(struct pnfs_layout_hdr *lo, u32 iomode) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 674 | { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 675 | struct pnfs_layout_segment *lseg, *ret = NULL; |
| 676 | |
| 677 | dprintk("%s:Begin\n", __func__); |
| 678 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 679 | assert_spin_locked(&lo->plh_inode->i_lock); |
| 680 | list_for_each_entry(lseg, &lo->plh_segs, pls_list) { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 681 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) && |
| 682 | is_matching_lseg(lseg, iomode)) { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 683 | ret = lseg; |
| 684 | break; |
| 685 | } |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 686 | if (cmp_layout(iomode, lseg->pls_range.iomode) > 0) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 687 | break; |
| 688 | } |
| 689 | |
| 690 | dprintk("%s:Return lseg %p ref %d\n", |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 691 | __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 692 | return ret; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /* |
| 696 | * Layout segment is retreived from the server if not cached. |
| 697 | * The appropriate layout segment is referenced and returned to the caller. |
| 698 | */ |
| 699 | struct pnfs_layout_segment * |
| 700 | pnfs_update_layout(struct inode *ino, |
| 701 | struct nfs_open_context *ctx, |
| 702 | enum pnfs_iomode iomode) |
| 703 | { |
| 704 | struct nfs_inode *nfsi = NFS_I(ino); |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 705 | struct nfs_client *clp = NFS_SERVER(ino)->nfs_client; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 706 | struct pnfs_layout_hdr *lo; |
| 707 | struct pnfs_layout_segment *lseg = NULL; |
| 708 | |
| 709 | if (!pnfs_enabled_sb(NFS_SERVER(ino))) |
| 710 | return NULL; |
| 711 | spin_lock(&ino->i_lock); |
| 712 | lo = pnfs_find_alloc_layout(ino); |
| 713 | if (lo == NULL) { |
| 714 | dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__); |
| 715 | goto out_unlock; |
| 716 | } |
| 717 | |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 718 | /* Do we even need to bother with this? */ |
| 719 | if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) || |
| 720 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
| 721 | dprintk("%s matches recall, use MDS\n", __func__); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 722 | goto out_unlock; |
| 723 | } |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 724 | /* Check to see if the layout for the given range already exists */ |
| 725 | lseg = pnfs_find_lseg(lo, iomode); |
| 726 | if (lseg) |
| 727 | goto out_unlock; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 728 | |
| 729 | /* if LAYOUTGET already failed once we don't try again */ |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 730 | if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags)) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 731 | goto out_unlock; |
| 732 | |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 733 | if (pnfs_layoutgets_blocked(lo, NULL, 0)) |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 734 | goto out_unlock; |
| 735 | atomic_inc(&lo->plh_outstanding); |
| 736 | |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 737 | get_layout_hdr(lo); |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 738 | if (list_empty(&lo->plh_segs)) { |
| 739 | /* The lo must be on the clp list if there is any |
| 740 | * chance of a CB_LAYOUTRECALL(FILE) coming in. |
| 741 | */ |
| 742 | spin_lock(&clp->cl_lock); |
| 743 | BUG_ON(!list_empty(&lo->plh_layouts)); |
| 744 | list_add_tail(&lo->plh_layouts, &clp->cl_layouts); |
| 745 | spin_unlock(&clp->cl_lock); |
| 746 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 747 | spin_unlock(&ino->i_lock); |
| 748 | |
| 749 | lseg = send_layoutget(lo, ctx, iomode); |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 750 | if (!lseg) { |
| 751 | spin_lock(&ino->i_lock); |
| 752 | if (list_empty(&lo->plh_segs)) { |
| 753 | spin_lock(&clp->cl_lock); |
| 754 | list_del_init(&lo->plh_layouts); |
| 755 | spin_unlock(&clp->cl_lock); |
| 756 | } |
| 757 | spin_unlock(&ino->i_lock); |
| 758 | } |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 759 | atomic_dec(&lo->plh_outstanding); |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 760 | put_layout_hdr(lo); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 761 | out: |
| 762 | dprintk("%s end, state 0x%lx lseg %p\n", __func__, |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 763 | nfsi->layout->plh_flags, lseg); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 764 | return lseg; |
| 765 | out_unlock: |
| 766 | spin_unlock(&ino->i_lock); |
| 767 | goto out; |
| 768 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 769 | |
| 770 | int |
| 771 | pnfs_layout_process(struct nfs4_layoutget *lgp) |
| 772 | { |
| 773 | struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout; |
| 774 | struct nfs4_layoutget_res *res = &lgp->res; |
| 775 | struct pnfs_layout_segment *lseg; |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 776 | struct inode *ino = lo->plh_inode; |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 777 | struct nfs_client *clp = NFS_SERVER(ino)->nfs_client; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 778 | int status = 0; |
| 779 | |
Fred Isaman | fc1794c | 2011-01-06 11:36:27 +0000 | [diff] [blame] | 780 | /* Verify we got what we asked for. |
| 781 | * Note that because the xdr parsing only accepts a single |
| 782 | * element array, this can fail even if the server is behaving |
| 783 | * correctly. |
| 784 | */ |
| 785 | if (lgp->args.range.iomode > res->range.iomode || |
| 786 | res->range.offset != 0 || |
| 787 | res->range.length != NFS4_MAX_UINT64) { |
| 788 | status = -EINVAL; |
| 789 | goto out; |
| 790 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 791 | /* Inject layout blob into I/O device driver */ |
| 792 | lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res); |
| 793 | if (!lseg || IS_ERR(lseg)) { |
| 794 | if (!lseg) |
| 795 | status = -ENOMEM; |
| 796 | else |
| 797 | status = PTR_ERR(lseg); |
| 798 | dprintk("%s: Could not allocate layout: error %d\n", |
| 799 | __func__, status); |
| 800 | goto out; |
| 801 | } |
| 802 | |
| 803 | spin_lock(&ino->i_lock); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 804 | if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) || |
| 805 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
| 806 | dprintk("%s forget reply due to recall\n", __func__); |
| 807 | goto out_forget_reply; |
| 808 | } |
| 809 | |
| 810 | if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) { |
| 811 | dprintk("%s forget reply due to state\n", __func__); |
| 812 | goto out_forget_reply; |
| 813 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 814 | init_lseg(lo, lseg); |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 815 | lseg->pls_range = res->range; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 816 | *lgp->lsegpp = lseg; |
| 817 | pnfs_insert_layout(lo, lseg); |
| 818 | |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 819 | if (res->return_on_close) { |
| 820 | set_bit(NFS_LSEG_ROC, &lseg->pls_flags); |
| 821 | set_bit(NFS_LAYOUT_ROC, &lo->plh_flags); |
| 822 | } |
| 823 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 824 | /* Done processing layoutget. Set the layout stateid */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 825 | pnfs_set_layout_stateid(lo, &res->stateid, false); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 826 | spin_unlock(&ino->i_lock); |
| 827 | out: |
| 828 | return status; |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 829 | |
| 830 | out_forget_reply: |
| 831 | spin_unlock(&ino->i_lock); |
| 832 | lseg->pls_layout = lo; |
| 833 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
| 834 | goto out; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /* |
| 838 | * Device ID cache. Currently supports one layout type per struct nfs_client. |
| 839 | * Add layout type to the lookup key to expand to support multiple types. |
| 840 | */ |
| 841 | int |
| 842 | pnfs_alloc_init_deviceid_cache(struct nfs_client *clp, |
| 843 | void (*free_callback)(struct pnfs_deviceid_node *)) |
| 844 | { |
| 845 | struct pnfs_deviceid_cache *c; |
| 846 | |
| 847 | c = kzalloc(sizeof(struct pnfs_deviceid_cache), GFP_KERNEL); |
| 848 | if (!c) |
| 849 | return -ENOMEM; |
| 850 | spin_lock(&clp->cl_lock); |
| 851 | if (clp->cl_devid_cache != NULL) { |
| 852 | atomic_inc(&clp->cl_devid_cache->dc_ref); |
| 853 | dprintk("%s [kref [%d]]\n", __func__, |
| 854 | atomic_read(&clp->cl_devid_cache->dc_ref)); |
| 855 | kfree(c); |
| 856 | } else { |
| 857 | /* kzalloc initializes hlists */ |
| 858 | spin_lock_init(&c->dc_lock); |
| 859 | atomic_set(&c->dc_ref, 1); |
| 860 | c->dc_free_callback = free_callback; |
| 861 | clp->cl_devid_cache = c; |
| 862 | dprintk("%s [new]\n", __func__); |
| 863 | } |
| 864 | spin_unlock(&clp->cl_lock); |
| 865 | return 0; |
| 866 | } |
| 867 | EXPORT_SYMBOL_GPL(pnfs_alloc_init_deviceid_cache); |
| 868 | |
| 869 | /* |
| 870 | * Called from pnfs_layoutdriver_type->free_lseg |
| 871 | * last layout segment reference frees deviceid |
| 872 | */ |
| 873 | void |
| 874 | pnfs_put_deviceid(struct pnfs_deviceid_cache *c, |
| 875 | struct pnfs_deviceid_node *devid) |
| 876 | { |
| 877 | struct nfs4_deviceid *id = &devid->de_id; |
| 878 | struct pnfs_deviceid_node *d; |
| 879 | struct hlist_node *n; |
| 880 | long h = nfs4_deviceid_hash(id); |
| 881 | |
| 882 | dprintk("%s [%d]\n", __func__, atomic_read(&devid->de_ref)); |
| 883 | if (!atomic_dec_and_lock(&devid->de_ref, &c->dc_lock)) |
| 884 | return; |
| 885 | |
| 886 | hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[h], de_node) |
| 887 | if (!memcmp(&d->de_id, id, sizeof(*id))) { |
| 888 | hlist_del_rcu(&d->de_node); |
| 889 | spin_unlock(&c->dc_lock); |
| 890 | synchronize_rcu(); |
| 891 | c->dc_free_callback(devid); |
| 892 | return; |
| 893 | } |
| 894 | spin_unlock(&c->dc_lock); |
| 895 | /* Why wasn't it found in the list? */ |
| 896 | BUG(); |
| 897 | } |
| 898 | EXPORT_SYMBOL_GPL(pnfs_put_deviceid); |
| 899 | |
| 900 | /* Find and reference a deviceid */ |
| 901 | struct pnfs_deviceid_node * |
| 902 | pnfs_find_get_deviceid(struct pnfs_deviceid_cache *c, struct nfs4_deviceid *id) |
| 903 | { |
| 904 | struct pnfs_deviceid_node *d; |
| 905 | struct hlist_node *n; |
| 906 | long hash = nfs4_deviceid_hash(id); |
| 907 | |
| 908 | dprintk("--> %s hash %ld\n", __func__, hash); |
| 909 | rcu_read_lock(); |
| 910 | hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[hash], de_node) { |
| 911 | if (!memcmp(&d->de_id, id, sizeof(*id))) { |
| 912 | if (!atomic_inc_not_zero(&d->de_ref)) { |
| 913 | goto fail; |
| 914 | } else { |
| 915 | rcu_read_unlock(); |
| 916 | return d; |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | fail: |
| 921 | rcu_read_unlock(); |
| 922 | return NULL; |
| 923 | } |
| 924 | EXPORT_SYMBOL_GPL(pnfs_find_get_deviceid); |
| 925 | |
| 926 | /* |
| 927 | * Add a deviceid to the cache. |
| 928 | * GETDEVICEINFOs for same deviceid can race. If deviceid is found, discard new |
| 929 | */ |
| 930 | struct pnfs_deviceid_node * |
| 931 | pnfs_add_deviceid(struct pnfs_deviceid_cache *c, struct pnfs_deviceid_node *new) |
| 932 | { |
| 933 | struct pnfs_deviceid_node *d; |
| 934 | long hash = nfs4_deviceid_hash(&new->de_id); |
| 935 | |
| 936 | dprintk("--> %s hash %ld\n", __func__, hash); |
| 937 | spin_lock(&c->dc_lock); |
| 938 | d = pnfs_find_get_deviceid(c, &new->de_id); |
| 939 | if (d) { |
| 940 | spin_unlock(&c->dc_lock); |
| 941 | dprintk("%s [discard]\n", __func__); |
| 942 | c->dc_free_callback(new); |
| 943 | return d; |
| 944 | } |
| 945 | INIT_HLIST_NODE(&new->de_node); |
| 946 | atomic_set(&new->de_ref, 1); |
| 947 | hlist_add_head_rcu(&new->de_node, &c->dc_deviceids[hash]); |
| 948 | spin_unlock(&c->dc_lock); |
| 949 | dprintk("%s [new]\n", __func__); |
| 950 | return new; |
| 951 | } |
| 952 | EXPORT_SYMBOL_GPL(pnfs_add_deviceid); |
| 953 | |
| 954 | void |
| 955 | pnfs_put_deviceid_cache(struct nfs_client *clp) |
| 956 | { |
| 957 | struct pnfs_deviceid_cache *local = clp->cl_devid_cache; |
| 958 | |
Andy Adamson | b2a2897 | 2011-01-25 15:38:03 +0000 | [diff] [blame] | 959 | dprintk("--> %s ({%d})\n", __func__, atomic_read(&local->dc_ref)); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 960 | if (atomic_dec_and_lock(&local->dc_ref, &clp->cl_lock)) { |
| 961 | int i; |
| 962 | /* Verify cache is empty */ |
| 963 | for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i++) |
| 964 | BUG_ON(!hlist_empty(&local->dc_deviceids[i])); |
| 965 | clp->cl_devid_cache = NULL; |
| 966 | spin_unlock(&clp->cl_lock); |
| 967 | kfree(local); |
| 968 | } |
| 969 | } |
| 970 | EXPORT_SYMBOL_GPL(pnfs_put_deviceid_cache); |