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"
42 #define NFSDBG_FACILITY NFSDBG_PNFS
43 #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
48 * protects pnfs_modules_tbl.
50 static DEFINE_SPINLOCK(pnfs_spinlock);
53 * pnfs_modules_tbl holds all pnfs modules
55 static LIST_HEAD(pnfs_modules_tbl);
57 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo);
58 static void pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
59 struct list_head *free_me,
60 const struct pnfs_layout_range *range,
62 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
63 struct list_head *tmp_list);
65 /* Return the registered pnfs layout driver module matching given id */
66 static struct pnfs_layoutdriver_type *
67 find_pnfs_driver_locked(u32 id)
69 struct pnfs_layoutdriver_type *local;
71 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
76 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
80 static struct pnfs_layoutdriver_type *
81 find_pnfs_driver(u32 id)
83 struct pnfs_layoutdriver_type *local;
85 spin_lock(&pnfs_spinlock);
86 local = find_pnfs_driver_locked(id);
87 if (local != NULL && !try_module_get(local->owner)) {
88 dprintk("%s: Could not grab reference on module\n", __func__);
91 spin_unlock(&pnfs_spinlock);
96 unset_pnfs_layoutdriver(struct nfs_server *nfss)
98 if (nfss->pnfs_curr_ld) {
99 if (nfss->pnfs_curr_ld->clear_layoutdriver)
100 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
101 /* Decrement the MDS count. Purge the deviceid cache if zero */
102 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
103 nfs4_deviceid_purge_client(nfss->nfs_client);
104 module_put(nfss->pnfs_curr_ld->owner);
106 nfss->pnfs_curr_ld = NULL;
110 * When the server sends a list of layout types, we choose one in the order
111 * given in the list below.
113 * FIXME: should this list be configurable in some fashion? module param?
114 * mount option? something else?
116 static const u32 ld_prefs[] = {
121 LAYOUT_NFSV4_1_FILES,
126 ld_cmp(const void *e1, const void *e2)
128 u32 ld1 = *((u32 *)e1);
129 u32 ld2 = *((u32 *)e2);
132 for (i = 0; ld_prefs[i] != 0; i++) {
133 if (ld1 == ld_prefs[i])
136 if (ld2 == ld_prefs[i])
143 * Try to set the server's pnfs module to the pnfs layout type specified by id.
144 * Currently only one pNFS layout driver per filesystem is supported.
146 * @ids array of layout types supported by MDS.
149 set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
150 struct nfs_fsinfo *fsinfo)
152 struct pnfs_layoutdriver_type *ld_type = NULL;
156 if (fsinfo->nlayouttypes == 0)
158 if (!(server->nfs_client->cl_exchange_flags &
159 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
160 printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n",
161 __func__, server->nfs_client->cl_exchange_flags);
165 sort(fsinfo->layouttype, fsinfo->nlayouttypes,
166 sizeof(*fsinfo->layouttype), ld_cmp, NULL);
168 for (i = 0; i < fsinfo->nlayouttypes; i++) {
169 id = fsinfo->layouttype[i];
170 ld_type = find_pnfs_driver(id);
172 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX,
174 ld_type = find_pnfs_driver(id);
181 dprintk("%s: No pNFS module found!\n", __func__);
185 server->pnfs_curr_ld = ld_type;
186 if (ld_type->set_layoutdriver
187 && ld_type->set_layoutdriver(server, mntfh)) {
188 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
189 "driver %u.\n", __func__, id);
190 module_put(ld_type->owner);
193 /* Bump the MDS count */
194 atomic_inc(&server->nfs_client->cl_mds_count);
196 dprintk("%s: pNFS module for %u set\n", __func__, id);
200 dprintk("%s: Using NFSv4 I/O\n", __func__);
201 server->pnfs_curr_ld = NULL;
205 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
207 int status = -EINVAL;
208 struct pnfs_layoutdriver_type *tmp;
210 if (ld_type->id == 0) {
211 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
214 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
215 printk(KERN_ERR "NFS: %s Layout driver must provide "
216 "alloc_lseg and free_lseg.\n", __func__);
220 spin_lock(&pnfs_spinlock);
221 tmp = find_pnfs_driver_locked(ld_type->id);
223 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
225 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
228 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
229 __func__, ld_type->id);
231 spin_unlock(&pnfs_spinlock);
235 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
238 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
240 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
241 spin_lock(&pnfs_spinlock);
242 list_del(&ld_type->pnfs_tblid);
243 spin_unlock(&pnfs_spinlock);
245 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
248 * pNFS client layout cache
251 /* Need to hold i_lock if caller does not already hold reference */
253 pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
255 refcount_inc(&lo->plh_refcount);
258 static struct pnfs_layout_hdr *
259 pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
261 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
262 return ld->alloc_layout_hdr(ino, gfp_flags);
266 pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
268 struct nfs_server *server = NFS_SERVER(lo->plh_inode);
269 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
271 if (!list_empty(&lo->plh_layouts)) {
272 struct nfs_client *clp = server->nfs_client;
274 spin_lock(&clp->cl_lock);
275 list_del_init(&lo->plh_layouts);
276 spin_unlock(&clp->cl_lock);
278 put_rpccred(lo->plh_lc_cred);
279 return ld->free_layout_hdr(lo);
283 pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
285 struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
286 dprintk("%s: freeing layout cache %p\n", __func__, lo);
288 /* Reset MDS Threshold I/O counters */
294 pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
297 unsigned long i_state;
301 inode = lo->plh_inode;
302 pnfs_layoutreturn_before_put_layout_hdr(lo);
304 if (refcount_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
305 if (!list_empty(&lo->plh_segs))
306 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
307 pnfs_detach_layout_hdr(lo);
308 i_state = inode->i_state;
309 spin_unlock(&inode->i_lock);
310 pnfs_free_layout_hdr(lo);
311 /* Notify pnfs_destroy_layout_final() that we're done */
312 if (i_state & (I_FREEING | I_CLEAR))
318 pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
321 if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
323 lo->plh_return_iomode = iomode;
324 set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
326 WARN_ON_ONCE(lo->plh_return_seq != 0 && lo->plh_return_seq != seq);
327 lo->plh_return_seq = seq;
332 pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo)
334 struct pnfs_layout_segment *lseg;
335 lo->plh_return_iomode = 0;
336 lo->plh_return_seq = 0;
337 clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
338 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
339 if (!test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
341 pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
345 static void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
347 clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
348 clear_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags);
349 smp_mb__after_atomic();
350 wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
351 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
355 pnfs_clear_lseg_state(struct pnfs_layout_segment *lseg,
356 struct list_head *free_me)
358 clear_bit(NFS_LSEG_ROC, &lseg->pls_flags);
359 clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
360 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags))
361 pnfs_lseg_dec_and_remove_zero(lseg, free_me);
362 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
363 pnfs_lseg_dec_and_remove_zero(lseg, free_me);
367 * Update the seqid of a layout stateid
369 bool nfs4_layoutreturn_refresh_stateid(nfs4_stateid *dst,
370 struct pnfs_layout_range *dst_range,
373 struct pnfs_layout_hdr *lo;
374 struct pnfs_layout_range range = {
375 .iomode = IOMODE_ANY,
377 .length = NFS4_MAX_UINT64,
383 spin_lock(&inode->i_lock);
384 lo = NFS_I(inode)->layout;
385 if (lo && nfs4_stateid_match_other(dst, &lo->plh_stateid)) {
386 err = pnfs_mark_matching_lsegs_return(lo, &head, &range, 0);
388 dst->seqid = lo->plh_stateid.seqid;
393 spin_unlock(&inode->i_lock);
394 pnfs_free_lseg_list(&head);
399 * Mark a pnfs_layout_hdr and all associated layout segments as invalid
401 * In order to continue using the pnfs_layout_hdr, a full recovery
403 * Note that caller must hold inode->i_lock.
406 pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
407 struct list_head *lseg_list)
409 struct pnfs_layout_range range = {
410 .iomode = IOMODE_ANY,
412 .length = NFS4_MAX_UINT64,
414 struct pnfs_layout_segment *lseg, *next;
416 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
417 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
418 pnfs_clear_lseg_state(lseg, lseg_list);
419 pnfs_clear_layoutreturn_info(lo);
420 pnfs_free_returned_lsegs(lo, lseg_list, &range, 0);
421 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) &&
422 !test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
423 pnfs_clear_layoutreturn_waitbit(lo);
424 return !list_empty(&lo->plh_segs);
428 pnfs_iomode_to_fail_bit(u32 iomode)
430 return iomode == IOMODE_RW ?
431 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
435 pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
437 lo->plh_retry_timestamp = jiffies;
438 if (!test_and_set_bit(fail_bit, &lo->plh_flags))
439 refcount_inc(&lo->plh_refcount);
443 pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
445 if (test_and_clear_bit(fail_bit, &lo->plh_flags))
446 refcount_dec(&lo->plh_refcount);
450 pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
452 struct inode *inode = lo->plh_inode;
453 struct pnfs_layout_range range = {
456 .length = NFS4_MAX_UINT64,
460 spin_lock(&inode->i_lock);
461 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
462 pnfs_mark_matching_lsegs_invalid(lo, &head, &range, 0);
463 spin_unlock(&inode->i_lock);
464 pnfs_free_lseg_list(&head);
465 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
466 iomode == IOMODE_RW ? "RW" : "READ");
470 pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
472 unsigned long start, end;
473 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
475 if (test_bit(fail_bit, &lo->plh_flags) == 0)
478 start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
479 if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
480 /* It is time to retry the failed layoutgets */
481 pnfs_layout_clear_fail_bit(lo, fail_bit);
488 pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
489 const struct pnfs_layout_range *range,
490 const nfs4_stateid *stateid)
492 INIT_LIST_HEAD(&lseg->pls_list);
493 INIT_LIST_HEAD(&lseg->pls_lc_list);
494 refcount_set(&lseg->pls_refcount, 1);
495 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
496 lseg->pls_layout = lo;
497 lseg->pls_range = *range;
498 lseg->pls_seq = be32_to_cpu(stateid->seqid);
501 static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
504 struct inode *inode = lseg->pls_layout->plh_inode;
505 NFS_SERVER(inode)->pnfs_curr_ld->free_lseg(lseg);
510 pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
511 struct pnfs_layout_segment *lseg)
513 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
514 list_del_init(&lseg->pls_list);
515 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
516 refcount_dec(&lo->plh_refcount);
517 if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
519 if (list_empty(&lo->plh_segs) &&
520 !test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) &&
521 !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
522 if (atomic_read(&lo->plh_outstanding) == 0)
523 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
524 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
529 pnfs_cache_lseg_for_layoutreturn(struct pnfs_layout_hdr *lo,
530 struct pnfs_layout_segment *lseg)
532 if (test_and_clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
533 pnfs_layout_is_valid(lo)) {
534 pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
535 list_move_tail(&lseg->pls_list, &lo->plh_return_segs);
542 pnfs_put_lseg(struct pnfs_layout_segment *lseg)
544 struct pnfs_layout_hdr *lo;
550 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
551 refcount_read(&lseg->pls_refcount),
552 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
554 lo = lseg->pls_layout;
555 inode = lo->plh_inode;
557 if (refcount_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
558 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
559 spin_unlock(&inode->i_lock);
562 pnfs_get_layout_hdr(lo);
563 pnfs_layout_remove_lseg(lo, lseg);
564 if (pnfs_cache_lseg_for_layoutreturn(lo, lseg))
566 spin_unlock(&inode->i_lock);
567 pnfs_free_lseg(lseg);
568 pnfs_put_layout_hdr(lo);
571 EXPORT_SYMBOL_GPL(pnfs_put_lseg);
574 * is l2 fully contained in l1?
576 * [----------------------------------)
581 pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
582 const struct pnfs_layout_range *l2)
584 u64 start1 = l1->offset;
585 u64 end1 = pnfs_end_offset(start1, l1->length);
586 u64 start2 = l2->offset;
587 u64 end2 = pnfs_end_offset(start2, l2->length);
589 return (start1 <= start2) && (end1 >= end2);
592 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
593 struct list_head *tmp_list)
595 if (!refcount_dec_and_test(&lseg->pls_refcount))
597 pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
598 list_add(&lseg->pls_list, tmp_list);
602 /* Returns 1 if lseg is removed from list, 0 otherwise */
603 static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
604 struct list_head *tmp_list)
608 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
609 /* Remove the reference keeping the lseg in the
610 * list. It will now be removed when all
611 * outstanding io is finished.
613 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
614 refcount_read(&lseg->pls_refcount));
615 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
622 * Compare 2 layout stateid sequence ids, to see which is newer,
623 * taking into account wraparound issues.
625 static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
627 return (s32)(s1 - s2) > 0;
631 pnfs_should_free_range(const struct pnfs_layout_range *lseg_range,
632 const struct pnfs_layout_range *recall_range)
634 return (recall_range->iomode == IOMODE_ANY ||
635 lseg_range->iomode == recall_range->iomode) &&
636 pnfs_lseg_range_intersecting(lseg_range, recall_range);
640 pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg,
641 const struct pnfs_layout_range *recall_range,
644 if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq))
646 if (recall_range == NULL)
648 return pnfs_should_free_range(&lseg->pls_range, recall_range);
652 * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later
653 * @lo: layout header containing the lsegs
654 * @tmp_list: list head where doomed lsegs should go
655 * @recall_range: optional recall range argument to match (may be NULL)
656 * @seq: only invalidate lsegs obtained prior to this sequence (may be 0)
658 * Walk the list of lsegs in the layout header, and tear down any that should
659 * be destroyed. If "recall_range" is specified then the segment must match
660 * that range. If "seq" is non-zero, then only match segments that were handed
661 * out at or before that sequence.
663 * Returns number of matching invalid lsegs remaining in list after scanning
664 * it and purging them.
667 pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
668 struct list_head *tmp_list,
669 const struct pnfs_layout_range *recall_range,
672 struct pnfs_layout_segment *lseg, *next;
675 dprintk("%s:Begin lo %p\n", __func__, lo);
677 if (list_empty(&lo->plh_segs))
679 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
680 if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
681 dprintk("%s: freeing lseg %p iomode %d seq %u "
682 "offset %llu length %llu\n", __func__,
683 lseg, lseg->pls_range.iomode, lseg->pls_seq,
684 lseg->pls_range.offset, lseg->pls_range.length);
685 if (!mark_lseg_invalid(lseg, tmp_list))
688 dprintk("%s:Return %i\n", __func__, remaining);
693 pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
694 struct list_head *free_me,
695 const struct pnfs_layout_range *range,
698 struct pnfs_layout_segment *lseg, *next;
700 list_for_each_entry_safe(lseg, next, &lo->plh_return_segs, pls_list) {
701 if (pnfs_match_lseg_recall(lseg, range, seq))
702 list_move_tail(&lseg->pls_list, free_me);
706 /* note free_me must contain lsegs from a single layout_hdr */
708 pnfs_free_lseg_list(struct list_head *free_me)
710 struct pnfs_layout_segment *lseg, *tmp;
712 if (list_empty(free_me))
715 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
716 list_del(&lseg->pls_list);
717 pnfs_free_lseg(lseg);
721 static struct pnfs_layout_hdr *__pnfs_destroy_layout(struct nfs_inode *nfsi)
723 struct pnfs_layout_hdr *lo;
726 spin_lock(&nfsi->vfs_inode.i_lock);
729 pnfs_get_layout_hdr(lo);
730 pnfs_mark_layout_stateid_invalid(lo, &tmp_list);
731 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
732 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
733 spin_unlock(&nfsi->vfs_inode.i_lock);
734 pnfs_free_lseg_list(&tmp_list);
735 nfs_commit_inode(&nfsi->vfs_inode, 0);
736 pnfs_put_layout_hdr(lo);
738 spin_unlock(&nfsi->vfs_inode.i_lock);
742 void pnfs_destroy_layout(struct nfs_inode *nfsi)
744 __pnfs_destroy_layout(nfsi);
746 EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
748 static bool pnfs_layout_removed(struct nfs_inode *nfsi,
749 struct pnfs_layout_hdr *lo)
753 spin_lock(&nfsi->vfs_inode.i_lock);
754 ret = nfsi->layout != lo;
755 spin_unlock(&nfsi->vfs_inode.i_lock);
759 void pnfs_destroy_layout_final(struct nfs_inode *nfsi)
761 struct pnfs_layout_hdr *lo = __pnfs_destroy_layout(nfsi);
764 wait_var_event(lo, pnfs_layout_removed(nfsi, lo));
768 pnfs_layout_add_bulk_destroy_list(struct inode *inode,
769 struct list_head *layout_list)
771 struct pnfs_layout_hdr *lo;
774 spin_lock(&inode->i_lock);
775 lo = NFS_I(inode)->layout;
776 if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
777 pnfs_get_layout_hdr(lo);
778 list_add(&lo->plh_bulk_destroy, layout_list);
781 spin_unlock(&inode->i_lock);
785 /* Caller must hold rcu_read_lock and clp->cl_lock */
787 pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
788 struct nfs_server *server,
789 struct list_head *layout_list)
790 __must_hold(&clp->cl_lock)
793 struct pnfs_layout_hdr *lo, *next;
796 list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
797 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
798 test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) ||
799 !list_empty(&lo->plh_bulk_destroy))
801 /* If the sb is being destroyed, just bail */
802 if (!nfs_sb_active(server->super))
804 inode = igrab(lo->plh_inode);
806 list_del_init(&lo->plh_layouts);
807 if (pnfs_layout_add_bulk_destroy_list(inode,
811 spin_unlock(&clp->cl_lock);
815 spin_unlock(&clp->cl_lock);
816 set_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags);
818 nfs_sb_deactive(server->super);
819 spin_lock(&clp->cl_lock);
827 pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
830 struct pnfs_layout_hdr *lo;
832 LIST_HEAD(lseg_list);
835 while (!list_empty(layout_list)) {
836 lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
838 dprintk("%s freeing layout for inode %lu\n", __func__,
839 lo->plh_inode->i_ino);
840 inode = lo->plh_inode;
842 pnfs_layoutcommit_inode(inode, false);
844 spin_lock(&inode->i_lock);
845 list_del_init(&lo->plh_bulk_destroy);
846 if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
848 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
851 spin_unlock(&inode->i_lock);
852 pnfs_free_lseg_list(&lseg_list);
853 /* Free all lsegs that are attached to commit buckets */
854 nfs_commit_inode(inode, 0);
855 pnfs_put_layout_hdr(lo);
856 nfs_iput_and_deactive(inode);
862 pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
863 struct nfs_fsid *fsid,
866 struct nfs_server *server;
867 LIST_HEAD(layout_list);
869 spin_lock(&clp->cl_lock);
872 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
873 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
875 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
881 spin_unlock(&clp->cl_lock);
883 if (list_empty(&layout_list))
885 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
889 pnfs_destroy_layouts_byclid(struct nfs_client *clp,
892 struct nfs_server *server;
893 LIST_HEAD(layout_list);
895 spin_lock(&clp->cl_lock);
898 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
899 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
905 spin_unlock(&clp->cl_lock);
907 if (list_empty(&layout_list))
909 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
913 * Called by the state manger to remove all layouts established under an
917 pnfs_destroy_all_layouts(struct nfs_client *clp)
919 nfs4_deviceid_mark_client_invalid(clp);
920 nfs4_deviceid_purge_client(clp);
922 pnfs_destroy_layouts_byclid(clp, false);
925 /* update lo->plh_stateid with new if is more recent */
927 pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
930 u32 oldseq, newseq, new_barrier = 0;
932 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
933 newseq = be32_to_cpu(new->seqid);
935 if (!pnfs_layout_is_valid(lo)) {
936 nfs4_stateid_copy(&lo->plh_stateid, new);
937 lo->plh_barrier = newseq;
938 pnfs_clear_layoutreturn_info(lo);
939 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
942 if (pnfs_seqid_is_newer(newseq, oldseq)) {
943 nfs4_stateid_copy(&lo->plh_stateid, new);
945 * Because of wraparound, we want to keep the barrier
946 * "close" to the current seqids.
948 new_barrier = newseq - atomic_read(&lo->plh_outstanding);
951 new_barrier = be32_to_cpu(new->seqid);
952 else if (new_barrier == 0)
954 if (pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
955 lo->plh_barrier = new_barrier;
959 pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
960 const nfs4_stateid *stateid)
962 u32 seqid = be32_to_cpu(stateid->seqid);
964 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
967 /* lget is set to 1 if called from inside send_layoutget call chain */
969 pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
971 return lo->plh_block_lgets ||
972 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
975 static struct nfs_server *
976 pnfs_find_server(struct inode *inode, struct nfs_open_context *ctx)
978 struct nfs_server *server;
981 server = NFS_SERVER(inode);
983 struct dentry *parent_dir = dget_parent(ctx->dentry);
984 server = NFS_SERVER(parent_dir->d_inode);
990 static void nfs4_free_pages(struct page **pages, size_t size)
997 for (i = 0; i < size; i++) {
1000 __free_page(pages[i]);
1005 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
1007 struct page **pages;
1010 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
1012 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
1016 for (i = 0; i < size; i++) {
1017 pages[i] = alloc_page(gfp_flags);
1019 dprintk("%s: failed to allocate page\n", __func__);
1020 nfs4_free_pages(pages, size);
1028 static struct nfs4_layoutget *
1029 pnfs_alloc_init_layoutget_args(struct inode *ino,
1030 struct nfs_open_context *ctx,
1031 const nfs4_stateid *stateid,
1032 const struct pnfs_layout_range *range,
1035 struct nfs_server *server = pnfs_find_server(ino, ctx);
1036 size_t max_pages = max_response_pages(server);
1037 struct nfs4_layoutget *lgp;
1039 dprintk("--> %s\n", __func__);
1041 lgp = kzalloc(sizeof(*lgp), gfp_flags);
1045 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
1046 if (!lgp->args.layout.pages) {
1050 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
1051 lgp->res.layoutp = &lgp->args.layout;
1053 /* Don't confuse uninitialised result and success */
1054 lgp->res.status = -NFS4ERR_DELAY;
1056 lgp->args.minlength = PAGE_SIZE;
1057 if (lgp->args.minlength > range->length)
1058 lgp->args.minlength = range->length;
1060 loff_t i_size = i_size_read(ino);
1062 if (range->iomode == IOMODE_READ) {
1063 if (range->offset >= i_size)
1064 lgp->args.minlength = 0;
1065 else if (i_size - range->offset < lgp->args.minlength)
1066 lgp->args.minlength = i_size - range->offset;
1069 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
1070 pnfs_copy_range(&lgp->args.range, range);
1071 lgp->args.type = server->pnfs_curr_ld->id;
1072 lgp->args.inode = ino;
1073 lgp->args.ctx = get_nfs_open_context(ctx);
1074 nfs4_stateid_copy(&lgp->args.stateid, stateid);
1075 lgp->gfp_flags = gfp_flags;
1076 lgp->cred = get_rpccred(ctx->cred);
1080 void pnfs_layoutget_free(struct nfs4_layoutget *lgp)
1082 size_t max_pages = lgp->args.layout.pglen / PAGE_SIZE;
1084 nfs4_free_pages(lgp->args.layout.pages, max_pages);
1085 if (lgp->args.inode)
1086 pnfs_put_layout_hdr(NFS_I(lgp->args.inode)->layout);
1087 put_rpccred(lgp->cred);
1088 put_nfs_open_context(lgp->args.ctx);
1092 static void pnfs_clear_layoutcommit(struct inode *inode,
1093 struct list_head *head)
1095 struct nfs_inode *nfsi = NFS_I(inode);
1096 struct pnfs_layout_segment *lseg, *tmp;
1098 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1100 list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
1101 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
1103 pnfs_lseg_dec_and_remove_zero(lseg, head);
1107 void pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr *lo,
1108 const nfs4_stateid *arg_stateid,
1109 const struct pnfs_layout_range *range,
1110 const nfs4_stateid *stateid)
1112 struct inode *inode = lo->plh_inode;
1115 spin_lock(&inode->i_lock);
1116 if (!pnfs_layout_is_valid(lo) || !arg_stateid ||
1117 !nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid))
1120 u32 seq = be32_to_cpu(arg_stateid->seqid);
1122 pnfs_mark_matching_lsegs_invalid(lo, &freeme, range, seq);
1123 pnfs_free_returned_lsegs(lo, &freeme, range, seq);
1124 pnfs_set_layout_stateid(lo, stateid, true);
1126 pnfs_mark_layout_stateid_invalid(lo, &freeme);
1128 pnfs_clear_layoutreturn_waitbit(lo);
1129 spin_unlock(&inode->i_lock);
1130 pnfs_free_lseg_list(&freeme);
1135 pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
1136 nfs4_stateid *stateid,
1137 enum pnfs_iomode *iomode)
1139 /* Serialise LAYOUTGET/LAYOUTRETURN */
1140 if (atomic_read(&lo->plh_outstanding) != 0)
1142 if (test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
1144 set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
1145 pnfs_get_layout_hdr(lo);
1146 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
1147 if (stateid != NULL) {
1148 nfs4_stateid_copy(stateid, &lo->plh_stateid);
1149 if (lo->plh_return_seq != 0)
1150 stateid->seqid = cpu_to_be32(lo->plh_return_seq);
1153 *iomode = lo->plh_return_iomode;
1154 pnfs_clear_layoutreturn_info(lo);
1157 if (stateid != NULL)
1158 nfs4_stateid_copy(stateid, &lo->plh_stateid);
1160 *iomode = IOMODE_ANY;
1165 pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args *args,
1166 struct pnfs_layout_hdr *lo,
1167 const nfs4_stateid *stateid,
1168 enum pnfs_iomode iomode)
1170 struct inode *inode = lo->plh_inode;
1172 args->layout_type = NFS_SERVER(inode)->pnfs_curr_ld->id;
1173 args->inode = inode;
1174 args->range.iomode = iomode;
1175 args->range.offset = 0;
1176 args->range.length = NFS4_MAX_UINT64;
1178 nfs4_stateid_copy(&args->stateid, stateid);
1182 pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, const nfs4_stateid *stateid,
1183 enum pnfs_iomode iomode, bool sync)
1185 struct inode *ino = lo->plh_inode;
1186 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
1187 struct nfs4_layoutreturn *lrp;
1190 lrp = kzalloc(sizeof(*lrp), GFP_NOFS);
1191 if (unlikely(lrp == NULL)) {
1193 spin_lock(&ino->i_lock);
1194 pnfs_clear_layoutreturn_waitbit(lo);
1195 spin_unlock(&ino->i_lock);
1196 pnfs_put_layout_hdr(lo);
1200 pnfs_init_layoutreturn_args(&lrp->args, lo, stateid, iomode);
1201 lrp->args.ld_private = &lrp->ld_private;
1202 lrp->clp = NFS_SERVER(ino)->nfs_client;
1203 lrp->cred = lo->plh_lc_cred;
1204 if (ld->prepare_layoutreturn)
1205 ld->prepare_layoutreturn(&lrp->args);
1207 status = nfs4_proc_layoutreturn(lrp, sync);
1209 dprintk("<-- %s status: %d\n", __func__, status);
1214 pnfs_layout_segments_returnable(struct pnfs_layout_hdr *lo,
1215 enum pnfs_iomode iomode,
1218 struct pnfs_layout_range recall_range = {
1219 .length = NFS4_MAX_UINT64,
1222 return pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs,
1223 &recall_range, seq) != -EBUSY;
1226 /* Return true if layoutreturn is needed */
1228 pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
1230 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1232 return pnfs_layout_segments_returnable(lo, lo->plh_return_iomode,
1233 lo->plh_return_seq);
1236 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
1238 struct inode *inode= lo->plh_inode;
1240 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1242 spin_lock(&inode->i_lock);
1243 if (pnfs_layout_need_return(lo)) {
1244 nfs4_stateid stateid;
1245 enum pnfs_iomode iomode;
1248 send = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
1249 spin_unlock(&inode->i_lock);
1251 /* Send an async layoutreturn so we dont deadlock */
1252 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1255 spin_unlock(&inode->i_lock);
1259 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
1260 * when the layout segment list is empty.
1262 * Note that a pnfs_layout_hdr can exist with an empty layout segment
1263 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
1264 * deviceid is marked invalid.
1267 _pnfs_return_layout(struct inode *ino)
1269 struct pnfs_layout_hdr *lo = NULL;
1270 struct nfs_inode *nfsi = NFS_I(ino);
1271 struct pnfs_layout_range range = {
1272 .iomode = IOMODE_ANY,
1274 .length = NFS4_MAX_UINT64,
1276 LIST_HEAD(tmp_list);
1277 nfs4_stateid stateid;
1279 bool send, valid_layout;
1281 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
1283 spin_lock(&ino->i_lock);
1286 spin_unlock(&ino->i_lock);
1287 dprintk("NFS: %s no layout to return\n", __func__);
1290 /* Reference matched in nfs4_layoutreturn_release */
1291 pnfs_get_layout_hdr(lo);
1292 /* Is there an outstanding layoutreturn ? */
1293 if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
1294 spin_unlock(&ino->i_lock);
1295 if (wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
1296 TASK_UNINTERRUPTIBLE))
1297 goto out_put_layout_hdr;
1298 spin_lock(&ino->i_lock);
1300 valid_layout = pnfs_layout_is_valid(lo);
1301 pnfs_clear_layoutcommit(ino, &tmp_list);
1302 pnfs_mark_matching_lsegs_return(lo, &tmp_list, &range, 0);
1304 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range)
1305 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1307 /* Don't send a LAYOUTRETURN if list was initially empty */
1308 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) ||
1310 spin_unlock(&ino->i_lock);
1311 dprintk("NFS: %s no layout segments to return\n", __func__);
1312 goto out_put_layout_hdr;
1315 send = pnfs_prepare_layoutreturn(lo, &stateid, NULL);
1316 spin_unlock(&ino->i_lock);
1318 status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
1320 pnfs_free_lseg_list(&tmp_list);
1321 pnfs_put_layout_hdr(lo);
1323 dprintk("<-- %s status: %d\n", __func__, status);
1328 pnfs_commit_and_return_layout(struct inode *inode)
1330 struct pnfs_layout_hdr *lo;
1333 spin_lock(&inode->i_lock);
1334 lo = NFS_I(inode)->layout;
1336 spin_unlock(&inode->i_lock);
1339 pnfs_get_layout_hdr(lo);
1340 /* Block new layoutgets and read/write to ds */
1341 lo->plh_block_lgets++;
1342 spin_unlock(&inode->i_lock);
1343 filemap_fdatawait(inode->i_mapping);
1344 ret = pnfs_layoutcommit_inode(inode, true);
1346 ret = _pnfs_return_layout(inode);
1347 spin_lock(&inode->i_lock);
1348 lo->plh_block_lgets--;
1349 spin_unlock(&inode->i_lock);
1350 pnfs_put_layout_hdr(lo);
1354 bool pnfs_roc(struct inode *ino,
1355 struct nfs4_layoutreturn_args *args,
1356 struct nfs4_layoutreturn_res *res,
1357 const struct rpc_cred *cred)
1359 struct nfs_inode *nfsi = NFS_I(ino);
1360 struct nfs_open_context *ctx;
1361 struct nfs4_state *state;
1362 struct pnfs_layout_hdr *lo;
1363 struct pnfs_layout_segment *lseg, *next;
1364 nfs4_stateid stateid;
1365 enum pnfs_iomode iomode = 0;
1366 bool layoutreturn = false, roc = false;
1367 bool skip_read = false;
1369 if (!nfs_have_layout(ino))
1372 spin_lock(&ino->i_lock);
1374 if (!lo || !pnfs_layout_is_valid(lo) ||
1375 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1379 pnfs_get_layout_hdr(lo);
1380 if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
1381 spin_unlock(&ino->i_lock);
1382 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
1383 TASK_UNINTERRUPTIBLE);
1384 pnfs_put_layout_hdr(lo);
1388 /* no roc if we hold a delegation */
1389 if (nfs4_check_delegation(ino, FMODE_READ)) {
1390 if (nfs4_check_delegation(ino, FMODE_WRITE))
1395 list_for_each_entry(ctx, &nfsi->open_files, list) {
1399 /* Don't return layout if there is open file state */
1400 if (state->state & FMODE_WRITE)
1402 if (state->state & FMODE_READ)
1407 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) {
1408 if (skip_read && lseg->pls_range.iomode == IOMODE_READ)
1410 /* If we are sending layoutreturn, invalidate all valid lsegs */
1411 if (!test_and_clear_bit(NFS_LSEG_ROC, &lseg->pls_flags))
1414 * Note: mark lseg for return so pnfs_layout_remove_lseg
1415 * doesn't invalidate the layout for us.
1417 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
1418 if (!mark_lseg_invalid(lseg, &lo->plh_return_segs))
1420 pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
1423 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1426 /* ROC in two conditions:
1427 * 1. there are ROC lsegs
1428 * 2. we don't send layoutreturn
1430 /* lo ref dropped in pnfs_roc_release() */
1431 layoutreturn = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
1432 /* If the creds don't match, we can't compound the layoutreturn */
1433 if (!layoutreturn || cred != lo->plh_lc_cred)
1437 pnfs_init_layoutreturn_args(args, lo, &stateid, iomode);
1438 res->lrs_present = 0;
1439 layoutreturn = false;
1442 spin_unlock(&ino->i_lock);
1443 pnfs_layoutcommit_inode(ino, true);
1445 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
1446 if (ld->prepare_layoutreturn)
1447 ld->prepare_layoutreturn(args);
1448 pnfs_put_layout_hdr(lo);
1452 pnfs_send_layoutreturn(lo, &stateid, iomode, true);
1453 pnfs_put_layout_hdr(lo);
1457 void pnfs_roc_release(struct nfs4_layoutreturn_args *args,
1458 struct nfs4_layoutreturn_res *res,
1461 struct pnfs_layout_hdr *lo = args->layout;
1462 struct inode *inode = args->inode;
1463 const nfs4_stateid *arg_stateid = NULL;
1464 const nfs4_stateid *res_stateid = NULL;
1465 struct nfs4_xdr_opaque_data *ld_private = args->ld_private;
1468 case -NFS4ERR_NOMATCHING_LAYOUT:
1469 spin_lock(&inode->i_lock);
1470 if (pnfs_layout_is_valid(lo) &&
1471 nfs4_stateid_match_other(&args->stateid, &lo->plh_stateid))
1472 pnfs_set_plh_return_info(lo, args->range.iomode, 0);
1473 spin_unlock(&inode->i_lock);
1476 if (res->lrs_present)
1477 res_stateid = &res->stateid;
1480 arg_stateid = &args->stateid;
1482 pnfs_layoutreturn_free_lsegs(lo, arg_stateid, &args->range,
1484 if (ld_private && ld_private->ops && ld_private->ops->free)
1485 ld_private->ops->free(ld_private);
1486 pnfs_put_layout_hdr(lo);
1487 trace_nfs4_layoutreturn_on_close(args->inode, 0);
1490 bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
1492 struct nfs_inode *nfsi = NFS_I(ino);
1493 struct pnfs_layout_hdr *lo;
1496 /* we might not have grabbed lo reference. so need to check under
1498 spin_lock(&ino->i_lock);
1500 if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1501 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
1504 spin_unlock(&ino->i_lock);
1509 * Compare two layout segments for sorting into layout cache.
1510 * We want to preferentially return RW over RO layouts, so ensure those
1514 pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
1515 const struct pnfs_layout_range *l2)
1519 /* high offset > low offset */
1520 d = l1->offset - l2->offset;
1524 /* short length > long length */
1525 d = l2->length - l1->length;
1529 /* read > read/write */
1530 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
1534 pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
1535 const struct pnfs_layout_range *l2)
1537 return pnfs_lseg_range_cmp(l1, l2) > 0;
1541 pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
1542 struct pnfs_layout_segment *old)
1548 pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1549 struct pnfs_layout_segment *lseg,
1550 bool (*is_after)(const struct pnfs_layout_range *,
1551 const struct pnfs_layout_range *),
1552 bool (*do_merge)(struct pnfs_layout_segment *,
1553 struct pnfs_layout_segment *),
1554 struct list_head *free_me)
1556 struct pnfs_layout_segment *lp, *tmp;
1558 dprintk("%s:Begin\n", __func__);
1560 list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
1561 if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
1563 if (do_merge(lseg, lp)) {
1564 mark_lseg_invalid(lp, free_me);
1567 if (is_after(&lseg->pls_range, &lp->pls_range))
1569 list_add_tail(&lseg->pls_list, &lp->pls_list);
1570 dprintk("%s: inserted lseg %p "
1571 "iomode %d offset %llu length %llu before "
1572 "lp %p iomode %d offset %llu length %llu\n",
1573 __func__, lseg, lseg->pls_range.iomode,
1574 lseg->pls_range.offset, lseg->pls_range.length,
1575 lp, lp->pls_range.iomode, lp->pls_range.offset,
1576 lp->pls_range.length);
1579 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1580 dprintk("%s: inserted lseg %p "
1581 "iomode %d offset %llu length %llu at tail\n",
1582 __func__, lseg, lseg->pls_range.iomode,
1583 lseg->pls_range.offset, lseg->pls_range.length);
1585 pnfs_get_layout_hdr(lo);
1587 dprintk("%s:Return\n", __func__);
1589 EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
1592 pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1593 struct pnfs_layout_segment *lseg,
1594 struct list_head *free_me)
1596 struct inode *inode = lo->plh_inode;
1597 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
1599 if (ld->add_lseg != NULL)
1600 ld->add_lseg(lo, lseg, free_me);
1602 pnfs_generic_layout_insert_lseg(lo, lseg,
1603 pnfs_lseg_range_is_after,
1608 static struct pnfs_layout_hdr *
1609 alloc_init_layout_hdr(struct inode *ino,
1610 struct nfs_open_context *ctx,
1613 struct pnfs_layout_hdr *lo;
1615 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
1618 refcount_set(&lo->plh_refcount, 1);
1619 INIT_LIST_HEAD(&lo->plh_layouts);
1620 INIT_LIST_HEAD(&lo->plh_segs);
1621 INIT_LIST_HEAD(&lo->plh_return_segs);
1622 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
1623 lo->plh_inode = ino;
1624 lo->plh_lc_cred = get_rpccred(ctx->cred);
1625 lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
1629 static struct pnfs_layout_hdr *
1630 pnfs_find_alloc_layout(struct inode *ino,
1631 struct nfs_open_context *ctx,
1633 __releases(&ino->i_lock)
1634 __acquires(&ino->i_lock)
1636 struct nfs_inode *nfsi = NFS_I(ino);
1637 struct pnfs_layout_hdr *new = NULL;
1639 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1641 if (nfsi->layout != NULL)
1643 spin_unlock(&ino->i_lock);
1644 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
1645 spin_lock(&ino->i_lock);
1647 if (likely(nfsi->layout == NULL)) { /* Won the race? */
1650 } else if (new != NULL)
1651 pnfs_free_layout_hdr(new);
1653 pnfs_get_layout_hdr(nfsi->layout);
1654 return nfsi->layout;
1658 * iomode matching rules:
1659 * iomode lseg strict match
1661 * ----- ----- ------ -----
1666 * READ READ N/A true
1667 * READ RW true false
1668 * READ RW false true
1671 pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
1672 const struct pnfs_layout_range *range,
1675 struct pnfs_layout_range range1;
1677 if ((range->iomode == IOMODE_RW &&
1678 ls_range->iomode != IOMODE_RW) ||
1679 (range->iomode != ls_range->iomode &&
1681 !pnfs_lseg_range_intersecting(ls_range, range))
1684 /* range1 covers only the first byte in the range */
1687 return pnfs_lseg_range_contained(ls_range, &range1);
1691 * lookup range in layout
1693 static struct pnfs_layout_segment *
1694 pnfs_find_lseg(struct pnfs_layout_hdr *lo,
1695 struct pnfs_layout_range *range,
1698 struct pnfs_layout_segment *lseg, *ret = NULL;
1700 dprintk("%s:Begin\n", __func__);
1702 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
1703 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
1704 pnfs_lseg_range_match(&lseg->pls_range, range,
1706 ret = pnfs_get_lseg(lseg);
1711 dprintk("%s:Return lseg %p ref %d\n",
1712 __func__, ret, ret ? refcount_read(&ret->pls_refcount) : 0);
1717 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1718 * to the MDS or over pNFS
1720 * The nfs_inode read_io and write_io fields are cumulative counters reset
1721 * when there are no layout segments. Note that in pnfs_update_layout iomode
1722 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1725 * A return of true means use MDS I/O.
1728 * If a file's size is smaller than the file size threshold, data accesses
1729 * SHOULD be sent to the metadata server. If an I/O request has a length that
1730 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1731 * server. If both file size and I/O size are provided, the client SHOULD
1732 * reach or exceed both thresholds before sending its read or write
1733 * requests to the data server.
1735 static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1736 struct inode *ino, int iomode)
1738 struct nfs4_threshold *t = ctx->mdsthreshold;
1739 struct nfs_inode *nfsi = NFS_I(ino);
1740 loff_t fsize = i_size_read(ino);
1741 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1746 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1747 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1751 if (t->bm & THRESHOLD_RD) {
1752 dprintk("%s fsize %llu\n", __func__, fsize);
1754 if (fsize < t->rd_sz)
1757 if (t->bm & THRESHOLD_RD_IO) {
1758 dprintk("%s nfsi->read_io %llu\n", __func__,
1761 if (nfsi->read_io < t->rd_io_sz)
1766 if (t->bm & THRESHOLD_WR) {
1767 dprintk("%s fsize %llu\n", __func__, fsize);
1769 if (fsize < t->wr_sz)
1772 if (t->bm & THRESHOLD_WR_IO) {
1773 dprintk("%s nfsi->write_io %llu\n", __func__,
1776 if (nfsi->write_io < t->wr_io_sz)
1781 if (size_set && io_set) {
1784 } else if (size || io)
1787 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1791 static int pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1794 * send layoutcommit as it can hold up layoutreturn due to lseg
1797 pnfs_layoutcommit_inode(lo->plh_inode, false);
1798 return wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
1799 nfs_wait_bit_killable,
1803 static void nfs_layoutget_begin(struct pnfs_layout_hdr *lo)
1805 atomic_inc(&lo->plh_outstanding);
1808 static void nfs_layoutget_end(struct pnfs_layout_hdr *lo)
1810 if (atomic_dec_and_test(&lo->plh_outstanding))
1811 wake_up_var(&lo->plh_outstanding);
1814 static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1816 unsigned long *bitlock = &lo->plh_flags;
1818 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1819 smp_mb__after_atomic();
1820 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1823 static void _add_to_server_list(struct pnfs_layout_hdr *lo,
1824 struct nfs_server *server)
1826 if (list_empty(&lo->plh_layouts)) {
1827 struct nfs_client *clp = server->nfs_client;
1829 /* The lo must be on the clp list if there is any
1830 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1832 spin_lock(&clp->cl_lock);
1833 if (list_empty(&lo->plh_layouts))
1834 list_add_tail(&lo->plh_layouts, &server->layouts);
1835 spin_unlock(&clp->cl_lock);
1840 * Layout segment is retreived from the server if not cached.
1841 * The appropriate layout segment is referenced and returned to the caller.
1843 struct pnfs_layout_segment *
1844 pnfs_update_layout(struct inode *ino,
1845 struct nfs_open_context *ctx,
1848 enum pnfs_iomode iomode,
1852 struct pnfs_layout_range arg = {
1858 struct nfs_server *server = NFS_SERVER(ino);
1859 struct nfs_client *clp = server->nfs_client;
1860 struct pnfs_layout_hdr *lo = NULL;
1861 struct pnfs_layout_segment *lseg = NULL;
1862 struct nfs4_layoutget *lgp;
1863 nfs4_stateid stateid;
1865 unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
1868 if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
1869 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1870 PNFS_UPDATE_LAYOUT_NO_PNFS);
1874 if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
1875 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1876 PNFS_UPDATE_LAYOUT_MDSTHRESH);
1881 lseg = ERR_PTR(nfs4_client_recover_expired_lease(clp));
1885 spin_lock(&ino->i_lock);
1886 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
1888 spin_unlock(&ino->i_lock);
1889 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1890 PNFS_UPDATE_LAYOUT_NOMEM);
1894 /* Do we even need to bother with this? */
1895 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1896 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1897 PNFS_UPDATE_LAYOUT_BULK_RECALL);
1898 dprintk("%s matches recall, use MDS\n", __func__);
1902 /* if LAYOUTGET already failed once we don't try again */
1903 if (pnfs_layout_io_test_failed(lo, iomode)) {
1904 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1905 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
1910 * If the layout segment list is empty, but there are outstanding
1911 * layoutget calls, then they might be subject to a layoutrecall.
1913 if (list_empty(&lo->plh_segs) &&
1914 atomic_read(&lo->plh_outstanding) != 0) {
1915 spin_unlock(&ino->i_lock);
1916 lseg = ERR_PTR(wait_var_event_killable(&lo->plh_outstanding,
1917 !atomic_read(&lo->plh_outstanding)));
1919 goto out_put_layout_hdr;
1920 pnfs_put_layout_hdr(lo);
1924 lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
1926 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1927 PNFS_UPDATE_LAYOUT_FOUND_CACHED);
1931 if (!nfs4_valid_open_stateid(ctx->state)) {
1932 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1933 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
1938 * Choose a stateid for the LAYOUTGET. If we don't have a layout
1939 * stateid, or it has been invalidated, then we must use the open
1942 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
1945 * The first layoutget for the file. Need to serialize per
1946 * RFC 5661 Errata 3208.
1948 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1950 spin_unlock(&ino->i_lock);
1951 lseg = ERR_PTR(wait_on_bit(&lo->plh_flags,
1952 NFS_LAYOUT_FIRST_LAYOUTGET,
1955 goto out_put_layout_hdr;
1956 pnfs_put_layout_hdr(lo);
1957 dprintk("%s retrying\n", __func__);
1962 if (nfs4_select_rw_stateid(ctx->state,
1963 iomode == IOMODE_RW ? FMODE_WRITE : FMODE_READ,
1964 NULL, &stateid, NULL) != 0) {
1965 trace_pnfs_update_layout(ino, pos, count,
1967 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
1971 nfs4_stateid_copy(&stateid, &lo->plh_stateid);
1975 * Because we free lsegs before sending LAYOUTRETURN, we need to wait
1976 * for LAYOUTRETURN even if first is true.
1978 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1979 spin_unlock(&ino->i_lock);
1980 dprintk("%s wait for layoutreturn\n", __func__);
1981 lseg = ERR_PTR(pnfs_prepare_to_retry_layoutget(lo));
1982 if (!IS_ERR(lseg)) {
1984 pnfs_clear_first_layoutget(lo);
1985 pnfs_put_layout_hdr(lo);
1986 dprintk("%s retrying\n", __func__);
1987 trace_pnfs_update_layout(ino, pos, count, iomode, lo,
1988 lseg, PNFS_UPDATE_LAYOUT_RETRY);
1991 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1992 PNFS_UPDATE_LAYOUT_RETURN);
1993 goto out_put_layout_hdr;
1996 if (pnfs_layoutgets_blocked(lo)) {
1997 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1998 PNFS_UPDATE_LAYOUT_BLOCKED);
2001 nfs_layoutget_begin(lo);
2002 spin_unlock(&ino->i_lock);
2004 _add_to_server_list(lo, server);
2006 pg_offset = arg.offset & ~PAGE_MASK;
2008 arg.offset -= pg_offset;
2009 arg.length += pg_offset;
2011 if (arg.length != NFS4_MAX_UINT64)
2012 arg.length = PAGE_ALIGN(arg.length);
2014 lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &stateid, &arg, gfp_flags);
2016 trace_pnfs_update_layout(ino, pos, count, iomode, lo, NULL,
2017 PNFS_UPDATE_LAYOUT_NOMEM);
2018 nfs_layoutget_end(lo);
2019 goto out_put_layout_hdr;
2022 lseg = nfs4_proc_layoutget(lgp, &timeout);
2023 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2024 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
2025 nfs_layoutget_end(lo);
2027 switch(PTR_ERR(lseg)) {
2029 if (time_after(jiffies, giveup))
2032 case -ERECALLCONFLICT:
2036 if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
2037 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
2040 goto out_put_layout_hdr;
2044 pnfs_clear_first_layoutget(lo);
2045 trace_pnfs_update_layout(ino, pos, count,
2046 iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
2047 pnfs_put_layout_hdr(lo);
2051 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
2056 pnfs_clear_first_layoutget(lo);
2057 pnfs_put_layout_hdr(lo);
2059 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
2060 "(%s, offset: %llu, length: %llu)\n",
2061 __func__, ino->i_sb->s_id,
2062 (unsigned long long)NFS_FILEID(ino),
2063 IS_ERR_OR_NULL(lseg) ? "not found" : "found",
2064 iomode==IOMODE_RW ? "read/write" : "read-only",
2065 (unsigned long long)pos,
2066 (unsigned long long)count);
2069 spin_unlock(&ino->i_lock);
2070 goto out_put_layout_hdr;
2072 EXPORT_SYMBOL_GPL(pnfs_update_layout);
2075 pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
2077 switch (range->iomode) {
2084 if (range->offset == NFS4_MAX_UINT64)
2086 if (range->length == 0)
2088 if (range->length != NFS4_MAX_UINT64 &&
2089 range->length > NFS4_MAX_UINT64 - range->offset)
2094 static struct pnfs_layout_hdr *
2095 _pnfs_grab_empty_layout(struct inode *ino, struct nfs_open_context *ctx)
2097 struct pnfs_layout_hdr *lo;
2099 spin_lock(&ino->i_lock);
2100 lo = pnfs_find_alloc_layout(ino, ctx, GFP_KERNEL);
2103 if (!test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags))
2105 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
2107 if (pnfs_layoutgets_blocked(lo))
2109 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags))
2111 nfs_layoutget_begin(lo);
2112 spin_unlock(&ino->i_lock);
2113 _add_to_server_list(lo, NFS_SERVER(ino));
2117 spin_unlock(&ino->i_lock);
2118 pnfs_put_layout_hdr(lo);
2122 extern const nfs4_stateid current_stateid;
2124 static void _lgopen_prepare_attached(struct nfs4_opendata *data,
2125 struct nfs_open_context *ctx)
2127 struct inode *ino = data->dentry->d_inode;
2128 struct pnfs_layout_range rng = {
2129 .iomode = (data->o_arg.fmode & FMODE_WRITE) ?
2130 IOMODE_RW: IOMODE_READ,
2132 .length = NFS4_MAX_UINT64,
2134 struct nfs4_layoutget *lgp;
2135 struct pnfs_layout_hdr *lo;
2137 /* Heuristic: don't send layoutget if we have cached data */
2138 if (rng.iomode == IOMODE_READ &&
2139 (i_size_read(ino) == 0 || ino->i_mapping->nrpages != 0))
2142 lo = _pnfs_grab_empty_layout(ino, ctx);
2145 lgp = pnfs_alloc_init_layoutget_args(ino, ctx, ¤t_stateid,
2148 pnfs_clear_first_layoutget(lo);
2149 nfs_layoutget_end(lo);
2150 pnfs_put_layout_hdr(lo);
2154 data->o_arg.lg_args = &lgp->args;
2155 data->o_res.lg_res = &lgp->res;
2158 static void _lgopen_prepare_floating(struct nfs4_opendata *data,
2159 struct nfs_open_context *ctx)
2161 struct pnfs_layout_range rng = {
2162 .iomode = (data->o_arg.fmode & FMODE_WRITE) ?
2163 IOMODE_RW: IOMODE_READ,
2165 .length = NFS4_MAX_UINT64,
2167 struct nfs4_layoutget *lgp;
2169 lgp = pnfs_alloc_init_layoutget_args(NULL, ctx, ¤t_stateid,
2174 data->o_arg.lg_args = &lgp->args;
2175 data->o_res.lg_res = &lgp->res;
2178 void pnfs_lgopen_prepare(struct nfs4_opendata *data,
2179 struct nfs_open_context *ctx)
2181 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
2183 if (!(pnfs_enabled_sb(server) &&
2184 server->pnfs_curr_ld->flags & PNFS_LAYOUTGET_ON_OPEN))
2186 /* Could check on max_ops, but currently hardcoded high enough */
2187 if (!nfs_server_capable(data->dir->d_inode, NFS_CAP_LGOPEN))
2190 _lgopen_prepare_attached(data, ctx);
2192 _lgopen_prepare_floating(data, ctx);
2195 void pnfs_parse_lgopen(struct inode *ino, struct nfs4_layoutget *lgp,
2196 struct nfs_open_context *ctx)
2198 struct pnfs_layout_hdr *lo;
2199 struct pnfs_layout_segment *lseg;
2200 struct nfs_server *srv = NFS_SERVER(ino);
2205 dprintk("%s: entered with status %i\n", __func__, lgp->res.status);
2206 if (lgp->res.status) {
2207 switch (lgp->res.status) {
2211 * Halt lgopen attempts if the server doesn't recognise
2212 * the "current stateid" value, the layout type, or the
2213 * layoutget operation as being valid.
2214 * Also if it complains about too many ops in the compound
2215 * or of the request/reply being too big.
2217 case -NFS4ERR_BAD_STATEID:
2218 case -NFS4ERR_NOTSUPP:
2219 case -NFS4ERR_REP_TOO_BIG:
2220 case -NFS4ERR_REP_TOO_BIG_TO_CACHE:
2221 case -NFS4ERR_REQ_TOO_BIG:
2222 case -NFS4ERR_TOO_MANY_OPS:
2223 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
2224 srv->caps &= ~NFS_CAP_LGOPEN;
2228 if (!lgp->args.inode) {
2229 lo = _pnfs_grab_empty_layout(ino, ctx);
2232 lgp->args.inode = ino;
2234 lo = NFS_I(lgp->args.inode)->layout;
2236 lseg = pnfs_layout_process(lgp);
2237 if (!IS_ERR(lseg)) {
2238 iomode = lgp->args.range.iomode;
2239 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
2240 pnfs_put_lseg(lseg);
2244 void nfs4_lgopen_release(struct nfs4_layoutget *lgp)
2247 struct inode *inode = lgp->args.inode;
2249 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
2250 pnfs_clear_first_layoutget(lo);
2251 nfs_layoutget_end(lo);
2253 pnfs_layoutget_free(lgp);
2257 struct pnfs_layout_segment *
2258 pnfs_layout_process(struct nfs4_layoutget *lgp)
2260 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
2261 struct nfs4_layoutget_res *res = &lgp->res;
2262 struct pnfs_layout_segment *lseg;
2263 struct inode *ino = lo->plh_inode;
2266 if (!pnfs_sanity_check_layout_range(&res->range))
2267 return ERR_PTR(-EINVAL);
2269 /* Inject layout blob into I/O device driver */
2270 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
2271 if (IS_ERR_OR_NULL(lseg)) {
2273 lseg = ERR_PTR(-ENOMEM);
2275 dprintk("%s: Could not allocate layout: error %ld\n",
2276 __func__, PTR_ERR(lseg));
2280 pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
2282 spin_lock(&ino->i_lock);
2283 if (pnfs_layoutgets_blocked(lo)) {
2284 dprintk("%s forget reply due to state\n", __func__);
2288 if (!pnfs_layout_is_valid(lo)) {
2289 /* We have a completely new layout */
2290 pnfs_set_layout_stateid(lo, &res->stateid, true);
2291 } else if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
2292 /* existing state ID, make sure the sequence number matches. */
2293 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
2294 dprintk("%s forget reply due to sequence\n", __func__);
2297 pnfs_set_layout_stateid(lo, &res->stateid, false);
2300 * We got an entirely new state ID. Mark all segments for the
2301 * inode invalid, and retry the layoutget
2303 struct pnfs_layout_range range = {
2304 .iomode = IOMODE_ANY,
2305 .length = NFS4_MAX_UINT64,
2307 pnfs_set_plh_return_info(lo, IOMODE_ANY, 0);
2308 pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs,
2313 pnfs_get_lseg(lseg);
2314 pnfs_layout_insert_lseg(lo, lseg, &free_me);
2317 if (res->return_on_close)
2318 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
2320 spin_unlock(&ino->i_lock);
2321 pnfs_free_lseg_list(&free_me);
2325 spin_unlock(&ino->i_lock);
2326 lseg->pls_layout = lo;
2327 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
2328 pnfs_free_lseg_list(&free_me);
2329 return ERR_PTR(-EAGAIN);
2333 * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
2334 * @lo: pointer to layout header
2335 * @tmp_list: list header to be used with pnfs_free_lseg_list()
2336 * @return_range: describe layout segment ranges to be returned
2337 * @seq: stateid seqid to match
2339 * This function is mainly intended for use by layoutrecall. It attempts
2340 * to free the layout segment immediately, or else to mark it for return
2341 * as soon as its reference count drops to zero.
2344 * - 0: a layoutreturn needs to be scheduled.
2345 * - EBUSY: there are layout segment that are still in use.
2346 * - ENOENT: there are no layout segments that need to be returned.
2349 pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
2350 struct list_head *tmp_list,
2351 const struct pnfs_layout_range *return_range,
2354 struct pnfs_layout_segment *lseg, *next;
2357 dprintk("%s:Begin lo %p\n", __func__, lo);
2359 assert_spin_locked(&lo->plh_inode->i_lock);
2361 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
2362 tmp_list = &lo->plh_return_segs;
2364 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
2365 if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
2366 dprintk("%s: marking lseg %p iomode %d "
2367 "offset %llu length %llu\n", __func__,
2368 lseg, lseg->pls_range.iomode,
2369 lseg->pls_range.offset,
2370 lseg->pls_range.length);
2371 if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
2372 tmp_list = &lo->plh_return_segs;
2373 if (mark_lseg_invalid(lseg, tmp_list))
2376 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
2380 pnfs_set_plh_return_info(lo, return_range->iomode, seq);
2384 if (!list_empty(&lo->plh_return_segs)) {
2385 pnfs_set_plh_return_info(lo, return_range->iomode, seq);
2392 void pnfs_error_mark_layout_for_return(struct inode *inode,
2393 struct pnfs_layout_segment *lseg)
2395 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
2396 struct pnfs_layout_range range = {
2397 .iomode = lseg->pls_range.iomode,
2399 .length = NFS4_MAX_UINT64,
2401 bool return_now = false;
2403 spin_lock(&inode->i_lock);
2404 if (!pnfs_layout_is_valid(lo)) {
2405 spin_unlock(&inode->i_lock);
2408 pnfs_set_plh_return_info(lo, range.iomode, 0);
2410 * mark all matching lsegs so that we are sure to have no live
2411 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
2414 if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, &range, 0) != -EBUSY) {
2415 nfs4_stateid stateid;
2416 enum pnfs_iomode iomode;
2418 return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
2419 spin_unlock(&inode->i_lock);
2421 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
2423 spin_unlock(&inode->i_lock);
2424 nfs_commit_inode(inode, 0);
2427 EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
2430 pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor *pgio)
2432 if (pgio->pg_lseg == NULL ||
2433 test_bit(NFS_LSEG_VALID, &pgio->pg_lseg->pls_flags))
2435 pnfs_put_lseg(pgio->pg_lseg);
2436 pgio->pg_lseg = NULL;
2438 EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_layout);
2441 * Check for any intersection between the request and the pgio->pg_lseg,
2442 * and if none, put this pgio->pg_lseg away.
2445 pnfs_generic_pg_check_range(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
2447 if (pgio->pg_lseg && !pnfs_lseg_request_intersecting(pgio->pg_lseg, req)) {
2448 pnfs_put_lseg(pgio->pg_lseg);
2449 pgio->pg_lseg = NULL;
2454 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
2456 u64 rd_size = req->wb_bytes;
2458 pnfs_generic_pg_check_layout(pgio);
2459 pnfs_generic_pg_check_range(pgio, req);
2460 if (pgio->pg_lseg == NULL) {
2461 if (pgio->pg_dreq == NULL)
2462 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
2464 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
2466 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2473 if (IS_ERR(pgio->pg_lseg)) {
2474 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2475 pgio->pg_lseg = NULL;
2479 /* If no lseg, fall back to read through mds */
2480 if (pgio->pg_lseg == NULL)
2481 nfs_pageio_reset_read_mds(pgio);
2484 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
2487 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
2488 struct nfs_page *req, u64 wb_size)
2490 pnfs_generic_pg_check_layout(pgio);
2491 pnfs_generic_pg_check_range(pgio, req);
2492 if (pgio->pg_lseg == NULL) {
2493 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2500 if (IS_ERR(pgio->pg_lseg)) {
2501 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2502 pgio->pg_lseg = NULL;
2506 /* If no lseg, fall back to write through mds */
2507 if (pgio->pg_lseg == NULL)
2508 nfs_pageio_reset_write_mds(pgio);
2510 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
2513 pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
2515 if (desc->pg_lseg) {
2516 pnfs_put_lseg(desc->pg_lseg);
2517 desc->pg_lseg = NULL;
2520 EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
2523 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
2524 * of bytes (maximum @req->wb_bytes) that can be coalesced.
2527 pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
2528 struct nfs_page *prev, struct nfs_page *req)
2531 u64 seg_end, req_start, seg_left;
2533 size = nfs_generic_pg_test(pgio, prev, req);
2538 * 'size' contains the number of bytes left in the current page (up
2539 * to the original size asked for in @req->wb_bytes).
2541 * Calculate how many bytes are left in the layout segment
2542 * and if there are less bytes than 'size', return that instead.
2544 * Please also note that 'end_offset' is actually the offset of the
2545 * first byte that lies outside the pnfs_layout_range. FIXME?
2548 if (pgio->pg_lseg) {
2549 seg_end = pnfs_end_offset(pgio->pg_lseg->pls_range.offset,
2550 pgio->pg_lseg->pls_range.length);
2551 req_start = req_offset(req);
2553 /* start of request is past the last byte of this segment */
2554 if (req_start >= seg_end)
2557 /* adjust 'size' iff there are fewer bytes left in the
2558 * segment than what nfs_generic_pg_test returned */
2559 seg_left = seg_end - req_start;
2560 if (seg_left < size)
2561 size = (unsigned int)seg_left;
2566 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
2568 int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
2570 struct nfs_pageio_descriptor pgio;
2572 /* Resend all requests through the MDS */
2573 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
2574 hdr->completion_ops);
2575 set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags);
2576 return nfs_pageio_resend(&pgio, hdr);
2578 EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
2580 static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
2583 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
2584 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2585 PNFS_LAYOUTRET_ON_ERROR) {
2586 pnfs_return_layout(hdr->inode);
2588 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2589 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
2593 * Called by non rpc-based layout drivers
2595 void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
2597 if (likely(!hdr->pnfs_error)) {
2598 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
2599 hdr->mds_offset + hdr->res.count);
2600 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
2602 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
2603 if (unlikely(hdr->pnfs_error))
2604 pnfs_ld_handle_write_error(hdr);
2605 hdr->mds_ops->rpc_release(hdr);
2607 EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
2610 pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
2611 struct nfs_pgio_header *hdr)
2613 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2615 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2616 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
2617 nfs_pageio_reset_write_mds(desc);
2618 mirror->pg_recoalesce = 1;
2620 hdr->completion_ops->completion(hdr);
2623 static enum pnfs_try_status
2624 pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
2625 const struct rpc_call_ops *call_ops,
2626 struct pnfs_layout_segment *lseg,
2629 struct inode *inode = hdr->inode;
2630 enum pnfs_try_status trypnfs;
2631 struct nfs_server *nfss = NFS_SERVER(inode);
2633 hdr->mds_ops = call_ops;
2635 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
2636 inode->i_ino, hdr->args.count, hdr->args.offset, how);
2637 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
2638 if (trypnfs != PNFS_NOT_ATTEMPTED)
2639 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
2640 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2645 pnfs_do_write(struct nfs_pageio_descriptor *desc,
2646 struct nfs_pgio_header *hdr, int how)
2648 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2649 struct pnfs_layout_segment *lseg = desc->pg_lseg;
2650 enum pnfs_try_status trypnfs;
2652 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
2654 case PNFS_NOT_ATTEMPTED:
2655 pnfs_write_through_mds(desc, hdr);
2656 case PNFS_ATTEMPTED:
2658 case PNFS_TRY_AGAIN:
2659 /* cleanup hdr and prepare to redo pnfs */
2660 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2661 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2662 list_splice_init(&hdr->pages, &mirror->pg_list);
2663 mirror->pg_recoalesce = 1;
2665 hdr->mds_ops->rpc_release(hdr);
2669 static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
2671 pnfs_put_lseg(hdr->lseg);
2672 nfs_pgio_header_free(hdr);
2676 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
2678 struct nfs_pgio_header *hdr;
2681 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2683 desc->pg_error = -ENOMEM;
2684 return desc->pg_error;
2686 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
2688 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
2689 ret = nfs_generic_pgio(desc, hdr);
2691 pnfs_do_write(desc, hdr, desc->pg_ioflags);
2695 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
2697 int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
2699 struct nfs_pageio_descriptor pgio;
2701 /* Resend all requests through the MDS */
2702 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
2703 return nfs_pageio_resend(&pgio, hdr);
2705 EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
2707 static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
2709 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
2710 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2711 PNFS_LAYOUTRET_ON_ERROR) {
2712 pnfs_return_layout(hdr->inode);
2714 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2715 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
2719 * Called by non rpc-based layout drivers
2721 void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
2723 if (likely(!hdr->pnfs_error))
2724 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
2725 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
2726 if (unlikely(hdr->pnfs_error))
2727 pnfs_ld_handle_read_error(hdr);
2728 hdr->mds_ops->rpc_release(hdr);
2730 EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
2733 pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
2734 struct nfs_pgio_header *hdr)
2736 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2738 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2739 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
2740 nfs_pageio_reset_read_mds(desc);
2741 mirror->pg_recoalesce = 1;
2743 hdr->completion_ops->completion(hdr);
2747 * Call the appropriate parallel I/O subsystem read function.
2749 static enum pnfs_try_status
2750 pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
2751 const struct rpc_call_ops *call_ops,
2752 struct pnfs_layout_segment *lseg)
2754 struct inode *inode = hdr->inode;
2755 struct nfs_server *nfss = NFS_SERVER(inode);
2756 enum pnfs_try_status trypnfs;
2758 hdr->mds_ops = call_ops;
2760 dprintk("%s: Reading ino:%lu %u@%llu\n",
2761 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
2763 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
2764 if (trypnfs != PNFS_NOT_ATTEMPTED)
2765 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
2766 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2770 /* Resend all requests through pnfs. */
2771 void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
2773 struct nfs_pageio_descriptor pgio;
2775 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2776 /* Prevent deadlocks with layoutreturn! */
2777 pnfs_put_lseg(hdr->lseg);
2780 nfs_pageio_init_read(&pgio, hdr->inode, false,
2781 hdr->completion_ops);
2782 hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
2785 EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
2788 pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
2790 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2791 struct pnfs_layout_segment *lseg = desc->pg_lseg;
2792 enum pnfs_try_status trypnfs;
2794 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
2796 case PNFS_NOT_ATTEMPTED:
2797 pnfs_read_through_mds(desc, hdr);
2798 case PNFS_ATTEMPTED:
2800 case PNFS_TRY_AGAIN:
2801 /* cleanup hdr and prepare to redo pnfs */
2802 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2803 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2804 list_splice_init(&hdr->pages, &mirror->pg_list);
2805 mirror->pg_recoalesce = 1;
2807 hdr->mds_ops->rpc_release(hdr);
2811 static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
2813 pnfs_put_lseg(hdr->lseg);
2814 nfs_pgio_header_free(hdr);
2818 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2820 struct nfs_pgio_header *hdr;
2823 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2825 desc->pg_error = -ENOMEM;
2826 return desc->pg_error;
2828 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
2829 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
2830 ret = nfs_generic_pgio(desc, hdr);
2832 pnfs_do_read(desc, hdr);
2835 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
2837 static void pnfs_clear_layoutcommitting(struct inode *inode)
2839 unsigned long *bitlock = &NFS_I(inode)->flags;
2841 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
2842 smp_mb__after_atomic();
2843 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2847 * There can be multiple RW segments.
2849 static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
2851 struct pnfs_layout_segment *lseg;
2853 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2854 if (lseg->pls_range.iomode == IOMODE_RW &&
2855 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
2856 list_add(&lseg->pls_lc_list, listp);
2860 static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2862 struct pnfs_layout_segment *lseg, *tmp;
2864 /* Matched by references in pnfs_set_layoutcommit */
2865 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2866 list_del_init(&lseg->pls_lc_list);
2867 pnfs_put_lseg(lseg);
2870 pnfs_clear_layoutcommitting(inode);
2873 void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2875 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
2877 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2880 pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
2883 struct nfs_inode *nfsi = NFS_I(inode);
2884 bool mark_as_dirty = false;
2886 spin_lock(&inode->i_lock);
2887 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
2888 nfsi->layout->plh_lwb = end_pos;
2889 mark_as_dirty = true;
2890 dprintk("%s: Set layoutcommit for inode %lu ",
2891 __func__, inode->i_ino);
2892 } else if (end_pos > nfsi->layout->plh_lwb)
2893 nfsi->layout->plh_lwb = end_pos;
2894 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
2895 /* references matched in nfs4_layoutcommit_release */
2896 pnfs_get_lseg(lseg);
2898 spin_unlock(&inode->i_lock);
2899 dprintk("%s: lseg %p end_pos %llu\n",
2900 __func__, lseg, nfsi->layout->plh_lwb);
2902 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2903 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2905 mark_inode_dirty_sync(inode);
2907 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2909 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2911 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2913 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2914 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
2915 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
2919 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2920 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2921 * data to disk to allow the server to recover the data if it crashes.
2922 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2923 * is off, and a COMMIT is sent to a data server, or
2924 * if WRITEs to a data server return NFS_DATA_SYNC.
2927 pnfs_layoutcommit_inode(struct inode *inode, bool sync)
2929 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2930 struct nfs4_layoutcommit_data *data;
2931 struct nfs_inode *nfsi = NFS_I(inode);
2935 if (!pnfs_layoutcommit_outstanding(inode))
2938 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
2941 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
2944 status = wait_on_bit_lock_action(&nfsi->flags,
2945 NFS_INO_LAYOUTCOMMITTING,
2946 nfs_wait_bit_killable,
2953 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2954 data = kzalloc(sizeof(*data), GFP_NOFS);
2956 goto clear_layoutcommitting;
2959 spin_lock(&inode->i_lock);
2960 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2963 INIT_LIST_HEAD(&data->lseg_list);
2964 pnfs_list_write_lseg(inode, &data->lseg_list);
2966 end_pos = nfsi->layout->plh_lwb;
2968 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
2969 spin_unlock(&inode->i_lock);
2971 data->args.inode = inode;
2972 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
2973 nfs_fattr_init(&data->fattr);
2974 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2975 data->res.fattr = &data->fattr;
2977 data->args.lastbytewritten = end_pos - 1;
2979 data->args.lastbytewritten = U64_MAX;
2980 data->res.server = NFS_SERVER(inode);
2982 if (ld->prepare_layoutcommit) {
2983 status = ld->prepare_layoutcommit(&data->args);
2985 put_rpccred(data->cred);
2986 spin_lock(&inode->i_lock);
2987 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2988 if (end_pos > nfsi->layout->plh_lwb)
2989 nfsi->layout->plh_lwb = end_pos;
2995 status = nfs4_proc_layoutcommit(data, sync);
2998 mark_inode_dirty_sync(inode);
2999 dprintk("<-- %s status %d\n", __func__, status);
3002 spin_unlock(&inode->i_lock);
3004 clear_layoutcommitting:
3005 pnfs_clear_layoutcommitting(inode);
3008 EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
3011 pnfs_generic_sync(struct inode *inode, bool datasync)
3013 return pnfs_layoutcommit_inode(inode, true);
3015 EXPORT_SYMBOL_GPL(pnfs_generic_sync);
3017 struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
3019 struct nfs4_threshold *thp;
3021 thp = kzalloc(sizeof(*thp), GFP_NOFS);
3023 dprintk("%s mdsthreshold allocation failed\n", __func__);
3029 #if IS_ENABLED(CONFIG_NFS_V4_2)
3031 pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
3033 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
3034 struct nfs_server *server = NFS_SERVER(inode);
3035 struct nfs_inode *nfsi = NFS_I(inode);
3036 struct nfs42_layoutstat_data *data;
3037 struct pnfs_layout_hdr *hdr;
3040 if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
3043 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
3046 if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
3049 spin_lock(&inode->i_lock);
3050 if (!NFS_I(inode)->layout) {
3051 spin_unlock(&inode->i_lock);
3052 goto out_clear_layoutstats;
3054 hdr = NFS_I(inode)->layout;
3055 pnfs_get_layout_hdr(hdr);
3056 spin_unlock(&inode->i_lock);
3058 data = kzalloc(sizeof(*data), gfp_flags);
3064 data->args.fh = NFS_FH(inode);
3065 data->args.inode = inode;
3066 status = ld->prepare_layoutstats(&data->args);
3070 status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
3073 dprintk("%s returns %d\n", __func__, status);
3079 pnfs_put_layout_hdr(hdr);
3080 out_clear_layoutstats:
3081 smp_mb__before_atomic();
3082 clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
3083 smp_mb__after_atomic();
3086 EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
3089 unsigned int layoutstats_timer;
3090 module_param(layoutstats_timer, uint, 0644);
3091 EXPORT_SYMBOL_GPL(layoutstats_timer);