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