blob: f61cb81eb5ab6531f4af07a21d8e1cc6b5e57970 [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);
Trond Myklebust68f74472016-10-12 19:50:54 -040057static void pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
58 struct list_head *free_me,
59 const struct pnfs_layout_range *range,
60 u32 seq);
Peng Taoaa1e0e3a2014-09-06 00:53:25 +080061
Fred Isaman02c35fc2010-10-20 00:17:59 -040062/* Return the registered pnfs layout driver module matching given id */
63static struct pnfs_layoutdriver_type *
64find_pnfs_driver_locked(u32 id)
65{
66 struct pnfs_layoutdriver_type *local;
67
68 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
69 if (local->id == id)
70 goto out;
71 local = NULL;
72out:
73 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
74 return local;
75}
76
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040077static struct pnfs_layoutdriver_type *
78find_pnfs_driver(u32 id)
79{
Fred Isaman02c35fc2010-10-20 00:17:59 -040080 struct pnfs_layoutdriver_type *local;
81
82 spin_lock(&pnfs_spinlock);
83 local = find_pnfs_driver_locked(id);
Trond Myklebust0a9c63f2012-06-15 13:02:58 -040084 if (local != NULL && !try_module_get(local->owner)) {
85 dprintk("%s: Could not grab reference on module\n", __func__);
86 local = NULL;
87 }
Fred Isaman02c35fc2010-10-20 00:17:59 -040088 spin_unlock(&pnfs_spinlock);
89 return local;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040090}
91
92void
93unset_pnfs_layoutdriver(struct nfs_server *nfss)
94{
Benny Halevy738fd0f32011-07-30 20:52:36 -040095 if (nfss->pnfs_curr_ld) {
96 if (nfss->pnfs_curr_ld->clear_layoutdriver)
97 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
Trond Myklebust2a4c8992012-06-14 13:08:38 -040098 /* Decrement the MDS count. Purge the deviceid cache if zero */
99 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
100 nfs4_deviceid_purge_client(nfss->nfs_client);
Fred Isaman02c35fc2010-10-20 00:17:59 -0400101 module_put(nfss->pnfs_curr_ld->owner);
Benny Halevy738fd0f32011-07-30 20:52:36 -0400102 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400103 nfss->pnfs_curr_ld = NULL;
104}
105
106/*
Jeff Laytonca440c32016-09-15 14:40:49 -0400107 * When the server sends a list of layout types, we choose one in the order
108 * given in the list below.
109 *
110 * FIXME: should this list be configurable in some fashion? module param?
111 * mount option? something else?
112 */
113static const u32 ld_prefs[] = {
114 LAYOUT_SCSI,
115 LAYOUT_BLOCK_VOLUME,
116 LAYOUT_OSD2_OBJECTS,
117 LAYOUT_FLEX_FILES,
118 LAYOUT_NFSV4_1_FILES,
119 0
120};
121
122static int
123ld_cmp(const void *e1, const void *e2)
124{
125 u32 ld1 = *((u32 *)e1);
126 u32 ld2 = *((u32 *)e2);
127 int i;
128
129 for (i = 0; ld_prefs[i] != 0; i++) {
130 if (ld1 == ld_prefs[i])
131 return -1;
132
133 if (ld2 == ld_prefs[i])
134 return 1;
135 }
136 return 0;
137}
138
139/*
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400140 * Try to set the server's pnfs module to the pnfs layout type specified by id.
141 * Currently only one pNFS layout driver per filesystem is supported.
142 *
Jeff Layton3132e492016-08-10 15:58:24 -0400143 * @ids array of layout types supported by MDS.
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400144 */
145void
Benny Halevy738fd0f32011-07-30 20:52:36 -0400146set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
Jeff Laytonca440c32016-09-15 14:40:49 -0400147 struct nfs_fsinfo *fsinfo)
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400148{
149 struct pnfs_layoutdriver_type *ld_type = NULL;
Jeff Layton3132e492016-08-10 15:58:24 -0400150 u32 id;
Jeff Laytonca440c32016-09-15 14:40:49 -0400151 int i;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400152
Anna Schumaker19274712016-10-26 15:54:31 -0400153 if (fsinfo->nlayouttypes == 0)
154 goto out_no_driver;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400155 if (!(server->nfs_client->cl_exchange_flags &
156 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
Jeff Layton3132e492016-08-10 15:58:24 -0400157 printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n",
158 __func__, server->nfs_client->cl_exchange_flags);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400159 goto out_no_driver;
160 }
Jeff Layton3132e492016-08-10 15:58:24 -0400161
Jeff Laytonca440c32016-09-15 14:40:49 -0400162 sort(fsinfo->layouttype, fsinfo->nlayouttypes,
163 sizeof(*fsinfo->layouttype), ld_cmp, NULL);
Jeff Layton3132e492016-08-10 15:58:24 -0400164
Jeff Laytonca440c32016-09-15 14:40:49 -0400165 for (i = 0; i < fsinfo->nlayouttypes; i++) {
166 id = fsinfo->layouttype[i];
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400167 ld_type = find_pnfs_driver(id);
Jeff Laytonca440c32016-09-15 14:40:49 -0400168 if (!ld_type) {
169 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX,
170 id);
171 ld_type = find_pnfs_driver(id);
172 }
173 if (ld_type)
174 break;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400175 }
Jeff Layton3132e492016-08-10 15:58:24 -0400176
177 if (!ld_type) {
Jeff Laytonca440c32016-09-15 14:40:49 -0400178 dprintk("%s: No pNFS module found!\n", __func__);
Jeff Layton3132e492016-08-10 15:58:24 -0400179 goto out_no_driver;
180 }
181
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400182 server->pnfs_curr_ld = ld_type;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400183 if (ld_type->set_layoutdriver
184 && ld_type->set_layoutdriver(server, mntfh)) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500185 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
186 "driver %u.\n", __func__, id);
Benny Halevy738fd0f32011-07-30 20:52:36 -0400187 module_put(ld_type->owner);
188 goto out_no_driver;
189 }
Trond Myklebust2a4c8992012-06-14 13:08:38 -0400190 /* Bump the MDS count */
191 atomic_inc(&server->nfs_client->cl_mds_count);
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000192
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400193 dprintk("%s: pNFS module for %u set\n", __func__, id);
194 return;
195
196out_no_driver:
197 dprintk("%s: Using NFSv4 I/O\n", __func__);
198 server->pnfs_curr_ld = NULL;
199}
Fred Isaman02c35fc2010-10-20 00:17:59 -0400200
201int
202pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
203{
204 int status = -EINVAL;
205 struct pnfs_layoutdriver_type *tmp;
206
207 if (ld_type->id == 0) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500208 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
Fred Isaman02c35fc2010-10-20 00:17:59 -0400209 return status;
210 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400211 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500212 printk(KERN_ERR "NFS: %s Layout driver must provide "
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400213 "alloc_lseg and free_lseg.\n", __func__);
214 return status;
215 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400216
217 spin_lock(&pnfs_spinlock);
218 tmp = find_pnfs_driver_locked(ld_type->id);
219 if (!tmp) {
220 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
221 status = 0;
222 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
223 ld_type->name);
224 } else {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500225 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
Fred Isaman02c35fc2010-10-20 00:17:59 -0400226 __func__, ld_type->id);
227 }
228 spin_unlock(&pnfs_spinlock);
229
230 return status;
231}
232EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
233
234void
235pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
236{
237 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
238 spin_lock(&pnfs_spinlock);
239 list_del(&ld_type->pnfs_tblid);
240 spin_unlock(&pnfs_spinlock);
241}
242EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
Benny Halevye5e94012010-10-20 00:18:01 -0400243
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400244/*
245 * pNFS client layout cache
246 */
247
Fred Isamancc6e5342011-01-06 11:36:28 +0000248/* Need to hold i_lock if caller does not already hold reference */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000249void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400250pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
Benny Halevye5e94012010-10-20 00:18:01 -0400251{
Fred Isamancc6e5342011-01-06 11:36:28 +0000252 atomic_inc(&lo->plh_refcount);
253}
254
Benny Halevy636fb9c2011-05-22 19:51:33 +0300255static struct pnfs_layout_hdr *
256pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
257{
258 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
Trond Myklebust57934272012-09-20 20:37:23 -0400259 return ld->alloc_layout_hdr(ino, gfp_flags);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300260}
261
262static void
263pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
264{
Trond Myklebust9c626382012-09-20 17:31:43 -0400265 struct nfs_server *server = NFS_SERVER(lo->plh_inode);
266 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
267
268 if (!list_empty(&lo->plh_layouts)) {
269 struct nfs_client *clp = server->nfs_client;
270
271 spin_lock(&clp->cl_lock);
272 list_del_init(&lo->plh_layouts);
273 spin_unlock(&clp->cl_lock);
274 }
Peng Tao9fa40752011-07-30 20:52:32 -0400275 put_rpccred(lo->plh_lc_cred);
Trond Myklebust57934272012-09-20 20:37:23 -0400276 return ld->free_layout_hdr(lo);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300277}
278
Fred Isamancc6e5342011-01-06 11:36:28 +0000279static void
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400280pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
Fred Isamancc6e5342011-01-06 11:36:28 +0000281{
Trond Myklebustbb346f62012-09-20 15:52:13 -0400282 struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
Fred Isamancc6e5342011-01-06 11:36:28 +0000283 dprintk("%s: freeing layout cache %p\n", __func__, lo);
Trond Myklebustbb346f62012-09-20 15:52:13 -0400284 nfsi->layout = NULL;
285 /* Reset MDS Threshold I/O counters */
286 nfsi->write_io = 0;
287 nfsi->read_io = 0;
Benny Halevye5e94012010-10-20 00:18:01 -0400288}
289
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400290void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400291pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400292{
Fred Isamancc6e5342011-01-06 11:36:28 +0000293 struct inode *inode = lo->plh_inode;
294
Trond Myklebust13c13a62016-01-26 23:12:11 -0500295 pnfs_layoutreturn_before_put_layout_hdr(lo);
296
Fred Isamancc6e5342011-01-06 11:36:28 +0000297 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
Peng Tao566f8732014-10-10 23:25:46 +0800298 if (!list_empty(&lo->plh_segs))
299 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400300 pnfs_detach_layout_hdr(lo);
Fred Isamancc6e5342011-01-06 11:36:28 +0000301 spin_unlock(&inode->i_lock);
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400302 pnfs_free_layout_hdr(lo);
Fred Isamancc6e5342011-01-06 11:36:28 +0000303 }
Andy Adamson974cec82010-10-20 00:18:02 -0400304}
305
Trond Myklebustae5a4592016-11-14 14:34:18 -0500306static void
307pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo)
308{
309 lo->plh_return_iomode = 0;
310 lo->plh_return_seq = 0;
311 clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
312}
313
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500314/*
315 * Mark a pnfs_layout_hdr and all associated layout segments as invalid
316 *
317 * In order to continue using the pnfs_layout_hdr, a full recovery
318 * is required.
319 * Note that caller must hold inode->i_lock.
320 */
Trond Myklebust5f46be02016-07-22 11:25:27 -0400321int
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500322pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
323 struct list_head *lseg_list)
324{
325 struct pnfs_layout_range range = {
326 .iomode = IOMODE_ANY,
327 .offset = 0,
328 .length = NFS4_MAX_UINT64,
329 };
330
331 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Trond Myklebustae5a4592016-11-14 14:34:18 -0500332 pnfs_clear_layoutreturn_info(lo);
Trond Myklebust68f74472016-10-12 19:50:54 -0400333 pnfs_free_returned_lsegs(lo, lseg_list, &range, 0);
Jeff Layton6d597e12016-05-17 12:28:42 -0400334 return pnfs_mark_matching_lsegs_invalid(lo, lseg_list, &range, 0);
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500335}
336
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400337static int
338pnfs_iomode_to_fail_bit(u32 iomode)
339{
340 return iomode == IOMODE_RW ?
341 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
342}
343
344static void
Trond Myklebust3e621212012-09-24 13:07:16 -0400345pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400346{
Trond Myklebust25c75332012-09-18 17:01:12 -0400347 lo->plh_retry_timestamp = jiffies;
Yanchuan Nian39e88fc2013-01-04 20:19:49 +0800348 if (!test_and_set_bit(fail_bit, &lo->plh_flags))
Trond Myklebust3e621212012-09-24 13:07:16 -0400349 atomic_inc(&lo->plh_refcount);
350}
351
352static void
353pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
354{
355 if (test_and_clear_bit(fail_bit, &lo->plh_flags))
356 atomic_dec(&lo->plh_refcount);
357}
358
359static void
360pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
361{
362 struct inode *inode = lo->plh_inode;
Trond Myklebust115ce572012-09-20 21:19:43 -0400363 struct pnfs_layout_range range = {
364 .iomode = iomode,
365 .offset = 0,
366 .length = NFS4_MAX_UINT64,
367 };
368 LIST_HEAD(head);
Trond Myklebust3e621212012-09-24 13:07:16 -0400369
370 spin_lock(&inode->i_lock);
371 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
Jeff Layton6d597e12016-05-17 12:28:42 -0400372 pnfs_mark_matching_lsegs_invalid(lo, &head, &range, 0);
Trond Myklebust3e621212012-09-24 13:07:16 -0400373 spin_unlock(&inode->i_lock);
Trond Myklebust115ce572012-09-20 21:19:43 -0400374 pnfs_free_lseg_list(&head);
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400375 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
376 iomode == IOMODE_RW ? "RW" : "READ");
377}
378
379static bool
380pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
381{
Trond Myklebust25c75332012-09-18 17:01:12 -0400382 unsigned long start, end;
Trond Myklebust3e621212012-09-24 13:07:16 -0400383 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
384
385 if (test_bit(fail_bit, &lo->plh_flags) == 0)
Trond Myklebust25c75332012-09-18 17:01:12 -0400386 return false;
387 end = jiffies;
388 start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
389 if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
390 /* It is time to retry the failed layoutgets */
Trond Myklebust3e621212012-09-24 13:07:16 -0400391 pnfs_layout_clear_fail_bit(lo, fail_bit);
Trond Myklebust25c75332012-09-18 17:01:12 -0400392 return false;
393 }
394 return true;
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400395}
396
Andy Adamson974cec82010-10-20 00:18:02 -0400397static void
Trond Myklebust119cef972016-07-24 15:10:12 -0400398pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
399 const struct pnfs_layout_range *range,
400 const nfs4_stateid *stateid)
Andy Adamson974cec82010-10-20 00:18:02 -0400401{
Fred Isaman566052c2011-01-06 11:36:20 +0000402 INIT_LIST_HEAD(&lseg->pls_list);
Peng Taoa9bae562011-07-30 20:52:33 -0400403 INIT_LIST_HEAD(&lseg->pls_lc_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000404 atomic_set(&lseg->pls_refcount, 1);
Fred Isaman4541d162011-01-06 11:36:23 +0000405 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000406 lseg->pls_layout = lo;
Trond Myklebust119cef972016-07-24 15:10:12 -0400407 lseg->pls_range = *range;
408 lseg->pls_seq = be32_to_cpu(stateid->seqid);
Andy Adamson974cec82010-10-20 00:18:02 -0400409}
410
Trond Myklebust905ca192012-09-20 20:46:49 -0400411static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400412{
Trond Myklebust68f74472016-10-12 19:50:54 -0400413 if (lseg != NULL) {
414 struct inode *inode = lseg->pls_layout->plh_inode;
415 NFS_SERVER(inode)->pnfs_curr_ld->free_lseg(lseg);
416 }
Andy Adamson974cec82010-10-20 00:18:02 -0400417}
418
Fred Isamand684d2a2011-03-01 01:34:13 +0000419static void
Trond Myklebust57036a32012-09-20 16:33:30 -0400420pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
421 struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400422{
Benny Halevyd20581a2011-05-22 19:52:03 +0300423 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000424 list_del_init(&lseg->pls_list);
Trond Myklebust8f0d27d2012-09-20 20:57:11 -0400425 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
426 atomic_dec(&lo->plh_refcount);
Trond Myklebust7b650992016-11-14 13:10:48 -0500427 if (list_empty(&lo->plh_segs) &&
428 !test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) &&
429 !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
Trond Myklebust334a8f32016-09-04 12:46:35 -0400430 if (atomic_read(&lo->plh_outstanding) == 0)
431 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Trond Myklebust173f77e2012-09-21 15:49:42 -0400432 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Trond Myklebust2d148c72016-06-17 16:48:26 -0400433 }
Fred Isamand684d2a2011-03-01 01:34:13 +0000434}
435
Trond Myklebust68f74472016-10-12 19:50:54 -0400436static bool
437pnfs_cache_lseg_for_layoutreturn(struct pnfs_layout_hdr *lo,
438 struct pnfs_layout_segment *lseg)
439{
440 if (test_and_clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
441 pnfs_layout_is_valid(lo)) {
442 list_move_tail(&lseg->pls_list, &lo->plh_return_segs);
443 return true;
444 }
445 return false;
446}
447
Fred Isamanbae724e2011-03-01 01:34:15 +0000448void
Trond Myklebust9369a432012-09-18 20:57:08 -0400449pnfs_put_lseg(struct pnfs_layout_segment *lseg)
Fred Isamand684d2a2011-03-01 01:34:13 +0000450{
Trond Myklebust57036a32012-09-20 16:33:30 -0400451 struct pnfs_layout_hdr *lo;
Fred Isamand684d2a2011-03-01 01:34:13 +0000452 struct inode *inode;
453
454 if (!lseg)
455 return;
456
Fred Isaman4541d162011-01-06 11:36:23 +0000457 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
458 atomic_read(&lseg->pls_refcount),
459 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500460
Trond Myklebust57036a32012-09-20 16:33:30 -0400461 lo = lseg->pls_layout;
462 inode = lo->plh_inode;
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500463
Fred Isamand684d2a2011-03-01 01:34:13 +0000464 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
Trond Myklebustfaa4a542015-07-09 18:24:07 +0200465 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
466 spin_unlock(&inode->i_lock);
467 return;
468 }
Trond Myklebust8f0d27d2012-09-20 20:57:11 -0400469 pnfs_get_layout_hdr(lo);
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500470 pnfs_layout_remove_lseg(lo, lseg);
Trond Myklebust68f74472016-10-12 19:50:54 -0400471 if (pnfs_cache_lseg_for_layoutreturn(lo, lseg))
472 lseg = NULL;
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500473 spin_unlock(&inode->i_lock);
474 pnfs_free_lseg(lseg);
475 pnfs_put_layout_hdr(lo);
Fred Isaman4541d162011-01-06 11:36:23 +0000476 }
Andy Adamson974cec82010-10-20 00:18:02 -0400477}
Trond Myklebust9369a432012-09-18 20:57:08 -0400478EXPORT_SYMBOL_GPL(pnfs_put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400479
Trond Myklebust6543f802014-10-08 16:39:12 -0400480static void pnfs_free_lseg_async_work(struct work_struct *work)
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400481{
482 struct pnfs_layout_segment *lseg;
Trond Myklebust6543f802014-10-08 16:39:12 -0400483 struct pnfs_layout_hdr *lo;
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400484
485 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
Trond Myklebust6543f802014-10-08 16:39:12 -0400486 lo = lseg->pls_layout;
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400487
Trond Myklebust6543f802014-10-08 16:39:12 -0400488 pnfs_free_lseg(lseg);
489 pnfs_put_layout_hdr(lo);
490}
491
492static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg)
493{
494 INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work);
495 schedule_work(&lseg->pls_work);
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400496}
497
498void
Trond Myklebust6543f802014-10-08 16:39:12 -0400499pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg)
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400500{
Trond Myklebust6543f802014-10-08 16:39:12 -0400501 if (!lseg)
502 return;
503
504 assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock);
505
506 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
507 atomic_read(&lseg->pls_refcount),
508 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
509 if (atomic_dec_and_test(&lseg->pls_refcount)) {
510 struct pnfs_layout_hdr *lo = lseg->pls_layout;
Trond Myklebustfaa4a542015-07-09 18:24:07 +0200511 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
512 return;
Trond Myklebust6543f802014-10-08 16:39:12 -0400513 pnfs_layout_remove_lseg(lo, lseg);
Trond Myklebust68f74472016-10-12 19:50:54 -0400514 if (!pnfs_cache_lseg_for_layoutreturn(lo, lseg)) {
515 pnfs_get_layout_hdr(lo);
516 pnfs_free_lseg_async(lseg);
517 }
Trond Myklebust6543f802014-10-08 16:39:12 -0400518 }
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400519}
Trond Myklebust6543f802014-10-08 16:39:12 -0400520EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked);
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400521
Benny Halevyfb3296e2011-05-22 19:47:26 +0300522/*
523 * is l2 fully contained in l1?
524 * start1 end1
525 * [----------------------------------)
526 * start2 end2
527 * [----------------)
528 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400529static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -0400530pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400531 const struct pnfs_layout_range *l2)
Benny Halevyfb3296e2011-05-22 19:47:26 +0300532{
533 u64 start1 = l1->offset;
Trond Myklebust17822b22016-10-25 12:24:25 -0400534 u64 end1 = pnfs_end_offset(start1, l1->length);
Benny Halevyfb3296e2011-05-22 19:47:26 +0300535 u64 start2 = l2->offset;
Trond Myklebust17822b22016-10-25 12:24:25 -0400536 u64 end2 = pnfs_end_offset(start2, l2->length);
Benny Halevyfb3296e2011-05-22 19:47:26 +0300537
538 return (start1 <= start2) && (end1 >= end2);
539}
540
Trond Myklebust24956802013-03-20 13:03:00 -0400541static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
542 struct list_head *tmp_list)
543{
544 if (!atomic_dec_and_test(&lseg->pls_refcount))
545 return false;
546 pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
547 list_add(&lseg->pls_list, tmp_list);
548 return true;
549}
550
Fred Isaman4541d162011-01-06 11:36:23 +0000551/* Returns 1 if lseg is removed from list, 0 otherwise */
552static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
553 struct list_head *tmp_list)
554{
555 int rv = 0;
556
557 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
558 /* Remove the reference keeping the lseg in the
559 * list. It will now be removed when all
560 * outstanding io is finished.
561 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000562 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
563 atomic_read(&lseg->pls_refcount));
Trond Myklebust24956802013-03-20 13:03:00 -0400564 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
Fred Isamand684d2a2011-03-01 01:34:13 +0000565 rv = 1;
Fred Isaman4541d162011-01-06 11:36:23 +0000566 }
567 return rv;
568}
569
Jeff Layton6d597e12016-05-17 12:28:42 -0400570/*
571 * Compare 2 layout stateid sequence ids, to see which is newer,
572 * taking into account wraparound issues.
573 */
574static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
575{
576 return (s32)(s1 - s2) > 0;
577}
578
Trond Myklebuste036f462016-07-22 11:13:22 -0400579static bool
580pnfs_should_free_range(const struct pnfs_layout_range *lseg_range,
581 const struct pnfs_layout_range *recall_range)
582{
583 return (recall_range->iomode == IOMODE_ANY ||
584 lseg_range->iomode == recall_range->iomode) &&
585 pnfs_lseg_range_intersecting(lseg_range, recall_range);
586}
587
588static bool
589pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg,
590 const struct pnfs_layout_range *recall_range,
591 u32 seq)
592{
593 if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq))
594 return false;
595 if (recall_range == NULL)
596 return true;
597 return pnfs_should_free_range(&lseg->pls_range, recall_range);
598}
599
Jeff Layton6d597e12016-05-17 12:28:42 -0400600/**
601 * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later
602 * @lo: layout header containing the lsegs
603 * @tmp_list: list head where doomed lsegs should go
604 * @recall_range: optional recall range argument to match (may be NULL)
605 * @seq: only invalidate lsegs obtained prior to this sequence (may be 0)
606 *
607 * Walk the list of lsegs in the layout header, and tear down any that should
608 * be destroyed. If "recall_range" is specified then the segment must match
609 * that range. If "seq" is non-zero, then only match segments that were handed
610 * out at or before that sequence.
611 *
612 * Returns number of matching invalid lsegs remaining in list after scanning
613 * it and purging them.
Fred Isaman4541d162011-01-06 11:36:23 +0000614 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000615int
Trond Myklebust49a85062012-09-18 20:43:31 -0400616pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
Fred Isaman4541d162011-01-06 11:36:23 +0000617 struct list_head *tmp_list,
Jeff Layton6d597e12016-05-17 12:28:42 -0400618 const struct pnfs_layout_range *recall_range,
619 u32 seq)
Andy Adamson974cec82010-10-20 00:18:02 -0400620{
621 struct pnfs_layout_segment *lseg, *next;
Trond Myklebust71b39852016-01-04 12:41:15 -0500622 int remaining = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400623
624 dprintk("%s:Begin lo %p\n", __func__, lo);
625
Trond Myklebust8006bfb2012-09-21 14:48:04 -0400626 if (list_empty(&lo->plh_segs))
Fred Isaman38511722011-02-03 18:28:50 +0000627 return 0;
Fred Isaman4541d162011-01-06 11:36:23 +0000628 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Trond Myklebuste036f462016-07-22 11:13:22 -0400629 if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
Jeff Layton6d597e12016-05-17 12:28:42 -0400630 dprintk("%s: freeing lseg %p iomode %d seq %u"
Fred Isaman4541d162011-01-06 11:36:23 +0000631 "offset %llu length %llu\n", __func__,
Jeff Layton6d597e12016-05-17 12:28:42 -0400632 lseg, lseg->pls_range.iomode, lseg->pls_seq,
633 lseg->pls_range.offset, lseg->pls_range.length);
Trond Myklebust71b39852016-01-04 12:41:15 -0500634 if (!mark_lseg_invalid(lseg, tmp_list))
635 remaining++;
Fred Isaman4541d162011-01-06 11:36:23 +0000636 }
Trond Myklebust71b39852016-01-04 12:41:15 -0500637 dprintk("%s:Return %i\n", __func__, remaining);
638 return remaining;
Andy Adamson974cec82010-10-20 00:18:02 -0400639}
640
Trond Myklebust68f74472016-10-12 19:50:54 -0400641static void
642pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
643 struct list_head *free_me,
644 const struct pnfs_layout_range *range,
645 u32 seq)
646{
647 struct pnfs_layout_segment *lseg, *next;
648
649 list_for_each_entry_safe(lseg, next, &lo->plh_return_segs, pls_list) {
650 if (pnfs_match_lseg_recall(lseg, range, seq))
651 list_move_tail(&lseg->pls_list, free_me);
652 }
653}
654
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000655/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000656void
Fred Isaman4541d162011-01-06 11:36:23 +0000657pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400658{
Fred Isaman4541d162011-01-06 11:36:23 +0000659 struct pnfs_layout_segment *lseg, *tmp;
Andy Adamson974cec82010-10-20 00:18:02 -0400660
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000661 if (list_empty(free_me))
662 return;
663
Fred Isaman4541d162011-01-06 11:36:23 +0000664 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000665 list_del(&lseg->pls_list);
Trond Myklebust905ca192012-09-20 20:46:49 -0400666 pnfs_free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400667 }
668}
669
Benny Halevye5e94012010-10-20 00:18:01 -0400670void
671pnfs_destroy_layout(struct nfs_inode *nfsi)
672{
673 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400674 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400675
676 spin_lock(&nfsi->vfs_inode.i_lock);
677 lo = nfsi->layout;
678 if (lo) {
Trond Myklebust3e621212012-09-24 13:07:16 -0400679 pnfs_get_layout_hdr(lo);
Trond Myklebust2454dfe2016-02-22 17:34:59 -0500680 pnfs_mark_layout_stateid_invalid(lo, &tmp_list);
Trond Myklebust3e621212012-09-24 13:07:16 -0400681 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
682 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
683 spin_unlock(&nfsi->vfs_inode.i_lock);
684 pnfs_free_lseg_list(&tmp_list);
685 pnfs_put_layout_hdr(lo);
686 } else
687 spin_unlock(&nfsi->vfs_inode.i_lock);
Benny Halevye5e94012010-10-20 00:18:01 -0400688}
Andy Adamson041245c2012-04-27 17:53:53 -0400689EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
Benny Halevye5e94012010-10-20 00:18:01 -0400690
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500691static bool
692pnfs_layout_add_bulk_destroy_list(struct inode *inode,
693 struct list_head *layout_list)
694{
695 struct pnfs_layout_hdr *lo;
696 bool ret = false;
697
698 spin_lock(&inode->i_lock);
699 lo = NFS_I(inode)->layout;
700 if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
701 pnfs_get_layout_hdr(lo);
702 list_add(&lo->plh_bulk_destroy, layout_list);
703 ret = true;
704 }
705 spin_unlock(&inode->i_lock);
706 return ret;
707}
708
709/* Caller must hold rcu_read_lock and clp->cl_lock */
710static int
711pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
712 struct nfs_server *server,
713 struct list_head *layout_list)
714{
715 struct pnfs_layout_hdr *lo, *next;
716 struct inode *inode;
717
718 list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
719 inode = igrab(lo->plh_inode);
720 if (inode == NULL)
721 continue;
722 list_del_init(&lo->plh_layouts);
723 if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
724 continue;
725 rcu_read_unlock();
726 spin_unlock(&clp->cl_lock);
727 iput(inode);
728 spin_lock(&clp->cl_lock);
729 rcu_read_lock();
730 return -EAGAIN;
731 }
732 return 0;
733}
734
735static int
736pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
737 bool is_bulk_recall)
738{
739 struct pnfs_layout_hdr *lo;
740 struct inode *inode;
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500741 LIST_HEAD(lseg_list);
742 int ret = 0;
743
744 while (!list_empty(layout_list)) {
745 lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
746 plh_bulk_destroy);
747 dprintk("%s freeing layout for inode %lu\n", __func__,
748 lo->plh_inode->i_ino);
749 inode = lo->plh_inode;
Christoph Hellwig7c5d1872014-09-10 08:23:29 -0700750
751 pnfs_layoutcommit_inode(inode, false);
752
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500753 spin_lock(&inode->i_lock);
754 list_del_init(&lo->plh_bulk_destroy);
Trond Myklebust9fd4b9f2016-02-22 17:46:34 -0500755 if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
756 if (is_bulk_recall)
757 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500758 ret = -EAGAIN;
Trond Myklebust9fd4b9f2016-02-22 17:46:34 -0500759 }
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500760 spin_unlock(&inode->i_lock);
761 pnfs_free_lseg_list(&lseg_list);
Trond Myklebustb20135d2015-12-31 09:28:06 -0500762 /* Free all lsegs that are attached to commit buckets */
763 nfs_commit_inode(inode, 0);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500764 pnfs_put_layout_hdr(lo);
765 iput(inode);
766 }
767 return ret;
768}
769
770int
771pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
772 struct nfs_fsid *fsid,
773 bool is_recall)
774{
775 struct nfs_server *server;
776 LIST_HEAD(layout_list);
777
778 spin_lock(&clp->cl_lock);
779 rcu_read_lock();
780restart:
781 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
782 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
783 continue;
784 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
785 server,
786 &layout_list) != 0)
787 goto restart;
788 }
789 rcu_read_unlock();
790 spin_unlock(&clp->cl_lock);
791
792 if (list_empty(&layout_list))
793 return 0;
794 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
795}
796
797int
798pnfs_destroy_layouts_byclid(struct nfs_client *clp,
799 bool is_recall)
800{
801 struct nfs_server *server;
802 LIST_HEAD(layout_list);
803
804 spin_lock(&clp->cl_lock);
805 rcu_read_lock();
806restart:
807 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
808 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
809 server,
810 &layout_list) != 0)
811 goto restart;
812 }
813 rcu_read_unlock();
814 spin_unlock(&clp->cl_lock);
815
816 if (list_empty(&layout_list))
817 return 0;
818 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
819}
820
Andy Adamson974cec82010-10-20 00:18:02 -0400821/*
822 * Called by the state manger to remove all layouts established under an
823 * expired lease.
824 */
825void
826pnfs_destroy_all_layouts(struct nfs_client *clp)
827{
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400828 nfs4_deviceid_mark_client_invalid(clp);
829 nfs4_deviceid_purge_client(clp);
830
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500831 pnfs_destroy_layouts_byclid(clp, false);
Andy Adamson974cec82010-10-20 00:18:02 -0400832}
833
Fred Isamanfd6002e2011-01-06 11:36:22 +0000834/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000835void
836pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
837 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400838{
Trond Myklebustecebb802016-07-24 11:46:06 -0400839 u32 oldseq, newseq, new_barrier = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400840
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500841 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
842 newseq = be32_to_cpu(new->seqid);
Trond Myklebust2a59a042016-09-03 11:20:04 -0400843
844 if (!pnfs_layout_is_valid(lo)) {
845 nfs4_stateid_copy(&lo->plh_stateid, new);
846 lo->plh_barrier = newseq;
847 pnfs_clear_layoutreturn_info(lo);
848 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
849 return;
850 }
851 if (pnfs_seqid_is_newer(newseq, oldseq)) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500852 nfs4_stateid_copy(&lo->plh_stateid, new);
Trond Myklebustecebb802016-07-24 11:46:06 -0400853 /*
854 * Because of wraparound, we want to keep the barrier
855 * "close" to the current seqids.
856 */
857 new_barrier = newseq - atomic_read(&lo->plh_outstanding);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000858 }
Trond Myklebustecebb802016-07-24 11:46:06 -0400859 if (update_barrier)
860 new_barrier = be32_to_cpu(new->seqid);
861 else if (new_barrier == 0)
862 return;
Trond Myklebust2a59a042016-09-03 11:20:04 -0400863 if (pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
Trond Myklebustecebb802016-07-24 11:46:06 -0400864 lo->plh_barrier = new_barrier;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400865}
866
Trond Myklebust19c54ab2012-10-05 16:56:58 -0700867static bool
868pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
869 const nfs4_stateid *stateid)
870{
871 u32 seqid = be32_to_cpu(stateid->seqid);
872
873 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
874}
875
Fred Isamancf7d63f2011-01-06 11:36:25 +0000876/* lget is set to 1 if called from inside send_layoutget call chain */
877static bool
Trond Myklebuste1c06f82015-08-04 16:40:08 -0400878pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000879{
Fred Isamanf7e89172011-01-06 11:36:32 +0000880 return lo->plh_block_lgets ||
Trond Myklebuste1c06f82015-08-04 16:40:08 -0400881 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000882}
883
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400884/*
Jeff Layton183d9e72016-05-17 12:28:47 -0400885 * Get layout from server.
886 * for now, assume that whole file layouts are requested.
887 * arg->offset: 0
888 * arg->length: all ones
889 */
Benny Halevye5e94012010-10-20 00:18:01 -0400890static struct pnfs_layout_segment *
891send_layoutget(struct pnfs_layout_hdr *lo,
892 struct nfs_open_context *ctx,
Jeff Layton183d9e72016-05-17 12:28:47 -0400893 nfs4_stateid *stateid,
Trond Myklebuste144e532016-01-04 12:52:53 -0500894 const struct pnfs_layout_range *range,
Jeff Layton183d9e72016-05-17 12:28:47 -0400895 long *timeout, gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400896{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000897 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400898 struct nfs_server *server = NFS_SERVER(ino);
899 struct nfs4_layoutget *lgp;
Trond Myklebust2d89a1d2015-08-31 02:05:47 -0700900 loff_t i_size;
Benny Halevye5e94012010-10-20 00:18:01 -0400901
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400902 dprintk("--> %s\n", __func__);
903
Jeff Layton4f2e9dc2015-11-25 13:43:14 -0500904 /*
905 * Synchronously retrieve layout information from server and
906 * store in lseg. If we race with a concurrent seqid morphing
907 * op, then re-send the LAYOUTGET.
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400908 */
Jeff Layton83026d802016-05-17 12:28:46 -0400909 lgp = kzalloc(sizeof(*lgp), gfp_flags);
910 if (lgp == NULL)
911 return ERR_PTR(-ENOMEM);
Jeff Layton4f2e9dc2015-11-25 13:43:14 -0500912
Jeff Layton83026d802016-05-17 12:28:46 -0400913 i_size = i_size_read(ino);
Jeff Layton4f2e9dc2015-11-25 13:43:14 -0500914
Jeff Layton83026d802016-05-17 12:28:46 -0400915 lgp->args.minlength = PAGE_SIZE;
916 if (lgp->args.minlength > range->length)
917 lgp->args.minlength = range->length;
918 if (range->iomode == IOMODE_READ) {
919 if (range->offset >= i_size)
920 lgp->args.minlength = 0;
921 else if (i_size - range->offset < lgp->args.minlength)
922 lgp->args.minlength = i_size - range->offset;
Jeff Laytond03ab292016-05-17 12:28:45 -0400923 }
Jeff Layton83026d802016-05-17 12:28:46 -0400924 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
925 pnfs_copy_range(&lgp->args.range, range);
926 lgp->args.type = server->pnfs_curr_ld->id;
927 lgp->args.inode = ino;
928 lgp->args.ctx = get_nfs_open_context(ctx);
Jeff Layton183d9e72016-05-17 12:28:47 -0400929 nfs4_stateid_copy(&lgp->args.stateid, stateid);
Jeff Layton83026d802016-05-17 12:28:46 -0400930 lgp->gfp_flags = gfp_flags;
931 lgp->cred = lo->plh_lc_cred;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400932
Jeff Layton183d9e72016-05-17 12:28:47 -0400933 return nfs4_proc_layoutget(lgp, timeout, gfp_flags);
Andy Adamson974cec82010-10-20 00:18:02 -0400934}
935
Trond Myklebust24956802013-03-20 13:03:00 -0400936static void pnfs_clear_layoutcommit(struct inode *inode,
937 struct list_head *head)
938{
939 struct nfs_inode *nfsi = NFS_I(inode);
940 struct pnfs_layout_segment *lseg, *tmp;
941
942 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
943 return;
944 list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
945 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
946 continue;
947 pnfs_lseg_dec_and_remove_zero(lseg, head);
948 }
949}
950
Trond Myklebust68f74472016-10-12 19:50:54 -0400951static void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
Tom Haynesd67ae822014-12-11 17:02:04 -0500952{
953 clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
Trond Myklebust6604b202016-10-17 17:54:32 -0400954 clear_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags);
Tom Haynesd67ae822014-12-11 17:02:04 -0500955 smp_mb__after_atomic();
956 wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
Trond Myklebust7f273922015-07-09 18:40:01 +0200957 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
Tom Haynesd67ae822014-12-11 17:02:04 -0500958}
959
Trond Myklebust68f74472016-10-12 19:50:54 -0400960void pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr *lo,
Trond Myklebust2a974422016-11-20 13:13:54 -0500961 const nfs4_stateid *arg_stateid,
Trond Myklebust68f74472016-10-12 19:50:54 -0400962 const struct pnfs_layout_range *range,
Trond Myklebust68f74472016-10-12 19:50:54 -0400963 const nfs4_stateid *stateid)
964{
965 struct inode *inode = lo->plh_inode;
966 LIST_HEAD(freeme);
967
968 spin_lock(&inode->i_lock);
Trond Myklebust2a974422016-11-20 13:13:54 -0500969 if (!pnfs_layout_is_valid(lo) || !arg_stateid ||
970 !nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid))
971 goto out_unlock;
Trond Myklebust68f74472016-10-12 19:50:54 -0400972 if (stateid) {
Trond Myklebust2a974422016-11-20 13:13:54 -0500973 u32 seq = be32_to_cpu(arg_stateid->seqid);
974
Trond Myklebust68f74472016-10-12 19:50:54 -0400975 pnfs_mark_matching_lsegs_invalid(lo, &freeme, range, seq);
976 pnfs_free_returned_lsegs(lo, &freeme, range, seq);
977 pnfs_set_layout_stateid(lo, stateid, true);
978 } else
979 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Trond Myklebust2a974422016-11-20 13:13:54 -0500980out_unlock:
Trond Myklebust68f74472016-10-12 19:50:54 -0400981 pnfs_clear_layoutreturn_waitbit(lo);
982 spin_unlock(&inode->i_lock);
983 pnfs_free_lseg_list(&freeme);
984
985}
986
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -0500987static void
988pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
989 u32 seq)
990{
991 if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
992 iomode = IOMODE_ANY;
993 lo->plh_return_iomode = iomode;
994 set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
995 if (seq != 0) {
996 WARN_ON_ONCE(lo->plh_return_seq != 0 && lo->plh_return_seq != seq);
997 lo->plh_return_seq = seq;
998 }
999}
1000
Trond Myklebust13c13a62016-01-26 23:12:11 -05001001static bool
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001002pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
1003 nfs4_stateid *stateid,
1004 enum pnfs_iomode *iomode)
Trond Myklebust13c13a62016-01-26 23:12:11 -05001005{
Trond Myklebustbf0291d2016-09-03 10:39:51 -04001006 /* Serialise LAYOUTGET/LAYOUTRETURN */
1007 if (atomic_read(&lo->plh_outstanding) != 0)
1008 return false;
Trond Myklebust6604b202016-10-17 17:54:32 -04001009 if (test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
Trond Myklebust13c13a62016-01-26 23:12:11 -05001010 return false;
Trond Myklebust6604b202016-10-17 17:54:32 -04001011 set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
Trond Myklebust13c13a62016-01-26 23:12:11 -05001012 pnfs_get_layout_hdr(lo);
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001013 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
1014 if (stateid != NULL) {
1015 nfs4_stateid_copy(stateid, &lo->plh_stateid);
1016 if (lo->plh_return_seq != 0)
1017 stateid->seqid = cpu_to_be32(lo->plh_return_seq);
1018 }
1019 if (iomode != NULL)
1020 *iomode = lo->plh_return_iomode;
1021 pnfs_clear_layoutreturn_info(lo);
1022 return true;
1023 }
1024 if (stateid != NULL)
1025 nfs4_stateid_copy(stateid, &lo->plh_stateid);
1026 if (iomode != NULL)
1027 *iomode = IOMODE_ANY;
Trond Myklebust13c13a62016-01-26 23:12:11 -05001028 return true;
1029}
1030
Trond Myklebust828ed9e2016-11-15 21:47:27 -05001031static void
1032pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args *args,
1033 struct pnfs_layout_hdr *lo,
1034 const nfs4_stateid *stateid,
1035 enum pnfs_iomode iomode)
1036{
1037 struct inode *inode = lo->plh_inode;
1038
1039 args->layout_type = NFS_SERVER(inode)->pnfs_curr_ld->id;
1040 args->inode = inode;
1041 args->range.iomode = iomode;
1042 args->range.offset = 0;
1043 args->range.length = NFS4_MAX_UINT64;
1044 args->layout = lo;
1045 nfs4_stateid_copy(&args->stateid, stateid);
1046}
1047
Peng Taof40eb5d2014-09-06 00:53:22 +08001048static int
Trond Myklebusted429d62016-01-04 12:30:55 -05001049pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, const nfs4_stateid *stateid,
Peng Tao6c166052014-11-17 09:30:40 +08001050 enum pnfs_iomode iomode, bool sync)
Peng Taof40eb5d2014-09-06 00:53:22 +08001051{
1052 struct inode *ino = lo->plh_inode;
1053 struct nfs4_layoutreturn *lrp;
1054 int status = 0;
1055
Trond Myklebuste4af4402015-02-05 17:05:08 -05001056 lrp = kzalloc(sizeof(*lrp), GFP_NOFS);
Peng Taof40eb5d2014-09-06 00:53:22 +08001057 if (unlikely(lrp == NULL)) {
1058 status = -ENOMEM;
1059 spin_lock(&ino->i_lock);
Tom Haynesd67ae822014-12-11 17:02:04 -05001060 pnfs_clear_layoutreturn_waitbit(lo);
Peng Taof40eb5d2014-09-06 00:53:22 +08001061 spin_unlock(&ino->i_lock);
1062 pnfs_put_layout_hdr(lo);
1063 goto out;
1064 }
1065
Trond Myklebust828ed9e2016-11-15 21:47:27 -05001066 pnfs_init_layoutreturn_args(&lrp->args, lo, stateid, iomode);
Peng Taof40eb5d2014-09-06 00:53:22 +08001067 lrp->clp = NFS_SERVER(ino)->nfs_client;
1068 lrp->cred = lo->plh_lc_cred;
1069
Peng Tao6c166052014-11-17 09:30:40 +08001070 status = nfs4_proc_layoutreturn(lrp, sync);
Peng Taof40eb5d2014-09-06 00:53:22 +08001071out:
1072 dprintk("<-- %s status: %d\n", __func__, status);
1073 return status;
1074}
1075
Trond Myklebust13c13a62016-01-26 23:12:11 -05001076/* Return true if layoutreturn is needed */
1077static bool
1078pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
1079{
1080 struct pnfs_layout_segment *s;
1081
Trond Myklebust2370abd2016-01-27 20:32:50 -05001082 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
Trond Myklebust13c13a62016-01-26 23:12:11 -05001083 return false;
1084
1085 /* Defer layoutreturn until all lsegs are done */
1086 list_for_each_entry(s, &lo->plh_segs, pls_list) {
1087 if (test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
1088 return false;
1089 }
1090
1091 return true;
1092}
1093
1094static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
1095{
1096 struct inode *inode= lo->plh_inode;
1097
Trond Myklebust2370abd2016-01-27 20:32:50 -05001098 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
Trond Myklebust13c13a62016-01-26 23:12:11 -05001099 return;
1100 spin_lock(&inode->i_lock);
1101 if (pnfs_layout_need_return(lo)) {
1102 nfs4_stateid stateid;
1103 enum pnfs_iomode iomode;
1104 bool send;
1105
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001106 send = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
Trond Myklebust13c13a62016-01-26 23:12:11 -05001107 spin_unlock(&inode->i_lock);
1108 if (send) {
1109 /* Send an async layoutreturn so we dont deadlock */
1110 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1111 }
1112 } else
1113 spin_unlock(&inode->i_lock);
1114}
1115
Andy Adamson293b3b02012-06-20 15:03:34 -04001116/*
1117 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
1118 * when the layout segment list is empty.
1119 *
1120 * Note that a pnfs_layout_hdr can exist with an empty layout segment
1121 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
1122 * deviceid is marked invalid.
1123 */
Benny Halevycbe82602011-05-22 19:52:37 +03001124int
1125_pnfs_return_layout(struct inode *ino)
1126{
1127 struct pnfs_layout_hdr *lo = NULL;
1128 struct nfs_inode *nfsi = NFS_I(ino);
1129 LIST_HEAD(tmp_list);
Benny Halevycbe82602011-05-22 19:52:37 +03001130 nfs4_stateid stateid;
Andy Adamson293b3b02012-06-20 15:03:34 -04001131 int status = 0, empty;
Trond Myklebust7f273922015-07-09 18:40:01 +02001132 bool send;
Benny Halevycbe82602011-05-22 19:52:37 +03001133
Andy Adamson366d5052012-06-20 15:03:33 -04001134 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
Benny Halevycbe82602011-05-22 19:52:37 +03001135
1136 spin_lock(&ino->i_lock);
1137 lo = nfsi->layout;
Trond Myklebuste5929f32012-09-21 16:37:02 -04001138 if (!lo) {
Benny Halevycbe82602011-05-22 19:52:37 +03001139 spin_unlock(&ino->i_lock);
Andy Adamson293b3b02012-06-20 15:03:34 -04001140 dprintk("NFS: %s no layout to return\n", __func__);
1141 goto out;
Benny Halevycbe82602011-05-22 19:52:37 +03001142 }
Benny Halevycbe82602011-05-22 19:52:37 +03001143 /* Reference matched in nfs4_layoutreturn_release */
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001144 pnfs_get_layout_hdr(lo);
Andy Adamson293b3b02012-06-20 15:03:34 -04001145 empty = list_empty(&lo->plh_segs);
Trond Myklebust24956802013-03-20 13:03:00 -04001146 pnfs_clear_layoutcommit(ino, &tmp_list);
Jeff Layton6d597e12016-05-17 12:28:42 -04001147 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL, 0);
Christoph Hellwigc88953d2014-09-10 08:23:31 -07001148
1149 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
1150 struct pnfs_layout_range range = {
1151 .iomode = IOMODE_ANY,
1152 .offset = 0,
1153 .length = NFS4_MAX_UINT64,
1154 };
1155 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1156 }
1157
Andy Adamson293b3b02012-06-20 15:03:34 -04001158 /* Don't send a LAYOUTRETURN if list was initially empty */
1159 if (empty) {
1160 spin_unlock(&ino->i_lock);
Andy Adamson293b3b02012-06-20 15:03:34 -04001161 dprintk("NFS: %s no layout segments to return\n", __func__);
Trond Myklebust7f273922015-07-09 18:40:01 +02001162 goto out_put_layout_hdr;
Andy Adamson293b3b02012-06-20 15:03:34 -04001163 }
Christoph Hellwig47abade2014-08-21 11:09:22 -05001164
Trond Myklebuste5fd1902016-07-21 12:44:15 -04001165 send = pnfs_prepare_layoutreturn(lo, &stateid, NULL);
Benny Halevycbe82602011-05-22 19:52:37 +03001166 spin_unlock(&ino->i_lock);
1167 pnfs_free_lseg_list(&tmp_list);
Trond Myklebust7f273922015-07-09 18:40:01 +02001168 if (send)
Trond Myklebusted429d62016-01-04 12:30:55 -05001169 status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
Trond Myklebust7f273922015-07-09 18:40:01 +02001170out_put_layout_hdr:
1171 pnfs_put_layout_hdr(lo);
Benny Halevycbe82602011-05-22 19:52:37 +03001172out:
1173 dprintk("<-- %s status: %d\n", __func__, status);
1174 return status;
1175}
Andy Adamson0a57cda2012-04-27 17:53:50 -04001176EXPORT_SYMBOL_GPL(_pnfs_return_layout);
Benny Halevycbe82602011-05-22 19:52:37 +03001177
Trond Myklebust24028672013-03-20 13:23:33 -04001178int
1179pnfs_commit_and_return_layout(struct inode *inode)
1180{
1181 struct pnfs_layout_hdr *lo;
1182 int ret;
1183
1184 spin_lock(&inode->i_lock);
1185 lo = NFS_I(inode)->layout;
1186 if (lo == NULL) {
1187 spin_unlock(&inode->i_lock);
1188 return 0;
1189 }
1190 pnfs_get_layout_hdr(lo);
1191 /* Block new layoutgets and read/write to ds */
1192 lo->plh_block_lgets++;
1193 spin_unlock(&inode->i_lock);
1194 filemap_fdatawait(inode->i_mapping);
1195 ret = pnfs_layoutcommit_inode(inode, true);
1196 if (ret == 0)
1197 ret = _pnfs_return_layout(inode);
1198 spin_lock(&inode->i_lock);
1199 lo->plh_block_lgets--;
1200 spin_unlock(&inode->i_lock);
1201 pnfs_put_layout_hdr(lo);
1202 return ret;
1203}
1204
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05001205bool pnfs_roc(struct inode *ino,
1206 struct nfs4_layoutreturn_args *args,
1207 struct nfs4_layoutreturn_res *res,
1208 const struct rpc_cred *cred)
Fred Isamanf7e89172011-01-06 11:36:32 +00001209{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001210 struct nfs_inode *nfsi = NFS_I(ino);
1211 struct nfs_open_context *ctx;
1212 struct nfs4_state *state;
Fred Isamanf7e89172011-01-06 11:36:32 +00001213 struct pnfs_layout_hdr *lo;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05001214 struct pnfs_layout_segment *lseg, *next;
Peng Tao193e3aa2014-11-17 09:30:41 +08001215 nfs4_stateid stateid;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05001216 enum pnfs_iomode iomode = 0;
1217 bool layoutreturn = false, roc = false;
Fred Isamanf7e89172011-01-06 11:36:32 +00001218
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05001219 if (!nfs_have_layout(ino))
1220 return false;
Fred Isamanf7e89172011-01-06 11:36:32 +00001221 spin_lock(&ino->i_lock);
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001222 lo = nfsi->layout;
Trond Myklebust0cdc3292016-11-21 11:05:33 -05001223 if (!lo || !pnfs_layout_is_valid(lo) ||
1224 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001225 goto out_noroc;
1226
Peng Taoe755d632015-08-19 13:49:19 +08001227 /* no roc if we hold a delegation */
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001228 if (nfs4_check_delegation(ino, FMODE_READ))
1229 goto out_noroc;
1230
1231 list_for_each_entry(ctx, &nfsi->open_files, list) {
1232 state = ctx->state;
1233 /* Don't return layout if there is open file state */
1234 if (state != NULL && state->state != 0)
1235 goto out_noroc;
1236 }
1237
Peng Taoe755d632015-08-19 13:49:19 +08001238
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05001239 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) {
Peng Taoe755d632015-08-19 13:49:19 +08001240 /* If we are sending layoutreturn, invalidate all valid lsegs */
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05001241 if (!test_and_clear_bit(NFS_LSEG_ROC, &lseg->pls_flags))
1242 continue;
1243 /*
1244 * Note: mark lseg for return so pnfs_layout_remove_lseg
1245 * doesn't invalidate the layout for us.
1246 */
1247 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
1248 if (!mark_lseg_invalid(lseg, &lo->plh_return_segs))
1249 continue;
1250 pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
Trond Myklebust69820d22016-11-15 18:29:59 -05001251 }
1252
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05001253 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1254 goto out_noroc;
Trond Myklebust69820d22016-11-15 18:29:59 -05001255
Peng Tao500d7012015-09-22 11:35:22 +08001256 /* ROC in two conditions:
Peng Taoe755d632015-08-19 13:49:19 +08001257 * 1. there are ROC lsegs
1258 * 2. we don't send layoutreturn
Peng Taoe755d632015-08-19 13:49:19 +08001259 */
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05001260 /* lo ref dropped in pnfs_roc_release() */
1261 layoutreturn = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
1262 /* If the creds don't match, we can't compound the layoutreturn */
1263 if (!layoutreturn || cred != lo->plh_lc_cred)
1264 goto out_noroc;
1265
1266 roc = layoutreturn;
1267 pnfs_init_layoutreturn_args(args, lo, &stateid, iomode);
1268 res->lrs_present = 0;
1269 layoutreturn = false;
Peng Taoe755d632015-08-19 13:49:19 +08001270
1271out_noroc:
Fred Isamanf7e89172011-01-06 11:36:32 +00001272 spin_unlock(&ino->i_lock);
Trond Myklebust71401712015-03-25 12:36:13 -04001273 pnfs_layoutcommit_inode(ino, true);
Peng Taoe755d632015-08-19 13:49:19 +08001274 if (layoutreturn)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05001275 pnfs_send_layoutreturn(lo, &stateid, iomode, true);
Peng Taoe755d632015-08-19 13:49:19 +08001276 return roc;
Fred Isamanf7e89172011-01-06 11:36:32 +00001277}
1278
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05001279void pnfs_roc_release(struct nfs4_layoutreturn_args *args,
1280 struct nfs4_layoutreturn_res *res,
1281 int ret)
Fred Isamanf7e89172011-01-06 11:36:32 +00001282{
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05001283 struct pnfs_layout_hdr *lo = args->layout;
1284 const nfs4_stateid *arg_stateid = NULL;
1285 const nfs4_stateid *res_stateid = NULL;
Fred Isamanf7e89172011-01-06 11:36:32 +00001286
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05001287 if (ret == 0) {
1288 arg_stateid = &args->stateid;
1289 if (res->lrs_present)
1290 res_stateid = &res->stateid;
1291 }
1292 pnfs_layoutreturn_free_lsegs(lo, arg_stateid, &args->range,
1293 res_stateid);
1294 pnfs_put_layout_hdr(lo);
1295 trace_nfs4_layoutreturn_on_close(args->inode, 0);
Fred Isamanf7e89172011-01-06 11:36:32 +00001296}
1297
Peng Tao500d7012015-09-22 11:35:22 +08001298bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
1299{
1300 struct nfs_inode *nfsi = NFS_I(ino);
1301 struct pnfs_layout_hdr *lo;
1302 bool sleep = false;
1303
1304 /* we might not have grabbed lo reference. so need to check under
1305 * i_lock */
1306 spin_lock(&ino->i_lock);
1307 lo = nfsi->layout;
Trond Myklebustee284e32016-11-18 15:21:30 -05001308 if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
Peng Tao500d7012015-09-22 11:35:22 +08001309 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
Trond Myklebustee284e32016-11-18 15:21:30 -05001310 sleep = true;
1311 }
1312 spin_unlock(&ino->i_lock);
Peng Tao500d7012015-09-22 11:35:22 +08001313 return sleep;
1314}
1315
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001316/*
1317 * Compare two layout segments for sorting into layout cache.
1318 * We want to preferentially return RW over RO layouts, so ensure those
1319 * are seen first.
1320 */
1321static s64
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001322pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001323 const struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001324{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001325 s64 d;
1326
1327 /* high offset > low offset */
1328 d = l1->offset - l2->offset;
1329 if (d)
1330 return d;
1331
1332 /* short length > long length */
1333 d = l2->length - l1->length;
1334 if (d)
1335 return d;
1336
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001337 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +03001338 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001339}
1340
Trond Myklebust03772d22015-08-25 08:54:17 -04001341static bool
1342pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
1343 const struct pnfs_layout_range *l2)
Andy Adamson974cec82010-10-20 00:18:02 -04001344{
Trond Myklebust03772d22015-08-25 08:54:17 -04001345 return pnfs_lseg_range_cmp(l1, l2) > 0;
1346}
1347
1348static bool
1349pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
1350 struct pnfs_layout_segment *old)
1351{
1352 return false;
1353}
1354
1355void
1356pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1357 struct pnfs_layout_segment *lseg,
1358 bool (*is_after)(const struct pnfs_layout_range *,
1359 const struct pnfs_layout_range *),
1360 bool (*do_merge)(struct pnfs_layout_segment *,
1361 struct pnfs_layout_segment *),
1362 struct list_head *free_me)
1363{
1364 struct pnfs_layout_segment *lp, *tmp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001365
Andy Adamson974cec82010-10-20 00:18:02 -04001366 dprintk("%s:Begin\n", __func__);
1367
Trond Myklebust03772d22015-08-25 08:54:17 -04001368 list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
1369 if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
1370 continue;
1371 if (do_merge(lseg, lp)) {
1372 mark_lseg_invalid(lp, free_me);
1373 continue;
1374 }
1375 if (is_after(&lseg->pls_range, &lp->pls_range))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001376 continue;
Fred Isaman566052c2011-01-06 11:36:20 +00001377 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001378 dprintk("%s: inserted lseg %p "
1379 "iomode %d offset %llu length %llu before "
1380 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +00001381 __func__, lseg, lseg->pls_range.iomode,
1382 lseg->pls_range.offset, lseg->pls_range.length,
1383 lp, lp->pls_range.iomode, lp->pls_range.offset,
1384 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +03001385 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -04001386 }
Benny Halevyfb3296e2011-05-22 19:47:26 +03001387 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1388 dprintk("%s: inserted lseg %p "
1389 "iomode %d offset %llu length %llu at tail\n",
1390 __func__, lseg, lseg->pls_range.iomode,
1391 lseg->pls_range.offset, lseg->pls_range.length);
1392out:
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001393 pnfs_get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -04001394
1395 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001396}
Trond Myklebust03772d22015-08-25 08:54:17 -04001397EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
1398
1399static void
1400pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1401 struct pnfs_layout_segment *lseg,
1402 struct list_head *free_me)
1403{
1404 struct inode *inode = lo->plh_inode;
1405 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
1406
1407 if (ld->add_lseg != NULL)
1408 ld->add_lseg(lo, lseg, free_me);
1409 else
1410 pnfs_generic_layout_insert_lseg(lo, lseg,
1411 pnfs_lseg_range_is_after,
1412 pnfs_lseg_no_merge,
1413 free_me);
1414}
Benny Halevye5e94012010-10-20 00:18:01 -04001415
1416static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -04001417alloc_init_layout_hdr(struct inode *ino,
1418 struct nfs_open_context *ctx,
1419 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001420{
1421 struct pnfs_layout_hdr *lo;
1422
Benny Halevy636fb9c2011-05-22 19:51:33 +03001423 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -04001424 if (!lo)
1425 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +00001426 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001427 INIT_LIST_HEAD(&lo->plh_layouts);
1428 INIT_LIST_HEAD(&lo->plh_segs);
Trond Myklebust68f74472016-10-12 19:50:54 -04001429 INIT_LIST_HEAD(&lo->plh_return_segs);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -05001430 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001431 lo->plh_inode = ino;
Trond Myklebust5cc22162013-05-21 09:26:49 -04001432 lo->plh_lc_cred = get_rpccred(ctx->cred);
Trond Myklebust67a3b722016-06-17 16:48:19 -04001433 lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
Benny Halevye5e94012010-10-20 00:18:01 -04001434 return lo;
1435}
1436
1437static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -04001438pnfs_find_alloc_layout(struct inode *ino,
1439 struct nfs_open_context *ctx,
1440 gfp_t gfp_flags)
Trond Myklebuste5241e42016-06-17 16:48:20 -04001441 __releases(&ino->i_lock)
1442 __acquires(&ino->i_lock)
Benny Halevye5e94012010-10-20 00:18:01 -04001443{
1444 struct nfs_inode *nfsi = NFS_I(ino);
1445 struct pnfs_layout_hdr *new = NULL;
1446
1447 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1448
Trond Myklebust251ec412012-10-02 15:41:05 -07001449 if (nfsi->layout != NULL)
1450 goto out_existing;
Benny Halevye5e94012010-10-20 00:18:01 -04001451 spin_unlock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001452 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -04001453 spin_lock(&ino->i_lock);
1454
Trond Myklebust251ec412012-10-02 15:41:05 -07001455 if (likely(nfsi->layout == NULL)) { /* Won the race? */
Benny Halevye5e94012010-10-20 00:18:01 -04001456 nfsi->layout = new;
Trond Myklebust251ec412012-10-02 15:41:05 -07001457 return new;
Yanchuan Nian7175fe92012-10-31 16:05:48 +08001458 } else if (new != NULL)
1459 pnfs_free_layout_hdr(new);
Trond Myklebust251ec412012-10-02 15:41:05 -07001460out_existing:
1461 pnfs_get_layout_hdr(nfsi->layout);
Benny Halevye5e94012010-10-20 00:18:01 -04001462 return nfsi->layout;
1463}
1464
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001465/*
1466 * iomode matching rules:
Tom Haynesc7d73af2016-05-25 07:31:14 -07001467 * iomode lseg strict match
1468 * iomode
1469 * ----- ----- ------ -----
1470 * ANY READ N/A true
1471 * ANY RW N/A true
1472 * RW READ N/A false
1473 * RW RW N/A true
1474 * READ READ N/A true
1475 * READ RW true false
1476 * READ RW false true
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001477 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001478static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001479pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001480 const struct pnfs_layout_range *range,
1481 bool strict_iomode)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001482{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001483 struct pnfs_layout_range range1;
1484
1485 if ((range->iomode == IOMODE_RW &&
1486 ls_range->iomode != IOMODE_RW) ||
Tom Haynesc7d73af2016-05-25 07:31:14 -07001487 (range->iomode != ls_range->iomode &&
1488 strict_iomode == true) ||
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001489 !pnfs_lseg_range_intersecting(ls_range, range))
Benny Halevyfb3296e2011-05-22 19:47:26 +03001490 return 0;
1491
1492 /* range1 covers only the first byte in the range */
1493 range1 = *range;
1494 range1.length = 1;
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001495 return pnfs_lseg_range_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001496}
1497
1498/*
1499 * lookup range in layout
1500 */
Benny Halevye5e94012010-10-20 00:18:01 -04001501static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +03001502pnfs_find_lseg(struct pnfs_layout_hdr *lo,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001503 struct pnfs_layout_range *range,
1504 bool strict_iomode)
Benny Halevye5e94012010-10-20 00:18:01 -04001505{
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001506 struct pnfs_layout_segment *lseg, *ret = NULL;
1507
1508 dprintk("%s:Begin\n", __func__);
1509
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001510 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +00001511 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Peng Taoce6ab4f2014-09-06 00:53:24 +08001512 !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
Tom Haynesc7d73af2016-05-25 07:31:14 -07001513 pnfs_lseg_range_match(&lseg->pls_range, range,
1514 strict_iomode)) {
Trond Myklebust9369a432012-09-18 20:57:08 -04001515 ret = pnfs_get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001516 break;
1517 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001518 }
1519
1520 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +00001521 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001522 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -04001523}
1524
1525/*
Andy Adamsond23d61c2012-05-23 05:02:37 -04001526 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1527 * to the MDS or over pNFS
1528 *
1529 * The nfs_inode read_io and write_io fields are cumulative counters reset
1530 * when there are no layout segments. Note that in pnfs_update_layout iomode
1531 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1532 * WRITE request.
1533 *
1534 * A return of true means use MDS I/O.
1535 *
1536 * From rfc 5661:
1537 * If a file's size is smaller than the file size threshold, data accesses
1538 * SHOULD be sent to the metadata server. If an I/O request has a length that
1539 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1540 * server. If both file size and I/O size are provided, the client SHOULD
1541 * reach or exceed both thresholds before sending its read or write
1542 * requests to the data server.
1543 */
1544static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1545 struct inode *ino, int iomode)
1546{
1547 struct nfs4_threshold *t = ctx->mdsthreshold;
1548 struct nfs_inode *nfsi = NFS_I(ino);
1549 loff_t fsize = i_size_read(ino);
1550 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1551
1552 if (t == NULL)
1553 return ret;
1554
1555 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1556 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1557
1558 switch (iomode) {
1559 case IOMODE_READ:
1560 if (t->bm & THRESHOLD_RD) {
1561 dprintk("%s fsize %llu\n", __func__, fsize);
1562 size_set = true;
1563 if (fsize < t->rd_sz)
1564 size = true;
1565 }
1566 if (t->bm & THRESHOLD_RD_IO) {
1567 dprintk("%s nfsi->read_io %llu\n", __func__,
1568 nfsi->read_io);
1569 io_set = true;
1570 if (nfsi->read_io < t->rd_io_sz)
1571 io = true;
1572 }
1573 break;
1574 case IOMODE_RW:
1575 if (t->bm & THRESHOLD_WR) {
1576 dprintk("%s fsize %llu\n", __func__, fsize);
1577 size_set = true;
1578 if (fsize < t->wr_sz)
1579 size = true;
1580 }
1581 if (t->bm & THRESHOLD_WR_IO) {
1582 dprintk("%s nfsi->write_io %llu\n", __func__,
1583 nfsi->write_io);
1584 io_set = true;
1585 if (nfsi->write_io < t->wr_io_sz)
1586 io = true;
1587 }
1588 break;
1589 }
1590 if (size_set && io_set) {
1591 if (size && io)
1592 ret = true;
1593 } else if (size || io)
1594 ret = true;
1595
1596 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1597 return ret;
1598}
1599
Peng Taoaa8a45e2014-12-01 08:22:23 +08001600static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1601{
1602 /*
1603 * send layoutcommit as it can hold up layoutreturn due to lseg
1604 * reference
1605 */
1606 pnfs_layoutcommit_inode(lo->plh_inode, false);
1607 return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
Trond Myklebust2e5b29f2015-12-14 16:25:11 -05001608 nfs_wait_bit_killable,
Peng Taoaa8a45e2014-12-01 08:22:23 +08001609 TASK_UNINTERRUPTIBLE);
1610}
1611
Tom Haynesd67ae822014-12-11 17:02:04 -05001612static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1613{
1614 unsigned long *bitlock = &lo->plh_flags;
1615
1616 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1617 smp_mb__after_atomic();
1618 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1619}
1620
Andy Adamsond23d61c2012-05-23 05:02:37 -04001621/*
Benny Halevye5e94012010-10-20 00:18:01 -04001622 * Layout segment is retreived from the server if not cached.
1623 * The appropriate layout segment is referenced and returned to the caller.
1624 */
Andy Adamson7c24d942011-06-13 18:22:38 -04001625struct pnfs_layout_segment *
Benny Halevye5e94012010-10-20 00:18:01 -04001626pnfs_update_layout(struct inode *ino,
1627 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +03001628 loff_t pos,
1629 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001630 enum pnfs_iomode iomode,
Tom Haynesc7d73af2016-05-25 07:31:14 -07001631 bool strict_iomode,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001632 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001633{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001634 struct pnfs_layout_range arg = {
1635 .iomode = iomode,
1636 .offset = pos,
1637 .length = count,
1638 };
Jeff Layton183d9e72016-05-17 12:28:47 -04001639 unsigned pg_offset, seq;
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001640 struct nfs_server *server = NFS_SERVER(ino);
1641 struct nfs_client *clp = server->nfs_client;
Jeff Layton183d9e72016-05-17 12:28:47 -04001642 struct pnfs_layout_hdr *lo = NULL;
Benny Halevye5e94012010-10-20 00:18:01 -04001643 struct pnfs_layout_segment *lseg = NULL;
Jeff Layton183d9e72016-05-17 12:28:47 -04001644 nfs4_stateid stateid;
1645 long timeout = 0;
Trond Myklebust66b53f32016-07-14 14:28:31 -04001646 unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
Weston Andros Adamson30005122013-02-28 20:30:10 -05001647 bool first;
Benny Halevye5e94012010-10-20 00:18:01 -04001648
Jeff Layton9a4bf312015-12-10 10:41:58 -05001649 if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001650 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001651 PNFS_UPDATE_LAYOUT_NO_PNFS);
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001652 goto out;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001653 }
Andy Adamsond23d61c2012-05-23 05:02:37 -04001654
Jeff Layton9a4bf312015-12-10 10:41:58 -05001655 if (iomode == IOMODE_READ && i_size_read(ino) == 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001656 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001657 PNFS_UPDATE_LAYOUT_RD_ZEROLEN);
Trond Myklebust4ae935602015-08-31 01:25:11 -07001658 goto out;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001659 }
Trond Myklebust4ae935602015-08-31 01:25:11 -07001660
Jeff Layton9a4bf312015-12-10 10:41:58 -05001661 if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001662 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001663 PNFS_UPDATE_LAYOUT_MDSTHRESH);
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001664 goto out;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001665 }
Andy Adamsond23d61c2012-05-23 05:02:37 -04001666
Peng Tao9bf87482014-08-22 17:37:41 +08001667lookup_again:
Trond Myklebustb88fa692016-08-23 11:19:33 -04001668 nfs4_client_recover_expired_lease(clp);
Peng Tao9bf87482014-08-22 17:37:41 +08001669 first = false;
Benny Halevye5e94012010-10-20 00:18:01 -04001670 spin_lock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001671 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001672 if (lo == NULL) {
1673 spin_unlock(&ino->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04001674 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001675 PNFS_UPDATE_LAYOUT_NOMEM);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001676 goto out;
1677 }
Benny Halevye5e94012010-10-20 00:18:01 -04001678
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001679 /* Do we even need to bother with this? */
Trond Myklebusta59c30a2012-03-01 11:17:47 -05001680 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001681 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001682 PNFS_UPDATE_LAYOUT_BULK_RECALL);
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001683 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001684 goto out_unlock;
1685 }
1686
1687 /* if LAYOUTGET already failed once we don't try again */
Trond Myklebust2e5b29f2015-12-14 16:25:11 -05001688 if (pnfs_layout_io_test_failed(lo, iomode)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001689 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001690 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
Benny Halevye5e94012010-10-20 00:18:01 -04001691 goto out_unlock;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001692 }
Benny Halevye5e94012010-10-20 00:18:01 -04001693
Tom Haynesc7d73af2016-05-25 07:31:14 -07001694 lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
Jeff Layton183d9e72016-05-17 12:28:47 -04001695 if (lseg) {
1696 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1697 PNFS_UPDATE_LAYOUT_FOUND_CACHED);
1698 goto out_unlock;
1699 }
1700
1701 if (!nfs4_valid_open_stateid(ctx->state)) {
1702 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1703 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
1704 goto out_unlock;
1705 }
1706
1707 /*
1708 * Choose a stateid for the LAYOUTGET. If we don't have a layout
1709 * stateid, or it has been invalidated, then we must use the open
1710 * stateid.
1711 */
Trond Myklebust67a3b722016-06-17 16:48:19 -04001712 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001713
1714 /*
1715 * The first layoutget for the file. Need to serialize per
Peng Tao9bf87482014-08-22 17:37:41 +08001716 * RFC 5661 Errata 3208.
1717 */
1718 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1719 &lo->plh_flags)) {
1720 spin_unlock(&ino->i_lock);
1721 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
1722 TASK_UNINTERRUPTIBLE);
1723 pnfs_put_layout_hdr(lo);
Jeff Layton183d9e72016-05-17 12:28:47 -04001724 dprintk("%s retrying\n", __func__);
Peng Tao9bf87482014-08-22 17:37:41 +08001725 goto lookup_again;
1726 }
Jeff Layton183d9e72016-05-17 12:28:47 -04001727
1728 first = true;
1729 do {
1730 seq = read_seqbegin(&ctx->state->seqlock);
1731 nfs4_stateid_copy(&stateid, &ctx->state->stateid);
1732 } while (read_seqretry(&ctx->state->seqlock, seq));
Peng Tao9bf87482014-08-22 17:37:41 +08001733 } else {
Jeff Layton183d9e72016-05-17 12:28:47 -04001734 nfs4_stateid_copy(&stateid, &lo->plh_stateid);
Peng Tao9bf87482014-08-22 17:37:41 +08001735 }
Andy Adamson568e8c42011-03-01 01:34:22 +00001736
Peng Taoaa8a45e2014-12-01 08:22:23 +08001737 /*
1738 * Because we free lsegs before sending LAYOUTRETURN, we need to wait
1739 * for LAYOUTRETURN even if first is true.
1740 */
Trond Myklebust8f70f532015-08-04 16:09:44 -04001741 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
Peng Taoaa8a45e2014-12-01 08:22:23 +08001742 spin_unlock(&ino->i_lock);
1743 dprintk("%s wait for layoutreturn\n", __func__);
1744 if (pnfs_prepare_to_retry_layoutget(lo)) {
Tom Haynesd67ae822014-12-11 17:02:04 -05001745 if (first)
1746 pnfs_clear_first_layoutget(lo);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001747 pnfs_put_layout_hdr(lo);
1748 dprintk("%s retrying\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04001749 trace_pnfs_update_layout(ino, pos, count, iomode, lo,
1750 lseg, PNFS_UPDATE_LAYOUT_RETRY);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001751 goto lookup_again;
1752 }
Jeff Layton183d9e72016-05-17 12:28:47 -04001753 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001754 PNFS_UPDATE_LAYOUT_RETURN);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001755 goto out_put_layout_hdr;
1756 }
1757
Jeff Layton9a4bf312015-12-10 10:41:58 -05001758 if (pnfs_layoutgets_blocked(lo)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001759 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
Jeff Layton9a4bf312015-12-10 10:41:58 -05001760 PNFS_UPDATE_LAYOUT_BLOCKED);
Fred Isamancf7d63f2011-01-06 11:36:25 +00001761 goto out_unlock;
Jeff Layton9a4bf312015-12-10 10:41:58 -05001762 }
Fred Isamancf7d63f2011-01-06 11:36:25 +00001763 atomic_inc(&lo->plh_outstanding);
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001764 spin_unlock(&ino->i_lock);
Weston Andros Adamson30005122013-02-28 20:30:10 -05001765
Peng Taoabb9a002014-08-22 17:37:40 +08001766 if (list_empty(&lo->plh_layouts)) {
Fred Isaman2130ff62011-01-06 11:36:26 +00001767 /* The lo must be on the clp list if there is any
1768 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1769 */
1770 spin_lock(&clp->cl_lock);
Peng Taoabb9a002014-08-22 17:37:40 +08001771 if (list_empty(&lo->plh_layouts))
1772 list_add_tail(&lo->plh_layouts, &server->layouts);
Fred Isaman2130ff62011-01-06 11:36:26 +00001773 spin_unlock(&clp->cl_lock);
1774 }
Benny Halevye5e94012010-10-20 00:18:01 -04001775
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001776 pg_offset = arg.offset & ~PAGE_MASK;
Benny Halevy707ed5f2011-05-22 19:47:46 +03001777 if (pg_offset) {
1778 arg.offset -= pg_offset;
1779 arg.length += pg_offset;
1780 }
Andy Adamson7c24d942011-06-13 18:22:38 -04001781 if (arg.length != NFS4_MAX_UINT64)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001782 arg.length = PAGE_ALIGN(arg.length);
Benny Halevy707ed5f2011-05-22 19:47:46 +03001783
Jeff Layton183d9e72016-05-17 12:28:47 -04001784 lseg = send_layoutget(lo, ctx, &stateid, &arg, &timeout, gfp_flags);
1785 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1786 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
Trond Myklebust56b38a12016-07-14 18:34:12 -04001787 atomic_dec(&lo->plh_outstanding);
Jeff Layton83026d802016-05-17 12:28:46 -04001788 if (IS_ERR(lseg)) {
Jeff Layton183d9e72016-05-17 12:28:47 -04001789 switch(PTR_ERR(lseg)) {
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04001790 case -EBUSY:
Jeff Layton183d9e72016-05-17 12:28:47 -04001791 if (time_after(jiffies, giveup))
1792 lseg = NULL;
Trond Myklebust66b53f32016-07-14 14:28:31 -04001793 break;
1794 case -ERECALLCONFLICT:
1795 /* Huh? We hold no layouts, how is there a recall? */
1796 if (first) {
1797 lseg = NULL;
1798 break;
1799 }
1800 /* Destroy the existing layout and start over */
1801 if (time_after(jiffies, giveup))
1802 pnfs_destroy_layout(NFS_I(ino));
Jeff Layton183d9e72016-05-17 12:28:47 -04001803 /* Fallthrough */
1804 case -EAGAIN:
Trond Myklebust56b38a12016-07-14 18:34:12 -04001805 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04001806 default:
1807 if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
1808 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1809 lseg = NULL;
1810 }
Trond Myklebust56b38a12016-07-14 18:34:12 -04001811 goto out_put_layout_hdr;
1812 }
1813 if (lseg) {
1814 if (first)
1815 pnfs_clear_first_layoutget(lo);
1816 trace_pnfs_update_layout(ino, pos, count,
1817 iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
1818 pnfs_put_layout_hdr(lo);
1819 goto lookup_again;
Jeff Layton83026d802016-05-17 12:28:46 -04001820 }
1821 } else {
1822 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1823 }
1824
Trond Myklebust830ffb52012-09-20 21:25:19 -04001825out_put_layout_hdr:
Tom Haynesd67ae822014-12-11 17:02:04 -05001826 if (first)
1827 pnfs_clear_first_layoutget(lo);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001828 pnfs_put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -04001829out:
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001830 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1831 "(%s, offset: %llu, length: %llu)\n",
1832 __func__, ino->i_sb->s_id,
1833 (unsigned long long)NFS_FILEID(ino),
Peng Taod600ad12015-12-04 02:57:48 +08001834 IS_ERR_OR_NULL(lseg) ? "not found" : "found",
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001835 iomode==IOMODE_RW ? "read/write" : "read-only",
1836 (unsigned long long)pos,
1837 (unsigned long long)count);
Benny Halevye5e94012010-10-20 00:18:01 -04001838 return lseg;
1839out_unlock:
1840 spin_unlock(&ino->i_lock);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001841 goto out_put_layout_hdr;
Benny Halevye5e94012010-10-20 00:18:01 -04001842}
Andy Adamson7c24d942011-06-13 18:22:38 -04001843EXPORT_SYMBOL_GPL(pnfs_update_layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001844
Trond Myklebust540d9862015-08-25 11:16:13 -04001845static bool
1846pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
1847{
1848 switch (range->iomode) {
1849 case IOMODE_READ:
1850 case IOMODE_RW:
1851 break;
1852 default:
1853 return false;
1854 }
1855 if (range->offset == NFS4_MAX_UINT64)
1856 return false;
1857 if (range->length == 0)
1858 return false;
1859 if (range->length != NFS4_MAX_UINT64 &&
1860 range->length > NFS4_MAX_UINT64 - range->offset)
1861 return false;
1862 return true;
1863}
1864
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001865struct pnfs_layout_segment *
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001866pnfs_layout_process(struct nfs4_layoutget *lgp)
1867{
1868 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1869 struct nfs4_layoutget_res *res = &lgp->res;
1870 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001871 struct inode *ino = lo->plh_inode;
Trond Myklebust78096cc2014-02-12 10:02:27 -05001872 LIST_HEAD(free_me);
Trond Myklebust540d9862015-08-25 11:16:13 -04001873
1874 if (!pnfs_sanity_check_layout_range(&res->range))
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001875 return ERR_PTR(-EINVAL);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001876
1877 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001878 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001879 if (IS_ERR_OR_NULL(lseg)) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001880 if (!lseg)
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001881 lseg = ERR_PTR(-ENOMEM);
1882
1883 dprintk("%s: Could not allocate layout: error %ld\n",
1884 __func__, PTR_ERR(lseg));
1885 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001886 }
1887
Trond Myklebust119cef972016-07-24 15:10:12 -04001888 pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
Christoph Hellwig1013df612014-08-21 11:09:18 -05001889
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001890 spin_lock(&ino->i_lock);
Trond Myklebuste1c06f82015-08-04 16:40:08 -04001891 if (pnfs_layoutgets_blocked(lo)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001892 dprintk("%s forget reply due to state\n", __func__);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001893 goto out_forget;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001894 }
Trond Myklebust038d6492012-10-02 16:38:41 -07001895
Trond Myklebust9888d832016-11-23 12:36:04 -05001896 if (!pnfs_layout_is_valid(lo)) {
1897 /* We have a completely new layout */
1898 pnfs_set_layout_stateid(lo, &res->stateid, true);
1899 } else if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
Christoph Hellwig362f7472014-08-21 11:09:20 -05001900 /* existing state ID, make sure the sequence number matches. */
1901 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1902 dprintk("%s forget reply due to sequence\n", __func__);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001903 goto out_forget;
Christoph Hellwig362f7472014-08-21 11:09:20 -05001904 }
1905 pnfs_set_layout_stateid(lo, &res->stateid, false);
1906 } else {
1907 /*
1908 * We got an entirely new state ID. Mark all segments for the
Trond Myklebust9888d832016-11-23 12:36:04 -05001909 * inode invalid, and retry the layoutget
Christoph Hellwig362f7472014-08-21 11:09:20 -05001910 */
Trond Myklebustd9b61702016-07-24 15:04:07 -04001911 pnfs_mark_layout_stateid_invalid(lo, &free_me);
Trond Myklebust9888d832016-11-23 12:36:04 -05001912 goto out_forget;
Christoph Hellwig362f7472014-08-21 11:09:20 -05001913 }
Trond Myklebust038d6492012-10-02 16:38:41 -07001914
Trond Myklebust9369a432012-09-18 20:57:08 -04001915 pnfs_get_lseg(lseg);
Trond Myklebust03772d22015-08-25 08:54:17 -04001916 pnfs_layout_insert_lseg(lo, lseg, &free_me);
Trond Myklebust8e0acf92016-07-21 11:53:29 -04001917
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001918
Peng Tao39761432015-08-21 12:49:44 +08001919 if (res->return_on_close)
Fred Isamanf7e89172011-01-06 11:36:32 +00001920 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
Fred Isamanf7e89172011-01-06 11:36:32 +00001921
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001922 spin_unlock(&ino->i_lock);
Trond Myklebust78096cc2014-02-12 10:02:27 -05001923 pnfs_free_lseg_list(&free_me);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001924 return lseg;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001925
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001926out_forget:
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001927 spin_unlock(&ino->i_lock);
1928 lseg->pls_layout = lo;
1929 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Jeff Layton1b3c6d02016-05-17 12:28:48 -04001930 return ERR_PTR(-EAGAIN);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001931}
1932
Trond Myklebust2f215962016-02-15 12:36:04 -05001933/**
1934 * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
1935 * @lo: pointer to layout header
1936 * @tmp_list: list header to be used with pnfs_free_lseg_list()
1937 * @return_range: describe layout segment ranges to be returned
1938 *
1939 * This function is mainly intended for use by layoutrecall. It attempts
1940 * to free the layout segment immediately, or else to mark it for return
1941 * as soon as its reference count drops to zero.
1942 */
Trond Myklebust10335552016-01-04 11:23:52 -05001943int
Peng Tao016256d2014-09-06 00:53:23 +08001944pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
1945 struct list_head *tmp_list,
Jeff Layton6d597e12016-05-17 12:28:42 -04001946 const struct pnfs_layout_range *return_range,
1947 u32 seq)
Peng Tao016256d2014-09-06 00:53:23 +08001948{
1949 struct pnfs_layout_segment *lseg, *next;
Trond Myklebust10335552016-01-04 11:23:52 -05001950 int remaining = 0;
Peng Tao016256d2014-09-06 00:53:23 +08001951
1952 dprintk("%s:Begin lo %p\n", __func__, lo);
1953
1954 if (list_empty(&lo->plh_segs))
Trond Myklebust10335552016-01-04 11:23:52 -05001955 return 0;
Peng Tao016256d2014-09-06 00:53:23 +08001956
Trond Myklebustfc7ff362015-12-28 10:28:59 -05001957 assert_spin_locked(&lo->plh_inode->i_lock);
Peng Tao016256d2014-09-06 00:53:23 +08001958
1959 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Trond Myklebuste036f462016-07-22 11:13:22 -04001960 if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
Peng Tao016256d2014-09-06 00:53:23 +08001961 dprintk("%s: marking lseg %p iomode %d "
1962 "offset %llu length %llu\n", __func__,
1963 lseg, lseg->pls_range.iomode,
1964 lseg->pls_range.offset,
1965 lseg->pls_range.length);
Trond Myklebust2f215962016-02-15 12:36:04 -05001966 if (mark_lseg_invalid(lseg, tmp_list))
1967 continue;
1968 remaining++;
Peng Tao016256d2014-09-06 00:53:23 +08001969 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
Peng Tao016256d2014-09-06 00:53:23 +08001970 }
Jeff Layton6d597e12016-05-17 12:28:42 -04001971
1972 if (remaining)
1973 pnfs_set_plh_return_info(lo, return_range->iomode, seq);
1974
Trond Myklebust10335552016-01-04 11:23:52 -05001975 return remaining;
Peng Tao016256d2014-09-06 00:53:23 +08001976}
1977
1978void pnfs_error_mark_layout_for_return(struct inode *inode,
1979 struct pnfs_layout_segment *lseg)
1980{
1981 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
Peng Tao016256d2014-09-06 00:53:23 +08001982 struct pnfs_layout_range range = {
1983 .iomode = lseg->pls_range.iomode,
1984 .offset = 0,
1985 .length = NFS4_MAX_UINT64,
1986 };
Trond Myklebust10335552016-01-04 11:23:52 -05001987 bool return_now = false;
Peng Tao016256d2014-09-06 00:53:23 +08001988
1989 spin_lock(&inode->i_lock);
Trond Myklebust2d6cf5a2016-07-21 13:06:18 -04001990 pnfs_set_plh_return_info(lo, range.iomode, 0);
Trond Myklebust6604b202016-10-17 17:54:32 -04001991 /* Block LAYOUTGET */
1992 set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
Peng Tao016256d2014-09-06 00:53:23 +08001993 /*
1994 * mark all matching lsegs so that we are sure to have no live
1995 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
1996 * for how it works.
1997 */
Trond Myklebust68f74472016-10-12 19:50:54 -04001998 if (!pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, &range, 0)) {
Trond Myklebust10335552016-01-04 11:23:52 -05001999 nfs4_stateid stateid;
Trond Myklebuste5fd1902016-07-21 12:44:15 -04002000 enum pnfs_iomode iomode;
Trond Myklebust10335552016-01-04 11:23:52 -05002001
Trond Myklebuste5fd1902016-07-21 12:44:15 -04002002 return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
Trond Myklebust10335552016-01-04 11:23:52 -05002003 spin_unlock(&inode->i_lock);
2004 if (return_now)
2005 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
2006 } else {
2007 spin_unlock(&inode->i_lock);
2008 nfs_commit_inode(inode, 0);
2009 }
Peng Tao016256d2014-09-06 00:53:23 +08002010}
2011EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
2012
Trond Myklebustd8007d42011-06-10 13:30:23 -04002013void
2014pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
2015{
Peng Tao1fd937b2012-09-25 14:55:57 +08002016 u64 rd_size = req->wb_bytes;
2017
Peng Taocb5d04b2015-01-24 22:14:52 +08002018 if (pgio->pg_lseg == NULL) {
2019 if (pgio->pg_dreq == NULL)
2020 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
2021 else
2022 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
Trond Myklebustd8007d42011-06-10 13:30:23 -04002023
Peng Taocb5d04b2015-01-24 22:14:52 +08002024 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2025 req->wb_context,
2026 req_offset(req),
2027 rd_size,
2028 IOMODE_READ,
Tom Haynesc7d73af2016-05-25 07:31:14 -07002029 false,
Peng Taocb5d04b2015-01-24 22:14:52 +08002030 GFP_KERNEL);
Peng Taod600ad12015-12-04 02:57:48 +08002031 if (IS_ERR(pgio->pg_lseg)) {
2032 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2033 pgio->pg_lseg = NULL;
2034 return;
2035 }
Peng Taocb5d04b2015-01-24 22:14:52 +08002036 }
Trond Myklebuste885de12011-06-10 13:30:23 -04002037 /* If no lseg, fall back to read through mds */
2038 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04002039 nfs_pageio_reset_read_mds(pgio);
Trond Myklebuste885de12011-06-10 13:30:23 -04002040
Trond Myklebustd8007d42011-06-10 13:30:23 -04002041}
2042EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
2043
2044void
Peng Tao62965562012-09-25 14:55:57 +08002045pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
2046 struct nfs_page *req, u64 wb_size)
Trond Myklebustd8007d42011-06-10 13:30:23 -04002047{
Peng Taod600ad12015-12-04 02:57:48 +08002048 if (pgio->pg_lseg == NULL) {
Peng Taocb5d04b2015-01-24 22:14:52 +08002049 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2050 req->wb_context,
2051 req_offset(req),
2052 wb_size,
2053 IOMODE_RW,
Tom Haynesc7d73af2016-05-25 07:31:14 -07002054 false,
Peng Taocb5d04b2015-01-24 22:14:52 +08002055 GFP_NOFS);
Peng Taod600ad12015-12-04 02:57:48 +08002056 if (IS_ERR(pgio->pg_lseg)) {
2057 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2058 pgio->pg_lseg = NULL;
2059 return;
2060 }
2061 }
Trond Myklebuste885de12011-06-10 13:30:23 -04002062 /* If no lseg, fall back to write through mds */
2063 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04002064 nfs_pageio_reset_write_mds(pgio);
Trond Myklebustd8007d42011-06-10 13:30:23 -04002065}
2066EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
2067
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002068void
2069pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
2070{
2071 if (desc->pg_lseg) {
2072 pnfs_put_lseg(desc->pg_lseg);
2073 desc->pg_lseg = NULL;
2074 }
2075}
2076EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
2077
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -04002078/*
2079 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
2080 * of bytes (maximum @req->wb_bytes) that can be coalesced.
2081 */
2082size_t
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002083pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
2084 struct nfs_page *prev, struct nfs_page *req)
Fred Isamanbae724e2011-03-01 01:34:15 +00002085{
Weston Andros Adamson0f9c429e2014-05-15 11:56:51 -04002086 unsigned int size;
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002087 u64 seg_end, req_start, seg_left;
Weston Andros Adamson0f9c429e2014-05-15 11:56:51 -04002088
2089 size = nfs_generic_pg_test(pgio, prev, req);
Weston Andros Adamson0f9c429e2014-05-15 11:56:51 -04002090 if (!size)
2091 return 0;
Benny Halevy89a58e32011-05-25 20:54:40 +03002092
Trond Myklebust19982ba2011-06-10 13:30:23 -04002093 /*
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002094 * 'size' contains the number of bytes left in the current page (up
2095 * to the original size asked for in @req->wb_bytes).
2096 *
2097 * Calculate how many bytes are left in the layout segment
2098 * and if there are less bytes than 'size', return that instead.
Trond Myklebust19982ba2011-06-10 13:30:23 -04002099 *
2100 * Please also note that 'end_offset' is actually the offset of the
2101 * first byte that lies outside the pnfs_layout_range. FIXME?
2102 *
2103 */
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002104 if (pgio->pg_lseg) {
Trond Myklebust17822b22016-10-25 12:24:25 -04002105 seg_end = pnfs_end_offset(pgio->pg_lseg->pls_range.offset,
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002106 pgio->pg_lseg->pls_range.length);
2107 req_start = req_offset(req);
Weston Andros Adamson7c137892015-01-30 11:01:02 -05002108 WARN_ON_ONCE(req_start >= seg_end);
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002109 /* start of request is past the last byte of this segment */
Weston Andros Adamson7c137892015-01-30 11:01:02 -05002110 if (req_start >= seg_end) {
2111 /* reference the new lseg */
2112 if (pgio->pg_ops->pg_cleanup)
2113 pgio->pg_ops->pg_cleanup(pgio);
2114 if (pgio->pg_ops->pg_init)
2115 pgio->pg_ops->pg_init(pgio, req);
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002116 return 0;
Weston Andros Adamson7c137892015-01-30 11:01:02 -05002117 }
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04002118
2119 /* adjust 'size' iff there are fewer bytes left in the
2120 * segment than what nfs_generic_pg_test returned */
2121 seg_left = seg_end - req_start;
2122 if (seg_left < size)
2123 size = (unsigned int)seg_left;
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002124 }
Weston Andros Adamson0f9c429e2014-05-15 11:56:51 -04002125
Weston Andros Adamson19b54842014-05-15 11:56:55 -04002126 return size;
Fred Isamanbae724e2011-03-01 01:34:15 +00002127}
Benny Halevy89a58e32011-05-25 20:54:40 +03002128EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
Fred Isamanbae724e2011-03-01 01:34:15 +00002129
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002130int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002131{
2132 struct nfs_pageio_descriptor pgio;
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002133
2134 /* Resend all requests through the MDS */
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002135 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
2136 hdr->completion_ops);
Trond Myklebustc7070112015-06-17 19:56:22 -04002137 set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags);
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002138 return nfs_pageio_resend(&pgio, hdr);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002139}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04002140EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05002141
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002142static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002143{
Fred Isamancd841602012-04-20 14:47:44 -04002144
2145 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
2146 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002147 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04002148 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002149 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04002150 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002151 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002152}
2153
Benny Halevyd20581a2011-05-22 19:52:03 +03002154/*
2155 * Called by non rpc-based layout drivers
2156 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002157void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
Fred Isaman94ad1c82011-03-01 01:34:14 +00002158{
Kinglong Meef8417b482015-10-16 17:23:29 +08002159 if (likely(!hdr->pnfs_error)) {
Trond Myklebust67af7612015-03-25 20:40:38 -04002160 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
2161 hdr->mds_offset + hdr->res.count);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002162 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
Kinglong Meef8417b482015-10-16 17:23:29 +08002163 }
2164 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
2165 if (unlikely(hdr->pnfs_error))
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002166 pnfs_ld_handle_write_error(hdr);
2167 hdr->mds_ops->rpc_release(hdr);
Fred Isaman44b83792011-03-03 15:13:44 +00002168}
Benny Halevyd20581a2011-05-22 19:52:03 +03002169EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
Fred Isaman44b83792011-03-03 15:13:44 +00002170
Trond Myklebustdce81292011-07-13 15:59:19 -04002171static void
2172pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002173 struct nfs_pgio_header *hdr)
Trond Myklebustdce81292011-07-13 15:59:19 -04002174{
Peng Tao48d635f2014-11-10 08:35:35 +08002175 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002176
Fred Isaman6c75dc02012-04-20 14:47:47 -04002177 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002178 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
Fred Isaman6c75dc02012-04-20 14:47:47 -04002179 nfs_pageio_reset_write_mds(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002180 mirror->pg_recoalesce = 1;
Fred Isaman6c75dc02012-04-20 14:47:47 -04002181 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002182 nfs_pgio_data_destroy(hdr);
Trond Myklebust1ca018d2015-06-17 19:41:51 -04002183 hdr->release(hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04002184}
2185
2186static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002187pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
Trond Myklebustdce81292011-07-13 15:59:19 -04002188 const struct rpc_call_ops *call_ops,
2189 struct pnfs_layout_segment *lseg,
2190 int how)
Andy Adamson0382b742011-03-03 15:13:45 +00002191{
Fred Isamancd841602012-04-20 14:47:44 -04002192 struct inode *inode = hdr->inode;
Andy Adamson0382b742011-03-03 15:13:45 +00002193 enum pnfs_try_status trypnfs;
2194 struct nfs_server *nfss = NFS_SERVER(inode);
2195
Fred Isamancd841602012-04-20 14:47:44 -04002196 hdr->mds_ops = call_ops;
Andy Adamson0382b742011-03-03 15:13:45 +00002197
2198 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002199 inode->i_ino, hdr->args.count, hdr->args.offset, how);
2200 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
Fred Isaman6c75dc02012-04-20 14:47:47 -04002201 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson0382b742011-03-03 15:13:45 +00002202 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
Andy Adamson0382b742011-03-03 15:13:45 +00002203 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2204 return trypnfs;
2205}
2206
Trond Myklebustdce81292011-07-13 15:59:19 -04002207static void
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002208pnfs_do_write(struct nfs_pageio_descriptor *desc,
2209 struct nfs_pgio_header *hdr, int how)
Trond Myklebustdce81292011-07-13 15:59:19 -04002210{
Trond Myklebustdce81292011-07-13 15:59:19 -04002211 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2212 struct pnfs_layout_segment *lseg = desc->pg_lseg;
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002213 enum pnfs_try_status trypnfs;
Trond Myklebustdce81292011-07-13 15:59:19 -04002214
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002215 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002216 if (trypnfs == PNFS_NOT_ATTEMPTED)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002217 pnfs_write_through_mds(desc, hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04002218}
2219
Fred Isaman6c75dc02012-04-20 14:47:47 -04002220static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
2221{
Trond Myklebust9369a432012-09-18 20:57:08 -04002222 pnfs_put_lseg(hdr->lseg);
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002223 nfs_pgio_header_free(hdr);
Fred Isaman6c75dc02012-04-20 14:47:47 -04002224}
2225
Trond Myklebustdce81292011-07-13 15:59:19 -04002226int
2227pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
2228{
Fred Isaman6c75dc02012-04-20 14:47:47 -04002229 struct nfs_pgio_header *hdr;
Trond Myklebustdce81292011-07-13 15:59:19 -04002230 int ret;
2231
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002232 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2233 if (!hdr) {
Peng Tao2bff2282015-12-05 02:03:17 +08002234 desc->pg_error = -ENOMEM;
2235 return desc->pg_error;
Fred Isaman6c75dc02012-04-20 14:47:47 -04002236 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04002237 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002238
Trond Myklebust9369a432012-09-18 20:57:08 -04002239 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Anna Schumakeref2c4882014-05-06 09:12:36 -04002240 ret = nfs_generic_pgio(desc, hdr);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002241 if (!ret)
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002242 pnfs_do_write(desc, hdr, desc->pg_ioflags);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002243
Fred Isaman6c75dc02012-04-20 14:47:47 -04002244 return ret;
Trond Myklebustdce81292011-07-13 15:59:19 -04002245}
2246EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
2247
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002248int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
Trond Myklebust62e4a762011-11-10 14:30:37 -05002249{
2250 struct nfs_pageio_descriptor pgio;
2251
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002252 /* Resend all requests through the MDS */
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002253 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
2254 return nfs_pageio_resend(&pgio, hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002255}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04002256EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002257
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002258static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002259{
Fred Isamancd841602012-04-20 14:47:44 -04002260 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
2261 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002262 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04002263 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04002264 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002265 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04002266 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
Trond Myklebust62e4a762011-11-10 14:30:37 -05002267}
2268
Andy Adamsonb1f69b72010-10-20 00:18:03 -04002269/*
Benny Halevyd20581a2011-05-22 19:52:03 +03002270 * Called by non rpc-based layout drivers
2271 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002272void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03002273{
Trond Myklebustbfc505d2016-09-15 18:26:05 -04002274 if (likely(!hdr->pnfs_error))
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002275 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
Kinglong Meef8417b482015-10-16 17:23:29 +08002276 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
2277 if (unlikely(hdr->pnfs_error))
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002278 pnfs_ld_handle_read_error(hdr);
2279 hdr->mds_ops->rpc_release(hdr);
Benny Halevyd20581a2011-05-22 19:52:03 +03002280}
2281EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
2282
Trond Myklebust493292d2011-07-13 15:58:28 -04002283static void
2284pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002285 struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -04002286{
Peng Tao48d635f2014-11-10 08:35:35 +08002287 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002288
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002289 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002290 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002291 nfs_pageio_reset_read_mds(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002292 mirror->pg_recoalesce = 1;
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002293 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002294 nfs_pgio_data_destroy(hdr);
Trond Myklebust1ca018d2015-06-17 19:41:51 -04002295 hdr->release(hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -04002296}
2297
Benny Halevyd20581a2011-05-22 19:52:03 +03002298/*
Andy Adamson64419a92011-03-01 01:34:16 +00002299 * Call the appropriate parallel I/O subsystem read function.
2300 */
Trond Myklebust493292d2011-07-13 15:58:28 -04002301static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002302pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
Trond Myklebust493292d2011-07-13 15:58:28 -04002303 const struct rpc_call_ops *call_ops,
2304 struct pnfs_layout_segment *lseg)
Andy Adamson64419a92011-03-01 01:34:16 +00002305{
Fred Isamancd841602012-04-20 14:47:44 -04002306 struct inode *inode = hdr->inode;
Andy Adamson64419a92011-03-01 01:34:16 +00002307 struct nfs_server *nfss = NFS_SERVER(inode);
2308 enum pnfs_try_status trypnfs;
2309
Fred Isamancd841602012-04-20 14:47:44 -04002310 hdr->mds_ops = call_ops;
Andy Adamson64419a92011-03-01 01:34:16 +00002311
2312 dprintk("%s: Reading ino:%lu %u@%llu\n",
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002313 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
Andy Adamson64419a92011-03-01 01:34:16 +00002314
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002315 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002316 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson64419a92011-03-01 01:34:16 +00002317 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
Andy Adamson64419a92011-03-01 01:34:16 +00002318 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2319 return trypnfs;
2320}
Andy Adamson863a3c62011-03-23 13:27:54 +00002321
Peng Taoceb11e12014-11-10 08:35:38 +08002322/* Resend all requests through pnfs. */
Weston Andros Adamson1b1bc662016-04-01 11:42:28 -04002323void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
Peng Taoceb11e12014-11-10 08:35:38 +08002324{
2325 struct nfs_pageio_descriptor pgio;
2326
Weston Andros Adamson1b1bc662016-04-01 11:42:28 -04002327 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
Trond Myklebust54e4a0d2016-11-27 15:12:39 -05002328 /* Prevent deadlocks with layoutreturn! */
2329 pnfs_put_lseg(hdr->lseg);
2330 hdr->lseg = NULL;
2331
Weston Andros Adamson1b1bc662016-04-01 11:42:28 -04002332 nfs_pageio_init_read(&pgio, hdr->inode, false,
2333 hdr->completion_ops);
2334 hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
2335 }
Peng Taoceb11e12014-11-10 08:35:38 +08002336}
2337EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
2338
Trond Myklebust493292d2011-07-13 15:58:28 -04002339static void
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002340pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -04002341{
Trond Myklebust493292d2011-07-13 15:58:28 -04002342 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2343 struct pnfs_layout_segment *lseg = desc->pg_lseg;
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002344 enum pnfs_try_status trypnfs;
Trond Myklebust493292d2011-07-13 15:58:28 -04002345
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002346 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
Peng Taoceb11e12014-11-10 08:35:38 +08002347 if (trypnfs == PNFS_TRY_AGAIN)
Weston Andros Adamson1b1bc662016-04-01 11:42:28 -04002348 pnfs_read_resend_pnfs(hdr);
2349 if (trypnfs == PNFS_NOT_ATTEMPTED || hdr->task.tk_status)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002350 pnfs_read_through_mds(desc, hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -04002351}
2352
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002353static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
2354{
Trond Myklebust9369a432012-09-18 20:57:08 -04002355 pnfs_put_lseg(hdr->lseg);
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002356 nfs_pgio_header_free(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002357}
2358
Trond Myklebust493292d2011-07-13 15:58:28 -04002359int
2360pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2361{
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002362 struct nfs_pgio_header *hdr;
Trond Myklebust493292d2011-07-13 15:58:28 -04002363 int ret;
2364
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002365 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2366 if (!hdr) {
Peng Tao2bff2282015-12-05 02:03:17 +08002367 desc->pg_error = -ENOMEM;
2368 return desc->pg_error;
Trond Myklebust493292d2011-07-13 15:58:28 -04002369 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002370 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
Trond Myklebust9369a432012-09-18 20:57:08 -04002371 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Anna Schumakeref2c4882014-05-06 09:12:36 -04002372 ret = nfs_generic_pgio(desc, hdr);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002373 if (!ret)
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002374 pnfs_do_read(desc, hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002375 return ret;
Trond Myklebust493292d2011-07-13 15:58:28 -04002376}
2377EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
2378
Trond Myklebust71244d92014-01-13 13:34:36 -05002379static void pnfs_clear_layoutcommitting(struct inode *inode)
2380{
2381 unsigned long *bitlock = &NFS_I(inode)->flags;
2382
2383 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002384 smp_mb__after_atomic();
Trond Myklebust71244d92014-01-13 13:34:36 -05002385 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2386}
2387
Andy Adamson863a3c62011-03-23 13:27:54 +00002388/*
Peng Taoa9bae562011-07-30 20:52:33 -04002389 * There can be multiple RW segments.
Andy Adamson863a3c62011-03-23 13:27:54 +00002390 */
Peng Taoa9bae562011-07-30 20:52:33 -04002391static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
Andy Adamson863a3c62011-03-23 13:27:54 +00002392{
Peng Taoa9bae562011-07-30 20:52:33 -04002393 struct pnfs_layout_segment *lseg;
Andy Adamson863a3c62011-03-23 13:27:54 +00002394
Peng Taoa9bae562011-07-30 20:52:33 -04002395 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2396 if (lseg->pls_range.iomode == IOMODE_RW &&
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002397 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
Peng Taoa9bae562011-07-30 20:52:33 -04002398 list_add(&lseg->pls_lc_list, listp);
2399 }
Andy Adamson863a3c62011-03-23 13:27:54 +00002400}
2401
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002402static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2403{
2404 struct pnfs_layout_segment *lseg, *tmp;
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002405
2406 /* Matched by references in pnfs_set_layoutcommit */
2407 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2408 list_del_init(&lseg->pls_lc_list);
2409 pnfs_put_lseg(lseg);
2410 }
2411
Trond Myklebust71244d92014-01-13 13:34:36 -05002412 pnfs_clear_layoutcommitting(inode);
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002413}
2414
Peng Tao1b0ae062011-09-22 21:50:12 -04002415void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2416{
Trond Myklebustb9e028f2012-09-18 16:41:18 -04002417 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
Peng Tao1b0ae062011-09-22 21:50:12 -04002418}
2419EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2420
Andy Adamson863a3c62011-03-23 13:27:54 +00002421void
Trond Myklebust67af7612015-03-25 20:40:38 -04002422pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
2423 loff_t end_pos)
Andy Adamson863a3c62011-03-23 13:27:54 +00002424{
Fred Isamancd841602012-04-20 14:47:44 -04002425 struct nfs_inode *nfsi = NFS_I(inode);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002426 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00002427
Fred Isamancd841602012-04-20 14:47:44 -04002428 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00002429 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
Trond Myklebust29559b12015-03-25 20:10:20 -04002430 nfsi->layout->plh_lwb = end_pos;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002431 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00002432 dprintk("%s: Set layoutcommit for inode %lu ",
Fred Isamancd841602012-04-20 14:47:44 -04002433 __func__, inode->i_ino);
Trond Myklebust29559b12015-03-25 20:10:20 -04002434 } else if (end_pos > nfsi->layout->plh_lwb)
2435 nfsi->layout->plh_lwb = end_pos;
Trond Myklebust67af7612015-03-25 20:40:38 -04002436 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
Peng Taoa9bae562011-07-30 20:52:33 -04002437 /* references matched in nfs4_layoutcommit_release */
Trond Myklebust67af7612015-03-25 20:40:38 -04002438 pnfs_get_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04002439 }
Fred Isamancd841602012-04-20 14:47:44 -04002440 spin_unlock(&inode->i_lock);
Peng Taoacff58802011-07-30 20:52:31 -04002441 dprintk("%s: lseg %p end_pos %llu\n",
Trond Myklebust67af7612015-03-25 20:40:38 -04002442 __func__, lseg, nfsi->layout->plh_lwb);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002443
2444 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2445 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2446 if (mark_as_dirty)
Fred Isamancd841602012-04-20 14:47:44 -04002447 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002448}
2449EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2450
Andy Adamsondb29c082011-07-30 20:52:38 -04002451void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2452{
2453 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2454
2455 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2456 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002457 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
Andy Adamsondb29c082011-07-30 20:52:38 -04002458}
2459
Andy Adamsonde4b15c2011-03-12 02:58:09 -05002460/*
2461 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2462 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2463 * data to disk to allow the server to recover the data if it crashes.
2464 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2465 * is off, and a COMMIT is sent to a data server, or
2466 * if WRITEs to a data server return NFS_DATA_SYNC.
2467 */
Andy Adamson863a3c62011-03-23 13:27:54 +00002468int
Andy Adamsonef311532011-03-12 02:58:10 -05002469pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00002470{
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002471 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
Andy Adamson863a3c62011-03-23 13:27:54 +00002472 struct nfs4_layoutcommit_data *data;
2473 struct nfs_inode *nfsi = NFS_I(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002474 loff_t end_pos;
Trond Myklebust71244d92014-01-13 13:34:36 -05002475 int status;
2476
2477 if (!pnfs_layoutcommit_outstanding(inode))
2478 return 0;
Andy Adamson863a3c62011-03-23 13:27:54 +00002479
2480 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
2481
Trond Myklebust71244d92014-01-13 13:34:36 -05002482 status = -EAGAIN;
2483 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
2484 if (!sync)
2485 goto out;
NeilBrown74316202014-07-07 15:16:04 +10002486 status = wait_on_bit_lock_action(&nfsi->flags,
Trond Myklebust71244d92014-01-13 13:34:36 -05002487 NFS_INO_LAYOUTCOMMITTING,
2488 nfs_wait_bit_killable,
2489 TASK_KILLABLE);
2490 if (status)
2491 goto out;
2492 }
Andy Adamsonde4b15c2011-03-12 02:58:09 -05002493
Trond Myklebust71244d92014-01-13 13:34:36 -05002494 status = -ENOMEM;
Andy Adamson863a3c62011-03-23 13:27:54 +00002495 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2496 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebust71244d92014-01-13 13:34:36 -05002497 if (!data)
2498 goto clear_layoutcommitting;
Andy Adamson863a3c62011-03-23 13:27:54 +00002499
Trond Myklebust71244d92014-01-13 13:34:36 -05002500 status = 0;
2501 spin_lock(&inode->i_lock);
2502 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2503 goto out_unlock;
Peng Tao92407e72011-10-23 20:21:17 -07002504
Peng Taoa9bae562011-07-30 20:52:33 -04002505 INIT_LIST_HEAD(&data->lseg_list);
Peng Taoa9bae562011-07-30 20:52:33 -04002506 pnfs_list_write_lseg(inode, &data->lseg_list);
Andy Adamson863a3c62011-03-23 13:27:54 +00002507
Peng Taoacff58802011-07-30 20:52:31 -04002508 end_pos = nfsi->layout->plh_lwb;
Andy Adamson863a3c62011-03-23 13:27:54 +00002509
Trond Myklebustf597c532012-03-04 18:13:56 -05002510 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
Andy Adamson863a3c62011-03-23 13:27:54 +00002511 spin_unlock(&inode->i_lock);
2512
2513 data->args.inode = inode;
Peng Tao9fa40752011-07-30 20:52:32 -04002514 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
Andy Adamson863a3c62011-03-23 13:27:54 +00002515 nfs_fattr_init(&data->fattr);
2516 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2517 data->res.fattr = &data->fattr;
Trond Myklebust2e18d4d2016-06-26 18:54:58 -04002518 if (end_pos != 0)
2519 data->args.lastbytewritten = end_pos - 1;
2520 else
2521 data->args.lastbytewritten = U64_MAX;
Andy Adamson863a3c62011-03-23 13:27:54 +00002522 data->res.server = NFS_SERVER(inode);
2523
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002524 if (ld->prepare_layoutcommit) {
2525 status = ld->prepare_layoutcommit(&data->args);
2526 if (status) {
Jeff Layton3471648a2015-07-10 15:58:42 -04002527 put_rpccred(data->cred);
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002528 spin_lock(&inode->i_lock);
Trond Myklebust29559b12015-03-25 20:10:20 -04002529 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2530 if (end_pos > nfsi->layout->plh_lwb)
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002531 nfsi->layout->plh_lwb = end_pos;
Jeff Layton3471648a2015-07-10 15:58:42 -04002532 goto out_unlock;
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002533 }
2534 }
2535
2536
Andy Adamson863a3c62011-03-23 13:27:54 +00002537 status = nfs4_proc_layoutcommit(data, sync);
2538out:
Peng Tao92407e72011-10-23 20:21:17 -07002539 if (status)
2540 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002541 dprintk("<-- %s status %d\n", __func__, status);
2542 return status;
Trond Myklebust71244d92014-01-13 13:34:36 -05002543out_unlock:
2544 spin_unlock(&inode->i_lock);
Peng Tao92407e72011-10-23 20:21:17 -07002545 kfree(data);
Trond Myklebust71244d92014-01-13 13:34:36 -05002546clear_layoutcommitting:
2547 pnfs_clear_layoutcommitting(inode);
Peng Tao92407e72011-10-23 20:21:17 -07002548 goto out;
Andy Adamson863a3c62011-03-23 13:27:54 +00002549}
Peng Tao72cff442014-08-07 10:12:38 +08002550EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
Andy Adamson82be4172012-05-23 05:02:35 -04002551
Trond Myklebust5bb89b42015-03-25 14:14:42 -04002552int
2553pnfs_generic_sync(struct inode *inode, bool datasync)
2554{
2555 return pnfs_layoutcommit_inode(inode, true);
2556}
2557EXPORT_SYMBOL_GPL(pnfs_generic_sync);
2558
Andy Adamson82be4172012-05-23 05:02:35 -04002559struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
2560{
2561 struct nfs4_threshold *thp;
2562
2563 thp = kzalloc(sizeof(*thp), GFP_NOFS);
2564 if (!thp) {
2565 dprintk("%s mdsthreshold allocation failed\n", __func__);
2566 return NULL;
2567 }
2568 return thp;
2569}
Peng Tao87334082015-06-23 19:51:57 +08002570
Peng Tao865a7ec2015-06-25 18:19:32 +08002571#if IS_ENABLED(CONFIG_NFS_V4_2)
Peng Tao87334082015-06-23 19:51:57 +08002572int
Trond Myklebustc8ad8892015-08-05 17:31:58 -04002573pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
Peng Tao87334082015-06-23 19:51:57 +08002574{
2575 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2576 struct nfs_server *server = NFS_SERVER(inode);
Peng Tao1bfe3b22015-06-23 19:52:03 +08002577 struct nfs_inode *nfsi = NFS_I(inode);
Peng Tao87334082015-06-23 19:51:57 +08002578 struct nfs42_layoutstat_data *data;
2579 struct pnfs_layout_hdr *hdr;
2580 int status = 0;
2581
2582 if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
2583 goto out;
2584
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04002585 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
2586 goto out;
2587
Peng Tao1bfe3b22015-06-23 19:52:03 +08002588 if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
2589 goto out;
2590
Peng Tao87334082015-06-23 19:51:57 +08002591 spin_lock(&inode->i_lock);
2592 if (!NFS_I(inode)->layout) {
2593 spin_unlock(&inode->i_lock);
Trond Myklebustf538d0b2016-05-16 14:41:14 -04002594 goto out_clear_layoutstats;
Peng Tao87334082015-06-23 19:51:57 +08002595 }
2596 hdr = NFS_I(inode)->layout;
2597 pnfs_get_layout_hdr(hdr);
2598 spin_unlock(&inode->i_lock);
2599
Trond Myklebustc8ad8892015-08-05 17:31:58 -04002600 data = kzalloc(sizeof(*data), gfp_flags);
Peng Tao87334082015-06-23 19:51:57 +08002601 if (!data) {
2602 status = -ENOMEM;
2603 goto out_put;
2604 }
2605
2606 data->args.fh = NFS_FH(inode);
2607 data->args.inode = inode;
Peng Tao87334082015-06-23 19:51:57 +08002608 status = ld->prepare_layoutstats(&data->args);
2609 if (status)
2610 goto out_free;
2611
2612 status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
2613
2614out:
2615 dprintk("%s returns %d\n", __func__, status);
2616 return status;
2617
2618out_free:
2619 kfree(data);
2620out_put:
2621 pnfs_put_layout_hdr(hdr);
Trond Myklebustf538d0b2016-05-16 14:41:14 -04002622out_clear_layoutstats:
Peng Tao1bfe3b22015-06-23 19:52:03 +08002623 smp_mb__before_atomic();
2624 clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
2625 smp_mb__after_atomic();
Peng Tao87334082015-06-23 19:51:57 +08002626 goto out;
2627}
2628EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
Peng Tao865a7ec2015-06-25 18:19:32 +08002629#endif
Trond Myklebustbbf58bf2015-08-24 20:39:18 -04002630
2631unsigned int layoutstats_timer;
2632module_param(layoutstats_timer, uint, 0644);
2633EXPORT_SYMBOL_GPL(layoutstats_timer);