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