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>
36 #include "nfs4trace.h"
37 #include "delegation.h"
39 #define NFSDBG_FACILITY NFSDBG_PNFS
40 #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
45 * protects pnfs_modules_tbl.
47 static DEFINE_SPINLOCK(pnfs_spinlock);
50 * pnfs_modules_tbl holds all pnfs modules
52 static LIST_HEAD(pnfs_modules_tbl);
54 /* Return the registered pnfs layout driver module matching given id */
55 static struct pnfs_layoutdriver_type *
56 find_pnfs_driver_locked(u32 id)
58 struct pnfs_layoutdriver_type *local;
60 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
65 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
69 static struct pnfs_layoutdriver_type *
70 find_pnfs_driver(u32 id)
72 struct pnfs_layoutdriver_type *local;
74 spin_lock(&pnfs_spinlock);
75 local = find_pnfs_driver_locked(id);
76 if (local != NULL && !try_module_get(local->owner)) {
77 dprintk("%s: Could not grab reference on module\n", __func__);
80 spin_unlock(&pnfs_spinlock);
85 unset_pnfs_layoutdriver(struct nfs_server *nfss)
87 if (nfss->pnfs_curr_ld) {
88 if (nfss->pnfs_curr_ld->clear_layoutdriver)
89 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
90 /* Decrement the MDS count. Purge the deviceid cache if zero */
91 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
92 nfs4_deviceid_purge_client(nfss->nfs_client);
93 module_put(nfss->pnfs_curr_ld->owner);
95 nfss->pnfs_curr_ld = NULL;
99 * Try to set the server's pnfs module to the pnfs layout type specified by id.
100 * Currently only one pNFS layout driver per filesystem is supported.
102 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
105 set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
108 struct pnfs_layoutdriver_type *ld_type = NULL;
112 if (!(server->nfs_client->cl_exchange_flags &
113 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
114 printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n",
115 __func__, id, server->nfs_client->cl_exchange_flags);
118 ld_type = find_pnfs_driver(id);
120 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
121 ld_type = find_pnfs_driver(id);
123 dprintk("%s: No pNFS module found for %u.\n",
128 server->pnfs_curr_ld = ld_type;
129 if (ld_type->set_layoutdriver
130 && ld_type->set_layoutdriver(server, mntfh)) {
131 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
132 "driver %u.\n", __func__, id);
133 module_put(ld_type->owner);
136 /* Bump the MDS count */
137 atomic_inc(&server->nfs_client->cl_mds_count);
139 dprintk("%s: pNFS module for %u set\n", __func__, id);
143 dprintk("%s: Using NFSv4 I/O\n", __func__);
144 server->pnfs_curr_ld = NULL;
148 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
150 int status = -EINVAL;
151 struct pnfs_layoutdriver_type *tmp;
153 if (ld_type->id == 0) {
154 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
157 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
158 printk(KERN_ERR "NFS: %s Layout driver must provide "
159 "alloc_lseg and free_lseg.\n", __func__);
163 spin_lock(&pnfs_spinlock);
164 tmp = find_pnfs_driver_locked(ld_type->id);
166 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
168 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
171 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
172 __func__, ld_type->id);
174 spin_unlock(&pnfs_spinlock);
178 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
181 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
183 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
184 spin_lock(&pnfs_spinlock);
185 list_del(&ld_type->pnfs_tblid);
186 spin_unlock(&pnfs_spinlock);
188 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
191 * pNFS client layout cache
194 /* Need to hold i_lock if caller does not already hold reference */
196 pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
198 atomic_inc(&lo->plh_refcount);
201 static struct pnfs_layout_hdr *
202 pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
204 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
205 return ld->alloc_layout_hdr(ino, gfp_flags);
209 pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
211 struct nfs_server *server = NFS_SERVER(lo->plh_inode);
212 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
214 if (!list_empty(&lo->plh_layouts)) {
215 struct nfs_client *clp = server->nfs_client;
217 spin_lock(&clp->cl_lock);
218 list_del_init(&lo->plh_layouts);
219 spin_unlock(&clp->cl_lock);
221 put_rpccred(lo->plh_lc_cred);
222 return ld->free_layout_hdr(lo);
226 pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
228 struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
229 dprintk("%s: freeing layout cache %p\n", __func__, lo);
231 /* Reset MDS Threshold I/O counters */
237 pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
239 struct inode *inode = lo->plh_inode;
241 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
242 pnfs_detach_layout_hdr(lo);
243 spin_unlock(&inode->i_lock);
244 pnfs_free_layout_hdr(lo);
249 pnfs_iomode_to_fail_bit(u32 iomode)
251 return iomode == IOMODE_RW ?
252 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
256 pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
258 lo->plh_retry_timestamp = jiffies;
259 if (!test_and_set_bit(fail_bit, &lo->plh_flags))
260 atomic_inc(&lo->plh_refcount);
264 pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
266 if (test_and_clear_bit(fail_bit, &lo->plh_flags))
267 atomic_dec(&lo->plh_refcount);
271 pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
273 struct inode *inode = lo->plh_inode;
274 struct pnfs_layout_range range = {
277 .length = NFS4_MAX_UINT64,
281 spin_lock(&inode->i_lock);
282 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
283 pnfs_mark_matching_lsegs_invalid(lo, &head, &range);
284 spin_unlock(&inode->i_lock);
285 pnfs_free_lseg_list(&head);
286 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
287 iomode == IOMODE_RW ? "RW" : "READ");
291 pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
293 unsigned long start, end;
294 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
296 if (test_bit(fail_bit, &lo->plh_flags) == 0)
299 start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
300 if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
301 /* It is time to retry the failed layoutgets */
302 pnfs_layout_clear_fail_bit(lo, fail_bit);
309 init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
311 INIT_LIST_HEAD(&lseg->pls_list);
312 INIT_LIST_HEAD(&lseg->pls_lc_list);
313 atomic_set(&lseg->pls_refcount, 1);
315 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
316 lseg->pls_layout = lo;
319 static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
321 struct inode *ino = lseg->pls_layout->plh_inode;
323 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
327 pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
328 struct pnfs_layout_segment *lseg)
330 struct inode *inode = lo->plh_inode;
332 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
333 list_del_init(&lseg->pls_list);
334 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
335 atomic_dec(&lo->plh_refcount);
336 if (list_empty(&lo->plh_segs))
337 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
338 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
342 pnfs_put_lseg(struct pnfs_layout_segment *lseg)
344 struct pnfs_layout_hdr *lo;
350 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
351 atomic_read(&lseg->pls_refcount),
352 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
353 lo = lseg->pls_layout;
354 inode = lo->plh_inode;
355 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
356 pnfs_get_layout_hdr(lo);
357 pnfs_layout_remove_lseg(lo, lseg);
358 spin_unlock(&inode->i_lock);
359 pnfs_free_lseg(lseg);
360 pnfs_put_layout_hdr(lo);
363 EXPORT_SYMBOL_GPL(pnfs_put_lseg);
365 static void pnfs_free_lseg_async_work(struct work_struct *work)
367 struct pnfs_layout_segment *lseg;
368 struct pnfs_layout_hdr *lo;
370 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
371 lo = lseg->pls_layout;
373 pnfs_free_lseg(lseg);
374 pnfs_put_layout_hdr(lo);
377 static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg)
379 INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work);
380 schedule_work(&lseg->pls_work);
384 pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg)
389 assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock);
391 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
392 atomic_read(&lseg->pls_refcount),
393 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
394 if (atomic_dec_and_test(&lseg->pls_refcount)) {
395 struct pnfs_layout_hdr *lo = lseg->pls_layout;
396 pnfs_get_layout_hdr(lo);
397 pnfs_layout_remove_lseg(lo, lseg);
398 pnfs_free_lseg_async(lseg);
401 EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked);
404 end_offset(u64 start, u64 len)
409 return end >= start ? end : NFS4_MAX_UINT64;
413 * is l2 fully contained in l1?
415 * [----------------------------------)
420 pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
421 const struct pnfs_layout_range *l2)
423 u64 start1 = l1->offset;
424 u64 end1 = end_offset(start1, l1->length);
425 u64 start2 = l2->offset;
426 u64 end2 = end_offset(start2, l2->length);
428 return (start1 <= start2) && (end1 >= end2);
432 * is l1 and l2 intersecting?
434 * [----------------------------------)
439 pnfs_lseg_range_intersecting(const struct pnfs_layout_range *l1,
440 const struct pnfs_layout_range *l2)
442 u64 start1 = l1->offset;
443 u64 end1 = end_offset(start1, l1->length);
444 u64 start2 = l2->offset;
445 u64 end2 = end_offset(start2, l2->length);
447 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
448 (end2 == NFS4_MAX_UINT64 || end2 > start1);
452 should_free_lseg(const struct pnfs_layout_range *lseg_range,
453 const struct pnfs_layout_range *recall_range)
455 return (recall_range->iomode == IOMODE_ANY ||
456 lseg_range->iomode == recall_range->iomode) &&
457 pnfs_lseg_range_intersecting(lseg_range, recall_range);
460 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
461 struct list_head *tmp_list)
463 if (!atomic_dec_and_test(&lseg->pls_refcount))
465 pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
466 list_add(&lseg->pls_list, tmp_list);
470 /* Returns 1 if lseg is removed from list, 0 otherwise */
471 static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
472 struct list_head *tmp_list)
476 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
477 /* Remove the reference keeping the lseg in the
478 * list. It will now be removed when all
479 * outstanding io is finished.
481 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
482 atomic_read(&lseg->pls_refcount));
483 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
489 /* Returns count of number of matching invalid lsegs remaining in list
493 pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
494 struct list_head *tmp_list,
495 struct pnfs_layout_range *recall_range)
497 struct pnfs_layout_segment *lseg, *next;
498 int invalid = 0, removed = 0;
500 dprintk("%s:Begin lo %p\n", __func__, lo);
502 if (list_empty(&lo->plh_segs))
504 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
506 should_free_lseg(&lseg->pls_range, recall_range)) {
507 dprintk("%s: freeing lseg %p iomode %d "
508 "offset %llu length %llu\n", __func__,
509 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
510 lseg->pls_range.length);
512 removed += mark_lseg_invalid(lseg, tmp_list);
514 dprintk("%s:Return %i\n", __func__, invalid - removed);
515 return invalid - removed;
518 /* note free_me must contain lsegs from a single layout_hdr */
520 pnfs_free_lseg_list(struct list_head *free_me)
522 struct pnfs_layout_segment *lseg, *tmp;
524 if (list_empty(free_me))
527 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
528 list_del(&lseg->pls_list);
529 pnfs_free_lseg(lseg);
534 pnfs_destroy_layout(struct nfs_inode *nfsi)
536 struct pnfs_layout_hdr *lo;
539 spin_lock(&nfsi->vfs_inode.i_lock);
542 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
543 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
544 pnfs_get_layout_hdr(lo);
545 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
546 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
547 spin_unlock(&nfsi->vfs_inode.i_lock);
548 pnfs_free_lseg_list(&tmp_list);
549 pnfs_put_layout_hdr(lo);
551 spin_unlock(&nfsi->vfs_inode.i_lock);
553 EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
556 pnfs_layout_add_bulk_destroy_list(struct inode *inode,
557 struct list_head *layout_list)
559 struct pnfs_layout_hdr *lo;
562 spin_lock(&inode->i_lock);
563 lo = NFS_I(inode)->layout;
564 if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
565 pnfs_get_layout_hdr(lo);
566 list_add(&lo->plh_bulk_destroy, layout_list);
569 spin_unlock(&inode->i_lock);
573 /* Caller must hold rcu_read_lock and clp->cl_lock */
575 pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
576 struct nfs_server *server,
577 struct list_head *layout_list)
579 struct pnfs_layout_hdr *lo, *next;
582 list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
583 inode = igrab(lo->plh_inode);
586 list_del_init(&lo->plh_layouts);
587 if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
590 spin_unlock(&clp->cl_lock);
592 spin_lock(&clp->cl_lock);
600 pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
603 struct pnfs_layout_hdr *lo;
605 struct pnfs_layout_range range = {
606 .iomode = IOMODE_ANY,
608 .length = NFS4_MAX_UINT64,
610 LIST_HEAD(lseg_list);
613 while (!list_empty(layout_list)) {
614 lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
616 dprintk("%s freeing layout for inode %lu\n", __func__,
617 lo->plh_inode->i_ino);
618 inode = lo->plh_inode;
620 pnfs_layoutcommit_inode(inode, false);
622 spin_lock(&inode->i_lock);
623 list_del_init(&lo->plh_bulk_destroy);
624 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
626 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
627 if (pnfs_mark_matching_lsegs_invalid(lo, &lseg_list, &range))
629 spin_unlock(&inode->i_lock);
630 pnfs_free_lseg_list(&lseg_list);
631 pnfs_put_layout_hdr(lo);
638 pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
639 struct nfs_fsid *fsid,
642 struct nfs_server *server;
643 LIST_HEAD(layout_list);
645 spin_lock(&clp->cl_lock);
648 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
649 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
651 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
657 spin_unlock(&clp->cl_lock);
659 if (list_empty(&layout_list))
661 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
665 pnfs_destroy_layouts_byclid(struct nfs_client *clp,
668 struct nfs_server *server;
669 LIST_HEAD(layout_list);
671 spin_lock(&clp->cl_lock);
674 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
675 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
681 spin_unlock(&clp->cl_lock);
683 if (list_empty(&layout_list))
685 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
689 * Called by the state manger to remove all layouts established under an
693 pnfs_destroy_all_layouts(struct nfs_client *clp)
695 nfs4_deviceid_mark_client_invalid(clp);
696 nfs4_deviceid_purge_client(clp);
698 pnfs_destroy_layouts_byclid(clp, false);
702 * Compare 2 layout stateid sequence ids, to see which is newer,
703 * taking into account wraparound issues.
705 static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
707 return (s32)(s1 - s2) > 0;
710 /* update lo->plh_stateid with new if is more recent */
712 pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
715 u32 oldseq, newseq, new_barrier;
716 int empty = list_empty(&lo->plh_segs);
718 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
719 newseq = be32_to_cpu(new->seqid);
720 if (empty || pnfs_seqid_is_newer(newseq, oldseq)) {
721 nfs4_stateid_copy(&lo->plh_stateid, new);
722 if (update_barrier) {
723 new_barrier = be32_to_cpu(new->seqid);
725 /* Because of wraparound, we want to keep the barrier
726 * "close" to the current seqids.
728 new_barrier = newseq - atomic_read(&lo->plh_outstanding);
730 if (empty || pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
731 lo->plh_barrier = new_barrier;
736 pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
737 const nfs4_stateid *stateid)
739 u32 seqid = be32_to_cpu(stateid->seqid);
741 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
744 /* lget is set to 1 if called from inside send_layoutget call chain */
746 pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo, int lget)
748 return lo->plh_block_lgets ||
749 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
750 (list_empty(&lo->plh_segs) &&
751 (atomic_read(&lo->plh_outstanding) > lget));
755 pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
756 struct nfs4_state *open_state)
760 dprintk("--> %s\n", __func__);
761 spin_lock(&lo->plh_inode->i_lock);
762 if (pnfs_layoutgets_blocked(lo, 1)) {
764 } else if (!nfs4_valid_open_stateid(open_state)) {
766 } else if (list_empty(&lo->plh_segs) ||
767 test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
771 seq = read_seqbegin(&open_state->seqlock);
772 nfs4_stateid_copy(dst, &open_state->stateid);
773 } while (read_seqretry(&open_state->seqlock, seq));
775 nfs4_stateid_copy(dst, &lo->plh_stateid);
776 spin_unlock(&lo->plh_inode->i_lock);
777 dprintk("<-- %s\n", __func__);
782 * Get layout from server.
783 * for now, assume that whole file layouts are requested.
785 * arg->length: all ones
787 static struct pnfs_layout_segment *
788 send_layoutget(struct pnfs_layout_hdr *lo,
789 struct nfs_open_context *ctx,
790 struct pnfs_layout_range *range,
793 struct inode *ino = lo->plh_inode;
794 struct nfs_server *server = NFS_SERVER(ino);
795 struct nfs4_layoutget *lgp;
796 struct pnfs_layout_segment *lseg;
798 dprintk("--> %s\n", __func__);
800 lgp = kzalloc(sizeof(*lgp), gfp_flags);
804 lgp->args.minlength = PAGE_CACHE_SIZE;
805 if (lgp->args.minlength > range->length)
806 lgp->args.minlength = range->length;
807 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
808 lgp->args.range = *range;
809 lgp->args.type = server->pnfs_curr_ld->id;
810 lgp->args.inode = ino;
811 lgp->args.ctx = get_nfs_open_context(ctx);
812 lgp->gfp_flags = gfp_flags;
813 lgp->cred = lo->plh_lc_cred;
815 /* Synchronously retrieve layout information from server and
818 lseg = nfs4_proc_layoutget(lgp, gfp_flags);
820 switch (PTR_ERR(lseg)) {
825 /* remember that LAYOUTGET failed and suspend trying */
826 pnfs_layout_io_set_failed(lo, range->iomode);
834 static void pnfs_clear_layoutcommit(struct inode *inode,
835 struct list_head *head)
837 struct nfs_inode *nfsi = NFS_I(inode);
838 struct pnfs_layout_segment *lseg, *tmp;
840 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
842 list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
843 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
845 pnfs_lseg_dec_and_remove_zero(lseg, head);
850 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
851 * when the layout segment list is empty.
853 * Note that a pnfs_layout_hdr can exist with an empty layout segment
854 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
855 * deviceid is marked invalid.
858 _pnfs_return_layout(struct inode *ino)
860 struct pnfs_layout_hdr *lo = NULL;
861 struct nfs_inode *nfsi = NFS_I(ino);
863 struct nfs4_layoutreturn *lrp;
864 nfs4_stateid stateid;
865 int status = 0, empty;
867 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
869 spin_lock(&ino->i_lock);
872 spin_unlock(&ino->i_lock);
873 dprintk("NFS: %s no layout to return\n", __func__);
876 stateid = nfsi->layout->plh_stateid;
877 /* Reference matched in nfs4_layoutreturn_release */
878 pnfs_get_layout_hdr(lo);
879 empty = list_empty(&lo->plh_segs);
880 pnfs_clear_layoutcommit(ino, &tmp_list);
881 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
883 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
884 struct pnfs_layout_range range = {
885 .iomode = IOMODE_ANY,
887 .length = NFS4_MAX_UINT64,
889 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
892 /* Don't send a LAYOUTRETURN if list was initially empty */
894 spin_unlock(&ino->i_lock);
895 pnfs_put_layout_hdr(lo);
896 dprintk("NFS: %s no layout segments to return\n", __func__);
900 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
901 lo->plh_block_lgets++;
902 spin_unlock(&ino->i_lock);
903 pnfs_free_lseg_list(&tmp_list);
905 lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
906 if (unlikely(lrp == NULL)) {
908 spin_lock(&ino->i_lock);
909 lo->plh_block_lgets--;
910 spin_unlock(&ino->i_lock);
911 pnfs_put_layout_hdr(lo);
915 lrp->args.stateid = stateid;
916 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
917 lrp->args.inode = ino;
918 lrp->args.layout = lo;
919 lrp->clp = NFS_SERVER(ino)->nfs_client;
920 lrp->cred = lo->plh_lc_cred;
922 status = nfs4_proc_layoutreturn(lrp);
924 dprintk("<-- %s status: %d\n", __func__, status);
927 EXPORT_SYMBOL_GPL(_pnfs_return_layout);
930 pnfs_commit_and_return_layout(struct inode *inode)
932 struct pnfs_layout_hdr *lo;
935 spin_lock(&inode->i_lock);
936 lo = NFS_I(inode)->layout;
938 spin_unlock(&inode->i_lock);
941 pnfs_get_layout_hdr(lo);
942 /* Block new layoutgets and read/write to ds */
943 lo->plh_block_lgets++;
944 spin_unlock(&inode->i_lock);
945 filemap_fdatawait(inode->i_mapping);
946 ret = pnfs_layoutcommit_inode(inode, true);
948 ret = _pnfs_return_layout(inode);
949 spin_lock(&inode->i_lock);
950 lo->plh_block_lgets--;
951 spin_unlock(&inode->i_lock);
952 pnfs_put_layout_hdr(lo);
956 bool pnfs_roc(struct inode *ino)
958 struct nfs_inode *nfsi = NFS_I(ino);
959 struct nfs_open_context *ctx;
960 struct nfs4_state *state;
961 struct pnfs_layout_hdr *lo;
962 struct pnfs_layout_segment *lseg, *tmp;
966 spin_lock(&ino->i_lock);
968 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
969 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
972 /* Don't return layout if we hold a delegation */
973 if (nfs4_check_delegation(ino, FMODE_READ))
976 list_for_each_entry(ctx, &nfsi->open_files, list) {
978 /* Don't return layout if there is open file state */
979 if (state != NULL && state->state != 0)
983 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
984 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
985 mark_lseg_invalid(lseg, &tmp_list);
990 lo->plh_block_lgets++;
991 pnfs_get_layout_hdr(lo); /* matched in pnfs_roc_release */
992 spin_unlock(&ino->i_lock);
993 pnfs_free_lseg_list(&tmp_list);
997 spin_unlock(&ino->i_lock);
1001 void pnfs_roc_release(struct inode *ino)
1003 struct pnfs_layout_hdr *lo;
1005 spin_lock(&ino->i_lock);
1006 lo = NFS_I(ino)->layout;
1007 lo->plh_block_lgets--;
1008 if (atomic_dec_and_test(&lo->plh_refcount)) {
1009 pnfs_detach_layout_hdr(lo);
1010 spin_unlock(&ino->i_lock);
1011 pnfs_free_layout_hdr(lo);
1013 spin_unlock(&ino->i_lock);
1016 void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
1018 struct pnfs_layout_hdr *lo;
1020 spin_lock(&ino->i_lock);
1021 lo = NFS_I(ino)->layout;
1022 if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
1023 lo->plh_barrier = barrier;
1024 spin_unlock(&ino->i_lock);
1027 bool pnfs_roc_drain(struct inode *ino, u32 *barrier, struct rpc_task *task)
1029 struct nfs_inode *nfsi = NFS_I(ino);
1030 struct pnfs_layout_hdr *lo;
1031 struct pnfs_layout_segment *lseg;
1035 spin_lock(&ino->i_lock);
1036 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
1037 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
1038 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
1043 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
1045 /* Since close does not return a layout stateid for use as
1046 * a barrier, we choose the worst-case barrier.
1048 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
1050 spin_unlock(&ino->i_lock);
1055 * Compare two layout segments for sorting into layout cache.
1056 * We want to preferentially return RW over RO layouts, so ensure those
1060 pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
1061 const struct pnfs_layout_range *l2)
1065 /* high offset > low offset */
1066 d = l1->offset - l2->offset;
1070 /* short length > long length */
1071 d = l2->length - l1->length;
1075 /* read > read/write */
1076 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
1080 pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1081 struct pnfs_layout_segment *lseg)
1083 struct pnfs_layout_segment *lp;
1085 dprintk("%s:Begin\n", __func__);
1087 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
1088 if (pnfs_lseg_range_cmp(&lseg->pls_range, &lp->pls_range) > 0)
1090 list_add_tail(&lseg->pls_list, &lp->pls_list);
1091 dprintk("%s: inserted lseg %p "
1092 "iomode %d offset %llu length %llu before "
1093 "lp %p iomode %d offset %llu length %llu\n",
1094 __func__, lseg, lseg->pls_range.iomode,
1095 lseg->pls_range.offset, lseg->pls_range.length,
1096 lp, lp->pls_range.iomode, lp->pls_range.offset,
1097 lp->pls_range.length);
1100 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1101 dprintk("%s: inserted lseg %p "
1102 "iomode %d offset %llu length %llu at tail\n",
1103 __func__, lseg, lseg->pls_range.iomode,
1104 lseg->pls_range.offset, lseg->pls_range.length);
1106 pnfs_get_layout_hdr(lo);
1108 dprintk("%s:Return\n", __func__);
1111 static struct pnfs_layout_hdr *
1112 alloc_init_layout_hdr(struct inode *ino,
1113 struct nfs_open_context *ctx,
1116 struct pnfs_layout_hdr *lo;
1118 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
1121 atomic_set(&lo->plh_refcount, 1);
1122 INIT_LIST_HEAD(&lo->plh_layouts);
1123 INIT_LIST_HEAD(&lo->plh_segs);
1124 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
1125 lo->plh_inode = ino;
1126 lo->plh_lc_cred = get_rpccred(ctx->cred);
1130 static struct pnfs_layout_hdr *
1131 pnfs_find_alloc_layout(struct inode *ino,
1132 struct nfs_open_context *ctx,
1135 struct nfs_inode *nfsi = NFS_I(ino);
1136 struct pnfs_layout_hdr *new = NULL;
1138 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1140 if (nfsi->layout != NULL)
1142 spin_unlock(&ino->i_lock);
1143 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
1144 spin_lock(&ino->i_lock);
1146 if (likely(nfsi->layout == NULL)) { /* Won the race? */
1149 } else if (new != NULL)
1150 pnfs_free_layout_hdr(new);
1152 pnfs_get_layout_hdr(nfsi->layout);
1153 return nfsi->layout;
1157 * iomode matching rules:
1168 pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
1169 const struct pnfs_layout_range *range)
1171 struct pnfs_layout_range range1;
1173 if ((range->iomode == IOMODE_RW &&
1174 ls_range->iomode != IOMODE_RW) ||
1175 !pnfs_lseg_range_intersecting(ls_range, range))
1178 /* range1 covers only the first byte in the range */
1181 return pnfs_lseg_range_contained(ls_range, &range1);
1185 * lookup range in layout
1187 static struct pnfs_layout_segment *
1188 pnfs_find_lseg(struct pnfs_layout_hdr *lo,
1189 struct pnfs_layout_range *range)
1191 struct pnfs_layout_segment *lseg, *ret = NULL;
1193 dprintk("%s:Begin\n", __func__);
1195 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
1196 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
1197 pnfs_lseg_range_match(&lseg->pls_range, range)) {
1198 ret = pnfs_get_lseg(lseg);
1201 if (lseg->pls_range.offset > range->offset)
1205 dprintk("%s:Return lseg %p ref %d\n",
1206 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
1211 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1212 * to the MDS or over pNFS
1214 * The nfs_inode read_io and write_io fields are cumulative counters reset
1215 * when there are no layout segments. Note that in pnfs_update_layout iomode
1216 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1219 * A return of true means use MDS I/O.
1222 * If a file's size is smaller than the file size threshold, data accesses
1223 * SHOULD be sent to the metadata server. If an I/O request has a length that
1224 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1225 * server. If both file size and I/O size are provided, the client SHOULD
1226 * reach or exceed both thresholds before sending its read or write
1227 * requests to the data server.
1229 static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1230 struct inode *ino, int iomode)
1232 struct nfs4_threshold *t = ctx->mdsthreshold;
1233 struct nfs_inode *nfsi = NFS_I(ino);
1234 loff_t fsize = i_size_read(ino);
1235 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1240 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1241 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1245 if (t->bm & THRESHOLD_RD) {
1246 dprintk("%s fsize %llu\n", __func__, fsize);
1248 if (fsize < t->rd_sz)
1251 if (t->bm & THRESHOLD_RD_IO) {
1252 dprintk("%s nfsi->read_io %llu\n", __func__,
1255 if (nfsi->read_io < t->rd_io_sz)
1260 if (t->bm & THRESHOLD_WR) {
1261 dprintk("%s fsize %llu\n", __func__, fsize);
1263 if (fsize < t->wr_sz)
1266 if (t->bm & THRESHOLD_WR_IO) {
1267 dprintk("%s nfsi->write_io %llu\n", __func__,
1270 if (nfsi->write_io < t->wr_io_sz)
1275 if (size_set && io_set) {
1278 } else if (size || io)
1281 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1286 * Layout segment is retreived from the server if not cached.
1287 * The appropriate layout segment is referenced and returned to the caller.
1289 struct pnfs_layout_segment *
1290 pnfs_update_layout(struct inode *ino,
1291 struct nfs_open_context *ctx,
1294 enum pnfs_iomode iomode,
1297 struct pnfs_layout_range arg = {
1303 struct nfs_server *server = NFS_SERVER(ino);
1304 struct nfs_client *clp = server->nfs_client;
1305 struct pnfs_layout_hdr *lo;
1306 struct pnfs_layout_segment *lseg = NULL;
1309 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
1312 if (pnfs_within_mdsthreshold(ctx, ino, iomode))
1315 spin_lock(&ino->i_lock);
1316 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
1318 spin_unlock(&ino->i_lock);
1322 /* Do we even need to bother with this? */
1323 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1324 dprintk("%s matches recall, use MDS\n", __func__);
1328 /* if LAYOUTGET already failed once we don't try again */
1329 if (pnfs_layout_io_test_failed(lo, iomode))
1332 /* Check to see if the layout for the given range already exists */
1333 lseg = pnfs_find_lseg(lo, &arg);
1337 if (pnfs_layoutgets_blocked(lo, 0))
1339 atomic_inc(&lo->plh_outstanding);
1341 first = list_empty(&lo->plh_layouts) ? true : false;
1342 spin_unlock(&ino->i_lock);
1345 /* The lo must be on the clp list if there is any
1346 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1348 spin_lock(&clp->cl_lock);
1349 list_add_tail(&lo->plh_layouts, &server->layouts);
1350 spin_unlock(&clp->cl_lock);
1353 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
1355 arg.offset -= pg_offset;
1356 arg.length += pg_offset;
1358 if (arg.length != NFS4_MAX_UINT64)
1359 arg.length = PAGE_CACHE_ALIGN(arg.length);
1361 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
1362 atomic_dec(&lo->plh_outstanding);
1364 pnfs_put_layout_hdr(lo);
1366 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1367 "(%s, offset: %llu, length: %llu)\n",
1368 __func__, ino->i_sb->s_id,
1369 (unsigned long long)NFS_FILEID(ino),
1370 lseg == NULL ? "not found" : "found",
1371 iomode==IOMODE_RW ? "read/write" : "read-only",
1372 (unsigned long long)pos,
1373 (unsigned long long)count);
1376 spin_unlock(&ino->i_lock);
1377 goto out_put_layout_hdr;
1379 EXPORT_SYMBOL_GPL(pnfs_update_layout);
1381 struct pnfs_layout_segment *
1382 pnfs_layout_process(struct nfs4_layoutget *lgp)
1384 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1385 struct nfs4_layoutget_res *res = &lgp->res;
1386 struct pnfs_layout_segment *lseg;
1387 struct inode *ino = lo->plh_inode;
1391 /* Inject layout blob into I/O device driver */
1392 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
1393 if (!lseg || IS_ERR(lseg)) {
1397 status = PTR_ERR(lseg);
1398 dprintk("%s: Could not allocate layout: error %d\n",
1403 init_lseg(lo, lseg);
1404 lseg->pls_range = res->range;
1406 spin_lock(&ino->i_lock);
1407 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1408 dprintk("%s forget reply due to recall\n", __func__);
1409 goto out_forget_reply;
1412 if (pnfs_layoutgets_blocked(lo, 1)) {
1413 dprintk("%s forget reply due to state\n", __func__);
1414 goto out_forget_reply;
1417 if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
1418 /* existing state ID, make sure the sequence number matches. */
1419 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1420 dprintk("%s forget reply due to sequence\n", __func__);
1421 goto out_forget_reply;
1423 pnfs_set_layout_stateid(lo, &res->stateid, false);
1426 * We got an entirely new state ID. Mark all segments for the
1427 * inode invalid, and don't bother validating the stateid
1430 pnfs_mark_matching_lsegs_invalid(lo, &free_me, NULL);
1432 nfs4_stateid_copy(&lo->plh_stateid, &res->stateid);
1433 lo->plh_barrier = be32_to_cpu(res->stateid.seqid);
1436 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
1438 pnfs_get_lseg(lseg);
1439 pnfs_layout_insert_lseg(lo, lseg);
1441 if (res->return_on_close) {
1442 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1443 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1446 spin_unlock(&ino->i_lock);
1447 pnfs_free_lseg_list(&free_me);
1450 return ERR_PTR(status);
1453 spin_unlock(&ino->i_lock);
1454 lseg->pls_layout = lo;
1455 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1460 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1462 u64 rd_size = req->wb_bytes;
1464 WARN_ON_ONCE(pgio->pg_lseg != NULL);
1466 if (pgio->pg_dreq == NULL)
1467 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
1469 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
1471 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1477 /* If no lseg, fall back to read through mds */
1478 if (pgio->pg_lseg == NULL)
1479 nfs_pageio_reset_read_mds(pgio);
1482 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1485 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
1486 struct nfs_page *req, u64 wb_size)
1488 WARN_ON_ONCE(pgio->pg_lseg != NULL);
1490 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1496 /* If no lseg, fall back to write through mds */
1497 if (pgio->pg_lseg == NULL)
1498 nfs_pageio_reset_write_mds(pgio);
1500 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1503 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
1504 * of bytes (maximum @req->wb_bytes) that can be coalesced.
1507 pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1508 struct nfs_page *req)
1511 u64 seg_end, req_start, seg_left;
1513 size = nfs_generic_pg_test(pgio, prev, req);
1518 * 'size' contains the number of bytes left in the current page (up
1519 * to the original size asked for in @req->wb_bytes).
1521 * Calculate how many bytes are left in the layout segment
1522 * and if there are less bytes than 'size', return that instead.
1524 * Please also note that 'end_offset' is actually the offset of the
1525 * first byte that lies outside the pnfs_layout_range. FIXME?
1528 if (pgio->pg_lseg) {
1529 seg_end = end_offset(pgio->pg_lseg->pls_range.offset,
1530 pgio->pg_lseg->pls_range.length);
1531 req_start = req_offset(req);
1532 WARN_ON_ONCE(req_start > seg_end);
1533 /* start of request is past the last byte of this segment */
1534 if (req_start >= seg_end)
1537 /* adjust 'size' iff there are fewer bytes left in the
1538 * segment than what nfs_generic_pg_test returned */
1539 seg_left = seg_end - req_start;
1540 if (seg_left < size)
1541 size = (unsigned int)seg_left;
1546 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
1548 int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
1550 struct nfs_pageio_descriptor pgio;
1552 /* Resend all requests through the MDS */
1553 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
1554 hdr->completion_ops);
1555 return nfs_pageio_resend(&pgio, hdr);
1557 EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
1559 static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
1562 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
1563 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
1564 PNFS_LAYOUTRET_ON_ERROR) {
1565 pnfs_return_layout(hdr->inode);
1567 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
1568 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
1572 * Called by non rpc-based layout drivers
1574 void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
1576 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
1577 if (!hdr->pnfs_error) {
1578 pnfs_set_layoutcommit(hdr);
1579 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
1581 pnfs_ld_handle_write_error(hdr);
1582 hdr->mds_ops->rpc_release(hdr);
1584 EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
1587 pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
1588 struct nfs_pgio_header *hdr)
1590 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1591 list_splice_tail_init(&hdr->pages, &desc->pg_list);
1592 nfs_pageio_reset_write_mds(desc);
1593 desc->pg_recoalesce = 1;
1595 nfs_pgio_data_destroy(hdr);
1598 static enum pnfs_try_status
1599 pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
1600 const struct rpc_call_ops *call_ops,
1601 struct pnfs_layout_segment *lseg,
1604 struct inode *inode = hdr->inode;
1605 enum pnfs_try_status trypnfs;
1606 struct nfs_server *nfss = NFS_SERVER(inode);
1608 hdr->mds_ops = call_ops;
1610 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1611 inode->i_ino, hdr->args.count, hdr->args.offset, how);
1612 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
1613 if (trypnfs != PNFS_NOT_ATTEMPTED)
1614 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1615 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1620 pnfs_do_write(struct nfs_pageio_descriptor *desc,
1621 struct nfs_pgio_header *hdr, int how)
1623 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1624 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1625 enum pnfs_try_status trypnfs;
1627 desc->pg_lseg = NULL;
1628 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
1629 if (trypnfs == PNFS_NOT_ATTEMPTED)
1630 pnfs_write_through_mds(desc, hdr);
1631 pnfs_put_lseg(lseg);
1634 static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
1636 pnfs_put_lseg(hdr->lseg);
1637 nfs_pgio_header_free(hdr);
1639 EXPORT_SYMBOL_GPL(pnfs_writehdr_free);
1642 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1644 struct nfs_pgio_header *hdr;
1647 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
1649 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
1650 pnfs_put_lseg(desc->pg_lseg);
1651 desc->pg_lseg = NULL;
1654 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
1655 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
1656 ret = nfs_generic_pgio(desc, hdr);
1658 pnfs_put_lseg(desc->pg_lseg);
1659 desc->pg_lseg = NULL;
1661 pnfs_do_write(desc, hdr, desc->pg_ioflags);
1664 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
1666 int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
1668 struct nfs_pageio_descriptor pgio;
1670 /* Resend all requests through the MDS */
1671 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
1672 return nfs_pageio_resend(&pgio, hdr);
1674 EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
1676 static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
1678 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
1679 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
1680 PNFS_LAYOUTRET_ON_ERROR) {
1681 pnfs_return_layout(hdr->inode);
1683 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
1684 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
1688 * Called by non rpc-based layout drivers
1690 void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
1692 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
1693 if (likely(!hdr->pnfs_error)) {
1694 __nfs4_read_done_cb(hdr);
1695 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
1697 pnfs_ld_handle_read_error(hdr);
1698 hdr->mds_ops->rpc_release(hdr);
1700 EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1703 pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
1704 struct nfs_pgio_header *hdr)
1706 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1707 list_splice_tail_init(&hdr->pages, &desc->pg_list);
1708 nfs_pageio_reset_read_mds(desc);
1709 desc->pg_recoalesce = 1;
1711 nfs_pgio_data_destroy(hdr);
1715 * Call the appropriate parallel I/O subsystem read function.
1717 static enum pnfs_try_status
1718 pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
1719 const struct rpc_call_ops *call_ops,
1720 struct pnfs_layout_segment *lseg)
1722 struct inode *inode = hdr->inode;
1723 struct nfs_server *nfss = NFS_SERVER(inode);
1724 enum pnfs_try_status trypnfs;
1726 hdr->mds_ops = call_ops;
1728 dprintk("%s: Reading ino:%lu %u@%llu\n",
1729 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
1731 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
1732 if (trypnfs != PNFS_NOT_ATTEMPTED)
1733 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1734 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1739 pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
1741 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1742 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1743 enum pnfs_try_status trypnfs;
1745 desc->pg_lseg = NULL;
1746 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
1747 if (trypnfs == PNFS_NOT_ATTEMPTED)
1748 pnfs_read_through_mds(desc, hdr);
1749 pnfs_put_lseg(lseg);
1752 static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
1754 pnfs_put_lseg(hdr->lseg);
1755 nfs_pgio_header_free(hdr);
1757 EXPORT_SYMBOL_GPL(pnfs_readhdr_free);
1760 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
1762 struct nfs_pgio_header *hdr;
1765 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
1767 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
1769 pnfs_put_lseg(desc->pg_lseg);
1770 desc->pg_lseg = NULL;
1773 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
1774 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
1775 ret = nfs_generic_pgio(desc, hdr);
1777 pnfs_put_lseg(desc->pg_lseg);
1778 desc->pg_lseg = NULL;
1780 pnfs_do_read(desc, hdr);
1783 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
1785 static void pnfs_clear_layoutcommitting(struct inode *inode)
1787 unsigned long *bitlock = &NFS_I(inode)->flags;
1789 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
1790 smp_mb__after_atomic();
1791 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
1795 * There can be multiple RW segments.
1797 static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
1799 struct pnfs_layout_segment *lseg;
1801 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
1802 if (lseg->pls_range.iomode == IOMODE_RW &&
1803 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
1804 list_add(&lseg->pls_lc_list, listp);
1808 static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
1810 struct pnfs_layout_segment *lseg, *tmp;
1812 /* Matched by references in pnfs_set_layoutcommit */
1813 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
1814 list_del_init(&lseg->pls_lc_list);
1815 pnfs_put_lseg(lseg);
1818 pnfs_clear_layoutcommitting(inode);
1821 void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
1823 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
1825 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
1828 pnfs_set_layoutcommit(struct nfs_pgio_header *hdr)
1830 struct inode *inode = hdr->inode;
1831 struct nfs_inode *nfsi = NFS_I(inode);
1832 loff_t end_pos = hdr->mds_offset + hdr->res.count;
1833 bool mark_as_dirty = false;
1835 spin_lock(&inode->i_lock);
1836 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1837 mark_as_dirty = true;
1838 dprintk("%s: Set layoutcommit for inode %lu ",
1839 __func__, inode->i_ino);
1841 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) {
1842 /* references matched in nfs4_layoutcommit_release */
1843 pnfs_get_lseg(hdr->lseg);
1845 if (end_pos > nfsi->layout->plh_lwb)
1846 nfsi->layout->plh_lwb = end_pos;
1847 spin_unlock(&inode->i_lock);
1848 dprintk("%s: lseg %p end_pos %llu\n",
1849 __func__, hdr->lseg, nfsi->layout->plh_lwb);
1851 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1852 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1854 mark_inode_dirty_sync(inode);
1856 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1858 void pnfs_commit_set_layoutcommit(struct nfs_commit_data *data)
1860 struct inode *inode = data->inode;
1861 struct nfs_inode *nfsi = NFS_I(inode);
1862 bool mark_as_dirty = false;
1864 spin_lock(&inode->i_lock);
1865 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1866 mark_as_dirty = true;
1867 dprintk("%s: Set layoutcommit for inode %lu ",
1868 __func__, inode->i_ino);
1870 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &data->lseg->pls_flags)) {
1871 /* references matched in nfs4_layoutcommit_release */
1872 pnfs_get_lseg(data->lseg);
1874 if (data->lwb > nfsi->layout->plh_lwb)
1875 nfsi->layout->plh_lwb = data->lwb;
1876 spin_unlock(&inode->i_lock);
1877 dprintk("%s: lseg %p end_pos %llu\n",
1878 __func__, data->lseg, nfsi->layout->plh_lwb);
1880 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1881 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1883 mark_inode_dirty_sync(inode);
1885 EXPORT_SYMBOL_GPL(pnfs_commit_set_layoutcommit);
1887 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
1889 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
1891 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
1892 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
1893 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
1897 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1898 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1899 * data to disk to allow the server to recover the data if it crashes.
1900 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1901 * is off, and a COMMIT is sent to a data server, or
1902 * if WRITEs to a data server return NFS_DATA_SYNC.
1905 pnfs_layoutcommit_inode(struct inode *inode, bool sync)
1907 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
1908 struct nfs4_layoutcommit_data *data;
1909 struct nfs_inode *nfsi = NFS_I(inode);
1913 if (!pnfs_layoutcommit_outstanding(inode))
1916 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1919 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
1922 status = wait_on_bit_lock_action(&nfsi->flags,
1923 NFS_INO_LAYOUTCOMMITTING,
1924 nfs_wait_bit_killable,
1931 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1932 data = kzalloc(sizeof(*data), GFP_NOFS);
1934 goto clear_layoutcommitting;
1937 spin_lock(&inode->i_lock);
1938 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1941 INIT_LIST_HEAD(&data->lseg_list);
1942 pnfs_list_write_lseg(inode, &data->lseg_list);
1944 end_pos = nfsi->layout->plh_lwb;
1945 nfsi->layout->plh_lwb = 0;
1947 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
1948 spin_unlock(&inode->i_lock);
1950 data->args.inode = inode;
1951 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
1952 nfs_fattr_init(&data->fattr);
1953 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1954 data->res.fattr = &data->fattr;
1955 data->args.lastbytewritten = end_pos - 1;
1956 data->res.server = NFS_SERVER(inode);
1958 if (ld->prepare_layoutcommit) {
1959 status = ld->prepare_layoutcommit(&data->args);
1961 spin_lock(&inode->i_lock);
1962 if (end_pos < nfsi->layout->plh_lwb)
1963 nfsi->layout->plh_lwb = end_pos;
1964 spin_unlock(&inode->i_lock);
1965 put_rpccred(data->cred);
1966 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
1967 goto clear_layoutcommitting;
1972 status = nfs4_proc_layoutcommit(data, sync);
1975 mark_inode_dirty_sync(inode);
1976 dprintk("<-- %s status %d\n", __func__, status);
1979 spin_unlock(&inode->i_lock);
1981 clear_layoutcommitting:
1982 pnfs_clear_layoutcommitting(inode);
1986 struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
1988 struct nfs4_threshold *thp;
1990 thp = kzalloc(sizeof(*thp), GFP_NOFS);
1992 dprintk("%s mdsthreshold allocation failed\n", __func__);