blob: 259ef85f435aa7f9e0b0e4d06d3ce24a9e7a3ad3 [file] [log] [blame]
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001/*
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 Myklebust493292d2011-07-13 15:58:28 -040031#include <linux/nfs_page.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040032#include <linux/module.h>
Jeff Laytonca440c32016-09-15 14:40:49 -040033#include <linux/sort.h>
Andy Adamson974cec82010-10-20 00:18:02 -040034#include "internal.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040035#include "pnfs.h"
Andy Adamson64419a92011-03-01 01:34:16 +000036#include "iostat.h"
Trond Myklebustcc668ab2013-08-14 15:31:28 -040037#include "nfs4trace.h"
Trond Myklebust40dd4b72015-01-24 13:54:37 -050038#include "delegation.h"
Peng Tao87334082015-06-23 19:51:57 +080039#include "nfs42.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040040
41#define NFSDBG_FACILITY NFSDBG_PNFS
Trond Myklebust25c75332012-09-18 17:01:12 -040042#define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040043
Fred Isaman02c35fc2010-10-20 00:17:59 -040044/* Locking:
45 *
46 * pnfs_spinlock:
47 * protects pnfs_modules_tbl.
48 */
49static DEFINE_SPINLOCK(pnfs_spinlock);
50
51/*
52 * pnfs_modules_tbl holds all pnfs modules
53 */
54static LIST_HEAD(pnfs_modules_tbl);
55
Trond Myklebust13c13a62016-01-26 23:12:11 -050056static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo);
Peng Taoaa1e0e3a2014-09-06 00:53:25 +080057
Fred Isaman02c35fc2010-10-20 00:17:59 -040058/* Return the registered pnfs layout driver module matching given id */
59static struct pnfs_layoutdriver_type *
60find_pnfs_driver_locked(u32 id)
61{
62 struct pnfs_layoutdriver_type *local;
63
64 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
65 if (local->id == id)
66 goto out;
67 local = NULL;
68out:
69 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
70 return local;
71}
72
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040073static struct pnfs_layoutdriver_type *
74find_pnfs_driver(u32 id)
75{
Fred Isaman02c35fc2010-10-20 00:17:59 -040076 struct pnfs_layoutdriver_type *local;
77
78 spin_lock(&pnfs_spinlock);
79 local = find_pnfs_driver_locked(id);
Trond Myklebust0a9c63f2012-06-15 13:02:58 -040080 if (local != NULL && !try_module_get(local->owner)) {
81 dprintk("%s: Could not grab reference on module\n", __func__);
82 local = NULL;
83 }
Fred Isaman02c35fc2010-10-20 00:17:59 -040084 spin_unlock(&pnfs_spinlock);
85 return local;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040086}
87
88void
89unset_pnfs_layoutdriver(struct nfs_server *nfss)
90{
Benny Halevy738fd0f32011-07-30 20:52:36 -040091 if (nfss->pnfs_curr_ld) {
92 if (nfss->pnfs_curr_ld->clear_layoutdriver)
93 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
Trond Myklebust2a4c8992012-06-14 13:08:38 -040094 /* Decrement the MDS count. Purge the deviceid cache if zero */
95 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
96 nfs4_deviceid_purge_client(nfss->nfs_client);
Fred Isaman02c35fc2010-10-20 00:17:59 -040097 module_put(nfss->pnfs_curr_ld->owner);
Benny Halevy738fd0f32011-07-30 20:52:36 -040098 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040099 nfss->pnfs_curr_ld = NULL;
100}
101
102/*
Jeff Laytonca440c32016-09-15 14:40:49 -0400103 * When the server sends a list of layout types, we choose one in the order
104 * given in the list below.
105 *
106 * FIXME: should this list be configurable in some fashion? module param?
107 * mount option? something else?
108 */
109static const u32 ld_prefs[] = {
110 LAYOUT_SCSI,
111 LAYOUT_BLOCK_VOLUME,
112 LAYOUT_OSD2_OBJECTS,
113 LAYOUT_FLEX_FILES,
114 LAYOUT_NFSV4_1_FILES,
115 0
116};
117
118static int
119ld_cmp(const void *e1, const void *e2)
120{
121 u32 ld1 = *((u32 *)e1);
122 u32 ld2 = *((u32 *)e2);
123 int i;
124
125 for (i = 0; ld_prefs[i] != 0; i++) {
126 if (ld1 == ld_prefs[i])
127 return -1;
128
129 if (ld2 == ld_prefs[i])
130 return 1;
131 }
132 return 0;
133}
134
135/*
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400136 * Try to set the server's pnfs module to the pnfs layout type specified by id.
137 * Currently only one pNFS layout driver per filesystem is supported.
138 *
Jeff Layton3132e492016-08-10 15:58:24 -0400139 * @ids array of layout types supported by MDS.
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400140 */
141void
Benny Halevy738fd0f32011-07-30 20:52:36 -0400142set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
Jeff Laytonca440c32016-09-15 14:40:49 -0400143 struct nfs_fsinfo *fsinfo)
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400144{
145 struct pnfs_layoutdriver_type *ld_type = NULL;
Jeff Layton3132e492016-08-10 15:58:24 -0400146 u32 id;
Jeff Laytonca440c32016-09-15 14:40:49 -0400147 int i;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400148
Anna Schumaker19274712016-10-26 15:54:31 -0400149 if (fsinfo->nlayouttypes == 0)
150 goto out_no_driver;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400151 if (!(server->nfs_client->cl_exchange_flags &
152 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
Jeff Layton3132e492016-08-10 15:58:24 -0400153 printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n",
154 __func__, server->nfs_client->cl_exchange_flags);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400155 goto out_no_driver;
156 }
Jeff Layton3132e492016-08-10 15:58:24 -0400157
Jeff Laytonca440c32016-09-15 14:40:49 -0400158 sort(fsinfo->layouttype, fsinfo->nlayouttypes,
159 sizeof(*fsinfo->layouttype), ld_cmp, NULL);
Jeff Layton3132e492016-08-10 15:58:24 -0400160
Jeff Laytonca440c32016-09-15 14:40:49 -0400161 for (i = 0; i < fsinfo->nlayouttypes; i++) {
162 id = fsinfo->layouttype[i];
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400163 ld_type = find_pnfs_driver(id);
Jeff Laytonca440c32016-09-15 14:40:49 -0400164 if (!ld_type) {
165 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX,
166 id);
167 ld_type = find_pnfs_driver(id);
168 }
169 if (ld_type)
170 break;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400171 }
Jeff Layton3132e492016-08-10 15:58:24 -0400172
173 if (!ld_type) {
Jeff Laytonca440c32016-09-15 14:40:49 -0400174 dprintk("%s: No pNFS module found!\n", __func__);
Jeff Layton3132e492016-08-10 15:58:24 -0400175 goto out_no_driver;
176 }
177
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400178 server->pnfs_curr_ld = ld_type;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400179 if (ld_type->set_layoutdriver
180 && ld_type->set_layoutdriver(server, mntfh)) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500181 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
182 "driver %u.\n", __func__, id);
Benny Halevy738fd0f32011-07-30 20:52:36 -0400183 module_put(ld_type->owner);
184 goto out_no_driver;
185 }
Trond Myklebust2a4c8992012-06-14 13:08:38 -0400186 /* Bump the MDS count */
187 atomic_inc(&server->nfs_client->cl_mds_count);
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000188
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400189 dprintk("%s: pNFS module for %u set\n", __func__, id);
190 return;
191
192out_no_driver:
193 dprintk("%s: Using NFSv4 I/O\n", __func__);
194 server->pnfs_curr_ld = NULL;
195}
Fred Isaman02c35fc2010-10-20 00:17:59 -0400196
197int
198pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
199{
200 int status = -EINVAL;
201 struct pnfs_layoutdriver_type *tmp;
202
203 if (ld_type->id == 0) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500204 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
Fred Isaman02c35fc2010-10-20 00:17:59 -0400205 return status;
206 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400207 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500208 printk(KERN_ERR "NFS: %s Layout driver must provide "
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400209 "alloc_lseg and free_lseg.\n", __func__);
210 return status;
211 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400212
213 spin_lock(&pnfs_spinlock);
214 tmp = find_pnfs_driver_locked(ld_type->id);
215 if (!tmp) {
216 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
217 status = 0;
218 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
219 ld_type->name);
220 } else {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500221 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
Fred Isaman02c35fc2010-10-20 00:17:59 -0400222 __func__, ld_type->id);
223 }
224 spin_unlock(&pnfs_spinlock);
225
226 return status;
227}
228EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
229
230void
231pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
232{
233 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
234 spin_lock(&pnfs_spinlock);
235 list_del(&ld_type->pnfs_tblid);
236 spin_unlock(&pnfs_spinlock);
237}
238EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
Benny Halevye5e94012010-10-20 00:18:01 -0400239
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400240/*
241 * pNFS client layout cache
242 */
243
Fred Isamancc6e5342011-01-06 11:36:28 +0000244/* Need to hold i_lock if caller does not already hold reference */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000245void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400246pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
Benny Halevye5e94012010-10-20 00:18:01 -0400247{
Fred Isamancc6e5342011-01-06 11:36:28 +0000248 atomic_inc(&lo->plh_refcount);
249}
250
Benny Halevy636fb9c2011-05-22 19:51:33 +0300251static struct pnfs_layout_hdr *
252pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
253{
254 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
Trond Myklebust57934272012-09-20 20:37:23 -0400255 return ld->alloc_layout_hdr(ino, gfp_flags);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300256}
257
258static void
259pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
260{
Trond Myklebust9c626382012-09-20 17:31:43 -0400261 struct nfs_server *server = NFS_SERVER(lo->plh_inode);
262 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
263
264 if (!list_empty(&lo->plh_layouts)) {
265 struct nfs_client *clp = server->nfs_client;
266
267 spin_lock(&clp->cl_lock);
268 list_del_init(&lo->plh_layouts);
269 spin_unlock(&clp->cl_lock);
270 }
Peng Tao9fa40752011-07-30 20:52:32 -0400271 put_rpccred(lo->plh_lc_cred);
Trond Myklebust57934272012-09-20 20:37:23 -0400272 return ld->free_layout_hdr(lo);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300273}
274
Fred Isamancc6e5342011-01-06 11:36:28 +0000275static void
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400276pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
Fred Isamancc6e5342011-01-06 11:36:28 +0000277{
Trond Myklebustbb346f62012-09-20 15:52:13 -0400278 struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
Fred Isamancc6e5342011-01-06 11:36:28 +0000279 dprintk("%s: freeing layout cache %p\n", __func__, lo);
Trond Myklebustbb346f62012-09-20 15:52:13 -0400280 nfsi->layout = NULL;
281 /* Reset MDS Threshold I/O counters */
282 nfsi->write_io = 0;
283 nfsi->read_io = 0;
Benny Halevye5e94012010-10-20 00:18:01 -0400284}
285
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400286void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400287pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400288{
Fred Isamancc6e5342011-01-06 11:36:28 +0000289 struct inode *inode = lo->plh_inode;
290
Trond Myklebust13c13a62016-01-26 23:12:11 -0500291 pnfs_layoutreturn_before_put_layout_hdr(lo);
292
Fred Isamancc6e5342011-01-06 11:36:28 +0000293 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
Peng Tao566f8732014-10-10 23:25:46 +0800294 if (!list_empty(&lo->plh_segs))
295 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400296 pnfs_detach_layout_hdr(lo);
Fred Isamancc6e5342011-01-06 11:36:28 +0000297 spin_unlock(&inode->i_lock);
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400298 pnfs_free_layout_hdr(lo);
Fred Isamancc6e5342011-01-06 11:36:28 +0000299 }
Andy Adamson974cec82010-10-20 00:18:02 -0400300}
301
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500302/*
303 * Mark a pnfs_layout_hdr and all associated layout segments as invalid
304 *
305 * In order to continue using the pnfs_layout_hdr, a full recovery
306 * is required.
307 * Note that caller must hold inode->i_lock.
308 */
Trond Myklebust5f46be02016-07-22 11:25:27 -0400309int
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500310pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
311 struct list_head *lseg_list)
312{
313 struct pnfs_layout_range range = {
314 .iomode = IOMODE_ANY,
315 .offset = 0,
316 .length = NFS4_MAX_UINT64,
317 };
318
319 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Jeff Layton6d597e12016-05-17 12:28:42 -0400320 return pnfs_mark_matching_lsegs_invalid(lo, lseg_list, &range, 0);
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500321}
322
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400323static int
324pnfs_iomode_to_fail_bit(u32 iomode)
325{
326 return iomode == IOMODE_RW ?
327 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
328}
329
330static void
Trond Myklebust3e621212012-09-24 13:07:16 -0400331pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400332{
Trond Myklebust25c75332012-09-18 17:01:12 -0400333 lo->plh_retry_timestamp = jiffies;
Yanchuan Nian39e88fc2013-01-04 20:19:49 +0800334 if (!test_and_set_bit(fail_bit, &lo->plh_flags))
Trond Myklebust3e621212012-09-24 13:07:16 -0400335 atomic_inc(&lo->plh_refcount);
336}
337
338static void
339pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
340{
341 if (test_and_clear_bit(fail_bit, &lo->plh_flags))
342 atomic_dec(&lo->plh_refcount);
343}
344
345static void
346pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
347{
348 struct inode *inode = lo->plh_inode;
Trond Myklebust115ce572012-09-20 21:19:43 -0400349 struct pnfs_layout_range range = {
350 .iomode = iomode,
351 .offset = 0,
352 .length = NFS4_MAX_UINT64,
353 };
354 LIST_HEAD(head);
Trond Myklebust3e621212012-09-24 13:07:16 -0400355
356 spin_lock(&inode->i_lock);
357 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
Jeff Layton6d597e12016-05-17 12:28:42 -0400358 pnfs_mark_matching_lsegs_invalid(lo, &head, &range, 0);
Trond Myklebust3e621212012-09-24 13:07:16 -0400359 spin_unlock(&inode->i_lock);
Trond Myklebust115ce572012-09-20 21:19:43 -0400360 pnfs_free_lseg_list(&head);
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400361 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
362 iomode == IOMODE_RW ? "RW" : "READ");
363}
364
365static bool
366pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
367{
Trond Myklebust25c75332012-09-18 17:01:12 -0400368 unsigned long start, end;
Trond Myklebust3e621212012-09-24 13:07:16 -0400369 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
370
371 if (test_bit(fail_bit, &lo->plh_flags) == 0)
Trond Myklebust25c75332012-09-18 17:01:12 -0400372 return false;
373 end = jiffies;
374 start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
375 if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
376 /* It is time to retry the failed layoutgets */
Trond Myklebust3e621212012-09-24 13:07:16 -0400377 pnfs_layout_clear_fail_bit(lo, fail_bit);
Trond Myklebust25c75332012-09-18 17:01:12 -0400378 return false;
379 }
380 return true;
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400381}
382
Andy Adamson974cec82010-10-20 00:18:02 -0400383static void
Trond Myklebust119cef972016-07-24 15:10:12 -0400384pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
385 const struct pnfs_layout_range *range,
386 const nfs4_stateid *stateid)
Andy Adamson974cec82010-10-20 00:18:02 -0400387{
Fred Isaman566052c2011-01-06 11:36:20 +0000388 INIT_LIST_HEAD(&lseg->pls_list);
Peng Taoa9bae562011-07-30 20:52:33 -0400389 INIT_LIST_HEAD(&lseg->pls_lc_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000390 atomic_set(&lseg->pls_refcount, 1);
Fred Isaman4541d162011-01-06 11:36:23 +0000391 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000392 lseg->pls_layout = lo;
Trond Myklebust119cef972016-07-24 15:10:12 -0400393 lseg->pls_range = *range;
394 lseg->pls_seq = be32_to_cpu(stateid->seqid);
Andy Adamson974cec82010-10-20 00:18:02 -0400395}
396
Trond Myklebust905ca192012-09-20 20:46:49 -0400397static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400398{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000399 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400400
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400401 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400402}
403
Fred Isamand684d2a2011-03-01 01:34:13 +0000404static void
Trond Myklebust57036a32012-09-20 16:33:30 -0400405pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
406 struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400407{
Trond Myklebust57036a32012-09-20 16:33:30 -0400408 struct inode *inode = lo->plh_inode;
Fred Isamand684d2a2011-03-01 01:34:13 +0000409
Benny Halevyd20581a2011-05-22 19:52:03 +0300410 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000411 list_del_init(&lseg->pls_list);
Trond Myklebust8f0d27d2012-09-20 20:57:11 -0400412 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
413 atomic_dec(&lo->plh_refcount);
Trond Myklebust2d148c72016-06-17 16:48:26 -0400414 if (list_empty(&lo->plh_segs)) {
Trond Myklebust334a8f32016-09-04 12:46:35 -0400415 if (atomic_read(&lo->plh_outstanding) == 0)
416 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Trond Myklebust173f77e2012-09-21 15:49:42 -0400417 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Trond Myklebust2d148c72016-06-17 16:48:26 -0400418 }
Fred Isamand684d2a2011-03-01 01:34:13 +0000419 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
420}
421
Fred Isamanbae724e2011-03-01 01:34:15 +0000422void
Trond Myklebust9369a432012-09-18 20:57:08 -0400423pnfs_put_lseg(struct pnfs_layout_segment *lseg)
Fred Isamand684d2a2011-03-01 01:34:13 +0000424{
Trond Myklebust57036a32012-09-20 16:33:30 -0400425 struct pnfs_layout_hdr *lo;
Fred Isamand684d2a2011-03-01 01:34:13 +0000426 struct inode *inode;
427
428 if (!lseg)
429 return;
430
Fred Isaman4541d162011-01-06 11:36:23 +0000431 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
432 atomic_read(&lseg->pls_refcount),
433 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500434
Trond Myklebust57036a32012-09-20 16:33:30 -0400435 lo = lseg->pls_layout;
436 inode = lo->plh_inode;
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500437
Fred Isamand684d2a2011-03-01 01:34:13 +0000438 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
Trond Myklebustfaa4a542015-07-09 18:24:07 +0200439 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
440 spin_unlock(&inode->i_lock);
441 return;
442 }
Trond Myklebust8f0d27d2012-09-20 20:57:11 -0400443 pnfs_get_layout_hdr(lo);
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500444 pnfs_layout_remove_lseg(lo, lseg);
445 spin_unlock(&inode->i_lock);
446 pnfs_free_lseg(lseg);
447 pnfs_put_layout_hdr(lo);
Fred Isaman4541d162011-01-06 11:36:23 +0000448 }
Andy Adamson974cec82010-10-20 00:18:02 -0400449}
Trond Myklebust9369a432012-09-18 20:57:08 -0400450EXPORT_SYMBOL_GPL(pnfs_put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400451
Trond Myklebust6543f802014-10-08 16:39:12 -0400452static void pnfs_free_lseg_async_work(struct work_struct *work)
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400453{
454 struct pnfs_layout_segment *lseg;
Trond Myklebust6543f802014-10-08 16:39:12 -0400455 struct pnfs_layout_hdr *lo;
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400456
457 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
Trond Myklebust6543f802014-10-08 16:39:12 -0400458 lo = lseg->pls_layout;
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400459
Trond Myklebust6543f802014-10-08 16:39:12 -0400460 pnfs_free_lseg(lseg);
461 pnfs_put_layout_hdr(lo);
462}
463
464static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg)
465{
466 INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work);
467 schedule_work(&lseg->pls_work);
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400468}
469
470void
Trond Myklebust6543f802014-10-08 16:39:12 -0400471pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg)
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400472{
Trond Myklebust6543f802014-10-08 16:39:12 -0400473 if (!lseg)
474 return;
475
476 assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock);
477
478 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
479 atomic_read(&lseg->pls_refcount),
480 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
481 if (atomic_dec_and_test(&lseg->pls_refcount)) {
482 struct pnfs_layout_hdr *lo = lseg->pls_layout;
Trond Myklebustfaa4a542015-07-09 18:24:07 +0200483 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
484 return;
Trond Myklebust6543f802014-10-08 16:39:12 -0400485 pnfs_get_layout_hdr(lo);
486 pnfs_layout_remove_lseg(lo, lseg);
487 pnfs_free_lseg_async(lseg);
488 }
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400489}
Trond Myklebust6543f802014-10-08 16:39:12 -0400490EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked);
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400491
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400492static u64
Benny Halevyfb3296e2011-05-22 19:47:26 +0300493end_offset(u64 start, u64 len)
Fred Isaman4541d162011-01-06 11:36:23 +0000494{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300495 u64 end;
496
497 end = start + len;
498 return end >= start ? end : NFS4_MAX_UINT64;
499}
500
Benny Halevyfb3296e2011-05-22 19:47:26 +0300501/*
502 * is l2 fully contained in l1?
503 * start1 end1
504 * [----------------------------------)
505 * start2 end2
506 * [----------------)
507 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400508static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -0400509pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400510 const struct pnfs_layout_range *l2)
Benny Halevyfb3296e2011-05-22 19:47:26 +0300511{
512 u64 start1 = l1->offset;
513 u64 end1 = end_offset(start1, l1->length);
514 u64 start2 = l2->offset;
515 u64 end2 = end_offset(start2, l2->length);
516
517 return (start1 <= start2) && (end1 >= end2);
518}
519
520/*
521 * is l1 and l2 intersecting?
522 * start1 end1
523 * [----------------------------------)
524 * start2 end2
525 * [----------------)
526 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400527static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -0400528pnfs_lseg_range_intersecting(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400529 const struct pnfs_layout_range *l2)
Benny Halevyfb3296e2011-05-22 19:47:26 +0300530{
531 u64 start1 = l1->offset;
532 u64 end1 = end_offset(start1, l1->length);
533 u64 start2 = l2->offset;
534 u64 end2 = end_offset(start2, l2->length);
535
536 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
537 (end2 == NFS4_MAX_UINT64 || end2 > start1);
538}
539
Trond Myklebust24956802013-03-20 13:03:00 -0400540static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
541 struct list_head *tmp_list)
542{
543 if (!atomic_dec_and_test(&lseg->pls_refcount))
544 return false;
545 pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
546 list_add(&lseg->pls_list, tmp_list);
547 return true;
548}
549
Fred Isaman4541d162011-01-06 11:36:23 +0000550/* Returns 1 if lseg is removed from list, 0 otherwise */
551static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
552 struct list_head *tmp_list)
553{
554 int rv = 0;
555
556 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
557 /* Remove the reference keeping the lseg in the
558 * list. It will now be removed when all
559 * outstanding io is finished.
560 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000561 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
562 atomic_read(&lseg->pls_refcount));
Trond Myklebust24956802013-03-20 13:03:00 -0400563 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
Fred Isamand684d2a2011-03-01 01:34:13 +0000564 rv = 1;
Fred Isaman4541d162011-01-06 11:36:23 +0000565 }
566 return rv;
567}
568
Jeff Layton6d597e12016-05-17 12:28:42 -0400569/*
570 * Compare 2 layout stateid sequence ids, to see which is newer,
571 * taking into account wraparound issues.
572 */
573static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
574{
575 return (s32)(s1 - s2) > 0;
576}
577
Trond Myklebuste036f462016-07-22 11:13:22 -0400578static bool
579pnfs_should_free_range(const struct pnfs_layout_range *lseg_range,
580 const struct pnfs_layout_range *recall_range)
581{
582 return (recall_range->iomode == IOMODE_ANY ||
583 lseg_range->iomode == recall_range->iomode) &&
584 pnfs_lseg_range_intersecting(lseg_range, recall_range);
585}
586
587static bool
588pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg,
589 const struct pnfs_layout_range *recall_range,
590 u32 seq)
591{
592 if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq))
593 return false;
594 if (recall_range == NULL)
595 return true;
596 return pnfs_should_free_range(&lseg->pls_range, recall_range);
597}
598
Jeff Layton6d597e12016-05-17 12:28:42 -0400599/**
600 * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later
601 * @lo: layout header containing the lsegs
602 * @tmp_list: list head where doomed lsegs should go
603 * @recall_range: optional recall range argument to match (may be NULL)
604 * @seq: only invalidate lsegs obtained prior to this sequence (may be 0)
605 *
606 * Walk the list of lsegs in the layout header, and tear down any that should
607 * be destroyed. If "recall_range" is specified then the segment must match
608 * that range. If "seq" is non-zero, then only match segments that were handed
609 * out at or before that sequence.
610 *
611 * Returns number of matching invalid lsegs remaining in list after scanning
612 * it and purging them.
Fred Isaman4541d162011-01-06 11:36:23 +0000613 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000614int
Trond Myklebust49a85062012-09-18 20:43:31 -0400615pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
Fred Isaman4541d162011-01-06 11:36:23 +0000616 struct list_head *tmp_list,
Jeff Layton6d597e12016-05-17 12:28:42 -0400617 const struct pnfs_layout_range *recall_range,
618 u32 seq)
Andy Adamson974cec82010-10-20 00:18:02 -0400619{
620 struct pnfs_layout_segment *lseg, *next;
Trond Myklebust71b39852016-01-04 12:41:15 -0500621 int remaining = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400622
623 dprintk("%s:Begin lo %p\n", __func__, lo);
624
Trond Myklebust8006bfb2012-09-21 14:48:04 -0400625 if (list_empty(&lo->plh_segs))
Fred Isaman38511722011-02-03 18:28:50 +0000626 return 0;
Fred Isaman4541d162011-01-06 11:36:23 +0000627 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Trond Myklebuste036f462016-07-22 11:13:22 -0400628 if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
Jeff Layton6d597e12016-05-17 12:28:42 -0400629 dprintk("%s: freeing lseg %p iomode %d seq %u"
Fred Isaman4541d162011-01-06 11:36:23 +0000630 "offset %llu length %llu\n", __func__,
Jeff Layton6d597e12016-05-17 12:28:42 -0400631 lseg, lseg->pls_range.iomode, lseg->pls_seq,
632 lseg->pls_range.offset, lseg->pls_range.length);
Trond Myklebust71b39852016-01-04 12:41:15 -0500633 if (!mark_lseg_invalid(lseg, tmp_list))
634 remaining++;
Fred Isaman4541d162011-01-06 11:36:23 +0000635 }
Trond Myklebust71b39852016-01-04 12:41:15 -0500636 dprintk("%s:Return %i\n", __func__, remaining);
637 return remaining;
Andy Adamson974cec82010-10-20 00:18:02 -0400638}
639
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000640/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000641void
Fred Isaman4541d162011-01-06 11:36:23 +0000642pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400643{
Fred Isaman4541d162011-01-06 11:36:23 +0000644 struct pnfs_layout_segment *lseg, *tmp;
Andy Adamson974cec82010-10-20 00:18:02 -0400645
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000646 if (list_empty(free_me))
647 return;
648
Fred Isaman4541d162011-01-06 11:36:23 +0000649 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000650 list_del(&lseg->pls_list);
Trond Myklebust905ca192012-09-20 20:46:49 -0400651 pnfs_free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400652 }
653}
654
Benny Halevye5e94012010-10-20 00:18:01 -0400655void
656pnfs_destroy_layout(struct nfs_inode *nfsi)
657{
658 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400659 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400660
661 spin_lock(&nfsi->vfs_inode.i_lock);
662 lo = nfsi->layout;
663 if (lo) {
Trond Myklebust3e621212012-09-24 13:07:16 -0400664 pnfs_get_layout_hdr(lo);
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500665 pnfs_mark_layout_stateid_invalid(lo, &tmp_list);
Trond Myklebust3e621212012-09-24 13:07:16 -0400666 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
667 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
668 spin_unlock(&nfsi->vfs_inode.i_lock);
669 pnfs_free_lseg_list(&tmp_list);
670 pnfs_put_layout_hdr(lo);
671 } else
672 spin_unlock(&nfsi->vfs_inode.i_lock);
Benny Halevye5e94012010-10-20 00:18:01 -0400673}
Andy Adamson041245c2012-04-27 17:53:53 -0400674EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
Benny Halevye5e94012010-10-20 00:18:01 -0400675
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500676static bool
677pnfs_layout_add_bulk_destroy_list(struct inode *inode,
678 struct list_head *layout_list)
679{
680 struct pnfs_layout_hdr *lo;
681 bool ret = false;
682
683 spin_lock(&inode->i_lock);
684 lo = NFS_I(inode)->layout;
685 if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
686 pnfs_get_layout_hdr(lo);
687 list_add(&lo->plh_bulk_destroy, layout_list);
688 ret = true;
689 }
690 spin_unlock(&inode->i_lock);
691 return ret;
692}
693
694/* Caller must hold rcu_read_lock and clp->cl_lock */
695static int
696pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
697 struct nfs_server *server,
698 struct list_head *layout_list)
699{
700 struct pnfs_layout_hdr *lo, *next;
701 struct inode *inode;
702
703 list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
704 inode = igrab(lo->plh_inode);
705 if (inode == NULL)
706 continue;
707 list_del_init(&lo->plh_layouts);
708 if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
709 continue;
710 rcu_read_unlock();
711 spin_unlock(&clp->cl_lock);
712 iput(inode);
713 spin_lock(&clp->cl_lock);
714 rcu_read_lock();
715 return -EAGAIN;
716 }
717 return 0;
718}
719
720static int
721pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
722 bool is_bulk_recall)
723{
724 struct pnfs_layout_hdr *lo;
725 struct inode *inode;
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500726 LIST_HEAD(lseg_list);
727 int ret = 0;
728
729 while (!list_empty(layout_list)) {
730 lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
731 plh_bulk_destroy);
732 dprintk("%s freeing layout for inode %lu\n", __func__,
733 lo->plh_inode->i_ino);
734 inode = lo->plh_inode;
Christoph Hellwig7c5d1872014-09-10 08:23:29 -0700735
736 pnfs_layoutcommit_inode(inode, false);
737
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500738 spin_lock(&inode->i_lock);
739 list_del_init(&lo->plh_bulk_destroy);
Trond Myklebust9fd4b9f2016-02-22 17:46:34 -0500740 if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
741 if (is_bulk_recall)
742 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500743 ret = -EAGAIN;
Trond Myklebust9fd4b9f2016-02-22 17:46:34 -0500744 }
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500745 spin_unlock(&inode->i_lock);
746 pnfs_free_lseg_list(&lseg_list);
Trond Myklebustb20135d2015-12-31 09:28:06 -0500747 /* Free all lsegs that are attached to commit buckets */
748 nfs_commit_inode(inode, 0);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500749 pnfs_put_layout_hdr(lo);
750 iput(inode);
751 }
752 return ret;
753}
754
755int
756pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
757 struct nfs_fsid *fsid,
758 bool is_recall)
759{
760 struct nfs_server *server;
761 LIST_HEAD(layout_list);
762
763 spin_lock(&clp->cl_lock);
764 rcu_read_lock();
765restart:
766 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
767 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
768 continue;
769 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
770 server,
771 &layout_list) != 0)
772 goto restart;
773 }
774 rcu_read_unlock();
775 spin_unlock(&clp->cl_lock);
776
777 if (list_empty(&layout_list))
778 return 0;
779 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
780}
781
782int
783pnfs_destroy_layouts_byclid(struct nfs_client *clp,
784 bool is_recall)
785{
786 struct nfs_server *server;
787 LIST_HEAD(layout_list);
788
789 spin_lock(&clp->cl_lock);
790 rcu_read_lock();
791restart:
792 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
793 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
794 server,
795 &layout_list) != 0)
796 goto restart;
797 }
798 rcu_read_unlock();
799 spin_unlock(&clp->cl_lock);
800
801 if (list_empty(&layout_list))
802 return 0;
803 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
804}
805
Andy Adamson974cec82010-10-20 00:18:02 -0400806/*
807 * Called by the state manger to remove all layouts established under an
808 * expired lease.
809 */
810void
811pnfs_destroy_all_layouts(struct nfs_client *clp)
812{
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400813 nfs4_deviceid_mark_client_invalid(clp);
814 nfs4_deviceid_purge_client(clp);
815
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500816 pnfs_destroy_layouts_byclid(clp, false);
Andy Adamson974cec82010-10-20 00:18:02 -0400817}
818
Trond Myklebust2a59a042016-09-03 11:20:04 -0400819static void
820pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo)
821{
822 lo->plh_return_iomode = 0;
823 lo->plh_return_seq = 0;
824 clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
825}
826
Fred Isamanfd6002e2011-01-06 11:36:22 +0000827/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000828void
829pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
830 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400831{
Trond Myklebustecebb802016-07-24 11:46:06 -0400832 u32 oldseq, newseq, new_barrier = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400833
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500834 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
835 newseq = be32_to_cpu(new->seqid);
Trond Myklebust2a59a042016-09-03 11:20:04 -0400836
837 if (!pnfs_layout_is_valid(lo)) {
838 nfs4_stateid_copy(&lo->plh_stateid, new);
839 lo->plh_barrier = newseq;
840 pnfs_clear_layoutreturn_info(lo);
841 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
842 return;
843 }
844 if (pnfs_seqid_is_newer(newseq, oldseq)) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500845 nfs4_stateid_copy(&lo->plh_stateid, new);
Trond Myklebustecebb802016-07-24 11:46:06 -0400846 /*
847 * Because of wraparound, we want to keep the barrier
848 * "close" to the current seqids.
849 */
850 new_barrier = newseq - atomic_read(&lo->plh_outstanding);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000851 }
Trond Myklebustecebb802016-07-24 11:46:06 -0400852 if (update_barrier)
853 new_barrier = be32_to_cpu(new->seqid);
854 else if (new_barrier == 0)
855 return;
Trond Myklebust2a59a042016-09-03 11:20:04 -0400856 if (pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
Trond Myklebustecebb802016-07-24 11:46:06 -0400857 lo->plh_barrier = new_barrier;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400858}
859
Trond Myklebust19c54ab2012-10-05 16:56:58 -0700860static bool
861pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
862 const nfs4_stateid *stateid)
863{
864 u32 seqid = be32_to_cpu(stateid->seqid);
865
866 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
867}
868
Fred Isamancf7d63f2011-01-06 11:36:25 +0000869/* lget is set to 1 if called from inside send_layoutget call chain */
870static bool
Trond Myklebuste1c06f82015-08-04 16:40:08 -0400871pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000872{
Fred Isamanf7e89172011-01-06 11:36:32 +0000873 return lo->plh_block_lgets ||
Trond Myklebuste1c06f82015-08-04 16:40:08 -0400874 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000875}
876
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400877/*
Jeff Layton183d9e72016-05-17 12:28:47 -0400878 * Get layout from server.
879 * for now, assume that whole file layouts are requested.
880 * arg->offset: 0
881 * arg->length: all ones
882 */
Benny Halevye5e94012010-10-20 00:18:01 -0400883static struct pnfs_layout_segment *
884send_layoutget(struct pnfs_layout_hdr *lo,
885 struct nfs_open_context *ctx,
Jeff Layton183d9e72016-05-17 12:28:47 -0400886 nfs4_stateid *stateid,
Trond Myklebuste144e532016-01-04 12:52:53 -0500887 const struct pnfs_layout_range *range,
Jeff Layton183d9e72016-05-17 12:28:47 -0400888 long *timeout, gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400889{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000890 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400891 struct nfs_server *server = NFS_SERVER(ino);
892 struct nfs4_layoutget *lgp;
Trond Myklebust2d89a1d2015-08-31 02:05:47 -0700893 loff_t i_size;
Benny Halevye5e94012010-10-20 00:18:01 -0400894
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400895 dprintk("--> %s\n", __func__);
896
Jeff Layton4f2e9dc2015-11-25 13:43:14 -0500897 /*
898 * Synchronously retrieve layout information from server and
899 * store in lseg. If we race with a concurrent seqid morphing
900 * op, then re-send the LAYOUTGET.
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400901 */
Jeff Layton83026d82016-05-17 12:28:46 -0400902 lgp = kzalloc(sizeof(*lgp), gfp_flags);
903 if (lgp == NULL)
904 return ERR_PTR(-ENOMEM);
Jeff Layton4f2e9dc2015-11-25 13:43:14 -0500905
Jeff Layton83026d82016-05-17 12:28:46 -0400906 i_size = i_size_read(ino);
Jeff Layton4f2e9dc2015-11-25 13:43:14 -0500907
Jeff Layton83026d82016-05-17 12:28:46 -0400908 lgp->args.minlength = PAGE_SIZE;
909 if (lgp->args.minlength > range->length)
910 lgp->args.minlength = range->length;
911 if (range->iomode == IOMODE_READ) {
912 if (range->offset >= i_size)
913 lgp->args.minlength = 0;
914 else if (i_size - range->offset < lgp->args.minlength)
915 lgp->args.minlength = i_size - range->offset;
Jeff Laytond03ab292016-05-17 12:28:45 -0400916 }
Jeff Layton83026d82016-05-17 12:28:46 -0400917 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
918 pnfs_copy_range(&lgp->args.range, range);
919 lgp->args.type = server->pnfs_curr_ld->id;
920 lgp->args.inode = ino;
921 lgp->args.ctx = get_nfs_open_context(ctx);
Jeff Layton183d9e72016-05-17 12:28:47 -0400922 nfs4_stateid_copy(&lgp->args.stateid, stateid);
Jeff Layton83026d82016-05-17 12:28:46 -0400923 lgp->gfp_flags = gfp_flags;
924 lgp->cred = lo->plh_lc_cred;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400925
Jeff Layton183d9e72016-05-17 12:28:47 -0400926 return nfs4_proc_layoutget(lgp, timeout, gfp_flags);
Andy Adamson974cec82010-10-20 00:18:02 -0400927}
928
Trond Myklebust24956802013-03-20 13:03:00 -0400929static void pnfs_clear_layoutcommit(struct inode *inode,
930 struct list_head *head)
931{
932 struct nfs_inode *nfsi = NFS_I(inode);
933 struct pnfs_layout_segment *lseg, *tmp;
934
935 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
936 return;
937 list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
938 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
939 continue;
940 pnfs_lseg_dec_and_remove_zero(lseg, head);
941 }
942}
943
Tom Haynesd67ae822014-12-11 17:02:04 -0500944void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
945{
946 clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
947 smp_mb__after_atomic();
948 wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
Trond Myklebust7f273922015-07-09 18:40:01 +0200949 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
Tom Haynesd67ae822014-12-11 17:02:04 -0500950}
951
Trond Myklebust13c13a62016-01-26 23:12:11 -0500952static bool
Trond Myklebuste5fd1902016-07-21 12:44:15 -0400953pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
954 nfs4_stateid *stateid,
955 enum pnfs_iomode *iomode)
Trond Myklebust13c13a62016-01-26 23:12:11 -0500956{
Trond Myklebustbf0291d2016-09-03 10:39:51 -0400957 /* Serialise LAYOUTGET/LAYOUTRETURN */
958 if (atomic_read(&lo->plh_outstanding) != 0)
959 return false;
Trond Myklebust13c13a62016-01-26 23:12:11 -0500960 if (test_and_set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
961 return false;
Trond Myklebust13c13a62016-01-26 23:12:11 -0500962 pnfs_get_layout_hdr(lo);
Trond Myklebuste5fd1902016-07-21 12:44:15 -0400963 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
964 if (stateid != NULL) {
965 nfs4_stateid_copy(stateid, &lo->plh_stateid);
966 if (lo->plh_return_seq != 0)
967 stateid->seqid = cpu_to_be32(lo->plh_return_seq);
968 }
969 if (iomode != NULL)
970 *iomode = lo->plh_return_iomode;
971 pnfs_clear_layoutreturn_info(lo);
972 return true;
973 }
974 if (stateid != NULL)
975 nfs4_stateid_copy(stateid, &lo->plh_stateid);
976 if (iomode != NULL)
977 *iomode = IOMODE_ANY;
Trond Myklebust13c13a62016-01-26 23:12:11 -0500978 return true;
979}
980
Peng Taof40eb5d2014-09-06 00:53:22 +0800981static int
Trond Myklebusted429d62016-01-04 12:30:55 -0500982pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, const nfs4_stateid *stateid,
Peng Tao6c166052014-11-17 09:30:40 +0800983 enum pnfs_iomode iomode, bool sync)
Peng Taof40eb5d2014-09-06 00:53:22 +0800984{
985 struct inode *ino = lo->plh_inode;
986 struct nfs4_layoutreturn *lrp;
987 int status = 0;
988
Trond Myklebuste4af4402015-02-05 17:05:08 -0500989 lrp = kzalloc(sizeof(*lrp), GFP_NOFS);
Peng Taof40eb5d2014-09-06 00:53:22 +0800990 if (unlikely(lrp == NULL)) {
991 status = -ENOMEM;
992 spin_lock(&ino->i_lock);
Tom Haynesd67ae822014-12-11 17:02:04 -0500993 pnfs_clear_layoutreturn_waitbit(lo);
Peng Taof40eb5d2014-09-06 00:53:22 +0800994 spin_unlock(&ino->i_lock);
995 pnfs_put_layout_hdr(lo);
996 goto out;
997 }
998
Trond Myklebusted429d62016-01-04 12:30:55 -0500999 nfs4_stateid_copy(&lrp->args.stateid, stateid);
Peng Taof40eb5d2014-09-06 00:53:22 +08001000 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
1001 lrp->args.inode = ino;
Peng Tao15eb67c2014-11-17 09:30:36 +08001002 lrp->args.range.iomode = iomode;
1003 lrp->args.range.offset = 0;
1004 lrp->args.range.length = NFS4_MAX_UINT64;
Peng Taof40eb5d2014-09-06 00:53:22 +08001005 lrp->args.layout = lo;
1006 lrp->clp = NFS_SERVER(ino)->nfs_client;
1007 lrp->cred = lo->plh_lc_cred;
1008
Peng Tao6c166052014-11-17 09:30:40 +08001009 status = nfs4_proc_layoutreturn(lrp, sync);
Peng Taof40eb5d2014-09-06 00:53:22 +08001010out:
1011 dprintk("<-- %s status: %d\n", __func__, status);
1012 return status;
1013}
1014
Trond Myklebust13c13a62016-01-26 23:12:11 -05001015/* Return true if layoutreturn is needed */
1016static bool
1017pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
1018{
1019 struct pnfs_layout_segment *s;
1020
Trond Myklebust2370abd2016-01-27 20:32:50 -05001021 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
Trond Myklebust13c13a62016-01-26 23:12:11 -05001022 return false;
1023
1024 /* Defer layoutreturn until all lsegs are done */
1025 list_for_each_entry(s, &lo->plh_segs, pls_list) {
1026 if (test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
1027 return false;
1028 }
1029
1030 return true;
1031}
1032
1033static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
1034{
1035 struct inode *inode= lo->plh_inode;
1036
Trond Myklebust2370abd2016-01-27 20:32:50 -05001037 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
Trond Myklebust13c13a62016-01-26 23:12:11 -05001038 return;
1039 spin_lock(&inode->i_lock);
1040 if (pnfs_layout_need_return(lo)) {
1041 nfs4_stateid stateid;
1042 enum pnfs_iomode iomode;
1043 bool send;
1044
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001045 send = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
Trond Myklebust13c13a62016-01-26 23:12:11 -05001046 spin_unlock(&inode->i_lock);
1047 if (send) {
1048 /* Send an async layoutreturn so we dont deadlock */
1049 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1050 }
1051 } else
1052 spin_unlock(&inode->i_lock);
1053}
1054
Andy Adamson293b3b02012-06-20 15:03:34 -04001055/*
1056 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
1057 * when the layout segment list is empty.
1058 *
1059 * Note that a pnfs_layout_hdr can exist with an empty layout segment
1060 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
1061 * deviceid is marked invalid.
1062 */
Benny Halevycbe82602011-05-22 19:52:37 +03001063int
1064_pnfs_return_layout(struct inode *ino)
1065{
1066 struct pnfs_layout_hdr *lo = NULL;
1067 struct nfs_inode *nfsi = NFS_I(ino);
1068 LIST_HEAD(tmp_list);
Benny Halevycbe82602011-05-22 19:52:37 +03001069 nfs4_stateid stateid;
Andy Adamson293b3b02012-06-20 15:03:34 -04001070 int status = 0, empty;
Trond Myklebust7f273922015-07-09 18:40:01 +02001071 bool send;
Benny Halevycbe82602011-05-22 19:52:37 +03001072
Andy Adamson366d5052012-06-20 15:03:33 -04001073 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
Benny Halevycbe82602011-05-22 19:52:37 +03001074
1075 spin_lock(&ino->i_lock);
1076 lo = nfsi->layout;
Trond Myklebuste5929f32012-09-21 16:37:02 -04001077 if (!lo) {
Benny Halevycbe82602011-05-22 19:52:37 +03001078 spin_unlock(&ino->i_lock);
Andy Adamson293b3b02012-06-20 15:03:34 -04001079 dprintk("NFS: %s no layout to return\n", __func__);
1080 goto out;
Benny Halevycbe82602011-05-22 19:52:37 +03001081 }
Benny Halevycbe82602011-05-22 19:52:37 +03001082 /* Reference matched in nfs4_layoutreturn_release */
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001083 pnfs_get_layout_hdr(lo);
Andy Adamson293b3b02012-06-20 15:03:34 -04001084 empty = list_empty(&lo->plh_segs);
Trond Myklebust24956802013-03-20 13:03:00 -04001085 pnfs_clear_layoutcommit(ino, &tmp_list);
Jeff Layton6d597e12016-05-17 12:28:42 -04001086 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL, 0);
Christoph Hellwigc88953d2014-09-10 08:23:31 -07001087
1088 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
1089 struct pnfs_layout_range range = {
1090 .iomode = IOMODE_ANY,
1091 .offset = 0,
1092 .length = NFS4_MAX_UINT64,
1093 };
1094 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1095 }
1096
Andy Adamson293b3b02012-06-20 15:03:34 -04001097 /* Don't send a LAYOUTRETURN if list was initially empty */
1098 if (empty) {
1099 spin_unlock(&ino->i_lock);
Andy Adamson293b3b02012-06-20 15:03:34 -04001100 dprintk("NFS: %s no layout segments to return\n", __func__);
Trond Myklebust7f273922015-07-09 18:40:01 +02001101 goto out_put_layout_hdr;
Andy Adamson293b3b02012-06-20 15:03:34 -04001102 }
Christoph Hellwig47abade2014-08-21 11:09:22 -05001103
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001104 send = pnfs_prepare_layoutreturn(lo, &stateid, NULL);
Benny Halevycbe82602011-05-22 19:52:37 +03001105 spin_unlock(&ino->i_lock);
1106 pnfs_free_lseg_list(&tmp_list);
Trond Myklebust7f273922015-07-09 18:40:01 +02001107 if (send)
Trond Myklebusted429d62016-01-04 12:30:55 -05001108 status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
Trond Myklebust7f273922015-07-09 18:40:01 +02001109out_put_layout_hdr:
1110 pnfs_put_layout_hdr(lo);
Benny Halevycbe82602011-05-22 19:52:37 +03001111out:
1112 dprintk("<-- %s status: %d\n", __func__, status);
1113 return status;
1114}
Andy Adamson0a57cda2012-04-27 17:53:50 -04001115EXPORT_SYMBOL_GPL(_pnfs_return_layout);
Benny Halevycbe82602011-05-22 19:52:37 +03001116
Trond Myklebust24028672013-03-20 13:23:33 -04001117int
1118pnfs_commit_and_return_layout(struct inode *inode)
1119{
1120 struct pnfs_layout_hdr *lo;
1121 int ret;
1122
1123 spin_lock(&inode->i_lock);
1124 lo = NFS_I(inode)->layout;
1125 if (lo == NULL) {
1126 spin_unlock(&inode->i_lock);
1127 return 0;
1128 }
1129 pnfs_get_layout_hdr(lo);
1130 /* Block new layoutgets and read/write to ds */
1131 lo->plh_block_lgets++;
1132 spin_unlock(&inode->i_lock);
1133 filemap_fdatawait(inode->i_mapping);
1134 ret = pnfs_layoutcommit_inode(inode, true);
1135 if (ret == 0)
1136 ret = _pnfs_return_layout(inode);
1137 spin_lock(&inode->i_lock);
1138 lo->plh_block_lgets--;
1139 spin_unlock(&inode->i_lock);
1140 pnfs_put_layout_hdr(lo);
1141 return ret;
1142}
1143
Fred Isamanf7e89172011-01-06 11:36:32 +00001144bool pnfs_roc(struct inode *ino)
1145{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001146 struct nfs_inode *nfsi = NFS_I(ino);
1147 struct nfs_open_context *ctx;
1148 struct nfs4_state *state;
Fred Isamanf7e89172011-01-06 11:36:32 +00001149 struct pnfs_layout_hdr *lo;
1150 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao193e3aa2014-11-17 09:30:41 +08001151 nfs4_stateid stateid;
Fred Isamanf7e89172011-01-06 11:36:32 +00001152 LIST_HEAD(tmp_list);
Peng Taoe755d632015-08-19 13:49:19 +08001153 bool found = false, layoutreturn = false, roc = false;
Fred Isamanf7e89172011-01-06 11:36:32 +00001154
1155 spin_lock(&ino->i_lock);
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001156 lo = nfsi->layout;
Peng Tao39761432015-08-21 12:49:44 +08001157 if (!lo || test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001158 goto out_noroc;
1159
Peng Taoe755d632015-08-19 13:49:19 +08001160 /* no roc if we hold a delegation */
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001161 if (nfs4_check_delegation(ino, FMODE_READ))
1162 goto out_noroc;
1163
1164 list_for_each_entry(ctx, &nfsi->open_files, list) {
1165 state = ctx->state;
1166 /* Don't return layout if there is open file state */
1167 if (state != NULL && state->state != 0)
1168 goto out_noroc;
1169 }
1170
Peng Taoe755d632015-08-19 13:49:19 +08001171 /* always send layoutreturn if being marked so */
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001172 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1173 layoutreturn = pnfs_prepare_layoutreturn(lo,
1174 &stateid, NULL);
Peng Taoe755d632015-08-19 13:49:19 +08001175
Fred Isamanf7e89172011-01-06 11:36:32 +00001176 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
Peng Taoe755d632015-08-19 13:49:19 +08001177 /* If we are sending layoutreturn, invalidate all valid lsegs */
1178 if (layoutreturn || test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
Fred Isamanf7e89172011-01-06 11:36:32 +00001179 mark_lseg_invalid(lseg, &tmp_list);
1180 found = true;
1181 }
Peng Tao500d7012015-09-22 11:35:22 +08001182 /* ROC in two conditions:
Peng Taoe755d632015-08-19 13:49:19 +08001183 * 1. there are ROC lsegs
1184 * 2. we don't send layoutreturn
Peng Taoe755d632015-08-19 13:49:19 +08001185 */
Peng Tao500d7012015-09-22 11:35:22 +08001186 if (found && !layoutreturn) {
1187 /* lo ref dropped in pnfs_roc_release() */
1188 pnfs_get_layout_hdr(lo);
Peng Taoe755d632015-08-19 13:49:19 +08001189 roc = true;
Peng Tao500d7012015-09-22 11:35:22 +08001190 }
Peng Taoe755d632015-08-19 13:49:19 +08001191
1192out_noroc:
Fred Isamanf7e89172011-01-06 11:36:32 +00001193 spin_unlock(&ino->i_lock);
1194 pnfs_free_lseg_list(&tmp_list);
Trond Myklebust71401712015-03-25 12:36:13 -04001195 pnfs_layoutcommit_inode(ino, true);
Peng Taoe755d632015-08-19 13:49:19 +08001196 if (layoutreturn)
Trond Myklebusted429d62016-01-04 12:30:55 -05001197 pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
Peng Taoe755d632015-08-19 13:49:19 +08001198 return roc;
Fred Isamanf7e89172011-01-06 11:36:32 +00001199}
1200
1201void pnfs_roc_release(struct inode *ino)
1202{
1203 struct pnfs_layout_hdr *lo;
1204
1205 spin_lock(&ino->i_lock);
1206 lo = NFS_I(ino)->layout;
Trond Myklebust5c4a79f2015-08-04 15:57:13 -04001207 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust6622c3e2012-09-20 17:23:11 -04001208 if (atomic_dec_and_test(&lo->plh_refcount)) {
1209 pnfs_detach_layout_hdr(lo);
1210 spin_unlock(&ino->i_lock);
1211 pnfs_free_layout_hdr(lo);
1212 } else
1213 spin_unlock(&ino->i_lock);
Fred Isamanf7e89172011-01-06 11:36:32 +00001214}
1215
1216void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
1217{
1218 struct pnfs_layout_hdr *lo;
1219
1220 spin_lock(&ino->i_lock);
1221 lo = NFS_I(ino)->layout;
Trond Myklebust0f35ad62012-10-04 16:28:17 -07001222 if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
Fred Isamanf7e89172011-01-06 11:36:32 +00001223 lo->plh_barrier = barrier;
1224 spin_unlock(&ino->i_lock);
Trond Myklebust6a463beb2015-08-20 15:40:47 -05001225 trace_nfs4_layoutreturn_on_close(ino, 0);
Fred Isamanf7e89172011-01-06 11:36:32 +00001226}
1227
Trond Myklebust4ff376f2015-08-18 23:23:21 -05001228void pnfs_roc_get_barrier(struct inode *ino, u32 *barrier)
Fred Isamanf7e89172011-01-06 11:36:32 +00001229{
1230 struct nfs_inode *nfsi = NFS_I(ino);
Trond Myklebust7fdab062012-09-20 20:15:57 -04001231 struct pnfs_layout_hdr *lo;
Trond Myklebust7fdab062012-09-20 20:15:57 -04001232 u32 current_seqid;
Fred Isamanf7e89172011-01-06 11:36:32 +00001233
1234 spin_lock(&ino->i_lock);
Trond Myklebust7fdab062012-09-20 20:15:57 -04001235 lo = nfsi->layout;
1236 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
Fred Isamanf7e89172011-01-06 11:36:32 +00001237
Trond Myklebust7fdab062012-09-20 20:15:57 -04001238 /* Since close does not return a layout stateid for use as
1239 * a barrier, we choose the worst-case barrier.
1240 */
1241 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
Fred Isamanf7e89172011-01-06 11:36:32 +00001242 spin_unlock(&ino->i_lock);
Fred Isamanf7e89172011-01-06 11:36:32 +00001243}
1244
Peng Tao500d7012015-09-22 11:35:22 +08001245bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
1246{
1247 struct nfs_inode *nfsi = NFS_I(ino);
1248 struct pnfs_layout_hdr *lo;
1249 bool sleep = false;
1250
1251 /* we might not have grabbed lo reference. so need to check under
1252 * i_lock */
1253 spin_lock(&ino->i_lock);
1254 lo = nfsi->layout;
1255 if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
1256 sleep = true;
1257 spin_unlock(&ino->i_lock);
1258
1259 if (sleep)
1260 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
1261
1262 return sleep;
1263}
1264
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001265/*
1266 * Compare two layout segments for sorting into layout cache.
1267 * We want to preferentially return RW over RO layouts, so ensure those
1268 * are seen first.
1269 */
1270static s64
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001271pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001272 const struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001273{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001274 s64 d;
1275
1276 /* high offset > low offset */
1277 d = l1->offset - l2->offset;
1278 if (d)
1279 return d;
1280
1281 /* short length > long length */
1282 d = l2->length - l1->length;
1283 if (d)
1284 return d;
1285
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001286 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +03001287 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001288}
1289
Trond Myklebust03772d22015-08-25 08:54:17 -04001290static bool
1291pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
1292 const struct pnfs_layout_range *l2)
Andy Adamson974cec82010-10-20 00:18:02 -04001293{
Trond Myklebust03772d22015-08-25 08:54:17 -04001294 return pnfs_lseg_range_cmp(l1, l2) > 0;
1295}
1296
1297static bool
1298pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
1299 struct pnfs_layout_segment *old)
1300{
1301 return false;
1302}
1303
1304void
1305pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1306 struct pnfs_layout_segment *lseg,
1307 bool (*is_after)(const struct pnfs_layout_range *,
1308 const struct pnfs_layout_range *),
1309 bool (*do_merge)(struct pnfs_layout_segment *,
1310 struct pnfs_layout_segment *),
1311 struct list_head *free_me)
1312{
1313 struct pnfs_layout_segment *lp, *tmp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001314
Andy Adamson974cec82010-10-20 00:18:02 -04001315 dprintk("%s:Begin\n", __func__);
1316
Trond Myklebust03772d22015-08-25 08:54:17 -04001317 list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
1318 if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
1319 continue;
1320 if (do_merge(lseg, lp)) {
1321 mark_lseg_invalid(lp, free_me);
1322 continue;
1323 }
1324 if (is_after(&lseg->pls_range, &lp->pls_range))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001325 continue;
Fred Isaman566052c2011-01-06 11:36:20 +00001326 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001327 dprintk("%s: inserted lseg %p "
1328 "iomode %d offset %llu length %llu before "
1329 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +00001330 __func__, lseg, lseg->pls_range.iomode,
1331 lseg->pls_range.offset, lseg->pls_range.length,
1332 lp, lp->pls_range.iomode, lp->pls_range.offset,
1333 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +03001334 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -04001335 }
Benny Halevyfb3296e2011-05-22 19:47:26 +03001336 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1337 dprintk("%s: inserted lseg %p "
1338 "iomode %d offset %llu length %llu at tail\n",
1339 __func__, lseg, lseg->pls_range.iomode,
1340 lseg->pls_range.offset, lseg->pls_range.length);
1341out:
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001342 pnfs_get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -04001343
1344 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001345}
Trond Myklebust03772d22015-08-25 08:54:17 -04001346EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
1347
1348static void
1349pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1350 struct pnfs_layout_segment *lseg,
1351 struct list_head *free_me)
1352{
1353 struct inode *inode = lo->plh_inode;
1354 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
1355
1356 if (ld->add_lseg != NULL)
1357 ld->add_lseg(lo, lseg, free_me);
1358 else
1359 pnfs_generic_layout_insert_lseg(lo, lseg,
1360 pnfs_lseg_range_is_after,
1361 pnfs_lseg_no_merge,
1362 free_me);
1363}
Benny Halevye5e94012010-10-20 00:18:01 -04001364
1365static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -04001366alloc_init_layout_hdr(struct inode *ino,
1367 struct nfs_open_context *ctx,
1368 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001369{
1370 struct pnfs_layout_hdr *lo;
1371
Benny Halevy636fb9c2011-05-22 19:51:33 +03001372 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -04001373 if (!lo)
1374 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +00001375 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001376 INIT_LIST_HEAD(&lo->plh_layouts);
1377 INIT_LIST_HEAD(&lo->plh_segs);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -05001378 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001379 lo->plh_inode = ino;
Trond Myklebust5cc22162013-05-21 09:26:49 -04001380 lo->plh_lc_cred = get_rpccred(ctx->cred);
Trond Myklebust67a3b722016-06-17 16:48:19 -04001381 lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
Benny Halevye5e94012010-10-20 00:18:01 -04001382 return lo;
1383}
1384
1385static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -04001386pnfs_find_alloc_layout(struct inode *ino,
1387 struct nfs_open_context *ctx,
1388 gfp_t gfp_flags)
Trond Myklebuste5241e42016-06-17 16:48:20 -04001389 __releases(&ino->i_lock)
1390 __acquires(&ino->i_lock)
Benny Halevye5e94012010-10-20 00:18:01 -04001391{
1392 struct nfs_inode *nfsi = NFS_I(ino);
1393 struct pnfs_layout_hdr *new = NULL;
1394
1395 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1396
Trond Myklebust251ec412012-10-02 15:41:05 -07001397 if (nfsi->layout != NULL)
1398 goto out_existing;
Benny Halevye5e94012010-10-20 00:18:01 -04001399 spin_unlock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001400 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -04001401 spin_lock(&ino->i_lock);
1402
Trond Myklebust251ec412012-10-02 15:41:05 -07001403 if (likely(nfsi->layout == NULL)) { /* Won the race? */
Benny Halevye5e94012010-10-20 00:18:01 -04001404 nfsi->layout = new;
Trond Myklebust251ec412012-10-02 15:41:05 -07001405 return new;
Yanchuan Nian7175fe92012-10-31 16:05:48 +08001406 } else if (new != NULL)
1407 pnfs_free_layout_hdr(new);
Trond Myklebust251ec412012-10-02 15:41:05 -07001408out_existing:
1409 pnfs_get_layout_hdr(nfsi->layout);
Benny Halevye5e94012010-10-20 00:18:01 -04001410 return nfsi->layout;
1411}
1412
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001413/*
1414 * iomode matching rules:
Tom Haynesc7d73af2016-05-25 07:31:14 -07001415 * iomode lseg strict match
1416 * iomode
1417 * ----- ----- ------ -----
1418 * ANY READ N/A true
1419 * ANY RW N/A true
1420 * RW READ N/A false
1421 * RW RW N/A true
1422 * READ READ N/A true
1423 * READ RW true false
1424 * READ RW false true
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001425 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001426static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001427pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001428 const struct pnfs_layout_range *range,
1429 bool strict_iomode)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001430{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001431 struct pnfs_layout_range range1;
1432
1433 if ((range->iomode == IOMODE_RW &&
1434 ls_range->iomode != IOMODE_RW) ||
Tom Haynesc7d73af2016-05-25 07:31:14 -07001435 (range->iomode != ls_range->iomode &&
1436 strict_iomode == true) ||
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001437 !pnfs_lseg_range_intersecting(ls_range, range))
Benny Halevyfb3296e2011-05-22 19:47:26 +03001438 return 0;
1439
1440 /* range1 covers only the first byte in the range */
1441 range1 = *range;
1442 range1.length = 1;
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001443 return pnfs_lseg_range_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001444}
1445
1446/*
1447 * lookup range in layout
1448 */
Benny Halevye5e94012010-10-20 00:18:01 -04001449static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +03001450pnfs_find_lseg(struct pnfs_layout_hdr *lo,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001451 struct pnfs_layout_range *range,
1452 bool strict_iomode)
Benny Halevye5e94012010-10-20 00:18:01 -04001453{
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001454 struct pnfs_layout_segment *lseg, *ret = NULL;
1455
1456 dprintk("%s:Begin\n", __func__);
1457
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001458 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +00001459 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Peng Taoce6ab4f2014-09-06 00:53:24 +08001460 !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
Tom Haynesc7d73af2016-05-25 07:31:14 -07001461 pnfs_lseg_range_match(&lseg->pls_range, range,
1462 strict_iomode)) {
Trond Myklebust9369a432012-09-18 20:57:08 -04001463 ret = pnfs_get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001464 break;
1465 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001466 }
1467
1468 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +00001469 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001470 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -04001471}
1472
1473/*
Andy Adamsond23d61c2012-05-23 05:02:37 -04001474 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1475 * to the MDS or over pNFS
1476 *
1477 * The nfs_inode read_io and write_io fields are cumulative counters reset
1478 * when there are no layout segments. Note that in pnfs_update_layout iomode
1479 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1480 * WRITE request.
1481 *
1482 * A return of true means use MDS I/O.
1483 *
1484 * From rfc 5661:
1485 * If a file's size is smaller than the file size threshold, data accesses
1486 * SHOULD be sent to the metadata server. If an I/O request has a length that
1487 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1488 * server. If both file size and I/O size are provided, the client SHOULD
1489 * reach or exceed both thresholds before sending its read or write
1490 * requests to the data server.
1491 */
1492static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1493 struct inode *ino, int iomode)
1494{
1495 struct nfs4_threshold *t = ctx->mdsthreshold;
1496 struct nfs_inode *nfsi = NFS_I(ino);
1497 loff_t fsize = i_size_read(ino);
1498 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1499
1500 if (t == NULL)
1501 return ret;
1502
1503 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1504 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1505
1506 switch (iomode) {
1507 case IOMODE_READ:
1508 if (t->bm & THRESHOLD_RD) {
1509 dprintk("%s fsize %llu\n", __func__, fsize);
1510 size_set = true;
1511 if (fsize < t->rd_sz)
1512 size = true;
1513 }
1514 if (t->bm & THRESHOLD_RD_IO) {
1515 dprintk("%s nfsi->read_io %llu\n", __func__,
1516 nfsi->read_io);
1517 io_set = true;
1518 if (nfsi->read_io < t->rd_io_sz)
1519 io = true;
1520 }
1521 break;
1522 case IOMODE_RW:
1523 if (t->bm & THRESHOLD_WR) {
1524 dprintk("%s fsize %llu\n", __func__, fsize);
1525 size_set = true;
1526 if (fsize < t->wr_sz)
1527 size = true;
1528 }
1529 if (t->bm & THRESHOLD_WR_IO) {
1530 dprintk("%s nfsi->write_io %llu\n", __func__,
1531 nfsi->write_io);
1532 io_set = true;
1533 if (nfsi->write_io < t->wr_io_sz)
1534 io = true;
1535 }
1536 break;
1537 }
1538 if (size_set && io_set) {
1539 if (size && io)
1540 ret = true;
1541 } else if (size || io)
1542 ret = true;
1543
1544 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1545 return ret;
1546}
1547
Peng Taoaa8a45e2014-12-01 08:22:23 +08001548static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1549{
1550 /*
1551 * send layoutcommit as it can hold up layoutreturn due to lseg
1552 * reference
1553 */
1554 pnfs_layoutcommit_inode(lo->plh_inode, false);
1555 return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
Trond Myklebust2e5b29f2015-12-14 16:25:11 -05001556 nfs_wait_bit_killable,
Peng Taoaa8a45e2014-12-01 08:22:23 +08001557 TASK_UNINTERRUPTIBLE);
1558}
1559
Tom Haynesd67ae822014-12-11 17:02:04 -05001560static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1561{
1562 unsigned long *bitlock = &lo->plh_flags;
1563
1564 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1565 smp_mb__after_atomic();
1566 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1567}
1568
Andy Adamsond23d61c2012-05-23 05:02:37 -04001569/*
Benny Halevye5e94012010-10-20 00:18:01 -04001570 * Layout segment is retreived from the server if not cached.
1571 * The appropriate layout segment is referenced and returned to the caller.
1572 */
Andy Adamson7c24d942011-06-13 18:22:38 -04001573struct pnfs_layout_segment *
Benny Halevye5e94012010-10-20 00:18:01 -04001574pnfs_update_layout(struct inode *ino,
1575 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +03001576 loff_t pos,
1577 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001578 enum pnfs_iomode iomode,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001579 bool strict_iomode,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001580 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001581{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001582 struct pnfs_layout_range arg = {
1583 .iomode = iomode,
1584 .offset = pos,
1585 .length = count,
1586 };
Jeff Layton183d9e72016-05-17 12:28:47 -04001587 unsigned pg_offset, seq;
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001588 struct nfs_server *server = NFS_SERVER(ino);
1589 struct nfs_client *clp = server->nfs_client;
Jeff Layton183d9e72016-05-17 12:28:47 -04001590 struct pnfs_layout_hdr *lo = NULL;
Benny Halevye5e94012010-10-20 00:18:01 -04001591 struct pnfs_layout_segment *lseg = NULL;
Jeff Layton183d9e72016-05-17 12:28:47 -04001592 nfs4_stateid stateid;
1593 long timeout = 0;
Trond Myklebust66b53f32016-07-14 14:28:31 -04001594 unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
Weston Andros Adamson30005122013-02-28 20:30:10 -05001595 bool first;
Benny Halevye5e94012010-10-20 00:18:01 -04001596
Jeff Layton9a4bf312015-12-10 10:41:58 -05001597 if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001598 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001599 PNFS_UPDATE_LAYOUT_NO_PNFS);
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001600 goto out;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001601 }
Andy Adamsond23d61c2012-05-23 05:02:37 -04001602
Jeff Layton9a4bf312015-12-10 10:41:58 -05001603 if (iomode == IOMODE_READ && i_size_read(ino) == 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001604 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001605 PNFS_UPDATE_LAYOUT_RD_ZEROLEN);
Trond Myklebust4ae935602015-08-31 01:25:11 -07001606 goto out;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001607 }
Trond Myklebust4ae935602015-08-31 01:25:11 -07001608
Jeff Layton9a4bf312015-12-10 10:41:58 -05001609 if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001610 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001611 PNFS_UPDATE_LAYOUT_MDSTHRESH);
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001612 goto out;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001613 }
Andy Adamsond23d61c2012-05-23 05:02:37 -04001614
Peng Tao9bf87482014-08-22 17:37:41 +08001615lookup_again:
Trond Myklebustb88fa692016-08-23 11:19:33 -04001616 nfs4_client_recover_expired_lease(clp);
Peng Tao9bf87482014-08-22 17:37:41 +08001617 first = false;
Benny Halevye5e94012010-10-20 00:18:01 -04001618 spin_lock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001619 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001620 if (lo == NULL) {
1621 spin_unlock(&ino->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04001622 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001623 PNFS_UPDATE_LAYOUT_NOMEM);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001624 goto out;
1625 }
Benny Halevye5e94012010-10-20 00:18:01 -04001626
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001627 /* Do we even need to bother with this? */
Trond Myklebusta59c30a2012-03-01 11:17:47 -05001628 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001629 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001630 PNFS_UPDATE_LAYOUT_BULK_RECALL);
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001631 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001632 goto out_unlock;
1633 }
1634
1635 /* if LAYOUTGET already failed once we don't try again */
Trond Myklebust2e5b29f2015-12-14 16:25:11 -05001636 if (pnfs_layout_io_test_failed(lo, iomode)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001637 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001638 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
Benny Halevye5e94012010-10-20 00:18:01 -04001639 goto out_unlock;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001640 }
Benny Halevye5e94012010-10-20 00:18:01 -04001641
Tom Haynesc7d73af2016-05-25 07:31:14 -07001642 lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
Jeff Layton183d9e72016-05-17 12:28:47 -04001643 if (lseg) {
1644 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1645 PNFS_UPDATE_LAYOUT_FOUND_CACHED);
1646 goto out_unlock;
1647 }
1648
1649 if (!nfs4_valid_open_stateid(ctx->state)) {
1650 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1651 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
1652 goto out_unlock;
1653 }
1654
1655 /*
1656 * Choose a stateid for the LAYOUTGET. If we don't have a layout
1657 * stateid, or it has been invalidated, then we must use the open
1658 * stateid.
1659 */
Trond Myklebust67a3b722016-06-17 16:48:19 -04001660 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001661
1662 /*
1663 * The first layoutget for the file. Need to serialize per
Peng Tao9bf87482014-08-22 17:37:41 +08001664 * RFC 5661 Errata 3208.
1665 */
1666 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1667 &lo->plh_flags)) {
1668 spin_unlock(&ino->i_lock);
1669 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
1670 TASK_UNINTERRUPTIBLE);
1671 pnfs_put_layout_hdr(lo);
Jeff Layton183d9e72016-05-17 12:28:47 -04001672 dprintk("%s retrying\n", __func__);
Peng Tao9bf87482014-08-22 17:37:41 +08001673 goto lookup_again;
1674 }
Jeff Layton183d9e72016-05-17 12:28:47 -04001675
1676 first = true;
1677 do {
1678 seq = read_seqbegin(&ctx->state->seqlock);
1679 nfs4_stateid_copy(&stateid, &ctx->state->stateid);
1680 } while (read_seqretry(&ctx->state->seqlock, seq));
Peng Tao9bf87482014-08-22 17:37:41 +08001681 } else {
Jeff Layton183d9e72016-05-17 12:28:47 -04001682 nfs4_stateid_copy(&stateid, &lo->plh_stateid);
Peng Tao9bf87482014-08-22 17:37:41 +08001683 }
Andy Adamson568e8c42011-03-01 01:34:22 +00001684
Peng Taoaa8a45e2014-12-01 08:22:23 +08001685 /*
1686 * Because we free lsegs before sending LAYOUTRETURN, we need to wait
1687 * for LAYOUTRETURN even if first is true.
1688 */
Trond Myklebust8f70f532015-08-04 16:09:44 -04001689 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
Peng Taoaa8a45e2014-12-01 08:22:23 +08001690 spin_unlock(&ino->i_lock);
1691 dprintk("%s wait for layoutreturn\n", __func__);
1692 if (pnfs_prepare_to_retry_layoutget(lo)) {
Tom Haynesd67ae822014-12-11 17:02:04 -05001693 if (first)
1694 pnfs_clear_first_layoutget(lo);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001695 pnfs_put_layout_hdr(lo);
1696 dprintk("%s retrying\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04001697 trace_pnfs_update_layout(ino, pos, count, iomode, lo,
1698 lseg, PNFS_UPDATE_LAYOUT_RETRY);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001699 goto lookup_again;
1700 }
Jeff Layton183d9e72016-05-17 12:28:47 -04001701 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001702 PNFS_UPDATE_LAYOUT_RETURN);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001703 goto out_put_layout_hdr;
1704 }
1705
Jeff Layton9a4bf312015-12-10 10:41:58 -05001706 if (pnfs_layoutgets_blocked(lo)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001707 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001708 PNFS_UPDATE_LAYOUT_BLOCKED);
Fred Isamancf7d63f2011-01-06 11:36:25 +00001709 goto out_unlock;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001710 }
Fred Isamancf7d63f2011-01-06 11:36:25 +00001711 atomic_inc(&lo->plh_outstanding);
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001712 spin_unlock(&ino->i_lock);
Weston Andros Adamson30005122013-02-28 20:30:10 -05001713
Peng Taoabb9a002014-08-22 17:37:40 +08001714 if (list_empty(&lo->plh_layouts)) {
Fred Isaman2130ff62011-01-06 11:36:26 +00001715 /* The lo must be on the clp list if there is any
1716 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1717 */
1718 spin_lock(&clp->cl_lock);
Peng Taoabb9a002014-08-22 17:37:40 +08001719 if (list_empty(&lo->plh_layouts))
1720 list_add_tail(&lo->plh_layouts, &server->layouts);
Fred Isaman2130ff62011-01-06 11:36:26 +00001721 spin_unlock(&clp->cl_lock);
1722 }
Benny Halevye5e94012010-10-20 00:18:01 -04001723
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001724 pg_offset = arg.offset & ~PAGE_MASK;
Benny Halevy707ed5f2011-05-22 19:47:46 +03001725 if (pg_offset) {
1726 arg.offset -= pg_offset;
1727 arg.length += pg_offset;
1728 }
Andy Adamson7c24d942011-06-13 18:22:38 -04001729 if (arg.length != NFS4_MAX_UINT64)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001730 arg.length = PAGE_ALIGN(arg.length);
Benny Halevy707ed5f2011-05-22 19:47:46 +03001731
Jeff Layton183d9e72016-05-17 12:28:47 -04001732 lseg = send_layoutget(lo, ctx, &stateid, &arg, &timeout, gfp_flags);
1733 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1734 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
Trond Myklebust56b38a12016-07-14 18:34:12 -04001735 atomic_dec(&lo->plh_outstanding);
Jeff Layton83026d82016-05-17 12:28:46 -04001736 if (IS_ERR(lseg)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001737 switch(PTR_ERR(lseg)) {
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04001738 case -EBUSY:
Jeff Layton183d9e72016-05-17 12:28:47 -04001739 if (time_after(jiffies, giveup))
1740 lseg = NULL;
Trond Myklebust66b53f32016-07-14 14:28:31 -04001741 break;
1742 case -ERECALLCONFLICT:
1743 /* Huh? We hold no layouts, how is there a recall? */
1744 if (first) {
1745 lseg = NULL;
1746 break;
1747 }
1748 /* Destroy the existing layout and start over */
1749 if (time_after(jiffies, giveup))
1750 pnfs_destroy_layout(NFS_I(ino));
Jeff Layton183d9e72016-05-17 12:28:47 -04001751 /* Fallthrough */
1752 case -EAGAIN:
Trond Myklebust56b38a12016-07-14 18:34:12 -04001753 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04001754 default:
1755 if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
1756 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1757 lseg = NULL;
1758 }
Trond Myklebust56b38a12016-07-14 18:34:12 -04001759 goto out_put_layout_hdr;
1760 }
1761 if (lseg) {
1762 if (first)
1763 pnfs_clear_first_layoutget(lo);
1764 trace_pnfs_update_layout(ino, pos, count,
1765 iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
1766 pnfs_put_layout_hdr(lo);
1767 goto lookup_again;
Jeff Layton83026d82016-05-17 12:28:46 -04001768 }
1769 } else {
1770 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1771 }
1772
Trond Myklebust830ffb52012-09-20 21:25:19 -04001773out_put_layout_hdr:
Tom Haynesd67ae822014-12-11 17:02:04 -05001774 if (first)
1775 pnfs_clear_first_layoutget(lo);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001776 pnfs_put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -04001777out:
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001778 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1779 "(%s, offset: %llu, length: %llu)\n",
1780 __func__, ino->i_sb->s_id,
1781 (unsigned long long)NFS_FILEID(ino),
Peng Taod600ad12015-12-04 02:57:48 +08001782 IS_ERR_OR_NULL(lseg) ? "not found" : "found",
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001783 iomode==IOMODE_RW ? "read/write" : "read-only",
1784 (unsigned long long)pos,
1785 (unsigned long long)count);
Benny Halevye5e94012010-10-20 00:18:01 -04001786 return lseg;
1787out_unlock:
1788 spin_unlock(&ino->i_lock);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001789 goto out_put_layout_hdr;
Benny Halevye5e94012010-10-20 00:18:01 -04001790}
Andy Adamson7c24d942011-06-13 18:22:38 -04001791EXPORT_SYMBOL_GPL(pnfs_update_layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001792
Trond Myklebust540d9862015-08-25 11:16:13 -04001793static bool
1794pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
1795{
1796 switch (range->iomode) {
1797 case IOMODE_READ:
1798 case IOMODE_RW:
1799 break;
1800 default:
1801 return false;
1802 }
1803 if (range->offset == NFS4_MAX_UINT64)
1804 return false;
1805 if (range->length == 0)
1806 return false;
1807 if (range->length != NFS4_MAX_UINT64 &&
1808 range->length > NFS4_MAX_UINT64 - range->offset)
1809 return false;
1810 return true;
1811}
1812
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001813struct pnfs_layout_segment *
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001814pnfs_layout_process(struct nfs4_layoutget *lgp)
1815{
1816 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1817 struct nfs4_layoutget_res *res = &lgp->res;
1818 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001819 struct inode *ino = lo->plh_inode;
Trond Myklebust78096cc2014-02-12 10:02:27 -05001820 LIST_HEAD(free_me);
Trond Myklebust540d9862015-08-25 11:16:13 -04001821
1822 if (!pnfs_sanity_check_layout_range(&res->range))
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001823 return ERR_PTR(-EINVAL);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001824
1825 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001826 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001827 if (IS_ERR_OR_NULL(lseg)) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001828 if (!lseg)
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001829 lseg = ERR_PTR(-ENOMEM);
1830
1831 dprintk("%s: Could not allocate layout: error %ld\n",
1832 __func__, PTR_ERR(lseg));
1833 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001834 }
1835
Trond Myklebust119cef972016-07-24 15:10:12 -04001836 pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
Christoph Hellwig1013df612014-08-21 11:09:18 -05001837
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001838 spin_lock(&ino->i_lock);
Trond Myklebuste1c06f82015-08-04 16:40:08 -04001839 if (pnfs_layoutgets_blocked(lo)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001840 dprintk("%s forget reply due to state\n", __func__);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001841 goto out_forget;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001842 }
Trond Myklebust038d6492012-10-02 16:38:41 -07001843
Christoph Hellwig362f7472014-08-21 11:09:20 -05001844 if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
1845 /* existing state ID, make sure the sequence number matches. */
1846 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1847 dprintk("%s forget reply due to sequence\n", __func__);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001848 goto out_forget;
Christoph Hellwig362f7472014-08-21 11:09:20 -05001849 }
1850 pnfs_set_layout_stateid(lo, &res->stateid, false);
1851 } else {
1852 /*
1853 * We got an entirely new state ID. Mark all segments for the
1854 * inode invalid, and don't bother validating the stateid
1855 * sequence number.
1856 */
Trond Myklebustd9b61702016-07-24 15:04:07 -04001857 pnfs_mark_layout_stateid_invalid(lo, &free_me);
Christoph Hellwig362f7472014-08-21 11:09:20 -05001858
Trond Myklebust2a59a042016-09-03 11:20:04 -04001859 pnfs_set_layout_stateid(lo, &res->stateid, true);
Christoph Hellwig362f7472014-08-21 11:09:20 -05001860 }
Trond Myklebust038d6492012-10-02 16:38:41 -07001861
Trond Myklebust9369a432012-09-18 20:57:08 -04001862 pnfs_get_lseg(lseg);
Trond Myklebust03772d22015-08-25 08:54:17 -04001863 pnfs_layout_insert_lseg(lo, lseg, &free_me);
Trond Myklebust8e0acf92016-07-21 11:53:29 -04001864
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001865
Peng Tao39761432015-08-21 12:49:44 +08001866 if (res->return_on_close)
Fred Isamanf7e89172011-01-06 11:36:32 +00001867 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
Fred Isamanf7e89172011-01-06 11:36:32 +00001868
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001869 spin_unlock(&ino->i_lock);
Trond Myklebust78096cc2014-02-12 10:02:27 -05001870 pnfs_free_lseg_list(&free_me);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001871 return lseg;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001872
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001873out_forget:
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001874 spin_unlock(&ino->i_lock);
1875 lseg->pls_layout = lo;
1876 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001877 return ERR_PTR(-EAGAIN);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001878}
1879
Peng Tao016256d2014-09-06 00:53:23 +08001880static void
Jeff Layton3982a6a2016-05-17 12:28:41 -04001881pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
1882 u32 seq)
Trond Myklebust5c97f5d2016-01-04 11:53:04 -05001883{
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001884 if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
Trond Myklebust5c97f5d2016-01-04 11:53:04 -05001885 iomode = IOMODE_ANY;
1886 lo->plh_return_iomode = iomode;
Trond Myklebuste0fa0d02016-02-15 12:56:17 -05001887 set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001888 if (seq != 0) {
1889 WARN_ON_ONCE(lo->plh_return_seq != 0 && lo->plh_return_seq != seq);
Jeff Layton3982a6a2016-05-17 12:28:41 -04001890 lo->plh_return_seq = seq;
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001891 }
Trond Myklebust5c97f5d2016-01-04 11:53:04 -05001892}
1893
Trond Myklebust2f215962016-02-15 12:36:04 -05001894/**
1895 * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
1896 * @lo: pointer to layout header
1897 * @tmp_list: list header to be used with pnfs_free_lseg_list()
1898 * @return_range: describe layout segment ranges to be returned
1899 *
1900 * This function is mainly intended for use by layoutrecall. It attempts
1901 * to free the layout segment immediately, or else to mark it for return
1902 * as soon as its reference count drops to zero.
1903 */
Trond Myklebust10335552016-01-04 11:23:52 -05001904int
Peng Tao016256d2014-09-06 00:53:23 +08001905pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
1906 struct list_head *tmp_list,
Jeff Layton6d597e12016-05-17 12:28:42 -04001907 const struct pnfs_layout_range *return_range,
1908 u32 seq)
Peng Tao016256d2014-09-06 00:53:23 +08001909{
1910 struct pnfs_layout_segment *lseg, *next;
Trond Myklebust10335552016-01-04 11:23:52 -05001911 int remaining = 0;
Peng Tao016256d2014-09-06 00:53:23 +08001912
1913 dprintk("%s:Begin lo %p\n", __func__, lo);
1914
1915 if (list_empty(&lo->plh_segs))
Trond Myklebust10335552016-01-04 11:23:52 -05001916 return 0;
Peng Tao016256d2014-09-06 00:53:23 +08001917
Trond Myklebustfc7ff362015-12-28 10:28:59 -05001918 assert_spin_locked(&lo->plh_inode->i_lock);
Peng Tao016256d2014-09-06 00:53:23 +08001919
1920 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Trond Myklebuste036f462016-07-22 11:13:22 -04001921 if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
Peng Tao016256d2014-09-06 00:53:23 +08001922 dprintk("%s: marking lseg %p iomode %d "
1923 "offset %llu length %llu\n", __func__,
1924 lseg, lseg->pls_range.iomode,
1925 lseg->pls_range.offset,
1926 lseg->pls_range.length);
Trond Myklebust2f215962016-02-15 12:36:04 -05001927 if (mark_lseg_invalid(lseg, tmp_list))
1928 continue;
1929 remaining++;
Peng Tao016256d2014-09-06 00:53:23 +08001930 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
Peng Tao016256d2014-09-06 00:53:23 +08001931 }
Jeff Layton6d597e12016-05-17 12:28:42 -04001932
1933 if (remaining)
1934 pnfs_set_plh_return_info(lo, return_range->iomode, seq);
1935
Trond Myklebust10335552016-01-04 11:23:52 -05001936 return remaining;
Peng Tao016256d2014-09-06 00:53:23 +08001937}
1938
1939void pnfs_error_mark_layout_for_return(struct inode *inode,
1940 struct pnfs_layout_segment *lseg)
1941{
1942 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
Peng Tao016256d2014-09-06 00:53:23 +08001943 struct pnfs_layout_range range = {
1944 .iomode = lseg->pls_range.iomode,
1945 .offset = 0,
1946 .length = NFS4_MAX_UINT64,
1947 };
1948 LIST_HEAD(free_me);
Trond Myklebust10335552016-01-04 11:23:52 -05001949 bool return_now = false;
Peng Tao016256d2014-09-06 00:53:23 +08001950
1951 spin_lock(&inode->i_lock);
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001952 pnfs_set_plh_return_info(lo, range.iomode, 0);
Peng Tao016256d2014-09-06 00:53:23 +08001953 /*
1954 * mark all matching lsegs so that we are sure to have no live
1955 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
1956 * for how it works.
1957 */
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001958 if (!pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0)) {
Trond Myklebust10335552016-01-04 11:23:52 -05001959 nfs4_stateid stateid;
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001960 enum pnfs_iomode iomode;
Trond Myklebust10335552016-01-04 11:23:52 -05001961
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001962 return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
Trond Myklebust10335552016-01-04 11:23:52 -05001963 spin_unlock(&inode->i_lock);
1964 if (return_now)
1965 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1966 } else {
1967 spin_unlock(&inode->i_lock);
1968 nfs_commit_inode(inode, 0);
1969 }
Peng Tao016256d2014-09-06 00:53:23 +08001970 pnfs_free_lseg_list(&free_me);
1971}
1972EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
1973
Trond Myklebustd8007d42011-06-10 13:30:23 -04001974void
1975pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1976{
Peng Tao1fd937b2012-09-25 14:55:57 +08001977 u64 rd_size = req->wb_bytes;
1978
Peng Taocb5d04b2015-01-24 22:14:52 +08001979 if (pgio->pg_lseg == NULL) {
1980 if (pgio->pg_dreq == NULL)
1981 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
1982 else
1983 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
Trond Myklebustd8007d42011-06-10 13:30:23 -04001984
Peng Taocb5d04b2015-01-24 22:14:52 +08001985 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1986 req->wb_context,
1987 req_offset(req),
1988 rd_size,
1989 IOMODE_READ,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001990 false,
Peng Taocb5d04b2015-01-24 22:14:52 +08001991 GFP_KERNEL);
Peng Taod600ad12015-12-04 02:57:48 +08001992 if (IS_ERR(pgio->pg_lseg)) {
1993 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
1994 pgio->pg_lseg = NULL;
1995 return;
1996 }
Peng Taocb5d04b2015-01-24 22:14:52 +08001997 }
Trond Myklebuste885de12011-06-10 13:30:23 -04001998 /* If no lseg, fall back to read through mds */
1999 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04002000 nfs_pageio_reset_read_mds(pgio);
Trond Myklebuste885de12011-06-10 13:30:23 -04002001
Trond Myklebustd8007d42011-06-10 13:30:23 -04002002}
2003EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
2004
2005void
Peng Tao62965562012-09-25 14:55:57 +08002006pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
2007 struct nfs_page *req, u64 wb_size)
Trond Myklebustd8007d42011-06-10 13:30:23 -04002008{
Peng Taod600ad12015-12-04 02:57:48 +08002009 if (pgio->pg_lseg == NULL) {
Peng Taocb5d04b2015-01-24 22:14:52 +08002010 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2011 req->wb_context,
2012 req_offset(req),
2013 wb_size,
2014 IOMODE_RW,
Tom Haynesc7d73af2016-05-25 07:31:14 -07002015 false,
Peng Taocb5d04b2015-01-24 22:14:52 +08002016 GFP_NOFS);
Peng Taod600ad12015-12-04 02:57:48 +08002017 if (IS_ERR(pgio->pg_lseg)) {
2018 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2019 pgio->pg_lseg = NULL;
2020 return;
2021 }
2022 }
Trond Myklebuste885de12011-06-10 13:30:23 -04002023 /* If no lseg, fall back to write through mds */
2024 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04002025 nfs_pageio_reset_write_mds(pgio);
Trond Myklebustd8007d42011-06-10 13:30:23 -04002026}
2027EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
2028
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002029void
2030pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
2031{
2032 if (desc->pg_lseg) {
2033 pnfs_put_lseg(desc->pg_lseg);
2034 desc->pg_lseg = NULL;
2035 }
2036}
2037EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
2038
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -04002039/*
2040 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
2041 * of bytes (maximum @req->wb_bytes) that can be coalesced.
2042 */
2043size_t
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002044pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
2045 struct nfs_page *prev, struct nfs_page *req)
Fred Isamanbae724e2011-03-01 01:34:15 +00002046{
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04002047 unsigned int size;
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002048 u64 seg_end, req_start, seg_left;
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04002049
2050 size = nfs_generic_pg_test(pgio, prev, req);
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04002051 if (!size)
2052 return 0;
Benny Halevy89a58e32011-05-25 20:54:40 +03002053
Trond Myklebust19982ba2011-06-10 13:30:23 -04002054 /*
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002055 * 'size' contains the number of bytes left in the current page (up
2056 * to the original size asked for in @req->wb_bytes).
2057 *
2058 * Calculate how many bytes are left in the layout segment
2059 * and if there are less bytes than 'size', return that instead.
Trond Myklebust19982ba2011-06-10 13:30:23 -04002060 *
2061 * Please also note that 'end_offset' is actually the offset of the
2062 * first byte that lies outside the pnfs_layout_range. FIXME?
2063 *
2064 */
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002065 if (pgio->pg_lseg) {
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002066 seg_end = end_offset(pgio->pg_lseg->pls_range.offset,
2067 pgio->pg_lseg->pls_range.length);
2068 req_start = req_offset(req);
Weston Andros Adamson7c137892015-01-30 11:01:02 -05002069 WARN_ON_ONCE(req_start >= seg_end);
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002070 /* start of request is past the last byte of this segment */
Weston Andros Adamson7c137892015-01-30 11:01:02 -05002071 if (req_start >= seg_end) {
2072 /* reference the new lseg */
2073 if (pgio->pg_ops->pg_cleanup)
2074 pgio->pg_ops->pg_cleanup(pgio);
2075 if (pgio->pg_ops->pg_init)
2076 pgio->pg_ops->pg_init(pgio, req);
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002077 return 0;
Weston Andros Adamson7c137892015-01-30 11:01:02 -05002078 }
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002079
2080 /* adjust 'size' iff there are fewer bytes left in the
2081 * segment than what nfs_generic_pg_test returned */
2082 seg_left = seg_end - req_start;
2083 if (seg_left < size)
2084 size = (unsigned int)seg_left;
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002085 }
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04002086
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002087 return size;
Fred Isamanbae724e2011-03-01 01:34:15 +00002088}
Benny Halevy89a58e32011-05-25 20:54:40 +03002089EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
Fred Isamanbae724e2011-03-01 01:34:15 +00002090
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002091int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002092{
2093 struct nfs_pageio_descriptor pgio;
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002094
2095 /* Resend all requests through the MDS */
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002096 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
2097 hdr->completion_ops);
Trond Myklebustc7070112015-06-17 19:56:22 -04002098 set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags);
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002099 return nfs_pageio_resend(&pgio, hdr);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002100}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04002101EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002102
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002103static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002104{
Fred Isamancd841602012-04-20 14:47:44 -04002105
2106 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
2107 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002108 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04002109 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002110 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04002111 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002112 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002113}
2114
Benny Halevyd20581a2011-05-22 19:52:03 +03002115/*
2116 * Called by non rpc-based layout drivers
2117 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002118void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
Fred Isaman94ad1c82011-03-01 01:34:14 +00002119{
Kinglong Meef8417b482015-10-16 17:23:29 +08002120 if (likely(!hdr->pnfs_error)) {
Trond Myklebust67af7612015-03-25 20:40:38 -04002121 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
2122 hdr->mds_offset + hdr->res.count);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002123 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
Kinglong Meef8417b482015-10-16 17:23:29 +08002124 }
2125 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
2126 if (unlikely(hdr->pnfs_error))
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002127 pnfs_ld_handle_write_error(hdr);
2128 hdr->mds_ops->rpc_release(hdr);
Fred Isaman44b83792011-03-03 15:13:44 +00002129}
Benny Halevyd20581a2011-05-22 19:52:03 +03002130EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
Fred Isaman44b83792011-03-03 15:13:44 +00002131
Trond Myklebustdce81292011-07-13 15:59:19 -04002132static void
2133pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002134 struct nfs_pgio_header *hdr)
Trond Myklebustdce81292011-07-13 15:59:19 -04002135{
Peng Tao48d635f2014-11-10 08:35:35 +08002136 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002137
Fred Isaman6c75dc02012-04-20 14:47:47 -04002138 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002139 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
Fred Isaman6c75dc02012-04-20 14:47:47 -04002140 nfs_pageio_reset_write_mds(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002141 mirror->pg_recoalesce = 1;
Fred Isaman6c75dc02012-04-20 14:47:47 -04002142 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002143 nfs_pgio_data_destroy(hdr);
Trond Myklebust1ca018d2015-06-17 19:41:51 -04002144 hdr->release(hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04002145}
2146
2147static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002148pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
Trond Myklebustdce81292011-07-13 15:59:19 -04002149 const struct rpc_call_ops *call_ops,
2150 struct pnfs_layout_segment *lseg,
2151 int how)
Andy Adamson0382b742011-03-03 15:13:45 +00002152{
Fred Isamancd841602012-04-20 14:47:44 -04002153 struct inode *inode = hdr->inode;
Andy Adamson0382b742011-03-03 15:13:45 +00002154 enum pnfs_try_status trypnfs;
2155 struct nfs_server *nfss = NFS_SERVER(inode);
2156
Fred Isamancd841602012-04-20 14:47:44 -04002157 hdr->mds_ops = call_ops;
Andy Adamson0382b742011-03-03 15:13:45 +00002158
2159 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002160 inode->i_ino, hdr->args.count, hdr->args.offset, how);
2161 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
Fred Isaman6c75dc02012-04-20 14:47:47 -04002162 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson0382b742011-03-03 15:13:45 +00002163 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
Andy Adamson0382b742011-03-03 15:13:45 +00002164 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2165 return trypnfs;
2166}
2167
Trond Myklebustdce81292011-07-13 15:59:19 -04002168static void
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002169pnfs_do_write(struct nfs_pageio_descriptor *desc,
2170 struct nfs_pgio_header *hdr, int how)
Trond Myklebustdce81292011-07-13 15:59:19 -04002171{
Trond Myklebustdce81292011-07-13 15:59:19 -04002172 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2173 struct pnfs_layout_segment *lseg = desc->pg_lseg;
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002174 enum pnfs_try_status trypnfs;
Trond Myklebustdce81292011-07-13 15:59:19 -04002175
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002176 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002177 if (trypnfs == PNFS_NOT_ATTEMPTED)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002178 pnfs_write_through_mds(desc, hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04002179}
2180
Fred Isaman6c75dc02012-04-20 14:47:47 -04002181static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
2182{
Trond Myklebust9369a432012-09-18 20:57:08 -04002183 pnfs_put_lseg(hdr->lseg);
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002184 nfs_pgio_header_free(hdr);
Fred Isaman6c75dc02012-04-20 14:47:47 -04002185}
2186
Trond Myklebustdce81292011-07-13 15:59:19 -04002187int
2188pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
2189{
Fred Isaman6c75dc02012-04-20 14:47:47 -04002190 struct nfs_pgio_header *hdr;
Trond Myklebustdce81292011-07-13 15:59:19 -04002191 int ret;
2192
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002193 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2194 if (!hdr) {
Peng Tao2bff2282015-12-05 02:03:17 +08002195 desc->pg_error = -ENOMEM;
2196 return desc->pg_error;
Fred Isaman6c75dc02012-04-20 14:47:47 -04002197 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04002198 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002199
Trond Myklebust9369a432012-09-18 20:57:08 -04002200 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Anna Schumakeref2c4882014-05-06 09:12:36 -04002201 ret = nfs_generic_pgio(desc, hdr);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002202 if (!ret)
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002203 pnfs_do_write(desc, hdr, desc->pg_ioflags);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002204
Fred Isaman6c75dc02012-04-20 14:47:47 -04002205 return ret;
Trond Myklebustdce81292011-07-13 15:59:19 -04002206}
2207EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
2208
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002209int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
Trond Myklebust62e4a762011-11-10 14:30:37 -05002210{
2211 struct nfs_pageio_descriptor pgio;
2212
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002213 /* Resend all requests through the MDS */
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002214 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
2215 return nfs_pageio_resend(&pgio, hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002216}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04002217EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002218
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002219static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002220{
Fred Isamancd841602012-04-20 14:47:44 -04002221 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
2222 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002223 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04002224 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002225 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002226 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002227 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
Trond Myklebust62e4a762011-11-10 14:30:37 -05002228}
2229
Andy Adamsonb1f69b72010-10-20 00:18:03 -04002230/*
Benny Halevyd20581a2011-05-22 19:52:03 +03002231 * Called by non rpc-based layout drivers
2232 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002233void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03002234{
Trond Myklebustbfc505d2016-09-15 18:26:05 -04002235 if (likely(!hdr->pnfs_error))
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002236 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
Kinglong Meef8417b482015-10-16 17:23:29 +08002237 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
2238 if (unlikely(hdr->pnfs_error))
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002239 pnfs_ld_handle_read_error(hdr);
2240 hdr->mds_ops->rpc_release(hdr);
Benny Halevyd20581a2011-05-22 19:52:03 +03002241}
2242EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
2243
Trond Myklebust493292d2011-07-13 15:58:28 -04002244static void
2245pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002246 struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -04002247{
Peng Tao48d635f2014-11-10 08:35:35 +08002248 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002249
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002250 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002251 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002252 nfs_pageio_reset_read_mds(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002253 mirror->pg_recoalesce = 1;
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002254 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002255 nfs_pgio_data_destroy(hdr);
Trond Myklebust1ca018d2015-06-17 19:41:51 -04002256 hdr->release(hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -04002257}
2258
Benny Halevyd20581a2011-05-22 19:52:03 +03002259/*
Andy Adamson64419a92011-03-01 01:34:16 +00002260 * Call the appropriate parallel I/O subsystem read function.
2261 */
Trond Myklebust493292d2011-07-13 15:58:28 -04002262static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002263pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
Trond Myklebust493292d2011-07-13 15:58:28 -04002264 const struct rpc_call_ops *call_ops,
2265 struct pnfs_layout_segment *lseg)
Andy Adamson64419a92011-03-01 01:34:16 +00002266{
Fred Isamancd841602012-04-20 14:47:44 -04002267 struct inode *inode = hdr->inode;
Andy Adamson64419a92011-03-01 01:34:16 +00002268 struct nfs_server *nfss = NFS_SERVER(inode);
2269 enum pnfs_try_status trypnfs;
2270
Fred Isamancd841602012-04-20 14:47:44 -04002271 hdr->mds_ops = call_ops;
Andy Adamson64419a92011-03-01 01:34:16 +00002272
2273 dprintk("%s: Reading ino:%lu %u@%llu\n",
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002274 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
Andy Adamson64419a92011-03-01 01:34:16 +00002275
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002276 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002277 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson64419a92011-03-01 01:34:16 +00002278 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
Andy Adamson64419a92011-03-01 01:34:16 +00002279 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2280 return trypnfs;
2281}
Andy Adamson863a3c62011-03-23 13:27:54 +00002282
Peng Taoceb11e12014-11-10 08:35:38 +08002283/* Resend all requests through pnfs. */
Weston Andros Adamson1b1bc662016-04-01 11:42:28 -04002284void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
Peng Taoceb11e12014-11-10 08:35:38 +08002285{
2286 struct nfs_pageio_descriptor pgio;
2287
Weston Andros Adamson1b1bc662016-04-01 11:42:28 -04002288 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2289 nfs_pageio_init_read(&pgio, hdr->inode, false,
2290 hdr->completion_ops);
2291 hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
2292 }
Peng Taoceb11e12014-11-10 08:35:38 +08002293}
2294EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
2295
Trond Myklebust493292d2011-07-13 15:58:28 -04002296static void
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002297pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -04002298{
Trond Myklebust493292d2011-07-13 15:58:28 -04002299 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2300 struct pnfs_layout_segment *lseg = desc->pg_lseg;
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002301 enum pnfs_try_status trypnfs;
Trond Myklebust493292d2011-07-13 15:58:28 -04002302
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002303 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
Peng Taoceb11e12014-11-10 08:35:38 +08002304 if (trypnfs == PNFS_TRY_AGAIN)
Weston Andros Adamson1b1bc662016-04-01 11:42:28 -04002305 pnfs_read_resend_pnfs(hdr);
2306 if (trypnfs == PNFS_NOT_ATTEMPTED || hdr->task.tk_status)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002307 pnfs_read_through_mds(desc, hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -04002308}
2309
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002310static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
2311{
Trond Myklebust9369a432012-09-18 20:57:08 -04002312 pnfs_put_lseg(hdr->lseg);
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002313 nfs_pgio_header_free(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002314}
2315
Trond Myklebust493292d2011-07-13 15:58:28 -04002316int
2317pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2318{
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002319 struct nfs_pgio_header *hdr;
Trond Myklebust493292d2011-07-13 15:58:28 -04002320 int ret;
2321
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002322 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2323 if (!hdr) {
Peng Tao2bff2282015-12-05 02:03:17 +08002324 desc->pg_error = -ENOMEM;
2325 return desc->pg_error;
Trond Myklebust493292d2011-07-13 15:58:28 -04002326 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002327 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
Trond Myklebust9369a432012-09-18 20:57:08 -04002328 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Anna Schumakeref2c4882014-05-06 09:12:36 -04002329 ret = nfs_generic_pgio(desc, hdr);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002330 if (!ret)
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002331 pnfs_do_read(desc, hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002332 return ret;
Trond Myklebust493292d2011-07-13 15:58:28 -04002333}
2334EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
2335
Trond Myklebust71244d92014-01-13 13:34:36 -05002336static void pnfs_clear_layoutcommitting(struct inode *inode)
2337{
2338 unsigned long *bitlock = &NFS_I(inode)->flags;
2339
2340 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002341 smp_mb__after_atomic();
Trond Myklebust71244d92014-01-13 13:34:36 -05002342 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2343}
2344
Andy Adamson863a3c62011-03-23 13:27:54 +00002345/*
Peng Taoa9bae562011-07-30 20:52:33 -04002346 * There can be multiple RW segments.
Andy Adamson863a3c62011-03-23 13:27:54 +00002347 */
Peng Taoa9bae562011-07-30 20:52:33 -04002348static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
Andy Adamson863a3c62011-03-23 13:27:54 +00002349{
Peng Taoa9bae562011-07-30 20:52:33 -04002350 struct pnfs_layout_segment *lseg;
Andy Adamson863a3c62011-03-23 13:27:54 +00002351
Peng Taoa9bae562011-07-30 20:52:33 -04002352 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2353 if (lseg->pls_range.iomode == IOMODE_RW &&
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002354 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
Peng Taoa9bae562011-07-30 20:52:33 -04002355 list_add(&lseg->pls_lc_list, listp);
2356 }
Andy Adamson863a3c62011-03-23 13:27:54 +00002357}
2358
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002359static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2360{
2361 struct pnfs_layout_segment *lseg, *tmp;
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002362
2363 /* Matched by references in pnfs_set_layoutcommit */
2364 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2365 list_del_init(&lseg->pls_lc_list);
2366 pnfs_put_lseg(lseg);
2367 }
2368
Trond Myklebust71244d92014-01-13 13:34:36 -05002369 pnfs_clear_layoutcommitting(inode);
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002370}
2371
Peng Tao1b0ae062011-09-22 21:50:12 -04002372void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2373{
Trond Myklebustb9e028f2012-09-18 16:41:18 -04002374 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
Peng Tao1b0ae062011-09-22 21:50:12 -04002375}
2376EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2377
Andy Adamson863a3c62011-03-23 13:27:54 +00002378void
Trond Myklebust67af7612015-03-25 20:40:38 -04002379pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
2380 loff_t end_pos)
Andy Adamson863a3c62011-03-23 13:27:54 +00002381{
Fred Isamancd841602012-04-20 14:47:44 -04002382 struct nfs_inode *nfsi = NFS_I(inode);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002383 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00002384
Fred Isamancd841602012-04-20 14:47:44 -04002385 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00002386 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
Trond Myklebust29559b12015-03-25 20:10:20 -04002387 nfsi->layout->plh_lwb = end_pos;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002388 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00002389 dprintk("%s: Set layoutcommit for inode %lu ",
Fred Isamancd841602012-04-20 14:47:44 -04002390 __func__, inode->i_ino);
Trond Myklebust29559b12015-03-25 20:10:20 -04002391 } else if (end_pos > nfsi->layout->plh_lwb)
2392 nfsi->layout->plh_lwb = end_pos;
Trond Myklebust67af7612015-03-25 20:40:38 -04002393 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
Peng Taoa9bae562011-07-30 20:52:33 -04002394 /* references matched in nfs4_layoutcommit_release */
Trond Myklebust67af7612015-03-25 20:40:38 -04002395 pnfs_get_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04002396 }
Fred Isamancd841602012-04-20 14:47:44 -04002397 spin_unlock(&inode->i_lock);
Peng Taoacff58802011-07-30 20:52:31 -04002398 dprintk("%s: lseg %p end_pos %llu\n",
Trond Myklebust67af7612015-03-25 20:40:38 -04002399 __func__, lseg, nfsi->layout->plh_lwb);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002400
2401 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2402 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2403 if (mark_as_dirty)
Fred Isamancd841602012-04-20 14:47:44 -04002404 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002405}
2406EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2407
Andy Adamsondb29c082011-07-30 20:52:38 -04002408void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2409{
2410 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2411
2412 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2413 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002414 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
Andy Adamsondb29c082011-07-30 20:52:38 -04002415}
2416
Andy Adamsonde4b15c2011-03-12 02:58:09 -05002417/*
2418 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2419 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2420 * data to disk to allow the server to recover the data if it crashes.
2421 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2422 * is off, and a COMMIT is sent to a data server, or
2423 * if WRITEs to a data server return NFS_DATA_SYNC.
2424 */
Andy Adamson863a3c62011-03-23 13:27:54 +00002425int
Andy Adamsonef311532011-03-12 02:58:10 -05002426pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00002427{
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002428 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
Andy Adamson863a3c62011-03-23 13:27:54 +00002429 struct nfs4_layoutcommit_data *data;
2430 struct nfs_inode *nfsi = NFS_I(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002431 loff_t end_pos;
Trond Myklebust71244d92014-01-13 13:34:36 -05002432 int status;
2433
2434 if (!pnfs_layoutcommit_outstanding(inode))
2435 return 0;
Andy Adamson863a3c62011-03-23 13:27:54 +00002436
2437 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
2438
Trond Myklebust71244d92014-01-13 13:34:36 -05002439 status = -EAGAIN;
2440 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
2441 if (!sync)
2442 goto out;
NeilBrown74316202014-07-07 15:16:04 +10002443 status = wait_on_bit_lock_action(&nfsi->flags,
Trond Myklebust71244d92014-01-13 13:34:36 -05002444 NFS_INO_LAYOUTCOMMITTING,
2445 nfs_wait_bit_killable,
2446 TASK_KILLABLE);
2447 if (status)
2448 goto out;
2449 }
Andy Adamsonde4b15c2011-03-12 02:58:09 -05002450
Trond Myklebust71244d92014-01-13 13:34:36 -05002451 status = -ENOMEM;
Andy Adamson863a3c62011-03-23 13:27:54 +00002452 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2453 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebust71244d92014-01-13 13:34:36 -05002454 if (!data)
2455 goto clear_layoutcommitting;
Andy Adamson863a3c62011-03-23 13:27:54 +00002456
Trond Myklebust71244d92014-01-13 13:34:36 -05002457 status = 0;
2458 spin_lock(&inode->i_lock);
2459 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2460 goto out_unlock;
Peng Tao92407e72011-10-23 20:21:17 -07002461
Peng Taoa9bae562011-07-30 20:52:33 -04002462 INIT_LIST_HEAD(&data->lseg_list);
Peng Taoa9bae562011-07-30 20:52:33 -04002463 pnfs_list_write_lseg(inode, &data->lseg_list);
Andy Adamson863a3c62011-03-23 13:27:54 +00002464
Peng Taoacff58802011-07-30 20:52:31 -04002465 end_pos = nfsi->layout->plh_lwb;
Andy Adamson863a3c62011-03-23 13:27:54 +00002466
Trond Myklebustf597c532012-03-04 18:13:56 -05002467 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
Andy Adamson863a3c62011-03-23 13:27:54 +00002468 spin_unlock(&inode->i_lock);
2469
2470 data->args.inode = inode;
Peng Tao9fa40752011-07-30 20:52:32 -04002471 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
Andy Adamson863a3c62011-03-23 13:27:54 +00002472 nfs_fattr_init(&data->fattr);
2473 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2474 data->res.fattr = &data->fattr;
Trond Myklebust2e18d4d2016-06-26 18:54:58 -04002475 if (end_pos != 0)
2476 data->args.lastbytewritten = end_pos - 1;
2477 else
2478 data->args.lastbytewritten = U64_MAX;
Andy Adamson863a3c62011-03-23 13:27:54 +00002479 data->res.server = NFS_SERVER(inode);
2480
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002481 if (ld->prepare_layoutcommit) {
2482 status = ld->prepare_layoutcommit(&data->args);
2483 if (status) {
Jeff Layton3471648a2015-07-10 15:58:42 -04002484 put_rpccred(data->cred);
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002485 spin_lock(&inode->i_lock);
Trond Myklebust29559b12015-03-25 20:10:20 -04002486 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2487 if (end_pos > nfsi->layout->plh_lwb)
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002488 nfsi->layout->plh_lwb = end_pos;
Jeff Layton3471648a2015-07-10 15:58:42 -04002489 goto out_unlock;
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002490 }
2491 }
2492
2493
Andy Adamson863a3c62011-03-23 13:27:54 +00002494 status = nfs4_proc_layoutcommit(data, sync);
2495out:
Peng Tao92407e72011-10-23 20:21:17 -07002496 if (status)
2497 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002498 dprintk("<-- %s status %d\n", __func__, status);
2499 return status;
Trond Myklebust71244d92014-01-13 13:34:36 -05002500out_unlock:
2501 spin_unlock(&inode->i_lock);
Peng Tao92407e72011-10-23 20:21:17 -07002502 kfree(data);
Trond Myklebust71244d92014-01-13 13:34:36 -05002503clear_layoutcommitting:
2504 pnfs_clear_layoutcommitting(inode);
Peng Tao92407e72011-10-23 20:21:17 -07002505 goto out;
Andy Adamson863a3c62011-03-23 13:27:54 +00002506}
Peng Tao72cff442014-08-07 10:12:38 +08002507EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
Andy Adamson82be4172012-05-23 05:02:35 -04002508
Trond Myklebust5bb89b42015-03-25 14:14:42 -04002509int
2510pnfs_generic_sync(struct inode *inode, bool datasync)
2511{
2512 return pnfs_layoutcommit_inode(inode, true);
2513}
2514EXPORT_SYMBOL_GPL(pnfs_generic_sync);
2515
Andy Adamson82be4172012-05-23 05:02:35 -04002516struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
2517{
2518 struct nfs4_threshold *thp;
2519
2520 thp = kzalloc(sizeof(*thp), GFP_NOFS);
2521 if (!thp) {
2522 dprintk("%s mdsthreshold allocation failed\n", __func__);
2523 return NULL;
2524 }
2525 return thp;
2526}
Peng Tao87334082015-06-23 19:51:57 +08002527
Peng Tao865a7ec2015-06-25 18:19:32 +08002528#if IS_ENABLED(CONFIG_NFS_V4_2)
Peng Tao87334082015-06-23 19:51:57 +08002529int
Trond Myklebustc8ad8892015-08-05 17:31:58 -04002530pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
Peng Tao87334082015-06-23 19:51:57 +08002531{
2532 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2533 struct nfs_server *server = NFS_SERVER(inode);
Peng Tao1bfe3b22015-06-23 19:52:03 +08002534 struct nfs_inode *nfsi = NFS_I(inode);
Peng Tao87334082015-06-23 19:51:57 +08002535 struct nfs42_layoutstat_data *data;
2536 struct pnfs_layout_hdr *hdr;
2537 int status = 0;
2538
2539 if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
2540 goto out;
2541
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04002542 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
2543 goto out;
2544
Peng Tao1bfe3b22015-06-23 19:52:03 +08002545 if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
2546 goto out;
2547
Peng Tao87334082015-06-23 19:51:57 +08002548 spin_lock(&inode->i_lock);
2549 if (!NFS_I(inode)->layout) {
2550 spin_unlock(&inode->i_lock);
Trond Myklebustf538d0b2016-05-16 14:41:14 -04002551 goto out_clear_layoutstats;
Peng Tao87334082015-06-23 19:51:57 +08002552 }
2553 hdr = NFS_I(inode)->layout;
2554 pnfs_get_layout_hdr(hdr);
2555 spin_unlock(&inode->i_lock);
2556
Trond Myklebustc8ad8892015-08-05 17:31:58 -04002557 data = kzalloc(sizeof(*data), gfp_flags);
Peng Tao87334082015-06-23 19:51:57 +08002558 if (!data) {
2559 status = -ENOMEM;
2560 goto out_put;
2561 }
2562
2563 data->args.fh = NFS_FH(inode);
2564 data->args.inode = inode;
Peng Tao87334082015-06-23 19:51:57 +08002565 status = ld->prepare_layoutstats(&data->args);
2566 if (status)
2567 goto out_free;
2568
2569 status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
2570
2571out:
2572 dprintk("%s returns %d\n", __func__, status);
2573 return status;
2574
2575out_free:
2576 kfree(data);
2577out_put:
2578 pnfs_put_layout_hdr(hdr);
Trond Myklebustf538d0b2016-05-16 14:41:14 -04002579out_clear_layoutstats:
Peng Tao1bfe3b22015-06-23 19:52:03 +08002580 smp_mb__before_atomic();
2581 clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
2582 smp_mb__after_atomic();
Peng Tao87334082015-06-23 19:51:57 +08002583 goto out;
2584}
2585EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
Peng Tao865a7ec2015-06-25 18:19:32 +08002586#endif
Trond Myklebustbbf58bf2015-08-24 20:39:18 -04002587
2588unsigned int layoutstats_timer;
2589module_param(layoutstats_timer, uint, 0644);
2590EXPORT_SYMBOL_GPL(layoutstats_timer);