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)) { |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 250 | set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags); |
| 251 | /* Matched by initial refcount set in alloc_init_layout_hdr */ |
| 252 | put_layout_hdr_locked(lseg->pls_layout); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 253 | } |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 254 | rpc_wake_up(&NFS_SERVER(ino)->roc_rpcwaitq); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 255 | list_add(&lseg->pls_list, tmp_list); |
| 256 | return 1; |
| 257 | } |
| 258 | return 0; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 259 | } |
| 260 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 261 | static bool |
| 262 | should_free_lseg(u32 lseg_iomode, u32 recall_iomode) |
| 263 | { |
| 264 | return (recall_iomode == IOMODE_ANY || |
| 265 | lseg_iomode == recall_iomode); |
| 266 | } |
| 267 | |
| 268 | /* Returns 1 if lseg is removed from list, 0 otherwise */ |
| 269 | static int mark_lseg_invalid(struct pnfs_layout_segment *lseg, |
| 270 | struct list_head *tmp_list) |
| 271 | { |
| 272 | int rv = 0; |
| 273 | |
| 274 | if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) { |
| 275 | /* Remove the reference keeping the lseg in the |
| 276 | * list. It will now be removed when all |
| 277 | * outstanding io is finished. |
| 278 | */ |
| 279 | rv = put_lseg_locked(lseg, tmp_list); |
| 280 | } |
| 281 | return rv; |
| 282 | } |
| 283 | |
| 284 | /* Returns count of number of matching invalid lsegs remaining in list |
| 285 | * after call. |
| 286 | */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 287 | int |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 288 | mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo, |
| 289 | struct list_head *tmp_list, |
| 290 | u32 iomode) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 291 | { |
| 292 | struct pnfs_layout_segment *lseg, *next; |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 293 | int invalid = 0, removed = 0; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 294 | |
| 295 | dprintk("%s:Begin lo %p\n", __func__, lo); |
| 296 | |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 297 | if (list_empty(&lo->plh_segs)) { |
| 298 | if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) |
| 299 | put_layout_hdr_locked(lo); |
| 300 | return 0; |
| 301 | } |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 302 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) |
| 303 | if (should_free_lseg(lseg->pls_range.iomode, iomode)) { |
| 304 | dprintk("%s: freeing lseg %p iomode %d " |
| 305 | "offset %llu length %llu\n", __func__, |
| 306 | lseg, lseg->pls_range.iomode, lseg->pls_range.offset, |
| 307 | lseg->pls_range.length); |
| 308 | invalid++; |
| 309 | removed += mark_lseg_invalid(lseg, tmp_list); |
| 310 | } |
| 311 | dprintk("%s:Return %i\n", __func__, invalid - removed); |
| 312 | return invalid - removed; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 313 | } |
| 314 | |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame^] | 315 | /* note free_me must contain lsegs from a single layout_hdr */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 316 | void |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 317 | pnfs_free_lseg_list(struct list_head *free_me) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 318 | { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 319 | struct pnfs_layout_segment *lseg, *tmp; |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame^] | 320 | struct pnfs_layout_hdr *lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 321 | |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame^] | 322 | if (list_empty(free_me)) |
| 323 | return; |
| 324 | |
| 325 | lo = list_first_entry(free_me, struct pnfs_layout_segment, |
| 326 | pls_list)->pls_layout; |
| 327 | |
| 328 | if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) { |
| 329 | struct nfs_client *clp; |
| 330 | |
| 331 | clp = NFS_SERVER(lo->plh_inode)->nfs_client; |
| 332 | spin_lock(&clp->cl_lock); |
| 333 | list_del_init(&lo->plh_layouts); |
| 334 | spin_unlock(&clp->cl_lock); |
| 335 | } |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 336 | list_for_each_entry_safe(lseg, tmp, free_me, pls_list) { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 337 | list_del(&lseg->pls_list); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 338 | free_lseg(lseg); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 342 | void |
| 343 | pnfs_destroy_layout(struct nfs_inode *nfsi) |
| 344 | { |
| 345 | struct pnfs_layout_hdr *lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 346 | LIST_HEAD(tmp_list); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 347 | |
| 348 | spin_lock(&nfsi->vfs_inode.i_lock); |
| 349 | lo = nfsi->layout; |
| 350 | if (lo) { |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 351 | lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */ |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 352 | mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 353 | } |
| 354 | spin_unlock(&nfsi->vfs_inode.i_lock); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 355 | pnfs_free_lseg_list(&tmp_list); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 356 | } |
| 357 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 358 | /* |
| 359 | * Called by the state manger to remove all layouts established under an |
| 360 | * expired lease. |
| 361 | */ |
| 362 | void |
| 363 | pnfs_destroy_all_layouts(struct nfs_client *clp) |
| 364 | { |
| 365 | struct pnfs_layout_hdr *lo; |
| 366 | LIST_HEAD(tmp_list); |
| 367 | |
| 368 | spin_lock(&clp->cl_lock); |
| 369 | list_splice_init(&clp->cl_layouts, &tmp_list); |
| 370 | spin_unlock(&clp->cl_lock); |
| 371 | |
| 372 | while (!list_empty(&tmp_list)) { |
| 373 | lo = list_entry(tmp_list.next, struct pnfs_layout_hdr, |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 374 | plh_layouts); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 375 | dprintk("%s freeing layout for inode %lu\n", __func__, |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 376 | lo->plh_inode->i_ino); |
| 377 | pnfs_destroy_layout(NFS_I(lo->plh_inode)); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 381 | /* update lo->plh_stateid with new if is more recent */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 382 | void |
| 383 | pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new, |
| 384 | bool update_barrier) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 385 | { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 386 | u32 oldseq, newseq; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 387 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 388 | oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid); |
| 389 | newseq = be32_to_cpu(new->stateid.seqid); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 390 | if ((int)(newseq - oldseq) > 0) { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 391 | memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid)); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 392 | if (update_barrier) { |
| 393 | u32 new_barrier = be32_to_cpu(new->stateid.seqid); |
| 394 | |
| 395 | if ((int)(new_barrier - lo->plh_barrier)) |
| 396 | lo->plh_barrier = new_barrier; |
| 397 | } else { |
| 398 | /* Because of wraparound, we want to keep the barrier |
| 399 | * "close" to the current seqids. It needs to be |
| 400 | * within 2**31 to count as "behind", so if it |
| 401 | * gets too near that limit, give us a litle leeway |
| 402 | * and bring it to within 2**30. |
| 403 | * NOTE - and yes, this is all unsigned arithmetic. |
| 404 | */ |
| 405 | if (unlikely((newseq - lo->plh_barrier) > (3 << 29))) |
| 406 | lo->plh_barrier = newseq - (1 << 30); |
| 407 | } |
| 408 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 409 | } |
| 410 | |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 411 | /* lget is set to 1 if called from inside send_layoutget call chain */ |
| 412 | static bool |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 413 | pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid, |
| 414 | int lget) |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 415 | { |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 416 | if ((stateid) && |
| 417 | (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0) |
| 418 | return true; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 419 | return lo->plh_block_lgets || |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 420 | test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) || |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 421 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) || |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 422 | (list_empty(&lo->plh_segs) && |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 423 | (atomic_read(&lo->plh_outstanding) > lget)); |
| 424 | } |
| 425 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 426 | int |
| 427 | pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo, |
| 428 | struct nfs4_state *open_state) |
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 | int status = 0; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 431 | |
| 432 | dprintk("--> %s\n", __func__); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 433 | spin_lock(&lo->plh_inode->i_lock); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 434 | if (pnfs_layoutgets_blocked(lo, NULL, 1)) { |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 435 | status = -EAGAIN; |
| 436 | } else if (list_empty(&lo->plh_segs)) { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 437 | int seq; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 438 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 439 | do { |
| 440 | seq = read_seqbegin(&open_state->seqlock); |
| 441 | memcpy(dst->data, open_state->stateid.data, |
| 442 | sizeof(open_state->stateid.data)); |
| 443 | } while (read_seqretry(&open_state->seqlock, seq)); |
| 444 | } else |
| 445 | memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data)); |
| 446 | spin_unlock(&lo->plh_inode->i_lock); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 447 | dprintk("<-- %s\n", __func__); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 448 | return status; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Get layout from server. |
| 453 | * for now, assume that whole file layouts are requested. |
| 454 | * arg->offset: 0 |
| 455 | * arg->length: all ones |
| 456 | */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 457 | static struct pnfs_layout_segment * |
| 458 | send_layoutget(struct pnfs_layout_hdr *lo, |
| 459 | struct nfs_open_context *ctx, |
| 460 | u32 iomode) |
| 461 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 462 | struct inode *ino = lo->plh_inode; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 463 | struct nfs_server *server = NFS_SERVER(ino); |
| 464 | struct nfs4_layoutget *lgp; |
| 465 | struct pnfs_layout_segment *lseg = NULL; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 466 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 467 | dprintk("--> %s\n", __func__); |
| 468 | |
| 469 | BUG_ON(ctx == NULL); |
| 470 | lgp = kzalloc(sizeof(*lgp), GFP_KERNEL); |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 471 | if (lgp == NULL) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 472 | return NULL; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 473 | lgp->args.minlength = NFS4_MAX_UINT64; |
| 474 | lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE; |
| 475 | lgp->args.range.iomode = iomode; |
| 476 | lgp->args.range.offset = 0; |
| 477 | lgp->args.range.length = NFS4_MAX_UINT64; |
| 478 | lgp->args.type = server->pnfs_curr_ld->id; |
| 479 | lgp->args.inode = ino; |
| 480 | lgp->args.ctx = get_nfs_open_context(ctx); |
| 481 | lgp->lsegpp = &lseg; |
| 482 | |
| 483 | /* Synchronously retrieve layout information from server and |
| 484 | * store in lseg. |
| 485 | */ |
| 486 | nfs4_proc_layoutget(lgp); |
| 487 | if (!lseg) { |
| 488 | /* remember that LAYOUTGET failed and suspend trying */ |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 489 | set_bit(lo_fail_bit(iomode), &lo->plh_flags); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 490 | } |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 491 | return lseg; |
| 492 | } |
| 493 | |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 494 | bool pnfs_roc(struct inode *ino) |
| 495 | { |
| 496 | struct pnfs_layout_hdr *lo; |
| 497 | struct pnfs_layout_segment *lseg, *tmp; |
| 498 | LIST_HEAD(tmp_list); |
| 499 | bool found = false; |
| 500 | |
| 501 | spin_lock(&ino->i_lock); |
| 502 | lo = NFS_I(ino)->layout; |
| 503 | if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) || |
| 504 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) |
| 505 | goto out_nolayout; |
| 506 | list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list) |
| 507 | if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { |
| 508 | mark_lseg_invalid(lseg, &tmp_list); |
| 509 | found = true; |
| 510 | } |
| 511 | if (!found) |
| 512 | goto out_nolayout; |
| 513 | lo->plh_block_lgets++; |
| 514 | get_layout_hdr(lo); /* matched in pnfs_roc_release */ |
| 515 | spin_unlock(&ino->i_lock); |
| 516 | pnfs_free_lseg_list(&tmp_list); |
| 517 | return true; |
| 518 | |
| 519 | out_nolayout: |
| 520 | spin_unlock(&ino->i_lock); |
| 521 | return false; |
| 522 | } |
| 523 | |
| 524 | void pnfs_roc_release(struct inode *ino) |
| 525 | { |
| 526 | struct pnfs_layout_hdr *lo; |
| 527 | |
| 528 | spin_lock(&ino->i_lock); |
| 529 | lo = NFS_I(ino)->layout; |
| 530 | lo->plh_block_lgets--; |
| 531 | put_layout_hdr_locked(lo); |
| 532 | spin_unlock(&ino->i_lock); |
| 533 | } |
| 534 | |
| 535 | void pnfs_roc_set_barrier(struct inode *ino, u32 barrier) |
| 536 | { |
| 537 | struct pnfs_layout_hdr *lo; |
| 538 | |
| 539 | spin_lock(&ino->i_lock); |
| 540 | lo = NFS_I(ino)->layout; |
| 541 | if ((int)(barrier - lo->plh_barrier) > 0) |
| 542 | lo->plh_barrier = barrier; |
| 543 | spin_unlock(&ino->i_lock); |
| 544 | } |
| 545 | |
| 546 | bool pnfs_roc_drain(struct inode *ino, u32 *barrier) |
| 547 | { |
| 548 | struct nfs_inode *nfsi = NFS_I(ino); |
| 549 | struct pnfs_layout_segment *lseg; |
| 550 | bool found = false; |
| 551 | |
| 552 | spin_lock(&ino->i_lock); |
| 553 | list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list) |
| 554 | if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { |
| 555 | found = true; |
| 556 | break; |
| 557 | } |
| 558 | if (!found) { |
| 559 | struct pnfs_layout_hdr *lo = nfsi->layout; |
| 560 | u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid); |
| 561 | |
| 562 | /* Since close does not return a layout stateid for use as |
| 563 | * a barrier, we choose the worst-case barrier. |
| 564 | */ |
| 565 | *barrier = current_seqid + atomic_read(&lo->plh_outstanding); |
| 566 | } |
| 567 | spin_unlock(&ino->i_lock); |
| 568 | return found; |
| 569 | } |
| 570 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 571 | /* |
| 572 | * Compare two layout segments for sorting into layout cache. |
| 573 | * We want to preferentially return RW over RO layouts, so ensure those |
| 574 | * are seen first. |
| 575 | */ |
| 576 | static s64 |
| 577 | cmp_layout(u32 iomode1, u32 iomode2) |
| 578 | { |
| 579 | /* read > read/write */ |
| 580 | return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ); |
| 581 | } |
| 582 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 583 | static void |
| 584 | pnfs_insert_layout(struct pnfs_layout_hdr *lo, |
| 585 | struct pnfs_layout_segment *lseg) |
| 586 | { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 587 | struct pnfs_layout_segment *lp; |
| 588 | int found = 0; |
| 589 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 590 | dprintk("%s:Begin\n", __func__); |
| 591 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 592 | assert_spin_locked(&lo->plh_inode->i_lock); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 593 | list_for_each_entry(lp, &lo->plh_segs, pls_list) { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 594 | if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 595 | continue; |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 596 | list_add_tail(&lseg->pls_list, &lp->pls_list); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 597 | dprintk("%s: inserted lseg %p " |
| 598 | "iomode %d offset %llu length %llu before " |
| 599 | "lp %p iomode %d offset %llu length %llu\n", |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 600 | __func__, lseg, lseg->pls_range.iomode, |
| 601 | lseg->pls_range.offset, lseg->pls_range.length, |
| 602 | lp, lp->pls_range.iomode, lp->pls_range.offset, |
| 603 | lp->pls_range.length); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 604 | found = 1; |
| 605 | break; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 606 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 607 | if (!found) { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 608 | list_add_tail(&lseg->pls_list, &lo->plh_segs); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 609 | dprintk("%s: inserted lseg %p " |
| 610 | "iomode %d offset %llu length %llu at tail\n", |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 611 | __func__, lseg, lseg->pls_range.iomode, |
| 612 | lseg->pls_range.offset, lseg->pls_range.length); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 613 | } |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 614 | get_layout_hdr(lo); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 615 | |
| 616 | dprintk("%s:Return\n", __func__); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | static struct pnfs_layout_hdr * |
| 620 | alloc_init_layout_hdr(struct inode *ino) |
| 621 | { |
| 622 | struct pnfs_layout_hdr *lo; |
| 623 | |
| 624 | lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL); |
| 625 | if (!lo) |
| 626 | return NULL; |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 627 | atomic_set(&lo->plh_refcount, 1); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 628 | INIT_LIST_HEAD(&lo->plh_layouts); |
| 629 | INIT_LIST_HEAD(&lo->plh_segs); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 630 | INIT_LIST_HEAD(&lo->plh_bulk_recall); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 631 | lo->plh_inode = ino; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 632 | return lo; |
| 633 | } |
| 634 | |
| 635 | static struct pnfs_layout_hdr * |
| 636 | pnfs_find_alloc_layout(struct inode *ino) |
| 637 | { |
| 638 | struct nfs_inode *nfsi = NFS_I(ino); |
| 639 | struct pnfs_layout_hdr *new = NULL; |
| 640 | |
| 641 | dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout); |
| 642 | |
| 643 | assert_spin_locked(&ino->i_lock); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 644 | if (nfsi->layout) { |
| 645 | if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags)) |
| 646 | return NULL; |
| 647 | else |
| 648 | return nfsi->layout; |
| 649 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 650 | spin_unlock(&ino->i_lock); |
| 651 | new = alloc_init_layout_hdr(ino); |
| 652 | spin_lock(&ino->i_lock); |
| 653 | |
| 654 | if (likely(nfsi->layout == NULL)) /* Won the race? */ |
| 655 | nfsi->layout = new; |
| 656 | else |
| 657 | kfree(new); |
| 658 | return nfsi->layout; |
| 659 | } |
| 660 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 661 | /* |
| 662 | * iomode matching rules: |
| 663 | * iomode lseg match |
| 664 | * ----- ----- ----- |
| 665 | * ANY READ true |
| 666 | * ANY RW true |
| 667 | * RW READ false |
| 668 | * RW RW true |
| 669 | * READ READ true |
| 670 | * READ RW true |
| 671 | */ |
| 672 | static int |
| 673 | is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode) |
| 674 | { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 675 | return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /* |
| 679 | * lookup range in layout |
| 680 | */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 681 | static struct pnfs_layout_segment * |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 682 | pnfs_find_lseg(struct pnfs_layout_hdr *lo, u32 iomode) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 683 | { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 684 | struct pnfs_layout_segment *lseg, *ret = NULL; |
| 685 | |
| 686 | dprintk("%s:Begin\n", __func__); |
| 687 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 688 | assert_spin_locked(&lo->plh_inode->i_lock); |
| 689 | list_for_each_entry(lseg, &lo->plh_segs, pls_list) { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 690 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) && |
| 691 | is_matching_lseg(lseg, iomode)) { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 692 | ret = lseg; |
| 693 | break; |
| 694 | } |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 695 | if (cmp_layout(iomode, lseg->pls_range.iomode) > 0) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 696 | break; |
| 697 | } |
| 698 | |
| 699 | dprintk("%s:Return lseg %p ref %d\n", |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 700 | __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 701 | return ret; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Layout segment is retreived from the server if not cached. |
| 706 | * The appropriate layout segment is referenced and returned to the caller. |
| 707 | */ |
| 708 | struct pnfs_layout_segment * |
| 709 | pnfs_update_layout(struct inode *ino, |
| 710 | struct nfs_open_context *ctx, |
| 711 | enum pnfs_iomode iomode) |
| 712 | { |
| 713 | struct nfs_inode *nfsi = NFS_I(ino); |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 714 | struct nfs_client *clp = NFS_SERVER(ino)->nfs_client; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 715 | struct pnfs_layout_hdr *lo; |
| 716 | struct pnfs_layout_segment *lseg = NULL; |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame^] | 717 | bool first = false; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 718 | |
| 719 | if (!pnfs_enabled_sb(NFS_SERVER(ino))) |
| 720 | return NULL; |
| 721 | spin_lock(&ino->i_lock); |
| 722 | lo = pnfs_find_alloc_layout(ino); |
| 723 | if (lo == NULL) { |
| 724 | dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__); |
| 725 | goto out_unlock; |
| 726 | } |
| 727 | |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 728 | /* Do we even need to bother with this? */ |
| 729 | if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) || |
| 730 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
| 731 | dprintk("%s matches recall, use MDS\n", __func__); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 732 | goto out_unlock; |
| 733 | } |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 734 | /* Check to see if the layout for the given range already exists */ |
| 735 | lseg = pnfs_find_lseg(lo, iomode); |
| 736 | if (lseg) |
| 737 | goto out_unlock; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 738 | |
| 739 | /* if LAYOUTGET already failed once we don't try again */ |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 740 | if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags)) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 741 | goto out_unlock; |
| 742 | |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 743 | if (pnfs_layoutgets_blocked(lo, NULL, 0)) |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 744 | goto out_unlock; |
| 745 | atomic_inc(&lo->plh_outstanding); |
| 746 | |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 747 | get_layout_hdr(lo); |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame^] | 748 | if (list_empty(&lo->plh_segs)) |
| 749 | first = true; |
| 750 | spin_unlock(&ino->i_lock); |
| 751 | if (first) { |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 752 | /* The lo must be on the clp list if there is any |
| 753 | * chance of a CB_LAYOUTRECALL(FILE) coming in. |
| 754 | */ |
| 755 | spin_lock(&clp->cl_lock); |
| 756 | BUG_ON(!list_empty(&lo->plh_layouts)); |
| 757 | list_add_tail(&lo->plh_layouts, &clp->cl_layouts); |
| 758 | spin_unlock(&clp->cl_lock); |
| 759 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 760 | |
| 761 | lseg = send_layoutget(lo, ctx, iomode); |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame^] | 762 | if (!lseg && first) { |
| 763 | spin_lock(&clp->cl_lock); |
| 764 | list_del_init(&lo->plh_layouts); |
| 765 | spin_unlock(&clp->cl_lock); |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 766 | } |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 767 | atomic_dec(&lo->plh_outstanding); |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 768 | put_layout_hdr(lo); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 769 | out: |
| 770 | dprintk("%s end, state 0x%lx lseg %p\n", __func__, |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 771 | nfsi->layout->plh_flags, lseg); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 772 | return lseg; |
| 773 | out_unlock: |
| 774 | spin_unlock(&ino->i_lock); |
| 775 | goto out; |
| 776 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 777 | |
| 778 | int |
| 779 | pnfs_layout_process(struct nfs4_layoutget *lgp) |
| 780 | { |
| 781 | struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout; |
| 782 | struct nfs4_layoutget_res *res = &lgp->res; |
| 783 | struct pnfs_layout_segment *lseg; |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 784 | struct inode *ino = lo->plh_inode; |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 785 | struct nfs_client *clp = NFS_SERVER(ino)->nfs_client; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 786 | int status = 0; |
| 787 | |
Fred Isaman | fc1794c | 2011-01-06 11:36:27 +0000 | [diff] [blame] | 788 | /* Verify we got what we asked for. |
| 789 | * Note that because the xdr parsing only accepts a single |
| 790 | * element array, this can fail even if the server is behaving |
| 791 | * correctly. |
| 792 | */ |
| 793 | if (lgp->args.range.iomode > res->range.iomode || |
| 794 | res->range.offset != 0 || |
| 795 | res->range.length != NFS4_MAX_UINT64) { |
| 796 | status = -EINVAL; |
| 797 | goto out; |
| 798 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 799 | /* Inject layout blob into I/O device driver */ |
| 800 | lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res); |
| 801 | if (!lseg || IS_ERR(lseg)) { |
| 802 | if (!lseg) |
| 803 | status = -ENOMEM; |
| 804 | else |
| 805 | status = PTR_ERR(lseg); |
| 806 | dprintk("%s: Could not allocate layout: error %d\n", |
| 807 | __func__, status); |
| 808 | goto out; |
| 809 | } |
| 810 | |
| 811 | spin_lock(&ino->i_lock); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 812 | if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) || |
| 813 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
| 814 | dprintk("%s forget reply due to recall\n", __func__); |
| 815 | goto out_forget_reply; |
| 816 | } |
| 817 | |
| 818 | if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) { |
| 819 | dprintk("%s forget reply due to state\n", __func__); |
| 820 | goto out_forget_reply; |
| 821 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 822 | init_lseg(lo, lseg); |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 823 | lseg->pls_range = res->range; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 824 | *lgp->lsegpp = lseg; |
| 825 | pnfs_insert_layout(lo, lseg); |
| 826 | |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 827 | if (res->return_on_close) { |
| 828 | set_bit(NFS_LSEG_ROC, &lseg->pls_flags); |
| 829 | set_bit(NFS_LAYOUT_ROC, &lo->plh_flags); |
| 830 | } |
| 831 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 832 | /* Done processing layoutget. Set the layout stateid */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 833 | pnfs_set_layout_stateid(lo, &res->stateid, false); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 834 | spin_unlock(&ino->i_lock); |
| 835 | out: |
| 836 | return status; |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 837 | |
| 838 | out_forget_reply: |
| 839 | spin_unlock(&ino->i_lock); |
| 840 | lseg->pls_layout = lo; |
| 841 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
| 842 | goto out; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /* |
| 846 | * Device ID cache. Currently supports one layout type per struct nfs_client. |
| 847 | * Add layout type to the lookup key to expand to support multiple types. |
| 848 | */ |
| 849 | int |
| 850 | pnfs_alloc_init_deviceid_cache(struct nfs_client *clp, |
| 851 | void (*free_callback)(struct pnfs_deviceid_node *)) |
| 852 | { |
| 853 | struct pnfs_deviceid_cache *c; |
| 854 | |
| 855 | c = kzalloc(sizeof(struct pnfs_deviceid_cache), GFP_KERNEL); |
| 856 | if (!c) |
| 857 | return -ENOMEM; |
| 858 | spin_lock(&clp->cl_lock); |
| 859 | if (clp->cl_devid_cache != NULL) { |
| 860 | atomic_inc(&clp->cl_devid_cache->dc_ref); |
| 861 | dprintk("%s [kref [%d]]\n", __func__, |
| 862 | atomic_read(&clp->cl_devid_cache->dc_ref)); |
| 863 | kfree(c); |
| 864 | } else { |
| 865 | /* kzalloc initializes hlists */ |
| 866 | spin_lock_init(&c->dc_lock); |
| 867 | atomic_set(&c->dc_ref, 1); |
| 868 | c->dc_free_callback = free_callback; |
| 869 | clp->cl_devid_cache = c; |
| 870 | dprintk("%s [new]\n", __func__); |
| 871 | } |
| 872 | spin_unlock(&clp->cl_lock); |
| 873 | return 0; |
| 874 | } |
| 875 | EXPORT_SYMBOL_GPL(pnfs_alloc_init_deviceid_cache); |
| 876 | |
| 877 | /* |
| 878 | * Called from pnfs_layoutdriver_type->free_lseg |
| 879 | * last layout segment reference frees deviceid |
| 880 | */ |
| 881 | void |
| 882 | pnfs_put_deviceid(struct pnfs_deviceid_cache *c, |
| 883 | struct pnfs_deviceid_node *devid) |
| 884 | { |
| 885 | struct nfs4_deviceid *id = &devid->de_id; |
| 886 | struct pnfs_deviceid_node *d; |
| 887 | struct hlist_node *n; |
| 888 | long h = nfs4_deviceid_hash(id); |
| 889 | |
| 890 | dprintk("%s [%d]\n", __func__, atomic_read(&devid->de_ref)); |
| 891 | if (!atomic_dec_and_lock(&devid->de_ref, &c->dc_lock)) |
| 892 | return; |
| 893 | |
| 894 | hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[h], de_node) |
| 895 | if (!memcmp(&d->de_id, id, sizeof(*id))) { |
| 896 | hlist_del_rcu(&d->de_node); |
| 897 | spin_unlock(&c->dc_lock); |
| 898 | synchronize_rcu(); |
| 899 | c->dc_free_callback(devid); |
| 900 | return; |
| 901 | } |
| 902 | spin_unlock(&c->dc_lock); |
| 903 | /* Why wasn't it found in the list? */ |
| 904 | BUG(); |
| 905 | } |
| 906 | EXPORT_SYMBOL_GPL(pnfs_put_deviceid); |
| 907 | |
| 908 | /* Find and reference a deviceid */ |
| 909 | struct pnfs_deviceid_node * |
| 910 | pnfs_find_get_deviceid(struct pnfs_deviceid_cache *c, struct nfs4_deviceid *id) |
| 911 | { |
| 912 | struct pnfs_deviceid_node *d; |
| 913 | struct hlist_node *n; |
| 914 | long hash = nfs4_deviceid_hash(id); |
| 915 | |
| 916 | dprintk("--> %s hash %ld\n", __func__, hash); |
| 917 | rcu_read_lock(); |
| 918 | hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[hash], de_node) { |
| 919 | if (!memcmp(&d->de_id, id, sizeof(*id))) { |
| 920 | if (!atomic_inc_not_zero(&d->de_ref)) { |
| 921 | goto fail; |
| 922 | } else { |
| 923 | rcu_read_unlock(); |
| 924 | return d; |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | fail: |
| 929 | rcu_read_unlock(); |
| 930 | return NULL; |
| 931 | } |
| 932 | EXPORT_SYMBOL_GPL(pnfs_find_get_deviceid); |
| 933 | |
| 934 | /* |
| 935 | * Add a deviceid to the cache. |
| 936 | * GETDEVICEINFOs for same deviceid can race. If deviceid is found, discard new |
| 937 | */ |
| 938 | struct pnfs_deviceid_node * |
| 939 | pnfs_add_deviceid(struct pnfs_deviceid_cache *c, struct pnfs_deviceid_node *new) |
| 940 | { |
| 941 | struct pnfs_deviceid_node *d; |
| 942 | long hash = nfs4_deviceid_hash(&new->de_id); |
| 943 | |
| 944 | dprintk("--> %s hash %ld\n", __func__, hash); |
| 945 | spin_lock(&c->dc_lock); |
| 946 | d = pnfs_find_get_deviceid(c, &new->de_id); |
| 947 | if (d) { |
| 948 | spin_unlock(&c->dc_lock); |
| 949 | dprintk("%s [discard]\n", __func__); |
| 950 | c->dc_free_callback(new); |
| 951 | return d; |
| 952 | } |
| 953 | INIT_HLIST_NODE(&new->de_node); |
| 954 | atomic_set(&new->de_ref, 1); |
| 955 | hlist_add_head_rcu(&new->de_node, &c->dc_deviceids[hash]); |
| 956 | spin_unlock(&c->dc_lock); |
| 957 | dprintk("%s [new]\n", __func__); |
| 958 | return new; |
| 959 | } |
| 960 | EXPORT_SYMBOL_GPL(pnfs_add_deviceid); |
| 961 | |
| 962 | void |
| 963 | pnfs_put_deviceid_cache(struct nfs_client *clp) |
| 964 | { |
| 965 | struct pnfs_deviceid_cache *local = clp->cl_devid_cache; |
| 966 | |
Andy Adamson | b2a2897 | 2011-01-25 15:38:03 +0000 | [diff] [blame] | 967 | dprintk("--> %s ({%d})\n", __func__, atomic_read(&local->dc_ref)); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 968 | if (atomic_dec_and_lock(&local->dc_ref, &clp->cl_lock)) { |
| 969 | int i; |
| 970 | /* Verify cache is empty */ |
| 971 | for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i++) |
| 972 | BUG_ON(!hlist_empty(&local->dc_deviceids[i])); |
| 973 | clp->cl_devid_cache = NULL; |
| 974 | spin_unlock(&clp->cl_lock); |
| 975 | kfree(local); |
| 976 | } |
| 977 | } |
| 978 | EXPORT_SYMBOL_GPL(pnfs_put_deviceid_cache); |