i40e: lock service task correctly
[linux-2.6/btrfs-unstable.git] / fs / nfs / pnfs.c
blob259ef85f435aa7f9e0b0e4d06d3ce24a9e7a3ad3
1 /*
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
6 * All Rights Reserved
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
27 * such damages.
30 #include <linux/nfs_fs.h>
31 #include <linux/nfs_page.h>
32 #include <linux/module.h>
33 #include <linux/sort.h>
34 #include "internal.h"
35 #include "pnfs.h"
36 #include "iostat.h"
37 #include "nfs4trace.h"
38 #include "delegation.h"
39 #include "nfs42.h"
41 #define NFSDBG_FACILITY NFSDBG_PNFS
42 #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
44 /* Locking:
46 * pnfs_spinlock:
47 * protects pnfs_modules_tbl.
49 static DEFINE_SPINLOCK(pnfs_spinlock);
52 * pnfs_modules_tbl holds all pnfs modules
54 static LIST_HEAD(pnfs_modules_tbl);
56 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo);
58 /* Return the registered pnfs layout driver module matching given id */
59 static struct pnfs_layoutdriver_type *
60 find_pnfs_driver_locked(u32 id)
62 struct pnfs_layoutdriver_type *local;
64 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
65 if (local->id == id)
66 goto out;
67 local = NULL;
68 out:
69 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
70 return local;
73 static struct pnfs_layoutdriver_type *
74 find_pnfs_driver(u32 id)
76 struct pnfs_layoutdriver_type *local;
78 spin_lock(&pnfs_spinlock);
79 local = find_pnfs_driver_locked(id);
80 if (local != NULL && !try_module_get(local->owner)) {
81 dprintk("%s: Could not grab reference on module\n", __func__);
82 local = NULL;
84 spin_unlock(&pnfs_spinlock);
85 return local;
88 void
89 unset_pnfs_layoutdriver(struct nfs_server *nfss)
91 if (nfss->pnfs_curr_ld) {
92 if (nfss->pnfs_curr_ld->clear_layoutdriver)
93 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
94 /* Decrement the MDS count. Purge the deviceid cache if zero */
95 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
96 nfs4_deviceid_purge_client(nfss->nfs_client);
97 module_put(nfss->pnfs_curr_ld->owner);
99 nfss->pnfs_curr_ld = NULL;
103 * When the server sends a list of layout types, we choose one in the order
104 * given in the list below.
106 * FIXME: should this list be configurable in some fashion? module param?
107 * mount option? something else?
109 static const u32 ld_prefs[] = {
110 LAYOUT_SCSI,
111 LAYOUT_BLOCK_VOLUME,
112 LAYOUT_OSD2_OBJECTS,
113 LAYOUT_FLEX_FILES,
114 LAYOUT_NFSV4_1_FILES,
118 static int
119 ld_cmp(const void *e1, const void *e2)
121 u32 ld1 = *((u32 *)e1);
122 u32 ld2 = *((u32 *)e2);
123 int i;
125 for (i = 0; ld_prefs[i] != 0; i++) {
126 if (ld1 == ld_prefs[i])
127 return -1;
129 if (ld2 == ld_prefs[i])
130 return 1;
132 return 0;
136 * Try to set the server's pnfs module to the pnfs layout type specified by id.
137 * Currently only one pNFS layout driver per filesystem is supported.
139 * @ids array of layout types supported by MDS.
141 void
142 set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
143 struct nfs_fsinfo *fsinfo)
145 struct pnfs_layoutdriver_type *ld_type = NULL;
146 u32 id;
147 int i;
149 if (fsinfo->nlayouttypes == 0)
150 goto out_no_driver;
151 if (!(server->nfs_client->cl_exchange_flags &
152 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
153 printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n",
154 __func__, server->nfs_client->cl_exchange_flags);
155 goto out_no_driver;
158 sort(fsinfo->layouttype, fsinfo->nlayouttypes,
159 sizeof(*fsinfo->layouttype), ld_cmp, NULL);
161 for (i = 0; i < fsinfo->nlayouttypes; i++) {
162 id = fsinfo->layouttype[i];
163 ld_type = find_pnfs_driver(id);
164 if (!ld_type) {
165 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX,
166 id);
167 ld_type = find_pnfs_driver(id);
169 if (ld_type)
170 break;
173 if (!ld_type) {
174 dprintk("%s: No pNFS module found!\n", __func__);
175 goto out_no_driver;
178 server->pnfs_curr_ld = ld_type;
179 if (ld_type->set_layoutdriver
180 && ld_type->set_layoutdriver(server, mntfh)) {
181 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
182 "driver %u.\n", __func__, id);
183 module_put(ld_type->owner);
184 goto out_no_driver;
186 /* Bump the MDS count */
187 atomic_inc(&server->nfs_client->cl_mds_count);
189 dprintk("%s: pNFS module for %u set\n", __func__, id);
190 return;
192 out_no_driver:
193 dprintk("%s: Using NFSv4 I/O\n", __func__);
194 server->pnfs_curr_ld = NULL;
198 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
200 int status = -EINVAL;
201 struct pnfs_layoutdriver_type *tmp;
203 if (ld_type->id == 0) {
204 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
205 return status;
207 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
208 printk(KERN_ERR "NFS: %s Layout driver must provide "
209 "alloc_lseg and free_lseg.\n", __func__);
210 return status;
213 spin_lock(&pnfs_spinlock);
214 tmp = find_pnfs_driver_locked(ld_type->id);
215 if (!tmp) {
216 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
217 status = 0;
218 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
219 ld_type->name);
220 } else {
221 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
222 __func__, ld_type->id);
224 spin_unlock(&pnfs_spinlock);
226 return status;
228 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
230 void
231 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
233 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
234 spin_lock(&pnfs_spinlock);
235 list_del(&ld_type->pnfs_tblid);
236 spin_unlock(&pnfs_spinlock);
238 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
241 * pNFS client layout cache
244 /* Need to hold i_lock if caller does not already hold reference */
245 void
246 pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
248 atomic_inc(&lo->plh_refcount);
251 static struct pnfs_layout_hdr *
252 pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
254 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
255 return ld->alloc_layout_hdr(ino, gfp_flags);
258 static void
259 pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
261 struct nfs_server *server = NFS_SERVER(lo->plh_inode);
262 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
264 if (!list_empty(&lo->plh_layouts)) {
265 struct nfs_client *clp = server->nfs_client;
267 spin_lock(&clp->cl_lock);
268 list_del_init(&lo->plh_layouts);
269 spin_unlock(&clp->cl_lock);
271 put_rpccred(lo->plh_lc_cred);
272 return ld->free_layout_hdr(lo);
275 static void
276 pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
278 struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
279 dprintk("%s: freeing layout cache %p\n", __func__, lo);
280 nfsi->layout = NULL;
281 /* Reset MDS Threshold I/O counters */
282 nfsi->write_io = 0;
283 nfsi->read_io = 0;
286 void
287 pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
289 struct inode *inode = lo->plh_inode;
291 pnfs_layoutreturn_before_put_layout_hdr(lo);
293 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
294 if (!list_empty(&lo->plh_segs))
295 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
296 pnfs_detach_layout_hdr(lo);
297 spin_unlock(&inode->i_lock);
298 pnfs_free_layout_hdr(lo);
303 * Mark a pnfs_layout_hdr and all associated layout segments as invalid
305 * In order to continue using the pnfs_layout_hdr, a full recovery
306 * is required.
307 * Note that caller must hold inode->i_lock.
310 pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
311 struct list_head *lseg_list)
313 struct pnfs_layout_range range = {
314 .iomode = IOMODE_ANY,
315 .offset = 0,
316 .length = NFS4_MAX_UINT64,
319 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
320 return pnfs_mark_matching_lsegs_invalid(lo, lseg_list, &range, 0);
323 static int
324 pnfs_iomode_to_fail_bit(u32 iomode)
326 return iomode == IOMODE_RW ?
327 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
330 static void
331 pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
333 lo->plh_retry_timestamp = jiffies;
334 if (!test_and_set_bit(fail_bit, &lo->plh_flags))
335 atomic_inc(&lo->plh_refcount);
338 static void
339 pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
341 if (test_and_clear_bit(fail_bit, &lo->plh_flags))
342 atomic_dec(&lo->plh_refcount);
345 static void
346 pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
348 struct inode *inode = lo->plh_inode;
349 struct pnfs_layout_range range = {
350 .iomode = iomode,
351 .offset = 0,
352 .length = NFS4_MAX_UINT64,
354 LIST_HEAD(head);
356 spin_lock(&inode->i_lock);
357 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
358 pnfs_mark_matching_lsegs_invalid(lo, &head, &range, 0);
359 spin_unlock(&inode->i_lock);
360 pnfs_free_lseg_list(&head);
361 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
362 iomode == IOMODE_RW ? "RW" : "READ");
365 static bool
366 pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
368 unsigned long start, end;
369 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
371 if (test_bit(fail_bit, &lo->plh_flags) == 0)
372 return false;
373 end = jiffies;
374 start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
375 if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
376 /* It is time to retry the failed layoutgets */
377 pnfs_layout_clear_fail_bit(lo, fail_bit);
378 return false;
380 return true;
383 static void
384 pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
385 const struct pnfs_layout_range *range,
386 const nfs4_stateid *stateid)
388 INIT_LIST_HEAD(&lseg->pls_list);
389 INIT_LIST_HEAD(&lseg->pls_lc_list);
390 atomic_set(&lseg->pls_refcount, 1);
391 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
392 lseg->pls_layout = lo;
393 lseg->pls_range = *range;
394 lseg->pls_seq = be32_to_cpu(stateid->seqid);
397 static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
399 struct inode *ino = lseg->pls_layout->plh_inode;
401 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
404 static void
405 pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
406 struct pnfs_layout_segment *lseg)
408 struct inode *inode = lo->plh_inode;
410 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
411 list_del_init(&lseg->pls_list);
412 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
413 atomic_dec(&lo->plh_refcount);
414 if (list_empty(&lo->plh_segs)) {
415 if (atomic_read(&lo->plh_outstanding) == 0)
416 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
417 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
419 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
422 void
423 pnfs_put_lseg(struct pnfs_layout_segment *lseg)
425 struct pnfs_layout_hdr *lo;
426 struct inode *inode;
428 if (!lseg)
429 return;
431 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
432 atomic_read(&lseg->pls_refcount),
433 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
435 lo = lseg->pls_layout;
436 inode = lo->plh_inode;
438 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
439 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
440 spin_unlock(&inode->i_lock);
441 return;
443 pnfs_get_layout_hdr(lo);
444 pnfs_layout_remove_lseg(lo, lseg);
445 spin_unlock(&inode->i_lock);
446 pnfs_free_lseg(lseg);
447 pnfs_put_layout_hdr(lo);
450 EXPORT_SYMBOL_GPL(pnfs_put_lseg);
452 static void pnfs_free_lseg_async_work(struct work_struct *work)
454 struct pnfs_layout_segment *lseg;
455 struct pnfs_layout_hdr *lo;
457 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
458 lo = lseg->pls_layout;
460 pnfs_free_lseg(lseg);
461 pnfs_put_layout_hdr(lo);
464 static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg)
466 INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work);
467 schedule_work(&lseg->pls_work);
470 void
471 pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg)
473 if (!lseg)
474 return;
476 assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock);
478 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
479 atomic_read(&lseg->pls_refcount),
480 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
481 if (atomic_dec_and_test(&lseg->pls_refcount)) {
482 struct pnfs_layout_hdr *lo = lseg->pls_layout;
483 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
484 return;
485 pnfs_get_layout_hdr(lo);
486 pnfs_layout_remove_lseg(lo, lseg);
487 pnfs_free_lseg_async(lseg);
490 EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked);
492 static u64
493 end_offset(u64 start, u64 len)
495 u64 end;
497 end = start + len;
498 return end >= start ? end : NFS4_MAX_UINT64;
502 * is l2 fully contained in l1?
503 * start1 end1
504 * [----------------------------------)
505 * start2 end2
506 * [----------------)
508 static bool
509 pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
510 const struct pnfs_layout_range *l2)
512 u64 start1 = l1->offset;
513 u64 end1 = end_offset(start1, l1->length);
514 u64 start2 = l2->offset;
515 u64 end2 = end_offset(start2, l2->length);
517 return (start1 <= start2) && (end1 >= end2);
521 * is l1 and l2 intersecting?
522 * start1 end1
523 * [----------------------------------)
524 * start2 end2
525 * [----------------)
527 static bool
528 pnfs_lseg_range_intersecting(const struct pnfs_layout_range *l1,
529 const struct pnfs_layout_range *l2)
531 u64 start1 = l1->offset;
532 u64 end1 = end_offset(start1, l1->length);
533 u64 start2 = l2->offset;
534 u64 end2 = end_offset(start2, l2->length);
536 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
537 (end2 == NFS4_MAX_UINT64 || end2 > start1);
540 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
541 struct list_head *tmp_list)
543 if (!atomic_dec_and_test(&lseg->pls_refcount))
544 return false;
545 pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
546 list_add(&lseg->pls_list, tmp_list);
547 return true;
550 /* Returns 1 if lseg is removed from list, 0 otherwise */
551 static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
552 struct list_head *tmp_list)
554 int rv = 0;
556 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
557 /* Remove the reference keeping the lseg in the
558 * list. It will now be removed when all
559 * outstanding io is finished.
561 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
562 atomic_read(&lseg->pls_refcount));
563 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
564 rv = 1;
566 return rv;
570 * Compare 2 layout stateid sequence ids, to see which is newer,
571 * taking into account wraparound issues.
573 static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
575 return (s32)(s1 - s2) > 0;
578 static bool
579 pnfs_should_free_range(const struct pnfs_layout_range *lseg_range,
580 const struct pnfs_layout_range *recall_range)
582 return (recall_range->iomode == IOMODE_ANY ||
583 lseg_range->iomode == recall_range->iomode) &&
584 pnfs_lseg_range_intersecting(lseg_range, recall_range);
587 static bool
588 pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg,
589 const struct pnfs_layout_range *recall_range,
590 u32 seq)
592 if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq))
593 return false;
594 if (recall_range == NULL)
595 return true;
596 return pnfs_should_free_range(&lseg->pls_range, recall_range);
600 * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later
601 * @lo: layout header containing the lsegs
602 * @tmp_list: list head where doomed lsegs should go
603 * @recall_range: optional recall range argument to match (may be NULL)
604 * @seq: only invalidate lsegs obtained prior to this sequence (may be 0)
606 * Walk the list of lsegs in the layout header, and tear down any that should
607 * be destroyed. If "recall_range" is specified then the segment must match
608 * that range. If "seq" is non-zero, then only match segments that were handed
609 * out at or before that sequence.
611 * Returns number of matching invalid lsegs remaining in list after scanning
612 * it and purging them.
615 pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
616 struct list_head *tmp_list,
617 const struct pnfs_layout_range *recall_range,
618 u32 seq)
620 struct pnfs_layout_segment *lseg, *next;
621 int remaining = 0;
623 dprintk("%s:Begin lo %p\n", __func__, lo);
625 if (list_empty(&lo->plh_segs))
626 return 0;
627 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
628 if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
629 dprintk("%s: freeing lseg %p iomode %d seq %u"
630 "offset %llu length %llu\n", __func__,
631 lseg, lseg->pls_range.iomode, lseg->pls_seq,
632 lseg->pls_range.offset, lseg->pls_range.length);
633 if (!mark_lseg_invalid(lseg, tmp_list))
634 remaining++;
636 dprintk("%s:Return %i\n", __func__, remaining);
637 return remaining;
640 /* note free_me must contain lsegs from a single layout_hdr */
641 void
642 pnfs_free_lseg_list(struct list_head *free_me)
644 struct pnfs_layout_segment *lseg, *tmp;
646 if (list_empty(free_me))
647 return;
649 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
650 list_del(&lseg->pls_list);
651 pnfs_free_lseg(lseg);
655 void
656 pnfs_destroy_layout(struct nfs_inode *nfsi)
658 struct pnfs_layout_hdr *lo;
659 LIST_HEAD(tmp_list);
661 spin_lock(&nfsi->vfs_inode.i_lock);
662 lo = nfsi->layout;
663 if (lo) {
664 pnfs_get_layout_hdr(lo);
665 pnfs_mark_layout_stateid_invalid(lo, &tmp_list);
666 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
667 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
668 spin_unlock(&nfsi->vfs_inode.i_lock);
669 pnfs_free_lseg_list(&tmp_list);
670 pnfs_put_layout_hdr(lo);
671 } else
672 spin_unlock(&nfsi->vfs_inode.i_lock);
674 EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
676 static bool
677 pnfs_layout_add_bulk_destroy_list(struct inode *inode,
678 struct list_head *layout_list)
680 struct pnfs_layout_hdr *lo;
681 bool ret = false;
683 spin_lock(&inode->i_lock);
684 lo = NFS_I(inode)->layout;
685 if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
686 pnfs_get_layout_hdr(lo);
687 list_add(&lo->plh_bulk_destroy, layout_list);
688 ret = true;
690 spin_unlock(&inode->i_lock);
691 return ret;
694 /* Caller must hold rcu_read_lock and clp->cl_lock */
695 static int
696 pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
697 struct nfs_server *server,
698 struct list_head *layout_list)
700 struct pnfs_layout_hdr *lo, *next;
701 struct inode *inode;
703 list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
704 inode = igrab(lo->plh_inode);
705 if (inode == NULL)
706 continue;
707 list_del_init(&lo->plh_layouts);
708 if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
709 continue;
710 rcu_read_unlock();
711 spin_unlock(&clp->cl_lock);
712 iput(inode);
713 spin_lock(&clp->cl_lock);
714 rcu_read_lock();
715 return -EAGAIN;
717 return 0;
720 static int
721 pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
722 bool is_bulk_recall)
724 struct pnfs_layout_hdr *lo;
725 struct inode *inode;
726 LIST_HEAD(lseg_list);
727 int ret = 0;
729 while (!list_empty(layout_list)) {
730 lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
731 plh_bulk_destroy);
732 dprintk("%s freeing layout for inode %lu\n", __func__,
733 lo->plh_inode->i_ino);
734 inode = lo->plh_inode;
736 pnfs_layoutcommit_inode(inode, false);
738 spin_lock(&inode->i_lock);
739 list_del_init(&lo->plh_bulk_destroy);
740 if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
741 if (is_bulk_recall)
742 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
743 ret = -EAGAIN;
745 spin_unlock(&inode->i_lock);
746 pnfs_free_lseg_list(&lseg_list);
747 /* Free all lsegs that are attached to commit buckets */
748 nfs_commit_inode(inode, 0);
749 pnfs_put_layout_hdr(lo);
750 iput(inode);
752 return ret;
756 pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
757 struct nfs_fsid *fsid,
758 bool is_recall)
760 struct nfs_server *server;
761 LIST_HEAD(layout_list);
763 spin_lock(&clp->cl_lock);
764 rcu_read_lock();
765 restart:
766 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
767 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
768 continue;
769 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
770 server,
771 &layout_list) != 0)
772 goto restart;
774 rcu_read_unlock();
775 spin_unlock(&clp->cl_lock);
777 if (list_empty(&layout_list))
778 return 0;
779 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
783 pnfs_destroy_layouts_byclid(struct nfs_client *clp,
784 bool is_recall)
786 struct nfs_server *server;
787 LIST_HEAD(layout_list);
789 spin_lock(&clp->cl_lock);
790 rcu_read_lock();
791 restart:
792 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
793 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
794 server,
795 &layout_list) != 0)
796 goto restart;
798 rcu_read_unlock();
799 spin_unlock(&clp->cl_lock);
801 if (list_empty(&layout_list))
802 return 0;
803 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
807 * Called by the state manger to remove all layouts established under an
808 * expired lease.
810 void
811 pnfs_destroy_all_layouts(struct nfs_client *clp)
813 nfs4_deviceid_mark_client_invalid(clp);
814 nfs4_deviceid_purge_client(clp);
816 pnfs_destroy_layouts_byclid(clp, false);
819 static void
820 pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo)
822 lo->plh_return_iomode = 0;
823 lo->plh_return_seq = 0;
824 clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
827 /* update lo->plh_stateid with new if is more recent */
828 void
829 pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
830 bool update_barrier)
832 u32 oldseq, newseq, new_barrier = 0;
834 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
835 newseq = be32_to_cpu(new->seqid);
837 if (!pnfs_layout_is_valid(lo)) {
838 nfs4_stateid_copy(&lo->plh_stateid, new);
839 lo->plh_barrier = newseq;
840 pnfs_clear_layoutreturn_info(lo);
841 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
842 return;
844 if (pnfs_seqid_is_newer(newseq, oldseq)) {
845 nfs4_stateid_copy(&lo->plh_stateid, new);
847 * Because of wraparound, we want to keep the barrier
848 * "close" to the current seqids.
850 new_barrier = newseq - atomic_read(&lo->plh_outstanding);
852 if (update_barrier)
853 new_barrier = be32_to_cpu(new->seqid);
854 else if (new_barrier == 0)
855 return;
856 if (pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
857 lo->plh_barrier = new_barrier;
860 static bool
861 pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
862 const nfs4_stateid *stateid)
864 u32 seqid = be32_to_cpu(stateid->seqid);
866 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
869 /* lget is set to 1 if called from inside send_layoutget call chain */
870 static bool
871 pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
873 return lo->plh_block_lgets ||
874 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
878 * Get layout from server.
879 * for now, assume that whole file layouts are requested.
880 * arg->offset: 0
881 * arg->length: all ones
883 static struct pnfs_layout_segment *
884 send_layoutget(struct pnfs_layout_hdr *lo,
885 struct nfs_open_context *ctx,
886 nfs4_stateid *stateid,
887 const struct pnfs_layout_range *range,
888 long *timeout, gfp_t gfp_flags)
890 struct inode *ino = lo->plh_inode;
891 struct nfs_server *server = NFS_SERVER(ino);
892 struct nfs4_layoutget *lgp;
893 loff_t i_size;
895 dprintk("--> %s\n", __func__);
898 * Synchronously retrieve layout information from server and
899 * store in lseg. If we race with a concurrent seqid morphing
900 * op, then re-send the LAYOUTGET.
902 lgp = kzalloc(sizeof(*lgp), gfp_flags);
903 if (lgp == NULL)
904 return ERR_PTR(-ENOMEM);
906 i_size = i_size_read(ino);
908 lgp->args.minlength = PAGE_SIZE;
909 if (lgp->args.minlength > range->length)
910 lgp->args.minlength = range->length;
911 if (range->iomode == IOMODE_READ) {
912 if (range->offset >= i_size)
913 lgp->args.minlength = 0;
914 else if (i_size - range->offset < lgp->args.minlength)
915 lgp->args.minlength = i_size - range->offset;
917 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
918 pnfs_copy_range(&lgp->args.range, range);
919 lgp->args.type = server->pnfs_curr_ld->id;
920 lgp->args.inode = ino;
921 lgp->args.ctx = get_nfs_open_context(ctx);
922 nfs4_stateid_copy(&lgp->args.stateid, stateid);
923 lgp->gfp_flags = gfp_flags;
924 lgp->cred = lo->plh_lc_cred;
926 return nfs4_proc_layoutget(lgp, timeout, gfp_flags);
929 static void pnfs_clear_layoutcommit(struct inode *inode,
930 struct list_head *head)
932 struct nfs_inode *nfsi = NFS_I(inode);
933 struct pnfs_layout_segment *lseg, *tmp;
935 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
936 return;
937 list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
938 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
939 continue;
940 pnfs_lseg_dec_and_remove_zero(lseg, head);
944 void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
946 clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
947 smp_mb__after_atomic();
948 wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
949 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
952 static bool
953 pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
954 nfs4_stateid *stateid,
955 enum pnfs_iomode *iomode)
957 /* Serialise LAYOUTGET/LAYOUTRETURN */
958 if (atomic_read(&lo->plh_outstanding) != 0)
959 return false;
960 if (test_and_set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
961 return false;
962 pnfs_get_layout_hdr(lo);
963 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
964 if (stateid != NULL) {
965 nfs4_stateid_copy(stateid, &lo->plh_stateid);
966 if (lo->plh_return_seq != 0)
967 stateid->seqid = cpu_to_be32(lo->plh_return_seq);
969 if (iomode != NULL)
970 *iomode = lo->plh_return_iomode;
971 pnfs_clear_layoutreturn_info(lo);
972 return true;
974 if (stateid != NULL)
975 nfs4_stateid_copy(stateid, &lo->plh_stateid);
976 if (iomode != NULL)
977 *iomode = IOMODE_ANY;
978 return true;
981 static int
982 pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, const nfs4_stateid *stateid,
983 enum pnfs_iomode iomode, bool sync)
985 struct inode *ino = lo->plh_inode;
986 struct nfs4_layoutreturn *lrp;
987 int status = 0;
989 lrp = kzalloc(sizeof(*lrp), GFP_NOFS);
990 if (unlikely(lrp == NULL)) {
991 status = -ENOMEM;
992 spin_lock(&ino->i_lock);
993 pnfs_clear_layoutreturn_waitbit(lo);
994 spin_unlock(&ino->i_lock);
995 pnfs_put_layout_hdr(lo);
996 goto out;
999 nfs4_stateid_copy(&lrp->args.stateid, stateid);
1000 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
1001 lrp->args.inode = ino;
1002 lrp->args.range.iomode = iomode;
1003 lrp->args.range.offset = 0;
1004 lrp->args.range.length = NFS4_MAX_UINT64;
1005 lrp->args.layout = lo;
1006 lrp->clp = NFS_SERVER(ino)->nfs_client;
1007 lrp->cred = lo->plh_lc_cred;
1009 status = nfs4_proc_layoutreturn(lrp, sync);
1010 out:
1011 dprintk("<-- %s status: %d\n", __func__, status);
1012 return status;
1015 /* Return true if layoutreturn is needed */
1016 static bool
1017 pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
1019 struct pnfs_layout_segment *s;
1021 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1022 return false;
1024 /* Defer layoutreturn until all lsegs are done */
1025 list_for_each_entry(s, &lo->plh_segs, pls_list) {
1026 if (test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
1027 return false;
1030 return true;
1033 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
1035 struct inode *inode= lo->plh_inode;
1037 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1038 return;
1039 spin_lock(&inode->i_lock);
1040 if (pnfs_layout_need_return(lo)) {
1041 nfs4_stateid stateid;
1042 enum pnfs_iomode iomode;
1043 bool send;
1045 send = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
1046 spin_unlock(&inode->i_lock);
1047 if (send) {
1048 /* Send an async layoutreturn so we dont deadlock */
1049 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1051 } else
1052 spin_unlock(&inode->i_lock);
1056 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
1057 * when the layout segment list is empty.
1059 * Note that a pnfs_layout_hdr can exist with an empty layout segment
1060 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
1061 * deviceid is marked invalid.
1064 _pnfs_return_layout(struct inode *ino)
1066 struct pnfs_layout_hdr *lo = NULL;
1067 struct nfs_inode *nfsi = NFS_I(ino);
1068 LIST_HEAD(tmp_list);
1069 nfs4_stateid stateid;
1070 int status = 0, empty;
1071 bool send;
1073 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
1075 spin_lock(&ino->i_lock);
1076 lo = nfsi->layout;
1077 if (!lo) {
1078 spin_unlock(&ino->i_lock);
1079 dprintk("NFS: %s no layout to return\n", __func__);
1080 goto out;
1082 /* Reference matched in nfs4_layoutreturn_release */
1083 pnfs_get_layout_hdr(lo);
1084 empty = list_empty(&lo->plh_segs);
1085 pnfs_clear_layoutcommit(ino, &tmp_list);
1086 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL, 0);
1088 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
1089 struct pnfs_layout_range range = {
1090 .iomode = IOMODE_ANY,
1091 .offset = 0,
1092 .length = NFS4_MAX_UINT64,
1094 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1097 /* Don't send a LAYOUTRETURN if list was initially empty */
1098 if (empty) {
1099 spin_unlock(&ino->i_lock);
1100 dprintk("NFS: %s no layout segments to return\n", __func__);
1101 goto out_put_layout_hdr;
1104 send = pnfs_prepare_layoutreturn(lo, &stateid, NULL);
1105 spin_unlock(&ino->i_lock);
1106 pnfs_free_lseg_list(&tmp_list);
1107 if (send)
1108 status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
1109 out_put_layout_hdr:
1110 pnfs_put_layout_hdr(lo);
1111 out:
1112 dprintk("<-- %s status: %d\n", __func__, status);
1113 return status;
1115 EXPORT_SYMBOL_GPL(_pnfs_return_layout);
1118 pnfs_commit_and_return_layout(struct inode *inode)
1120 struct pnfs_layout_hdr *lo;
1121 int ret;
1123 spin_lock(&inode->i_lock);
1124 lo = NFS_I(inode)->layout;
1125 if (lo == NULL) {
1126 spin_unlock(&inode->i_lock);
1127 return 0;
1129 pnfs_get_layout_hdr(lo);
1130 /* Block new layoutgets and read/write to ds */
1131 lo->plh_block_lgets++;
1132 spin_unlock(&inode->i_lock);
1133 filemap_fdatawait(inode->i_mapping);
1134 ret = pnfs_layoutcommit_inode(inode, true);
1135 if (ret == 0)
1136 ret = _pnfs_return_layout(inode);
1137 spin_lock(&inode->i_lock);
1138 lo->plh_block_lgets--;
1139 spin_unlock(&inode->i_lock);
1140 pnfs_put_layout_hdr(lo);
1141 return ret;
1144 bool pnfs_roc(struct inode *ino)
1146 struct nfs_inode *nfsi = NFS_I(ino);
1147 struct nfs_open_context *ctx;
1148 struct nfs4_state *state;
1149 struct pnfs_layout_hdr *lo;
1150 struct pnfs_layout_segment *lseg, *tmp;
1151 nfs4_stateid stateid;
1152 LIST_HEAD(tmp_list);
1153 bool found = false, layoutreturn = false, roc = false;
1155 spin_lock(&ino->i_lock);
1156 lo = nfsi->layout;
1157 if (!lo || test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
1158 goto out_noroc;
1160 /* no roc if we hold a delegation */
1161 if (nfs4_check_delegation(ino, FMODE_READ))
1162 goto out_noroc;
1164 list_for_each_entry(ctx, &nfsi->open_files, list) {
1165 state = ctx->state;
1166 /* Don't return layout if there is open file state */
1167 if (state != NULL && state->state != 0)
1168 goto out_noroc;
1171 /* always send layoutreturn if being marked so */
1172 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1173 layoutreturn = pnfs_prepare_layoutreturn(lo,
1174 &stateid, NULL);
1176 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
1177 /* If we are sending layoutreturn, invalidate all valid lsegs */
1178 if (layoutreturn || test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
1179 mark_lseg_invalid(lseg, &tmp_list);
1180 found = true;
1182 /* ROC in two conditions:
1183 * 1. there are ROC lsegs
1184 * 2. we don't send layoutreturn
1186 if (found && !layoutreturn) {
1187 /* lo ref dropped in pnfs_roc_release() */
1188 pnfs_get_layout_hdr(lo);
1189 roc = true;
1192 out_noroc:
1193 spin_unlock(&ino->i_lock);
1194 pnfs_free_lseg_list(&tmp_list);
1195 pnfs_layoutcommit_inode(ino, true);
1196 if (layoutreturn)
1197 pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
1198 return roc;
1201 void pnfs_roc_release(struct inode *ino)
1203 struct pnfs_layout_hdr *lo;
1205 spin_lock(&ino->i_lock);
1206 lo = NFS_I(ino)->layout;
1207 pnfs_clear_layoutreturn_waitbit(lo);
1208 if (atomic_dec_and_test(&lo->plh_refcount)) {
1209 pnfs_detach_layout_hdr(lo);
1210 spin_unlock(&ino->i_lock);
1211 pnfs_free_layout_hdr(lo);
1212 } else
1213 spin_unlock(&ino->i_lock);
1216 void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
1218 struct pnfs_layout_hdr *lo;
1220 spin_lock(&ino->i_lock);
1221 lo = NFS_I(ino)->layout;
1222 if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
1223 lo->plh_barrier = barrier;
1224 spin_unlock(&ino->i_lock);
1225 trace_nfs4_layoutreturn_on_close(ino, 0);
1228 void pnfs_roc_get_barrier(struct inode *ino, u32 *barrier)
1230 struct nfs_inode *nfsi = NFS_I(ino);
1231 struct pnfs_layout_hdr *lo;
1232 u32 current_seqid;
1234 spin_lock(&ino->i_lock);
1235 lo = nfsi->layout;
1236 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
1238 /* Since close does not return a layout stateid for use as
1239 * a barrier, we choose the worst-case barrier.
1241 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
1242 spin_unlock(&ino->i_lock);
1245 bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
1247 struct nfs_inode *nfsi = NFS_I(ino);
1248 struct pnfs_layout_hdr *lo;
1249 bool sleep = false;
1251 /* we might not have grabbed lo reference. so need to check under
1252 * i_lock */
1253 spin_lock(&ino->i_lock);
1254 lo = nfsi->layout;
1255 if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
1256 sleep = true;
1257 spin_unlock(&ino->i_lock);
1259 if (sleep)
1260 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
1262 return sleep;
1266 * Compare two layout segments for sorting into layout cache.
1267 * We want to preferentially return RW over RO layouts, so ensure those
1268 * are seen first.
1270 static s64
1271 pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
1272 const struct pnfs_layout_range *l2)
1274 s64 d;
1276 /* high offset > low offset */
1277 d = l1->offset - l2->offset;
1278 if (d)
1279 return d;
1281 /* short length > long length */
1282 d = l2->length - l1->length;
1283 if (d)
1284 return d;
1286 /* read > read/write */
1287 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
1290 static bool
1291 pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
1292 const struct pnfs_layout_range *l2)
1294 return pnfs_lseg_range_cmp(l1, l2) > 0;
1297 static bool
1298 pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
1299 struct pnfs_layout_segment *old)
1301 return false;
1304 void
1305 pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1306 struct pnfs_layout_segment *lseg,
1307 bool (*is_after)(const struct pnfs_layout_range *,
1308 const struct pnfs_layout_range *),
1309 bool (*do_merge)(struct pnfs_layout_segment *,
1310 struct pnfs_layout_segment *),
1311 struct list_head *free_me)
1313 struct pnfs_layout_segment *lp, *tmp;
1315 dprintk("%s:Begin\n", __func__);
1317 list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
1318 if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
1319 continue;
1320 if (do_merge(lseg, lp)) {
1321 mark_lseg_invalid(lp, free_me);
1322 continue;
1324 if (is_after(&lseg->pls_range, &lp->pls_range))
1325 continue;
1326 list_add_tail(&lseg->pls_list, &lp->pls_list);
1327 dprintk("%s: inserted lseg %p "
1328 "iomode %d offset %llu length %llu before "
1329 "lp %p iomode %d offset %llu length %llu\n",
1330 __func__, lseg, lseg->pls_range.iomode,
1331 lseg->pls_range.offset, lseg->pls_range.length,
1332 lp, lp->pls_range.iomode, lp->pls_range.offset,
1333 lp->pls_range.length);
1334 goto out;
1336 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1337 dprintk("%s: inserted lseg %p "
1338 "iomode %d offset %llu length %llu at tail\n",
1339 __func__, lseg, lseg->pls_range.iomode,
1340 lseg->pls_range.offset, lseg->pls_range.length);
1341 out:
1342 pnfs_get_layout_hdr(lo);
1344 dprintk("%s:Return\n", __func__);
1346 EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
1348 static void
1349 pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1350 struct pnfs_layout_segment *lseg,
1351 struct list_head *free_me)
1353 struct inode *inode = lo->plh_inode;
1354 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
1356 if (ld->add_lseg != NULL)
1357 ld->add_lseg(lo, lseg, free_me);
1358 else
1359 pnfs_generic_layout_insert_lseg(lo, lseg,
1360 pnfs_lseg_range_is_after,
1361 pnfs_lseg_no_merge,
1362 free_me);
1365 static struct pnfs_layout_hdr *
1366 alloc_init_layout_hdr(struct inode *ino,
1367 struct nfs_open_context *ctx,
1368 gfp_t gfp_flags)
1370 struct pnfs_layout_hdr *lo;
1372 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
1373 if (!lo)
1374 return NULL;
1375 atomic_set(&lo->plh_refcount, 1);
1376 INIT_LIST_HEAD(&lo->plh_layouts);
1377 INIT_LIST_HEAD(&lo->plh_segs);
1378 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
1379 lo->plh_inode = ino;
1380 lo->plh_lc_cred = get_rpccred(ctx->cred);
1381 lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
1382 return lo;
1385 static struct pnfs_layout_hdr *
1386 pnfs_find_alloc_layout(struct inode *ino,
1387 struct nfs_open_context *ctx,
1388 gfp_t gfp_flags)
1389 __releases(&ino->i_lock)
1390 __acquires(&ino->i_lock)
1392 struct nfs_inode *nfsi = NFS_I(ino);
1393 struct pnfs_layout_hdr *new = NULL;
1395 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1397 if (nfsi->layout != NULL)
1398 goto out_existing;
1399 spin_unlock(&ino->i_lock);
1400 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
1401 spin_lock(&ino->i_lock);
1403 if (likely(nfsi->layout == NULL)) { /* Won the race? */
1404 nfsi->layout = new;
1405 return new;
1406 } else if (new != NULL)
1407 pnfs_free_layout_hdr(new);
1408 out_existing:
1409 pnfs_get_layout_hdr(nfsi->layout);
1410 return nfsi->layout;
1414 * iomode matching rules:
1415 * iomode lseg strict match
1416 * iomode
1417 * ----- ----- ------ -----
1418 * ANY READ N/A true
1419 * ANY RW N/A true
1420 * RW READ N/A false
1421 * RW RW N/A true
1422 * READ READ N/A true
1423 * READ RW true false
1424 * READ RW false true
1426 static bool
1427 pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
1428 const struct pnfs_layout_range *range,
1429 bool strict_iomode)
1431 struct pnfs_layout_range range1;
1433 if ((range->iomode == IOMODE_RW &&
1434 ls_range->iomode != IOMODE_RW) ||
1435 (range->iomode != ls_range->iomode &&
1436 strict_iomode == true) ||
1437 !pnfs_lseg_range_intersecting(ls_range, range))
1438 return 0;
1440 /* range1 covers only the first byte in the range */
1441 range1 = *range;
1442 range1.length = 1;
1443 return pnfs_lseg_range_contained(ls_range, &range1);
1447 * lookup range in layout
1449 static struct pnfs_layout_segment *
1450 pnfs_find_lseg(struct pnfs_layout_hdr *lo,
1451 struct pnfs_layout_range *range,
1452 bool strict_iomode)
1454 struct pnfs_layout_segment *lseg, *ret = NULL;
1456 dprintk("%s:Begin\n", __func__);
1458 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
1459 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
1460 !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
1461 pnfs_lseg_range_match(&lseg->pls_range, range,
1462 strict_iomode)) {
1463 ret = pnfs_get_lseg(lseg);
1464 break;
1468 dprintk("%s:Return lseg %p ref %d\n",
1469 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
1470 return ret;
1474 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1475 * to the MDS or over pNFS
1477 * The nfs_inode read_io and write_io fields are cumulative counters reset
1478 * when there are no layout segments. Note that in pnfs_update_layout iomode
1479 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1480 * WRITE request.
1482 * A return of true means use MDS I/O.
1484 * From rfc 5661:
1485 * If a file's size is smaller than the file size threshold, data accesses
1486 * SHOULD be sent to the metadata server. If an I/O request has a length that
1487 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1488 * server. If both file size and I/O size are provided, the client SHOULD
1489 * reach or exceed both thresholds before sending its read or write
1490 * requests to the data server.
1492 static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1493 struct inode *ino, int iomode)
1495 struct nfs4_threshold *t = ctx->mdsthreshold;
1496 struct nfs_inode *nfsi = NFS_I(ino);
1497 loff_t fsize = i_size_read(ino);
1498 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1500 if (t == NULL)
1501 return ret;
1503 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1504 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1506 switch (iomode) {
1507 case IOMODE_READ:
1508 if (t->bm & THRESHOLD_RD) {
1509 dprintk("%s fsize %llu\n", __func__, fsize);
1510 size_set = true;
1511 if (fsize < t->rd_sz)
1512 size = true;
1514 if (t->bm & THRESHOLD_RD_IO) {
1515 dprintk("%s nfsi->read_io %llu\n", __func__,
1516 nfsi->read_io);
1517 io_set = true;
1518 if (nfsi->read_io < t->rd_io_sz)
1519 io = true;
1521 break;
1522 case IOMODE_RW:
1523 if (t->bm & THRESHOLD_WR) {
1524 dprintk("%s fsize %llu\n", __func__, fsize);
1525 size_set = true;
1526 if (fsize < t->wr_sz)
1527 size = true;
1529 if (t->bm & THRESHOLD_WR_IO) {
1530 dprintk("%s nfsi->write_io %llu\n", __func__,
1531 nfsi->write_io);
1532 io_set = true;
1533 if (nfsi->write_io < t->wr_io_sz)
1534 io = true;
1536 break;
1538 if (size_set && io_set) {
1539 if (size && io)
1540 ret = true;
1541 } else if (size || io)
1542 ret = true;
1544 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1545 return ret;
1548 static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1551 * send layoutcommit as it can hold up layoutreturn due to lseg
1552 * reference
1554 pnfs_layoutcommit_inode(lo->plh_inode, false);
1555 return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
1556 nfs_wait_bit_killable,
1557 TASK_UNINTERRUPTIBLE);
1560 static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1562 unsigned long *bitlock = &lo->plh_flags;
1564 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1565 smp_mb__after_atomic();
1566 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1570 * Layout segment is retreived from the server if not cached.
1571 * The appropriate layout segment is referenced and returned to the caller.
1573 struct pnfs_layout_segment *
1574 pnfs_update_layout(struct inode *ino,
1575 struct nfs_open_context *ctx,
1576 loff_t pos,
1577 u64 count,
1578 enum pnfs_iomode iomode,
1579 bool strict_iomode,
1580 gfp_t gfp_flags)
1582 struct pnfs_layout_range arg = {
1583 .iomode = iomode,
1584 .offset = pos,
1585 .length = count,
1587 unsigned pg_offset, seq;
1588 struct nfs_server *server = NFS_SERVER(ino);
1589 struct nfs_client *clp = server->nfs_client;
1590 struct pnfs_layout_hdr *lo = NULL;
1591 struct pnfs_layout_segment *lseg = NULL;
1592 nfs4_stateid stateid;
1593 long timeout = 0;
1594 unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
1595 bool first;
1597 if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
1598 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1599 PNFS_UPDATE_LAYOUT_NO_PNFS);
1600 goto out;
1603 if (iomode == IOMODE_READ && i_size_read(ino) == 0) {
1604 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1605 PNFS_UPDATE_LAYOUT_RD_ZEROLEN);
1606 goto out;
1609 if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
1610 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1611 PNFS_UPDATE_LAYOUT_MDSTHRESH);
1612 goto out;
1615 lookup_again:
1616 nfs4_client_recover_expired_lease(clp);
1617 first = false;
1618 spin_lock(&ino->i_lock);
1619 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
1620 if (lo == NULL) {
1621 spin_unlock(&ino->i_lock);
1622 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1623 PNFS_UPDATE_LAYOUT_NOMEM);
1624 goto out;
1627 /* Do we even need to bother with this? */
1628 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1629 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1630 PNFS_UPDATE_LAYOUT_BULK_RECALL);
1631 dprintk("%s matches recall, use MDS\n", __func__);
1632 goto out_unlock;
1635 /* if LAYOUTGET already failed once we don't try again */
1636 if (pnfs_layout_io_test_failed(lo, iomode)) {
1637 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1638 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
1639 goto out_unlock;
1642 lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
1643 if (lseg) {
1644 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1645 PNFS_UPDATE_LAYOUT_FOUND_CACHED);
1646 goto out_unlock;
1649 if (!nfs4_valid_open_stateid(ctx->state)) {
1650 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1651 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
1652 goto out_unlock;
1656 * Choose a stateid for the LAYOUTGET. If we don't have a layout
1657 * stateid, or it has been invalidated, then we must use the open
1658 * stateid.
1660 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
1663 * The first layoutget for the file. Need to serialize per
1664 * RFC 5661 Errata 3208.
1666 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1667 &lo->plh_flags)) {
1668 spin_unlock(&ino->i_lock);
1669 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
1670 TASK_UNINTERRUPTIBLE);
1671 pnfs_put_layout_hdr(lo);
1672 dprintk("%s retrying\n", __func__);
1673 goto lookup_again;
1676 first = true;
1677 do {
1678 seq = read_seqbegin(&ctx->state->seqlock);
1679 nfs4_stateid_copy(&stateid, &ctx->state->stateid);
1680 } while (read_seqretry(&ctx->state->seqlock, seq));
1681 } else {
1682 nfs4_stateid_copy(&stateid, &lo->plh_stateid);
1686 * Because we free lsegs before sending LAYOUTRETURN, we need to wait
1687 * for LAYOUTRETURN even if first is true.
1689 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1690 spin_unlock(&ino->i_lock);
1691 dprintk("%s wait for layoutreturn\n", __func__);
1692 if (pnfs_prepare_to_retry_layoutget(lo)) {
1693 if (first)
1694 pnfs_clear_first_layoutget(lo);
1695 pnfs_put_layout_hdr(lo);
1696 dprintk("%s retrying\n", __func__);
1697 trace_pnfs_update_layout(ino, pos, count, iomode, lo,
1698 lseg, PNFS_UPDATE_LAYOUT_RETRY);
1699 goto lookup_again;
1701 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1702 PNFS_UPDATE_LAYOUT_RETURN);
1703 goto out_put_layout_hdr;
1706 if (pnfs_layoutgets_blocked(lo)) {
1707 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1708 PNFS_UPDATE_LAYOUT_BLOCKED);
1709 goto out_unlock;
1711 atomic_inc(&lo->plh_outstanding);
1712 spin_unlock(&ino->i_lock);
1714 if (list_empty(&lo->plh_layouts)) {
1715 /* The lo must be on the clp list if there is any
1716 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1718 spin_lock(&clp->cl_lock);
1719 if (list_empty(&lo->plh_layouts))
1720 list_add_tail(&lo->plh_layouts, &server->layouts);
1721 spin_unlock(&clp->cl_lock);
1724 pg_offset = arg.offset & ~PAGE_MASK;
1725 if (pg_offset) {
1726 arg.offset -= pg_offset;
1727 arg.length += pg_offset;
1729 if (arg.length != NFS4_MAX_UINT64)
1730 arg.length = PAGE_ALIGN(arg.length);
1732 lseg = send_layoutget(lo, ctx, &stateid, &arg, &timeout, gfp_flags);
1733 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1734 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
1735 atomic_dec(&lo->plh_outstanding);
1736 if (IS_ERR(lseg)) {
1737 switch(PTR_ERR(lseg)) {
1738 case -EBUSY:
1739 if (time_after(jiffies, giveup))
1740 lseg = NULL;
1741 break;
1742 case -ERECALLCONFLICT:
1743 /* Huh? We hold no layouts, how is there a recall? */
1744 if (first) {
1745 lseg = NULL;
1746 break;
1748 /* Destroy the existing layout and start over */
1749 if (time_after(jiffies, giveup))
1750 pnfs_destroy_layout(NFS_I(ino));
1751 /* Fallthrough */
1752 case -EAGAIN:
1753 break;
1754 default:
1755 if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
1756 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1757 lseg = NULL;
1759 goto out_put_layout_hdr;
1761 if (lseg) {
1762 if (first)
1763 pnfs_clear_first_layoutget(lo);
1764 trace_pnfs_update_layout(ino, pos, count,
1765 iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
1766 pnfs_put_layout_hdr(lo);
1767 goto lookup_again;
1769 } else {
1770 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1773 out_put_layout_hdr:
1774 if (first)
1775 pnfs_clear_first_layoutget(lo);
1776 pnfs_put_layout_hdr(lo);
1777 out:
1778 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1779 "(%s, offset: %llu, length: %llu)\n",
1780 __func__, ino->i_sb->s_id,
1781 (unsigned long long)NFS_FILEID(ino),
1782 IS_ERR_OR_NULL(lseg) ? "not found" : "found",
1783 iomode==IOMODE_RW ? "read/write" : "read-only",
1784 (unsigned long long)pos,
1785 (unsigned long long)count);
1786 return lseg;
1787 out_unlock:
1788 spin_unlock(&ino->i_lock);
1789 goto out_put_layout_hdr;
1791 EXPORT_SYMBOL_GPL(pnfs_update_layout);
1793 static bool
1794 pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
1796 switch (range->iomode) {
1797 case IOMODE_READ:
1798 case IOMODE_RW:
1799 break;
1800 default:
1801 return false;
1803 if (range->offset == NFS4_MAX_UINT64)
1804 return false;
1805 if (range->length == 0)
1806 return false;
1807 if (range->length != NFS4_MAX_UINT64 &&
1808 range->length > NFS4_MAX_UINT64 - range->offset)
1809 return false;
1810 return true;
1813 struct pnfs_layout_segment *
1814 pnfs_layout_process(struct nfs4_layoutget *lgp)
1816 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1817 struct nfs4_layoutget_res *res = &lgp->res;
1818 struct pnfs_layout_segment *lseg;
1819 struct inode *ino = lo->plh_inode;
1820 LIST_HEAD(free_me);
1822 if (!pnfs_sanity_check_layout_range(&res->range))
1823 return ERR_PTR(-EINVAL);
1825 /* Inject layout blob into I/O device driver */
1826 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
1827 if (IS_ERR_OR_NULL(lseg)) {
1828 if (!lseg)
1829 lseg = ERR_PTR(-ENOMEM);
1831 dprintk("%s: Could not allocate layout: error %ld\n",
1832 __func__, PTR_ERR(lseg));
1833 return lseg;
1836 pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
1838 spin_lock(&ino->i_lock);
1839 if (pnfs_layoutgets_blocked(lo)) {
1840 dprintk("%s forget reply due to state\n", __func__);
1841 goto out_forget;
1844 if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
1845 /* existing state ID, make sure the sequence number matches. */
1846 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1847 dprintk("%s forget reply due to sequence\n", __func__);
1848 goto out_forget;
1850 pnfs_set_layout_stateid(lo, &res->stateid, false);
1851 } else {
1853 * We got an entirely new state ID. Mark all segments for the
1854 * inode invalid, and don't bother validating the stateid
1855 * sequence number.
1857 pnfs_mark_layout_stateid_invalid(lo, &free_me);
1859 pnfs_set_layout_stateid(lo, &res->stateid, true);
1862 pnfs_get_lseg(lseg);
1863 pnfs_layout_insert_lseg(lo, lseg, &free_me);
1866 if (res->return_on_close)
1867 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1869 spin_unlock(&ino->i_lock);
1870 pnfs_free_lseg_list(&free_me);
1871 return lseg;
1873 out_forget:
1874 spin_unlock(&ino->i_lock);
1875 lseg->pls_layout = lo;
1876 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1877 return ERR_PTR(-EAGAIN);
1880 static void
1881 pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
1882 u32 seq)
1884 if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
1885 iomode = IOMODE_ANY;
1886 lo->plh_return_iomode = iomode;
1887 set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
1888 if (seq != 0) {
1889 WARN_ON_ONCE(lo->plh_return_seq != 0 && lo->plh_return_seq != seq);
1890 lo->plh_return_seq = seq;
1895 * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
1896 * @lo: pointer to layout header
1897 * @tmp_list: list header to be used with pnfs_free_lseg_list()
1898 * @return_range: describe layout segment ranges to be returned
1900 * This function is mainly intended for use by layoutrecall. It attempts
1901 * to free the layout segment immediately, or else to mark it for return
1902 * as soon as its reference count drops to zero.
1905 pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
1906 struct list_head *tmp_list,
1907 const struct pnfs_layout_range *return_range,
1908 u32 seq)
1910 struct pnfs_layout_segment *lseg, *next;
1911 int remaining = 0;
1913 dprintk("%s:Begin lo %p\n", __func__, lo);
1915 if (list_empty(&lo->plh_segs))
1916 return 0;
1918 assert_spin_locked(&lo->plh_inode->i_lock);
1920 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
1921 if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
1922 dprintk("%s: marking lseg %p iomode %d "
1923 "offset %llu length %llu\n", __func__,
1924 lseg, lseg->pls_range.iomode,
1925 lseg->pls_range.offset,
1926 lseg->pls_range.length);
1927 if (mark_lseg_invalid(lseg, tmp_list))
1928 continue;
1929 remaining++;
1930 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
1933 if (remaining)
1934 pnfs_set_plh_return_info(lo, return_range->iomode, seq);
1936 return remaining;
1939 void pnfs_error_mark_layout_for_return(struct inode *inode,
1940 struct pnfs_layout_segment *lseg)
1942 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
1943 struct pnfs_layout_range range = {
1944 .iomode = lseg->pls_range.iomode,
1945 .offset = 0,
1946 .length = NFS4_MAX_UINT64,
1948 LIST_HEAD(free_me);
1949 bool return_now = false;
1951 spin_lock(&inode->i_lock);
1952 pnfs_set_plh_return_info(lo, range.iomode, 0);
1954 * mark all matching lsegs so that we are sure to have no live
1955 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
1956 * for how it works.
1958 if (!pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0)) {
1959 nfs4_stateid stateid;
1960 enum pnfs_iomode iomode;
1962 return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
1963 spin_unlock(&inode->i_lock);
1964 if (return_now)
1965 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1966 } else {
1967 spin_unlock(&inode->i_lock);
1968 nfs_commit_inode(inode, 0);
1970 pnfs_free_lseg_list(&free_me);
1972 EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
1974 void
1975 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1977 u64 rd_size = req->wb_bytes;
1979 if (pgio->pg_lseg == NULL) {
1980 if (pgio->pg_dreq == NULL)
1981 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
1982 else
1983 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
1985 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1986 req->wb_context,
1987 req_offset(req),
1988 rd_size,
1989 IOMODE_READ,
1990 false,
1991 GFP_KERNEL);
1992 if (IS_ERR(pgio->pg_lseg)) {
1993 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
1994 pgio->pg_lseg = NULL;
1995 return;
1998 /* If no lseg, fall back to read through mds */
1999 if (pgio->pg_lseg == NULL)
2000 nfs_pageio_reset_read_mds(pgio);
2003 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
2005 void
2006 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
2007 struct nfs_page *req, u64 wb_size)
2009 if (pgio->pg_lseg == NULL) {
2010 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2011 req->wb_context,
2012 req_offset(req),
2013 wb_size,
2014 IOMODE_RW,
2015 false,
2016 GFP_NOFS);
2017 if (IS_ERR(pgio->pg_lseg)) {
2018 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2019 pgio->pg_lseg = NULL;
2020 return;
2023 /* If no lseg, fall back to write through mds */
2024 if (pgio->pg_lseg == NULL)
2025 nfs_pageio_reset_write_mds(pgio);
2027 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
2029 void
2030 pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
2032 if (desc->pg_lseg) {
2033 pnfs_put_lseg(desc->pg_lseg);
2034 desc->pg_lseg = NULL;
2037 EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
2040 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
2041 * of bytes (maximum @req->wb_bytes) that can be coalesced.
2043 size_t
2044 pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
2045 struct nfs_page *prev, struct nfs_page *req)
2047 unsigned int size;
2048 u64 seg_end, req_start, seg_left;
2050 size = nfs_generic_pg_test(pgio, prev, req);
2051 if (!size)
2052 return 0;
2055 * 'size' contains the number of bytes left in the current page (up
2056 * to the original size asked for in @req->wb_bytes).
2058 * Calculate how many bytes are left in the layout segment
2059 * and if there are less bytes than 'size', return that instead.
2061 * Please also note that 'end_offset' is actually the offset of the
2062 * first byte that lies outside the pnfs_layout_range. FIXME?
2065 if (pgio->pg_lseg) {
2066 seg_end = end_offset(pgio->pg_lseg->pls_range.offset,
2067 pgio->pg_lseg->pls_range.length);
2068 req_start = req_offset(req);
2069 WARN_ON_ONCE(req_start >= seg_end);
2070 /* start of request is past the last byte of this segment */
2071 if (req_start >= seg_end) {
2072 /* reference the new lseg */
2073 if (pgio->pg_ops->pg_cleanup)
2074 pgio->pg_ops->pg_cleanup(pgio);
2075 if (pgio->pg_ops->pg_init)
2076 pgio->pg_ops->pg_init(pgio, req);
2077 return 0;
2080 /* adjust 'size' iff there are fewer bytes left in the
2081 * segment than what nfs_generic_pg_test returned */
2082 seg_left = seg_end - req_start;
2083 if (seg_left < size)
2084 size = (unsigned int)seg_left;
2087 return size;
2089 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
2091 int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
2093 struct nfs_pageio_descriptor pgio;
2095 /* Resend all requests through the MDS */
2096 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
2097 hdr->completion_ops);
2098 set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags);
2099 return nfs_pageio_resend(&pgio, hdr);
2101 EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
2103 static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
2106 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
2107 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2108 PNFS_LAYOUTRET_ON_ERROR) {
2109 pnfs_return_layout(hdr->inode);
2111 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2112 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
2116 * Called by non rpc-based layout drivers
2118 void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
2120 if (likely(!hdr->pnfs_error)) {
2121 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
2122 hdr->mds_offset + hdr->res.count);
2123 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
2125 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
2126 if (unlikely(hdr->pnfs_error))
2127 pnfs_ld_handle_write_error(hdr);
2128 hdr->mds_ops->rpc_release(hdr);
2130 EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
2132 static void
2133 pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
2134 struct nfs_pgio_header *hdr)
2136 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2138 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2139 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
2140 nfs_pageio_reset_write_mds(desc);
2141 mirror->pg_recoalesce = 1;
2143 nfs_pgio_data_destroy(hdr);
2144 hdr->release(hdr);
2147 static enum pnfs_try_status
2148 pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
2149 const struct rpc_call_ops *call_ops,
2150 struct pnfs_layout_segment *lseg,
2151 int how)
2153 struct inode *inode = hdr->inode;
2154 enum pnfs_try_status trypnfs;
2155 struct nfs_server *nfss = NFS_SERVER(inode);
2157 hdr->mds_ops = call_ops;
2159 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
2160 inode->i_ino, hdr->args.count, hdr->args.offset, how);
2161 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
2162 if (trypnfs != PNFS_NOT_ATTEMPTED)
2163 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
2164 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2165 return trypnfs;
2168 static void
2169 pnfs_do_write(struct nfs_pageio_descriptor *desc,
2170 struct nfs_pgio_header *hdr, int how)
2172 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2173 struct pnfs_layout_segment *lseg = desc->pg_lseg;
2174 enum pnfs_try_status trypnfs;
2176 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
2177 if (trypnfs == PNFS_NOT_ATTEMPTED)
2178 pnfs_write_through_mds(desc, hdr);
2181 static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
2183 pnfs_put_lseg(hdr->lseg);
2184 nfs_pgio_header_free(hdr);
2188 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
2190 struct nfs_pgio_header *hdr;
2191 int ret;
2193 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2194 if (!hdr) {
2195 desc->pg_error = -ENOMEM;
2196 return desc->pg_error;
2198 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
2200 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
2201 ret = nfs_generic_pgio(desc, hdr);
2202 if (!ret)
2203 pnfs_do_write(desc, hdr, desc->pg_ioflags);
2205 return ret;
2207 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
2209 int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
2211 struct nfs_pageio_descriptor pgio;
2213 /* Resend all requests through the MDS */
2214 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
2215 return nfs_pageio_resend(&pgio, hdr);
2217 EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
2219 static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
2221 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
2222 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2223 PNFS_LAYOUTRET_ON_ERROR) {
2224 pnfs_return_layout(hdr->inode);
2226 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2227 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
2231 * Called by non rpc-based layout drivers
2233 void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
2235 if (likely(!hdr->pnfs_error))
2236 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
2237 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
2238 if (unlikely(hdr->pnfs_error))
2239 pnfs_ld_handle_read_error(hdr);
2240 hdr->mds_ops->rpc_release(hdr);
2242 EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
2244 static void
2245 pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
2246 struct nfs_pgio_header *hdr)
2248 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2250 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2251 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
2252 nfs_pageio_reset_read_mds(desc);
2253 mirror->pg_recoalesce = 1;
2255 nfs_pgio_data_destroy(hdr);
2256 hdr->release(hdr);
2260 * Call the appropriate parallel I/O subsystem read function.
2262 static enum pnfs_try_status
2263 pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
2264 const struct rpc_call_ops *call_ops,
2265 struct pnfs_layout_segment *lseg)
2267 struct inode *inode = hdr->inode;
2268 struct nfs_server *nfss = NFS_SERVER(inode);
2269 enum pnfs_try_status trypnfs;
2271 hdr->mds_ops = call_ops;
2273 dprintk("%s: Reading ino:%lu %u@%llu\n",
2274 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
2276 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
2277 if (trypnfs != PNFS_NOT_ATTEMPTED)
2278 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
2279 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2280 return trypnfs;
2283 /* Resend all requests through pnfs. */
2284 void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
2286 struct nfs_pageio_descriptor pgio;
2288 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2289 nfs_pageio_init_read(&pgio, hdr->inode, false,
2290 hdr->completion_ops);
2291 hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
2294 EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
2296 static void
2297 pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
2299 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2300 struct pnfs_layout_segment *lseg = desc->pg_lseg;
2301 enum pnfs_try_status trypnfs;
2303 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
2304 if (trypnfs == PNFS_TRY_AGAIN)
2305 pnfs_read_resend_pnfs(hdr);
2306 if (trypnfs == PNFS_NOT_ATTEMPTED || hdr->task.tk_status)
2307 pnfs_read_through_mds(desc, hdr);
2310 static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
2312 pnfs_put_lseg(hdr->lseg);
2313 nfs_pgio_header_free(hdr);
2317 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2319 struct nfs_pgio_header *hdr;
2320 int ret;
2322 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2323 if (!hdr) {
2324 desc->pg_error = -ENOMEM;
2325 return desc->pg_error;
2327 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
2328 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
2329 ret = nfs_generic_pgio(desc, hdr);
2330 if (!ret)
2331 pnfs_do_read(desc, hdr);
2332 return ret;
2334 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
2336 static void pnfs_clear_layoutcommitting(struct inode *inode)
2338 unsigned long *bitlock = &NFS_I(inode)->flags;
2340 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
2341 smp_mb__after_atomic();
2342 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2346 * There can be multiple RW segments.
2348 static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
2350 struct pnfs_layout_segment *lseg;
2352 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2353 if (lseg->pls_range.iomode == IOMODE_RW &&
2354 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
2355 list_add(&lseg->pls_lc_list, listp);
2359 static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2361 struct pnfs_layout_segment *lseg, *tmp;
2363 /* Matched by references in pnfs_set_layoutcommit */
2364 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2365 list_del_init(&lseg->pls_lc_list);
2366 pnfs_put_lseg(lseg);
2369 pnfs_clear_layoutcommitting(inode);
2372 void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2374 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
2376 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2378 void
2379 pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
2380 loff_t end_pos)
2382 struct nfs_inode *nfsi = NFS_I(inode);
2383 bool mark_as_dirty = false;
2385 spin_lock(&inode->i_lock);
2386 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
2387 nfsi->layout->plh_lwb = end_pos;
2388 mark_as_dirty = true;
2389 dprintk("%s: Set layoutcommit for inode %lu ",
2390 __func__, inode->i_ino);
2391 } else if (end_pos > nfsi->layout->plh_lwb)
2392 nfsi->layout->plh_lwb = end_pos;
2393 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
2394 /* references matched in nfs4_layoutcommit_release */
2395 pnfs_get_lseg(lseg);
2397 spin_unlock(&inode->i_lock);
2398 dprintk("%s: lseg %p end_pos %llu\n",
2399 __func__, lseg, nfsi->layout->plh_lwb);
2401 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2402 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2403 if (mark_as_dirty)
2404 mark_inode_dirty_sync(inode);
2406 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2408 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2410 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2412 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2413 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
2414 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
2418 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2419 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2420 * data to disk to allow the server to recover the data if it crashes.
2421 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2422 * is off, and a COMMIT is sent to a data server, or
2423 * if WRITEs to a data server return NFS_DATA_SYNC.
2426 pnfs_layoutcommit_inode(struct inode *inode, bool sync)
2428 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2429 struct nfs4_layoutcommit_data *data;
2430 struct nfs_inode *nfsi = NFS_I(inode);
2431 loff_t end_pos;
2432 int status;
2434 if (!pnfs_layoutcommit_outstanding(inode))
2435 return 0;
2437 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
2439 status = -EAGAIN;
2440 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
2441 if (!sync)
2442 goto out;
2443 status = wait_on_bit_lock_action(&nfsi->flags,
2444 NFS_INO_LAYOUTCOMMITTING,
2445 nfs_wait_bit_killable,
2446 TASK_KILLABLE);
2447 if (status)
2448 goto out;
2451 status = -ENOMEM;
2452 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2453 data = kzalloc(sizeof(*data), GFP_NOFS);
2454 if (!data)
2455 goto clear_layoutcommitting;
2457 status = 0;
2458 spin_lock(&inode->i_lock);
2459 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2460 goto out_unlock;
2462 INIT_LIST_HEAD(&data->lseg_list);
2463 pnfs_list_write_lseg(inode, &data->lseg_list);
2465 end_pos = nfsi->layout->plh_lwb;
2467 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
2468 spin_unlock(&inode->i_lock);
2470 data->args.inode = inode;
2471 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
2472 nfs_fattr_init(&data->fattr);
2473 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2474 data->res.fattr = &data->fattr;
2475 if (end_pos != 0)
2476 data->args.lastbytewritten = end_pos - 1;
2477 else
2478 data->args.lastbytewritten = U64_MAX;
2479 data->res.server = NFS_SERVER(inode);
2481 if (ld->prepare_layoutcommit) {
2482 status = ld->prepare_layoutcommit(&data->args);
2483 if (status) {
2484 put_rpccred(data->cred);
2485 spin_lock(&inode->i_lock);
2486 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2487 if (end_pos > nfsi->layout->plh_lwb)
2488 nfsi->layout->plh_lwb = end_pos;
2489 goto out_unlock;
2494 status = nfs4_proc_layoutcommit(data, sync);
2495 out:
2496 if (status)
2497 mark_inode_dirty_sync(inode);
2498 dprintk("<-- %s status %d\n", __func__, status);
2499 return status;
2500 out_unlock:
2501 spin_unlock(&inode->i_lock);
2502 kfree(data);
2503 clear_layoutcommitting:
2504 pnfs_clear_layoutcommitting(inode);
2505 goto out;
2507 EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
2510 pnfs_generic_sync(struct inode *inode, bool datasync)
2512 return pnfs_layoutcommit_inode(inode, true);
2514 EXPORT_SYMBOL_GPL(pnfs_generic_sync);
2516 struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
2518 struct nfs4_threshold *thp;
2520 thp = kzalloc(sizeof(*thp), GFP_NOFS);
2521 if (!thp) {
2522 dprintk("%s mdsthreshold allocation failed\n", __func__);
2523 return NULL;
2525 return thp;
2528 #if IS_ENABLED(CONFIG_NFS_V4_2)
2530 pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
2532 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2533 struct nfs_server *server = NFS_SERVER(inode);
2534 struct nfs_inode *nfsi = NFS_I(inode);
2535 struct nfs42_layoutstat_data *data;
2536 struct pnfs_layout_hdr *hdr;
2537 int status = 0;
2539 if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
2540 goto out;
2542 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
2543 goto out;
2545 if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
2546 goto out;
2548 spin_lock(&inode->i_lock);
2549 if (!NFS_I(inode)->layout) {
2550 spin_unlock(&inode->i_lock);
2551 goto out_clear_layoutstats;
2553 hdr = NFS_I(inode)->layout;
2554 pnfs_get_layout_hdr(hdr);
2555 spin_unlock(&inode->i_lock);
2557 data = kzalloc(sizeof(*data), gfp_flags);
2558 if (!data) {
2559 status = -ENOMEM;
2560 goto out_put;
2563 data->args.fh = NFS_FH(inode);
2564 data->args.inode = inode;
2565 status = ld->prepare_layoutstats(&data->args);
2566 if (status)
2567 goto out_free;
2569 status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
2571 out:
2572 dprintk("%s returns %d\n", __func__, status);
2573 return status;
2575 out_free:
2576 kfree(data);
2577 out_put:
2578 pnfs_put_layout_hdr(hdr);
2579 out_clear_layoutstats:
2580 smp_mb__before_atomic();
2581 clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
2582 smp_mb__after_atomic();
2583 goto out;
2585 EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
2586 #endif
2588 unsigned int layoutstats_timer;
2589 module_param(layoutstats_timer, uint, 0644);
2590 EXPORT_SYMBOL_GPL(layoutstats_timer);