Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 1 | /* |
| 2 | * pNFS functions to call and manage layout drivers. |
| 3 | * |
| 4 | * Copyright (c) 2002 [year of first publication] |
| 5 | * The Regents of the University of Michigan |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * Dean Hildebrand <dhildebz@umich.edu> |
| 9 | * |
| 10 | * Permission is granted to use, copy, create derivative works, and |
| 11 | * redistribute this software and such derivative works for any purpose, |
| 12 | * so long as the name of the University of Michigan is not used in |
| 13 | * any advertising or publicity pertaining to the use or distribution |
| 14 | * of this software without specific, written prior authorization. If |
| 15 | * the above copyright notice or any other identification of the |
| 16 | * University of Michigan is included in any copy of any portion of |
| 17 | * this software, then the disclaimer below must also be included. |
| 18 | * |
| 19 | * This software is provided as is, without representation or warranty |
| 20 | * of any kind either express or implied, including without limitation |
| 21 | * the implied warranties of merchantability, fitness for a particular |
| 22 | * purpose, or noninfringement. The Regents of the University of |
| 23 | * Michigan shall not be liable for any damages, including special, |
| 24 | * indirect, incidental, or consequential damages, with respect to any |
| 25 | * claim arising out of or in connection with the use of the software, |
| 26 | * even if it has been or is hereafter advised of the possibility of |
| 27 | * such damages. |
| 28 | */ |
| 29 | |
| 30 | #include <linux/nfs_fs.h> |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 31 | #include "internal.h" |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 32 | #include "pnfs.h" |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 33 | #include "iostat.h" |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 34 | |
| 35 | #define NFSDBG_FACILITY NFSDBG_PNFS |
| 36 | |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 37 | /* Locking: |
| 38 | * |
| 39 | * pnfs_spinlock: |
| 40 | * protects pnfs_modules_tbl. |
| 41 | */ |
| 42 | static DEFINE_SPINLOCK(pnfs_spinlock); |
| 43 | |
| 44 | /* |
| 45 | * pnfs_modules_tbl holds all pnfs modules |
| 46 | */ |
| 47 | static LIST_HEAD(pnfs_modules_tbl); |
| 48 | |
| 49 | /* Return the registered pnfs layout driver module matching given id */ |
| 50 | static struct pnfs_layoutdriver_type * |
| 51 | find_pnfs_driver_locked(u32 id) |
| 52 | { |
| 53 | struct pnfs_layoutdriver_type *local; |
| 54 | |
| 55 | list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid) |
| 56 | if (local->id == id) |
| 57 | goto out; |
| 58 | local = NULL; |
| 59 | out: |
| 60 | dprintk("%s: Searching for id %u, found %p\n", __func__, id, local); |
| 61 | return local; |
| 62 | } |
| 63 | |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 64 | static struct pnfs_layoutdriver_type * |
| 65 | find_pnfs_driver(u32 id) |
| 66 | { |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 67 | struct pnfs_layoutdriver_type *local; |
| 68 | |
| 69 | spin_lock(&pnfs_spinlock); |
| 70 | local = find_pnfs_driver_locked(id); |
| 71 | spin_unlock(&pnfs_spinlock); |
| 72 | return local; |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
| 76 | unset_pnfs_layoutdriver(struct nfs_server *nfss) |
| 77 | { |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 78 | if (nfss->pnfs_curr_ld) |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 79 | module_put(nfss->pnfs_curr_ld->owner); |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 80 | nfss->pnfs_curr_ld = NULL; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Try to set the server's pnfs module to the pnfs layout type specified by id. |
| 85 | * Currently only one pNFS layout driver per filesystem is supported. |
| 86 | * |
| 87 | * @id layout type. Zero (illegal layout type) indicates pNFS not in use. |
| 88 | */ |
| 89 | void |
| 90 | set_pnfs_layoutdriver(struct nfs_server *server, u32 id) |
| 91 | { |
| 92 | struct pnfs_layoutdriver_type *ld_type = NULL; |
| 93 | |
| 94 | if (id == 0) |
| 95 | goto out_no_driver; |
| 96 | if (!(server->nfs_client->cl_exchange_flags & |
| 97 | (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) { |
| 98 | printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__, |
| 99 | id, server->nfs_client->cl_exchange_flags); |
| 100 | goto out_no_driver; |
| 101 | } |
| 102 | ld_type = find_pnfs_driver(id); |
| 103 | if (!ld_type) { |
| 104 | request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id); |
| 105 | ld_type = find_pnfs_driver(id); |
| 106 | if (!ld_type) { |
| 107 | dprintk("%s: No pNFS module found for %u.\n", |
| 108 | __func__, id); |
| 109 | goto out_no_driver; |
| 110 | } |
| 111 | } |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 112 | if (!try_module_get(ld_type->owner)) { |
| 113 | dprintk("%s: Could not grab reference on module\n", __func__); |
| 114 | goto out_no_driver; |
| 115 | } |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 116 | server->pnfs_curr_ld = ld_type; |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 117 | |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 118 | dprintk("%s: pNFS module for %u set\n", __func__, id); |
| 119 | return; |
| 120 | |
| 121 | out_no_driver: |
| 122 | dprintk("%s: Using NFSv4 I/O\n", __func__); |
| 123 | server->pnfs_curr_ld = NULL; |
| 124 | } |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 125 | |
| 126 | int |
| 127 | pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type) |
| 128 | { |
| 129 | int status = -EINVAL; |
| 130 | struct pnfs_layoutdriver_type *tmp; |
| 131 | |
| 132 | if (ld_type->id == 0) { |
| 133 | printk(KERN_ERR "%s id 0 is reserved\n", __func__); |
| 134 | return status; |
| 135 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 136 | if (!ld_type->alloc_lseg || !ld_type->free_lseg) { |
| 137 | printk(KERN_ERR "%s Layout driver must provide " |
| 138 | "alloc_lseg and free_lseg.\n", __func__); |
| 139 | return status; |
| 140 | } |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 141 | |
| 142 | spin_lock(&pnfs_spinlock); |
| 143 | tmp = find_pnfs_driver_locked(ld_type->id); |
| 144 | if (!tmp) { |
| 145 | list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl); |
| 146 | status = 0; |
| 147 | dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id, |
| 148 | ld_type->name); |
| 149 | } else { |
| 150 | printk(KERN_ERR "%s Module with id %d already loaded!\n", |
| 151 | __func__, ld_type->id); |
| 152 | } |
| 153 | spin_unlock(&pnfs_spinlock); |
| 154 | |
| 155 | return status; |
| 156 | } |
| 157 | EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver); |
| 158 | |
| 159 | void |
| 160 | pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type) |
| 161 | { |
| 162 | dprintk("%s Deregistering id:%u\n", __func__, ld_type->id); |
| 163 | spin_lock(&pnfs_spinlock); |
| 164 | list_del(&ld_type->pnfs_tblid); |
| 165 | spin_unlock(&pnfs_spinlock); |
| 166 | } |
| 167 | EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 168 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 169 | /* |
| 170 | * pNFS client layout cache |
| 171 | */ |
| 172 | |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 173 | /* Need to hold i_lock if caller does not already hold reference */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 174 | void |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 175 | get_layout_hdr(struct pnfs_layout_hdr *lo) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 176 | { |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 177 | atomic_inc(&lo->plh_refcount); |
| 178 | } |
| 179 | |
| 180 | static void |
| 181 | destroy_layout_hdr(struct pnfs_layout_hdr *lo) |
| 182 | { |
| 183 | dprintk("%s: freeing layout cache %p\n", __func__, lo); |
| 184 | BUG_ON(!list_empty(&lo->plh_layouts)); |
| 185 | NFS_I(lo->plh_inode)->layout = NULL; |
| 186 | kfree(lo); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static void |
| 190 | put_layout_hdr_locked(struct pnfs_layout_hdr *lo) |
| 191 | { |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 192 | if (atomic_dec_and_test(&lo->plh_refcount)) |
| 193 | destroy_layout_hdr(lo); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 196 | void |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 197 | put_layout_hdr(struct pnfs_layout_hdr *lo) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 198 | { |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 199 | struct inode *inode = lo->plh_inode; |
| 200 | |
| 201 | if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) { |
| 202 | destroy_layout_hdr(lo); |
| 203 | spin_unlock(&inode->i_lock); |
| 204 | } |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void |
| 208 | init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg) |
| 209 | { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 210 | INIT_LIST_HEAD(&lseg->pls_list); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 211 | atomic_set(&lseg->pls_refcount, 1); |
| 212 | smp_mb(); |
| 213 | set_bit(NFS_LSEG_VALID, &lseg->pls_flags); |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 214 | lseg->pls_layout = lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 217 | static void free_lseg(struct pnfs_layout_segment *lseg) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 218 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 219 | struct inode *ino = lseg->pls_layout->plh_inode; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 220 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 221 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
Fred Isaman | 52fabd7 | 2011-01-06 11:36:18 +0000 | [diff] [blame] | 222 | /* Matched by get_layout_hdr in pnfs_insert_layout */ |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 223 | put_layout_hdr(NFS_I(ino)->layout); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 226 | static void |
| 227 | put_lseg_common(struct pnfs_layout_segment *lseg) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 228 | { |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 229 | struct inode *inode = lseg->pls_layout->plh_inode; |
| 230 | |
| 231 | BUG_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
| 232 | list_del_init(&lseg->pls_list); |
| 233 | if (list_empty(&lseg->pls_layout->plh_segs)) { |
| 234 | set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags); |
| 235 | /* Matched by initial refcount set in alloc_init_layout_hdr */ |
| 236 | put_layout_hdr_locked(lseg->pls_layout); |
| 237 | } |
| 238 | rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq); |
| 239 | } |
| 240 | |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 241 | void |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 242 | put_lseg(struct pnfs_layout_segment *lseg) |
| 243 | { |
| 244 | struct inode *inode; |
| 245 | |
| 246 | if (!lseg) |
| 247 | return; |
| 248 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 249 | dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg, |
| 250 | atomic_read(&lseg->pls_refcount), |
| 251 | test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 252 | inode = lseg->pls_layout->plh_inode; |
| 253 | if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) { |
| 254 | LIST_HEAD(free_me); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 255 | |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 256 | put_lseg_common(lseg); |
| 257 | list_add(&lseg->pls_list, &free_me); |
| 258 | spin_unlock(&inode->i_lock); |
| 259 | pnfs_free_lseg_list(&free_me); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 260 | } |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 261 | } |
Fred Isaman | e0c2b38 | 2011-03-23 13:27:53 +0000 | [diff] [blame] | 262 | EXPORT_SYMBOL_GPL(put_lseg); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 263 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 264 | static bool |
| 265 | should_free_lseg(u32 lseg_iomode, u32 recall_iomode) |
| 266 | { |
| 267 | return (recall_iomode == IOMODE_ANY || |
| 268 | lseg_iomode == recall_iomode); |
| 269 | } |
| 270 | |
| 271 | /* Returns 1 if lseg is removed from list, 0 otherwise */ |
| 272 | static int mark_lseg_invalid(struct pnfs_layout_segment *lseg, |
| 273 | struct list_head *tmp_list) |
| 274 | { |
| 275 | int rv = 0; |
| 276 | |
| 277 | if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) { |
| 278 | /* Remove the reference keeping the lseg in the |
| 279 | * list. It will now be removed when all |
| 280 | * outstanding io is finished. |
| 281 | */ |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 282 | dprintk("%s: lseg %p ref %d\n", __func__, lseg, |
| 283 | atomic_read(&lseg->pls_refcount)); |
| 284 | if (atomic_dec_and_test(&lseg->pls_refcount)) { |
| 285 | put_lseg_common(lseg); |
| 286 | list_add(&lseg->pls_list, tmp_list); |
| 287 | rv = 1; |
| 288 | } |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 289 | } |
| 290 | return rv; |
| 291 | } |
| 292 | |
| 293 | /* Returns count of number of matching invalid lsegs remaining in list |
| 294 | * after call. |
| 295 | */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 296 | int |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 297 | mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo, |
| 298 | struct list_head *tmp_list, |
| 299 | u32 iomode) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 300 | { |
| 301 | struct pnfs_layout_segment *lseg, *next; |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 302 | int invalid = 0, removed = 0; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 303 | |
| 304 | dprintk("%s:Begin lo %p\n", __func__, lo); |
| 305 | |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 306 | if (list_empty(&lo->plh_segs)) { |
| 307 | if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) |
| 308 | put_layout_hdr_locked(lo); |
| 309 | return 0; |
| 310 | } |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 311 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) |
| 312 | if (should_free_lseg(lseg->pls_range.iomode, iomode)) { |
| 313 | dprintk("%s: freeing lseg %p iomode %d " |
| 314 | "offset %llu length %llu\n", __func__, |
| 315 | lseg, lseg->pls_range.iomode, lseg->pls_range.offset, |
| 316 | lseg->pls_range.length); |
| 317 | invalid++; |
| 318 | removed += mark_lseg_invalid(lseg, tmp_list); |
| 319 | } |
| 320 | dprintk("%s:Return %i\n", __func__, invalid - removed); |
| 321 | return invalid - removed; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 322 | } |
| 323 | |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 324 | /* note free_me must contain lsegs from a single layout_hdr */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 325 | void |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 326 | pnfs_free_lseg_list(struct list_head *free_me) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 327 | { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 328 | struct pnfs_layout_segment *lseg, *tmp; |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 329 | struct pnfs_layout_hdr *lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 330 | |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 331 | if (list_empty(free_me)) |
| 332 | return; |
| 333 | |
| 334 | lo = list_first_entry(free_me, struct pnfs_layout_segment, |
| 335 | pls_list)->pls_layout; |
| 336 | |
| 337 | if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) { |
| 338 | struct nfs_client *clp; |
| 339 | |
| 340 | clp = NFS_SERVER(lo->plh_inode)->nfs_client; |
| 341 | spin_lock(&clp->cl_lock); |
| 342 | list_del_init(&lo->plh_layouts); |
| 343 | spin_unlock(&clp->cl_lock); |
| 344 | } |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 345 | list_for_each_entry_safe(lseg, tmp, free_me, pls_list) { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 346 | list_del(&lseg->pls_list); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 347 | free_lseg(lseg); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 351 | void |
| 352 | pnfs_destroy_layout(struct nfs_inode *nfsi) |
| 353 | { |
| 354 | struct pnfs_layout_hdr *lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 355 | LIST_HEAD(tmp_list); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 356 | |
| 357 | spin_lock(&nfsi->vfs_inode.i_lock); |
| 358 | lo = nfsi->layout; |
| 359 | if (lo) { |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 360 | lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */ |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 361 | mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 362 | } |
| 363 | spin_unlock(&nfsi->vfs_inode.i_lock); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 364 | pnfs_free_lseg_list(&tmp_list); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 365 | } |
| 366 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 367 | /* |
| 368 | * Called by the state manger to remove all layouts established under an |
| 369 | * expired lease. |
| 370 | */ |
| 371 | void |
| 372 | pnfs_destroy_all_layouts(struct nfs_client *clp) |
| 373 | { |
| 374 | struct pnfs_layout_hdr *lo; |
| 375 | LIST_HEAD(tmp_list); |
| 376 | |
| 377 | spin_lock(&clp->cl_lock); |
| 378 | list_splice_init(&clp->cl_layouts, &tmp_list); |
| 379 | spin_unlock(&clp->cl_lock); |
| 380 | |
| 381 | while (!list_empty(&tmp_list)) { |
| 382 | lo = list_entry(tmp_list.next, struct pnfs_layout_hdr, |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 383 | plh_layouts); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 384 | dprintk("%s freeing layout for inode %lu\n", __func__, |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 385 | lo->plh_inode->i_ino); |
| 386 | pnfs_destroy_layout(NFS_I(lo->plh_inode)); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 390 | /* update lo->plh_stateid with new if is more recent */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 391 | void |
| 392 | pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new, |
| 393 | bool update_barrier) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 394 | { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 395 | u32 oldseq, newseq; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 396 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 397 | oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid); |
| 398 | newseq = be32_to_cpu(new->stateid.seqid); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 399 | if ((int)(newseq - oldseq) > 0) { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 400 | memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid)); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 401 | if (update_barrier) { |
| 402 | u32 new_barrier = be32_to_cpu(new->stateid.seqid); |
| 403 | |
| 404 | if ((int)(new_barrier - lo->plh_barrier)) |
| 405 | lo->plh_barrier = new_barrier; |
| 406 | } else { |
| 407 | /* Because of wraparound, we want to keep the barrier |
| 408 | * "close" to the current seqids. It needs to be |
| 409 | * within 2**31 to count as "behind", so if it |
| 410 | * gets too near that limit, give us a litle leeway |
| 411 | * and bring it to within 2**30. |
| 412 | * NOTE - and yes, this is all unsigned arithmetic. |
| 413 | */ |
| 414 | if (unlikely((newseq - lo->plh_barrier) > (3 << 29))) |
| 415 | lo->plh_barrier = newseq - (1 << 30); |
| 416 | } |
| 417 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 418 | } |
| 419 | |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 420 | /* lget is set to 1 if called from inside send_layoutget call chain */ |
| 421 | static bool |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 422 | pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid, |
| 423 | int lget) |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 424 | { |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 425 | if ((stateid) && |
| 426 | (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0) |
| 427 | return true; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 428 | return lo->plh_block_lgets || |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 429 | test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) || |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 430 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) || |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 431 | (list_empty(&lo->plh_segs) && |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 432 | (atomic_read(&lo->plh_outstanding) > lget)); |
| 433 | } |
| 434 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 435 | int |
| 436 | pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo, |
| 437 | struct nfs4_state *open_state) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 438 | { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 439 | int status = 0; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 440 | |
| 441 | dprintk("--> %s\n", __func__); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 442 | spin_lock(&lo->plh_inode->i_lock); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 443 | if (pnfs_layoutgets_blocked(lo, NULL, 1)) { |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 444 | status = -EAGAIN; |
| 445 | } else if (list_empty(&lo->plh_segs)) { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 446 | int seq; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 447 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 448 | do { |
| 449 | seq = read_seqbegin(&open_state->seqlock); |
| 450 | memcpy(dst->data, open_state->stateid.data, |
| 451 | sizeof(open_state->stateid.data)); |
| 452 | } while (read_seqretry(&open_state->seqlock, seq)); |
| 453 | } else |
| 454 | memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data)); |
| 455 | spin_unlock(&lo->plh_inode->i_lock); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 456 | dprintk("<-- %s\n", __func__); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 457 | return status; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Get layout from server. |
| 462 | * for now, assume that whole file layouts are requested. |
| 463 | * arg->offset: 0 |
| 464 | * arg->length: all ones |
| 465 | */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 466 | static struct pnfs_layout_segment * |
| 467 | send_layoutget(struct pnfs_layout_hdr *lo, |
| 468 | struct nfs_open_context *ctx, |
| 469 | u32 iomode) |
| 470 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 471 | struct inode *ino = lo->plh_inode; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 472 | struct nfs_server *server = NFS_SERVER(ino); |
| 473 | struct nfs4_layoutget *lgp; |
| 474 | struct pnfs_layout_segment *lseg = NULL; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 475 | struct page **pages = NULL; |
| 476 | int i; |
| 477 | u32 max_resp_sz, max_pages; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 478 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 479 | dprintk("--> %s\n", __func__); |
| 480 | |
| 481 | BUG_ON(ctx == NULL); |
| 482 | lgp = kzalloc(sizeof(*lgp), GFP_KERNEL); |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 483 | if (lgp == NULL) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 484 | return NULL; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 485 | |
| 486 | /* allocate pages for xdr post processing */ |
| 487 | max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; |
| 488 | max_pages = max_resp_sz >> PAGE_SHIFT; |
| 489 | |
| 490 | pages = kzalloc(max_pages * sizeof(struct page *), GFP_KERNEL); |
| 491 | if (!pages) |
| 492 | goto out_err_free; |
| 493 | |
| 494 | for (i = 0; i < max_pages; i++) { |
| 495 | pages[i] = alloc_page(GFP_KERNEL); |
| 496 | if (!pages[i]) |
| 497 | goto out_err_free; |
| 498 | } |
| 499 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 500 | lgp->args.minlength = NFS4_MAX_UINT64; |
| 501 | lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE; |
| 502 | lgp->args.range.iomode = iomode; |
| 503 | lgp->args.range.offset = 0; |
| 504 | lgp->args.range.length = NFS4_MAX_UINT64; |
| 505 | lgp->args.type = server->pnfs_curr_ld->id; |
| 506 | lgp->args.inode = ino; |
| 507 | lgp->args.ctx = get_nfs_open_context(ctx); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 508 | lgp->args.layout.pages = pages; |
| 509 | lgp->args.layout.pglen = max_pages * PAGE_SIZE; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 510 | lgp->lsegpp = &lseg; |
| 511 | |
| 512 | /* Synchronously retrieve layout information from server and |
| 513 | * store in lseg. |
| 514 | */ |
| 515 | nfs4_proc_layoutget(lgp); |
| 516 | if (!lseg) { |
| 517 | /* remember that LAYOUTGET failed and suspend trying */ |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 518 | set_bit(lo_fail_bit(iomode), &lo->plh_flags); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 519 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 520 | |
| 521 | /* free xdr pages */ |
| 522 | for (i = 0; i < max_pages; i++) |
| 523 | __free_page(pages[i]); |
| 524 | kfree(pages); |
| 525 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 526 | return lseg; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 527 | |
| 528 | out_err_free: |
| 529 | /* free any allocated xdr pages, lgp as it's not used */ |
| 530 | if (pages) { |
| 531 | for (i = 0; i < max_pages; i++) { |
| 532 | if (!pages[i]) |
| 533 | break; |
| 534 | __free_page(pages[i]); |
| 535 | } |
| 536 | kfree(pages); |
| 537 | } |
| 538 | kfree(lgp); |
| 539 | return NULL; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 540 | } |
| 541 | |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 542 | bool pnfs_roc(struct inode *ino) |
| 543 | { |
| 544 | struct pnfs_layout_hdr *lo; |
| 545 | struct pnfs_layout_segment *lseg, *tmp; |
| 546 | LIST_HEAD(tmp_list); |
| 547 | bool found = false; |
| 548 | |
| 549 | spin_lock(&ino->i_lock); |
| 550 | lo = NFS_I(ino)->layout; |
| 551 | if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) || |
| 552 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) |
| 553 | goto out_nolayout; |
| 554 | list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list) |
| 555 | if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { |
| 556 | mark_lseg_invalid(lseg, &tmp_list); |
| 557 | found = true; |
| 558 | } |
| 559 | if (!found) |
| 560 | goto out_nolayout; |
| 561 | lo->plh_block_lgets++; |
| 562 | get_layout_hdr(lo); /* matched in pnfs_roc_release */ |
| 563 | spin_unlock(&ino->i_lock); |
| 564 | pnfs_free_lseg_list(&tmp_list); |
| 565 | return true; |
| 566 | |
| 567 | out_nolayout: |
| 568 | spin_unlock(&ino->i_lock); |
| 569 | return false; |
| 570 | } |
| 571 | |
| 572 | void pnfs_roc_release(struct inode *ino) |
| 573 | { |
| 574 | struct pnfs_layout_hdr *lo; |
| 575 | |
| 576 | spin_lock(&ino->i_lock); |
| 577 | lo = NFS_I(ino)->layout; |
| 578 | lo->plh_block_lgets--; |
| 579 | put_layout_hdr_locked(lo); |
| 580 | spin_unlock(&ino->i_lock); |
| 581 | } |
| 582 | |
| 583 | void pnfs_roc_set_barrier(struct inode *ino, u32 barrier) |
| 584 | { |
| 585 | struct pnfs_layout_hdr *lo; |
| 586 | |
| 587 | spin_lock(&ino->i_lock); |
| 588 | lo = NFS_I(ino)->layout; |
| 589 | if ((int)(barrier - lo->plh_barrier) > 0) |
| 590 | lo->plh_barrier = barrier; |
| 591 | spin_unlock(&ino->i_lock); |
| 592 | } |
| 593 | |
| 594 | bool pnfs_roc_drain(struct inode *ino, u32 *barrier) |
| 595 | { |
| 596 | struct nfs_inode *nfsi = NFS_I(ino); |
| 597 | struct pnfs_layout_segment *lseg; |
| 598 | bool found = false; |
| 599 | |
| 600 | spin_lock(&ino->i_lock); |
| 601 | list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list) |
| 602 | if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { |
| 603 | found = true; |
| 604 | break; |
| 605 | } |
| 606 | if (!found) { |
| 607 | struct pnfs_layout_hdr *lo = nfsi->layout; |
| 608 | u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid); |
| 609 | |
| 610 | /* Since close does not return a layout stateid for use as |
| 611 | * a barrier, we choose the worst-case barrier. |
| 612 | */ |
| 613 | *barrier = current_seqid + atomic_read(&lo->plh_outstanding); |
| 614 | } |
| 615 | spin_unlock(&ino->i_lock); |
| 616 | return found; |
| 617 | } |
| 618 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 619 | /* |
| 620 | * Compare two layout segments for sorting into layout cache. |
| 621 | * We want to preferentially return RW over RO layouts, so ensure those |
| 622 | * are seen first. |
| 623 | */ |
| 624 | static s64 |
| 625 | cmp_layout(u32 iomode1, u32 iomode2) |
| 626 | { |
| 627 | /* read > read/write */ |
| 628 | return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ); |
| 629 | } |
| 630 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 631 | static void |
| 632 | pnfs_insert_layout(struct pnfs_layout_hdr *lo, |
| 633 | struct pnfs_layout_segment *lseg) |
| 634 | { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 635 | struct pnfs_layout_segment *lp; |
| 636 | int found = 0; |
| 637 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 638 | dprintk("%s:Begin\n", __func__); |
| 639 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 640 | assert_spin_locked(&lo->plh_inode->i_lock); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 641 | list_for_each_entry(lp, &lo->plh_segs, pls_list) { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 642 | if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 643 | continue; |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 644 | list_add_tail(&lseg->pls_list, &lp->pls_list); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 645 | dprintk("%s: inserted lseg %p " |
| 646 | "iomode %d offset %llu length %llu before " |
| 647 | "lp %p iomode %d offset %llu length %llu\n", |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 648 | __func__, lseg, lseg->pls_range.iomode, |
| 649 | lseg->pls_range.offset, lseg->pls_range.length, |
| 650 | lp, lp->pls_range.iomode, lp->pls_range.offset, |
| 651 | lp->pls_range.length); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 652 | found = 1; |
| 653 | break; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 654 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 655 | if (!found) { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 656 | list_add_tail(&lseg->pls_list, &lo->plh_segs); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 657 | dprintk("%s: inserted lseg %p " |
| 658 | "iomode %d offset %llu length %llu at tail\n", |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 659 | __func__, lseg, lseg->pls_range.iomode, |
| 660 | lseg->pls_range.offset, lseg->pls_range.length); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 661 | } |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 662 | get_layout_hdr(lo); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 663 | |
| 664 | dprintk("%s:Return\n", __func__); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static struct pnfs_layout_hdr * |
| 668 | alloc_init_layout_hdr(struct inode *ino) |
| 669 | { |
| 670 | struct pnfs_layout_hdr *lo; |
| 671 | |
| 672 | lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL); |
| 673 | if (!lo) |
| 674 | return NULL; |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 675 | atomic_set(&lo->plh_refcount, 1); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 676 | INIT_LIST_HEAD(&lo->plh_layouts); |
| 677 | INIT_LIST_HEAD(&lo->plh_segs); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 678 | INIT_LIST_HEAD(&lo->plh_bulk_recall); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 679 | lo->plh_inode = ino; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 680 | return lo; |
| 681 | } |
| 682 | |
| 683 | static struct pnfs_layout_hdr * |
| 684 | pnfs_find_alloc_layout(struct inode *ino) |
| 685 | { |
| 686 | struct nfs_inode *nfsi = NFS_I(ino); |
| 687 | struct pnfs_layout_hdr *new = NULL; |
| 688 | |
| 689 | dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout); |
| 690 | |
| 691 | assert_spin_locked(&ino->i_lock); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 692 | if (nfsi->layout) { |
| 693 | if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags)) |
| 694 | return NULL; |
| 695 | else |
| 696 | return nfsi->layout; |
| 697 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 698 | spin_unlock(&ino->i_lock); |
| 699 | new = alloc_init_layout_hdr(ino); |
| 700 | spin_lock(&ino->i_lock); |
| 701 | |
| 702 | if (likely(nfsi->layout == NULL)) /* Won the race? */ |
| 703 | nfsi->layout = new; |
| 704 | else |
| 705 | kfree(new); |
| 706 | return nfsi->layout; |
| 707 | } |
| 708 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 709 | /* |
| 710 | * iomode matching rules: |
| 711 | * iomode lseg match |
| 712 | * ----- ----- ----- |
| 713 | * ANY READ true |
| 714 | * ANY RW true |
| 715 | * RW READ false |
| 716 | * RW RW true |
| 717 | * READ READ true |
| 718 | * READ RW true |
| 719 | */ |
| 720 | static int |
| 721 | is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode) |
| 722 | { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 723 | return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /* |
| 727 | * lookup range in layout |
| 728 | */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 729 | static struct pnfs_layout_segment * |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 730 | pnfs_find_lseg(struct pnfs_layout_hdr *lo, u32 iomode) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 731 | { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 732 | struct pnfs_layout_segment *lseg, *ret = NULL; |
| 733 | |
| 734 | dprintk("%s:Begin\n", __func__); |
| 735 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 736 | assert_spin_locked(&lo->plh_inode->i_lock); |
| 737 | list_for_each_entry(lseg, &lo->plh_segs, pls_list) { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 738 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) && |
| 739 | is_matching_lseg(lseg, iomode)) { |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 740 | ret = get_lseg(lseg); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 741 | break; |
| 742 | } |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 743 | if (cmp_layout(iomode, lseg->pls_range.iomode) > 0) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 744 | break; |
| 745 | } |
| 746 | |
| 747 | dprintk("%s:Return lseg %p ref %d\n", |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 748 | __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 749 | return ret; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* |
| 753 | * Layout segment is retreived from the server if not cached. |
| 754 | * The appropriate layout segment is referenced and returned to the caller. |
| 755 | */ |
| 756 | struct pnfs_layout_segment * |
| 757 | pnfs_update_layout(struct inode *ino, |
| 758 | struct nfs_open_context *ctx, |
| 759 | enum pnfs_iomode iomode) |
| 760 | { |
| 761 | struct nfs_inode *nfsi = NFS_I(ino); |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 762 | struct nfs_client *clp = NFS_SERVER(ino)->nfs_client; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 763 | struct pnfs_layout_hdr *lo; |
| 764 | struct pnfs_layout_segment *lseg = NULL; |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 765 | bool first = false; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 766 | |
| 767 | if (!pnfs_enabled_sb(NFS_SERVER(ino))) |
| 768 | return NULL; |
| 769 | spin_lock(&ino->i_lock); |
| 770 | lo = pnfs_find_alloc_layout(ino); |
| 771 | if (lo == NULL) { |
| 772 | dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__); |
| 773 | goto out_unlock; |
| 774 | } |
| 775 | |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 776 | /* Do we even need to bother with this? */ |
| 777 | if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) || |
| 778 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
| 779 | dprintk("%s matches recall, use MDS\n", __func__); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 780 | goto out_unlock; |
| 781 | } |
| 782 | |
| 783 | /* if LAYOUTGET already failed once we don't try again */ |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 784 | if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags)) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 785 | goto out_unlock; |
| 786 | |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 787 | /* Check to see if the layout for the given range already exists */ |
| 788 | lseg = pnfs_find_lseg(lo, iomode); |
| 789 | if (lseg) |
| 790 | goto out_unlock; |
| 791 | |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 792 | if (pnfs_layoutgets_blocked(lo, NULL, 0)) |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 793 | goto out_unlock; |
| 794 | atomic_inc(&lo->plh_outstanding); |
| 795 | |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 796 | get_layout_hdr(lo); |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 797 | if (list_empty(&lo->plh_segs)) |
| 798 | first = true; |
| 799 | spin_unlock(&ino->i_lock); |
| 800 | if (first) { |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 801 | /* The lo must be on the clp list if there is any |
| 802 | * chance of a CB_LAYOUTRECALL(FILE) coming in. |
| 803 | */ |
| 804 | spin_lock(&clp->cl_lock); |
| 805 | BUG_ON(!list_empty(&lo->plh_layouts)); |
| 806 | list_add_tail(&lo->plh_layouts, &clp->cl_layouts); |
| 807 | spin_unlock(&clp->cl_lock); |
| 808 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 809 | |
| 810 | lseg = send_layoutget(lo, ctx, iomode); |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 811 | if (!lseg && first) { |
| 812 | spin_lock(&clp->cl_lock); |
| 813 | list_del_init(&lo->plh_layouts); |
| 814 | spin_unlock(&clp->cl_lock); |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 815 | } |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 816 | atomic_dec(&lo->plh_outstanding); |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 817 | put_layout_hdr(lo); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 818 | out: |
| 819 | dprintk("%s end, state 0x%lx lseg %p\n", __func__, |
Andy Adamson | bf9c138 | 2011-03-01 01:34:07 +0000 | [diff] [blame] | 820 | nfsi->layout ? nfsi->layout->plh_flags : -1, lseg); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 821 | return lseg; |
| 822 | out_unlock: |
| 823 | spin_unlock(&ino->i_lock); |
| 824 | goto out; |
| 825 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 826 | |
| 827 | int |
| 828 | pnfs_layout_process(struct nfs4_layoutget *lgp) |
| 829 | { |
| 830 | struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout; |
| 831 | struct nfs4_layoutget_res *res = &lgp->res; |
| 832 | struct pnfs_layout_segment *lseg; |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 833 | struct inode *ino = lo->plh_inode; |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 834 | struct nfs_client *clp = NFS_SERVER(ino)->nfs_client; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 835 | int status = 0; |
| 836 | |
Fred Isaman | fc1794c | 2011-01-06 11:36:27 +0000 | [diff] [blame] | 837 | /* Verify we got what we asked for. |
| 838 | * Note that because the xdr parsing only accepts a single |
| 839 | * element array, this can fail even if the server is behaving |
| 840 | * correctly. |
| 841 | */ |
| 842 | if (lgp->args.range.iomode > res->range.iomode || |
| 843 | res->range.offset != 0 || |
| 844 | res->range.length != NFS4_MAX_UINT64) { |
| 845 | status = -EINVAL; |
| 846 | goto out; |
| 847 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 848 | /* Inject layout blob into I/O device driver */ |
| 849 | lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res); |
| 850 | if (!lseg || IS_ERR(lseg)) { |
| 851 | if (!lseg) |
| 852 | status = -ENOMEM; |
| 853 | else |
| 854 | status = PTR_ERR(lseg); |
| 855 | dprintk("%s: Could not allocate layout: error %d\n", |
| 856 | __func__, status); |
| 857 | goto out; |
| 858 | } |
| 859 | |
| 860 | spin_lock(&ino->i_lock); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 861 | if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) || |
| 862 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
| 863 | dprintk("%s forget reply due to recall\n", __func__); |
| 864 | goto out_forget_reply; |
| 865 | } |
| 866 | |
| 867 | if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) { |
| 868 | dprintk("%s forget reply due to state\n", __func__); |
| 869 | goto out_forget_reply; |
| 870 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 871 | init_lseg(lo, lseg); |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 872 | lseg->pls_range = res->range; |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 873 | *lgp->lsegpp = get_lseg(lseg); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 874 | pnfs_insert_layout(lo, lseg); |
| 875 | |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 876 | if (res->return_on_close) { |
| 877 | set_bit(NFS_LSEG_ROC, &lseg->pls_flags); |
| 878 | set_bit(NFS_LAYOUT_ROC, &lo->plh_flags); |
| 879 | } |
| 880 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 881 | /* Done processing layoutget. Set the layout stateid */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 882 | pnfs_set_layout_stateid(lo, &res->stateid, false); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 883 | spin_unlock(&ino->i_lock); |
| 884 | out: |
| 885 | return status; |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 886 | |
| 887 | out_forget_reply: |
| 888 | spin_unlock(&ino->i_lock); |
| 889 | lseg->pls_layout = lo; |
| 890 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
| 891 | goto out; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 892 | } |
| 893 | |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 894 | static int pnfs_read_pg_test(struct nfs_pageio_descriptor *pgio, |
| 895 | struct nfs_page *prev, |
| 896 | struct nfs_page *req) |
| 897 | { |
| 898 | if (pgio->pg_count == prev->wb_bytes) { |
| 899 | /* This is first coelesce call for a series of nfs_pages */ |
| 900 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
| 901 | prev->wb_context, |
| 902 | IOMODE_READ); |
| 903 | } |
| 904 | return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req); |
| 905 | } |
| 906 | |
| 907 | void |
| 908 | pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode) |
Fred Isaman | 94ad1c8 | 2011-03-01 01:34:14 +0000 | [diff] [blame] | 909 | { |
| 910 | struct pnfs_layoutdriver_type *ld; |
| 911 | |
| 912 | ld = NFS_SERVER(inode)->pnfs_curr_ld; |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 913 | pgio->pg_test = (ld && ld->pg_test) ? pnfs_read_pg_test : NULL; |
Fred Isaman | 94ad1c8 | 2011-03-01 01:34:14 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Fred Isaman | 44b8379 | 2011-03-03 15:13:44 +0000 | [diff] [blame] | 916 | static int pnfs_write_pg_test(struct nfs_pageio_descriptor *pgio, |
| 917 | struct nfs_page *prev, |
| 918 | struct nfs_page *req) |
| 919 | { |
| 920 | if (pgio->pg_count == prev->wb_bytes) { |
| 921 | /* This is first coelesce call for a series of nfs_pages */ |
| 922 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
| 923 | prev->wb_context, |
| 924 | IOMODE_RW); |
| 925 | } |
| 926 | return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req); |
| 927 | } |
| 928 | |
| 929 | void |
| 930 | pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode) |
| 931 | { |
| 932 | struct pnfs_layoutdriver_type *ld; |
| 933 | |
| 934 | ld = NFS_SERVER(inode)->pnfs_curr_ld; |
| 935 | pgio->pg_test = (ld && ld->pg_test) ? pnfs_write_pg_test : NULL; |
| 936 | } |
| 937 | |
Andy Adamson | 0382b74 | 2011-03-03 15:13:45 +0000 | [diff] [blame] | 938 | enum pnfs_try_status |
| 939 | pnfs_try_to_write_data(struct nfs_write_data *wdata, |
| 940 | const struct rpc_call_ops *call_ops, int how) |
| 941 | { |
| 942 | struct inode *inode = wdata->inode; |
| 943 | enum pnfs_try_status trypnfs; |
| 944 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 945 | |
| 946 | wdata->mds_ops = call_ops; |
| 947 | |
| 948 | dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__, |
| 949 | inode->i_ino, wdata->args.count, wdata->args.offset, how); |
| 950 | |
| 951 | trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how); |
| 952 | if (trypnfs == PNFS_NOT_ATTEMPTED) { |
| 953 | put_lseg(wdata->lseg); |
| 954 | wdata->lseg = NULL; |
| 955 | } else |
| 956 | nfs_inc_stats(inode, NFSIOS_PNFS_WRITE); |
| 957 | |
| 958 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
| 959 | return trypnfs; |
| 960 | } |
| 961 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 962 | /* |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 963 | * Call the appropriate parallel I/O subsystem read function. |
| 964 | */ |
| 965 | enum pnfs_try_status |
| 966 | pnfs_try_to_read_data(struct nfs_read_data *rdata, |
| 967 | const struct rpc_call_ops *call_ops) |
| 968 | { |
| 969 | struct inode *inode = rdata->inode; |
| 970 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 971 | enum pnfs_try_status trypnfs; |
| 972 | |
| 973 | rdata->mds_ops = call_ops; |
| 974 | |
| 975 | dprintk("%s: Reading ino:%lu %u@%llu\n", |
| 976 | __func__, inode->i_ino, rdata->args.count, rdata->args.offset); |
| 977 | |
| 978 | trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata); |
| 979 | if (trypnfs == PNFS_NOT_ATTEMPTED) { |
| 980 | put_lseg(rdata->lseg); |
| 981 | rdata->lseg = NULL; |
| 982 | } else { |
| 983 | nfs_inc_stats(inode, NFSIOS_PNFS_READ); |
| 984 | } |
| 985 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
| 986 | return trypnfs; |
| 987 | } |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 988 | |
| 989 | /* |
| 990 | * Currently there is only one (whole file) write lseg. |
| 991 | */ |
| 992 | static struct pnfs_layout_segment *pnfs_list_write_lseg(struct inode *inode) |
| 993 | { |
| 994 | struct pnfs_layout_segment *lseg, *rv = NULL; |
| 995 | |
| 996 | list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) |
| 997 | if (lseg->pls_range.iomode == IOMODE_RW) |
| 998 | rv = lseg; |
| 999 | return rv; |
| 1000 | } |
| 1001 | |
| 1002 | void |
| 1003 | pnfs_set_layoutcommit(struct nfs_write_data *wdata) |
| 1004 | { |
| 1005 | struct nfs_inode *nfsi = NFS_I(wdata->inode); |
| 1006 | loff_t end_pos = wdata->args.offset + wdata->res.count; |
Weston Andros Adamson | 79a48a1 | 2011-04-13 10:53:51 -0400 | [diff] [blame^] | 1007 | bool mark_as_dirty = false; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1008 | |
| 1009 | spin_lock(&nfsi->vfs_inode.i_lock); |
| 1010 | if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) { |
| 1011 | /* references matched in nfs4_layoutcommit_release */ |
| 1012 | get_lseg(wdata->lseg); |
| 1013 | wdata->lseg->pls_lc_cred = |
| 1014 | get_rpccred(wdata->args.context->state->owner->so_cred); |
Weston Andros Adamson | 79a48a1 | 2011-04-13 10:53:51 -0400 | [diff] [blame^] | 1015 | mark_as_dirty = true; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1016 | dprintk("%s: Set layoutcommit for inode %lu ", |
| 1017 | __func__, wdata->inode->i_ino); |
| 1018 | } |
| 1019 | if (end_pos > wdata->lseg->pls_end_pos) |
| 1020 | wdata->lseg->pls_end_pos = end_pos; |
| 1021 | spin_unlock(&nfsi->vfs_inode.i_lock); |
Weston Andros Adamson | 79a48a1 | 2011-04-13 10:53:51 -0400 | [diff] [blame^] | 1022 | |
| 1023 | /* if pnfs_layoutcommit_inode() runs between inode locks, the next one |
| 1024 | * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */ |
| 1025 | if (mark_as_dirty) |
| 1026 | mark_inode_dirty_sync(wdata->inode); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1027 | } |
| 1028 | EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit); |
| 1029 | |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 1030 | /* |
| 1031 | * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and |
| 1032 | * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough |
| 1033 | * data to disk to allow the server to recover the data if it crashes. |
| 1034 | * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag |
| 1035 | * is off, and a COMMIT is sent to a data server, or |
| 1036 | * if WRITEs to a data server return NFS_DATA_SYNC. |
| 1037 | */ |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1038 | int |
Andy Adamson | ef31153 | 2011-03-12 02:58:10 -0500 | [diff] [blame] | 1039 | pnfs_layoutcommit_inode(struct inode *inode, bool sync) |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1040 | { |
| 1041 | struct nfs4_layoutcommit_data *data; |
| 1042 | struct nfs_inode *nfsi = NFS_I(inode); |
| 1043 | struct pnfs_layout_segment *lseg; |
| 1044 | struct rpc_cred *cred; |
| 1045 | loff_t end_pos; |
| 1046 | int status = 0; |
| 1047 | |
| 1048 | dprintk("--> %s inode %lu\n", __func__, inode->i_ino); |
| 1049 | |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 1050 | if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) |
| 1051 | return 0; |
| 1052 | |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1053 | /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */ |
| 1054 | data = kzalloc(sizeof(*data), GFP_NOFS); |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 1055 | if (!data) { |
| 1056 | mark_inode_dirty_sync(inode); |
| 1057 | status = -ENOMEM; |
| 1058 | goto out; |
| 1059 | } |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1060 | |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 1061 | spin_lock(&inode->i_lock); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1062 | if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) { |
| 1063 | spin_unlock(&inode->i_lock); |
| 1064 | kfree(data); |
| 1065 | goto out; |
| 1066 | } |
| 1067 | /* |
| 1068 | * Currently only one (whole file) write lseg which is referenced |
| 1069 | * in pnfs_set_layoutcommit and will be found. |
| 1070 | */ |
| 1071 | lseg = pnfs_list_write_lseg(inode); |
| 1072 | |
| 1073 | end_pos = lseg->pls_end_pos; |
| 1074 | cred = lseg->pls_lc_cred; |
| 1075 | lseg->pls_end_pos = 0; |
| 1076 | lseg->pls_lc_cred = NULL; |
| 1077 | |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 1078 | memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data, |
| 1079 | sizeof(nfsi->layout->plh_stateid.data)); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1080 | spin_unlock(&inode->i_lock); |
| 1081 | |
| 1082 | data->args.inode = inode; |
| 1083 | data->lseg = lseg; |
| 1084 | data->cred = cred; |
| 1085 | nfs_fattr_init(&data->fattr); |
| 1086 | data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask; |
| 1087 | data->res.fattr = &data->fattr; |
| 1088 | data->args.lastbytewritten = end_pos - 1; |
| 1089 | data->res.server = NFS_SERVER(inode); |
| 1090 | |
| 1091 | status = nfs4_proc_layoutcommit(data, sync); |
| 1092 | out: |
| 1093 | dprintk("<-- %s status %d\n", __func__, status); |
| 1094 | return status; |
| 1095 | } |