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