2 * pNFS functions to call and manage layout drivers.
4 * Copyright (c) 2002 [year of first publication]
5 * The Regents of the University of Michigan
8 * Dean Hildebrand <dhildebz@umich.edu>
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.
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
30 #include <linux/nfs_fs.h>
31 #include <linux/nfs_page.h>
32 #include <linux/module.h>
33 #include <linux/sort.h>
37 #include "nfs4trace.h"
38 #include "delegation.h"
41 #define NFSDBG_FACILITY NFSDBG_PNFS
42 #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
47 * protects pnfs_modules_tbl.
49 static DEFINE_SPINLOCK(pnfs_spinlock
);
52 * pnfs_modules_tbl holds all pnfs modules
54 static LIST_HEAD(pnfs_modules_tbl
);
56 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr
*lo
);
58 /* Return the registered pnfs layout driver module matching given id */
59 static struct pnfs_layoutdriver_type
*
60 find_pnfs_driver_locked(u32 id
)
62 struct pnfs_layoutdriver_type
*local
;
64 list_for_each_entry(local
, &pnfs_modules_tbl
, pnfs_tblid
)
69 dprintk("%s: Searching for id %u, found %p\n", __func__
, id
, local
);
73 static struct pnfs_layoutdriver_type
*
74 find_pnfs_driver(u32 id
)
76 struct pnfs_layoutdriver_type
*local
;
78 spin_lock(&pnfs_spinlock
);
79 local
= find_pnfs_driver_locked(id
);
80 if (local
!= NULL
&& !try_module_get(local
->owner
)) {
81 dprintk("%s: Could not grab reference on module\n", __func__
);
84 spin_unlock(&pnfs_spinlock
);
89 unset_pnfs_layoutdriver(struct nfs_server
*nfss
)
91 if (nfss
->pnfs_curr_ld
) {
92 if (nfss
->pnfs_curr_ld
->clear_layoutdriver
)
93 nfss
->pnfs_curr_ld
->clear_layoutdriver(nfss
);
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
);
97 module_put(nfss
->pnfs_curr_ld
->owner
);
99 nfss
->pnfs_curr_ld
= NULL
;
103 * When the server sends a list of layout types, we choose one in the order
104 * given in the list below.
106 * FIXME: should this list be configurable in some fashion? module param?
107 * mount option? something else?
109 static const u32 ld_prefs
[] = {
114 LAYOUT_NFSV4_1_FILES
,
119 ld_cmp(const void *e1
, const void *e2
)
121 u32 ld1
= *((u32
*)e1
);
122 u32 ld2
= *((u32
*)e2
);
125 for (i
= 0; ld_prefs
[i
] != 0; i
++) {
126 if (ld1
== ld_prefs
[i
])
129 if (ld2
== ld_prefs
[i
])
136 * Try to set the server's pnfs module to the pnfs layout type specified by id.
137 * Currently only one pNFS layout driver per filesystem is supported.
139 * @ids array of layout types supported by MDS.
142 set_pnfs_layoutdriver(struct nfs_server
*server
, const struct nfs_fh
*mntfh
,
143 struct nfs_fsinfo
*fsinfo
)
145 struct pnfs_layoutdriver_type
*ld_type
= NULL
;
149 if (fsinfo
->nlayouttypes
== 0)
151 if (!(server
->nfs_client
->cl_exchange_flags
&
152 (EXCHGID4_FLAG_USE_NON_PNFS
| EXCHGID4_FLAG_USE_PNFS_MDS
))) {
153 printk(KERN_ERR
"NFS: %s: cl_exchange_flags 0x%x\n",
154 __func__
, server
->nfs_client
->cl_exchange_flags
);
158 sort(fsinfo
->layouttype
, fsinfo
->nlayouttypes
,
159 sizeof(*fsinfo
->layouttype
), ld_cmp
, NULL
);
161 for (i
= 0; i
< fsinfo
->nlayouttypes
; i
++) {
162 id
= fsinfo
->layouttype
[i
];
163 ld_type
= find_pnfs_driver(id
);
165 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX
,
167 ld_type
= find_pnfs_driver(id
);
174 dprintk("%s: No pNFS module found!\n", __func__
);
178 server
->pnfs_curr_ld
= ld_type
;
179 if (ld_type
->set_layoutdriver
180 && ld_type
->set_layoutdriver(server
, mntfh
)) {
181 printk(KERN_ERR
"NFS: %s: Error initializing pNFS layout "
182 "driver %u.\n", __func__
, id
);
183 module_put(ld_type
->owner
);
186 /* Bump the MDS count */
187 atomic_inc(&server
->nfs_client
->cl_mds_count
);
189 dprintk("%s: pNFS module for %u set\n", __func__
, id
);
193 dprintk("%s: Using NFSv4 I/O\n", __func__
);
194 server
->pnfs_curr_ld
= NULL
;
198 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type
*ld_type
)
200 int status
= -EINVAL
;
201 struct pnfs_layoutdriver_type
*tmp
;
203 if (ld_type
->id
== 0) {
204 printk(KERN_ERR
"NFS: %s id 0 is reserved\n", __func__
);
207 if (!ld_type
->alloc_lseg
|| !ld_type
->free_lseg
) {
208 printk(KERN_ERR
"NFS: %s Layout driver must provide "
209 "alloc_lseg and free_lseg.\n", __func__
);
213 spin_lock(&pnfs_spinlock
);
214 tmp
= find_pnfs_driver_locked(ld_type
->id
);
216 list_add(&ld_type
->pnfs_tblid
, &pnfs_modules_tbl
);
218 dprintk("%s Registering id:%u name:%s\n", __func__
, ld_type
->id
,
221 printk(KERN_ERR
"NFS: %s Module with id %d already loaded!\n",
222 __func__
, ld_type
->id
);
224 spin_unlock(&pnfs_spinlock
);
228 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver
);
231 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type
*ld_type
)
233 dprintk("%s Deregistering id:%u\n", __func__
, ld_type
->id
);
234 spin_lock(&pnfs_spinlock
);
235 list_del(&ld_type
->pnfs_tblid
);
236 spin_unlock(&pnfs_spinlock
);
238 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver
);
241 * pNFS client layout cache
244 /* Need to hold i_lock if caller does not already hold reference */
246 pnfs_get_layout_hdr(struct pnfs_layout_hdr
*lo
)
248 atomic_inc(&lo
->plh_refcount
);
251 static struct pnfs_layout_hdr
*
252 pnfs_alloc_layout_hdr(struct inode
*ino
, gfp_t gfp_flags
)
254 struct pnfs_layoutdriver_type
*ld
= NFS_SERVER(ino
)->pnfs_curr_ld
;
255 return ld
->alloc_layout_hdr(ino
, gfp_flags
);
259 pnfs_free_layout_hdr(struct pnfs_layout_hdr
*lo
)
261 struct nfs_server
*server
= NFS_SERVER(lo
->plh_inode
);
262 struct pnfs_layoutdriver_type
*ld
= server
->pnfs_curr_ld
;
264 if (!list_empty(&lo
->plh_layouts
)) {
265 struct nfs_client
*clp
= server
->nfs_client
;
267 spin_lock(&clp
->cl_lock
);
268 list_del_init(&lo
->plh_layouts
);
269 spin_unlock(&clp
->cl_lock
);
271 put_rpccred(lo
->plh_lc_cred
);
272 return ld
->free_layout_hdr(lo
);
276 pnfs_detach_layout_hdr(struct pnfs_layout_hdr
*lo
)
278 struct nfs_inode
*nfsi
= NFS_I(lo
->plh_inode
);
279 dprintk("%s: freeing layout cache %p\n", __func__
, lo
);
281 /* Reset MDS Threshold I/O counters */
287 pnfs_put_layout_hdr(struct pnfs_layout_hdr
*lo
)
289 struct inode
*inode
= lo
->plh_inode
;
291 pnfs_layoutreturn_before_put_layout_hdr(lo
);
293 if (atomic_dec_and_lock(&lo
->plh_refcount
, &inode
->i_lock
)) {
294 if (!list_empty(&lo
->plh_segs
))
295 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
296 pnfs_detach_layout_hdr(lo
);
297 spin_unlock(&inode
->i_lock
);
298 pnfs_free_layout_hdr(lo
);
303 pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr
*lo
)
305 lo
->plh_return_iomode
= 0;
306 lo
->plh_return_seq
= 0;
307 clear_bit(NFS_LAYOUT_RETURN_REQUESTED
, &lo
->plh_flags
);
311 * Mark a pnfs_layout_hdr and all associated layout segments as invalid
313 * In order to continue using the pnfs_layout_hdr, a full recovery
315 * Note that caller must hold inode->i_lock.
318 pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr
*lo
,
319 struct list_head
*lseg_list
)
321 struct pnfs_layout_range range
= {
322 .iomode
= IOMODE_ANY
,
324 .length
= NFS4_MAX_UINT64
,
327 set_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
);
328 pnfs_clear_layoutreturn_info(lo
);
329 return pnfs_mark_matching_lsegs_invalid(lo
, lseg_list
, &range
, 0);
333 pnfs_iomode_to_fail_bit(u32 iomode
)
335 return iomode
== IOMODE_RW
?
336 NFS_LAYOUT_RW_FAILED
: NFS_LAYOUT_RO_FAILED
;
340 pnfs_layout_set_fail_bit(struct pnfs_layout_hdr
*lo
, int fail_bit
)
342 lo
->plh_retry_timestamp
= jiffies
;
343 if (!test_and_set_bit(fail_bit
, &lo
->plh_flags
))
344 atomic_inc(&lo
->plh_refcount
);
348 pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr
*lo
, int fail_bit
)
350 if (test_and_clear_bit(fail_bit
, &lo
->plh_flags
))
351 atomic_dec(&lo
->plh_refcount
);
355 pnfs_layout_io_set_failed(struct pnfs_layout_hdr
*lo
, u32 iomode
)
357 struct inode
*inode
= lo
->plh_inode
;
358 struct pnfs_layout_range range
= {
361 .length
= NFS4_MAX_UINT64
,
365 spin_lock(&inode
->i_lock
);
366 pnfs_layout_set_fail_bit(lo
, pnfs_iomode_to_fail_bit(iomode
));
367 pnfs_mark_matching_lsegs_invalid(lo
, &head
, &range
, 0);
368 spin_unlock(&inode
->i_lock
);
369 pnfs_free_lseg_list(&head
);
370 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__
,
371 iomode
== IOMODE_RW
? "RW" : "READ");
375 pnfs_layout_io_test_failed(struct pnfs_layout_hdr
*lo
, u32 iomode
)
377 unsigned long start
, end
;
378 int fail_bit
= pnfs_iomode_to_fail_bit(iomode
);
380 if (test_bit(fail_bit
, &lo
->plh_flags
) == 0)
383 start
= end
- PNFS_LAYOUTGET_RETRY_TIMEOUT
;
384 if (!time_in_range(lo
->plh_retry_timestamp
, start
, end
)) {
385 /* It is time to retry the failed layoutgets */
386 pnfs_layout_clear_fail_bit(lo
, fail_bit
);
393 pnfs_init_lseg(struct pnfs_layout_hdr
*lo
, struct pnfs_layout_segment
*lseg
,
394 const struct pnfs_layout_range
*range
,
395 const nfs4_stateid
*stateid
)
397 INIT_LIST_HEAD(&lseg
->pls_list
);
398 INIT_LIST_HEAD(&lseg
->pls_lc_list
);
399 atomic_set(&lseg
->pls_refcount
, 1);
400 set_bit(NFS_LSEG_VALID
, &lseg
->pls_flags
);
401 lseg
->pls_layout
= lo
;
402 lseg
->pls_range
= *range
;
403 lseg
->pls_seq
= be32_to_cpu(stateid
->seqid
);
406 static void pnfs_free_lseg(struct pnfs_layout_segment
*lseg
)
408 struct inode
*ino
= lseg
->pls_layout
->plh_inode
;
410 NFS_SERVER(ino
)->pnfs_curr_ld
->free_lseg(lseg
);
414 pnfs_layout_remove_lseg(struct pnfs_layout_hdr
*lo
,
415 struct pnfs_layout_segment
*lseg
)
417 struct inode
*inode
= lo
->plh_inode
;
419 WARN_ON(test_bit(NFS_LSEG_VALID
, &lseg
->pls_flags
));
420 list_del_init(&lseg
->pls_list
);
421 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
422 atomic_dec(&lo
->plh_refcount
);
423 if (list_empty(&lo
->plh_segs
) &&
424 !test_bit(NFS_LAYOUT_RETURN_REQUESTED
, &lo
->plh_flags
) &&
425 !test_bit(NFS_LAYOUT_RETURN
, &lo
->plh_flags
)) {
426 if (atomic_read(&lo
->plh_outstanding
) == 0)
427 set_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
);
428 clear_bit(NFS_LAYOUT_BULK_RECALL
, &lo
->plh_flags
);
430 rpc_wake_up(&NFS_SERVER(inode
)->roc_rpcwaitq
);
434 pnfs_put_lseg(struct pnfs_layout_segment
*lseg
)
436 struct pnfs_layout_hdr
*lo
;
442 dprintk("%s: lseg %p ref %d valid %d\n", __func__
, lseg
,
443 atomic_read(&lseg
->pls_refcount
),
444 test_bit(NFS_LSEG_VALID
, &lseg
->pls_flags
));
446 lo
= lseg
->pls_layout
;
447 inode
= lo
->plh_inode
;
449 if (atomic_dec_and_lock(&lseg
->pls_refcount
, &inode
->i_lock
)) {
450 if (test_bit(NFS_LSEG_VALID
, &lseg
->pls_flags
)) {
451 spin_unlock(&inode
->i_lock
);
454 pnfs_get_layout_hdr(lo
);
455 pnfs_layout_remove_lseg(lo
, lseg
);
456 spin_unlock(&inode
->i_lock
);
457 pnfs_free_lseg(lseg
);
458 pnfs_put_layout_hdr(lo
);
461 EXPORT_SYMBOL_GPL(pnfs_put_lseg
);
463 static void pnfs_free_lseg_async_work(struct work_struct
*work
)
465 struct pnfs_layout_segment
*lseg
;
466 struct pnfs_layout_hdr
*lo
;
468 lseg
= container_of(work
, struct pnfs_layout_segment
, pls_work
);
469 lo
= lseg
->pls_layout
;
471 pnfs_free_lseg(lseg
);
472 pnfs_put_layout_hdr(lo
);
475 static void pnfs_free_lseg_async(struct pnfs_layout_segment
*lseg
)
477 INIT_WORK(&lseg
->pls_work
, pnfs_free_lseg_async_work
);
478 schedule_work(&lseg
->pls_work
);
482 pnfs_put_lseg_locked(struct pnfs_layout_segment
*lseg
)
487 assert_spin_locked(&lseg
->pls_layout
->plh_inode
->i_lock
);
489 dprintk("%s: lseg %p ref %d valid %d\n", __func__
, lseg
,
490 atomic_read(&lseg
->pls_refcount
),
491 test_bit(NFS_LSEG_VALID
, &lseg
->pls_flags
));
492 if (atomic_dec_and_test(&lseg
->pls_refcount
)) {
493 struct pnfs_layout_hdr
*lo
= lseg
->pls_layout
;
494 if (test_bit(NFS_LSEG_VALID
, &lseg
->pls_flags
))
496 pnfs_get_layout_hdr(lo
);
497 pnfs_layout_remove_lseg(lo
, lseg
);
498 pnfs_free_lseg_async(lseg
);
501 EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked
);
504 end_offset(u64 start
, u64 len
)
509 return end
>= start
? end
: NFS4_MAX_UINT64
;
513 * is l2 fully contained in l1?
515 * [----------------------------------)
520 pnfs_lseg_range_contained(const struct pnfs_layout_range
*l1
,
521 const struct pnfs_layout_range
*l2
)
523 u64 start1
= l1
->offset
;
524 u64 end1
= end_offset(start1
, l1
->length
);
525 u64 start2
= l2
->offset
;
526 u64 end2
= end_offset(start2
, l2
->length
);
528 return (start1
<= start2
) && (end1
>= end2
);
532 * is l1 and l2 intersecting?
534 * [----------------------------------)
539 pnfs_lseg_range_intersecting(const struct pnfs_layout_range
*l1
,
540 const struct pnfs_layout_range
*l2
)
542 u64 start1
= l1
->offset
;
543 u64 end1
= end_offset(start1
, l1
->length
);
544 u64 start2
= l2
->offset
;
545 u64 end2
= end_offset(start2
, l2
->length
);
547 return (end1
== NFS4_MAX_UINT64
|| end1
> start2
) &&
548 (end2
== NFS4_MAX_UINT64
|| end2
> start1
);
551 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment
*lseg
,
552 struct list_head
*tmp_list
)
554 if (!atomic_dec_and_test(&lseg
->pls_refcount
))
556 pnfs_layout_remove_lseg(lseg
->pls_layout
, lseg
);
557 list_add(&lseg
->pls_list
, tmp_list
);
561 /* Returns 1 if lseg is removed from list, 0 otherwise */
562 static int mark_lseg_invalid(struct pnfs_layout_segment
*lseg
,
563 struct list_head
*tmp_list
)
567 if (test_and_clear_bit(NFS_LSEG_VALID
, &lseg
->pls_flags
)) {
568 /* Remove the reference keeping the lseg in the
569 * list. It will now be removed when all
570 * outstanding io is finished.
572 dprintk("%s: lseg %p ref %d\n", __func__
, lseg
,
573 atomic_read(&lseg
->pls_refcount
));
574 if (pnfs_lseg_dec_and_remove_zero(lseg
, tmp_list
))
581 * Compare 2 layout stateid sequence ids, to see which is newer,
582 * taking into account wraparound issues.
584 static bool pnfs_seqid_is_newer(u32 s1
, u32 s2
)
586 return (s32
)(s1
- s2
) > 0;
590 pnfs_should_free_range(const struct pnfs_layout_range
*lseg_range
,
591 const struct pnfs_layout_range
*recall_range
)
593 return (recall_range
->iomode
== IOMODE_ANY
||
594 lseg_range
->iomode
== recall_range
->iomode
) &&
595 pnfs_lseg_range_intersecting(lseg_range
, recall_range
);
599 pnfs_match_lseg_recall(const struct pnfs_layout_segment
*lseg
,
600 const struct pnfs_layout_range
*recall_range
,
603 if (seq
!= 0 && pnfs_seqid_is_newer(lseg
->pls_seq
, seq
))
605 if (recall_range
== NULL
)
607 return pnfs_should_free_range(&lseg
->pls_range
, recall_range
);
611 * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later
612 * @lo: layout header containing the lsegs
613 * @tmp_list: list head where doomed lsegs should go
614 * @recall_range: optional recall range argument to match (may be NULL)
615 * @seq: only invalidate lsegs obtained prior to this sequence (may be 0)
617 * Walk the list of lsegs in the layout header, and tear down any that should
618 * be destroyed. If "recall_range" is specified then the segment must match
619 * that range. If "seq" is non-zero, then only match segments that were handed
620 * out at or before that sequence.
622 * Returns number of matching invalid lsegs remaining in list after scanning
623 * it and purging them.
626 pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr
*lo
,
627 struct list_head
*tmp_list
,
628 const struct pnfs_layout_range
*recall_range
,
631 struct pnfs_layout_segment
*lseg
, *next
;
634 dprintk("%s:Begin lo %p\n", __func__
, lo
);
636 if (list_empty(&lo
->plh_segs
))
638 list_for_each_entry_safe(lseg
, next
, &lo
->plh_segs
, pls_list
)
639 if (pnfs_match_lseg_recall(lseg
, recall_range
, seq
)) {
640 dprintk("%s: freeing lseg %p iomode %d seq %u"
641 "offset %llu length %llu\n", __func__
,
642 lseg
, lseg
->pls_range
.iomode
, lseg
->pls_seq
,
643 lseg
->pls_range
.offset
, lseg
->pls_range
.length
);
644 if (!mark_lseg_invalid(lseg
, tmp_list
))
647 dprintk("%s:Return %i\n", __func__
, remaining
);
651 /* note free_me must contain lsegs from a single layout_hdr */
653 pnfs_free_lseg_list(struct list_head
*free_me
)
655 struct pnfs_layout_segment
*lseg
, *tmp
;
657 if (list_empty(free_me
))
660 list_for_each_entry_safe(lseg
, tmp
, free_me
, pls_list
) {
661 list_del(&lseg
->pls_list
);
662 pnfs_free_lseg(lseg
);
667 pnfs_destroy_layout(struct nfs_inode
*nfsi
)
669 struct pnfs_layout_hdr
*lo
;
672 spin_lock(&nfsi
->vfs_inode
.i_lock
);
675 pnfs_get_layout_hdr(lo
);
676 pnfs_mark_layout_stateid_invalid(lo
, &tmp_list
);
677 pnfs_layout_clear_fail_bit(lo
, NFS_LAYOUT_RO_FAILED
);
678 pnfs_layout_clear_fail_bit(lo
, NFS_LAYOUT_RW_FAILED
);
679 spin_unlock(&nfsi
->vfs_inode
.i_lock
);
680 pnfs_free_lseg_list(&tmp_list
);
681 pnfs_put_layout_hdr(lo
);
683 spin_unlock(&nfsi
->vfs_inode
.i_lock
);
685 EXPORT_SYMBOL_GPL(pnfs_destroy_layout
);
688 pnfs_layout_add_bulk_destroy_list(struct inode
*inode
,
689 struct list_head
*layout_list
)
691 struct pnfs_layout_hdr
*lo
;
694 spin_lock(&inode
->i_lock
);
695 lo
= NFS_I(inode
)->layout
;
696 if (lo
!= NULL
&& list_empty(&lo
->plh_bulk_destroy
)) {
697 pnfs_get_layout_hdr(lo
);
698 list_add(&lo
->plh_bulk_destroy
, layout_list
);
701 spin_unlock(&inode
->i_lock
);
705 /* Caller must hold rcu_read_lock and clp->cl_lock */
707 pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client
*clp
,
708 struct nfs_server
*server
,
709 struct list_head
*layout_list
)
711 struct pnfs_layout_hdr
*lo
, *next
;
714 list_for_each_entry_safe(lo
, next
, &server
->layouts
, plh_layouts
) {
715 inode
= igrab(lo
->plh_inode
);
718 list_del_init(&lo
->plh_layouts
);
719 if (pnfs_layout_add_bulk_destroy_list(inode
, layout_list
))
722 spin_unlock(&clp
->cl_lock
);
724 spin_lock(&clp
->cl_lock
);
732 pnfs_layout_free_bulk_destroy_list(struct list_head
*layout_list
,
735 struct pnfs_layout_hdr
*lo
;
737 LIST_HEAD(lseg_list
);
740 while (!list_empty(layout_list
)) {
741 lo
= list_entry(layout_list
->next
, struct pnfs_layout_hdr
,
743 dprintk("%s freeing layout for inode %lu\n", __func__
,
744 lo
->plh_inode
->i_ino
);
745 inode
= lo
->plh_inode
;
747 pnfs_layoutcommit_inode(inode
, false);
749 spin_lock(&inode
->i_lock
);
750 list_del_init(&lo
->plh_bulk_destroy
);
751 if (pnfs_mark_layout_stateid_invalid(lo
, &lseg_list
)) {
753 set_bit(NFS_LAYOUT_BULK_RECALL
, &lo
->plh_flags
);
756 spin_unlock(&inode
->i_lock
);
757 pnfs_free_lseg_list(&lseg_list
);
758 /* Free all lsegs that are attached to commit buckets */
759 nfs_commit_inode(inode
, 0);
760 pnfs_put_layout_hdr(lo
);
767 pnfs_destroy_layouts_byfsid(struct nfs_client
*clp
,
768 struct nfs_fsid
*fsid
,
771 struct nfs_server
*server
;
772 LIST_HEAD(layout_list
);
774 spin_lock(&clp
->cl_lock
);
777 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
778 if (memcmp(&server
->fsid
, fsid
, sizeof(*fsid
)) != 0)
780 if (pnfs_layout_bulk_destroy_byserver_locked(clp
,
786 spin_unlock(&clp
->cl_lock
);
788 if (list_empty(&layout_list
))
790 return pnfs_layout_free_bulk_destroy_list(&layout_list
, is_recall
);
794 pnfs_destroy_layouts_byclid(struct nfs_client
*clp
,
797 struct nfs_server
*server
;
798 LIST_HEAD(layout_list
);
800 spin_lock(&clp
->cl_lock
);
803 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
804 if (pnfs_layout_bulk_destroy_byserver_locked(clp
,
810 spin_unlock(&clp
->cl_lock
);
812 if (list_empty(&layout_list
))
814 return pnfs_layout_free_bulk_destroy_list(&layout_list
, is_recall
);
818 * Called by the state manger to remove all layouts established under an
822 pnfs_destroy_all_layouts(struct nfs_client
*clp
)
824 nfs4_deviceid_mark_client_invalid(clp
);
825 nfs4_deviceid_purge_client(clp
);
827 pnfs_destroy_layouts_byclid(clp
, false);
830 /* update lo->plh_stateid with new if is more recent */
832 pnfs_set_layout_stateid(struct pnfs_layout_hdr
*lo
, const nfs4_stateid
*new,
835 u32 oldseq
, newseq
, new_barrier
= 0;
837 oldseq
= be32_to_cpu(lo
->plh_stateid
.seqid
);
838 newseq
= be32_to_cpu(new->seqid
);
840 if (!pnfs_layout_is_valid(lo
)) {
841 nfs4_stateid_copy(&lo
->plh_stateid
, new);
842 lo
->plh_barrier
= newseq
;
843 pnfs_clear_layoutreturn_info(lo
);
844 clear_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
);
847 if (pnfs_seqid_is_newer(newseq
, oldseq
)) {
848 nfs4_stateid_copy(&lo
->plh_stateid
, new);
850 * Because of wraparound, we want to keep the barrier
851 * "close" to the current seqids.
853 new_barrier
= newseq
- atomic_read(&lo
->plh_outstanding
);
856 new_barrier
= be32_to_cpu(new->seqid
);
857 else if (new_barrier
== 0)
859 if (pnfs_seqid_is_newer(new_barrier
, lo
->plh_barrier
))
860 lo
->plh_barrier
= new_barrier
;
864 pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr
*lo
,
865 const nfs4_stateid
*stateid
)
867 u32 seqid
= be32_to_cpu(stateid
->seqid
);
869 return !pnfs_seqid_is_newer(seqid
, lo
->plh_barrier
);
872 /* lget is set to 1 if called from inside send_layoutget call chain */
874 pnfs_layoutgets_blocked(const struct pnfs_layout_hdr
*lo
)
876 return lo
->plh_block_lgets
||
877 test_bit(NFS_LAYOUT_BULK_RECALL
, &lo
->plh_flags
);
881 * Get layout from server.
882 * for now, assume that whole file layouts are requested.
884 * arg->length: all ones
886 static struct pnfs_layout_segment
*
887 send_layoutget(struct pnfs_layout_hdr
*lo
,
888 struct nfs_open_context
*ctx
,
889 nfs4_stateid
*stateid
,
890 const struct pnfs_layout_range
*range
,
891 long *timeout
, gfp_t gfp_flags
)
893 struct inode
*ino
= lo
->plh_inode
;
894 struct nfs_server
*server
= NFS_SERVER(ino
);
895 struct nfs4_layoutget
*lgp
;
898 dprintk("--> %s\n", __func__
);
901 * Synchronously retrieve layout information from server and
902 * store in lseg. If we race with a concurrent seqid morphing
903 * op, then re-send the LAYOUTGET.
905 lgp
= kzalloc(sizeof(*lgp
), gfp_flags
);
907 return ERR_PTR(-ENOMEM
);
909 i_size
= i_size_read(ino
);
911 lgp
->args
.minlength
= PAGE_SIZE
;
912 if (lgp
->args
.minlength
> range
->length
)
913 lgp
->args
.minlength
= range
->length
;
914 if (range
->iomode
== IOMODE_READ
) {
915 if (range
->offset
>= i_size
)
916 lgp
->args
.minlength
= 0;
917 else if (i_size
- range
->offset
< lgp
->args
.minlength
)
918 lgp
->args
.minlength
= i_size
- range
->offset
;
920 lgp
->args
.maxcount
= PNFS_LAYOUT_MAXSIZE
;
921 pnfs_copy_range(&lgp
->args
.range
, range
);
922 lgp
->args
.type
= server
->pnfs_curr_ld
->id
;
923 lgp
->args
.inode
= ino
;
924 lgp
->args
.ctx
= get_nfs_open_context(ctx
);
925 nfs4_stateid_copy(&lgp
->args
.stateid
, stateid
);
926 lgp
->gfp_flags
= gfp_flags
;
927 lgp
->cred
= lo
->plh_lc_cred
;
929 return nfs4_proc_layoutget(lgp
, timeout
, gfp_flags
);
932 static void pnfs_clear_layoutcommit(struct inode
*inode
,
933 struct list_head
*head
)
935 struct nfs_inode
*nfsi
= NFS_I(inode
);
936 struct pnfs_layout_segment
*lseg
, *tmp
;
938 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT
, &nfsi
->flags
))
940 list_for_each_entry_safe(lseg
, tmp
, &nfsi
->layout
->plh_segs
, pls_list
) {
941 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT
, &lseg
->pls_flags
))
943 pnfs_lseg_dec_and_remove_zero(lseg
, head
);
947 void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr
*lo
)
949 clear_bit_unlock(NFS_LAYOUT_RETURN
, &lo
->plh_flags
);
950 clear_bit(NFS_LAYOUT_RETURN_LOCK
, &lo
->plh_flags
);
951 smp_mb__after_atomic();
952 wake_up_bit(&lo
->plh_flags
, NFS_LAYOUT_RETURN
);
953 rpc_wake_up(&NFS_SERVER(lo
->plh_inode
)->roc_rpcwaitq
);
957 pnfs_prepare_layoutreturn(struct pnfs_layout_hdr
*lo
,
958 nfs4_stateid
*stateid
,
959 enum pnfs_iomode
*iomode
)
961 /* Serialise LAYOUTGET/LAYOUTRETURN */
962 if (atomic_read(&lo
->plh_outstanding
) != 0)
964 if (test_and_set_bit(NFS_LAYOUT_RETURN_LOCK
, &lo
->plh_flags
))
966 set_bit(NFS_LAYOUT_RETURN
, &lo
->plh_flags
);
967 pnfs_get_layout_hdr(lo
);
968 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED
, &lo
->plh_flags
)) {
969 if (stateid
!= NULL
) {
970 nfs4_stateid_copy(stateid
, &lo
->plh_stateid
);
971 if (lo
->plh_return_seq
!= 0)
972 stateid
->seqid
= cpu_to_be32(lo
->plh_return_seq
);
975 *iomode
= lo
->plh_return_iomode
;
976 pnfs_clear_layoutreturn_info(lo
);
980 nfs4_stateid_copy(stateid
, &lo
->plh_stateid
);
982 *iomode
= IOMODE_ANY
;
987 pnfs_send_layoutreturn(struct pnfs_layout_hdr
*lo
, const nfs4_stateid
*stateid
,
988 enum pnfs_iomode iomode
, bool sync
)
990 struct inode
*ino
= lo
->plh_inode
;
991 struct nfs4_layoutreturn
*lrp
;
994 lrp
= kzalloc(sizeof(*lrp
), GFP_NOFS
);
995 if (unlikely(lrp
== NULL
)) {
997 spin_lock(&ino
->i_lock
);
998 pnfs_clear_layoutreturn_waitbit(lo
);
999 spin_unlock(&ino
->i_lock
);
1000 pnfs_put_layout_hdr(lo
);
1004 nfs4_stateid_copy(&lrp
->args
.stateid
, stateid
);
1005 lrp
->args
.layout_type
= NFS_SERVER(ino
)->pnfs_curr_ld
->id
;
1006 lrp
->args
.inode
= ino
;
1007 lrp
->args
.range
.iomode
= iomode
;
1008 lrp
->args
.range
.offset
= 0;
1009 lrp
->args
.range
.length
= NFS4_MAX_UINT64
;
1010 lrp
->args
.layout
= lo
;
1011 lrp
->clp
= NFS_SERVER(ino
)->nfs_client
;
1012 lrp
->cred
= lo
->plh_lc_cred
;
1014 status
= nfs4_proc_layoutreturn(lrp
, sync
);
1016 dprintk("<-- %s status: %d\n", __func__
, status
);
1020 /* Return true if layoutreturn is needed */
1022 pnfs_layout_need_return(struct pnfs_layout_hdr
*lo
)
1024 struct pnfs_layout_segment
*s
;
1026 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED
, &lo
->plh_flags
))
1029 /* Defer layoutreturn until all lsegs are done */
1030 list_for_each_entry(s
, &lo
->plh_segs
, pls_list
) {
1031 if (test_bit(NFS_LSEG_LAYOUTRETURN
, &s
->pls_flags
))
1038 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr
*lo
)
1040 struct inode
*inode
= lo
->plh_inode
;
1042 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED
, &lo
->plh_flags
))
1044 spin_lock(&inode
->i_lock
);
1045 if (pnfs_layout_need_return(lo
)) {
1046 nfs4_stateid stateid
;
1047 enum pnfs_iomode iomode
;
1050 send
= pnfs_prepare_layoutreturn(lo
, &stateid
, &iomode
);
1051 spin_unlock(&inode
->i_lock
);
1053 /* Send an async layoutreturn so we dont deadlock */
1054 pnfs_send_layoutreturn(lo
, &stateid
, iomode
, false);
1057 spin_unlock(&inode
->i_lock
);
1061 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
1062 * when the layout segment list is empty.
1064 * Note that a pnfs_layout_hdr can exist with an empty layout segment
1065 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
1066 * deviceid is marked invalid.
1069 _pnfs_return_layout(struct inode
*ino
)
1071 struct pnfs_layout_hdr
*lo
= NULL
;
1072 struct nfs_inode
*nfsi
= NFS_I(ino
);
1073 LIST_HEAD(tmp_list
);
1074 nfs4_stateid stateid
;
1075 int status
= 0, empty
;
1078 dprintk("NFS: %s for inode %lu\n", __func__
, ino
->i_ino
);
1080 spin_lock(&ino
->i_lock
);
1083 spin_unlock(&ino
->i_lock
);
1084 dprintk("NFS: %s no layout to return\n", __func__
);
1087 /* Reference matched in nfs4_layoutreturn_release */
1088 pnfs_get_layout_hdr(lo
);
1089 empty
= list_empty(&lo
->plh_segs
);
1090 pnfs_clear_layoutcommit(ino
, &tmp_list
);
1091 pnfs_mark_matching_lsegs_invalid(lo
, &tmp_list
, NULL
, 0);
1093 if (NFS_SERVER(ino
)->pnfs_curr_ld
->return_range
) {
1094 struct pnfs_layout_range range
= {
1095 .iomode
= IOMODE_ANY
,
1097 .length
= NFS4_MAX_UINT64
,
1099 NFS_SERVER(ino
)->pnfs_curr_ld
->return_range(lo
, &range
);
1102 /* Don't send a LAYOUTRETURN if list was initially empty */
1104 spin_unlock(&ino
->i_lock
);
1105 dprintk("NFS: %s no layout segments to return\n", __func__
);
1106 goto out_put_layout_hdr
;
1109 send
= pnfs_prepare_layoutreturn(lo
, &stateid
, NULL
);
1110 spin_unlock(&ino
->i_lock
);
1111 pnfs_free_lseg_list(&tmp_list
);
1113 status
= pnfs_send_layoutreturn(lo
, &stateid
, IOMODE_ANY
, true);
1115 pnfs_put_layout_hdr(lo
);
1117 dprintk("<-- %s status: %d\n", __func__
, status
);
1120 EXPORT_SYMBOL_GPL(_pnfs_return_layout
);
1123 pnfs_commit_and_return_layout(struct inode
*inode
)
1125 struct pnfs_layout_hdr
*lo
;
1128 spin_lock(&inode
->i_lock
);
1129 lo
= NFS_I(inode
)->layout
;
1131 spin_unlock(&inode
->i_lock
);
1134 pnfs_get_layout_hdr(lo
);
1135 /* Block new layoutgets and read/write to ds */
1136 lo
->plh_block_lgets
++;
1137 spin_unlock(&inode
->i_lock
);
1138 filemap_fdatawait(inode
->i_mapping
);
1139 ret
= pnfs_layoutcommit_inode(inode
, true);
1141 ret
= _pnfs_return_layout(inode
);
1142 spin_lock(&inode
->i_lock
);
1143 lo
->plh_block_lgets
--;
1144 spin_unlock(&inode
->i_lock
);
1145 pnfs_put_layout_hdr(lo
);
1149 bool pnfs_roc(struct inode
*ino
)
1151 struct nfs_inode
*nfsi
= NFS_I(ino
);
1152 struct nfs_open_context
*ctx
;
1153 struct nfs4_state
*state
;
1154 struct pnfs_layout_hdr
*lo
;
1155 struct pnfs_layout_segment
*lseg
, *tmp
;
1156 nfs4_stateid stateid
;
1157 LIST_HEAD(tmp_list
);
1158 bool found
= false, layoutreturn
= false, roc
= false;
1160 spin_lock(&ino
->i_lock
);
1162 if (!lo
|| test_bit(NFS_LAYOUT_BULK_RECALL
, &lo
->plh_flags
))
1165 /* no roc if we hold a delegation */
1166 if (nfs4_check_delegation(ino
, FMODE_READ
))
1169 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1171 /* Don't return layout if there is open file state */
1172 if (state
!= NULL
&& state
->state
!= 0)
1176 /* always send layoutreturn if being marked so */
1177 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED
, &lo
->plh_flags
))
1178 layoutreturn
= pnfs_prepare_layoutreturn(lo
,
1181 list_for_each_entry_safe(lseg
, tmp
, &lo
->plh_segs
, pls_list
)
1182 /* If we are sending layoutreturn, invalidate all valid lsegs */
1183 if (layoutreturn
|| test_bit(NFS_LSEG_ROC
, &lseg
->pls_flags
)) {
1184 mark_lseg_invalid(lseg
, &tmp_list
);
1187 /* ROC in two conditions:
1188 * 1. there are ROC lsegs
1189 * 2. we don't send layoutreturn
1191 if (found
&& !layoutreturn
) {
1192 /* lo ref dropped in pnfs_roc_release() */
1193 pnfs_get_layout_hdr(lo
);
1198 spin_unlock(&ino
->i_lock
);
1199 pnfs_free_lseg_list(&tmp_list
);
1200 pnfs_layoutcommit_inode(ino
, true);
1202 pnfs_send_layoutreturn(lo
, &stateid
, IOMODE_ANY
, true);
1206 void pnfs_roc_release(struct inode
*ino
)
1208 struct pnfs_layout_hdr
*lo
;
1210 spin_lock(&ino
->i_lock
);
1211 lo
= NFS_I(ino
)->layout
;
1212 pnfs_clear_layoutreturn_waitbit(lo
);
1213 if (atomic_dec_and_test(&lo
->plh_refcount
)) {
1214 pnfs_detach_layout_hdr(lo
);
1215 spin_unlock(&ino
->i_lock
);
1216 pnfs_free_layout_hdr(lo
);
1218 spin_unlock(&ino
->i_lock
);
1221 void pnfs_roc_set_barrier(struct inode
*ino
, u32 barrier
)
1223 struct pnfs_layout_hdr
*lo
;
1225 spin_lock(&ino
->i_lock
);
1226 lo
= NFS_I(ino
)->layout
;
1227 if (pnfs_seqid_is_newer(barrier
, lo
->plh_barrier
))
1228 lo
->plh_barrier
= barrier
;
1229 spin_unlock(&ino
->i_lock
);
1230 trace_nfs4_layoutreturn_on_close(ino
, 0);
1233 void pnfs_roc_get_barrier(struct inode
*ino
, u32
*barrier
)
1235 struct nfs_inode
*nfsi
= NFS_I(ino
);
1236 struct pnfs_layout_hdr
*lo
;
1239 spin_lock(&ino
->i_lock
);
1241 current_seqid
= be32_to_cpu(lo
->plh_stateid
.seqid
);
1243 /* Since close does not return a layout stateid for use as
1244 * a barrier, we choose the worst-case barrier.
1246 *barrier
= current_seqid
+ atomic_read(&lo
->plh_outstanding
);
1247 spin_unlock(&ino
->i_lock
);
1250 bool pnfs_wait_on_layoutreturn(struct inode
*ino
, struct rpc_task
*task
)
1252 struct nfs_inode
*nfsi
= NFS_I(ino
);
1253 struct pnfs_layout_hdr
*lo
;
1256 /* we might not have grabbed lo reference. so need to check under
1258 spin_lock(&ino
->i_lock
);
1260 if (lo
&& test_bit(NFS_LAYOUT_RETURN
, &lo
->plh_flags
)) {
1261 rpc_sleep_on(&NFS_SERVER(ino
)->roc_rpcwaitq
, task
, NULL
);
1264 spin_unlock(&ino
->i_lock
);
1269 * Compare two layout segments for sorting into layout cache.
1270 * We want to preferentially return RW over RO layouts, so ensure those
1274 pnfs_lseg_range_cmp(const struct pnfs_layout_range
*l1
,
1275 const struct pnfs_layout_range
*l2
)
1279 /* high offset > low offset */
1280 d
= l1
->offset
- l2
->offset
;
1284 /* short length > long length */
1285 d
= l2
->length
- l1
->length
;
1289 /* read > read/write */
1290 return (int)(l1
->iomode
== IOMODE_READ
) - (int)(l2
->iomode
== IOMODE_READ
);
1294 pnfs_lseg_range_is_after(const struct pnfs_layout_range
*l1
,
1295 const struct pnfs_layout_range
*l2
)
1297 return pnfs_lseg_range_cmp(l1
, l2
) > 0;
1301 pnfs_lseg_no_merge(struct pnfs_layout_segment
*lseg
,
1302 struct pnfs_layout_segment
*old
)
1308 pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr
*lo
,
1309 struct pnfs_layout_segment
*lseg
,
1310 bool (*is_after
)(const struct pnfs_layout_range
*,
1311 const struct pnfs_layout_range
*),
1312 bool (*do_merge
)(struct pnfs_layout_segment
*,
1313 struct pnfs_layout_segment
*),
1314 struct list_head
*free_me
)
1316 struct pnfs_layout_segment
*lp
, *tmp
;
1318 dprintk("%s:Begin\n", __func__
);
1320 list_for_each_entry_safe(lp
, tmp
, &lo
->plh_segs
, pls_list
) {
1321 if (test_bit(NFS_LSEG_VALID
, &lp
->pls_flags
) == 0)
1323 if (do_merge(lseg
, lp
)) {
1324 mark_lseg_invalid(lp
, free_me
);
1327 if (is_after(&lseg
->pls_range
, &lp
->pls_range
))
1329 list_add_tail(&lseg
->pls_list
, &lp
->pls_list
);
1330 dprintk("%s: inserted lseg %p "
1331 "iomode %d offset %llu length %llu before "
1332 "lp %p iomode %d offset %llu length %llu\n",
1333 __func__
, lseg
, lseg
->pls_range
.iomode
,
1334 lseg
->pls_range
.offset
, lseg
->pls_range
.length
,
1335 lp
, lp
->pls_range
.iomode
, lp
->pls_range
.offset
,
1336 lp
->pls_range
.length
);
1339 list_add_tail(&lseg
->pls_list
, &lo
->plh_segs
);
1340 dprintk("%s: inserted lseg %p "
1341 "iomode %d offset %llu length %llu at tail\n",
1342 __func__
, lseg
, lseg
->pls_range
.iomode
,
1343 lseg
->pls_range
.offset
, lseg
->pls_range
.length
);
1345 pnfs_get_layout_hdr(lo
);
1347 dprintk("%s:Return\n", __func__
);
1349 EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg
);
1352 pnfs_layout_insert_lseg(struct pnfs_layout_hdr
*lo
,
1353 struct pnfs_layout_segment
*lseg
,
1354 struct list_head
*free_me
)
1356 struct inode
*inode
= lo
->plh_inode
;
1357 struct pnfs_layoutdriver_type
*ld
= NFS_SERVER(inode
)->pnfs_curr_ld
;
1359 if (ld
->add_lseg
!= NULL
)
1360 ld
->add_lseg(lo
, lseg
, free_me
);
1362 pnfs_generic_layout_insert_lseg(lo
, lseg
,
1363 pnfs_lseg_range_is_after
,
1368 static struct pnfs_layout_hdr
*
1369 alloc_init_layout_hdr(struct inode
*ino
,
1370 struct nfs_open_context
*ctx
,
1373 struct pnfs_layout_hdr
*lo
;
1375 lo
= pnfs_alloc_layout_hdr(ino
, gfp_flags
);
1378 atomic_set(&lo
->plh_refcount
, 1);
1379 INIT_LIST_HEAD(&lo
->plh_layouts
);
1380 INIT_LIST_HEAD(&lo
->plh_segs
);
1381 INIT_LIST_HEAD(&lo
->plh_bulk_destroy
);
1382 lo
->plh_inode
= ino
;
1383 lo
->plh_lc_cred
= get_rpccred(ctx
->cred
);
1384 lo
->plh_flags
|= 1 << NFS_LAYOUT_INVALID_STID
;
1388 static struct pnfs_layout_hdr
*
1389 pnfs_find_alloc_layout(struct inode
*ino
,
1390 struct nfs_open_context
*ctx
,
1392 __releases(&ino
->i_lock
)
1393 __acquires(&ino
->i_lock
)
1395 struct nfs_inode
*nfsi
= NFS_I(ino
);
1396 struct pnfs_layout_hdr
*new = NULL
;
1398 dprintk("%s Begin ino=%p layout=%p\n", __func__
, ino
, nfsi
->layout
);
1400 if (nfsi
->layout
!= NULL
)
1402 spin_unlock(&ino
->i_lock
);
1403 new = alloc_init_layout_hdr(ino
, ctx
, gfp_flags
);
1404 spin_lock(&ino
->i_lock
);
1406 if (likely(nfsi
->layout
== NULL
)) { /* Won the race? */
1409 } else if (new != NULL
)
1410 pnfs_free_layout_hdr(new);
1412 pnfs_get_layout_hdr(nfsi
->layout
);
1413 return nfsi
->layout
;
1417 * iomode matching rules:
1418 * iomode lseg strict match
1420 * ----- ----- ------ -----
1425 * READ READ N/A true
1426 * READ RW true false
1427 * READ RW false true
1430 pnfs_lseg_range_match(const struct pnfs_layout_range
*ls_range
,
1431 const struct pnfs_layout_range
*range
,
1434 struct pnfs_layout_range range1
;
1436 if ((range
->iomode
== IOMODE_RW
&&
1437 ls_range
->iomode
!= IOMODE_RW
) ||
1438 (range
->iomode
!= ls_range
->iomode
&&
1439 strict_iomode
== true) ||
1440 !pnfs_lseg_range_intersecting(ls_range
, range
))
1443 /* range1 covers only the first byte in the range */
1446 return pnfs_lseg_range_contained(ls_range
, &range1
);
1450 * lookup range in layout
1452 static struct pnfs_layout_segment
*
1453 pnfs_find_lseg(struct pnfs_layout_hdr
*lo
,
1454 struct pnfs_layout_range
*range
,
1457 struct pnfs_layout_segment
*lseg
, *ret
= NULL
;
1459 dprintk("%s:Begin\n", __func__
);
1461 list_for_each_entry(lseg
, &lo
->plh_segs
, pls_list
) {
1462 if (test_bit(NFS_LSEG_VALID
, &lseg
->pls_flags
) &&
1463 !test_bit(NFS_LSEG_LAYOUTRETURN
, &lseg
->pls_flags
) &&
1464 pnfs_lseg_range_match(&lseg
->pls_range
, range
,
1466 ret
= pnfs_get_lseg(lseg
);
1471 dprintk("%s:Return lseg %p ref %d\n",
1472 __func__
, ret
, ret
? atomic_read(&ret
->pls_refcount
) : 0);
1477 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1478 * to the MDS or over pNFS
1480 * The nfs_inode read_io and write_io fields are cumulative counters reset
1481 * when there are no layout segments. Note that in pnfs_update_layout iomode
1482 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1485 * A return of true means use MDS I/O.
1488 * If a file's size is smaller than the file size threshold, data accesses
1489 * SHOULD be sent to the metadata server. If an I/O request has a length that
1490 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1491 * server. If both file size and I/O size are provided, the client SHOULD
1492 * reach or exceed both thresholds before sending its read or write
1493 * requests to the data server.
1495 static bool pnfs_within_mdsthreshold(struct nfs_open_context
*ctx
,
1496 struct inode
*ino
, int iomode
)
1498 struct nfs4_threshold
*t
= ctx
->mdsthreshold
;
1499 struct nfs_inode
*nfsi
= NFS_I(ino
);
1500 loff_t fsize
= i_size_read(ino
);
1501 bool size
= false, size_set
= false, io
= false, io_set
= false, ret
= false;
1506 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1507 __func__
, t
->bm
, t
->rd_sz
, t
->wr_sz
, t
->rd_io_sz
, t
->wr_io_sz
);
1511 if (t
->bm
& THRESHOLD_RD
) {
1512 dprintk("%s fsize %llu\n", __func__
, fsize
);
1514 if (fsize
< t
->rd_sz
)
1517 if (t
->bm
& THRESHOLD_RD_IO
) {
1518 dprintk("%s nfsi->read_io %llu\n", __func__
,
1521 if (nfsi
->read_io
< t
->rd_io_sz
)
1526 if (t
->bm
& THRESHOLD_WR
) {
1527 dprintk("%s fsize %llu\n", __func__
, fsize
);
1529 if (fsize
< t
->wr_sz
)
1532 if (t
->bm
& THRESHOLD_WR_IO
) {
1533 dprintk("%s nfsi->write_io %llu\n", __func__
,
1536 if (nfsi
->write_io
< t
->wr_io_sz
)
1541 if (size_set
&& io_set
) {
1544 } else if (size
|| io
)
1547 dprintk("<-- %s size %d io %d ret %d\n", __func__
, size
, io
, ret
);
1551 static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr
*lo
)
1554 * send layoutcommit as it can hold up layoutreturn due to lseg
1557 pnfs_layoutcommit_inode(lo
->plh_inode
, false);
1558 return !wait_on_bit_action(&lo
->plh_flags
, NFS_LAYOUT_RETURN
,
1559 nfs_wait_bit_killable
,
1560 TASK_UNINTERRUPTIBLE
);
1563 static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr
*lo
)
1565 unsigned long *bitlock
= &lo
->plh_flags
;
1567 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET
, bitlock
);
1568 smp_mb__after_atomic();
1569 wake_up_bit(bitlock
, NFS_LAYOUT_FIRST_LAYOUTGET
);
1573 * Layout segment is retreived from the server if not cached.
1574 * The appropriate layout segment is referenced and returned to the caller.
1576 struct pnfs_layout_segment
*
1577 pnfs_update_layout(struct inode
*ino
,
1578 struct nfs_open_context
*ctx
,
1581 enum pnfs_iomode iomode
,
1585 struct pnfs_layout_range arg
= {
1590 unsigned pg_offset
, seq
;
1591 struct nfs_server
*server
= NFS_SERVER(ino
);
1592 struct nfs_client
*clp
= server
->nfs_client
;
1593 struct pnfs_layout_hdr
*lo
= NULL
;
1594 struct pnfs_layout_segment
*lseg
= NULL
;
1595 nfs4_stateid stateid
;
1597 unsigned long giveup
= jiffies
+ (clp
->cl_lease_time
<< 1);
1600 if (!pnfs_enabled_sb(NFS_SERVER(ino
))) {
1601 trace_pnfs_update_layout(ino
, pos
, count
, iomode
, lo
, lseg
,
1602 PNFS_UPDATE_LAYOUT_NO_PNFS
);
1606 if (iomode
== IOMODE_READ
&& i_size_read(ino
) == 0) {
1607 trace_pnfs_update_layout(ino
, pos
, count
, iomode
, lo
, lseg
,
1608 PNFS_UPDATE_LAYOUT_RD_ZEROLEN
);
1612 if (pnfs_within_mdsthreshold(ctx
, ino
, iomode
)) {
1613 trace_pnfs_update_layout(ino
, pos
, count
, iomode
, lo
, lseg
,
1614 PNFS_UPDATE_LAYOUT_MDSTHRESH
);
1619 nfs4_client_recover_expired_lease(clp
);
1621 spin_lock(&ino
->i_lock
);
1622 lo
= pnfs_find_alloc_layout(ino
, ctx
, gfp_flags
);
1624 spin_unlock(&ino
->i_lock
);
1625 trace_pnfs_update_layout(ino
, pos
, count
, iomode
, lo
, lseg
,
1626 PNFS_UPDATE_LAYOUT_NOMEM
);
1630 /* Do we even need to bother with this? */
1631 if (test_bit(NFS_LAYOUT_BULK_RECALL
, &lo
->plh_flags
)) {
1632 trace_pnfs_update_layout(ino
, pos
, count
, iomode
, lo
, lseg
,
1633 PNFS_UPDATE_LAYOUT_BULK_RECALL
);
1634 dprintk("%s matches recall, use MDS\n", __func__
);
1638 /* if LAYOUTGET already failed once we don't try again */
1639 if (pnfs_layout_io_test_failed(lo
, iomode
)) {
1640 trace_pnfs_update_layout(ino
, pos
, count
, iomode
, lo
, lseg
,
1641 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL
);
1645 lseg
= pnfs_find_lseg(lo
, &arg
, strict_iomode
);
1647 trace_pnfs_update_layout(ino
, pos
, count
, iomode
, lo
, lseg
,
1648 PNFS_UPDATE_LAYOUT_FOUND_CACHED
);
1652 if (!nfs4_valid_open_stateid(ctx
->state
)) {
1653 trace_pnfs_update_layout(ino
, pos
, count
, iomode
, lo
, lseg
,
1654 PNFS_UPDATE_LAYOUT_INVALID_OPEN
);
1659 * Choose a stateid for the LAYOUTGET. If we don't have a layout
1660 * stateid, or it has been invalidated, then we must use the open
1663 if (test_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
)) {
1666 * The first layoutget for the file. Need to serialize per
1667 * RFC 5661 Errata 3208.
1669 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET
,
1671 spin_unlock(&ino
->i_lock
);
1672 wait_on_bit(&lo
->plh_flags
, NFS_LAYOUT_FIRST_LAYOUTGET
,
1673 TASK_UNINTERRUPTIBLE
);
1674 pnfs_put_layout_hdr(lo
);
1675 dprintk("%s retrying\n", __func__
);
1681 seq
= read_seqbegin(&ctx
->state
->seqlock
);
1682 nfs4_stateid_copy(&stateid
, &ctx
->state
->stateid
);
1683 } while (read_seqretry(&ctx
->state
->seqlock
, seq
));
1685 nfs4_stateid_copy(&stateid
, &lo
->plh_stateid
);
1689 * Because we free lsegs before sending LAYOUTRETURN, we need to wait
1690 * for LAYOUTRETURN even if first is true.
1692 if (test_bit(NFS_LAYOUT_RETURN
, &lo
->plh_flags
)) {
1693 spin_unlock(&ino
->i_lock
);
1694 dprintk("%s wait for layoutreturn\n", __func__
);
1695 if (pnfs_prepare_to_retry_layoutget(lo
)) {
1697 pnfs_clear_first_layoutget(lo
);
1698 pnfs_put_layout_hdr(lo
);
1699 dprintk("%s retrying\n", __func__
);
1700 trace_pnfs_update_layout(ino
, pos
, count
, iomode
, lo
,
1701 lseg
, PNFS_UPDATE_LAYOUT_RETRY
);
1704 trace_pnfs_update_layout(ino
, pos
, count
, iomode
, lo
, lseg
,
1705 PNFS_UPDATE_LAYOUT_RETURN
);
1706 goto out_put_layout_hdr
;
1709 if (pnfs_layoutgets_blocked(lo
)) {
1710 trace_pnfs_update_layout(ino
, pos
, count
, iomode
, lo
, lseg
,
1711 PNFS_UPDATE_LAYOUT_BLOCKED
);
1714 atomic_inc(&lo
->plh_outstanding
);
1715 spin_unlock(&ino
->i_lock
);
1717 if (list_empty(&lo
->plh_layouts
)) {
1718 /* The lo must be on the clp list if there is any
1719 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1721 spin_lock(&clp
->cl_lock
);
1722 if (list_empty(&lo
->plh_layouts
))
1723 list_add_tail(&lo
->plh_layouts
, &server
->layouts
);
1724 spin_unlock(&clp
->cl_lock
);
1727 pg_offset
= arg
.offset
& ~PAGE_MASK
;
1729 arg
.offset
-= pg_offset
;
1730 arg
.length
+= pg_offset
;
1732 if (arg
.length
!= NFS4_MAX_UINT64
)
1733 arg
.length
= PAGE_ALIGN(arg
.length
);
1735 lseg
= send_layoutget(lo
, ctx
, &stateid
, &arg
, &timeout
, gfp_flags
);
1736 trace_pnfs_update_layout(ino
, pos
, count
, iomode
, lo
, lseg
,
1737 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET
);
1738 atomic_dec(&lo
->plh_outstanding
);
1740 switch(PTR_ERR(lseg
)) {
1742 if (time_after(jiffies
, giveup
))
1745 case -ERECALLCONFLICT
:
1746 /* Huh? We hold no layouts, how is there a recall? */
1751 /* Destroy the existing layout and start over */
1752 if (time_after(jiffies
, giveup
))
1753 pnfs_destroy_layout(NFS_I(ino
));
1758 if (!nfs_error_is_fatal(PTR_ERR(lseg
))) {
1759 pnfs_layout_clear_fail_bit(lo
, pnfs_iomode_to_fail_bit(iomode
));
1762 goto out_put_layout_hdr
;
1766 pnfs_clear_first_layoutget(lo
);
1767 trace_pnfs_update_layout(ino
, pos
, count
,
1768 iomode
, lo
, lseg
, PNFS_UPDATE_LAYOUT_RETRY
);
1769 pnfs_put_layout_hdr(lo
);
1773 pnfs_layout_clear_fail_bit(lo
, pnfs_iomode_to_fail_bit(iomode
));
1778 pnfs_clear_first_layoutget(lo
);
1779 pnfs_put_layout_hdr(lo
);
1781 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1782 "(%s, offset: %llu, length: %llu)\n",
1783 __func__
, ino
->i_sb
->s_id
,
1784 (unsigned long long)NFS_FILEID(ino
),
1785 IS_ERR_OR_NULL(lseg
) ? "not found" : "found",
1786 iomode
==IOMODE_RW
? "read/write" : "read-only",
1787 (unsigned long long)pos
,
1788 (unsigned long long)count
);
1791 spin_unlock(&ino
->i_lock
);
1792 goto out_put_layout_hdr
;
1794 EXPORT_SYMBOL_GPL(pnfs_update_layout
);
1797 pnfs_sanity_check_layout_range(struct pnfs_layout_range
*range
)
1799 switch (range
->iomode
) {
1806 if (range
->offset
== NFS4_MAX_UINT64
)
1808 if (range
->length
== 0)
1810 if (range
->length
!= NFS4_MAX_UINT64
&&
1811 range
->length
> NFS4_MAX_UINT64
- range
->offset
)
1816 struct pnfs_layout_segment
*
1817 pnfs_layout_process(struct nfs4_layoutget
*lgp
)
1819 struct pnfs_layout_hdr
*lo
= NFS_I(lgp
->args
.inode
)->layout
;
1820 struct nfs4_layoutget_res
*res
= &lgp
->res
;
1821 struct pnfs_layout_segment
*lseg
;
1822 struct inode
*ino
= lo
->plh_inode
;
1825 if (!pnfs_sanity_check_layout_range(&res
->range
))
1826 return ERR_PTR(-EINVAL
);
1828 /* Inject layout blob into I/O device driver */
1829 lseg
= NFS_SERVER(ino
)->pnfs_curr_ld
->alloc_lseg(lo
, res
, lgp
->gfp_flags
);
1830 if (IS_ERR_OR_NULL(lseg
)) {
1832 lseg
= ERR_PTR(-ENOMEM
);
1834 dprintk("%s: Could not allocate layout: error %ld\n",
1835 __func__
, PTR_ERR(lseg
));
1839 pnfs_init_lseg(lo
, lseg
, &res
->range
, &res
->stateid
);
1841 spin_lock(&ino
->i_lock
);
1842 if (pnfs_layoutgets_blocked(lo
)) {
1843 dprintk("%s forget reply due to state\n", __func__
);
1847 if (nfs4_stateid_match_other(&lo
->plh_stateid
, &res
->stateid
)) {
1848 /* existing state ID, make sure the sequence number matches. */
1849 if (pnfs_layout_stateid_blocked(lo
, &res
->stateid
)) {
1850 dprintk("%s forget reply due to sequence\n", __func__
);
1853 pnfs_set_layout_stateid(lo
, &res
->stateid
, false);
1856 * We got an entirely new state ID. Mark all segments for the
1857 * inode invalid, and don't bother validating the stateid
1860 pnfs_mark_layout_stateid_invalid(lo
, &free_me
);
1862 pnfs_set_layout_stateid(lo
, &res
->stateid
, true);
1865 pnfs_get_lseg(lseg
);
1866 pnfs_layout_insert_lseg(lo
, lseg
, &free_me
);
1869 if (res
->return_on_close
)
1870 set_bit(NFS_LSEG_ROC
, &lseg
->pls_flags
);
1872 spin_unlock(&ino
->i_lock
);
1873 pnfs_free_lseg_list(&free_me
);
1877 spin_unlock(&ino
->i_lock
);
1878 lseg
->pls_layout
= lo
;
1879 NFS_SERVER(ino
)->pnfs_curr_ld
->free_lseg(lseg
);
1880 return ERR_PTR(-EAGAIN
);
1884 pnfs_set_plh_return_info(struct pnfs_layout_hdr
*lo
, enum pnfs_iomode iomode
,
1887 if (lo
->plh_return_iomode
!= 0 && lo
->plh_return_iomode
!= iomode
)
1888 iomode
= IOMODE_ANY
;
1889 lo
->plh_return_iomode
= iomode
;
1890 set_bit(NFS_LAYOUT_RETURN_REQUESTED
, &lo
->plh_flags
);
1892 WARN_ON_ONCE(lo
->plh_return_seq
!= 0 && lo
->plh_return_seq
!= seq
);
1893 lo
->plh_return_seq
= seq
;
1898 * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
1899 * @lo: pointer to layout header
1900 * @tmp_list: list header to be used with pnfs_free_lseg_list()
1901 * @return_range: describe layout segment ranges to be returned
1903 * This function is mainly intended for use by layoutrecall. It attempts
1904 * to free the layout segment immediately, or else to mark it for return
1905 * as soon as its reference count drops to zero.
1908 pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr
*lo
,
1909 struct list_head
*tmp_list
,
1910 const struct pnfs_layout_range
*return_range
,
1913 struct pnfs_layout_segment
*lseg
, *next
;
1916 dprintk("%s:Begin lo %p\n", __func__
, lo
);
1918 if (list_empty(&lo
->plh_segs
))
1921 assert_spin_locked(&lo
->plh_inode
->i_lock
);
1923 list_for_each_entry_safe(lseg
, next
, &lo
->plh_segs
, pls_list
)
1924 if (pnfs_match_lseg_recall(lseg
, return_range
, seq
)) {
1925 dprintk("%s: marking lseg %p iomode %d "
1926 "offset %llu length %llu\n", __func__
,
1927 lseg
, lseg
->pls_range
.iomode
,
1928 lseg
->pls_range
.offset
,
1929 lseg
->pls_range
.length
);
1930 if (mark_lseg_invalid(lseg
, tmp_list
))
1933 set_bit(NFS_LSEG_LAYOUTRETURN
, &lseg
->pls_flags
);
1937 pnfs_set_plh_return_info(lo
, return_range
->iomode
, seq
);
1942 void pnfs_error_mark_layout_for_return(struct inode
*inode
,
1943 struct pnfs_layout_segment
*lseg
)
1945 struct pnfs_layout_hdr
*lo
= NFS_I(inode
)->layout
;
1946 struct pnfs_layout_range range
= {
1947 .iomode
= lseg
->pls_range
.iomode
,
1949 .length
= NFS4_MAX_UINT64
,
1952 bool return_now
= false;
1954 spin_lock(&inode
->i_lock
);
1955 pnfs_set_plh_return_info(lo
, range
.iomode
, 0);
1956 /* Block LAYOUTGET */
1957 set_bit(NFS_LAYOUT_RETURN
, &lo
->plh_flags
);
1959 * mark all matching lsegs so that we are sure to have no live
1960 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
1963 if (!pnfs_mark_matching_lsegs_return(lo
, &free_me
, &range
, 0)) {
1964 nfs4_stateid stateid
;
1965 enum pnfs_iomode iomode
;
1967 return_now
= pnfs_prepare_layoutreturn(lo
, &stateid
, &iomode
);
1968 spin_unlock(&inode
->i_lock
);
1970 pnfs_send_layoutreturn(lo
, &stateid
, iomode
, false);
1972 spin_unlock(&inode
->i_lock
);
1973 nfs_commit_inode(inode
, 0);
1975 pnfs_free_lseg_list(&free_me
);
1977 EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return
);
1980 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor
*pgio
, struct nfs_page
*req
)
1982 u64 rd_size
= req
->wb_bytes
;
1984 if (pgio
->pg_lseg
== NULL
) {
1985 if (pgio
->pg_dreq
== NULL
)
1986 rd_size
= i_size_read(pgio
->pg_inode
) - req_offset(req
);
1988 rd_size
= nfs_dreq_bytes_left(pgio
->pg_dreq
);
1990 pgio
->pg_lseg
= pnfs_update_layout(pgio
->pg_inode
,
1997 if (IS_ERR(pgio
->pg_lseg
)) {
1998 pgio
->pg_error
= PTR_ERR(pgio
->pg_lseg
);
1999 pgio
->pg_lseg
= NULL
;
2003 /* If no lseg, fall back to read through mds */
2004 if (pgio
->pg_lseg
== NULL
)
2005 nfs_pageio_reset_read_mds(pgio
);
2008 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read
);
2011 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor
*pgio
,
2012 struct nfs_page
*req
, u64 wb_size
)
2014 if (pgio
->pg_lseg
== NULL
) {
2015 pgio
->pg_lseg
= pnfs_update_layout(pgio
->pg_inode
,
2022 if (IS_ERR(pgio
->pg_lseg
)) {
2023 pgio
->pg_error
= PTR_ERR(pgio
->pg_lseg
);
2024 pgio
->pg_lseg
= NULL
;
2028 /* If no lseg, fall back to write through mds */
2029 if (pgio
->pg_lseg
== NULL
)
2030 nfs_pageio_reset_write_mds(pgio
);
2032 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write
);
2035 pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor
*desc
)
2037 if (desc
->pg_lseg
) {
2038 pnfs_put_lseg(desc
->pg_lseg
);
2039 desc
->pg_lseg
= NULL
;
2042 EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup
);
2045 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
2046 * of bytes (maximum @req->wb_bytes) that can be coalesced.
2049 pnfs_generic_pg_test(struct nfs_pageio_descriptor
*pgio
,
2050 struct nfs_page
*prev
, struct nfs_page
*req
)
2053 u64 seg_end
, req_start
, seg_left
;
2055 size
= nfs_generic_pg_test(pgio
, prev
, req
);
2060 * 'size' contains the number of bytes left in the current page (up
2061 * to the original size asked for in @req->wb_bytes).
2063 * Calculate how many bytes are left in the layout segment
2064 * and if there are less bytes than 'size', return that instead.
2066 * Please also note that 'end_offset' is actually the offset of the
2067 * first byte that lies outside the pnfs_layout_range. FIXME?
2070 if (pgio
->pg_lseg
) {
2071 seg_end
= end_offset(pgio
->pg_lseg
->pls_range
.offset
,
2072 pgio
->pg_lseg
->pls_range
.length
);
2073 req_start
= req_offset(req
);
2074 WARN_ON_ONCE(req_start
>= seg_end
);
2075 /* start of request is past the last byte of this segment */
2076 if (req_start
>= seg_end
) {
2077 /* reference the new lseg */
2078 if (pgio
->pg_ops
->pg_cleanup
)
2079 pgio
->pg_ops
->pg_cleanup(pgio
);
2080 if (pgio
->pg_ops
->pg_init
)
2081 pgio
->pg_ops
->pg_init(pgio
, req
);
2085 /* adjust 'size' iff there are fewer bytes left in the
2086 * segment than what nfs_generic_pg_test returned */
2087 seg_left
= seg_end
- req_start
;
2088 if (seg_left
< size
)
2089 size
= (unsigned int)seg_left
;
2094 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test
);
2096 int pnfs_write_done_resend_to_mds(struct nfs_pgio_header
*hdr
)
2098 struct nfs_pageio_descriptor pgio
;
2100 /* Resend all requests through the MDS */
2101 nfs_pageio_init_write(&pgio
, hdr
->inode
, FLUSH_STABLE
, true,
2102 hdr
->completion_ops
);
2103 set_bit(NFS_CONTEXT_RESEND_WRITES
, &hdr
->args
.context
->flags
);
2104 return nfs_pageio_resend(&pgio
, hdr
);
2106 EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds
);
2108 static void pnfs_ld_handle_write_error(struct nfs_pgio_header
*hdr
)
2111 dprintk("pnfs write error = %d\n", hdr
->pnfs_error
);
2112 if (NFS_SERVER(hdr
->inode
)->pnfs_curr_ld
->flags
&
2113 PNFS_LAYOUTRET_ON_ERROR
) {
2114 pnfs_return_layout(hdr
->inode
);
2116 if (!test_and_set_bit(NFS_IOHDR_REDO
, &hdr
->flags
))
2117 hdr
->task
.tk_status
= pnfs_write_done_resend_to_mds(hdr
);
2121 * Called by non rpc-based layout drivers
2123 void pnfs_ld_write_done(struct nfs_pgio_header
*hdr
)
2125 if (likely(!hdr
->pnfs_error
)) {
2126 pnfs_set_layoutcommit(hdr
->inode
, hdr
->lseg
,
2127 hdr
->mds_offset
+ hdr
->res
.count
);
2128 hdr
->mds_ops
->rpc_call_done(&hdr
->task
, hdr
);
2130 trace_nfs4_pnfs_write(hdr
, hdr
->pnfs_error
);
2131 if (unlikely(hdr
->pnfs_error
))
2132 pnfs_ld_handle_write_error(hdr
);
2133 hdr
->mds_ops
->rpc_release(hdr
);
2135 EXPORT_SYMBOL_GPL(pnfs_ld_write_done
);
2138 pnfs_write_through_mds(struct nfs_pageio_descriptor
*desc
,
2139 struct nfs_pgio_header
*hdr
)
2141 struct nfs_pgio_mirror
*mirror
= nfs_pgio_current_mirror(desc
);
2143 if (!test_and_set_bit(NFS_IOHDR_REDO
, &hdr
->flags
)) {
2144 list_splice_tail_init(&hdr
->pages
, &mirror
->pg_list
);
2145 nfs_pageio_reset_write_mds(desc
);
2146 mirror
->pg_recoalesce
= 1;
2148 nfs_pgio_data_destroy(hdr
);
2152 static enum pnfs_try_status
2153 pnfs_try_to_write_data(struct nfs_pgio_header
*hdr
,
2154 const struct rpc_call_ops
*call_ops
,
2155 struct pnfs_layout_segment
*lseg
,
2158 struct inode
*inode
= hdr
->inode
;
2159 enum pnfs_try_status trypnfs
;
2160 struct nfs_server
*nfss
= NFS_SERVER(inode
);
2162 hdr
->mds_ops
= call_ops
;
2164 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__
,
2165 inode
->i_ino
, hdr
->args
.count
, hdr
->args
.offset
, how
);
2166 trypnfs
= nfss
->pnfs_curr_ld
->write_pagelist(hdr
, how
);
2167 if (trypnfs
!= PNFS_NOT_ATTEMPTED
)
2168 nfs_inc_stats(inode
, NFSIOS_PNFS_WRITE
);
2169 dprintk("%s End (trypnfs:%d)\n", __func__
, trypnfs
);
2174 pnfs_do_write(struct nfs_pageio_descriptor
*desc
,
2175 struct nfs_pgio_header
*hdr
, int how
)
2177 const struct rpc_call_ops
*call_ops
= desc
->pg_rpc_callops
;
2178 struct pnfs_layout_segment
*lseg
= desc
->pg_lseg
;
2179 enum pnfs_try_status trypnfs
;
2181 trypnfs
= pnfs_try_to_write_data(hdr
, call_ops
, lseg
, how
);
2182 if (trypnfs
== PNFS_NOT_ATTEMPTED
)
2183 pnfs_write_through_mds(desc
, hdr
);
2186 static void pnfs_writehdr_free(struct nfs_pgio_header
*hdr
)
2188 pnfs_put_lseg(hdr
->lseg
);
2189 nfs_pgio_header_free(hdr
);
2193 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor
*desc
)
2195 struct nfs_pgio_header
*hdr
;
2198 hdr
= nfs_pgio_header_alloc(desc
->pg_rw_ops
);
2200 desc
->pg_error
= -ENOMEM
;
2201 return desc
->pg_error
;
2203 nfs_pgheader_init(desc
, hdr
, pnfs_writehdr_free
);
2205 hdr
->lseg
= pnfs_get_lseg(desc
->pg_lseg
);
2206 ret
= nfs_generic_pgio(desc
, hdr
);
2208 pnfs_do_write(desc
, hdr
, desc
->pg_ioflags
);
2212 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages
);
2214 int pnfs_read_done_resend_to_mds(struct nfs_pgio_header
*hdr
)
2216 struct nfs_pageio_descriptor pgio
;
2218 /* Resend all requests through the MDS */
2219 nfs_pageio_init_read(&pgio
, hdr
->inode
, true, hdr
->completion_ops
);
2220 return nfs_pageio_resend(&pgio
, hdr
);
2222 EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds
);
2224 static void pnfs_ld_handle_read_error(struct nfs_pgio_header
*hdr
)
2226 dprintk("pnfs read error = %d\n", hdr
->pnfs_error
);
2227 if (NFS_SERVER(hdr
->inode
)->pnfs_curr_ld
->flags
&
2228 PNFS_LAYOUTRET_ON_ERROR
) {
2229 pnfs_return_layout(hdr
->inode
);
2231 if (!test_and_set_bit(NFS_IOHDR_REDO
, &hdr
->flags
))
2232 hdr
->task
.tk_status
= pnfs_read_done_resend_to_mds(hdr
);
2236 * Called by non rpc-based layout drivers
2238 void pnfs_ld_read_done(struct nfs_pgio_header
*hdr
)
2240 if (likely(!hdr
->pnfs_error
))
2241 hdr
->mds_ops
->rpc_call_done(&hdr
->task
, hdr
);
2242 trace_nfs4_pnfs_read(hdr
, hdr
->pnfs_error
);
2243 if (unlikely(hdr
->pnfs_error
))
2244 pnfs_ld_handle_read_error(hdr
);
2245 hdr
->mds_ops
->rpc_release(hdr
);
2247 EXPORT_SYMBOL_GPL(pnfs_ld_read_done
);
2250 pnfs_read_through_mds(struct nfs_pageio_descriptor
*desc
,
2251 struct nfs_pgio_header
*hdr
)
2253 struct nfs_pgio_mirror
*mirror
= nfs_pgio_current_mirror(desc
);
2255 if (!test_and_set_bit(NFS_IOHDR_REDO
, &hdr
->flags
)) {
2256 list_splice_tail_init(&hdr
->pages
, &mirror
->pg_list
);
2257 nfs_pageio_reset_read_mds(desc
);
2258 mirror
->pg_recoalesce
= 1;
2260 nfs_pgio_data_destroy(hdr
);
2265 * Call the appropriate parallel I/O subsystem read function.
2267 static enum pnfs_try_status
2268 pnfs_try_to_read_data(struct nfs_pgio_header
*hdr
,
2269 const struct rpc_call_ops
*call_ops
,
2270 struct pnfs_layout_segment
*lseg
)
2272 struct inode
*inode
= hdr
->inode
;
2273 struct nfs_server
*nfss
= NFS_SERVER(inode
);
2274 enum pnfs_try_status trypnfs
;
2276 hdr
->mds_ops
= call_ops
;
2278 dprintk("%s: Reading ino:%lu %u@%llu\n",
2279 __func__
, inode
->i_ino
, hdr
->args
.count
, hdr
->args
.offset
);
2281 trypnfs
= nfss
->pnfs_curr_ld
->read_pagelist(hdr
);
2282 if (trypnfs
!= PNFS_NOT_ATTEMPTED
)
2283 nfs_inc_stats(inode
, NFSIOS_PNFS_READ
);
2284 dprintk("%s End (trypnfs:%d)\n", __func__
, trypnfs
);
2288 /* Resend all requests through pnfs. */
2289 void pnfs_read_resend_pnfs(struct nfs_pgio_header
*hdr
)
2291 struct nfs_pageio_descriptor pgio
;
2293 if (!test_and_set_bit(NFS_IOHDR_REDO
, &hdr
->flags
)) {
2294 /* Prevent deadlocks with layoutreturn! */
2295 pnfs_put_lseg(hdr
->lseg
);
2298 nfs_pageio_init_read(&pgio
, hdr
->inode
, false,
2299 hdr
->completion_ops
);
2300 hdr
->task
.tk_status
= nfs_pageio_resend(&pgio
, hdr
);
2303 EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs
);
2306 pnfs_do_read(struct nfs_pageio_descriptor
*desc
, struct nfs_pgio_header
*hdr
)
2308 const struct rpc_call_ops
*call_ops
= desc
->pg_rpc_callops
;
2309 struct pnfs_layout_segment
*lseg
= desc
->pg_lseg
;
2310 enum pnfs_try_status trypnfs
;
2312 trypnfs
= pnfs_try_to_read_data(hdr
, call_ops
, lseg
);
2313 if (trypnfs
== PNFS_TRY_AGAIN
)
2314 pnfs_read_resend_pnfs(hdr
);
2315 if (trypnfs
== PNFS_NOT_ATTEMPTED
|| hdr
->task
.tk_status
)
2316 pnfs_read_through_mds(desc
, hdr
);
2319 static void pnfs_readhdr_free(struct nfs_pgio_header
*hdr
)
2321 pnfs_put_lseg(hdr
->lseg
);
2322 nfs_pgio_header_free(hdr
);
2326 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor
*desc
)
2328 struct nfs_pgio_header
*hdr
;
2331 hdr
= nfs_pgio_header_alloc(desc
->pg_rw_ops
);
2333 desc
->pg_error
= -ENOMEM
;
2334 return desc
->pg_error
;
2336 nfs_pgheader_init(desc
, hdr
, pnfs_readhdr_free
);
2337 hdr
->lseg
= pnfs_get_lseg(desc
->pg_lseg
);
2338 ret
= nfs_generic_pgio(desc
, hdr
);
2340 pnfs_do_read(desc
, hdr
);
2343 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages
);
2345 static void pnfs_clear_layoutcommitting(struct inode
*inode
)
2347 unsigned long *bitlock
= &NFS_I(inode
)->flags
;
2349 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING
, bitlock
);
2350 smp_mb__after_atomic();
2351 wake_up_bit(bitlock
, NFS_INO_LAYOUTCOMMITTING
);
2355 * There can be multiple RW segments.
2357 static void pnfs_list_write_lseg(struct inode
*inode
, struct list_head
*listp
)
2359 struct pnfs_layout_segment
*lseg
;
2361 list_for_each_entry(lseg
, &NFS_I(inode
)->layout
->plh_segs
, pls_list
) {
2362 if (lseg
->pls_range
.iomode
== IOMODE_RW
&&
2363 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT
, &lseg
->pls_flags
))
2364 list_add(&lseg
->pls_lc_list
, listp
);
2368 static void pnfs_list_write_lseg_done(struct inode
*inode
, struct list_head
*listp
)
2370 struct pnfs_layout_segment
*lseg
, *tmp
;
2372 /* Matched by references in pnfs_set_layoutcommit */
2373 list_for_each_entry_safe(lseg
, tmp
, listp
, pls_lc_list
) {
2374 list_del_init(&lseg
->pls_lc_list
);
2375 pnfs_put_lseg(lseg
);
2378 pnfs_clear_layoutcommitting(inode
);
2381 void pnfs_set_lo_fail(struct pnfs_layout_segment
*lseg
)
2383 pnfs_layout_io_set_failed(lseg
->pls_layout
, lseg
->pls_range
.iomode
);
2385 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail
);
2388 pnfs_set_layoutcommit(struct inode
*inode
, struct pnfs_layout_segment
*lseg
,
2391 struct nfs_inode
*nfsi
= NFS_I(inode
);
2392 bool mark_as_dirty
= false;
2394 spin_lock(&inode
->i_lock
);
2395 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT
, &nfsi
->flags
)) {
2396 nfsi
->layout
->plh_lwb
= end_pos
;
2397 mark_as_dirty
= true;
2398 dprintk("%s: Set layoutcommit for inode %lu ",
2399 __func__
, inode
->i_ino
);
2400 } else if (end_pos
> nfsi
->layout
->plh_lwb
)
2401 nfsi
->layout
->plh_lwb
= end_pos
;
2402 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT
, &lseg
->pls_flags
)) {
2403 /* references matched in nfs4_layoutcommit_release */
2404 pnfs_get_lseg(lseg
);
2406 spin_unlock(&inode
->i_lock
);
2407 dprintk("%s: lseg %p end_pos %llu\n",
2408 __func__
, lseg
, nfsi
->layout
->plh_lwb
);
2410 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2411 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2413 mark_inode_dirty_sync(inode
);
2415 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit
);
2417 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data
*data
)
2419 struct nfs_server
*nfss
= NFS_SERVER(data
->args
.inode
);
2421 if (nfss
->pnfs_curr_ld
->cleanup_layoutcommit
)
2422 nfss
->pnfs_curr_ld
->cleanup_layoutcommit(data
);
2423 pnfs_list_write_lseg_done(data
->args
.inode
, &data
->lseg_list
);
2427 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2428 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2429 * data to disk to allow the server to recover the data if it crashes.
2430 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2431 * is off, and a COMMIT is sent to a data server, or
2432 * if WRITEs to a data server return NFS_DATA_SYNC.
2435 pnfs_layoutcommit_inode(struct inode
*inode
, bool sync
)
2437 struct pnfs_layoutdriver_type
*ld
= NFS_SERVER(inode
)->pnfs_curr_ld
;
2438 struct nfs4_layoutcommit_data
*data
;
2439 struct nfs_inode
*nfsi
= NFS_I(inode
);
2443 if (!pnfs_layoutcommit_outstanding(inode
))
2446 dprintk("--> %s inode %lu\n", __func__
, inode
->i_ino
);
2449 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING
, &nfsi
->flags
)) {
2452 status
= wait_on_bit_lock_action(&nfsi
->flags
,
2453 NFS_INO_LAYOUTCOMMITTING
,
2454 nfs_wait_bit_killable
,
2461 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2462 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
2464 goto clear_layoutcommitting
;
2467 spin_lock(&inode
->i_lock
);
2468 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT
, &nfsi
->flags
))
2471 INIT_LIST_HEAD(&data
->lseg_list
);
2472 pnfs_list_write_lseg(inode
, &data
->lseg_list
);
2474 end_pos
= nfsi
->layout
->plh_lwb
;
2476 nfs4_stateid_copy(&data
->args
.stateid
, &nfsi
->layout
->plh_stateid
);
2477 spin_unlock(&inode
->i_lock
);
2479 data
->args
.inode
= inode
;
2480 data
->cred
= get_rpccred(nfsi
->layout
->plh_lc_cred
);
2481 nfs_fattr_init(&data
->fattr
);
2482 data
->args
.bitmask
= NFS_SERVER(inode
)->cache_consistency_bitmask
;
2483 data
->res
.fattr
= &data
->fattr
;
2485 data
->args
.lastbytewritten
= end_pos
- 1;
2487 data
->args
.lastbytewritten
= U64_MAX
;
2488 data
->res
.server
= NFS_SERVER(inode
);
2490 if (ld
->prepare_layoutcommit
) {
2491 status
= ld
->prepare_layoutcommit(&data
->args
);
2493 put_rpccred(data
->cred
);
2494 spin_lock(&inode
->i_lock
);
2495 set_bit(NFS_INO_LAYOUTCOMMIT
, &nfsi
->flags
);
2496 if (end_pos
> nfsi
->layout
->plh_lwb
)
2497 nfsi
->layout
->plh_lwb
= end_pos
;
2503 status
= nfs4_proc_layoutcommit(data
, sync
);
2506 mark_inode_dirty_sync(inode
);
2507 dprintk("<-- %s status %d\n", __func__
, status
);
2510 spin_unlock(&inode
->i_lock
);
2512 clear_layoutcommitting
:
2513 pnfs_clear_layoutcommitting(inode
);
2516 EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode
);
2519 pnfs_generic_sync(struct inode
*inode
, bool datasync
)
2521 return pnfs_layoutcommit_inode(inode
, true);
2523 EXPORT_SYMBOL_GPL(pnfs_generic_sync
);
2525 struct nfs4_threshold
*pnfs_mdsthreshold_alloc(void)
2527 struct nfs4_threshold
*thp
;
2529 thp
= kzalloc(sizeof(*thp
), GFP_NOFS
);
2531 dprintk("%s mdsthreshold allocation failed\n", __func__
);
2537 #if IS_ENABLED(CONFIG_NFS_V4_2)
2539 pnfs_report_layoutstat(struct inode
*inode
, gfp_t gfp_flags
)
2541 struct pnfs_layoutdriver_type
*ld
= NFS_SERVER(inode
)->pnfs_curr_ld
;
2542 struct nfs_server
*server
= NFS_SERVER(inode
);
2543 struct nfs_inode
*nfsi
= NFS_I(inode
);
2544 struct nfs42_layoutstat_data
*data
;
2545 struct pnfs_layout_hdr
*hdr
;
2548 if (!pnfs_enabled_sb(server
) || !ld
->prepare_layoutstats
)
2551 if (!nfs_server_capable(inode
, NFS_CAP_LAYOUTSTATS
))
2554 if (test_and_set_bit(NFS_INO_LAYOUTSTATS
, &nfsi
->flags
))
2557 spin_lock(&inode
->i_lock
);
2558 if (!NFS_I(inode
)->layout
) {
2559 spin_unlock(&inode
->i_lock
);
2560 goto out_clear_layoutstats
;
2562 hdr
= NFS_I(inode
)->layout
;
2563 pnfs_get_layout_hdr(hdr
);
2564 spin_unlock(&inode
->i_lock
);
2566 data
= kzalloc(sizeof(*data
), gfp_flags
);
2572 data
->args
.fh
= NFS_FH(inode
);
2573 data
->args
.inode
= inode
;
2574 status
= ld
->prepare_layoutstats(&data
->args
);
2578 status
= nfs42_proc_layoutstats_generic(NFS_SERVER(inode
), data
);
2581 dprintk("%s returns %d\n", __func__
, status
);
2587 pnfs_put_layout_hdr(hdr
);
2588 out_clear_layoutstats
:
2589 smp_mb__before_atomic();
2590 clear_bit(NFS_INO_LAYOUTSTATS
, &nfsi
->flags
);
2591 smp_mb__after_atomic();
2594 EXPORT_SYMBOL_GPL(pnfs_report_layoutstat
);
2597 unsigned int layoutstats_timer
;
2598 module_param(layoutstats_timer
, uint
, 0644);
2599 EXPORT_SYMBOL_GPL(layoutstats_timer
);