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