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