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 | |
Benny Halevy | 636fb9c | 2011-05-22 19:51:33 +0300 | [diff] [blame] | 180 | static struct pnfs_layout_hdr * |
| 181 | pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags) |
| 182 | { |
| 183 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld; |
| 184 | return ld->alloc_layout_hdr ? ld->alloc_layout_hdr(ino, gfp_flags) : |
| 185 | kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags); |
| 186 | } |
| 187 | |
| 188 | static void |
| 189 | pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo) |
| 190 | { |
| 191 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(lo->plh_inode)->pnfs_curr_ld; |
| 192 | return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo); |
| 193 | } |
| 194 | |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 195 | static void |
| 196 | destroy_layout_hdr(struct pnfs_layout_hdr *lo) |
| 197 | { |
| 198 | dprintk("%s: freeing layout cache %p\n", __func__, lo); |
| 199 | BUG_ON(!list_empty(&lo->plh_layouts)); |
| 200 | NFS_I(lo->plh_inode)->layout = NULL; |
Benny Halevy | 636fb9c | 2011-05-22 19:51:33 +0300 | [diff] [blame] | 201 | pnfs_free_layout_hdr(lo); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void |
| 205 | put_layout_hdr_locked(struct pnfs_layout_hdr *lo) |
| 206 | { |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 207 | if (atomic_dec_and_test(&lo->plh_refcount)) |
| 208 | destroy_layout_hdr(lo); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 209 | } |
| 210 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 211 | void |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 212 | put_layout_hdr(struct pnfs_layout_hdr *lo) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 213 | { |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 214 | struct inode *inode = lo->plh_inode; |
| 215 | |
| 216 | if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) { |
| 217 | destroy_layout_hdr(lo); |
| 218 | spin_unlock(&inode->i_lock); |
| 219 | } |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void |
| 223 | init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg) |
| 224 | { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 225 | INIT_LIST_HEAD(&lseg->pls_list); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 226 | atomic_set(&lseg->pls_refcount, 1); |
| 227 | smp_mb(); |
| 228 | set_bit(NFS_LSEG_VALID, &lseg->pls_flags); |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 229 | lseg->pls_layout = lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 230 | } |
| 231 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 232 | static void free_lseg(struct pnfs_layout_segment *lseg) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 233 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 234 | struct inode *ino = lseg->pls_layout->plh_inode; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 235 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 236 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
Fred Isaman | 52fabd7 | 2011-01-06 11:36:18 +0000 | [diff] [blame] | 237 | /* Matched by get_layout_hdr in pnfs_insert_layout */ |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 238 | put_layout_hdr(NFS_I(ino)->layout); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 241 | static void |
| 242 | put_lseg_common(struct pnfs_layout_segment *lseg) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 243 | { |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 244 | struct inode *inode = lseg->pls_layout->plh_inode; |
| 245 | |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 246 | WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 247 | list_del_init(&lseg->pls_list); |
| 248 | if (list_empty(&lseg->pls_layout->plh_segs)) { |
| 249 | set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags); |
| 250 | /* Matched by initial refcount set in alloc_init_layout_hdr */ |
| 251 | put_layout_hdr_locked(lseg->pls_layout); |
| 252 | } |
| 253 | rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq); |
| 254 | } |
| 255 | |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 256 | void |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 257 | put_lseg(struct pnfs_layout_segment *lseg) |
| 258 | { |
| 259 | struct inode *inode; |
| 260 | |
| 261 | if (!lseg) |
| 262 | return; |
| 263 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 264 | dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg, |
| 265 | atomic_read(&lseg->pls_refcount), |
| 266 | test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 267 | inode = lseg->pls_layout->plh_inode; |
| 268 | if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) { |
| 269 | LIST_HEAD(free_me); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 270 | |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 271 | put_lseg_common(lseg); |
| 272 | list_add(&lseg->pls_list, &free_me); |
| 273 | spin_unlock(&inode->i_lock); |
| 274 | pnfs_free_lseg_list(&free_me); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 275 | } |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 276 | } |
Fred Isaman | e0c2b38 | 2011-03-23 13:27:53 +0000 | [diff] [blame] | 277 | EXPORT_SYMBOL_GPL(put_lseg); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 278 | |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 279 | static inline u64 |
| 280 | end_offset(u64 start, u64 len) |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 281 | { |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 282 | u64 end; |
| 283 | |
| 284 | end = start + len; |
| 285 | return end >= start ? end : NFS4_MAX_UINT64; |
| 286 | } |
| 287 | |
| 288 | /* last octet in a range */ |
| 289 | static inline u64 |
| 290 | last_byte_offset(u64 start, u64 len) |
| 291 | { |
| 292 | u64 end; |
| 293 | |
| 294 | BUG_ON(!len); |
| 295 | end = start + len; |
| 296 | return end > start ? end - 1 : NFS4_MAX_UINT64; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * is l2 fully contained in l1? |
| 301 | * start1 end1 |
| 302 | * [----------------------------------) |
| 303 | * start2 end2 |
| 304 | * [----------------) |
| 305 | */ |
| 306 | static inline int |
| 307 | lo_seg_contained(struct pnfs_layout_range *l1, |
| 308 | struct pnfs_layout_range *l2) |
| 309 | { |
| 310 | u64 start1 = l1->offset; |
| 311 | u64 end1 = end_offset(start1, l1->length); |
| 312 | u64 start2 = l2->offset; |
| 313 | u64 end2 = end_offset(start2, l2->length); |
| 314 | |
| 315 | return (start1 <= start2) && (end1 >= end2); |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * is l1 and l2 intersecting? |
| 320 | * start1 end1 |
| 321 | * [----------------------------------) |
| 322 | * start2 end2 |
| 323 | * [----------------) |
| 324 | */ |
| 325 | static inline int |
| 326 | lo_seg_intersecting(struct pnfs_layout_range *l1, |
| 327 | struct pnfs_layout_range *l2) |
| 328 | { |
| 329 | u64 start1 = l1->offset; |
| 330 | u64 end1 = end_offset(start1, l1->length); |
| 331 | u64 start2 = l2->offset; |
| 332 | u64 end2 = end_offset(start2, l2->length); |
| 333 | |
| 334 | return (end1 == NFS4_MAX_UINT64 || end1 > start2) && |
| 335 | (end2 == NFS4_MAX_UINT64 || end2 > start1); |
| 336 | } |
| 337 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 338 | static bool |
Benny Halevy | 778b550 | 2011-05-22 19:48:02 +0300 | [diff] [blame] | 339 | should_free_lseg(struct pnfs_layout_range *lseg_range, |
| 340 | struct pnfs_layout_range *recall_range) |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 341 | { |
Benny Halevy | 778b550 | 2011-05-22 19:48:02 +0300 | [diff] [blame] | 342 | return (recall_range->iomode == IOMODE_ANY || |
| 343 | lseg_range->iomode == recall_range->iomode) && |
| 344 | lo_seg_intersecting(lseg_range, recall_range); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* Returns 1 if lseg is removed from list, 0 otherwise */ |
| 348 | static int mark_lseg_invalid(struct pnfs_layout_segment *lseg, |
| 349 | struct list_head *tmp_list) |
| 350 | { |
| 351 | int rv = 0; |
| 352 | |
| 353 | if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) { |
| 354 | /* Remove the reference keeping the lseg in the |
| 355 | * list. It will now be removed when all |
| 356 | * outstanding io is finished. |
| 357 | */ |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 358 | dprintk("%s: lseg %p ref %d\n", __func__, lseg, |
| 359 | atomic_read(&lseg->pls_refcount)); |
| 360 | if (atomic_dec_and_test(&lseg->pls_refcount)) { |
| 361 | put_lseg_common(lseg); |
| 362 | list_add(&lseg->pls_list, tmp_list); |
| 363 | rv = 1; |
| 364 | } |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 365 | } |
| 366 | return rv; |
| 367 | } |
| 368 | |
| 369 | /* Returns count of number of matching invalid lsegs remaining in list |
| 370 | * after call. |
| 371 | */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 372 | int |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 373 | mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo, |
| 374 | struct list_head *tmp_list, |
Benny Halevy | 778b550 | 2011-05-22 19:48:02 +0300 | [diff] [blame] | 375 | struct pnfs_layout_range *recall_range) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 376 | { |
| 377 | struct pnfs_layout_segment *lseg, *next; |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 378 | int invalid = 0, removed = 0; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 379 | |
| 380 | dprintk("%s:Begin lo %p\n", __func__, lo); |
| 381 | |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 382 | if (list_empty(&lo->plh_segs)) { |
| 383 | if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) |
| 384 | put_layout_hdr_locked(lo); |
| 385 | return 0; |
| 386 | } |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 387 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) |
Benny Halevy | 778b550 | 2011-05-22 19:48:02 +0300 | [diff] [blame] | 388 | if (!recall_range || |
| 389 | should_free_lseg(&lseg->pls_range, recall_range)) { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 390 | dprintk("%s: freeing lseg %p iomode %d " |
| 391 | "offset %llu length %llu\n", __func__, |
| 392 | lseg, lseg->pls_range.iomode, lseg->pls_range.offset, |
| 393 | lseg->pls_range.length); |
| 394 | invalid++; |
| 395 | removed += mark_lseg_invalid(lseg, tmp_list); |
| 396 | } |
| 397 | dprintk("%s:Return %i\n", __func__, invalid - removed); |
| 398 | return invalid - removed; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 399 | } |
| 400 | |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 401 | /* note free_me must contain lsegs from a single layout_hdr */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 402 | void |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 403 | pnfs_free_lseg_list(struct list_head *free_me) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 404 | { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 405 | struct pnfs_layout_segment *lseg, *tmp; |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 406 | struct pnfs_layout_hdr *lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 407 | |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 408 | if (list_empty(free_me)) |
| 409 | return; |
| 410 | |
| 411 | lo = list_first_entry(free_me, struct pnfs_layout_segment, |
| 412 | pls_list)->pls_layout; |
| 413 | |
| 414 | if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) { |
| 415 | struct nfs_client *clp; |
| 416 | |
| 417 | clp = NFS_SERVER(lo->plh_inode)->nfs_client; |
| 418 | spin_lock(&clp->cl_lock); |
| 419 | list_del_init(&lo->plh_layouts); |
| 420 | spin_unlock(&clp->cl_lock); |
| 421 | } |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 422 | list_for_each_entry_safe(lseg, tmp, free_me, pls_list) { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 423 | list_del(&lseg->pls_list); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 424 | free_lseg(lseg); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 428 | void |
| 429 | pnfs_destroy_layout(struct nfs_inode *nfsi) |
| 430 | { |
| 431 | struct pnfs_layout_hdr *lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 432 | LIST_HEAD(tmp_list); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 433 | |
| 434 | spin_lock(&nfsi->vfs_inode.i_lock); |
| 435 | lo = nfsi->layout; |
| 436 | if (lo) { |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 437 | lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */ |
Benny Halevy | 778b550 | 2011-05-22 19:48:02 +0300 | [diff] [blame] | 438 | mark_matching_lsegs_invalid(lo, &tmp_list, NULL); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 439 | } |
| 440 | spin_unlock(&nfsi->vfs_inode.i_lock); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 441 | pnfs_free_lseg_list(&tmp_list); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 442 | } |
| 443 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 444 | /* |
| 445 | * Called by the state manger to remove all layouts established under an |
| 446 | * expired lease. |
| 447 | */ |
| 448 | void |
| 449 | pnfs_destroy_all_layouts(struct nfs_client *clp) |
| 450 | { |
| 451 | struct pnfs_layout_hdr *lo; |
| 452 | LIST_HEAD(tmp_list); |
| 453 | |
| 454 | spin_lock(&clp->cl_lock); |
| 455 | list_splice_init(&clp->cl_layouts, &tmp_list); |
| 456 | spin_unlock(&clp->cl_lock); |
| 457 | |
| 458 | while (!list_empty(&tmp_list)) { |
| 459 | lo = list_entry(tmp_list.next, struct pnfs_layout_hdr, |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 460 | plh_layouts); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 461 | dprintk("%s freeing layout for inode %lu\n", __func__, |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 462 | lo->plh_inode->i_ino); |
Andy Adamson | 2887fe4 | 2011-05-11 01:19:58 -0400 | [diff] [blame] | 463 | list_del_init(&lo->plh_layouts); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 464 | pnfs_destroy_layout(NFS_I(lo->plh_inode)); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 468 | /* update lo->plh_stateid with new if is more recent */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 469 | void |
| 470 | pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new, |
| 471 | bool update_barrier) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 472 | { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 473 | u32 oldseq, newseq; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 474 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 475 | oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid); |
| 476 | newseq = be32_to_cpu(new->stateid.seqid); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 477 | if ((int)(newseq - oldseq) > 0) { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 478 | memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid)); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 479 | if (update_barrier) { |
| 480 | u32 new_barrier = be32_to_cpu(new->stateid.seqid); |
| 481 | |
| 482 | if ((int)(new_barrier - lo->plh_barrier)) |
| 483 | lo->plh_barrier = new_barrier; |
| 484 | } else { |
| 485 | /* Because of wraparound, we want to keep the barrier |
| 486 | * "close" to the current seqids. It needs to be |
| 487 | * within 2**31 to count as "behind", so if it |
| 488 | * gets too near that limit, give us a litle leeway |
| 489 | * and bring it to within 2**30. |
| 490 | * NOTE - and yes, this is all unsigned arithmetic. |
| 491 | */ |
| 492 | if (unlikely((newseq - lo->plh_barrier) > (3 << 29))) |
| 493 | lo->plh_barrier = newseq - (1 << 30); |
| 494 | } |
| 495 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 496 | } |
| 497 | |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 498 | /* lget is set to 1 if called from inside send_layoutget call chain */ |
| 499 | static bool |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 500 | pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid, |
| 501 | int lget) |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 502 | { |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 503 | if ((stateid) && |
| 504 | (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0) |
| 505 | return true; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 506 | return lo->plh_block_lgets || |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 507 | test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) || |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 508 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) || |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 509 | (list_empty(&lo->plh_segs) && |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 510 | (atomic_read(&lo->plh_outstanding) > lget)); |
| 511 | } |
| 512 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 513 | int |
| 514 | pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo, |
| 515 | struct nfs4_state *open_state) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 516 | { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 517 | int status = 0; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 518 | |
| 519 | dprintk("--> %s\n", __func__); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 520 | spin_lock(&lo->plh_inode->i_lock); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 521 | if (pnfs_layoutgets_blocked(lo, NULL, 1)) { |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 522 | status = -EAGAIN; |
| 523 | } else if (list_empty(&lo->plh_segs)) { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 524 | int seq; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 525 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 526 | do { |
| 527 | seq = read_seqbegin(&open_state->seqlock); |
| 528 | memcpy(dst->data, open_state->stateid.data, |
| 529 | sizeof(open_state->stateid.data)); |
| 530 | } while (read_seqretry(&open_state->seqlock, seq)); |
| 531 | } else |
| 532 | memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data)); |
| 533 | spin_unlock(&lo->plh_inode->i_lock); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 534 | dprintk("<-- %s\n", __func__); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 535 | return status; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Get layout from server. |
| 540 | * for now, assume that whole file layouts are requested. |
| 541 | * arg->offset: 0 |
| 542 | * arg->length: all ones |
| 543 | */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 544 | static struct pnfs_layout_segment * |
| 545 | send_layoutget(struct pnfs_layout_hdr *lo, |
| 546 | struct nfs_open_context *ctx, |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 547 | struct pnfs_layout_range *range, |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 548 | gfp_t gfp_flags) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 549 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 550 | struct inode *ino = lo->plh_inode; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 551 | struct nfs_server *server = NFS_SERVER(ino); |
| 552 | struct nfs4_layoutget *lgp; |
| 553 | struct pnfs_layout_segment *lseg = NULL; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 554 | struct page **pages = NULL; |
| 555 | int i; |
| 556 | u32 max_resp_sz, max_pages; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 557 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 558 | dprintk("--> %s\n", __func__); |
| 559 | |
| 560 | BUG_ON(ctx == NULL); |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 561 | lgp = kzalloc(sizeof(*lgp), gfp_flags); |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 562 | if (lgp == NULL) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 563 | return NULL; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 564 | |
| 565 | /* allocate pages for xdr post processing */ |
| 566 | max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; |
| 567 | max_pages = max_resp_sz >> PAGE_SHIFT; |
| 568 | |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 569 | pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 570 | if (!pages) |
| 571 | goto out_err_free; |
| 572 | |
| 573 | for (i = 0; i < max_pages; i++) { |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 574 | pages[i] = alloc_page(gfp_flags); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 575 | if (!pages[i]) |
| 576 | goto out_err_free; |
| 577 | } |
| 578 | |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 579 | lgp->args.minlength = PAGE_CACHE_SIZE; |
| 580 | if (lgp->args.minlength > range->length) |
| 581 | lgp->args.minlength = range->length; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 582 | lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE; |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 583 | lgp->args.range = *range; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 584 | lgp->args.type = server->pnfs_curr_ld->id; |
| 585 | lgp->args.inode = ino; |
| 586 | lgp->args.ctx = get_nfs_open_context(ctx); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 587 | lgp->args.layout.pages = pages; |
| 588 | lgp->args.layout.pglen = max_pages * PAGE_SIZE; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 589 | lgp->lsegpp = &lseg; |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 590 | lgp->gfp_flags = gfp_flags; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 591 | |
| 592 | /* Synchronously retrieve layout information from server and |
| 593 | * store in lseg. |
| 594 | */ |
| 595 | nfs4_proc_layoutget(lgp); |
| 596 | if (!lseg) { |
| 597 | /* remember that LAYOUTGET failed and suspend trying */ |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 598 | set_bit(lo_fail_bit(range->iomode), &lo->plh_flags); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 599 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 600 | |
| 601 | /* free xdr pages */ |
| 602 | for (i = 0; i < max_pages; i++) |
| 603 | __free_page(pages[i]); |
| 604 | kfree(pages); |
| 605 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 606 | return lseg; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 607 | |
| 608 | out_err_free: |
| 609 | /* free any allocated xdr pages, lgp as it's not used */ |
| 610 | if (pages) { |
| 611 | for (i = 0; i < max_pages; i++) { |
| 612 | if (!pages[i]) |
| 613 | break; |
| 614 | __free_page(pages[i]); |
| 615 | } |
| 616 | kfree(pages); |
| 617 | } |
| 618 | kfree(lgp); |
| 619 | return NULL; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 620 | } |
| 621 | |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 622 | /* Initiates a LAYOUTRETURN(FILE) */ |
| 623 | int |
| 624 | _pnfs_return_layout(struct inode *ino) |
| 625 | { |
| 626 | struct pnfs_layout_hdr *lo = NULL; |
| 627 | struct nfs_inode *nfsi = NFS_I(ino); |
| 628 | LIST_HEAD(tmp_list); |
| 629 | struct nfs4_layoutreturn *lrp; |
| 630 | nfs4_stateid stateid; |
| 631 | int status = 0; |
| 632 | |
| 633 | dprintk("--> %s\n", __func__); |
| 634 | |
| 635 | spin_lock(&ino->i_lock); |
| 636 | lo = nfsi->layout; |
Fred Isaman | a2e1d4f | 2011-06-13 18:54:53 -0400 | [diff] [blame] | 637 | if (!lo) { |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 638 | spin_unlock(&ino->i_lock); |
Fred Isaman | a2e1d4f | 2011-06-13 18:54:53 -0400 | [diff] [blame] | 639 | dprintk("%s: no layout to return\n", __func__); |
| 640 | return status; |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 641 | } |
| 642 | stateid = nfsi->layout->plh_stateid; |
| 643 | /* Reference matched in nfs4_layoutreturn_release */ |
| 644 | get_layout_hdr(lo); |
Fred Isaman | ea0ded7 | 2011-06-15 12:31:02 -0400 | [diff] [blame] | 645 | mark_matching_lsegs_invalid(lo, &tmp_list, NULL); |
| 646 | lo->plh_block_lgets++; |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 647 | spin_unlock(&ino->i_lock); |
| 648 | pnfs_free_lseg_list(&tmp_list); |
| 649 | |
| 650 | WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)); |
| 651 | |
| 652 | lrp = kzalloc(sizeof(*lrp), GFP_KERNEL); |
| 653 | if (unlikely(lrp == NULL)) { |
| 654 | status = -ENOMEM; |
Fred Isaman | 9e2dfdb | 2011-06-15 14:32:02 -0400 | [diff] [blame] | 655 | set_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags); |
| 656 | set_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags); |
Benny Halevy | 1ed3a85 | 2011-06-15 11:39:57 -0400 | [diff] [blame] | 657 | put_layout_hdr(lo); |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 658 | goto out; |
| 659 | } |
| 660 | |
| 661 | lrp->args.stateid = stateid; |
| 662 | lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id; |
| 663 | lrp->args.inode = ino; |
| 664 | lrp->clp = NFS_SERVER(ino)->nfs_client; |
| 665 | |
| 666 | status = nfs4_proc_layoutreturn(lrp); |
| 667 | out: |
| 668 | dprintk("<-- %s status: %d\n", __func__, status); |
| 669 | return status; |
| 670 | } |
| 671 | |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 672 | bool pnfs_roc(struct inode *ino) |
| 673 | { |
| 674 | struct pnfs_layout_hdr *lo; |
| 675 | struct pnfs_layout_segment *lseg, *tmp; |
| 676 | LIST_HEAD(tmp_list); |
| 677 | bool found = false; |
| 678 | |
| 679 | spin_lock(&ino->i_lock); |
| 680 | lo = NFS_I(ino)->layout; |
| 681 | if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) || |
| 682 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) |
| 683 | goto out_nolayout; |
| 684 | list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list) |
| 685 | if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { |
| 686 | mark_lseg_invalid(lseg, &tmp_list); |
| 687 | found = true; |
| 688 | } |
| 689 | if (!found) |
| 690 | goto out_nolayout; |
| 691 | lo->plh_block_lgets++; |
| 692 | get_layout_hdr(lo); /* matched in pnfs_roc_release */ |
| 693 | spin_unlock(&ino->i_lock); |
| 694 | pnfs_free_lseg_list(&tmp_list); |
| 695 | return true; |
| 696 | |
| 697 | out_nolayout: |
| 698 | spin_unlock(&ino->i_lock); |
| 699 | return false; |
| 700 | } |
| 701 | |
| 702 | void pnfs_roc_release(struct inode *ino) |
| 703 | { |
| 704 | struct pnfs_layout_hdr *lo; |
| 705 | |
| 706 | spin_lock(&ino->i_lock); |
| 707 | lo = NFS_I(ino)->layout; |
| 708 | lo->plh_block_lgets--; |
| 709 | put_layout_hdr_locked(lo); |
| 710 | spin_unlock(&ino->i_lock); |
| 711 | } |
| 712 | |
| 713 | void pnfs_roc_set_barrier(struct inode *ino, u32 barrier) |
| 714 | { |
| 715 | struct pnfs_layout_hdr *lo; |
| 716 | |
| 717 | spin_lock(&ino->i_lock); |
| 718 | lo = NFS_I(ino)->layout; |
| 719 | if ((int)(barrier - lo->plh_barrier) > 0) |
| 720 | lo->plh_barrier = barrier; |
| 721 | spin_unlock(&ino->i_lock); |
| 722 | } |
| 723 | |
| 724 | bool pnfs_roc_drain(struct inode *ino, u32 *barrier) |
| 725 | { |
| 726 | struct nfs_inode *nfsi = NFS_I(ino); |
| 727 | struct pnfs_layout_segment *lseg; |
| 728 | bool found = false; |
| 729 | |
| 730 | spin_lock(&ino->i_lock); |
| 731 | list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list) |
| 732 | if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { |
| 733 | found = true; |
| 734 | break; |
| 735 | } |
| 736 | if (!found) { |
| 737 | struct pnfs_layout_hdr *lo = nfsi->layout; |
| 738 | u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid); |
| 739 | |
| 740 | /* Since close does not return a layout stateid for use as |
| 741 | * a barrier, we choose the worst-case barrier. |
| 742 | */ |
| 743 | *barrier = current_seqid + atomic_read(&lo->plh_outstanding); |
| 744 | } |
| 745 | spin_unlock(&ino->i_lock); |
| 746 | return found; |
| 747 | } |
| 748 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 749 | /* |
| 750 | * Compare two layout segments for sorting into layout cache. |
| 751 | * We want to preferentially return RW over RO layouts, so ensure those |
| 752 | * are seen first. |
| 753 | */ |
| 754 | static s64 |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 755 | cmp_layout(struct pnfs_layout_range *l1, |
| 756 | struct pnfs_layout_range *l2) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 757 | { |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 758 | s64 d; |
| 759 | |
| 760 | /* high offset > low offset */ |
| 761 | d = l1->offset - l2->offset; |
| 762 | if (d) |
| 763 | return d; |
| 764 | |
| 765 | /* short length > long length */ |
| 766 | d = l2->length - l1->length; |
| 767 | if (d) |
| 768 | return d; |
| 769 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 770 | /* read > read/write */ |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 771 | return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 772 | } |
| 773 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 774 | static void |
| 775 | pnfs_insert_layout(struct pnfs_layout_hdr *lo, |
| 776 | struct pnfs_layout_segment *lseg) |
| 777 | { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 778 | struct pnfs_layout_segment *lp; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 779 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 780 | dprintk("%s:Begin\n", __func__); |
| 781 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 782 | assert_spin_locked(&lo->plh_inode->i_lock); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 783 | list_for_each_entry(lp, &lo->plh_segs, pls_list) { |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 784 | if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 785 | continue; |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 786 | list_add_tail(&lseg->pls_list, &lp->pls_list); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 787 | dprintk("%s: inserted lseg %p " |
| 788 | "iomode %d offset %llu length %llu before " |
| 789 | "lp %p iomode %d offset %llu length %llu\n", |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 790 | __func__, lseg, lseg->pls_range.iomode, |
| 791 | lseg->pls_range.offset, lseg->pls_range.length, |
| 792 | lp, lp->pls_range.iomode, lp->pls_range.offset, |
| 793 | lp->pls_range.length); |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 794 | goto out; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 795 | } |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 796 | list_add_tail(&lseg->pls_list, &lo->plh_segs); |
| 797 | dprintk("%s: inserted lseg %p " |
| 798 | "iomode %d offset %llu length %llu at tail\n", |
| 799 | __func__, lseg, lseg->pls_range.iomode, |
| 800 | lseg->pls_range.offset, lseg->pls_range.length); |
| 801 | out: |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 802 | get_layout_hdr(lo); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 803 | |
| 804 | dprintk("%s:Return\n", __func__); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | static struct pnfs_layout_hdr * |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 808 | alloc_init_layout_hdr(struct inode *ino, gfp_t gfp_flags) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 809 | { |
| 810 | struct pnfs_layout_hdr *lo; |
| 811 | |
Benny Halevy | 636fb9c | 2011-05-22 19:51:33 +0300 | [diff] [blame] | 812 | lo = pnfs_alloc_layout_hdr(ino, gfp_flags); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 813 | if (!lo) |
| 814 | return NULL; |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 815 | atomic_set(&lo->plh_refcount, 1); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 816 | INIT_LIST_HEAD(&lo->plh_layouts); |
| 817 | INIT_LIST_HEAD(&lo->plh_segs); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 818 | INIT_LIST_HEAD(&lo->plh_bulk_recall); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 819 | lo->plh_inode = ino; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 820 | return lo; |
| 821 | } |
| 822 | |
| 823 | static struct pnfs_layout_hdr * |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 824 | pnfs_find_alloc_layout(struct inode *ino, gfp_t gfp_flags) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 825 | { |
| 826 | struct nfs_inode *nfsi = NFS_I(ino); |
| 827 | struct pnfs_layout_hdr *new = NULL; |
| 828 | |
| 829 | dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout); |
| 830 | |
| 831 | assert_spin_locked(&ino->i_lock); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 832 | if (nfsi->layout) { |
| 833 | if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags)) |
| 834 | return NULL; |
| 835 | else |
| 836 | return nfsi->layout; |
| 837 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 838 | spin_unlock(&ino->i_lock); |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 839 | new = alloc_init_layout_hdr(ino, gfp_flags); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 840 | spin_lock(&ino->i_lock); |
| 841 | |
| 842 | if (likely(nfsi->layout == NULL)) /* Won the race? */ |
| 843 | nfsi->layout = new; |
| 844 | else |
Benny Halevy | 636fb9c | 2011-05-22 19:51:33 +0300 | [diff] [blame] | 845 | pnfs_free_layout_hdr(new); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 846 | return nfsi->layout; |
| 847 | } |
| 848 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 849 | /* |
| 850 | * iomode matching rules: |
| 851 | * iomode lseg match |
| 852 | * ----- ----- ----- |
| 853 | * ANY READ true |
| 854 | * ANY RW true |
| 855 | * RW READ false |
| 856 | * RW RW true |
| 857 | * READ READ true |
| 858 | * READ RW true |
| 859 | */ |
| 860 | static int |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 861 | is_matching_lseg(struct pnfs_layout_range *ls_range, |
| 862 | struct pnfs_layout_range *range) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 863 | { |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 864 | struct pnfs_layout_range range1; |
| 865 | |
| 866 | if ((range->iomode == IOMODE_RW && |
| 867 | ls_range->iomode != IOMODE_RW) || |
| 868 | !lo_seg_intersecting(ls_range, range)) |
| 869 | return 0; |
| 870 | |
| 871 | /* range1 covers only the first byte in the range */ |
| 872 | range1 = *range; |
| 873 | range1.length = 1; |
| 874 | return lo_seg_contained(ls_range, &range1); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* |
| 878 | * lookup range in layout |
| 879 | */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 880 | static struct pnfs_layout_segment * |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 881 | pnfs_find_lseg(struct pnfs_layout_hdr *lo, |
| 882 | struct pnfs_layout_range *range) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 883 | { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 884 | struct pnfs_layout_segment *lseg, *ret = NULL; |
| 885 | |
| 886 | dprintk("%s:Begin\n", __func__); |
| 887 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 888 | assert_spin_locked(&lo->plh_inode->i_lock); |
| 889 | list_for_each_entry(lseg, &lo->plh_segs, pls_list) { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 890 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) && |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 891 | is_matching_lseg(&lseg->pls_range, range)) { |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 892 | ret = get_lseg(lseg); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 893 | break; |
| 894 | } |
Benny Halevy | d771e3a | 2011-06-14 16:30:16 -0400 | [diff] [blame] | 895 | if (lseg->pls_range.offset > range->offset) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 896 | break; |
| 897 | } |
| 898 | |
| 899 | dprintk("%s:Return lseg %p ref %d\n", |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 900 | __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 901 | return ret; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | /* |
| 905 | * Layout segment is retreived from the server if not cached. |
| 906 | * The appropriate layout segment is referenced and returned to the caller. |
| 907 | */ |
| 908 | struct pnfs_layout_segment * |
| 909 | pnfs_update_layout(struct inode *ino, |
| 910 | struct nfs_open_context *ctx, |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 911 | loff_t pos, |
| 912 | u64 count, |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 913 | enum pnfs_iomode iomode, |
| 914 | gfp_t gfp_flags) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 915 | { |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 916 | struct pnfs_layout_range arg = { |
| 917 | .iomode = iomode, |
| 918 | .offset = pos, |
| 919 | .length = count, |
| 920 | }; |
Benny Halevy | 707ed5f | 2011-05-22 19:47:46 +0300 | [diff] [blame] | 921 | unsigned pg_offset; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 922 | struct nfs_inode *nfsi = NFS_I(ino); |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 923 | struct nfs_client *clp = NFS_SERVER(ino)->nfs_client; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 924 | struct pnfs_layout_hdr *lo; |
| 925 | struct pnfs_layout_segment *lseg = NULL; |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 926 | bool first = false; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 927 | |
| 928 | if (!pnfs_enabled_sb(NFS_SERVER(ino))) |
| 929 | return NULL; |
| 930 | spin_lock(&ino->i_lock); |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 931 | lo = pnfs_find_alloc_layout(ino, gfp_flags); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 932 | if (lo == NULL) { |
| 933 | dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__); |
| 934 | goto out_unlock; |
| 935 | } |
| 936 | |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 937 | /* Do we even need to bother with this? */ |
| 938 | if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) || |
| 939 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
| 940 | dprintk("%s matches recall, use MDS\n", __func__); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 941 | goto out_unlock; |
| 942 | } |
| 943 | |
| 944 | /* if LAYOUTGET already failed once we don't try again */ |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 945 | if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags)) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 946 | goto out_unlock; |
| 947 | |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 948 | /* Check to see if the layout for the given range already exists */ |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 949 | lseg = pnfs_find_lseg(lo, &arg); |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 950 | if (lseg) |
| 951 | goto out_unlock; |
| 952 | |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 953 | if (pnfs_layoutgets_blocked(lo, NULL, 0)) |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 954 | goto out_unlock; |
| 955 | atomic_inc(&lo->plh_outstanding); |
| 956 | |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 957 | get_layout_hdr(lo); |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 958 | if (list_empty(&lo->plh_segs)) |
| 959 | first = true; |
| 960 | spin_unlock(&ino->i_lock); |
| 961 | if (first) { |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 962 | /* The lo must be on the clp list if there is any |
| 963 | * chance of a CB_LAYOUTRECALL(FILE) coming in. |
| 964 | */ |
| 965 | spin_lock(&clp->cl_lock); |
| 966 | BUG_ON(!list_empty(&lo->plh_layouts)); |
| 967 | list_add_tail(&lo->plh_layouts, &clp->cl_layouts); |
| 968 | spin_unlock(&clp->cl_lock); |
| 969 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 970 | |
Benny Halevy | 707ed5f | 2011-05-22 19:47:46 +0300 | [diff] [blame] | 971 | pg_offset = arg.offset & ~PAGE_CACHE_MASK; |
| 972 | if (pg_offset) { |
| 973 | arg.offset -= pg_offset; |
| 974 | arg.length += pg_offset; |
| 975 | } |
| 976 | arg.length = PAGE_CACHE_ALIGN(arg.length); |
| 977 | |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 978 | lseg = send_layoutget(lo, ctx, &arg, gfp_flags); |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 979 | if (!lseg && first) { |
| 980 | spin_lock(&clp->cl_lock); |
| 981 | list_del_init(&lo->plh_layouts); |
| 982 | spin_unlock(&clp->cl_lock); |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 983 | } |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 984 | atomic_dec(&lo->plh_outstanding); |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 985 | put_layout_hdr(lo); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 986 | out: |
| 987 | dprintk("%s end, state 0x%lx lseg %p\n", __func__, |
Andy Adamson | bf9c138 | 2011-03-01 01:34:07 +0000 | [diff] [blame] | 988 | nfsi->layout ? nfsi->layout->plh_flags : -1, lseg); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 989 | return lseg; |
| 990 | out_unlock: |
| 991 | spin_unlock(&ino->i_lock); |
| 992 | goto out; |
| 993 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 994 | |
| 995 | int |
| 996 | pnfs_layout_process(struct nfs4_layoutget *lgp) |
| 997 | { |
| 998 | struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout; |
| 999 | struct nfs4_layoutget_res *res = &lgp->res; |
| 1000 | struct pnfs_layout_segment *lseg; |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 1001 | struct inode *ino = lo->plh_inode; |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 1002 | struct nfs_client *clp = NFS_SERVER(ino)->nfs_client; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1003 | int status = 0; |
| 1004 | |
| 1005 | /* Inject layout blob into I/O device driver */ |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 1006 | lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1007 | if (!lseg || IS_ERR(lseg)) { |
| 1008 | if (!lseg) |
| 1009 | status = -ENOMEM; |
| 1010 | else |
| 1011 | status = PTR_ERR(lseg); |
| 1012 | dprintk("%s: Could not allocate layout: error %d\n", |
| 1013 | __func__, status); |
| 1014 | goto out; |
| 1015 | } |
| 1016 | |
| 1017 | spin_lock(&ino->i_lock); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 1018 | if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) || |
| 1019 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
| 1020 | dprintk("%s forget reply due to recall\n", __func__); |
| 1021 | goto out_forget_reply; |
| 1022 | } |
| 1023 | |
| 1024 | if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) { |
| 1025 | dprintk("%s forget reply due to state\n", __func__); |
| 1026 | goto out_forget_reply; |
| 1027 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1028 | init_lseg(lo, lseg); |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 1029 | lseg->pls_range = res->range; |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 1030 | *lgp->lsegpp = get_lseg(lseg); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1031 | pnfs_insert_layout(lo, lseg); |
| 1032 | |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1033 | if (res->return_on_close) { |
| 1034 | set_bit(NFS_LSEG_ROC, &lseg->pls_flags); |
| 1035 | set_bit(NFS_LAYOUT_ROC, &lo->plh_flags); |
| 1036 | } |
| 1037 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1038 | /* Done processing layoutget. Set the layout stateid */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 1039 | pnfs_set_layout_stateid(lo, &res->stateid, false); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1040 | spin_unlock(&ino->i_lock); |
| 1041 | out: |
| 1042 | return status; |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 1043 | |
| 1044 | out_forget_reply: |
| 1045 | spin_unlock(&ino->i_lock); |
| 1046 | lseg->pls_layout = lo; |
| 1047 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
| 1048 | goto out; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1049 | } |
| 1050 | |
Benny Halevy | 18ad0a9 | 2011-05-25 21:03:56 +0300 | [diff] [blame] | 1051 | bool |
Benny Halevy | dfed206 | 2011-05-25 20:25:22 +0300 | [diff] [blame] | 1052 | pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev, |
| 1053 | struct nfs_page *req) |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 1054 | { |
Benny Halevy | dfed206 | 2011-05-25 20:25:22 +0300 | [diff] [blame] | 1055 | enum pnfs_iomode access_type; |
| 1056 | gfp_t gfp_flags; |
| 1057 | |
| 1058 | /* We assume that pg_ioflags == 0 iff we're reading a page */ |
| 1059 | if (pgio->pg_ioflags == 0) { |
| 1060 | access_type = IOMODE_READ; |
| 1061 | gfp_flags = GFP_KERNEL; |
| 1062 | } else { |
| 1063 | access_type = IOMODE_RW; |
| 1064 | gfp_flags = GFP_NOFS; |
| 1065 | } |
| 1066 | |
Trond Myklebust | 8f7d5ef | 2011-06-10 13:30:22 -0400 | [diff] [blame] | 1067 | if (pgio->pg_lseg == NULL) { |
| 1068 | if (pgio->pg_count != prev->wb_bytes) |
| 1069 | return true; |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 1070 | /* This is first coelesce call for a series of nfs_pages */ |
| 1071 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
| 1072 | prev->wb_context, |
Trond Myklebust | 8f7d5ef | 2011-06-10 13:30:22 -0400 | [diff] [blame] | 1073 | req_offset(prev), |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1074 | pgio->pg_count, |
Benny Halevy | dfed206 | 2011-05-25 20:25:22 +0300 | [diff] [blame] | 1075 | access_type, |
| 1076 | gfp_flags); |
Trond Myklebust | 8f7d5ef | 2011-06-10 13:30:22 -0400 | [diff] [blame] | 1077 | if (pgio->pg_lseg == NULL) |
| 1078 | return true; |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 1079 | } |
Benny Halevy | 89a58e3 | 2011-05-25 20:54:40 +0300 | [diff] [blame] | 1080 | |
Trond Myklebust | 19982ba | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1081 | /* |
| 1082 | * Test if a nfs_page is fully contained in the pnfs_layout_range. |
| 1083 | * Note that this test makes several assumptions: |
| 1084 | * - that the previous nfs_page in the struct nfs_pageio_descriptor |
| 1085 | * is known to lie within the range. |
| 1086 | * - that the nfs_page being tested is known to be contiguous with the |
| 1087 | * previous nfs_page. |
| 1088 | * - Layout ranges are page aligned, so we only have to test the |
| 1089 | * start offset of the request. |
| 1090 | * |
| 1091 | * Please also note that 'end_offset' is actually the offset of the |
| 1092 | * first byte that lies outside the pnfs_layout_range. FIXME? |
| 1093 | * |
| 1094 | */ |
| 1095 | return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset, |
| 1096 | pgio->pg_lseg->pls_range.length); |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 1097 | } |
Benny Halevy | 89a58e3 | 2011-05-25 20:54:40 +0300 | [diff] [blame] | 1098 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_test); |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 1099 | |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 1100 | /* |
| 1101 | * Called by non rpc-based layout drivers |
| 1102 | */ |
| 1103 | int |
| 1104 | pnfs_ld_write_done(struct nfs_write_data *data) |
Fred Isaman | 94ad1c8 | 2011-03-01 01:34:14 +0000 | [diff] [blame] | 1105 | { |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 1106 | int status; |
Fred Isaman | 94ad1c8 | 2011-03-01 01:34:14 +0000 | [diff] [blame] | 1107 | |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 1108 | if (!data->pnfs_error) { |
| 1109 | pnfs_set_layoutcommit(data); |
| 1110 | data->mds_ops->rpc_call_done(&data->task, data); |
| 1111 | data->mds_ops->rpc_release(data); |
| 1112 | return 0; |
Fred Isaman | 44b8379 | 2011-03-03 15:13:44 +0000 | [diff] [blame] | 1113 | } |
Fred Isaman | 44b8379 | 2011-03-03 15:13:44 +0000 | [diff] [blame] | 1114 | |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 1115 | dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__, |
| 1116 | data->pnfs_error); |
| 1117 | status = nfs_initiate_write(data, NFS_CLIENT(data->inode), |
| 1118 | data->mds_ops, NFS_FILE_SYNC); |
| 1119 | return status ? : -EAGAIN; |
Fred Isaman | 44b8379 | 2011-03-03 15:13:44 +0000 | [diff] [blame] | 1120 | } |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 1121 | EXPORT_SYMBOL_GPL(pnfs_ld_write_done); |
Fred Isaman | 44b8379 | 2011-03-03 15:13:44 +0000 | [diff] [blame] | 1122 | |
Andy Adamson | 0382b74 | 2011-03-03 15:13:45 +0000 | [diff] [blame] | 1123 | enum pnfs_try_status |
| 1124 | pnfs_try_to_write_data(struct nfs_write_data *wdata, |
| 1125 | const struct rpc_call_ops *call_ops, int how) |
| 1126 | { |
| 1127 | struct inode *inode = wdata->inode; |
| 1128 | enum pnfs_try_status trypnfs; |
| 1129 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 1130 | |
| 1131 | wdata->mds_ops = call_ops; |
| 1132 | |
| 1133 | dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__, |
| 1134 | inode->i_ino, wdata->args.count, wdata->args.offset, how); |
| 1135 | |
| 1136 | trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how); |
| 1137 | if (trypnfs == PNFS_NOT_ATTEMPTED) { |
| 1138 | put_lseg(wdata->lseg); |
| 1139 | wdata->lseg = NULL; |
| 1140 | } else |
| 1141 | nfs_inc_stats(inode, NFSIOS_PNFS_WRITE); |
| 1142 | |
| 1143 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
| 1144 | return trypnfs; |
| 1145 | } |
| 1146 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1147 | /* |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 1148 | * Called by non rpc-based layout drivers |
| 1149 | */ |
| 1150 | int |
| 1151 | pnfs_ld_read_done(struct nfs_read_data *data) |
| 1152 | { |
| 1153 | int status; |
| 1154 | |
| 1155 | if (!data->pnfs_error) { |
| 1156 | __nfs4_read_done_cb(data); |
| 1157 | data->mds_ops->rpc_call_done(&data->task, data); |
| 1158 | data->mds_ops->rpc_release(data); |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__, |
| 1163 | data->pnfs_error); |
| 1164 | status = nfs_initiate_read(data, NFS_CLIENT(data->inode), |
| 1165 | data->mds_ops); |
| 1166 | return status ? : -EAGAIN; |
| 1167 | } |
| 1168 | EXPORT_SYMBOL_GPL(pnfs_ld_read_done); |
| 1169 | |
| 1170 | /* |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 1171 | * Call the appropriate parallel I/O subsystem read function. |
| 1172 | */ |
| 1173 | enum pnfs_try_status |
| 1174 | pnfs_try_to_read_data(struct nfs_read_data *rdata, |
| 1175 | const struct rpc_call_ops *call_ops) |
| 1176 | { |
| 1177 | struct inode *inode = rdata->inode; |
| 1178 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 1179 | enum pnfs_try_status trypnfs; |
| 1180 | |
| 1181 | rdata->mds_ops = call_ops; |
| 1182 | |
| 1183 | dprintk("%s: Reading ino:%lu %u@%llu\n", |
| 1184 | __func__, inode->i_ino, rdata->args.count, rdata->args.offset); |
| 1185 | |
| 1186 | trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata); |
| 1187 | if (trypnfs == PNFS_NOT_ATTEMPTED) { |
| 1188 | put_lseg(rdata->lseg); |
| 1189 | rdata->lseg = NULL; |
| 1190 | } else { |
| 1191 | nfs_inc_stats(inode, NFSIOS_PNFS_READ); |
| 1192 | } |
| 1193 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
| 1194 | return trypnfs; |
| 1195 | } |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1196 | |
| 1197 | /* |
| 1198 | * Currently there is only one (whole file) write lseg. |
| 1199 | */ |
| 1200 | static struct pnfs_layout_segment *pnfs_list_write_lseg(struct inode *inode) |
| 1201 | { |
| 1202 | struct pnfs_layout_segment *lseg, *rv = NULL; |
| 1203 | |
| 1204 | list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) |
| 1205 | if (lseg->pls_range.iomode == IOMODE_RW) |
| 1206 | rv = lseg; |
| 1207 | return rv; |
| 1208 | } |
| 1209 | |
| 1210 | void |
| 1211 | pnfs_set_layoutcommit(struct nfs_write_data *wdata) |
| 1212 | { |
| 1213 | struct nfs_inode *nfsi = NFS_I(wdata->inode); |
Vitaliy Gusev | 4b8ee2b | 2011-05-20 01:34:46 +0400 | [diff] [blame] | 1214 | loff_t end_pos = wdata->mds_offset + wdata->res.count; |
Weston Andros Adamson | 79a48a1 | 2011-04-13 10:53:51 -0400 | [diff] [blame] | 1215 | bool mark_as_dirty = false; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1216 | |
| 1217 | spin_lock(&nfsi->vfs_inode.i_lock); |
| 1218 | if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) { |
| 1219 | /* references matched in nfs4_layoutcommit_release */ |
| 1220 | get_lseg(wdata->lseg); |
| 1221 | wdata->lseg->pls_lc_cred = |
| 1222 | get_rpccred(wdata->args.context->state->owner->so_cred); |
Weston Andros Adamson | 79a48a1 | 2011-04-13 10:53:51 -0400 | [diff] [blame] | 1223 | mark_as_dirty = true; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1224 | dprintk("%s: Set layoutcommit for inode %lu ", |
| 1225 | __func__, wdata->inode->i_ino); |
| 1226 | } |
| 1227 | if (end_pos > wdata->lseg->pls_end_pos) |
| 1228 | wdata->lseg->pls_end_pos = end_pos; |
| 1229 | spin_unlock(&nfsi->vfs_inode.i_lock); |
Weston Andros Adamson | 79a48a1 | 2011-04-13 10:53:51 -0400 | [diff] [blame] | 1230 | |
| 1231 | /* if pnfs_layoutcommit_inode() runs between inode locks, the next one |
| 1232 | * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */ |
| 1233 | if (mark_as_dirty) |
| 1234 | mark_inode_dirty_sync(wdata->inode); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1235 | } |
| 1236 | EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit); |
| 1237 | |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 1238 | /* |
| 1239 | * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and |
| 1240 | * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough |
| 1241 | * data to disk to allow the server to recover the data if it crashes. |
| 1242 | * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag |
| 1243 | * is off, and a COMMIT is sent to a data server, or |
| 1244 | * if WRITEs to a data server return NFS_DATA_SYNC. |
| 1245 | */ |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1246 | int |
Andy Adamson | ef31153 | 2011-03-12 02:58:10 -0500 | [diff] [blame] | 1247 | pnfs_layoutcommit_inode(struct inode *inode, bool sync) |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1248 | { |
| 1249 | struct nfs4_layoutcommit_data *data; |
| 1250 | struct nfs_inode *nfsi = NFS_I(inode); |
| 1251 | struct pnfs_layout_segment *lseg; |
| 1252 | struct rpc_cred *cred; |
| 1253 | loff_t end_pos; |
| 1254 | int status = 0; |
| 1255 | |
| 1256 | dprintk("--> %s inode %lu\n", __func__, inode->i_ino); |
| 1257 | |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 1258 | if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) |
| 1259 | return 0; |
| 1260 | |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1261 | /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */ |
| 1262 | data = kzalloc(sizeof(*data), GFP_NOFS); |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 1263 | if (!data) { |
| 1264 | mark_inode_dirty_sync(inode); |
| 1265 | status = -ENOMEM; |
| 1266 | goto out; |
| 1267 | } |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1268 | |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 1269 | spin_lock(&inode->i_lock); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1270 | if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) { |
| 1271 | spin_unlock(&inode->i_lock); |
| 1272 | kfree(data); |
| 1273 | goto out; |
| 1274 | } |
| 1275 | /* |
| 1276 | * Currently only one (whole file) write lseg which is referenced |
| 1277 | * in pnfs_set_layoutcommit and will be found. |
| 1278 | */ |
| 1279 | lseg = pnfs_list_write_lseg(inode); |
| 1280 | |
| 1281 | end_pos = lseg->pls_end_pos; |
| 1282 | cred = lseg->pls_lc_cred; |
| 1283 | lseg->pls_end_pos = 0; |
| 1284 | lseg->pls_lc_cred = NULL; |
| 1285 | |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 1286 | memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data, |
| 1287 | sizeof(nfsi->layout->plh_stateid.data)); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1288 | spin_unlock(&inode->i_lock); |
| 1289 | |
| 1290 | data->args.inode = inode; |
| 1291 | data->lseg = lseg; |
| 1292 | data->cred = cred; |
| 1293 | nfs_fattr_init(&data->fattr); |
| 1294 | data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask; |
| 1295 | data->res.fattr = &data->fattr; |
| 1296 | data->args.lastbytewritten = end_pos - 1; |
| 1297 | data->res.server = NFS_SERVER(inode); |
| 1298 | |
| 1299 | status = nfs4_proc_layoutcommit(data, sync); |
| 1300 | out: |
| 1301 | dprintk("<-- %s status %d\n", __func__, status); |
| 1302 | return status; |
| 1303 | } |