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