nfs/flexfiles: send layoutreturn before freeing lseg
[deliverable/linux.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"
85e174ba
RL
37
38#define NFSDBG_FACILITY NFSDBG_PNFS
25c75333 39#define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
85e174ba 40
02c35fca
FI
41/* Locking:
42 *
43 * pnfs_spinlock:
44 * protects pnfs_modules_tbl.
45 */
46static DEFINE_SPINLOCK(pnfs_spinlock);
47
48/*
49 * pnfs_modules_tbl holds all pnfs modules
50 */
51static LIST_HEAD(pnfs_modules_tbl);
52
aa1e0e3a
PT
53static int
54pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, nfs4_stateid stateid,
6c16605d 55 enum pnfs_iomode iomode, bool sync);
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
244 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
566f8737
PT
245 if (!list_empty(&lo->plh_segs))
246 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
6622c3ea 247 pnfs_detach_layout_hdr(lo);
cc6e5340 248 spin_unlock(&inode->i_lock);
6622c3ea 249 pnfs_free_layout_hdr(lo);
cc6e5340 250 }
974cec8c
AA
251}
252
b9e028fd
TM
253static int
254pnfs_iomode_to_fail_bit(u32 iomode)
255{
256 return iomode == IOMODE_RW ?
257 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
258}
259
260static void
3e621214 261pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
b9e028fd 262{
25c75333 263 lo->plh_retry_timestamp = jiffies;
39e88fcf 264 if (!test_and_set_bit(fail_bit, &lo->plh_flags))
3e621214
TM
265 atomic_inc(&lo->plh_refcount);
266}
267
268static void
269pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
270{
271 if (test_and_clear_bit(fail_bit, &lo->plh_flags))
272 atomic_dec(&lo->plh_refcount);
273}
274
275static void
276pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
277{
278 struct inode *inode = lo->plh_inode;
115ce575
TM
279 struct pnfs_layout_range range = {
280 .iomode = iomode,
281 .offset = 0,
282 .length = NFS4_MAX_UINT64,
283 };
284 LIST_HEAD(head);
3e621214
TM
285
286 spin_lock(&inode->i_lock);
287 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
115ce575 288 pnfs_mark_matching_lsegs_invalid(lo, &head, &range);
3e621214 289 spin_unlock(&inode->i_lock);
115ce575 290 pnfs_free_lseg_list(&head);
b9e028fd
TM
291 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
292 iomode == IOMODE_RW ? "RW" : "READ");
293}
294
295static bool
296pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
297{
25c75333 298 unsigned long start, end;
3e621214
TM
299 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
300
301 if (test_bit(fail_bit, &lo->plh_flags) == 0)
25c75333
TM
302 return false;
303 end = jiffies;
304 start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
305 if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
306 /* It is time to retry the failed layoutgets */
3e621214 307 pnfs_layout_clear_fail_bit(lo, fail_bit);
25c75333
TM
308 return false;
309 }
310 return true;
b9e028fd
TM
311}
312
974cec8c
AA
313static void
314init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
315{
566052c5 316 INIT_LIST_HEAD(&lseg->pls_list);
a9bae566 317 INIT_LIST_HEAD(&lseg->pls_lc_list);
4541d16c
FI
318 atomic_set(&lseg->pls_refcount, 1);
319 smp_mb();
320 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
566052c5 321 lseg->pls_layout = lo;
974cec8c
AA
322}
323
905ca191 324static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
974cec8c 325{
b7edfaa1 326 struct inode *ino = lseg->pls_layout->plh_inode;
974cec8c 327
b1f69b75 328 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
974cec8c
AA
329}
330
d684d2ae 331static void
57036a37
TM
332pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
333 struct pnfs_layout_segment *lseg)
d684d2ae 334{
57036a37 335 struct inode *inode = lo->plh_inode;
d684d2ae 336
d20581aa 337 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
d684d2ae 338 list_del_init(&lseg->pls_list);
8f0d27dc
TM
339 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
340 atomic_dec(&lo->plh_refcount);
173f77e9
TM
341 if (list_empty(&lo->plh_segs))
342 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
d684d2ae
FI
343 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
344}
345
aa1e0e3a
PT
346/* Return true if layoutreturn is needed */
347static bool
348pnfs_layout_need_return(struct pnfs_layout_hdr *lo,
27b6f539 349 struct pnfs_layout_segment *lseg)
aa1e0e3a
PT
350{
351 struct pnfs_layout_segment *s;
352
353 if (!test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
354 return false;
355
356 list_for_each_entry(s, &lo->plh_segs, pls_list)
27b6f539 357 if (s != lseg && test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
aa1e0e3a
PT
358 return false;
359
aa1e0e3a
PT
360 return true;
361}
362
27b6f539
PT
363static void pnfs_layoutreturn_free_lseg(struct work_struct *work)
364{
365 struct pnfs_layout_segment *lseg;
366 struct pnfs_layout_hdr *lo;
367 struct inode *inode;
368
369 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
370 WARN_ON(atomic_read(&lseg->pls_refcount));
371 lo = lseg->pls_layout;
372 inode = lo->plh_inode;
373
374 spin_lock(&inode->i_lock);
375 if (pnfs_layout_need_return(lo, lseg)) {
376 nfs4_stateid stateid;
377 enum pnfs_iomode iomode;
378
379 stateid = lo->plh_stateid;
380 iomode = lo->plh_return_iomode;
381 /* decreased in pnfs_send_layoutreturn() */
382 lo->plh_block_lgets++;
383 lo->plh_return_iomode = 0;
384 spin_unlock(&inode->i_lock);
385
386 pnfs_send_layoutreturn(lo, stateid, iomode, true);
387 spin_lock(&inode->i_lock);
388 } else
389 /* match pnfs_get_layout_hdr #2 in pnfs_put_lseg */
390 pnfs_put_layout_hdr(lo);
391 pnfs_layout_remove_lseg(lo, lseg);
392 spin_unlock(&inode->i_lock);
393 pnfs_free_lseg(lseg);
394 /* match pnfs_get_layout_hdr #1 in pnfs_put_lseg */
395 pnfs_put_layout_hdr(lo);
396}
397
398static void
399pnfs_layoutreturn_free_lseg_async(struct pnfs_layout_segment *lseg)
400{
401 INIT_WORK(&lseg->pls_work, pnfs_layoutreturn_free_lseg);
402 queue_work(nfsiod_workqueue, &lseg->pls_work);
403}
404
bae724ef 405void
9369a431 406pnfs_put_lseg(struct pnfs_layout_segment *lseg)
974cec8c 407{
57036a37 408 struct pnfs_layout_hdr *lo;
d684d2ae
FI
409 struct inode *inode;
410
411 if (!lseg)
412 return;
413
4541d16c
FI
414 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
415 atomic_read(&lseg->pls_refcount),
416 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
57036a37
TM
417 lo = lseg->pls_layout;
418 inode = lo->plh_inode;
d684d2ae 419 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
8f0d27dc 420 pnfs_get_layout_hdr(lo);
27b6f539
PT
421 if (pnfs_layout_need_return(lo, lseg)) {
422 spin_unlock(&inode->i_lock);
423 /* hdr reference dropped in nfs4_layoutreturn_release */
424 pnfs_get_layout_hdr(lo);
425 pnfs_layoutreturn_free_lseg_async(lseg);
426 } else {
427 pnfs_layout_remove_lseg(lo, lseg);
428 spin_unlock(&inode->i_lock);
429 pnfs_free_lseg(lseg);
aa1e0e3a 430 pnfs_put_layout_hdr(lo);
27b6f539 431 }
4541d16c 432 }
4541d16c 433}
9369a431 434EXPORT_SYMBOL_GPL(pnfs_put_lseg);
974cec8c 435
6543f803 436static void pnfs_free_lseg_async_work(struct work_struct *work)
e6cf82d1
WAA
437{
438 struct pnfs_layout_segment *lseg;
6543f803 439 struct pnfs_layout_hdr *lo;
e6cf82d1
WAA
440
441 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
6543f803 442 lo = lseg->pls_layout;
e6cf82d1 443
6543f803
TM
444 pnfs_free_lseg(lseg);
445 pnfs_put_layout_hdr(lo);
e6cf82d1
WAA
446}
447
6543f803 448static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg)
e6cf82d1 449{
6543f803 450 INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work);
e6cf82d1
WAA
451 schedule_work(&lseg->pls_work);
452}
6543f803
TM
453
454void
455pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg)
456{
457 if (!lseg)
458 return;
459
460 assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock);
461
462 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
463 atomic_read(&lseg->pls_refcount),
464 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
465 if (atomic_dec_and_test(&lseg->pls_refcount)) {
466 struct pnfs_layout_hdr *lo = lseg->pls_layout;
467 pnfs_get_layout_hdr(lo);
468 pnfs_layout_remove_lseg(lo, lseg);
469 pnfs_free_lseg_async(lseg);
470 }
471}
472EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked);
e6cf82d1 473
3cb2df17 474static u64
fb3296eb
BH
475end_offset(u64 start, u64 len)
476{
477 u64 end;
478
479 end = start + len;
480 return end >= start ? end : NFS4_MAX_UINT64;
481}
482
fb3296eb
BH
483/*
484 * is l2 fully contained in l1?
485 * start1 end1
486 * [----------------------------------)
487 * start2 end2
488 * [----------------)
489 */
3cb2df17 490static bool
7dc0ac70 491pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
3cb2df17 492 const struct pnfs_layout_range *l2)
fb3296eb
BH
493{
494 u64 start1 = l1->offset;
495 u64 end1 = end_offset(start1, l1->length);
496 u64 start2 = l2->offset;
497 u64 end2 = end_offset(start2, l2->length);
498
499 return (start1 <= start2) && (end1 >= end2);
500}
501
502/*
503 * is l1 and l2 intersecting?
504 * start1 end1
505 * [----------------------------------)
506 * start2 end2
507 * [----------------)
508 */
3cb2df17 509static bool
7dc0ac70 510pnfs_lseg_range_intersecting(const struct pnfs_layout_range *l1,
3cb2df17 511 const struct pnfs_layout_range *l2)
fb3296eb
BH
512{
513 u64 start1 = l1->offset;
514 u64 end1 = end_offset(start1, l1->length);
515 u64 start2 = l2->offset;
516 u64 end2 = end_offset(start2, l2->length);
517
518 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
519 (end2 == NFS4_MAX_UINT64 || end2 > start1);
520}
521
4541d16c 522static bool
3cb2df17
TM
523should_free_lseg(const struct pnfs_layout_range *lseg_range,
524 const struct pnfs_layout_range *recall_range)
4541d16c 525{
778b5502
BH
526 return (recall_range->iomode == IOMODE_ANY ||
527 lseg_range->iomode == recall_range->iomode) &&
7dc0ac70 528 pnfs_lseg_range_intersecting(lseg_range, recall_range);
974cec8c
AA
529}
530
24956804
TM
531static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
532 struct list_head *tmp_list)
533{
534 if (!atomic_dec_and_test(&lseg->pls_refcount))
535 return false;
536 pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
537 list_add(&lseg->pls_list, tmp_list);
538 return true;
539}
540
4541d16c
FI
541/* Returns 1 if lseg is removed from list, 0 otherwise */
542static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
543 struct list_head *tmp_list)
544{
545 int rv = 0;
546
547 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
548 /* Remove the reference keeping the lseg in the
549 * list. It will now be removed when all
550 * outstanding io is finished.
551 */
d684d2ae
FI
552 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
553 atomic_read(&lseg->pls_refcount));
24956804 554 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
d684d2ae 555 rv = 1;
4541d16c
FI
556 }
557 return rv;
558}
559
560/* Returns count of number of matching invalid lsegs remaining in list
561 * after call.
562 */
43f1b3da 563int
49a85061 564pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
4541d16c 565 struct list_head *tmp_list,
778b5502 566 struct pnfs_layout_range *recall_range)
974cec8c
AA
567{
568 struct pnfs_layout_segment *lseg, *next;
4541d16c 569 int invalid = 0, removed = 0;
974cec8c
AA
570
571 dprintk("%s:Begin lo %p\n", __func__, lo);
572
8006bfba 573 if (list_empty(&lo->plh_segs))
38511722 574 return 0;
4541d16c 575 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
778b5502
BH
576 if (!recall_range ||
577 should_free_lseg(&lseg->pls_range, recall_range)) {
4541d16c
FI
578 dprintk("%s: freeing lseg %p iomode %d "
579 "offset %llu length %llu\n", __func__,
580 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
581 lseg->pls_range.length);
582 invalid++;
583 removed += mark_lseg_invalid(lseg, tmp_list);
584 }
585 dprintk("%s:Return %i\n", __func__, invalid - removed);
586 return invalid - removed;
974cec8c
AA
587}
588
f49f9baa 589/* note free_me must contain lsegs from a single layout_hdr */
43f1b3da 590void
4541d16c 591pnfs_free_lseg_list(struct list_head *free_me)
974cec8c 592{
4541d16c 593 struct pnfs_layout_segment *lseg, *tmp;
f49f9baa
FI
594
595 if (list_empty(free_me))
596 return;
597
4541d16c 598 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
566052c5 599 list_del(&lseg->pls_list);
905ca191 600 pnfs_free_lseg(lseg);
974cec8c
AA
601 }
602}
603
e5e94017
BH
604void
605pnfs_destroy_layout(struct nfs_inode *nfsi)
606{
607 struct pnfs_layout_hdr *lo;
974cec8c 608 LIST_HEAD(tmp_list);
e5e94017
BH
609
610 spin_lock(&nfsi->vfs_inode.i_lock);
611 lo = nfsi->layout;
612 if (lo) {
38511722 613 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
49a85061 614 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
3e621214
TM
615 pnfs_get_layout_hdr(lo);
616 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
617 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
618 spin_unlock(&nfsi->vfs_inode.i_lock);
619 pnfs_free_lseg_list(&tmp_list);
620 pnfs_put_layout_hdr(lo);
621 } else
622 spin_unlock(&nfsi->vfs_inode.i_lock);
974cec8c 623}
041245c8 624EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
974cec8c 625
fd9a8d71
TM
626static bool
627pnfs_layout_add_bulk_destroy_list(struct inode *inode,
628 struct list_head *layout_list)
974cec8c
AA
629{
630 struct pnfs_layout_hdr *lo;
fd9a8d71 631 bool ret = false;
974cec8c 632
fd9a8d71
TM
633 spin_lock(&inode->i_lock);
634 lo = NFS_I(inode)->layout;
635 if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
636 pnfs_get_layout_hdr(lo);
637 list_add(&lo->plh_bulk_destroy, layout_list);
638 ret = true;
639 }
640 spin_unlock(&inode->i_lock);
641 return ret;
642}
643
644/* Caller must hold rcu_read_lock and clp->cl_lock */
645static int
646pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
647 struct nfs_server *server,
648 struct list_head *layout_list)
649{
650 struct pnfs_layout_hdr *lo, *next;
651 struct inode *inode;
652
653 list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
654 inode = igrab(lo->plh_inode);
655 if (inode == NULL)
656 continue;
657 list_del_init(&lo->plh_layouts);
658 if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
659 continue;
660 rcu_read_unlock();
661 spin_unlock(&clp->cl_lock);
662 iput(inode);
663 spin_lock(&clp->cl_lock);
664 rcu_read_lock();
665 return -EAGAIN;
666 }
667 return 0;
668}
669
670static int
671pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
672 bool is_bulk_recall)
673{
674 struct pnfs_layout_hdr *lo;
675 struct inode *inode;
676 struct pnfs_layout_range range = {
677 .iomode = IOMODE_ANY,
678 .offset = 0,
679 .length = NFS4_MAX_UINT64,
680 };
681 LIST_HEAD(lseg_list);
682 int ret = 0;
683
684 while (!list_empty(layout_list)) {
685 lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
686 plh_bulk_destroy);
687 dprintk("%s freeing layout for inode %lu\n", __func__,
688 lo->plh_inode->i_ino);
689 inode = lo->plh_inode;
7c5d1875
CH
690
691 pnfs_layoutcommit_inode(inode, false);
692
fd9a8d71
TM
693 spin_lock(&inode->i_lock);
694 list_del_init(&lo->plh_bulk_destroy);
695 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
696 if (is_bulk_recall)
697 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
698 if (pnfs_mark_matching_lsegs_invalid(lo, &lseg_list, &range))
699 ret = -EAGAIN;
700 spin_unlock(&inode->i_lock);
701 pnfs_free_lseg_list(&lseg_list);
702 pnfs_put_layout_hdr(lo);
703 iput(inode);
704 }
705 return ret;
706}
707
708int
709pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
710 struct nfs_fsid *fsid,
711 bool is_recall)
712{
713 struct nfs_server *server;
714 LIST_HEAD(layout_list);
c47abcf8 715
974cec8c 716 spin_lock(&clp->cl_lock);
6382a441 717 rcu_read_lock();
fd9a8d71 718restart:
6382a441 719 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
fd9a8d71
TM
720 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
721 continue;
722 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
723 server,
724 &layout_list) != 0)
725 goto restart;
6382a441
WAA
726 }
727 rcu_read_unlock();
974cec8c
AA
728 spin_unlock(&clp->cl_lock);
729
fd9a8d71
TM
730 if (list_empty(&layout_list))
731 return 0;
732 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
733}
734
735int
736pnfs_destroy_layouts_byclid(struct nfs_client *clp,
737 bool is_recall)
738{
739 struct nfs_server *server;
740 LIST_HEAD(layout_list);
741
742 spin_lock(&clp->cl_lock);
743 rcu_read_lock();
744restart:
745 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
746 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
747 server,
748 &layout_list) != 0)
749 goto restart;
974cec8c 750 }
fd9a8d71
TM
751 rcu_read_unlock();
752 spin_unlock(&clp->cl_lock);
753
754 if (list_empty(&layout_list))
755 return 0;
756 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
757}
758
759/*
760 * Called by the state manger to remove all layouts established under an
761 * expired lease.
762 */
763void
764pnfs_destroy_all_layouts(struct nfs_client *clp)
765{
766 nfs4_deviceid_mark_client_invalid(clp);
767 nfs4_deviceid_purge_client(clp);
768
769 pnfs_destroy_layouts_byclid(clp, false);
e5e94017
BH
770}
771
5a65503f
TM
772/*
773 * Compare 2 layout stateid sequence ids, to see which is newer,
774 * taking into account wraparound issues.
775 */
776static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
777{
2c64c57d 778 return (s32)(s1 - s2) > 0;
5a65503f
TM
779}
780
fd6002e9 781/* update lo->plh_stateid with new if is more recent */
43f1b3da
FI
782void
783pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
784 bool update_barrier)
b1f69b75 785{
22aaf714
TM
786 u32 oldseq, newseq, new_barrier;
787 int empty = list_empty(&lo->plh_segs);
b1f69b75 788
2d2f24ad
TM
789 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
790 newseq = be32_to_cpu(new->seqid);
22aaf714 791 if (empty || pnfs_seqid_is_newer(newseq, oldseq)) {
f597c537 792 nfs4_stateid_copy(&lo->plh_stateid, new);
43f1b3da 793 if (update_barrier) {
22aaf714 794 new_barrier = be32_to_cpu(new->seqid);
43f1b3da
FI
795 } else {
796 /* Because of wraparound, we want to keep the barrier
22aaf714 797 * "close" to the current seqids.
43f1b3da 798 */
22aaf714 799 new_barrier = newseq - atomic_read(&lo->plh_outstanding);
43f1b3da 800 }
22aaf714
TM
801 if (empty || pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
802 lo->plh_barrier = new_barrier;
43f1b3da 803 }
b1f69b75
AA
804}
805
cf7d63f1 806static bool
19c54aba
TM
807pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
808 const nfs4_stateid *stateid)
43f1b3da 809{
19c54aba 810 u32 seqid = be32_to_cpu(stateid->seqid);
25a1a621 811
19c54aba
TM
812 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
813}
814
ce6ab4f2
PT
815static bool
816pnfs_layout_returning(const struct pnfs_layout_hdr *lo,
817 struct pnfs_layout_range *range)
818{
819 return test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) &&
820 (lo->plh_return_iomode == IOMODE_ANY ||
821 lo->plh_return_iomode == range->iomode);
822}
823
19c54aba
TM
824/* lget is set to 1 if called from inside send_layoutget call chain */
825static bool
ce6ab4f2
PT
826pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo,
827 struct pnfs_layout_range *range, int lget)
19c54aba 828{
f7e8917a
FI
829 return lo->plh_block_lgets ||
830 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
43f1b3da 831 (list_empty(&lo->plh_segs) &&
ce6ab4f2
PT
832 (atomic_read(&lo->plh_outstanding) > lget)) ||
833 pnfs_layout_returning(lo, range);
cf7d63f1
FI
834}
835
fd6002e9
FI
836int
837pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
ce6ab4f2 838 struct pnfs_layout_range *range,
fd6002e9 839 struct nfs4_state *open_state)
b1f69b75 840{
fd6002e9 841 int status = 0;
974cec8c 842
b1f69b75 843 dprintk("--> %s\n", __func__);
fd6002e9 844 spin_lock(&lo->plh_inode->i_lock);
ce6ab4f2 845 if (pnfs_layoutgets_blocked(lo, range, 1)) {
cf7d63f1 846 status = -EAGAIN;
5d422301
TM
847 } else if (!nfs4_valid_open_stateid(open_state)) {
848 status = -EBADF;
47abadef
CH
849 } else if (list_empty(&lo->plh_segs) ||
850 test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
fd6002e9
FI
851 int seq;
852
853 do {
854 seq = read_seqbegin(&open_state->seqlock);
f597c537 855 nfs4_stateid_copy(dst, &open_state->stateid);
fd6002e9
FI
856 } while (read_seqretry(&open_state->seqlock, seq));
857 } else
f597c537 858 nfs4_stateid_copy(dst, &lo->plh_stateid);
fd6002e9 859 spin_unlock(&lo->plh_inode->i_lock);
b1f69b75 860 dprintk("<-- %s\n", __func__);
fd6002e9 861 return status;
b1f69b75
AA
862}
863
864/*
865* Get layout from server.
866* for now, assume that whole file layouts are requested.
867* arg->offset: 0
868* arg->length: all ones
869*/
e5e94017
BH
870static struct pnfs_layout_segment *
871send_layoutget(struct pnfs_layout_hdr *lo,
872 struct nfs_open_context *ctx,
fb3296eb 873 struct pnfs_layout_range *range,
a75b9df9 874 gfp_t gfp_flags)
e5e94017 875{
b7edfaa1 876 struct inode *ino = lo->plh_inode;
b1f69b75
AA
877 struct nfs_server *server = NFS_SERVER(ino);
878 struct nfs4_layoutget *lgp;
a0b0a6e3 879 struct pnfs_layout_segment *lseg;
b1f69b75
AA
880
881 dprintk("--> %s\n", __func__);
e5e94017 882
a75b9df9 883 lgp = kzalloc(sizeof(*lgp), gfp_flags);
cf7d63f1 884 if (lgp == NULL)
b1f69b75 885 return NULL;
35124a09 886
fb3296eb
BH
887 lgp->args.minlength = PAGE_CACHE_SIZE;
888 if (lgp->args.minlength > range->length)
889 lgp->args.minlength = range->length;
b1f69b75 890 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
fb3296eb 891 lgp->args.range = *range;
b1f69b75
AA
892 lgp->args.type = server->pnfs_curr_ld->id;
893 lgp->args.inode = ino;
894 lgp->args.ctx = get_nfs_open_context(ctx);
a75b9df9 895 lgp->gfp_flags = gfp_flags;
6ab59344 896 lgp->cred = lo->plh_lc_cred;
b1f69b75
AA
897
898 /* Synchronously retrieve layout information from server and
899 * store in lseg.
900 */
a0b0a6e3
TM
901 lseg = nfs4_proc_layoutget(lgp, gfp_flags);
902 if (IS_ERR(lseg)) {
903 switch (PTR_ERR(lseg)) {
904 case -ENOMEM:
905 case -ERESTARTSYS:
906 break;
907 default:
908 /* remember that LAYOUTGET failed and suspend trying */
b9e028fd 909 pnfs_layout_io_set_failed(lo, range->iomode);
a0b0a6e3
TM
910 }
911 return NULL;
974cec8c 912 }
35124a09 913
974cec8c
AA
914 return lseg;
915}
916
24956804
TM
917static void pnfs_clear_layoutcommit(struct inode *inode,
918 struct list_head *head)
919{
920 struct nfs_inode *nfsi = NFS_I(inode);
921 struct pnfs_layout_segment *lseg, *tmp;
922
923 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
924 return;
925 list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
926 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
927 continue;
928 pnfs_lseg_dec_and_remove_zero(lseg, head);
929 }
930}
931
f40eb5d0
PT
932static int
933pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, nfs4_stateid stateid,
6c16605d 934 enum pnfs_iomode iomode, bool sync)
f40eb5d0
PT
935{
936 struct inode *ino = lo->plh_inode;
937 struct nfs4_layoutreturn *lrp;
938 int status = 0;
939
940 lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
941 if (unlikely(lrp == NULL)) {
942 status = -ENOMEM;
943 spin_lock(&ino->i_lock);
944 lo->plh_block_lgets--;
193e3aa2 945 rpc_wake_up(&NFS_SERVER(ino)->roc_rpcwaitq);
f40eb5d0
PT
946 spin_unlock(&ino->i_lock);
947 pnfs_put_layout_hdr(lo);
948 goto out;
949 }
950
951 lrp->args.stateid = stateid;
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
293b3b06
AA
967/*
968 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
969 * when the layout segment list is empty.
970 *
971 * Note that a pnfs_layout_hdr can exist with an empty layout segment
972 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
973 * deviceid is marked invalid.
974 */
cbe82603
BH
975int
976_pnfs_return_layout(struct inode *ino)
977{
978 struct pnfs_layout_hdr *lo = NULL;
979 struct nfs_inode *nfsi = NFS_I(ino);
980 LIST_HEAD(tmp_list);
cbe82603 981 nfs4_stateid stateid;
293b3b06 982 int status = 0, empty;
cbe82603 983
366d5052 984 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
cbe82603
BH
985
986 spin_lock(&ino->i_lock);
987 lo = nfsi->layout;
e5929f3c 988 if (!lo) {
cbe82603 989 spin_unlock(&ino->i_lock);
293b3b06
AA
990 dprintk("NFS: %s no layout to return\n", __func__);
991 goto out;
cbe82603
BH
992 }
993 stateid = nfsi->layout->plh_stateid;
994 /* Reference matched in nfs4_layoutreturn_release */
70c3bd2b 995 pnfs_get_layout_hdr(lo);
293b3b06 996 empty = list_empty(&lo->plh_segs);
24956804 997 pnfs_clear_layoutcommit(ino, &tmp_list);
49a85061 998 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
c88953d8
CH
999
1000 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
1001 struct pnfs_layout_range range = {
1002 .iomode = IOMODE_ANY,
1003 .offset = 0,
1004 .length = NFS4_MAX_UINT64,
1005 };
1006 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1007 }
1008
293b3b06
AA
1009 /* Don't send a LAYOUTRETURN if list was initially empty */
1010 if (empty) {
1011 spin_unlock(&ino->i_lock);
70c3bd2b 1012 pnfs_put_layout_hdr(lo);
293b3b06
AA
1013 dprintk("NFS: %s no layout segments to return\n", __func__);
1014 goto out;
1015 }
47abadef
CH
1016
1017 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
ea0ded74 1018 lo->plh_block_lgets++;
cbe82603
BH
1019 spin_unlock(&ino->i_lock);
1020 pnfs_free_lseg_list(&tmp_list);
1021
6c16605d 1022 status = pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, true);
cbe82603
BH
1023out:
1024 dprintk("<-- %s status: %d\n", __func__, status);
1025 return status;
1026}
0a57cdac 1027EXPORT_SYMBOL_GPL(_pnfs_return_layout);
cbe82603 1028
24028672
TM
1029int
1030pnfs_commit_and_return_layout(struct inode *inode)
1031{
1032 struct pnfs_layout_hdr *lo;
1033 int ret;
1034
1035 spin_lock(&inode->i_lock);
1036 lo = NFS_I(inode)->layout;
1037 if (lo == NULL) {
1038 spin_unlock(&inode->i_lock);
1039 return 0;
1040 }
1041 pnfs_get_layout_hdr(lo);
1042 /* Block new layoutgets and read/write to ds */
1043 lo->plh_block_lgets++;
1044 spin_unlock(&inode->i_lock);
1045 filemap_fdatawait(inode->i_mapping);
1046 ret = pnfs_layoutcommit_inode(inode, true);
1047 if (ret == 0)
1048 ret = _pnfs_return_layout(inode);
1049 spin_lock(&inode->i_lock);
1050 lo->plh_block_lgets--;
1051 spin_unlock(&inode->i_lock);
1052 pnfs_put_layout_hdr(lo);
1053 return ret;
1054}
1055
f7e8917a
FI
1056bool pnfs_roc(struct inode *ino)
1057{
1058 struct pnfs_layout_hdr *lo;
1059 struct pnfs_layout_segment *lseg, *tmp;
193e3aa2 1060 nfs4_stateid stateid;
f7e8917a 1061 LIST_HEAD(tmp_list);
193e3aa2 1062 bool found = false, layoutreturn = false;
f7e8917a
FI
1063
1064 spin_lock(&ino->i_lock);
1065 lo = NFS_I(ino)->layout;
1066 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
1067 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
1068 goto out_nolayout;
1069 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
1070 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
1071 mark_lseg_invalid(lseg, &tmp_list);
1072 found = true;
1073 }
1074 if (!found)
1075 goto out_nolayout;
1076 lo->plh_block_lgets++;
70c3bd2b 1077 pnfs_get_layout_hdr(lo); /* matched in pnfs_roc_release */
f7e8917a
FI
1078 spin_unlock(&ino->i_lock);
1079 pnfs_free_lseg_list(&tmp_list);
1080 return true;
1081
1082out_nolayout:
193e3aa2
PT
1083 if (lo) {
1084 stateid = lo->plh_stateid;
1085 layoutreturn =
1086 test_and_clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
1087 &lo->plh_flags);
1088 if (layoutreturn) {
1089 lo->plh_block_lgets++;
1090 pnfs_get_layout_hdr(lo);
1091 }
1092 }
f7e8917a 1093 spin_unlock(&ino->i_lock);
193e3aa2 1094 if (layoutreturn)
27b6f539 1095 pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, true);
f7e8917a
FI
1096 return false;
1097}
1098
1099void pnfs_roc_release(struct inode *ino)
1100{
1101 struct pnfs_layout_hdr *lo;
1102
1103 spin_lock(&ino->i_lock);
1104 lo = NFS_I(ino)->layout;
1105 lo->plh_block_lgets--;
6622c3ea
TM
1106 if (atomic_dec_and_test(&lo->plh_refcount)) {
1107 pnfs_detach_layout_hdr(lo);
1108 spin_unlock(&ino->i_lock);
1109 pnfs_free_layout_hdr(lo);
1110 } else
1111 spin_unlock(&ino->i_lock);
f7e8917a
FI
1112}
1113
1114void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
1115{
1116 struct pnfs_layout_hdr *lo;
1117
1118 spin_lock(&ino->i_lock);
1119 lo = NFS_I(ino)->layout;
0f35ad6f 1120 if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
f7e8917a
FI
1121 lo->plh_barrier = barrier;
1122 spin_unlock(&ino->i_lock);
1123}
1124
7fdab069 1125bool pnfs_roc_drain(struct inode *ino, u32 *barrier, struct rpc_task *task)
f7e8917a
FI
1126{
1127 struct nfs_inode *nfsi = NFS_I(ino);
7fdab069 1128 struct pnfs_layout_hdr *lo;
f7e8917a 1129 struct pnfs_layout_segment *lseg;
193e3aa2 1130 nfs4_stateid stateid;
7fdab069 1131 u32 current_seqid;
193e3aa2 1132 bool found = false, layoutreturn = false;
f7e8917a
FI
1133
1134 spin_lock(&ino->i_lock);
1135 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
1136 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
7fdab069 1137 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
f7e8917a 1138 found = true;
7fdab069 1139 goto out;
f7e8917a 1140 }
7fdab069
TM
1141 lo = nfsi->layout;
1142 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
f7e8917a 1143
7fdab069
TM
1144 /* Since close does not return a layout stateid for use as
1145 * a barrier, we choose the worst-case barrier.
1146 */
1147 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
1148out:
193e3aa2
PT
1149 if (!found) {
1150 stateid = lo->plh_stateid;
1151 layoutreturn =
1152 test_and_clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
1153 &lo->plh_flags);
1154 if (layoutreturn) {
1155 lo->plh_block_lgets++;
1156 pnfs_get_layout_hdr(lo);
1157 }
1158 }
f7e8917a 1159 spin_unlock(&ino->i_lock);
193e3aa2
PT
1160 if (layoutreturn) {
1161 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
27b6f539 1162 pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, false);
193e3aa2 1163 }
f7e8917a
FI
1164 return found;
1165}
1166
b1f69b75
AA
1167/*
1168 * Compare two layout segments for sorting into layout cache.
1169 * We want to preferentially return RW over RO layouts, so ensure those
1170 * are seen first.
1171 */
1172static s64
7dc0ac70 1173pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
3cb2df17 1174 const struct pnfs_layout_range *l2)
b1f69b75 1175{
fb3296eb
BH
1176 s64 d;
1177
1178 /* high offset > low offset */
1179 d = l1->offset - l2->offset;
1180 if (d)
1181 return d;
1182
1183 /* short length > long length */
1184 d = l2->length - l1->length;
1185 if (d)
1186 return d;
1187
b1f69b75 1188 /* read > read/write */
fb3296eb 1189 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
b1f69b75
AA
1190}
1191
974cec8c 1192static void
57036a37 1193pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
974cec8c
AA
1194 struct pnfs_layout_segment *lseg)
1195{
b1f69b75 1196 struct pnfs_layout_segment *lp;
b1f69b75 1197
974cec8c
AA
1198 dprintk("%s:Begin\n", __func__);
1199
b7edfaa1 1200 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
7dc0ac70 1201 if (pnfs_lseg_range_cmp(&lseg->pls_range, &lp->pls_range) > 0)
b1f69b75 1202 continue;
566052c5 1203 list_add_tail(&lseg->pls_list, &lp->pls_list);
b1f69b75
AA
1204 dprintk("%s: inserted lseg %p "
1205 "iomode %d offset %llu length %llu before "
1206 "lp %p iomode %d offset %llu length %llu\n",
566052c5
FI
1207 __func__, lseg, lseg->pls_range.iomode,
1208 lseg->pls_range.offset, lseg->pls_range.length,
1209 lp, lp->pls_range.iomode, lp->pls_range.offset,
1210 lp->pls_range.length);
fb3296eb 1211 goto out;
974cec8c 1212 }
fb3296eb
BH
1213 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1214 dprintk("%s: inserted lseg %p "
1215 "iomode %d offset %llu length %llu at tail\n",
1216 __func__, lseg, lseg->pls_range.iomode,
1217 lseg->pls_range.offset, lseg->pls_range.length);
1218out:
70c3bd2b 1219 pnfs_get_layout_hdr(lo);
974cec8c
AA
1220
1221 dprintk("%s:Return\n", __func__);
e5e94017
BH
1222}
1223
1224static struct pnfs_layout_hdr *
9fa40758
PT
1225alloc_init_layout_hdr(struct inode *ino,
1226 struct nfs_open_context *ctx,
1227 gfp_t gfp_flags)
e5e94017
BH
1228{
1229 struct pnfs_layout_hdr *lo;
1230
636fb9c8 1231 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
e5e94017
BH
1232 if (!lo)
1233 return NULL;
cc6e5340 1234 atomic_set(&lo->plh_refcount, 1);
b7edfaa1
FI
1235 INIT_LIST_HEAD(&lo->plh_layouts);
1236 INIT_LIST_HEAD(&lo->plh_segs);
fd9a8d71 1237 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
b7edfaa1 1238 lo->plh_inode = ino;
5cc2216d 1239 lo->plh_lc_cred = get_rpccred(ctx->cred);
e5e94017
BH
1240 return lo;
1241}
1242
1243static struct pnfs_layout_hdr *
9fa40758
PT
1244pnfs_find_alloc_layout(struct inode *ino,
1245 struct nfs_open_context *ctx,
1246 gfp_t gfp_flags)
e5e94017
BH
1247{
1248 struct nfs_inode *nfsi = NFS_I(ino);
1249 struct pnfs_layout_hdr *new = NULL;
1250
1251 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1252
251ec410
TM
1253 if (nfsi->layout != NULL)
1254 goto out_existing;
e5e94017 1255 spin_unlock(&ino->i_lock);
9fa40758 1256 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
e5e94017
BH
1257 spin_lock(&ino->i_lock);
1258
251ec410 1259 if (likely(nfsi->layout == NULL)) { /* Won the race? */
e5e94017 1260 nfsi->layout = new;
251ec410 1261 return new;
7175fe90
YN
1262 } else if (new != NULL)
1263 pnfs_free_layout_hdr(new);
251ec410
TM
1264out_existing:
1265 pnfs_get_layout_hdr(nfsi->layout);
e5e94017
BH
1266 return nfsi->layout;
1267}
1268
b1f69b75
AA
1269/*
1270 * iomode matching rules:
1271 * iomode lseg match
1272 * ----- ----- -----
1273 * ANY READ true
1274 * ANY RW true
1275 * RW READ false
1276 * RW RW true
1277 * READ READ true
1278 * READ RW true
1279 */
3cb2df17 1280static bool
7dc0ac70 1281pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
3cb2df17 1282 const struct pnfs_layout_range *range)
b1f69b75 1283{
fb3296eb
BH
1284 struct pnfs_layout_range range1;
1285
1286 if ((range->iomode == IOMODE_RW &&
1287 ls_range->iomode != IOMODE_RW) ||
7dc0ac70 1288 !pnfs_lseg_range_intersecting(ls_range, range))
fb3296eb
BH
1289 return 0;
1290
1291 /* range1 covers only the first byte in the range */
1292 range1 = *range;
1293 range1.length = 1;
7dc0ac70 1294 return pnfs_lseg_range_contained(ls_range, &range1);
b1f69b75
AA
1295}
1296
1297/*
1298 * lookup range in layout
1299 */
e5e94017 1300static struct pnfs_layout_segment *
fb3296eb
BH
1301pnfs_find_lseg(struct pnfs_layout_hdr *lo,
1302 struct pnfs_layout_range *range)
e5e94017 1303{
b1f69b75
AA
1304 struct pnfs_layout_segment *lseg, *ret = NULL;
1305
1306 dprintk("%s:Begin\n", __func__);
1307
b7edfaa1 1308 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
4541d16c 1309 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
ce6ab4f2 1310 !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
7dc0ac70 1311 pnfs_lseg_range_match(&lseg->pls_range, range)) {
9369a431 1312 ret = pnfs_get_lseg(lseg);
b1f69b75
AA
1313 break;
1314 }
d771e3a4 1315 if (lseg->pls_range.offset > range->offset)
b1f69b75
AA
1316 break;
1317 }
1318
1319 dprintk("%s:Return lseg %p ref %d\n",
4541d16c 1320 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
b1f69b75 1321 return ret;
e5e94017
BH
1322}
1323
d23d61c8
AA
1324/*
1325 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1326 * to the MDS or over pNFS
1327 *
1328 * The nfs_inode read_io and write_io fields are cumulative counters reset
1329 * when there are no layout segments. Note that in pnfs_update_layout iomode
1330 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1331 * WRITE request.
1332 *
1333 * A return of true means use MDS I/O.
1334 *
1335 * From rfc 5661:
1336 * If a file's size is smaller than the file size threshold, data accesses
1337 * SHOULD be sent to the metadata server. If an I/O request has a length that
1338 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1339 * server. If both file size and I/O size are provided, the client SHOULD
1340 * reach or exceed both thresholds before sending its read or write
1341 * requests to the data server.
1342 */
1343static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1344 struct inode *ino, int iomode)
1345{
1346 struct nfs4_threshold *t = ctx->mdsthreshold;
1347 struct nfs_inode *nfsi = NFS_I(ino);
1348 loff_t fsize = i_size_read(ino);
1349 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1350
1351 if (t == NULL)
1352 return ret;
1353
1354 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1355 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1356
1357 switch (iomode) {
1358 case IOMODE_READ:
1359 if (t->bm & THRESHOLD_RD) {
1360 dprintk("%s fsize %llu\n", __func__, fsize);
1361 size_set = true;
1362 if (fsize < t->rd_sz)
1363 size = true;
1364 }
1365 if (t->bm & THRESHOLD_RD_IO) {
1366 dprintk("%s nfsi->read_io %llu\n", __func__,
1367 nfsi->read_io);
1368 io_set = true;
1369 if (nfsi->read_io < t->rd_io_sz)
1370 io = true;
1371 }
1372 break;
1373 case IOMODE_RW:
1374 if (t->bm & THRESHOLD_WR) {
1375 dprintk("%s fsize %llu\n", __func__, fsize);
1376 size_set = true;
1377 if (fsize < t->wr_sz)
1378 size = true;
1379 }
1380 if (t->bm & THRESHOLD_WR_IO) {
1381 dprintk("%s nfsi->write_io %llu\n", __func__,
1382 nfsi->write_io);
1383 io_set = true;
1384 if (nfsi->write_io < t->wr_io_sz)
1385 io = true;
1386 }
1387 break;
1388 }
1389 if (size_set && io_set) {
1390 if (size && io)
1391 ret = true;
1392 } else if (size || io)
1393 ret = true;
1394
1395 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1396 return ret;
1397}
1398
e5e94017
BH
1399/*
1400 * Layout segment is retreived from the server if not cached.
1401 * The appropriate layout segment is referenced and returned to the caller.
1402 */
7c24d948 1403struct pnfs_layout_segment *
e5e94017
BH
1404pnfs_update_layout(struct inode *ino,
1405 struct nfs_open_context *ctx,
fb3296eb
BH
1406 loff_t pos,
1407 u64 count,
a75b9df9
TM
1408 enum pnfs_iomode iomode,
1409 gfp_t gfp_flags)
e5e94017 1410{
fb3296eb
BH
1411 struct pnfs_layout_range arg = {
1412 .iomode = iomode,
1413 .offset = pos,
1414 .length = count,
1415 };
707ed5fd 1416 unsigned pg_offset;
6382a441
WAA
1417 struct nfs_server *server = NFS_SERVER(ino);
1418 struct nfs_client *clp = server->nfs_client;
e5e94017
BH
1419 struct pnfs_layout_hdr *lo;
1420 struct pnfs_layout_segment *lseg = NULL;
9bf87482 1421 bool first;
e5e94017
BH
1422
1423 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
f86bbcf8 1424 goto out;
d23d61c8
AA
1425
1426 if (pnfs_within_mdsthreshold(ctx, ino, iomode))
f86bbcf8 1427 goto out;
d23d61c8 1428
9bf87482
PT
1429lookup_again:
1430 first = false;
e5e94017 1431 spin_lock(&ino->i_lock);
9fa40758 1432 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
830ffb56
TM
1433 if (lo == NULL) {
1434 spin_unlock(&ino->i_lock);
1435 goto out;
1436 }
e5e94017 1437
43f1b3da 1438 /* Do we even need to bother with this? */
a59c30ac 1439 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
43f1b3da 1440 dprintk("%s matches recall, use MDS\n", __func__);
e5e94017
BH
1441 goto out_unlock;
1442 }
1443
1444 /* if LAYOUTGET already failed once we don't try again */
b9e028fd 1445 if (pnfs_layout_io_test_failed(lo, iomode))
e5e94017
BH
1446 goto out_unlock;
1447
9bf87482
PT
1448 first = list_empty(&lo->plh_segs);
1449 if (first) {
1450 /* The first layoutget for the file. Need to serialize per
1451 * RFC 5661 Errata 3208.
1452 */
1453 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1454 &lo->plh_flags)) {
1455 spin_unlock(&ino->i_lock);
1456 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
1457 TASK_UNINTERRUPTIBLE);
1458 pnfs_put_layout_hdr(lo);
1459 goto lookup_again;
1460 }
1461 } else {
1462 /* Check to see if the layout for the given range
1463 * already exists
1464 */
1465 lseg = pnfs_find_lseg(lo, &arg);
1466 if (lseg)
1467 goto out_unlock;
1468 }
568e8c49 1469
ce6ab4f2 1470 if (pnfs_layoutgets_blocked(lo, &arg, 0))
cf7d63f1
FI
1471 goto out_unlock;
1472 atomic_inc(&lo->plh_outstanding);
f49f9baa 1473 spin_unlock(&ino->i_lock);
30005121 1474
abb9a007 1475 if (list_empty(&lo->plh_layouts)) {
2130ff66
FI
1476 /* The lo must be on the clp list if there is any
1477 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1478 */
1479 spin_lock(&clp->cl_lock);
abb9a007
PT
1480 if (list_empty(&lo->plh_layouts))
1481 list_add_tail(&lo->plh_layouts, &server->layouts);
2130ff66
FI
1482 spin_unlock(&clp->cl_lock);
1483 }
e5e94017 1484
707ed5fd
BH
1485 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
1486 if (pg_offset) {
1487 arg.offset -= pg_offset;
1488 arg.length += pg_offset;
1489 }
7c24d948
AA
1490 if (arg.length != NFS4_MAX_UINT64)
1491 arg.length = PAGE_CACHE_ALIGN(arg.length);
707ed5fd 1492
fb3296eb 1493 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
cf7d63f1 1494 atomic_dec(&lo->plh_outstanding);
830ffb56 1495out_put_layout_hdr:
9bf87482
PT
1496 if (first) {
1497 unsigned long *bitlock = &lo->plh_flags;
1498
1499 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1500 smp_mb__after_atomic();
1501 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1502 }
70c3bd2b 1503 pnfs_put_layout_hdr(lo);
e5e94017 1504out:
f86bbcf8
TM
1505 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1506 "(%s, offset: %llu, length: %llu)\n",
1507 __func__, ino->i_sb->s_id,
1508 (unsigned long long)NFS_FILEID(ino),
1509 lseg == NULL ? "not found" : "found",
1510 iomode==IOMODE_RW ? "read/write" : "read-only",
1511 (unsigned long long)pos,
1512 (unsigned long long)count);
e5e94017
BH
1513 return lseg;
1514out_unlock:
1515 spin_unlock(&ino->i_lock);
830ffb56 1516 goto out_put_layout_hdr;
e5e94017 1517}
7c24d948 1518EXPORT_SYMBOL_GPL(pnfs_update_layout);
b1f69b75 1519
a0b0a6e3 1520struct pnfs_layout_segment *
b1f69b75
AA
1521pnfs_layout_process(struct nfs4_layoutget *lgp)
1522{
1523 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1524 struct nfs4_layoutget_res *res = &lgp->res;
1525 struct pnfs_layout_segment *lseg;
b7edfaa1 1526 struct inode *ino = lo->plh_inode;
78096cca 1527 LIST_HEAD(free_me);
b1f69b75
AA
1528 int status = 0;
1529
1530 /* Inject layout blob into I/O device driver */
a75b9df9 1531 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
b1f69b75
AA
1532 if (!lseg || IS_ERR(lseg)) {
1533 if (!lseg)
1534 status = -ENOMEM;
1535 else
1536 status = PTR_ERR(lseg);
1537 dprintk("%s: Could not allocate layout: error %d\n",
1538 __func__, status);
1539 goto out;
1540 }
1541
1013df61
CH
1542 init_lseg(lo, lseg);
1543 lseg->pls_range = res->range;
1544
b1f69b75 1545 spin_lock(&ino->i_lock);
a59c30ac 1546 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
43f1b3da
FI
1547 dprintk("%s forget reply due to recall\n", __func__);
1548 goto out_forget_reply;
1549 }
1550
ce6ab4f2 1551 if (pnfs_layoutgets_blocked(lo, &lgp->args.range, 1)) {
43f1b3da
FI
1552 dprintk("%s forget reply due to state\n", __func__);
1553 goto out_forget_reply;
1554 }
038d6493 1555
362f7474
CH
1556 if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
1557 /* existing state ID, make sure the sequence number matches. */
1558 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1559 dprintk("%s forget reply due to sequence\n", __func__);
1560 goto out_forget_reply;
1561 }
1562 pnfs_set_layout_stateid(lo, &res->stateid, false);
1563 } else {
1564 /*
1565 * We got an entirely new state ID. Mark all segments for the
1566 * inode invalid, and don't bother validating the stateid
1567 * sequence number.
1568 */
1569 pnfs_mark_matching_lsegs_invalid(lo, &free_me, NULL);
1570
1571 nfs4_stateid_copy(&lo->plh_stateid, &res->stateid);
1572 lo->plh_barrier = be32_to_cpu(res->stateid.seqid);
1573 }
038d6493 1574
47abadef
CH
1575 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
1576
9369a431 1577 pnfs_get_lseg(lseg);
57036a37 1578 pnfs_layout_insert_lseg(lo, lseg);
b1f69b75 1579
f7e8917a
FI
1580 if (res->return_on_close) {
1581 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1582 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1583 }
1584
b1f69b75 1585 spin_unlock(&ino->i_lock);
78096cca 1586 pnfs_free_lseg_list(&free_me);
a0b0a6e3 1587 return lseg;
b1f69b75 1588out:
a0b0a6e3 1589 return ERR_PTR(status);
43f1b3da
FI
1590
1591out_forget_reply:
1592 spin_unlock(&ino->i_lock);
1593 lseg->pls_layout = lo;
1594 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1595 goto out;
b1f69b75
AA
1596}
1597
016256df
PT
1598static void
1599pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
1600 struct list_head *tmp_list,
1601 struct pnfs_layout_range *return_range)
1602{
1603 struct pnfs_layout_segment *lseg, *next;
1604
1605 dprintk("%s:Begin lo %p\n", __func__, lo);
1606
1607 if (list_empty(&lo->plh_segs))
1608 return;
1609
1610 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
1611 if (should_free_lseg(&lseg->pls_range, return_range)) {
1612 dprintk("%s: marking lseg %p iomode %d "
1613 "offset %llu length %llu\n", __func__,
1614 lseg, lseg->pls_range.iomode,
1615 lseg->pls_range.offset,
1616 lseg->pls_range.length);
1617 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
1618 mark_lseg_invalid(lseg, tmp_list);
1619 }
1620}
1621
1622void pnfs_error_mark_layout_for_return(struct inode *inode,
1623 struct pnfs_layout_segment *lseg)
1624{
1625 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
1626 int iomode = pnfs_iomode_to_fail_bit(lseg->pls_range.iomode);
1627 struct pnfs_layout_range range = {
1628 .iomode = lseg->pls_range.iomode,
1629 .offset = 0,
1630 .length = NFS4_MAX_UINT64,
1631 };
1632 LIST_HEAD(free_me);
1633
1634 spin_lock(&inode->i_lock);
1635 /* set failure bit so that pnfs path will be retried later */
1636 pnfs_layout_set_fail_bit(lo, iomode);
1637 set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
1638 if (lo->plh_return_iomode == 0)
1639 lo->plh_return_iomode = range.iomode;
1640 else if (lo->plh_return_iomode != range.iomode)
1641 lo->plh_return_iomode = IOMODE_ANY;
1642 /*
1643 * mark all matching lsegs so that we are sure to have no live
1644 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
1645 * for how it works.
1646 */
1647 pnfs_mark_matching_lsegs_return(lo, &free_me, &range);
1648 spin_unlock(&inode->i_lock);
1649 pnfs_free_lseg_list(&free_me);
1650}
1651EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
1652
d8007d4d
TM
1653void
1654pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1655{
1fd937bd
PT
1656 u64 rd_size = req->wb_bytes;
1657
bc5a89b3 1658 WARN_ON_ONCE(pgio->pg_lseg != NULL);
d8007d4d 1659
1fd937bd
PT
1660 if (pgio->pg_dreq == NULL)
1661 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
1662 else
1663 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
1664
d8007d4d
TM
1665 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1666 req->wb_context,
1667 req_offset(req),
1fd937bd 1668 rd_size,
d8007d4d
TM
1669 IOMODE_READ,
1670 GFP_KERNEL);
e885de1a
TM
1671 /* If no lseg, fall back to read through mds */
1672 if (pgio->pg_lseg == NULL)
1f945357 1673 nfs_pageio_reset_read_mds(pgio);
e885de1a 1674
d8007d4d
TM
1675}
1676EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1677
1678void
6296556f
PT
1679pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
1680 struct nfs_page *req, u64 wb_size)
d8007d4d 1681{
bc5a89b3 1682 WARN_ON_ONCE(pgio->pg_lseg != NULL);
d8007d4d
TM
1683
1684 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1685 req->wb_context,
1686 req_offset(req),
6296556f 1687 wb_size,
d8007d4d
TM
1688 IOMODE_RW,
1689 GFP_NOFS);
e885de1a
TM
1690 /* If no lseg, fall back to write through mds */
1691 if (pgio->pg_lseg == NULL)
1f945357 1692 nfs_pageio_reset_write_mds(pgio);
d8007d4d
TM
1693}
1694EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1695
180bb5ec
WAA
1696void
1697pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
1698{
1699 if (desc->pg_lseg) {
1700 pnfs_put_lseg(desc->pg_lseg);
1701 desc->pg_lseg = NULL;
1702 }
1703}
1704EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
1705
b4fdac1a
WAA
1706/*
1707 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
1708 * of bytes (maximum @req->wb_bytes) that can be coalesced.
1709 */
1710size_t
a7d42ddb
WAA
1711pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
1712 struct nfs_page *prev, struct nfs_page *req)
94ad1c80 1713{
0f9c429e 1714 unsigned int size;
c5e20cb7 1715 u64 seg_end, req_start, seg_left;
0f9c429e
WAA
1716
1717 size = nfs_generic_pg_test(pgio, prev, req);
0f9c429e
WAA
1718 if (!size)
1719 return 0;
94ad1c80 1720
19982ba8 1721 /*
c5e20cb7
WAA
1722 * 'size' contains the number of bytes left in the current page (up
1723 * to the original size asked for in @req->wb_bytes).
1724 *
1725 * Calculate how many bytes are left in the layout segment
1726 * and if there are less bytes than 'size', return that instead.
19982ba8
TM
1727 *
1728 * Please also note that 'end_offset' is actually the offset of the
1729 * first byte that lies outside the pnfs_layout_range. FIXME?
1730 *
1731 */
19b54848 1732 if (pgio->pg_lseg) {
c5e20cb7
WAA
1733 seg_end = end_offset(pgio->pg_lseg->pls_range.offset,
1734 pgio->pg_lseg->pls_range.length);
1735 req_start = req_offset(req);
1736 WARN_ON_ONCE(req_start > seg_end);
1737 /* start of request is past the last byte of this segment */
1738 if (req_start >= seg_end)
19b54848 1739 return 0;
c5e20cb7
WAA
1740
1741 /* adjust 'size' iff there are fewer bytes left in the
1742 * segment than what nfs_generic_pg_test returned */
1743 seg_left = seg_end - req_start;
1744 if (seg_left < size)
1745 size = (unsigned int)seg_left;
19b54848 1746 }
0f9c429e 1747
19b54848 1748 return size;
94ad1c80 1749}
89a58e32 1750EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
94ad1c80 1751
53113ad3 1752int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
e2fecb21
TM
1753{
1754 struct nfs_pageio_descriptor pgio;
e2fecb21
TM
1755
1756 /* Resend all requests through the MDS */
53113ad3
WAA
1757 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
1758 hdr->completion_ops);
1759 return nfs_pageio_resend(&pgio, hdr);
e2fecb21 1760}
e7dd79af 1761EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
e2fecb21 1762
d45f60c6 1763static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
1acbbb4e 1764{
cd841605
FI
1765
1766 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
1767 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
1acbbb4e 1768 PNFS_LAYOUTRET_ON_ERROR) {
cd841605 1769 pnfs_return_layout(hdr->inode);
1acbbb4e 1770 }
6c75dc0d 1771 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
53113ad3 1772 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
1acbbb4e
FI
1773}
1774
d20581aa
BH
1775/*
1776 * Called by non rpc-based layout drivers
1777 */
d45f60c6 1778void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
44b83799 1779{
d45f60c6 1780 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
cd841605 1781 if (!hdr->pnfs_error) {
d45f60c6
WAA
1782 pnfs_set_layoutcommit(hdr);
1783 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
1acbbb4e 1784 } else
d45f60c6
WAA
1785 pnfs_ld_handle_write_error(hdr);
1786 hdr->mds_ops->rpc_release(hdr);
44b83799 1787}
d20581aa 1788EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
44b83799 1789
dce81290
TM
1790static void
1791pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
d45f60c6 1792 struct nfs_pgio_header *hdr)
dce81290 1793{
48d635f1 1794 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
a7d42ddb 1795
6c75dc0d 1796 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
a7d42ddb 1797 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
6c75dc0d 1798 nfs_pageio_reset_write_mds(desc);
a7d42ddb 1799 mirror->pg_recoalesce = 1;
6c75dc0d 1800 }
d45f60c6 1801 nfs_pgio_data_destroy(hdr);
dce81290
TM
1802}
1803
1804static enum pnfs_try_status
d45f60c6 1805pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
dce81290
TM
1806 const struct rpc_call_ops *call_ops,
1807 struct pnfs_layout_segment *lseg,
1808 int how)
0382b744 1809{
cd841605 1810 struct inode *inode = hdr->inode;
0382b744
AA
1811 enum pnfs_try_status trypnfs;
1812 struct nfs_server *nfss = NFS_SERVER(inode);
1813
cd841605 1814 hdr->mds_ops = call_ops;
0382b744
AA
1815
1816 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
d45f60c6
WAA
1817 inode->i_ino, hdr->args.count, hdr->args.offset, how);
1818 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
6c75dc0d 1819 if (trypnfs != PNFS_NOT_ATTEMPTED)
0382b744 1820 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
0382b744
AA
1821 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1822 return trypnfs;
1823}
1824
dce81290 1825static void
7f714720
WAA
1826pnfs_do_write(struct nfs_pageio_descriptor *desc,
1827 struct nfs_pgio_header *hdr, int how)
dce81290 1828{
dce81290
TM
1829 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1830 struct pnfs_layout_segment *lseg = desc->pg_lseg;
7f714720 1831 enum pnfs_try_status trypnfs;
dce81290 1832
d45f60c6 1833 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
7f714720 1834 if (trypnfs == PNFS_NOT_ATTEMPTED)
d45f60c6 1835 pnfs_write_through_mds(desc, hdr);
dce81290
TM
1836}
1837
6c75dc0d
FI
1838static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
1839{
9369a431 1840 pnfs_put_lseg(hdr->lseg);
1e7f3a48 1841 nfs_pgio_header_free(hdr);
6c75dc0d 1842}
89d77c8f 1843EXPORT_SYMBOL_GPL(pnfs_writehdr_free);
6c75dc0d 1844
dce81290
TM
1845int
1846pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1847{
48d635f1 1848 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
a7d42ddb 1849
6c75dc0d 1850 struct nfs_pgio_header *hdr;
dce81290
TM
1851 int ret;
1852
1e7f3a48
WAA
1853 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
1854 if (!hdr) {
a7d42ddb 1855 desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
6c75dc0d 1856 return -ENOMEM;
dce81290 1857 }
6c75dc0d 1858 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
180bb5ec 1859
9369a431 1860 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
ef2c488c 1861 ret = nfs_generic_pgio(desc, hdr);
180bb5ec 1862 if (!ret)
7f714720 1863 pnfs_do_write(desc, hdr, desc->pg_ioflags);
a7d42ddb 1864
6c75dc0d 1865 return ret;
dce81290
TM
1866}
1867EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
1868
53113ad3 1869int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
62e4a769
TM
1870{
1871 struct nfs_pageio_descriptor pgio;
1872
1acbbb4e 1873 /* Resend all requests through the MDS */
53113ad3
WAA
1874 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
1875 return nfs_pageio_resend(&pgio, hdr);
1acbbb4e 1876}
e7dd79af 1877EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
1acbbb4e 1878
d45f60c6 1879static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
1acbbb4e 1880{
cd841605
FI
1881 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
1882 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
1acbbb4e 1883 PNFS_LAYOUTRET_ON_ERROR) {
cd841605 1884 pnfs_return_layout(hdr->inode);
1acbbb4e 1885 }
4db6e0b7 1886 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
53113ad3 1887 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
62e4a769
TM
1888}
1889
d20581aa
BH
1890/*
1891 * Called by non rpc-based layout drivers
1892 */
d45f60c6 1893void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
d20581aa 1894{
d45f60c6 1895 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
cd841605 1896 if (likely(!hdr->pnfs_error)) {
d45f60c6
WAA
1897 __nfs4_read_done_cb(hdr);
1898 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
62e4a769 1899 } else
d45f60c6
WAA
1900 pnfs_ld_handle_read_error(hdr);
1901 hdr->mds_ops->rpc_release(hdr);
d20581aa
BH
1902}
1903EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1904
493292dd
TM
1905static void
1906pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
d45f60c6 1907 struct nfs_pgio_header *hdr)
493292dd 1908{
48d635f1 1909 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
a7d42ddb 1910
4db6e0b7 1911 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
a7d42ddb 1912 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
4db6e0b7 1913 nfs_pageio_reset_read_mds(desc);
a7d42ddb 1914 mirror->pg_recoalesce = 1;
4db6e0b7 1915 }
d45f60c6 1916 nfs_pgio_data_destroy(hdr);
493292dd
TM
1917}
1918
64419a9b
AA
1919/*
1920 * Call the appropriate parallel I/O subsystem read function.
1921 */
493292dd 1922static enum pnfs_try_status
d45f60c6 1923pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
493292dd
TM
1924 const struct rpc_call_ops *call_ops,
1925 struct pnfs_layout_segment *lseg)
64419a9b 1926{
cd841605 1927 struct inode *inode = hdr->inode;
64419a9b
AA
1928 struct nfs_server *nfss = NFS_SERVER(inode);
1929 enum pnfs_try_status trypnfs;
1930
cd841605 1931 hdr->mds_ops = call_ops;
64419a9b
AA
1932
1933 dprintk("%s: Reading ino:%lu %u@%llu\n",
d45f60c6 1934 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
64419a9b 1935
d45f60c6 1936 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
4db6e0b7 1937 if (trypnfs != PNFS_NOT_ATTEMPTED)
64419a9b 1938 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
64419a9b
AA
1939 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1940 return trypnfs;
1941}
863a3c6c 1942
ceb11e13
PT
1943/* Resend all requests through pnfs. */
1944int pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
1945{
1946 struct nfs_pageio_descriptor pgio;
1947
1948 nfs_pageio_init_read(&pgio, hdr->inode, false, hdr->completion_ops);
1949 return nfs_pageio_resend(&pgio, hdr);
1950}
1951EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
1952
493292dd 1953static void
7f714720 1954pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
493292dd 1955{
493292dd
TM
1956 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1957 struct pnfs_layout_segment *lseg = desc->pg_lseg;
7f714720 1958 enum pnfs_try_status trypnfs;
ceb11e13 1959 int err = 0;
493292dd 1960
d45f60c6 1961 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
ceb11e13
PT
1962 if (trypnfs == PNFS_TRY_AGAIN)
1963 err = pnfs_read_resend_pnfs(hdr);
1964 if (trypnfs == PNFS_NOT_ATTEMPTED || err)
d45f60c6 1965 pnfs_read_through_mds(desc, hdr);
493292dd
TM
1966}
1967
4db6e0b7
FI
1968static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
1969{
9369a431 1970 pnfs_put_lseg(hdr->lseg);
1e7f3a48 1971 nfs_pgio_header_free(hdr);
4db6e0b7 1972}
89d77c8f 1973EXPORT_SYMBOL_GPL(pnfs_readhdr_free);
4db6e0b7 1974
493292dd
TM
1975int
1976pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
1977{
48d635f1 1978 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
a7d42ddb 1979
4db6e0b7 1980 struct nfs_pgio_header *hdr;
493292dd
TM
1981 int ret;
1982
1e7f3a48
WAA
1983 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
1984 if (!hdr) {
a7d42ddb 1985 desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
180bb5ec 1986 return -ENOMEM;
493292dd 1987 }
4db6e0b7 1988 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
9369a431 1989 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
ef2c488c 1990 ret = nfs_generic_pgio(desc, hdr);
180bb5ec 1991 if (!ret)
7f714720 1992 pnfs_do_read(desc, hdr);
4db6e0b7 1993 return ret;
493292dd
TM
1994}
1995EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
1996
71244d9b
TM
1997static void pnfs_clear_layoutcommitting(struct inode *inode)
1998{
1999 unsigned long *bitlock = &NFS_I(inode)->flags;
2000
2001 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
4e857c58 2002 smp_mb__after_atomic();
71244d9b
TM
2003 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2004}
2005
863a3c6c 2006/*
a9bae566 2007 * There can be multiple RW segments.
863a3c6c 2008 */
a9bae566 2009static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
863a3c6c 2010{
a9bae566 2011 struct pnfs_layout_segment *lseg;
863a3c6c 2012
a9bae566
PT
2013 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2014 if (lseg->pls_range.iomode == IOMODE_RW &&
a073dbff 2015 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
a9bae566
PT
2016 list_add(&lseg->pls_lc_list, listp);
2017 }
863a3c6c
AA
2018}
2019
a073dbff
TM
2020static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2021{
2022 struct pnfs_layout_segment *lseg, *tmp;
a073dbff
TM
2023
2024 /* Matched by references in pnfs_set_layoutcommit */
2025 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2026 list_del_init(&lseg->pls_lc_list);
2027 pnfs_put_lseg(lseg);
2028 }
2029
71244d9b 2030 pnfs_clear_layoutcommitting(inode);
a073dbff
TM
2031}
2032
1b0ae068
PT
2033void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2034{
b9e028fd 2035 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
1b0ae068
PT
2036}
2037EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2038
863a3c6c 2039void
d45f60c6 2040pnfs_set_layoutcommit(struct nfs_pgio_header *hdr)
863a3c6c 2041{
cd841605
FI
2042 struct inode *inode = hdr->inode;
2043 struct nfs_inode *nfsi = NFS_I(inode);
d45f60c6 2044 loff_t end_pos = hdr->mds_offset + hdr->res.count;
79a48a1f 2045 bool mark_as_dirty = false;
863a3c6c 2046
cd841605 2047 spin_lock(&inode->i_lock);
863a3c6c 2048 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
79a48a1f 2049 mark_as_dirty = true;
863a3c6c 2050 dprintk("%s: Set layoutcommit for inode %lu ",
cd841605 2051 __func__, inode->i_ino);
863a3c6c 2052 }
cd841605 2053 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) {
a9bae566 2054 /* references matched in nfs4_layoutcommit_release */
9369a431 2055 pnfs_get_lseg(hdr->lseg);
a9bae566 2056 }
acff5880
PT
2057 if (end_pos > nfsi->layout->plh_lwb)
2058 nfsi->layout->plh_lwb = end_pos;
cd841605 2059 spin_unlock(&inode->i_lock);
acff5880 2060 dprintk("%s: lseg %p end_pos %llu\n",
cd841605 2061 __func__, hdr->lseg, nfsi->layout->plh_lwb);
79a48a1f
WAA
2062
2063 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2064 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2065 if (mark_as_dirty)
cd841605 2066 mark_inode_dirty_sync(inode);
863a3c6c
AA
2067}
2068EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2069
378520b8
PT
2070void pnfs_commit_set_layoutcommit(struct nfs_commit_data *data)
2071{
2072 struct inode *inode = data->inode;
2073 struct nfs_inode *nfsi = NFS_I(inode);
2074 bool mark_as_dirty = false;
2075
2076 spin_lock(&inode->i_lock);
2077 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
2078 mark_as_dirty = true;
2079 dprintk("%s: Set layoutcommit for inode %lu ",
2080 __func__, inode->i_ino);
2081 }
2082 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &data->lseg->pls_flags)) {
2083 /* references matched in nfs4_layoutcommit_release */
2084 pnfs_get_lseg(data->lseg);
2085 }
2086 if (data->lwb > nfsi->layout->plh_lwb)
2087 nfsi->layout->plh_lwb = data->lwb;
2088 spin_unlock(&inode->i_lock);
2089 dprintk("%s: lseg %p end_pos %llu\n",
2090 __func__, data->lseg, nfsi->layout->plh_lwb);
2091
2092 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2093 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2094 if (mark_as_dirty)
2095 mark_inode_dirty_sync(inode);
2096}
2097EXPORT_SYMBOL_GPL(pnfs_commit_set_layoutcommit);
2098
db29c089
AA
2099void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2100{
2101 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2102
2103 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2104 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
a073dbff 2105 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
db29c089
AA
2106}
2107
de4b15c7
AA
2108/*
2109 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2110 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2111 * data to disk to allow the server to recover the data if it crashes.
2112 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2113 * is off, and a COMMIT is sent to a data server, or
2114 * if WRITEs to a data server return NFS_DATA_SYNC.
2115 */
863a3c6c 2116int
ef311537 2117pnfs_layoutcommit_inode(struct inode *inode, bool sync)
863a3c6c 2118{
5f919c9f 2119 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
863a3c6c
AA
2120 struct nfs4_layoutcommit_data *data;
2121 struct nfs_inode *nfsi = NFS_I(inode);
863a3c6c 2122 loff_t end_pos;
71244d9b 2123 int status;
863a3c6c 2124
71244d9b 2125 if (!pnfs_layoutcommit_outstanding(inode))
de4b15c7
AA
2126 return 0;
2127
71244d9b 2128 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
92407e75 2129
71244d9b 2130 status = -EAGAIN;
92407e75 2131 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
71244d9b
TM
2132 if (!sync)
2133 goto out;
74316201 2134 status = wait_on_bit_lock_action(&nfsi->flags,
71244d9b
TM
2135 NFS_INO_LAYOUTCOMMITTING,
2136 nfs_wait_bit_killable,
2137 TASK_KILLABLE);
92407e75 2138 if (status)
71244d9b 2139 goto out;
92407e75
PT
2140 }
2141
71244d9b
TM
2142 status = -ENOMEM;
2143 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2144 data = kzalloc(sizeof(*data), GFP_NOFS);
2145 if (!data)
2146 goto clear_layoutcommitting;
2147
2148 status = 0;
de4b15c7 2149 spin_lock(&inode->i_lock);
71244d9b
TM
2150 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2151 goto out_unlock;
a9bae566 2152
71244d9b 2153 INIT_LIST_HEAD(&data->lseg_list);
a9bae566 2154 pnfs_list_write_lseg(inode, &data->lseg_list);
863a3c6c 2155
acff5880 2156 end_pos = nfsi->layout->plh_lwb;
acff5880 2157 nfsi->layout->plh_lwb = 0;
863a3c6c 2158
f597c537 2159 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
863a3c6c
AA
2160 spin_unlock(&inode->i_lock);
2161
2162 data->args.inode = inode;
9fa40758 2163 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
863a3c6c
AA
2164 nfs_fattr_init(&data->fattr);
2165 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2166 data->res.fattr = &data->fattr;
2167 data->args.lastbytewritten = end_pos - 1;
2168 data->res.server = NFS_SERVER(inode);
2169
5f919c9f
CH
2170 if (ld->prepare_layoutcommit) {
2171 status = ld->prepare_layoutcommit(&data->args);
2172 if (status) {
2173 spin_lock(&inode->i_lock);
2174 if (end_pos < nfsi->layout->plh_lwb)
2175 nfsi->layout->plh_lwb = end_pos;
2176 spin_unlock(&inode->i_lock);
2177 put_rpccred(data->cred);
2178 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2179 goto clear_layoutcommitting;
2180 }
2181 }
2182
2183
863a3c6c
AA
2184 status = nfs4_proc_layoutcommit(data, sync);
2185out:
92407e75
PT
2186 if (status)
2187 mark_inode_dirty_sync(inode);
863a3c6c
AA
2188 dprintk("<-- %s status %d\n", __func__, status);
2189 return status;
71244d9b
TM
2190out_unlock:
2191 spin_unlock(&inode->i_lock);
92407e75 2192 kfree(data);
71244d9b
TM
2193clear_layoutcommitting:
2194 pnfs_clear_layoutcommitting(inode);
92407e75 2195 goto out;
863a3c6c 2196}
72cff449 2197EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
82be417a
AA
2198
2199struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
2200{
2201 struct nfs4_threshold *thp;
2202
2203 thp = kzalloc(sizeof(*thp), GFP_NOFS);
2204 if (!thp) {
2205 dprintk("%s mdsthreshold allocation failed\n", __func__);
2206 return NULL;
2207 }
2208 return thp;
2209}
This page took 0.427904 seconds and 5 git commands to generate.