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" |
Trond Myklebust | cc668ab | 2013-08-14 15:31:28 -0400 | [diff] [blame] | 36 | #include "nfs4trace.h" |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 37 | #include "delegation.h" |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 38 | |
| 39 | #define NFSDBG_FACILITY NFSDBG_PNFS |
Trond Myklebust | 25c7533 | 2012-09-18 17:01:12 -0400 | [diff] [blame] | 40 | #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ) |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 41 | |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 42 | /* Locking: |
| 43 | * |
| 44 | * pnfs_spinlock: |
| 45 | * protects pnfs_modules_tbl. |
| 46 | */ |
| 47 | static DEFINE_SPINLOCK(pnfs_spinlock); |
| 48 | |
| 49 | /* |
| 50 | * pnfs_modules_tbl holds all pnfs modules |
| 51 | */ |
| 52 | static LIST_HEAD(pnfs_modules_tbl); |
| 53 | |
Peng Tao | aa1e0e3a | 2014-09-06 00:53:25 +0800 | [diff] [blame] | 54 | static int |
| 55 | pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, nfs4_stateid stateid, |
Peng Tao | 6c16605 | 2014-11-17 09:30:40 +0800 | [diff] [blame] | 56 | enum pnfs_iomode iomode, bool sync); |
Peng Tao | aa1e0e3a | 2014-09-06 00:53:25 +0800 | [diff] [blame] | 57 | |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 58 | /* Return the registered pnfs layout driver module matching given id */ |
| 59 | static struct pnfs_layoutdriver_type * |
| 60 | find_pnfs_driver_locked(u32 id) |
| 61 | { |
| 62 | struct pnfs_layoutdriver_type *local; |
| 63 | |
| 64 | list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid) |
| 65 | if (local->id == id) |
| 66 | goto out; |
| 67 | local = NULL; |
| 68 | out: |
| 69 | dprintk("%s: Searching for id %u, found %p\n", __func__, id, local); |
| 70 | return local; |
| 71 | } |
| 72 | |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 73 | static struct pnfs_layoutdriver_type * |
| 74 | find_pnfs_driver(u32 id) |
| 75 | { |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 76 | struct pnfs_layoutdriver_type *local; |
| 77 | |
| 78 | spin_lock(&pnfs_spinlock); |
| 79 | local = find_pnfs_driver_locked(id); |
Trond Myklebust | 0a9c63f | 2012-06-15 13:02:58 -0400 | [diff] [blame] | 80 | if (local != NULL && !try_module_get(local->owner)) { |
| 81 | dprintk("%s: Could not grab reference on module\n", __func__); |
| 82 | local = NULL; |
| 83 | } |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 84 | spin_unlock(&pnfs_spinlock); |
| 85 | return local; |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void |
| 89 | unset_pnfs_layoutdriver(struct nfs_server *nfss) |
| 90 | { |
Benny Halevy | 738fd0f3 | 2011-07-30 20:52:36 -0400 | [diff] [blame] | 91 | if (nfss->pnfs_curr_ld) { |
| 92 | if (nfss->pnfs_curr_ld->clear_layoutdriver) |
| 93 | nfss->pnfs_curr_ld->clear_layoutdriver(nfss); |
Trond Myklebust | 2a4c899 | 2012-06-14 13:08:38 -0400 | [diff] [blame] | 94 | /* Decrement the MDS count. Purge the deviceid cache if zero */ |
| 95 | if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count)) |
| 96 | nfs4_deviceid_purge_client(nfss->nfs_client); |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 97 | module_put(nfss->pnfs_curr_ld->owner); |
Benny Halevy | 738fd0f3 | 2011-07-30 20:52:36 -0400 | [diff] [blame] | 98 | } |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 99 | nfss->pnfs_curr_ld = NULL; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Try to set the server's pnfs module to the pnfs layout type specified by id. |
| 104 | * Currently only one pNFS layout driver per filesystem is supported. |
| 105 | * |
| 106 | * @id layout type. Zero (illegal layout type) indicates pNFS not in use. |
| 107 | */ |
| 108 | void |
Benny Halevy | 738fd0f3 | 2011-07-30 20:52:36 -0400 | [diff] [blame] | 109 | set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh, |
| 110 | u32 id) |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 111 | { |
| 112 | struct pnfs_layoutdriver_type *ld_type = NULL; |
| 113 | |
| 114 | if (id == 0) |
| 115 | goto out_no_driver; |
| 116 | if (!(server->nfs_client->cl_exchange_flags & |
| 117 | (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 118 | printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n", |
| 119 | __func__, id, server->nfs_client->cl_exchange_flags); |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 120 | goto out_no_driver; |
| 121 | } |
| 122 | ld_type = find_pnfs_driver(id); |
| 123 | if (!ld_type) { |
| 124 | request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id); |
| 125 | ld_type = find_pnfs_driver(id); |
| 126 | if (!ld_type) { |
| 127 | dprintk("%s: No pNFS module found for %u.\n", |
| 128 | __func__, id); |
| 129 | goto out_no_driver; |
| 130 | } |
| 131 | } |
| 132 | server->pnfs_curr_ld = ld_type; |
Benny Halevy | 738fd0f3 | 2011-07-30 20:52:36 -0400 | [diff] [blame] | 133 | if (ld_type->set_layoutdriver |
| 134 | && ld_type->set_layoutdriver(server, mntfh)) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 135 | printk(KERN_ERR "NFS: %s: Error initializing pNFS layout " |
| 136 | "driver %u.\n", __func__, id); |
Benny Halevy | 738fd0f3 | 2011-07-30 20:52:36 -0400 | [diff] [blame] | 137 | module_put(ld_type->owner); |
| 138 | goto out_no_driver; |
| 139 | } |
Trond Myklebust | 2a4c899 | 2012-06-14 13:08:38 -0400 | [diff] [blame] | 140 | /* Bump the MDS count */ |
| 141 | atomic_inc(&server->nfs_client->cl_mds_count); |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 142 | |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 143 | dprintk("%s: pNFS module for %u set\n", __func__, id); |
| 144 | return; |
| 145 | |
| 146 | out_no_driver: |
| 147 | dprintk("%s: Using NFSv4 I/O\n", __func__); |
| 148 | server->pnfs_curr_ld = NULL; |
| 149 | } |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 150 | |
| 151 | int |
| 152 | pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type) |
| 153 | { |
| 154 | int status = -EINVAL; |
| 155 | struct pnfs_layoutdriver_type *tmp; |
| 156 | |
| 157 | if (ld_type->id == 0) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 158 | printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__); |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 159 | return status; |
| 160 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 161 | if (!ld_type->alloc_lseg || !ld_type->free_lseg) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 162 | printk(KERN_ERR "NFS: %s Layout driver must provide " |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 163 | "alloc_lseg and free_lseg.\n", __func__); |
| 164 | return status; |
| 165 | } |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 166 | |
| 167 | spin_lock(&pnfs_spinlock); |
| 168 | tmp = find_pnfs_driver_locked(ld_type->id); |
| 169 | if (!tmp) { |
| 170 | list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl); |
| 171 | status = 0; |
| 172 | dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id, |
| 173 | ld_type->name); |
| 174 | } else { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 175 | printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n", |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 176 | __func__, ld_type->id); |
| 177 | } |
| 178 | spin_unlock(&pnfs_spinlock); |
| 179 | |
| 180 | return status; |
| 181 | } |
| 182 | EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver); |
| 183 | |
| 184 | void |
| 185 | pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type) |
| 186 | { |
| 187 | dprintk("%s Deregistering id:%u\n", __func__, ld_type->id); |
| 188 | spin_lock(&pnfs_spinlock); |
| 189 | list_del(&ld_type->pnfs_tblid); |
| 190 | spin_unlock(&pnfs_spinlock); |
| 191 | } |
| 192 | EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 193 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 194 | /* |
| 195 | * pNFS client layout cache |
| 196 | */ |
| 197 | |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 198 | /* Need to hold i_lock if caller does not already hold reference */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 199 | void |
Trond Myklebust | 70c3bd2 | 2012-09-18 20:51:13 -0400 | [diff] [blame] | 200 | pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 201 | { |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 202 | atomic_inc(&lo->plh_refcount); |
| 203 | } |
| 204 | |
Benny Halevy | 636fb9c | 2011-05-22 19:51:33 +0300 | [diff] [blame] | 205 | static struct pnfs_layout_hdr * |
| 206 | pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags) |
| 207 | { |
| 208 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld; |
Trond Myklebust | 5793427 | 2012-09-20 20:37:23 -0400 | [diff] [blame] | 209 | return ld->alloc_layout_hdr(ino, gfp_flags); |
Benny Halevy | 636fb9c | 2011-05-22 19:51:33 +0300 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static void |
| 213 | pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo) |
| 214 | { |
Trond Myklebust | 9c62638 | 2012-09-20 17:31:43 -0400 | [diff] [blame] | 215 | struct nfs_server *server = NFS_SERVER(lo->plh_inode); |
| 216 | struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld; |
| 217 | |
| 218 | if (!list_empty(&lo->plh_layouts)) { |
| 219 | struct nfs_client *clp = server->nfs_client; |
| 220 | |
| 221 | spin_lock(&clp->cl_lock); |
| 222 | list_del_init(&lo->plh_layouts); |
| 223 | spin_unlock(&clp->cl_lock); |
| 224 | } |
Peng Tao | 9fa4075 | 2011-07-30 20:52:32 -0400 | [diff] [blame] | 225 | put_rpccred(lo->plh_lc_cred); |
Trond Myklebust | 5793427 | 2012-09-20 20:37:23 -0400 | [diff] [blame] | 226 | return ld->free_layout_hdr(lo); |
Benny Halevy | 636fb9c | 2011-05-22 19:51:33 +0300 | [diff] [blame] | 227 | } |
| 228 | |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 229 | static void |
Trond Myklebust | 6622c3e | 2012-09-20 17:23:11 -0400 | [diff] [blame] | 230 | pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo) |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 231 | { |
Trond Myklebust | bb346f6 | 2012-09-20 15:52:13 -0400 | [diff] [blame] | 232 | struct nfs_inode *nfsi = NFS_I(lo->plh_inode); |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 233 | dprintk("%s: freeing layout cache %p\n", __func__, lo); |
Trond Myklebust | bb346f6 | 2012-09-20 15:52:13 -0400 | [diff] [blame] | 234 | nfsi->layout = NULL; |
| 235 | /* Reset MDS Threshold I/O counters */ |
| 236 | nfsi->write_io = 0; |
| 237 | nfsi->read_io = 0; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 238 | } |
| 239 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 240 | void |
Trond Myklebust | 70c3bd2 | 2012-09-18 20:51:13 -0400 | [diff] [blame] | 241 | pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 242 | { |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 243 | struct inode *inode = lo->plh_inode; |
| 244 | |
| 245 | if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) { |
Peng Tao | 566f873 | 2014-10-10 23:25:46 +0800 | [diff] [blame] | 246 | if (!list_empty(&lo->plh_segs)) |
| 247 | WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n"); |
Trond Myklebust | 6622c3e | 2012-09-20 17:23:11 -0400 | [diff] [blame] | 248 | pnfs_detach_layout_hdr(lo); |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 249 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 6622c3e | 2012-09-20 17:23:11 -0400 | [diff] [blame] | 250 | pnfs_free_layout_hdr(lo); |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 251 | } |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 252 | } |
| 253 | |
Trond Myklebust | b9e028f | 2012-09-18 16:41:18 -0400 | [diff] [blame] | 254 | static int |
| 255 | pnfs_iomode_to_fail_bit(u32 iomode) |
| 256 | { |
| 257 | return iomode == IOMODE_RW ? |
| 258 | NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED; |
| 259 | } |
| 260 | |
| 261 | static void |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 262 | pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit) |
Trond Myklebust | b9e028f | 2012-09-18 16:41:18 -0400 | [diff] [blame] | 263 | { |
Trond Myklebust | 25c7533 | 2012-09-18 17:01:12 -0400 | [diff] [blame] | 264 | lo->plh_retry_timestamp = jiffies; |
Yanchuan Nian | 39e88fc | 2013-01-04 20:19:49 +0800 | [diff] [blame] | 265 | if (!test_and_set_bit(fail_bit, &lo->plh_flags)) |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 266 | atomic_inc(&lo->plh_refcount); |
| 267 | } |
| 268 | |
| 269 | static void |
| 270 | pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit) |
| 271 | { |
| 272 | if (test_and_clear_bit(fail_bit, &lo->plh_flags)) |
| 273 | atomic_dec(&lo->plh_refcount); |
| 274 | } |
| 275 | |
| 276 | static void |
| 277 | pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode) |
| 278 | { |
| 279 | struct inode *inode = lo->plh_inode; |
Trond Myklebust | 115ce57 | 2012-09-20 21:19:43 -0400 | [diff] [blame] | 280 | struct pnfs_layout_range range = { |
| 281 | .iomode = iomode, |
| 282 | .offset = 0, |
| 283 | .length = NFS4_MAX_UINT64, |
| 284 | }; |
| 285 | LIST_HEAD(head); |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 286 | |
| 287 | spin_lock(&inode->i_lock); |
| 288 | pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); |
Trond Myklebust | 115ce57 | 2012-09-20 21:19:43 -0400 | [diff] [blame] | 289 | pnfs_mark_matching_lsegs_invalid(lo, &head, &range); |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 290 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 115ce57 | 2012-09-20 21:19:43 -0400 | [diff] [blame] | 291 | pnfs_free_lseg_list(&head); |
Trond Myklebust | b9e028f | 2012-09-18 16:41:18 -0400 | [diff] [blame] | 292 | dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__, |
| 293 | iomode == IOMODE_RW ? "RW" : "READ"); |
| 294 | } |
| 295 | |
| 296 | static bool |
| 297 | pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode) |
| 298 | { |
Trond Myklebust | 25c7533 | 2012-09-18 17:01:12 -0400 | [diff] [blame] | 299 | unsigned long start, end; |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 300 | int fail_bit = pnfs_iomode_to_fail_bit(iomode); |
| 301 | |
| 302 | if (test_bit(fail_bit, &lo->plh_flags) == 0) |
Trond Myklebust | 25c7533 | 2012-09-18 17:01:12 -0400 | [diff] [blame] | 303 | return false; |
| 304 | end = jiffies; |
| 305 | start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT; |
| 306 | if (!time_in_range(lo->plh_retry_timestamp, start, end)) { |
| 307 | /* It is time to retry the failed layoutgets */ |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 308 | pnfs_layout_clear_fail_bit(lo, fail_bit); |
Trond Myklebust | 25c7533 | 2012-09-18 17:01:12 -0400 | [diff] [blame] | 309 | return false; |
| 310 | } |
| 311 | return true; |
Trond Myklebust | b9e028f | 2012-09-18 16:41:18 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 314 | static void |
| 315 | init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg) |
| 316 | { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 317 | INIT_LIST_HEAD(&lseg->pls_list); |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 318 | INIT_LIST_HEAD(&lseg->pls_lc_list); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 319 | atomic_set(&lseg->pls_refcount, 1); |
| 320 | smp_mb(); |
| 321 | set_bit(NFS_LSEG_VALID, &lseg->pls_flags); |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 322 | lseg->pls_layout = lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 323 | } |
| 324 | |
Trond Myklebust | 905ca19 | 2012-09-20 20:46:49 -0400 | [diff] [blame] | 325 | static void pnfs_free_lseg(struct pnfs_layout_segment *lseg) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 326 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 327 | struct inode *ino = lseg->pls_layout->plh_inode; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 328 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 329 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 330 | } |
| 331 | |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 332 | static void |
Trond Myklebust | 57036a3 | 2012-09-20 16:33:30 -0400 | [diff] [blame] | 333 | pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo, |
| 334 | struct pnfs_layout_segment *lseg) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 335 | { |
Trond Myklebust | 57036a3 | 2012-09-20 16:33:30 -0400 | [diff] [blame] | 336 | struct inode *inode = lo->plh_inode; |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 337 | |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 338 | WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 339 | list_del_init(&lseg->pls_list); |
Trond Myklebust | 8f0d27d | 2012-09-20 20:57:11 -0400 | [diff] [blame] | 340 | /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */ |
| 341 | atomic_dec(&lo->plh_refcount); |
Trond Myklebust | 173f77e | 2012-09-21 15:49:42 -0400 | [diff] [blame] | 342 | if (list_empty(&lo->plh_segs)) |
| 343 | clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 344 | rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq); |
| 345 | } |
| 346 | |
Peng Tao | aa1e0e3a | 2014-09-06 00:53:25 +0800 | [diff] [blame] | 347 | /* Return true if layoutreturn is needed */ |
| 348 | static bool |
| 349 | pnfs_layout_need_return(struct pnfs_layout_hdr *lo, |
Peng Tao | 27b6f53 | 2014-10-20 14:44:38 +0800 | [diff] [blame] | 350 | struct pnfs_layout_segment *lseg) |
Peng Tao | aa1e0e3a | 2014-09-06 00:53:25 +0800 | [diff] [blame] | 351 | { |
| 352 | struct pnfs_layout_segment *s; |
| 353 | |
| 354 | if (!test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags)) |
| 355 | return false; |
| 356 | |
| 357 | list_for_each_entry(s, &lo->plh_segs, pls_list) |
Peng Tao | 27b6f53 | 2014-10-20 14:44:38 +0800 | [diff] [blame] | 358 | if (s != lseg && test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags)) |
Peng Tao | aa1e0e3a | 2014-09-06 00:53:25 +0800 | [diff] [blame] | 359 | return false; |
| 360 | |
Peng Tao | aa1e0e3a | 2014-09-06 00:53:25 +0800 | [diff] [blame] | 361 | return true; |
| 362 | } |
| 363 | |
Peng Tao | 27b6f53 | 2014-10-20 14:44:38 +0800 | [diff] [blame] | 364 | static void pnfs_layoutreturn_free_lseg(struct work_struct *work) |
| 365 | { |
| 366 | struct pnfs_layout_segment *lseg; |
| 367 | struct pnfs_layout_hdr *lo; |
| 368 | struct inode *inode; |
| 369 | |
| 370 | lseg = container_of(work, struct pnfs_layout_segment, pls_work); |
| 371 | WARN_ON(atomic_read(&lseg->pls_refcount)); |
| 372 | lo = lseg->pls_layout; |
| 373 | inode = lo->plh_inode; |
| 374 | |
| 375 | spin_lock(&inode->i_lock); |
| 376 | if (pnfs_layout_need_return(lo, lseg)) { |
| 377 | nfs4_stateid stateid; |
| 378 | enum pnfs_iomode iomode; |
| 379 | |
| 380 | stateid = lo->plh_stateid; |
| 381 | iomode = lo->plh_return_iomode; |
| 382 | /* decreased in pnfs_send_layoutreturn() */ |
| 383 | lo->plh_block_lgets++; |
| 384 | lo->plh_return_iomode = 0; |
| 385 | spin_unlock(&inode->i_lock); |
| 386 | |
| 387 | pnfs_send_layoutreturn(lo, stateid, iomode, true); |
| 388 | spin_lock(&inode->i_lock); |
| 389 | } else |
| 390 | /* match pnfs_get_layout_hdr #2 in pnfs_put_lseg */ |
| 391 | pnfs_put_layout_hdr(lo); |
| 392 | pnfs_layout_remove_lseg(lo, lseg); |
| 393 | spin_unlock(&inode->i_lock); |
| 394 | pnfs_free_lseg(lseg); |
| 395 | /* match pnfs_get_layout_hdr #1 in pnfs_put_lseg */ |
| 396 | pnfs_put_layout_hdr(lo); |
| 397 | } |
| 398 | |
| 399 | static void |
| 400 | pnfs_layoutreturn_free_lseg_async(struct pnfs_layout_segment *lseg) |
| 401 | { |
| 402 | INIT_WORK(&lseg->pls_work, pnfs_layoutreturn_free_lseg); |
| 403 | queue_work(nfsiod_workqueue, &lseg->pls_work); |
| 404 | } |
| 405 | |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 406 | void |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 407 | pnfs_put_lseg(struct pnfs_layout_segment *lseg) |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 408 | { |
Trond Myklebust | 57036a3 | 2012-09-20 16:33:30 -0400 | [diff] [blame] | 409 | struct pnfs_layout_hdr *lo; |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 410 | struct inode *inode; |
| 411 | |
| 412 | if (!lseg) |
| 413 | return; |
| 414 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 415 | dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg, |
| 416 | atomic_read(&lseg->pls_refcount), |
| 417 | test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
Trond Myklebust | 57036a3 | 2012-09-20 16:33:30 -0400 | [diff] [blame] | 418 | lo = lseg->pls_layout; |
| 419 | inode = lo->plh_inode; |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 420 | if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) { |
Trond Myklebust | 8f0d27d | 2012-09-20 20:57:11 -0400 | [diff] [blame] | 421 | pnfs_get_layout_hdr(lo); |
Peng Tao | 27b6f53 | 2014-10-20 14:44:38 +0800 | [diff] [blame] | 422 | if (pnfs_layout_need_return(lo, lseg)) { |
| 423 | spin_unlock(&inode->i_lock); |
| 424 | /* hdr reference dropped in nfs4_layoutreturn_release */ |
| 425 | pnfs_get_layout_hdr(lo); |
| 426 | pnfs_layoutreturn_free_lseg_async(lseg); |
| 427 | } else { |
| 428 | pnfs_layout_remove_lseg(lo, lseg); |
| 429 | spin_unlock(&inode->i_lock); |
| 430 | pnfs_free_lseg(lseg); |
Peng Tao | aa1e0e3a | 2014-09-06 00:53:25 +0800 | [diff] [blame] | 431 | pnfs_put_layout_hdr(lo); |
Peng Tao | 27b6f53 | 2014-10-20 14:44:38 +0800 | [diff] [blame] | 432 | } |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 433 | } |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 434 | } |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 435 | EXPORT_SYMBOL_GPL(pnfs_put_lseg); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 436 | |
Trond Myklebust | 6543f80 | 2014-10-08 16:39:12 -0400 | [diff] [blame] | 437 | static void pnfs_free_lseg_async_work(struct work_struct *work) |
Weston Andros Adamson | e6cf82d | 2014-07-17 20:42:18 -0400 | [diff] [blame] | 438 | { |
| 439 | struct pnfs_layout_segment *lseg; |
Trond Myklebust | 6543f80 | 2014-10-08 16:39:12 -0400 | [diff] [blame] | 440 | struct pnfs_layout_hdr *lo; |
Weston Andros Adamson | e6cf82d | 2014-07-17 20:42:18 -0400 | [diff] [blame] | 441 | |
| 442 | lseg = container_of(work, struct pnfs_layout_segment, pls_work); |
Trond Myklebust | 6543f80 | 2014-10-08 16:39:12 -0400 | [diff] [blame] | 443 | lo = lseg->pls_layout; |
Weston Andros Adamson | e6cf82d | 2014-07-17 20:42:18 -0400 | [diff] [blame] | 444 | |
Trond Myklebust | 6543f80 | 2014-10-08 16:39:12 -0400 | [diff] [blame] | 445 | pnfs_free_lseg(lseg); |
| 446 | pnfs_put_layout_hdr(lo); |
| 447 | } |
| 448 | |
| 449 | static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg) |
| 450 | { |
| 451 | INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work); |
| 452 | schedule_work(&lseg->pls_work); |
Weston Andros Adamson | e6cf82d | 2014-07-17 20:42:18 -0400 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | void |
Trond Myklebust | 6543f80 | 2014-10-08 16:39:12 -0400 | [diff] [blame] | 456 | pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg) |
Weston Andros Adamson | e6cf82d | 2014-07-17 20:42:18 -0400 | [diff] [blame] | 457 | { |
Trond Myklebust | 6543f80 | 2014-10-08 16:39:12 -0400 | [diff] [blame] | 458 | if (!lseg) |
| 459 | return; |
| 460 | |
| 461 | assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock); |
| 462 | |
| 463 | dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg, |
| 464 | atomic_read(&lseg->pls_refcount), |
| 465 | test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
| 466 | if (atomic_dec_and_test(&lseg->pls_refcount)) { |
| 467 | struct pnfs_layout_hdr *lo = lseg->pls_layout; |
| 468 | pnfs_get_layout_hdr(lo); |
| 469 | pnfs_layout_remove_lseg(lo, lseg); |
| 470 | pnfs_free_lseg_async(lseg); |
| 471 | } |
Weston Andros Adamson | e6cf82d | 2014-07-17 20:42:18 -0400 | [diff] [blame] | 472 | } |
Trond Myklebust | 6543f80 | 2014-10-08 16:39:12 -0400 | [diff] [blame] | 473 | EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked); |
Weston Andros Adamson | e6cf82d | 2014-07-17 20:42:18 -0400 | [diff] [blame] | 474 | |
Trond Myklebust | 3cb2df1 | 2013-06-03 11:24:36 -0400 | [diff] [blame] | 475 | static u64 |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 476 | end_offset(u64 start, u64 len) |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 477 | { |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 478 | u64 end; |
| 479 | |
| 480 | end = start + len; |
| 481 | return end >= start ? end : NFS4_MAX_UINT64; |
| 482 | } |
| 483 | |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 484 | /* |
| 485 | * is l2 fully contained in l1? |
| 486 | * start1 end1 |
| 487 | * [----------------------------------) |
| 488 | * start2 end2 |
| 489 | * [----------------) |
| 490 | */ |
Trond Myklebust | 3cb2df1 | 2013-06-03 11:24:36 -0400 | [diff] [blame] | 491 | static bool |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 492 | pnfs_lseg_range_contained(const struct pnfs_layout_range *l1, |
Trond Myklebust | 3cb2df1 | 2013-06-03 11:24:36 -0400 | [diff] [blame] | 493 | const struct pnfs_layout_range *l2) |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 494 | { |
| 495 | u64 start1 = l1->offset; |
| 496 | u64 end1 = end_offset(start1, l1->length); |
| 497 | u64 start2 = l2->offset; |
| 498 | u64 end2 = end_offset(start2, l2->length); |
| 499 | |
| 500 | return (start1 <= start2) && (end1 >= end2); |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * is l1 and l2 intersecting? |
| 505 | * start1 end1 |
| 506 | * [----------------------------------) |
| 507 | * start2 end2 |
| 508 | * [----------------) |
| 509 | */ |
Trond Myklebust | 3cb2df1 | 2013-06-03 11:24:36 -0400 | [diff] [blame] | 510 | static bool |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 511 | pnfs_lseg_range_intersecting(const struct pnfs_layout_range *l1, |
Trond Myklebust | 3cb2df1 | 2013-06-03 11:24:36 -0400 | [diff] [blame] | 512 | const struct pnfs_layout_range *l2) |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 513 | { |
| 514 | u64 start1 = l1->offset; |
| 515 | u64 end1 = end_offset(start1, l1->length); |
| 516 | u64 start2 = l2->offset; |
| 517 | u64 end2 = end_offset(start2, l2->length); |
| 518 | |
| 519 | return (end1 == NFS4_MAX_UINT64 || end1 > start2) && |
| 520 | (end2 == NFS4_MAX_UINT64 || end2 > start1); |
| 521 | } |
| 522 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 523 | static bool |
Trond Myklebust | 3cb2df1 | 2013-06-03 11:24:36 -0400 | [diff] [blame] | 524 | should_free_lseg(const struct pnfs_layout_range *lseg_range, |
| 525 | const struct pnfs_layout_range *recall_range) |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 526 | { |
Benny Halevy | 778b550 | 2011-05-22 19:48:02 +0300 | [diff] [blame] | 527 | return (recall_range->iomode == IOMODE_ANY || |
| 528 | lseg_range->iomode == recall_range->iomode) && |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 529 | pnfs_lseg_range_intersecting(lseg_range, recall_range); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Trond Myklebust | 2495680 | 2013-03-20 13:03:00 -0400 | [diff] [blame] | 532 | static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg, |
| 533 | struct list_head *tmp_list) |
| 534 | { |
| 535 | if (!atomic_dec_and_test(&lseg->pls_refcount)) |
| 536 | return false; |
| 537 | pnfs_layout_remove_lseg(lseg->pls_layout, lseg); |
| 538 | list_add(&lseg->pls_list, tmp_list); |
| 539 | return true; |
| 540 | } |
| 541 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 542 | /* Returns 1 if lseg is removed from list, 0 otherwise */ |
| 543 | static int mark_lseg_invalid(struct pnfs_layout_segment *lseg, |
| 544 | struct list_head *tmp_list) |
| 545 | { |
| 546 | int rv = 0; |
| 547 | |
| 548 | if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) { |
| 549 | /* Remove the reference keeping the lseg in the |
| 550 | * list. It will now be removed when all |
| 551 | * outstanding io is finished. |
| 552 | */ |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 553 | dprintk("%s: lseg %p ref %d\n", __func__, lseg, |
| 554 | atomic_read(&lseg->pls_refcount)); |
Trond Myklebust | 2495680 | 2013-03-20 13:03:00 -0400 | [diff] [blame] | 555 | if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list)) |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 556 | rv = 1; |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 557 | } |
| 558 | return rv; |
| 559 | } |
| 560 | |
| 561 | /* Returns count of number of matching invalid lsegs remaining in list |
| 562 | * after call. |
| 563 | */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 564 | int |
Trond Myklebust | 49a8506 | 2012-09-18 20:43:31 -0400 | [diff] [blame] | 565 | pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo, |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 566 | struct list_head *tmp_list, |
Benny Halevy | 778b550 | 2011-05-22 19:48:02 +0300 | [diff] [blame] | 567 | struct pnfs_layout_range *recall_range) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 568 | { |
| 569 | struct pnfs_layout_segment *lseg, *next; |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 570 | int invalid = 0, removed = 0; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 571 | |
| 572 | dprintk("%s:Begin lo %p\n", __func__, lo); |
| 573 | |
Trond Myklebust | 8006bfb | 2012-09-21 14:48:04 -0400 | [diff] [blame] | 574 | if (list_empty(&lo->plh_segs)) |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 575 | return 0; |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 576 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) |
Benny Halevy | 778b550 | 2011-05-22 19:48:02 +0300 | [diff] [blame] | 577 | if (!recall_range || |
| 578 | should_free_lseg(&lseg->pls_range, recall_range)) { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 579 | dprintk("%s: freeing lseg %p iomode %d " |
| 580 | "offset %llu length %llu\n", __func__, |
| 581 | lseg, lseg->pls_range.iomode, lseg->pls_range.offset, |
| 582 | lseg->pls_range.length); |
| 583 | invalid++; |
| 584 | removed += mark_lseg_invalid(lseg, tmp_list); |
| 585 | } |
| 586 | dprintk("%s:Return %i\n", __func__, invalid - removed); |
| 587 | return invalid - removed; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 588 | } |
| 589 | |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 590 | /* note free_me must contain lsegs from a single layout_hdr */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 591 | void |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 592 | pnfs_free_lseg_list(struct list_head *free_me) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 593 | { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 594 | struct pnfs_layout_segment *lseg, *tmp; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 595 | |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 596 | if (list_empty(free_me)) |
| 597 | return; |
| 598 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 599 | list_for_each_entry_safe(lseg, tmp, free_me, pls_list) { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 600 | list_del(&lseg->pls_list); |
Trond Myklebust | 905ca19 | 2012-09-20 20:46:49 -0400 | [diff] [blame] | 601 | pnfs_free_lseg(lseg); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 605 | void |
| 606 | pnfs_destroy_layout(struct nfs_inode *nfsi) |
| 607 | { |
| 608 | struct pnfs_layout_hdr *lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 609 | LIST_HEAD(tmp_list); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 610 | |
| 611 | spin_lock(&nfsi->vfs_inode.i_lock); |
| 612 | lo = nfsi->layout; |
| 613 | if (lo) { |
Fred Isaman | 3851172 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 614 | lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */ |
Trond Myklebust | 49a8506 | 2012-09-18 20:43:31 -0400 | [diff] [blame] | 615 | pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL); |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 616 | pnfs_get_layout_hdr(lo); |
| 617 | pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED); |
| 618 | pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED); |
Peng Tao | c829013 | 2014-12-01 08:22:18 +0800 | [diff] [blame] | 619 | pnfs_clear_retry_layoutget(lo); |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 620 | spin_unlock(&nfsi->vfs_inode.i_lock); |
| 621 | pnfs_free_lseg_list(&tmp_list); |
| 622 | pnfs_put_layout_hdr(lo); |
| 623 | } else |
| 624 | spin_unlock(&nfsi->vfs_inode.i_lock); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 625 | } |
Andy Adamson | 041245c | 2012-04-27 17:53:53 -0400 | [diff] [blame] | 626 | EXPORT_SYMBOL_GPL(pnfs_destroy_layout); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 627 | |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 628 | static bool |
| 629 | pnfs_layout_add_bulk_destroy_list(struct inode *inode, |
| 630 | struct list_head *layout_list) |
| 631 | { |
| 632 | struct pnfs_layout_hdr *lo; |
| 633 | bool ret = false; |
| 634 | |
| 635 | spin_lock(&inode->i_lock); |
| 636 | lo = NFS_I(inode)->layout; |
| 637 | if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) { |
| 638 | pnfs_get_layout_hdr(lo); |
| 639 | list_add(&lo->plh_bulk_destroy, layout_list); |
| 640 | ret = true; |
| 641 | } |
| 642 | spin_unlock(&inode->i_lock); |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | /* Caller must hold rcu_read_lock and clp->cl_lock */ |
| 647 | static int |
| 648 | pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp, |
| 649 | struct nfs_server *server, |
| 650 | struct list_head *layout_list) |
| 651 | { |
| 652 | struct pnfs_layout_hdr *lo, *next; |
| 653 | struct inode *inode; |
| 654 | |
| 655 | list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) { |
| 656 | inode = igrab(lo->plh_inode); |
| 657 | if (inode == NULL) |
| 658 | continue; |
| 659 | list_del_init(&lo->plh_layouts); |
| 660 | if (pnfs_layout_add_bulk_destroy_list(inode, layout_list)) |
| 661 | continue; |
| 662 | rcu_read_unlock(); |
| 663 | spin_unlock(&clp->cl_lock); |
| 664 | iput(inode); |
| 665 | spin_lock(&clp->cl_lock); |
| 666 | rcu_read_lock(); |
| 667 | return -EAGAIN; |
| 668 | } |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | static int |
| 673 | pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list, |
| 674 | bool is_bulk_recall) |
| 675 | { |
| 676 | struct pnfs_layout_hdr *lo; |
| 677 | struct inode *inode; |
| 678 | struct pnfs_layout_range range = { |
| 679 | .iomode = IOMODE_ANY, |
| 680 | .offset = 0, |
| 681 | .length = NFS4_MAX_UINT64, |
| 682 | }; |
| 683 | LIST_HEAD(lseg_list); |
| 684 | int ret = 0; |
| 685 | |
| 686 | while (!list_empty(layout_list)) { |
| 687 | lo = list_entry(layout_list->next, struct pnfs_layout_hdr, |
| 688 | plh_bulk_destroy); |
| 689 | dprintk("%s freeing layout for inode %lu\n", __func__, |
| 690 | lo->plh_inode->i_ino); |
| 691 | inode = lo->plh_inode; |
Christoph Hellwig | 7c5d187 | 2014-09-10 08:23:29 -0700 | [diff] [blame] | 692 | |
| 693 | pnfs_layoutcommit_inode(inode, false); |
| 694 | |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 695 | spin_lock(&inode->i_lock); |
| 696 | list_del_init(&lo->plh_bulk_destroy); |
| 697 | lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */ |
| 698 | if (is_bulk_recall) |
| 699 | set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); |
| 700 | if (pnfs_mark_matching_lsegs_invalid(lo, &lseg_list, &range)) |
| 701 | ret = -EAGAIN; |
| 702 | spin_unlock(&inode->i_lock); |
| 703 | pnfs_free_lseg_list(&lseg_list); |
| 704 | pnfs_put_layout_hdr(lo); |
| 705 | iput(inode); |
| 706 | } |
| 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | int |
| 711 | pnfs_destroy_layouts_byfsid(struct nfs_client *clp, |
| 712 | struct nfs_fsid *fsid, |
| 713 | bool is_recall) |
| 714 | { |
| 715 | struct nfs_server *server; |
| 716 | LIST_HEAD(layout_list); |
| 717 | |
| 718 | spin_lock(&clp->cl_lock); |
| 719 | rcu_read_lock(); |
| 720 | restart: |
| 721 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { |
| 722 | if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0) |
| 723 | continue; |
| 724 | if (pnfs_layout_bulk_destroy_byserver_locked(clp, |
| 725 | server, |
| 726 | &layout_list) != 0) |
| 727 | goto restart; |
| 728 | } |
| 729 | rcu_read_unlock(); |
| 730 | spin_unlock(&clp->cl_lock); |
| 731 | |
| 732 | if (list_empty(&layout_list)) |
| 733 | return 0; |
| 734 | return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall); |
| 735 | } |
| 736 | |
| 737 | int |
| 738 | pnfs_destroy_layouts_byclid(struct nfs_client *clp, |
| 739 | bool is_recall) |
| 740 | { |
| 741 | struct nfs_server *server; |
| 742 | LIST_HEAD(layout_list); |
| 743 | |
| 744 | spin_lock(&clp->cl_lock); |
| 745 | rcu_read_lock(); |
| 746 | restart: |
| 747 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { |
| 748 | if (pnfs_layout_bulk_destroy_byserver_locked(clp, |
| 749 | server, |
| 750 | &layout_list) != 0) |
| 751 | goto restart; |
| 752 | } |
| 753 | rcu_read_unlock(); |
| 754 | spin_unlock(&clp->cl_lock); |
| 755 | |
| 756 | if (list_empty(&layout_list)) |
| 757 | return 0; |
| 758 | return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall); |
| 759 | } |
| 760 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 761 | /* |
| 762 | * Called by the state manger to remove all layouts established under an |
| 763 | * expired lease. |
| 764 | */ |
| 765 | void |
| 766 | pnfs_destroy_all_layouts(struct nfs_client *clp) |
| 767 | { |
Andy Adamson | c47abcf | 2011-06-15 17:52:40 -0400 | [diff] [blame] | 768 | nfs4_deviceid_mark_client_invalid(clp); |
| 769 | nfs4_deviceid_purge_client(clp); |
| 770 | |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 771 | pnfs_destroy_layouts_byclid(clp, false); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 772 | } |
| 773 | |
Trond Myklebust | 5a65503 | 2012-10-02 16:47:14 -0700 | [diff] [blame] | 774 | /* |
| 775 | * Compare 2 layout stateid sequence ids, to see which is newer, |
| 776 | * taking into account wraparound issues. |
| 777 | */ |
| 778 | static bool pnfs_seqid_is_newer(u32 s1, u32 s2) |
| 779 | { |
Trond Myklebust | 2c64c57 | 2014-02-12 09:36:59 -0500 | [diff] [blame] | 780 | return (s32)(s1 - s2) > 0; |
Trond Myklebust | 5a65503 | 2012-10-02 16:47:14 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 783 | /* update lo->plh_stateid with new if is more recent */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 784 | void |
| 785 | pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new, |
| 786 | bool update_barrier) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 787 | { |
Trond Myklebust | 22aaf71 | 2012-10-04 16:32:22 -0700 | [diff] [blame] | 788 | u32 oldseq, newseq, new_barrier; |
| 789 | int empty = list_empty(&lo->plh_segs); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 790 | |
Trond Myklebust | 2d2f24a | 2012-03-04 18:13:57 -0500 | [diff] [blame] | 791 | oldseq = be32_to_cpu(lo->plh_stateid.seqid); |
| 792 | newseq = be32_to_cpu(new->seqid); |
Trond Myklebust | 22aaf71 | 2012-10-04 16:32:22 -0700 | [diff] [blame] | 793 | if (empty || pnfs_seqid_is_newer(newseq, oldseq)) { |
Trond Myklebust | f597c53 | 2012-03-04 18:13:56 -0500 | [diff] [blame] | 794 | nfs4_stateid_copy(&lo->plh_stateid, new); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 795 | if (update_barrier) { |
Trond Myklebust | 22aaf71 | 2012-10-04 16:32:22 -0700 | [diff] [blame] | 796 | new_barrier = be32_to_cpu(new->seqid); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 797 | } else { |
| 798 | /* Because of wraparound, we want to keep the barrier |
Trond Myklebust | 22aaf71 | 2012-10-04 16:32:22 -0700 | [diff] [blame] | 799 | * "close" to the current seqids. |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 800 | */ |
Trond Myklebust | 22aaf71 | 2012-10-04 16:32:22 -0700 | [diff] [blame] | 801 | new_barrier = newseq - atomic_read(&lo->plh_outstanding); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 802 | } |
Trond Myklebust | 22aaf71 | 2012-10-04 16:32:22 -0700 | [diff] [blame] | 803 | if (empty || pnfs_seqid_is_newer(new_barrier, lo->plh_barrier)) |
| 804 | lo->plh_barrier = new_barrier; |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 805 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 806 | } |
| 807 | |
Trond Myklebust | 19c54ab | 2012-10-05 16:56:58 -0700 | [diff] [blame] | 808 | static bool |
| 809 | pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo, |
| 810 | const nfs4_stateid *stateid) |
| 811 | { |
| 812 | u32 seqid = be32_to_cpu(stateid->seqid); |
| 813 | |
| 814 | return !pnfs_seqid_is_newer(seqid, lo->plh_barrier); |
| 815 | } |
| 816 | |
Peng Tao | ce6ab4f | 2014-09-06 00:53:24 +0800 | [diff] [blame] | 817 | static bool |
| 818 | pnfs_layout_returning(const struct pnfs_layout_hdr *lo, |
| 819 | struct pnfs_layout_range *range) |
| 820 | { |
| 821 | return test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) && |
| 822 | (lo->plh_return_iomode == IOMODE_ANY || |
| 823 | lo->plh_return_iomode == range->iomode); |
| 824 | } |
| 825 | |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 826 | /* lget is set to 1 if called from inside send_layoutget call chain */ |
| 827 | static bool |
Peng Tao | ce6ab4f | 2014-09-06 00:53:24 +0800 | [diff] [blame] | 828 | pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo, |
| 829 | struct pnfs_layout_range *range, int lget) |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 830 | { |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 831 | return lo->plh_block_lgets || |
| 832 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) || |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 833 | (list_empty(&lo->plh_segs) && |
Peng Tao | ce6ab4f | 2014-09-06 00:53:24 +0800 | [diff] [blame] | 834 | (atomic_read(&lo->plh_outstanding) > lget)) || |
| 835 | pnfs_layout_returning(lo, range); |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 838 | int |
| 839 | pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo, |
Peng Tao | ce6ab4f | 2014-09-06 00:53:24 +0800 | [diff] [blame] | 840 | struct pnfs_layout_range *range, |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 841 | struct nfs4_state *open_state) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 842 | { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 843 | int status = 0; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 844 | |
| 845 | dprintk("--> %s\n", __func__); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 846 | spin_lock(&lo->plh_inode->i_lock); |
Peng Tao | ce6ab4f | 2014-09-06 00:53:24 +0800 | [diff] [blame] | 847 | if (pnfs_layoutgets_blocked(lo, range, 1)) { |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 848 | status = -EAGAIN; |
Trond Myklebust | 5d42230 | 2013-03-14 16:57:48 -0400 | [diff] [blame] | 849 | } else if (!nfs4_valid_open_stateid(open_state)) { |
| 850 | status = -EBADF; |
Christoph Hellwig | 47abade | 2014-08-21 11:09:22 -0500 | [diff] [blame] | 851 | } else if (list_empty(&lo->plh_segs) || |
| 852 | test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) { |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 853 | int seq; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 854 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 855 | do { |
| 856 | seq = read_seqbegin(&open_state->seqlock); |
Trond Myklebust | f597c53 | 2012-03-04 18:13:56 -0500 | [diff] [blame] | 857 | nfs4_stateid_copy(dst, &open_state->stateid); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 858 | } while (read_seqretry(&open_state->seqlock, seq)); |
| 859 | } else |
Trond Myklebust | f597c53 | 2012-03-04 18:13:56 -0500 | [diff] [blame] | 860 | nfs4_stateid_copy(dst, &lo->plh_stateid); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 861 | spin_unlock(&lo->plh_inode->i_lock); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 862 | dprintk("<-- %s\n", __func__); |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 863 | return status; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /* |
| 867 | * Get layout from server. |
| 868 | * for now, assume that whole file layouts are requested. |
| 869 | * arg->offset: 0 |
| 870 | * arg->length: all ones |
| 871 | */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 872 | static struct pnfs_layout_segment * |
| 873 | send_layoutget(struct pnfs_layout_hdr *lo, |
| 874 | struct nfs_open_context *ctx, |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 875 | struct pnfs_layout_range *range, |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 876 | gfp_t gfp_flags) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 877 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 878 | struct inode *ino = lo->plh_inode; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 879 | struct nfs_server *server = NFS_SERVER(ino); |
| 880 | struct nfs4_layoutget *lgp; |
Trond Myklebust | a0b0a6e | 2012-09-17 17:12:15 -0400 | [diff] [blame] | 881 | struct pnfs_layout_segment *lseg; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 882 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 883 | dprintk("--> %s\n", __func__); |
| 884 | |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 885 | lgp = kzalloc(sizeof(*lgp), gfp_flags); |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 886 | if (lgp == NULL) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 887 | return NULL; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 888 | |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 889 | lgp->args.minlength = PAGE_CACHE_SIZE; |
| 890 | if (lgp->args.minlength > range->length) |
| 891 | lgp->args.minlength = range->length; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 892 | lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE; |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 893 | lgp->args.range = *range; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 894 | lgp->args.type = server->pnfs_curr_ld->id; |
| 895 | lgp->args.inode = ino; |
| 896 | lgp->args.ctx = get_nfs_open_context(ctx); |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 897 | lgp->gfp_flags = gfp_flags; |
Trond Myklebust | 6ab5934 | 2013-05-20 10:49:34 -0400 | [diff] [blame] | 898 | lgp->cred = lo->plh_lc_cred; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 899 | |
| 900 | /* Synchronously retrieve layout information from server and |
| 901 | * store in lseg. |
| 902 | */ |
Trond Myklebust | a0b0a6e | 2012-09-17 17:12:15 -0400 | [diff] [blame] | 903 | lseg = nfs4_proc_layoutget(lgp, gfp_flags); |
| 904 | if (IS_ERR(lseg)) { |
| 905 | switch (PTR_ERR(lseg)) { |
| 906 | case -ENOMEM: |
| 907 | case -ERESTARTSYS: |
| 908 | break; |
| 909 | default: |
| 910 | /* remember that LAYOUTGET failed and suspend trying */ |
Trond Myklebust | b9e028f | 2012-09-18 16:41:18 -0400 | [diff] [blame] | 911 | pnfs_layout_io_set_failed(lo, range->iomode); |
Trond Myklebust | a0b0a6e | 2012-09-17 17:12:15 -0400 | [diff] [blame] | 912 | } |
| 913 | return NULL; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 914 | } else |
| 915 | pnfs_layout_clear_fail_bit(lo, |
| 916 | pnfs_iomode_to_fail_bit(range->iomode)); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 917 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 918 | return lseg; |
| 919 | } |
| 920 | |
Trond Myklebust | 2495680 | 2013-03-20 13:03:00 -0400 | [diff] [blame] | 921 | static void pnfs_clear_layoutcommit(struct inode *inode, |
| 922 | struct list_head *head) |
| 923 | { |
| 924 | struct nfs_inode *nfsi = NFS_I(inode); |
| 925 | struct pnfs_layout_segment *lseg, *tmp; |
| 926 | |
| 927 | if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) |
| 928 | return; |
| 929 | list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) { |
| 930 | if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) |
| 931 | continue; |
| 932 | pnfs_lseg_dec_and_remove_zero(lseg, head); |
| 933 | } |
| 934 | } |
| 935 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 936 | void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo) |
| 937 | { |
| 938 | clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags); |
| 939 | smp_mb__after_atomic(); |
| 940 | wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN); |
| 941 | } |
| 942 | |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 943 | static int |
| 944 | pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, nfs4_stateid stateid, |
Peng Tao | 6c16605 | 2014-11-17 09:30:40 +0800 | [diff] [blame] | 945 | enum pnfs_iomode iomode, bool sync) |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 946 | { |
| 947 | struct inode *ino = lo->plh_inode; |
| 948 | struct nfs4_layoutreturn *lrp; |
| 949 | int status = 0; |
| 950 | |
Trond Myklebust | e4af440 | 2015-02-05 17:05:08 -0500 | [diff] [blame^] | 951 | lrp = kzalloc(sizeof(*lrp), GFP_NOFS); |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 952 | if (unlikely(lrp == NULL)) { |
| 953 | status = -ENOMEM; |
| 954 | spin_lock(&ino->i_lock); |
| 955 | lo->plh_block_lgets--; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 956 | pnfs_clear_layoutreturn_waitbit(lo); |
Peng Tao | 193e3aa | 2014-11-17 09:30:41 +0800 | [diff] [blame] | 957 | rpc_wake_up(&NFS_SERVER(ino)->roc_rpcwaitq); |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 958 | spin_unlock(&ino->i_lock); |
| 959 | pnfs_put_layout_hdr(lo); |
| 960 | goto out; |
| 961 | } |
| 962 | |
| 963 | lrp->args.stateid = stateid; |
| 964 | lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id; |
| 965 | lrp->args.inode = ino; |
Peng Tao | 15eb67c | 2014-11-17 09:30:36 +0800 | [diff] [blame] | 966 | lrp->args.range.iomode = iomode; |
| 967 | lrp->args.range.offset = 0; |
| 968 | lrp->args.range.length = NFS4_MAX_UINT64; |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 969 | lrp->args.layout = lo; |
| 970 | lrp->clp = NFS_SERVER(ino)->nfs_client; |
| 971 | lrp->cred = lo->plh_lc_cred; |
| 972 | |
Peng Tao | 6c16605 | 2014-11-17 09:30:40 +0800 | [diff] [blame] | 973 | status = nfs4_proc_layoutreturn(lrp, sync); |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 974 | out: |
| 975 | dprintk("<-- %s status: %d\n", __func__, status); |
| 976 | return status; |
| 977 | } |
| 978 | |
Andy Adamson | 293b3b0 | 2012-06-20 15:03:34 -0400 | [diff] [blame] | 979 | /* |
| 980 | * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr |
| 981 | * when the layout segment list is empty. |
| 982 | * |
| 983 | * Note that a pnfs_layout_hdr can exist with an empty layout segment |
| 984 | * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the |
| 985 | * deviceid is marked invalid. |
| 986 | */ |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 987 | int |
| 988 | _pnfs_return_layout(struct inode *ino) |
| 989 | { |
| 990 | struct pnfs_layout_hdr *lo = NULL; |
| 991 | struct nfs_inode *nfsi = NFS_I(ino); |
| 992 | LIST_HEAD(tmp_list); |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 993 | nfs4_stateid stateid; |
Andy Adamson | 293b3b0 | 2012-06-20 15:03:34 -0400 | [diff] [blame] | 994 | int status = 0, empty; |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 995 | |
Andy Adamson | 366d505 | 2012-06-20 15:03:33 -0400 | [diff] [blame] | 996 | dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino); |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 997 | |
| 998 | spin_lock(&ino->i_lock); |
| 999 | lo = nfsi->layout; |
Trond Myklebust | e5929f3 | 2012-09-21 16:37:02 -0400 | [diff] [blame] | 1000 | if (!lo) { |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1001 | spin_unlock(&ino->i_lock); |
Andy Adamson | 293b3b0 | 2012-06-20 15:03:34 -0400 | [diff] [blame] | 1002 | dprintk("NFS: %s no layout to return\n", __func__); |
| 1003 | goto out; |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1004 | } |
| 1005 | stateid = nfsi->layout->plh_stateid; |
| 1006 | /* Reference matched in nfs4_layoutreturn_release */ |
Trond Myklebust | 70c3bd2 | 2012-09-18 20:51:13 -0400 | [diff] [blame] | 1007 | pnfs_get_layout_hdr(lo); |
Andy Adamson | 293b3b0 | 2012-06-20 15:03:34 -0400 | [diff] [blame] | 1008 | empty = list_empty(&lo->plh_segs); |
Trond Myklebust | 2495680 | 2013-03-20 13:03:00 -0400 | [diff] [blame] | 1009 | pnfs_clear_layoutcommit(ino, &tmp_list); |
Trond Myklebust | 49a8506 | 2012-09-18 20:43:31 -0400 | [diff] [blame] | 1010 | pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL); |
Christoph Hellwig | c88953d | 2014-09-10 08:23:31 -0700 | [diff] [blame] | 1011 | |
| 1012 | if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) { |
| 1013 | struct pnfs_layout_range range = { |
| 1014 | .iomode = IOMODE_ANY, |
| 1015 | .offset = 0, |
| 1016 | .length = NFS4_MAX_UINT64, |
| 1017 | }; |
| 1018 | NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range); |
| 1019 | } |
| 1020 | |
Andy Adamson | 293b3b0 | 2012-06-20 15:03:34 -0400 | [diff] [blame] | 1021 | /* Don't send a LAYOUTRETURN if list was initially empty */ |
| 1022 | if (empty) { |
| 1023 | spin_unlock(&ino->i_lock); |
Trond Myklebust | 70c3bd2 | 2012-09-18 20:51:13 -0400 | [diff] [blame] | 1024 | pnfs_put_layout_hdr(lo); |
Andy Adamson | 293b3b0 | 2012-06-20 15:03:34 -0400 | [diff] [blame] | 1025 | dprintk("NFS: %s no layout segments to return\n", __func__); |
| 1026 | goto out; |
| 1027 | } |
Christoph Hellwig | 47abade | 2014-08-21 11:09:22 -0500 | [diff] [blame] | 1028 | |
| 1029 | set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); |
Fred Isaman | ea0ded7 | 2011-06-15 12:31:02 -0400 | [diff] [blame] | 1030 | lo->plh_block_lgets++; |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1031 | spin_unlock(&ino->i_lock); |
| 1032 | pnfs_free_lseg_list(&tmp_list); |
| 1033 | |
Peng Tao | 6c16605 | 2014-11-17 09:30:40 +0800 | [diff] [blame] | 1034 | status = pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, true); |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1035 | out: |
| 1036 | dprintk("<-- %s status: %d\n", __func__, status); |
| 1037 | return status; |
| 1038 | } |
Andy Adamson | 0a57cda | 2012-04-27 17:53:50 -0400 | [diff] [blame] | 1039 | EXPORT_SYMBOL_GPL(_pnfs_return_layout); |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1040 | |
Trond Myklebust | 2402867 | 2013-03-20 13:23:33 -0400 | [diff] [blame] | 1041 | int |
| 1042 | pnfs_commit_and_return_layout(struct inode *inode) |
| 1043 | { |
| 1044 | struct pnfs_layout_hdr *lo; |
| 1045 | int ret; |
| 1046 | |
| 1047 | spin_lock(&inode->i_lock); |
| 1048 | lo = NFS_I(inode)->layout; |
| 1049 | if (lo == NULL) { |
| 1050 | spin_unlock(&inode->i_lock); |
| 1051 | return 0; |
| 1052 | } |
| 1053 | pnfs_get_layout_hdr(lo); |
| 1054 | /* Block new layoutgets and read/write to ds */ |
| 1055 | lo->plh_block_lgets++; |
| 1056 | spin_unlock(&inode->i_lock); |
| 1057 | filemap_fdatawait(inode->i_mapping); |
| 1058 | ret = pnfs_layoutcommit_inode(inode, true); |
| 1059 | if (ret == 0) |
| 1060 | ret = _pnfs_return_layout(inode); |
| 1061 | spin_lock(&inode->i_lock); |
| 1062 | lo->plh_block_lgets--; |
| 1063 | spin_unlock(&inode->i_lock); |
| 1064 | pnfs_put_layout_hdr(lo); |
| 1065 | return ret; |
| 1066 | } |
| 1067 | |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1068 | bool pnfs_roc(struct inode *ino) |
| 1069 | { |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1070 | struct nfs_inode *nfsi = NFS_I(ino); |
| 1071 | struct nfs_open_context *ctx; |
| 1072 | struct nfs4_state *state; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1073 | struct pnfs_layout_hdr *lo; |
| 1074 | struct pnfs_layout_segment *lseg, *tmp; |
Peng Tao | 193e3aa | 2014-11-17 09:30:41 +0800 | [diff] [blame] | 1075 | nfs4_stateid stateid; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1076 | LIST_HEAD(tmp_list); |
Peng Tao | 193e3aa | 2014-11-17 09:30:41 +0800 | [diff] [blame] | 1077 | bool found = false, layoutreturn = false; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1078 | |
| 1079 | spin_lock(&ino->i_lock); |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1080 | lo = nfsi->layout; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1081 | if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) || |
| 1082 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1083 | goto out_noroc; |
| 1084 | |
| 1085 | /* Don't return layout if we hold a delegation */ |
| 1086 | if (nfs4_check_delegation(ino, FMODE_READ)) |
| 1087 | goto out_noroc; |
| 1088 | |
| 1089 | list_for_each_entry(ctx, &nfsi->open_files, list) { |
| 1090 | state = ctx->state; |
| 1091 | /* Don't return layout if there is open file state */ |
| 1092 | if (state != NULL && state->state != 0) |
| 1093 | goto out_noroc; |
| 1094 | } |
| 1095 | |
Trond Myklebust | e2c63e0 | 2015-02-03 16:01:27 -0500 | [diff] [blame] | 1096 | goto out_noroc; |
Peng Tao | c829013 | 2014-12-01 08:22:18 +0800 | [diff] [blame] | 1097 | pnfs_clear_retry_layoutget(lo); |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1098 | list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list) |
| 1099 | if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { |
| 1100 | mark_lseg_invalid(lseg, &tmp_list); |
| 1101 | found = true; |
| 1102 | } |
| 1103 | if (!found) |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1104 | goto out_noroc; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1105 | lo->plh_block_lgets++; |
Trond Myklebust | 70c3bd2 | 2012-09-18 20:51:13 -0400 | [diff] [blame] | 1106 | pnfs_get_layout_hdr(lo); /* matched in pnfs_roc_release */ |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1107 | spin_unlock(&ino->i_lock); |
| 1108 | pnfs_free_lseg_list(&tmp_list); |
| 1109 | return true; |
| 1110 | |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1111 | out_noroc: |
Peng Tao | 193e3aa | 2014-11-17 09:30:41 +0800 | [diff] [blame] | 1112 | if (lo) { |
| 1113 | stateid = lo->plh_stateid; |
| 1114 | layoutreturn = |
| 1115 | test_and_clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE, |
| 1116 | &lo->plh_flags); |
| 1117 | if (layoutreturn) { |
| 1118 | lo->plh_block_lgets++; |
| 1119 | pnfs_get_layout_hdr(lo); |
| 1120 | } |
| 1121 | } |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1122 | spin_unlock(&ino->i_lock); |
Peng Tao | 193e3aa | 2014-11-17 09:30:41 +0800 | [diff] [blame] | 1123 | if (layoutreturn) |
Peng Tao | 27b6f53 | 2014-10-20 14:44:38 +0800 | [diff] [blame] | 1124 | pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, true); |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1125 | return false; |
| 1126 | } |
| 1127 | |
| 1128 | void pnfs_roc_release(struct inode *ino) |
| 1129 | { |
| 1130 | struct pnfs_layout_hdr *lo; |
| 1131 | |
| 1132 | spin_lock(&ino->i_lock); |
| 1133 | lo = NFS_I(ino)->layout; |
| 1134 | lo->plh_block_lgets--; |
Trond Myklebust | 6622c3e | 2012-09-20 17:23:11 -0400 | [diff] [blame] | 1135 | if (atomic_dec_and_test(&lo->plh_refcount)) { |
| 1136 | pnfs_detach_layout_hdr(lo); |
| 1137 | spin_unlock(&ino->i_lock); |
| 1138 | pnfs_free_layout_hdr(lo); |
| 1139 | } else |
| 1140 | spin_unlock(&ino->i_lock); |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | void pnfs_roc_set_barrier(struct inode *ino, u32 barrier) |
| 1144 | { |
| 1145 | struct pnfs_layout_hdr *lo; |
| 1146 | |
| 1147 | spin_lock(&ino->i_lock); |
| 1148 | lo = NFS_I(ino)->layout; |
Trond Myklebust | 0f35ad6 | 2012-10-04 16:28:17 -0700 | [diff] [blame] | 1149 | if (pnfs_seqid_is_newer(barrier, lo->plh_barrier)) |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1150 | lo->plh_barrier = barrier; |
| 1151 | spin_unlock(&ino->i_lock); |
| 1152 | } |
| 1153 | |
Trond Myklebust | 7fdab06 | 2012-09-20 20:15:57 -0400 | [diff] [blame] | 1154 | bool pnfs_roc_drain(struct inode *ino, u32 *barrier, struct rpc_task *task) |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1155 | { |
| 1156 | struct nfs_inode *nfsi = NFS_I(ino); |
Trond Myklebust | 7fdab06 | 2012-09-20 20:15:57 -0400 | [diff] [blame] | 1157 | struct pnfs_layout_hdr *lo; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1158 | struct pnfs_layout_segment *lseg; |
Peng Tao | 193e3aa | 2014-11-17 09:30:41 +0800 | [diff] [blame] | 1159 | nfs4_stateid stateid; |
Trond Myklebust | 7fdab06 | 2012-09-20 20:15:57 -0400 | [diff] [blame] | 1160 | u32 current_seqid; |
Peng Tao | 193e3aa | 2014-11-17 09:30:41 +0800 | [diff] [blame] | 1161 | bool found = false, layoutreturn = false; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1162 | |
| 1163 | spin_lock(&ino->i_lock); |
| 1164 | list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list) |
| 1165 | if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { |
Trond Myklebust | 7fdab06 | 2012-09-20 20:15:57 -0400 | [diff] [blame] | 1166 | rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL); |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1167 | found = true; |
Trond Myklebust | 7fdab06 | 2012-09-20 20:15:57 -0400 | [diff] [blame] | 1168 | goto out; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1169 | } |
Trond Myklebust | 7fdab06 | 2012-09-20 20:15:57 -0400 | [diff] [blame] | 1170 | lo = nfsi->layout; |
| 1171 | current_seqid = be32_to_cpu(lo->plh_stateid.seqid); |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1172 | |
Trond Myklebust | 7fdab06 | 2012-09-20 20:15:57 -0400 | [diff] [blame] | 1173 | /* Since close does not return a layout stateid for use as |
| 1174 | * a barrier, we choose the worst-case barrier. |
| 1175 | */ |
| 1176 | *barrier = current_seqid + atomic_read(&lo->plh_outstanding); |
| 1177 | out: |
Peng Tao | 193e3aa | 2014-11-17 09:30:41 +0800 | [diff] [blame] | 1178 | if (!found) { |
| 1179 | stateid = lo->plh_stateid; |
| 1180 | layoutreturn = |
| 1181 | test_and_clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE, |
| 1182 | &lo->plh_flags); |
| 1183 | if (layoutreturn) { |
| 1184 | lo->plh_block_lgets++; |
| 1185 | pnfs_get_layout_hdr(lo); |
| 1186 | } |
| 1187 | } |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1188 | spin_unlock(&ino->i_lock); |
Peng Tao | 193e3aa | 2014-11-17 09:30:41 +0800 | [diff] [blame] | 1189 | if (layoutreturn) { |
| 1190 | rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL); |
Peng Tao | 27b6f53 | 2014-10-20 14:44:38 +0800 | [diff] [blame] | 1191 | pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, false); |
Peng Tao | 193e3aa | 2014-11-17 09:30:41 +0800 | [diff] [blame] | 1192 | } |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1193 | return found; |
| 1194 | } |
| 1195 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1196 | /* |
| 1197 | * Compare two layout segments for sorting into layout cache. |
| 1198 | * We want to preferentially return RW over RO layouts, so ensure those |
| 1199 | * are seen first. |
| 1200 | */ |
| 1201 | static s64 |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 1202 | pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1, |
Trond Myklebust | 3cb2df1 | 2013-06-03 11:24:36 -0400 | [diff] [blame] | 1203 | const struct pnfs_layout_range *l2) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1204 | { |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1205 | s64 d; |
| 1206 | |
| 1207 | /* high offset > low offset */ |
| 1208 | d = l1->offset - l2->offset; |
| 1209 | if (d) |
| 1210 | return d; |
| 1211 | |
| 1212 | /* short length > long length */ |
| 1213 | d = l2->length - l1->length; |
| 1214 | if (d) |
| 1215 | return d; |
| 1216 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1217 | /* read > read/write */ |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1218 | return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1219 | } |
| 1220 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 1221 | static void |
Trond Myklebust | 57036a3 | 2012-09-20 16:33:30 -0400 | [diff] [blame] | 1222 | pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo, |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 1223 | struct pnfs_layout_segment *lseg) |
| 1224 | { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1225 | struct pnfs_layout_segment *lp; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1226 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 1227 | dprintk("%s:Begin\n", __func__); |
| 1228 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 1229 | list_for_each_entry(lp, &lo->plh_segs, pls_list) { |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 1230 | if (pnfs_lseg_range_cmp(&lseg->pls_range, &lp->pls_range) > 0) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1231 | continue; |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 1232 | list_add_tail(&lseg->pls_list, &lp->pls_list); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1233 | dprintk("%s: inserted lseg %p " |
| 1234 | "iomode %d offset %llu length %llu before " |
| 1235 | "lp %p iomode %d offset %llu length %llu\n", |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 1236 | __func__, lseg, lseg->pls_range.iomode, |
| 1237 | lseg->pls_range.offset, lseg->pls_range.length, |
| 1238 | lp, lp->pls_range.iomode, lp->pls_range.offset, |
| 1239 | lp->pls_range.length); |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1240 | goto out; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 1241 | } |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1242 | list_add_tail(&lseg->pls_list, &lo->plh_segs); |
| 1243 | dprintk("%s: inserted lseg %p " |
| 1244 | "iomode %d offset %llu length %llu at tail\n", |
| 1245 | __func__, lseg, lseg->pls_range.iomode, |
| 1246 | lseg->pls_range.offset, lseg->pls_range.length); |
| 1247 | out: |
Trond Myklebust | 70c3bd2 | 2012-09-18 20:51:13 -0400 | [diff] [blame] | 1248 | pnfs_get_layout_hdr(lo); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 1249 | |
| 1250 | dprintk("%s:Return\n", __func__); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | static struct pnfs_layout_hdr * |
Peng Tao | 9fa4075 | 2011-07-30 20:52:32 -0400 | [diff] [blame] | 1254 | alloc_init_layout_hdr(struct inode *ino, |
| 1255 | struct nfs_open_context *ctx, |
| 1256 | gfp_t gfp_flags) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1257 | { |
| 1258 | struct pnfs_layout_hdr *lo; |
| 1259 | |
Benny Halevy | 636fb9c | 2011-05-22 19:51:33 +0300 | [diff] [blame] | 1260 | lo = pnfs_alloc_layout_hdr(ino, gfp_flags); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1261 | if (!lo) |
| 1262 | return NULL; |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 1263 | atomic_set(&lo->plh_refcount, 1); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 1264 | INIT_LIST_HEAD(&lo->plh_layouts); |
| 1265 | INIT_LIST_HEAD(&lo->plh_segs); |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 1266 | INIT_LIST_HEAD(&lo->plh_bulk_destroy); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 1267 | lo->plh_inode = ino; |
Trond Myklebust | 5cc2216 | 2013-05-21 09:26:49 -0400 | [diff] [blame] | 1268 | lo->plh_lc_cred = get_rpccred(ctx->cred); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1269 | return lo; |
| 1270 | } |
| 1271 | |
| 1272 | static struct pnfs_layout_hdr * |
Peng Tao | 9fa4075 | 2011-07-30 20:52:32 -0400 | [diff] [blame] | 1273 | pnfs_find_alloc_layout(struct inode *ino, |
| 1274 | struct nfs_open_context *ctx, |
| 1275 | gfp_t gfp_flags) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1276 | { |
| 1277 | struct nfs_inode *nfsi = NFS_I(ino); |
| 1278 | struct pnfs_layout_hdr *new = NULL; |
| 1279 | |
| 1280 | dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout); |
| 1281 | |
Trond Myklebust | 251ec41 | 2012-10-02 15:41:05 -0700 | [diff] [blame] | 1282 | if (nfsi->layout != NULL) |
| 1283 | goto out_existing; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1284 | spin_unlock(&ino->i_lock); |
Peng Tao | 9fa4075 | 2011-07-30 20:52:32 -0400 | [diff] [blame] | 1285 | new = alloc_init_layout_hdr(ino, ctx, gfp_flags); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1286 | spin_lock(&ino->i_lock); |
| 1287 | |
Trond Myklebust | 251ec41 | 2012-10-02 15:41:05 -0700 | [diff] [blame] | 1288 | if (likely(nfsi->layout == NULL)) { /* Won the race? */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1289 | nfsi->layout = new; |
Trond Myklebust | 251ec41 | 2012-10-02 15:41:05 -0700 | [diff] [blame] | 1290 | return new; |
Yanchuan Nian | 7175fe9 | 2012-10-31 16:05:48 +0800 | [diff] [blame] | 1291 | } else if (new != NULL) |
| 1292 | pnfs_free_layout_hdr(new); |
Trond Myklebust | 251ec41 | 2012-10-02 15:41:05 -0700 | [diff] [blame] | 1293 | out_existing: |
| 1294 | pnfs_get_layout_hdr(nfsi->layout); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1295 | return nfsi->layout; |
| 1296 | } |
| 1297 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1298 | /* |
| 1299 | * iomode matching rules: |
| 1300 | * iomode lseg match |
| 1301 | * ----- ----- ----- |
| 1302 | * ANY READ true |
| 1303 | * ANY RW true |
| 1304 | * RW READ false |
| 1305 | * RW RW true |
| 1306 | * READ READ true |
| 1307 | * READ RW true |
| 1308 | */ |
Trond Myklebust | 3cb2df1 | 2013-06-03 11:24:36 -0400 | [diff] [blame] | 1309 | static bool |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 1310 | pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range, |
Trond Myklebust | 3cb2df1 | 2013-06-03 11:24:36 -0400 | [diff] [blame] | 1311 | const struct pnfs_layout_range *range) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1312 | { |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1313 | struct pnfs_layout_range range1; |
| 1314 | |
| 1315 | if ((range->iomode == IOMODE_RW && |
| 1316 | ls_range->iomode != IOMODE_RW) || |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 1317 | !pnfs_lseg_range_intersecting(ls_range, range)) |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1318 | return 0; |
| 1319 | |
| 1320 | /* range1 covers only the first byte in the range */ |
| 1321 | range1 = *range; |
| 1322 | range1.length = 1; |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 1323 | return pnfs_lseg_range_contained(ls_range, &range1); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | /* |
| 1327 | * lookup range in layout |
| 1328 | */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1329 | static struct pnfs_layout_segment * |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1330 | pnfs_find_lseg(struct pnfs_layout_hdr *lo, |
| 1331 | struct pnfs_layout_range *range) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1332 | { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1333 | struct pnfs_layout_segment *lseg, *ret = NULL; |
| 1334 | |
| 1335 | dprintk("%s:Begin\n", __func__); |
| 1336 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 1337 | list_for_each_entry(lseg, &lo->plh_segs, pls_list) { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 1338 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) && |
Peng Tao | ce6ab4f | 2014-09-06 00:53:24 +0800 | [diff] [blame] | 1339 | !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) && |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 1340 | pnfs_lseg_range_match(&lseg->pls_range, range)) { |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 1341 | ret = pnfs_get_lseg(lseg); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1342 | break; |
| 1343 | } |
Benny Halevy | d771e3a | 2011-06-14 16:30:16 -0400 | [diff] [blame] | 1344 | if (lseg->pls_range.offset > range->offset) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1345 | break; |
| 1346 | } |
| 1347 | |
| 1348 | dprintk("%s:Return lseg %p ref %d\n", |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 1349 | __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1350 | return ret; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | /* |
Andy Adamson | d23d61c | 2012-05-23 05:02:37 -0400 | [diff] [blame] | 1354 | * Use mdsthreshold hints set at each OPEN to determine if I/O should go |
| 1355 | * to the MDS or over pNFS |
| 1356 | * |
| 1357 | * The nfs_inode read_io and write_io fields are cumulative counters reset |
| 1358 | * when there are no layout segments. Note that in pnfs_update_layout iomode |
| 1359 | * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a |
| 1360 | * WRITE request. |
| 1361 | * |
| 1362 | * A return of true means use MDS I/O. |
| 1363 | * |
| 1364 | * From rfc 5661: |
| 1365 | * If a file's size is smaller than the file size threshold, data accesses |
| 1366 | * SHOULD be sent to the metadata server. If an I/O request has a length that |
| 1367 | * is below the I/O size threshold, the I/O SHOULD be sent to the metadata |
| 1368 | * server. If both file size and I/O size are provided, the client SHOULD |
| 1369 | * reach or exceed both thresholds before sending its read or write |
| 1370 | * requests to the data server. |
| 1371 | */ |
| 1372 | static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx, |
| 1373 | struct inode *ino, int iomode) |
| 1374 | { |
| 1375 | struct nfs4_threshold *t = ctx->mdsthreshold; |
| 1376 | struct nfs_inode *nfsi = NFS_I(ino); |
| 1377 | loff_t fsize = i_size_read(ino); |
| 1378 | bool size = false, size_set = false, io = false, io_set = false, ret = false; |
| 1379 | |
| 1380 | if (t == NULL) |
| 1381 | return ret; |
| 1382 | |
| 1383 | dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n", |
| 1384 | __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz); |
| 1385 | |
| 1386 | switch (iomode) { |
| 1387 | case IOMODE_READ: |
| 1388 | if (t->bm & THRESHOLD_RD) { |
| 1389 | dprintk("%s fsize %llu\n", __func__, fsize); |
| 1390 | size_set = true; |
| 1391 | if (fsize < t->rd_sz) |
| 1392 | size = true; |
| 1393 | } |
| 1394 | if (t->bm & THRESHOLD_RD_IO) { |
| 1395 | dprintk("%s nfsi->read_io %llu\n", __func__, |
| 1396 | nfsi->read_io); |
| 1397 | io_set = true; |
| 1398 | if (nfsi->read_io < t->rd_io_sz) |
| 1399 | io = true; |
| 1400 | } |
| 1401 | break; |
| 1402 | case IOMODE_RW: |
| 1403 | if (t->bm & THRESHOLD_WR) { |
| 1404 | dprintk("%s fsize %llu\n", __func__, fsize); |
| 1405 | size_set = true; |
| 1406 | if (fsize < t->wr_sz) |
| 1407 | size = true; |
| 1408 | } |
| 1409 | if (t->bm & THRESHOLD_WR_IO) { |
| 1410 | dprintk("%s nfsi->write_io %llu\n", __func__, |
| 1411 | nfsi->write_io); |
| 1412 | io_set = true; |
| 1413 | if (nfsi->write_io < t->wr_io_sz) |
| 1414 | io = true; |
| 1415 | } |
| 1416 | break; |
| 1417 | } |
| 1418 | if (size_set && io_set) { |
| 1419 | if (size && io) |
| 1420 | ret = true; |
| 1421 | } else if (size || io) |
| 1422 | ret = true; |
| 1423 | |
| 1424 | dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret); |
| 1425 | return ret; |
| 1426 | } |
| 1427 | |
Peng Tao | aa8a45e | 2014-12-01 08:22:23 +0800 | [diff] [blame] | 1428 | /* stop waiting if someone clears NFS_LAYOUT_RETRY_LAYOUTGET bit. */ |
| 1429 | static int pnfs_layoutget_retry_bit_wait(struct wait_bit_key *key) |
| 1430 | { |
| 1431 | if (!test_bit(NFS_LAYOUT_RETRY_LAYOUTGET, key->flags)) |
| 1432 | return 1; |
| 1433 | return nfs_wait_bit_killable(key); |
| 1434 | } |
| 1435 | |
| 1436 | static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo) |
| 1437 | { |
| 1438 | /* |
| 1439 | * send layoutcommit as it can hold up layoutreturn due to lseg |
| 1440 | * reference |
| 1441 | */ |
| 1442 | pnfs_layoutcommit_inode(lo->plh_inode, false); |
| 1443 | return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN, |
| 1444 | pnfs_layoutget_retry_bit_wait, |
| 1445 | TASK_UNINTERRUPTIBLE); |
| 1446 | } |
| 1447 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1448 | static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo) |
| 1449 | { |
| 1450 | unsigned long *bitlock = &lo->plh_flags; |
| 1451 | |
| 1452 | clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock); |
| 1453 | smp_mb__after_atomic(); |
| 1454 | wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET); |
| 1455 | } |
| 1456 | |
Andy Adamson | d23d61c | 2012-05-23 05:02:37 -0400 | [diff] [blame] | 1457 | /* |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1458 | * Layout segment is retreived from the server if not cached. |
| 1459 | * The appropriate layout segment is referenced and returned to the caller. |
| 1460 | */ |
Andy Adamson | 7c24d94 | 2011-06-13 18:22:38 -0400 | [diff] [blame] | 1461 | struct pnfs_layout_segment * |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1462 | pnfs_update_layout(struct inode *ino, |
| 1463 | struct nfs_open_context *ctx, |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1464 | loff_t pos, |
| 1465 | u64 count, |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 1466 | enum pnfs_iomode iomode, |
| 1467 | gfp_t gfp_flags) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1468 | { |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1469 | struct pnfs_layout_range arg = { |
| 1470 | .iomode = iomode, |
| 1471 | .offset = pos, |
| 1472 | .length = count, |
| 1473 | }; |
Benny Halevy | 707ed5f | 2011-05-22 19:47:46 +0300 | [diff] [blame] | 1474 | unsigned pg_offset; |
Weston Andros Adamson | 6382a44 | 2011-06-01 16:44:44 -0400 | [diff] [blame] | 1475 | struct nfs_server *server = NFS_SERVER(ino); |
| 1476 | struct nfs_client *clp = server->nfs_client; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1477 | struct pnfs_layout_hdr *lo; |
| 1478 | struct pnfs_layout_segment *lseg = NULL; |
Weston Andros Adamson | 3000512 | 2013-02-28 20:30:10 -0500 | [diff] [blame] | 1479 | bool first; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1480 | |
| 1481 | if (!pnfs_enabled_sb(NFS_SERVER(ino))) |
Trond Myklebust | f86bbcf | 2012-09-26 11:21:40 -0400 | [diff] [blame] | 1482 | goto out; |
Andy Adamson | d23d61c | 2012-05-23 05:02:37 -0400 | [diff] [blame] | 1483 | |
| 1484 | if (pnfs_within_mdsthreshold(ctx, ino, iomode)) |
Trond Myklebust | f86bbcf | 2012-09-26 11:21:40 -0400 | [diff] [blame] | 1485 | goto out; |
Andy Adamson | d23d61c | 2012-05-23 05:02:37 -0400 | [diff] [blame] | 1486 | |
Peng Tao | 9bf8748 | 2014-08-22 17:37:41 +0800 | [diff] [blame] | 1487 | lookup_again: |
| 1488 | first = false; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1489 | spin_lock(&ino->i_lock); |
Peng Tao | 9fa4075 | 2011-07-30 20:52:32 -0400 | [diff] [blame] | 1490 | lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags); |
Trond Myklebust | 830ffb5 | 2012-09-20 21:25:19 -0400 | [diff] [blame] | 1491 | if (lo == NULL) { |
| 1492 | spin_unlock(&ino->i_lock); |
| 1493 | goto out; |
| 1494 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1495 | |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 1496 | /* Do we even need to bother with this? */ |
Trond Myklebust | a59c30a | 2012-03-01 11:17:47 -0500 | [diff] [blame] | 1497 | if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 1498 | dprintk("%s matches recall, use MDS\n", __func__); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1499 | goto out_unlock; |
| 1500 | } |
| 1501 | |
| 1502 | /* if LAYOUTGET already failed once we don't try again */ |
Peng Tao | aa8a45e | 2014-12-01 08:22:23 +0800 | [diff] [blame] | 1503 | if (pnfs_layout_io_test_failed(lo, iomode) && |
| 1504 | !pnfs_should_retry_layoutget(lo)) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1505 | goto out_unlock; |
| 1506 | |
Peng Tao | 9bf8748 | 2014-08-22 17:37:41 +0800 | [diff] [blame] | 1507 | first = list_empty(&lo->plh_segs); |
| 1508 | if (first) { |
| 1509 | /* The first layoutget for the file. Need to serialize per |
| 1510 | * RFC 5661 Errata 3208. |
| 1511 | */ |
| 1512 | if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, |
| 1513 | &lo->plh_flags)) { |
| 1514 | spin_unlock(&ino->i_lock); |
| 1515 | wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET, |
| 1516 | TASK_UNINTERRUPTIBLE); |
| 1517 | pnfs_put_layout_hdr(lo); |
| 1518 | goto lookup_again; |
| 1519 | } |
| 1520 | } else { |
| 1521 | /* Check to see if the layout for the given range |
| 1522 | * already exists |
| 1523 | */ |
| 1524 | lseg = pnfs_find_lseg(lo, &arg); |
| 1525 | if (lseg) |
| 1526 | goto out_unlock; |
| 1527 | } |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 1528 | |
Peng Tao | aa8a45e | 2014-12-01 08:22:23 +0800 | [diff] [blame] | 1529 | /* |
| 1530 | * Because we free lsegs before sending LAYOUTRETURN, we need to wait |
| 1531 | * for LAYOUTRETURN even if first is true. |
| 1532 | */ |
| 1533 | if (!lseg && pnfs_should_retry_layoutget(lo) && |
| 1534 | test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) { |
| 1535 | spin_unlock(&ino->i_lock); |
| 1536 | dprintk("%s wait for layoutreturn\n", __func__); |
| 1537 | if (pnfs_prepare_to_retry_layoutget(lo)) { |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1538 | if (first) |
| 1539 | pnfs_clear_first_layoutget(lo); |
Peng Tao | aa8a45e | 2014-12-01 08:22:23 +0800 | [diff] [blame] | 1540 | pnfs_put_layout_hdr(lo); |
| 1541 | dprintk("%s retrying\n", __func__); |
| 1542 | goto lookup_again; |
| 1543 | } |
| 1544 | goto out_put_layout_hdr; |
| 1545 | } |
| 1546 | |
Peng Tao | ce6ab4f | 2014-09-06 00:53:24 +0800 | [diff] [blame] | 1547 | if (pnfs_layoutgets_blocked(lo, &arg, 0)) |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 1548 | goto out_unlock; |
| 1549 | atomic_inc(&lo->plh_outstanding); |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 1550 | spin_unlock(&ino->i_lock); |
Weston Andros Adamson | 3000512 | 2013-02-28 20:30:10 -0500 | [diff] [blame] | 1551 | |
Peng Tao | abb9a00 | 2014-08-22 17:37:40 +0800 | [diff] [blame] | 1552 | if (list_empty(&lo->plh_layouts)) { |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 1553 | /* The lo must be on the clp list if there is any |
| 1554 | * chance of a CB_LAYOUTRECALL(FILE) coming in. |
| 1555 | */ |
| 1556 | spin_lock(&clp->cl_lock); |
Peng Tao | abb9a00 | 2014-08-22 17:37:40 +0800 | [diff] [blame] | 1557 | if (list_empty(&lo->plh_layouts)) |
| 1558 | list_add_tail(&lo->plh_layouts, &server->layouts); |
Fred Isaman | 2130ff6 | 2011-01-06 11:36:26 +0000 | [diff] [blame] | 1559 | spin_unlock(&clp->cl_lock); |
| 1560 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1561 | |
Benny Halevy | 707ed5f | 2011-05-22 19:47:46 +0300 | [diff] [blame] | 1562 | pg_offset = arg.offset & ~PAGE_CACHE_MASK; |
| 1563 | if (pg_offset) { |
| 1564 | arg.offset -= pg_offset; |
| 1565 | arg.length += pg_offset; |
| 1566 | } |
Andy Adamson | 7c24d94 | 2011-06-13 18:22:38 -0400 | [diff] [blame] | 1567 | if (arg.length != NFS4_MAX_UINT64) |
| 1568 | arg.length = PAGE_CACHE_ALIGN(arg.length); |
Benny Halevy | 707ed5f | 2011-05-22 19:47:46 +0300 | [diff] [blame] | 1569 | |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1570 | lseg = send_layoutget(lo, ctx, &arg, gfp_flags); |
Peng Tao | c829013 | 2014-12-01 08:22:18 +0800 | [diff] [blame] | 1571 | pnfs_clear_retry_layoutget(lo); |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 1572 | atomic_dec(&lo->plh_outstanding); |
Trond Myklebust | 830ffb5 | 2012-09-20 21:25:19 -0400 | [diff] [blame] | 1573 | out_put_layout_hdr: |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1574 | if (first) |
| 1575 | pnfs_clear_first_layoutget(lo); |
Trond Myklebust | 70c3bd2 | 2012-09-18 20:51:13 -0400 | [diff] [blame] | 1576 | pnfs_put_layout_hdr(lo); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1577 | out: |
Trond Myklebust | f86bbcf | 2012-09-26 11:21:40 -0400 | [diff] [blame] | 1578 | dprintk("%s: inode %s/%llu pNFS layout segment %s for " |
| 1579 | "(%s, offset: %llu, length: %llu)\n", |
| 1580 | __func__, ino->i_sb->s_id, |
| 1581 | (unsigned long long)NFS_FILEID(ino), |
| 1582 | lseg == NULL ? "not found" : "found", |
| 1583 | iomode==IOMODE_RW ? "read/write" : "read-only", |
| 1584 | (unsigned long long)pos, |
| 1585 | (unsigned long long)count); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1586 | return lseg; |
| 1587 | out_unlock: |
| 1588 | spin_unlock(&ino->i_lock); |
Trond Myklebust | 830ffb5 | 2012-09-20 21:25:19 -0400 | [diff] [blame] | 1589 | goto out_put_layout_hdr; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1590 | } |
Andy Adamson | 7c24d94 | 2011-06-13 18:22:38 -0400 | [diff] [blame] | 1591 | EXPORT_SYMBOL_GPL(pnfs_update_layout); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1592 | |
Trond Myklebust | a0b0a6e | 2012-09-17 17:12:15 -0400 | [diff] [blame] | 1593 | struct pnfs_layout_segment * |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1594 | pnfs_layout_process(struct nfs4_layoutget *lgp) |
| 1595 | { |
| 1596 | struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout; |
| 1597 | struct nfs4_layoutget_res *res = &lgp->res; |
| 1598 | struct pnfs_layout_segment *lseg; |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 1599 | struct inode *ino = lo->plh_inode; |
Trond Myklebust | 78096cc | 2014-02-12 10:02:27 -0500 | [diff] [blame] | 1600 | LIST_HEAD(free_me); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1601 | int status = 0; |
| 1602 | |
| 1603 | /* Inject layout blob into I/O device driver */ |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 1604 | 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] | 1605 | if (!lseg || IS_ERR(lseg)) { |
| 1606 | if (!lseg) |
| 1607 | status = -ENOMEM; |
| 1608 | else |
| 1609 | status = PTR_ERR(lseg); |
| 1610 | dprintk("%s: Could not allocate layout: error %d\n", |
| 1611 | __func__, status); |
| 1612 | goto out; |
| 1613 | } |
| 1614 | |
Christoph Hellwig | 1013df61 | 2014-08-21 11:09:18 -0500 | [diff] [blame] | 1615 | init_lseg(lo, lseg); |
| 1616 | lseg->pls_range = res->range; |
| 1617 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1618 | spin_lock(&ino->i_lock); |
Trond Myklebust | a59c30a | 2012-03-01 11:17:47 -0500 | [diff] [blame] | 1619 | if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 1620 | dprintk("%s forget reply due to recall\n", __func__); |
| 1621 | goto out_forget_reply; |
| 1622 | } |
| 1623 | |
Peng Tao | ce6ab4f | 2014-09-06 00:53:24 +0800 | [diff] [blame] | 1624 | if (pnfs_layoutgets_blocked(lo, &lgp->args.range, 1)) { |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 1625 | dprintk("%s forget reply due to state\n", __func__); |
| 1626 | goto out_forget_reply; |
| 1627 | } |
Trond Myklebust | 038d649 | 2012-10-02 16:38:41 -0700 | [diff] [blame] | 1628 | |
Christoph Hellwig | 362f747 | 2014-08-21 11:09:20 -0500 | [diff] [blame] | 1629 | if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) { |
| 1630 | /* existing state ID, make sure the sequence number matches. */ |
| 1631 | if (pnfs_layout_stateid_blocked(lo, &res->stateid)) { |
| 1632 | dprintk("%s forget reply due to sequence\n", __func__); |
| 1633 | goto out_forget_reply; |
| 1634 | } |
| 1635 | pnfs_set_layout_stateid(lo, &res->stateid, false); |
| 1636 | } else { |
| 1637 | /* |
| 1638 | * We got an entirely new state ID. Mark all segments for the |
| 1639 | * inode invalid, and don't bother validating the stateid |
| 1640 | * sequence number. |
| 1641 | */ |
| 1642 | pnfs_mark_matching_lsegs_invalid(lo, &free_me, NULL); |
| 1643 | |
| 1644 | nfs4_stateid_copy(&lo->plh_stateid, &res->stateid); |
| 1645 | lo->plh_barrier = be32_to_cpu(res->stateid.seqid); |
| 1646 | } |
Trond Myklebust | 038d649 | 2012-10-02 16:38:41 -0700 | [diff] [blame] | 1647 | |
Christoph Hellwig | 47abade | 2014-08-21 11:09:22 -0500 | [diff] [blame] | 1648 | clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); |
| 1649 | |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 1650 | pnfs_get_lseg(lseg); |
Trond Myklebust | 57036a3 | 2012-09-20 16:33:30 -0400 | [diff] [blame] | 1651 | pnfs_layout_insert_lseg(lo, lseg); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1652 | |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1653 | if (res->return_on_close) { |
| 1654 | set_bit(NFS_LSEG_ROC, &lseg->pls_flags); |
| 1655 | set_bit(NFS_LAYOUT_ROC, &lo->plh_flags); |
| 1656 | } |
| 1657 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1658 | spin_unlock(&ino->i_lock); |
Trond Myklebust | 78096cc | 2014-02-12 10:02:27 -0500 | [diff] [blame] | 1659 | pnfs_free_lseg_list(&free_me); |
Trond Myklebust | a0b0a6e | 2012-09-17 17:12:15 -0400 | [diff] [blame] | 1660 | return lseg; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1661 | out: |
Trond Myklebust | a0b0a6e | 2012-09-17 17:12:15 -0400 | [diff] [blame] | 1662 | return ERR_PTR(status); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 1663 | |
| 1664 | out_forget_reply: |
| 1665 | spin_unlock(&ino->i_lock); |
| 1666 | lseg->pls_layout = lo; |
| 1667 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
| 1668 | goto out; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1669 | } |
| 1670 | |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 1671 | static void |
| 1672 | pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo, |
| 1673 | struct list_head *tmp_list, |
| 1674 | struct pnfs_layout_range *return_range) |
| 1675 | { |
| 1676 | struct pnfs_layout_segment *lseg, *next; |
| 1677 | |
| 1678 | dprintk("%s:Begin lo %p\n", __func__, lo); |
| 1679 | |
| 1680 | if (list_empty(&lo->plh_segs)) |
| 1681 | return; |
| 1682 | |
| 1683 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) |
| 1684 | if (should_free_lseg(&lseg->pls_range, return_range)) { |
| 1685 | dprintk("%s: marking lseg %p iomode %d " |
| 1686 | "offset %llu length %llu\n", __func__, |
| 1687 | lseg, lseg->pls_range.iomode, |
| 1688 | lseg->pls_range.offset, |
| 1689 | lseg->pls_range.length); |
| 1690 | set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags); |
| 1691 | mark_lseg_invalid(lseg, tmp_list); |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | void pnfs_error_mark_layout_for_return(struct inode *inode, |
| 1696 | struct pnfs_layout_segment *lseg) |
| 1697 | { |
| 1698 | struct pnfs_layout_hdr *lo = NFS_I(inode)->layout; |
| 1699 | int iomode = pnfs_iomode_to_fail_bit(lseg->pls_range.iomode); |
| 1700 | struct pnfs_layout_range range = { |
| 1701 | .iomode = lseg->pls_range.iomode, |
| 1702 | .offset = 0, |
| 1703 | .length = NFS4_MAX_UINT64, |
| 1704 | }; |
| 1705 | LIST_HEAD(free_me); |
| 1706 | |
| 1707 | spin_lock(&inode->i_lock); |
| 1708 | /* set failure bit so that pnfs path will be retried later */ |
| 1709 | pnfs_layout_set_fail_bit(lo, iomode); |
| 1710 | set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags); |
| 1711 | if (lo->plh_return_iomode == 0) |
| 1712 | lo->plh_return_iomode = range.iomode; |
| 1713 | else if (lo->plh_return_iomode != range.iomode) |
| 1714 | lo->plh_return_iomode = IOMODE_ANY; |
| 1715 | /* |
| 1716 | * mark all matching lsegs so that we are sure to have no live |
| 1717 | * segments at hand when sending layoutreturn. See pnfs_put_lseg() |
| 1718 | * for how it works. |
| 1719 | */ |
| 1720 | pnfs_mark_matching_lsegs_return(lo, &free_me, &range); |
| 1721 | spin_unlock(&inode->i_lock); |
| 1722 | pnfs_free_lseg_list(&free_me); |
| 1723 | } |
| 1724 | EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return); |
| 1725 | |
Trond Myklebust | d8007d4 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1726 | void |
| 1727 | pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req) |
| 1728 | { |
Peng Tao | 1fd937b | 2012-09-25 14:55:57 +0800 | [diff] [blame] | 1729 | u64 rd_size = req->wb_bytes; |
| 1730 | |
Peng Tao | cb5d04b | 2015-01-24 22:14:52 +0800 | [diff] [blame] | 1731 | if (pgio->pg_lseg == NULL) { |
| 1732 | if (pgio->pg_dreq == NULL) |
| 1733 | rd_size = i_size_read(pgio->pg_inode) - req_offset(req); |
| 1734 | else |
| 1735 | rd_size = nfs_dreq_bytes_left(pgio->pg_dreq); |
Trond Myklebust | d8007d4 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1736 | |
Peng Tao | cb5d04b | 2015-01-24 22:14:52 +0800 | [diff] [blame] | 1737 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
| 1738 | req->wb_context, |
| 1739 | req_offset(req), |
| 1740 | rd_size, |
| 1741 | IOMODE_READ, |
| 1742 | GFP_KERNEL); |
| 1743 | } |
Trond Myklebust | e885de1 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1744 | /* If no lseg, fall back to read through mds */ |
| 1745 | if (pgio->pg_lseg == NULL) |
Trond Myklebust | 1f94535 | 2011-07-13 15:59:57 -0400 | [diff] [blame] | 1746 | nfs_pageio_reset_read_mds(pgio); |
Trond Myklebust | e885de1 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1747 | |
Trond Myklebust | d8007d4 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1748 | } |
| 1749 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read); |
| 1750 | |
| 1751 | void |
Peng Tao | 6296556 | 2012-09-25 14:55:57 +0800 | [diff] [blame] | 1752 | pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, |
| 1753 | struct nfs_page *req, u64 wb_size) |
Trond Myklebust | d8007d4 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1754 | { |
Peng Tao | cb5d04b | 2015-01-24 22:14:52 +0800 | [diff] [blame] | 1755 | if (pgio->pg_lseg == NULL) |
| 1756 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
| 1757 | req->wb_context, |
| 1758 | req_offset(req), |
| 1759 | wb_size, |
| 1760 | IOMODE_RW, |
| 1761 | GFP_NOFS); |
Trond Myklebust | e885de1 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1762 | /* If no lseg, fall back to write through mds */ |
| 1763 | if (pgio->pg_lseg == NULL) |
Trond Myklebust | 1f94535 | 2011-07-13 15:59:57 -0400 | [diff] [blame] | 1764 | nfs_pageio_reset_write_mds(pgio); |
Trond Myklebust | d8007d4 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1765 | } |
| 1766 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write); |
| 1767 | |
Weston Andros Adamson | 180bb5e | 2014-09-10 15:48:01 -0400 | [diff] [blame] | 1768 | void |
| 1769 | pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc) |
| 1770 | { |
| 1771 | if (desc->pg_lseg) { |
| 1772 | pnfs_put_lseg(desc->pg_lseg); |
| 1773 | desc->pg_lseg = NULL; |
| 1774 | } |
| 1775 | } |
| 1776 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup); |
| 1777 | |
Weston Andros Adamson | b4fdac1 | 2014-05-15 11:56:43 -0400 | [diff] [blame] | 1778 | /* |
| 1779 | * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number |
| 1780 | * of bytes (maximum @req->wb_bytes) that can be coalesced. |
| 1781 | */ |
| 1782 | size_t |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 1783 | pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, |
| 1784 | struct nfs_page *prev, struct nfs_page *req) |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 1785 | { |
Weston Andros Adamson | 0f9c429 | 2014-05-15 11:56:51 -0400 | [diff] [blame] | 1786 | unsigned int size; |
Weston Andros Adamson | c5e20cb | 2014-06-09 17:47:26 -0400 | [diff] [blame] | 1787 | u64 seg_end, req_start, seg_left; |
Weston Andros Adamson | 0f9c429 | 2014-05-15 11:56:51 -0400 | [diff] [blame] | 1788 | |
| 1789 | size = nfs_generic_pg_test(pgio, prev, req); |
Weston Andros Adamson | 0f9c429 | 2014-05-15 11:56:51 -0400 | [diff] [blame] | 1790 | if (!size) |
| 1791 | return 0; |
Benny Halevy | 89a58e3 | 2011-05-25 20:54:40 +0300 | [diff] [blame] | 1792 | |
Trond Myklebust | 19982ba | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1793 | /* |
Weston Andros Adamson | c5e20cb | 2014-06-09 17:47:26 -0400 | [diff] [blame] | 1794 | * 'size' contains the number of bytes left in the current page (up |
| 1795 | * to the original size asked for in @req->wb_bytes). |
| 1796 | * |
| 1797 | * Calculate how many bytes are left in the layout segment |
| 1798 | * and if there are less bytes than 'size', return that instead. |
Trond Myklebust | 19982ba | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1799 | * |
| 1800 | * Please also note that 'end_offset' is actually the offset of the |
| 1801 | * first byte that lies outside the pnfs_layout_range. FIXME? |
| 1802 | * |
| 1803 | */ |
Weston Andros Adamson | 19b5484 | 2014-05-15 11:56:55 -0400 | [diff] [blame] | 1804 | if (pgio->pg_lseg) { |
Weston Andros Adamson | c5e20cb | 2014-06-09 17:47:26 -0400 | [diff] [blame] | 1805 | seg_end = end_offset(pgio->pg_lseg->pls_range.offset, |
| 1806 | pgio->pg_lseg->pls_range.length); |
| 1807 | req_start = req_offset(req); |
Weston Andros Adamson | 7c13789 | 2015-01-30 11:01:02 -0500 | [diff] [blame] | 1808 | WARN_ON_ONCE(req_start >= seg_end); |
Weston Andros Adamson | c5e20cb | 2014-06-09 17:47:26 -0400 | [diff] [blame] | 1809 | /* start of request is past the last byte of this segment */ |
Weston Andros Adamson | 7c13789 | 2015-01-30 11:01:02 -0500 | [diff] [blame] | 1810 | if (req_start >= seg_end) { |
| 1811 | /* reference the new lseg */ |
| 1812 | if (pgio->pg_ops->pg_cleanup) |
| 1813 | pgio->pg_ops->pg_cleanup(pgio); |
| 1814 | if (pgio->pg_ops->pg_init) |
| 1815 | pgio->pg_ops->pg_init(pgio, req); |
Weston Andros Adamson | 19b5484 | 2014-05-15 11:56:55 -0400 | [diff] [blame] | 1816 | return 0; |
Weston Andros Adamson | 7c13789 | 2015-01-30 11:01:02 -0500 | [diff] [blame] | 1817 | } |
Weston Andros Adamson | c5e20cb | 2014-06-09 17:47:26 -0400 | [diff] [blame] | 1818 | |
| 1819 | /* adjust 'size' iff there are fewer bytes left in the |
| 1820 | * segment than what nfs_generic_pg_test returned */ |
| 1821 | seg_left = seg_end - req_start; |
| 1822 | if (seg_left < size) |
| 1823 | size = (unsigned int)seg_left; |
Weston Andros Adamson | 19b5484 | 2014-05-15 11:56:55 -0400 | [diff] [blame] | 1824 | } |
Weston Andros Adamson | 0f9c429 | 2014-05-15 11:56:51 -0400 | [diff] [blame] | 1825 | |
Weston Andros Adamson | 19b5484 | 2014-05-15 11:56:55 -0400 | [diff] [blame] | 1826 | return size; |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 1827 | } |
Benny Halevy | 89a58e3 | 2011-05-25 20:54:40 +0300 | [diff] [blame] | 1828 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_test); |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 1829 | |
Weston Andros Adamson | 53113ad | 2014-06-09 11:48:38 -0400 | [diff] [blame] | 1830 | int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr) |
Trond Myklebust | e2fecb2 | 2012-01-06 08:57:46 -0500 | [diff] [blame] | 1831 | { |
| 1832 | struct nfs_pageio_descriptor pgio; |
Trond Myklebust | e2fecb2 | 2012-01-06 08:57:46 -0500 | [diff] [blame] | 1833 | |
| 1834 | /* Resend all requests through the MDS */ |
Weston Andros Adamson | 53113ad | 2014-06-09 11:48:38 -0400 | [diff] [blame] | 1835 | nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true, |
| 1836 | hdr->completion_ops); |
| 1837 | return nfs_pageio_resend(&pgio, hdr); |
Trond Myklebust | e2fecb2 | 2012-01-06 08:57:46 -0500 | [diff] [blame] | 1838 | } |
Andy Adamson | e7dd79af | 2012-04-27 17:53:46 -0400 | [diff] [blame] | 1839 | EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds); |
Trond Myklebust | e2fecb2 | 2012-01-06 08:57:46 -0500 | [diff] [blame] | 1840 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1841 | static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr) |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 1842 | { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1843 | |
| 1844 | dprintk("pnfs write error = %d\n", hdr->pnfs_error); |
| 1845 | if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags & |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 1846 | PNFS_LAYOUTRET_ON_ERROR) { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1847 | pnfs_return_layout(hdr->inode); |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 1848 | } |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1849 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) |
Weston Andros Adamson | 53113ad | 2014-06-09 11:48:38 -0400 | [diff] [blame] | 1850 | hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr); |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 1851 | } |
| 1852 | |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 1853 | /* |
| 1854 | * Called by non rpc-based layout drivers |
| 1855 | */ |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1856 | void pnfs_ld_write_done(struct nfs_pgio_header *hdr) |
Fred Isaman | 94ad1c8 | 2011-03-01 01:34:14 +0000 | [diff] [blame] | 1857 | { |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1858 | trace_nfs4_pnfs_write(hdr, hdr->pnfs_error); |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1859 | if (!hdr->pnfs_error) { |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1860 | pnfs_set_layoutcommit(hdr); |
| 1861 | hdr->mds_ops->rpc_call_done(&hdr->task, hdr); |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 1862 | } else |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1863 | pnfs_ld_handle_write_error(hdr); |
| 1864 | hdr->mds_ops->rpc_release(hdr); |
Fred Isaman | 44b8379 | 2011-03-03 15:13:44 +0000 | [diff] [blame] | 1865 | } |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 1866 | EXPORT_SYMBOL_GPL(pnfs_ld_write_done); |
Fred Isaman | 44b8379 | 2011-03-03 15:13:44 +0000 | [diff] [blame] | 1867 | |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1868 | static void |
| 1869 | pnfs_write_through_mds(struct nfs_pageio_descriptor *desc, |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1870 | struct nfs_pgio_header *hdr) |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1871 | { |
Peng Tao | 48d635f | 2014-11-10 08:35:35 +0800 | [diff] [blame] | 1872 | struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 1873 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1874 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 1875 | list_splice_tail_init(&hdr->pages, &mirror->pg_list); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1876 | nfs_pageio_reset_write_mds(desc); |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 1877 | mirror->pg_recoalesce = 1; |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1878 | } |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1879 | nfs_pgio_data_destroy(hdr); |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | static enum pnfs_try_status |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1883 | pnfs_try_to_write_data(struct nfs_pgio_header *hdr, |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1884 | const struct rpc_call_ops *call_ops, |
| 1885 | struct pnfs_layout_segment *lseg, |
| 1886 | int how) |
Andy Adamson | 0382b74 | 2011-03-03 15:13:45 +0000 | [diff] [blame] | 1887 | { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1888 | struct inode *inode = hdr->inode; |
Andy Adamson | 0382b74 | 2011-03-03 15:13:45 +0000 | [diff] [blame] | 1889 | enum pnfs_try_status trypnfs; |
| 1890 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 1891 | |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1892 | hdr->mds_ops = call_ops; |
Andy Adamson | 0382b74 | 2011-03-03 15:13:45 +0000 | [diff] [blame] | 1893 | |
| 1894 | dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__, |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1895 | inode->i_ino, hdr->args.count, hdr->args.offset, how); |
| 1896 | trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1897 | if (trypnfs != PNFS_NOT_ATTEMPTED) |
Andy Adamson | 0382b74 | 2011-03-03 15:13:45 +0000 | [diff] [blame] | 1898 | nfs_inc_stats(inode, NFSIOS_PNFS_WRITE); |
Andy Adamson | 0382b74 | 2011-03-03 15:13:45 +0000 | [diff] [blame] | 1899 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
| 1900 | return trypnfs; |
| 1901 | } |
| 1902 | |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1903 | static void |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 1904 | pnfs_do_write(struct nfs_pageio_descriptor *desc, |
| 1905 | struct nfs_pgio_header *hdr, int how) |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1906 | { |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1907 | const struct rpc_call_ops *call_ops = desc->pg_rpc_callops; |
| 1908 | struct pnfs_layout_segment *lseg = desc->pg_lseg; |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 1909 | enum pnfs_try_status trypnfs; |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1910 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1911 | trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how); |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 1912 | if (trypnfs == PNFS_NOT_ATTEMPTED) |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1913 | pnfs_write_through_mds(desc, hdr); |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1914 | } |
| 1915 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1916 | static void pnfs_writehdr_free(struct nfs_pgio_header *hdr) |
| 1917 | { |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 1918 | pnfs_put_lseg(hdr->lseg); |
Weston Andros Adamson | 1e7f3a4 | 2014-06-09 11:48:33 -0400 | [diff] [blame] | 1919 | nfs_pgio_header_free(hdr); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1920 | } |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 1921 | EXPORT_SYMBOL_GPL(pnfs_writehdr_free); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1922 | |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1923 | int |
| 1924 | pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) |
| 1925 | { |
Peng Tao | 48d635f | 2014-11-10 08:35:35 +0800 | [diff] [blame] | 1926 | struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 1927 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1928 | struct nfs_pgio_header *hdr; |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1929 | int ret; |
| 1930 | |
Weston Andros Adamson | 1e7f3a4 | 2014-06-09 11:48:33 -0400 | [diff] [blame] | 1931 | hdr = nfs_pgio_header_alloc(desc->pg_rw_ops); |
| 1932 | if (!hdr) { |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 1933 | desc->pg_completion_ops->error_cleanup(&mirror->pg_list); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1934 | return -ENOMEM; |
| 1935 | } |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1936 | nfs_pgheader_init(desc, hdr, pnfs_writehdr_free); |
Weston Andros Adamson | 180bb5e | 2014-09-10 15:48:01 -0400 | [diff] [blame] | 1937 | |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 1938 | hdr->lseg = pnfs_get_lseg(desc->pg_lseg); |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 1939 | ret = nfs_generic_pgio(desc, hdr); |
Weston Andros Adamson | 180bb5e | 2014-09-10 15:48:01 -0400 | [diff] [blame] | 1940 | if (!ret) |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 1941 | pnfs_do_write(desc, hdr, desc->pg_ioflags); |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 1942 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1943 | return ret; |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1944 | } |
| 1945 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages); |
| 1946 | |
Weston Andros Adamson | 53113ad | 2014-06-09 11:48:38 -0400 | [diff] [blame] | 1947 | int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr) |
Trond Myklebust | 62e4a76 | 2011-11-10 14:30:37 -0500 | [diff] [blame] | 1948 | { |
| 1949 | struct nfs_pageio_descriptor pgio; |
| 1950 | |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 1951 | /* Resend all requests through the MDS */ |
Weston Andros Adamson | 53113ad | 2014-06-09 11:48:38 -0400 | [diff] [blame] | 1952 | nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops); |
| 1953 | return nfs_pageio_resend(&pgio, hdr); |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 1954 | } |
Andy Adamson | e7dd79af | 2012-04-27 17:53:46 -0400 | [diff] [blame] | 1955 | EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds); |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 1956 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1957 | static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr) |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 1958 | { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1959 | dprintk("pnfs read error = %d\n", hdr->pnfs_error); |
| 1960 | if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags & |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 1961 | PNFS_LAYOUTRET_ON_ERROR) { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1962 | pnfs_return_layout(hdr->inode); |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 1963 | } |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 1964 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) |
Weston Andros Adamson | 53113ad | 2014-06-09 11:48:38 -0400 | [diff] [blame] | 1965 | hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr); |
Trond Myklebust | 62e4a76 | 2011-11-10 14:30:37 -0500 | [diff] [blame] | 1966 | } |
| 1967 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1968 | /* |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 1969 | * Called by non rpc-based layout drivers |
| 1970 | */ |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1971 | void pnfs_ld_read_done(struct nfs_pgio_header *hdr) |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 1972 | { |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1973 | trace_nfs4_pnfs_read(hdr, hdr->pnfs_error); |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1974 | if (likely(!hdr->pnfs_error)) { |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1975 | __nfs4_read_done_cb(hdr); |
| 1976 | hdr->mds_ops->rpc_call_done(&hdr->task, hdr); |
Trond Myklebust | 62e4a76 | 2011-11-10 14:30:37 -0500 | [diff] [blame] | 1977 | } else |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1978 | pnfs_ld_handle_read_error(hdr); |
| 1979 | hdr->mds_ops->rpc_release(hdr); |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 1980 | } |
| 1981 | EXPORT_SYMBOL_GPL(pnfs_ld_read_done); |
| 1982 | |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 1983 | static void |
| 1984 | pnfs_read_through_mds(struct nfs_pageio_descriptor *desc, |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1985 | struct nfs_pgio_header *hdr) |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 1986 | { |
Peng Tao | 48d635f | 2014-11-10 08:35:35 +0800 | [diff] [blame] | 1987 | struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 1988 | |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 1989 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 1990 | list_splice_tail_init(&hdr->pages, &mirror->pg_list); |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 1991 | nfs_pageio_reset_read_mds(desc); |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 1992 | mirror->pg_recoalesce = 1; |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 1993 | } |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 1994 | nfs_pgio_data_destroy(hdr); |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 1995 | } |
| 1996 | |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 1997 | /* |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 1998 | * Call the appropriate parallel I/O subsystem read function. |
| 1999 | */ |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2000 | static enum pnfs_try_status |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2001 | pnfs_try_to_read_data(struct nfs_pgio_header *hdr, |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2002 | const struct rpc_call_ops *call_ops, |
| 2003 | struct pnfs_layout_segment *lseg) |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 2004 | { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2005 | struct inode *inode = hdr->inode; |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 2006 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 2007 | enum pnfs_try_status trypnfs; |
| 2008 | |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2009 | hdr->mds_ops = call_ops; |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 2010 | |
| 2011 | dprintk("%s: Reading ino:%lu %u@%llu\n", |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2012 | __func__, inode->i_ino, hdr->args.count, hdr->args.offset); |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 2013 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2014 | trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr); |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 2015 | if (trypnfs != PNFS_NOT_ATTEMPTED) |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 2016 | nfs_inc_stats(inode, NFSIOS_PNFS_READ); |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 2017 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
| 2018 | return trypnfs; |
| 2019 | } |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2020 | |
Peng Tao | ceb11e1 | 2014-11-10 08:35:38 +0800 | [diff] [blame] | 2021 | /* Resend all requests through pnfs. */ |
| 2022 | int pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr) |
| 2023 | { |
| 2024 | struct nfs_pageio_descriptor pgio; |
| 2025 | |
| 2026 | nfs_pageio_init_read(&pgio, hdr->inode, false, hdr->completion_ops); |
| 2027 | return nfs_pageio_resend(&pgio, hdr); |
| 2028 | } |
| 2029 | EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs); |
| 2030 | |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2031 | static void |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 2032 | pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr) |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2033 | { |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2034 | const struct rpc_call_ops *call_ops = desc->pg_rpc_callops; |
| 2035 | struct pnfs_layout_segment *lseg = desc->pg_lseg; |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 2036 | enum pnfs_try_status trypnfs; |
Peng Tao | ceb11e1 | 2014-11-10 08:35:38 +0800 | [diff] [blame] | 2037 | int err = 0; |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2038 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2039 | trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg); |
Peng Tao | ceb11e1 | 2014-11-10 08:35:38 +0800 | [diff] [blame] | 2040 | if (trypnfs == PNFS_TRY_AGAIN) |
| 2041 | err = pnfs_read_resend_pnfs(hdr); |
| 2042 | if (trypnfs == PNFS_NOT_ATTEMPTED || err) |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2043 | pnfs_read_through_mds(desc, hdr); |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2044 | } |
| 2045 | |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 2046 | static void pnfs_readhdr_free(struct nfs_pgio_header *hdr) |
| 2047 | { |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 2048 | pnfs_put_lseg(hdr->lseg); |
Weston Andros Adamson | 1e7f3a4 | 2014-06-09 11:48:33 -0400 | [diff] [blame] | 2049 | nfs_pgio_header_free(hdr); |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 2050 | } |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 2051 | EXPORT_SYMBOL_GPL(pnfs_readhdr_free); |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 2052 | |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2053 | int |
| 2054 | pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc) |
| 2055 | { |
Peng Tao | 48d635f | 2014-11-10 08:35:35 +0800 | [diff] [blame] | 2056 | struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 2057 | |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 2058 | struct nfs_pgio_header *hdr; |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2059 | int ret; |
| 2060 | |
Weston Andros Adamson | 1e7f3a4 | 2014-06-09 11:48:33 -0400 | [diff] [blame] | 2061 | hdr = nfs_pgio_header_alloc(desc->pg_rw_ops); |
| 2062 | if (!hdr) { |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 2063 | desc->pg_completion_ops->error_cleanup(&mirror->pg_list); |
Weston Andros Adamson | 180bb5e | 2014-09-10 15:48:01 -0400 | [diff] [blame] | 2064 | return -ENOMEM; |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2065 | } |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 2066 | nfs_pgheader_init(desc, hdr, pnfs_readhdr_free); |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 2067 | hdr->lseg = pnfs_get_lseg(desc->pg_lseg); |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 2068 | ret = nfs_generic_pgio(desc, hdr); |
Weston Andros Adamson | 180bb5e | 2014-09-10 15:48:01 -0400 | [diff] [blame] | 2069 | if (!ret) |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 2070 | pnfs_do_read(desc, hdr); |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 2071 | return ret; |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2072 | } |
| 2073 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages); |
| 2074 | |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 2075 | static void pnfs_clear_layoutcommitting(struct inode *inode) |
| 2076 | { |
| 2077 | unsigned long *bitlock = &NFS_I(inode)->flags; |
| 2078 | |
| 2079 | clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 2080 | smp_mb__after_atomic(); |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 2081 | wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING); |
| 2082 | } |
| 2083 | |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2084 | /* |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 2085 | * There can be multiple RW segments. |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2086 | */ |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 2087 | 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] | 2088 | { |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 2089 | struct pnfs_layout_segment *lseg; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2090 | |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 2091 | list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) { |
| 2092 | if (lseg->pls_range.iomode == IOMODE_RW && |
Trond Myklebust | a073dbf | 2013-03-20 12:34:32 -0400 | [diff] [blame] | 2093 | test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 2094 | list_add(&lseg->pls_lc_list, listp); |
| 2095 | } |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Trond Myklebust | a073dbf | 2013-03-20 12:34:32 -0400 | [diff] [blame] | 2098 | static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp) |
| 2099 | { |
| 2100 | struct pnfs_layout_segment *lseg, *tmp; |
Trond Myklebust | a073dbf | 2013-03-20 12:34:32 -0400 | [diff] [blame] | 2101 | |
| 2102 | /* Matched by references in pnfs_set_layoutcommit */ |
| 2103 | list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) { |
| 2104 | list_del_init(&lseg->pls_lc_list); |
| 2105 | pnfs_put_lseg(lseg); |
| 2106 | } |
| 2107 | |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 2108 | pnfs_clear_layoutcommitting(inode); |
Trond Myklebust | a073dbf | 2013-03-20 12:34:32 -0400 | [diff] [blame] | 2109 | } |
| 2110 | |
Peng Tao | 1b0ae06 | 2011-09-22 21:50:12 -0400 | [diff] [blame] | 2111 | void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg) |
| 2112 | { |
Trond Myklebust | b9e028f | 2012-09-18 16:41:18 -0400 | [diff] [blame] | 2113 | pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode); |
Peng Tao | 1b0ae06 | 2011-09-22 21:50:12 -0400 | [diff] [blame] | 2114 | } |
| 2115 | EXPORT_SYMBOL_GPL(pnfs_set_lo_fail); |
| 2116 | |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2117 | void |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2118 | pnfs_set_layoutcommit(struct nfs_pgio_header *hdr) |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2119 | { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2120 | struct inode *inode = hdr->inode; |
| 2121 | struct nfs_inode *nfsi = NFS_I(inode); |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2122 | loff_t end_pos = hdr->mds_offset + hdr->res.count; |
Weston Andros Adamson | 79a48a1 | 2011-04-13 10:53:51 -0400 | [diff] [blame] | 2123 | bool mark_as_dirty = false; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2124 | |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2125 | spin_lock(&inode->i_lock); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2126 | if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) { |
Weston Andros Adamson | 79a48a1 | 2011-04-13 10:53:51 -0400 | [diff] [blame] | 2127 | mark_as_dirty = true; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2128 | dprintk("%s: Set layoutcommit for inode %lu ", |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2129 | __func__, inode->i_ino); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2130 | } |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2131 | if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) { |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 2132 | /* references matched in nfs4_layoutcommit_release */ |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 2133 | pnfs_get_lseg(hdr->lseg); |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 2134 | } |
Peng Tao | acff5880 | 2011-07-30 20:52:31 -0400 | [diff] [blame] | 2135 | if (end_pos > nfsi->layout->plh_lwb) |
| 2136 | nfsi->layout->plh_lwb = end_pos; |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2137 | spin_unlock(&inode->i_lock); |
Peng Tao | acff5880 | 2011-07-30 20:52:31 -0400 | [diff] [blame] | 2138 | dprintk("%s: lseg %p end_pos %llu\n", |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2139 | __func__, hdr->lseg, nfsi->layout->plh_lwb); |
Weston Andros Adamson | 79a48a1 | 2011-04-13 10:53:51 -0400 | [diff] [blame] | 2140 | |
| 2141 | /* if pnfs_layoutcommit_inode() runs between inode locks, the next one |
| 2142 | * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */ |
| 2143 | if (mark_as_dirty) |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2144 | mark_inode_dirty_sync(inode); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2145 | } |
| 2146 | EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit); |
| 2147 | |
Peng Tao | 378520b | 2014-08-07 10:15:02 +0800 | [diff] [blame] | 2148 | void pnfs_commit_set_layoutcommit(struct nfs_commit_data *data) |
| 2149 | { |
| 2150 | struct inode *inode = data->inode; |
| 2151 | struct nfs_inode *nfsi = NFS_I(inode); |
| 2152 | bool mark_as_dirty = false; |
| 2153 | |
| 2154 | spin_lock(&inode->i_lock); |
| 2155 | if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) { |
| 2156 | mark_as_dirty = true; |
| 2157 | dprintk("%s: Set layoutcommit for inode %lu ", |
| 2158 | __func__, inode->i_ino); |
| 2159 | } |
| 2160 | if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &data->lseg->pls_flags)) { |
| 2161 | /* references matched in nfs4_layoutcommit_release */ |
| 2162 | pnfs_get_lseg(data->lseg); |
| 2163 | } |
| 2164 | if (data->lwb > nfsi->layout->plh_lwb) |
| 2165 | nfsi->layout->plh_lwb = data->lwb; |
| 2166 | spin_unlock(&inode->i_lock); |
| 2167 | dprintk("%s: lseg %p end_pos %llu\n", |
| 2168 | __func__, data->lseg, nfsi->layout->plh_lwb); |
| 2169 | |
| 2170 | /* if pnfs_layoutcommit_inode() runs between inode locks, the next one |
| 2171 | * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */ |
| 2172 | if (mark_as_dirty) |
| 2173 | mark_inode_dirty_sync(inode); |
| 2174 | } |
| 2175 | EXPORT_SYMBOL_GPL(pnfs_commit_set_layoutcommit); |
| 2176 | |
Andy Adamson | db29c08 | 2011-07-30 20:52:38 -0400 | [diff] [blame] | 2177 | void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data) |
| 2178 | { |
| 2179 | struct nfs_server *nfss = NFS_SERVER(data->args.inode); |
| 2180 | |
| 2181 | if (nfss->pnfs_curr_ld->cleanup_layoutcommit) |
| 2182 | nfss->pnfs_curr_ld->cleanup_layoutcommit(data); |
Trond Myklebust | a073dbf | 2013-03-20 12:34:32 -0400 | [diff] [blame] | 2183 | pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list); |
Andy Adamson | db29c08 | 2011-07-30 20:52:38 -0400 | [diff] [blame] | 2184 | } |
| 2185 | |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 2186 | /* |
| 2187 | * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and |
| 2188 | * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough |
| 2189 | * data to disk to allow the server to recover the data if it crashes. |
| 2190 | * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag |
| 2191 | * is off, and a COMMIT is sent to a data server, or |
| 2192 | * if WRITEs to a data server return NFS_DATA_SYNC. |
| 2193 | */ |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2194 | int |
Andy Adamson | ef31153 | 2011-03-12 02:58:10 -0500 | [diff] [blame] | 2195 | pnfs_layoutcommit_inode(struct inode *inode, bool sync) |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2196 | { |
Christoph Hellwig | 5f919c9 | 2014-08-21 11:09:25 -0500 | [diff] [blame] | 2197 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2198 | struct nfs4_layoutcommit_data *data; |
| 2199 | struct nfs_inode *nfsi = NFS_I(inode); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2200 | loff_t end_pos; |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 2201 | int status; |
| 2202 | |
| 2203 | if (!pnfs_layoutcommit_outstanding(inode)) |
| 2204 | return 0; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2205 | |
| 2206 | dprintk("--> %s inode %lu\n", __func__, inode->i_ino); |
| 2207 | |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 2208 | status = -EAGAIN; |
| 2209 | if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) { |
| 2210 | if (!sync) |
| 2211 | goto out; |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 2212 | status = wait_on_bit_lock_action(&nfsi->flags, |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 2213 | NFS_INO_LAYOUTCOMMITTING, |
| 2214 | nfs_wait_bit_killable, |
| 2215 | TASK_KILLABLE); |
| 2216 | if (status) |
| 2217 | goto out; |
| 2218 | } |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 2219 | |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 2220 | status = -ENOMEM; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2221 | /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */ |
| 2222 | data = kzalloc(sizeof(*data), GFP_NOFS); |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 2223 | if (!data) |
| 2224 | goto clear_layoutcommitting; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2225 | |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 2226 | status = 0; |
| 2227 | spin_lock(&inode->i_lock); |
| 2228 | if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) |
| 2229 | goto out_unlock; |
Peng Tao | 92407e7 | 2011-10-23 20:21:17 -0700 | [diff] [blame] | 2230 | |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 2231 | INIT_LIST_HEAD(&data->lseg_list); |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 2232 | pnfs_list_write_lseg(inode, &data->lseg_list); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2233 | |
Peng Tao | acff5880 | 2011-07-30 20:52:31 -0400 | [diff] [blame] | 2234 | end_pos = nfsi->layout->plh_lwb; |
Peng Tao | acff5880 | 2011-07-30 20:52:31 -0400 | [diff] [blame] | 2235 | nfsi->layout->plh_lwb = 0; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2236 | |
Trond Myklebust | f597c53 | 2012-03-04 18:13:56 -0500 | [diff] [blame] | 2237 | nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2238 | spin_unlock(&inode->i_lock); |
| 2239 | |
| 2240 | data->args.inode = inode; |
Peng Tao | 9fa4075 | 2011-07-30 20:52:32 -0400 | [diff] [blame] | 2241 | data->cred = get_rpccred(nfsi->layout->plh_lc_cred); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2242 | nfs_fattr_init(&data->fattr); |
| 2243 | data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask; |
| 2244 | data->res.fattr = &data->fattr; |
| 2245 | data->args.lastbytewritten = end_pos - 1; |
| 2246 | data->res.server = NFS_SERVER(inode); |
| 2247 | |
Christoph Hellwig | 5f919c9 | 2014-08-21 11:09:25 -0500 | [diff] [blame] | 2248 | if (ld->prepare_layoutcommit) { |
| 2249 | status = ld->prepare_layoutcommit(&data->args); |
| 2250 | if (status) { |
| 2251 | spin_lock(&inode->i_lock); |
| 2252 | if (end_pos < nfsi->layout->plh_lwb) |
| 2253 | nfsi->layout->plh_lwb = end_pos; |
| 2254 | spin_unlock(&inode->i_lock); |
| 2255 | put_rpccred(data->cred); |
| 2256 | set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags); |
| 2257 | goto clear_layoutcommitting; |
| 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2262 | status = nfs4_proc_layoutcommit(data, sync); |
| 2263 | out: |
Peng Tao | 92407e7 | 2011-10-23 20:21:17 -0700 | [diff] [blame] | 2264 | if (status) |
| 2265 | mark_inode_dirty_sync(inode); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2266 | dprintk("<-- %s status %d\n", __func__, status); |
| 2267 | return status; |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 2268 | out_unlock: |
| 2269 | spin_unlock(&inode->i_lock); |
Peng Tao | 92407e7 | 2011-10-23 20:21:17 -0700 | [diff] [blame] | 2270 | kfree(data); |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 2271 | clear_layoutcommitting: |
| 2272 | pnfs_clear_layoutcommitting(inode); |
Peng Tao | 92407e7 | 2011-10-23 20:21:17 -0700 | [diff] [blame] | 2273 | goto out; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 2274 | } |
Peng Tao | 72cff44 | 2014-08-07 10:12:38 +0800 | [diff] [blame] | 2275 | EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode); |
Andy Adamson | 82be417 | 2012-05-23 05:02:35 -0400 | [diff] [blame] | 2276 | |
| 2277 | struct nfs4_threshold *pnfs_mdsthreshold_alloc(void) |
| 2278 | { |
| 2279 | struct nfs4_threshold *thp; |
| 2280 | |
| 2281 | thp = kzalloc(sizeof(*thp), GFP_NOFS); |
| 2282 | if (!thp) { |
| 2283 | dprintk("%s mdsthreshold allocation failed\n", __func__); |
| 2284 | return NULL; |
| 2285 | } |
| 2286 | return thp; |
| 2287 | } |