blob: 56b2d96f9103e42c57e5dd490f2561c8a10c554a [file] [log] [blame]
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001/*
2 * pNFS functions to call and manage layout drivers.
3 *
4 * Copyright (c) 2002 [year of first publication]
5 * The Regents of the University of Michigan
6 * All Rights Reserved
7 *
8 * Dean Hildebrand <dhildebz@umich.edu>
9 *
10 * Permission is granted to use, copy, create derivative works, and
11 * redistribute this software and such derivative works for any purpose,
12 * so long as the name of the University of Michigan is not used in
13 * any advertising or publicity pertaining to the use or distribution
14 * of this software without specific, written prior authorization. If
15 * the above copyright notice or any other identification of the
16 * University of Michigan is included in any copy of any portion of
17 * this software, then the disclaimer below must also be included.
18 *
19 * This software is provided as is, without representation or warranty
20 * of any kind either express or implied, including without limitation
21 * the implied warranties of merchantability, fitness for a particular
22 * purpose, or noninfringement. The Regents of the University of
23 * Michigan shall not be liable for any damages, including special,
24 * indirect, incidental, or consequential damages, with respect to any
25 * claim arising out of or in connection with the use of the software,
26 * even if it has been or is hereafter advised of the possibility of
27 * such damages.
28 */
29
30#include <linux/nfs_fs.h>
Trond Myklebust493292d2011-07-13 15:58:28 -040031#include <linux/nfs_page.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040032#include <linux/module.h>
Jeff Laytonca440c32016-09-15 14:40:49 -040033#include <linux/sort.h>
Andy Adamson974cec82010-10-20 00:18:02 -040034#include "internal.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040035#include "pnfs.h"
Andy Adamson64419a92011-03-01 01:34:16 +000036#include "iostat.h"
Trond Myklebustcc668ab2013-08-14 15:31:28 -040037#include "nfs4trace.h"
Trond Myklebust40dd4b72015-01-24 13:54:37 -050038#include "delegation.h"
Peng Tao87334082015-06-23 19:51:57 +080039#include "nfs42.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040040
41#define NFSDBG_FACILITY NFSDBG_PNFS
Trond Myklebust25c75332012-09-18 17:01:12 -040042#define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040043
Fred Isaman02c35fc2010-10-20 00:17:59 -040044/* Locking:
45 *
46 * pnfs_spinlock:
47 * protects pnfs_modules_tbl.
48 */
49static DEFINE_SPINLOCK(pnfs_spinlock);
50
51/*
52 * pnfs_modules_tbl holds all pnfs modules
53 */
54static LIST_HEAD(pnfs_modules_tbl);
55
Trond Myklebust13c13a62016-01-26 23:12:11 -050056static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo);
Peng Taoaa1e0e3a2014-09-06 00:53:25 +080057
Fred Isaman02c35fc2010-10-20 00:17:59 -040058/* Return the registered pnfs layout driver module matching given id */
59static struct pnfs_layoutdriver_type *
60find_pnfs_driver_locked(u32 id)
61{
62 struct pnfs_layoutdriver_type *local;
63
64 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
65 if (local->id == id)
66 goto out;
67 local = NULL;
68out:
69 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
70 return local;
71}
72
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040073static struct pnfs_layoutdriver_type *
74find_pnfs_driver(u32 id)
75{
Fred Isaman02c35fc2010-10-20 00:17:59 -040076 struct pnfs_layoutdriver_type *local;
77
78 spin_lock(&pnfs_spinlock);
79 local = find_pnfs_driver_locked(id);
Trond Myklebust0a9c63f2012-06-15 13:02:58 -040080 if (local != NULL && !try_module_get(local->owner)) {
81 dprintk("%s: Could not grab reference on module\n", __func__);
82 local = NULL;
83 }
Fred Isaman02c35fc2010-10-20 00:17:59 -040084 spin_unlock(&pnfs_spinlock);
85 return local;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040086}
87
88void
89unset_pnfs_layoutdriver(struct nfs_server *nfss)
90{
Benny Halevy738fd0f32011-07-30 20:52:36 -040091 if (nfss->pnfs_curr_ld) {
92 if (nfss->pnfs_curr_ld->clear_layoutdriver)
93 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
Trond Myklebust2a4c8992012-06-14 13:08:38 -040094 /* Decrement the MDS count. Purge the deviceid cache if zero */
95 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
96 nfs4_deviceid_purge_client(nfss->nfs_client);
Fred Isaman02c35fc2010-10-20 00:17:59 -040097 module_put(nfss->pnfs_curr_ld->owner);
Benny Halevy738fd0f32011-07-30 20:52:36 -040098 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040099 nfss->pnfs_curr_ld = NULL;
100}
101
102/*
Jeff Laytonca440c32016-09-15 14:40:49 -0400103 * When the server sends a list of layout types, we choose one in the order
104 * given in the list below.
105 *
106 * FIXME: should this list be configurable in some fashion? module param?
107 * mount option? something else?
108 */
109static const u32 ld_prefs[] = {
110 LAYOUT_SCSI,
111 LAYOUT_BLOCK_VOLUME,
112 LAYOUT_OSD2_OBJECTS,
113 LAYOUT_FLEX_FILES,
114 LAYOUT_NFSV4_1_FILES,
115 0
116};
117
118static int
119ld_cmp(const void *e1, const void *e2)
120{
121 u32 ld1 = *((u32 *)e1);
122 u32 ld2 = *((u32 *)e2);
123 int i;
124
125 for (i = 0; ld_prefs[i] != 0; i++) {
126 if (ld1 == ld_prefs[i])
127 return -1;
128
129 if (ld2 == ld_prefs[i])
130 return 1;
131 }
132 return 0;
133}
134
135/*
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400136 * Try to set the server's pnfs module to the pnfs layout type specified by id.
137 * Currently only one pNFS layout driver per filesystem is supported.
138 *
Jeff Layton3132e492016-08-10 15:58:24 -0400139 * @ids array of layout types supported by MDS.
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400140 */
141void
Benny Halevy738fd0f32011-07-30 20:52:36 -0400142set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
Jeff Laytonca440c32016-09-15 14:40:49 -0400143 struct nfs_fsinfo *fsinfo)
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400144{
145 struct pnfs_layoutdriver_type *ld_type = NULL;
Jeff Layton3132e492016-08-10 15:58:24 -0400146 u32 id;
Jeff Laytonca440c32016-09-15 14:40:49 -0400147 int i;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400148
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400149 if (!(server->nfs_client->cl_exchange_flags &
150 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
Jeff Layton3132e492016-08-10 15:58:24 -0400151 printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n",
152 __func__, server->nfs_client->cl_exchange_flags);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400153 goto out_no_driver;
154 }
Jeff Layton3132e492016-08-10 15:58:24 -0400155
Jeff Laytonca440c32016-09-15 14:40:49 -0400156 sort(fsinfo->layouttype, fsinfo->nlayouttypes,
157 sizeof(*fsinfo->layouttype), ld_cmp, NULL);
Jeff Layton3132e492016-08-10 15:58:24 -0400158
Jeff Laytonca440c32016-09-15 14:40:49 -0400159 for (i = 0; i < fsinfo->nlayouttypes; i++) {
160 id = fsinfo->layouttype[i];
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400161 ld_type = find_pnfs_driver(id);
Jeff Laytonca440c32016-09-15 14:40:49 -0400162 if (!ld_type) {
163 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX,
164 id);
165 ld_type = find_pnfs_driver(id);
166 }
167 if (ld_type)
168 break;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400169 }
Jeff Layton3132e492016-08-10 15:58:24 -0400170
171 if (!ld_type) {
Jeff Laytonca440c32016-09-15 14:40:49 -0400172 dprintk("%s: No pNFS module found!\n", __func__);
Jeff Layton3132e492016-08-10 15:58:24 -0400173 goto out_no_driver;
174 }
175
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400176 server->pnfs_curr_ld = ld_type;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400177 if (ld_type->set_layoutdriver
178 && ld_type->set_layoutdriver(server, mntfh)) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500179 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
180 "driver %u.\n", __func__, id);
Benny Halevy738fd0f32011-07-30 20:52:36 -0400181 module_put(ld_type->owner);
182 goto out_no_driver;
183 }
Trond Myklebust2a4c8992012-06-14 13:08:38 -0400184 /* Bump the MDS count */
185 atomic_inc(&server->nfs_client->cl_mds_count);
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000186
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400187 dprintk("%s: pNFS module for %u set\n", __func__, id);
188 return;
189
190out_no_driver:
191 dprintk("%s: Using NFSv4 I/O\n", __func__);
192 server->pnfs_curr_ld = NULL;
193}
Fred Isaman02c35fc2010-10-20 00:17:59 -0400194
195int
196pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
197{
198 int status = -EINVAL;
199 struct pnfs_layoutdriver_type *tmp;
200
201 if (ld_type->id == 0) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500202 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
Fred Isaman02c35fc2010-10-20 00:17:59 -0400203 return status;
204 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400205 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500206 printk(KERN_ERR "NFS: %s Layout driver must provide "
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400207 "alloc_lseg and free_lseg.\n", __func__);
208 return status;
209 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400210
211 spin_lock(&pnfs_spinlock);
212 tmp = find_pnfs_driver_locked(ld_type->id);
213 if (!tmp) {
214 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
215 status = 0;
216 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
217 ld_type->name);
218 } else {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500219 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
Fred Isaman02c35fc2010-10-20 00:17:59 -0400220 __func__, ld_type->id);
221 }
222 spin_unlock(&pnfs_spinlock);
223
224 return status;
225}
226EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
227
228void
229pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
230{
231 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
232 spin_lock(&pnfs_spinlock);
233 list_del(&ld_type->pnfs_tblid);
234 spin_unlock(&pnfs_spinlock);
235}
236EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
Benny Halevye5e94012010-10-20 00:18:01 -0400237
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400238/*
239 * pNFS client layout cache
240 */
241
Fred Isamancc6e5342011-01-06 11:36:28 +0000242/* Need to hold i_lock if caller does not already hold reference */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000243void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400244pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
Benny Halevye5e94012010-10-20 00:18:01 -0400245{
Fred Isamancc6e5342011-01-06 11:36:28 +0000246 atomic_inc(&lo->plh_refcount);
247}
248
Benny Halevy636fb9c2011-05-22 19:51:33 +0300249static struct pnfs_layout_hdr *
250pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
251{
252 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
Trond Myklebust57934272012-09-20 20:37:23 -0400253 return ld->alloc_layout_hdr(ino, gfp_flags);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300254}
255
256static void
257pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
258{
Trond Myklebust9c626382012-09-20 17:31:43 -0400259 struct nfs_server *server = NFS_SERVER(lo->plh_inode);
260 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
261
262 if (!list_empty(&lo->plh_layouts)) {
263 struct nfs_client *clp = server->nfs_client;
264
265 spin_lock(&clp->cl_lock);
266 list_del_init(&lo->plh_layouts);
267 spin_unlock(&clp->cl_lock);
268 }
Peng Tao9fa40752011-07-30 20:52:32 -0400269 put_rpccred(lo->plh_lc_cred);
Trond Myklebust57934272012-09-20 20:37:23 -0400270 return ld->free_layout_hdr(lo);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300271}
272
Fred Isamancc6e5342011-01-06 11:36:28 +0000273static void
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400274pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
Fred Isamancc6e5342011-01-06 11:36:28 +0000275{
Trond Myklebustbb346f62012-09-20 15:52:13 -0400276 struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
Fred Isamancc6e5342011-01-06 11:36:28 +0000277 dprintk("%s: freeing layout cache %p\n", __func__, lo);
Trond Myklebustbb346f62012-09-20 15:52:13 -0400278 nfsi->layout = NULL;
279 /* Reset MDS Threshold I/O counters */
280 nfsi->write_io = 0;
281 nfsi->read_io = 0;
Benny Halevye5e94012010-10-20 00:18:01 -0400282}
283
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400284void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400285pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400286{
Fred Isamancc6e5342011-01-06 11:36:28 +0000287 struct inode *inode = lo->plh_inode;
288
Trond Myklebust13c13a62016-01-26 23:12:11 -0500289 pnfs_layoutreturn_before_put_layout_hdr(lo);
290
Fred Isamancc6e5342011-01-06 11:36:28 +0000291 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
Peng Tao566f8732014-10-10 23:25:46 +0800292 if (!list_empty(&lo->plh_segs))
293 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400294 pnfs_detach_layout_hdr(lo);
Fred Isamancc6e5342011-01-06 11:36:28 +0000295 spin_unlock(&inode->i_lock);
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400296 pnfs_free_layout_hdr(lo);
Fred Isamancc6e5342011-01-06 11:36:28 +0000297 }
Andy Adamson974cec82010-10-20 00:18:02 -0400298}
299
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500300/*
301 * Mark a pnfs_layout_hdr and all associated layout segments as invalid
302 *
303 * In order to continue using the pnfs_layout_hdr, a full recovery
304 * is required.
305 * Note that caller must hold inode->i_lock.
306 */
Trond Myklebust5f46be02016-07-22 11:25:27 -0400307int
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500308pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
309 struct list_head *lseg_list)
310{
311 struct pnfs_layout_range range = {
312 .iomode = IOMODE_ANY,
313 .offset = 0,
314 .length = NFS4_MAX_UINT64,
315 };
316
317 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Jeff Layton6d597e12016-05-17 12:28:42 -0400318 return pnfs_mark_matching_lsegs_invalid(lo, lseg_list, &range, 0);
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500319}
320
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400321static int
322pnfs_iomode_to_fail_bit(u32 iomode)
323{
324 return iomode == IOMODE_RW ?
325 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
326}
327
328static void
Trond Myklebust3e621212012-09-24 13:07:16 -0400329pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400330{
Trond Myklebust25c75332012-09-18 17:01:12 -0400331 lo->plh_retry_timestamp = jiffies;
Yanchuan Nian39e88fc2013-01-04 20:19:49 +0800332 if (!test_and_set_bit(fail_bit, &lo->plh_flags))
Trond Myklebust3e621212012-09-24 13:07:16 -0400333 atomic_inc(&lo->plh_refcount);
334}
335
336static void
337pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
338{
339 if (test_and_clear_bit(fail_bit, &lo->plh_flags))
340 atomic_dec(&lo->plh_refcount);
341}
342
343static void
344pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
345{
346 struct inode *inode = lo->plh_inode;
Trond Myklebust115ce572012-09-20 21:19:43 -0400347 struct pnfs_layout_range range = {
348 .iomode = iomode,
349 .offset = 0,
350 .length = NFS4_MAX_UINT64,
351 };
352 LIST_HEAD(head);
Trond Myklebust3e621212012-09-24 13:07:16 -0400353
354 spin_lock(&inode->i_lock);
355 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
Jeff Layton6d597e12016-05-17 12:28:42 -0400356 pnfs_mark_matching_lsegs_invalid(lo, &head, &range, 0);
Trond Myklebust3e621212012-09-24 13:07:16 -0400357 spin_unlock(&inode->i_lock);
Trond Myklebust115ce572012-09-20 21:19:43 -0400358 pnfs_free_lseg_list(&head);
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400359 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
360 iomode == IOMODE_RW ? "RW" : "READ");
361}
362
363static bool
364pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
365{
Trond Myklebust25c75332012-09-18 17:01:12 -0400366 unsigned long start, end;
Trond Myklebust3e621212012-09-24 13:07:16 -0400367 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
368
369 if (test_bit(fail_bit, &lo->plh_flags) == 0)
Trond Myklebust25c75332012-09-18 17:01:12 -0400370 return false;
371 end = jiffies;
372 start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
373 if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
374 /* It is time to retry the failed layoutgets */
Trond Myklebust3e621212012-09-24 13:07:16 -0400375 pnfs_layout_clear_fail_bit(lo, fail_bit);
Trond Myklebust25c75332012-09-18 17:01:12 -0400376 return false;
377 }
378 return true;
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400379}
380
Andy Adamson974cec82010-10-20 00:18:02 -0400381static void
Trond Myklebust119cef972016-07-24 15:10:12 -0400382pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
383 const struct pnfs_layout_range *range,
384 const nfs4_stateid *stateid)
Andy Adamson974cec82010-10-20 00:18:02 -0400385{
Fred Isaman566052c2011-01-06 11:36:20 +0000386 INIT_LIST_HEAD(&lseg->pls_list);
Peng Taoa9bae562011-07-30 20:52:33 -0400387 INIT_LIST_HEAD(&lseg->pls_lc_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000388 atomic_set(&lseg->pls_refcount, 1);
Fred Isaman4541d162011-01-06 11:36:23 +0000389 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000390 lseg->pls_layout = lo;
Trond Myklebust119cef972016-07-24 15:10:12 -0400391 lseg->pls_range = *range;
392 lseg->pls_seq = be32_to_cpu(stateid->seqid);
Andy Adamson974cec82010-10-20 00:18:02 -0400393}
394
Trond Myklebust905ca192012-09-20 20:46:49 -0400395static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400396{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000397 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400398
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400399 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400400}
401
Fred Isamand684d2a2011-03-01 01:34:13 +0000402static void
Trond Myklebust57036a32012-09-20 16:33:30 -0400403pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
404 struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400405{
Trond Myklebust57036a32012-09-20 16:33:30 -0400406 struct inode *inode = lo->plh_inode;
Fred Isamand684d2a2011-03-01 01:34:13 +0000407
Benny Halevyd20581a2011-05-22 19:52:03 +0300408 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000409 list_del_init(&lseg->pls_list);
Trond Myklebust8f0d27d2012-09-20 20:57:11 -0400410 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
411 atomic_dec(&lo->plh_refcount);
Trond Myklebust2d148c72016-06-17 16:48:26 -0400412 if (list_empty(&lo->plh_segs)) {
Trond Myklebust334a8f32016-09-04 12:46:35 -0400413 if (atomic_read(&lo->plh_outstanding) == 0)
414 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Trond Myklebust173f77e2012-09-21 15:49:42 -0400415 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Trond Myklebust2d148c72016-06-17 16:48:26 -0400416 }
Fred Isamand684d2a2011-03-01 01:34:13 +0000417 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
418}
419
Fred Isamanbae724e2011-03-01 01:34:15 +0000420void
Trond Myklebust9369a432012-09-18 20:57:08 -0400421pnfs_put_lseg(struct pnfs_layout_segment *lseg)
Fred Isamand684d2a2011-03-01 01:34:13 +0000422{
Trond Myklebust57036a32012-09-20 16:33:30 -0400423 struct pnfs_layout_hdr *lo;
Fred Isamand684d2a2011-03-01 01:34:13 +0000424 struct inode *inode;
425
426 if (!lseg)
427 return;
428
Fred Isaman4541d162011-01-06 11:36:23 +0000429 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
430 atomic_read(&lseg->pls_refcount),
431 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500432
Trond Myklebust57036a32012-09-20 16:33:30 -0400433 lo = lseg->pls_layout;
434 inode = lo->plh_inode;
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500435
Fred Isamand684d2a2011-03-01 01:34:13 +0000436 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
Trond Myklebustfaa4a542015-07-09 18:24:07 +0200437 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
438 spin_unlock(&inode->i_lock);
439 return;
440 }
Trond Myklebust8f0d27d2012-09-20 20:57:11 -0400441 pnfs_get_layout_hdr(lo);
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500442 pnfs_layout_remove_lseg(lo, lseg);
443 spin_unlock(&inode->i_lock);
444 pnfs_free_lseg(lseg);
445 pnfs_put_layout_hdr(lo);
Fred Isaman4541d162011-01-06 11:36:23 +0000446 }
Andy Adamson974cec82010-10-20 00:18:02 -0400447}
Trond Myklebust9369a432012-09-18 20:57:08 -0400448EXPORT_SYMBOL_GPL(pnfs_put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400449
Trond Myklebust6543f802014-10-08 16:39:12 -0400450static void pnfs_free_lseg_async_work(struct work_struct *work)
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400451{
452 struct pnfs_layout_segment *lseg;
Trond Myklebust6543f802014-10-08 16:39:12 -0400453 struct pnfs_layout_hdr *lo;
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400454
455 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
Trond Myklebust6543f802014-10-08 16:39:12 -0400456 lo = lseg->pls_layout;
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400457
Trond Myklebust6543f802014-10-08 16:39:12 -0400458 pnfs_free_lseg(lseg);
459 pnfs_put_layout_hdr(lo);
460}
461
462static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg)
463{
464 INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work);
465 schedule_work(&lseg->pls_work);
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400466}
467
468void
Trond Myklebust6543f802014-10-08 16:39:12 -0400469pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg)
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400470{
Trond Myklebust6543f802014-10-08 16:39:12 -0400471 if (!lseg)
472 return;
473
474 assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock);
475
476 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
477 atomic_read(&lseg->pls_refcount),
478 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
479 if (atomic_dec_and_test(&lseg->pls_refcount)) {
480 struct pnfs_layout_hdr *lo = lseg->pls_layout;
Trond Myklebustfaa4a542015-07-09 18:24:07 +0200481 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
482 return;
Trond Myklebust6543f802014-10-08 16:39:12 -0400483 pnfs_get_layout_hdr(lo);
484 pnfs_layout_remove_lseg(lo, lseg);
485 pnfs_free_lseg_async(lseg);
486 }
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400487}
Trond Myklebust6543f802014-10-08 16:39:12 -0400488EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked);
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400489
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400490static u64
Benny Halevyfb3296e2011-05-22 19:47:26 +0300491end_offset(u64 start, u64 len)
Fred Isaman4541d162011-01-06 11:36:23 +0000492{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300493 u64 end;
494
495 end = start + len;
496 return end >= start ? end : NFS4_MAX_UINT64;
497}
498
Benny Halevyfb3296e2011-05-22 19:47:26 +0300499/*
500 * is l2 fully contained in l1?
501 * start1 end1
502 * [----------------------------------)
503 * start2 end2
504 * [----------------)
505 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400506static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -0400507pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400508 const struct pnfs_layout_range *l2)
Benny Halevyfb3296e2011-05-22 19:47:26 +0300509{
510 u64 start1 = l1->offset;
511 u64 end1 = end_offset(start1, l1->length);
512 u64 start2 = l2->offset;
513 u64 end2 = end_offset(start2, l2->length);
514
515 return (start1 <= start2) && (end1 >= end2);
516}
517
518/*
519 * is l1 and l2 intersecting?
520 * start1 end1
521 * [----------------------------------)
522 * start2 end2
523 * [----------------)
524 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400525static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -0400526pnfs_lseg_range_intersecting(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400527 const struct pnfs_layout_range *l2)
Benny Halevyfb3296e2011-05-22 19:47:26 +0300528{
529 u64 start1 = l1->offset;
530 u64 end1 = end_offset(start1, l1->length);
531 u64 start2 = l2->offset;
532 u64 end2 = end_offset(start2, l2->length);
533
534 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
535 (end2 == NFS4_MAX_UINT64 || end2 > start1);
536}
537
Trond Myklebust24956802013-03-20 13:03:00 -0400538static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
539 struct list_head *tmp_list)
540{
541 if (!atomic_dec_and_test(&lseg->pls_refcount))
542 return false;
543 pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
544 list_add(&lseg->pls_list, tmp_list);
545 return true;
546}
547
Fred Isaman4541d162011-01-06 11:36:23 +0000548/* Returns 1 if lseg is removed from list, 0 otherwise */
549static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
550 struct list_head *tmp_list)
551{
552 int rv = 0;
553
554 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
555 /* Remove the reference keeping the lseg in the
556 * list. It will now be removed when all
557 * outstanding io is finished.
558 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000559 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
560 atomic_read(&lseg->pls_refcount));
Trond Myklebust24956802013-03-20 13:03:00 -0400561 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
Fred Isamand684d2a2011-03-01 01:34:13 +0000562 rv = 1;
Fred Isaman4541d162011-01-06 11:36:23 +0000563 }
564 return rv;
565}
566
Jeff Layton6d597e12016-05-17 12:28:42 -0400567/*
568 * Compare 2 layout stateid sequence ids, to see which is newer,
569 * taking into account wraparound issues.
570 */
571static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
572{
573 return (s32)(s1 - s2) > 0;
574}
575
Trond Myklebuste036f462016-07-22 11:13:22 -0400576static bool
577pnfs_should_free_range(const struct pnfs_layout_range *lseg_range,
578 const struct pnfs_layout_range *recall_range)
579{
580 return (recall_range->iomode == IOMODE_ANY ||
581 lseg_range->iomode == recall_range->iomode) &&
582 pnfs_lseg_range_intersecting(lseg_range, recall_range);
583}
584
585static bool
586pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg,
587 const struct pnfs_layout_range *recall_range,
588 u32 seq)
589{
590 if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq))
591 return false;
592 if (recall_range == NULL)
593 return true;
594 return pnfs_should_free_range(&lseg->pls_range, recall_range);
595}
596
Jeff Layton6d597e12016-05-17 12:28:42 -0400597/**
598 * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later
599 * @lo: layout header containing the lsegs
600 * @tmp_list: list head where doomed lsegs should go
601 * @recall_range: optional recall range argument to match (may be NULL)
602 * @seq: only invalidate lsegs obtained prior to this sequence (may be 0)
603 *
604 * Walk the list of lsegs in the layout header, and tear down any that should
605 * be destroyed. If "recall_range" is specified then the segment must match
606 * that range. If "seq" is non-zero, then only match segments that were handed
607 * out at or before that sequence.
608 *
609 * Returns number of matching invalid lsegs remaining in list after scanning
610 * it and purging them.
Fred Isaman4541d162011-01-06 11:36:23 +0000611 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000612int
Trond Myklebust49a85062012-09-18 20:43:31 -0400613pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
Fred Isaman4541d162011-01-06 11:36:23 +0000614 struct list_head *tmp_list,
Jeff Layton6d597e12016-05-17 12:28:42 -0400615 const struct pnfs_layout_range *recall_range,
616 u32 seq)
Andy Adamson974cec82010-10-20 00:18:02 -0400617{
618 struct pnfs_layout_segment *lseg, *next;
Trond Myklebust71b39852016-01-04 12:41:15 -0500619 int remaining = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400620
621 dprintk("%s:Begin lo %p\n", __func__, lo);
622
Trond Myklebust8006bfb2012-09-21 14:48:04 -0400623 if (list_empty(&lo->plh_segs))
Fred Isaman38511722011-02-03 18:28:50 +0000624 return 0;
Fred Isaman4541d162011-01-06 11:36:23 +0000625 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Trond Myklebuste036f462016-07-22 11:13:22 -0400626 if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
Jeff Layton6d597e12016-05-17 12:28:42 -0400627 dprintk("%s: freeing lseg %p iomode %d seq %u"
Fred Isaman4541d162011-01-06 11:36:23 +0000628 "offset %llu length %llu\n", __func__,
Jeff Layton6d597e12016-05-17 12:28:42 -0400629 lseg, lseg->pls_range.iomode, lseg->pls_seq,
630 lseg->pls_range.offset, lseg->pls_range.length);
Trond Myklebust71b39852016-01-04 12:41:15 -0500631 if (!mark_lseg_invalid(lseg, tmp_list))
632 remaining++;
Fred Isaman4541d162011-01-06 11:36:23 +0000633 }
Trond Myklebust71b39852016-01-04 12:41:15 -0500634 dprintk("%s:Return %i\n", __func__, remaining);
635 return remaining;
Andy Adamson974cec82010-10-20 00:18:02 -0400636}
637
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000638/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000639void
Fred Isaman4541d162011-01-06 11:36:23 +0000640pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400641{
Fred Isaman4541d162011-01-06 11:36:23 +0000642 struct pnfs_layout_segment *lseg, *tmp;
Andy Adamson974cec82010-10-20 00:18:02 -0400643
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000644 if (list_empty(free_me))
645 return;
646
Fred Isaman4541d162011-01-06 11:36:23 +0000647 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000648 list_del(&lseg->pls_list);
Trond Myklebust905ca192012-09-20 20:46:49 -0400649 pnfs_free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400650 }
651}
652
Benny Halevye5e94012010-10-20 00:18:01 -0400653void
654pnfs_destroy_layout(struct nfs_inode *nfsi)
655{
656 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400657 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400658
659 spin_lock(&nfsi->vfs_inode.i_lock);
660 lo = nfsi->layout;
661 if (lo) {
Trond Myklebust3e621212012-09-24 13:07:16 -0400662 pnfs_get_layout_hdr(lo);
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500663 pnfs_mark_layout_stateid_invalid(lo, &tmp_list);
Trond Myklebust3e621212012-09-24 13:07:16 -0400664 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
665 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
666 spin_unlock(&nfsi->vfs_inode.i_lock);
667 pnfs_free_lseg_list(&tmp_list);
668 pnfs_put_layout_hdr(lo);
669 } else
670 spin_unlock(&nfsi->vfs_inode.i_lock);
Benny Halevye5e94012010-10-20 00:18:01 -0400671}
Andy Adamson041245c2012-04-27 17:53:53 -0400672EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
Benny Halevye5e94012010-10-20 00:18:01 -0400673
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500674static bool
675pnfs_layout_add_bulk_destroy_list(struct inode *inode,
676 struct list_head *layout_list)
677{
678 struct pnfs_layout_hdr *lo;
679 bool ret = false;
680
681 spin_lock(&inode->i_lock);
682 lo = NFS_I(inode)->layout;
683 if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
684 pnfs_get_layout_hdr(lo);
685 list_add(&lo->plh_bulk_destroy, layout_list);
686 ret = true;
687 }
688 spin_unlock(&inode->i_lock);
689 return ret;
690}
691
692/* Caller must hold rcu_read_lock and clp->cl_lock */
693static int
694pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
695 struct nfs_server *server,
696 struct list_head *layout_list)
697{
698 struct pnfs_layout_hdr *lo, *next;
699 struct inode *inode;
700
701 list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
702 inode = igrab(lo->plh_inode);
703 if (inode == NULL)
704 continue;
705 list_del_init(&lo->plh_layouts);
706 if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
707 continue;
708 rcu_read_unlock();
709 spin_unlock(&clp->cl_lock);
710 iput(inode);
711 spin_lock(&clp->cl_lock);
712 rcu_read_lock();
713 return -EAGAIN;
714 }
715 return 0;
716}
717
718static int
719pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
720 bool is_bulk_recall)
721{
722 struct pnfs_layout_hdr *lo;
723 struct inode *inode;
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500724 LIST_HEAD(lseg_list);
725 int ret = 0;
726
727 while (!list_empty(layout_list)) {
728 lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
729 plh_bulk_destroy);
730 dprintk("%s freeing layout for inode %lu\n", __func__,
731 lo->plh_inode->i_ino);
732 inode = lo->plh_inode;
Christoph Hellwig7c5d1872014-09-10 08:23:29 -0700733
734 pnfs_layoutcommit_inode(inode, false);
735
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500736 spin_lock(&inode->i_lock);
737 list_del_init(&lo->plh_bulk_destroy);
Trond Myklebust9fd4b9f2016-02-22 17:46:34 -0500738 if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
739 if (is_bulk_recall)
740 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500741 ret = -EAGAIN;
Trond Myklebust9fd4b9f2016-02-22 17:46:34 -0500742 }
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500743 spin_unlock(&inode->i_lock);
744 pnfs_free_lseg_list(&lseg_list);
Trond Myklebustb20135d2015-12-31 09:28:06 -0500745 /* Free all lsegs that are attached to commit buckets */
746 nfs_commit_inode(inode, 0);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500747 pnfs_put_layout_hdr(lo);
748 iput(inode);
749 }
750 return ret;
751}
752
753int
754pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
755 struct nfs_fsid *fsid,
756 bool is_recall)
757{
758 struct nfs_server *server;
759 LIST_HEAD(layout_list);
760
761 spin_lock(&clp->cl_lock);
762 rcu_read_lock();
763restart:
764 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
765 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
766 continue;
767 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
768 server,
769 &layout_list) != 0)
770 goto restart;
771 }
772 rcu_read_unlock();
773 spin_unlock(&clp->cl_lock);
774
775 if (list_empty(&layout_list))
776 return 0;
777 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
778}
779
780int
781pnfs_destroy_layouts_byclid(struct nfs_client *clp,
782 bool is_recall)
783{
784 struct nfs_server *server;
785 LIST_HEAD(layout_list);
786
787 spin_lock(&clp->cl_lock);
788 rcu_read_lock();
789restart:
790 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
791 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
792 server,
793 &layout_list) != 0)
794 goto restart;
795 }
796 rcu_read_unlock();
797 spin_unlock(&clp->cl_lock);
798
799 if (list_empty(&layout_list))
800 return 0;
801 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
802}
803
Andy Adamson974cec82010-10-20 00:18:02 -0400804/*
805 * Called by the state manger to remove all layouts established under an
806 * expired lease.
807 */
808void
809pnfs_destroy_all_layouts(struct nfs_client *clp)
810{
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400811 nfs4_deviceid_mark_client_invalid(clp);
812 nfs4_deviceid_purge_client(clp);
813
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500814 pnfs_destroy_layouts_byclid(clp, false);
Andy Adamson974cec82010-10-20 00:18:02 -0400815}
816
Trond Myklebust2a59a042016-09-03 11:20:04 -0400817static void
818pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo)
819{
820 lo->plh_return_iomode = 0;
821 lo->plh_return_seq = 0;
822 clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
823}
824
Fred Isamanfd6002e2011-01-06 11:36:22 +0000825/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000826void
827pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
828 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400829{
Trond Myklebustecebb802016-07-24 11:46:06 -0400830 u32 oldseq, newseq, new_barrier = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400831
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500832 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
833 newseq = be32_to_cpu(new->seqid);
Trond Myklebust2a59a042016-09-03 11:20:04 -0400834
835 if (!pnfs_layout_is_valid(lo)) {
836 nfs4_stateid_copy(&lo->plh_stateid, new);
837 lo->plh_barrier = newseq;
838 pnfs_clear_layoutreturn_info(lo);
839 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
840 return;
841 }
842 if (pnfs_seqid_is_newer(newseq, oldseq)) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500843 nfs4_stateid_copy(&lo->plh_stateid, new);
Trond Myklebustecebb802016-07-24 11:46:06 -0400844 /*
845 * Because of wraparound, we want to keep the barrier
846 * "close" to the current seqids.
847 */
848 new_barrier = newseq - atomic_read(&lo->plh_outstanding);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000849 }
Trond Myklebustecebb802016-07-24 11:46:06 -0400850 if (update_barrier)
851 new_barrier = be32_to_cpu(new->seqid);
852 else if (new_barrier == 0)
853 return;
Trond Myklebust2a59a042016-09-03 11:20:04 -0400854 if (pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
Trond Myklebustecebb802016-07-24 11:46:06 -0400855 lo->plh_barrier = new_barrier;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400856}
857
Trond Myklebust19c54ab2012-10-05 16:56:58 -0700858static bool
859pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
860 const nfs4_stateid *stateid)
861{
862 u32 seqid = be32_to_cpu(stateid->seqid);
863
864 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
865}
866
Fred Isamancf7d63f2011-01-06 11:36:25 +0000867/* lget is set to 1 if called from inside send_layoutget call chain */
868static bool
Trond Myklebuste1c06f82015-08-04 16:40:08 -0400869pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000870{
Fred Isamanf7e89172011-01-06 11:36:32 +0000871 return lo->plh_block_lgets ||
Trond Myklebuste1c06f82015-08-04 16:40:08 -0400872 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000873}
874
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400875/*
Jeff Layton183d9e72016-05-17 12:28:47 -0400876 * Get layout from server.
877 * for now, assume that whole file layouts are requested.
878 * arg->offset: 0
879 * arg->length: all ones
880 */
Benny Halevye5e94012010-10-20 00:18:01 -0400881static struct pnfs_layout_segment *
882send_layoutget(struct pnfs_layout_hdr *lo,
883 struct nfs_open_context *ctx,
Jeff Layton183d9e72016-05-17 12:28:47 -0400884 nfs4_stateid *stateid,
Trond Myklebuste144e532016-01-04 12:52:53 -0500885 const struct pnfs_layout_range *range,
Jeff Layton183d9e72016-05-17 12:28:47 -0400886 long *timeout, gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400887{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000888 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400889 struct nfs_server *server = NFS_SERVER(ino);
890 struct nfs4_layoutget *lgp;
Trond Myklebust2d89a1d2015-08-31 02:05:47 -0700891 loff_t i_size;
Benny Halevye5e94012010-10-20 00:18:01 -0400892
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400893 dprintk("--> %s\n", __func__);
894
Jeff Layton4f2e9dc2015-11-25 13:43:14 -0500895 /*
896 * Synchronously retrieve layout information from server and
897 * store in lseg. If we race with a concurrent seqid morphing
898 * op, then re-send the LAYOUTGET.
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400899 */
Jeff Layton83026d802016-05-17 12:28:46 -0400900 lgp = kzalloc(sizeof(*lgp), gfp_flags);
901 if (lgp == NULL)
902 return ERR_PTR(-ENOMEM);
Jeff Layton4f2e9dc2015-11-25 13:43:14 -0500903
Jeff Layton83026d802016-05-17 12:28:46 -0400904 i_size = i_size_read(ino);
Jeff Layton4f2e9dc2015-11-25 13:43:14 -0500905
Jeff Layton83026d802016-05-17 12:28:46 -0400906 lgp->args.minlength = PAGE_SIZE;
907 if (lgp->args.minlength > range->length)
908 lgp->args.minlength = range->length;
909 if (range->iomode == IOMODE_READ) {
910 if (range->offset >= i_size)
911 lgp->args.minlength = 0;
912 else if (i_size - range->offset < lgp->args.minlength)
913 lgp->args.minlength = i_size - range->offset;
Jeff Laytond03ab292016-05-17 12:28:45 -0400914 }
Jeff Layton83026d802016-05-17 12:28:46 -0400915 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
916 pnfs_copy_range(&lgp->args.range, range);
917 lgp->args.type = server->pnfs_curr_ld->id;
918 lgp->args.inode = ino;
919 lgp->args.ctx = get_nfs_open_context(ctx);
Jeff Layton183d9e72016-05-17 12:28:47 -0400920 nfs4_stateid_copy(&lgp->args.stateid, stateid);
Jeff Layton83026d802016-05-17 12:28:46 -0400921 lgp->gfp_flags = gfp_flags;
922 lgp->cred = lo->plh_lc_cred;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400923
Jeff Layton183d9e72016-05-17 12:28:47 -0400924 return nfs4_proc_layoutget(lgp, timeout, gfp_flags);
Andy Adamson974cec82010-10-20 00:18:02 -0400925}
926
Trond Myklebust24956802013-03-20 13:03:00 -0400927static void pnfs_clear_layoutcommit(struct inode *inode,
928 struct list_head *head)
929{
930 struct nfs_inode *nfsi = NFS_I(inode);
931 struct pnfs_layout_segment *lseg, *tmp;
932
933 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
934 return;
935 list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
936 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
937 continue;
938 pnfs_lseg_dec_and_remove_zero(lseg, head);
939 }
940}
941
Tom Haynesd67ae822014-12-11 17:02:04 -0500942void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
943{
944 clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
945 smp_mb__after_atomic();
946 wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
Trond Myklebust7f273922015-07-09 18:40:01 +0200947 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
Tom Haynesd67ae822014-12-11 17:02:04 -0500948}
949
Trond Myklebust13c13a62016-01-26 23:12:11 -0500950static bool
Trond Myklebuste5fd1902016-07-21 12:44:15 -0400951pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
952 nfs4_stateid *stateid,
953 enum pnfs_iomode *iomode)
Trond Myklebust13c13a62016-01-26 23:12:11 -0500954{
Trond Myklebustbf0291d2016-09-03 10:39:51 -0400955 /* Serialise LAYOUTGET/LAYOUTRETURN */
956 if (atomic_read(&lo->plh_outstanding) != 0)
957 return false;
Trond Myklebust13c13a62016-01-26 23:12:11 -0500958 if (test_and_set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
959 return false;
Trond Myklebust13c13a62016-01-26 23:12:11 -0500960 pnfs_get_layout_hdr(lo);
Trond Myklebuste5fd1902016-07-21 12:44:15 -0400961 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
962 if (stateid != NULL) {
963 nfs4_stateid_copy(stateid, &lo->plh_stateid);
964 if (lo->plh_return_seq != 0)
965 stateid->seqid = cpu_to_be32(lo->plh_return_seq);
966 }
967 if (iomode != NULL)
968 *iomode = lo->plh_return_iomode;
969 pnfs_clear_layoutreturn_info(lo);
970 return true;
971 }
972 if (stateid != NULL)
973 nfs4_stateid_copy(stateid, &lo->plh_stateid);
974 if (iomode != NULL)
975 *iomode = IOMODE_ANY;
Trond Myklebust13c13a62016-01-26 23:12:11 -0500976 return true;
977}
978
Peng Taof40eb5d2014-09-06 00:53:22 +0800979static int
Trond Myklebusted429d62016-01-04 12:30:55 -0500980pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, const nfs4_stateid *stateid,
Peng Tao6c166052014-11-17 09:30:40 +0800981 enum pnfs_iomode iomode, bool sync)
Peng Taof40eb5d2014-09-06 00:53:22 +0800982{
983 struct inode *ino = lo->plh_inode;
984 struct nfs4_layoutreturn *lrp;
985 int status = 0;
986
Trond Myklebuste4af4402015-02-05 17:05:08 -0500987 lrp = kzalloc(sizeof(*lrp), GFP_NOFS);
Peng Taof40eb5d2014-09-06 00:53:22 +0800988 if (unlikely(lrp == NULL)) {
989 status = -ENOMEM;
990 spin_lock(&ino->i_lock);
Tom Haynesd67ae822014-12-11 17:02:04 -0500991 pnfs_clear_layoutreturn_waitbit(lo);
Peng Taof40eb5d2014-09-06 00:53:22 +0800992 spin_unlock(&ino->i_lock);
993 pnfs_put_layout_hdr(lo);
994 goto out;
995 }
996
Trond Myklebusted429d62016-01-04 12:30:55 -0500997 nfs4_stateid_copy(&lrp->args.stateid, stateid);
Peng Taof40eb5d2014-09-06 00:53:22 +0800998 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
999 lrp->args.inode = ino;
Peng Tao15eb67c2014-11-17 09:30:36 +08001000 lrp->args.range.iomode = iomode;
1001 lrp->args.range.offset = 0;
1002 lrp->args.range.length = NFS4_MAX_UINT64;
Peng Taof40eb5d2014-09-06 00:53:22 +08001003 lrp->args.layout = lo;
1004 lrp->clp = NFS_SERVER(ino)->nfs_client;
1005 lrp->cred = lo->plh_lc_cred;
1006
Peng Tao6c166052014-11-17 09:30:40 +08001007 status = nfs4_proc_layoutreturn(lrp, sync);
Peng Taof40eb5d2014-09-06 00:53:22 +08001008out:
1009 dprintk("<-- %s status: %d\n", __func__, status);
1010 return status;
1011}
1012
Trond Myklebust13c13a62016-01-26 23:12:11 -05001013/* Return true if layoutreturn is needed */
1014static bool
1015pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
1016{
1017 struct pnfs_layout_segment *s;
1018
Trond Myklebust2370abd2016-01-27 20:32:50 -05001019 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
Trond Myklebust13c13a62016-01-26 23:12:11 -05001020 return false;
1021
1022 /* Defer layoutreturn until all lsegs are done */
1023 list_for_each_entry(s, &lo->plh_segs, pls_list) {
1024 if (test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
1025 return false;
1026 }
1027
1028 return true;
1029}
1030
1031static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
1032{
1033 struct inode *inode= lo->plh_inode;
1034
Trond Myklebust2370abd2016-01-27 20:32:50 -05001035 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
Trond Myklebust13c13a62016-01-26 23:12:11 -05001036 return;
1037 spin_lock(&inode->i_lock);
1038 if (pnfs_layout_need_return(lo)) {
1039 nfs4_stateid stateid;
1040 enum pnfs_iomode iomode;
1041 bool send;
1042
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001043 send = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
Trond Myklebust13c13a62016-01-26 23:12:11 -05001044 spin_unlock(&inode->i_lock);
1045 if (send) {
1046 /* Send an async layoutreturn so we dont deadlock */
1047 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1048 }
1049 } else
1050 spin_unlock(&inode->i_lock);
1051}
1052
Andy Adamson293b3b02012-06-20 15:03:34 -04001053/*
1054 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
1055 * when the layout segment list is empty.
1056 *
1057 * Note that a pnfs_layout_hdr can exist with an empty layout segment
1058 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
1059 * deviceid is marked invalid.
1060 */
Benny Halevycbe82602011-05-22 19:52:37 +03001061int
1062_pnfs_return_layout(struct inode *ino)
1063{
1064 struct pnfs_layout_hdr *lo = NULL;
1065 struct nfs_inode *nfsi = NFS_I(ino);
1066 LIST_HEAD(tmp_list);
Benny Halevycbe82602011-05-22 19:52:37 +03001067 nfs4_stateid stateid;
Andy Adamson293b3b02012-06-20 15:03:34 -04001068 int status = 0, empty;
Trond Myklebust7f273922015-07-09 18:40:01 +02001069 bool send;
Benny Halevycbe82602011-05-22 19:52:37 +03001070
Andy Adamson366d5052012-06-20 15:03:33 -04001071 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
Benny Halevycbe82602011-05-22 19:52:37 +03001072
1073 spin_lock(&ino->i_lock);
1074 lo = nfsi->layout;
Trond Myklebuste5929f32012-09-21 16:37:02 -04001075 if (!lo) {
Benny Halevycbe82602011-05-22 19:52:37 +03001076 spin_unlock(&ino->i_lock);
Andy Adamson293b3b02012-06-20 15:03:34 -04001077 dprintk("NFS: %s no layout to return\n", __func__);
1078 goto out;
Benny Halevycbe82602011-05-22 19:52:37 +03001079 }
Benny Halevycbe82602011-05-22 19:52:37 +03001080 /* Reference matched in nfs4_layoutreturn_release */
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001081 pnfs_get_layout_hdr(lo);
Andy Adamson293b3b02012-06-20 15:03:34 -04001082 empty = list_empty(&lo->plh_segs);
Trond Myklebust24956802013-03-20 13:03:00 -04001083 pnfs_clear_layoutcommit(ino, &tmp_list);
Jeff Layton6d597e12016-05-17 12:28:42 -04001084 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL, 0);
Christoph Hellwigc88953d2014-09-10 08:23:31 -07001085
1086 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
1087 struct pnfs_layout_range range = {
1088 .iomode = IOMODE_ANY,
1089 .offset = 0,
1090 .length = NFS4_MAX_UINT64,
1091 };
1092 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1093 }
1094
Andy Adamson293b3b02012-06-20 15:03:34 -04001095 /* Don't send a LAYOUTRETURN if list was initially empty */
1096 if (empty) {
1097 spin_unlock(&ino->i_lock);
Andy Adamson293b3b02012-06-20 15:03:34 -04001098 dprintk("NFS: %s no layout segments to return\n", __func__);
Trond Myklebust7f273922015-07-09 18:40:01 +02001099 goto out_put_layout_hdr;
Andy Adamson293b3b02012-06-20 15:03:34 -04001100 }
Christoph Hellwig47abade2014-08-21 11:09:22 -05001101
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001102 send = pnfs_prepare_layoutreturn(lo, &stateid, NULL);
Benny Halevycbe82602011-05-22 19:52:37 +03001103 spin_unlock(&ino->i_lock);
1104 pnfs_free_lseg_list(&tmp_list);
Trond Myklebust7f273922015-07-09 18:40:01 +02001105 if (send)
Trond Myklebusted429d62016-01-04 12:30:55 -05001106 status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
Trond Myklebust7f273922015-07-09 18:40:01 +02001107out_put_layout_hdr:
1108 pnfs_put_layout_hdr(lo);
Benny Halevycbe82602011-05-22 19:52:37 +03001109out:
1110 dprintk("<-- %s status: %d\n", __func__, status);
1111 return status;
1112}
Andy Adamson0a57cda2012-04-27 17:53:50 -04001113EXPORT_SYMBOL_GPL(_pnfs_return_layout);
Benny Halevycbe82602011-05-22 19:52:37 +03001114
Trond Myklebust24028672013-03-20 13:23:33 -04001115int
1116pnfs_commit_and_return_layout(struct inode *inode)
1117{
1118 struct pnfs_layout_hdr *lo;
1119 int ret;
1120
1121 spin_lock(&inode->i_lock);
1122 lo = NFS_I(inode)->layout;
1123 if (lo == NULL) {
1124 spin_unlock(&inode->i_lock);
1125 return 0;
1126 }
1127 pnfs_get_layout_hdr(lo);
1128 /* Block new layoutgets and read/write to ds */
1129 lo->plh_block_lgets++;
1130 spin_unlock(&inode->i_lock);
1131 filemap_fdatawait(inode->i_mapping);
1132 ret = pnfs_layoutcommit_inode(inode, true);
1133 if (ret == 0)
1134 ret = _pnfs_return_layout(inode);
1135 spin_lock(&inode->i_lock);
1136 lo->plh_block_lgets--;
1137 spin_unlock(&inode->i_lock);
1138 pnfs_put_layout_hdr(lo);
1139 return ret;
1140}
1141
Fred Isamanf7e89172011-01-06 11:36:32 +00001142bool pnfs_roc(struct inode *ino)
1143{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001144 struct nfs_inode *nfsi = NFS_I(ino);
1145 struct nfs_open_context *ctx;
1146 struct nfs4_state *state;
Fred Isamanf7e89172011-01-06 11:36:32 +00001147 struct pnfs_layout_hdr *lo;
1148 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao193e3aa2014-11-17 09:30:41 +08001149 nfs4_stateid stateid;
Fred Isamanf7e89172011-01-06 11:36:32 +00001150 LIST_HEAD(tmp_list);
Peng Taoe755d632015-08-19 13:49:19 +08001151 bool found = false, layoutreturn = false, roc = false;
Fred Isamanf7e89172011-01-06 11:36:32 +00001152
1153 spin_lock(&ino->i_lock);
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001154 lo = nfsi->layout;
Peng Tao39761432015-08-21 12:49:44 +08001155 if (!lo || test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001156 goto out_noroc;
1157
Peng Taoe755d632015-08-19 13:49:19 +08001158 /* no roc if we hold a delegation */
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001159 if (nfs4_check_delegation(ino, FMODE_READ))
1160 goto out_noroc;
1161
1162 list_for_each_entry(ctx, &nfsi->open_files, list) {
1163 state = ctx->state;
1164 /* Don't return layout if there is open file state */
1165 if (state != NULL && state->state != 0)
1166 goto out_noroc;
1167 }
1168
Peng Taoe755d632015-08-19 13:49:19 +08001169 /* always send layoutreturn if being marked so */
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001170 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1171 layoutreturn = pnfs_prepare_layoutreturn(lo,
1172 &stateid, NULL);
Peng Taoe755d632015-08-19 13:49:19 +08001173
Fred Isamanf7e89172011-01-06 11:36:32 +00001174 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
Peng Taoe755d632015-08-19 13:49:19 +08001175 /* If we are sending layoutreturn, invalidate all valid lsegs */
1176 if (layoutreturn || test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
Fred Isamanf7e89172011-01-06 11:36:32 +00001177 mark_lseg_invalid(lseg, &tmp_list);
1178 found = true;
1179 }
Peng Tao500d7012015-09-22 11:35:22 +08001180 /* ROC in two conditions:
Peng Taoe755d632015-08-19 13:49:19 +08001181 * 1. there are ROC lsegs
1182 * 2. we don't send layoutreturn
Peng Taoe755d632015-08-19 13:49:19 +08001183 */
Peng Tao500d7012015-09-22 11:35:22 +08001184 if (found && !layoutreturn) {
1185 /* lo ref dropped in pnfs_roc_release() */
1186 pnfs_get_layout_hdr(lo);
Peng Taoe755d632015-08-19 13:49:19 +08001187 roc = true;
Peng Tao500d7012015-09-22 11:35:22 +08001188 }
Peng Taoe755d632015-08-19 13:49:19 +08001189
1190out_noroc:
Fred Isamanf7e89172011-01-06 11:36:32 +00001191 spin_unlock(&ino->i_lock);
1192 pnfs_free_lseg_list(&tmp_list);
Trond Myklebust71401712015-03-25 12:36:13 -04001193 pnfs_layoutcommit_inode(ino, true);
Peng Taoe755d632015-08-19 13:49:19 +08001194 if (layoutreturn)
Trond Myklebusted429d62016-01-04 12:30:55 -05001195 pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
Peng Taoe755d632015-08-19 13:49:19 +08001196 return roc;
Fred Isamanf7e89172011-01-06 11:36:32 +00001197}
1198
1199void pnfs_roc_release(struct inode *ino)
1200{
1201 struct pnfs_layout_hdr *lo;
1202
1203 spin_lock(&ino->i_lock);
1204 lo = NFS_I(ino)->layout;
Trond Myklebust5c4a79f2015-08-04 15:57:13 -04001205 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust6622c3e2012-09-20 17:23:11 -04001206 if (atomic_dec_and_test(&lo->plh_refcount)) {
1207 pnfs_detach_layout_hdr(lo);
1208 spin_unlock(&ino->i_lock);
1209 pnfs_free_layout_hdr(lo);
1210 } else
1211 spin_unlock(&ino->i_lock);
Fred Isamanf7e89172011-01-06 11:36:32 +00001212}
1213
1214void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
1215{
1216 struct pnfs_layout_hdr *lo;
1217
1218 spin_lock(&ino->i_lock);
1219 lo = NFS_I(ino)->layout;
Trond Myklebust0f35ad62012-10-04 16:28:17 -07001220 if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
Fred Isamanf7e89172011-01-06 11:36:32 +00001221 lo->plh_barrier = barrier;
1222 spin_unlock(&ino->i_lock);
Trond Myklebust6a463beb2015-08-20 15:40:47 -05001223 trace_nfs4_layoutreturn_on_close(ino, 0);
Fred Isamanf7e89172011-01-06 11:36:32 +00001224}
1225
Trond Myklebust4ff376f2015-08-18 23:23:21 -05001226void pnfs_roc_get_barrier(struct inode *ino, u32 *barrier)
Fred Isamanf7e89172011-01-06 11:36:32 +00001227{
1228 struct nfs_inode *nfsi = NFS_I(ino);
Trond Myklebust7fdab062012-09-20 20:15:57 -04001229 struct pnfs_layout_hdr *lo;
Trond Myklebust7fdab062012-09-20 20:15:57 -04001230 u32 current_seqid;
Fred Isamanf7e89172011-01-06 11:36:32 +00001231
1232 spin_lock(&ino->i_lock);
Trond Myklebust7fdab062012-09-20 20:15:57 -04001233 lo = nfsi->layout;
1234 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
Fred Isamanf7e89172011-01-06 11:36:32 +00001235
Trond Myklebust7fdab062012-09-20 20:15:57 -04001236 /* Since close does not return a layout stateid for use as
1237 * a barrier, we choose the worst-case barrier.
1238 */
1239 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
Fred Isamanf7e89172011-01-06 11:36:32 +00001240 spin_unlock(&ino->i_lock);
Fred Isamanf7e89172011-01-06 11:36:32 +00001241}
1242
Peng Tao500d7012015-09-22 11:35:22 +08001243bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
1244{
1245 struct nfs_inode *nfsi = NFS_I(ino);
1246 struct pnfs_layout_hdr *lo;
1247 bool sleep = false;
1248
1249 /* we might not have grabbed lo reference. so need to check under
1250 * i_lock */
1251 spin_lock(&ino->i_lock);
1252 lo = nfsi->layout;
1253 if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
1254 sleep = true;
1255 spin_unlock(&ino->i_lock);
1256
1257 if (sleep)
1258 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
1259
1260 return sleep;
1261}
1262
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001263/*
1264 * Compare two layout segments for sorting into layout cache.
1265 * We want to preferentially return RW over RO layouts, so ensure those
1266 * are seen first.
1267 */
1268static s64
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001269pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001270 const struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001271{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001272 s64 d;
1273
1274 /* high offset > low offset */
1275 d = l1->offset - l2->offset;
1276 if (d)
1277 return d;
1278
1279 /* short length > long length */
1280 d = l2->length - l1->length;
1281 if (d)
1282 return d;
1283
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001284 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +03001285 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001286}
1287
Trond Myklebust03772d22015-08-25 08:54:17 -04001288static bool
1289pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
1290 const struct pnfs_layout_range *l2)
Andy Adamson974cec82010-10-20 00:18:02 -04001291{
Trond Myklebust03772d22015-08-25 08:54:17 -04001292 return pnfs_lseg_range_cmp(l1, l2) > 0;
1293}
1294
1295static bool
1296pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
1297 struct pnfs_layout_segment *old)
1298{
1299 return false;
1300}
1301
1302void
1303pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1304 struct pnfs_layout_segment *lseg,
1305 bool (*is_after)(const struct pnfs_layout_range *,
1306 const struct pnfs_layout_range *),
1307 bool (*do_merge)(struct pnfs_layout_segment *,
1308 struct pnfs_layout_segment *),
1309 struct list_head *free_me)
1310{
1311 struct pnfs_layout_segment *lp, *tmp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001312
Andy Adamson974cec82010-10-20 00:18:02 -04001313 dprintk("%s:Begin\n", __func__);
1314
Trond Myklebust03772d22015-08-25 08:54:17 -04001315 list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
1316 if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
1317 continue;
1318 if (do_merge(lseg, lp)) {
1319 mark_lseg_invalid(lp, free_me);
1320 continue;
1321 }
1322 if (is_after(&lseg->pls_range, &lp->pls_range))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001323 continue;
Fred Isaman566052c2011-01-06 11:36:20 +00001324 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001325 dprintk("%s: inserted lseg %p "
1326 "iomode %d offset %llu length %llu before "
1327 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +00001328 __func__, lseg, lseg->pls_range.iomode,
1329 lseg->pls_range.offset, lseg->pls_range.length,
1330 lp, lp->pls_range.iomode, lp->pls_range.offset,
1331 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +03001332 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -04001333 }
Benny Halevyfb3296e2011-05-22 19:47:26 +03001334 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1335 dprintk("%s: inserted lseg %p "
1336 "iomode %d offset %llu length %llu at tail\n",
1337 __func__, lseg, lseg->pls_range.iomode,
1338 lseg->pls_range.offset, lseg->pls_range.length);
1339out:
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001340 pnfs_get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -04001341
1342 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001343}
Trond Myklebust03772d22015-08-25 08:54:17 -04001344EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
1345
1346static void
1347pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1348 struct pnfs_layout_segment *lseg,
1349 struct list_head *free_me)
1350{
1351 struct inode *inode = lo->plh_inode;
1352 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
1353
1354 if (ld->add_lseg != NULL)
1355 ld->add_lseg(lo, lseg, free_me);
1356 else
1357 pnfs_generic_layout_insert_lseg(lo, lseg,
1358 pnfs_lseg_range_is_after,
1359 pnfs_lseg_no_merge,
1360 free_me);
1361}
Benny Halevye5e94012010-10-20 00:18:01 -04001362
1363static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -04001364alloc_init_layout_hdr(struct inode *ino,
1365 struct nfs_open_context *ctx,
1366 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001367{
1368 struct pnfs_layout_hdr *lo;
1369
Benny Halevy636fb9c2011-05-22 19:51:33 +03001370 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -04001371 if (!lo)
1372 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +00001373 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001374 INIT_LIST_HEAD(&lo->plh_layouts);
1375 INIT_LIST_HEAD(&lo->plh_segs);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -05001376 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001377 lo->plh_inode = ino;
Trond Myklebust5cc22162013-05-21 09:26:49 -04001378 lo->plh_lc_cred = get_rpccred(ctx->cred);
Trond Myklebust67a3b722016-06-17 16:48:19 -04001379 lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
Benny Halevye5e94012010-10-20 00:18:01 -04001380 return lo;
1381}
1382
1383static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -04001384pnfs_find_alloc_layout(struct inode *ino,
1385 struct nfs_open_context *ctx,
1386 gfp_t gfp_flags)
Trond Myklebuste5241e42016-06-17 16:48:20 -04001387 __releases(&ino->i_lock)
1388 __acquires(&ino->i_lock)
Benny Halevye5e94012010-10-20 00:18:01 -04001389{
1390 struct nfs_inode *nfsi = NFS_I(ino);
1391 struct pnfs_layout_hdr *new = NULL;
1392
1393 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1394
Trond Myklebust251ec412012-10-02 15:41:05 -07001395 if (nfsi->layout != NULL)
1396 goto out_existing;
Benny Halevye5e94012010-10-20 00:18:01 -04001397 spin_unlock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001398 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -04001399 spin_lock(&ino->i_lock);
1400
Trond Myklebust251ec412012-10-02 15:41:05 -07001401 if (likely(nfsi->layout == NULL)) { /* Won the race? */
Benny Halevye5e94012010-10-20 00:18:01 -04001402 nfsi->layout = new;
Trond Myklebust251ec412012-10-02 15:41:05 -07001403 return new;
Yanchuan Nian7175fe92012-10-31 16:05:48 +08001404 } else if (new != NULL)
1405 pnfs_free_layout_hdr(new);
Trond Myklebust251ec412012-10-02 15:41:05 -07001406out_existing:
1407 pnfs_get_layout_hdr(nfsi->layout);
Benny Halevye5e94012010-10-20 00:18:01 -04001408 return nfsi->layout;
1409}
1410
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001411/*
1412 * iomode matching rules:
Tom Haynesc7d73af2016-05-25 07:31:14 -07001413 * iomode lseg strict match
1414 * iomode
1415 * ----- ----- ------ -----
1416 * ANY READ N/A true
1417 * ANY RW N/A true
1418 * RW READ N/A false
1419 * RW RW N/A true
1420 * READ READ N/A true
1421 * READ RW true false
1422 * READ RW false true
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001423 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001424static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001425pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001426 const struct pnfs_layout_range *range,
1427 bool strict_iomode)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001428{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001429 struct pnfs_layout_range range1;
1430
1431 if ((range->iomode == IOMODE_RW &&
1432 ls_range->iomode != IOMODE_RW) ||
Tom Haynesc7d73af2016-05-25 07:31:14 -07001433 (range->iomode != ls_range->iomode &&
1434 strict_iomode == true) ||
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001435 !pnfs_lseg_range_intersecting(ls_range, range))
Benny Halevyfb3296e2011-05-22 19:47:26 +03001436 return 0;
1437
1438 /* range1 covers only the first byte in the range */
1439 range1 = *range;
1440 range1.length = 1;
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001441 return pnfs_lseg_range_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001442}
1443
1444/*
1445 * lookup range in layout
1446 */
Benny Halevye5e94012010-10-20 00:18:01 -04001447static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +03001448pnfs_find_lseg(struct pnfs_layout_hdr *lo,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001449 struct pnfs_layout_range *range,
1450 bool strict_iomode)
Benny Halevye5e94012010-10-20 00:18:01 -04001451{
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001452 struct pnfs_layout_segment *lseg, *ret = NULL;
1453
1454 dprintk("%s:Begin\n", __func__);
1455
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001456 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +00001457 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Peng Taoce6ab4f2014-09-06 00:53:24 +08001458 !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
Tom Haynesc7d73af2016-05-25 07:31:14 -07001459 pnfs_lseg_range_match(&lseg->pls_range, range,
1460 strict_iomode)) {
Trond Myklebust9369a432012-09-18 20:57:08 -04001461 ret = pnfs_get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001462 break;
1463 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001464 }
1465
1466 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +00001467 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001468 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -04001469}
1470
1471/*
Andy Adamsond23d61c2012-05-23 05:02:37 -04001472 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1473 * to the MDS or over pNFS
1474 *
1475 * The nfs_inode read_io and write_io fields are cumulative counters reset
1476 * when there are no layout segments. Note that in pnfs_update_layout iomode
1477 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1478 * WRITE request.
1479 *
1480 * A return of true means use MDS I/O.
1481 *
1482 * From rfc 5661:
1483 * If a file's size is smaller than the file size threshold, data accesses
1484 * SHOULD be sent to the metadata server. If an I/O request has a length that
1485 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1486 * server. If both file size and I/O size are provided, the client SHOULD
1487 * reach or exceed both thresholds before sending its read or write
1488 * requests to the data server.
1489 */
1490static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1491 struct inode *ino, int iomode)
1492{
1493 struct nfs4_threshold *t = ctx->mdsthreshold;
1494 struct nfs_inode *nfsi = NFS_I(ino);
1495 loff_t fsize = i_size_read(ino);
1496 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1497
1498 if (t == NULL)
1499 return ret;
1500
1501 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1502 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1503
1504 switch (iomode) {
1505 case IOMODE_READ:
1506 if (t->bm & THRESHOLD_RD) {
1507 dprintk("%s fsize %llu\n", __func__, fsize);
1508 size_set = true;
1509 if (fsize < t->rd_sz)
1510 size = true;
1511 }
1512 if (t->bm & THRESHOLD_RD_IO) {
1513 dprintk("%s nfsi->read_io %llu\n", __func__,
1514 nfsi->read_io);
1515 io_set = true;
1516 if (nfsi->read_io < t->rd_io_sz)
1517 io = true;
1518 }
1519 break;
1520 case IOMODE_RW:
1521 if (t->bm & THRESHOLD_WR) {
1522 dprintk("%s fsize %llu\n", __func__, fsize);
1523 size_set = true;
1524 if (fsize < t->wr_sz)
1525 size = true;
1526 }
1527 if (t->bm & THRESHOLD_WR_IO) {
1528 dprintk("%s nfsi->write_io %llu\n", __func__,
1529 nfsi->write_io);
1530 io_set = true;
1531 if (nfsi->write_io < t->wr_io_sz)
1532 io = true;
1533 }
1534 break;
1535 }
1536 if (size_set && io_set) {
1537 if (size && io)
1538 ret = true;
1539 } else if (size || io)
1540 ret = true;
1541
1542 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1543 return ret;
1544}
1545
Peng Taoaa8a45e2014-12-01 08:22:23 +08001546static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1547{
1548 /*
1549 * send layoutcommit as it can hold up layoutreturn due to lseg
1550 * reference
1551 */
1552 pnfs_layoutcommit_inode(lo->plh_inode, false);
1553 return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
Trond Myklebust2e5b29f2015-12-14 16:25:11 -05001554 nfs_wait_bit_killable,
Peng Taoaa8a45e2014-12-01 08:22:23 +08001555 TASK_UNINTERRUPTIBLE);
1556}
1557
Tom Haynesd67ae822014-12-11 17:02:04 -05001558static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1559{
1560 unsigned long *bitlock = &lo->plh_flags;
1561
1562 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1563 smp_mb__after_atomic();
1564 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1565}
1566
Andy Adamsond23d61c2012-05-23 05:02:37 -04001567/*
Benny Halevye5e94012010-10-20 00:18:01 -04001568 * Layout segment is retreived from the server if not cached.
1569 * The appropriate layout segment is referenced and returned to the caller.
1570 */
Andy Adamson7c24d942011-06-13 18:22:38 -04001571struct pnfs_layout_segment *
Benny Halevye5e94012010-10-20 00:18:01 -04001572pnfs_update_layout(struct inode *ino,
1573 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +03001574 loff_t pos,
1575 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001576 enum pnfs_iomode iomode,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001577 bool strict_iomode,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001578 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001579{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001580 struct pnfs_layout_range arg = {
1581 .iomode = iomode,
1582 .offset = pos,
1583 .length = count,
1584 };
Jeff Layton183d9e72016-05-17 12:28:47 -04001585 unsigned pg_offset, seq;
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001586 struct nfs_server *server = NFS_SERVER(ino);
1587 struct nfs_client *clp = server->nfs_client;
Jeff Layton183d9e72016-05-17 12:28:47 -04001588 struct pnfs_layout_hdr *lo = NULL;
Benny Halevye5e94012010-10-20 00:18:01 -04001589 struct pnfs_layout_segment *lseg = NULL;
Jeff Layton183d9e72016-05-17 12:28:47 -04001590 nfs4_stateid stateid;
1591 long timeout = 0;
Trond Myklebust66b53f32016-07-14 14:28:31 -04001592 unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
Weston Andros Adamson30005122013-02-28 20:30:10 -05001593 bool first;
Benny Halevye5e94012010-10-20 00:18:01 -04001594
Jeff Layton9a4bf312015-12-10 10:41:58 -05001595 if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001596 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001597 PNFS_UPDATE_LAYOUT_NO_PNFS);
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001598 goto out;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001599 }
Andy Adamsond23d61c2012-05-23 05:02:37 -04001600
Jeff Layton9a4bf312015-12-10 10:41:58 -05001601 if (iomode == IOMODE_READ && i_size_read(ino) == 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001602 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001603 PNFS_UPDATE_LAYOUT_RD_ZEROLEN);
Trond Myklebust4ae935602015-08-31 01:25:11 -07001604 goto out;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001605 }
Trond Myklebust4ae935602015-08-31 01:25:11 -07001606
Jeff Layton9a4bf312015-12-10 10:41:58 -05001607 if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001608 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001609 PNFS_UPDATE_LAYOUT_MDSTHRESH);
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001610 goto out;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001611 }
Andy Adamsond23d61c2012-05-23 05:02:37 -04001612
Peng Tao9bf87482014-08-22 17:37:41 +08001613lookup_again:
Trond Myklebustb88fa692016-08-23 11:19:33 -04001614 nfs4_client_recover_expired_lease(clp);
Peng Tao9bf87482014-08-22 17:37:41 +08001615 first = false;
Benny Halevye5e94012010-10-20 00:18:01 -04001616 spin_lock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001617 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001618 if (lo == NULL) {
1619 spin_unlock(&ino->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04001620 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001621 PNFS_UPDATE_LAYOUT_NOMEM);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001622 goto out;
1623 }
Benny Halevye5e94012010-10-20 00:18:01 -04001624
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001625 /* Do we even need to bother with this? */
Trond Myklebusta59c30a2012-03-01 11:17:47 -05001626 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001627 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001628 PNFS_UPDATE_LAYOUT_BULK_RECALL);
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001629 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001630 goto out_unlock;
1631 }
1632
1633 /* if LAYOUTGET already failed once we don't try again */
Trond Myklebust2e5b29f2015-12-14 16:25:11 -05001634 if (pnfs_layout_io_test_failed(lo, iomode)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001635 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001636 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
Benny Halevye5e94012010-10-20 00:18:01 -04001637 goto out_unlock;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001638 }
Benny Halevye5e94012010-10-20 00:18:01 -04001639
Tom Haynesc7d73af2016-05-25 07:31:14 -07001640 lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
Jeff Layton183d9e72016-05-17 12:28:47 -04001641 if (lseg) {
1642 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1643 PNFS_UPDATE_LAYOUT_FOUND_CACHED);
1644 goto out_unlock;
1645 }
1646
1647 if (!nfs4_valid_open_stateid(ctx->state)) {
1648 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1649 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
1650 goto out_unlock;
1651 }
1652
1653 /*
1654 * Choose a stateid for the LAYOUTGET. If we don't have a layout
1655 * stateid, or it has been invalidated, then we must use the open
1656 * stateid.
1657 */
Trond Myklebust67a3b722016-06-17 16:48:19 -04001658 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001659
1660 /*
1661 * The first layoutget for the file. Need to serialize per
Peng Tao9bf87482014-08-22 17:37:41 +08001662 * RFC 5661 Errata 3208.
1663 */
1664 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1665 &lo->plh_flags)) {
1666 spin_unlock(&ino->i_lock);
1667 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
1668 TASK_UNINTERRUPTIBLE);
1669 pnfs_put_layout_hdr(lo);
Jeff Layton183d9e72016-05-17 12:28:47 -04001670 dprintk("%s retrying\n", __func__);
Peng Tao9bf87482014-08-22 17:37:41 +08001671 goto lookup_again;
1672 }
Jeff Layton183d9e72016-05-17 12:28:47 -04001673
1674 first = true;
1675 do {
1676 seq = read_seqbegin(&ctx->state->seqlock);
1677 nfs4_stateid_copy(&stateid, &ctx->state->stateid);
1678 } while (read_seqretry(&ctx->state->seqlock, seq));
Peng Tao9bf87482014-08-22 17:37:41 +08001679 } else {
Jeff Layton183d9e72016-05-17 12:28:47 -04001680 nfs4_stateid_copy(&stateid, &lo->plh_stateid);
Peng Tao9bf87482014-08-22 17:37:41 +08001681 }
Andy Adamson568e8c42011-03-01 01:34:22 +00001682
Peng Taoaa8a45e2014-12-01 08:22:23 +08001683 /*
1684 * Because we free lsegs before sending LAYOUTRETURN, we need to wait
1685 * for LAYOUTRETURN even if first is true.
1686 */
Trond Myklebust8f70f532015-08-04 16:09:44 -04001687 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
Peng Taoaa8a45e2014-12-01 08:22:23 +08001688 spin_unlock(&ino->i_lock);
1689 dprintk("%s wait for layoutreturn\n", __func__);
1690 if (pnfs_prepare_to_retry_layoutget(lo)) {
Tom Haynesd67ae822014-12-11 17:02:04 -05001691 if (first)
1692 pnfs_clear_first_layoutget(lo);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001693 pnfs_put_layout_hdr(lo);
1694 dprintk("%s retrying\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04001695 trace_pnfs_update_layout(ino, pos, count, iomode, lo,
1696 lseg, PNFS_UPDATE_LAYOUT_RETRY);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001697 goto lookup_again;
1698 }
Jeff Layton183d9e72016-05-17 12:28:47 -04001699 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001700 PNFS_UPDATE_LAYOUT_RETURN);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001701 goto out_put_layout_hdr;
1702 }
1703
Jeff Layton9a4bf312015-12-10 10:41:58 -05001704 if (pnfs_layoutgets_blocked(lo)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001705 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001706 PNFS_UPDATE_LAYOUT_BLOCKED);
Fred Isamancf7d63f2011-01-06 11:36:25 +00001707 goto out_unlock;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001708 }
Fred Isamancf7d63f2011-01-06 11:36:25 +00001709 atomic_inc(&lo->plh_outstanding);
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001710 spin_unlock(&ino->i_lock);
Weston Andros Adamson30005122013-02-28 20:30:10 -05001711
Peng Taoabb9a002014-08-22 17:37:40 +08001712 if (list_empty(&lo->plh_layouts)) {
Fred Isaman2130ff62011-01-06 11:36:26 +00001713 /* The lo must be on the clp list if there is any
1714 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1715 */
1716 spin_lock(&clp->cl_lock);
Peng Taoabb9a002014-08-22 17:37:40 +08001717 if (list_empty(&lo->plh_layouts))
1718 list_add_tail(&lo->plh_layouts, &server->layouts);
Fred Isaman2130ff62011-01-06 11:36:26 +00001719 spin_unlock(&clp->cl_lock);
1720 }
Benny Halevye5e94012010-10-20 00:18:01 -04001721
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001722 pg_offset = arg.offset & ~PAGE_MASK;
Benny Halevy707ed5f2011-05-22 19:47:46 +03001723 if (pg_offset) {
1724 arg.offset -= pg_offset;
1725 arg.length += pg_offset;
1726 }
Andy Adamson7c24d942011-06-13 18:22:38 -04001727 if (arg.length != NFS4_MAX_UINT64)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001728 arg.length = PAGE_ALIGN(arg.length);
Benny Halevy707ed5f2011-05-22 19:47:46 +03001729
Jeff Layton183d9e72016-05-17 12:28:47 -04001730 lseg = send_layoutget(lo, ctx, &stateid, &arg, &timeout, gfp_flags);
1731 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1732 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
Trond Myklebust56b38a12016-07-14 18:34:12 -04001733 atomic_dec(&lo->plh_outstanding);
Jeff Layton83026d802016-05-17 12:28:46 -04001734 if (IS_ERR(lseg)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001735 switch(PTR_ERR(lseg)) {
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04001736 case -EBUSY:
Jeff Layton183d9e72016-05-17 12:28:47 -04001737 if (time_after(jiffies, giveup))
1738 lseg = NULL;
Trond Myklebust66b53f32016-07-14 14:28:31 -04001739 break;
1740 case -ERECALLCONFLICT:
1741 /* Huh? We hold no layouts, how is there a recall? */
1742 if (first) {
1743 lseg = NULL;
1744 break;
1745 }
1746 /* Destroy the existing layout and start over */
1747 if (time_after(jiffies, giveup))
1748 pnfs_destroy_layout(NFS_I(ino));
Jeff Layton183d9e72016-05-17 12:28:47 -04001749 /* Fallthrough */
1750 case -EAGAIN:
Trond Myklebust56b38a12016-07-14 18:34:12 -04001751 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04001752 default:
1753 if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
1754 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1755 lseg = NULL;
1756 }
Trond Myklebust56b38a12016-07-14 18:34:12 -04001757 goto out_put_layout_hdr;
1758 }
1759 if (lseg) {
1760 if (first)
1761 pnfs_clear_first_layoutget(lo);
1762 trace_pnfs_update_layout(ino, pos, count,
1763 iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
1764 pnfs_put_layout_hdr(lo);
1765 goto lookup_again;
Jeff Layton83026d802016-05-17 12:28:46 -04001766 }
1767 } else {
1768 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1769 }
1770
Trond Myklebust830ffb52012-09-20 21:25:19 -04001771out_put_layout_hdr:
Tom Haynesd67ae822014-12-11 17:02:04 -05001772 if (first)
1773 pnfs_clear_first_layoutget(lo);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001774 pnfs_put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -04001775out:
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001776 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1777 "(%s, offset: %llu, length: %llu)\n",
1778 __func__, ino->i_sb->s_id,
1779 (unsigned long long)NFS_FILEID(ino),
Peng Taod600ad12015-12-04 02:57:48 +08001780 IS_ERR_OR_NULL(lseg) ? "not found" : "found",
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001781 iomode==IOMODE_RW ? "read/write" : "read-only",
1782 (unsigned long long)pos,
1783 (unsigned long long)count);
Benny Halevye5e94012010-10-20 00:18:01 -04001784 return lseg;
1785out_unlock:
1786 spin_unlock(&ino->i_lock);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001787 goto out_put_layout_hdr;
Benny Halevye5e94012010-10-20 00:18:01 -04001788}
Andy Adamson7c24d942011-06-13 18:22:38 -04001789EXPORT_SYMBOL_GPL(pnfs_update_layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001790
Trond Myklebust540d9862015-08-25 11:16:13 -04001791static bool
1792pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
1793{
1794 switch (range->iomode) {
1795 case IOMODE_READ:
1796 case IOMODE_RW:
1797 break;
1798 default:
1799 return false;
1800 }
1801 if (range->offset == NFS4_MAX_UINT64)
1802 return false;
1803 if (range->length == 0)
1804 return false;
1805 if (range->length != NFS4_MAX_UINT64 &&
1806 range->length > NFS4_MAX_UINT64 - range->offset)
1807 return false;
1808 return true;
1809}
1810
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001811struct pnfs_layout_segment *
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001812pnfs_layout_process(struct nfs4_layoutget *lgp)
1813{
1814 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1815 struct nfs4_layoutget_res *res = &lgp->res;
1816 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001817 struct inode *ino = lo->plh_inode;
Trond Myklebust78096cc2014-02-12 10:02:27 -05001818 LIST_HEAD(free_me);
Trond Myklebust540d9862015-08-25 11:16:13 -04001819
1820 if (!pnfs_sanity_check_layout_range(&res->range))
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001821 return ERR_PTR(-EINVAL);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001822
1823 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001824 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001825 if (IS_ERR_OR_NULL(lseg)) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001826 if (!lseg)
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001827 lseg = ERR_PTR(-ENOMEM);
1828
1829 dprintk("%s: Could not allocate layout: error %ld\n",
1830 __func__, PTR_ERR(lseg));
1831 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001832 }
1833
Trond Myklebust119cef972016-07-24 15:10:12 -04001834 pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
Christoph Hellwig1013df612014-08-21 11:09:18 -05001835
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001836 spin_lock(&ino->i_lock);
Trond Myklebuste1c06f82015-08-04 16:40:08 -04001837 if (pnfs_layoutgets_blocked(lo)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001838 dprintk("%s forget reply due to state\n", __func__);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001839 goto out_forget;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001840 }
Trond Myklebust038d6492012-10-02 16:38:41 -07001841
Christoph Hellwig362f7472014-08-21 11:09:20 -05001842 if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
1843 /* existing state ID, make sure the sequence number matches. */
1844 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1845 dprintk("%s forget reply due to sequence\n", __func__);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001846 goto out_forget;
Christoph Hellwig362f7472014-08-21 11:09:20 -05001847 }
1848 pnfs_set_layout_stateid(lo, &res->stateid, false);
1849 } else {
1850 /*
1851 * We got an entirely new state ID. Mark all segments for the
1852 * inode invalid, and don't bother validating the stateid
1853 * sequence number.
1854 */
Trond Myklebustd9b61702016-07-24 15:04:07 -04001855 pnfs_mark_layout_stateid_invalid(lo, &free_me);
Christoph Hellwig362f7472014-08-21 11:09:20 -05001856
Trond Myklebust2a59a042016-09-03 11:20:04 -04001857 pnfs_set_layout_stateid(lo, &res->stateid, true);
Christoph Hellwig362f7472014-08-21 11:09:20 -05001858 }
Trond Myklebust038d6492012-10-02 16:38:41 -07001859
Trond Myklebust9369a432012-09-18 20:57:08 -04001860 pnfs_get_lseg(lseg);
Trond Myklebust03772d22015-08-25 08:54:17 -04001861 pnfs_layout_insert_lseg(lo, lseg, &free_me);
Trond Myklebust8e0acf92016-07-21 11:53:29 -04001862
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001863
Peng Tao39761432015-08-21 12:49:44 +08001864 if (res->return_on_close)
Fred Isamanf7e89172011-01-06 11:36:32 +00001865 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
Fred Isamanf7e89172011-01-06 11:36:32 +00001866
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001867 spin_unlock(&ino->i_lock);
Trond Myklebust78096cc2014-02-12 10:02:27 -05001868 pnfs_free_lseg_list(&free_me);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001869 return lseg;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001870
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001871out_forget:
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001872 spin_unlock(&ino->i_lock);
1873 lseg->pls_layout = lo;
1874 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001875 return ERR_PTR(-EAGAIN);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001876}
1877
Peng Tao016256d2014-09-06 00:53:23 +08001878static void
Jeff Layton3982a6a2016-05-17 12:28:41 -04001879pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
1880 u32 seq)
Trond Myklebust5c97f5d2016-01-04 11:53:04 -05001881{
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001882 if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
Trond Myklebust5c97f5d2016-01-04 11:53:04 -05001883 iomode = IOMODE_ANY;
1884 lo->plh_return_iomode = iomode;
Trond Myklebuste0fa0d02016-02-15 12:56:17 -05001885 set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001886 if (seq != 0) {
1887 WARN_ON_ONCE(lo->plh_return_seq != 0 && lo->plh_return_seq != seq);
Jeff Layton3982a6a2016-05-17 12:28:41 -04001888 lo->plh_return_seq = seq;
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001889 }
Trond Myklebust5c97f5d2016-01-04 11:53:04 -05001890}
1891
Trond Myklebust2f215962016-02-15 12:36:04 -05001892/**
1893 * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
1894 * @lo: pointer to layout header
1895 * @tmp_list: list header to be used with pnfs_free_lseg_list()
1896 * @return_range: describe layout segment ranges to be returned
1897 *
1898 * This function is mainly intended for use by layoutrecall. It attempts
1899 * to free the layout segment immediately, or else to mark it for return
1900 * as soon as its reference count drops to zero.
1901 */
Trond Myklebust10335552016-01-04 11:23:52 -05001902int
Peng Tao016256d2014-09-06 00:53:23 +08001903pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
1904 struct list_head *tmp_list,
Jeff Layton6d597e12016-05-17 12:28:42 -04001905 const struct pnfs_layout_range *return_range,
1906 u32 seq)
Peng Tao016256d2014-09-06 00:53:23 +08001907{
1908 struct pnfs_layout_segment *lseg, *next;
Trond Myklebust10335552016-01-04 11:23:52 -05001909 int remaining = 0;
Peng Tao016256d2014-09-06 00:53:23 +08001910
1911 dprintk("%s:Begin lo %p\n", __func__, lo);
1912
1913 if (list_empty(&lo->plh_segs))
Trond Myklebust10335552016-01-04 11:23:52 -05001914 return 0;
Peng Tao016256d2014-09-06 00:53:23 +08001915
Trond Myklebustfc7ff362015-12-28 10:28:59 -05001916 assert_spin_locked(&lo->plh_inode->i_lock);
Peng Tao016256d2014-09-06 00:53:23 +08001917
1918 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Trond Myklebuste036f462016-07-22 11:13:22 -04001919 if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
Peng Tao016256d2014-09-06 00:53:23 +08001920 dprintk("%s: marking lseg %p iomode %d "
1921 "offset %llu length %llu\n", __func__,
1922 lseg, lseg->pls_range.iomode,
1923 lseg->pls_range.offset,
1924 lseg->pls_range.length);
Trond Myklebust2f215962016-02-15 12:36:04 -05001925 if (mark_lseg_invalid(lseg, tmp_list))
1926 continue;
1927 remaining++;
Peng Tao016256d2014-09-06 00:53:23 +08001928 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
Peng Tao016256d2014-09-06 00:53:23 +08001929 }
Jeff Layton6d597e12016-05-17 12:28:42 -04001930
1931 if (remaining)
1932 pnfs_set_plh_return_info(lo, return_range->iomode, seq);
1933
Trond Myklebust10335552016-01-04 11:23:52 -05001934 return remaining;
Peng Tao016256d2014-09-06 00:53:23 +08001935}
1936
1937void pnfs_error_mark_layout_for_return(struct inode *inode,
1938 struct pnfs_layout_segment *lseg)
1939{
1940 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
Peng Tao016256d2014-09-06 00:53:23 +08001941 struct pnfs_layout_range range = {
1942 .iomode = lseg->pls_range.iomode,
1943 .offset = 0,
1944 .length = NFS4_MAX_UINT64,
1945 };
1946 LIST_HEAD(free_me);
Trond Myklebust10335552016-01-04 11:23:52 -05001947 bool return_now = false;
Peng Tao016256d2014-09-06 00:53:23 +08001948
1949 spin_lock(&inode->i_lock);
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001950 pnfs_set_plh_return_info(lo, range.iomode, 0);
Peng Tao016256d2014-09-06 00:53:23 +08001951 /*
1952 * mark all matching lsegs so that we are sure to have no live
1953 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
1954 * for how it works.
1955 */
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001956 if (!pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0)) {
Trond Myklebust10335552016-01-04 11:23:52 -05001957 nfs4_stateid stateid;
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001958 enum pnfs_iomode iomode;
Trond Myklebust10335552016-01-04 11:23:52 -05001959
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001960 return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
Trond Myklebust10335552016-01-04 11:23:52 -05001961 spin_unlock(&inode->i_lock);
1962 if (return_now)
1963 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1964 } else {
1965 spin_unlock(&inode->i_lock);
1966 nfs_commit_inode(inode, 0);
1967 }
Peng Tao016256d2014-09-06 00:53:23 +08001968 pnfs_free_lseg_list(&free_me);
1969}
1970EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
1971
Trond Myklebustd8007d42011-06-10 13:30:23 -04001972void
1973pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1974{
Peng Tao1fd937b2012-09-25 14:55:57 +08001975 u64 rd_size = req->wb_bytes;
1976
Peng Taocb5d04b2015-01-24 22:14:52 +08001977 if (pgio->pg_lseg == NULL) {
1978 if (pgio->pg_dreq == NULL)
1979 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
1980 else
1981 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
Trond Myklebustd8007d42011-06-10 13:30:23 -04001982
Peng Taocb5d04b2015-01-24 22:14:52 +08001983 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1984 req->wb_context,
1985 req_offset(req),
1986 rd_size,
1987 IOMODE_READ,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001988 false,
Peng Taocb5d04b2015-01-24 22:14:52 +08001989 GFP_KERNEL);
Peng Taod600ad12015-12-04 02:57:48 +08001990 if (IS_ERR(pgio->pg_lseg)) {
1991 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
1992 pgio->pg_lseg = NULL;
1993 return;
1994 }
Peng Taocb5d04b2015-01-24 22:14:52 +08001995 }
Trond Myklebuste885de12011-06-10 13:30:23 -04001996 /* If no lseg, fall back to read through mds */
1997 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001998 nfs_pageio_reset_read_mds(pgio);
Trond Myklebuste885de12011-06-10 13:30:23 -04001999
Trond Myklebustd8007d42011-06-10 13:30:23 -04002000}
2001EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
2002
2003void
Peng Tao62965562012-09-25 14:55:57 +08002004pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
2005 struct nfs_page *req, u64 wb_size)
Trond Myklebustd8007d42011-06-10 13:30:23 -04002006{
Peng Taod600ad12015-12-04 02:57:48 +08002007 if (pgio->pg_lseg == NULL) {
Peng Taocb5d04b2015-01-24 22:14:52 +08002008 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2009 req->wb_context,
2010 req_offset(req),
2011 wb_size,
2012 IOMODE_RW,
Tom Haynesc7d73af2016-05-25 07:31:14 -07002013 false,
Peng Taocb5d04b2015-01-24 22:14:52 +08002014 GFP_NOFS);
Peng Taod600ad12015-12-04 02:57:48 +08002015 if (IS_ERR(pgio->pg_lseg)) {
2016 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2017 pgio->pg_lseg = NULL;
2018 return;
2019 }
2020 }
Trond Myklebuste885de12011-06-10 13:30:23 -04002021 /* If no lseg, fall back to write through mds */
2022 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04002023 nfs_pageio_reset_write_mds(pgio);
Trond Myklebustd8007d42011-06-10 13:30:23 -04002024}
2025EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
2026
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002027void
2028pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
2029{
2030 if (desc->pg_lseg) {
2031 pnfs_put_lseg(desc->pg_lseg);
2032 desc->pg_lseg = NULL;
2033 }
2034}
2035EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
2036
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -04002037/*
2038 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
2039 * of bytes (maximum @req->wb_bytes) that can be coalesced.
2040 */
2041size_t
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002042pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
2043 struct nfs_page *prev, struct nfs_page *req)
Fred Isamanbae724e2011-03-01 01:34:15 +00002044{
Weston Andros Adamson0f9c429e2014-05-15 11:56:51 -04002045 unsigned int size;
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002046 u64 seg_end, req_start, seg_left;
Weston Andros Adamson0f9c429e2014-05-15 11:56:51 -04002047
2048 size = nfs_generic_pg_test(pgio, prev, req);
Weston Andros Adamson0f9c429e2014-05-15 11:56:51 -04002049 if (!size)
2050 return 0;
Benny Halevy89a58e32011-05-25 20:54:40 +03002051
Trond Myklebust19982ba2011-06-10 13:30:23 -04002052 /*
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002053 * 'size' contains the number of bytes left in the current page (up
2054 * to the original size asked for in @req->wb_bytes).
2055 *
2056 * Calculate how many bytes are left in the layout segment
2057 * and if there are less bytes than 'size', return that instead.
Trond Myklebust19982ba2011-06-10 13:30:23 -04002058 *
2059 * Please also note that 'end_offset' is actually the offset of the
2060 * first byte that lies outside the pnfs_layout_range. FIXME?
2061 *
2062 */
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002063 if (pgio->pg_lseg) {
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002064 seg_end = end_offset(pgio->pg_lseg->pls_range.offset,
2065 pgio->pg_lseg->pls_range.length);
2066 req_start = req_offset(req);
Weston Andros Adamson7c137892015-01-30 11:01:02 -05002067 WARN_ON_ONCE(req_start >= seg_end);
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002068 /* start of request is past the last byte of this segment */
Weston Andros Adamson7c137892015-01-30 11:01:02 -05002069 if (req_start >= seg_end) {
2070 /* reference the new lseg */
2071 if (pgio->pg_ops->pg_cleanup)
2072 pgio->pg_ops->pg_cleanup(pgio);
2073 if (pgio->pg_ops->pg_init)
2074 pgio->pg_ops->pg_init(pgio, req);
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002075 return 0;
Weston Andros Adamson7c137892015-01-30 11:01:02 -05002076 }
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002077
2078 /* adjust 'size' iff there are fewer bytes left in the
2079 * segment than what nfs_generic_pg_test returned */
2080 seg_left = seg_end - req_start;
2081 if (seg_left < size)
2082 size = (unsigned int)seg_left;
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002083 }
Weston Andros Adamson0f9c429e2014-05-15 11:56:51 -04002084
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002085 return size;
Fred Isamanbae724e2011-03-01 01:34:15 +00002086}
Benny Halevy89a58e32011-05-25 20:54:40 +03002087EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
Fred Isamanbae724e2011-03-01 01:34:15 +00002088
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002089int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002090{
2091 struct nfs_pageio_descriptor pgio;
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002092
2093 /* Resend all requests through the MDS */
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002094 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
2095 hdr->completion_ops);
Trond Myklebustc7070112015-06-17 19:56:22 -04002096 set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags);
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002097 return nfs_pageio_resend(&pgio, hdr);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002098}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04002099EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002100
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002101static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002102{
Fred Isamancd841602012-04-20 14:47:44 -04002103
2104 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
2105 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002106 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04002107 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002108 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04002109 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002110 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002111}
2112
Benny Halevyd20581a2011-05-22 19:52:03 +03002113/*
2114 * Called by non rpc-based layout drivers
2115 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002116void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
Fred Isaman94ad1c82011-03-01 01:34:14 +00002117{
Kinglong Meef8417b482015-10-16 17:23:29 +08002118 if (likely(!hdr->pnfs_error)) {
Trond Myklebust67af7612015-03-25 20:40:38 -04002119 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
2120 hdr->mds_offset + hdr->res.count);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002121 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
Kinglong Meef8417b482015-10-16 17:23:29 +08002122 }
2123 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
2124 if (unlikely(hdr->pnfs_error))
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002125 pnfs_ld_handle_write_error(hdr);
2126 hdr->mds_ops->rpc_release(hdr);
Fred Isaman44b83792011-03-03 15:13:44 +00002127}
Benny Halevyd20581a2011-05-22 19:52:03 +03002128EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
Fred Isaman44b83792011-03-03 15:13:44 +00002129
Trond Myklebustdce81292011-07-13 15:59:19 -04002130static void
2131pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002132 struct nfs_pgio_header *hdr)
Trond Myklebustdce81292011-07-13 15:59:19 -04002133{
Peng Tao48d635f2014-11-10 08:35:35 +08002134 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002135
Fred Isaman6c75dc02012-04-20 14:47:47 -04002136 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002137 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
Fred Isaman6c75dc02012-04-20 14:47:47 -04002138 nfs_pageio_reset_write_mds(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002139 mirror->pg_recoalesce = 1;
Fred Isaman6c75dc02012-04-20 14:47:47 -04002140 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002141 nfs_pgio_data_destroy(hdr);
Trond Myklebust1ca018d2015-06-17 19:41:51 -04002142 hdr->release(hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04002143}
2144
2145static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002146pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
Trond Myklebustdce81292011-07-13 15:59:19 -04002147 const struct rpc_call_ops *call_ops,
2148 struct pnfs_layout_segment *lseg,
2149 int how)
Andy Adamson0382b742011-03-03 15:13:45 +00002150{
Fred Isamancd841602012-04-20 14:47:44 -04002151 struct inode *inode = hdr->inode;
Andy Adamson0382b742011-03-03 15:13:45 +00002152 enum pnfs_try_status trypnfs;
2153 struct nfs_server *nfss = NFS_SERVER(inode);
2154
Fred Isamancd841602012-04-20 14:47:44 -04002155 hdr->mds_ops = call_ops;
Andy Adamson0382b742011-03-03 15:13:45 +00002156
2157 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002158 inode->i_ino, hdr->args.count, hdr->args.offset, how);
2159 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
Fred Isaman6c75dc02012-04-20 14:47:47 -04002160 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson0382b742011-03-03 15:13:45 +00002161 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
Andy Adamson0382b742011-03-03 15:13:45 +00002162 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2163 return trypnfs;
2164}
2165
Trond Myklebustdce81292011-07-13 15:59:19 -04002166static void
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002167pnfs_do_write(struct nfs_pageio_descriptor *desc,
2168 struct nfs_pgio_header *hdr, int how)
Trond Myklebustdce81292011-07-13 15:59:19 -04002169{
Trond Myklebustdce81292011-07-13 15:59:19 -04002170 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2171 struct pnfs_layout_segment *lseg = desc->pg_lseg;
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002172 enum pnfs_try_status trypnfs;
Trond Myklebustdce81292011-07-13 15:59:19 -04002173
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002174 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002175 if (trypnfs == PNFS_NOT_ATTEMPTED)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002176 pnfs_write_through_mds(desc, hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04002177}
2178
Fred Isaman6c75dc02012-04-20 14:47:47 -04002179static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
2180{
Trond Myklebust9369a432012-09-18 20:57:08 -04002181 pnfs_put_lseg(hdr->lseg);
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002182 nfs_pgio_header_free(hdr);
Fred Isaman6c75dc02012-04-20 14:47:47 -04002183}
2184
Trond Myklebustdce81292011-07-13 15:59:19 -04002185int
2186pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
2187{
Fred Isaman6c75dc02012-04-20 14:47:47 -04002188 struct nfs_pgio_header *hdr;
Trond Myklebustdce81292011-07-13 15:59:19 -04002189 int ret;
2190
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002191 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2192 if (!hdr) {
Peng Tao2bff2282015-12-05 02:03:17 +08002193 desc->pg_error = -ENOMEM;
2194 return desc->pg_error;
Fred Isaman6c75dc02012-04-20 14:47:47 -04002195 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04002196 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002197
Trond Myklebust9369a432012-09-18 20:57:08 -04002198 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Anna Schumakeref2c4882014-05-06 09:12:36 -04002199 ret = nfs_generic_pgio(desc, hdr);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002200 if (!ret)
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002201 pnfs_do_write(desc, hdr, desc->pg_ioflags);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002202
Fred Isaman6c75dc02012-04-20 14:47:47 -04002203 return ret;
Trond Myklebustdce81292011-07-13 15:59:19 -04002204}
2205EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
2206
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002207int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
Trond Myklebust62e4a762011-11-10 14:30:37 -05002208{
2209 struct nfs_pageio_descriptor pgio;
2210
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002211 /* Resend all requests through the MDS */
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002212 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
2213 return nfs_pageio_resend(&pgio, hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002214}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04002215EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002216
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002217static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002218{
Fred Isamancd841602012-04-20 14:47:44 -04002219 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
2220 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002221 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04002222 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002223 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002224 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002225 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
Trond Myklebust62e4a762011-11-10 14:30:37 -05002226}
2227
Andy Adamsonb1f69b72010-10-20 00:18:03 -04002228/*
Benny Halevyd20581a2011-05-22 19:52:03 +03002229 * Called by non rpc-based layout drivers
2230 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002231void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03002232{
Trond Myklebustbfc505d2016-09-15 18:26:05 -04002233 if (likely(!hdr->pnfs_error))
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002234 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
Kinglong Meef8417b482015-10-16 17:23:29 +08002235 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
2236 if (unlikely(hdr->pnfs_error))
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002237 pnfs_ld_handle_read_error(hdr);
2238 hdr->mds_ops->rpc_release(hdr);
Benny Halevyd20581a2011-05-22 19:52:03 +03002239}
2240EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
2241
Trond Myklebust493292d2011-07-13 15:58:28 -04002242static void
2243pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002244 struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -04002245{
Peng Tao48d635f2014-11-10 08:35:35 +08002246 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002247
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002248 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002249 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002250 nfs_pageio_reset_read_mds(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002251 mirror->pg_recoalesce = 1;
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002252 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002253 nfs_pgio_data_destroy(hdr);
Trond Myklebust1ca018d2015-06-17 19:41:51 -04002254 hdr->release(hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -04002255}
2256
Benny Halevyd20581a2011-05-22 19:52:03 +03002257/*
Andy Adamson64419a92011-03-01 01:34:16 +00002258 * Call the appropriate parallel I/O subsystem read function.
2259 */
Trond Myklebust493292d2011-07-13 15:58:28 -04002260static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002261pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
Trond Myklebust493292d2011-07-13 15:58:28 -04002262 const struct rpc_call_ops *call_ops,
2263 struct pnfs_layout_segment *lseg)
Andy Adamson64419a92011-03-01 01:34:16 +00002264{
Fred Isamancd841602012-04-20 14:47:44 -04002265 struct inode *inode = hdr->inode;
Andy Adamson64419a92011-03-01 01:34:16 +00002266 struct nfs_server *nfss = NFS_SERVER(inode);
2267 enum pnfs_try_status trypnfs;
2268
Fred Isamancd841602012-04-20 14:47:44 -04002269 hdr->mds_ops = call_ops;
Andy Adamson64419a92011-03-01 01:34:16 +00002270
2271 dprintk("%s: Reading ino:%lu %u@%llu\n",
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002272 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
Andy Adamson64419a92011-03-01 01:34:16 +00002273
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002274 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002275 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson64419a92011-03-01 01:34:16 +00002276 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
Andy Adamson64419a92011-03-01 01:34:16 +00002277 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2278 return trypnfs;
2279}
Andy Adamson863a3c62011-03-23 13:27:54 +00002280
Peng Taoceb11e12014-11-10 08:35:38 +08002281/* Resend all requests through pnfs. */
Weston Andros Adamson1b1bc662016-04-01 11:42:28 -04002282void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
Peng Taoceb11e12014-11-10 08:35:38 +08002283{
2284 struct nfs_pageio_descriptor pgio;
2285
Weston Andros Adamson1b1bc662016-04-01 11:42:28 -04002286 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2287 nfs_pageio_init_read(&pgio, hdr->inode, false,
2288 hdr->completion_ops);
2289 hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
2290 }
Peng Taoceb11e12014-11-10 08:35:38 +08002291}
2292EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
2293
Trond Myklebust493292d2011-07-13 15:58:28 -04002294static void
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002295pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -04002296{
Trond Myklebust493292d2011-07-13 15:58:28 -04002297 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2298 struct pnfs_layout_segment *lseg = desc->pg_lseg;
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002299 enum pnfs_try_status trypnfs;
Trond Myklebust493292d2011-07-13 15:58:28 -04002300
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002301 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
Peng Taoceb11e12014-11-10 08:35:38 +08002302 if (trypnfs == PNFS_TRY_AGAIN)
Weston Andros Adamson1b1bc662016-04-01 11:42:28 -04002303 pnfs_read_resend_pnfs(hdr);
2304 if (trypnfs == PNFS_NOT_ATTEMPTED || hdr->task.tk_status)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002305 pnfs_read_through_mds(desc, hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -04002306}
2307
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002308static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
2309{
Trond Myklebust9369a432012-09-18 20:57:08 -04002310 pnfs_put_lseg(hdr->lseg);
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002311 nfs_pgio_header_free(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002312}
2313
Trond Myklebust493292d2011-07-13 15:58:28 -04002314int
2315pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2316{
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002317 struct nfs_pgio_header *hdr;
Trond Myklebust493292d2011-07-13 15:58:28 -04002318 int ret;
2319
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002320 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2321 if (!hdr) {
Peng Tao2bff2282015-12-05 02:03:17 +08002322 desc->pg_error = -ENOMEM;
2323 return desc->pg_error;
Trond Myklebust493292d2011-07-13 15:58:28 -04002324 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002325 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
Trond Myklebust9369a432012-09-18 20:57:08 -04002326 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Anna Schumakeref2c4882014-05-06 09:12:36 -04002327 ret = nfs_generic_pgio(desc, hdr);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002328 if (!ret)
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002329 pnfs_do_read(desc, hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002330 return ret;
Trond Myklebust493292d2011-07-13 15:58:28 -04002331}
2332EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
2333
Trond Myklebust71244d92014-01-13 13:34:36 -05002334static void pnfs_clear_layoutcommitting(struct inode *inode)
2335{
2336 unsigned long *bitlock = &NFS_I(inode)->flags;
2337
2338 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002339 smp_mb__after_atomic();
Trond Myklebust71244d92014-01-13 13:34:36 -05002340 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2341}
2342
Andy Adamson863a3c62011-03-23 13:27:54 +00002343/*
Peng Taoa9bae562011-07-30 20:52:33 -04002344 * There can be multiple RW segments.
Andy Adamson863a3c62011-03-23 13:27:54 +00002345 */
Peng Taoa9bae562011-07-30 20:52:33 -04002346static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
Andy Adamson863a3c62011-03-23 13:27:54 +00002347{
Peng Taoa9bae562011-07-30 20:52:33 -04002348 struct pnfs_layout_segment *lseg;
Andy Adamson863a3c62011-03-23 13:27:54 +00002349
Peng Taoa9bae562011-07-30 20:52:33 -04002350 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2351 if (lseg->pls_range.iomode == IOMODE_RW &&
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002352 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
Peng Taoa9bae562011-07-30 20:52:33 -04002353 list_add(&lseg->pls_lc_list, listp);
2354 }
Andy Adamson863a3c62011-03-23 13:27:54 +00002355}
2356
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002357static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2358{
2359 struct pnfs_layout_segment *lseg, *tmp;
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002360
2361 /* Matched by references in pnfs_set_layoutcommit */
2362 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2363 list_del_init(&lseg->pls_lc_list);
2364 pnfs_put_lseg(lseg);
2365 }
2366
Trond Myklebust71244d92014-01-13 13:34:36 -05002367 pnfs_clear_layoutcommitting(inode);
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002368}
2369
Peng Tao1b0ae062011-09-22 21:50:12 -04002370void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2371{
Trond Myklebustb9e028f2012-09-18 16:41:18 -04002372 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
Peng Tao1b0ae062011-09-22 21:50:12 -04002373}
2374EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2375
Andy Adamson863a3c62011-03-23 13:27:54 +00002376void
Trond Myklebust67af7612015-03-25 20:40:38 -04002377pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
2378 loff_t end_pos)
Andy Adamson863a3c62011-03-23 13:27:54 +00002379{
Fred Isamancd841602012-04-20 14:47:44 -04002380 struct nfs_inode *nfsi = NFS_I(inode);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002381 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00002382
Fred Isamancd841602012-04-20 14:47:44 -04002383 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00002384 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
Trond Myklebust29559b12015-03-25 20:10:20 -04002385 nfsi->layout->plh_lwb = end_pos;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002386 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00002387 dprintk("%s: Set layoutcommit for inode %lu ",
Fred Isamancd841602012-04-20 14:47:44 -04002388 __func__, inode->i_ino);
Trond Myklebust29559b12015-03-25 20:10:20 -04002389 } else if (end_pos > nfsi->layout->plh_lwb)
2390 nfsi->layout->plh_lwb = end_pos;
Trond Myklebust67af7612015-03-25 20:40:38 -04002391 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
Peng Taoa9bae562011-07-30 20:52:33 -04002392 /* references matched in nfs4_layoutcommit_release */
Trond Myklebust67af7612015-03-25 20:40:38 -04002393 pnfs_get_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04002394 }
Fred Isamancd841602012-04-20 14:47:44 -04002395 spin_unlock(&inode->i_lock);
Peng Taoacff58802011-07-30 20:52:31 -04002396 dprintk("%s: lseg %p end_pos %llu\n",
Trond Myklebust67af7612015-03-25 20:40:38 -04002397 __func__, lseg, nfsi->layout->plh_lwb);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002398
2399 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2400 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2401 if (mark_as_dirty)
Fred Isamancd841602012-04-20 14:47:44 -04002402 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002403}
2404EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2405
Andy Adamsondb29c082011-07-30 20:52:38 -04002406void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2407{
2408 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2409
2410 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2411 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002412 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
Andy Adamsondb29c082011-07-30 20:52:38 -04002413}
2414
Andy Adamsonde4b15c2011-03-12 02:58:09 -05002415/*
2416 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2417 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2418 * data to disk to allow the server to recover the data if it crashes.
2419 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2420 * is off, and a COMMIT is sent to a data server, or
2421 * if WRITEs to a data server return NFS_DATA_SYNC.
2422 */
Andy Adamson863a3c62011-03-23 13:27:54 +00002423int
Andy Adamsonef311532011-03-12 02:58:10 -05002424pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00002425{
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002426 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
Andy Adamson863a3c62011-03-23 13:27:54 +00002427 struct nfs4_layoutcommit_data *data;
2428 struct nfs_inode *nfsi = NFS_I(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002429 loff_t end_pos;
Trond Myklebust71244d92014-01-13 13:34:36 -05002430 int status;
2431
2432 if (!pnfs_layoutcommit_outstanding(inode))
2433 return 0;
Andy Adamson863a3c62011-03-23 13:27:54 +00002434
2435 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
2436
Trond Myklebust71244d92014-01-13 13:34:36 -05002437 status = -EAGAIN;
2438 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
2439 if (!sync)
2440 goto out;
NeilBrown74316202014-07-07 15:16:04 +10002441 status = wait_on_bit_lock_action(&nfsi->flags,
Trond Myklebust71244d92014-01-13 13:34:36 -05002442 NFS_INO_LAYOUTCOMMITTING,
2443 nfs_wait_bit_killable,
2444 TASK_KILLABLE);
2445 if (status)
2446 goto out;
2447 }
Andy Adamsonde4b15c2011-03-12 02:58:09 -05002448
Trond Myklebust71244d92014-01-13 13:34:36 -05002449 status = -ENOMEM;
Andy Adamson863a3c62011-03-23 13:27:54 +00002450 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2451 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebust71244d92014-01-13 13:34:36 -05002452 if (!data)
2453 goto clear_layoutcommitting;
Andy Adamson863a3c62011-03-23 13:27:54 +00002454
Trond Myklebust71244d92014-01-13 13:34:36 -05002455 status = 0;
2456 spin_lock(&inode->i_lock);
2457 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2458 goto out_unlock;
Peng Tao92407e72011-10-23 20:21:17 -07002459
Peng Taoa9bae562011-07-30 20:52:33 -04002460 INIT_LIST_HEAD(&data->lseg_list);
Peng Taoa9bae562011-07-30 20:52:33 -04002461 pnfs_list_write_lseg(inode, &data->lseg_list);
Andy Adamson863a3c62011-03-23 13:27:54 +00002462
Peng Taoacff58802011-07-30 20:52:31 -04002463 end_pos = nfsi->layout->plh_lwb;
Andy Adamson863a3c62011-03-23 13:27:54 +00002464
Trond Myklebustf597c532012-03-04 18:13:56 -05002465 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
Andy Adamson863a3c62011-03-23 13:27:54 +00002466 spin_unlock(&inode->i_lock);
2467
2468 data->args.inode = inode;
Peng Tao9fa40752011-07-30 20:52:32 -04002469 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
Andy Adamson863a3c62011-03-23 13:27:54 +00002470 nfs_fattr_init(&data->fattr);
2471 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2472 data->res.fattr = &data->fattr;
Trond Myklebust2e18d4d2016-06-26 18:54:58 -04002473 if (end_pos != 0)
2474 data->args.lastbytewritten = end_pos - 1;
2475 else
2476 data->args.lastbytewritten = U64_MAX;
Andy Adamson863a3c62011-03-23 13:27:54 +00002477 data->res.server = NFS_SERVER(inode);
2478
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002479 if (ld->prepare_layoutcommit) {
2480 status = ld->prepare_layoutcommit(&data->args);
2481 if (status) {
Jeff Layton3471648a2015-07-10 15:58:42 -04002482 put_rpccred(data->cred);
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002483 spin_lock(&inode->i_lock);
Trond Myklebust29559b12015-03-25 20:10:20 -04002484 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2485 if (end_pos > nfsi->layout->plh_lwb)
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002486 nfsi->layout->plh_lwb = end_pos;
Jeff Layton3471648a2015-07-10 15:58:42 -04002487 goto out_unlock;
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002488 }
2489 }
2490
2491
Andy Adamson863a3c62011-03-23 13:27:54 +00002492 status = nfs4_proc_layoutcommit(data, sync);
2493out:
Peng Tao92407e72011-10-23 20:21:17 -07002494 if (status)
2495 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002496 dprintk("<-- %s status %d\n", __func__, status);
2497 return status;
Trond Myklebust71244d92014-01-13 13:34:36 -05002498out_unlock:
2499 spin_unlock(&inode->i_lock);
Peng Tao92407e72011-10-23 20:21:17 -07002500 kfree(data);
Trond Myklebust71244d92014-01-13 13:34:36 -05002501clear_layoutcommitting:
2502 pnfs_clear_layoutcommitting(inode);
Peng Tao92407e72011-10-23 20:21:17 -07002503 goto out;
Andy Adamson863a3c62011-03-23 13:27:54 +00002504}
Peng Tao72cff442014-08-07 10:12:38 +08002505EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
Andy Adamson82be4172012-05-23 05:02:35 -04002506
Trond Myklebust5bb89b42015-03-25 14:14:42 -04002507int
2508pnfs_generic_sync(struct inode *inode, bool datasync)
2509{
2510 return pnfs_layoutcommit_inode(inode, true);
2511}
2512EXPORT_SYMBOL_GPL(pnfs_generic_sync);
2513
Andy Adamson82be4172012-05-23 05:02:35 -04002514struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
2515{
2516 struct nfs4_threshold *thp;
2517
2518 thp = kzalloc(sizeof(*thp), GFP_NOFS);
2519 if (!thp) {
2520 dprintk("%s mdsthreshold allocation failed\n", __func__);
2521 return NULL;
2522 }
2523 return thp;
2524}
Peng Tao87334082015-06-23 19:51:57 +08002525
Peng Tao865a7ec2015-06-25 18:19:32 +08002526#if IS_ENABLED(CONFIG_NFS_V4_2)
Peng Tao87334082015-06-23 19:51:57 +08002527int
Trond Myklebustc8ad8892015-08-05 17:31:58 -04002528pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
Peng Tao87334082015-06-23 19:51:57 +08002529{
2530 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2531 struct nfs_server *server = NFS_SERVER(inode);
Peng Tao1bfe3b22015-06-23 19:52:03 +08002532 struct nfs_inode *nfsi = NFS_I(inode);
Peng Tao87334082015-06-23 19:51:57 +08002533 struct nfs42_layoutstat_data *data;
2534 struct pnfs_layout_hdr *hdr;
2535 int status = 0;
2536
2537 if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
2538 goto out;
2539
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04002540 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
2541 goto out;
2542
Peng Tao1bfe3b22015-06-23 19:52:03 +08002543 if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
2544 goto out;
2545
Peng Tao87334082015-06-23 19:51:57 +08002546 spin_lock(&inode->i_lock);
2547 if (!NFS_I(inode)->layout) {
2548 spin_unlock(&inode->i_lock);
Trond Myklebustf538d0b2016-05-16 14:41:14 -04002549 goto out_clear_layoutstats;
Peng Tao87334082015-06-23 19:51:57 +08002550 }
2551 hdr = NFS_I(inode)->layout;
2552 pnfs_get_layout_hdr(hdr);
2553 spin_unlock(&inode->i_lock);
2554
Trond Myklebustc8ad8892015-08-05 17:31:58 -04002555 data = kzalloc(sizeof(*data), gfp_flags);
Peng Tao87334082015-06-23 19:51:57 +08002556 if (!data) {
2557 status = -ENOMEM;
2558 goto out_put;
2559 }
2560
2561 data->args.fh = NFS_FH(inode);
2562 data->args.inode = inode;
Peng Tao87334082015-06-23 19:51:57 +08002563 status = ld->prepare_layoutstats(&data->args);
2564 if (status)
2565 goto out_free;
2566
2567 status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
2568
2569out:
2570 dprintk("%s returns %d\n", __func__, status);
2571 return status;
2572
2573out_free:
2574 kfree(data);
2575out_put:
2576 pnfs_put_layout_hdr(hdr);
Trond Myklebustf538d0b2016-05-16 14:41:14 -04002577out_clear_layoutstats:
Peng Tao1bfe3b22015-06-23 19:52:03 +08002578 smp_mb__before_atomic();
2579 clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
2580 smp_mb__after_atomic();
Peng Tao87334082015-06-23 19:51:57 +08002581 goto out;
2582}
2583EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
Peng Tao865a7ec2015-06-25 18:19:32 +08002584#endif
Trond Myklebustbbf58bf2015-08-24 20:39:18 -04002585
2586unsigned int layoutstats_timer;
2587module_param(layoutstats_timer, uint, 0644);
2588EXPORT_SYMBOL_GPL(layoutstats_timer);