blob: a6a683fbd230d2e9d2742f93875cf60ef920c736 [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>
Andy Adamson974cec82010-10-20 00:18:02 -040033#include "internal.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040034#include "pnfs.h"
Andy Adamson64419a92011-03-01 01:34:16 +000035#include "iostat.h"
Trond Myklebustcc668ab2013-08-14 15:31:28 -040036#include "nfs4trace.h"
Trond Myklebust40dd4b72015-01-24 13:54:37 -050037#include "delegation.h"
Peng Tao87334082015-06-23 19:51:57 +080038#include "nfs42.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040039
40#define NFSDBG_FACILITY NFSDBG_PNFS
Trond Myklebust25c75332012-09-18 17:01:12 -040041#define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040042
Fred Isaman02c35fc2010-10-20 00:17:59 -040043/* Locking:
44 *
45 * pnfs_spinlock:
46 * protects pnfs_modules_tbl.
47 */
48static DEFINE_SPINLOCK(pnfs_spinlock);
49
50/*
51 * pnfs_modules_tbl holds all pnfs modules
52 */
53static LIST_HEAD(pnfs_modules_tbl);
54
Trond Myklebust13c13a62016-01-26 23:12:11 -050055static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo);
Peng Taoaa1e0e3a2014-09-06 00:53:25 +080056
Fred Isaman02c35fc2010-10-20 00:17:59 -040057/* Return the registered pnfs layout driver module matching given id */
58static struct pnfs_layoutdriver_type *
59find_pnfs_driver_locked(u32 id)
60{
61 struct pnfs_layoutdriver_type *local;
62
63 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
64 if (local->id == id)
65 goto out;
66 local = NULL;
67out:
68 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
69 return local;
70}
71
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040072static struct pnfs_layoutdriver_type *
73find_pnfs_driver(u32 id)
74{
Fred Isaman02c35fc2010-10-20 00:17:59 -040075 struct pnfs_layoutdriver_type *local;
76
77 spin_lock(&pnfs_spinlock);
78 local = find_pnfs_driver_locked(id);
Trond Myklebust0a9c63f2012-06-15 13:02:58 -040079 if (local != NULL && !try_module_get(local->owner)) {
80 dprintk("%s: Could not grab reference on module\n", __func__);
81 local = NULL;
82 }
Fred Isaman02c35fc2010-10-20 00:17:59 -040083 spin_unlock(&pnfs_spinlock);
84 return local;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040085}
86
87void
88unset_pnfs_layoutdriver(struct nfs_server *nfss)
89{
Benny Halevy738fd0f32011-07-30 20:52:36 -040090 if (nfss->pnfs_curr_ld) {
91 if (nfss->pnfs_curr_ld->clear_layoutdriver)
92 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
Trond Myklebust2a4c8992012-06-14 13:08:38 -040093 /* Decrement the MDS count. Purge the deviceid cache if zero */
94 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
95 nfs4_deviceid_purge_client(nfss->nfs_client);
Fred Isaman02c35fc2010-10-20 00:17:59 -040096 module_put(nfss->pnfs_curr_ld->owner);
Benny Halevy738fd0f32011-07-30 20:52:36 -040097 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040098 nfss->pnfs_curr_ld = NULL;
99}
100
101/*
102 * Try to set the server's pnfs module to the pnfs layout type specified by id.
103 * Currently only one pNFS layout driver per filesystem is supported.
104 *
Jeff Layton3132e492016-08-10 15:58:24 -0400105 * @ids array of layout types supported by MDS.
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400106 */
107void
Benny Halevy738fd0f32011-07-30 20:52:36 -0400108set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
Jeff Layton3132e492016-08-10 15:58:24 -0400109 u32 *ids)
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400110{
111 struct pnfs_layoutdriver_type *ld_type = NULL;
Jeff Layton3132e492016-08-10 15:58:24 -0400112 u32 id;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400113
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400114 if (!(server->nfs_client->cl_exchange_flags &
115 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
Jeff Layton3132e492016-08-10 15:58:24 -0400116 printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n",
117 __func__, server->nfs_client->cl_exchange_flags);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400118 goto out_no_driver;
119 }
Jeff Layton3132e492016-08-10 15:58:24 -0400120
121 id = ids[0];
122 if (!id)
123 goto out_no_driver;
124
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400125 ld_type = find_pnfs_driver(id);
126 if (!ld_type) {
127 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
128 ld_type = find_pnfs_driver(id);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400129 }
Jeff Layton3132e492016-08-10 15:58:24 -0400130
131 if (!ld_type) {
132 dprintk("%s: No pNFS module found for %u.\n", __func__, id);
133 goto out_no_driver;
134 }
135
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400136 server->pnfs_curr_ld = ld_type;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400137 if (ld_type->set_layoutdriver
138 && ld_type->set_layoutdriver(server, mntfh)) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500139 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
140 "driver %u.\n", __func__, id);
Benny Halevy738fd0f32011-07-30 20:52:36 -0400141 module_put(ld_type->owner);
142 goto out_no_driver;
143 }
Trond Myklebust2a4c8992012-06-14 13:08:38 -0400144 /* Bump the MDS count */
145 atomic_inc(&server->nfs_client->cl_mds_count);
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000146
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400147 dprintk("%s: pNFS module for %u set\n", __func__, id);
148 return;
149
150out_no_driver:
151 dprintk("%s: Using NFSv4 I/O\n", __func__);
152 server->pnfs_curr_ld = NULL;
153}
Fred Isaman02c35fc2010-10-20 00:17:59 -0400154
155int
156pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
157{
158 int status = -EINVAL;
159 struct pnfs_layoutdriver_type *tmp;
160
161 if (ld_type->id == 0) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500162 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
Fred Isaman02c35fc2010-10-20 00:17:59 -0400163 return status;
164 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400165 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500166 printk(KERN_ERR "NFS: %s Layout driver must provide "
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400167 "alloc_lseg and free_lseg.\n", __func__);
168 return status;
169 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400170
171 spin_lock(&pnfs_spinlock);
172 tmp = find_pnfs_driver_locked(ld_type->id);
173 if (!tmp) {
174 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
175 status = 0;
176 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
177 ld_type->name);
178 } else {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500179 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
Fred Isaman02c35fc2010-10-20 00:17:59 -0400180 __func__, ld_type->id);
181 }
182 spin_unlock(&pnfs_spinlock);
183
184 return status;
185}
186EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
187
188void
189pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
190{
191 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
192 spin_lock(&pnfs_spinlock);
193 list_del(&ld_type->pnfs_tblid);
194 spin_unlock(&pnfs_spinlock);
195}
196EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
Benny Halevye5e94012010-10-20 00:18:01 -0400197
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400198/*
199 * pNFS client layout cache
200 */
201
Fred Isamancc6e5342011-01-06 11:36:28 +0000202/* Need to hold i_lock if caller does not already hold reference */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000203void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400204pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
Benny Halevye5e94012010-10-20 00:18:01 -0400205{
Fred Isamancc6e5342011-01-06 11:36:28 +0000206 atomic_inc(&lo->plh_refcount);
207}
208
Benny Halevy636fb9c2011-05-22 19:51:33 +0300209static struct pnfs_layout_hdr *
210pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
211{
212 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
Trond Myklebust57934272012-09-20 20:37:23 -0400213 return ld->alloc_layout_hdr(ino, gfp_flags);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300214}
215
216static void
217pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
218{
Trond Myklebust9c626382012-09-20 17:31:43 -0400219 struct nfs_server *server = NFS_SERVER(lo->plh_inode);
220 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
221
222 if (!list_empty(&lo->plh_layouts)) {
223 struct nfs_client *clp = server->nfs_client;
224
225 spin_lock(&clp->cl_lock);
226 list_del_init(&lo->plh_layouts);
227 spin_unlock(&clp->cl_lock);
228 }
Peng Tao9fa40752011-07-30 20:52:32 -0400229 put_rpccred(lo->plh_lc_cred);
Trond Myklebust57934272012-09-20 20:37:23 -0400230 return ld->free_layout_hdr(lo);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300231}
232
Fred Isamancc6e5342011-01-06 11:36:28 +0000233static void
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400234pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
Fred Isamancc6e5342011-01-06 11:36:28 +0000235{
Trond Myklebustbb346f62012-09-20 15:52:13 -0400236 struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
Fred Isamancc6e5342011-01-06 11:36:28 +0000237 dprintk("%s: freeing layout cache %p\n", __func__, lo);
Trond Myklebustbb346f62012-09-20 15:52:13 -0400238 nfsi->layout = NULL;
239 /* Reset MDS Threshold I/O counters */
240 nfsi->write_io = 0;
241 nfsi->read_io = 0;
Benny Halevye5e94012010-10-20 00:18:01 -0400242}
243
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400244void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400245pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400246{
Fred Isamancc6e5342011-01-06 11:36:28 +0000247 struct inode *inode = lo->plh_inode;
248
Trond Myklebust13c13a62016-01-26 23:12:11 -0500249 pnfs_layoutreturn_before_put_layout_hdr(lo);
250
Fred Isamancc6e5342011-01-06 11:36:28 +0000251 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
Peng Tao566f8732014-10-10 23:25:46 +0800252 if (!list_empty(&lo->plh_segs))
253 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400254 pnfs_detach_layout_hdr(lo);
Fred Isamancc6e5342011-01-06 11:36:28 +0000255 spin_unlock(&inode->i_lock);
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400256 pnfs_free_layout_hdr(lo);
Fred Isamancc6e5342011-01-06 11:36:28 +0000257 }
Andy Adamson974cec82010-10-20 00:18:02 -0400258}
259
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500260/*
261 * Mark a pnfs_layout_hdr and all associated layout segments as invalid
262 *
263 * In order to continue using the pnfs_layout_hdr, a full recovery
264 * is required.
265 * Note that caller must hold inode->i_lock.
266 */
Trond Myklebust5f46be02016-07-22 11:25:27 -0400267int
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500268pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
269 struct list_head *lseg_list)
270{
271 struct pnfs_layout_range range = {
272 .iomode = IOMODE_ANY,
273 .offset = 0,
274 .length = NFS4_MAX_UINT64,
275 };
276
277 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Jeff Layton6d597e12016-05-17 12:28:42 -0400278 return pnfs_mark_matching_lsegs_invalid(lo, lseg_list, &range, 0);
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500279}
280
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400281static int
282pnfs_iomode_to_fail_bit(u32 iomode)
283{
284 return iomode == IOMODE_RW ?
285 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
286}
287
288static void
Trond Myklebust3e621212012-09-24 13:07:16 -0400289pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400290{
Trond Myklebust25c75332012-09-18 17:01:12 -0400291 lo->plh_retry_timestamp = jiffies;
Yanchuan Nian39e88fc2013-01-04 20:19:49 +0800292 if (!test_and_set_bit(fail_bit, &lo->plh_flags))
Trond Myklebust3e621212012-09-24 13:07:16 -0400293 atomic_inc(&lo->plh_refcount);
294}
295
296static void
297pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
298{
299 if (test_and_clear_bit(fail_bit, &lo->plh_flags))
300 atomic_dec(&lo->plh_refcount);
301}
302
303static void
304pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
305{
306 struct inode *inode = lo->plh_inode;
Trond Myklebust115ce572012-09-20 21:19:43 -0400307 struct pnfs_layout_range range = {
308 .iomode = iomode,
309 .offset = 0,
310 .length = NFS4_MAX_UINT64,
311 };
312 LIST_HEAD(head);
Trond Myklebust3e621212012-09-24 13:07:16 -0400313
314 spin_lock(&inode->i_lock);
315 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
Jeff Layton6d597e12016-05-17 12:28:42 -0400316 pnfs_mark_matching_lsegs_invalid(lo, &head, &range, 0);
Trond Myklebust3e621212012-09-24 13:07:16 -0400317 spin_unlock(&inode->i_lock);
Trond Myklebust115ce572012-09-20 21:19:43 -0400318 pnfs_free_lseg_list(&head);
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400319 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
320 iomode == IOMODE_RW ? "RW" : "READ");
321}
322
323static bool
324pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
325{
Trond Myklebust25c75332012-09-18 17:01:12 -0400326 unsigned long start, end;
Trond Myklebust3e621212012-09-24 13:07:16 -0400327 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
328
329 if (test_bit(fail_bit, &lo->plh_flags) == 0)
Trond Myklebust25c75332012-09-18 17:01:12 -0400330 return false;
331 end = jiffies;
332 start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
333 if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
334 /* It is time to retry the failed layoutgets */
Trond Myklebust3e621212012-09-24 13:07:16 -0400335 pnfs_layout_clear_fail_bit(lo, fail_bit);
Trond Myklebust25c75332012-09-18 17:01:12 -0400336 return false;
337 }
338 return true;
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400339}
340
Andy Adamson974cec82010-10-20 00:18:02 -0400341static void
Trond Myklebust119cef972016-07-24 15:10:12 -0400342pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
343 const struct pnfs_layout_range *range,
344 const nfs4_stateid *stateid)
Andy Adamson974cec82010-10-20 00:18:02 -0400345{
Fred Isaman566052c2011-01-06 11:36:20 +0000346 INIT_LIST_HEAD(&lseg->pls_list);
Peng Taoa9bae562011-07-30 20:52:33 -0400347 INIT_LIST_HEAD(&lseg->pls_lc_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000348 atomic_set(&lseg->pls_refcount, 1);
Fred Isaman4541d162011-01-06 11:36:23 +0000349 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000350 lseg->pls_layout = lo;
Trond Myklebust119cef972016-07-24 15:10:12 -0400351 lseg->pls_range = *range;
352 lseg->pls_seq = be32_to_cpu(stateid->seqid);
Andy Adamson974cec82010-10-20 00:18:02 -0400353}
354
Trond Myklebust905ca192012-09-20 20:46:49 -0400355static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400356{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000357 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400358
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400359 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400360}
361
Fred Isamand684d2a2011-03-01 01:34:13 +0000362static void
Trond Myklebust57036a32012-09-20 16:33:30 -0400363pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
364 struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400365{
Trond Myklebust57036a32012-09-20 16:33:30 -0400366 struct inode *inode = lo->plh_inode;
Fred Isamand684d2a2011-03-01 01:34:13 +0000367
Benny Halevyd20581a2011-05-22 19:52:03 +0300368 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000369 list_del_init(&lseg->pls_list);
Trond Myklebust8f0d27d2012-09-20 20:57:11 -0400370 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
371 atomic_dec(&lo->plh_refcount);
Trond Myklebust2d148c72016-06-17 16:48:26 -0400372 if (list_empty(&lo->plh_segs)) {
Trond Myklebust334a8f32016-09-04 12:46:35 -0400373 if (atomic_read(&lo->plh_outstanding) == 0)
374 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Trond Myklebust173f77e2012-09-21 15:49:42 -0400375 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Trond Myklebust2d148c72016-06-17 16:48:26 -0400376 }
Fred Isamand684d2a2011-03-01 01:34:13 +0000377 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
378}
379
Fred Isamanbae724e2011-03-01 01:34:15 +0000380void
Trond Myklebust9369a432012-09-18 20:57:08 -0400381pnfs_put_lseg(struct pnfs_layout_segment *lseg)
Fred Isamand684d2a2011-03-01 01:34:13 +0000382{
Trond Myklebust57036a32012-09-20 16:33:30 -0400383 struct pnfs_layout_hdr *lo;
Fred Isamand684d2a2011-03-01 01:34:13 +0000384 struct inode *inode;
385
386 if (!lseg)
387 return;
388
Fred Isaman4541d162011-01-06 11:36:23 +0000389 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
390 atomic_read(&lseg->pls_refcount),
391 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500392
Trond Myklebust57036a32012-09-20 16:33:30 -0400393 lo = lseg->pls_layout;
394 inode = lo->plh_inode;
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500395
Fred Isamand684d2a2011-03-01 01:34:13 +0000396 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
Trond Myklebustfaa4a542015-07-09 18:24:07 +0200397 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
398 spin_unlock(&inode->i_lock);
399 return;
400 }
Trond Myklebust8f0d27d2012-09-20 20:57:11 -0400401 pnfs_get_layout_hdr(lo);
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500402 pnfs_layout_remove_lseg(lo, lseg);
403 spin_unlock(&inode->i_lock);
404 pnfs_free_lseg(lseg);
405 pnfs_put_layout_hdr(lo);
Fred Isaman4541d162011-01-06 11:36:23 +0000406 }
Andy Adamson974cec82010-10-20 00:18:02 -0400407}
Trond Myklebust9369a432012-09-18 20:57:08 -0400408EXPORT_SYMBOL_GPL(pnfs_put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400409
Trond Myklebust6543f802014-10-08 16:39:12 -0400410static void pnfs_free_lseg_async_work(struct work_struct *work)
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400411{
412 struct pnfs_layout_segment *lseg;
Trond Myklebust6543f802014-10-08 16:39:12 -0400413 struct pnfs_layout_hdr *lo;
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400414
415 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
Trond Myklebust6543f802014-10-08 16:39:12 -0400416 lo = lseg->pls_layout;
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400417
Trond Myklebust6543f802014-10-08 16:39:12 -0400418 pnfs_free_lseg(lseg);
419 pnfs_put_layout_hdr(lo);
420}
421
422static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg)
423{
424 INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work);
425 schedule_work(&lseg->pls_work);
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400426}
427
428void
Trond Myklebust6543f802014-10-08 16:39:12 -0400429pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg)
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400430{
Trond Myklebust6543f802014-10-08 16:39:12 -0400431 if (!lseg)
432 return;
433
434 assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock);
435
436 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
437 atomic_read(&lseg->pls_refcount),
438 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
439 if (atomic_dec_and_test(&lseg->pls_refcount)) {
440 struct pnfs_layout_hdr *lo = lseg->pls_layout;
Trond Myklebustfaa4a542015-07-09 18:24:07 +0200441 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
442 return;
Trond Myklebust6543f802014-10-08 16:39:12 -0400443 pnfs_get_layout_hdr(lo);
444 pnfs_layout_remove_lseg(lo, lseg);
445 pnfs_free_lseg_async(lseg);
446 }
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400447}
Trond Myklebust6543f802014-10-08 16:39:12 -0400448EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked);
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400449
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400450static u64
Benny Halevyfb3296e2011-05-22 19:47:26 +0300451end_offset(u64 start, u64 len)
Fred Isaman4541d162011-01-06 11:36:23 +0000452{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300453 u64 end;
454
455 end = start + len;
456 return end >= start ? end : NFS4_MAX_UINT64;
457}
458
Benny Halevyfb3296e2011-05-22 19:47:26 +0300459/*
460 * is l2 fully contained in l1?
461 * start1 end1
462 * [----------------------------------)
463 * start2 end2
464 * [----------------)
465 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400466static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -0400467pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400468 const struct pnfs_layout_range *l2)
Benny Halevyfb3296e2011-05-22 19:47:26 +0300469{
470 u64 start1 = l1->offset;
471 u64 end1 = end_offset(start1, l1->length);
472 u64 start2 = l2->offset;
473 u64 end2 = end_offset(start2, l2->length);
474
475 return (start1 <= start2) && (end1 >= end2);
476}
477
478/*
479 * is l1 and l2 intersecting?
480 * start1 end1
481 * [----------------------------------)
482 * start2 end2
483 * [----------------)
484 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400485static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -0400486pnfs_lseg_range_intersecting(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400487 const struct pnfs_layout_range *l2)
Benny Halevyfb3296e2011-05-22 19:47:26 +0300488{
489 u64 start1 = l1->offset;
490 u64 end1 = end_offset(start1, l1->length);
491 u64 start2 = l2->offset;
492 u64 end2 = end_offset(start2, l2->length);
493
494 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
495 (end2 == NFS4_MAX_UINT64 || end2 > start1);
496}
497
Trond Myklebust24956802013-03-20 13:03:00 -0400498static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
499 struct list_head *tmp_list)
500{
501 if (!atomic_dec_and_test(&lseg->pls_refcount))
502 return false;
503 pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
504 list_add(&lseg->pls_list, tmp_list);
505 return true;
506}
507
Fred Isaman4541d162011-01-06 11:36:23 +0000508/* Returns 1 if lseg is removed from list, 0 otherwise */
509static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
510 struct list_head *tmp_list)
511{
512 int rv = 0;
513
514 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
515 /* Remove the reference keeping the lseg in the
516 * list. It will now be removed when all
517 * outstanding io is finished.
518 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000519 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
520 atomic_read(&lseg->pls_refcount));
Trond Myklebust24956802013-03-20 13:03:00 -0400521 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
Fred Isamand684d2a2011-03-01 01:34:13 +0000522 rv = 1;
Fred Isaman4541d162011-01-06 11:36:23 +0000523 }
524 return rv;
525}
526
Jeff Layton6d597e12016-05-17 12:28:42 -0400527/*
528 * Compare 2 layout stateid sequence ids, to see which is newer,
529 * taking into account wraparound issues.
530 */
531static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
532{
533 return (s32)(s1 - s2) > 0;
534}
535
Trond Myklebuste036f462016-07-22 11:13:22 -0400536static bool
537pnfs_should_free_range(const struct pnfs_layout_range *lseg_range,
538 const struct pnfs_layout_range *recall_range)
539{
540 return (recall_range->iomode == IOMODE_ANY ||
541 lseg_range->iomode == recall_range->iomode) &&
542 pnfs_lseg_range_intersecting(lseg_range, recall_range);
543}
544
545static bool
546pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg,
547 const struct pnfs_layout_range *recall_range,
548 u32 seq)
549{
550 if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq))
551 return false;
552 if (recall_range == NULL)
553 return true;
554 return pnfs_should_free_range(&lseg->pls_range, recall_range);
555}
556
Jeff Layton6d597e12016-05-17 12:28:42 -0400557/**
558 * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later
559 * @lo: layout header containing the lsegs
560 * @tmp_list: list head where doomed lsegs should go
561 * @recall_range: optional recall range argument to match (may be NULL)
562 * @seq: only invalidate lsegs obtained prior to this sequence (may be 0)
563 *
564 * Walk the list of lsegs in the layout header, and tear down any that should
565 * be destroyed. If "recall_range" is specified then the segment must match
566 * that range. If "seq" is non-zero, then only match segments that were handed
567 * out at or before that sequence.
568 *
569 * Returns number of matching invalid lsegs remaining in list after scanning
570 * it and purging them.
Fred Isaman4541d162011-01-06 11:36:23 +0000571 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000572int
Trond Myklebust49a85062012-09-18 20:43:31 -0400573pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
Fred Isaman4541d162011-01-06 11:36:23 +0000574 struct list_head *tmp_list,
Jeff Layton6d597e12016-05-17 12:28:42 -0400575 const struct pnfs_layout_range *recall_range,
576 u32 seq)
Andy Adamson974cec82010-10-20 00:18:02 -0400577{
578 struct pnfs_layout_segment *lseg, *next;
Trond Myklebust71b39852016-01-04 12:41:15 -0500579 int remaining = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400580
581 dprintk("%s:Begin lo %p\n", __func__, lo);
582
Trond Myklebust8006bfb2012-09-21 14:48:04 -0400583 if (list_empty(&lo->plh_segs))
Fred Isaman38511722011-02-03 18:28:50 +0000584 return 0;
Fred Isaman4541d162011-01-06 11:36:23 +0000585 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Trond Myklebuste036f462016-07-22 11:13:22 -0400586 if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
Jeff Layton6d597e12016-05-17 12:28:42 -0400587 dprintk("%s: freeing lseg %p iomode %d seq %u"
Fred Isaman4541d162011-01-06 11:36:23 +0000588 "offset %llu length %llu\n", __func__,
Jeff Layton6d597e12016-05-17 12:28:42 -0400589 lseg, lseg->pls_range.iomode, lseg->pls_seq,
590 lseg->pls_range.offset, lseg->pls_range.length);
Trond Myklebust71b39852016-01-04 12:41:15 -0500591 if (!mark_lseg_invalid(lseg, tmp_list))
592 remaining++;
Fred Isaman4541d162011-01-06 11:36:23 +0000593 }
Trond Myklebust71b39852016-01-04 12:41:15 -0500594 dprintk("%s:Return %i\n", __func__, remaining);
595 return remaining;
Andy Adamson974cec82010-10-20 00:18:02 -0400596}
597
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000598/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000599void
Fred Isaman4541d162011-01-06 11:36:23 +0000600pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400601{
Fred Isaman4541d162011-01-06 11:36:23 +0000602 struct pnfs_layout_segment *lseg, *tmp;
Andy Adamson974cec82010-10-20 00:18:02 -0400603
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000604 if (list_empty(free_me))
605 return;
606
Fred Isaman4541d162011-01-06 11:36:23 +0000607 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000608 list_del(&lseg->pls_list);
Trond Myklebust905ca192012-09-20 20:46:49 -0400609 pnfs_free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400610 }
611}
612
Benny Halevye5e94012010-10-20 00:18:01 -0400613void
614pnfs_destroy_layout(struct nfs_inode *nfsi)
615{
616 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400617 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400618
619 spin_lock(&nfsi->vfs_inode.i_lock);
620 lo = nfsi->layout;
621 if (lo) {
Trond Myklebust3e621212012-09-24 13:07:16 -0400622 pnfs_get_layout_hdr(lo);
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500623 pnfs_mark_layout_stateid_invalid(lo, &tmp_list);
Trond Myklebust3e621212012-09-24 13:07:16 -0400624 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
625 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
626 spin_unlock(&nfsi->vfs_inode.i_lock);
627 pnfs_free_lseg_list(&tmp_list);
628 pnfs_put_layout_hdr(lo);
629 } else
630 spin_unlock(&nfsi->vfs_inode.i_lock);
Benny Halevye5e94012010-10-20 00:18:01 -0400631}
Andy Adamson041245c2012-04-27 17:53:53 -0400632EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
Benny Halevye5e94012010-10-20 00:18:01 -0400633
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500634static bool
635pnfs_layout_add_bulk_destroy_list(struct inode *inode,
636 struct list_head *layout_list)
637{
638 struct pnfs_layout_hdr *lo;
639 bool ret = false;
640
641 spin_lock(&inode->i_lock);
642 lo = NFS_I(inode)->layout;
643 if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
644 pnfs_get_layout_hdr(lo);
645 list_add(&lo->plh_bulk_destroy, layout_list);
646 ret = true;
647 }
648 spin_unlock(&inode->i_lock);
649 return ret;
650}
651
652/* Caller must hold rcu_read_lock and clp->cl_lock */
653static int
654pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
655 struct nfs_server *server,
656 struct list_head *layout_list)
657{
658 struct pnfs_layout_hdr *lo, *next;
659 struct inode *inode;
660
661 list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
662 inode = igrab(lo->plh_inode);
663 if (inode == NULL)
664 continue;
665 list_del_init(&lo->plh_layouts);
666 if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
667 continue;
668 rcu_read_unlock();
669 spin_unlock(&clp->cl_lock);
670 iput(inode);
671 spin_lock(&clp->cl_lock);
672 rcu_read_lock();
673 return -EAGAIN;
674 }
675 return 0;
676}
677
678static int
679pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
680 bool is_bulk_recall)
681{
682 struct pnfs_layout_hdr *lo;
683 struct inode *inode;
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500684 LIST_HEAD(lseg_list);
685 int ret = 0;
686
687 while (!list_empty(layout_list)) {
688 lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
689 plh_bulk_destroy);
690 dprintk("%s freeing layout for inode %lu\n", __func__,
691 lo->plh_inode->i_ino);
692 inode = lo->plh_inode;
Christoph Hellwig7c5d1872014-09-10 08:23:29 -0700693
694 pnfs_layoutcommit_inode(inode, false);
695
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500696 spin_lock(&inode->i_lock);
697 list_del_init(&lo->plh_bulk_destroy);
Trond Myklebust9fd4b9f2016-02-22 17:46:34 -0500698 if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
699 if (is_bulk_recall)
700 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500701 ret = -EAGAIN;
Trond Myklebust9fd4b9f2016-02-22 17:46:34 -0500702 }
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500703 spin_unlock(&inode->i_lock);
704 pnfs_free_lseg_list(&lseg_list);
Trond Myklebustb20135d2015-12-31 09:28:06 -0500705 /* Free all lsegs that are attached to commit buckets */
706 nfs_commit_inode(inode, 0);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500707 pnfs_put_layout_hdr(lo);
708 iput(inode);
709 }
710 return ret;
711}
712
713int
714pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
715 struct nfs_fsid *fsid,
716 bool is_recall)
717{
718 struct nfs_server *server;
719 LIST_HEAD(layout_list);
720
721 spin_lock(&clp->cl_lock);
722 rcu_read_lock();
723restart:
724 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
725 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
726 continue;
727 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
728 server,
729 &layout_list) != 0)
730 goto restart;
731 }
732 rcu_read_unlock();
733 spin_unlock(&clp->cl_lock);
734
735 if (list_empty(&layout_list))
736 return 0;
737 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
738}
739
740int
741pnfs_destroy_layouts_byclid(struct nfs_client *clp,
742 bool is_recall)
743{
744 struct nfs_server *server;
745 LIST_HEAD(layout_list);
746
747 spin_lock(&clp->cl_lock);
748 rcu_read_lock();
749restart:
750 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
751 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
752 server,
753 &layout_list) != 0)
754 goto restart;
755 }
756 rcu_read_unlock();
757 spin_unlock(&clp->cl_lock);
758
759 if (list_empty(&layout_list))
760 return 0;
761 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
762}
763
Andy Adamson974cec82010-10-20 00:18:02 -0400764/*
765 * Called by the state manger to remove all layouts established under an
766 * expired lease.
767 */
768void
769pnfs_destroy_all_layouts(struct nfs_client *clp)
770{
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400771 nfs4_deviceid_mark_client_invalid(clp);
772 nfs4_deviceid_purge_client(clp);
773
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500774 pnfs_destroy_layouts_byclid(clp, false);
Andy Adamson974cec82010-10-20 00:18:02 -0400775}
776
Trond Myklebust2a59a042016-09-03 11:20:04 -0400777static void
778pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo)
779{
780 lo->plh_return_iomode = 0;
781 lo->plh_return_seq = 0;
782 clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
783}
784
Fred Isamanfd6002e2011-01-06 11:36:22 +0000785/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000786void
787pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
788 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400789{
Trond Myklebustecebb802016-07-24 11:46:06 -0400790 u32 oldseq, newseq, new_barrier = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400791
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500792 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
793 newseq = be32_to_cpu(new->seqid);
Trond Myklebust2a59a042016-09-03 11:20:04 -0400794
795 if (!pnfs_layout_is_valid(lo)) {
796 nfs4_stateid_copy(&lo->plh_stateid, new);
797 lo->plh_barrier = newseq;
798 pnfs_clear_layoutreturn_info(lo);
799 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
800 return;
801 }
802 if (pnfs_seqid_is_newer(newseq, oldseq)) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500803 nfs4_stateid_copy(&lo->plh_stateid, new);
Trond Myklebustecebb802016-07-24 11:46:06 -0400804 /*
805 * Because of wraparound, we want to keep the barrier
806 * "close" to the current seqids.
807 */
808 new_barrier = newseq - atomic_read(&lo->plh_outstanding);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000809 }
Trond Myklebustecebb802016-07-24 11:46:06 -0400810 if (update_barrier)
811 new_barrier = be32_to_cpu(new->seqid);
812 else if (new_barrier == 0)
813 return;
Trond Myklebust2a59a042016-09-03 11:20:04 -0400814 if (pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
Trond Myklebustecebb802016-07-24 11:46:06 -0400815 lo->plh_barrier = new_barrier;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400816}
817
Trond Myklebust19c54ab2012-10-05 16:56:58 -0700818static bool
819pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
820 const nfs4_stateid *stateid)
821{
822 u32 seqid = be32_to_cpu(stateid->seqid);
823
824 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
825}
826
Fred Isamancf7d63f2011-01-06 11:36:25 +0000827/* lget is set to 1 if called from inside send_layoutget call chain */
828static bool
Trond Myklebuste1c06f82015-08-04 16:40:08 -0400829pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000830{
Fred Isamanf7e89172011-01-06 11:36:32 +0000831 return lo->plh_block_lgets ||
Trond Myklebuste1c06f82015-08-04 16:40:08 -0400832 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000833}
834
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400835/*
Jeff Layton183d9e72016-05-17 12:28:47 -0400836 * Get layout from server.
837 * for now, assume that whole file layouts are requested.
838 * arg->offset: 0
839 * arg->length: all ones
840 */
Benny Halevye5e94012010-10-20 00:18:01 -0400841static struct pnfs_layout_segment *
842send_layoutget(struct pnfs_layout_hdr *lo,
843 struct nfs_open_context *ctx,
Jeff Layton183d9e72016-05-17 12:28:47 -0400844 nfs4_stateid *stateid,
Trond Myklebuste144e532016-01-04 12:52:53 -0500845 const struct pnfs_layout_range *range,
Jeff Layton183d9e72016-05-17 12:28:47 -0400846 long *timeout, gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400847{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000848 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400849 struct nfs_server *server = NFS_SERVER(ino);
850 struct nfs4_layoutget *lgp;
Trond Myklebust2d89a1d2015-08-31 02:05:47 -0700851 loff_t i_size;
Benny Halevye5e94012010-10-20 00:18:01 -0400852
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400853 dprintk("--> %s\n", __func__);
854
Jeff Layton4f2e9dc2015-11-25 13:43:14 -0500855 /*
856 * Synchronously retrieve layout information from server and
857 * store in lseg. If we race with a concurrent seqid morphing
858 * op, then re-send the LAYOUTGET.
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400859 */
Jeff Layton83026d82016-05-17 12:28:46 -0400860 lgp = kzalloc(sizeof(*lgp), gfp_flags);
861 if (lgp == NULL)
862 return ERR_PTR(-ENOMEM);
Jeff Layton4f2e9dc2015-11-25 13:43:14 -0500863
Jeff Layton83026d82016-05-17 12:28:46 -0400864 i_size = i_size_read(ino);
Jeff Layton4f2e9dc2015-11-25 13:43:14 -0500865
Jeff Layton83026d82016-05-17 12:28:46 -0400866 lgp->args.minlength = PAGE_SIZE;
867 if (lgp->args.minlength > range->length)
868 lgp->args.minlength = range->length;
869 if (range->iomode == IOMODE_READ) {
870 if (range->offset >= i_size)
871 lgp->args.minlength = 0;
872 else if (i_size - range->offset < lgp->args.minlength)
873 lgp->args.minlength = i_size - range->offset;
Jeff Laytond03ab292016-05-17 12:28:45 -0400874 }
Jeff Layton83026d82016-05-17 12:28:46 -0400875 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
876 pnfs_copy_range(&lgp->args.range, range);
877 lgp->args.type = server->pnfs_curr_ld->id;
878 lgp->args.inode = ino;
879 lgp->args.ctx = get_nfs_open_context(ctx);
Jeff Layton183d9e72016-05-17 12:28:47 -0400880 nfs4_stateid_copy(&lgp->args.stateid, stateid);
Jeff Layton83026d82016-05-17 12:28:46 -0400881 lgp->gfp_flags = gfp_flags;
882 lgp->cred = lo->plh_lc_cred;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400883
Jeff Layton183d9e72016-05-17 12:28:47 -0400884 return nfs4_proc_layoutget(lgp, timeout, gfp_flags);
Andy Adamson974cec82010-10-20 00:18:02 -0400885}
886
Trond Myklebust24956802013-03-20 13:03:00 -0400887static void pnfs_clear_layoutcommit(struct inode *inode,
888 struct list_head *head)
889{
890 struct nfs_inode *nfsi = NFS_I(inode);
891 struct pnfs_layout_segment *lseg, *tmp;
892
893 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
894 return;
895 list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
896 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
897 continue;
898 pnfs_lseg_dec_and_remove_zero(lseg, head);
899 }
900}
901
Tom Haynesd67ae822014-12-11 17:02:04 -0500902void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
903{
904 clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
905 smp_mb__after_atomic();
906 wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
Trond Myklebust7f273922015-07-09 18:40:01 +0200907 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
Tom Haynesd67ae822014-12-11 17:02:04 -0500908}
909
Trond Myklebust13c13a62016-01-26 23:12:11 -0500910static bool
Trond Myklebuste5fd1902016-07-21 12:44:15 -0400911pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
912 nfs4_stateid *stateid,
913 enum pnfs_iomode *iomode)
Trond Myklebust13c13a62016-01-26 23:12:11 -0500914{
Trond Myklebustbf0291d2016-09-03 10:39:51 -0400915 /* Serialise LAYOUTGET/LAYOUTRETURN */
916 if (atomic_read(&lo->plh_outstanding) != 0)
917 return false;
Trond Myklebust13c13a62016-01-26 23:12:11 -0500918 if (test_and_set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
919 return false;
Trond Myklebust13c13a62016-01-26 23:12:11 -0500920 pnfs_get_layout_hdr(lo);
Trond Myklebuste5fd1902016-07-21 12:44:15 -0400921 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
922 if (stateid != NULL) {
923 nfs4_stateid_copy(stateid, &lo->plh_stateid);
924 if (lo->plh_return_seq != 0)
925 stateid->seqid = cpu_to_be32(lo->plh_return_seq);
926 }
927 if (iomode != NULL)
928 *iomode = lo->plh_return_iomode;
929 pnfs_clear_layoutreturn_info(lo);
930 return true;
931 }
932 if (stateid != NULL)
933 nfs4_stateid_copy(stateid, &lo->plh_stateid);
934 if (iomode != NULL)
935 *iomode = IOMODE_ANY;
Trond Myklebust13c13a62016-01-26 23:12:11 -0500936 return true;
937}
938
Peng Taof40eb5d2014-09-06 00:53:22 +0800939static int
Trond Myklebusted429d62016-01-04 12:30:55 -0500940pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, const nfs4_stateid *stateid,
Peng Tao6c166052014-11-17 09:30:40 +0800941 enum pnfs_iomode iomode, bool sync)
Peng Taof40eb5d2014-09-06 00:53:22 +0800942{
943 struct inode *ino = lo->plh_inode;
944 struct nfs4_layoutreturn *lrp;
945 int status = 0;
946
Trond Myklebuste4af4402015-02-05 17:05:08 -0500947 lrp = kzalloc(sizeof(*lrp), GFP_NOFS);
Peng Taof40eb5d2014-09-06 00:53:22 +0800948 if (unlikely(lrp == NULL)) {
949 status = -ENOMEM;
950 spin_lock(&ino->i_lock);
Tom Haynesd67ae822014-12-11 17:02:04 -0500951 pnfs_clear_layoutreturn_waitbit(lo);
Peng Taof40eb5d2014-09-06 00:53:22 +0800952 spin_unlock(&ino->i_lock);
953 pnfs_put_layout_hdr(lo);
954 goto out;
955 }
956
Trond Myklebusted429d62016-01-04 12:30:55 -0500957 nfs4_stateid_copy(&lrp->args.stateid, stateid);
Peng Taof40eb5d2014-09-06 00:53:22 +0800958 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
959 lrp->args.inode = ino;
Peng Tao15eb67c2014-11-17 09:30:36 +0800960 lrp->args.range.iomode = iomode;
961 lrp->args.range.offset = 0;
962 lrp->args.range.length = NFS4_MAX_UINT64;
Peng Taof40eb5d2014-09-06 00:53:22 +0800963 lrp->args.layout = lo;
964 lrp->clp = NFS_SERVER(ino)->nfs_client;
965 lrp->cred = lo->plh_lc_cred;
966
Peng Tao6c166052014-11-17 09:30:40 +0800967 status = nfs4_proc_layoutreturn(lrp, sync);
Peng Taof40eb5d2014-09-06 00:53:22 +0800968out:
969 dprintk("<-- %s status: %d\n", __func__, status);
970 return status;
971}
972
Trond Myklebust13c13a62016-01-26 23:12:11 -0500973/* Return true if layoutreturn is needed */
974static bool
975pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
976{
977 struct pnfs_layout_segment *s;
978
Trond Myklebust2370abd2016-01-27 20:32:50 -0500979 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
Trond Myklebust13c13a62016-01-26 23:12:11 -0500980 return false;
981
982 /* Defer layoutreturn until all lsegs are done */
983 list_for_each_entry(s, &lo->plh_segs, pls_list) {
984 if (test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
985 return false;
986 }
987
988 return true;
989}
990
991static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
992{
993 struct inode *inode= lo->plh_inode;
994
Trond Myklebust2370abd2016-01-27 20:32:50 -0500995 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
Trond Myklebust13c13a62016-01-26 23:12:11 -0500996 return;
997 spin_lock(&inode->i_lock);
998 if (pnfs_layout_need_return(lo)) {
999 nfs4_stateid stateid;
1000 enum pnfs_iomode iomode;
1001 bool send;
1002
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001003 send = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
Trond Myklebust13c13a62016-01-26 23:12:11 -05001004 spin_unlock(&inode->i_lock);
1005 if (send) {
1006 /* Send an async layoutreturn so we dont deadlock */
1007 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1008 }
1009 } else
1010 spin_unlock(&inode->i_lock);
1011}
1012
Andy Adamson293b3b02012-06-20 15:03:34 -04001013/*
1014 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
1015 * when the layout segment list is empty.
1016 *
1017 * Note that a pnfs_layout_hdr can exist with an empty layout segment
1018 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
1019 * deviceid is marked invalid.
1020 */
Benny Halevycbe82602011-05-22 19:52:37 +03001021int
1022_pnfs_return_layout(struct inode *ino)
1023{
1024 struct pnfs_layout_hdr *lo = NULL;
1025 struct nfs_inode *nfsi = NFS_I(ino);
1026 LIST_HEAD(tmp_list);
Benny Halevycbe82602011-05-22 19:52:37 +03001027 nfs4_stateid stateid;
Andy Adamson293b3b02012-06-20 15:03:34 -04001028 int status = 0, empty;
Trond Myklebust7f273922015-07-09 18:40:01 +02001029 bool send;
Benny Halevycbe82602011-05-22 19:52:37 +03001030
Andy Adamson366d5052012-06-20 15:03:33 -04001031 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
Benny Halevycbe82602011-05-22 19:52:37 +03001032
1033 spin_lock(&ino->i_lock);
1034 lo = nfsi->layout;
Trond Myklebuste5929f32012-09-21 16:37:02 -04001035 if (!lo) {
Benny Halevycbe82602011-05-22 19:52:37 +03001036 spin_unlock(&ino->i_lock);
Andy Adamson293b3b02012-06-20 15:03:34 -04001037 dprintk("NFS: %s no layout to return\n", __func__);
1038 goto out;
Benny Halevycbe82602011-05-22 19:52:37 +03001039 }
Benny Halevycbe82602011-05-22 19:52:37 +03001040 /* Reference matched in nfs4_layoutreturn_release */
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001041 pnfs_get_layout_hdr(lo);
Andy Adamson293b3b02012-06-20 15:03:34 -04001042 empty = list_empty(&lo->plh_segs);
Trond Myklebust24956802013-03-20 13:03:00 -04001043 pnfs_clear_layoutcommit(ino, &tmp_list);
Jeff Layton6d597e12016-05-17 12:28:42 -04001044 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL, 0);
Christoph Hellwigc88953d2014-09-10 08:23:31 -07001045
1046 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
1047 struct pnfs_layout_range range = {
1048 .iomode = IOMODE_ANY,
1049 .offset = 0,
1050 .length = NFS4_MAX_UINT64,
1051 };
1052 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1053 }
1054
Andy Adamson293b3b02012-06-20 15:03:34 -04001055 /* Don't send a LAYOUTRETURN if list was initially empty */
1056 if (empty) {
1057 spin_unlock(&ino->i_lock);
Andy Adamson293b3b02012-06-20 15:03:34 -04001058 dprintk("NFS: %s no layout segments to return\n", __func__);
Trond Myklebust7f273922015-07-09 18:40:01 +02001059 goto out_put_layout_hdr;
Andy Adamson293b3b02012-06-20 15:03:34 -04001060 }
Christoph Hellwig47abade2014-08-21 11:09:22 -05001061
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001062 send = pnfs_prepare_layoutreturn(lo, &stateid, NULL);
Benny Halevycbe82602011-05-22 19:52:37 +03001063 spin_unlock(&ino->i_lock);
1064 pnfs_free_lseg_list(&tmp_list);
Trond Myklebust7f273922015-07-09 18:40:01 +02001065 if (send)
Trond Myklebusted429d62016-01-04 12:30:55 -05001066 status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
Trond Myklebust7f273922015-07-09 18:40:01 +02001067out_put_layout_hdr:
1068 pnfs_put_layout_hdr(lo);
Benny Halevycbe82602011-05-22 19:52:37 +03001069out:
1070 dprintk("<-- %s status: %d\n", __func__, status);
1071 return status;
1072}
Andy Adamson0a57cda2012-04-27 17:53:50 -04001073EXPORT_SYMBOL_GPL(_pnfs_return_layout);
Benny Halevycbe82602011-05-22 19:52:37 +03001074
Trond Myklebust24028672013-03-20 13:23:33 -04001075int
1076pnfs_commit_and_return_layout(struct inode *inode)
1077{
1078 struct pnfs_layout_hdr *lo;
1079 int ret;
1080
1081 spin_lock(&inode->i_lock);
1082 lo = NFS_I(inode)->layout;
1083 if (lo == NULL) {
1084 spin_unlock(&inode->i_lock);
1085 return 0;
1086 }
1087 pnfs_get_layout_hdr(lo);
1088 /* Block new layoutgets and read/write to ds */
1089 lo->plh_block_lgets++;
1090 spin_unlock(&inode->i_lock);
1091 filemap_fdatawait(inode->i_mapping);
1092 ret = pnfs_layoutcommit_inode(inode, true);
1093 if (ret == 0)
1094 ret = _pnfs_return_layout(inode);
1095 spin_lock(&inode->i_lock);
1096 lo->plh_block_lgets--;
1097 spin_unlock(&inode->i_lock);
1098 pnfs_put_layout_hdr(lo);
1099 return ret;
1100}
1101
Fred Isamanf7e89172011-01-06 11:36:32 +00001102bool pnfs_roc(struct inode *ino)
1103{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001104 struct nfs_inode *nfsi = NFS_I(ino);
1105 struct nfs_open_context *ctx;
1106 struct nfs4_state *state;
Fred Isamanf7e89172011-01-06 11:36:32 +00001107 struct pnfs_layout_hdr *lo;
1108 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao193e3aa2014-11-17 09:30:41 +08001109 nfs4_stateid stateid;
Fred Isamanf7e89172011-01-06 11:36:32 +00001110 LIST_HEAD(tmp_list);
Peng Taoe755d632015-08-19 13:49:19 +08001111 bool found = false, layoutreturn = false, roc = false;
Fred Isamanf7e89172011-01-06 11:36:32 +00001112
1113 spin_lock(&ino->i_lock);
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001114 lo = nfsi->layout;
Peng Tao39761432015-08-21 12:49:44 +08001115 if (!lo || test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001116 goto out_noroc;
1117
Peng Taoe755d632015-08-19 13:49:19 +08001118 /* no roc if we hold a delegation */
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001119 if (nfs4_check_delegation(ino, FMODE_READ))
1120 goto out_noroc;
1121
1122 list_for_each_entry(ctx, &nfsi->open_files, list) {
1123 state = ctx->state;
1124 /* Don't return layout if there is open file state */
1125 if (state != NULL && state->state != 0)
1126 goto out_noroc;
1127 }
1128
Peng Taoe755d632015-08-19 13:49:19 +08001129 /* always send layoutreturn if being marked so */
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001130 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1131 layoutreturn = pnfs_prepare_layoutreturn(lo,
1132 &stateid, NULL);
Peng Taoe755d632015-08-19 13:49:19 +08001133
Fred Isamanf7e89172011-01-06 11:36:32 +00001134 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
Peng Taoe755d632015-08-19 13:49:19 +08001135 /* If we are sending layoutreturn, invalidate all valid lsegs */
1136 if (layoutreturn || test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
Fred Isamanf7e89172011-01-06 11:36:32 +00001137 mark_lseg_invalid(lseg, &tmp_list);
1138 found = true;
1139 }
Peng Tao500d7012015-09-22 11:35:22 +08001140 /* ROC in two conditions:
Peng Taoe755d632015-08-19 13:49:19 +08001141 * 1. there are ROC lsegs
1142 * 2. we don't send layoutreturn
Peng Taoe755d632015-08-19 13:49:19 +08001143 */
Peng Tao500d7012015-09-22 11:35:22 +08001144 if (found && !layoutreturn) {
1145 /* lo ref dropped in pnfs_roc_release() */
1146 pnfs_get_layout_hdr(lo);
Peng Taoe755d632015-08-19 13:49:19 +08001147 roc = true;
Peng Tao500d7012015-09-22 11:35:22 +08001148 }
Peng Taoe755d632015-08-19 13:49:19 +08001149
1150out_noroc:
Fred Isamanf7e89172011-01-06 11:36:32 +00001151 spin_unlock(&ino->i_lock);
1152 pnfs_free_lseg_list(&tmp_list);
Trond Myklebust71401712015-03-25 12:36:13 -04001153 pnfs_layoutcommit_inode(ino, true);
Peng Taoe755d632015-08-19 13:49:19 +08001154 if (layoutreturn)
Trond Myklebusted429d62016-01-04 12:30:55 -05001155 pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
Peng Taoe755d632015-08-19 13:49:19 +08001156 return roc;
Fred Isamanf7e89172011-01-06 11:36:32 +00001157}
1158
1159void pnfs_roc_release(struct inode *ino)
1160{
1161 struct pnfs_layout_hdr *lo;
1162
1163 spin_lock(&ino->i_lock);
1164 lo = NFS_I(ino)->layout;
Trond Myklebust5c4a79f2015-08-04 15:57:13 -04001165 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust6622c3e2012-09-20 17:23:11 -04001166 if (atomic_dec_and_test(&lo->plh_refcount)) {
1167 pnfs_detach_layout_hdr(lo);
1168 spin_unlock(&ino->i_lock);
1169 pnfs_free_layout_hdr(lo);
1170 } else
1171 spin_unlock(&ino->i_lock);
Fred Isamanf7e89172011-01-06 11:36:32 +00001172}
1173
1174void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
1175{
1176 struct pnfs_layout_hdr *lo;
1177
1178 spin_lock(&ino->i_lock);
1179 lo = NFS_I(ino)->layout;
Trond Myklebust0f35ad62012-10-04 16:28:17 -07001180 if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
Fred Isamanf7e89172011-01-06 11:36:32 +00001181 lo->plh_barrier = barrier;
1182 spin_unlock(&ino->i_lock);
Trond Myklebust6a463beb2015-08-20 15:40:47 -05001183 trace_nfs4_layoutreturn_on_close(ino, 0);
Fred Isamanf7e89172011-01-06 11:36:32 +00001184}
1185
Trond Myklebust4ff376f2015-08-18 23:23:21 -05001186void pnfs_roc_get_barrier(struct inode *ino, u32 *barrier)
Fred Isamanf7e89172011-01-06 11:36:32 +00001187{
1188 struct nfs_inode *nfsi = NFS_I(ino);
Trond Myklebust7fdab062012-09-20 20:15:57 -04001189 struct pnfs_layout_hdr *lo;
Trond Myklebust7fdab062012-09-20 20:15:57 -04001190 u32 current_seqid;
Fred Isamanf7e89172011-01-06 11:36:32 +00001191
1192 spin_lock(&ino->i_lock);
Trond Myklebust7fdab062012-09-20 20:15:57 -04001193 lo = nfsi->layout;
1194 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
Fred Isamanf7e89172011-01-06 11:36:32 +00001195
Trond Myklebust7fdab062012-09-20 20:15:57 -04001196 /* Since close does not return a layout stateid for use as
1197 * a barrier, we choose the worst-case barrier.
1198 */
1199 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
Fred Isamanf7e89172011-01-06 11:36:32 +00001200 spin_unlock(&ino->i_lock);
Fred Isamanf7e89172011-01-06 11:36:32 +00001201}
1202
Peng Tao500d7012015-09-22 11:35:22 +08001203bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
1204{
1205 struct nfs_inode *nfsi = NFS_I(ino);
1206 struct pnfs_layout_hdr *lo;
1207 bool sleep = false;
1208
1209 /* we might not have grabbed lo reference. so need to check under
1210 * i_lock */
1211 spin_lock(&ino->i_lock);
1212 lo = nfsi->layout;
1213 if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
1214 sleep = true;
1215 spin_unlock(&ino->i_lock);
1216
1217 if (sleep)
1218 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
1219
1220 return sleep;
1221}
1222
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001223/*
1224 * Compare two layout segments for sorting into layout cache.
1225 * We want to preferentially return RW over RO layouts, so ensure those
1226 * are seen first.
1227 */
1228static s64
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001229pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001230 const struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001231{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001232 s64 d;
1233
1234 /* high offset > low offset */
1235 d = l1->offset - l2->offset;
1236 if (d)
1237 return d;
1238
1239 /* short length > long length */
1240 d = l2->length - l1->length;
1241 if (d)
1242 return d;
1243
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001244 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +03001245 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001246}
1247
Trond Myklebust03772d22015-08-25 08:54:17 -04001248static bool
1249pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
1250 const struct pnfs_layout_range *l2)
Andy Adamson974cec82010-10-20 00:18:02 -04001251{
Trond Myklebust03772d22015-08-25 08:54:17 -04001252 return pnfs_lseg_range_cmp(l1, l2) > 0;
1253}
1254
1255static bool
1256pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
1257 struct pnfs_layout_segment *old)
1258{
1259 return false;
1260}
1261
1262void
1263pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1264 struct pnfs_layout_segment *lseg,
1265 bool (*is_after)(const struct pnfs_layout_range *,
1266 const struct pnfs_layout_range *),
1267 bool (*do_merge)(struct pnfs_layout_segment *,
1268 struct pnfs_layout_segment *),
1269 struct list_head *free_me)
1270{
1271 struct pnfs_layout_segment *lp, *tmp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001272
Andy Adamson974cec82010-10-20 00:18:02 -04001273 dprintk("%s:Begin\n", __func__);
1274
Trond Myklebust03772d22015-08-25 08:54:17 -04001275 list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
1276 if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
1277 continue;
1278 if (do_merge(lseg, lp)) {
1279 mark_lseg_invalid(lp, free_me);
1280 continue;
1281 }
1282 if (is_after(&lseg->pls_range, &lp->pls_range))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001283 continue;
Fred Isaman566052c2011-01-06 11:36:20 +00001284 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001285 dprintk("%s: inserted lseg %p "
1286 "iomode %d offset %llu length %llu before "
1287 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +00001288 __func__, lseg, lseg->pls_range.iomode,
1289 lseg->pls_range.offset, lseg->pls_range.length,
1290 lp, lp->pls_range.iomode, lp->pls_range.offset,
1291 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +03001292 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -04001293 }
Benny Halevyfb3296e2011-05-22 19:47:26 +03001294 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1295 dprintk("%s: inserted lseg %p "
1296 "iomode %d offset %llu length %llu at tail\n",
1297 __func__, lseg, lseg->pls_range.iomode,
1298 lseg->pls_range.offset, lseg->pls_range.length);
1299out:
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001300 pnfs_get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -04001301
1302 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001303}
Trond Myklebust03772d22015-08-25 08:54:17 -04001304EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
1305
1306static void
1307pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1308 struct pnfs_layout_segment *lseg,
1309 struct list_head *free_me)
1310{
1311 struct inode *inode = lo->plh_inode;
1312 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
1313
1314 if (ld->add_lseg != NULL)
1315 ld->add_lseg(lo, lseg, free_me);
1316 else
1317 pnfs_generic_layout_insert_lseg(lo, lseg,
1318 pnfs_lseg_range_is_after,
1319 pnfs_lseg_no_merge,
1320 free_me);
1321}
Benny Halevye5e94012010-10-20 00:18:01 -04001322
1323static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -04001324alloc_init_layout_hdr(struct inode *ino,
1325 struct nfs_open_context *ctx,
1326 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001327{
1328 struct pnfs_layout_hdr *lo;
1329
Benny Halevy636fb9c2011-05-22 19:51:33 +03001330 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -04001331 if (!lo)
1332 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +00001333 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001334 INIT_LIST_HEAD(&lo->plh_layouts);
1335 INIT_LIST_HEAD(&lo->plh_segs);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -05001336 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001337 lo->plh_inode = ino;
Trond Myklebust5cc22162013-05-21 09:26:49 -04001338 lo->plh_lc_cred = get_rpccred(ctx->cred);
Trond Myklebust67a3b722016-06-17 16:48:19 -04001339 lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
Benny Halevye5e94012010-10-20 00:18:01 -04001340 return lo;
1341}
1342
1343static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -04001344pnfs_find_alloc_layout(struct inode *ino,
1345 struct nfs_open_context *ctx,
1346 gfp_t gfp_flags)
Trond Myklebuste5241e42016-06-17 16:48:20 -04001347 __releases(&ino->i_lock)
1348 __acquires(&ino->i_lock)
Benny Halevye5e94012010-10-20 00:18:01 -04001349{
1350 struct nfs_inode *nfsi = NFS_I(ino);
1351 struct pnfs_layout_hdr *new = NULL;
1352
1353 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1354
Trond Myklebust251ec412012-10-02 15:41:05 -07001355 if (nfsi->layout != NULL)
1356 goto out_existing;
Benny Halevye5e94012010-10-20 00:18:01 -04001357 spin_unlock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001358 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -04001359 spin_lock(&ino->i_lock);
1360
Trond Myklebust251ec412012-10-02 15:41:05 -07001361 if (likely(nfsi->layout == NULL)) { /* Won the race? */
Benny Halevye5e94012010-10-20 00:18:01 -04001362 nfsi->layout = new;
Trond Myklebust251ec412012-10-02 15:41:05 -07001363 return new;
Yanchuan Nian7175fe92012-10-31 16:05:48 +08001364 } else if (new != NULL)
1365 pnfs_free_layout_hdr(new);
Trond Myklebust251ec412012-10-02 15:41:05 -07001366out_existing:
1367 pnfs_get_layout_hdr(nfsi->layout);
Benny Halevye5e94012010-10-20 00:18:01 -04001368 return nfsi->layout;
1369}
1370
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001371/*
1372 * iomode matching rules:
Tom Haynesc7d73af2016-05-25 07:31:14 -07001373 * iomode lseg strict match
1374 * iomode
1375 * ----- ----- ------ -----
1376 * ANY READ N/A true
1377 * ANY RW N/A true
1378 * RW READ N/A false
1379 * RW RW N/A true
1380 * READ READ N/A true
1381 * READ RW true false
1382 * READ RW false true
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001383 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001384static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001385pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001386 const struct pnfs_layout_range *range,
1387 bool strict_iomode)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001388{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001389 struct pnfs_layout_range range1;
1390
1391 if ((range->iomode == IOMODE_RW &&
1392 ls_range->iomode != IOMODE_RW) ||
Tom Haynesc7d73af2016-05-25 07:31:14 -07001393 (range->iomode != ls_range->iomode &&
1394 strict_iomode == true) ||
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001395 !pnfs_lseg_range_intersecting(ls_range, range))
Benny Halevyfb3296e2011-05-22 19:47:26 +03001396 return 0;
1397
1398 /* range1 covers only the first byte in the range */
1399 range1 = *range;
1400 range1.length = 1;
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001401 return pnfs_lseg_range_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001402}
1403
1404/*
1405 * lookup range in layout
1406 */
Benny Halevye5e94012010-10-20 00:18:01 -04001407static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +03001408pnfs_find_lseg(struct pnfs_layout_hdr *lo,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001409 struct pnfs_layout_range *range,
1410 bool strict_iomode)
Benny Halevye5e94012010-10-20 00:18:01 -04001411{
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001412 struct pnfs_layout_segment *lseg, *ret = NULL;
1413
1414 dprintk("%s:Begin\n", __func__);
1415
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001416 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +00001417 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Peng Taoce6ab4f2014-09-06 00:53:24 +08001418 !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
Tom Haynesc7d73af2016-05-25 07:31:14 -07001419 pnfs_lseg_range_match(&lseg->pls_range, range,
1420 strict_iomode)) {
Trond Myklebust9369a432012-09-18 20:57:08 -04001421 ret = pnfs_get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001422 break;
1423 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001424 }
1425
1426 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +00001427 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001428 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -04001429}
1430
1431/*
Andy Adamsond23d61c2012-05-23 05:02:37 -04001432 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1433 * to the MDS or over pNFS
1434 *
1435 * The nfs_inode read_io and write_io fields are cumulative counters reset
1436 * when there are no layout segments. Note that in pnfs_update_layout iomode
1437 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1438 * WRITE request.
1439 *
1440 * A return of true means use MDS I/O.
1441 *
1442 * From rfc 5661:
1443 * If a file's size is smaller than the file size threshold, data accesses
1444 * SHOULD be sent to the metadata server. If an I/O request has a length that
1445 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1446 * server. If both file size and I/O size are provided, the client SHOULD
1447 * reach or exceed both thresholds before sending its read or write
1448 * requests to the data server.
1449 */
1450static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1451 struct inode *ino, int iomode)
1452{
1453 struct nfs4_threshold *t = ctx->mdsthreshold;
1454 struct nfs_inode *nfsi = NFS_I(ino);
1455 loff_t fsize = i_size_read(ino);
1456 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1457
1458 if (t == NULL)
1459 return ret;
1460
1461 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1462 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1463
1464 switch (iomode) {
1465 case IOMODE_READ:
1466 if (t->bm & THRESHOLD_RD) {
1467 dprintk("%s fsize %llu\n", __func__, fsize);
1468 size_set = true;
1469 if (fsize < t->rd_sz)
1470 size = true;
1471 }
1472 if (t->bm & THRESHOLD_RD_IO) {
1473 dprintk("%s nfsi->read_io %llu\n", __func__,
1474 nfsi->read_io);
1475 io_set = true;
1476 if (nfsi->read_io < t->rd_io_sz)
1477 io = true;
1478 }
1479 break;
1480 case IOMODE_RW:
1481 if (t->bm & THRESHOLD_WR) {
1482 dprintk("%s fsize %llu\n", __func__, fsize);
1483 size_set = true;
1484 if (fsize < t->wr_sz)
1485 size = true;
1486 }
1487 if (t->bm & THRESHOLD_WR_IO) {
1488 dprintk("%s nfsi->write_io %llu\n", __func__,
1489 nfsi->write_io);
1490 io_set = true;
1491 if (nfsi->write_io < t->wr_io_sz)
1492 io = true;
1493 }
1494 break;
1495 }
1496 if (size_set && io_set) {
1497 if (size && io)
1498 ret = true;
1499 } else if (size || io)
1500 ret = true;
1501
1502 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1503 return ret;
1504}
1505
Peng Taoaa8a45e2014-12-01 08:22:23 +08001506static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1507{
1508 /*
1509 * send layoutcommit as it can hold up layoutreturn due to lseg
1510 * reference
1511 */
1512 pnfs_layoutcommit_inode(lo->plh_inode, false);
1513 return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
Trond Myklebust2e5b29f2015-12-14 16:25:11 -05001514 nfs_wait_bit_killable,
Peng Taoaa8a45e2014-12-01 08:22:23 +08001515 TASK_UNINTERRUPTIBLE);
1516}
1517
Tom Haynesd67ae822014-12-11 17:02:04 -05001518static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1519{
1520 unsigned long *bitlock = &lo->plh_flags;
1521
1522 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1523 smp_mb__after_atomic();
1524 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1525}
1526
Andy Adamsond23d61c2012-05-23 05:02:37 -04001527/*
Benny Halevye5e94012010-10-20 00:18:01 -04001528 * Layout segment is retreived from the server if not cached.
1529 * The appropriate layout segment is referenced and returned to the caller.
1530 */
Andy Adamson7c24d942011-06-13 18:22:38 -04001531struct pnfs_layout_segment *
Benny Halevye5e94012010-10-20 00:18:01 -04001532pnfs_update_layout(struct inode *ino,
1533 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +03001534 loff_t pos,
1535 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001536 enum pnfs_iomode iomode,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001537 bool strict_iomode,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001538 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001539{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001540 struct pnfs_layout_range arg = {
1541 .iomode = iomode,
1542 .offset = pos,
1543 .length = count,
1544 };
Jeff Layton183d9e72016-05-17 12:28:47 -04001545 unsigned pg_offset, seq;
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001546 struct nfs_server *server = NFS_SERVER(ino);
1547 struct nfs_client *clp = server->nfs_client;
Jeff Layton183d9e72016-05-17 12:28:47 -04001548 struct pnfs_layout_hdr *lo = NULL;
Benny Halevye5e94012010-10-20 00:18:01 -04001549 struct pnfs_layout_segment *lseg = NULL;
Jeff Layton183d9e72016-05-17 12:28:47 -04001550 nfs4_stateid stateid;
1551 long timeout = 0;
Trond Myklebust66b53f32016-07-14 14:28:31 -04001552 unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
Weston Andros Adamson30005122013-02-28 20:30:10 -05001553 bool first;
Benny Halevye5e94012010-10-20 00:18:01 -04001554
Jeff Layton9a4bf312015-12-10 10:41:58 -05001555 if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001556 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001557 PNFS_UPDATE_LAYOUT_NO_PNFS);
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001558 goto out;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001559 }
Andy Adamsond23d61c2012-05-23 05:02:37 -04001560
Jeff Layton9a4bf312015-12-10 10:41:58 -05001561 if (iomode == IOMODE_READ && i_size_read(ino) == 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001562 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001563 PNFS_UPDATE_LAYOUT_RD_ZEROLEN);
Trond Myklebust4ae935602015-08-31 01:25:11 -07001564 goto out;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001565 }
Trond Myklebust4ae935602015-08-31 01:25:11 -07001566
Jeff Layton9a4bf312015-12-10 10:41:58 -05001567 if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001568 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001569 PNFS_UPDATE_LAYOUT_MDSTHRESH);
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001570 goto out;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001571 }
Andy Adamsond23d61c2012-05-23 05:02:37 -04001572
Peng Tao9bf87482014-08-22 17:37:41 +08001573lookup_again:
Trond Myklebustb88fa692016-08-23 11:19:33 -04001574 nfs4_client_recover_expired_lease(clp);
Peng Tao9bf87482014-08-22 17:37:41 +08001575 first = false;
Benny Halevye5e94012010-10-20 00:18:01 -04001576 spin_lock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001577 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001578 if (lo == NULL) {
1579 spin_unlock(&ino->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04001580 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001581 PNFS_UPDATE_LAYOUT_NOMEM);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001582 goto out;
1583 }
Benny Halevye5e94012010-10-20 00:18:01 -04001584
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001585 /* Do we even need to bother with this? */
Trond Myklebusta59c30a2012-03-01 11:17:47 -05001586 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001587 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001588 PNFS_UPDATE_LAYOUT_BULK_RECALL);
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001589 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001590 goto out_unlock;
1591 }
1592
1593 /* if LAYOUTGET already failed once we don't try again */
Trond Myklebust2e5b29f2015-12-14 16:25:11 -05001594 if (pnfs_layout_io_test_failed(lo, iomode)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001595 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001596 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
Benny Halevye5e94012010-10-20 00:18:01 -04001597 goto out_unlock;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001598 }
Benny Halevye5e94012010-10-20 00:18:01 -04001599
Tom Haynesc7d73af2016-05-25 07:31:14 -07001600 lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
Jeff Layton183d9e72016-05-17 12:28:47 -04001601 if (lseg) {
1602 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1603 PNFS_UPDATE_LAYOUT_FOUND_CACHED);
1604 goto out_unlock;
1605 }
1606
1607 if (!nfs4_valid_open_stateid(ctx->state)) {
1608 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1609 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
1610 goto out_unlock;
1611 }
1612
1613 /*
1614 * Choose a stateid for the LAYOUTGET. If we don't have a layout
1615 * stateid, or it has been invalidated, then we must use the open
1616 * stateid.
1617 */
Trond Myklebust67a3b722016-06-17 16:48:19 -04001618 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001619
1620 /*
1621 * The first layoutget for the file. Need to serialize per
Peng Tao9bf87482014-08-22 17:37:41 +08001622 * RFC 5661 Errata 3208.
1623 */
1624 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1625 &lo->plh_flags)) {
1626 spin_unlock(&ino->i_lock);
1627 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
1628 TASK_UNINTERRUPTIBLE);
1629 pnfs_put_layout_hdr(lo);
Jeff Layton183d9e72016-05-17 12:28:47 -04001630 dprintk("%s retrying\n", __func__);
Peng Tao9bf87482014-08-22 17:37:41 +08001631 goto lookup_again;
1632 }
Jeff Layton183d9e72016-05-17 12:28:47 -04001633
1634 first = true;
1635 do {
1636 seq = read_seqbegin(&ctx->state->seqlock);
1637 nfs4_stateid_copy(&stateid, &ctx->state->stateid);
1638 } while (read_seqretry(&ctx->state->seqlock, seq));
Peng Tao9bf87482014-08-22 17:37:41 +08001639 } else {
Jeff Layton183d9e72016-05-17 12:28:47 -04001640 nfs4_stateid_copy(&stateid, &lo->plh_stateid);
Peng Tao9bf87482014-08-22 17:37:41 +08001641 }
Andy Adamson568e8c42011-03-01 01:34:22 +00001642
Peng Taoaa8a45e2014-12-01 08:22:23 +08001643 /*
1644 * Because we free lsegs before sending LAYOUTRETURN, we need to wait
1645 * for LAYOUTRETURN even if first is true.
1646 */
Trond Myklebust8f70f532015-08-04 16:09:44 -04001647 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
Peng Taoaa8a45e2014-12-01 08:22:23 +08001648 spin_unlock(&ino->i_lock);
1649 dprintk("%s wait for layoutreturn\n", __func__);
1650 if (pnfs_prepare_to_retry_layoutget(lo)) {
Tom Haynesd67ae822014-12-11 17:02:04 -05001651 if (first)
1652 pnfs_clear_first_layoutget(lo);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001653 pnfs_put_layout_hdr(lo);
1654 dprintk("%s retrying\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04001655 trace_pnfs_update_layout(ino, pos, count, iomode, lo,
1656 lseg, PNFS_UPDATE_LAYOUT_RETRY);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001657 goto lookup_again;
1658 }
Jeff Layton183d9e72016-05-17 12:28:47 -04001659 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001660 PNFS_UPDATE_LAYOUT_RETURN);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001661 goto out_put_layout_hdr;
1662 }
1663
Jeff Layton9a4bf312015-12-10 10:41:58 -05001664 if (pnfs_layoutgets_blocked(lo)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001665 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001666 PNFS_UPDATE_LAYOUT_BLOCKED);
Fred Isamancf7d63f2011-01-06 11:36:25 +00001667 goto out_unlock;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001668 }
Fred Isamancf7d63f2011-01-06 11:36:25 +00001669 atomic_inc(&lo->plh_outstanding);
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001670 spin_unlock(&ino->i_lock);
Weston Andros Adamson30005122013-02-28 20:30:10 -05001671
Peng Taoabb9a002014-08-22 17:37:40 +08001672 if (list_empty(&lo->plh_layouts)) {
Fred Isaman2130ff62011-01-06 11:36:26 +00001673 /* The lo must be on the clp list if there is any
1674 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1675 */
1676 spin_lock(&clp->cl_lock);
Peng Taoabb9a002014-08-22 17:37:40 +08001677 if (list_empty(&lo->plh_layouts))
1678 list_add_tail(&lo->plh_layouts, &server->layouts);
Fred Isaman2130ff62011-01-06 11:36:26 +00001679 spin_unlock(&clp->cl_lock);
1680 }
Benny Halevye5e94012010-10-20 00:18:01 -04001681
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001682 pg_offset = arg.offset & ~PAGE_MASK;
Benny Halevy707ed5f2011-05-22 19:47:46 +03001683 if (pg_offset) {
1684 arg.offset -= pg_offset;
1685 arg.length += pg_offset;
1686 }
Andy Adamson7c24d942011-06-13 18:22:38 -04001687 if (arg.length != NFS4_MAX_UINT64)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001688 arg.length = PAGE_ALIGN(arg.length);
Benny Halevy707ed5f2011-05-22 19:47:46 +03001689
Jeff Layton183d9e72016-05-17 12:28:47 -04001690 lseg = send_layoutget(lo, ctx, &stateid, &arg, &timeout, gfp_flags);
1691 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1692 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
Trond Myklebust56b38a12016-07-14 18:34:12 -04001693 atomic_dec(&lo->plh_outstanding);
Jeff Layton83026d82016-05-17 12:28:46 -04001694 if (IS_ERR(lseg)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001695 switch(PTR_ERR(lseg)) {
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04001696 case -EBUSY:
Jeff Layton183d9e72016-05-17 12:28:47 -04001697 if (time_after(jiffies, giveup))
1698 lseg = NULL;
Trond Myklebust66b53f32016-07-14 14:28:31 -04001699 break;
1700 case -ERECALLCONFLICT:
1701 /* Huh? We hold no layouts, how is there a recall? */
1702 if (first) {
1703 lseg = NULL;
1704 break;
1705 }
1706 /* Destroy the existing layout and start over */
1707 if (time_after(jiffies, giveup))
1708 pnfs_destroy_layout(NFS_I(ino));
Jeff Layton183d9e72016-05-17 12:28:47 -04001709 /* Fallthrough */
1710 case -EAGAIN:
Trond Myklebust56b38a12016-07-14 18:34:12 -04001711 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04001712 default:
1713 if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
1714 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1715 lseg = NULL;
1716 }
Trond Myklebust56b38a12016-07-14 18:34:12 -04001717 goto out_put_layout_hdr;
1718 }
1719 if (lseg) {
1720 if (first)
1721 pnfs_clear_first_layoutget(lo);
1722 trace_pnfs_update_layout(ino, pos, count,
1723 iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
1724 pnfs_put_layout_hdr(lo);
1725 goto lookup_again;
Jeff Layton83026d82016-05-17 12:28:46 -04001726 }
1727 } else {
1728 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1729 }
1730
Trond Myklebust830ffb52012-09-20 21:25:19 -04001731out_put_layout_hdr:
Tom Haynesd67ae822014-12-11 17:02:04 -05001732 if (first)
1733 pnfs_clear_first_layoutget(lo);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001734 pnfs_put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -04001735out:
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001736 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1737 "(%s, offset: %llu, length: %llu)\n",
1738 __func__, ino->i_sb->s_id,
1739 (unsigned long long)NFS_FILEID(ino),
Peng Taod600ad12015-12-04 02:57:48 +08001740 IS_ERR_OR_NULL(lseg) ? "not found" : "found",
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001741 iomode==IOMODE_RW ? "read/write" : "read-only",
1742 (unsigned long long)pos,
1743 (unsigned long long)count);
Benny Halevye5e94012010-10-20 00:18:01 -04001744 return lseg;
1745out_unlock:
1746 spin_unlock(&ino->i_lock);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001747 goto out_put_layout_hdr;
Benny Halevye5e94012010-10-20 00:18:01 -04001748}
Andy Adamson7c24d942011-06-13 18:22:38 -04001749EXPORT_SYMBOL_GPL(pnfs_update_layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001750
Trond Myklebust540d9862015-08-25 11:16:13 -04001751static bool
1752pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
1753{
1754 switch (range->iomode) {
1755 case IOMODE_READ:
1756 case IOMODE_RW:
1757 break;
1758 default:
1759 return false;
1760 }
1761 if (range->offset == NFS4_MAX_UINT64)
1762 return false;
1763 if (range->length == 0)
1764 return false;
1765 if (range->length != NFS4_MAX_UINT64 &&
1766 range->length > NFS4_MAX_UINT64 - range->offset)
1767 return false;
1768 return true;
1769}
1770
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001771struct pnfs_layout_segment *
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001772pnfs_layout_process(struct nfs4_layoutget *lgp)
1773{
1774 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1775 struct nfs4_layoutget_res *res = &lgp->res;
1776 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001777 struct inode *ino = lo->plh_inode;
Trond Myklebust78096cc2014-02-12 10:02:27 -05001778 LIST_HEAD(free_me);
Trond Myklebust540d9862015-08-25 11:16:13 -04001779
1780 if (!pnfs_sanity_check_layout_range(&res->range))
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001781 return ERR_PTR(-EINVAL);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001782
1783 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001784 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001785 if (IS_ERR_OR_NULL(lseg)) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001786 if (!lseg)
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001787 lseg = ERR_PTR(-ENOMEM);
1788
1789 dprintk("%s: Could not allocate layout: error %ld\n",
1790 __func__, PTR_ERR(lseg));
1791 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001792 }
1793
Trond Myklebust119cef972016-07-24 15:10:12 -04001794 pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
Christoph Hellwig1013df612014-08-21 11:09:18 -05001795
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001796 spin_lock(&ino->i_lock);
Trond Myklebuste1c06f82015-08-04 16:40:08 -04001797 if (pnfs_layoutgets_blocked(lo)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001798 dprintk("%s forget reply due to state\n", __func__);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001799 goto out_forget;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001800 }
Trond Myklebust038d6492012-10-02 16:38:41 -07001801
Christoph Hellwig362f7472014-08-21 11:09:20 -05001802 if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
1803 /* existing state ID, make sure the sequence number matches. */
1804 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1805 dprintk("%s forget reply due to sequence\n", __func__);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001806 goto out_forget;
Christoph Hellwig362f7472014-08-21 11:09:20 -05001807 }
1808 pnfs_set_layout_stateid(lo, &res->stateid, false);
1809 } else {
1810 /*
1811 * We got an entirely new state ID. Mark all segments for the
1812 * inode invalid, and don't bother validating the stateid
1813 * sequence number.
1814 */
Trond Myklebustd9b61702016-07-24 15:04:07 -04001815 pnfs_mark_layout_stateid_invalid(lo, &free_me);
Christoph Hellwig362f7472014-08-21 11:09:20 -05001816
Trond Myklebust2a59a042016-09-03 11:20:04 -04001817 pnfs_set_layout_stateid(lo, &res->stateid, true);
Christoph Hellwig362f7472014-08-21 11:09:20 -05001818 }
Trond Myklebust038d6492012-10-02 16:38:41 -07001819
Trond Myklebust9369a432012-09-18 20:57:08 -04001820 pnfs_get_lseg(lseg);
Trond Myklebust03772d22015-08-25 08:54:17 -04001821 pnfs_layout_insert_lseg(lo, lseg, &free_me);
Trond Myklebust8e0acf92016-07-21 11:53:29 -04001822
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001823
Peng Tao39761432015-08-21 12:49:44 +08001824 if (res->return_on_close)
Fred Isamanf7e89172011-01-06 11:36:32 +00001825 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
Fred Isamanf7e89172011-01-06 11:36:32 +00001826
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001827 spin_unlock(&ino->i_lock);
Trond Myklebust78096cc2014-02-12 10:02:27 -05001828 pnfs_free_lseg_list(&free_me);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001829 return lseg;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001830
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001831out_forget:
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001832 spin_unlock(&ino->i_lock);
1833 lseg->pls_layout = lo;
1834 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001835 return ERR_PTR(-EAGAIN);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001836}
1837
Peng Tao016256d2014-09-06 00:53:23 +08001838static void
Jeff Layton3982a6a2016-05-17 12:28:41 -04001839pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
1840 u32 seq)
Trond Myklebust5c97f5d2016-01-04 11:53:04 -05001841{
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001842 if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
Trond Myklebust5c97f5d2016-01-04 11:53:04 -05001843 iomode = IOMODE_ANY;
1844 lo->plh_return_iomode = iomode;
Trond Myklebuste0fa0d02016-02-15 12:56:17 -05001845 set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001846 if (seq != 0) {
1847 WARN_ON_ONCE(lo->plh_return_seq != 0 && lo->plh_return_seq != seq);
Jeff Layton3982a6a2016-05-17 12:28:41 -04001848 lo->plh_return_seq = seq;
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001849 }
Trond Myklebust5c97f5d2016-01-04 11:53:04 -05001850}
1851
Trond Myklebust2f215962016-02-15 12:36:04 -05001852/**
1853 * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
1854 * @lo: pointer to layout header
1855 * @tmp_list: list header to be used with pnfs_free_lseg_list()
1856 * @return_range: describe layout segment ranges to be returned
1857 *
1858 * This function is mainly intended for use by layoutrecall. It attempts
1859 * to free the layout segment immediately, or else to mark it for return
1860 * as soon as its reference count drops to zero.
1861 */
Trond Myklebust10335552016-01-04 11:23:52 -05001862int
Peng Tao016256d2014-09-06 00:53:23 +08001863pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
1864 struct list_head *tmp_list,
Jeff Layton6d597e12016-05-17 12:28:42 -04001865 const struct pnfs_layout_range *return_range,
1866 u32 seq)
Peng Tao016256d2014-09-06 00:53:23 +08001867{
1868 struct pnfs_layout_segment *lseg, *next;
Trond Myklebust10335552016-01-04 11:23:52 -05001869 int remaining = 0;
Peng Tao016256d2014-09-06 00:53:23 +08001870
1871 dprintk("%s:Begin lo %p\n", __func__, lo);
1872
1873 if (list_empty(&lo->plh_segs))
Trond Myklebust10335552016-01-04 11:23:52 -05001874 return 0;
Peng Tao016256d2014-09-06 00:53:23 +08001875
Trond Myklebustfc7ff362015-12-28 10:28:59 -05001876 assert_spin_locked(&lo->plh_inode->i_lock);
Peng Tao016256d2014-09-06 00:53:23 +08001877
1878 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Trond Myklebuste036f462016-07-22 11:13:22 -04001879 if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
Peng Tao016256d2014-09-06 00:53:23 +08001880 dprintk("%s: marking lseg %p iomode %d "
1881 "offset %llu length %llu\n", __func__,
1882 lseg, lseg->pls_range.iomode,
1883 lseg->pls_range.offset,
1884 lseg->pls_range.length);
Trond Myklebust2f215962016-02-15 12:36:04 -05001885 if (mark_lseg_invalid(lseg, tmp_list))
1886 continue;
1887 remaining++;
Peng Tao016256d2014-09-06 00:53:23 +08001888 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
Peng Tao016256d2014-09-06 00:53:23 +08001889 }
Jeff Layton6d597e12016-05-17 12:28:42 -04001890
1891 if (remaining)
1892 pnfs_set_plh_return_info(lo, return_range->iomode, seq);
1893
Trond Myklebust10335552016-01-04 11:23:52 -05001894 return remaining;
Peng Tao016256d2014-09-06 00:53:23 +08001895}
1896
1897void pnfs_error_mark_layout_for_return(struct inode *inode,
1898 struct pnfs_layout_segment *lseg)
1899{
1900 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
Peng Tao016256d2014-09-06 00:53:23 +08001901 struct pnfs_layout_range range = {
1902 .iomode = lseg->pls_range.iomode,
1903 .offset = 0,
1904 .length = NFS4_MAX_UINT64,
1905 };
1906 LIST_HEAD(free_me);
Trond Myklebust10335552016-01-04 11:23:52 -05001907 bool return_now = false;
Peng Tao016256d2014-09-06 00:53:23 +08001908
1909 spin_lock(&inode->i_lock);
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001910 pnfs_set_plh_return_info(lo, range.iomode, 0);
Peng Tao016256d2014-09-06 00:53:23 +08001911 /*
1912 * mark all matching lsegs so that we are sure to have no live
1913 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
1914 * for how it works.
1915 */
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001916 if (!pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0)) {
Trond Myklebust10335552016-01-04 11:23:52 -05001917 nfs4_stateid stateid;
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001918 enum pnfs_iomode iomode;
Trond Myklebust10335552016-01-04 11:23:52 -05001919
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001920 return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
Trond Myklebust10335552016-01-04 11:23:52 -05001921 spin_unlock(&inode->i_lock);
1922 if (return_now)
1923 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1924 } else {
1925 spin_unlock(&inode->i_lock);
1926 nfs_commit_inode(inode, 0);
1927 }
Peng Tao016256d2014-09-06 00:53:23 +08001928 pnfs_free_lseg_list(&free_me);
1929}
1930EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
1931
Trond Myklebustd8007d42011-06-10 13:30:23 -04001932void
1933pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1934{
Peng Tao1fd937b2012-09-25 14:55:57 +08001935 u64 rd_size = req->wb_bytes;
1936
Peng Taocb5d04b2015-01-24 22:14:52 +08001937 if (pgio->pg_lseg == NULL) {
1938 if (pgio->pg_dreq == NULL)
1939 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
1940 else
1941 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
Trond Myklebustd8007d42011-06-10 13:30:23 -04001942
Peng Taocb5d04b2015-01-24 22:14:52 +08001943 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1944 req->wb_context,
1945 req_offset(req),
1946 rd_size,
1947 IOMODE_READ,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001948 false,
Peng Taocb5d04b2015-01-24 22:14:52 +08001949 GFP_KERNEL);
Peng Taod600ad12015-12-04 02:57:48 +08001950 if (IS_ERR(pgio->pg_lseg)) {
1951 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
1952 pgio->pg_lseg = NULL;
1953 return;
1954 }
Peng Taocb5d04b2015-01-24 22:14:52 +08001955 }
Trond Myklebuste885de12011-06-10 13:30:23 -04001956 /* If no lseg, fall back to read through mds */
1957 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001958 nfs_pageio_reset_read_mds(pgio);
Trond Myklebuste885de12011-06-10 13:30:23 -04001959
Trond Myklebustd8007d42011-06-10 13:30:23 -04001960}
1961EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1962
1963void
Peng Tao62965562012-09-25 14:55:57 +08001964pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
1965 struct nfs_page *req, u64 wb_size)
Trond Myklebustd8007d42011-06-10 13:30:23 -04001966{
Peng Taod600ad12015-12-04 02:57:48 +08001967 if (pgio->pg_lseg == NULL) {
Peng Taocb5d04b2015-01-24 22:14:52 +08001968 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1969 req->wb_context,
1970 req_offset(req),
1971 wb_size,
1972 IOMODE_RW,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001973 false,
Peng Taocb5d04b2015-01-24 22:14:52 +08001974 GFP_NOFS);
Peng Taod600ad12015-12-04 02:57:48 +08001975 if (IS_ERR(pgio->pg_lseg)) {
1976 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
1977 pgio->pg_lseg = NULL;
1978 return;
1979 }
1980 }
Trond Myklebuste885de12011-06-10 13:30:23 -04001981 /* If no lseg, fall back to write through mds */
1982 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001983 nfs_pageio_reset_write_mds(pgio);
Trond Myklebustd8007d42011-06-10 13:30:23 -04001984}
1985EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1986
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04001987void
1988pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
1989{
1990 if (desc->pg_lseg) {
1991 pnfs_put_lseg(desc->pg_lseg);
1992 desc->pg_lseg = NULL;
1993 }
1994}
1995EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
1996
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -04001997/*
1998 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
1999 * of bytes (maximum @req->wb_bytes) that can be coalesced.
2000 */
2001size_t
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002002pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
2003 struct nfs_page *prev, struct nfs_page *req)
Fred Isamanbae724e2011-03-01 01:34:15 +00002004{
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04002005 unsigned int size;
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002006 u64 seg_end, req_start, seg_left;
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04002007
2008 size = nfs_generic_pg_test(pgio, prev, req);
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04002009 if (!size)
2010 return 0;
Benny Halevy89a58e32011-05-25 20:54:40 +03002011
Trond Myklebust19982ba2011-06-10 13:30:23 -04002012 /*
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002013 * 'size' contains the number of bytes left in the current page (up
2014 * to the original size asked for in @req->wb_bytes).
2015 *
2016 * Calculate how many bytes are left in the layout segment
2017 * and if there are less bytes than 'size', return that instead.
Trond Myklebust19982ba2011-06-10 13:30:23 -04002018 *
2019 * Please also note that 'end_offset' is actually the offset of the
2020 * first byte that lies outside the pnfs_layout_range. FIXME?
2021 *
2022 */
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002023 if (pgio->pg_lseg) {
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002024 seg_end = end_offset(pgio->pg_lseg->pls_range.offset,
2025 pgio->pg_lseg->pls_range.length);
2026 req_start = req_offset(req);
Weston Andros Adamson7c137892015-01-30 11:01:02 -05002027 WARN_ON_ONCE(req_start >= seg_end);
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002028 /* start of request is past the last byte of this segment */
Weston Andros Adamson7c137892015-01-30 11:01:02 -05002029 if (req_start >= seg_end) {
2030 /* reference the new lseg */
2031 if (pgio->pg_ops->pg_cleanup)
2032 pgio->pg_ops->pg_cleanup(pgio);
2033 if (pgio->pg_ops->pg_init)
2034 pgio->pg_ops->pg_init(pgio, req);
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002035 return 0;
Weston Andros Adamson7c137892015-01-30 11:01:02 -05002036 }
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002037
2038 /* adjust 'size' iff there are fewer bytes left in the
2039 * segment than what nfs_generic_pg_test returned */
2040 seg_left = seg_end - req_start;
2041 if (seg_left < size)
2042 size = (unsigned int)seg_left;
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002043 }
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04002044
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002045 return size;
Fred Isamanbae724e2011-03-01 01:34:15 +00002046}
Benny Halevy89a58e32011-05-25 20:54:40 +03002047EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
Fred Isamanbae724e2011-03-01 01:34:15 +00002048
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002049int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002050{
2051 struct nfs_pageio_descriptor pgio;
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002052
2053 /* Resend all requests through the MDS */
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002054 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
2055 hdr->completion_ops);
Trond Myklebustc7070112015-06-17 19:56:22 -04002056 set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags);
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002057 return nfs_pageio_resend(&pgio, hdr);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002058}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04002059EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002060
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002061static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002062{
Fred Isamancd841602012-04-20 14:47:44 -04002063
2064 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
2065 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002066 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04002067 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002068 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04002069 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002070 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002071}
2072
Benny Halevyd20581a2011-05-22 19:52:03 +03002073/*
2074 * Called by non rpc-based layout drivers
2075 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002076void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
Fred Isaman94ad1c82011-03-01 01:34:14 +00002077{
Kinglong Meef8417b482015-10-16 17:23:29 +08002078 if (likely(!hdr->pnfs_error)) {
Trond Myklebust67af7612015-03-25 20:40:38 -04002079 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
2080 hdr->mds_offset + hdr->res.count);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002081 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
Kinglong Meef8417b482015-10-16 17:23:29 +08002082 }
2083 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
2084 if (unlikely(hdr->pnfs_error))
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002085 pnfs_ld_handle_write_error(hdr);
2086 hdr->mds_ops->rpc_release(hdr);
Fred Isaman44b83792011-03-03 15:13:44 +00002087}
Benny Halevyd20581a2011-05-22 19:52:03 +03002088EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
Fred Isaman44b83792011-03-03 15:13:44 +00002089
Trond Myklebustdce81292011-07-13 15:59:19 -04002090static void
2091pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002092 struct nfs_pgio_header *hdr)
Trond Myklebustdce81292011-07-13 15:59:19 -04002093{
Peng Tao48d635f2014-11-10 08:35:35 +08002094 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002095
Fred Isaman6c75dc02012-04-20 14:47:47 -04002096 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002097 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
Fred Isaman6c75dc02012-04-20 14:47:47 -04002098 nfs_pageio_reset_write_mds(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002099 mirror->pg_recoalesce = 1;
Fred Isaman6c75dc02012-04-20 14:47:47 -04002100 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002101 nfs_pgio_data_destroy(hdr);
Trond Myklebust1ca018d2015-06-17 19:41:51 -04002102 hdr->release(hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04002103}
2104
2105static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002106pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
Trond Myklebustdce81292011-07-13 15:59:19 -04002107 const struct rpc_call_ops *call_ops,
2108 struct pnfs_layout_segment *lseg,
2109 int how)
Andy Adamson0382b742011-03-03 15:13:45 +00002110{
Fred Isamancd841602012-04-20 14:47:44 -04002111 struct inode *inode = hdr->inode;
Andy Adamson0382b742011-03-03 15:13:45 +00002112 enum pnfs_try_status trypnfs;
2113 struct nfs_server *nfss = NFS_SERVER(inode);
2114
Fred Isamancd841602012-04-20 14:47:44 -04002115 hdr->mds_ops = call_ops;
Andy Adamson0382b742011-03-03 15:13:45 +00002116
2117 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002118 inode->i_ino, hdr->args.count, hdr->args.offset, how);
2119 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
Fred Isaman6c75dc02012-04-20 14:47:47 -04002120 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson0382b742011-03-03 15:13:45 +00002121 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
Andy Adamson0382b742011-03-03 15:13:45 +00002122 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2123 return trypnfs;
2124}
2125
Trond Myklebustdce81292011-07-13 15:59:19 -04002126static void
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002127pnfs_do_write(struct nfs_pageio_descriptor *desc,
2128 struct nfs_pgio_header *hdr, int how)
Trond Myklebustdce81292011-07-13 15:59:19 -04002129{
Trond Myklebustdce81292011-07-13 15:59:19 -04002130 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2131 struct pnfs_layout_segment *lseg = desc->pg_lseg;
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002132 enum pnfs_try_status trypnfs;
Trond Myklebustdce81292011-07-13 15:59:19 -04002133
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002134 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002135 if (trypnfs == PNFS_NOT_ATTEMPTED)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002136 pnfs_write_through_mds(desc, hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04002137}
2138
Fred Isaman6c75dc02012-04-20 14:47:47 -04002139static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
2140{
Trond Myklebust9369a432012-09-18 20:57:08 -04002141 pnfs_put_lseg(hdr->lseg);
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002142 nfs_pgio_header_free(hdr);
Fred Isaman6c75dc02012-04-20 14:47:47 -04002143}
2144
Trond Myklebustdce81292011-07-13 15:59:19 -04002145int
2146pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
2147{
Fred Isaman6c75dc02012-04-20 14:47:47 -04002148 struct nfs_pgio_header *hdr;
Trond Myklebustdce81292011-07-13 15:59:19 -04002149 int ret;
2150
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002151 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2152 if (!hdr) {
Peng Tao2bff2282015-12-05 02:03:17 +08002153 desc->pg_error = -ENOMEM;
2154 return desc->pg_error;
Fred Isaman6c75dc02012-04-20 14:47:47 -04002155 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04002156 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002157
Trond Myklebust9369a432012-09-18 20:57:08 -04002158 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Anna Schumakeref2c4882014-05-06 09:12:36 -04002159 ret = nfs_generic_pgio(desc, hdr);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002160 if (!ret)
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002161 pnfs_do_write(desc, hdr, desc->pg_ioflags);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002162
Fred Isaman6c75dc02012-04-20 14:47:47 -04002163 return ret;
Trond Myklebustdce81292011-07-13 15:59:19 -04002164}
2165EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
2166
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002167int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
Trond Myklebust62e4a762011-11-10 14:30:37 -05002168{
2169 struct nfs_pageio_descriptor pgio;
2170
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002171 /* Resend all requests through the MDS */
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002172 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
2173 return nfs_pageio_resend(&pgio, hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002174}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04002175EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002176
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002177static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002178{
Fred Isamancd841602012-04-20 14:47:44 -04002179 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
2180 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002181 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04002182 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002183 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002184 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002185 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
Trond Myklebust62e4a762011-11-10 14:30:37 -05002186}
2187
Andy Adamsonb1f69b72010-10-20 00:18:03 -04002188/*
Benny Halevyd20581a2011-05-22 19:52:03 +03002189 * Called by non rpc-based layout drivers
2190 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002191void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03002192{
Fred Isamancd841602012-04-20 14:47:44 -04002193 if (likely(!hdr->pnfs_error)) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002194 __nfs4_read_done_cb(hdr);
2195 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
Kinglong Meef8417b482015-10-16 17:23:29 +08002196 }
2197 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
2198 if (unlikely(hdr->pnfs_error))
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002199 pnfs_ld_handle_read_error(hdr);
2200 hdr->mds_ops->rpc_release(hdr);
Benny Halevyd20581a2011-05-22 19:52:03 +03002201}
2202EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
2203
Trond Myklebust493292d2011-07-13 15:58:28 -04002204static void
2205pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002206 struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -04002207{
Peng Tao48d635f2014-11-10 08:35:35 +08002208 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002209
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002210 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002211 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002212 nfs_pageio_reset_read_mds(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002213 mirror->pg_recoalesce = 1;
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002214 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002215 nfs_pgio_data_destroy(hdr);
Trond Myklebust1ca018d2015-06-17 19:41:51 -04002216 hdr->release(hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -04002217}
2218
Benny Halevyd20581a2011-05-22 19:52:03 +03002219/*
Andy Adamson64419a92011-03-01 01:34:16 +00002220 * Call the appropriate parallel I/O subsystem read function.
2221 */
Trond Myklebust493292d2011-07-13 15:58:28 -04002222static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002223pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
Trond Myklebust493292d2011-07-13 15:58:28 -04002224 const struct rpc_call_ops *call_ops,
2225 struct pnfs_layout_segment *lseg)
Andy Adamson64419a92011-03-01 01:34:16 +00002226{
Fred Isamancd841602012-04-20 14:47:44 -04002227 struct inode *inode = hdr->inode;
Andy Adamson64419a92011-03-01 01:34:16 +00002228 struct nfs_server *nfss = NFS_SERVER(inode);
2229 enum pnfs_try_status trypnfs;
2230
Fred Isamancd841602012-04-20 14:47:44 -04002231 hdr->mds_ops = call_ops;
Andy Adamson64419a92011-03-01 01:34:16 +00002232
2233 dprintk("%s: Reading ino:%lu %u@%llu\n",
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002234 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
Andy Adamson64419a92011-03-01 01:34:16 +00002235
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002236 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002237 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson64419a92011-03-01 01:34:16 +00002238 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
Andy Adamson64419a92011-03-01 01:34:16 +00002239 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2240 return trypnfs;
2241}
Andy Adamson863a3c62011-03-23 13:27:54 +00002242
Peng Taoceb11e12014-11-10 08:35:38 +08002243/* Resend all requests through pnfs. */
Weston Andros Adamson1b1bc662016-04-01 11:42:28 -04002244void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
Peng Taoceb11e12014-11-10 08:35:38 +08002245{
2246 struct nfs_pageio_descriptor pgio;
2247
Weston Andros Adamson1b1bc662016-04-01 11:42:28 -04002248 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2249 nfs_pageio_init_read(&pgio, hdr->inode, false,
2250 hdr->completion_ops);
2251 hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
2252 }
Peng Taoceb11e12014-11-10 08:35:38 +08002253}
2254EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
2255
Trond Myklebust493292d2011-07-13 15:58:28 -04002256static void
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002257pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -04002258{
Trond Myklebust493292d2011-07-13 15:58:28 -04002259 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2260 struct pnfs_layout_segment *lseg = desc->pg_lseg;
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002261 enum pnfs_try_status trypnfs;
Trond Myklebust493292d2011-07-13 15:58:28 -04002262
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002263 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
Peng Taoceb11e12014-11-10 08:35:38 +08002264 if (trypnfs == PNFS_TRY_AGAIN)
Weston Andros Adamson1b1bc662016-04-01 11:42:28 -04002265 pnfs_read_resend_pnfs(hdr);
2266 if (trypnfs == PNFS_NOT_ATTEMPTED || hdr->task.tk_status)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002267 pnfs_read_through_mds(desc, hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -04002268}
2269
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002270static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
2271{
Trond Myklebust9369a432012-09-18 20:57:08 -04002272 pnfs_put_lseg(hdr->lseg);
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002273 nfs_pgio_header_free(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002274}
2275
Trond Myklebust493292d2011-07-13 15:58:28 -04002276int
2277pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2278{
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002279 struct nfs_pgio_header *hdr;
Trond Myklebust493292d2011-07-13 15:58:28 -04002280 int ret;
2281
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002282 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2283 if (!hdr) {
Peng Tao2bff2282015-12-05 02:03:17 +08002284 desc->pg_error = -ENOMEM;
2285 return desc->pg_error;
Trond Myklebust493292d2011-07-13 15:58:28 -04002286 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002287 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
Trond Myklebust9369a432012-09-18 20:57:08 -04002288 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Anna Schumakeref2c4882014-05-06 09:12:36 -04002289 ret = nfs_generic_pgio(desc, hdr);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002290 if (!ret)
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002291 pnfs_do_read(desc, hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002292 return ret;
Trond Myklebust493292d2011-07-13 15:58:28 -04002293}
2294EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
2295
Trond Myklebust71244d92014-01-13 13:34:36 -05002296static void pnfs_clear_layoutcommitting(struct inode *inode)
2297{
2298 unsigned long *bitlock = &NFS_I(inode)->flags;
2299
2300 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002301 smp_mb__after_atomic();
Trond Myklebust71244d92014-01-13 13:34:36 -05002302 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2303}
2304
Andy Adamson863a3c62011-03-23 13:27:54 +00002305/*
Peng Taoa9bae562011-07-30 20:52:33 -04002306 * There can be multiple RW segments.
Andy Adamson863a3c62011-03-23 13:27:54 +00002307 */
Peng Taoa9bae562011-07-30 20:52:33 -04002308static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
Andy Adamson863a3c62011-03-23 13:27:54 +00002309{
Peng Taoa9bae562011-07-30 20:52:33 -04002310 struct pnfs_layout_segment *lseg;
Andy Adamson863a3c62011-03-23 13:27:54 +00002311
Peng Taoa9bae562011-07-30 20:52:33 -04002312 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2313 if (lseg->pls_range.iomode == IOMODE_RW &&
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002314 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
Peng Taoa9bae562011-07-30 20:52:33 -04002315 list_add(&lseg->pls_lc_list, listp);
2316 }
Andy Adamson863a3c62011-03-23 13:27:54 +00002317}
2318
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002319static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2320{
2321 struct pnfs_layout_segment *lseg, *tmp;
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002322
2323 /* Matched by references in pnfs_set_layoutcommit */
2324 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2325 list_del_init(&lseg->pls_lc_list);
2326 pnfs_put_lseg(lseg);
2327 }
2328
Trond Myklebust71244d92014-01-13 13:34:36 -05002329 pnfs_clear_layoutcommitting(inode);
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002330}
2331
Peng Tao1b0ae062011-09-22 21:50:12 -04002332void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2333{
Trond Myklebustb9e028f2012-09-18 16:41:18 -04002334 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
Peng Tao1b0ae062011-09-22 21:50:12 -04002335}
2336EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2337
Andy Adamson863a3c62011-03-23 13:27:54 +00002338void
Trond Myklebust67af7612015-03-25 20:40:38 -04002339pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
2340 loff_t end_pos)
Andy Adamson863a3c62011-03-23 13:27:54 +00002341{
Fred Isamancd841602012-04-20 14:47:44 -04002342 struct nfs_inode *nfsi = NFS_I(inode);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002343 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00002344
Fred Isamancd841602012-04-20 14:47:44 -04002345 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00002346 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
Trond Myklebust29559b12015-03-25 20:10:20 -04002347 nfsi->layout->plh_lwb = end_pos;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002348 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00002349 dprintk("%s: Set layoutcommit for inode %lu ",
Fred Isamancd841602012-04-20 14:47:44 -04002350 __func__, inode->i_ino);
Trond Myklebust29559b12015-03-25 20:10:20 -04002351 } else if (end_pos > nfsi->layout->plh_lwb)
2352 nfsi->layout->plh_lwb = end_pos;
Trond Myklebust67af7612015-03-25 20:40:38 -04002353 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
Peng Taoa9bae562011-07-30 20:52:33 -04002354 /* references matched in nfs4_layoutcommit_release */
Trond Myklebust67af7612015-03-25 20:40:38 -04002355 pnfs_get_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04002356 }
Fred Isamancd841602012-04-20 14:47:44 -04002357 spin_unlock(&inode->i_lock);
Peng Taoacff58802011-07-30 20:52:31 -04002358 dprintk("%s: lseg %p end_pos %llu\n",
Trond Myklebust67af7612015-03-25 20:40:38 -04002359 __func__, lseg, nfsi->layout->plh_lwb);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002360
2361 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2362 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2363 if (mark_as_dirty)
Fred Isamancd841602012-04-20 14:47:44 -04002364 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002365}
2366EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2367
Andy Adamsondb29c082011-07-30 20:52:38 -04002368void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2369{
2370 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2371
2372 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2373 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002374 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
Andy Adamsondb29c082011-07-30 20:52:38 -04002375}
2376
Andy Adamsonde4b15c2011-03-12 02:58:09 -05002377/*
2378 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2379 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2380 * data to disk to allow the server to recover the data if it crashes.
2381 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2382 * is off, and a COMMIT is sent to a data server, or
2383 * if WRITEs to a data server return NFS_DATA_SYNC.
2384 */
Andy Adamson863a3c62011-03-23 13:27:54 +00002385int
Andy Adamsonef311532011-03-12 02:58:10 -05002386pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00002387{
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002388 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
Andy Adamson863a3c62011-03-23 13:27:54 +00002389 struct nfs4_layoutcommit_data *data;
2390 struct nfs_inode *nfsi = NFS_I(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002391 loff_t end_pos;
Trond Myklebust71244d92014-01-13 13:34:36 -05002392 int status;
2393
2394 if (!pnfs_layoutcommit_outstanding(inode))
2395 return 0;
Andy Adamson863a3c62011-03-23 13:27:54 +00002396
2397 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
2398
Trond Myklebust71244d92014-01-13 13:34:36 -05002399 status = -EAGAIN;
2400 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
2401 if (!sync)
2402 goto out;
NeilBrown74316202014-07-07 15:16:04 +10002403 status = wait_on_bit_lock_action(&nfsi->flags,
Trond Myklebust71244d92014-01-13 13:34:36 -05002404 NFS_INO_LAYOUTCOMMITTING,
2405 nfs_wait_bit_killable,
2406 TASK_KILLABLE);
2407 if (status)
2408 goto out;
2409 }
Andy Adamsonde4b15c2011-03-12 02:58:09 -05002410
Trond Myklebust71244d92014-01-13 13:34:36 -05002411 status = -ENOMEM;
Andy Adamson863a3c62011-03-23 13:27:54 +00002412 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2413 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebust71244d92014-01-13 13:34:36 -05002414 if (!data)
2415 goto clear_layoutcommitting;
Andy Adamson863a3c62011-03-23 13:27:54 +00002416
Trond Myklebust71244d92014-01-13 13:34:36 -05002417 status = 0;
2418 spin_lock(&inode->i_lock);
2419 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2420 goto out_unlock;
Peng Tao92407e72011-10-23 20:21:17 -07002421
Peng Taoa9bae562011-07-30 20:52:33 -04002422 INIT_LIST_HEAD(&data->lseg_list);
Peng Taoa9bae562011-07-30 20:52:33 -04002423 pnfs_list_write_lseg(inode, &data->lseg_list);
Andy Adamson863a3c62011-03-23 13:27:54 +00002424
Peng Taoacff58802011-07-30 20:52:31 -04002425 end_pos = nfsi->layout->plh_lwb;
Andy Adamson863a3c62011-03-23 13:27:54 +00002426
Trond Myklebustf597c532012-03-04 18:13:56 -05002427 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
Andy Adamson863a3c62011-03-23 13:27:54 +00002428 spin_unlock(&inode->i_lock);
2429
2430 data->args.inode = inode;
Peng Tao9fa40752011-07-30 20:52:32 -04002431 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
Andy Adamson863a3c62011-03-23 13:27:54 +00002432 nfs_fattr_init(&data->fattr);
2433 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2434 data->res.fattr = &data->fattr;
Trond Myklebust2e18d4d2016-06-26 18:54:58 -04002435 if (end_pos != 0)
2436 data->args.lastbytewritten = end_pos - 1;
2437 else
2438 data->args.lastbytewritten = U64_MAX;
Andy Adamson863a3c62011-03-23 13:27:54 +00002439 data->res.server = NFS_SERVER(inode);
2440
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002441 if (ld->prepare_layoutcommit) {
2442 status = ld->prepare_layoutcommit(&data->args);
2443 if (status) {
Jeff Layton3471648a2015-07-10 15:58:42 -04002444 put_rpccred(data->cred);
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002445 spin_lock(&inode->i_lock);
Trond Myklebust29559b12015-03-25 20:10:20 -04002446 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2447 if (end_pos > nfsi->layout->plh_lwb)
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002448 nfsi->layout->plh_lwb = end_pos;
Jeff Layton3471648a2015-07-10 15:58:42 -04002449 goto out_unlock;
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002450 }
2451 }
2452
2453
Andy Adamson863a3c62011-03-23 13:27:54 +00002454 status = nfs4_proc_layoutcommit(data, sync);
2455out:
Peng Tao92407e72011-10-23 20:21:17 -07002456 if (status)
2457 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002458 dprintk("<-- %s status %d\n", __func__, status);
2459 return status;
Trond Myklebust71244d92014-01-13 13:34:36 -05002460out_unlock:
2461 spin_unlock(&inode->i_lock);
Peng Tao92407e72011-10-23 20:21:17 -07002462 kfree(data);
Trond Myklebust71244d92014-01-13 13:34:36 -05002463clear_layoutcommitting:
2464 pnfs_clear_layoutcommitting(inode);
Peng Tao92407e72011-10-23 20:21:17 -07002465 goto out;
Andy Adamson863a3c62011-03-23 13:27:54 +00002466}
Peng Tao72cff442014-08-07 10:12:38 +08002467EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
Andy Adamson82be4172012-05-23 05:02:35 -04002468
Trond Myklebust5bb89b42015-03-25 14:14:42 -04002469int
2470pnfs_generic_sync(struct inode *inode, bool datasync)
2471{
2472 return pnfs_layoutcommit_inode(inode, true);
2473}
2474EXPORT_SYMBOL_GPL(pnfs_generic_sync);
2475
Andy Adamson82be4172012-05-23 05:02:35 -04002476struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
2477{
2478 struct nfs4_threshold *thp;
2479
2480 thp = kzalloc(sizeof(*thp), GFP_NOFS);
2481 if (!thp) {
2482 dprintk("%s mdsthreshold allocation failed\n", __func__);
2483 return NULL;
2484 }
2485 return thp;
2486}
Peng Tao87334082015-06-23 19:51:57 +08002487
Peng Tao865a7ec2015-06-25 18:19:32 +08002488#if IS_ENABLED(CONFIG_NFS_V4_2)
Peng Tao87334082015-06-23 19:51:57 +08002489int
Trond Myklebustc8ad8892015-08-05 17:31:58 -04002490pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
Peng Tao87334082015-06-23 19:51:57 +08002491{
2492 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2493 struct nfs_server *server = NFS_SERVER(inode);
Peng Tao1bfe3b22015-06-23 19:52:03 +08002494 struct nfs_inode *nfsi = NFS_I(inode);
Peng Tao87334082015-06-23 19:51:57 +08002495 struct nfs42_layoutstat_data *data;
2496 struct pnfs_layout_hdr *hdr;
2497 int status = 0;
2498
2499 if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
2500 goto out;
2501
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04002502 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
2503 goto out;
2504
Peng Tao1bfe3b22015-06-23 19:52:03 +08002505 if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
2506 goto out;
2507
Peng Tao87334082015-06-23 19:51:57 +08002508 spin_lock(&inode->i_lock);
2509 if (!NFS_I(inode)->layout) {
2510 spin_unlock(&inode->i_lock);
Trond Myklebustf538d0b2016-05-16 14:41:14 -04002511 goto out_clear_layoutstats;
Peng Tao87334082015-06-23 19:51:57 +08002512 }
2513 hdr = NFS_I(inode)->layout;
2514 pnfs_get_layout_hdr(hdr);
2515 spin_unlock(&inode->i_lock);
2516
Trond Myklebustc8ad8892015-08-05 17:31:58 -04002517 data = kzalloc(sizeof(*data), gfp_flags);
Peng Tao87334082015-06-23 19:51:57 +08002518 if (!data) {
2519 status = -ENOMEM;
2520 goto out_put;
2521 }
2522
2523 data->args.fh = NFS_FH(inode);
2524 data->args.inode = inode;
Peng Tao87334082015-06-23 19:51:57 +08002525 status = ld->prepare_layoutstats(&data->args);
2526 if (status)
2527 goto out_free;
2528
2529 status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
2530
2531out:
2532 dprintk("%s returns %d\n", __func__, status);
2533 return status;
2534
2535out_free:
2536 kfree(data);
2537out_put:
2538 pnfs_put_layout_hdr(hdr);
Trond Myklebustf538d0b2016-05-16 14:41:14 -04002539out_clear_layoutstats:
Peng Tao1bfe3b22015-06-23 19:52:03 +08002540 smp_mb__before_atomic();
2541 clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
2542 smp_mb__after_atomic();
Peng Tao87334082015-06-23 19:51:57 +08002543 goto out;
2544}
2545EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
Peng Tao865a7ec2015-06-25 18:19:32 +08002546#endif
Trond Myklebustbbf58bf2015-08-24 20:39:18 -04002547
2548unsigned int layoutstats_timer;
2549module_param(layoutstats_timer, uint, 0644);
2550EXPORT_SYMBOL_GPL(layoutstats_timer);