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