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 struct pnfs_layout_range range = {
1074 .iomode = IOMODE_ANY,
1076 .length = NFS4_MAX_UINT64,
1078 LIST_HEAD(tmp_list);
1079 nfs4_stateid stateid;
1080 int status = 0, empty;
1083 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
1085 spin_lock(&ino->i_lock);
1088 spin_unlock(&ino->i_lock);
1089 dprintk("NFS: %s no layout to return\n", __func__);
1092 /* Reference matched in nfs4_layoutreturn_release */
1093 pnfs_get_layout_hdr(lo);
1094 empty = list_empty(&lo->plh_segs);
1095 pnfs_clear_layoutcommit(ino, &tmp_list);
1096 pnfs_mark_matching_lsegs_return(lo, &tmp_list, &range, 0);
1098 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range)
1099 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1101 /* Don't send a LAYOUTRETURN if list was initially empty */
1103 spin_unlock(&ino->i_lock);
1104 dprintk("NFS: %s no layout segments to return\n", __func__);
1105 goto out_put_layout_hdr;
1108 send = pnfs_prepare_layoutreturn(lo, &stateid, NULL);
1109 spin_unlock(&ino->i_lock);
1110 pnfs_free_lseg_list(&tmp_list);
1112 status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
1114 pnfs_put_layout_hdr(lo);
1116 dprintk("<-- %s status: %d\n", __func__, status);
1119 EXPORT_SYMBOL_GPL(_pnfs_return_layout);
1122 pnfs_commit_and_return_layout(struct inode *inode)
1124 struct pnfs_layout_hdr *lo;
1127 spin_lock(&inode->i_lock);
1128 lo = NFS_I(inode)->layout;
1130 spin_unlock(&inode->i_lock);
1133 pnfs_get_layout_hdr(lo);
1134 /* Block new layoutgets and read/write to ds */
1135 lo->plh_block_lgets++;
1136 spin_unlock(&inode->i_lock);
1137 filemap_fdatawait(inode->i_mapping);
1138 ret = pnfs_layoutcommit_inode(inode, true);
1140 ret = _pnfs_return_layout(inode);
1141 spin_lock(&inode->i_lock);
1142 lo->plh_block_lgets--;
1143 spin_unlock(&inode->i_lock);
1144 pnfs_put_layout_hdr(lo);
1148 bool pnfs_roc(struct inode *ino)
1150 struct nfs_inode *nfsi = NFS_I(ino);
1151 struct nfs_open_context *ctx;
1152 struct nfs4_state *state;
1153 struct pnfs_layout_hdr *lo;
1154 struct pnfs_layout_segment *lseg, *tmp;
1155 nfs4_stateid stateid;
1156 LIST_HEAD(tmp_list);
1157 bool found = false, layoutreturn = false, roc = false;
1159 spin_lock(&ino->i_lock);
1161 if (!lo || test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
1164 /* no roc if we hold a delegation */
1165 if (nfs4_check_delegation(ino, FMODE_READ))
1168 list_for_each_entry(ctx, &nfsi->open_files, list) {
1170 /* Don't return layout if there is open file state */
1171 if (state != NULL && state->state != 0)
1175 /* always send layoutreturn if being marked so */
1176 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1177 layoutreturn = pnfs_prepare_layoutreturn(lo,
1180 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
1181 /* If we are sending layoutreturn, invalidate all valid lsegs */
1182 if (layoutreturn || test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
1183 mark_lseg_invalid(lseg, &tmp_list);
1186 /* ROC in two conditions:
1187 * 1. there are ROC lsegs
1188 * 2. we don't send layoutreturn
1190 if (found && !layoutreturn) {
1191 /* lo ref dropped in pnfs_roc_release() */
1192 pnfs_get_layout_hdr(lo);
1197 spin_unlock(&ino->i_lock);
1198 pnfs_free_lseg_list(&tmp_list);
1199 pnfs_layoutcommit_inode(ino, true);
1201 pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
1205 void pnfs_roc_release(struct inode *ino)
1207 struct pnfs_layout_hdr *lo;
1209 spin_lock(&ino->i_lock);
1210 lo = NFS_I(ino)->layout;
1211 pnfs_clear_layoutreturn_waitbit(lo);
1212 if (atomic_dec_and_test(&lo->plh_refcount)) {
1213 pnfs_detach_layout_hdr(lo);
1214 spin_unlock(&ino->i_lock);
1215 pnfs_free_layout_hdr(lo);
1217 spin_unlock(&ino->i_lock);
1220 void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
1222 struct pnfs_layout_hdr *lo;
1224 spin_lock(&ino->i_lock);
1225 lo = NFS_I(ino)->layout;
1226 if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
1227 lo->plh_barrier = barrier;
1228 spin_unlock(&ino->i_lock);
1229 trace_nfs4_layoutreturn_on_close(ino, 0);
1232 void pnfs_roc_get_barrier(struct inode *ino, u32 *barrier)
1234 struct nfs_inode *nfsi = NFS_I(ino);
1235 struct pnfs_layout_hdr *lo;
1238 spin_lock(&ino->i_lock);
1240 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
1242 /* Since close does not return a layout stateid for use as
1243 * a barrier, we choose the worst-case barrier.
1245 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
1246 spin_unlock(&ino->i_lock);
1249 bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
1251 struct nfs_inode *nfsi = NFS_I(ino);
1252 struct pnfs_layout_hdr *lo;
1255 /* we might not have grabbed lo reference. so need to check under
1257 spin_lock(&ino->i_lock);
1259 if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1260 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
1263 spin_unlock(&ino->i_lock);
1268 * Compare two layout segments for sorting into layout cache.
1269 * We want to preferentially return RW over RO layouts, so ensure those
1273 pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
1274 const struct pnfs_layout_range *l2)
1278 /* high offset > low offset */
1279 d = l1->offset - l2->offset;
1283 /* short length > long length */
1284 d = l2->length - l1->length;
1288 /* read > read/write */
1289 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
1293 pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
1294 const struct pnfs_layout_range *l2)
1296 return pnfs_lseg_range_cmp(l1, l2) > 0;
1300 pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
1301 struct pnfs_layout_segment *old)
1307 pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1308 struct pnfs_layout_segment *lseg,
1309 bool (*is_after)(const struct pnfs_layout_range *,
1310 const struct pnfs_layout_range *),
1311 bool (*do_merge)(struct pnfs_layout_segment *,
1312 struct pnfs_layout_segment *),
1313 struct list_head *free_me)
1315 struct pnfs_layout_segment *lp, *tmp;
1317 dprintk("%s:Begin\n", __func__);
1319 list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
1320 if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
1322 if (do_merge(lseg, lp)) {
1323 mark_lseg_invalid(lp, free_me);
1326 if (is_after(&lseg->pls_range, &lp->pls_range))
1328 list_add_tail(&lseg->pls_list, &lp->pls_list);
1329 dprintk("%s: inserted lseg %p "
1330 "iomode %d offset %llu length %llu before "
1331 "lp %p iomode %d offset %llu length %llu\n",
1332 __func__, lseg, lseg->pls_range.iomode,
1333 lseg->pls_range.offset, lseg->pls_range.length,
1334 lp, lp->pls_range.iomode, lp->pls_range.offset,
1335 lp->pls_range.length);
1338 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1339 dprintk("%s: inserted lseg %p "
1340 "iomode %d offset %llu length %llu at tail\n",
1341 __func__, lseg, lseg->pls_range.iomode,
1342 lseg->pls_range.offset, lseg->pls_range.length);
1344 pnfs_get_layout_hdr(lo);
1346 dprintk("%s:Return\n", __func__);
1348 EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
1351 pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1352 struct pnfs_layout_segment *lseg,
1353 struct list_head *free_me)
1355 struct inode *inode = lo->plh_inode;
1356 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
1358 if (ld->add_lseg != NULL)
1359 ld->add_lseg(lo, lseg, free_me);
1361 pnfs_generic_layout_insert_lseg(lo, lseg,
1362 pnfs_lseg_range_is_after,
1367 static struct pnfs_layout_hdr *
1368 alloc_init_layout_hdr(struct inode *ino,
1369 struct nfs_open_context *ctx,
1372 struct pnfs_layout_hdr *lo;
1374 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
1377 atomic_set(&lo->plh_refcount, 1);
1378 INIT_LIST_HEAD(&lo->plh_layouts);
1379 INIT_LIST_HEAD(&lo->plh_segs);
1380 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
1381 lo->plh_inode = ino;
1382 lo->plh_lc_cred = get_rpccred(ctx->cred);
1383 lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
1387 static struct pnfs_layout_hdr *
1388 pnfs_find_alloc_layout(struct inode *ino,
1389 struct nfs_open_context *ctx,
1391 __releases(&ino->i_lock)
1392 __acquires(&ino->i_lock)
1394 struct nfs_inode *nfsi = NFS_I(ino);
1395 struct pnfs_layout_hdr *new = NULL;
1397 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1399 if (nfsi->layout != NULL)
1401 spin_unlock(&ino->i_lock);
1402 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
1403 spin_lock(&ino->i_lock);
1405 if (likely(nfsi->layout == NULL)) { /* Won the race? */
1408 } else if (new != NULL)
1409 pnfs_free_layout_hdr(new);
1411 pnfs_get_layout_hdr(nfsi->layout);
1412 return nfsi->layout;
1416 * iomode matching rules:
1417 * iomode lseg strict match
1419 * ----- ----- ------ -----
1424 * READ READ N/A true
1425 * READ RW true false
1426 * READ RW false true
1429 pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
1430 const struct pnfs_layout_range *range,
1433 struct pnfs_layout_range range1;
1435 if ((range->iomode == IOMODE_RW &&
1436 ls_range->iomode != IOMODE_RW) ||
1437 (range->iomode != ls_range->iomode &&
1439 !pnfs_lseg_range_intersecting(ls_range, range))
1442 /* range1 covers only the first byte in the range */
1445 return pnfs_lseg_range_contained(ls_range, &range1);
1449 * lookup range in layout
1451 static struct pnfs_layout_segment *
1452 pnfs_find_lseg(struct pnfs_layout_hdr *lo,
1453 struct pnfs_layout_range *range,
1456 struct pnfs_layout_segment *lseg, *ret = NULL;
1458 dprintk("%s:Begin\n", __func__);
1460 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
1461 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
1462 !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
1463 pnfs_lseg_range_match(&lseg->pls_range, range,
1465 ret = pnfs_get_lseg(lseg);
1470 dprintk("%s:Return lseg %p ref %d\n",
1471 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
1476 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1477 * to the MDS or over pNFS
1479 * The nfs_inode read_io and write_io fields are cumulative counters reset
1480 * when there are no layout segments. Note that in pnfs_update_layout iomode
1481 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1484 * A return of true means use MDS I/O.
1487 * If a file's size is smaller than the file size threshold, data accesses
1488 * SHOULD be sent to the metadata server. If an I/O request has a length that
1489 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1490 * server. If both file size and I/O size are provided, the client SHOULD
1491 * reach or exceed both thresholds before sending its read or write
1492 * requests to the data server.
1494 static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1495 struct inode *ino, int iomode)
1497 struct nfs4_threshold *t = ctx->mdsthreshold;
1498 struct nfs_inode *nfsi = NFS_I(ino);
1499 loff_t fsize = i_size_read(ino);
1500 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1505 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1506 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1510 if (t->bm & THRESHOLD_RD) {
1511 dprintk("%s fsize %llu\n", __func__, fsize);
1513 if (fsize < t->rd_sz)
1516 if (t->bm & THRESHOLD_RD_IO) {
1517 dprintk("%s nfsi->read_io %llu\n", __func__,
1520 if (nfsi->read_io < t->rd_io_sz)
1525 if (t->bm & THRESHOLD_WR) {
1526 dprintk("%s fsize %llu\n", __func__, fsize);
1528 if (fsize < t->wr_sz)
1531 if (t->bm & THRESHOLD_WR_IO) {
1532 dprintk("%s nfsi->write_io %llu\n", __func__,
1535 if (nfsi->write_io < t->wr_io_sz)
1540 if (size_set && io_set) {
1543 } else if (size || io)
1546 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1550 static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1553 * send layoutcommit as it can hold up layoutreturn due to lseg
1556 pnfs_layoutcommit_inode(lo->plh_inode, false);
1557 return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
1558 nfs_wait_bit_killable,
1559 TASK_UNINTERRUPTIBLE);
1562 static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1564 unsigned long *bitlock = &lo->plh_flags;
1566 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1567 smp_mb__after_atomic();
1568 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1572 * Layout segment is retreived from the server if not cached.
1573 * The appropriate layout segment is referenced and returned to the caller.
1575 struct pnfs_layout_segment *
1576 pnfs_update_layout(struct inode *ino,
1577 struct nfs_open_context *ctx,
1580 enum pnfs_iomode iomode,
1584 struct pnfs_layout_range arg = {
1589 unsigned pg_offset, seq;
1590 struct nfs_server *server = NFS_SERVER(ino);
1591 struct nfs_client *clp = server->nfs_client;
1592 struct pnfs_layout_hdr *lo = NULL;
1593 struct pnfs_layout_segment *lseg = NULL;
1594 nfs4_stateid stateid;
1596 unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
1599 if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
1600 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1601 PNFS_UPDATE_LAYOUT_NO_PNFS);
1605 if (iomode == IOMODE_READ && i_size_read(ino) == 0) {
1606 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1607 PNFS_UPDATE_LAYOUT_RD_ZEROLEN);
1611 if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
1612 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1613 PNFS_UPDATE_LAYOUT_MDSTHRESH);
1618 nfs4_client_recover_expired_lease(clp);
1620 spin_lock(&ino->i_lock);
1621 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
1623 spin_unlock(&ino->i_lock);
1624 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1625 PNFS_UPDATE_LAYOUT_NOMEM);
1629 /* Do we even need to bother with this? */
1630 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1631 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1632 PNFS_UPDATE_LAYOUT_BULK_RECALL);
1633 dprintk("%s matches recall, use MDS\n", __func__);
1637 /* if LAYOUTGET already failed once we don't try again */
1638 if (pnfs_layout_io_test_failed(lo, iomode)) {
1639 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1640 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
1644 lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
1646 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1647 PNFS_UPDATE_LAYOUT_FOUND_CACHED);
1651 if (!nfs4_valid_open_stateid(ctx->state)) {
1652 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1653 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
1658 * Choose a stateid for the LAYOUTGET. If we don't have a layout
1659 * stateid, or it has been invalidated, then we must use the open
1662 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
1665 * The first layoutget for the file. Need to serialize per
1666 * RFC 5661 Errata 3208.
1668 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1670 spin_unlock(&ino->i_lock);
1671 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
1672 TASK_UNINTERRUPTIBLE);
1673 pnfs_put_layout_hdr(lo);
1674 dprintk("%s retrying\n", __func__);
1680 seq = read_seqbegin(&ctx->state->seqlock);
1681 nfs4_stateid_copy(&stateid, &ctx->state->stateid);
1682 } while (read_seqretry(&ctx->state->seqlock, seq));
1684 nfs4_stateid_copy(&stateid, &lo->plh_stateid);
1688 * Because we free lsegs before sending LAYOUTRETURN, we need to wait
1689 * for LAYOUTRETURN even if first is true.
1691 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1692 spin_unlock(&ino->i_lock);
1693 dprintk("%s wait for layoutreturn\n", __func__);
1694 if (pnfs_prepare_to_retry_layoutget(lo)) {
1696 pnfs_clear_first_layoutget(lo);
1697 pnfs_put_layout_hdr(lo);
1698 dprintk("%s retrying\n", __func__);
1699 trace_pnfs_update_layout(ino, pos, count, iomode, lo,
1700 lseg, PNFS_UPDATE_LAYOUT_RETRY);
1703 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1704 PNFS_UPDATE_LAYOUT_RETURN);
1705 goto out_put_layout_hdr;
1708 if (pnfs_layoutgets_blocked(lo)) {
1709 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1710 PNFS_UPDATE_LAYOUT_BLOCKED);
1713 atomic_inc(&lo->plh_outstanding);
1714 spin_unlock(&ino->i_lock);
1716 if (list_empty(&lo->plh_layouts)) {
1717 /* The lo must be on the clp list if there is any
1718 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1720 spin_lock(&clp->cl_lock);
1721 if (list_empty(&lo->plh_layouts))
1722 list_add_tail(&lo->plh_layouts, &server->layouts);
1723 spin_unlock(&clp->cl_lock);
1726 pg_offset = arg.offset & ~PAGE_MASK;
1728 arg.offset -= pg_offset;
1729 arg.length += pg_offset;
1731 if (arg.length != NFS4_MAX_UINT64)
1732 arg.length = PAGE_ALIGN(arg.length);
1734 lseg = send_layoutget(lo, ctx, &stateid, &arg, &timeout, gfp_flags);
1735 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1736 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
1737 atomic_dec(&lo->plh_outstanding);
1739 switch(PTR_ERR(lseg)) {
1741 if (time_after(jiffies, giveup))
1744 case -ERECALLCONFLICT:
1745 /* Huh? We hold no layouts, how is there a recall? */
1750 /* Destroy the existing layout and start over */
1751 if (time_after(jiffies, giveup))
1752 pnfs_destroy_layout(NFS_I(ino));
1757 /* The server returned NFS4ERR_LAYOUTUNAVAILABLE */
1758 pnfs_layout_set_fail_bit(
1759 lo, pnfs_iomode_to_fail_bit(iomode));
1761 goto out_put_layout_hdr;
1763 if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
1764 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1767 goto out_put_layout_hdr;
1771 pnfs_clear_first_layoutget(lo);
1772 trace_pnfs_update_layout(ino, pos, count,
1773 iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
1774 pnfs_put_layout_hdr(lo);
1778 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1783 pnfs_clear_first_layoutget(lo);
1784 pnfs_put_layout_hdr(lo);
1786 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1787 "(%s, offset: %llu, length: %llu)\n",
1788 __func__, ino->i_sb->s_id,
1789 (unsigned long long)NFS_FILEID(ino),
1790 IS_ERR_OR_NULL(lseg) ? "not found" : "found",
1791 iomode==IOMODE_RW ? "read/write" : "read-only",
1792 (unsigned long long)pos,
1793 (unsigned long long)count);
1796 spin_unlock(&ino->i_lock);
1797 goto out_put_layout_hdr;
1799 EXPORT_SYMBOL_GPL(pnfs_update_layout);
1802 pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
1804 switch (range->iomode) {
1811 if (range->offset == NFS4_MAX_UINT64)
1813 if (range->length == 0)
1815 if (range->length != NFS4_MAX_UINT64 &&
1816 range->length > NFS4_MAX_UINT64 - range->offset)
1821 struct pnfs_layout_segment *
1822 pnfs_layout_process(struct nfs4_layoutget *lgp)
1824 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1825 struct nfs4_layoutget_res *res = &lgp->res;
1826 struct pnfs_layout_segment *lseg;
1827 struct inode *ino = lo->plh_inode;
1830 if (!pnfs_sanity_check_layout_range(&res->range))
1831 return ERR_PTR(-EINVAL);
1833 /* Inject layout blob into I/O device driver */
1834 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
1835 if (IS_ERR_OR_NULL(lseg)) {
1837 lseg = ERR_PTR(-ENOMEM);
1839 dprintk("%s: Could not allocate layout: error %ld\n",
1840 __func__, PTR_ERR(lseg));
1844 pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
1846 spin_lock(&ino->i_lock);
1847 if (pnfs_layoutgets_blocked(lo)) {
1848 dprintk("%s forget reply due to state\n", __func__);
1852 if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
1853 /* existing state ID, make sure the sequence number matches. */
1854 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1855 dprintk("%s forget reply due to sequence\n", __func__);
1858 pnfs_set_layout_stateid(lo, &res->stateid, false);
1861 * We got an entirely new state ID. Mark all segments for the
1862 * inode invalid, and don't bother validating the stateid
1865 pnfs_mark_layout_stateid_invalid(lo, &free_me);
1867 pnfs_set_layout_stateid(lo, &res->stateid, true);
1870 pnfs_get_lseg(lseg);
1871 pnfs_layout_insert_lseg(lo, lseg, &free_me);
1874 if (res->return_on_close)
1875 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1877 spin_unlock(&ino->i_lock);
1878 pnfs_free_lseg_list(&free_me);
1882 spin_unlock(&ino->i_lock);
1883 lseg->pls_layout = lo;
1884 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1885 return ERR_PTR(-EAGAIN);
1889 pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
1892 if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
1893 iomode = IOMODE_ANY;
1894 lo->plh_return_iomode = iomode;
1895 set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
1897 WARN_ON_ONCE(lo->plh_return_seq != 0 && lo->plh_return_seq != seq);
1898 lo->plh_return_seq = seq;
1903 * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
1904 * @lo: pointer to layout header
1905 * @tmp_list: list header to be used with pnfs_free_lseg_list()
1906 * @return_range: describe layout segment ranges to be returned
1908 * This function is mainly intended for use by layoutrecall. It attempts
1909 * to free the layout segment immediately, or else to mark it for return
1910 * as soon as its reference count drops to zero.
1913 pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
1914 struct list_head *tmp_list,
1915 const struct pnfs_layout_range *return_range,
1918 struct pnfs_layout_segment *lseg, *next;
1921 dprintk("%s:Begin lo %p\n", __func__, lo);
1923 if (list_empty(&lo->plh_segs))
1926 assert_spin_locked(&lo->plh_inode->i_lock);
1928 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
1929 if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
1930 dprintk("%s: marking lseg %p iomode %d "
1931 "offset %llu length %llu\n", __func__,
1932 lseg, lseg->pls_range.iomode,
1933 lseg->pls_range.offset,
1934 lseg->pls_range.length);
1935 if (mark_lseg_invalid(lseg, tmp_list))
1938 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
1942 pnfs_set_plh_return_info(lo, return_range->iomode, seq);
1947 void pnfs_error_mark_layout_for_return(struct inode *inode,
1948 struct pnfs_layout_segment *lseg)
1950 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
1951 struct pnfs_layout_range range = {
1952 .iomode = lseg->pls_range.iomode,
1954 .length = NFS4_MAX_UINT64,
1957 bool return_now = false;
1959 spin_lock(&inode->i_lock);
1960 pnfs_set_plh_return_info(lo, range.iomode, 0);
1962 * mark all matching lsegs so that we are sure to have no live
1963 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
1966 if (!pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0)) {
1967 nfs4_stateid stateid;
1968 enum pnfs_iomode iomode;
1970 return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
1971 spin_unlock(&inode->i_lock);
1973 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1975 spin_unlock(&inode->i_lock);
1976 nfs_commit_inode(inode, 0);
1978 pnfs_free_lseg_list(&free_me);
1980 EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
1983 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1985 u64 rd_size = req->wb_bytes;
1987 if (pgio->pg_lseg == NULL) {
1988 if (pgio->pg_dreq == NULL)
1989 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
1991 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
1993 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2000 if (IS_ERR(pgio->pg_lseg)) {
2001 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2002 pgio->pg_lseg = NULL;
2006 /* If no lseg, fall back to read through mds */
2007 if (pgio->pg_lseg == NULL)
2008 nfs_pageio_reset_read_mds(pgio);
2011 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
2014 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
2015 struct nfs_page *req, u64 wb_size)
2017 if (pgio->pg_lseg == NULL) {
2018 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2025 if (IS_ERR(pgio->pg_lseg)) {
2026 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2027 pgio->pg_lseg = NULL;
2031 /* If no lseg, fall back to write through mds */
2032 if (pgio->pg_lseg == NULL)
2033 nfs_pageio_reset_write_mds(pgio);
2035 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
2038 pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
2040 if (desc->pg_lseg) {
2041 pnfs_put_lseg(desc->pg_lseg);
2042 desc->pg_lseg = NULL;
2045 EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
2048 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
2049 * of bytes (maximum @req->wb_bytes) that can be coalesced.
2052 pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
2053 struct nfs_page *prev, struct nfs_page *req)
2056 u64 seg_end, req_start, seg_left;
2058 size = nfs_generic_pg_test(pgio, prev, req);
2063 * 'size' contains the number of bytes left in the current page (up
2064 * to the original size asked for in @req->wb_bytes).
2066 * Calculate how many bytes are left in the layout segment
2067 * and if there are less bytes than 'size', return that instead.
2069 * Please also note that 'end_offset' is actually the offset of the
2070 * first byte that lies outside the pnfs_layout_range. FIXME?
2073 if (pgio->pg_lseg) {
2074 seg_end = end_offset(pgio->pg_lseg->pls_range.offset,
2075 pgio->pg_lseg->pls_range.length);
2076 req_start = req_offset(req);
2077 WARN_ON_ONCE(req_start >= seg_end);
2078 /* start of request is past the last byte of this segment */
2079 if (req_start >= seg_end) {
2080 /* reference the new lseg */
2081 if (pgio->pg_ops->pg_cleanup)
2082 pgio->pg_ops->pg_cleanup(pgio);
2083 if (pgio->pg_ops->pg_init)
2084 pgio->pg_ops->pg_init(pgio, req);
2088 /* adjust 'size' iff there are fewer bytes left in the
2089 * segment than what nfs_generic_pg_test returned */
2090 seg_left = seg_end - req_start;
2091 if (seg_left < size)
2092 size = (unsigned int)seg_left;
2097 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
2099 int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
2101 struct nfs_pageio_descriptor pgio;
2103 /* Resend all requests through the MDS */
2104 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
2105 hdr->completion_ops);
2106 set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags);
2107 return nfs_pageio_resend(&pgio, hdr);
2109 EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
2111 static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
2114 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
2115 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2116 PNFS_LAYOUTRET_ON_ERROR) {
2117 pnfs_return_layout(hdr->inode);
2119 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2120 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
2124 * Called by non rpc-based layout drivers
2126 void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
2128 if (likely(!hdr->pnfs_error)) {
2129 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
2130 hdr->mds_offset + hdr->res.count);
2131 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
2133 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
2134 if (unlikely(hdr->pnfs_error))
2135 pnfs_ld_handle_write_error(hdr);
2136 hdr->mds_ops->rpc_release(hdr);
2138 EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
2141 pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
2142 struct nfs_pgio_header *hdr)
2144 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2146 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2147 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
2148 nfs_pageio_reset_write_mds(desc);
2149 mirror->pg_recoalesce = 1;
2151 hdr->completion_ops->completion(hdr);
2154 static enum pnfs_try_status
2155 pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
2156 const struct rpc_call_ops *call_ops,
2157 struct pnfs_layout_segment *lseg,
2160 struct inode *inode = hdr->inode;
2161 enum pnfs_try_status trypnfs;
2162 struct nfs_server *nfss = NFS_SERVER(inode);
2164 hdr->mds_ops = call_ops;
2166 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
2167 inode->i_ino, hdr->args.count, hdr->args.offset, how);
2168 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
2169 if (trypnfs != PNFS_NOT_ATTEMPTED)
2170 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
2171 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2176 pnfs_do_write(struct nfs_pageio_descriptor *desc,
2177 struct nfs_pgio_header *hdr, int how)
2179 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2180 struct pnfs_layout_segment *lseg = desc->pg_lseg;
2181 enum pnfs_try_status trypnfs;
2183 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
2184 if (trypnfs == PNFS_NOT_ATTEMPTED)
2185 pnfs_write_through_mds(desc, hdr);
2188 static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
2190 pnfs_put_lseg(hdr->lseg);
2191 nfs_pgio_header_free(hdr);
2195 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
2197 struct nfs_pgio_header *hdr;
2200 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2202 desc->pg_error = -ENOMEM;
2203 return desc->pg_error;
2205 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
2207 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
2208 ret = nfs_generic_pgio(desc, hdr);
2210 pnfs_do_write(desc, hdr, desc->pg_ioflags);
2214 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
2216 int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
2218 struct nfs_pageio_descriptor pgio;
2220 /* Resend all requests through the MDS */
2221 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
2222 return nfs_pageio_resend(&pgio, hdr);
2224 EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
2226 static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
2228 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
2229 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2230 PNFS_LAYOUTRET_ON_ERROR) {
2231 pnfs_return_layout(hdr->inode);
2233 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2234 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
2238 * Called by non rpc-based layout drivers
2240 void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
2242 if (likely(!hdr->pnfs_error))
2243 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
2244 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
2245 if (unlikely(hdr->pnfs_error))
2246 pnfs_ld_handle_read_error(hdr);
2247 hdr->mds_ops->rpc_release(hdr);
2249 EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
2252 pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
2253 struct nfs_pgio_header *hdr)
2255 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2257 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2258 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
2259 nfs_pageio_reset_read_mds(desc);
2260 mirror->pg_recoalesce = 1;
2262 hdr->completion_ops->completion(hdr);
2266 * Call the appropriate parallel I/O subsystem read function.
2268 static enum pnfs_try_status
2269 pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
2270 const struct rpc_call_ops *call_ops,
2271 struct pnfs_layout_segment *lseg)
2273 struct inode *inode = hdr->inode;
2274 struct nfs_server *nfss = NFS_SERVER(inode);
2275 enum pnfs_try_status trypnfs;
2277 hdr->mds_ops = call_ops;
2279 dprintk("%s: Reading ino:%lu %u@%llu\n",
2280 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
2282 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
2283 if (trypnfs != PNFS_NOT_ATTEMPTED)
2284 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
2285 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2289 /* Resend all requests through pnfs. */
2290 void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
2292 struct nfs_pageio_descriptor pgio;
2294 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2295 /* Prevent deadlocks with layoutreturn! */
2296 pnfs_put_lseg(hdr->lseg);
2299 nfs_pageio_init_read(&pgio, hdr->inode, false,
2300 hdr->completion_ops);
2301 hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
2304 EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
2307 pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
2309 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2310 struct pnfs_layout_segment *lseg = desc->pg_lseg;
2311 enum pnfs_try_status trypnfs;
2313 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
2315 case PNFS_NOT_ATTEMPTED:
2316 pnfs_read_through_mds(desc, hdr);
2317 case PNFS_ATTEMPTED:
2319 case PNFS_TRY_AGAIN:
2320 /* cleanup hdr and prepare to redo pnfs */
2321 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2322 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2323 list_splice_init(&hdr->pages, &mirror->pg_list);
2324 mirror->pg_recoalesce = 1;
2326 hdr->mds_ops->rpc_release(hdr);
2330 static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
2332 pnfs_put_lseg(hdr->lseg);
2333 nfs_pgio_header_free(hdr);
2337 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2339 struct nfs_pgio_header *hdr;
2342 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2344 desc->pg_error = -ENOMEM;
2345 return desc->pg_error;
2347 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
2348 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
2349 ret = nfs_generic_pgio(desc, hdr);
2351 pnfs_do_read(desc, hdr);
2354 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
2356 static void pnfs_clear_layoutcommitting(struct inode *inode)
2358 unsigned long *bitlock = &NFS_I(inode)->flags;
2360 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
2361 smp_mb__after_atomic();
2362 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2366 * There can be multiple RW segments.
2368 static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
2370 struct pnfs_layout_segment *lseg;
2372 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2373 if (lseg->pls_range.iomode == IOMODE_RW &&
2374 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
2375 list_add(&lseg->pls_lc_list, listp);
2379 static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2381 struct pnfs_layout_segment *lseg, *tmp;
2383 /* Matched by references in pnfs_set_layoutcommit */
2384 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2385 list_del_init(&lseg->pls_lc_list);
2386 pnfs_put_lseg(lseg);
2389 pnfs_clear_layoutcommitting(inode);
2392 void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2394 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
2396 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2399 pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
2402 struct nfs_inode *nfsi = NFS_I(inode);
2403 bool mark_as_dirty = false;
2405 spin_lock(&inode->i_lock);
2406 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
2407 nfsi->layout->plh_lwb = end_pos;
2408 mark_as_dirty = true;
2409 dprintk("%s: Set layoutcommit for inode %lu ",
2410 __func__, inode->i_ino);
2411 } else if (end_pos > nfsi->layout->plh_lwb)
2412 nfsi->layout->plh_lwb = end_pos;
2413 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
2414 /* references matched in nfs4_layoutcommit_release */
2415 pnfs_get_lseg(lseg);
2417 spin_unlock(&inode->i_lock);
2418 dprintk("%s: lseg %p end_pos %llu\n",
2419 __func__, lseg, nfsi->layout->plh_lwb);
2421 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2422 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2424 mark_inode_dirty_sync(inode);
2426 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2428 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2430 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2432 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2433 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
2434 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
2438 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2439 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2440 * data to disk to allow the server to recover the data if it crashes.
2441 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2442 * is off, and a COMMIT is sent to a data server, or
2443 * if WRITEs to a data server return NFS_DATA_SYNC.
2446 pnfs_layoutcommit_inode(struct inode *inode, bool sync)
2448 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2449 struct nfs4_layoutcommit_data *data;
2450 struct nfs_inode *nfsi = NFS_I(inode);
2454 if (!pnfs_layoutcommit_outstanding(inode))
2457 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
2460 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
2463 status = wait_on_bit_lock_action(&nfsi->flags,
2464 NFS_INO_LAYOUTCOMMITTING,
2465 nfs_wait_bit_killable,
2472 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2473 data = kzalloc(sizeof(*data), GFP_NOFS);
2475 goto clear_layoutcommitting;
2478 spin_lock(&inode->i_lock);
2479 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2482 INIT_LIST_HEAD(&data->lseg_list);
2483 pnfs_list_write_lseg(inode, &data->lseg_list);
2485 end_pos = nfsi->layout->plh_lwb;
2487 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
2488 spin_unlock(&inode->i_lock);
2490 data->args.inode = inode;
2491 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
2492 nfs_fattr_init(&data->fattr);
2493 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2494 data->res.fattr = &data->fattr;
2496 data->args.lastbytewritten = end_pos - 1;
2498 data->args.lastbytewritten = U64_MAX;
2499 data->res.server = NFS_SERVER(inode);
2501 if (ld->prepare_layoutcommit) {
2502 status = ld->prepare_layoutcommit(&data->args);
2504 put_rpccred(data->cred);
2505 spin_lock(&inode->i_lock);
2506 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2507 if (end_pos > nfsi->layout->plh_lwb)
2508 nfsi->layout->plh_lwb = end_pos;
2514 status = nfs4_proc_layoutcommit(data, sync);
2517 mark_inode_dirty_sync(inode);
2518 dprintk("<-- %s status %d\n", __func__, status);
2521 spin_unlock(&inode->i_lock);
2523 clear_layoutcommitting:
2524 pnfs_clear_layoutcommitting(inode);
2527 EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
2530 pnfs_generic_sync(struct inode *inode, bool datasync)
2532 return pnfs_layoutcommit_inode(inode, true);
2534 EXPORT_SYMBOL_GPL(pnfs_generic_sync);
2536 struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
2538 struct nfs4_threshold *thp;
2540 thp = kzalloc(sizeof(*thp), GFP_NOFS);
2542 dprintk("%s mdsthreshold allocation failed\n", __func__);
2548 #if IS_ENABLED(CONFIG_NFS_V4_2)
2550 pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
2552 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2553 struct nfs_server *server = NFS_SERVER(inode);
2554 struct nfs_inode *nfsi = NFS_I(inode);
2555 struct nfs42_layoutstat_data *data;
2556 struct pnfs_layout_hdr *hdr;
2559 if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
2562 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
2565 if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
2568 spin_lock(&inode->i_lock);
2569 if (!NFS_I(inode)->layout) {
2570 spin_unlock(&inode->i_lock);
2571 goto out_clear_layoutstats;
2573 hdr = NFS_I(inode)->layout;
2574 pnfs_get_layout_hdr(hdr);
2575 spin_unlock(&inode->i_lock);
2577 data = kzalloc(sizeof(*data), gfp_flags);
2583 data->args.fh = NFS_FH(inode);
2584 data->args.inode = inode;
2585 status = ld->prepare_layoutstats(&data->args);
2589 status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
2592 dprintk("%s returns %d\n", __func__, status);
2598 pnfs_put_layout_hdr(hdr);
2599 out_clear_layoutstats:
2600 smp_mb__before_atomic();
2601 clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
2602 smp_mb__after_atomic();
2605 EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
2608 unsigned int layoutstats_timer;
2609 module_param(layoutstats_timer, uint, 0644);
2610 EXPORT_SYMBOL_GPL(layoutstats_timer);