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