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" |
Fred Isaman | 1b146fc | 2016-09-21 15:24:26 -0400 | [diff] [blame] | 40 | #include "nfs4_fs.h" |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 41 | |
| 42 | #define NFSDBG_FACILITY NFSDBG_PNFS |
Trond Myklebust | 25c7533 | 2012-09-18 17:01:12 -0400 | [diff] [blame] | 43 | #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ) |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 44 | |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 45 | /* Locking: |
| 46 | * |
| 47 | * pnfs_spinlock: |
| 48 | * protects pnfs_modules_tbl. |
| 49 | */ |
| 50 | static DEFINE_SPINLOCK(pnfs_spinlock); |
| 51 | |
| 52 | /* |
| 53 | * pnfs_modules_tbl holds all pnfs modules |
| 54 | */ |
| 55 | static LIST_HEAD(pnfs_modules_tbl); |
| 56 | |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 57 | static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo); |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 58 | static void pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo, |
| 59 | struct list_head *free_me, |
| 60 | const struct pnfs_layout_range *range, |
| 61 | u32 seq); |
Trond Myklebust | fe1cf94 | 2016-11-30 12:32:55 -0500 | [diff] [blame] | 62 | static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg, |
| 63 | struct list_head *tmp_list); |
Peng Tao | aa1e0e3a | 2014-09-06 00:53:25 +0800 | [diff] [blame] | 64 | |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 65 | /* Return the registered pnfs layout driver module matching given id */ |
| 66 | static struct pnfs_layoutdriver_type * |
| 67 | find_pnfs_driver_locked(u32 id) |
| 68 | { |
| 69 | struct pnfs_layoutdriver_type *local; |
| 70 | |
| 71 | list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid) |
| 72 | if (local->id == id) |
| 73 | goto out; |
| 74 | local = NULL; |
| 75 | out: |
| 76 | dprintk("%s: Searching for id %u, found %p\n", __func__, id, local); |
| 77 | return local; |
| 78 | } |
| 79 | |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 80 | static struct pnfs_layoutdriver_type * |
| 81 | find_pnfs_driver(u32 id) |
| 82 | { |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 83 | struct pnfs_layoutdriver_type *local; |
| 84 | |
| 85 | spin_lock(&pnfs_spinlock); |
| 86 | local = find_pnfs_driver_locked(id); |
Trond Myklebust | 0a9c63f | 2012-06-15 13:02:58 -0400 | [diff] [blame] | 87 | if (local != NULL && !try_module_get(local->owner)) { |
| 88 | dprintk("%s: Could not grab reference on module\n", __func__); |
| 89 | local = NULL; |
| 90 | } |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 91 | spin_unlock(&pnfs_spinlock); |
| 92 | return local; |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Trond Myklebust | 7c9d845 | 2022-03-28 08:36:34 -0400 | [diff] [blame] | 95 | const struct pnfs_layoutdriver_type *pnfs_find_layoutdriver(u32 id) |
| 96 | { |
| 97 | return find_pnfs_driver(id); |
| 98 | } |
| 99 | |
| 100 | void pnfs_put_layoutdriver(const struct pnfs_layoutdriver_type *ld) |
| 101 | { |
| 102 | if (ld) |
| 103 | module_put(ld->owner); |
| 104 | } |
| 105 | |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 106 | void |
| 107 | unset_pnfs_layoutdriver(struct nfs_server *nfss) |
| 108 | { |
Benny Halevy | 738fd0f3 | 2011-07-30 20:52:36 -0400 | [diff] [blame] | 109 | if (nfss->pnfs_curr_ld) { |
| 110 | if (nfss->pnfs_curr_ld->clear_layoutdriver) |
| 111 | nfss->pnfs_curr_ld->clear_layoutdriver(nfss); |
Trond Myklebust | 2a4c899 | 2012-06-14 13:08:38 -0400 | [diff] [blame] | 112 | /* Decrement the MDS count. Purge the deviceid cache if zero */ |
| 113 | if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count)) |
| 114 | nfs4_deviceid_purge_client(nfss->nfs_client); |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 115 | module_put(nfss->pnfs_curr_ld->owner); |
Benny Halevy | 738fd0f3 | 2011-07-30 20:52:36 -0400 | [diff] [blame] | 116 | } |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 117 | nfss->pnfs_curr_ld = NULL; |
| 118 | } |
| 119 | |
| 120 | /* |
Jeff Layton | ca440c3 | 2016-09-15 14:40:49 -0400 | [diff] [blame] | 121 | * When the server sends a list of layout types, we choose one in the order |
| 122 | * given in the list below. |
| 123 | * |
| 124 | * FIXME: should this list be configurable in some fashion? module param? |
| 125 | * mount option? something else? |
| 126 | */ |
| 127 | static const u32 ld_prefs[] = { |
| 128 | LAYOUT_SCSI, |
| 129 | LAYOUT_BLOCK_VOLUME, |
| 130 | LAYOUT_OSD2_OBJECTS, |
| 131 | LAYOUT_FLEX_FILES, |
| 132 | LAYOUT_NFSV4_1_FILES, |
| 133 | 0 |
| 134 | }; |
| 135 | |
| 136 | static int |
| 137 | ld_cmp(const void *e1, const void *e2) |
| 138 | { |
| 139 | u32 ld1 = *((u32 *)e1); |
| 140 | u32 ld2 = *((u32 *)e2); |
| 141 | int i; |
| 142 | |
| 143 | for (i = 0; ld_prefs[i] != 0; i++) { |
| 144 | if (ld1 == ld_prefs[i]) |
| 145 | return -1; |
| 146 | |
| 147 | if (ld2 == ld_prefs[i]) |
| 148 | return 1; |
| 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /* |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 154 | * Try to set the server's pnfs module to the pnfs layout type specified by id. |
| 155 | * Currently only one pNFS layout driver per filesystem is supported. |
| 156 | * |
Jeff Layton | 3132e49 | 2016-08-10 15:58:24 -0400 | [diff] [blame] | 157 | * @ids array of layout types supported by MDS. |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 158 | */ |
| 159 | void |
Benny Halevy | 738fd0f3 | 2011-07-30 20:52:36 -0400 | [diff] [blame] | 160 | set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh, |
Jeff Layton | ca440c3 | 2016-09-15 14:40:49 -0400 | [diff] [blame] | 161 | struct nfs_fsinfo *fsinfo) |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 162 | { |
| 163 | struct pnfs_layoutdriver_type *ld_type = NULL; |
Jeff Layton | 3132e49 | 2016-08-10 15:58:24 -0400 | [diff] [blame] | 164 | u32 id; |
Jeff Layton | ca440c3 | 2016-09-15 14:40:49 -0400 | [diff] [blame] | 165 | int i; |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 166 | |
Anna Schumaker | 1927471 | 2016-10-26 15:54:31 -0400 | [diff] [blame] | 167 | if (fsinfo->nlayouttypes == 0) |
| 168 | goto out_no_driver; |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 169 | if (!(server->nfs_client->cl_exchange_flags & |
| 170 | (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) { |
Jeff Layton | 3132e49 | 2016-08-10 15:58:24 -0400 | [diff] [blame] | 171 | printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n", |
| 172 | __func__, server->nfs_client->cl_exchange_flags); |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 173 | goto out_no_driver; |
| 174 | } |
Jeff Layton | 3132e49 | 2016-08-10 15:58:24 -0400 | [diff] [blame] | 175 | |
Jeff Layton | ca440c3 | 2016-09-15 14:40:49 -0400 | [diff] [blame] | 176 | sort(fsinfo->layouttype, fsinfo->nlayouttypes, |
| 177 | sizeof(*fsinfo->layouttype), ld_cmp, NULL); |
Jeff Layton | 3132e49 | 2016-08-10 15:58:24 -0400 | [diff] [blame] | 178 | |
Jeff Layton | ca440c3 | 2016-09-15 14:40:49 -0400 | [diff] [blame] | 179 | for (i = 0; i < fsinfo->nlayouttypes; i++) { |
| 180 | id = fsinfo->layouttype[i]; |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 181 | ld_type = find_pnfs_driver(id); |
Jeff Layton | ca440c3 | 2016-09-15 14:40:49 -0400 | [diff] [blame] | 182 | if (!ld_type) { |
| 183 | request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, |
| 184 | id); |
| 185 | ld_type = find_pnfs_driver(id); |
| 186 | } |
| 187 | if (ld_type) |
| 188 | break; |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 189 | } |
Jeff Layton | 3132e49 | 2016-08-10 15:58:24 -0400 | [diff] [blame] | 190 | |
| 191 | if (!ld_type) { |
Jeff Layton | ca440c3 | 2016-09-15 14:40:49 -0400 | [diff] [blame] | 192 | dprintk("%s: No pNFS module found!\n", __func__); |
Jeff Layton | 3132e49 | 2016-08-10 15:58:24 -0400 | [diff] [blame] | 193 | goto out_no_driver; |
| 194 | } |
| 195 | |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 196 | server->pnfs_curr_ld = ld_type; |
Benny Halevy | 738fd0f3 | 2011-07-30 20:52:36 -0400 | [diff] [blame] | 197 | if (ld_type->set_layoutdriver |
| 198 | && ld_type->set_layoutdriver(server, mntfh)) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 199 | printk(KERN_ERR "NFS: %s: Error initializing pNFS layout " |
| 200 | "driver %u.\n", __func__, id); |
Benny Halevy | 738fd0f3 | 2011-07-30 20:52:36 -0400 | [diff] [blame] | 201 | module_put(ld_type->owner); |
| 202 | goto out_no_driver; |
| 203 | } |
Trond Myklebust | 2a4c899 | 2012-06-14 13:08:38 -0400 | [diff] [blame] | 204 | /* Bump the MDS count */ |
| 205 | atomic_inc(&server->nfs_client->cl_mds_count); |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 206 | |
Ricardo Labiaga | 85e174b | 2010-10-20 00:17:58 -0400 | [diff] [blame] | 207 | dprintk("%s: pNFS module for %u set\n", __func__, id); |
| 208 | return; |
| 209 | |
| 210 | out_no_driver: |
| 211 | dprintk("%s: Using NFSv4 I/O\n", __func__); |
| 212 | server->pnfs_curr_ld = NULL; |
| 213 | } |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 214 | |
| 215 | int |
| 216 | pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type) |
| 217 | { |
| 218 | int status = -EINVAL; |
| 219 | struct pnfs_layoutdriver_type *tmp; |
| 220 | |
| 221 | if (ld_type->id == 0) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 222 | printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__); |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 223 | return status; |
| 224 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 225 | if (!ld_type->alloc_lseg || !ld_type->free_lseg) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 226 | printk(KERN_ERR "NFS: %s Layout driver must provide " |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 227 | "alloc_lseg and free_lseg.\n", __func__); |
| 228 | return status; |
| 229 | } |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 230 | |
| 231 | spin_lock(&pnfs_spinlock); |
| 232 | tmp = find_pnfs_driver_locked(ld_type->id); |
| 233 | if (!tmp) { |
| 234 | list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl); |
| 235 | status = 0; |
| 236 | dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id, |
| 237 | ld_type->name); |
| 238 | } else { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 239 | printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n", |
Fred Isaman | 02c35fc | 2010-10-20 00:17:59 -0400 | [diff] [blame] | 240 | __func__, ld_type->id); |
| 241 | } |
| 242 | spin_unlock(&pnfs_spinlock); |
| 243 | |
| 244 | return status; |
| 245 | } |
| 246 | EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver); |
| 247 | |
| 248 | void |
| 249 | pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type) |
| 250 | { |
| 251 | dprintk("%s Deregistering id:%u\n", __func__, ld_type->id); |
| 252 | spin_lock(&pnfs_spinlock); |
| 253 | list_del(&ld_type->pnfs_tblid); |
| 254 | spin_unlock(&pnfs_spinlock); |
| 255 | } |
| 256 | EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 257 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 258 | /* |
| 259 | * pNFS client layout cache |
| 260 | */ |
| 261 | |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 262 | /* Need to hold i_lock if caller does not already hold reference */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 263 | void |
Trond Myklebust | 70c3bd2 | 2012-09-18 20:51:13 -0400 | [diff] [blame] | 264 | pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 265 | { |
Elena Reshetova | 2b28a7b | 2017-10-20 12:53:33 +0300 | [diff] [blame] | 266 | refcount_inc(&lo->plh_refcount); |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Benny Halevy | 636fb9c | 2011-05-22 19:51:33 +0300 | [diff] [blame] | 269 | static struct pnfs_layout_hdr * |
| 270 | pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags) |
| 271 | { |
| 272 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld; |
Trond Myklebust | 5793427 | 2012-09-20 20:37:23 -0400 | [diff] [blame] | 273 | return ld->alloc_layout_hdr(ino, gfp_flags); |
Benny Halevy | 636fb9c | 2011-05-22 19:51:33 +0300 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static void |
| 277 | pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo) |
| 278 | { |
Trond Myklebust | 9c62638 | 2012-09-20 17:31:43 -0400 | [diff] [blame] | 279 | struct nfs_server *server = NFS_SERVER(lo->plh_inode); |
| 280 | struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld; |
| 281 | |
Trond Myklebust | cf6605d | 2020-02-18 17:14:40 -0500 | [diff] [blame] | 282 | if (test_and_clear_bit(NFS_LAYOUT_HASHED, &lo->plh_flags)) { |
Trond Myklebust | 9c62638 | 2012-09-20 17:31:43 -0400 | [diff] [blame] | 283 | struct nfs_client *clp = server->nfs_client; |
| 284 | |
| 285 | spin_lock(&clp->cl_lock); |
Trond Myklebust | cf6605d | 2020-02-18 17:14:40 -0500 | [diff] [blame] | 286 | list_del_rcu(&lo->plh_layouts); |
Trond Myklebust | 9c62638 | 2012-09-20 17:31:43 -0400 | [diff] [blame] | 287 | spin_unlock(&clp->cl_lock); |
| 288 | } |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 289 | put_cred(lo->plh_lc_cred); |
Trond Myklebust | 5793427 | 2012-09-20 20:37:23 -0400 | [diff] [blame] | 290 | return ld->free_layout_hdr(lo); |
Benny Halevy | 636fb9c | 2011-05-22 19:51:33 +0300 | [diff] [blame] | 291 | } |
| 292 | |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 293 | static void |
Trond Myklebust | 6622c3e | 2012-09-20 17:23:11 -0400 | [diff] [blame] | 294 | pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo) |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 295 | { |
Trond Myklebust | bb346f6 | 2012-09-20 15:52:13 -0400 | [diff] [blame] | 296 | struct nfs_inode *nfsi = NFS_I(lo->plh_inode); |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 297 | dprintk("%s: freeing layout cache %p\n", __func__, lo); |
Trond Myklebust | bb346f6 | 2012-09-20 15:52:13 -0400 | [diff] [blame] | 298 | nfsi->layout = NULL; |
| 299 | /* Reset MDS Threshold I/O counters */ |
| 300 | nfsi->write_io = 0; |
| 301 | nfsi->read_io = 0; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 302 | } |
| 303 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 304 | void |
Trond Myklebust | 70c3bd2 | 2012-09-18 20:51:13 -0400 | [diff] [blame] | 305 | pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 306 | { |
Trond Myklebust | 9c6376e | 2018-03-07 14:49:06 -0500 | [diff] [blame] | 307 | struct inode *inode; |
Trond Myklebust | b6d49ec | 2020-11-25 12:06:14 -0500 | [diff] [blame] | 308 | unsigned long i_state; |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 309 | |
Trond Myklebust | 9c6376e | 2018-03-07 14:49:06 -0500 | [diff] [blame] | 310 | if (!lo) |
| 311 | return; |
| 312 | inode = lo->plh_inode; |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 313 | pnfs_layoutreturn_before_put_layout_hdr(lo); |
| 314 | |
Elena Reshetova | 2b28a7b | 2017-10-20 12:53:33 +0300 | [diff] [blame] | 315 | if (refcount_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) { |
Peng Tao | 566f873 | 2014-10-10 23:25:46 +0800 | [diff] [blame] | 316 | if (!list_empty(&lo->plh_segs)) |
| 317 | WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n"); |
Trond Myklebust | 6622c3e | 2012-09-20 17:23:11 -0400 | [diff] [blame] | 318 | pnfs_detach_layout_hdr(lo); |
Trond Myklebust | b6d49ec | 2020-11-25 12:06:14 -0500 | [diff] [blame] | 319 | i_state = inode->i_state; |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 320 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 6622c3e | 2012-09-20 17:23:11 -0400 | [diff] [blame] | 321 | pnfs_free_layout_hdr(lo); |
Trond Myklebust | b6d49ec | 2020-11-25 12:06:14 -0500 | [diff] [blame] | 322 | /* Notify pnfs_destroy_layout_final() that we're done */ |
| 323 | if (i_state & (I_FREEING | I_CLEAR)) |
| 324 | wake_up_var(lo); |
Fred Isaman | cc6e534 | 2011-01-06 11:36:28 +0000 | [diff] [blame] | 325 | } |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Trond Myklebust | b5fdf84 | 2020-02-18 15:58:31 -0500 | [diff] [blame] | 328 | static struct inode * |
| 329 | pnfs_grab_inode_layout_hdr(struct pnfs_layout_hdr *lo) |
| 330 | { |
| 331 | struct inode *inode = igrab(lo->plh_inode); |
| 332 | if (inode) |
| 333 | return inode; |
| 334 | set_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags); |
| 335 | return NULL; |
| 336 | } |
| 337 | |
Trond Myklebust | 1bcf34f | 2021-01-21 13:51:50 -0500 | [diff] [blame] | 338 | /* |
| 339 | * Compare 2 layout stateid sequence ids, to see which is newer, |
| 340 | * taking into account wraparound issues. |
| 341 | */ |
| 342 | static bool pnfs_seqid_is_newer(u32 s1, u32 s2) |
| 343 | { |
| 344 | return (s32)(s1 - s2) > 0; |
| 345 | } |
| 346 | |
| 347 | static void pnfs_barrier_update(struct pnfs_layout_hdr *lo, u32 newseq) |
| 348 | { |
Trond Myklebust | 45baada | 2021-07-23 08:57:20 -0400 | [diff] [blame] | 349 | if (pnfs_seqid_is_newer(newseq, lo->plh_barrier) || !lo->plh_barrier) |
Trond Myklebust | 1bcf34f | 2021-01-21 13:51:50 -0500 | [diff] [blame] | 350 | lo->plh_barrier = newseq; |
| 351 | } |
| 352 | |
Trond Myklebust | ae5a459 | 2016-11-14 14:34:18 -0500 | [diff] [blame] | 353 | static void |
Trond Myklebust | 4aab973 | 2016-11-30 10:47:48 -0500 | [diff] [blame] | 354 | pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode, |
| 355 | u32 seq) |
| 356 | { |
| 357 | if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode) |
| 358 | iomode = IOMODE_ANY; |
| 359 | lo->plh_return_iomode = iomode; |
| 360 | set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags); |
Trond Myklebust | e20772c | 2021-07-26 07:58:49 -0400 | [diff] [blame] | 361 | /* |
| 362 | * We must set lo->plh_return_seq to avoid livelocks with |
| 363 | * pnfs_layout_need_return() |
| 364 | */ |
| 365 | if (seq == 0) |
| 366 | seq = be32_to_cpu(lo->plh_stateid.seqid); |
| 367 | if (!lo->plh_return_seq || pnfs_seqid_is_newer(seq, lo->plh_return_seq)) |
Trond Myklebust | 4aab973 | 2016-11-30 10:47:48 -0500 | [diff] [blame] | 368 | lo->plh_return_seq = seq; |
Trond Myklebust | e20772c | 2021-07-26 07:58:49 -0400 | [diff] [blame] | 369 | pnfs_barrier_update(lo, seq); |
Trond Myklebust | 4aab973 | 2016-11-30 10:47:48 -0500 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static void |
Trond Myklebust | ae5a459 | 2016-11-14 14:34:18 -0500 | [diff] [blame] | 373 | pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo) |
| 374 | { |
Trond Myklebust | 5466d21 | 2017-05-01 17:03:44 -0400 | [diff] [blame] | 375 | struct pnfs_layout_segment *lseg; |
Trond Myklebust | ae5a459 | 2016-11-14 14:34:18 -0500 | [diff] [blame] | 376 | lo->plh_return_iomode = 0; |
| 377 | lo->plh_return_seq = 0; |
| 378 | clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags); |
Trond Myklebust | 5466d21 | 2017-05-01 17:03:44 -0400 | [diff] [blame] | 379 | list_for_each_entry(lseg, &lo->plh_segs, pls_list) { |
| 380 | if (!test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags)) |
| 381 | continue; |
| 382 | pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0); |
| 383 | } |
Trond Myklebust | ae5a459 | 2016-11-14 14:34:18 -0500 | [diff] [blame] | 384 | } |
| 385 | |
Trond Myklebust | 362fb578 | 2016-12-05 17:33:07 -0500 | [diff] [blame] | 386 | static void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo) |
| 387 | { |
| 388 | clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags); |
| 389 | clear_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags); |
| 390 | smp_mb__after_atomic(); |
| 391 | wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN); |
| 392 | rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq); |
| 393 | } |
| 394 | |
Trond Myklebust | fe1cf94 | 2016-11-30 12:32:55 -0500 | [diff] [blame] | 395 | static void |
| 396 | pnfs_clear_lseg_state(struct pnfs_layout_segment *lseg, |
| 397 | struct list_head *free_me) |
| 398 | { |
| 399 | clear_bit(NFS_LSEG_ROC, &lseg->pls_flags); |
| 400 | clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags); |
| 401 | if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) |
| 402 | pnfs_lseg_dec_and_remove_zero(lseg, free_me); |
| 403 | if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) |
| 404 | pnfs_lseg_dec_and_remove_zero(lseg, free_me); |
| 405 | } |
| 406 | |
Trond Myklebust | 2454dfe | 2016-02-22 17:34:59 -0500 | [diff] [blame] | 407 | /* |
Trond Myklebust | 30cb3ee | 2019-09-20 07:23:45 -0400 | [diff] [blame] | 408 | * Update the seqid of a layout stateid after receiving |
| 409 | * NFS4ERR_OLD_STATEID |
Trond Myklebust | 7380020 | 2017-11-06 15:28:07 -0500 | [diff] [blame] | 410 | */ |
Trond Myklebust | 30cb3ee | 2019-09-20 07:23:45 -0400 | [diff] [blame] | 411 | bool nfs4_layout_refresh_old_stateid(nfs4_stateid *dst, |
Trond Myklebust | ecf8402 | 2018-08-15 21:35:46 -0400 | [diff] [blame] | 412 | struct pnfs_layout_range *dst_range, |
| 413 | struct inode *inode) |
Trond Myklebust | 7380020 | 2017-11-06 15:28:07 -0500 | [diff] [blame] | 414 | { |
| 415 | struct pnfs_layout_hdr *lo; |
Trond Myklebust | c16467d | 2018-07-29 22:39:15 -0400 | [diff] [blame] | 416 | struct pnfs_layout_range range = { |
| 417 | .iomode = IOMODE_ANY, |
| 418 | .offset = 0, |
| 419 | .length = NFS4_MAX_UINT64, |
| 420 | }; |
Trond Myklebust | 7380020 | 2017-11-06 15:28:07 -0500 | [diff] [blame] | 421 | bool ret = false; |
Trond Myklebust | c16467d | 2018-07-29 22:39:15 -0400 | [diff] [blame] | 422 | LIST_HEAD(head); |
| 423 | int err; |
Trond Myklebust | 7380020 | 2017-11-06 15:28:07 -0500 | [diff] [blame] | 424 | |
| 425 | spin_lock(&inode->i_lock); |
| 426 | lo = NFS_I(inode)->layout; |
Trond Myklebust | 30cb3ee | 2019-09-20 07:23:45 -0400 | [diff] [blame] | 427 | if (lo && pnfs_layout_is_valid(lo) && |
| 428 | nfs4_stateid_match_other(dst, &lo->plh_stateid)) { |
| 429 | /* Is our call using the most recent seqid? If so, bump it */ |
| 430 | if (!nfs4_stateid_is_newer(&lo->plh_stateid, dst)) { |
| 431 | nfs4_stateid_seqid_inc(dst); |
| 432 | ret = true; |
| 433 | goto out; |
| 434 | } |
| 435 | /* Try to update the seqid to the most recent */ |
Trond Myklebust | c16467d | 2018-07-29 22:39:15 -0400 | [diff] [blame] | 436 | err = pnfs_mark_matching_lsegs_return(lo, &head, &range, 0); |
| 437 | if (err != -EBUSY) { |
| 438 | dst->seqid = lo->plh_stateid.seqid; |
Trond Myklebust | ecf8402 | 2018-08-15 21:35:46 -0400 | [diff] [blame] | 439 | *dst_range = range; |
Trond Myklebust | c16467d | 2018-07-29 22:39:15 -0400 | [diff] [blame] | 440 | ret = true; |
| 441 | } |
Trond Myklebust | 7380020 | 2017-11-06 15:28:07 -0500 | [diff] [blame] | 442 | } |
Trond Myklebust | 30cb3ee | 2019-09-20 07:23:45 -0400 | [diff] [blame] | 443 | out: |
Trond Myklebust | 7380020 | 2017-11-06 15:28:07 -0500 | [diff] [blame] | 444 | spin_unlock(&inode->i_lock); |
Trond Myklebust | c16467d | 2018-07-29 22:39:15 -0400 | [diff] [blame] | 445 | pnfs_free_lseg_list(&head); |
Trond Myklebust | 7380020 | 2017-11-06 15:28:07 -0500 | [diff] [blame] | 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | /* |
Trond Myklebust | 2454dfe | 2016-02-22 17:34:59 -0500 | [diff] [blame] | 450 | * Mark a pnfs_layout_hdr and all associated layout segments as invalid |
| 451 | * |
| 452 | * In order to continue using the pnfs_layout_hdr, a full recovery |
| 453 | * is required. |
| 454 | * Note that caller must hold inode->i_lock. |
| 455 | */ |
Trond Myklebust | 5f46be0 | 2016-07-22 11:25:27 -0400 | [diff] [blame] | 456 | int |
Trond Myklebust | 2454dfe | 2016-02-22 17:34:59 -0500 | [diff] [blame] | 457 | pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo, |
| 458 | struct list_head *lseg_list) |
| 459 | { |
| 460 | struct pnfs_layout_range range = { |
| 461 | .iomode = IOMODE_ANY, |
| 462 | .offset = 0, |
| 463 | .length = NFS4_MAX_UINT64, |
| 464 | }; |
Trond Myklebust | fe1cf94 | 2016-11-30 12:32:55 -0500 | [diff] [blame] | 465 | struct pnfs_layout_segment *lseg, *next; |
Trond Myklebust | 2454dfe | 2016-02-22 17:34:59 -0500 | [diff] [blame] | 466 | |
| 467 | set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); |
Trond Myklebust | fe1cf94 | 2016-11-30 12:32:55 -0500 | [diff] [blame] | 468 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) |
| 469 | pnfs_clear_lseg_state(lseg, lseg_list); |
Trond Myklebust | 5466d21 | 2017-05-01 17:03:44 -0400 | [diff] [blame] | 470 | pnfs_clear_layoutreturn_info(lo); |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 471 | pnfs_free_returned_lsegs(lo, lseg_list, &range, 0); |
Trond Myklebust | 880265c | 2022-05-31 11:03:07 -0400 | [diff] [blame] | 472 | set_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags); |
Trond Myklebust | 362fb578 | 2016-12-05 17:33:07 -0500 | [diff] [blame] | 473 | if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) && |
| 474 | !test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) |
| 475 | pnfs_clear_layoutreturn_waitbit(lo); |
Trond Myklebust | fe1cf94 | 2016-11-30 12:32:55 -0500 | [diff] [blame] | 476 | return !list_empty(&lo->plh_segs); |
Trond Myklebust | 2454dfe | 2016-02-22 17:34:59 -0500 | [diff] [blame] | 477 | } |
| 478 | |
Trond Myklebust | b9e028f | 2012-09-18 16:41:18 -0400 | [diff] [blame] | 479 | static int |
| 480 | pnfs_iomode_to_fail_bit(u32 iomode) |
| 481 | { |
| 482 | return iomode == IOMODE_RW ? |
| 483 | NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED; |
| 484 | } |
| 485 | |
| 486 | static void |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 487 | 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] | 488 | { |
Trond Myklebust | 25c7533 | 2012-09-18 17:01:12 -0400 | [diff] [blame] | 489 | lo->plh_retry_timestamp = jiffies; |
Yanchuan Nian | 39e88fc | 2013-01-04 20:19:49 +0800 | [diff] [blame] | 490 | if (!test_and_set_bit(fail_bit, &lo->plh_flags)) |
Elena Reshetova | 2b28a7b | 2017-10-20 12:53:33 +0300 | [diff] [blame] | 491 | refcount_inc(&lo->plh_refcount); |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static void |
| 495 | pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit) |
| 496 | { |
| 497 | if (test_and_clear_bit(fail_bit, &lo->plh_flags)) |
Elena Reshetova | 2b28a7b | 2017-10-20 12:53:33 +0300 | [diff] [blame] | 498 | refcount_dec(&lo->plh_refcount); |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static void |
| 502 | pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode) |
| 503 | { |
| 504 | struct inode *inode = lo->plh_inode; |
Trond Myklebust | 115ce57 | 2012-09-20 21:19:43 -0400 | [diff] [blame] | 505 | struct pnfs_layout_range range = { |
| 506 | .iomode = iomode, |
| 507 | .offset = 0, |
| 508 | .length = NFS4_MAX_UINT64, |
| 509 | }; |
| 510 | LIST_HEAD(head); |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 511 | |
| 512 | spin_lock(&inode->i_lock); |
| 513 | pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); |
Olga Kornievskaia | 28d4411 | 2023-01-20 13:40:20 -0500 | [diff] [blame] | 514 | pnfs_mark_matching_lsegs_return(lo, &head, &range, 0); |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 515 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 115ce57 | 2012-09-20 21:19:43 -0400 | [diff] [blame] | 516 | pnfs_free_lseg_list(&head); |
Trond Myklebust | b9e028f | 2012-09-18 16:41:18 -0400 | [diff] [blame] | 517 | dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__, |
| 518 | iomode == IOMODE_RW ? "RW" : "READ"); |
| 519 | } |
| 520 | |
| 521 | static bool |
| 522 | pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode) |
| 523 | { |
Trond Myklebust | 25c7533 | 2012-09-18 17:01:12 -0400 | [diff] [blame] | 524 | unsigned long start, end; |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 525 | int fail_bit = pnfs_iomode_to_fail_bit(iomode); |
| 526 | |
| 527 | if (test_bit(fail_bit, &lo->plh_flags) == 0) |
Trond Myklebust | 25c7533 | 2012-09-18 17:01:12 -0400 | [diff] [blame] | 528 | return false; |
| 529 | end = jiffies; |
| 530 | start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT; |
| 531 | if (!time_in_range(lo->plh_retry_timestamp, start, end)) { |
| 532 | /* It is time to retry the failed layoutgets */ |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 533 | pnfs_layout_clear_fail_bit(lo, fail_bit); |
Trond Myklebust | 25c7533 | 2012-09-18 17:01:12 -0400 | [diff] [blame] | 534 | return false; |
| 535 | } |
| 536 | return true; |
Trond Myklebust | b9e028f | 2012-09-18 16:41:18 -0400 | [diff] [blame] | 537 | } |
| 538 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 539 | static void |
Trond Myklebust | 119cef97 | 2016-07-24 15:10:12 -0400 | [diff] [blame] | 540 | pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg, |
| 541 | const struct pnfs_layout_range *range, |
| 542 | const nfs4_stateid *stateid) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 543 | { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 544 | INIT_LIST_HEAD(&lseg->pls_list); |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 545 | INIT_LIST_HEAD(&lseg->pls_lc_list); |
Trond Myklebust | a990189 | 2020-03-20 16:04:06 -0400 | [diff] [blame] | 546 | INIT_LIST_HEAD(&lseg->pls_commits); |
Elena Reshetova | eba6dd6 | 2017-10-20 12:53:32 +0300 | [diff] [blame] | 547 | refcount_set(&lseg->pls_refcount, 1); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 548 | set_bit(NFS_LSEG_VALID, &lseg->pls_flags); |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 549 | lseg->pls_layout = lo; |
Trond Myklebust | 119cef97 | 2016-07-24 15:10:12 -0400 | [diff] [blame] | 550 | lseg->pls_range = *range; |
| 551 | lseg->pls_seq = be32_to_cpu(stateid->seqid); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 552 | } |
| 553 | |
Trond Myklebust | 905ca19 | 2012-09-20 20:46:49 -0400 | [diff] [blame] | 554 | static void pnfs_free_lseg(struct pnfs_layout_segment *lseg) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 555 | { |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 556 | if (lseg != NULL) { |
| 557 | struct inode *inode = lseg->pls_layout->plh_inode; |
| 558 | NFS_SERVER(inode)->pnfs_curr_ld->free_lseg(lseg); |
| 559 | } |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 562 | static void |
Trond Myklebust | 57036a3 | 2012-09-20 16:33:30 -0400 | [diff] [blame] | 563 | pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo, |
| 564 | struct pnfs_layout_segment *lseg) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 565 | { |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 566 | WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 567 | list_del_init(&lseg->pls_list); |
Trond Myklebust | 8f0d27d | 2012-09-20 20:57:11 -0400 | [diff] [blame] | 568 | /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */ |
Elena Reshetova | 2b28a7b | 2017-10-20 12:53:33 +0300 | [diff] [blame] | 569 | refcount_dec(&lo->plh_refcount); |
Trond Myklebust | abb3e1c | 2016-11-30 11:38:10 -0500 | [diff] [blame] | 570 | if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags)) |
| 571 | return; |
Trond Myklebust | 7b65099 | 2016-11-14 13:10:48 -0500 | [diff] [blame] | 572 | if (list_empty(&lo->plh_segs) && |
| 573 | !test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) && |
| 574 | !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) { |
Trond Myklebust | 334a8f3 | 2016-09-04 12:46:35 -0400 | [diff] [blame] | 575 | if (atomic_read(&lo->plh_outstanding) == 0) |
| 576 | set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); |
Trond Myklebust | 173f77e9c | 2012-09-21 15:49:42 -0400 | [diff] [blame] | 577 | clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); |
Trond Myklebust | 2d148c7 | 2016-06-17 16:48:26 -0400 | [diff] [blame] | 578 | } |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 581 | static bool |
| 582 | pnfs_cache_lseg_for_layoutreturn(struct pnfs_layout_hdr *lo, |
| 583 | struct pnfs_layout_segment *lseg) |
| 584 | { |
| 585 | if (test_and_clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) && |
| 586 | pnfs_layout_is_valid(lo)) { |
Trond Myklebust | 4aab973 | 2016-11-30 10:47:48 -0500 | [diff] [blame] | 587 | pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0); |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 588 | list_move_tail(&lseg->pls_list, &lo->plh_return_segs); |
| 589 | return true; |
| 590 | } |
| 591 | return false; |
| 592 | } |
| 593 | |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 594 | void |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 595 | pnfs_put_lseg(struct pnfs_layout_segment *lseg) |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 596 | { |
Trond Myklebust | 57036a3 | 2012-09-20 16:33:30 -0400 | [diff] [blame] | 597 | struct pnfs_layout_hdr *lo; |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 598 | struct inode *inode; |
| 599 | |
| 600 | if (!lseg) |
| 601 | return; |
| 602 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 603 | dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg, |
Elena Reshetova | eba6dd6 | 2017-10-20 12:53:32 +0300 | [diff] [blame] | 604 | refcount_read(&lseg->pls_refcount), |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 605 | test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
Trond Myklebust | 4ef2e4f | 2015-02-05 17:27:39 -0500 | [diff] [blame] | 606 | |
Trond Myklebust | 57036a3 | 2012-09-20 16:33:30 -0400 | [diff] [blame] | 607 | lo = lseg->pls_layout; |
| 608 | inode = lo->plh_inode; |
Trond Myklebust | 4ef2e4f | 2015-02-05 17:27:39 -0500 | [diff] [blame] | 609 | |
Elena Reshetova | eba6dd6 | 2017-10-20 12:53:32 +0300 | [diff] [blame] | 610 | if (refcount_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) { |
Trond Myklebust | 8f0d27d | 2012-09-20 20:57:11 -0400 | [diff] [blame] | 611 | pnfs_get_layout_hdr(lo); |
Trond Myklebust | 4ef2e4f | 2015-02-05 17:27:39 -0500 | [diff] [blame] | 612 | pnfs_layout_remove_lseg(lo, lseg); |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 613 | if (pnfs_cache_lseg_for_layoutreturn(lo, lseg)) |
| 614 | lseg = NULL; |
Trond Myklebust | 4ef2e4f | 2015-02-05 17:27:39 -0500 | [diff] [blame] | 615 | spin_unlock(&inode->i_lock); |
| 616 | pnfs_free_lseg(lseg); |
| 617 | pnfs_put_layout_hdr(lo); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 618 | } |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 619 | } |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 620 | EXPORT_SYMBOL_GPL(pnfs_put_lseg); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 621 | |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 622 | /* |
| 623 | * is l2 fully contained in l1? |
| 624 | * start1 end1 |
| 625 | * [----------------------------------) |
| 626 | * start2 end2 |
| 627 | * [----------------) |
| 628 | */ |
Trond Myklebust | 3cb2df1 | 2013-06-03 11:24:36 -0400 | [diff] [blame] | 629 | static bool |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 630 | pnfs_lseg_range_contained(const struct pnfs_layout_range *l1, |
Trond Myklebust | 3cb2df1 | 2013-06-03 11:24:36 -0400 | [diff] [blame] | 631 | const struct pnfs_layout_range *l2) |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 632 | { |
| 633 | u64 start1 = l1->offset; |
Trond Myklebust | 17822b2 | 2016-10-25 12:24:25 -0400 | [diff] [blame] | 634 | u64 end1 = pnfs_end_offset(start1, l1->length); |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 635 | u64 start2 = l2->offset; |
Trond Myklebust | 17822b2 | 2016-10-25 12:24:25 -0400 | [diff] [blame] | 636 | u64 end2 = pnfs_end_offset(start2, l2->length); |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 637 | |
| 638 | return (start1 <= start2) && (end1 >= end2); |
| 639 | } |
| 640 | |
Trond Myklebust | 2495680 | 2013-03-20 13:03:00 -0400 | [diff] [blame] | 641 | static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg, |
| 642 | struct list_head *tmp_list) |
| 643 | { |
Elena Reshetova | eba6dd6 | 2017-10-20 12:53:32 +0300 | [diff] [blame] | 644 | if (!refcount_dec_and_test(&lseg->pls_refcount)) |
Trond Myklebust | 2495680 | 2013-03-20 13:03:00 -0400 | [diff] [blame] | 645 | return false; |
| 646 | pnfs_layout_remove_lseg(lseg->pls_layout, lseg); |
| 647 | list_add(&lseg->pls_list, tmp_list); |
| 648 | return true; |
| 649 | } |
| 650 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 651 | /* Returns 1 if lseg is removed from list, 0 otherwise */ |
| 652 | static int mark_lseg_invalid(struct pnfs_layout_segment *lseg, |
| 653 | struct list_head *tmp_list) |
| 654 | { |
| 655 | int rv = 0; |
| 656 | |
| 657 | if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) { |
| 658 | /* Remove the reference keeping the lseg in the |
| 659 | * list. It will now be removed when all |
| 660 | * outstanding io is finished. |
| 661 | */ |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 662 | dprintk("%s: lseg %p ref %d\n", __func__, lseg, |
Elena Reshetova | eba6dd6 | 2017-10-20 12:53:32 +0300 | [diff] [blame] | 663 | refcount_read(&lseg->pls_refcount)); |
Trond Myklebust | 2495680 | 2013-03-20 13:03:00 -0400 | [diff] [blame] | 664 | if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list)) |
Fred Isaman | d684d2a | 2011-03-01 01:34:13 +0000 | [diff] [blame] | 665 | rv = 1; |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 666 | } |
| 667 | return rv; |
| 668 | } |
| 669 | |
Trond Myklebust | e036f46 | 2016-07-22 11:13:22 -0400 | [diff] [blame] | 670 | static bool |
| 671 | pnfs_should_free_range(const struct pnfs_layout_range *lseg_range, |
| 672 | const struct pnfs_layout_range *recall_range) |
| 673 | { |
| 674 | return (recall_range->iomode == IOMODE_ANY || |
| 675 | lseg_range->iomode == recall_range->iomode) && |
| 676 | pnfs_lseg_range_intersecting(lseg_range, recall_range); |
| 677 | } |
| 678 | |
| 679 | static bool |
| 680 | pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg, |
| 681 | const struct pnfs_layout_range *recall_range, |
| 682 | u32 seq) |
| 683 | { |
| 684 | if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq)) |
| 685 | return false; |
| 686 | if (recall_range == NULL) |
| 687 | return true; |
| 688 | return pnfs_should_free_range(&lseg->pls_range, recall_range); |
| 689 | } |
| 690 | |
Jeff Layton | 6d597e1 | 2016-05-17 12:28:42 -0400 | [diff] [blame] | 691 | /** |
| 692 | * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later |
| 693 | * @lo: layout header containing the lsegs |
| 694 | * @tmp_list: list head where doomed lsegs should go |
| 695 | * @recall_range: optional recall range argument to match (may be NULL) |
| 696 | * @seq: only invalidate lsegs obtained prior to this sequence (may be 0) |
| 697 | * |
| 698 | * Walk the list of lsegs in the layout header, and tear down any that should |
| 699 | * be destroyed. If "recall_range" is specified then the segment must match |
| 700 | * that range. If "seq" is non-zero, then only match segments that were handed |
| 701 | * out at or before that sequence. |
| 702 | * |
| 703 | * Returns number of matching invalid lsegs remaining in list after scanning |
| 704 | * it and purging them. |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 705 | */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 706 | int |
Trond Myklebust | 49a8506 | 2012-09-18 20:43:31 -0400 | [diff] [blame] | 707 | pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo, |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 708 | struct list_head *tmp_list, |
Jeff Layton | 6d597e1 | 2016-05-17 12:28:42 -0400 | [diff] [blame] | 709 | const struct pnfs_layout_range *recall_range, |
| 710 | u32 seq) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 711 | { |
| 712 | struct pnfs_layout_segment *lseg, *next; |
Trond Myklebust | b739a5b | 2022-10-05 15:57:38 -0400 | [diff] [blame] | 713 | struct nfs_server *server = NFS_SERVER(lo->plh_inode); |
Trond Myklebust | 71b3985 | 2016-01-04 12:41:15 -0500 | [diff] [blame] | 714 | int remaining = 0; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 715 | |
| 716 | dprintk("%s:Begin lo %p\n", __func__, lo); |
| 717 | |
Trond Myklebust | 8006bfb | 2012-09-21 14:48:04 -0400 | [diff] [blame] | 718 | if (list_empty(&lo->plh_segs)) |
Fred Isaman | 38511722 | 2011-02-03 18:28:50 +0000 | [diff] [blame] | 719 | return 0; |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 720 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) |
Trond Myklebust | e036f46 | 2016-07-22 11:13:22 -0400 | [diff] [blame] | 721 | if (pnfs_match_lseg_recall(lseg, recall_range, seq)) { |
Benjamin Coddington | b3dce6a | 2017-12-08 12:52:59 -0500 | [diff] [blame] | 722 | dprintk("%s: freeing lseg %p iomode %d seq %u " |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 723 | "offset %llu length %llu\n", __func__, |
Jeff Layton | 6d597e1 | 2016-05-17 12:28:42 -0400 | [diff] [blame] | 724 | lseg, lseg->pls_range.iomode, lseg->pls_seq, |
| 725 | lseg->pls_range.offset, lseg->pls_range.length); |
Trond Myklebust | b739a5b | 2022-10-05 15:57:38 -0400 | [diff] [blame] | 726 | if (mark_lseg_invalid(lseg, tmp_list)) |
| 727 | continue; |
| 728 | remaining++; |
| 729 | pnfs_lseg_cancel_io(server, lseg); |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 730 | } |
Trond Myklebust | 71b3985 | 2016-01-04 12:41:15 -0500 | [diff] [blame] | 731 | dprintk("%s:Return %i\n", __func__, remaining); |
| 732 | return remaining; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 733 | } |
| 734 | |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 735 | static void |
| 736 | pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo, |
| 737 | struct list_head *free_me, |
| 738 | const struct pnfs_layout_range *range, |
| 739 | u32 seq) |
| 740 | { |
| 741 | struct pnfs_layout_segment *lseg, *next; |
| 742 | |
| 743 | list_for_each_entry_safe(lseg, next, &lo->plh_return_segs, pls_list) { |
| 744 | if (pnfs_match_lseg_recall(lseg, range, seq)) |
| 745 | list_move_tail(&lseg->pls_list, free_me); |
| 746 | } |
| 747 | } |
| 748 | |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 749 | /* note free_me must contain lsegs from a single layout_hdr */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 750 | void |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 751 | pnfs_free_lseg_list(struct list_head *free_me) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 752 | { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 753 | struct pnfs_layout_segment *lseg, *tmp; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 754 | |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 755 | if (list_empty(free_me)) |
| 756 | return; |
| 757 | |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 758 | list_for_each_entry_safe(lseg, tmp, free_me, pls_list) { |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 759 | list_del(&lseg->pls_list); |
Trond Myklebust | 905ca19 | 2012-09-20 20:46:49 -0400 | [diff] [blame] | 760 | pnfs_free_lseg(lseg); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
Trond Myklebust | b6d49ec | 2020-11-25 12:06:14 -0500 | [diff] [blame] | 764 | static struct pnfs_layout_hdr *__pnfs_destroy_layout(struct nfs_inode *nfsi) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 765 | { |
| 766 | struct pnfs_layout_hdr *lo; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 767 | LIST_HEAD(tmp_list); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 768 | |
| 769 | spin_lock(&nfsi->vfs_inode.i_lock); |
| 770 | lo = nfsi->layout; |
| 771 | if (lo) { |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 772 | pnfs_get_layout_hdr(lo); |
Trond Myklebust | 2454dfe | 2016-02-22 17:34:59 -0500 | [diff] [blame] | 773 | pnfs_mark_layout_stateid_invalid(lo, &tmp_list); |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 774 | pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED); |
| 775 | pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED); |
| 776 | spin_unlock(&nfsi->vfs_inode.i_lock); |
| 777 | pnfs_free_lseg_list(&tmp_list); |
Trond Myklebust | 1f18b82 | 2017-04-29 10:10:17 -0400 | [diff] [blame] | 778 | nfs_commit_inode(&nfsi->vfs_inode, 0); |
Trond Myklebust | 3e62121 | 2012-09-24 13:07:16 -0400 | [diff] [blame] | 779 | pnfs_put_layout_hdr(lo); |
| 780 | } else |
| 781 | spin_unlock(&nfsi->vfs_inode.i_lock); |
Trond Myklebust | b6d49ec | 2020-11-25 12:06:14 -0500 | [diff] [blame] | 782 | return lo; |
| 783 | } |
| 784 | |
| 785 | void pnfs_destroy_layout(struct nfs_inode *nfsi) |
| 786 | { |
| 787 | __pnfs_destroy_layout(nfsi); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 788 | } |
Andy Adamson | 041245c | 2012-04-27 17:53:53 -0400 | [diff] [blame] | 789 | EXPORT_SYMBOL_GPL(pnfs_destroy_layout); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 790 | |
Trond Myklebust | b6d49ec | 2020-11-25 12:06:14 -0500 | [diff] [blame] | 791 | static bool pnfs_layout_removed(struct nfs_inode *nfsi, |
| 792 | struct pnfs_layout_hdr *lo) |
| 793 | { |
| 794 | bool ret; |
| 795 | |
| 796 | spin_lock(&nfsi->vfs_inode.i_lock); |
| 797 | ret = nfsi->layout != lo; |
| 798 | spin_unlock(&nfsi->vfs_inode.i_lock); |
| 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | void pnfs_destroy_layout_final(struct nfs_inode *nfsi) |
| 803 | { |
| 804 | struct pnfs_layout_hdr *lo = __pnfs_destroy_layout(nfsi); |
| 805 | |
| 806 | if (lo) |
| 807 | wait_var_event(lo, pnfs_layout_removed(nfsi, lo)); |
| 808 | } |
| 809 | |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 810 | static bool |
| 811 | pnfs_layout_add_bulk_destroy_list(struct inode *inode, |
| 812 | struct list_head *layout_list) |
| 813 | { |
| 814 | struct pnfs_layout_hdr *lo; |
| 815 | bool ret = false; |
| 816 | |
| 817 | spin_lock(&inode->i_lock); |
| 818 | lo = NFS_I(inode)->layout; |
| 819 | if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) { |
| 820 | pnfs_get_layout_hdr(lo); |
| 821 | list_add(&lo->plh_bulk_destroy, layout_list); |
| 822 | ret = true; |
| 823 | } |
| 824 | spin_unlock(&inode->i_lock); |
| 825 | return ret; |
| 826 | } |
| 827 | |
| 828 | /* Caller must hold rcu_read_lock and clp->cl_lock */ |
| 829 | static int |
| 830 | pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp, |
| 831 | struct nfs_server *server, |
| 832 | struct list_head *layout_list) |
Trond Myklebust | 5085607 | 2019-02-22 14:20:27 -0500 | [diff] [blame] | 833 | __must_hold(&clp->cl_lock) |
| 834 | __must_hold(RCU) |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 835 | { |
| 836 | struct pnfs_layout_hdr *lo, *next; |
| 837 | struct inode *inode; |
| 838 | |
| 839 | list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) { |
Trond Myklebust | 5085607 | 2019-02-22 14:20:27 -0500 | [diff] [blame] | 840 | if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) || |
| 841 | test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) || |
| 842 | !list_empty(&lo->plh_bulk_destroy)) |
Trond Myklebust | b85f562 | 2016-11-30 18:00:07 -0500 | [diff] [blame] | 843 | continue; |
Trond Myklebust | 5085607 | 2019-02-22 14:20:27 -0500 | [diff] [blame] | 844 | /* If the sb is being destroyed, just bail */ |
| 845 | if (!nfs_sb_active(server->super)) |
| 846 | break; |
Trond Myklebust | b5fdf84 | 2020-02-18 15:58:31 -0500 | [diff] [blame] | 847 | inode = pnfs_grab_inode_layout_hdr(lo); |
Trond Myklebust | 5085607 | 2019-02-22 14:20:27 -0500 | [diff] [blame] | 848 | if (inode != NULL) { |
Trond Myklebust | cf6605d | 2020-02-18 17:14:40 -0500 | [diff] [blame] | 849 | if (test_and_clear_bit(NFS_LAYOUT_HASHED, &lo->plh_flags)) |
| 850 | list_del_rcu(&lo->plh_layouts); |
Trond Myklebust | 5085607 | 2019-02-22 14:20:27 -0500 | [diff] [blame] | 851 | if (pnfs_layout_add_bulk_destroy_list(inode, |
| 852 | layout_list)) |
| 853 | continue; |
| 854 | rcu_read_unlock(); |
| 855 | spin_unlock(&clp->cl_lock); |
| 856 | iput(inode); |
| 857 | } else { |
| 858 | rcu_read_unlock(); |
| 859 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 5085607 | 2019-02-22 14:20:27 -0500 | [diff] [blame] | 860 | } |
| 861 | nfs_sb_deactive(server->super); |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 862 | spin_lock(&clp->cl_lock); |
| 863 | rcu_read_lock(); |
| 864 | return -EAGAIN; |
| 865 | } |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | static int |
| 870 | pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list, |
| 871 | bool is_bulk_recall) |
| 872 | { |
| 873 | struct pnfs_layout_hdr *lo; |
| 874 | struct inode *inode; |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 875 | LIST_HEAD(lseg_list); |
| 876 | int ret = 0; |
| 877 | |
| 878 | while (!list_empty(layout_list)) { |
| 879 | lo = list_entry(layout_list->next, struct pnfs_layout_hdr, |
| 880 | plh_bulk_destroy); |
| 881 | dprintk("%s freeing layout for inode %lu\n", __func__, |
| 882 | lo->plh_inode->i_ino); |
| 883 | inode = lo->plh_inode; |
Christoph Hellwig | 7c5d187 | 2014-09-10 08:23:29 -0700 | [diff] [blame] | 884 | |
| 885 | pnfs_layoutcommit_inode(inode, false); |
| 886 | |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 887 | spin_lock(&inode->i_lock); |
| 888 | list_del_init(&lo->plh_bulk_destroy); |
Trond Myklebust | 9fd4b9f | 2016-02-22 17:46:34 -0500 | [diff] [blame] | 889 | if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) { |
| 890 | if (is_bulk_recall) |
| 891 | set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 892 | ret = -EAGAIN; |
Trond Myklebust | 9fd4b9f | 2016-02-22 17:46:34 -0500 | [diff] [blame] | 893 | } |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 894 | spin_unlock(&inode->i_lock); |
| 895 | pnfs_free_lseg_list(&lseg_list); |
Trond Myklebust | b20135d | 2015-12-31 09:28:06 -0500 | [diff] [blame] | 896 | /* Free all lsegs that are attached to commit buckets */ |
| 897 | nfs_commit_inode(inode, 0); |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 898 | pnfs_put_layout_hdr(lo); |
Trond Myklebust | 5085607 | 2019-02-22 14:20:27 -0500 | [diff] [blame] | 899 | nfs_iput_and_deactive(inode); |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 900 | } |
| 901 | return ret; |
| 902 | } |
| 903 | |
| 904 | int |
| 905 | pnfs_destroy_layouts_byfsid(struct nfs_client *clp, |
| 906 | struct nfs_fsid *fsid, |
| 907 | bool is_recall) |
| 908 | { |
| 909 | struct nfs_server *server; |
| 910 | LIST_HEAD(layout_list); |
| 911 | |
| 912 | spin_lock(&clp->cl_lock); |
| 913 | rcu_read_lock(); |
| 914 | restart: |
| 915 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { |
| 916 | if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0) |
| 917 | continue; |
| 918 | if (pnfs_layout_bulk_destroy_byserver_locked(clp, |
| 919 | server, |
| 920 | &layout_list) != 0) |
| 921 | goto restart; |
| 922 | } |
| 923 | rcu_read_unlock(); |
| 924 | spin_unlock(&clp->cl_lock); |
| 925 | |
| 926 | if (list_empty(&layout_list)) |
| 927 | return 0; |
| 928 | return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall); |
| 929 | } |
| 930 | |
| 931 | int |
| 932 | pnfs_destroy_layouts_byclid(struct nfs_client *clp, |
| 933 | bool is_recall) |
| 934 | { |
| 935 | struct nfs_server *server; |
| 936 | LIST_HEAD(layout_list); |
| 937 | |
| 938 | spin_lock(&clp->cl_lock); |
| 939 | rcu_read_lock(); |
| 940 | restart: |
| 941 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { |
| 942 | if (pnfs_layout_bulk_destroy_byserver_locked(clp, |
| 943 | server, |
| 944 | &layout_list) != 0) |
| 945 | goto restart; |
| 946 | } |
| 947 | rcu_read_unlock(); |
| 948 | spin_unlock(&clp->cl_lock); |
| 949 | |
| 950 | if (list_empty(&layout_list)) |
| 951 | return 0; |
| 952 | return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall); |
| 953 | } |
| 954 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 955 | /* |
Wang Qing | 9f26645 | 2020-09-17 10:19:00 +0800 | [diff] [blame] | 956 | * Called by the state manager to remove all layouts established under an |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 957 | * expired lease. |
| 958 | */ |
| 959 | void |
| 960 | pnfs_destroy_all_layouts(struct nfs_client *clp) |
| 961 | { |
Andy Adamson | c47abcf | 2011-06-15 17:52:40 -0400 | [diff] [blame] | 962 | nfs4_deviceid_mark_client_invalid(clp); |
| 963 | nfs4_deviceid_purge_client(clp); |
| 964 | |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 965 | pnfs_destroy_layouts_byclid(clp, false); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 966 | } |
| 967 | |
Trond Myklebust | 59b5639 | 2020-01-27 13:07:26 -0500 | [diff] [blame] | 968 | static void |
| 969 | pnfs_set_layout_cred(struct pnfs_layout_hdr *lo, const struct cred *cred) |
| 970 | { |
| 971 | const struct cred *old; |
| 972 | |
| 973 | if (cred && cred_fscmp(lo->plh_lc_cred, cred) != 0) { |
| 974 | old = xchg(&lo->plh_lc_cred, get_cred(cred)); |
| 975 | put_cred(old); |
| 976 | } |
| 977 | } |
| 978 | |
Fred Isaman | fd6002e | 2011-01-06 11:36:22 +0000 | [diff] [blame] | 979 | /* update lo->plh_stateid with new if is more recent */ |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 980 | void |
| 981 | pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new, |
Trond Myklebust | 59b5639 | 2020-01-27 13:07:26 -0500 | [diff] [blame] | 982 | const struct cred *cred, bool update_barrier) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 983 | { |
Trond Myklebust | aa95edf | 2021-07-02 16:37:15 -0400 | [diff] [blame] | 984 | u32 oldseq = be32_to_cpu(lo->plh_stateid.seqid); |
| 985 | u32 newseq = be32_to_cpu(new->seqid); |
Trond Myklebust | 2a59a04 | 2016-09-03 11:20:04 -0400 | [diff] [blame] | 986 | |
| 987 | if (!pnfs_layout_is_valid(lo)) { |
Trond Myklebust | 59b5639 | 2020-01-27 13:07:26 -0500 | [diff] [blame] | 988 | pnfs_set_layout_cred(lo, cred); |
Trond Myklebust | 2a59a04 | 2016-09-03 11:20:04 -0400 | [diff] [blame] | 989 | nfs4_stateid_copy(&lo->plh_stateid, new); |
| 990 | lo->plh_barrier = newseq; |
| 991 | pnfs_clear_layoutreturn_info(lo); |
| 992 | clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); |
| 993 | return; |
| 994 | } |
Trond Myklebust | aa95edf | 2021-07-02 16:37:15 -0400 | [diff] [blame] | 995 | |
| 996 | if (pnfs_seqid_is_newer(newseq, oldseq)) |
Trond Myklebust | f597c53 | 2012-03-04 18:13:56 -0500 | [diff] [blame] | 997 | nfs4_stateid_copy(&lo->plh_stateid, new); |
Trond Myklebust | aa95edf | 2021-07-02 16:37:15 -0400 | [diff] [blame] | 998 | |
| 999 | if (update_barrier) { |
| 1000 | pnfs_barrier_update(lo, newseq); |
Trond Myklebust | ecebb80 | 2016-07-24 11:46:06 -0400 | [diff] [blame] | 1001 | return; |
Trond Myklebust | aa95edf | 2021-07-02 16:37:15 -0400 | [diff] [blame] | 1002 | } |
| 1003 | /* |
| 1004 | * Because of wraparound, we want to keep the barrier |
| 1005 | * "close" to the current seqids. We really only want to |
| 1006 | * get here from a layoutget call. |
| 1007 | */ |
| 1008 | if (atomic_read(&lo->plh_outstanding) == 1) |
| 1009 | pnfs_barrier_update(lo, be32_to_cpu(lo->plh_stateid.seqid)); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1010 | } |
| 1011 | |
Trond Myklebust | 19c54ab | 2012-10-05 16:56:58 -0700 | [diff] [blame] | 1012 | static bool |
| 1013 | pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo, |
| 1014 | const nfs4_stateid *stateid) |
| 1015 | { |
| 1016 | u32 seqid = be32_to_cpu(stateid->seqid); |
| 1017 | |
Trond Myklebust | d6236a9 | 2021-07-23 08:57:21 -0400 | [diff] [blame] | 1018 | return lo->plh_barrier && pnfs_seqid_is_newer(lo->plh_barrier, seqid); |
Trond Myklebust | 19c54ab | 2012-10-05 16:56:58 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 1021 | /* lget is set to 1 if called from inside send_layoutget call chain */ |
| 1022 | static bool |
Trond Myklebust | e1c06f8 | 2015-08-04 16:40:08 -0400 | [diff] [blame] | 1023 | pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo) |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 1024 | { |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1025 | return lo->plh_block_lgets || |
Trond Myklebust | e1c06f8 | 2015-08-04 16:40:08 -0400 | [diff] [blame] | 1026 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Fred Isaman | 5e36e2a | 2016-10-06 12:08:51 -0400 | [diff] [blame] | 1029 | static struct nfs_server * |
| 1030 | pnfs_find_server(struct inode *inode, struct nfs_open_context *ctx) |
| 1031 | { |
| 1032 | struct nfs_server *server; |
| 1033 | |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 1034 | if (inode) { |
Fred Isaman | 5e36e2a | 2016-10-06 12:08:51 -0400 | [diff] [blame] | 1035 | server = NFS_SERVER(inode); |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 1036 | } else { |
Fred Isaman | 5e36e2a | 2016-10-06 12:08:51 -0400 | [diff] [blame] | 1037 | struct dentry *parent_dir = dget_parent(ctx->dentry); |
| 1038 | server = NFS_SERVER(parent_dir->d_inode); |
| 1039 | dput(parent_dir); |
| 1040 | } |
| 1041 | return server; |
| 1042 | } |
| 1043 | |
Trond Myklebust | 29a8bfe | 2018-05-30 17:16:20 -0400 | [diff] [blame] | 1044 | static void nfs4_free_pages(struct page **pages, size_t size) |
| 1045 | { |
| 1046 | int i; |
| 1047 | |
| 1048 | if (!pages) |
| 1049 | return; |
| 1050 | |
| 1051 | for (i = 0; i < size; i++) { |
| 1052 | if (!pages[i]) |
| 1053 | break; |
| 1054 | __free_page(pages[i]); |
| 1055 | } |
| 1056 | kfree(pages); |
| 1057 | } |
| 1058 | |
| 1059 | static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags) |
| 1060 | { |
| 1061 | struct page **pages; |
| 1062 | int i; |
| 1063 | |
Trond Myklebust | a2791d3 | 2018-09-03 12:57:52 -0400 | [diff] [blame] | 1064 | pages = kmalloc_array(size, sizeof(struct page *), gfp_flags); |
Trond Myklebust | 29a8bfe | 2018-05-30 17:16:20 -0400 | [diff] [blame] | 1065 | if (!pages) { |
| 1066 | dprintk("%s: can't alloc array of %zu pages\n", __func__, size); |
| 1067 | return NULL; |
| 1068 | } |
| 1069 | |
| 1070 | for (i = 0; i < size; i++) { |
| 1071 | pages[i] = alloc_page(gfp_flags); |
| 1072 | if (!pages[i]) { |
| 1073 | dprintk("%s: failed to allocate page\n", __func__); |
Trond Myklebust | a2791d3 | 2018-09-03 12:57:52 -0400 | [diff] [blame] | 1074 | nfs4_free_pages(pages, i); |
Trond Myklebust | 29a8bfe | 2018-05-30 17:16:20 -0400 | [diff] [blame] | 1075 | return NULL; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | return pages; |
| 1080 | } |
| 1081 | |
Fred Isaman | 587f03d | 2016-09-21 05:14:28 -0400 | [diff] [blame] | 1082 | static struct nfs4_layoutget * |
Fred Isaman | 5e36e2a | 2016-10-06 12:08:51 -0400 | [diff] [blame] | 1083 | pnfs_alloc_init_layoutget_args(struct inode *ino, |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1084 | struct nfs_open_context *ctx, |
Fred Isaman | 2409a97 | 2016-10-06 12:11:21 -0400 | [diff] [blame] | 1085 | const nfs4_stateid *stateid, |
Trond Myklebust | e144e53 | 2016-01-04 12:52:53 -0500 | [diff] [blame] | 1086 | const struct pnfs_layout_range *range, |
Fred Isaman | 587f03d | 2016-09-21 05:14:28 -0400 | [diff] [blame] | 1087 | gfp_t gfp_flags) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1088 | { |
Fred Isaman | 5e36e2a | 2016-10-06 12:08:51 -0400 | [diff] [blame] | 1089 | struct nfs_server *server = pnfs_find_server(ino, ctx); |
Trond Myklebust | 28ced9a | 2018-09-03 13:12:15 -0400 | [diff] [blame] | 1090 | size_t max_reply_sz = server->pnfs_curr_ld->max_layoutget_response; |
Fred Isaman | dacb452 | 2016-09-19 17:47:09 -0400 | [diff] [blame] | 1091 | size_t max_pages = max_response_pages(server); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1092 | struct nfs4_layoutget *lgp; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1093 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1094 | dprintk("--> %s\n", __func__); |
| 1095 | |
Jeff Layton | 83026d80 | 2016-05-17 12:28:46 -0400 | [diff] [blame] | 1096 | lgp = kzalloc(sizeof(*lgp), gfp_flags); |
| 1097 | if (lgp == NULL) |
Fred Isaman | 587f03d | 2016-09-21 05:14:28 -0400 | [diff] [blame] | 1098 | return NULL; |
Jeff Layton | 4f2e9dc | 2015-11-25 13:43:14 -0500 | [diff] [blame] | 1099 | |
Trond Myklebust | 28ced9a | 2018-09-03 13:12:15 -0400 | [diff] [blame] | 1100 | if (max_reply_sz) { |
| 1101 | size_t npages = (max_reply_sz + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1102 | if (npages < max_pages) |
| 1103 | max_pages = npages; |
| 1104 | } |
| 1105 | |
Fred Isaman | dacb452 | 2016-09-19 17:47:09 -0400 | [diff] [blame] | 1106 | lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags); |
| 1107 | if (!lgp->args.layout.pages) { |
| 1108 | kfree(lgp); |
| 1109 | return NULL; |
| 1110 | } |
| 1111 | lgp->args.layout.pglen = max_pages * PAGE_SIZE; |
| 1112 | lgp->res.layoutp = &lgp->args.layout; |
| 1113 | |
Trond Myklebust | d49e0d5 | 2017-02-01 20:42:44 -0500 | [diff] [blame] | 1114 | /* Don't confuse uninitialised result and success */ |
| 1115 | lgp->res.status = -NFS4ERR_DELAY; |
Jeff Layton | 4f2e9dc | 2015-11-25 13:43:14 -0500 | [diff] [blame] | 1116 | |
Jeff Layton | 83026d80 | 2016-05-17 12:28:46 -0400 | [diff] [blame] | 1117 | lgp->args.minlength = PAGE_SIZE; |
| 1118 | if (lgp->args.minlength > range->length) |
| 1119 | lgp->args.minlength = range->length; |
Fred Isaman | 5e36e2a | 2016-10-06 12:08:51 -0400 | [diff] [blame] | 1120 | if (ino) { |
| 1121 | loff_t i_size = i_size_read(ino); |
| 1122 | |
| 1123 | if (range->iomode == IOMODE_READ) { |
| 1124 | if (range->offset >= i_size) |
| 1125 | lgp->args.minlength = 0; |
| 1126 | else if (i_size - range->offset < lgp->args.minlength) |
| 1127 | lgp->args.minlength = i_size - range->offset; |
| 1128 | } |
Jeff Layton | d03ab29 | 2016-05-17 12:28:45 -0400 | [diff] [blame] | 1129 | } |
Jeff Layton | 83026d80 | 2016-05-17 12:28:46 -0400 | [diff] [blame] | 1130 | lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE; |
| 1131 | pnfs_copy_range(&lgp->args.range, range); |
| 1132 | lgp->args.type = server->pnfs_curr_ld->id; |
| 1133 | lgp->args.inode = ino; |
| 1134 | lgp->args.ctx = get_nfs_open_context(ctx); |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 1135 | nfs4_stateid_copy(&lgp->args.stateid, stateid); |
Jeff Layton | 83026d80 | 2016-05-17 12:28:46 -0400 | [diff] [blame] | 1136 | lgp->gfp_flags = gfp_flags; |
Trond Myklebust | 63ec2b6 | 2020-02-07 19:40:14 -0500 | [diff] [blame] | 1137 | lgp->cred = ctx->cred; |
Fred Isaman | 587f03d | 2016-09-21 05:14:28 -0400 | [diff] [blame] | 1138 | return lgp; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 1139 | } |
| 1140 | |
Trond Myklebust | 29a8bfe | 2018-05-30 17:16:20 -0400 | [diff] [blame] | 1141 | void pnfs_layoutget_free(struct nfs4_layoutget *lgp) |
| 1142 | { |
| 1143 | size_t max_pages = lgp->args.layout.pglen / PAGE_SIZE; |
| 1144 | |
| 1145 | nfs4_free_pages(lgp->args.layout.pages, max_pages); |
Trond Myklebust | b4e89bc | 2021-07-02 17:24:22 -0400 | [diff] [blame] | 1146 | pnfs_put_layout_hdr(lgp->lo); |
Trond Myklebust | 29a8bfe | 2018-05-30 17:16:20 -0400 | [diff] [blame] | 1147 | put_nfs_open_context(lgp->args.ctx); |
| 1148 | kfree(lgp); |
| 1149 | } |
| 1150 | |
Trond Myklebust | 2495680 | 2013-03-20 13:03:00 -0400 | [diff] [blame] | 1151 | static void pnfs_clear_layoutcommit(struct inode *inode, |
| 1152 | struct list_head *head) |
| 1153 | { |
| 1154 | struct nfs_inode *nfsi = NFS_I(inode); |
| 1155 | struct pnfs_layout_segment *lseg, *tmp; |
| 1156 | |
| 1157 | if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) |
| 1158 | return; |
| 1159 | list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) { |
| 1160 | if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) |
| 1161 | continue; |
| 1162 | pnfs_lseg_dec_and_remove_zero(lseg, head); |
| 1163 | } |
| 1164 | } |
| 1165 | |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 1166 | void pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr *lo, |
Trond Myklebust | 2a97442 | 2016-11-20 13:13:54 -0500 | [diff] [blame] | 1167 | const nfs4_stateid *arg_stateid, |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 1168 | const struct pnfs_layout_range *range, |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 1169 | const nfs4_stateid *stateid) |
| 1170 | { |
| 1171 | struct inode *inode = lo->plh_inode; |
| 1172 | LIST_HEAD(freeme); |
| 1173 | |
| 1174 | spin_lock(&inode->i_lock); |
Trond Myklebust | c18d1e1 | 2021-01-04 15:01:18 -0500 | [diff] [blame] | 1175 | if (!pnfs_layout_is_valid(lo) || |
Trond Myklebust | 2a97442 | 2016-11-20 13:13:54 -0500 | [diff] [blame] | 1176 | !nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid)) |
| 1177 | goto out_unlock; |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 1178 | if (stateid) { |
Trond Myklebust | 2a97442 | 2016-11-20 13:13:54 -0500 | [diff] [blame] | 1179 | u32 seq = be32_to_cpu(arg_stateid->seqid); |
| 1180 | |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 1181 | pnfs_mark_matching_lsegs_invalid(lo, &freeme, range, seq); |
| 1182 | pnfs_free_returned_lsegs(lo, &freeme, range, seq); |
Trond Myklebust | 59b5639 | 2020-01-27 13:07:26 -0500 | [diff] [blame] | 1183 | pnfs_set_layout_stateid(lo, stateid, NULL, true); |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 1184 | } else |
| 1185 | pnfs_mark_layout_stateid_invalid(lo, &freeme); |
Trond Myklebust | 2a97442 | 2016-11-20 13:13:54 -0500 | [diff] [blame] | 1186 | out_unlock: |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 1187 | pnfs_clear_layoutreturn_waitbit(lo); |
| 1188 | spin_unlock(&inode->i_lock); |
| 1189 | pnfs_free_lseg_list(&freeme); |
| 1190 | |
| 1191 | } |
| 1192 | |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 1193 | static bool |
Trond Myklebust | e5fd190 | 2016-07-21 12:44:15 -0400 | [diff] [blame] | 1194 | pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo, |
| 1195 | nfs4_stateid *stateid, |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1196 | const struct cred **cred, |
Trond Myklebust | e5fd190 | 2016-07-21 12:44:15 -0400 | [diff] [blame] | 1197 | enum pnfs_iomode *iomode) |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 1198 | { |
Trond Myklebust | bf0291d | 2016-09-03 10:39:51 -0400 | [diff] [blame] | 1199 | /* Serialise LAYOUTGET/LAYOUTRETURN */ |
| 1200 | if (atomic_read(&lo->plh_outstanding) != 0) |
| 1201 | return false; |
Trond Myklebust | 6604b20 | 2016-10-17 17:54:32 -0400 | [diff] [blame] | 1202 | if (test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 1203 | return false; |
Trond Myklebust | 6604b20 | 2016-10-17 17:54:32 -0400 | [diff] [blame] | 1204 | set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags); |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 1205 | pnfs_get_layout_hdr(lo); |
Trond Myklebust | 1bcf34f | 2021-01-21 13:51:50 -0500 | [diff] [blame] | 1206 | nfs4_stateid_copy(stateid, &lo->plh_stateid); |
| 1207 | *cred = get_cred(lo->plh_lc_cred); |
Trond Myklebust | e5fd190 | 2016-07-21 12:44:15 -0400 | [diff] [blame] | 1208 | if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) { |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1209 | if (lo->plh_return_seq != 0) |
| 1210 | stateid->seqid = cpu_to_be32(lo->plh_return_seq); |
Trond Myklebust | e5fd190 | 2016-07-21 12:44:15 -0400 | [diff] [blame] | 1211 | if (iomode != NULL) |
| 1212 | *iomode = lo->plh_return_iomode; |
| 1213 | pnfs_clear_layoutreturn_info(lo); |
Trond Myklebust | 1bcf34f | 2021-01-21 13:51:50 -0500 | [diff] [blame] | 1214 | } else if (iomode != NULL) |
Trond Myklebust | e5fd190 | 2016-07-21 12:44:15 -0400 | [diff] [blame] | 1215 | *iomode = IOMODE_ANY; |
Trond Myklebust | 1bcf34f | 2021-01-21 13:51:50 -0500 | [diff] [blame] | 1216 | pnfs_barrier_update(lo, be32_to_cpu(stateid->seqid)); |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 1217 | return true; |
| 1218 | } |
| 1219 | |
Trond Myklebust | 828ed9e | 2016-11-15 21:47:27 -0500 | [diff] [blame] | 1220 | static void |
| 1221 | pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args *args, |
| 1222 | struct pnfs_layout_hdr *lo, |
| 1223 | const nfs4_stateid *stateid, |
| 1224 | enum pnfs_iomode iomode) |
| 1225 | { |
| 1226 | struct inode *inode = lo->plh_inode; |
| 1227 | |
| 1228 | args->layout_type = NFS_SERVER(inode)->pnfs_curr_ld->id; |
| 1229 | args->inode = inode; |
| 1230 | args->range.iomode = iomode; |
| 1231 | args->range.offset = 0; |
| 1232 | args->range.length = NFS4_MAX_UINT64; |
| 1233 | args->layout = lo; |
| 1234 | nfs4_stateid_copy(&args->stateid, stateid); |
| 1235 | } |
| 1236 | |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 1237 | static int |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1238 | pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, |
| 1239 | const nfs4_stateid *stateid, |
| 1240 | const struct cred **pcred, |
| 1241 | enum pnfs_iomode iomode, |
| 1242 | bool sync) |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 1243 | { |
| 1244 | struct inode *ino = lo->plh_inode; |
Trond Myklebust | 287bd3e | 2016-12-02 16:12:12 -0500 | [diff] [blame] | 1245 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld; |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 1246 | struct nfs4_layoutreturn *lrp; |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1247 | const struct cred *cred = *pcred; |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 1248 | int status = 0; |
| 1249 | |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1250 | *pcred = NULL; |
Trond Myklebust | 63d8a41 | 2022-03-21 15:32:09 -0400 | [diff] [blame] | 1251 | lrp = kzalloc(sizeof(*lrp), nfs_io_gfp_mask()); |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 1252 | if (unlikely(lrp == NULL)) { |
| 1253 | status = -ENOMEM; |
| 1254 | spin_lock(&ino->i_lock); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1255 | pnfs_clear_layoutreturn_waitbit(lo); |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 1256 | spin_unlock(&ino->i_lock); |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1257 | put_cred(cred); |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 1258 | pnfs_put_layout_hdr(lo); |
| 1259 | goto out; |
| 1260 | } |
| 1261 | |
Trond Myklebust | 828ed9e | 2016-11-15 21:47:27 -0500 | [diff] [blame] | 1262 | pnfs_init_layoutreturn_args(&lrp->args, lo, stateid, iomode); |
Trond Myklebust | 4d796d7 | 2016-09-23 11:38:08 -0400 | [diff] [blame] | 1263 | lrp->args.ld_private = &lrp->ld_private; |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 1264 | lrp->clp = NFS_SERVER(ino)->nfs_client; |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1265 | lrp->cred = cred; |
Trond Myklebust | 287bd3e | 2016-12-02 16:12:12 -0500 | [diff] [blame] | 1266 | if (ld->prepare_layoutreturn) |
| 1267 | ld->prepare_layoutreturn(&lrp->args); |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 1268 | |
Peng Tao | 6c16605 | 2014-11-17 09:30:40 +0800 | [diff] [blame] | 1269 | status = nfs4_proc_layoutreturn(lrp, sync); |
Peng Tao | f40eb5d | 2014-09-06 00:53:22 +0800 | [diff] [blame] | 1270 | out: |
| 1271 | dprintk("<-- %s status: %d\n", __func__, status); |
| 1272 | return status; |
| 1273 | } |
| 1274 | |
Trond Myklebust | d474f96 | 2020-08-05 09:03:56 -0400 | [diff] [blame] | 1275 | static bool |
| 1276 | pnfs_layout_segments_returnable(struct pnfs_layout_hdr *lo, |
| 1277 | enum pnfs_iomode iomode, |
| 1278 | u32 seq) |
| 1279 | { |
| 1280 | struct pnfs_layout_range recall_range = { |
| 1281 | .length = NFS4_MAX_UINT64, |
| 1282 | .iomode = iomode, |
| 1283 | }; |
| 1284 | return pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, |
| 1285 | &recall_range, seq) != -EBUSY; |
| 1286 | } |
| 1287 | |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 1288 | /* Return true if layoutreturn is needed */ |
| 1289 | static bool |
| 1290 | pnfs_layout_need_return(struct pnfs_layout_hdr *lo) |
| 1291 | { |
Trond Myklebust | 2370abd | 2016-01-27 20:32:50 -0500 | [diff] [blame] | 1292 | if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 1293 | return false; |
Trond Myklebust | d474f96 | 2020-08-05 09:03:56 -0400 | [diff] [blame] | 1294 | return pnfs_layout_segments_returnable(lo, lo->plh_return_iomode, |
| 1295 | lo->plh_return_seq); |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo) |
| 1299 | { |
| 1300 | struct inode *inode= lo->plh_inode; |
| 1301 | |
Trond Myklebust | 2370abd | 2016-01-27 20:32:50 -0500 | [diff] [blame] | 1302 | if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 1303 | return; |
| 1304 | spin_lock(&inode->i_lock); |
| 1305 | if (pnfs_layout_need_return(lo)) { |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1306 | const struct cred *cred; |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 1307 | nfs4_stateid stateid; |
| 1308 | enum pnfs_iomode iomode; |
| 1309 | bool send; |
| 1310 | |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1311 | send = pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode); |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 1312 | spin_unlock(&inode->i_lock); |
| 1313 | if (send) { |
| 1314 | /* Send an async layoutreturn so we dont deadlock */ |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1315 | pnfs_send_layoutreturn(lo, &stateid, &cred, iomode, false); |
Trond Myklebust | 13c13a6 | 2016-01-26 23:12:11 -0500 | [diff] [blame] | 1316 | } |
| 1317 | } else |
| 1318 | spin_unlock(&inode->i_lock); |
| 1319 | } |
| 1320 | |
Andy Adamson | 293b3b0 | 2012-06-20 15:03:34 -0400 | [diff] [blame] | 1321 | /* |
| 1322 | * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr |
| 1323 | * when the layout segment list is empty. |
| 1324 | * |
| 1325 | * Note that a pnfs_layout_hdr can exist with an empty layout segment |
| 1326 | * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the |
| 1327 | * deviceid is marked invalid. |
| 1328 | */ |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1329 | int |
| 1330 | _pnfs_return_layout(struct inode *ino) |
| 1331 | { |
| 1332 | struct pnfs_layout_hdr *lo = NULL; |
| 1333 | struct nfs_inode *nfsi = NFS_I(ino); |
Anna Schumaker | a421d21 | 2021-05-19 12:54:51 -0400 | [diff] [blame] | 1334 | struct pnfs_layout_range range = { |
| 1335 | .iomode = IOMODE_ANY, |
| 1336 | .offset = 0, |
| 1337 | .length = NFS4_MAX_UINT64, |
| 1338 | }; |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1339 | LIST_HEAD(tmp_list); |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1340 | const struct cred *cred; |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1341 | nfs4_stateid stateid; |
Trond Myklebust | 24408f5 | 2016-11-30 10:19:09 -0500 | [diff] [blame] | 1342 | int status = 0; |
Olga Kornievskaia | 93b7f7a | 2018-06-11 15:32:06 -0400 | [diff] [blame] | 1343 | bool send, valid_layout; |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1344 | |
Andy Adamson | 366d505 | 2012-06-20 15:03:33 -0400 | [diff] [blame] | 1345 | dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino); |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1346 | |
| 1347 | spin_lock(&ino->i_lock); |
| 1348 | lo = nfsi->layout; |
Trond Myklebust | e5929f3 | 2012-09-21 16:37:02 -0400 | [diff] [blame] | 1349 | if (!lo) { |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1350 | spin_unlock(&ino->i_lock); |
Andy Adamson | 293b3b0 | 2012-06-20 15:03:34 -0400 | [diff] [blame] | 1351 | dprintk("NFS: %s no layout to return\n", __func__); |
| 1352 | goto out; |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1353 | } |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1354 | /* Reference matched in nfs4_layoutreturn_release */ |
Trond Myklebust | 70c3bd2 | 2012-09-18 20:51:13 -0400 | [diff] [blame] | 1355 | pnfs_get_layout_hdr(lo); |
Trond Myklebust | 24408f5 | 2016-11-30 10:19:09 -0500 | [diff] [blame] | 1356 | /* Is there an outstanding layoutreturn ? */ |
| 1357 | if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) { |
| 1358 | spin_unlock(&ino->i_lock); |
| 1359 | if (wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN, |
| 1360 | TASK_UNINTERRUPTIBLE)) |
| 1361 | goto out_put_layout_hdr; |
| 1362 | spin_lock(&ino->i_lock); |
| 1363 | } |
Olga Kornievskaia | 93b7f7a | 2018-06-11 15:32:06 -0400 | [diff] [blame] | 1364 | valid_layout = pnfs_layout_is_valid(lo); |
Trond Myklebust | 2495680 | 2013-03-20 13:03:00 -0400 | [diff] [blame] | 1365 | pnfs_clear_layoutcommit(ino, &tmp_list); |
Anna Schumaker | a421d21 | 2021-05-19 12:54:51 -0400 | [diff] [blame] | 1366 | pnfs_mark_matching_lsegs_return(lo, &tmp_list, &range, 0); |
Christoph Hellwig | c88953d | 2014-09-10 08:23:31 -0700 | [diff] [blame] | 1367 | |
Anna Schumaker | a421d21 | 2021-05-19 12:54:51 -0400 | [diff] [blame] | 1368 | if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) |
Christoph Hellwig | c88953d | 2014-09-10 08:23:31 -0700 | [diff] [blame] | 1369 | NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range); |
Christoph Hellwig | c88953d | 2014-09-10 08:23:31 -0700 | [diff] [blame] | 1370 | |
Andy Adamson | 293b3b0 | 2012-06-20 15:03:34 -0400 | [diff] [blame] | 1371 | /* Don't send a LAYOUTRETURN if list was initially empty */ |
Olga Kornievskaia | 93b7f7a | 2018-06-11 15:32:06 -0400 | [diff] [blame] | 1372 | if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) || |
| 1373 | !valid_layout) { |
Andy Adamson | 293b3b0 | 2012-06-20 15:03:34 -0400 | [diff] [blame] | 1374 | spin_unlock(&ino->i_lock); |
Andy Adamson | 293b3b0 | 2012-06-20 15:03:34 -0400 | [diff] [blame] | 1375 | dprintk("NFS: %s no layout segments to return\n", __func__); |
Trond Myklebust | 7bcc105 | 2020-04-19 14:38:00 -0400 | [diff] [blame] | 1376 | goto out_wait_layoutreturn; |
Andy Adamson | 293b3b0 | 2012-06-20 15:03:34 -0400 | [diff] [blame] | 1377 | } |
Christoph Hellwig | 47abade | 2014-08-21 11:09:22 -0500 | [diff] [blame] | 1378 | |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1379 | send = pnfs_prepare_layoutreturn(lo, &stateid, &cred, NULL); |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1380 | spin_unlock(&ino->i_lock); |
Trond Myklebust | 7f27392 | 2015-07-09 18:40:01 +0200 | [diff] [blame] | 1381 | if (send) |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1382 | status = pnfs_send_layoutreturn(lo, &stateid, &cred, IOMODE_ANY, true); |
Trond Myklebust | 7bcc105 | 2020-04-19 14:38:00 -0400 | [diff] [blame] | 1383 | out_wait_layoutreturn: |
| 1384 | wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN, TASK_UNINTERRUPTIBLE); |
Trond Myklebust | 7f27392 | 2015-07-09 18:40:01 +0200 | [diff] [blame] | 1385 | out_put_layout_hdr: |
Trond Myklebust | ee6625a | 2017-01-26 15:50:41 -0500 | [diff] [blame] | 1386 | pnfs_free_lseg_list(&tmp_list); |
Trond Myklebust | 7f27392 | 2015-07-09 18:40:01 +0200 | [diff] [blame] | 1387 | pnfs_put_layout_hdr(lo); |
Benny Halevy | cbe8260 | 2011-05-22 19:52:37 +0300 | [diff] [blame] | 1388 | out: |
| 1389 | dprintk("<-- %s status: %d\n", __func__, status); |
| 1390 | return status; |
| 1391 | } |
| 1392 | |
Trond Myklebust | 2402867 | 2013-03-20 13:23:33 -0400 | [diff] [blame] | 1393 | int |
| 1394 | pnfs_commit_and_return_layout(struct inode *inode) |
| 1395 | { |
| 1396 | struct pnfs_layout_hdr *lo; |
| 1397 | int ret; |
| 1398 | |
| 1399 | spin_lock(&inode->i_lock); |
| 1400 | lo = NFS_I(inode)->layout; |
| 1401 | if (lo == NULL) { |
| 1402 | spin_unlock(&inode->i_lock); |
| 1403 | return 0; |
| 1404 | } |
| 1405 | pnfs_get_layout_hdr(lo); |
| 1406 | /* Block new layoutgets and read/write to ds */ |
| 1407 | lo->plh_block_lgets++; |
| 1408 | spin_unlock(&inode->i_lock); |
| 1409 | filemap_fdatawait(inode->i_mapping); |
| 1410 | ret = pnfs_layoutcommit_inode(inode, true); |
| 1411 | if (ret == 0) |
| 1412 | ret = _pnfs_return_layout(inode); |
| 1413 | spin_lock(&inode->i_lock); |
| 1414 | lo->plh_block_lgets--; |
| 1415 | spin_unlock(&inode->i_lock); |
| 1416 | pnfs_put_layout_hdr(lo); |
| 1417 | return ret; |
| 1418 | } |
| 1419 | |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1420 | bool pnfs_roc(struct inode *ino, |
| 1421 | struct nfs4_layoutreturn_args *args, |
| 1422 | struct nfs4_layoutreturn_res *res, |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 1423 | const struct cred *cred) |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1424 | { |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1425 | struct nfs_inode *nfsi = NFS_I(ino); |
| 1426 | struct nfs_open_context *ctx; |
| 1427 | struct nfs4_state *state; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1428 | struct pnfs_layout_hdr *lo; |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1429 | struct pnfs_layout_segment *lseg, *next; |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1430 | const struct cred *lc_cred; |
Peng Tao | 193e3aa | 2014-11-17 09:30:41 +0800 | [diff] [blame] | 1431 | nfs4_stateid stateid; |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1432 | enum pnfs_iomode iomode = 0; |
| 1433 | bool layoutreturn = false, roc = false; |
Trond Myklebust | e71708d | 2016-11-21 10:56:38 -0500 | [diff] [blame] | 1434 | bool skip_read = false; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1435 | |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1436 | if (!nfs_have_layout(ino)) |
| 1437 | return false; |
Trond Myklebust | 29ade5d | 2016-11-30 16:23:38 -0500 | [diff] [blame] | 1438 | retry: |
Trond Myklebust | 0de4397 | 2018-09-02 15:57:01 -0400 | [diff] [blame] | 1439 | rcu_read_lock(); |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1440 | spin_lock(&ino->i_lock); |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1441 | lo = nfsi->layout; |
Trond Myklebust | 0cdc329 | 2016-11-21 11:05:33 -0500 | [diff] [blame] | 1442 | if (!lo || !pnfs_layout_is_valid(lo) || |
Trond Myklebust | 9c6376e | 2018-03-07 14:49:06 -0500 | [diff] [blame] | 1443 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
| 1444 | lo = NULL; |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1445 | goto out_noroc; |
Trond Myklebust | 9c6376e | 2018-03-07 14:49:06 -0500 | [diff] [blame] | 1446 | } |
| 1447 | pnfs_get_layout_hdr(lo); |
Trond Myklebust | 29ade5d | 2016-11-30 16:23:38 -0500 | [diff] [blame] | 1448 | if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) { |
Trond Myklebust | 29ade5d | 2016-11-30 16:23:38 -0500 | [diff] [blame] | 1449 | spin_unlock(&ino->i_lock); |
Trond Myklebust | 0de4397 | 2018-09-02 15:57:01 -0400 | [diff] [blame] | 1450 | rcu_read_unlock(); |
Trond Myklebust | 29ade5d | 2016-11-30 16:23:38 -0500 | [diff] [blame] | 1451 | wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN, |
| 1452 | TASK_UNINTERRUPTIBLE); |
| 1453 | pnfs_put_layout_hdr(lo); |
| 1454 | goto retry; |
| 1455 | } |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1456 | |
Peng Tao | e755d63 | 2015-08-19 13:49:19 +0800 | [diff] [blame] | 1457 | /* no roc if we hold a delegation */ |
Trond Myklebust | e71708d | 2016-11-21 10:56:38 -0500 | [diff] [blame] | 1458 | if (nfs4_check_delegation(ino, FMODE_READ)) { |
| 1459 | if (nfs4_check_delegation(ino, FMODE_WRITE)) |
| 1460 | goto out_noroc; |
| 1461 | skip_read = true; |
| 1462 | } |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1463 | |
Trond Myklebust | 0de4397 | 2018-09-02 15:57:01 -0400 | [diff] [blame] | 1464 | list_for_each_entry_rcu(ctx, &nfsi->open_files, list) { |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1465 | state = ctx->state; |
Trond Myklebust | e71708d | 2016-11-21 10:56:38 -0500 | [diff] [blame] | 1466 | if (state == NULL) |
| 1467 | continue; |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1468 | /* Don't return layout if there is open file state */ |
Trond Myklebust | e71708d | 2016-11-21 10:56:38 -0500 | [diff] [blame] | 1469 | if (state->state & FMODE_WRITE) |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1470 | goto out_noroc; |
Trond Myklebust | e71708d | 2016-11-21 10:56:38 -0500 | [diff] [blame] | 1471 | if (state->state & FMODE_READ) |
| 1472 | skip_read = true; |
Trond Myklebust | 40dd4b7 | 2015-01-24 13:54:37 -0500 | [diff] [blame] | 1473 | } |
| 1474 | |
Peng Tao | e755d63 | 2015-08-19 13:49:19 +0800 | [diff] [blame] | 1475 | |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1476 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) { |
Trond Myklebust | e71708d | 2016-11-21 10:56:38 -0500 | [diff] [blame] | 1477 | if (skip_read && lseg->pls_range.iomode == IOMODE_READ) |
| 1478 | continue; |
Peng Tao | e755d63 | 2015-08-19 13:49:19 +0800 | [diff] [blame] | 1479 | /* If we are sending layoutreturn, invalidate all valid lsegs */ |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1480 | if (!test_and_clear_bit(NFS_LSEG_ROC, &lseg->pls_flags)) |
| 1481 | continue; |
| 1482 | /* |
| 1483 | * Note: mark lseg for return so pnfs_layout_remove_lseg |
| 1484 | * doesn't invalidate the layout for us. |
| 1485 | */ |
| 1486 | set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags); |
| 1487 | if (!mark_lseg_invalid(lseg, &lo->plh_return_segs)) |
| 1488 | continue; |
| 1489 | pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0); |
Trond Myklebust | 69820d2 | 2016-11-15 18:29:59 -0500 | [diff] [blame] | 1490 | } |
| 1491 | |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1492 | if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) |
| 1493 | goto out_noroc; |
Trond Myklebust | 69820d2 | 2016-11-15 18:29:59 -0500 | [diff] [blame] | 1494 | |
Peng Tao | 500d701 | 2015-09-22 11:35:22 +0800 | [diff] [blame] | 1495 | /* ROC in two conditions: |
Peng Tao | e755d63 | 2015-08-19 13:49:19 +0800 | [diff] [blame] | 1496 | * 1. there are ROC lsegs |
| 1497 | * 2. we don't send layoutreturn |
Peng Tao | e755d63 | 2015-08-19 13:49:19 +0800 | [diff] [blame] | 1498 | */ |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1499 | /* lo ref dropped in pnfs_roc_release() */ |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1500 | layoutreturn = pnfs_prepare_layoutreturn(lo, &stateid, &lc_cred, &iomode); |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1501 | /* If the creds don't match, we can't compound the layoutreturn */ |
Trond Myklebust | 4d8948c | 2020-04-19 23:53:52 -0400 | [diff] [blame] | 1502 | if (!layoutreturn || cred_fscmp(cred, lc_cred) != 0) |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1503 | goto out_noroc; |
| 1504 | |
| 1505 | roc = layoutreturn; |
| 1506 | pnfs_init_layoutreturn_args(args, lo, &stateid, iomode); |
| 1507 | res->lrs_present = 0; |
| 1508 | layoutreturn = false; |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1509 | put_cred(lc_cred); |
Trond Myklebust | 4d8948c | 2020-04-19 23:53:52 -0400 | [diff] [blame] | 1510 | |
Peng Tao | e755d63 | 2015-08-19 13:49:19 +0800 | [diff] [blame] | 1511 | out_noroc: |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1512 | spin_unlock(&ino->i_lock); |
Trond Myklebust | 0de4397 | 2018-09-02 15:57:01 -0400 | [diff] [blame] | 1513 | rcu_read_unlock(); |
Trond Myklebust | 7140171 | 2015-03-25 12:36:13 -0400 | [diff] [blame] | 1514 | pnfs_layoutcommit_inode(ino, true); |
Trond Myklebust | 287bd3e | 2016-12-02 16:12:12 -0500 | [diff] [blame] | 1515 | if (roc) { |
| 1516 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld; |
| 1517 | if (ld->prepare_layoutreturn) |
| 1518 | ld->prepare_layoutreturn(args); |
Trond Myklebust | 9c6376e | 2018-03-07 14:49:06 -0500 | [diff] [blame] | 1519 | pnfs_put_layout_hdr(lo); |
Trond Myklebust | 287bd3e | 2016-12-02 16:12:12 -0500 | [diff] [blame] | 1520 | return true; |
| 1521 | } |
Peng Tao | e755d63 | 2015-08-19 13:49:19 +0800 | [diff] [blame] | 1522 | if (layoutreturn) |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 1523 | pnfs_send_layoutreturn(lo, &stateid, &lc_cred, iomode, true); |
Trond Myklebust | 9c6376e | 2018-03-07 14:49:06 -0500 | [diff] [blame] | 1524 | pnfs_put_layout_hdr(lo); |
Trond Myklebust | 287bd3e | 2016-12-02 16:12:12 -0500 | [diff] [blame] | 1525 | return false; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Trond Myklebust | 078000d | 2021-01-04 13:18:03 -0500 | [diff] [blame] | 1528 | int pnfs_roc_done(struct rpc_task *task, struct nfs4_layoutreturn_args **argpp, |
| 1529 | struct nfs4_layoutreturn_res **respp, int *ret) |
Trond Myklebust | 287a9c5 | 2019-09-20 07:23:41 -0400 | [diff] [blame] | 1530 | { |
| 1531 | struct nfs4_layoutreturn_args *arg = *argpp; |
| 1532 | int retval = -EAGAIN; |
| 1533 | |
| 1534 | if (!arg) |
| 1535 | return 0; |
| 1536 | /* Handle Layoutreturn errors */ |
| 1537 | switch (*ret) { |
| 1538 | case 0: |
| 1539 | retval = 0; |
| 1540 | break; |
Trond Myklebust | 6109bcf | 2019-09-20 07:23:43 -0400 | [diff] [blame] | 1541 | case -NFS4ERR_NOMATCHING_LAYOUT: |
| 1542 | /* Was there an RPC level error? If not, retry */ |
| 1543 | if (task->tk_rpc_status == 0) |
| 1544 | break; |
| 1545 | /* If the call was not sent, let caller handle it */ |
| 1546 | if (!RPC_WAS_SENT(task)) |
| 1547 | return 0; |
| 1548 | /* |
| 1549 | * Otherwise, assume the call succeeded and |
| 1550 | * that we need to release the layout |
| 1551 | */ |
| 1552 | *ret = 0; |
| 1553 | (*respp)->lrs_present = 0; |
| 1554 | retval = 0; |
| 1555 | break; |
Trond Myklebust | 078a432 | 2019-09-20 07:23:42 -0400 | [diff] [blame] | 1556 | case -NFS4ERR_DELAY: |
| 1557 | /* Let the caller handle the retry */ |
| 1558 | *ret = -NFS4ERR_NOMATCHING_LAYOUT; |
| 1559 | return 0; |
Trond Myklebust | 287a9c5 | 2019-09-20 07:23:41 -0400 | [diff] [blame] | 1560 | case -NFS4ERR_OLD_STATEID: |
Trond Myklebust | 30cb3ee | 2019-09-20 07:23:45 -0400 | [diff] [blame] | 1561 | if (!nfs4_layout_refresh_old_stateid(&arg->stateid, |
Trond Myklebust | 078000d | 2021-01-04 13:18:03 -0500 | [diff] [blame] | 1562 | &arg->range, arg->inode)) |
Trond Myklebust | 287a9c5 | 2019-09-20 07:23:41 -0400 | [diff] [blame] | 1563 | break; |
| 1564 | *ret = -NFS4ERR_NOMATCHING_LAYOUT; |
| 1565 | return -EAGAIN; |
| 1566 | } |
| 1567 | *argpp = NULL; |
| 1568 | *respp = NULL; |
| 1569 | return retval; |
| 1570 | } |
| 1571 | |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1572 | void pnfs_roc_release(struct nfs4_layoutreturn_args *args, |
| 1573 | struct nfs4_layoutreturn_res *res, |
| 1574 | int ret) |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1575 | { |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1576 | struct pnfs_layout_hdr *lo = args->layout; |
Trond Myklebust | 67bbcee | 2021-01-04 13:35:46 -0500 | [diff] [blame] | 1577 | struct inode *inode = args->inode; |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1578 | const nfs4_stateid *res_stateid = NULL; |
Trond Myklebust | 287bd3e | 2016-12-02 16:12:12 -0500 | [diff] [blame] | 1579 | struct nfs4_xdr_opaque_data *ld_private = args->ld_private; |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1580 | |
Trond Myklebust | 9c47b18 | 2019-09-20 07:23:40 -0400 | [diff] [blame] | 1581 | switch (ret) { |
| 1582 | case -NFS4ERR_NOMATCHING_LAYOUT: |
Trond Myklebust | 67bbcee | 2021-01-04 13:35:46 -0500 | [diff] [blame] | 1583 | spin_lock(&inode->i_lock); |
| 1584 | if (pnfs_layout_is_valid(lo) && |
| 1585 | nfs4_stateid_match_other(&args->stateid, &lo->plh_stateid)) |
| 1586 | pnfs_set_plh_return_info(lo, args->range.iomode, 0); |
Trond Myklebust | c18d1e1 | 2021-01-04 15:01:18 -0500 | [diff] [blame] | 1587 | pnfs_clear_layoutreturn_waitbit(lo); |
Trond Myklebust | 67bbcee | 2021-01-04 13:35:46 -0500 | [diff] [blame] | 1588 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 9c47b18 | 2019-09-20 07:23:40 -0400 | [diff] [blame] | 1589 | break; |
| 1590 | case 0: |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1591 | if (res->lrs_present) |
| 1592 | res_stateid = &res->stateid; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1593 | fallthrough; |
Trond Myklebust | 9c47b18 | 2019-09-20 07:23:40 -0400 | [diff] [blame] | 1594 | default: |
Trond Myklebust | c18d1e1 | 2021-01-04 15:01:18 -0500 | [diff] [blame] | 1595 | pnfs_layoutreturn_free_lsegs(lo, &args->stateid, &args->range, |
| 1596 | res_stateid); |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1597 | } |
Trond Myklebust | a19b478 | 2020-08-03 18:32:28 -0400 | [diff] [blame] | 1598 | trace_nfs4_layoutreturn_on_close(args->inode, &args->stateid, ret); |
Trond Myklebust | 287bd3e | 2016-12-02 16:12:12 -0500 | [diff] [blame] | 1599 | if (ld_private && ld_private->ops && ld_private->ops->free) |
| 1600 | ld_private->ops->free(ld_private); |
Trond Myklebust | 1c5bd76d | 2016-11-16 01:11:25 -0500 | [diff] [blame] | 1601 | pnfs_put_layout_hdr(lo); |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Peng Tao | 500d701 | 2015-09-22 11:35:22 +0800 | [diff] [blame] | 1604 | bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task) |
| 1605 | { |
| 1606 | struct nfs_inode *nfsi = NFS_I(ino); |
| 1607 | struct pnfs_layout_hdr *lo; |
| 1608 | bool sleep = false; |
| 1609 | |
| 1610 | /* we might not have grabbed lo reference. so need to check under |
| 1611 | * i_lock */ |
| 1612 | spin_lock(&ino->i_lock); |
| 1613 | lo = nfsi->layout; |
Trond Myklebust | ee284e3 | 2016-11-18 15:21:30 -0500 | [diff] [blame] | 1614 | if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) { |
Peng Tao | 500d701 | 2015-09-22 11:35:22 +0800 | [diff] [blame] | 1615 | rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL); |
Trond Myklebust | ee284e3 | 2016-11-18 15:21:30 -0500 | [diff] [blame] | 1616 | sleep = true; |
| 1617 | } |
| 1618 | spin_unlock(&ino->i_lock); |
Peng Tao | 500d701 | 2015-09-22 11:35:22 +0800 | [diff] [blame] | 1619 | return sleep; |
| 1620 | } |
| 1621 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1622 | /* |
| 1623 | * Compare two layout segments for sorting into layout cache. |
| 1624 | * We want to preferentially return RW over RO layouts, so ensure those |
| 1625 | * are seen first. |
| 1626 | */ |
| 1627 | static s64 |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 1628 | pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1, |
Trond Myklebust | 3cb2df1 | 2013-06-03 11:24:36 -0400 | [diff] [blame] | 1629 | const struct pnfs_layout_range *l2) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1630 | { |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1631 | s64 d; |
| 1632 | |
| 1633 | /* high offset > low offset */ |
| 1634 | d = l1->offset - l2->offset; |
| 1635 | if (d) |
| 1636 | return d; |
| 1637 | |
| 1638 | /* short length > long length */ |
| 1639 | d = l2->length - l1->length; |
| 1640 | if (d) |
| 1641 | return d; |
| 1642 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1643 | /* read > read/write */ |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1644 | return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1645 | } |
| 1646 | |
Trond Myklebust | 03772d2 | 2015-08-25 08:54:17 -0400 | [diff] [blame] | 1647 | static bool |
| 1648 | pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1, |
| 1649 | const struct pnfs_layout_range *l2) |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 1650 | { |
Trond Myklebust | 03772d2 | 2015-08-25 08:54:17 -0400 | [diff] [blame] | 1651 | return pnfs_lseg_range_cmp(l1, l2) > 0; |
| 1652 | } |
| 1653 | |
| 1654 | static bool |
| 1655 | pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg, |
| 1656 | struct pnfs_layout_segment *old) |
| 1657 | { |
| 1658 | return false; |
| 1659 | } |
| 1660 | |
| 1661 | void |
| 1662 | pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo, |
| 1663 | struct pnfs_layout_segment *lseg, |
| 1664 | bool (*is_after)(const struct pnfs_layout_range *, |
| 1665 | const struct pnfs_layout_range *), |
| 1666 | bool (*do_merge)(struct pnfs_layout_segment *, |
| 1667 | struct pnfs_layout_segment *), |
| 1668 | struct list_head *free_me) |
| 1669 | { |
| 1670 | struct pnfs_layout_segment *lp, *tmp; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1671 | |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 1672 | dprintk("%s:Begin\n", __func__); |
| 1673 | |
Trond Myklebust | 03772d2 | 2015-08-25 08:54:17 -0400 | [diff] [blame] | 1674 | list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) { |
| 1675 | if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0) |
| 1676 | continue; |
| 1677 | if (do_merge(lseg, lp)) { |
| 1678 | mark_lseg_invalid(lp, free_me); |
| 1679 | continue; |
| 1680 | } |
| 1681 | if (is_after(&lseg->pls_range, &lp->pls_range)) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1682 | continue; |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 1683 | list_add_tail(&lseg->pls_list, &lp->pls_list); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1684 | dprintk("%s: inserted lseg %p " |
| 1685 | "iomode %d offset %llu length %llu before " |
| 1686 | "lp %p iomode %d offset %llu length %llu\n", |
Fred Isaman | 566052c | 2011-01-06 11:36:20 +0000 | [diff] [blame] | 1687 | __func__, lseg, lseg->pls_range.iomode, |
| 1688 | lseg->pls_range.offset, lseg->pls_range.length, |
| 1689 | lp, lp->pls_range.iomode, lp->pls_range.offset, |
| 1690 | lp->pls_range.length); |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1691 | goto out; |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 1692 | } |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1693 | list_add_tail(&lseg->pls_list, &lo->plh_segs); |
| 1694 | dprintk("%s: inserted lseg %p " |
| 1695 | "iomode %d offset %llu length %llu at tail\n", |
| 1696 | __func__, lseg, lseg->pls_range.iomode, |
| 1697 | lseg->pls_range.offset, lseg->pls_range.length); |
| 1698 | out: |
Trond Myklebust | 70c3bd2 | 2012-09-18 20:51:13 -0400 | [diff] [blame] | 1699 | pnfs_get_layout_hdr(lo); |
Andy Adamson | 974cec8 | 2010-10-20 00:18:02 -0400 | [diff] [blame] | 1700 | |
| 1701 | dprintk("%s:Return\n", __func__); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1702 | } |
Trond Myklebust | 03772d2 | 2015-08-25 08:54:17 -0400 | [diff] [blame] | 1703 | EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg); |
| 1704 | |
| 1705 | static void |
| 1706 | pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo, |
| 1707 | struct pnfs_layout_segment *lseg, |
| 1708 | struct list_head *free_me) |
| 1709 | { |
| 1710 | struct inode *inode = lo->plh_inode; |
| 1711 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld; |
| 1712 | |
| 1713 | if (ld->add_lseg != NULL) |
| 1714 | ld->add_lseg(lo, lseg, free_me); |
| 1715 | else |
| 1716 | pnfs_generic_layout_insert_lseg(lo, lseg, |
| 1717 | pnfs_lseg_range_is_after, |
| 1718 | pnfs_lseg_no_merge, |
| 1719 | free_me); |
| 1720 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1721 | |
| 1722 | static struct pnfs_layout_hdr * |
Peng Tao | 9fa4075 | 2011-07-30 20:52:32 -0400 | [diff] [blame] | 1723 | alloc_init_layout_hdr(struct inode *ino, |
| 1724 | struct nfs_open_context *ctx, |
| 1725 | gfp_t gfp_flags) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1726 | { |
| 1727 | struct pnfs_layout_hdr *lo; |
| 1728 | |
Benny Halevy | 636fb9c | 2011-05-22 19:51:33 +0300 | [diff] [blame] | 1729 | lo = pnfs_alloc_layout_hdr(ino, gfp_flags); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1730 | if (!lo) |
| 1731 | return NULL; |
Elena Reshetova | 2b28a7b | 2017-10-20 12:53:33 +0300 | [diff] [blame] | 1732 | refcount_set(&lo->plh_refcount, 1); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 1733 | INIT_LIST_HEAD(&lo->plh_layouts); |
| 1734 | INIT_LIST_HEAD(&lo->plh_segs); |
Trond Myklebust | 68f7447 | 2016-10-12 19:50:54 -0400 | [diff] [blame] | 1735 | INIT_LIST_HEAD(&lo->plh_return_segs); |
Trond Myklebust | fd9a8d7 | 2013-02-12 09:48:42 -0500 | [diff] [blame] | 1736 | INIT_LIST_HEAD(&lo->plh_bulk_destroy); |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 1737 | lo->plh_inode = ino; |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 1738 | lo->plh_lc_cred = get_cred(ctx->cred); |
Trond Myklebust | 67a3b72 | 2016-06-17 16:48:19 -0400 | [diff] [blame] | 1739 | lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1740 | return lo; |
| 1741 | } |
| 1742 | |
| 1743 | static struct pnfs_layout_hdr * |
Peng Tao | 9fa4075 | 2011-07-30 20:52:32 -0400 | [diff] [blame] | 1744 | pnfs_find_alloc_layout(struct inode *ino, |
| 1745 | struct nfs_open_context *ctx, |
| 1746 | gfp_t gfp_flags) |
Trond Myklebust | e5241e4 | 2016-06-17 16:48:20 -0400 | [diff] [blame] | 1747 | __releases(&ino->i_lock) |
| 1748 | __acquires(&ino->i_lock) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1749 | { |
| 1750 | struct nfs_inode *nfsi = NFS_I(ino); |
| 1751 | struct pnfs_layout_hdr *new = NULL; |
| 1752 | |
| 1753 | dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout); |
| 1754 | |
Trond Myklebust | 251ec41 | 2012-10-02 15:41:05 -0700 | [diff] [blame] | 1755 | if (nfsi->layout != NULL) |
| 1756 | goto out_existing; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1757 | spin_unlock(&ino->i_lock); |
Peng Tao | 9fa4075 | 2011-07-30 20:52:32 -0400 | [diff] [blame] | 1758 | new = alloc_init_layout_hdr(ino, ctx, gfp_flags); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1759 | spin_lock(&ino->i_lock); |
| 1760 | |
Trond Myklebust | 251ec41 | 2012-10-02 15:41:05 -0700 | [diff] [blame] | 1761 | if (likely(nfsi->layout == NULL)) { /* Won the race? */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1762 | nfsi->layout = new; |
Trond Myklebust | 251ec41 | 2012-10-02 15:41:05 -0700 | [diff] [blame] | 1763 | return new; |
Yanchuan Nian | 7175fe9 | 2012-10-31 16:05:48 +0800 | [diff] [blame] | 1764 | } else if (new != NULL) |
| 1765 | pnfs_free_layout_hdr(new); |
Trond Myklebust | 251ec41 | 2012-10-02 15:41:05 -0700 | [diff] [blame] | 1766 | out_existing: |
| 1767 | pnfs_get_layout_hdr(nfsi->layout); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1768 | return nfsi->layout; |
| 1769 | } |
| 1770 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1771 | /* |
| 1772 | * iomode matching rules: |
Tom Haynes | c7d73af | 2016-05-25 07:31:14 -0700 | [diff] [blame] | 1773 | * iomode lseg strict match |
| 1774 | * iomode |
| 1775 | * ----- ----- ------ ----- |
| 1776 | * ANY READ N/A true |
| 1777 | * ANY RW N/A true |
| 1778 | * RW READ N/A false |
| 1779 | * RW RW N/A true |
| 1780 | * READ READ N/A true |
| 1781 | * READ RW true false |
| 1782 | * READ RW false true |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1783 | */ |
Trond Myklebust | 3cb2df1 | 2013-06-03 11:24:36 -0400 | [diff] [blame] | 1784 | static bool |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 1785 | pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range, |
Tom Haynes | c7d73af | 2016-05-25 07:31:14 -0700 | [diff] [blame] | 1786 | const struct pnfs_layout_range *range, |
| 1787 | bool strict_iomode) |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1788 | { |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1789 | struct pnfs_layout_range range1; |
| 1790 | |
| 1791 | if ((range->iomode == IOMODE_RW && |
| 1792 | ls_range->iomode != IOMODE_RW) || |
Tom Haynes | c7d73af | 2016-05-25 07:31:14 -0700 | [diff] [blame] | 1793 | (range->iomode != ls_range->iomode && |
Thomas Meyer | 6089dd0 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 1794 | strict_iomode) || |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 1795 | !pnfs_lseg_range_intersecting(ls_range, range)) |
Gustavo A. R. Silva | 10db5b7 | 2018-08-01 19:55:51 -0500 | [diff] [blame] | 1796 | return false; |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1797 | |
| 1798 | /* range1 covers only the first byte in the range */ |
| 1799 | range1 = *range; |
| 1800 | range1.length = 1; |
Trond Myklebust | 7dc0ac7 | 2013-06-03 11:30:24 -0400 | [diff] [blame] | 1801 | return pnfs_lseg_range_contained(ls_range, &range1); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | /* |
| 1805 | * lookup range in layout |
| 1806 | */ |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1807 | static struct pnfs_layout_segment * |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1808 | pnfs_find_lseg(struct pnfs_layout_hdr *lo, |
Tom Haynes | c7d73af | 2016-05-25 07:31:14 -0700 | [diff] [blame] | 1809 | struct pnfs_layout_range *range, |
| 1810 | bool strict_iomode) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1811 | { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1812 | struct pnfs_layout_segment *lseg, *ret = NULL; |
| 1813 | |
| 1814 | dprintk("%s:Begin\n", __func__); |
| 1815 | |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 1816 | list_for_each_entry(lseg, &lo->plh_segs, pls_list) { |
Fred Isaman | 4541d16 | 2011-01-06 11:36:23 +0000 | [diff] [blame] | 1817 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) && |
Tom Haynes | c7d73af | 2016-05-25 07:31:14 -0700 | [diff] [blame] | 1818 | pnfs_lseg_range_match(&lseg->pls_range, range, |
| 1819 | strict_iomode)) { |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 1820 | ret = pnfs_get_lseg(lseg); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1821 | break; |
| 1822 | } |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | dprintk("%s:Return lseg %p ref %d\n", |
Elena Reshetova | eba6dd6 | 2017-10-20 12:53:32 +0300 | [diff] [blame] | 1826 | __func__, ret, ret ? refcount_read(&ret->pls_refcount) : 0); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 1827 | return ret; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | /* |
Andy Adamson | d23d61c | 2012-05-23 05:02:37 -0400 | [diff] [blame] | 1831 | * Use mdsthreshold hints set at each OPEN to determine if I/O should go |
| 1832 | * to the MDS or over pNFS |
| 1833 | * |
| 1834 | * The nfs_inode read_io and write_io fields are cumulative counters reset |
| 1835 | * when there are no layout segments. Note that in pnfs_update_layout iomode |
| 1836 | * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a |
| 1837 | * WRITE request. |
| 1838 | * |
| 1839 | * A return of true means use MDS I/O. |
| 1840 | * |
| 1841 | * From rfc 5661: |
| 1842 | * If a file's size is smaller than the file size threshold, data accesses |
| 1843 | * SHOULD be sent to the metadata server. If an I/O request has a length that |
| 1844 | * is below the I/O size threshold, the I/O SHOULD be sent to the metadata |
| 1845 | * server. If both file size and I/O size are provided, the client SHOULD |
| 1846 | * reach or exceed both thresholds before sending its read or write |
| 1847 | * requests to the data server. |
| 1848 | */ |
| 1849 | static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx, |
| 1850 | struct inode *ino, int iomode) |
| 1851 | { |
| 1852 | struct nfs4_threshold *t = ctx->mdsthreshold; |
| 1853 | struct nfs_inode *nfsi = NFS_I(ino); |
| 1854 | loff_t fsize = i_size_read(ino); |
| 1855 | bool size = false, size_set = false, io = false, io_set = false, ret = false; |
| 1856 | |
| 1857 | if (t == NULL) |
| 1858 | return ret; |
| 1859 | |
| 1860 | dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n", |
| 1861 | __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz); |
| 1862 | |
| 1863 | switch (iomode) { |
| 1864 | case IOMODE_READ: |
| 1865 | if (t->bm & THRESHOLD_RD) { |
| 1866 | dprintk("%s fsize %llu\n", __func__, fsize); |
| 1867 | size_set = true; |
| 1868 | if (fsize < t->rd_sz) |
| 1869 | size = true; |
| 1870 | } |
| 1871 | if (t->bm & THRESHOLD_RD_IO) { |
| 1872 | dprintk("%s nfsi->read_io %llu\n", __func__, |
| 1873 | nfsi->read_io); |
| 1874 | io_set = true; |
| 1875 | if (nfsi->read_io < t->rd_io_sz) |
| 1876 | io = true; |
| 1877 | } |
| 1878 | break; |
| 1879 | case IOMODE_RW: |
| 1880 | if (t->bm & THRESHOLD_WR) { |
| 1881 | dprintk("%s fsize %llu\n", __func__, fsize); |
| 1882 | size_set = true; |
| 1883 | if (fsize < t->wr_sz) |
| 1884 | size = true; |
| 1885 | } |
| 1886 | if (t->bm & THRESHOLD_WR_IO) { |
| 1887 | dprintk("%s nfsi->write_io %llu\n", __func__, |
| 1888 | nfsi->write_io); |
| 1889 | io_set = true; |
| 1890 | if (nfsi->write_io < t->wr_io_sz) |
| 1891 | io = true; |
| 1892 | } |
| 1893 | break; |
| 1894 | } |
| 1895 | if (size_set && io_set) { |
| 1896 | if (size && io) |
| 1897 | ret = true; |
| 1898 | } else if (size || io) |
| 1899 | ret = true; |
| 1900 | |
| 1901 | dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret); |
| 1902 | return ret; |
| 1903 | } |
| 1904 | |
Trond Myklebust | d03360a | 2018-09-05 14:07:12 -0400 | [diff] [blame] | 1905 | static int pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo) |
Peng Tao | aa8a45e | 2014-12-01 08:22:23 +0800 | [diff] [blame] | 1906 | { |
| 1907 | /* |
| 1908 | * send layoutcommit as it can hold up layoutreturn due to lseg |
| 1909 | * reference |
| 1910 | */ |
| 1911 | pnfs_layoutcommit_inode(lo->plh_inode, false); |
Trond Myklebust | d03360a | 2018-09-05 14:07:12 -0400 | [diff] [blame] | 1912 | return wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN, |
Trond Myklebust | 2e5b29f | 2015-12-14 16:25:11 -0500 | [diff] [blame] | 1913 | nfs_wait_bit_killable, |
Peter Zijlstra | f5d39b0 | 2022-08-22 13:18:22 +0200 | [diff] [blame] | 1914 | TASK_KILLABLE|TASK_FREEZABLE_UNSAFE); |
Peng Tao | aa8a45e | 2014-12-01 08:22:23 +0800 | [diff] [blame] | 1915 | } |
| 1916 | |
Trond Myklebust | 411ae72 | 2018-06-23 22:54:33 -0400 | [diff] [blame] | 1917 | static void nfs_layoutget_begin(struct pnfs_layout_hdr *lo) |
| 1918 | { |
| 1919 | atomic_inc(&lo->plh_outstanding); |
| 1920 | } |
| 1921 | |
| 1922 | static void nfs_layoutget_end(struct pnfs_layout_hdr *lo) |
| 1923 | { |
Trond Myklebust | 880265c | 2022-05-31 11:03:07 -0400 | [diff] [blame] | 1924 | if (atomic_dec_and_test(&lo->plh_outstanding) && |
| 1925 | test_and_clear_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags)) |
| 1926 | wake_up_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN); |
Trond Myklebust | 411ae72 | 2018-06-23 22:54:33 -0400 | [diff] [blame] | 1927 | } |
| 1928 | |
Trond Myklebust | d29b468d | 2021-01-22 10:05:51 -0500 | [diff] [blame] | 1929 | static bool pnfs_is_first_layoutget(struct pnfs_layout_hdr *lo) |
| 1930 | { |
| 1931 | return test_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags); |
| 1932 | } |
| 1933 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1934 | static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo) |
| 1935 | { |
| 1936 | unsigned long *bitlock = &lo->plh_flags; |
| 1937 | |
| 1938 | clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock); |
| 1939 | smp_mb__after_atomic(); |
| 1940 | wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET); |
| 1941 | } |
| 1942 | |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 1943 | static void _add_to_server_list(struct pnfs_layout_hdr *lo, |
| 1944 | struct nfs_server *server) |
| 1945 | { |
Trond Myklebust | cf6605d | 2020-02-18 17:14:40 -0500 | [diff] [blame] | 1946 | if (!test_and_set_bit(NFS_LAYOUT_HASHED, &lo->plh_flags)) { |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 1947 | struct nfs_client *clp = server->nfs_client; |
| 1948 | |
| 1949 | /* The lo must be on the clp list if there is any |
| 1950 | * chance of a CB_LAYOUTRECALL(FILE) coming in. |
| 1951 | */ |
| 1952 | spin_lock(&clp->cl_lock); |
Trond Myklebust | cf6605d | 2020-02-18 17:14:40 -0500 | [diff] [blame] | 1953 | list_add_tail_rcu(&lo->plh_layouts, &server->layouts); |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 1954 | spin_unlock(&clp->cl_lock); |
| 1955 | } |
| 1956 | } |
| 1957 | |
Andy Adamson | d23d61c | 2012-05-23 05:02:37 -0400 | [diff] [blame] | 1958 | /* |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1959 | * Layout segment is retreived from the server if not cached. |
| 1960 | * The appropriate layout segment is referenced and returned to the caller. |
| 1961 | */ |
Andy Adamson | 7c24d94 | 2011-06-13 18:22:38 -0400 | [diff] [blame] | 1962 | struct pnfs_layout_segment * |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1963 | pnfs_update_layout(struct inode *ino, |
| 1964 | struct nfs_open_context *ctx, |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1965 | loff_t pos, |
| 1966 | u64 count, |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 1967 | enum pnfs_iomode iomode, |
Tom Haynes | c7d73af | 2016-05-25 07:31:14 -0700 | [diff] [blame] | 1968 | bool strict_iomode, |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 1969 | gfp_t gfp_flags) |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1970 | { |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 1971 | struct pnfs_layout_range arg = { |
| 1972 | .iomode = iomode, |
| 1973 | .offset = pos, |
| 1974 | .length = count, |
| 1975 | }; |
Trond Myklebust | 70d2f7b | 2017-09-11 13:09:37 -0400 | [diff] [blame] | 1976 | unsigned pg_offset; |
Weston Andros Adamson | 6382a44 | 2011-06-01 16:44:44 -0400 | [diff] [blame] | 1977 | struct nfs_server *server = NFS_SERVER(ino); |
| 1978 | struct nfs_client *clp = server->nfs_client; |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 1979 | struct pnfs_layout_hdr *lo = NULL; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1980 | struct pnfs_layout_segment *lseg = NULL; |
Fred Isaman | 587f03d | 2016-09-21 05:14:28 -0400 | [diff] [blame] | 1981 | struct nfs4_layoutget *lgp; |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 1982 | nfs4_stateid stateid; |
| 1983 | long timeout = 0; |
Trond Myklebust | 66b53f3 | 2016-07-14 14:28:31 -0400 | [diff] [blame] | 1984 | unsigned long giveup = jiffies + (clp->cl_lease_time << 1); |
Weston Andros Adamson | 3000512 | 2013-02-28 20:30:10 -0500 | [diff] [blame] | 1985 | bool first; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 1986 | |
Jeff Layton | 9a4bf31 | 2015-12-10 10:41:58 -0500 | [diff] [blame] | 1987 | if (!pnfs_enabled_sb(NFS_SERVER(ino))) { |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 1988 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
Jeff Layton | 9a4bf31 | 2015-12-10 10:41:58 -0500 | [diff] [blame] | 1989 | PNFS_UPDATE_LAYOUT_NO_PNFS); |
Trond Myklebust | f86bbcf | 2012-09-26 11:21:40 -0400 | [diff] [blame] | 1990 | goto out; |
Jeff Layton | 9a4bf31 | 2015-12-10 10:41:58 -0500 | [diff] [blame] | 1991 | } |
Andy Adamson | d23d61c | 2012-05-23 05:02:37 -0400 | [diff] [blame] | 1992 | |
Jeff Layton | 9a4bf31 | 2015-12-10 10:41:58 -0500 | [diff] [blame] | 1993 | if (pnfs_within_mdsthreshold(ctx, ino, iomode)) { |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 1994 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
Jeff Layton | 9a4bf31 | 2015-12-10 10:41:58 -0500 | [diff] [blame] | 1995 | PNFS_UPDATE_LAYOUT_MDSTHRESH); |
Trond Myklebust | f86bbcf | 2012-09-26 11:21:40 -0400 | [diff] [blame] | 1996 | goto out; |
Jeff Layton | 9a4bf31 | 2015-12-10 10:41:58 -0500 | [diff] [blame] | 1997 | } |
Andy Adamson | d23d61c | 2012-05-23 05:02:37 -0400 | [diff] [blame] | 1998 | |
Peng Tao | 9bf8748 | 2014-08-22 17:37:41 +0800 | [diff] [blame] | 1999 | lookup_again: |
Trond Myklebust | d03360a | 2018-09-05 14:07:12 -0400 | [diff] [blame] | 2000 | lseg = ERR_PTR(nfs4_client_recover_expired_lease(clp)); |
| 2001 | if (IS_ERR(lseg)) |
| 2002 | goto out; |
Peng Tao | 9bf8748 | 2014-08-22 17:37:41 +0800 | [diff] [blame] | 2003 | first = false; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 2004 | spin_lock(&ino->i_lock); |
Peng Tao | 9fa4075 | 2011-07-30 20:52:32 -0400 | [diff] [blame] | 2005 | lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags); |
Trond Myklebust | 830ffb5 | 2012-09-20 21:25:19 -0400 | [diff] [blame] | 2006 | if (lo == NULL) { |
| 2007 | spin_unlock(&ino->i_lock); |
Trond Myklebust | 3764a17 | 2022-05-14 10:08:11 -0400 | [diff] [blame] | 2008 | lseg = ERR_PTR(-ENOMEM); |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2009 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
Jeff Layton | 9a4bf31 | 2015-12-10 10:41:58 -0500 | [diff] [blame] | 2010 | PNFS_UPDATE_LAYOUT_NOMEM); |
Trond Myklebust | 830ffb5 | 2012-09-20 21:25:19 -0400 | [diff] [blame] | 2011 | goto out; |
| 2012 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 2013 | |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 2014 | /* Do we even need to bother with this? */ |
Trond Myklebust | a59c30a | 2012-03-01 11:17:47 -0500 | [diff] [blame] | 2015 | if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2016 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
Jeff Layton | 9a4bf31 | 2015-12-10 10:41:58 -0500 | [diff] [blame] | 2017 | PNFS_UPDATE_LAYOUT_BULK_RECALL); |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 2018 | dprintk("%s matches recall, use MDS\n", __func__); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 2019 | goto out_unlock; |
| 2020 | } |
| 2021 | |
| 2022 | /* if LAYOUTGET already failed once we don't try again */ |
Trond Myklebust | 2e5b29f | 2015-12-14 16:25:11 -0500 | [diff] [blame] | 2023 | if (pnfs_layout_io_test_failed(lo, iomode)) { |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2024 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
Jeff Layton | 9a4bf31 | 2015-12-10 10:41:58 -0500 | [diff] [blame] | 2025 | PNFS_UPDATE_LAYOUT_IO_TEST_FAIL); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 2026 | goto out_unlock; |
Jeff Layton | 9a4bf31 | 2015-12-10 10:41:58 -0500 | [diff] [blame] | 2027 | } |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 2028 | |
Trond Myklebust | 411ae72 | 2018-06-23 22:54:33 -0400 | [diff] [blame] | 2029 | /* |
| 2030 | * If the layout segment list is empty, but there are outstanding |
| 2031 | * layoutget calls, then they might be subject to a layoutrecall. |
| 2032 | */ |
Trond Myklebust | 880265c | 2022-05-31 11:03:07 -0400 | [diff] [blame] | 2033 | if (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) && |
Trond Myklebust | 411ae72 | 2018-06-23 22:54:33 -0400 | [diff] [blame] | 2034 | atomic_read(&lo->plh_outstanding) != 0) { |
| 2035 | spin_unlock(&ino->i_lock); |
Trond Myklebust | 880265c | 2022-05-31 11:03:07 -0400 | [diff] [blame] | 2036 | lseg = ERR_PTR(wait_on_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN, |
| 2037 | TASK_KILLABLE)); |
Trond Myklebust | 58bbeab | 2019-07-18 15:33:42 -0400 | [diff] [blame] | 2038 | if (IS_ERR(lseg)) |
Trond Myklebust | 411ae72 | 2018-06-23 22:54:33 -0400 | [diff] [blame] | 2039 | goto out_put_layout_hdr; |
| 2040 | pnfs_put_layout_hdr(lo); |
| 2041 | goto lookup_again; |
| 2042 | } |
| 2043 | |
Trond Myklebust | 2c8d5fc | 2021-01-05 06:43:45 -0500 | [diff] [blame] | 2044 | /* |
| 2045 | * Because we free lsegs when sending LAYOUTRETURN, we need to wait |
| 2046 | * for LAYOUTRETURN. |
| 2047 | */ |
| 2048 | if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) { |
| 2049 | spin_unlock(&ino->i_lock); |
| 2050 | dprintk("%s wait for layoutreturn\n", __func__); |
| 2051 | lseg = ERR_PTR(pnfs_prepare_to_retry_layoutget(lo)); |
| 2052 | if (!IS_ERR(lseg)) { |
| 2053 | pnfs_put_layout_hdr(lo); |
| 2054 | dprintk("%s retrying\n", __func__); |
| 2055 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, |
| 2056 | lseg, |
| 2057 | PNFS_UPDATE_LAYOUT_RETRY); |
| 2058 | goto lookup_again; |
| 2059 | } |
| 2060 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
| 2061 | PNFS_UPDATE_LAYOUT_RETURN); |
| 2062 | goto out_put_layout_hdr; |
| 2063 | } |
| 2064 | |
Tom Haynes | c7d73af | 2016-05-25 07:31:14 -0700 | [diff] [blame] | 2065 | lseg = pnfs_find_lseg(lo, &arg, strict_iomode); |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2066 | if (lseg) { |
| 2067 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
| 2068 | PNFS_UPDATE_LAYOUT_FOUND_CACHED); |
| 2069 | goto out_unlock; |
| 2070 | } |
| 2071 | |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2072 | /* |
| 2073 | * Choose a stateid for the LAYOUTGET. If we don't have a layout |
| 2074 | * stateid, or it has been invalidated, then we must use the open |
| 2075 | * stateid. |
| 2076 | */ |
Trond Myklebust | 67a3b72 | 2016-06-17 16:48:19 -0400 | [diff] [blame] | 2077 | if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) { |
Trond Myklebust | d9aba2b | 2019-07-16 15:38:28 -0400 | [diff] [blame] | 2078 | int status; |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2079 | |
| 2080 | /* |
| 2081 | * The first layoutget for the file. Need to serialize per |
Peng Tao | 9bf8748 | 2014-08-22 17:37:41 +0800 | [diff] [blame] | 2082 | * RFC 5661 Errata 3208. |
| 2083 | */ |
| 2084 | if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, |
| 2085 | &lo->plh_flags)) { |
| 2086 | spin_unlock(&ino->i_lock); |
Trond Myklebust | d03360a | 2018-09-05 14:07:12 -0400 | [diff] [blame] | 2087 | lseg = ERR_PTR(wait_on_bit(&lo->plh_flags, |
| 2088 | NFS_LAYOUT_FIRST_LAYOUTGET, |
| 2089 | TASK_KILLABLE)); |
| 2090 | if (IS_ERR(lseg)) |
| 2091 | goto out_put_layout_hdr; |
Peng Tao | 9bf8748 | 2014-08-22 17:37:41 +0800 | [diff] [blame] | 2092 | pnfs_put_layout_hdr(lo); |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2093 | dprintk("%s retrying\n", __func__); |
Peng Tao | 9bf8748 | 2014-08-22 17:37:41 +0800 | [diff] [blame] | 2094 | goto lookup_again; |
| 2095 | } |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2096 | |
Trond Myklebust | fbf4bcc | 2020-04-13 15:55:21 -0400 | [diff] [blame] | 2097 | spin_unlock(&ino->i_lock); |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2098 | first = true; |
Trond Myklebust | d9aba2b | 2019-07-16 15:38:28 -0400 | [diff] [blame] | 2099 | status = nfs4_select_rw_stateid(ctx->state, |
Trond Myklebust | 70d2f7b | 2017-09-11 13:09:37 -0400 | [diff] [blame] | 2100 | iomode == IOMODE_RW ? FMODE_WRITE : FMODE_READ, |
Trond Myklebust | d9aba2b | 2019-07-16 15:38:28 -0400 | [diff] [blame] | 2101 | NULL, &stateid, NULL); |
| 2102 | if (status != 0) { |
Trond Myklebust | 731c74d | 2019-07-22 18:06:17 +0100 | [diff] [blame] | 2103 | lseg = ERR_PTR(status); |
Trond Myklebust | 70d2f7b | 2017-09-11 13:09:37 -0400 | [diff] [blame] | 2104 | trace_pnfs_update_layout(ino, pos, count, |
| 2105 | iomode, lo, lseg, |
| 2106 | PNFS_UPDATE_LAYOUT_INVALID_OPEN); |
Trond Myklebust | d9aba2b | 2019-07-16 15:38:28 -0400 | [diff] [blame] | 2107 | nfs4_schedule_stateid_recovery(server, ctx->state); |
| 2108 | pnfs_clear_first_layoutget(lo); |
| 2109 | pnfs_put_layout_hdr(lo); |
| 2110 | goto lookup_again; |
Trond Myklebust | 70d2f7b | 2017-09-11 13:09:37 -0400 | [diff] [blame] | 2111 | } |
Trond Myklebust | fbf4bcc | 2020-04-13 15:55:21 -0400 | [diff] [blame] | 2112 | spin_lock(&ino->i_lock); |
Peng Tao | 9bf8748 | 2014-08-22 17:37:41 +0800 | [diff] [blame] | 2113 | } else { |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2114 | nfs4_stateid_copy(&stateid, &lo->plh_stateid); |
Peng Tao | 9bf8748 | 2014-08-22 17:37:41 +0800 | [diff] [blame] | 2115 | } |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 2116 | |
Jeff Layton | 9a4bf31 | 2015-12-10 10:41:58 -0500 | [diff] [blame] | 2117 | if (pnfs_layoutgets_blocked(lo)) { |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2118 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
Jeff Layton | 9a4bf31 | 2015-12-10 10:41:58 -0500 | [diff] [blame] | 2119 | PNFS_UPDATE_LAYOUT_BLOCKED); |
Fred Isaman | cf7d63f | 2011-01-06 11:36:25 +0000 | [diff] [blame] | 2120 | goto out_unlock; |
Jeff Layton | 9a4bf31 | 2015-12-10 10:41:58 -0500 | [diff] [blame] | 2121 | } |
Trond Myklebust | 411ae72 | 2018-06-23 22:54:33 -0400 | [diff] [blame] | 2122 | nfs_layoutget_begin(lo); |
Fred Isaman | f49f9ba | 2011-02-03 18:28:52 +0000 | [diff] [blame] | 2123 | spin_unlock(&ino->i_lock); |
Weston Andros Adamson | 3000512 | 2013-02-28 20:30:10 -0500 | [diff] [blame] | 2124 | |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 2125 | _add_to_server_list(lo, server); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 2126 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2127 | pg_offset = arg.offset & ~PAGE_MASK; |
Benny Halevy | 707ed5f | 2011-05-22 19:47:46 +0300 | [diff] [blame] | 2128 | if (pg_offset) { |
| 2129 | arg.offset -= pg_offset; |
| 2130 | arg.length += pg_offset; |
| 2131 | } |
Andy Adamson | 7c24d94 | 2011-06-13 18:22:38 -0400 | [diff] [blame] | 2132 | if (arg.length != NFS4_MAX_UINT64) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2133 | arg.length = PAGE_ALIGN(arg.length); |
Benny Halevy | 707ed5f | 2011-05-22 19:47:46 +0300 | [diff] [blame] | 2134 | |
Fred Isaman | 5e36e2a | 2016-10-06 12:08:51 -0400 | [diff] [blame] | 2135 | lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &stateid, &arg, gfp_flags); |
Fred Isaman | 587f03d | 2016-09-21 05:14:28 -0400 | [diff] [blame] | 2136 | if (!lgp) { |
Trond Myklebust | 3764a17 | 2022-05-14 10:08:11 -0400 | [diff] [blame] | 2137 | lseg = ERR_PTR(-ENOMEM); |
Fred Isaman | 587f03d | 2016-09-21 05:14:28 -0400 | [diff] [blame] | 2138 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, NULL, |
| 2139 | PNFS_UPDATE_LAYOUT_NOMEM); |
Trond Myklebust | 411ae72 | 2018-06-23 22:54:33 -0400 | [diff] [blame] | 2140 | nfs_layoutget_end(lo); |
Fred Isaman | 587f03d | 2016-09-21 05:14:28 -0400 | [diff] [blame] | 2141 | goto out_put_layout_hdr; |
| 2142 | } |
| 2143 | |
Trond Myklebust | b4e89bc | 2021-07-02 17:24:22 -0400 | [diff] [blame] | 2144 | lgp->lo = lo; |
| 2145 | pnfs_get_layout_hdr(lo); |
| 2146 | |
Fred Isaman | dacb452 | 2016-09-19 17:47:09 -0400 | [diff] [blame] | 2147 | lseg = nfs4_proc_layoutget(lgp, &timeout); |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2148 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
| 2149 | PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET); |
Trond Myklebust | 411ae72 | 2018-06-23 22:54:33 -0400 | [diff] [blame] | 2150 | nfs_layoutget_end(lo); |
Jeff Layton | 83026d80 | 2016-05-17 12:28:46 -0400 | [diff] [blame] | 2151 | if (IS_ERR(lseg)) { |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2152 | switch(PTR_ERR(lseg)) { |
Trond Myklebust | e85d7ee | 2016-07-14 18:46:24 -0400 | [diff] [blame] | 2153 | case -EBUSY: |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2154 | if (time_after(jiffies, giveup)) |
| 2155 | lseg = NULL; |
Trond Myklebust | 66b53f3 | 2016-07-14 14:28:31 -0400 | [diff] [blame] | 2156 | break; |
| 2157 | case -ERECALLCONFLICT: |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2158 | case -EAGAIN: |
Trond Myklebust | 56b38a1 | 2016-07-14 18:34:12 -0400 | [diff] [blame] | 2159 | break; |
Trond Myklebust | fe44fb2 | 2022-05-31 11:03:06 -0400 | [diff] [blame] | 2160 | case -ENODATA: |
| 2161 | /* The server returned NFS4ERR_LAYOUTUNAVAILABLE */ |
| 2162 | pnfs_layout_set_fail_bit( |
| 2163 | lo, pnfs_iomode_to_fail_bit(iomode)); |
| 2164 | lseg = NULL; |
| 2165 | goto out_put_layout_hdr; |
Jeff Layton | 183d9e7 | 2016-05-17 12:28:47 -0400 | [diff] [blame] | 2166 | default: |
| 2167 | if (!nfs_error_is_fatal(PTR_ERR(lseg))) { |
| 2168 | pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); |
| 2169 | lseg = NULL; |
| 2170 | } |
Trond Myklebust | 56b38a1 | 2016-07-14 18:34:12 -0400 | [diff] [blame] | 2171 | goto out_put_layout_hdr; |
| 2172 | } |
| 2173 | if (lseg) { |
| 2174 | if (first) |
| 2175 | pnfs_clear_first_layoutget(lo); |
| 2176 | trace_pnfs_update_layout(ino, pos, count, |
| 2177 | iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY); |
| 2178 | pnfs_put_layout_hdr(lo); |
| 2179 | goto lookup_again; |
Jeff Layton | 83026d80 | 2016-05-17 12:28:46 -0400 | [diff] [blame] | 2180 | } |
| 2181 | } else { |
| 2182 | pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); |
| 2183 | } |
| 2184 | |
Trond Myklebust | 830ffb5 | 2012-09-20 21:25:19 -0400 | [diff] [blame] | 2185 | out_put_layout_hdr: |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 2186 | if (first) |
| 2187 | pnfs_clear_first_layoutget(lo); |
Trond Myklebust | d5b9216 | 2019-07-18 09:32:17 -0400 | [diff] [blame] | 2188 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
| 2189 | PNFS_UPDATE_LAYOUT_EXIT); |
Trond Myklebust | 70c3bd2 | 2012-09-18 20:51:13 -0400 | [diff] [blame] | 2190 | pnfs_put_layout_hdr(lo); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 2191 | out: |
Trond Myklebust | f86bbcf | 2012-09-26 11:21:40 -0400 | [diff] [blame] | 2192 | dprintk("%s: inode %s/%llu pNFS layout segment %s for " |
| 2193 | "(%s, offset: %llu, length: %llu)\n", |
| 2194 | __func__, ino->i_sb->s_id, |
| 2195 | (unsigned long long)NFS_FILEID(ino), |
Peng Tao | d600ad1 | 2015-12-04 02:57:48 +0800 | [diff] [blame] | 2196 | IS_ERR_OR_NULL(lseg) ? "not found" : "found", |
Trond Myklebust | f86bbcf | 2012-09-26 11:21:40 -0400 | [diff] [blame] | 2197 | iomode==IOMODE_RW ? "read/write" : "read-only", |
| 2198 | (unsigned long long)pos, |
| 2199 | (unsigned long long)count); |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 2200 | return lseg; |
| 2201 | out_unlock: |
| 2202 | spin_unlock(&ino->i_lock); |
Trond Myklebust | 830ffb5 | 2012-09-20 21:25:19 -0400 | [diff] [blame] | 2203 | goto out_put_layout_hdr; |
Benny Halevy | e5e9401 | 2010-10-20 00:18:01 -0400 | [diff] [blame] | 2204 | } |
Andy Adamson | 7c24d94 | 2011-06-13 18:22:38 -0400 | [diff] [blame] | 2205 | EXPORT_SYMBOL_GPL(pnfs_update_layout); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 2206 | |
Trond Myklebust | 540d986 | 2015-08-25 11:16:13 -0400 | [diff] [blame] | 2207 | static bool |
| 2208 | pnfs_sanity_check_layout_range(struct pnfs_layout_range *range) |
| 2209 | { |
| 2210 | switch (range->iomode) { |
| 2211 | case IOMODE_READ: |
| 2212 | case IOMODE_RW: |
| 2213 | break; |
| 2214 | default: |
| 2215 | return false; |
| 2216 | } |
| 2217 | if (range->offset == NFS4_MAX_UINT64) |
| 2218 | return false; |
| 2219 | if (range->length == 0) |
| 2220 | return false; |
| 2221 | if (range->length != NFS4_MAX_UINT64 && |
| 2222 | range->length > NFS4_MAX_UINT64 - range->offset) |
| 2223 | return false; |
| 2224 | return true; |
| 2225 | } |
| 2226 | |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 2227 | static struct pnfs_layout_hdr * |
| 2228 | _pnfs_grab_empty_layout(struct inode *ino, struct nfs_open_context *ctx) |
| 2229 | { |
| 2230 | struct pnfs_layout_hdr *lo; |
| 2231 | |
| 2232 | spin_lock(&ino->i_lock); |
Trond Myklebust | 63d8a41 | 2022-03-21 15:32:09 -0400 | [diff] [blame] | 2233 | lo = pnfs_find_alloc_layout(ino, ctx, nfs_io_gfp_mask()); |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 2234 | if (!lo) |
| 2235 | goto out_unlock; |
| 2236 | if (!test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) |
| 2237 | goto out_unlock; |
| 2238 | if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) |
| 2239 | goto out_unlock; |
| 2240 | if (pnfs_layoutgets_blocked(lo)) |
| 2241 | goto out_unlock; |
| 2242 | if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags)) |
| 2243 | goto out_unlock; |
Trond Myklebust | 411ae72 | 2018-06-23 22:54:33 -0400 | [diff] [blame] | 2244 | nfs_layoutget_begin(lo); |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 2245 | spin_unlock(&ino->i_lock); |
| 2246 | _add_to_server_list(lo, NFS_SERVER(ino)); |
| 2247 | return lo; |
| 2248 | |
| 2249 | out_unlock: |
| 2250 | spin_unlock(&ino->i_lock); |
| 2251 | pnfs_put_layout_hdr(lo); |
| 2252 | return NULL; |
| 2253 | } |
| 2254 | |
Fred Isaman | 2409a97 | 2016-10-06 12:11:21 -0400 | [diff] [blame] | 2255 | static void _lgopen_prepare_attached(struct nfs4_opendata *data, |
| 2256 | struct nfs_open_context *ctx) |
| 2257 | { |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 2258 | struct inode *ino = data->dentry->d_inode; |
| 2259 | struct pnfs_layout_range rng = { |
| 2260 | .iomode = (data->o_arg.fmode & FMODE_WRITE) ? |
| 2261 | IOMODE_RW: IOMODE_READ, |
| 2262 | .offset = 0, |
| 2263 | .length = NFS4_MAX_UINT64, |
| 2264 | }; |
| 2265 | struct nfs4_layoutget *lgp; |
| 2266 | struct pnfs_layout_hdr *lo; |
| 2267 | |
Trond Myklebust | 64294b0 | 2017-02-02 12:26:38 -0500 | [diff] [blame] | 2268 | /* Heuristic: don't send layoutget if we have cached data */ |
| 2269 | if (rng.iomode == IOMODE_READ && |
| 2270 | (i_size_read(ino) == 0 || ino->i_mapping->nrpages != 0)) |
| 2271 | return; |
| 2272 | |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 2273 | lo = _pnfs_grab_empty_layout(ino, ctx); |
| 2274 | if (!lo) |
| 2275 | return; |
Trond Myklebust | 63d8a41 | 2022-03-21 15:32:09 -0400 | [diff] [blame] | 2276 | lgp = pnfs_alloc_init_layoutget_args(ino, ctx, ¤t_stateid, &rng, |
| 2277 | nfs_io_gfp_mask()); |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 2278 | if (!lgp) { |
| 2279 | pnfs_clear_first_layoutget(lo); |
Trond Myklebust | cb2856c | 2021-01-06 14:13:22 -0500 | [diff] [blame] | 2280 | nfs_layoutget_end(lo); |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 2281 | pnfs_put_layout_hdr(lo); |
| 2282 | return; |
| 2283 | } |
Trond Myklebust | b4e89bc | 2021-07-02 17:24:22 -0400 | [diff] [blame] | 2284 | lgp->lo = lo; |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 2285 | data->lgp = lgp; |
| 2286 | data->o_arg.lg_args = &lgp->args; |
| 2287 | data->o_res.lg_res = &lgp->res; |
Fred Isaman | 2409a97 | 2016-10-06 12:11:21 -0400 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | static void _lgopen_prepare_floating(struct nfs4_opendata *data, |
| 2291 | struct nfs_open_context *ctx) |
| 2292 | { |
Trond Myklebust | b4e89bc | 2021-07-02 17:24:22 -0400 | [diff] [blame] | 2293 | struct inode *ino = data->dentry->d_inode; |
Fred Isaman | 2409a97 | 2016-10-06 12:11:21 -0400 | [diff] [blame] | 2294 | struct pnfs_layout_range rng = { |
| 2295 | .iomode = (data->o_arg.fmode & FMODE_WRITE) ? |
| 2296 | IOMODE_RW: IOMODE_READ, |
| 2297 | .offset = 0, |
| 2298 | .length = NFS4_MAX_UINT64, |
| 2299 | }; |
| 2300 | struct nfs4_layoutget *lgp; |
| 2301 | |
Trond Myklebust | 63d8a41 | 2022-03-21 15:32:09 -0400 | [diff] [blame] | 2302 | lgp = pnfs_alloc_init_layoutget_args(ino, ctx, ¤t_stateid, &rng, |
| 2303 | nfs_io_gfp_mask()); |
Fred Isaman | 2409a97 | 2016-10-06 12:11:21 -0400 | [diff] [blame] | 2304 | if (!lgp) |
| 2305 | return; |
| 2306 | data->lgp = lgp; |
| 2307 | data->o_arg.lg_args = &lgp->args; |
| 2308 | data->o_res.lg_res = &lgp->res; |
| 2309 | } |
| 2310 | |
| 2311 | void pnfs_lgopen_prepare(struct nfs4_opendata *data, |
| 2312 | struct nfs_open_context *ctx) |
| 2313 | { |
| 2314 | struct nfs_server *server = NFS_SERVER(data->dir->d_inode); |
| 2315 | |
| 2316 | if (!(pnfs_enabled_sb(server) && |
| 2317 | server->pnfs_curr_ld->flags & PNFS_LAYOUTGET_ON_OPEN)) |
| 2318 | return; |
| 2319 | /* Could check on max_ops, but currently hardcoded high enough */ |
Fred Isaman | 6e01260 | 2016-10-04 15:26:41 -0400 | [diff] [blame] | 2320 | if (!nfs_server_capable(data->dir->d_inode, NFS_CAP_LGOPEN)) |
| 2321 | return; |
Trond Myklebust | b4e89bc | 2021-07-02 17:24:22 -0400 | [diff] [blame] | 2322 | if (data->lgp) |
| 2323 | return; |
Fred Isaman | 2409a97 | 2016-10-06 12:11:21 -0400 | [diff] [blame] | 2324 | if (data->state) |
| 2325 | _lgopen_prepare_attached(data, ctx); |
| 2326 | else |
| 2327 | _lgopen_prepare_floating(data, ctx); |
| 2328 | } |
| 2329 | |
| 2330 | void pnfs_parse_lgopen(struct inode *ino, struct nfs4_layoutget *lgp, |
| 2331 | struct nfs_open_context *ctx) |
| 2332 | { |
| 2333 | struct pnfs_layout_hdr *lo; |
| 2334 | struct pnfs_layout_segment *lseg; |
Fred Isaman | c49b520 | 2016-10-05 09:37:12 -0400 | [diff] [blame] | 2335 | struct nfs_server *srv = NFS_SERVER(ino); |
Fred Isaman | 2409a97 | 2016-10-06 12:11:21 -0400 | [diff] [blame] | 2336 | u32 iomode; |
| 2337 | |
Fred Isaman | 6e01260 | 2016-10-04 15:26:41 -0400 | [diff] [blame] | 2338 | if (!lgp) |
Fred Isaman | 2409a97 | 2016-10-06 12:11:21 -0400 | [diff] [blame] | 2339 | return; |
Fred Isaman | 6e01260 | 2016-10-04 15:26:41 -0400 | [diff] [blame] | 2340 | dprintk("%s: entered with status %i\n", __func__, lgp->res.status); |
| 2341 | if (lgp->res.status) { |
| 2342 | switch (lgp->res.status) { |
Fred Isaman | 6e01260 | 2016-10-04 15:26:41 -0400 | [diff] [blame] | 2343 | default: |
Trond Myklebust | 8dc9656 | 2017-02-01 21:02:07 -0500 | [diff] [blame] | 2344 | break; |
| 2345 | /* |
| 2346 | * Halt lgopen attempts if the server doesn't recognise |
| 2347 | * the "current stateid" value, the layout type, or the |
| 2348 | * layoutget operation as being valid. |
| 2349 | * Also if it complains about too many ops in the compound |
| 2350 | * or of the request/reply being too big. |
| 2351 | */ |
| 2352 | case -NFS4ERR_BAD_STATEID: |
| 2353 | case -NFS4ERR_NOTSUPP: |
| 2354 | case -NFS4ERR_REP_TOO_BIG: |
| 2355 | case -NFS4ERR_REP_TOO_BIG_TO_CACHE: |
| 2356 | case -NFS4ERR_REQ_TOO_BIG: |
| 2357 | case -NFS4ERR_TOO_MANY_OPS: |
| 2358 | case -NFS4ERR_UNKNOWN_LAYOUTTYPE: |
Fred Isaman | c49b520 | 2016-10-05 09:37:12 -0400 | [diff] [blame] | 2359 | srv->caps &= ~NFS_CAP_LGOPEN; |
Fred Isaman | 6e01260 | 2016-10-04 15:26:41 -0400 | [diff] [blame] | 2360 | } |
| 2361 | return; |
| 2362 | } |
Trond Myklebust | b4e89bc | 2021-07-02 17:24:22 -0400 | [diff] [blame] | 2363 | if (!lgp->lo) { |
Fred Isaman | 78746a3 | 2016-09-22 12:30:20 -0400 | [diff] [blame] | 2364 | lo = _pnfs_grab_empty_layout(ino, ctx); |
| 2365 | if (!lo) |
| 2366 | return; |
Trond Myklebust | b4e89bc | 2021-07-02 17:24:22 -0400 | [diff] [blame] | 2367 | lgp->lo = lo; |
Fred Isaman | 2409a97 | 2016-10-06 12:11:21 -0400 | [diff] [blame] | 2368 | } else |
Trond Myklebust | b4e89bc | 2021-07-02 17:24:22 -0400 | [diff] [blame] | 2369 | lo = lgp->lo; |
Fred Isaman | 2409a97 | 2016-10-06 12:11:21 -0400 | [diff] [blame] | 2370 | |
| 2371 | lseg = pnfs_layout_process(lgp); |
Trond Myklebust | 32f1c28 | 2018-05-22 11:15:32 -0400 | [diff] [blame] | 2372 | if (!IS_ERR(lseg)) { |
Fred Isaman | 2409a97 | 2016-10-06 12:11:21 -0400 | [diff] [blame] | 2373 | iomode = lgp->args.range.iomode; |
| 2374 | pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); |
| 2375 | pnfs_put_lseg(lseg); |
| 2376 | } |
Fred Isaman | 30ae241 | 2016-10-18 13:39:51 -0400 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | void nfs4_lgopen_release(struct nfs4_layoutget *lgp) |
| 2380 | { |
| 2381 | if (lgp != NULL) { |
Trond Myklebust | b4e89bc | 2021-07-02 17:24:22 -0400 | [diff] [blame] | 2382 | if (lgp->lo) { |
| 2383 | pnfs_clear_first_layoutget(lgp->lo); |
| 2384 | nfs_layoutget_end(lgp->lo); |
Fred Isaman | 30ae241 | 2016-10-18 13:39:51 -0400 | [diff] [blame] | 2385 | } |
| 2386 | pnfs_layoutget_free(lgp); |
| 2387 | } |
Fred Isaman | 2409a97 | 2016-10-06 12:11:21 -0400 | [diff] [blame] | 2388 | } |
| 2389 | |
Trond Myklebust | a0b0a6e | 2012-09-17 17:12:15 -0400 | [diff] [blame] | 2390 | struct pnfs_layout_segment * |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 2391 | pnfs_layout_process(struct nfs4_layoutget *lgp) |
| 2392 | { |
Trond Myklebust | b4e89bc | 2021-07-02 17:24:22 -0400 | [diff] [blame] | 2393 | struct pnfs_layout_hdr *lo = lgp->lo; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 2394 | struct nfs4_layoutget_res *res = &lgp->res; |
| 2395 | struct pnfs_layout_segment *lseg; |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 2396 | struct inode *ino = lo->plh_inode; |
Trond Myklebust | 78096cc | 2014-02-12 10:02:27 -0500 | [diff] [blame] | 2397 | LIST_HEAD(free_me); |
Trond Myklebust | 540d986 | 2015-08-25 11:16:13 -0400 | [diff] [blame] | 2398 | |
| 2399 | if (!pnfs_sanity_check_layout_range(&res->range)) |
Jeff Layton | 1b3c6d0 | 2016-05-17 12:28:48 -0400 | [diff] [blame] | 2400 | return ERR_PTR(-EINVAL); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 2401 | |
| 2402 | /* Inject layout blob into I/O device driver */ |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 2403 | 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] | 2404 | if (IS_ERR_OR_NULL(lseg)) { |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 2405 | if (!lseg) |
Jeff Layton | 1b3c6d0 | 2016-05-17 12:28:48 -0400 | [diff] [blame] | 2406 | lseg = ERR_PTR(-ENOMEM); |
| 2407 | |
| 2408 | dprintk("%s: Could not allocate layout: error %ld\n", |
| 2409 | __func__, PTR_ERR(lseg)); |
| 2410 | return lseg; |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 2411 | } |
| 2412 | |
Trond Myklebust | 119cef97 | 2016-07-24 15:10:12 -0400 | [diff] [blame] | 2413 | pnfs_init_lseg(lo, lseg, &res->range, &res->stateid); |
Christoph Hellwig | 1013df61 | 2014-08-21 11:09:18 -0500 | [diff] [blame] | 2414 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 2415 | spin_lock(&ino->i_lock); |
Trond Myklebust | e1c06f8 | 2015-08-04 16:40:08 -0400 | [diff] [blame] | 2416 | if (pnfs_layoutgets_blocked(lo)) { |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 2417 | dprintk("%s forget reply due to state\n", __func__); |
Jeff Layton | 1b3c6d0 | 2016-05-17 12:28:48 -0400 | [diff] [blame] | 2418 | goto out_forget; |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 2419 | } |
Trond Myklebust | 038d649 | 2012-10-02 16:38:41 -0700 | [diff] [blame] | 2420 | |
Trond Myklebust | 880265c | 2022-05-31 11:03:07 -0400 | [diff] [blame] | 2421 | if (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) && |
| 2422 | !pnfs_is_first_layoutget(lo)) |
Trond Myklebust | 0b77f97 | 2021-07-02 19:48:41 -0400 | [diff] [blame] | 2423 | goto out_forget; |
| 2424 | |
Trond Myklebust | d29b468d | 2021-01-22 10:05:51 -0500 | [diff] [blame] | 2425 | if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) { |
Christoph Hellwig | 362f747 | 2014-08-21 11:09:20 -0500 | [diff] [blame] | 2426 | /* existing state ID, make sure the sequence number matches. */ |
| 2427 | if (pnfs_layout_stateid_blocked(lo, &res->stateid)) { |
Trond Myklebust | 0b77f97 | 2021-07-02 19:48:41 -0400 | [diff] [blame] | 2428 | if (!pnfs_layout_is_valid(lo)) |
Trond Myklebust | d29b468d | 2021-01-22 10:05:51 -0500 | [diff] [blame] | 2429 | lo->plh_barrier = 0; |
Christoph Hellwig | 362f747 | 2014-08-21 11:09:20 -0500 | [diff] [blame] | 2430 | dprintk("%s forget reply due to sequence\n", __func__); |
Jeff Layton | 1b3c6d0 | 2016-05-17 12:28:48 -0400 | [diff] [blame] | 2431 | goto out_forget; |
Christoph Hellwig | 362f747 | 2014-08-21 11:09:20 -0500 | [diff] [blame] | 2432 | } |
Trond Myklebust | 59b5639 | 2020-01-27 13:07:26 -0500 | [diff] [blame] | 2433 | pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, false); |
Trond Myklebust | d29b468d | 2021-01-22 10:05:51 -0500 | [diff] [blame] | 2434 | } else if (pnfs_layout_is_valid(lo)) { |
Christoph Hellwig | 362f747 | 2014-08-21 11:09:20 -0500 | [diff] [blame] | 2435 | /* |
| 2436 | * We got an entirely new state ID. Mark all segments for the |
Trond Myklebust | 9888d83 | 2016-11-23 12:36:04 -0500 | [diff] [blame] | 2437 | * inode invalid, and retry the layoutget |
Christoph Hellwig | 362f747 | 2014-08-21 11:09:20 -0500 | [diff] [blame] | 2438 | */ |
Trond Myklebust | 08bd8db | 2021-01-21 17:11:42 -0500 | [diff] [blame] | 2439 | struct pnfs_layout_range range = { |
| 2440 | .iomode = IOMODE_ANY, |
| 2441 | .length = NFS4_MAX_UINT64, |
| 2442 | }; |
Trond Myklebust | fb700ef | 2021-04-15 17:33:07 -0400 | [diff] [blame] | 2443 | pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0); |
Trond Myklebust | 9888d83 | 2016-11-23 12:36:04 -0500 | [diff] [blame] | 2444 | goto out_forget; |
Trond Myklebust | d29b468d | 2021-01-22 10:05:51 -0500 | [diff] [blame] | 2445 | } else { |
| 2446 | /* We have a completely new layout */ |
Trond Myklebust | d29b468d | 2021-01-22 10:05:51 -0500 | [diff] [blame] | 2447 | pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, true); |
Christoph Hellwig | 362f747 | 2014-08-21 11:09:20 -0500 | [diff] [blame] | 2448 | } |
Trond Myklebust | 038d649 | 2012-10-02 16:38:41 -0700 | [diff] [blame] | 2449 | |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 2450 | pnfs_get_lseg(lseg); |
Trond Myklebust | 03772d2 | 2015-08-25 08:54:17 -0400 | [diff] [blame] | 2451 | pnfs_layout_insert_lseg(lo, lseg, &free_me); |
Trond Myklebust | 8e0acf9 | 2016-07-21 11:53:29 -0400 | [diff] [blame] | 2452 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 2453 | |
Peng Tao | 3976143 | 2015-08-21 12:49:44 +0800 | [diff] [blame] | 2454 | if (res->return_on_close) |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 2455 | set_bit(NFS_LSEG_ROC, &lseg->pls_flags); |
Fred Isaman | f7e8917 | 2011-01-06 11:36:32 +0000 | [diff] [blame] | 2456 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 2457 | spin_unlock(&ino->i_lock); |
Trond Myklebust | 78096cc | 2014-02-12 10:02:27 -0500 | [diff] [blame] | 2458 | pnfs_free_lseg_list(&free_me); |
Trond Myklebust | a0b0a6e | 2012-09-17 17:12:15 -0400 | [diff] [blame] | 2459 | return lseg; |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 2460 | |
Jeff Layton | 1b3c6d0 | 2016-05-17 12:28:48 -0400 | [diff] [blame] | 2461 | out_forget: |
Fred Isaman | 43f1b3d | 2011-01-06 11:36:30 +0000 | [diff] [blame] | 2462 | spin_unlock(&ino->i_lock); |
| 2463 | lseg->pls_layout = lo; |
| 2464 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
Jeff Layton | 1b3c6d0 | 2016-05-17 12:28:48 -0400 | [diff] [blame] | 2465 | return ERR_PTR(-EAGAIN); |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 2466 | } |
| 2467 | |
Trond Myklebust | 2f21596 | 2016-02-15 12:36:04 -0500 | [diff] [blame] | 2468 | /** |
| 2469 | * pnfs_mark_matching_lsegs_return - Free or return matching layout segments |
| 2470 | * @lo: pointer to layout header |
| 2471 | * @tmp_list: list header to be used with pnfs_free_lseg_list() |
| 2472 | * @return_range: describe layout segment ranges to be returned |
Trond Myklebust | e0b7d42 | 2018-06-23 10:28:40 -0400 | [diff] [blame] | 2473 | * @seq: stateid seqid to match |
Trond Myklebust | 2f21596 | 2016-02-15 12:36:04 -0500 | [diff] [blame] | 2474 | * |
| 2475 | * This function is mainly intended for use by layoutrecall. It attempts |
| 2476 | * to free the layout segment immediately, or else to mark it for return |
| 2477 | * as soon as its reference count drops to zero. |
Trond Myklebust | e0b7d42 | 2018-06-23 10:28:40 -0400 | [diff] [blame] | 2478 | * |
| 2479 | * Returns |
| 2480 | * - 0: a layoutreturn needs to be scheduled. |
| 2481 | * - EBUSY: there are layout segment that are still in use. |
| 2482 | * - ENOENT: there are no layout segments that need to be returned. |
Trond Myklebust | 2f21596 | 2016-02-15 12:36:04 -0500 | [diff] [blame] | 2483 | */ |
Trond Myklebust | 1033555 | 2016-01-04 11:23:52 -0500 | [diff] [blame] | 2484 | int |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2485 | pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo, |
| 2486 | struct list_head *tmp_list, |
Jeff Layton | 6d597e1 | 2016-05-17 12:28:42 -0400 | [diff] [blame] | 2487 | const struct pnfs_layout_range *return_range, |
| 2488 | u32 seq) |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2489 | { |
| 2490 | struct pnfs_layout_segment *lseg, *next; |
Trond Myklebust | b739a5b | 2022-10-05 15:57:38 -0400 | [diff] [blame] | 2491 | struct nfs_server *server = NFS_SERVER(lo->plh_inode); |
Trond Myklebust | 1033555 | 2016-01-04 11:23:52 -0500 | [diff] [blame] | 2492 | int remaining = 0; |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2493 | |
| 2494 | dprintk("%s:Begin lo %p\n", __func__, lo); |
| 2495 | |
Trond Myklebust | fc7ff36 | 2015-12-28 10:28:59 -0500 | [diff] [blame] | 2496 | assert_spin_locked(&lo->plh_inode->i_lock); |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2497 | |
Trond Myklebust | 39fd018 | 2021-04-15 15:41:57 -0400 | [diff] [blame] | 2498 | if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) |
| 2499 | tmp_list = &lo->plh_return_segs; |
| 2500 | |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2501 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) |
Trond Myklebust | e036f46 | 2016-07-22 11:13:22 -0400 | [diff] [blame] | 2502 | if (pnfs_match_lseg_recall(lseg, return_range, seq)) { |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2503 | dprintk("%s: marking lseg %p iomode %d " |
| 2504 | "offset %llu length %llu\n", __func__, |
| 2505 | lseg, lseg->pls_range.iomode, |
| 2506 | lseg->pls_range.offset, |
| 2507 | lseg->pls_range.length); |
Trond Myklebust | 39fd018 | 2021-04-15 15:41:57 -0400 | [diff] [blame] | 2508 | if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags)) |
| 2509 | tmp_list = &lo->plh_return_segs; |
Trond Myklebust | ff04172 | 2020-08-04 16:30:30 -0400 | [diff] [blame] | 2510 | if (mark_lseg_invalid(lseg, tmp_list)) |
Trond Myklebust | 2f21596 | 2016-02-15 12:36:04 -0500 | [diff] [blame] | 2511 | continue; |
| 2512 | remaining++; |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2513 | set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags); |
Trond Myklebust | b739a5b | 2022-10-05 15:57:38 -0400 | [diff] [blame] | 2514 | pnfs_lseg_cancel_io(server, lseg); |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2515 | } |
Jeff Layton | 6d597e1 | 2016-05-17 12:28:42 -0400 | [diff] [blame] | 2516 | |
Trond Myklebust | e0b7d42 | 2018-06-23 10:28:40 -0400 | [diff] [blame] | 2517 | if (remaining) { |
Jeff Layton | 6d597e1 | 2016-05-17 12:28:42 -0400 | [diff] [blame] | 2518 | pnfs_set_plh_return_info(lo, return_range->iomode, seq); |
Trond Myklebust | e0b7d42 | 2018-06-23 10:28:40 -0400 | [diff] [blame] | 2519 | return -EBUSY; |
| 2520 | } |
Jeff Layton | 6d597e1 | 2016-05-17 12:28:42 -0400 | [diff] [blame] | 2521 | |
Trond Myklebust | e0b7d42 | 2018-06-23 10:28:40 -0400 | [diff] [blame] | 2522 | if (!list_empty(&lo->plh_return_segs)) { |
| 2523 | pnfs_set_plh_return_info(lo, return_range->iomode, seq); |
| 2524 | return 0; |
| 2525 | } |
| 2526 | |
| 2527 | return -ENOENT; |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2528 | } |
| 2529 | |
Trond Myklebust | b5fdf84 | 2020-02-18 15:58:31 -0500 | [diff] [blame] | 2530 | static void |
| 2531 | pnfs_mark_layout_for_return(struct inode *inode, |
| 2532 | const struct pnfs_layout_range *range) |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2533 | { |
Trond Myklebust | b5fdf84 | 2020-02-18 15:58:31 -0500 | [diff] [blame] | 2534 | struct pnfs_layout_hdr *lo; |
Trond Myklebust | 1033555 | 2016-01-04 11:23:52 -0500 | [diff] [blame] | 2535 | bool return_now = false; |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2536 | |
| 2537 | spin_lock(&inode->i_lock); |
Trond Myklebust | b5fdf84 | 2020-02-18 15:58:31 -0500 | [diff] [blame] | 2538 | lo = NFS_I(inode)->layout; |
Trond Myklebust | bdebfcc | 2017-04-27 15:30:00 -0400 | [diff] [blame] | 2539 | if (!pnfs_layout_is_valid(lo)) { |
| 2540 | spin_unlock(&inode->i_lock); |
| 2541 | return; |
| 2542 | } |
Trond Myklebust | b5fdf84 | 2020-02-18 15:58:31 -0500 | [diff] [blame] | 2543 | pnfs_set_plh_return_info(lo, range->iomode, 0); |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2544 | /* |
| 2545 | * mark all matching lsegs so that we are sure to have no live |
| 2546 | * segments at hand when sending layoutreturn. See pnfs_put_lseg() |
| 2547 | * for how it works. |
| 2548 | */ |
Trond Myklebust | b5fdf84 | 2020-02-18 15:58:31 -0500 | [diff] [blame] | 2549 | if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, range, 0) != -EBUSY) { |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 2550 | const struct cred *cred; |
Trond Myklebust | 1033555 | 2016-01-04 11:23:52 -0500 | [diff] [blame] | 2551 | nfs4_stateid stateid; |
Trond Myklebust | e5fd190 | 2016-07-21 12:44:15 -0400 | [diff] [blame] | 2552 | enum pnfs_iomode iomode; |
Trond Myklebust | 1033555 | 2016-01-04 11:23:52 -0500 | [diff] [blame] | 2553 | |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 2554 | return_now = pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode); |
Trond Myklebust | 1033555 | 2016-01-04 11:23:52 -0500 | [diff] [blame] | 2555 | spin_unlock(&inode->i_lock); |
| 2556 | if (return_now) |
Trond Myklebust | 44ea8df | 2020-04-02 15:37:02 -0400 | [diff] [blame] | 2557 | pnfs_send_layoutreturn(lo, &stateid, &cred, iomode, false); |
Trond Myklebust | 1033555 | 2016-01-04 11:23:52 -0500 | [diff] [blame] | 2558 | } else { |
| 2559 | spin_unlock(&inode->i_lock); |
| 2560 | nfs_commit_inode(inode, 0); |
| 2561 | } |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2562 | } |
Trond Myklebust | b5fdf84 | 2020-02-18 15:58:31 -0500 | [diff] [blame] | 2563 | |
| 2564 | void pnfs_error_mark_layout_for_return(struct inode *inode, |
| 2565 | struct pnfs_layout_segment *lseg) |
| 2566 | { |
| 2567 | struct pnfs_layout_range range = { |
| 2568 | .iomode = lseg->pls_range.iomode, |
| 2569 | .offset = 0, |
| 2570 | .length = NFS4_MAX_UINT64, |
| 2571 | }; |
| 2572 | |
| 2573 | pnfs_mark_layout_for_return(inode, &range); |
| 2574 | } |
Peng Tao | 016256d | 2014-09-06 00:53:23 +0800 | [diff] [blame] | 2575 | EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return); |
| 2576 | |
Trond Myklebust | b5fdf84 | 2020-02-18 15:58:31 -0500 | [diff] [blame] | 2577 | static bool |
| 2578 | pnfs_layout_can_be_returned(struct pnfs_layout_hdr *lo) |
| 2579 | { |
| 2580 | return pnfs_layout_is_valid(lo) && |
| 2581 | !test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) && |
| 2582 | !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags); |
| 2583 | } |
| 2584 | |
| 2585 | static struct pnfs_layout_segment * |
| 2586 | pnfs_find_first_lseg(struct pnfs_layout_hdr *lo, |
| 2587 | const struct pnfs_layout_range *range, |
| 2588 | enum pnfs_iomode iomode) |
| 2589 | { |
| 2590 | struct pnfs_layout_segment *lseg; |
| 2591 | |
| 2592 | list_for_each_entry(lseg, &lo->plh_segs, pls_list) { |
| 2593 | if (!test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) |
| 2594 | continue; |
| 2595 | if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags)) |
| 2596 | continue; |
| 2597 | if (lseg->pls_range.iomode != iomode && iomode != IOMODE_ANY) |
| 2598 | continue; |
| 2599 | if (pnfs_lseg_range_intersecting(&lseg->pls_range, range)) |
| 2600 | return lseg; |
| 2601 | } |
| 2602 | return NULL; |
| 2603 | } |
| 2604 | |
| 2605 | /* Find open file states whose mode matches that of the range */ |
| 2606 | static bool |
| 2607 | pnfs_should_return_unused_layout(struct pnfs_layout_hdr *lo, |
| 2608 | const struct pnfs_layout_range *range) |
| 2609 | { |
| 2610 | struct list_head *head; |
| 2611 | struct nfs_open_context *ctx; |
| 2612 | fmode_t mode = 0; |
| 2613 | |
| 2614 | if (!pnfs_layout_can_be_returned(lo) || |
| 2615 | !pnfs_find_first_lseg(lo, range, range->iomode)) |
| 2616 | return false; |
| 2617 | |
| 2618 | head = &NFS_I(lo->plh_inode)->open_files; |
| 2619 | list_for_each_entry_rcu(ctx, head, list) { |
| 2620 | if (ctx->state) |
| 2621 | mode |= ctx->state->state & (FMODE_READ|FMODE_WRITE); |
| 2622 | } |
| 2623 | |
| 2624 | switch (range->iomode) { |
| 2625 | default: |
| 2626 | break; |
| 2627 | case IOMODE_READ: |
| 2628 | mode &= ~FMODE_WRITE; |
| 2629 | break; |
| 2630 | case IOMODE_RW: |
| 2631 | if (pnfs_find_first_lseg(lo, range, IOMODE_READ)) |
| 2632 | mode &= ~FMODE_READ; |
| 2633 | } |
| 2634 | return mode == 0; |
| 2635 | } |
| 2636 | |
| 2637 | static int |
| 2638 | pnfs_layout_return_unused_byserver(struct nfs_server *server, void *data) |
| 2639 | { |
| 2640 | const struct pnfs_layout_range *range = data; |
| 2641 | struct pnfs_layout_hdr *lo; |
| 2642 | struct inode *inode; |
| 2643 | restart: |
| 2644 | rcu_read_lock(); |
| 2645 | list_for_each_entry_rcu(lo, &server->layouts, plh_layouts) { |
| 2646 | if (!pnfs_layout_can_be_returned(lo) || |
| 2647 | test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) |
| 2648 | continue; |
| 2649 | inode = lo->plh_inode; |
| 2650 | spin_lock(&inode->i_lock); |
| 2651 | if (!pnfs_should_return_unused_layout(lo, range)) { |
| 2652 | spin_unlock(&inode->i_lock); |
| 2653 | continue; |
| 2654 | } |
| 2655 | spin_unlock(&inode->i_lock); |
| 2656 | inode = pnfs_grab_inode_layout_hdr(lo); |
| 2657 | if (!inode) |
| 2658 | continue; |
| 2659 | rcu_read_unlock(); |
| 2660 | pnfs_mark_layout_for_return(inode, range); |
| 2661 | iput(inode); |
| 2662 | cond_resched(); |
| 2663 | goto restart; |
| 2664 | } |
| 2665 | rcu_read_unlock(); |
| 2666 | return 0; |
| 2667 | } |
| 2668 | |
| 2669 | void |
| 2670 | pnfs_layout_return_unused_byclid(struct nfs_client *clp, |
| 2671 | enum pnfs_iomode iomode) |
| 2672 | { |
| 2673 | struct pnfs_layout_range range = { |
| 2674 | .iomode = iomode, |
| 2675 | .offset = 0, |
| 2676 | .length = NFS4_MAX_UINT64, |
| 2677 | }; |
| 2678 | |
| 2679 | nfs_client_for_each_server(clp, pnfs_layout_return_unused_byserver, |
| 2680 | &range); |
| 2681 | } |
| 2682 | |
Trond Myklebust | d8007d4 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 2683 | void |
Trond Myklebust | b3230e8 | 2017-04-25 10:56:19 -0400 | [diff] [blame] | 2684 | pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor *pgio) |
| 2685 | { |
| 2686 | if (pgio->pg_lseg == NULL || |
| 2687 | test_bit(NFS_LSEG_VALID, &pgio->pg_lseg->pls_flags)) |
| 2688 | return; |
| 2689 | pnfs_put_lseg(pgio->pg_lseg); |
| 2690 | pgio->pg_lseg = NULL; |
| 2691 | } |
| 2692 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_layout); |
| 2693 | |
Benjamin Coddington | 08cb5b0 | 2017-05-22 20:20:23 -0400 | [diff] [blame] | 2694 | /* |
| 2695 | * Check for any intersection between the request and the pgio->pg_lseg, |
| 2696 | * and if none, put this pgio->pg_lseg away. |
| 2697 | */ |
Trond Myklebust | e1e54ab | 2020-03-23 14:48:23 -0400 | [diff] [blame] | 2698 | void |
Benjamin Coddington | 08cb5b0 | 2017-05-22 20:20:23 -0400 | [diff] [blame] | 2699 | pnfs_generic_pg_check_range(struct nfs_pageio_descriptor *pgio, struct nfs_page *req) |
| 2700 | { |
| 2701 | if (pgio->pg_lseg && !pnfs_lseg_request_intersecting(pgio->pg_lseg, req)) { |
| 2702 | pnfs_put_lseg(pgio->pg_lseg); |
| 2703 | pgio->pg_lseg = NULL; |
| 2704 | } |
| 2705 | } |
Trond Myklebust | e1e54ab | 2020-03-23 14:48:23 -0400 | [diff] [blame] | 2706 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_range); |
Benjamin Coddington | 08cb5b0 | 2017-05-22 20:20:23 -0400 | [diff] [blame] | 2707 | |
Trond Myklebust | b3230e8 | 2017-04-25 10:56:19 -0400 | [diff] [blame] | 2708 | void |
Trond Myklebust | d8007d4 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 2709 | pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req) |
| 2710 | { |
Yang Li | d1d9739 | 2021-05-12 17:20:04 +0800 | [diff] [blame] | 2711 | u64 rd_size; |
Peng Tao | 1fd937b | 2012-09-25 14:55:57 +0800 | [diff] [blame] | 2712 | |
Trond Myklebust | b3230e8 | 2017-04-25 10:56:19 -0400 | [diff] [blame] | 2713 | pnfs_generic_pg_check_layout(pgio); |
Benjamin Coddington | 08cb5b0 | 2017-05-22 20:20:23 -0400 | [diff] [blame] | 2714 | pnfs_generic_pg_check_range(pgio, req); |
Peng Tao | cb5d04b | 2015-01-24 22:14:52 +0800 | [diff] [blame] | 2715 | if (pgio->pg_lseg == NULL) { |
| 2716 | if (pgio->pg_dreq == NULL) |
| 2717 | rd_size = i_size_read(pgio->pg_inode) - req_offset(req); |
| 2718 | else |
| 2719 | rd_size = nfs_dreq_bytes_left(pgio->pg_dreq); |
Trond Myklebust | d8007d4 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 2720 | |
Trond Myklebust | 63d8a41 | 2022-03-21 15:32:09 -0400 | [diff] [blame] | 2721 | pgio->pg_lseg = |
| 2722 | pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req), |
| 2723 | req_offset(req), rd_size, |
| 2724 | IOMODE_READ, false, |
| 2725 | nfs_io_gfp_mask()); |
Peng Tao | d600ad1 | 2015-12-04 02:57:48 +0800 | [diff] [blame] | 2726 | if (IS_ERR(pgio->pg_lseg)) { |
| 2727 | pgio->pg_error = PTR_ERR(pgio->pg_lseg); |
| 2728 | pgio->pg_lseg = NULL; |
| 2729 | return; |
| 2730 | } |
Peng Tao | cb5d04b | 2015-01-24 22:14:52 +0800 | [diff] [blame] | 2731 | } |
Trond Myklebust | e885de1 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 2732 | /* If no lseg, fall back to read through mds */ |
| 2733 | if (pgio->pg_lseg == NULL) |
Trond Myklebust | 1f94535 | 2011-07-13 15:59:57 -0400 | [diff] [blame] | 2734 | nfs_pageio_reset_read_mds(pgio); |
Trond Myklebust | e885de1 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 2735 | |
Trond Myklebust | d8007d4 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 2736 | } |
| 2737 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read); |
| 2738 | |
| 2739 | void |
Peng Tao | 6296556 | 2012-09-25 14:55:57 +0800 | [diff] [blame] | 2740 | pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, |
| 2741 | struct nfs_page *req, u64 wb_size) |
Trond Myklebust | d8007d4 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 2742 | { |
Trond Myklebust | b3230e8 | 2017-04-25 10:56:19 -0400 | [diff] [blame] | 2743 | pnfs_generic_pg_check_layout(pgio); |
Benjamin Coddington | 08cb5b0 | 2017-05-22 20:20:23 -0400 | [diff] [blame] | 2744 | pnfs_generic_pg_check_range(pgio, req); |
Peng Tao | d600ad1 | 2015-12-04 02:57:48 +0800 | [diff] [blame] | 2745 | if (pgio->pg_lseg == NULL) { |
Trond Myklebust | 63d8a41 | 2022-03-21 15:32:09 -0400 | [diff] [blame] | 2746 | pgio->pg_lseg = |
| 2747 | pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req), |
| 2748 | req_offset(req), wb_size, IOMODE_RW, |
| 2749 | false, nfs_io_gfp_mask()); |
Peng Tao | d600ad1 | 2015-12-04 02:57:48 +0800 | [diff] [blame] | 2750 | if (IS_ERR(pgio->pg_lseg)) { |
| 2751 | pgio->pg_error = PTR_ERR(pgio->pg_lseg); |
| 2752 | pgio->pg_lseg = NULL; |
| 2753 | return; |
| 2754 | } |
| 2755 | } |
Trond Myklebust | e885de1 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 2756 | /* If no lseg, fall back to write through mds */ |
| 2757 | if (pgio->pg_lseg == NULL) |
Trond Myklebust | 1f94535 | 2011-07-13 15:59:57 -0400 | [diff] [blame] | 2758 | nfs_pageio_reset_write_mds(pgio); |
Trond Myklebust | d8007d4 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 2759 | } |
| 2760 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write); |
| 2761 | |
Weston Andros Adamson | 180bb5e | 2014-09-10 15:48:01 -0400 | [diff] [blame] | 2762 | void |
| 2763 | pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc) |
| 2764 | { |
| 2765 | if (desc->pg_lseg) { |
| 2766 | pnfs_put_lseg(desc->pg_lseg); |
| 2767 | desc->pg_lseg = NULL; |
| 2768 | } |
| 2769 | } |
| 2770 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup); |
| 2771 | |
Weston Andros Adamson | b4fdac1 | 2014-05-15 11:56:43 -0400 | [diff] [blame] | 2772 | /* |
| 2773 | * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number |
| 2774 | * of bytes (maximum @req->wb_bytes) that can be coalesced. |
| 2775 | */ |
| 2776 | size_t |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 2777 | pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, |
| 2778 | struct nfs_page *prev, struct nfs_page *req) |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 2779 | { |
Weston Andros Adamson | 0f9c429e | 2014-05-15 11:56:51 -0400 | [diff] [blame] | 2780 | unsigned int size; |
Weston Andros Adamson | c5e20cb | 2014-06-09 17:47:26 -0400 | [diff] [blame] | 2781 | u64 seg_end, req_start, seg_left; |
Weston Andros Adamson | 0f9c429e | 2014-05-15 11:56:51 -0400 | [diff] [blame] | 2782 | |
| 2783 | size = nfs_generic_pg_test(pgio, prev, req); |
Weston Andros Adamson | 0f9c429e | 2014-05-15 11:56:51 -0400 | [diff] [blame] | 2784 | if (!size) |
| 2785 | return 0; |
Benny Halevy | 89a58e3 | 2011-05-25 20:54:40 +0300 | [diff] [blame] | 2786 | |
Trond Myklebust | 19982ba | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 2787 | /* |
Weston Andros Adamson | c5e20cb | 2014-06-09 17:47:26 -0400 | [diff] [blame] | 2788 | * 'size' contains the number of bytes left in the current page (up |
| 2789 | * to the original size asked for in @req->wb_bytes). |
| 2790 | * |
| 2791 | * Calculate how many bytes are left in the layout segment |
| 2792 | * and if there are less bytes than 'size', return that instead. |
Trond Myklebust | 19982ba | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 2793 | * |
| 2794 | * Please also note that 'end_offset' is actually the offset of the |
| 2795 | * first byte that lies outside the pnfs_layout_range. FIXME? |
| 2796 | * |
| 2797 | */ |
Weston Andros Adamson | 19b5484 | 2014-05-15 11:56:55 -0400 | [diff] [blame] | 2798 | if (pgio->pg_lseg) { |
Trond Myklebust | 17822b2 | 2016-10-25 12:24:25 -0400 | [diff] [blame] | 2799 | seg_end = pnfs_end_offset(pgio->pg_lseg->pls_range.offset, |
Weston Andros Adamson | c5e20cb | 2014-06-09 17:47:26 -0400 | [diff] [blame] | 2800 | pgio->pg_lseg->pls_range.length); |
| 2801 | req_start = req_offset(req); |
Benjamin Coddington | 08cb5b0 | 2017-05-22 20:20:23 -0400 | [diff] [blame] | 2802 | |
Weston Andros Adamson | c5e20cb | 2014-06-09 17:47:26 -0400 | [diff] [blame] | 2803 | /* start of request is past the last byte of this segment */ |
Benjamin Coddington | 08cb5b0 | 2017-05-22 20:20:23 -0400 | [diff] [blame] | 2804 | if (req_start >= seg_end) |
Weston Andros Adamson | 19b5484 | 2014-05-15 11:56:55 -0400 | [diff] [blame] | 2805 | return 0; |
Weston Andros Adamson | c5e20cb | 2014-06-09 17:47:26 -0400 | [diff] [blame] | 2806 | |
| 2807 | /* adjust 'size' iff there are fewer bytes left in the |
| 2808 | * segment than what nfs_generic_pg_test returned */ |
| 2809 | seg_left = seg_end - req_start; |
| 2810 | if (seg_left < size) |
| 2811 | size = (unsigned int)seg_left; |
Weston Andros Adamson | 19b5484 | 2014-05-15 11:56:55 -0400 | [diff] [blame] | 2812 | } |
Weston Andros Adamson | 0f9c429e | 2014-05-15 11:56:51 -0400 | [diff] [blame] | 2813 | |
Weston Andros Adamson | 19b5484 | 2014-05-15 11:56:55 -0400 | [diff] [blame] | 2814 | return size; |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 2815 | } |
Benny Halevy | 89a58e3 | 2011-05-25 20:54:40 +0300 | [diff] [blame] | 2816 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_test); |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 2817 | |
Weston Andros Adamson | 53113ad | 2014-06-09 11:48:38 -0400 | [diff] [blame] | 2818 | int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr) |
Trond Myklebust | e2fecb2 | 2012-01-06 08:57:46 -0500 | [diff] [blame] | 2819 | { |
| 2820 | struct nfs_pageio_descriptor pgio; |
Trond Myklebust | e2fecb2 | 2012-01-06 08:57:46 -0500 | [diff] [blame] | 2821 | |
| 2822 | /* Resend all requests through the MDS */ |
Weston Andros Adamson | 53113ad | 2014-06-09 11:48:38 -0400 | [diff] [blame] | 2823 | nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true, |
| 2824 | hdr->completion_ops); |
| 2825 | return nfs_pageio_resend(&pgio, hdr); |
Trond Myklebust | e2fecb2 | 2012-01-06 08:57:46 -0500 | [diff] [blame] | 2826 | } |
Andy Adamson | e7dd79af | 2012-04-27 17:53:46 -0400 | [diff] [blame] | 2827 | EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds); |
Trond Myklebust | e2fecb2 | 2012-01-06 08:57:46 -0500 | [diff] [blame] | 2828 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2829 | static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr) |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 2830 | { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2831 | |
| 2832 | dprintk("pnfs write error = %d\n", hdr->pnfs_error); |
| 2833 | if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags & |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 2834 | PNFS_LAYOUTRET_ON_ERROR) { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2835 | pnfs_return_layout(hdr->inode); |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 2836 | } |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 2837 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) |
Weston Andros Adamson | 53113ad | 2014-06-09 11:48:38 -0400 | [diff] [blame] | 2838 | hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr); |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 2839 | } |
| 2840 | |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 2841 | /* |
| 2842 | * Called by non rpc-based layout drivers |
| 2843 | */ |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2844 | void pnfs_ld_write_done(struct nfs_pgio_header *hdr) |
Fred Isaman | 94ad1c8 | 2011-03-01 01:34:14 +0000 | [diff] [blame] | 2845 | { |
Kinglong Mee | f8417b48 | 2015-10-16 17:23:29 +0800 | [diff] [blame] | 2846 | if (likely(!hdr->pnfs_error)) { |
Trond Myklebust | 67af761 | 2015-03-25 20:40:38 -0400 | [diff] [blame] | 2847 | pnfs_set_layoutcommit(hdr->inode, hdr->lseg, |
| 2848 | hdr->mds_offset + hdr->res.count); |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2849 | hdr->mds_ops->rpc_call_done(&hdr->task, hdr); |
Kinglong Mee | f8417b48 | 2015-10-16 17:23:29 +0800 | [diff] [blame] | 2850 | } |
| 2851 | trace_nfs4_pnfs_write(hdr, hdr->pnfs_error); |
| 2852 | if (unlikely(hdr->pnfs_error)) |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2853 | pnfs_ld_handle_write_error(hdr); |
| 2854 | hdr->mds_ops->rpc_release(hdr); |
Fred Isaman | 44b8379 | 2011-03-03 15:13:44 +0000 | [diff] [blame] | 2855 | } |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 2856 | EXPORT_SYMBOL_GPL(pnfs_ld_write_done); |
Fred Isaman | 44b8379 | 2011-03-03 15:13:44 +0000 | [diff] [blame] | 2857 | |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 2858 | static void |
| 2859 | pnfs_write_through_mds(struct nfs_pageio_descriptor *desc, |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2860 | struct nfs_pgio_header *hdr) |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 2861 | { |
Peng Tao | 48d635f | 2014-11-10 08:35:35 +0800 | [diff] [blame] | 2862 | struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 2863 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 2864 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 2865 | list_splice_tail_init(&hdr->pages, &mirror->pg_list); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 2866 | nfs_pageio_reset_write_mds(desc); |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 2867 | mirror->pg_recoalesce = 1; |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 2868 | } |
Scott Mayhew | ba4a76f | 2017-12-15 16:12:32 -0500 | [diff] [blame] | 2869 | hdr->completion_ops->completion(hdr); |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | static enum pnfs_try_status |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2873 | pnfs_try_to_write_data(struct nfs_pgio_header *hdr, |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 2874 | const struct rpc_call_ops *call_ops, |
| 2875 | struct pnfs_layout_segment *lseg, |
| 2876 | int how) |
Andy Adamson | 0382b74 | 2011-03-03 15:13:45 +0000 | [diff] [blame] | 2877 | { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2878 | struct inode *inode = hdr->inode; |
Andy Adamson | 0382b74 | 2011-03-03 15:13:45 +0000 | [diff] [blame] | 2879 | enum pnfs_try_status trypnfs; |
| 2880 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 2881 | |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2882 | hdr->mds_ops = call_ops; |
Andy Adamson | 0382b74 | 2011-03-03 15:13:45 +0000 | [diff] [blame] | 2883 | |
| 2884 | dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__, |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2885 | inode->i_ino, hdr->args.count, hdr->args.offset, how); |
| 2886 | trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 2887 | if (trypnfs != PNFS_NOT_ATTEMPTED) |
Andy Adamson | 0382b74 | 2011-03-03 15:13:45 +0000 | [diff] [blame] | 2888 | nfs_inc_stats(inode, NFSIOS_PNFS_WRITE); |
Andy Adamson | 0382b74 | 2011-03-03 15:13:45 +0000 | [diff] [blame] | 2889 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
| 2890 | return trypnfs; |
| 2891 | } |
| 2892 | |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 2893 | static void |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 2894 | pnfs_do_write(struct nfs_pageio_descriptor *desc, |
| 2895 | struct nfs_pgio_header *hdr, int how) |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 2896 | { |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 2897 | const struct rpc_call_ops *call_ops = desc->pg_rpc_callops; |
| 2898 | struct pnfs_layout_segment *lseg = desc->pg_lseg; |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 2899 | enum pnfs_try_status trypnfs; |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 2900 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2901 | trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how); |
Trond Myklebust | 37f8aa1 | 2017-04-29 00:02:37 -0400 | [diff] [blame] | 2902 | switch (trypnfs) { |
| 2903 | case PNFS_NOT_ATTEMPTED: |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2904 | pnfs_write_through_mds(desc, hdr); |
Gustavo A. R. Silva | ffb8171 | 2020-11-20 12:26:46 -0600 | [diff] [blame] | 2905 | break; |
Trond Myklebust | 37f8aa1 | 2017-04-29 00:02:37 -0400 | [diff] [blame] | 2906 | case PNFS_ATTEMPTED: |
| 2907 | break; |
| 2908 | case PNFS_TRY_AGAIN: |
| 2909 | /* cleanup hdr and prepare to redo pnfs */ |
| 2910 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
| 2911 | struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); |
| 2912 | list_splice_init(&hdr->pages, &mirror->pg_list); |
| 2913 | mirror->pg_recoalesce = 1; |
| 2914 | } |
| 2915 | hdr->mds_ops->rpc_release(hdr); |
| 2916 | } |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 2917 | } |
| 2918 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 2919 | static void pnfs_writehdr_free(struct nfs_pgio_header *hdr) |
| 2920 | { |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 2921 | pnfs_put_lseg(hdr->lseg); |
Weston Andros Adamson | 1e7f3a4 | 2014-06-09 11:48:33 -0400 | [diff] [blame] | 2922 | nfs_pgio_header_free(hdr); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 2923 | } |
| 2924 | |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 2925 | int |
| 2926 | pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) |
| 2927 | { |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 2928 | struct nfs_pgio_header *hdr; |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 2929 | int ret; |
| 2930 | |
Weston Andros Adamson | 1e7f3a4 | 2014-06-09 11:48:33 -0400 | [diff] [blame] | 2931 | hdr = nfs_pgio_header_alloc(desc->pg_rw_ops); |
| 2932 | if (!hdr) { |
Peng Tao | 2bff228 | 2015-12-05 02:03:17 +0800 | [diff] [blame] | 2933 | desc->pg_error = -ENOMEM; |
| 2934 | return desc->pg_error; |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 2935 | } |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 2936 | nfs_pgheader_init(desc, hdr, pnfs_writehdr_free); |
Weston Andros Adamson | 180bb5e | 2014-09-10 15:48:01 -0400 | [diff] [blame] | 2937 | |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 2938 | hdr->lseg = pnfs_get_lseg(desc->pg_lseg); |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 2939 | ret = nfs_generic_pgio(desc, hdr); |
Weston Andros Adamson | 180bb5e | 2014-09-10 15:48:01 -0400 | [diff] [blame] | 2940 | if (!ret) |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 2941 | pnfs_do_write(desc, hdr, desc->pg_ioflags); |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 2942 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 2943 | return ret; |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 2944 | } |
| 2945 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages); |
| 2946 | |
Weston Andros Adamson | 53113ad | 2014-06-09 11:48:38 -0400 | [diff] [blame] | 2947 | int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr) |
Trond Myklebust | 62e4a76 | 2011-11-10 14:30:37 -0500 | [diff] [blame] | 2948 | { |
| 2949 | struct nfs_pageio_descriptor pgio; |
| 2950 | |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 2951 | /* Resend all requests through the MDS */ |
Weston Andros Adamson | 53113ad | 2014-06-09 11:48:38 -0400 | [diff] [blame] | 2952 | nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops); |
| 2953 | return nfs_pageio_resend(&pgio, hdr); |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 2954 | } |
Andy Adamson | e7dd79af | 2012-04-27 17:53:46 -0400 | [diff] [blame] | 2955 | EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds); |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 2956 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2957 | static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr) |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 2958 | { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2959 | dprintk("pnfs read error = %d\n", hdr->pnfs_error); |
| 2960 | if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags & |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 2961 | PNFS_LAYOUTRET_ON_ERROR) { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 2962 | pnfs_return_layout(hdr->inode); |
Fred Isaman | 1acbbb4e1 | 2012-04-20 14:47:37 -0400 | [diff] [blame] | 2963 | } |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 2964 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) |
Weston Andros Adamson | 53113ad | 2014-06-09 11:48:38 -0400 | [diff] [blame] | 2965 | hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr); |
Trond Myklebust | 62e4a76 | 2011-11-10 14:30:37 -0500 | [diff] [blame] | 2966 | } |
| 2967 | |
Andy Adamson | b1f69b7 | 2010-10-20 00:18:03 -0400 | [diff] [blame] | 2968 | /* |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 2969 | * Called by non rpc-based layout drivers |
| 2970 | */ |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2971 | void pnfs_ld_read_done(struct nfs_pgio_header *hdr) |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 2972 | { |
Trond Myklebust | bfc505d | 2016-09-15 18:26:05 -0400 | [diff] [blame] | 2973 | if (likely(!hdr->pnfs_error)) |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2974 | hdr->mds_ops->rpc_call_done(&hdr->task, hdr); |
Kinglong Mee | f8417b48 | 2015-10-16 17:23:29 +0800 | [diff] [blame] | 2975 | trace_nfs4_pnfs_read(hdr, hdr->pnfs_error); |
| 2976 | if (unlikely(hdr->pnfs_error)) |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2977 | pnfs_ld_handle_read_error(hdr); |
| 2978 | hdr->mds_ops->rpc_release(hdr); |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 2979 | } |
| 2980 | EXPORT_SYMBOL_GPL(pnfs_ld_read_done); |
| 2981 | |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2982 | static void |
| 2983 | pnfs_read_through_mds(struct nfs_pageio_descriptor *desc, |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 2984 | struct nfs_pgio_header *hdr) |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2985 | { |
Peng Tao | 48d635f | 2014-11-10 08:35:35 +0800 | [diff] [blame] | 2986 | struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 2987 | |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 2988 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 2989 | list_splice_tail_init(&hdr->pages, &mirror->pg_list); |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 2990 | nfs_pageio_reset_read_mds(desc); |
Weston Andros Adamson | a7d42dd | 2014-09-19 10:55:07 -0400 | [diff] [blame] | 2991 | mirror->pg_recoalesce = 1; |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 2992 | } |
Scott Mayhew | ba4a76f | 2017-12-15 16:12:32 -0500 | [diff] [blame] | 2993 | hdr->completion_ops->completion(hdr); |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2994 | } |
| 2995 | |
Benny Halevy | d20581a | 2011-05-22 19:52:03 +0300 | [diff] [blame] | 2996 | /* |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 2997 | * Call the appropriate parallel I/O subsystem read function. |
| 2998 | */ |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 2999 | static enum pnfs_try_status |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 3000 | pnfs_try_to_read_data(struct nfs_pgio_header *hdr, |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 3001 | const struct rpc_call_ops *call_ops, |
| 3002 | struct pnfs_layout_segment *lseg) |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 3003 | { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 3004 | struct inode *inode = hdr->inode; |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 3005 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 3006 | enum pnfs_try_status trypnfs; |
| 3007 | |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 3008 | hdr->mds_ops = call_ops; |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 3009 | |
| 3010 | dprintk("%s: Reading ino:%lu %u@%llu\n", |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 3011 | __func__, inode->i_ino, hdr->args.count, hdr->args.offset); |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 3012 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 3013 | trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr); |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 3014 | if (trypnfs != PNFS_NOT_ATTEMPTED) |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 3015 | nfs_inc_stats(inode, NFSIOS_PNFS_READ); |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 3016 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
| 3017 | return trypnfs; |
| 3018 | } |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3019 | |
Peng Tao | ceb11e1 | 2014-11-10 08:35:38 +0800 | [diff] [blame] | 3020 | /* Resend all requests through pnfs. */ |
Trond Myklebust | 563c53e | 2020-08-11 13:36:32 -0400 | [diff] [blame] | 3021 | void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr, |
| 3022 | unsigned int mirror_idx) |
Peng Tao | ceb11e1 | 2014-11-10 08:35:38 +0800 | [diff] [blame] | 3023 | { |
| 3024 | struct nfs_pageio_descriptor pgio; |
| 3025 | |
Weston Andros Adamson | 1b1bc66 | 2016-04-01 11:42:28 -0400 | [diff] [blame] | 3026 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
Trond Myklebust | 54e4a0d | 2016-11-27 15:12:39 -0500 | [diff] [blame] | 3027 | /* Prevent deadlocks with layoutreturn! */ |
| 3028 | pnfs_put_lseg(hdr->lseg); |
| 3029 | hdr->lseg = NULL; |
| 3030 | |
Weston Andros Adamson | 1b1bc66 | 2016-04-01 11:42:28 -0400 | [diff] [blame] | 3031 | nfs_pageio_init_read(&pgio, hdr->inode, false, |
| 3032 | hdr->completion_ops); |
Trond Myklebust | 563c53e | 2020-08-11 13:36:32 -0400 | [diff] [blame] | 3033 | pgio.pg_mirror_idx = mirror_idx; |
Weston Andros Adamson | 1b1bc66 | 2016-04-01 11:42:28 -0400 | [diff] [blame] | 3034 | hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr); |
| 3035 | } |
Peng Tao | ceb11e1 | 2014-11-10 08:35:38 +0800 | [diff] [blame] | 3036 | } |
| 3037 | EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs); |
| 3038 | |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 3039 | static void |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 3040 | 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] | 3041 | { |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 3042 | const struct rpc_call_ops *call_ops = desc->pg_rpc_callops; |
| 3043 | struct pnfs_layout_segment *lseg = desc->pg_lseg; |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 3044 | enum pnfs_try_status trypnfs; |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 3045 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 3046 | trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg); |
Trond Myklebust | 6aeafd0 | 2017-04-25 11:26:53 -0400 | [diff] [blame] | 3047 | switch (trypnfs) { |
| 3048 | case PNFS_NOT_ATTEMPTED: |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 3049 | pnfs_read_through_mds(desc, hdr); |
Gustavo A. R. Silva | ffb8171 | 2020-11-20 12:26:46 -0600 | [diff] [blame] | 3050 | break; |
Trond Myklebust | 6aeafd0 | 2017-04-25 11:26:53 -0400 | [diff] [blame] | 3051 | case PNFS_ATTEMPTED: |
| 3052 | break; |
| 3053 | case PNFS_TRY_AGAIN: |
| 3054 | /* cleanup hdr and prepare to redo pnfs */ |
| 3055 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
| 3056 | struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); |
| 3057 | list_splice_init(&hdr->pages, &mirror->pg_list); |
| 3058 | mirror->pg_recoalesce = 1; |
| 3059 | } |
| 3060 | hdr->mds_ops->rpc_release(hdr); |
| 3061 | } |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 3062 | } |
| 3063 | |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 3064 | static void pnfs_readhdr_free(struct nfs_pgio_header *hdr) |
| 3065 | { |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 3066 | pnfs_put_lseg(hdr->lseg); |
Weston Andros Adamson | 1e7f3a4 | 2014-06-09 11:48:33 -0400 | [diff] [blame] | 3067 | nfs_pgio_header_free(hdr); |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 3068 | } |
| 3069 | |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 3070 | int |
| 3071 | pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc) |
| 3072 | { |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 3073 | struct nfs_pgio_header *hdr; |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 3074 | int ret; |
| 3075 | |
Weston Andros Adamson | 1e7f3a4 | 2014-06-09 11:48:33 -0400 | [diff] [blame] | 3076 | hdr = nfs_pgio_header_alloc(desc->pg_rw_ops); |
| 3077 | if (!hdr) { |
Peng Tao | 2bff228 | 2015-12-05 02:03:17 +0800 | [diff] [blame] | 3078 | desc->pg_error = -ENOMEM; |
| 3079 | return desc->pg_error; |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 3080 | } |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 3081 | nfs_pgheader_init(desc, hdr, pnfs_readhdr_free); |
Trond Myklebust | 9369a43 | 2012-09-18 20:57:08 -0400 | [diff] [blame] | 3082 | hdr->lseg = pnfs_get_lseg(desc->pg_lseg); |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 3083 | ret = nfs_generic_pgio(desc, hdr); |
Weston Andros Adamson | 180bb5e | 2014-09-10 15:48:01 -0400 | [diff] [blame] | 3084 | if (!ret) |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 3085 | pnfs_do_read(desc, hdr); |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 3086 | return ret; |
Trond Myklebust | 493292d | 2011-07-13 15:58:28 -0400 | [diff] [blame] | 3087 | } |
| 3088 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages); |
| 3089 | |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 3090 | static void pnfs_clear_layoutcommitting(struct inode *inode) |
| 3091 | { |
| 3092 | unsigned long *bitlock = &NFS_I(inode)->flags; |
| 3093 | |
| 3094 | clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 3095 | smp_mb__after_atomic(); |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 3096 | wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING); |
| 3097 | } |
| 3098 | |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3099 | /* |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 3100 | * There can be multiple RW segments. |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3101 | */ |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 3102 | 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] | 3103 | { |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 3104 | struct pnfs_layout_segment *lseg; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3105 | |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 3106 | list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) { |
| 3107 | if (lseg->pls_range.iomode == IOMODE_RW && |
Trond Myklebust | a073dbf | 2013-03-20 12:34:32 -0400 | [diff] [blame] | 3108 | test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 3109 | list_add(&lseg->pls_lc_list, listp); |
| 3110 | } |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
Trond Myklebust | a073dbf | 2013-03-20 12:34:32 -0400 | [diff] [blame] | 3113 | static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp) |
| 3114 | { |
| 3115 | struct pnfs_layout_segment *lseg, *tmp; |
Trond Myklebust | a073dbf | 2013-03-20 12:34:32 -0400 | [diff] [blame] | 3116 | |
| 3117 | /* Matched by references in pnfs_set_layoutcommit */ |
| 3118 | list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) { |
| 3119 | list_del_init(&lseg->pls_lc_list); |
| 3120 | pnfs_put_lseg(lseg); |
| 3121 | } |
| 3122 | |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 3123 | pnfs_clear_layoutcommitting(inode); |
Trond Myklebust | a073dbf | 2013-03-20 12:34:32 -0400 | [diff] [blame] | 3124 | } |
| 3125 | |
Peng Tao | 1b0ae06 | 2011-09-22 21:50:12 -0400 | [diff] [blame] | 3126 | void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg) |
| 3127 | { |
Trond Myklebust | b9e028f | 2012-09-18 16:41:18 -0400 | [diff] [blame] | 3128 | pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode); |
Peng Tao | 1b0ae06 | 2011-09-22 21:50:12 -0400 | [diff] [blame] | 3129 | } |
| 3130 | EXPORT_SYMBOL_GPL(pnfs_set_lo_fail); |
| 3131 | |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3132 | void |
Trond Myklebust | 67af761 | 2015-03-25 20:40:38 -0400 | [diff] [blame] | 3133 | pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg, |
| 3134 | loff_t end_pos) |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3135 | { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 3136 | struct nfs_inode *nfsi = NFS_I(inode); |
Weston Andros Adamson | 79a48a1 | 2011-04-13 10:53:51 -0400 | [diff] [blame] | 3137 | bool mark_as_dirty = false; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3138 | |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 3139 | spin_lock(&inode->i_lock); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3140 | if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) { |
Trond Myklebust | 29559b1 | 2015-03-25 20:10:20 -0400 | [diff] [blame] | 3141 | nfsi->layout->plh_lwb = end_pos; |
Weston Andros Adamson | 79a48a1 | 2011-04-13 10:53:51 -0400 | [diff] [blame] | 3142 | mark_as_dirty = true; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3143 | dprintk("%s: Set layoutcommit for inode %lu ", |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 3144 | __func__, inode->i_ino); |
Trond Myklebust | 29559b1 | 2015-03-25 20:10:20 -0400 | [diff] [blame] | 3145 | } else if (end_pos > nfsi->layout->plh_lwb) |
| 3146 | nfsi->layout->plh_lwb = end_pos; |
Trond Myklebust | 67af761 | 2015-03-25 20:40:38 -0400 | [diff] [blame] | 3147 | if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) { |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 3148 | /* references matched in nfs4_layoutcommit_release */ |
Trond Myklebust | 67af761 | 2015-03-25 20:40:38 -0400 | [diff] [blame] | 3149 | pnfs_get_lseg(lseg); |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 3150 | } |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 3151 | spin_unlock(&inode->i_lock); |
Peng Tao | acff5880 | 2011-07-30 20:52:31 -0400 | [diff] [blame] | 3152 | dprintk("%s: lseg %p end_pos %llu\n", |
Trond Myklebust | 67af761 | 2015-03-25 20:40:38 -0400 | [diff] [blame] | 3153 | __func__, lseg, nfsi->layout->plh_lwb); |
Weston Andros Adamson | 79a48a1 | 2011-04-13 10:53:51 -0400 | [diff] [blame] | 3154 | |
| 3155 | /* if pnfs_layoutcommit_inode() runs between inode locks, the next one |
| 3156 | * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */ |
| 3157 | if (mark_as_dirty) |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 3158 | mark_inode_dirty_sync(inode); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3159 | } |
| 3160 | EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit); |
| 3161 | |
Andy Adamson | db29c08 | 2011-07-30 20:52:38 -0400 | [diff] [blame] | 3162 | void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data) |
| 3163 | { |
| 3164 | struct nfs_server *nfss = NFS_SERVER(data->args.inode); |
| 3165 | |
| 3166 | if (nfss->pnfs_curr_ld->cleanup_layoutcommit) |
| 3167 | nfss->pnfs_curr_ld->cleanup_layoutcommit(data); |
Trond Myklebust | a073dbf | 2013-03-20 12:34:32 -0400 | [diff] [blame] | 3168 | pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list); |
Andy Adamson | db29c08 | 2011-07-30 20:52:38 -0400 | [diff] [blame] | 3169 | } |
| 3170 | |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 3171 | /* |
| 3172 | * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and |
| 3173 | * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough |
| 3174 | * data to disk to allow the server to recover the data if it crashes. |
| 3175 | * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag |
| 3176 | * is off, and a COMMIT is sent to a data server, or |
| 3177 | * if WRITEs to a data server return NFS_DATA_SYNC. |
| 3178 | */ |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3179 | int |
Andy Adamson | ef31153 | 2011-03-12 02:58:10 -0500 | [diff] [blame] | 3180 | pnfs_layoutcommit_inode(struct inode *inode, bool sync) |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3181 | { |
Christoph Hellwig | 5f919c9 | 2014-08-21 11:09:25 -0500 | [diff] [blame] | 3182 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3183 | struct nfs4_layoutcommit_data *data; |
| 3184 | struct nfs_inode *nfsi = NFS_I(inode); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3185 | loff_t end_pos; |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 3186 | int status; |
| 3187 | |
| 3188 | if (!pnfs_layoutcommit_outstanding(inode)) |
| 3189 | return 0; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3190 | |
| 3191 | dprintk("--> %s inode %lu\n", __func__, inode->i_ino); |
| 3192 | |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 3193 | status = -EAGAIN; |
| 3194 | if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) { |
| 3195 | if (!sync) |
| 3196 | goto out; |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 3197 | status = wait_on_bit_lock_action(&nfsi->flags, |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 3198 | NFS_INO_LAYOUTCOMMITTING, |
| 3199 | nfs_wait_bit_killable, |
Peter Zijlstra | f5d39b0 | 2022-08-22 13:18:22 +0200 | [diff] [blame] | 3200 | TASK_KILLABLE|TASK_FREEZABLE_UNSAFE); |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 3201 | if (status) |
| 3202 | goto out; |
| 3203 | } |
Andy Adamson | de4b15c | 2011-03-12 02:58:09 -0500 | [diff] [blame] | 3204 | |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 3205 | status = -ENOMEM; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3206 | /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */ |
Trond Myklebust | 63d8a41 | 2022-03-21 15:32:09 -0400 | [diff] [blame] | 3207 | data = kzalloc(sizeof(*data), nfs_io_gfp_mask()); |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 3208 | if (!data) |
| 3209 | goto clear_layoutcommitting; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3210 | |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 3211 | status = 0; |
| 3212 | spin_lock(&inode->i_lock); |
| 3213 | if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) |
| 3214 | goto out_unlock; |
Peng Tao | 92407e7 | 2011-10-23 20:21:17 -0700 | [diff] [blame] | 3215 | |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 3216 | INIT_LIST_HEAD(&data->lseg_list); |
Peng Tao | a9bae56 | 2011-07-30 20:52:33 -0400 | [diff] [blame] | 3217 | pnfs_list_write_lseg(inode, &data->lseg_list); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3218 | |
Peng Tao | acff5880 | 2011-07-30 20:52:31 -0400 | [diff] [blame] | 3219 | end_pos = nfsi->layout->plh_lwb; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3220 | |
Trond Myklebust | f597c53 | 2012-03-04 18:13:56 -0500 | [diff] [blame] | 3221 | nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid); |
Trond Myklebust | 97a728f | 2020-04-02 15:47:08 -0400 | [diff] [blame] | 3222 | data->cred = get_cred(nfsi->layout->plh_lc_cred); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3223 | spin_unlock(&inode->i_lock); |
| 3224 | |
| 3225 | data->args.inode = inode; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3226 | nfs_fattr_init(&data->fattr); |
| 3227 | data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask; |
| 3228 | data->res.fattr = &data->fattr; |
Trond Myklebust | 2e18d4d | 2016-06-26 18:54:58 -0400 | [diff] [blame] | 3229 | if (end_pos != 0) |
| 3230 | data->args.lastbytewritten = end_pos - 1; |
| 3231 | else |
| 3232 | data->args.lastbytewritten = U64_MAX; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3233 | data->res.server = NFS_SERVER(inode); |
| 3234 | |
Christoph Hellwig | 5f919c9 | 2014-08-21 11:09:25 -0500 | [diff] [blame] | 3235 | if (ld->prepare_layoutcommit) { |
| 3236 | status = ld->prepare_layoutcommit(&data->args); |
| 3237 | if (status) { |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 3238 | put_cred(data->cred); |
Christoph Hellwig | 5f919c9 | 2014-08-21 11:09:25 -0500 | [diff] [blame] | 3239 | spin_lock(&inode->i_lock); |
Trond Myklebust | 29559b1 | 2015-03-25 20:10:20 -0400 | [diff] [blame] | 3240 | set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags); |
| 3241 | if (end_pos > nfsi->layout->plh_lwb) |
Christoph Hellwig | 5f919c9 | 2014-08-21 11:09:25 -0500 | [diff] [blame] | 3242 | nfsi->layout->plh_lwb = end_pos; |
Jeff Layton | 3471648a | 2015-07-10 15:58:42 -0400 | [diff] [blame] | 3243 | goto out_unlock; |
Christoph Hellwig | 5f919c9 | 2014-08-21 11:09:25 -0500 | [diff] [blame] | 3244 | } |
| 3245 | } |
| 3246 | |
| 3247 | |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3248 | status = nfs4_proc_layoutcommit(data, sync); |
| 3249 | out: |
Peng Tao | 92407e7 | 2011-10-23 20:21:17 -0700 | [diff] [blame] | 3250 | if (status) |
| 3251 | mark_inode_dirty_sync(inode); |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3252 | dprintk("<-- %s status %d\n", __func__, status); |
| 3253 | return status; |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 3254 | out_unlock: |
| 3255 | spin_unlock(&inode->i_lock); |
Peng Tao | 92407e7 | 2011-10-23 20:21:17 -0700 | [diff] [blame] | 3256 | kfree(data); |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 3257 | clear_layoutcommitting: |
| 3258 | pnfs_clear_layoutcommitting(inode); |
Peng Tao | 92407e7 | 2011-10-23 20:21:17 -0700 | [diff] [blame] | 3259 | goto out; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 3260 | } |
Peng Tao | 72cff44 | 2014-08-07 10:12:38 +0800 | [diff] [blame] | 3261 | EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode); |
Andy Adamson | 82be417 | 2012-05-23 05:02:35 -0400 | [diff] [blame] | 3262 | |
Trond Myklebust | 5bb89b4 | 2015-03-25 14:14:42 -0400 | [diff] [blame] | 3263 | int |
| 3264 | pnfs_generic_sync(struct inode *inode, bool datasync) |
| 3265 | { |
| 3266 | return pnfs_layoutcommit_inode(inode, true); |
| 3267 | } |
| 3268 | EXPORT_SYMBOL_GPL(pnfs_generic_sync); |
| 3269 | |
Andy Adamson | 82be417 | 2012-05-23 05:02:35 -0400 | [diff] [blame] | 3270 | struct nfs4_threshold *pnfs_mdsthreshold_alloc(void) |
| 3271 | { |
| 3272 | struct nfs4_threshold *thp; |
| 3273 | |
Trond Myklebust | 63d8a41 | 2022-03-21 15:32:09 -0400 | [diff] [blame] | 3274 | thp = kzalloc(sizeof(*thp), nfs_io_gfp_mask()); |
Andy Adamson | 82be417 | 2012-05-23 05:02:35 -0400 | [diff] [blame] | 3275 | if (!thp) { |
| 3276 | dprintk("%s mdsthreshold allocation failed\n", __func__); |
| 3277 | return NULL; |
| 3278 | } |
| 3279 | return thp; |
| 3280 | } |
Peng Tao | 8733408 | 2015-06-23 19:51:57 +0800 | [diff] [blame] | 3281 | |
Peng Tao | 865a7ec | 2015-06-25 18:19:32 +0800 | [diff] [blame] | 3282 | #if IS_ENABLED(CONFIG_NFS_V4_2) |
Peng Tao | 8733408 | 2015-06-23 19:51:57 +0800 | [diff] [blame] | 3283 | int |
Trond Myklebust | c8ad889 | 2015-08-05 17:31:58 -0400 | [diff] [blame] | 3284 | pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags) |
Peng Tao | 8733408 | 2015-06-23 19:51:57 +0800 | [diff] [blame] | 3285 | { |
| 3286 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld; |
| 3287 | struct nfs_server *server = NFS_SERVER(inode); |
Peng Tao | 1bfe3b2 | 2015-06-23 19:52:03 +0800 | [diff] [blame] | 3288 | struct nfs_inode *nfsi = NFS_I(inode); |
Peng Tao | 8733408 | 2015-06-23 19:51:57 +0800 | [diff] [blame] | 3289 | struct nfs42_layoutstat_data *data; |
| 3290 | struct pnfs_layout_hdr *hdr; |
| 3291 | int status = 0; |
| 3292 | |
| 3293 | if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats) |
| 3294 | goto out; |
| 3295 | |
Trond Myklebust | 6c5a0d8 | 2015-06-27 11:45:46 -0400 | [diff] [blame] | 3296 | if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS)) |
| 3297 | goto out; |
| 3298 | |
Peng Tao | 1bfe3b2 | 2015-06-23 19:52:03 +0800 | [diff] [blame] | 3299 | if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags)) |
| 3300 | goto out; |
| 3301 | |
Peng Tao | 8733408 | 2015-06-23 19:51:57 +0800 | [diff] [blame] | 3302 | spin_lock(&inode->i_lock); |
| 3303 | if (!NFS_I(inode)->layout) { |
| 3304 | spin_unlock(&inode->i_lock); |
Trond Myklebust | f538d0b | 2016-05-16 14:41:14 -0400 | [diff] [blame] | 3305 | goto out_clear_layoutstats; |
Peng Tao | 8733408 | 2015-06-23 19:51:57 +0800 | [diff] [blame] | 3306 | } |
| 3307 | hdr = NFS_I(inode)->layout; |
| 3308 | pnfs_get_layout_hdr(hdr); |
| 3309 | spin_unlock(&inode->i_lock); |
| 3310 | |
Trond Myklebust | c8ad889 | 2015-08-05 17:31:58 -0400 | [diff] [blame] | 3311 | data = kzalloc(sizeof(*data), gfp_flags); |
Peng Tao | 8733408 | 2015-06-23 19:51:57 +0800 | [diff] [blame] | 3312 | if (!data) { |
| 3313 | status = -ENOMEM; |
| 3314 | goto out_put; |
| 3315 | } |
| 3316 | |
| 3317 | data->args.fh = NFS_FH(inode); |
| 3318 | data->args.inode = inode; |
Peng Tao | 8733408 | 2015-06-23 19:51:57 +0800 | [diff] [blame] | 3319 | status = ld->prepare_layoutstats(&data->args); |
| 3320 | if (status) |
| 3321 | goto out_free; |
| 3322 | |
| 3323 | status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data); |
| 3324 | |
| 3325 | out: |
| 3326 | dprintk("%s returns %d\n", __func__, status); |
| 3327 | return status; |
| 3328 | |
| 3329 | out_free: |
| 3330 | kfree(data); |
| 3331 | out_put: |
| 3332 | pnfs_put_layout_hdr(hdr); |
Trond Myklebust | f538d0b | 2016-05-16 14:41:14 -0400 | [diff] [blame] | 3333 | out_clear_layoutstats: |
Peng Tao | 1bfe3b2 | 2015-06-23 19:52:03 +0800 | [diff] [blame] | 3334 | smp_mb__before_atomic(); |
| 3335 | clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags); |
| 3336 | smp_mb__after_atomic(); |
Peng Tao | 8733408 | 2015-06-23 19:51:57 +0800 | [diff] [blame] | 3337 | goto out; |
| 3338 | } |
| 3339 | EXPORT_SYMBOL_GPL(pnfs_report_layoutstat); |
Peng Tao | 865a7ec | 2015-06-25 18:19:32 +0800 | [diff] [blame] | 3340 | #endif |
Trond Myklebust | bbf58bf | 2015-08-24 20:39:18 -0400 | [diff] [blame] | 3341 | |
| 3342 | unsigned int layoutstats_timer; |
| 3343 | module_param(layoutstats_timer, uint, 0644); |
| 3344 | EXPORT_SYMBOL_GPL(layoutstats_timer); |