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 | |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 180 | static void |
| 181 | get_layout_hdr_locked(struct pnfs_layout_hdr *lo) |
| 182 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 183 | assert_spin_locked(&lo->plh_inode->i_lock); |
| 184 | lo->plh_refcount++; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static void |
| 188 | put_layout_hdr_locked(struct pnfs_layout_hdr *lo) |
| 189 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 190 | assert_spin_locked(&lo->plh_inode->i_lock); |
| 191 | BUG_ON(lo->plh_refcount == 0); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 192 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 193 | lo->plh_refcount--; |
| 194 | if (!lo->plh_refcount) { |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 195 | dprintk("%s: freeing layout cache %p\n", __func__, lo); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 196 | BUG_ON(!list_empty(&lo->plh_layouts)); |
| 197 | NFS_I(lo->plh_inode)->layout = NULL; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 198 | kfree(lo); |
| 199 | } |
| 200 | } |
| 201 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 202 | void |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 203 | put_layout_hdr(struct inode *inode) |
| 204 | { |
| 205 | spin_lock(&inode->i_lock); |
| 206 | put_layout_hdr_locked(NFS_I(inode)->layout); |
| 207 | spin_unlock(&inode->i_lock); |
| 208 | } |
| 209 | |
| 210 | static void |
| 211 | init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg) |
| 212 | { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 213 | INIT_LIST_HEAD(&lseg->pls_list); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 214 | atomic_set(&lseg->pls_refcount, 1); |
| 215 | smp_mb(); |
| 216 | set_bit(NFS_LSEG_VALID, &lseg->pls_flags); |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 217 | lseg->pls_layout = lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 220 | static void free_lseg(struct pnfs_layout_segment *lseg) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 221 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 222 | struct inode *ino = lseg->pls_layout->plh_inode; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 223 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 224 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
Fred Isaman | 52fabd7 | 2011-01-06 11:36:18 +0000 | [diff] [blame] | 225 | /* Matched by get_layout_hdr in pnfs_insert_layout */ |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 226 | put_layout_hdr(ino); |
| 227 | } |
| 228 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 229 | /* The use of tmp_list is necessary because pnfs_curr_ld->free_lseg |
| 230 | * could sleep, so must be called outside of the lock. |
| 231 | * Returns 1 if object was removed, otherwise return 0. |
| 232 | */ |
| 233 | static int |
| 234 | put_lseg_locked(struct pnfs_layout_segment *lseg, |
| 235 | struct list_head *tmp_list) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 236 | { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 237 | dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg, |
| 238 | atomic_read(&lseg->pls_refcount), |
| 239 | test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
| 240 | if (atomic_dec_and_test(&lseg->pls_refcount)) { |
| 241 | struct inode *ino = lseg->pls_layout->plh_inode; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 242 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 243 | BUG_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
| 244 | list_del(&lseg->pls_list); |
| 245 | if (list_empty(&lseg->pls_layout->plh_segs)) { |
| 246 | struct nfs_client *clp; |
| 247 | |
| 248 | clp = NFS_SERVER(ino)->nfs_client; |
| 249 | spin_lock(&clp->cl_lock); |
| 250 | /* List does not take a reference, so no need for put here */ |
| 251 | list_del_init(&lseg->pls_layout->plh_layouts); |
| 252 | spin_unlock(&clp->cl_lock); |
| 253 | } |
| 254 | list_add(&lseg->pls_list, tmp_list); |
| 255 | return 1; |
| 256 | } |
| 257 | return 0; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 258 | } |
| 259 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 260 | static bool |
| 261 | should_free_lseg(u32 lseg_iomode, u32 recall_iomode) |
| 262 | { |
| 263 | return (recall_iomode == IOMODE_ANY || |
| 264 | lseg_iomode == recall_iomode); |
| 265 | } |
| 266 | |
| 267 | /* Returns 1 if lseg is removed from list, 0 otherwise */ |
| 268 | static int mark_lseg_invalid(struct pnfs_layout_segment *lseg, |
| 269 | struct list_head *tmp_list) |
| 270 | { |
| 271 | int rv = 0; |
| 272 | |
| 273 | if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) { |
| 274 | /* Remove the reference keeping the lseg in the |
| 275 | * list. It will now be removed when all |
| 276 | * outstanding io is finished. |
| 277 | */ |
| 278 | rv = put_lseg_locked(lseg, tmp_list); |
| 279 | } |
| 280 | return rv; |
| 281 | } |
| 282 | |
| 283 | /* Returns count of number of matching invalid lsegs remaining in list |
| 284 | * after call. |
| 285 | */ |
| 286 | static int |
| 287 | mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo, |
| 288 | struct list_head *tmp_list, |
| 289 | u32 iomode) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 290 | { |
| 291 | struct pnfs_layout_segment *lseg, *next; |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 292 | int invalid = 0, removed = 0; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 293 | |
| 294 | dprintk("%s:Begin lo %p\n", __func__, lo); |
| 295 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 296 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) |
| 297 | if (should_free_lseg(lseg->pls_range.iomode, iomode)) { |
| 298 | dprintk("%s: freeing lseg %p iomode %d " |
| 299 | "offset %llu length %llu\n", __func__, |
| 300 | lseg, lseg->pls_range.iomode, lseg->pls_range.offset, |
| 301 | lseg->pls_range.length); |
| 302 | invalid++; |
| 303 | removed += mark_lseg_invalid(lseg, tmp_list); |
| 304 | } |
| 305 | dprintk("%s:Return %i\n", __func__, invalid - removed); |
| 306 | return invalid - removed; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static void |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 310 | pnfs_free_lseg_list(struct list_head *free_me) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 311 | { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 312 | struct pnfs_layout_segment *lseg, *tmp; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 313 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 314 | list_for_each_entry_safe(lseg, tmp, free_me, pls_list) { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 315 | list_del(&lseg->pls_list); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 316 | free_lseg(lseg); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 320 | void |
| 321 | pnfs_destroy_layout(struct nfs_inode *nfsi) |
| 322 | { |
| 323 | struct pnfs_layout_hdr *lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 324 | LIST_HEAD(tmp_list); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 325 | |
| 326 | spin_lock(&nfsi->vfs_inode.i_lock); |
| 327 | lo = nfsi->layout; |
| 328 | if (lo) { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 329 | set_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags); |
| 330 | mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 331 | /* Matched by refcount set to 1 in alloc_init_layout_hdr */ |
| 332 | put_layout_hdr_locked(lo); |
| 333 | } |
| 334 | spin_unlock(&nfsi->vfs_inode.i_lock); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 335 | pnfs_free_lseg_list(&tmp_list); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 336 | } |
| 337 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 338 | /* |
| 339 | * Called by the state manger to remove all layouts established under an |
| 340 | * expired lease. |
| 341 | */ |
| 342 | void |
| 343 | pnfs_destroy_all_layouts(struct nfs_client *clp) |
| 344 | { |
| 345 | struct pnfs_layout_hdr *lo; |
| 346 | LIST_HEAD(tmp_list); |
| 347 | |
| 348 | spin_lock(&clp->cl_lock); |
| 349 | list_splice_init(&clp->cl_layouts, &tmp_list); |
| 350 | spin_unlock(&clp->cl_lock); |
| 351 | |
| 352 | while (!list_empty(&tmp_list)) { |
| 353 | lo = list_entry(tmp_list.next, struct pnfs_layout_hdr, |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 354 | plh_layouts); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 355 | dprintk("%s freeing layout for inode %lu\n", __func__, |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 356 | lo->plh_inode->i_ino); |
| 357 | pnfs_destroy_layout(NFS_I(lo->plh_inode)); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 361 | /* update lo->plh_stateid with new if is more recent */ |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 362 | static void |
| 363 | pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, |
| 364 | const nfs4_stateid *new) |
| 365 | { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 366 | u32 oldseq, newseq; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 367 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 368 | oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid); |
| 369 | newseq = be32_to_cpu(new->stateid.seqid); |
| 370 | if ((int)(newseq - oldseq) > 0) |
| 371 | memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid)); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 372 | } |
| 373 | |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 374 | /* lget is set to 1 if called from inside send_layoutget call chain */ |
| 375 | static bool |
| 376 | pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, int lget) |
| 377 | { |
| 378 | return (list_empty(&lo->plh_segs) && |
| 379 | (atomic_read(&lo->plh_outstanding) > lget)); |
| 380 | } |
| 381 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 382 | int |
| 383 | pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo, |
| 384 | struct nfs4_state *open_state) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 385 | { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 386 | int status = 0; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 387 | |
| 388 | dprintk("--> %s\n", __func__); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 389 | spin_lock(&lo->plh_inode->i_lock); |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 390 | if (pnfs_layoutgets_blocked(lo, 1)) { |
| 391 | status = -EAGAIN; |
| 392 | } else if (list_empty(&lo->plh_segs)) { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 393 | int seq; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 394 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 395 | do { |
| 396 | seq = read_seqbegin(&open_state->seqlock); |
| 397 | memcpy(dst->data, open_state->stateid.data, |
| 398 | sizeof(open_state->stateid.data)); |
| 399 | } while (read_seqretry(&open_state->seqlock, seq)); |
| 400 | } else |
| 401 | memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data)); |
| 402 | spin_unlock(&lo->plh_inode->i_lock); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 403 | dprintk("<-- %s\n", __func__); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 404 | return status; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Get layout from server. |
| 409 | * for now, assume that whole file layouts are requested. |
| 410 | * arg->offset: 0 |
| 411 | * arg->length: all ones |
| 412 | */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 413 | static struct pnfs_layout_segment * |
| 414 | send_layoutget(struct pnfs_layout_hdr *lo, |
| 415 | struct nfs_open_context *ctx, |
| 416 | u32 iomode) |
| 417 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 418 | struct inode *ino = lo->plh_inode; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 419 | struct nfs_server *server = NFS_SERVER(ino); |
| 420 | struct nfs4_layoutget *lgp; |
| 421 | struct pnfs_layout_segment *lseg = NULL; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 422 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 423 | dprintk("--> %s\n", __func__); |
| 424 | |
| 425 | BUG_ON(ctx == NULL); |
| 426 | lgp = kzalloc(sizeof(*lgp), GFP_KERNEL); |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 427 | if (lgp == NULL) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 428 | return NULL; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 429 | lgp->args.minlength = NFS4_MAX_UINT64; |
| 430 | lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE; |
| 431 | lgp->args.range.iomode = iomode; |
| 432 | lgp->args.range.offset = 0; |
| 433 | lgp->args.range.length = NFS4_MAX_UINT64; |
| 434 | lgp->args.type = server->pnfs_curr_ld->id; |
| 435 | lgp->args.inode = ino; |
| 436 | lgp->args.ctx = get_nfs_open_context(ctx); |
| 437 | lgp->lsegpp = &lseg; |
| 438 | |
| 439 | /* Synchronously retrieve layout information from server and |
| 440 | * store in lseg. |
| 441 | */ |
| 442 | nfs4_proc_layoutget(lgp); |
| 443 | if (!lseg) { |
| 444 | /* remember that LAYOUTGET failed and suspend trying */ |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 445 | set_bit(lo_fail_bit(iomode), &lo->plh_flags); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 446 | } |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 447 | return lseg; |
| 448 | } |
| 449 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 450 | /* |
| 451 | * Compare two layout segments for sorting into layout cache. |
| 452 | * We want to preferentially return RW over RO layouts, so ensure those |
| 453 | * are seen first. |
| 454 | */ |
| 455 | static s64 |
| 456 | cmp_layout(u32 iomode1, u32 iomode2) |
| 457 | { |
| 458 | /* read > read/write */ |
| 459 | return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ); |
| 460 | } |
| 461 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 462 | static void |
| 463 | pnfs_insert_layout(struct pnfs_layout_hdr *lo, |
| 464 | struct pnfs_layout_segment *lseg) |
| 465 | { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 466 | struct pnfs_layout_segment *lp; |
| 467 | int found = 0; |
| 468 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 469 | dprintk("%s:Begin\n", __func__); |
| 470 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 471 | assert_spin_locked(&lo->plh_inode->i_lock); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 472 | list_for_each_entry(lp, &lo->plh_segs, pls_list) { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 473 | if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 474 | continue; |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 475 | list_add_tail(&lseg->pls_list, &lp->pls_list); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 476 | dprintk("%s: inserted lseg %p " |
| 477 | "iomode %d offset %llu length %llu before " |
| 478 | "lp %p iomode %d offset %llu length %llu\n", |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 479 | __func__, lseg, lseg->pls_range.iomode, |
| 480 | lseg->pls_range.offset, lseg->pls_range.length, |
| 481 | lp, lp->pls_range.iomode, lp->pls_range.offset, |
| 482 | lp->pls_range.length); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 483 | found = 1; |
| 484 | break; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 485 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 486 | if (!found) { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 487 | list_add_tail(&lseg->pls_list, &lo->plh_segs); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 488 | dprintk("%s: inserted lseg %p " |
| 489 | "iomode %d offset %llu length %llu at tail\n", |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 490 | __func__, lseg, lseg->pls_range.iomode, |
| 491 | lseg->pls_range.offset, lseg->pls_range.length); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 492 | } |
| 493 | get_layout_hdr_locked(lo); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 494 | |
| 495 | dprintk("%s:Return\n", __func__); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static struct pnfs_layout_hdr * |
| 499 | alloc_init_layout_hdr(struct inode *ino) |
| 500 | { |
| 501 | struct pnfs_layout_hdr *lo; |
| 502 | |
| 503 | lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL); |
| 504 | if (!lo) |
| 505 | return NULL; |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 506 | lo->plh_refcount = 1; |
| 507 | INIT_LIST_HEAD(&lo->plh_layouts); |
| 508 | INIT_LIST_HEAD(&lo->plh_segs); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 509 | lo->plh_inode = ino; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 510 | return lo; |
| 511 | } |
| 512 | |
| 513 | static struct pnfs_layout_hdr * |
| 514 | pnfs_find_alloc_layout(struct inode *ino) |
| 515 | { |
| 516 | struct nfs_inode *nfsi = NFS_I(ino); |
| 517 | struct pnfs_layout_hdr *new = NULL; |
| 518 | |
| 519 | dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout); |
| 520 | |
| 521 | assert_spin_locked(&ino->i_lock); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 522 | if (nfsi->layout) { |
| 523 | if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags)) |
| 524 | return NULL; |
| 525 | else |
| 526 | return nfsi->layout; |
| 527 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 528 | spin_unlock(&ino->i_lock); |
| 529 | new = alloc_init_layout_hdr(ino); |
| 530 | spin_lock(&ino->i_lock); |
| 531 | |
| 532 | if (likely(nfsi->layout == NULL)) /* Won the race? */ |
| 533 | nfsi->layout = new; |
| 534 | else |
| 535 | kfree(new); |
| 536 | return nfsi->layout; |
| 537 | } |
| 538 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 539 | /* |
| 540 | * iomode matching rules: |
| 541 | * iomode lseg match |
| 542 | * ----- ----- ----- |
| 543 | * ANY READ true |
| 544 | * ANY RW true |
| 545 | * RW READ false |
| 546 | * RW RW true |
| 547 | * READ READ true |
| 548 | * READ RW true |
| 549 | */ |
| 550 | static int |
| 551 | is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode) |
| 552 | { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 553 | return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* |
| 557 | * lookup range in layout |
| 558 | */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 559 | static struct pnfs_layout_segment * |
| 560 | pnfs_has_layout(struct pnfs_layout_hdr *lo, u32 iomode) |
| 561 | { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 562 | struct pnfs_layout_segment *lseg, *ret = NULL; |
| 563 | |
| 564 | dprintk("%s:Begin\n", __func__); |
| 565 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 566 | assert_spin_locked(&lo->plh_inode->i_lock); |
| 567 | list_for_each_entry(lseg, &lo->plh_segs, pls_list) { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 568 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) && |
| 569 | is_matching_lseg(lseg, iomode)) { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 570 | ret = lseg; |
| 571 | break; |
| 572 | } |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 573 | if (cmp_layout(iomode, lseg->pls_range.iomode) > 0) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 574 | break; |
| 575 | } |
| 576 | |
| 577 | dprintk("%s:Return lseg %p ref %d\n", |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 578 | __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 579 | return ret; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Layout segment is retreived from the server if not cached. |
| 584 | * The appropriate layout segment is referenced and returned to the caller. |
| 585 | */ |
| 586 | struct pnfs_layout_segment * |
| 587 | pnfs_update_layout(struct inode *ino, |
| 588 | struct nfs_open_context *ctx, |
| 589 | enum pnfs_iomode iomode) |
| 590 | { |
| 591 | struct nfs_inode *nfsi = NFS_I(ino); |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 592 | struct nfs_client *clp = NFS_SERVER(ino)->nfs_client; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 593 | struct pnfs_layout_hdr *lo; |
| 594 | struct pnfs_layout_segment *lseg = NULL; |
| 595 | |
| 596 | if (!pnfs_enabled_sb(NFS_SERVER(ino))) |
| 597 | return NULL; |
| 598 | spin_lock(&ino->i_lock); |
| 599 | lo = pnfs_find_alloc_layout(ino); |
| 600 | if (lo == NULL) { |
| 601 | dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__); |
| 602 | goto out_unlock; |
| 603 | } |
| 604 | |
| 605 | /* Check to see if the layout for the given range already exists */ |
| 606 | lseg = pnfs_has_layout(lo, iomode); |
| 607 | if (lseg) { |
| 608 | dprintk("%s: Using cached lseg %p for iomode %d)\n", |
| 609 | __func__, lseg, iomode); |
| 610 | goto out_unlock; |
| 611 | } |
| 612 | |
| 613 | /* if LAYOUTGET already failed once we don't try again */ |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 614 | if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags)) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 615 | goto out_unlock; |
| 616 | |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 617 | if (pnfs_layoutgets_blocked(lo, 0)) |
| 618 | goto out_unlock; |
| 619 | atomic_inc(&lo->plh_outstanding); |
| 620 | |
| 621 | get_layout_hdr_locked(lo); |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 622 | if (list_empty(&lo->plh_segs)) { |
| 623 | /* The lo must be on the clp list if there is any |
| 624 | * chance of a CB_LAYOUTRECALL(FILE) coming in. |
| 625 | */ |
| 626 | spin_lock(&clp->cl_lock); |
| 627 | BUG_ON(!list_empty(&lo->plh_layouts)); |
| 628 | list_add_tail(&lo->plh_layouts, &clp->cl_layouts); |
| 629 | spin_unlock(&clp->cl_lock); |
| 630 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 631 | spin_unlock(&ino->i_lock); |
| 632 | |
| 633 | lseg = send_layoutget(lo, ctx, iomode); |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 634 | if (!lseg) { |
| 635 | spin_lock(&ino->i_lock); |
| 636 | if (list_empty(&lo->plh_segs)) { |
| 637 | spin_lock(&clp->cl_lock); |
| 638 | list_del_init(&lo->plh_layouts); |
| 639 | spin_unlock(&clp->cl_lock); |
| 640 | } |
| 641 | spin_unlock(&ino->i_lock); |
| 642 | } |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 643 | atomic_dec(&lo->plh_outstanding); |
| 644 | put_layout_hdr(ino); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 645 | out: |
| 646 | dprintk("%s end, state 0x%lx lseg %p\n", __func__, |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 647 | nfsi->layout->plh_flags, lseg); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 648 | return lseg; |
| 649 | out_unlock: |
| 650 | spin_unlock(&ino->i_lock); |
| 651 | goto out; |
| 652 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 653 | |
| 654 | int |
| 655 | pnfs_layout_process(struct nfs4_layoutget *lgp) |
| 656 | { |
| 657 | struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout; |
| 658 | struct nfs4_layoutget_res *res = &lgp->res; |
| 659 | struct pnfs_layout_segment *lseg; |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 660 | struct inode *ino = lo->plh_inode; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 661 | int status = 0; |
| 662 | |
Fred Isaman | fc1794c | 2011-01-06 11:36:27 +0000 | [diff] [blame^] | 663 | /* Verify we got what we asked for. |
| 664 | * Note that because the xdr parsing only accepts a single |
| 665 | * element array, this can fail even if the server is behaving |
| 666 | * correctly. |
| 667 | */ |
| 668 | if (lgp->args.range.iomode > res->range.iomode || |
| 669 | res->range.offset != 0 || |
| 670 | res->range.length != NFS4_MAX_UINT64) { |
| 671 | status = -EINVAL; |
| 672 | goto out; |
| 673 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 674 | /* Inject layout blob into I/O device driver */ |
| 675 | lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res); |
| 676 | if (!lseg || IS_ERR(lseg)) { |
| 677 | if (!lseg) |
| 678 | status = -ENOMEM; |
| 679 | else |
| 680 | status = PTR_ERR(lseg); |
| 681 | dprintk("%s: Could not allocate layout: error %d\n", |
| 682 | __func__, status); |
| 683 | goto out; |
| 684 | } |
| 685 | |
| 686 | spin_lock(&ino->i_lock); |
| 687 | init_lseg(lo, lseg); |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 688 | lseg->pls_range = res->range; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 689 | *lgp->lsegpp = lseg; |
| 690 | pnfs_insert_layout(lo, lseg); |
| 691 | |
| 692 | /* Done processing layoutget. Set the layout stateid */ |
| 693 | pnfs_set_layout_stateid(lo, &res->stateid); |
| 694 | spin_unlock(&ino->i_lock); |
| 695 | out: |
| 696 | return status; |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * Device ID cache. Currently supports one layout type per struct nfs_client. |
| 701 | * Add layout type to the lookup key to expand to support multiple types. |
| 702 | */ |
| 703 | int |
| 704 | pnfs_alloc_init_deviceid_cache(struct nfs_client *clp, |
| 705 | void (*free_callback)(struct pnfs_deviceid_node *)) |
| 706 | { |
| 707 | struct pnfs_deviceid_cache *c; |
| 708 | |
| 709 | c = kzalloc(sizeof(struct pnfs_deviceid_cache), GFP_KERNEL); |
| 710 | if (!c) |
| 711 | return -ENOMEM; |
| 712 | spin_lock(&clp->cl_lock); |
| 713 | if (clp->cl_devid_cache != NULL) { |
| 714 | atomic_inc(&clp->cl_devid_cache->dc_ref); |
| 715 | dprintk("%s [kref [%d]]\n", __func__, |
| 716 | atomic_read(&clp->cl_devid_cache->dc_ref)); |
| 717 | kfree(c); |
| 718 | } else { |
| 719 | /* kzalloc initializes hlists */ |
| 720 | spin_lock_init(&c->dc_lock); |
| 721 | atomic_set(&c->dc_ref, 1); |
| 722 | c->dc_free_callback = free_callback; |
| 723 | clp->cl_devid_cache = c; |
| 724 | dprintk("%s [new]\n", __func__); |
| 725 | } |
| 726 | spin_unlock(&clp->cl_lock); |
| 727 | return 0; |
| 728 | } |
| 729 | EXPORT_SYMBOL_GPL(pnfs_alloc_init_deviceid_cache); |
| 730 | |
| 731 | /* |
| 732 | * Called from pnfs_layoutdriver_type->free_lseg |
| 733 | * last layout segment reference frees deviceid |
| 734 | */ |
| 735 | void |
| 736 | pnfs_put_deviceid(struct pnfs_deviceid_cache *c, |
| 737 | struct pnfs_deviceid_node *devid) |
| 738 | { |
| 739 | struct nfs4_deviceid *id = &devid->de_id; |
| 740 | struct pnfs_deviceid_node *d; |
| 741 | struct hlist_node *n; |
| 742 | long h = nfs4_deviceid_hash(id); |
| 743 | |
| 744 | dprintk("%s [%d]\n", __func__, atomic_read(&devid->de_ref)); |
| 745 | if (!atomic_dec_and_lock(&devid->de_ref, &c->dc_lock)) |
| 746 | return; |
| 747 | |
| 748 | hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[h], de_node) |
| 749 | if (!memcmp(&d->de_id, id, sizeof(*id))) { |
| 750 | hlist_del_rcu(&d->de_node); |
| 751 | spin_unlock(&c->dc_lock); |
| 752 | synchronize_rcu(); |
| 753 | c->dc_free_callback(devid); |
| 754 | return; |
| 755 | } |
| 756 | spin_unlock(&c->dc_lock); |
| 757 | /* Why wasn't it found in the list? */ |
| 758 | BUG(); |
| 759 | } |
| 760 | EXPORT_SYMBOL_GPL(pnfs_put_deviceid); |
| 761 | |
| 762 | /* Find and reference a deviceid */ |
| 763 | struct pnfs_deviceid_node * |
| 764 | pnfs_find_get_deviceid(struct pnfs_deviceid_cache *c, struct nfs4_deviceid *id) |
| 765 | { |
| 766 | struct pnfs_deviceid_node *d; |
| 767 | struct hlist_node *n; |
| 768 | long hash = nfs4_deviceid_hash(id); |
| 769 | |
| 770 | dprintk("--> %s hash %ld\n", __func__, hash); |
| 771 | rcu_read_lock(); |
| 772 | hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[hash], de_node) { |
| 773 | if (!memcmp(&d->de_id, id, sizeof(*id))) { |
| 774 | if (!atomic_inc_not_zero(&d->de_ref)) { |
| 775 | goto fail; |
| 776 | } else { |
| 777 | rcu_read_unlock(); |
| 778 | return d; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | fail: |
| 783 | rcu_read_unlock(); |
| 784 | return NULL; |
| 785 | } |
| 786 | EXPORT_SYMBOL_GPL(pnfs_find_get_deviceid); |
| 787 | |
| 788 | /* |
| 789 | * Add a deviceid to the cache. |
| 790 | * GETDEVICEINFOs for same deviceid can race. If deviceid is found, discard new |
| 791 | */ |
| 792 | struct pnfs_deviceid_node * |
| 793 | pnfs_add_deviceid(struct pnfs_deviceid_cache *c, struct pnfs_deviceid_node *new) |
| 794 | { |
| 795 | struct pnfs_deviceid_node *d; |
| 796 | long hash = nfs4_deviceid_hash(&new->de_id); |
| 797 | |
| 798 | dprintk("--> %s hash %ld\n", __func__, hash); |
| 799 | spin_lock(&c->dc_lock); |
| 800 | d = pnfs_find_get_deviceid(c, &new->de_id); |
| 801 | if (d) { |
| 802 | spin_unlock(&c->dc_lock); |
| 803 | dprintk("%s [discard]\n", __func__); |
| 804 | c->dc_free_callback(new); |
| 805 | return d; |
| 806 | } |
| 807 | INIT_HLIST_NODE(&new->de_node); |
| 808 | atomic_set(&new->de_ref, 1); |
| 809 | hlist_add_head_rcu(&new->de_node, &c->dc_deviceids[hash]); |
| 810 | spin_unlock(&c->dc_lock); |
| 811 | dprintk("%s [new]\n", __func__); |
| 812 | return new; |
| 813 | } |
| 814 | EXPORT_SYMBOL_GPL(pnfs_add_deviceid); |
| 815 | |
| 816 | void |
| 817 | pnfs_put_deviceid_cache(struct nfs_client *clp) |
| 818 | { |
| 819 | struct pnfs_deviceid_cache *local = clp->cl_devid_cache; |
| 820 | |
| 821 | dprintk("--> %s cl_devid_cache %p\n", __func__, clp->cl_devid_cache); |
| 822 | if (atomic_dec_and_lock(&local->dc_ref, &clp->cl_lock)) { |
| 823 | int i; |
| 824 | /* Verify cache is empty */ |
| 825 | for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i++) |
| 826 | BUG_ON(!hlist_empty(&local->dc_deviceids[i])); |
| 827 | clp->cl_devid_cache = NULL; |
| 828 | spin_unlock(&clp->cl_lock); |
| 829 | kfree(local); |
| 830 | } |
| 831 | } |
| 832 | EXPORT_SYMBOL_GPL(pnfs_put_deviceid_cache); |