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