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