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