]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/nfs/pnfs.c
pNFS: Clean up - add a helper to initialise struct layoutreturn_args
[mirror_ubuntu-bionic-kernel.git] / fs / nfs / pnfs.c
CommitLineData
85e174ba
RL
1/*
2 * pNFS functions to call and manage layout drivers.
3 *
4 * Copyright (c) 2002 [year of first publication]
5 * The Regents of the University of Michigan
6 * All Rights Reserved
7 *
8 * Dean Hildebrand <dhildebz@umich.edu>
9 *
10 * Permission is granted to use, copy, create derivative works, and
11 * redistribute this software and such derivative works for any purpose,
12 * so long as the name of the University of Michigan is not used in
13 * any advertising or publicity pertaining to the use or distribution
14 * of this software without specific, written prior authorization. If
15 * the above copyright notice or any other identification of the
16 * University of Michigan is included in any copy of any portion of
17 * this software, then the disclaimer below must also be included.
18 *
19 * This software is provided as is, without representation or warranty
20 * of any kind either express or implied, including without limitation
21 * the implied warranties of merchantability, fitness for a particular
22 * purpose, or noninfringement. The Regents of the University of
23 * Michigan shall not be liable for any damages, including special,
24 * indirect, incidental, or consequential damages, with respect to any
25 * claim arising out of or in connection with the use of the software,
26 * even if it has been or is hereafter advised of the possibility of
27 * such damages.
28 */
29
30#include <linux/nfs_fs.h>
493292dd 31#include <linux/nfs_page.h>
143cb494 32#include <linux/module.h>
ca440c38 33#include <linux/sort.h>
974cec8c 34#include "internal.h"
85e174ba 35#include "pnfs.h"
64419a9b 36#include "iostat.h"
cc668ab3 37#include "nfs4trace.h"
40dd4b7a 38#include "delegation.h"
8733408d 39#include "nfs42.h"
85e174ba
RL
40
41#define NFSDBG_FACILITY NFSDBG_PNFS
25c75333 42#define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
85e174ba 43
02c35fca
FI
44/* 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
13c13a6a 56static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo);
68f74479
TM
57static 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);
aa1e0e3a 61
02c35fca
FI
62/* 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
85e174ba
RL
77static struct pnfs_layoutdriver_type *
78find_pnfs_driver(u32 id)
79{
02c35fca
FI
80 struct pnfs_layoutdriver_type *local;
81
82 spin_lock(&pnfs_spinlock);
83 local = find_pnfs_driver_locked(id);
0a9c63fa
TM
84 if (local != NULL && !try_module_get(local->owner)) {
85 dprintk("%s: Could not grab reference on module\n", __func__);
86 local = NULL;
87 }
02c35fca
FI
88 spin_unlock(&pnfs_spinlock);
89 return local;
85e174ba
RL
90}
91
92void
93unset_pnfs_layoutdriver(struct nfs_server *nfss)
94{
738fd0f3
BH
95 if (nfss->pnfs_curr_ld) {
96 if (nfss->pnfs_curr_ld->clear_layoutdriver)
97 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
2a4c8994
TM
98 /* 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);
02c35fca 101 module_put(nfss->pnfs_curr_ld->owner);
738fd0f3 102 }
85e174ba
RL
103 nfss->pnfs_curr_ld = NULL;
104}
105
ca440c38
JL
106/*
107 * 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
85e174ba
RL
139/*
140 * 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 *
3132e49e 143 * @ids array of layout types supported by MDS.
85e174ba
RL
144 */
145void
738fd0f3 146set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
ca440c38 147 struct nfs_fsinfo *fsinfo)
85e174ba
RL
148{
149 struct pnfs_layoutdriver_type *ld_type = NULL;
3132e49e 150 u32 id;
ca440c38 151 int i;
85e174ba 152
19274716
AS
153 if (fsinfo->nlayouttypes == 0)
154 goto out_no_driver;
85e174ba
RL
155 if (!(server->nfs_client->cl_exchange_flags &
156 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
3132e49e
JL
157 printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n",
158 __func__, server->nfs_client->cl_exchange_flags);
85e174ba
RL
159 goto out_no_driver;
160 }
3132e49e 161
ca440c38
JL
162 sort(fsinfo->layouttype, fsinfo->nlayouttypes,
163 sizeof(*fsinfo->layouttype), ld_cmp, NULL);
3132e49e 164
ca440c38
JL
165 for (i = 0; i < fsinfo->nlayouttypes; i++) {
166 id = fsinfo->layouttype[i];
85e174ba 167 ld_type = find_pnfs_driver(id);
ca440c38
JL
168 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;
85e174ba 175 }
3132e49e
JL
176
177 if (!ld_type) {
ca440c38 178 dprintk("%s: No pNFS module found!\n", __func__);
3132e49e
JL
179 goto out_no_driver;
180 }
181
85e174ba 182 server->pnfs_curr_ld = ld_type;
738fd0f3
BH
183 if (ld_type->set_layoutdriver
184 && ld_type->set_layoutdriver(server, mntfh)) {
a030889a
WAA
185 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
186 "driver %u.\n", __func__, id);
738fd0f3
BH
187 module_put(ld_type->owner);
188 goto out_no_driver;
189 }
2a4c8994
TM
190 /* Bump the MDS count */
191 atomic_inc(&server->nfs_client->cl_mds_count);
ea8eecdd 192
85e174ba
RL
193 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}
02c35fca
FI
200
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) {
a030889a 208 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
02c35fca
FI
209 return status;
210 }
b1f69b75 211 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
a030889a 212 printk(KERN_ERR "NFS: %s Layout driver must provide "
b1f69b75
AA
213 "alloc_lseg and free_lseg.\n", __func__);
214 return status;
215 }
02c35fca
FI
216
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 {
a030889a 225 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
02c35fca
FI
226 __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);
e5e94017 243
b1f69b75
AA
244/*
245 * pNFS client layout cache
246 */
247
cc6e5340 248/* Need to hold i_lock if caller does not already hold reference */
43f1b3da 249void
70c3bd2b 250pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
e5e94017 251{
cc6e5340 252 atomic_inc(&lo->plh_refcount);
e5e94017
BH
253}
254
636fb9c8
BH
255static 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;
57934278 259 return ld->alloc_layout_hdr(ino, gfp_flags);
636fb9c8
BH
260}
261
262static void
263pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
264{
9c626381
TM
265 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 }
9fa40758 275 put_rpccred(lo->plh_lc_cred);
57934278 276 return ld->free_layout_hdr(lo);
636fb9c8
BH
277}
278
e5e94017 279static void
6622c3ea 280pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
e5e94017 281{
bb346f63 282 struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
cc6e5340 283 dprintk("%s: freeing layout cache %p\n", __func__, lo);
bb346f63
TM
284 nfsi->layout = NULL;
285 /* Reset MDS Threshold I/O counters */
286 nfsi->write_io = 0;
287 nfsi->read_io = 0;
e5e94017
BH
288}
289
b1f69b75 290void
70c3bd2b 291pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
974cec8c 292{
cc6e5340
FI
293 struct inode *inode = lo->plh_inode;
294
13c13a6a
TM
295 pnfs_layoutreturn_before_put_layout_hdr(lo);
296
cc6e5340 297 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
566f8737
PT
298 if (!list_empty(&lo->plh_segs))
299 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
6622c3ea 300 pnfs_detach_layout_hdr(lo);
cc6e5340 301 spin_unlock(&inode->i_lock);
6622c3ea 302 pnfs_free_layout_hdr(lo);
cc6e5340 303 }
974cec8c
AA
304}
305
ae5a459d
TM
306static 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
2454dfea
TM
314/*
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 */
5f46be04 321int
2454dfea
TM
322pnfs_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);
ae5a459d 332 pnfs_clear_layoutreturn_info(lo);
68f74479 333 pnfs_free_returned_lsegs(lo, lseg_list, &range, 0);
6d597e17 334 return pnfs_mark_matching_lsegs_invalid(lo, lseg_list, &range, 0);
2454dfea
TM
335}
336
b9e028fd
TM
337static 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
3e621214 345pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
b9e028fd 346{
25c75333 347 lo->plh_retry_timestamp = jiffies;
39e88fcf 348 if (!test_and_set_bit(fail_bit, &lo->plh_flags))
3e621214
TM
349 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;
115ce575
TM
363 struct pnfs_layout_range range = {
364 .iomode = iomode,
365 .offset = 0,
366 .length = NFS4_MAX_UINT64,
367 };
368 LIST_HEAD(head);
3e621214
TM
369
370 spin_lock(&inode->i_lock);
371 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
6d597e17 372 pnfs_mark_matching_lsegs_invalid(lo, &head, &range, 0);
3e621214 373 spin_unlock(&inode->i_lock);
115ce575 374 pnfs_free_lseg_list(&head);
b9e028fd
TM
375 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{
25c75333 382 unsigned long start, end;
3e621214
TM
383 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
384
385 if (test_bit(fail_bit, &lo->plh_flags) == 0)
25c75333
TM
386 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 */
3e621214 391 pnfs_layout_clear_fail_bit(lo, fail_bit);
25c75333
TM
392 return false;
393 }
394 return true;
b9e028fd
TM
395}
396
974cec8c 397static void
119cef97
TM
398pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
399 const struct pnfs_layout_range *range,
400 const nfs4_stateid *stateid)
974cec8c 401{
566052c5 402 INIT_LIST_HEAD(&lseg->pls_list);
a9bae566 403 INIT_LIST_HEAD(&lseg->pls_lc_list);
4541d16c 404 atomic_set(&lseg->pls_refcount, 1);
4541d16c 405 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
566052c5 406 lseg->pls_layout = lo;
119cef97
TM
407 lseg->pls_range = *range;
408 lseg->pls_seq = be32_to_cpu(stateid->seqid);
974cec8c
AA
409}
410
905ca191 411static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
974cec8c 412{
68f74479
TM
413 if (lseg != NULL) {
414 struct inode *inode = lseg->pls_layout->plh_inode;
415 NFS_SERVER(inode)->pnfs_curr_ld->free_lseg(lseg);
416 }
974cec8c
AA
417}
418
d684d2ae 419static void
57036a37
TM
420pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
421 struct pnfs_layout_segment *lseg)
d684d2ae 422{
d20581aa 423 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
d684d2ae 424 list_del_init(&lseg->pls_list);
8f0d27dc
TM
425 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
426 atomic_dec(&lo->plh_refcount);
7b650994
TM
427 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)) {
334a8f37
TM
430 if (atomic_read(&lo->plh_outstanding) == 0)
431 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
173f77e9 432 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
2d148c7e 433 }
d684d2ae
FI
434}
435
68f74479
TM
436static 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
bae724ef 448void
9369a431 449pnfs_put_lseg(struct pnfs_layout_segment *lseg)
974cec8c 450{
57036a37 451 struct pnfs_layout_hdr *lo;
d684d2ae
FI
452 struct inode *inode;
453
454 if (!lseg)
455 return;
456
4541d16c
FI
457 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));
4ef2e4f8 460
57036a37
TM
461 lo = lseg->pls_layout;
462 inode = lo->plh_inode;
4ef2e4f8 463
d684d2ae 464 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
faa4a54f
TM
465 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
466 spin_unlock(&inode->i_lock);
467 return;
468 }
8f0d27dc 469 pnfs_get_layout_hdr(lo);
4ef2e4f8 470 pnfs_layout_remove_lseg(lo, lseg);
68f74479
TM
471 if (pnfs_cache_lseg_for_layoutreturn(lo, lseg))
472 lseg = NULL;
4ef2e4f8
TM
473 spin_unlock(&inode->i_lock);
474 pnfs_free_lseg(lseg);
475 pnfs_put_layout_hdr(lo);
4541d16c 476 }
4541d16c 477}
9369a431 478EXPORT_SYMBOL_GPL(pnfs_put_lseg);
974cec8c 479
6543f803 480static void pnfs_free_lseg_async_work(struct work_struct *work)
e6cf82d1
WAA
481{
482 struct pnfs_layout_segment *lseg;
6543f803 483 struct pnfs_layout_hdr *lo;
e6cf82d1
WAA
484
485 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
6543f803 486 lo = lseg->pls_layout;
e6cf82d1 487
6543f803
TM
488 pnfs_free_lseg(lseg);
489 pnfs_put_layout_hdr(lo);
e6cf82d1
WAA
490}
491
6543f803 492static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg)
e6cf82d1 493{
6543f803 494 INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work);
e6cf82d1
WAA
495 schedule_work(&lseg->pls_work);
496}
6543f803
TM
497
498void
499pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg)
500{
501 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;
faa4a54f
TM
511 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
512 return;
6543f803 513 pnfs_layout_remove_lseg(lo, lseg);
68f74479
TM
514 if (!pnfs_cache_lseg_for_layoutreturn(lo, lseg)) {
515 pnfs_get_layout_hdr(lo);
516 pnfs_free_lseg_async(lseg);
517 }
6543f803
TM
518 }
519}
520EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked);
e6cf82d1 521
fb3296eb
BH
522/*
523 * is l2 fully contained in l1?
524 * start1 end1
525 * [----------------------------------)
526 * start2 end2
527 * [----------------)
528 */
3cb2df17 529static bool
7dc0ac70 530pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
3cb2df17 531 const struct pnfs_layout_range *l2)
fb3296eb
BH
532{
533 u64 start1 = l1->offset;
17822b20 534 u64 end1 = pnfs_end_offset(start1, l1->length);
fb3296eb 535 u64 start2 = l2->offset;
17822b20 536 u64 end2 = pnfs_end_offset(start2, l2->length);
fb3296eb
BH
537
538 return (start1 <= start2) && (end1 >= end2);
539}
540
24956804
TM
541static 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
4541d16c
FI
551/* 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 */
d684d2ae
FI
562 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
563 atomic_read(&lseg->pls_refcount));
24956804 564 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
d684d2ae 565 rv = 1;
4541d16c
FI
566 }
567 return rv;
568}
569
6d597e17
JL
570/*
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
e036f464
TM
579static 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
6d597e17
JL
600/**
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.
4541d16c 614 */
43f1b3da 615int
49a85061 616pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
4541d16c 617 struct list_head *tmp_list,
6d597e17
JL
618 const struct pnfs_layout_range *recall_range,
619 u32 seq)
974cec8c
AA
620{
621 struct pnfs_layout_segment *lseg, *next;
71b39854 622 int remaining = 0;
974cec8c
AA
623
624 dprintk("%s:Begin lo %p\n", __func__, lo);
625
8006bfba 626 if (list_empty(&lo->plh_segs))
38511722 627 return 0;
4541d16c 628 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
e036f464 629 if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
6d597e17 630 dprintk("%s: freeing lseg %p iomode %d seq %u"
4541d16c 631 "offset %llu length %llu\n", __func__,
6d597e17
JL
632 lseg, lseg->pls_range.iomode, lseg->pls_seq,
633 lseg->pls_range.offset, lseg->pls_range.length);
71b39854
TM
634 if (!mark_lseg_invalid(lseg, tmp_list))
635 remaining++;
4541d16c 636 }
71b39854
TM
637 dprintk("%s:Return %i\n", __func__, remaining);
638 return remaining;
974cec8c
AA
639}
640
68f74479
TM
641static 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
f49f9baa 655/* note free_me must contain lsegs from a single layout_hdr */
43f1b3da 656void
4541d16c 657pnfs_free_lseg_list(struct list_head *free_me)
974cec8c 658{
4541d16c 659 struct pnfs_layout_segment *lseg, *tmp;
f49f9baa
FI
660
661 if (list_empty(free_me))
662 return;
663
4541d16c 664 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
566052c5 665 list_del(&lseg->pls_list);
905ca191 666 pnfs_free_lseg(lseg);
974cec8c
AA
667 }
668}
669
e5e94017
BH
670void
671pnfs_destroy_layout(struct nfs_inode *nfsi)
672{
673 struct pnfs_layout_hdr *lo;
974cec8c 674 LIST_HEAD(tmp_list);
e5e94017
BH
675
676 spin_lock(&nfsi->vfs_inode.i_lock);
677 lo = nfsi->layout;
678 if (lo) {
3e621214 679 pnfs_get_layout_hdr(lo);
2454dfea 680 pnfs_mark_layout_stateid_invalid(lo, &tmp_list);
3e621214
TM
681 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);
974cec8c 688}
041245c8 689EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
974cec8c 690
fd9a8d71
TM
691static bool
692pnfs_layout_add_bulk_destroy_list(struct inode *inode,
693 struct list_head *layout_list)
974cec8c
AA
694{
695 struct pnfs_layout_hdr *lo;
fd9a8d71 696 bool ret = false;
974cec8c 697
fd9a8d71
TM
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;
fd9a8d71
TM
741 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;
7c5d1875
CH
750
751 pnfs_layoutcommit_inode(inode, false);
752
fd9a8d71
TM
753 spin_lock(&inode->i_lock);
754 list_del_init(&lo->plh_bulk_destroy);
9fd4b9fc
TM
755 if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
756 if (is_bulk_recall)
757 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
fd9a8d71 758 ret = -EAGAIN;
9fd4b9fc 759 }
fd9a8d71
TM
760 spin_unlock(&inode->i_lock);
761 pnfs_free_lseg_list(&lseg_list);
b20135d0
TM
762 /* Free all lsegs that are attached to commit buckets */
763 nfs_commit_inode(inode, 0);
fd9a8d71
TM
764 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);
c47abcf8 777
974cec8c 778 spin_lock(&clp->cl_lock);
6382a441 779 rcu_read_lock();
fd9a8d71 780restart:
6382a441 781 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
fd9a8d71
TM
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;
6382a441
WAA
788 }
789 rcu_read_unlock();
974cec8c
AA
790 spin_unlock(&clp->cl_lock);
791
fd9a8d71
TM
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;
974cec8c 812 }
fd9a8d71
TM
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
821/*
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{
828 nfs4_deviceid_mark_client_invalid(clp);
829 nfs4_deviceid_purge_client(clp);
830
831 pnfs_destroy_layouts_byclid(clp, false);
e5e94017
BH
832}
833
fd6002e9 834/* update lo->plh_stateid with new if is more recent */
43f1b3da
FI
835void
836pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
837 bool update_barrier)
b1f69b75 838{
ecebb80b 839 u32 oldseq, newseq, new_barrier = 0;
b1f69b75 840
2d2f24ad
TM
841 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
842 newseq = be32_to_cpu(new->seqid);
2a59a041
TM
843
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)) {
f597c537 852 nfs4_stateid_copy(&lo->plh_stateid, new);
ecebb80b
TM
853 /*
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);
43f1b3da 858 }
ecebb80b
TM
859 if (update_barrier)
860 new_barrier = be32_to_cpu(new->seqid);
861 else if (new_barrier == 0)
862 return;
2a59a041 863 if (pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
ecebb80b 864 lo->plh_barrier = new_barrier;
b1f69b75
AA
865}
866
cf7d63f1 867static bool
19c54aba
TM
868pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
869 const nfs4_stateid *stateid)
43f1b3da 870{
19c54aba 871 u32 seqid = be32_to_cpu(stateid->seqid);
25a1a621 872
19c54aba
TM
873 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
874}
875
876/* lget is set to 1 if called from inside send_layoutget call chain */
877static bool
e1c06f80 878pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
19c54aba 879{
f7e8917a 880 return lo->plh_block_lgets ||
e1c06f80 881 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
cf7d63f1
FI
882}
883
b1f69b75 884/*
183d9e7b
JL
885 * Get layout from server.
886 * for now, assume that whole file layouts are requested.
887 * arg->offset: 0
888 * arg->length: all ones
889 */
e5e94017
BH
890static struct pnfs_layout_segment *
891send_layoutget(struct pnfs_layout_hdr *lo,
892 struct nfs_open_context *ctx,
183d9e7b 893 nfs4_stateid *stateid,
e144e539 894 const struct pnfs_layout_range *range,
183d9e7b 895 long *timeout, gfp_t gfp_flags)
e5e94017 896{
b7edfaa1 897 struct inode *ino = lo->plh_inode;
b1f69b75
AA
898 struct nfs_server *server = NFS_SERVER(ino);
899 struct nfs4_layoutget *lgp;
2d89a1d3 900 loff_t i_size;
b1f69b75
AA
901
902 dprintk("--> %s\n", __func__);
e5e94017 903
4f2e9dce
JL
904 /*
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.
908 */
83026d80
JL
909 lgp = kzalloc(sizeof(*lgp), gfp_flags);
910 if (lgp == NULL)
911 return ERR_PTR(-ENOMEM);
912
913 i_size = i_size_read(ino);
914
915 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;
d03ab29d 923 }
83026d80
JL
924 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);
183d9e7b 929 nfs4_stateid_copy(&lgp->args.stateid, stateid);
83026d80
JL
930 lgp->gfp_flags = gfp_flags;
931 lgp->cred = lo->plh_lc_cred;
35124a09 932
183d9e7b 933 return nfs4_proc_layoutget(lgp, timeout, gfp_flags);
974cec8c
AA
934}
935
24956804
TM
936static 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
68f74479 951static void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
d67ae825
TH
952{
953 clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
6604b203 954 clear_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags);
d67ae825
TH
955 smp_mb__after_atomic();
956 wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
7f27392c 957 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
d67ae825
TH
958}
959
68f74479 960void pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr *lo,
2a974425 961 const nfs4_stateid *arg_stateid,
68f74479 962 const struct pnfs_layout_range *range,
68f74479
TM
963 const nfs4_stateid *stateid)
964{
965 struct inode *inode = lo->plh_inode;
966 LIST_HEAD(freeme);
967
968 spin_lock(&inode->i_lock);
2a974425
TM
969 if (!pnfs_layout_is_valid(lo) || !arg_stateid ||
970 !nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid))
971 goto out_unlock;
68f74479 972 if (stateid) {
2a974425
TM
973 u32 seq = be32_to_cpu(arg_stateid->seqid);
974
68f74479
TM
975 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);
2a974425 980out_unlock:
68f74479
TM
981 pnfs_clear_layoutreturn_waitbit(lo);
982 spin_unlock(&inode->i_lock);
983 pnfs_free_lseg_list(&freeme);
984
985}
986
13c13a6a 987static bool
e5fd1904
TM
988pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
989 nfs4_stateid *stateid,
990 enum pnfs_iomode *iomode)
13c13a6a 991{
bf0291dd
TM
992 /* Serialise LAYOUTGET/LAYOUTRETURN */
993 if (atomic_read(&lo->plh_outstanding) != 0)
994 return false;
6604b203 995 if (test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
13c13a6a 996 return false;
6604b203 997 set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
13c13a6a 998 pnfs_get_layout_hdr(lo);
e5fd1904
TM
999 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
1000 if (stateid != NULL) {
1001 nfs4_stateid_copy(stateid, &lo->plh_stateid);
1002 if (lo->plh_return_seq != 0)
1003 stateid->seqid = cpu_to_be32(lo->plh_return_seq);
1004 }
1005 if (iomode != NULL)
1006 *iomode = lo->plh_return_iomode;
1007 pnfs_clear_layoutreturn_info(lo);
1008 return true;
1009 }
1010 if (stateid != NULL)
1011 nfs4_stateid_copy(stateid, &lo->plh_stateid);
1012 if (iomode != NULL)
1013 *iomode = IOMODE_ANY;
13c13a6a
TM
1014 return true;
1015}
1016
828ed9ec
TM
1017static void
1018pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args *args,
1019 struct pnfs_layout_hdr *lo,
1020 const nfs4_stateid *stateid,
1021 enum pnfs_iomode iomode)
1022{
1023 struct inode *inode = lo->plh_inode;
1024
1025 args->layout_type = NFS_SERVER(inode)->pnfs_curr_ld->id;
1026 args->inode = inode;
1027 args->range.iomode = iomode;
1028 args->range.offset = 0;
1029 args->range.length = NFS4_MAX_UINT64;
1030 args->layout = lo;
1031 nfs4_stateid_copy(&args->stateid, stateid);
1032}
1033
f40eb5d0 1034static int
ed429d6b 1035pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, const nfs4_stateid *stateid,
6c16605d 1036 enum pnfs_iomode iomode, bool sync)
f40eb5d0
PT
1037{
1038 struct inode *ino = lo->plh_inode;
1039 struct nfs4_layoutreturn *lrp;
1040 int status = 0;
1041
e4af440a 1042 lrp = kzalloc(sizeof(*lrp), GFP_NOFS);
f40eb5d0
PT
1043 if (unlikely(lrp == NULL)) {
1044 status = -ENOMEM;
1045 spin_lock(&ino->i_lock);
d67ae825 1046 pnfs_clear_layoutreturn_waitbit(lo);
f40eb5d0
PT
1047 spin_unlock(&ino->i_lock);
1048 pnfs_put_layout_hdr(lo);
1049 goto out;
1050 }
1051
828ed9ec 1052 pnfs_init_layoutreturn_args(&lrp->args, lo, stateid, iomode);
f40eb5d0
PT
1053 lrp->clp = NFS_SERVER(ino)->nfs_client;
1054 lrp->cred = lo->plh_lc_cred;
1055
6c16605d 1056 status = nfs4_proc_layoutreturn(lrp, sync);
f40eb5d0
PT
1057out:
1058 dprintk("<-- %s status: %d\n", __func__, status);
1059 return status;
1060}
1061
13c13a6a
TM
1062/* Return true if layoutreturn is needed */
1063static bool
1064pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
1065{
1066 struct pnfs_layout_segment *s;
1067
2370abda 1068 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
13c13a6a
TM
1069 return false;
1070
1071 /* Defer layoutreturn until all lsegs are done */
1072 list_for_each_entry(s, &lo->plh_segs, pls_list) {
1073 if (test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
1074 return false;
1075 }
1076
1077 return true;
1078}
1079
1080static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
1081{
1082 struct inode *inode= lo->plh_inode;
1083
2370abda 1084 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
13c13a6a
TM
1085 return;
1086 spin_lock(&inode->i_lock);
1087 if (pnfs_layout_need_return(lo)) {
1088 nfs4_stateid stateid;
1089 enum pnfs_iomode iomode;
1090 bool send;
1091
e5fd1904 1092 send = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
13c13a6a
TM
1093 spin_unlock(&inode->i_lock);
1094 if (send) {
1095 /* Send an async layoutreturn so we dont deadlock */
1096 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1097 }
1098 } else
1099 spin_unlock(&inode->i_lock);
1100}
1101
293b3b06
AA
1102/*
1103 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
1104 * when the layout segment list is empty.
1105 *
1106 * Note that a pnfs_layout_hdr can exist with an empty layout segment
1107 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
1108 * deviceid is marked invalid.
1109 */
cbe82603
BH
1110int
1111_pnfs_return_layout(struct inode *ino)
1112{
1113 struct pnfs_layout_hdr *lo = NULL;
1114 struct nfs_inode *nfsi = NFS_I(ino);
1115 LIST_HEAD(tmp_list);
cbe82603 1116 nfs4_stateid stateid;
293b3b06 1117 int status = 0, empty;
7f27392c 1118 bool send;
cbe82603 1119
366d5052 1120 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
cbe82603
BH
1121
1122 spin_lock(&ino->i_lock);
1123 lo = nfsi->layout;
e5929f3c 1124 if (!lo) {
cbe82603 1125 spin_unlock(&ino->i_lock);
293b3b06
AA
1126 dprintk("NFS: %s no layout to return\n", __func__);
1127 goto out;
cbe82603 1128 }
cbe82603 1129 /* Reference matched in nfs4_layoutreturn_release */
70c3bd2b 1130 pnfs_get_layout_hdr(lo);
293b3b06 1131 empty = list_empty(&lo->plh_segs);
24956804 1132 pnfs_clear_layoutcommit(ino, &tmp_list);
6d597e17 1133 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL, 0);
c88953d8
CH
1134
1135 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
1136 struct pnfs_layout_range range = {
1137 .iomode = IOMODE_ANY,
1138 .offset = 0,
1139 .length = NFS4_MAX_UINT64,
1140 };
1141 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1142 }
1143
293b3b06
AA
1144 /* Don't send a LAYOUTRETURN if list was initially empty */
1145 if (empty) {
1146 spin_unlock(&ino->i_lock);
293b3b06 1147 dprintk("NFS: %s no layout segments to return\n", __func__);
7f27392c 1148 goto out_put_layout_hdr;
293b3b06 1149 }
47abadef 1150
e5fd1904 1151 send = pnfs_prepare_layoutreturn(lo, &stateid, NULL);
cbe82603
BH
1152 spin_unlock(&ino->i_lock);
1153 pnfs_free_lseg_list(&tmp_list);
7f27392c 1154 if (send)
ed429d6b 1155 status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
7f27392c
TM
1156out_put_layout_hdr:
1157 pnfs_put_layout_hdr(lo);
cbe82603
BH
1158out:
1159 dprintk("<-- %s status: %d\n", __func__, status);
1160 return status;
1161}
0a57cdac 1162EXPORT_SYMBOL_GPL(_pnfs_return_layout);
cbe82603 1163
24028672
TM
1164int
1165pnfs_commit_and_return_layout(struct inode *inode)
1166{
1167 struct pnfs_layout_hdr *lo;
1168 int ret;
1169
1170 spin_lock(&inode->i_lock);
1171 lo = NFS_I(inode)->layout;
1172 if (lo == NULL) {
1173 spin_unlock(&inode->i_lock);
1174 return 0;
1175 }
1176 pnfs_get_layout_hdr(lo);
1177 /* Block new layoutgets and read/write to ds */
1178 lo->plh_block_lgets++;
1179 spin_unlock(&inode->i_lock);
1180 filemap_fdatawait(inode->i_mapping);
1181 ret = pnfs_layoutcommit_inode(inode, true);
1182 if (ret == 0)
1183 ret = _pnfs_return_layout(inode);
1184 spin_lock(&inode->i_lock);
1185 lo->plh_block_lgets--;
1186 spin_unlock(&inode->i_lock);
1187 pnfs_put_layout_hdr(lo);
1188 return ret;
1189}
1190
f7e8917a
FI
1191bool pnfs_roc(struct inode *ino)
1192{
40dd4b7a
TM
1193 struct nfs_inode *nfsi = NFS_I(ino);
1194 struct nfs_open_context *ctx;
1195 struct nfs4_state *state;
f7e8917a
FI
1196 struct pnfs_layout_hdr *lo;
1197 struct pnfs_layout_segment *lseg, *tmp;
193e3aa2 1198 nfs4_stateid stateid;
f7e8917a 1199 LIST_HEAD(tmp_list);
e755d638 1200 bool found = false, layoutreturn = false, roc = false;
f7e8917a
FI
1201
1202 spin_lock(&ino->i_lock);
40dd4b7a 1203 lo = nfsi->layout;
0cdc329e
TM
1204 if (!lo || !pnfs_layout_is_valid(lo) ||
1205 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
40dd4b7a
TM
1206 goto out_noroc;
1207
e755d638 1208 /* no roc if we hold a delegation */
40dd4b7a
TM
1209 if (nfs4_check_delegation(ino, FMODE_READ))
1210 goto out_noroc;
1211
1212 list_for_each_entry(ctx, &nfsi->open_files, list) {
1213 state = ctx->state;
1214 /* Don't return layout if there is open file state */
1215 if (state != NULL && state->state != 0)
1216 goto out_noroc;
1217 }
1218
e755d638 1219
69820d22 1220 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list) {
e755d638 1221 /* If we are sending layoutreturn, invalidate all valid lsegs */
69820d22 1222 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
f7e8917a
FI
1223 mark_lseg_invalid(lseg, &tmp_list);
1224 found = true;
1225 }
69820d22
TM
1226 }
1227
1228 /* always send layoutreturn if being marked so */
1229 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
1230 layoutreturn = pnfs_prepare_layoutreturn(lo,
1231 &stateid, NULL);
1232 if (layoutreturn)
1233 goto out_noroc;
1234 }
1235
500d701f 1236 /* ROC in two conditions:
e755d638
PT
1237 * 1. there are ROC lsegs
1238 * 2. we don't send layoutreturn
e755d638 1239 */
69820d22 1240 if (found) {
500d701f
PT
1241 /* lo ref dropped in pnfs_roc_release() */
1242 pnfs_get_layout_hdr(lo);
e755d638 1243 roc = true;
500d701f 1244 }
f7e8917a 1245
40dd4b7a 1246out_noroc:
f7e8917a 1247 spin_unlock(&ino->i_lock);
e755d638
PT
1248 pnfs_free_lseg_list(&tmp_list);
1249 pnfs_layoutcommit_inode(ino, true);
1250 if (layoutreturn)
ed429d6b 1251 pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
e755d638 1252 return roc;
f7e8917a
FI
1253}
1254
1255void pnfs_roc_release(struct inode *ino)
1256{
1257 struct pnfs_layout_hdr *lo;
1258
1259 spin_lock(&ino->i_lock);
1260 lo = NFS_I(ino)->layout;
5c4a79fb 1261 pnfs_clear_layoutreturn_waitbit(lo);
6622c3ea
TM
1262 if (atomic_dec_and_test(&lo->plh_refcount)) {
1263 pnfs_detach_layout_hdr(lo);
1264 spin_unlock(&ino->i_lock);
1265 pnfs_free_layout_hdr(lo);
1266 } else
1267 spin_unlock(&ino->i_lock);
f7e8917a
FI
1268}
1269
1270void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
1271{
1272 struct pnfs_layout_hdr *lo;
1273
1274 spin_lock(&ino->i_lock);
1275 lo = NFS_I(ino)->layout;
0f35ad6f 1276 if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
f7e8917a
FI
1277 lo->plh_barrier = barrier;
1278 spin_unlock(&ino->i_lock);
6a463beb 1279 trace_nfs4_layoutreturn_on_close(ino, 0);
f7e8917a
FI
1280}
1281
4ff376fe 1282void pnfs_roc_get_barrier(struct inode *ino, u32 *barrier)
f7e8917a
FI
1283{
1284 struct nfs_inode *nfsi = NFS_I(ino);
7fdab069 1285 struct pnfs_layout_hdr *lo;
7fdab069 1286 u32 current_seqid;
f7e8917a
FI
1287
1288 spin_lock(&ino->i_lock);
7fdab069
TM
1289 lo = nfsi->layout;
1290 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
f7e8917a 1291
7fdab069
TM
1292 /* Since close does not return a layout stateid for use as
1293 * a barrier, we choose the worst-case barrier.
1294 */
1295 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
f7e8917a 1296 spin_unlock(&ino->i_lock);
f7e8917a
FI
1297}
1298
500d701f
PT
1299bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
1300{
1301 struct nfs_inode *nfsi = NFS_I(ino);
1302 struct pnfs_layout_hdr *lo;
1303 bool sleep = false;
1304
1305 /* we might not have grabbed lo reference. so need to check under
1306 * i_lock */
1307 spin_lock(&ino->i_lock);
1308 lo = nfsi->layout;
ee284e35
TM
1309 if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1310 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
500d701f 1311 sleep = true;
ee284e35 1312 }
500d701f 1313 spin_unlock(&ino->i_lock);
500d701f
PT
1314 return sleep;
1315}
1316
b1f69b75
AA
1317/*
1318 * Compare two layout segments for sorting into layout cache.
1319 * We want to preferentially return RW over RO layouts, so ensure those
1320 * are seen first.
1321 */
1322static s64
7dc0ac70 1323pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
3cb2df17 1324 const struct pnfs_layout_range *l2)
b1f69b75 1325{
fb3296eb
BH
1326 s64 d;
1327
1328 /* high offset > low offset */
1329 d = l1->offset - l2->offset;
1330 if (d)
1331 return d;
1332
1333 /* short length > long length */
1334 d = l2->length - l1->length;
1335 if (d)
1336 return d;
1337
b1f69b75 1338 /* read > read/write */
fb3296eb 1339 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
b1f69b75
AA
1340}
1341
03772d2f
TM
1342static bool
1343pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
1344 const struct pnfs_layout_range *l2)
1345{
1346 return pnfs_lseg_range_cmp(l1, l2) > 0;
1347}
1348
1349static bool
1350pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
1351 struct pnfs_layout_segment *old)
1352{
1353 return false;
1354}
1355
1356void
1357pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1358 struct pnfs_layout_segment *lseg,
1359 bool (*is_after)(const struct pnfs_layout_range *,
1360 const struct pnfs_layout_range *),
1361 bool (*do_merge)(struct pnfs_layout_segment *,
1362 struct pnfs_layout_segment *),
1363 struct list_head *free_me)
974cec8c 1364{
03772d2f 1365 struct pnfs_layout_segment *lp, *tmp;
b1f69b75 1366
974cec8c
AA
1367 dprintk("%s:Begin\n", __func__);
1368
03772d2f
TM
1369 list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
1370 if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
1371 continue;
1372 if (do_merge(lseg, lp)) {
1373 mark_lseg_invalid(lp, free_me);
1374 continue;
1375 }
1376 if (is_after(&lseg->pls_range, &lp->pls_range))
b1f69b75 1377 continue;
566052c5 1378 list_add_tail(&lseg->pls_list, &lp->pls_list);
b1f69b75
AA
1379 dprintk("%s: inserted lseg %p "
1380 "iomode %d offset %llu length %llu before "
1381 "lp %p iomode %d offset %llu length %llu\n",
566052c5
FI
1382 __func__, lseg, lseg->pls_range.iomode,
1383 lseg->pls_range.offset, lseg->pls_range.length,
1384 lp, lp->pls_range.iomode, lp->pls_range.offset,
1385 lp->pls_range.length);
fb3296eb 1386 goto out;
974cec8c 1387 }
fb3296eb
BH
1388 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1389 dprintk("%s: inserted lseg %p "
1390 "iomode %d offset %llu length %llu at tail\n",
1391 __func__, lseg, lseg->pls_range.iomode,
1392 lseg->pls_range.offset, lseg->pls_range.length);
1393out:
70c3bd2b 1394 pnfs_get_layout_hdr(lo);
974cec8c
AA
1395
1396 dprintk("%s:Return\n", __func__);
e5e94017 1397}
03772d2f
TM
1398EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
1399
1400static void
1401pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1402 struct pnfs_layout_segment *lseg,
1403 struct list_head *free_me)
1404{
1405 struct inode *inode = lo->plh_inode;
1406 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
1407
1408 if (ld->add_lseg != NULL)
1409 ld->add_lseg(lo, lseg, free_me);
1410 else
1411 pnfs_generic_layout_insert_lseg(lo, lseg,
1412 pnfs_lseg_range_is_after,
1413 pnfs_lseg_no_merge,
1414 free_me);
1415}
e5e94017
BH
1416
1417static struct pnfs_layout_hdr *
9fa40758
PT
1418alloc_init_layout_hdr(struct inode *ino,
1419 struct nfs_open_context *ctx,
1420 gfp_t gfp_flags)
e5e94017
BH
1421{
1422 struct pnfs_layout_hdr *lo;
1423
636fb9c8 1424 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
e5e94017
BH
1425 if (!lo)
1426 return NULL;
cc6e5340 1427 atomic_set(&lo->plh_refcount, 1);
b7edfaa1
FI
1428 INIT_LIST_HEAD(&lo->plh_layouts);
1429 INIT_LIST_HEAD(&lo->plh_segs);
68f74479 1430 INIT_LIST_HEAD(&lo->plh_return_segs);
fd9a8d71 1431 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
b7edfaa1 1432 lo->plh_inode = ino;
5cc2216d 1433 lo->plh_lc_cred = get_rpccred(ctx->cred);
67a3b721 1434 lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
e5e94017
BH
1435 return lo;
1436}
1437
1438static struct pnfs_layout_hdr *
9fa40758
PT
1439pnfs_find_alloc_layout(struct inode *ino,
1440 struct nfs_open_context *ctx,
1441 gfp_t gfp_flags)
e5241e43
TM
1442 __releases(&ino->i_lock)
1443 __acquires(&ino->i_lock)
e5e94017
BH
1444{
1445 struct nfs_inode *nfsi = NFS_I(ino);
1446 struct pnfs_layout_hdr *new = NULL;
1447
1448 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1449
251ec410
TM
1450 if (nfsi->layout != NULL)
1451 goto out_existing;
e5e94017 1452 spin_unlock(&ino->i_lock);
9fa40758 1453 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
e5e94017
BH
1454 spin_lock(&ino->i_lock);
1455
251ec410 1456 if (likely(nfsi->layout == NULL)) { /* Won the race? */
e5e94017 1457 nfsi->layout = new;
251ec410 1458 return new;
7175fe90
YN
1459 } else if (new != NULL)
1460 pnfs_free_layout_hdr(new);
251ec410
TM
1461out_existing:
1462 pnfs_get_layout_hdr(nfsi->layout);
e5e94017
BH
1463 return nfsi->layout;
1464}
1465
b1f69b75
AA
1466/*
1467 * iomode matching rules:
c7d73af2
TH
1468 * iomode lseg strict match
1469 * iomode
1470 * ----- ----- ------ -----
1471 * ANY READ N/A true
1472 * ANY RW N/A true
1473 * RW READ N/A false
1474 * RW RW N/A true
1475 * READ READ N/A true
1476 * READ RW true false
1477 * READ RW false true
b1f69b75 1478 */
3cb2df17 1479static bool
7dc0ac70 1480pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
c7d73af2
TH
1481 const struct pnfs_layout_range *range,
1482 bool strict_iomode)
b1f69b75 1483{
fb3296eb
BH
1484 struct pnfs_layout_range range1;
1485
1486 if ((range->iomode == IOMODE_RW &&
1487 ls_range->iomode != IOMODE_RW) ||
c7d73af2
TH
1488 (range->iomode != ls_range->iomode &&
1489 strict_iomode == true) ||
7dc0ac70 1490 !pnfs_lseg_range_intersecting(ls_range, range))
fb3296eb
BH
1491 return 0;
1492
1493 /* range1 covers only the first byte in the range */
1494 range1 = *range;
1495 range1.length = 1;
7dc0ac70 1496 return pnfs_lseg_range_contained(ls_range, &range1);
b1f69b75
AA
1497}
1498
1499/*
1500 * lookup range in layout
1501 */
e5e94017 1502static struct pnfs_layout_segment *
fb3296eb 1503pnfs_find_lseg(struct pnfs_layout_hdr *lo,
c7d73af2
TH
1504 struct pnfs_layout_range *range,
1505 bool strict_iomode)
e5e94017 1506{
b1f69b75
AA
1507 struct pnfs_layout_segment *lseg, *ret = NULL;
1508
1509 dprintk("%s:Begin\n", __func__);
1510
b7edfaa1 1511 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
4541d16c 1512 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
ce6ab4f2 1513 !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
c7d73af2
TH
1514 pnfs_lseg_range_match(&lseg->pls_range, range,
1515 strict_iomode)) {
9369a431 1516 ret = pnfs_get_lseg(lseg);
b1f69b75
AA
1517 break;
1518 }
b1f69b75
AA
1519 }
1520
1521 dprintk("%s:Return lseg %p ref %d\n",
4541d16c 1522 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
b1f69b75 1523 return ret;
e5e94017
BH
1524}
1525
d23d61c8
AA
1526/*
1527 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1528 * to the MDS or over pNFS
1529 *
1530 * The nfs_inode read_io and write_io fields are cumulative counters reset
1531 * when there are no layout segments. Note that in pnfs_update_layout iomode
1532 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1533 * WRITE request.
1534 *
1535 * A return of true means use MDS I/O.
1536 *
1537 * From rfc 5661:
1538 * If a file's size is smaller than the file size threshold, data accesses
1539 * SHOULD be sent to the metadata server. If an I/O request has a length that
1540 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1541 * server. If both file size and I/O size are provided, the client SHOULD
1542 * reach or exceed both thresholds before sending its read or write
1543 * requests to the data server.
1544 */
1545static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1546 struct inode *ino, int iomode)
1547{
1548 struct nfs4_threshold *t = ctx->mdsthreshold;
1549 struct nfs_inode *nfsi = NFS_I(ino);
1550 loff_t fsize = i_size_read(ino);
1551 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1552
1553 if (t == NULL)
1554 return ret;
1555
1556 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1557 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1558
1559 switch (iomode) {
1560 case IOMODE_READ:
1561 if (t->bm & THRESHOLD_RD) {
1562 dprintk("%s fsize %llu\n", __func__, fsize);
1563 size_set = true;
1564 if (fsize < t->rd_sz)
1565 size = true;
1566 }
1567 if (t->bm & THRESHOLD_RD_IO) {
1568 dprintk("%s nfsi->read_io %llu\n", __func__,
1569 nfsi->read_io);
1570 io_set = true;
1571 if (nfsi->read_io < t->rd_io_sz)
1572 io = true;
1573 }
1574 break;
1575 case IOMODE_RW:
1576 if (t->bm & THRESHOLD_WR) {
1577 dprintk("%s fsize %llu\n", __func__, fsize);
1578 size_set = true;
1579 if (fsize < t->wr_sz)
1580 size = true;
1581 }
1582 if (t->bm & THRESHOLD_WR_IO) {
1583 dprintk("%s nfsi->write_io %llu\n", __func__,
1584 nfsi->write_io);
1585 io_set = true;
1586 if (nfsi->write_io < t->wr_io_sz)
1587 io = true;
1588 }
1589 break;
1590 }
1591 if (size_set && io_set) {
1592 if (size && io)
1593 ret = true;
1594 } else if (size || io)
1595 ret = true;
1596
1597 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1598 return ret;
1599}
1600
aa8a45ee
PT
1601static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1602{
1603 /*
1604 * send layoutcommit as it can hold up layoutreturn due to lseg
1605 * reference
1606 */
1607 pnfs_layoutcommit_inode(lo->plh_inode, false);
1608 return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
2e5b29f0 1609 nfs_wait_bit_killable,
aa8a45ee
PT
1610 TASK_UNINTERRUPTIBLE);
1611}
1612
d67ae825
TH
1613static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1614{
1615 unsigned long *bitlock = &lo->plh_flags;
1616
1617 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1618 smp_mb__after_atomic();
1619 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1620}
1621
e5e94017
BH
1622/*
1623 * Layout segment is retreived from the server if not cached.
1624 * The appropriate layout segment is referenced and returned to the caller.
1625 */
7c24d948 1626struct pnfs_layout_segment *
e5e94017
BH
1627pnfs_update_layout(struct inode *ino,
1628 struct nfs_open_context *ctx,
fb3296eb
BH
1629 loff_t pos,
1630 u64 count,
a75b9df9 1631 enum pnfs_iomode iomode,
c7d73af2 1632 bool strict_iomode,
a75b9df9 1633 gfp_t gfp_flags)
e5e94017 1634{
fb3296eb
BH
1635 struct pnfs_layout_range arg = {
1636 .iomode = iomode,
1637 .offset = pos,
1638 .length = count,
1639 };
183d9e7b 1640 unsigned pg_offset, seq;
6382a441
WAA
1641 struct nfs_server *server = NFS_SERVER(ino);
1642 struct nfs_client *clp = server->nfs_client;
183d9e7b 1643 struct pnfs_layout_hdr *lo = NULL;
e5e94017 1644 struct pnfs_layout_segment *lseg = NULL;
183d9e7b
JL
1645 nfs4_stateid stateid;
1646 long timeout = 0;
66b53f32 1647 unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
30005121 1648 bool first;
e5e94017 1649
9a4bf31d 1650 if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
183d9e7b 1651 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
9a4bf31d 1652 PNFS_UPDATE_LAYOUT_NO_PNFS);
f86bbcf8 1653 goto out;
9a4bf31d 1654 }
d23d61c8 1655
9a4bf31d 1656 if (iomode == IOMODE_READ && i_size_read(ino) == 0) {
183d9e7b 1657 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
9a4bf31d 1658 PNFS_UPDATE_LAYOUT_RD_ZEROLEN);
4ae93560 1659 goto out;
9a4bf31d 1660 }
4ae93560 1661
9a4bf31d 1662 if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
183d9e7b 1663 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
9a4bf31d 1664 PNFS_UPDATE_LAYOUT_MDSTHRESH);
f86bbcf8 1665 goto out;
9a4bf31d 1666 }
d23d61c8 1667
9bf87482 1668lookup_again:
b88fa69e 1669 nfs4_client_recover_expired_lease(clp);
9bf87482 1670 first = false;
e5e94017 1671 spin_lock(&ino->i_lock);
9fa40758 1672 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
830ffb56
TM
1673 if (lo == NULL) {
1674 spin_unlock(&ino->i_lock);
183d9e7b 1675 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
9a4bf31d 1676 PNFS_UPDATE_LAYOUT_NOMEM);
830ffb56
TM
1677 goto out;
1678 }
e5e94017 1679
43f1b3da 1680 /* Do we even need to bother with this? */
a59c30ac 1681 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
183d9e7b 1682 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
9a4bf31d 1683 PNFS_UPDATE_LAYOUT_BULK_RECALL);
43f1b3da 1684 dprintk("%s matches recall, use MDS\n", __func__);
e5e94017
BH
1685 goto out_unlock;
1686 }
1687
1688 /* if LAYOUTGET already failed once we don't try again */
2e5b29f0 1689 if (pnfs_layout_io_test_failed(lo, iomode)) {
183d9e7b 1690 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
9a4bf31d 1691 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
e5e94017 1692 goto out_unlock;
9a4bf31d 1693 }
e5e94017 1694
c7d73af2 1695 lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
183d9e7b
JL
1696 if (lseg) {
1697 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1698 PNFS_UPDATE_LAYOUT_FOUND_CACHED);
1699 goto out_unlock;
1700 }
1701
1702 if (!nfs4_valid_open_stateid(ctx->state)) {
1703 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1704 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
1705 goto out_unlock;
1706 }
1707
1708 /*
1709 * Choose a stateid for the LAYOUTGET. If we don't have a layout
1710 * stateid, or it has been invalidated, then we must use the open
1711 * stateid.
1712 */
67a3b721 1713 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
183d9e7b
JL
1714
1715 /*
1716 * The first layoutget for the file. Need to serialize per
9bf87482
PT
1717 * RFC 5661 Errata 3208.
1718 */
1719 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1720 &lo->plh_flags)) {
1721 spin_unlock(&ino->i_lock);
1722 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
1723 TASK_UNINTERRUPTIBLE);
1724 pnfs_put_layout_hdr(lo);
183d9e7b 1725 dprintk("%s retrying\n", __func__);
9bf87482
PT
1726 goto lookup_again;
1727 }
183d9e7b
JL
1728
1729 first = true;
1730 do {
1731 seq = read_seqbegin(&ctx->state->seqlock);
1732 nfs4_stateid_copy(&stateid, &ctx->state->stateid);
1733 } while (read_seqretry(&ctx->state->seqlock, seq));
9bf87482 1734 } else {
183d9e7b 1735 nfs4_stateid_copy(&stateid, &lo->plh_stateid);
9bf87482 1736 }
568e8c49 1737
aa8a45ee
PT
1738 /*
1739 * Because we free lsegs before sending LAYOUTRETURN, we need to wait
1740 * for LAYOUTRETURN even if first is true.
1741 */
8f70f53a 1742 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
aa8a45ee
PT
1743 spin_unlock(&ino->i_lock);
1744 dprintk("%s wait for layoutreturn\n", __func__);
1745 if (pnfs_prepare_to_retry_layoutget(lo)) {
d67ae825
TH
1746 if (first)
1747 pnfs_clear_first_layoutget(lo);
aa8a45ee
PT
1748 pnfs_put_layout_hdr(lo);
1749 dprintk("%s retrying\n", __func__);
183d9e7b
JL
1750 trace_pnfs_update_layout(ino, pos, count, iomode, lo,
1751 lseg, PNFS_UPDATE_LAYOUT_RETRY);
aa8a45ee
PT
1752 goto lookup_again;
1753 }
183d9e7b 1754 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
9a4bf31d 1755 PNFS_UPDATE_LAYOUT_RETURN);
aa8a45ee
PT
1756 goto out_put_layout_hdr;
1757 }
1758
9a4bf31d 1759 if (pnfs_layoutgets_blocked(lo)) {
183d9e7b 1760 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
9a4bf31d 1761 PNFS_UPDATE_LAYOUT_BLOCKED);
cf7d63f1 1762 goto out_unlock;
9a4bf31d 1763 }
cf7d63f1 1764 atomic_inc(&lo->plh_outstanding);
f49f9baa 1765 spin_unlock(&ino->i_lock);
30005121 1766
abb9a007 1767 if (list_empty(&lo->plh_layouts)) {
2130ff66
FI
1768 /* The lo must be on the clp list if there is any
1769 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1770 */
1771 spin_lock(&clp->cl_lock);
abb9a007
PT
1772 if (list_empty(&lo->plh_layouts))
1773 list_add_tail(&lo->plh_layouts, &server->layouts);
2130ff66
FI
1774 spin_unlock(&clp->cl_lock);
1775 }
e5e94017 1776
09cbfeaf 1777 pg_offset = arg.offset & ~PAGE_MASK;
707ed5fd
BH
1778 if (pg_offset) {
1779 arg.offset -= pg_offset;
1780 arg.length += pg_offset;
1781 }
7c24d948 1782 if (arg.length != NFS4_MAX_UINT64)
09cbfeaf 1783 arg.length = PAGE_ALIGN(arg.length);
707ed5fd 1784
183d9e7b
JL
1785 lseg = send_layoutget(lo, ctx, &stateid, &arg, &timeout, gfp_flags);
1786 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1787 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
56b38a1f 1788 atomic_dec(&lo->plh_outstanding);
83026d80 1789 if (IS_ERR(lseg)) {
183d9e7b 1790 switch(PTR_ERR(lseg)) {
e85d7ee4 1791 case -EBUSY:
183d9e7b
JL
1792 if (time_after(jiffies, giveup))
1793 lseg = NULL;
66b53f32
TM
1794 break;
1795 case -ERECALLCONFLICT:
1796 /* Huh? We hold no layouts, how is there a recall? */
1797 if (first) {
1798 lseg = NULL;
1799 break;
1800 }
1801 /* Destroy the existing layout and start over */
1802 if (time_after(jiffies, giveup))
1803 pnfs_destroy_layout(NFS_I(ino));
183d9e7b
JL
1804 /* Fallthrough */
1805 case -EAGAIN:
56b38a1f 1806 break;
183d9e7b
JL
1807 default:
1808 if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
1809 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1810 lseg = NULL;
1811 }
56b38a1f
TM
1812 goto out_put_layout_hdr;
1813 }
1814 if (lseg) {
1815 if (first)
1816 pnfs_clear_first_layoutget(lo);
1817 trace_pnfs_update_layout(ino, pos, count,
1818 iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
1819 pnfs_put_layout_hdr(lo);
1820 goto lookup_again;
83026d80
JL
1821 }
1822 } else {
1823 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1824 }
1825
830ffb56 1826out_put_layout_hdr:
d67ae825
TH
1827 if (first)
1828 pnfs_clear_first_layoutget(lo);
70c3bd2b 1829 pnfs_put_layout_hdr(lo);
e5e94017 1830out:
f86bbcf8
TM
1831 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1832 "(%s, offset: %llu, length: %llu)\n",
1833 __func__, ino->i_sb->s_id,
1834 (unsigned long long)NFS_FILEID(ino),
d600ad1f 1835 IS_ERR_OR_NULL(lseg) ? "not found" : "found",
f86bbcf8
TM
1836 iomode==IOMODE_RW ? "read/write" : "read-only",
1837 (unsigned long long)pos,
1838 (unsigned long long)count);
e5e94017
BH
1839 return lseg;
1840out_unlock:
1841 spin_unlock(&ino->i_lock);
830ffb56 1842 goto out_put_layout_hdr;
e5e94017 1843}
7c24d948 1844EXPORT_SYMBOL_GPL(pnfs_update_layout);
b1f69b75 1845
540d9864
TM
1846static bool
1847pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
1848{
1849 switch (range->iomode) {
1850 case IOMODE_READ:
1851 case IOMODE_RW:
1852 break;
1853 default:
1854 return false;
1855 }
1856 if (range->offset == NFS4_MAX_UINT64)
1857 return false;
1858 if (range->length == 0)
1859 return false;
1860 if (range->length != NFS4_MAX_UINT64 &&
1861 range->length > NFS4_MAX_UINT64 - range->offset)
1862 return false;
1863 return true;
1864}
1865
a0b0a6e3 1866struct pnfs_layout_segment *
b1f69b75
AA
1867pnfs_layout_process(struct nfs4_layoutget *lgp)
1868{
1869 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1870 struct nfs4_layoutget_res *res = &lgp->res;
1871 struct pnfs_layout_segment *lseg;
b7edfaa1 1872 struct inode *ino = lo->plh_inode;
78096cca 1873 LIST_HEAD(free_me);
540d9864
TM
1874
1875 if (!pnfs_sanity_check_layout_range(&res->range))
1b3c6d07 1876 return ERR_PTR(-EINVAL);
b1f69b75
AA
1877
1878 /* Inject layout blob into I/O device driver */
a75b9df9 1879 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
1b3c6d07 1880 if (IS_ERR_OR_NULL(lseg)) {
b1f69b75 1881 if (!lseg)
1b3c6d07
JL
1882 lseg = ERR_PTR(-ENOMEM);
1883
1884 dprintk("%s: Could not allocate layout: error %ld\n",
1885 __func__, PTR_ERR(lseg));
1886 return lseg;
b1f69b75
AA
1887 }
1888
119cef97 1889 pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
1013df61 1890
b1f69b75 1891 spin_lock(&ino->i_lock);
e1c06f80 1892 if (pnfs_layoutgets_blocked(lo)) {
43f1b3da 1893 dprintk("%s forget reply due to state\n", __func__);
1b3c6d07 1894 goto out_forget;
43f1b3da 1895 }
038d6493 1896
9888d837
TM
1897 if (!pnfs_layout_is_valid(lo)) {
1898 /* We have a completely new layout */
1899 pnfs_set_layout_stateid(lo, &res->stateid, true);
1900 } else if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
362f7474
CH
1901 /* existing state ID, make sure the sequence number matches. */
1902 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1903 dprintk("%s forget reply due to sequence\n", __func__);
1b3c6d07 1904 goto out_forget;
362f7474
CH
1905 }
1906 pnfs_set_layout_stateid(lo, &res->stateid, false);
1907 } else {
1908 /*
1909 * We got an entirely new state ID. Mark all segments for the
9888d837 1910 * inode invalid, and retry the layoutget
362f7474 1911 */
d9b61708 1912 pnfs_mark_layout_stateid_invalid(lo, &free_me);
9888d837 1913 goto out_forget;
362f7474 1914 }
038d6493 1915
9369a431 1916 pnfs_get_lseg(lseg);
03772d2f 1917 pnfs_layout_insert_lseg(lo, lseg, &free_me);
8e0acf90 1918
b1f69b75 1919
3976143b 1920 if (res->return_on_close)
f7e8917a 1921 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
f7e8917a 1922
b1f69b75 1923 spin_unlock(&ino->i_lock);
78096cca 1924 pnfs_free_lseg_list(&free_me);
a0b0a6e3 1925 return lseg;
43f1b3da 1926
1b3c6d07 1927out_forget:
43f1b3da
FI
1928 spin_unlock(&ino->i_lock);
1929 lseg->pls_layout = lo;
1930 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1b3c6d07 1931 return ERR_PTR(-EAGAIN);
b1f69b75
AA
1932}
1933
016256df 1934static void
3982a6a2
JL
1935pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
1936 u32 seq)
5c97f5de 1937{
2d6cf5ab 1938 if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
5c97f5de
TM
1939 iomode = IOMODE_ANY;
1940 lo->plh_return_iomode = iomode;
e0fa0d01 1941 set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
2d6cf5ab
TM
1942 if (seq != 0) {
1943 WARN_ON_ONCE(lo->plh_return_seq != 0 && lo->plh_return_seq != seq);
3982a6a2 1944 lo->plh_return_seq = seq;
2d6cf5ab 1945 }
5c97f5de
TM
1946}
1947
2f215968
TM
1948/**
1949 * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
1950 * @lo: pointer to layout header
1951 * @tmp_list: list header to be used with pnfs_free_lseg_list()
1952 * @return_range: describe layout segment ranges to be returned
1953 *
1954 * This function is mainly intended for use by layoutrecall. It attempts
1955 * to free the layout segment immediately, or else to mark it for return
1956 * as soon as its reference count drops to zero.
1957 */
10335556 1958int
016256df
PT
1959pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
1960 struct list_head *tmp_list,
6d597e17
JL
1961 const struct pnfs_layout_range *return_range,
1962 u32 seq)
016256df
PT
1963{
1964 struct pnfs_layout_segment *lseg, *next;
10335556 1965 int remaining = 0;
016256df
PT
1966
1967 dprintk("%s:Begin lo %p\n", __func__, lo);
1968
1969 if (list_empty(&lo->plh_segs))
10335556 1970 return 0;
016256df 1971
fc7ff367 1972 assert_spin_locked(&lo->plh_inode->i_lock);
016256df
PT
1973
1974 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
e036f464 1975 if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
016256df
PT
1976 dprintk("%s: marking lseg %p iomode %d "
1977 "offset %llu length %llu\n", __func__,
1978 lseg, lseg->pls_range.iomode,
1979 lseg->pls_range.offset,
1980 lseg->pls_range.length);
2f215968
TM
1981 if (mark_lseg_invalid(lseg, tmp_list))
1982 continue;
1983 remaining++;
016256df 1984 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
016256df 1985 }
6d597e17
JL
1986
1987 if (remaining)
1988 pnfs_set_plh_return_info(lo, return_range->iomode, seq);
1989
10335556 1990 return remaining;
016256df
PT
1991}
1992
1993void pnfs_error_mark_layout_for_return(struct inode *inode,
1994 struct pnfs_layout_segment *lseg)
1995{
1996 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
016256df
PT
1997 struct pnfs_layout_range range = {
1998 .iomode = lseg->pls_range.iomode,
1999 .offset = 0,
2000 .length = NFS4_MAX_UINT64,
2001 };
10335556 2002 bool return_now = false;
016256df
PT
2003
2004 spin_lock(&inode->i_lock);
2d6cf5ab 2005 pnfs_set_plh_return_info(lo, range.iomode, 0);
6604b203
TM
2006 /* Block LAYOUTGET */
2007 set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
016256df
PT
2008 /*
2009 * mark all matching lsegs so that we are sure to have no live
2010 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
2011 * for how it works.
2012 */
68f74479 2013 if (!pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, &range, 0)) {
10335556 2014 nfs4_stateid stateid;
e5fd1904 2015 enum pnfs_iomode iomode;
10335556 2016
e5fd1904 2017 return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
10335556
TM
2018 spin_unlock(&inode->i_lock);
2019 if (return_now)
2020 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
2021 } else {
2022 spin_unlock(&inode->i_lock);
2023 nfs_commit_inode(inode, 0);
2024 }
016256df
PT
2025}
2026EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
2027
d8007d4d
TM
2028void
2029pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
2030{
1fd937bd
PT
2031 u64 rd_size = req->wb_bytes;
2032
cb5d04bc
PT
2033 if (pgio->pg_lseg == NULL) {
2034 if (pgio->pg_dreq == NULL)
2035 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
2036 else
2037 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
2038
2039 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2040 req->wb_context,
2041 req_offset(req),
2042 rd_size,
2043 IOMODE_READ,
c7d73af2 2044 false,
cb5d04bc 2045 GFP_KERNEL);
d600ad1f
PT
2046 if (IS_ERR(pgio->pg_lseg)) {
2047 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2048 pgio->pg_lseg = NULL;
2049 return;
2050 }
cb5d04bc 2051 }
e885de1a
TM
2052 /* If no lseg, fall back to read through mds */
2053 if (pgio->pg_lseg == NULL)
1f945357 2054 nfs_pageio_reset_read_mds(pgio);
e885de1a 2055
d8007d4d
TM
2056}
2057EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
2058
2059void
6296556f
PT
2060pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
2061 struct nfs_page *req, u64 wb_size)
d8007d4d 2062{
d600ad1f 2063 if (pgio->pg_lseg == NULL) {
cb5d04bc
PT
2064 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2065 req->wb_context,
2066 req_offset(req),
2067 wb_size,
2068 IOMODE_RW,
c7d73af2 2069 false,
cb5d04bc 2070 GFP_NOFS);
d600ad1f
PT
2071 if (IS_ERR(pgio->pg_lseg)) {
2072 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2073 pgio->pg_lseg = NULL;
2074 return;
2075 }
2076 }
e885de1a
TM
2077 /* If no lseg, fall back to write through mds */
2078 if (pgio->pg_lseg == NULL)
1f945357 2079 nfs_pageio_reset_write_mds(pgio);
d8007d4d
TM
2080}
2081EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
2082
180bb5ec
WAA
2083void
2084pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
2085{
2086 if (desc->pg_lseg) {
2087 pnfs_put_lseg(desc->pg_lseg);
2088 desc->pg_lseg = NULL;
2089 }
2090}
2091EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
2092
b4fdac1a
WAA
2093/*
2094 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
2095 * of bytes (maximum @req->wb_bytes) that can be coalesced.
2096 */
2097size_t
a7d42ddb
WAA
2098pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
2099 struct nfs_page *prev, struct nfs_page *req)
94ad1c80 2100{
0f9c429e 2101 unsigned int size;
c5e20cb7 2102 u64 seg_end, req_start, seg_left;
0f9c429e
WAA
2103
2104 size = nfs_generic_pg_test(pgio, prev, req);
0f9c429e
WAA
2105 if (!size)
2106 return 0;
94ad1c80 2107
19982ba8 2108 /*
c5e20cb7
WAA
2109 * 'size' contains the number of bytes left in the current page (up
2110 * to the original size asked for in @req->wb_bytes).
2111 *
2112 * Calculate how many bytes are left in the layout segment
2113 * and if there are less bytes than 'size', return that instead.
19982ba8
TM
2114 *
2115 * Please also note that 'end_offset' is actually the offset of the
2116 * first byte that lies outside the pnfs_layout_range. FIXME?
2117 *
2118 */
19b54848 2119 if (pgio->pg_lseg) {
17822b20 2120 seg_end = pnfs_end_offset(pgio->pg_lseg->pls_range.offset,
c5e20cb7
WAA
2121 pgio->pg_lseg->pls_range.length);
2122 req_start = req_offset(req);
7c13789e 2123 WARN_ON_ONCE(req_start >= seg_end);
c5e20cb7 2124 /* start of request is past the last byte of this segment */
7c13789e
WAA
2125 if (req_start >= seg_end) {
2126 /* reference the new lseg */
2127 if (pgio->pg_ops->pg_cleanup)
2128 pgio->pg_ops->pg_cleanup(pgio);
2129 if (pgio->pg_ops->pg_init)
2130 pgio->pg_ops->pg_init(pgio, req);
19b54848 2131 return 0;
7c13789e 2132 }
c5e20cb7
WAA
2133
2134 /* adjust 'size' iff there are fewer bytes left in the
2135 * segment than what nfs_generic_pg_test returned */
2136 seg_left = seg_end - req_start;
2137 if (seg_left < size)
2138 size = (unsigned int)seg_left;
19b54848 2139 }
0f9c429e 2140
19b54848 2141 return size;
94ad1c80 2142}
89a58e32 2143EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
94ad1c80 2144
53113ad3 2145int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
e2fecb21
TM
2146{
2147 struct nfs_pageio_descriptor pgio;
e2fecb21
TM
2148
2149 /* Resend all requests through the MDS */
53113ad3
WAA
2150 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
2151 hdr->completion_ops);
c7070113 2152 set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags);
53113ad3 2153 return nfs_pageio_resend(&pgio, hdr);
e2fecb21 2154}
e7dd79af 2155EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
e2fecb21 2156
d45f60c6 2157static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
1acbbb4e 2158{
cd841605
FI
2159
2160 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
2161 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
1acbbb4e 2162 PNFS_LAYOUTRET_ON_ERROR) {
cd841605 2163 pnfs_return_layout(hdr->inode);
1acbbb4e 2164 }
6c75dc0d 2165 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
53113ad3 2166 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
1acbbb4e
FI
2167}
2168
d20581aa
BH
2169/*
2170 * Called by non rpc-based layout drivers
2171 */
d45f60c6 2172void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
44b83799 2173{
f8417b48 2174 if (likely(!hdr->pnfs_error)) {
67af7611
TM
2175 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
2176 hdr->mds_offset + hdr->res.count);
d45f60c6 2177 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
f8417b48
KM
2178 }
2179 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
2180 if (unlikely(hdr->pnfs_error))
d45f60c6
WAA
2181 pnfs_ld_handle_write_error(hdr);
2182 hdr->mds_ops->rpc_release(hdr);
44b83799 2183}
d20581aa 2184EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
44b83799 2185
dce81290
TM
2186static void
2187pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
d45f60c6 2188 struct nfs_pgio_header *hdr)
dce81290 2189{
48d635f1 2190 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
a7d42ddb 2191
6c75dc0d 2192 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
a7d42ddb 2193 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
6c75dc0d 2194 nfs_pageio_reset_write_mds(desc);
a7d42ddb 2195 mirror->pg_recoalesce = 1;
6c75dc0d 2196 }
d45f60c6 2197 nfs_pgio_data_destroy(hdr);
1ca018d2 2198 hdr->release(hdr);
dce81290
TM
2199}
2200
2201static enum pnfs_try_status
d45f60c6 2202pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
dce81290
TM
2203 const struct rpc_call_ops *call_ops,
2204 struct pnfs_layout_segment *lseg,
2205 int how)
0382b744 2206{
cd841605 2207 struct inode *inode = hdr->inode;
0382b744
AA
2208 enum pnfs_try_status trypnfs;
2209 struct nfs_server *nfss = NFS_SERVER(inode);
2210
cd841605 2211 hdr->mds_ops = call_ops;
0382b744
AA
2212
2213 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
d45f60c6
WAA
2214 inode->i_ino, hdr->args.count, hdr->args.offset, how);
2215 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
6c75dc0d 2216 if (trypnfs != PNFS_NOT_ATTEMPTED)
0382b744 2217 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
0382b744
AA
2218 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2219 return trypnfs;
2220}
2221
dce81290 2222static void
7f714720
WAA
2223pnfs_do_write(struct nfs_pageio_descriptor *desc,
2224 struct nfs_pgio_header *hdr, int how)
dce81290 2225{
dce81290
TM
2226 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2227 struct pnfs_layout_segment *lseg = desc->pg_lseg;
7f714720 2228 enum pnfs_try_status trypnfs;
dce81290 2229
d45f60c6 2230 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
7f714720 2231 if (trypnfs == PNFS_NOT_ATTEMPTED)
d45f60c6 2232 pnfs_write_through_mds(desc, hdr);
dce81290
TM
2233}
2234
6c75dc0d
FI
2235static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
2236{
9369a431 2237 pnfs_put_lseg(hdr->lseg);
1e7f3a48 2238 nfs_pgio_header_free(hdr);
6c75dc0d
FI
2239}
2240
dce81290
TM
2241int
2242pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
2243{
6c75dc0d 2244 struct nfs_pgio_header *hdr;
dce81290
TM
2245 int ret;
2246
1e7f3a48
WAA
2247 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2248 if (!hdr) {
2bff2288
PT
2249 desc->pg_error = -ENOMEM;
2250 return desc->pg_error;
dce81290 2251 }
6c75dc0d 2252 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
180bb5ec 2253
9369a431 2254 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
ef2c488c 2255 ret = nfs_generic_pgio(desc, hdr);
180bb5ec 2256 if (!ret)
7f714720 2257 pnfs_do_write(desc, hdr, desc->pg_ioflags);
a7d42ddb 2258
6c75dc0d 2259 return ret;
dce81290
TM
2260}
2261EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
2262
53113ad3 2263int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
62e4a769
TM
2264{
2265 struct nfs_pageio_descriptor pgio;
2266
1acbbb4e 2267 /* Resend all requests through the MDS */
53113ad3
WAA
2268 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
2269 return nfs_pageio_resend(&pgio, hdr);
1acbbb4e 2270}
e7dd79af 2271EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
1acbbb4e 2272
d45f60c6 2273static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
1acbbb4e 2274{
cd841605
FI
2275 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
2276 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
1acbbb4e 2277 PNFS_LAYOUTRET_ON_ERROR) {
cd841605 2278 pnfs_return_layout(hdr->inode);
1acbbb4e 2279 }
4db6e0b7 2280 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
53113ad3 2281 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
62e4a769
TM
2282}
2283
d20581aa
BH
2284/*
2285 * Called by non rpc-based layout drivers
2286 */
d45f60c6 2287void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
d20581aa 2288{
bfc505de 2289 if (likely(!hdr->pnfs_error))
d45f60c6 2290 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
f8417b48
KM
2291 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
2292 if (unlikely(hdr->pnfs_error))
d45f60c6
WAA
2293 pnfs_ld_handle_read_error(hdr);
2294 hdr->mds_ops->rpc_release(hdr);
d20581aa
BH
2295}
2296EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
2297
493292dd
TM
2298static void
2299pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
d45f60c6 2300 struct nfs_pgio_header *hdr)
493292dd 2301{
48d635f1 2302 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
a7d42ddb 2303
4db6e0b7 2304 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
a7d42ddb 2305 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
4db6e0b7 2306 nfs_pageio_reset_read_mds(desc);
a7d42ddb 2307 mirror->pg_recoalesce = 1;
4db6e0b7 2308 }
d45f60c6 2309 nfs_pgio_data_destroy(hdr);
1ca018d2 2310 hdr->release(hdr);
493292dd
TM
2311}
2312
64419a9b
AA
2313/*
2314 * Call the appropriate parallel I/O subsystem read function.
2315 */
493292dd 2316static enum pnfs_try_status
d45f60c6 2317pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
493292dd
TM
2318 const struct rpc_call_ops *call_ops,
2319 struct pnfs_layout_segment *lseg)
64419a9b 2320{
cd841605 2321 struct inode *inode = hdr->inode;
64419a9b
AA
2322 struct nfs_server *nfss = NFS_SERVER(inode);
2323 enum pnfs_try_status trypnfs;
2324
cd841605 2325 hdr->mds_ops = call_ops;
64419a9b
AA
2326
2327 dprintk("%s: Reading ino:%lu %u@%llu\n",
d45f60c6 2328 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
64419a9b 2329
d45f60c6 2330 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
4db6e0b7 2331 if (trypnfs != PNFS_NOT_ATTEMPTED)
64419a9b 2332 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
64419a9b
AA
2333 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2334 return trypnfs;
2335}
863a3c6c 2336
ceb11e13 2337/* Resend all requests through pnfs. */
1b1bc66b 2338void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
ceb11e13
PT
2339{
2340 struct nfs_pageio_descriptor pgio;
2341
1b1bc66b 2342 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
54e4a0df
TM
2343 /* Prevent deadlocks with layoutreturn! */
2344 pnfs_put_lseg(hdr->lseg);
2345 hdr->lseg = NULL;
2346
1b1bc66b
WAA
2347 nfs_pageio_init_read(&pgio, hdr->inode, false,
2348 hdr->completion_ops);
2349 hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
2350 }
ceb11e13
PT
2351}
2352EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
2353
493292dd 2354static void
7f714720 2355pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
493292dd 2356{
493292dd
TM
2357 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2358 struct pnfs_layout_segment *lseg = desc->pg_lseg;
7f714720 2359 enum pnfs_try_status trypnfs;
493292dd 2360
d45f60c6 2361 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
ceb11e13 2362 if (trypnfs == PNFS_TRY_AGAIN)
1b1bc66b
WAA
2363 pnfs_read_resend_pnfs(hdr);
2364 if (trypnfs == PNFS_NOT_ATTEMPTED || hdr->task.tk_status)
d45f60c6 2365 pnfs_read_through_mds(desc, hdr);
493292dd
TM
2366}
2367
4db6e0b7
FI
2368static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
2369{
9369a431 2370 pnfs_put_lseg(hdr->lseg);
1e7f3a48 2371 nfs_pgio_header_free(hdr);
4db6e0b7
FI
2372}
2373
493292dd
TM
2374int
2375pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2376{
4db6e0b7 2377 struct nfs_pgio_header *hdr;
493292dd
TM
2378 int ret;
2379
1e7f3a48
WAA
2380 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2381 if (!hdr) {
2bff2288
PT
2382 desc->pg_error = -ENOMEM;
2383 return desc->pg_error;
493292dd 2384 }
4db6e0b7 2385 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
9369a431 2386 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
ef2c488c 2387 ret = nfs_generic_pgio(desc, hdr);
180bb5ec 2388 if (!ret)
7f714720 2389 pnfs_do_read(desc, hdr);
4db6e0b7 2390 return ret;
493292dd
TM
2391}
2392EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
2393
71244d9b
TM
2394static void pnfs_clear_layoutcommitting(struct inode *inode)
2395{
2396 unsigned long *bitlock = &NFS_I(inode)->flags;
2397
2398 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
4e857c58 2399 smp_mb__after_atomic();
71244d9b
TM
2400 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2401}
2402
863a3c6c 2403/*
a9bae566 2404 * There can be multiple RW segments.
863a3c6c 2405 */
a9bae566 2406static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
863a3c6c 2407{
a9bae566 2408 struct pnfs_layout_segment *lseg;
863a3c6c 2409
a9bae566
PT
2410 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2411 if (lseg->pls_range.iomode == IOMODE_RW &&
a073dbff 2412 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
a9bae566
PT
2413 list_add(&lseg->pls_lc_list, listp);
2414 }
863a3c6c
AA
2415}
2416
a073dbff
TM
2417static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2418{
2419 struct pnfs_layout_segment *lseg, *tmp;
a073dbff
TM
2420
2421 /* Matched by references in pnfs_set_layoutcommit */
2422 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2423 list_del_init(&lseg->pls_lc_list);
2424 pnfs_put_lseg(lseg);
2425 }
2426
71244d9b 2427 pnfs_clear_layoutcommitting(inode);
a073dbff
TM
2428}
2429
1b0ae068
PT
2430void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2431{
b9e028fd 2432 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
1b0ae068
PT
2433}
2434EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2435
863a3c6c 2436void
67af7611
TM
2437pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
2438 loff_t end_pos)
863a3c6c 2439{
cd841605 2440 struct nfs_inode *nfsi = NFS_I(inode);
79a48a1f 2441 bool mark_as_dirty = false;
863a3c6c 2442
cd841605 2443 spin_lock(&inode->i_lock);
863a3c6c 2444 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
29559b11 2445 nfsi->layout->plh_lwb = end_pos;
79a48a1f 2446 mark_as_dirty = true;
863a3c6c 2447 dprintk("%s: Set layoutcommit for inode %lu ",
cd841605 2448 __func__, inode->i_ino);
29559b11
TM
2449 } else if (end_pos > nfsi->layout->plh_lwb)
2450 nfsi->layout->plh_lwb = end_pos;
67af7611 2451 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
a9bae566 2452 /* references matched in nfs4_layoutcommit_release */
67af7611 2453 pnfs_get_lseg(lseg);
a9bae566 2454 }
cd841605 2455 spin_unlock(&inode->i_lock);
acff5880 2456 dprintk("%s: lseg %p end_pos %llu\n",
67af7611 2457 __func__, lseg, nfsi->layout->plh_lwb);
79a48a1f
WAA
2458
2459 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2460 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2461 if (mark_as_dirty)
cd841605 2462 mark_inode_dirty_sync(inode);
863a3c6c
AA
2463}
2464EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2465
db29c089
AA
2466void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2467{
2468 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2469
2470 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2471 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
a073dbff 2472 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
db29c089
AA
2473}
2474
de4b15c7
AA
2475/*
2476 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2477 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2478 * data to disk to allow the server to recover the data if it crashes.
2479 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2480 * is off, and a COMMIT is sent to a data server, or
2481 * if WRITEs to a data server return NFS_DATA_SYNC.
2482 */
863a3c6c 2483int
ef311537 2484pnfs_layoutcommit_inode(struct inode *inode, bool sync)
863a3c6c 2485{
5f919c9f 2486 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
863a3c6c
AA
2487 struct nfs4_layoutcommit_data *data;
2488 struct nfs_inode *nfsi = NFS_I(inode);
863a3c6c 2489 loff_t end_pos;
71244d9b 2490 int status;
863a3c6c 2491
71244d9b 2492 if (!pnfs_layoutcommit_outstanding(inode))
de4b15c7
AA
2493 return 0;
2494
71244d9b 2495 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
92407e75 2496
71244d9b 2497 status = -EAGAIN;
92407e75 2498 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
71244d9b
TM
2499 if (!sync)
2500 goto out;
74316201 2501 status = wait_on_bit_lock_action(&nfsi->flags,
71244d9b
TM
2502 NFS_INO_LAYOUTCOMMITTING,
2503 nfs_wait_bit_killable,
2504 TASK_KILLABLE);
92407e75 2505 if (status)
71244d9b 2506 goto out;
92407e75
PT
2507 }
2508
71244d9b
TM
2509 status = -ENOMEM;
2510 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2511 data = kzalloc(sizeof(*data), GFP_NOFS);
2512 if (!data)
2513 goto clear_layoutcommitting;
2514
2515 status = 0;
de4b15c7 2516 spin_lock(&inode->i_lock);
71244d9b
TM
2517 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2518 goto out_unlock;
a9bae566 2519
71244d9b 2520 INIT_LIST_HEAD(&data->lseg_list);
a9bae566 2521 pnfs_list_write_lseg(inode, &data->lseg_list);
863a3c6c 2522
acff5880 2523 end_pos = nfsi->layout->plh_lwb;
863a3c6c 2524
f597c537 2525 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
863a3c6c
AA
2526 spin_unlock(&inode->i_lock);
2527
2528 data->args.inode = inode;
9fa40758 2529 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
863a3c6c
AA
2530 nfs_fattr_init(&data->fattr);
2531 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2532 data->res.fattr = &data->fattr;
2e18d4d8
TM
2533 if (end_pos != 0)
2534 data->args.lastbytewritten = end_pos - 1;
2535 else
2536 data->args.lastbytewritten = U64_MAX;
863a3c6c
AA
2537 data->res.server = NFS_SERVER(inode);
2538
5f919c9f
CH
2539 if (ld->prepare_layoutcommit) {
2540 status = ld->prepare_layoutcommit(&data->args);
2541 if (status) {
3471648a 2542 put_rpccred(data->cred);
5f919c9f 2543 spin_lock(&inode->i_lock);
29559b11
TM
2544 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2545 if (end_pos > nfsi->layout->plh_lwb)
5f919c9f 2546 nfsi->layout->plh_lwb = end_pos;
3471648a 2547 goto out_unlock;
5f919c9f
CH
2548 }
2549 }
2550
2551
863a3c6c
AA
2552 status = nfs4_proc_layoutcommit(data, sync);
2553out:
92407e75
PT
2554 if (status)
2555 mark_inode_dirty_sync(inode);
863a3c6c
AA
2556 dprintk("<-- %s status %d\n", __func__, status);
2557 return status;
71244d9b
TM
2558out_unlock:
2559 spin_unlock(&inode->i_lock);
92407e75 2560 kfree(data);
71244d9b
TM
2561clear_layoutcommitting:
2562 pnfs_clear_layoutcommitting(inode);
92407e75 2563 goto out;
863a3c6c 2564}
72cff449 2565EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
82be417a 2566
5bb89b47
TM
2567int
2568pnfs_generic_sync(struct inode *inode, bool datasync)
2569{
2570 return pnfs_layoutcommit_inode(inode, true);
2571}
2572EXPORT_SYMBOL_GPL(pnfs_generic_sync);
2573
82be417a
AA
2574struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
2575{
2576 struct nfs4_threshold *thp;
2577
2578 thp = kzalloc(sizeof(*thp), GFP_NOFS);
2579 if (!thp) {
2580 dprintk("%s mdsthreshold allocation failed\n", __func__);
2581 return NULL;
2582 }
2583 return thp;
2584}
8733408d 2585
865a7ecb 2586#if IS_ENABLED(CONFIG_NFS_V4_2)
8733408d 2587int
c8ad8894 2588pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
8733408d
PT
2589{
2590 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2591 struct nfs_server *server = NFS_SERVER(inode);
1bfe3b25 2592 struct nfs_inode *nfsi = NFS_I(inode);
8733408d
PT
2593 struct nfs42_layoutstat_data *data;
2594 struct pnfs_layout_hdr *hdr;
2595 int status = 0;
2596
2597 if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
2598 goto out;
2599
6c5a0d89
TM
2600 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
2601 goto out;
2602
1bfe3b25
PT
2603 if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
2604 goto out;
2605
8733408d
PT
2606 spin_lock(&inode->i_lock);
2607 if (!NFS_I(inode)->layout) {
2608 spin_unlock(&inode->i_lock);
f538d0ba 2609 goto out_clear_layoutstats;
8733408d
PT
2610 }
2611 hdr = NFS_I(inode)->layout;
2612 pnfs_get_layout_hdr(hdr);
2613 spin_unlock(&inode->i_lock);
2614
c8ad8894 2615 data = kzalloc(sizeof(*data), gfp_flags);
8733408d
PT
2616 if (!data) {
2617 status = -ENOMEM;
2618 goto out_put;
2619 }
2620
2621 data->args.fh = NFS_FH(inode);
2622 data->args.inode = inode;
8733408d
PT
2623 status = ld->prepare_layoutstats(&data->args);
2624 if (status)
2625 goto out_free;
2626
2627 status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
2628
2629out:
2630 dprintk("%s returns %d\n", __func__, status);
2631 return status;
2632
2633out_free:
2634 kfree(data);
2635out_put:
2636 pnfs_put_layout_hdr(hdr);
f538d0ba 2637out_clear_layoutstats:
1bfe3b25
PT
2638 smp_mb__before_atomic();
2639 clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
2640 smp_mb__after_atomic();
8733408d
PT
2641 goto out;
2642}
2643EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
865a7ecb 2644#endif
bbf58bf3
TM
2645
2646unsigned int layoutstats_timer;
2647module_param(layoutstats_timer, uint, 0644);
2648EXPORT_SYMBOL_GPL(layoutstats_timer);