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>
37 #define NFSDBG_FACILITY NFSDBG_PNFS
42 * protects pnfs_modules_tbl.
44 static DEFINE_SPINLOCK(pnfs_spinlock);
47 * pnfs_modules_tbl holds all pnfs modules
49 static LIST_HEAD(pnfs_modules_tbl);
51 /* Return the registered pnfs layout driver module matching given id */
52 static struct pnfs_layoutdriver_type *
53 find_pnfs_driver_locked(u32 id)
55 struct pnfs_layoutdriver_type *local;
57 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
62 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
66 static struct pnfs_layoutdriver_type *
67 find_pnfs_driver(u32 id)
69 struct pnfs_layoutdriver_type *local;
71 spin_lock(&pnfs_spinlock);
72 local = find_pnfs_driver_locked(id);
73 spin_unlock(&pnfs_spinlock);
78 unset_pnfs_layoutdriver(struct nfs_server *nfss)
80 if (nfss->pnfs_curr_ld) {
81 if (nfss->pnfs_curr_ld->clear_layoutdriver)
82 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
83 module_put(nfss->pnfs_curr_ld->owner);
85 nfss->pnfs_curr_ld = NULL;
89 * Try to set the server's pnfs module to the pnfs layout type specified by id.
90 * Currently only one pNFS layout driver per filesystem is supported.
92 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
95 set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
98 struct pnfs_layoutdriver_type *ld_type = NULL;
102 if (!(server->nfs_client->cl_exchange_flags &
103 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
104 printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__,
105 id, server->nfs_client->cl_exchange_flags);
108 ld_type = find_pnfs_driver(id);
110 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
111 ld_type = find_pnfs_driver(id);
113 dprintk("%s: No pNFS module found for %u.\n",
118 if (!try_module_get(ld_type->owner)) {
119 dprintk("%s: Could not grab reference on module\n", __func__);
122 server->pnfs_curr_ld = ld_type;
123 if (ld_type->set_layoutdriver
124 && ld_type->set_layoutdriver(server, mntfh)) {
125 printk(KERN_ERR "%s: Error initializing pNFS layout driver %u.\n",
127 module_put(ld_type->owner);
131 dprintk("%s: pNFS module for %u set\n", __func__, id);
135 dprintk("%s: Using NFSv4 I/O\n", __func__);
136 server->pnfs_curr_ld = NULL;
140 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
142 int status = -EINVAL;
143 struct pnfs_layoutdriver_type *tmp;
145 if (ld_type->id == 0) {
146 printk(KERN_ERR "%s id 0 is reserved\n", __func__);
149 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
150 printk(KERN_ERR "%s Layout driver must provide "
151 "alloc_lseg and free_lseg.\n", __func__);
155 spin_lock(&pnfs_spinlock);
156 tmp = find_pnfs_driver_locked(ld_type->id);
158 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
160 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
163 printk(KERN_ERR "%s Module with id %d already loaded!\n",
164 __func__, ld_type->id);
166 spin_unlock(&pnfs_spinlock);
170 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
173 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
175 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
176 spin_lock(&pnfs_spinlock);
177 list_del(&ld_type->pnfs_tblid);
178 spin_unlock(&pnfs_spinlock);
180 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
183 * pNFS client layout cache
186 /* Need to hold i_lock if caller does not already hold reference */
188 get_layout_hdr(struct pnfs_layout_hdr *lo)
190 atomic_inc(&lo->plh_refcount);
193 static struct pnfs_layout_hdr *
194 pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
196 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
197 return ld->alloc_layout_hdr ? ld->alloc_layout_hdr(ino, gfp_flags) :
198 kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags);
202 pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
204 struct pnfs_layoutdriver_type *ld = NFS_SERVER(lo->plh_inode)->pnfs_curr_ld;
205 put_rpccred(lo->plh_lc_cred);
206 return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo);
210 destroy_layout_hdr(struct pnfs_layout_hdr *lo)
212 dprintk("%s: freeing layout cache %p\n", __func__, lo);
213 BUG_ON(!list_empty(&lo->plh_layouts));
214 NFS_I(lo->plh_inode)->layout = NULL;
215 pnfs_free_layout_hdr(lo);
219 put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
221 if (atomic_dec_and_test(&lo->plh_refcount))
222 destroy_layout_hdr(lo);
226 put_layout_hdr(struct pnfs_layout_hdr *lo)
228 struct inode *inode = lo->plh_inode;
230 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
231 destroy_layout_hdr(lo);
232 spin_unlock(&inode->i_lock);
237 init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
239 INIT_LIST_HEAD(&lseg->pls_list);
240 INIT_LIST_HEAD(&lseg->pls_lc_list);
241 atomic_set(&lseg->pls_refcount, 1);
243 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
244 lseg->pls_layout = lo;
247 static void free_lseg(struct pnfs_layout_segment *lseg)
249 struct inode *ino = lseg->pls_layout->plh_inode;
251 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
252 /* Matched by get_layout_hdr in pnfs_insert_layout */
253 put_layout_hdr(NFS_I(ino)->layout);
257 put_lseg_common(struct pnfs_layout_segment *lseg)
259 struct inode *inode = lseg->pls_layout->plh_inode;
261 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
262 list_del_init(&lseg->pls_list);
263 if (list_empty(&lseg->pls_layout->plh_segs)) {
264 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
265 /* Matched by initial refcount set in alloc_init_layout_hdr */
266 put_layout_hdr_locked(lseg->pls_layout);
268 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
272 put_lseg(struct pnfs_layout_segment *lseg)
279 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
280 atomic_read(&lseg->pls_refcount),
281 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
282 inode = lseg->pls_layout->plh_inode;
283 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
286 put_lseg_common(lseg);
287 list_add(&lseg->pls_list, &free_me);
288 spin_unlock(&inode->i_lock);
289 pnfs_free_lseg_list(&free_me);
292 EXPORT_SYMBOL_GPL(put_lseg);
295 end_offset(u64 start, u64 len)
300 return end >= start ? end : NFS4_MAX_UINT64;
303 /* last octet in a range */
305 last_byte_offset(u64 start, u64 len)
311 return end > start ? end - 1 : NFS4_MAX_UINT64;
315 * is l2 fully contained in l1?
317 * [----------------------------------)
322 lo_seg_contained(struct pnfs_layout_range *l1,
323 struct pnfs_layout_range *l2)
325 u64 start1 = l1->offset;
326 u64 end1 = end_offset(start1, l1->length);
327 u64 start2 = l2->offset;
328 u64 end2 = end_offset(start2, l2->length);
330 return (start1 <= start2) && (end1 >= end2);
334 * is l1 and l2 intersecting?
336 * [----------------------------------)
341 lo_seg_intersecting(struct pnfs_layout_range *l1,
342 struct pnfs_layout_range *l2)
344 u64 start1 = l1->offset;
345 u64 end1 = end_offset(start1, l1->length);
346 u64 start2 = l2->offset;
347 u64 end2 = end_offset(start2, l2->length);
349 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
350 (end2 == NFS4_MAX_UINT64 || end2 > start1);
354 should_free_lseg(struct pnfs_layout_range *lseg_range,
355 struct pnfs_layout_range *recall_range)
357 return (recall_range->iomode == IOMODE_ANY ||
358 lseg_range->iomode == recall_range->iomode) &&
359 lo_seg_intersecting(lseg_range, recall_range);
362 /* Returns 1 if lseg is removed from list, 0 otherwise */
363 static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
364 struct list_head *tmp_list)
368 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
369 /* Remove the reference keeping the lseg in the
370 * list. It will now be removed when all
371 * outstanding io is finished.
373 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
374 atomic_read(&lseg->pls_refcount));
375 if (atomic_dec_and_test(&lseg->pls_refcount)) {
376 put_lseg_common(lseg);
377 list_add(&lseg->pls_list, tmp_list);
384 /* Returns count of number of matching invalid lsegs remaining in list
388 mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
389 struct list_head *tmp_list,
390 struct pnfs_layout_range *recall_range)
392 struct pnfs_layout_segment *lseg, *next;
393 int invalid = 0, removed = 0;
395 dprintk("%s:Begin lo %p\n", __func__, lo);
397 if (list_empty(&lo->plh_segs)) {
398 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
399 put_layout_hdr_locked(lo);
402 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
404 should_free_lseg(&lseg->pls_range, recall_range)) {
405 dprintk("%s: freeing lseg %p iomode %d "
406 "offset %llu length %llu\n", __func__,
407 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
408 lseg->pls_range.length);
410 removed += mark_lseg_invalid(lseg, tmp_list);
412 dprintk("%s:Return %i\n", __func__, invalid - removed);
413 return invalid - removed;
416 /* note free_me must contain lsegs from a single layout_hdr */
418 pnfs_free_lseg_list(struct list_head *free_me)
420 struct pnfs_layout_segment *lseg, *tmp;
421 struct pnfs_layout_hdr *lo;
423 if (list_empty(free_me))
426 lo = list_first_entry(free_me, struct pnfs_layout_segment,
427 pls_list)->pls_layout;
429 if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
430 struct nfs_client *clp;
432 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
433 spin_lock(&clp->cl_lock);
434 list_del_init(&lo->plh_layouts);
435 spin_unlock(&clp->cl_lock);
437 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
438 list_del(&lseg->pls_list);
444 pnfs_destroy_layout(struct nfs_inode *nfsi)
446 struct pnfs_layout_hdr *lo;
449 spin_lock(&nfsi->vfs_inode.i_lock);
452 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
453 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
455 spin_unlock(&nfsi->vfs_inode.i_lock);
456 pnfs_free_lseg_list(&tmp_list);
460 * Called by the state manger to remove all layouts established under an
464 pnfs_destroy_all_layouts(struct nfs_client *clp)
466 struct nfs_server *server;
467 struct pnfs_layout_hdr *lo;
470 nfs4_deviceid_mark_client_invalid(clp);
471 nfs4_deviceid_purge_client(clp);
473 spin_lock(&clp->cl_lock);
475 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
476 if (!list_empty(&server->layouts))
477 list_splice_init(&server->layouts, &tmp_list);
480 spin_unlock(&clp->cl_lock);
482 while (!list_empty(&tmp_list)) {
483 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
485 dprintk("%s freeing layout for inode %lu\n", __func__,
486 lo->plh_inode->i_ino);
487 list_del_init(&lo->plh_layouts);
488 pnfs_destroy_layout(NFS_I(lo->plh_inode));
492 /* update lo->plh_stateid with new if is more recent */
494 pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
499 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
500 newseq = be32_to_cpu(new->stateid.seqid);
501 if ((int)(newseq - oldseq) > 0) {
502 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
503 if (update_barrier) {
504 u32 new_barrier = be32_to_cpu(new->stateid.seqid);
506 if ((int)(new_barrier - lo->plh_barrier))
507 lo->plh_barrier = new_barrier;
509 /* Because of wraparound, we want to keep the barrier
510 * "close" to the current seqids. It needs to be
511 * within 2**31 to count as "behind", so if it
512 * gets too near that limit, give us a litle leeway
513 * and bring it to within 2**30.
514 * NOTE - and yes, this is all unsigned arithmetic.
516 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
517 lo->plh_barrier = newseq - (1 << 30);
522 /* lget is set to 1 if called from inside send_layoutget call chain */
524 pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
528 (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
530 return lo->plh_block_lgets ||
531 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
532 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
533 (list_empty(&lo->plh_segs) &&
534 (atomic_read(&lo->plh_outstanding) > lget));
538 pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
539 struct nfs4_state *open_state)
543 dprintk("--> %s\n", __func__);
544 spin_lock(&lo->plh_inode->i_lock);
545 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
547 } else if (list_empty(&lo->plh_segs)) {
551 seq = read_seqbegin(&open_state->seqlock);
552 memcpy(dst->data, open_state->stateid.data,
553 sizeof(open_state->stateid.data));
554 } while (read_seqretry(&open_state->seqlock, seq));
556 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
557 spin_unlock(&lo->plh_inode->i_lock);
558 dprintk("<-- %s\n", __func__);
563 * Get layout from server.
564 * for now, assume that whole file layouts are requested.
566 * arg->length: all ones
568 static struct pnfs_layout_segment *
569 send_layoutget(struct pnfs_layout_hdr *lo,
570 struct nfs_open_context *ctx,
571 struct pnfs_layout_range *range,
574 struct inode *ino = lo->plh_inode;
575 struct nfs_server *server = NFS_SERVER(ino);
576 struct nfs4_layoutget *lgp;
577 struct pnfs_layout_segment *lseg = NULL;
579 dprintk("--> %s\n", __func__);
582 lgp = kzalloc(sizeof(*lgp), gfp_flags);
586 lgp->args.minlength = PAGE_CACHE_SIZE;
587 if (lgp->args.minlength > range->length)
588 lgp->args.minlength = range->length;
589 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
590 lgp->args.range = *range;
591 lgp->args.type = server->pnfs_curr_ld->id;
592 lgp->args.inode = ino;
593 lgp->args.ctx = get_nfs_open_context(ctx);
595 lgp->gfp_flags = gfp_flags;
597 /* Synchronously retrieve layout information from server and
600 nfs4_proc_layoutget(lgp, gfp_flags);
602 /* remember that LAYOUTGET failed and suspend trying */
603 set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
609 /* Initiates a LAYOUTRETURN(FILE) */
611 _pnfs_return_layout(struct inode *ino)
613 struct pnfs_layout_hdr *lo = NULL;
614 struct nfs_inode *nfsi = NFS_I(ino);
616 struct nfs4_layoutreturn *lrp;
617 nfs4_stateid stateid;
620 dprintk("--> %s\n", __func__);
622 spin_lock(&ino->i_lock);
625 spin_unlock(&ino->i_lock);
626 dprintk("%s: no layout to return\n", __func__);
629 stateid = nfsi->layout->plh_stateid;
630 /* Reference matched in nfs4_layoutreturn_release */
632 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
633 lo->plh_block_lgets++;
634 spin_unlock(&ino->i_lock);
635 pnfs_free_lseg_list(&tmp_list);
637 WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
639 lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
640 if (unlikely(lrp == NULL)) {
642 set_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags);
643 set_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags);
648 lrp->args.stateid = stateid;
649 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
650 lrp->args.inode = ino;
651 lrp->args.layout = lo;
652 lrp->clp = NFS_SERVER(ino)->nfs_client;
654 status = nfs4_proc_layoutreturn(lrp);
656 dprintk("<-- %s status: %d\n", __func__, status);
660 bool pnfs_roc(struct inode *ino)
662 struct pnfs_layout_hdr *lo;
663 struct pnfs_layout_segment *lseg, *tmp;
667 spin_lock(&ino->i_lock);
668 lo = NFS_I(ino)->layout;
669 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
670 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
672 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
673 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
674 mark_lseg_invalid(lseg, &tmp_list);
679 lo->plh_block_lgets++;
680 get_layout_hdr(lo); /* matched in pnfs_roc_release */
681 spin_unlock(&ino->i_lock);
682 pnfs_free_lseg_list(&tmp_list);
686 spin_unlock(&ino->i_lock);
690 void pnfs_roc_release(struct inode *ino)
692 struct pnfs_layout_hdr *lo;
694 spin_lock(&ino->i_lock);
695 lo = NFS_I(ino)->layout;
696 lo->plh_block_lgets--;
697 put_layout_hdr_locked(lo);
698 spin_unlock(&ino->i_lock);
701 void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
703 struct pnfs_layout_hdr *lo;
705 spin_lock(&ino->i_lock);
706 lo = NFS_I(ino)->layout;
707 if ((int)(barrier - lo->plh_barrier) > 0)
708 lo->plh_barrier = barrier;
709 spin_unlock(&ino->i_lock);
712 bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
714 struct nfs_inode *nfsi = NFS_I(ino);
715 struct pnfs_layout_segment *lseg;
718 spin_lock(&ino->i_lock);
719 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
720 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
725 struct pnfs_layout_hdr *lo = nfsi->layout;
726 u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
728 /* Since close does not return a layout stateid for use as
729 * a barrier, we choose the worst-case barrier.
731 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
733 spin_unlock(&ino->i_lock);
738 * Compare two layout segments for sorting into layout cache.
739 * We want to preferentially return RW over RO layouts, so ensure those
743 cmp_layout(struct pnfs_layout_range *l1,
744 struct pnfs_layout_range *l2)
748 /* high offset > low offset */
749 d = l1->offset - l2->offset;
753 /* short length > long length */
754 d = l2->length - l1->length;
758 /* read > read/write */
759 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
763 pnfs_insert_layout(struct pnfs_layout_hdr *lo,
764 struct pnfs_layout_segment *lseg)
766 struct pnfs_layout_segment *lp;
768 dprintk("%s:Begin\n", __func__);
770 assert_spin_locked(&lo->plh_inode->i_lock);
771 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
772 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
774 list_add_tail(&lseg->pls_list, &lp->pls_list);
775 dprintk("%s: inserted lseg %p "
776 "iomode %d offset %llu length %llu before "
777 "lp %p iomode %d offset %llu length %llu\n",
778 __func__, lseg, lseg->pls_range.iomode,
779 lseg->pls_range.offset, lseg->pls_range.length,
780 lp, lp->pls_range.iomode, lp->pls_range.offset,
781 lp->pls_range.length);
784 list_add_tail(&lseg->pls_list, &lo->plh_segs);
785 dprintk("%s: inserted lseg %p "
786 "iomode %d offset %llu length %llu at tail\n",
787 __func__, lseg, lseg->pls_range.iomode,
788 lseg->pls_range.offset, lseg->pls_range.length);
792 dprintk("%s:Return\n", __func__);
795 static struct pnfs_layout_hdr *
796 alloc_init_layout_hdr(struct inode *ino,
797 struct nfs_open_context *ctx,
800 struct pnfs_layout_hdr *lo;
802 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
805 atomic_set(&lo->plh_refcount, 1);
806 INIT_LIST_HEAD(&lo->plh_layouts);
807 INIT_LIST_HEAD(&lo->plh_segs);
808 INIT_LIST_HEAD(&lo->plh_bulk_recall);
810 lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred);
814 static struct pnfs_layout_hdr *
815 pnfs_find_alloc_layout(struct inode *ino,
816 struct nfs_open_context *ctx,
819 struct nfs_inode *nfsi = NFS_I(ino);
820 struct pnfs_layout_hdr *new = NULL;
822 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
824 assert_spin_locked(&ino->i_lock);
826 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
831 spin_unlock(&ino->i_lock);
832 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
833 spin_lock(&ino->i_lock);
835 if (likely(nfsi->layout == NULL)) /* Won the race? */
838 pnfs_free_layout_hdr(new);
843 * iomode matching rules:
854 is_matching_lseg(struct pnfs_layout_range *ls_range,
855 struct pnfs_layout_range *range)
857 struct pnfs_layout_range range1;
859 if ((range->iomode == IOMODE_RW &&
860 ls_range->iomode != IOMODE_RW) ||
861 !lo_seg_intersecting(ls_range, range))
864 /* range1 covers only the first byte in the range */
867 return lo_seg_contained(ls_range, &range1);
871 * lookup range in layout
873 static struct pnfs_layout_segment *
874 pnfs_find_lseg(struct pnfs_layout_hdr *lo,
875 struct pnfs_layout_range *range)
877 struct pnfs_layout_segment *lseg, *ret = NULL;
879 dprintk("%s:Begin\n", __func__);
881 assert_spin_locked(&lo->plh_inode->i_lock);
882 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
883 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
884 is_matching_lseg(&lseg->pls_range, range)) {
885 ret = get_lseg(lseg);
888 if (lseg->pls_range.offset > range->offset)
892 dprintk("%s:Return lseg %p ref %d\n",
893 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
898 * Layout segment is retreived from the server if not cached.
899 * The appropriate layout segment is referenced and returned to the caller.
901 struct pnfs_layout_segment *
902 pnfs_update_layout(struct inode *ino,
903 struct nfs_open_context *ctx,
906 enum pnfs_iomode iomode,
909 struct pnfs_layout_range arg = {
915 struct nfs_inode *nfsi = NFS_I(ino);
916 struct nfs_server *server = NFS_SERVER(ino);
917 struct nfs_client *clp = server->nfs_client;
918 struct pnfs_layout_hdr *lo;
919 struct pnfs_layout_segment *lseg = NULL;
922 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
924 spin_lock(&ino->i_lock);
925 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
927 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
931 /* Do we even need to bother with this? */
932 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
933 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
934 dprintk("%s matches recall, use MDS\n", __func__);
938 /* if LAYOUTGET already failed once we don't try again */
939 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
942 /* Check to see if the layout for the given range already exists */
943 lseg = pnfs_find_lseg(lo, &arg);
947 if (pnfs_layoutgets_blocked(lo, NULL, 0))
949 atomic_inc(&lo->plh_outstanding);
952 if (list_empty(&lo->plh_segs))
954 spin_unlock(&ino->i_lock);
956 /* The lo must be on the clp list if there is any
957 * chance of a CB_LAYOUTRECALL(FILE) coming in.
959 spin_lock(&clp->cl_lock);
960 BUG_ON(!list_empty(&lo->plh_layouts));
961 list_add_tail(&lo->plh_layouts, &server->layouts);
962 spin_unlock(&clp->cl_lock);
965 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
967 arg.offset -= pg_offset;
968 arg.length += pg_offset;
970 if (arg.length != NFS4_MAX_UINT64)
971 arg.length = PAGE_CACHE_ALIGN(arg.length);
973 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
974 if (!lseg && first) {
975 spin_lock(&clp->cl_lock);
976 list_del_init(&lo->plh_layouts);
977 spin_unlock(&clp->cl_lock);
979 atomic_dec(&lo->plh_outstanding);
982 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
983 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
986 spin_unlock(&ino->i_lock);
989 EXPORT_SYMBOL_GPL(pnfs_update_layout);
992 pnfs_layout_process(struct nfs4_layoutget *lgp)
994 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
995 struct nfs4_layoutget_res *res = &lgp->res;
996 struct pnfs_layout_segment *lseg;
997 struct inode *ino = lo->plh_inode;
998 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
1001 /* Inject layout blob into I/O device driver */
1002 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
1003 if (!lseg || IS_ERR(lseg)) {
1007 status = PTR_ERR(lseg);
1008 dprintk("%s: Could not allocate layout: error %d\n",
1013 spin_lock(&ino->i_lock);
1014 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
1015 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1016 dprintk("%s forget reply due to recall\n", __func__);
1017 goto out_forget_reply;
1020 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
1021 dprintk("%s forget reply due to state\n", __func__);
1022 goto out_forget_reply;
1024 init_lseg(lo, lseg);
1025 lseg->pls_range = res->range;
1026 *lgp->lsegpp = get_lseg(lseg);
1027 pnfs_insert_layout(lo, lseg);
1029 if (res->return_on_close) {
1030 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1031 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1034 /* Done processing layoutget. Set the layout stateid */
1035 pnfs_set_layout_stateid(lo, &res->stateid, false);
1036 spin_unlock(&ino->i_lock);
1041 spin_unlock(&ino->i_lock);
1042 lseg->pls_layout = lo;
1043 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1048 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1050 BUG_ON(pgio->pg_lseg != NULL);
1052 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1058 /* If no lseg, fall back to read through mds */
1059 if (pgio->pg_lseg == NULL)
1060 nfs_pageio_reset_read_mds(pgio);
1063 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1066 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1068 BUG_ON(pgio->pg_lseg != NULL);
1070 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1076 /* If no lseg, fall back to write through mds */
1077 if (pgio->pg_lseg == NULL)
1078 nfs_pageio_reset_write_mds(pgio);
1080 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1083 pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode)
1085 struct nfs_server *server = NFS_SERVER(inode);
1086 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1090 nfs_pageio_init(pgio, inode, ld->pg_read_ops, server->rsize, 0);
1095 pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode, int ioflags)
1097 struct nfs_server *server = NFS_SERVER(inode);
1098 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1102 nfs_pageio_init(pgio, inode, ld->pg_write_ops, server->wsize, ioflags);
1107 pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1108 struct nfs_page *req)
1110 if (pgio->pg_lseg == NULL)
1111 return nfs_generic_pg_test(pgio, prev, req);
1114 * Test if a nfs_page is fully contained in the pnfs_layout_range.
1115 * Note that this test makes several assumptions:
1116 * - that the previous nfs_page in the struct nfs_pageio_descriptor
1117 * is known to lie within the range.
1118 * - that the nfs_page being tested is known to be contiguous with the
1119 * previous nfs_page.
1120 * - Layout ranges are page aligned, so we only have to test the
1121 * start offset of the request.
1123 * Please also note that 'end_offset' is actually the offset of the
1124 * first byte that lies outside the pnfs_layout_range. FIXME?
1127 return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
1128 pgio->pg_lseg->pls_range.length);
1130 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
1133 * Called by non rpc-based layout drivers
1135 void pnfs_ld_write_done(struct nfs_write_data *data)
1137 if (likely(!data->pnfs_error)) {
1138 pnfs_set_layoutcommit(data);
1139 data->mds_ops->rpc_call_done(&data->task, data);
1141 put_lseg(data->lseg);
1143 dprintk("pnfs write error = %d\n", data->pnfs_error);
1144 if (NFS_SERVER(data->inode)->pnfs_curr_ld->flags &
1145 PNFS_LAYOUTRET_ON_ERROR) {
1146 /* Don't lo_commit on error, Server will needs to
1147 * preform a file recovery.
1149 clear_bit(NFS_INO_LAYOUTCOMMIT,
1150 &NFS_I(data->inode)->flags);
1151 pnfs_return_layout(data->inode);
1154 data->mds_ops->rpc_release(data);
1156 EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
1159 pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
1160 struct nfs_write_data *data)
1162 list_splice_tail_init(&data->pages, &desc->pg_list);
1163 if (data->req && list_empty(&data->req->wb_list))
1164 nfs_list_add_request(data->req, &desc->pg_list);
1165 nfs_pageio_reset_write_mds(desc);
1166 desc->pg_recoalesce = 1;
1167 nfs_writedata_release(data);
1170 static enum pnfs_try_status
1171 pnfs_try_to_write_data(struct nfs_write_data *wdata,
1172 const struct rpc_call_ops *call_ops,
1173 struct pnfs_layout_segment *lseg,
1176 struct inode *inode = wdata->inode;
1177 enum pnfs_try_status trypnfs;
1178 struct nfs_server *nfss = NFS_SERVER(inode);
1180 wdata->mds_ops = call_ops;
1181 wdata->lseg = get_lseg(lseg);
1183 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1184 inode->i_ino, wdata->args.count, wdata->args.offset, how);
1186 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
1187 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1188 put_lseg(wdata->lseg);
1191 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1193 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1198 pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how)
1200 struct nfs_write_data *data;
1201 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1202 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1204 desc->pg_lseg = NULL;
1205 while (!list_empty(head)) {
1206 enum pnfs_try_status trypnfs;
1208 data = list_entry(head->next, struct nfs_write_data, list);
1209 list_del_init(&data->list);
1211 trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
1212 if (trypnfs == PNFS_NOT_ATTEMPTED)
1213 pnfs_write_through_mds(desc, data);
1219 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1224 ret = nfs_generic_flush(desc, &head);
1226 put_lseg(desc->pg_lseg);
1227 desc->pg_lseg = NULL;
1230 pnfs_do_multiple_writes(desc, &head, desc->pg_ioflags);
1233 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
1235 static void pnfs_ld_handle_read_error(struct nfs_read_data *data)
1237 struct nfs_pageio_descriptor pgio;
1239 put_lseg(data->lseg);
1241 dprintk("pnfs write error = %d\n", data->pnfs_error);
1242 if (NFS_SERVER(data->inode)->pnfs_curr_ld->flags &
1243 PNFS_LAYOUTRET_ON_ERROR)
1244 pnfs_return_layout(data->inode);
1246 nfs_pageio_init_read_mds(&pgio, data->inode);
1248 while (!list_empty(&data->pages)) {
1249 struct nfs_page *req = nfs_list_entry(data->pages.next);
1251 nfs_list_remove_request(req);
1252 nfs_pageio_add_request(&pgio, req);
1254 nfs_pageio_complete(&pgio);
1258 * Called by non rpc-based layout drivers
1260 void pnfs_ld_read_done(struct nfs_read_data *data)
1262 if (likely(!data->pnfs_error)) {
1263 __nfs4_read_done_cb(data);
1264 data->mds_ops->rpc_call_done(&data->task, data);
1266 pnfs_ld_handle_read_error(data);
1267 data->mds_ops->rpc_release(data);
1269 EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1272 pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
1273 struct nfs_read_data *data)
1275 list_splice_tail_init(&data->pages, &desc->pg_list);
1276 if (data->req && list_empty(&data->req->wb_list))
1277 nfs_list_add_request(data->req, &desc->pg_list);
1278 nfs_pageio_reset_read_mds(desc);
1279 desc->pg_recoalesce = 1;
1280 nfs_readdata_release(data);
1284 * Call the appropriate parallel I/O subsystem read function.
1286 static enum pnfs_try_status
1287 pnfs_try_to_read_data(struct nfs_read_data *rdata,
1288 const struct rpc_call_ops *call_ops,
1289 struct pnfs_layout_segment *lseg)
1291 struct inode *inode = rdata->inode;
1292 struct nfs_server *nfss = NFS_SERVER(inode);
1293 enum pnfs_try_status trypnfs;
1295 rdata->mds_ops = call_ops;
1296 rdata->lseg = get_lseg(lseg);
1298 dprintk("%s: Reading ino:%lu %u@%llu\n",
1299 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1301 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
1302 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1303 put_lseg(rdata->lseg);
1306 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1308 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1313 pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
1315 struct nfs_read_data *data;
1316 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1317 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1319 desc->pg_lseg = NULL;
1320 while (!list_empty(head)) {
1321 enum pnfs_try_status trypnfs;
1323 data = list_entry(head->next, struct nfs_read_data, list);
1324 list_del_init(&data->list);
1326 trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
1327 if (trypnfs == PNFS_NOT_ATTEMPTED)
1328 pnfs_read_through_mds(desc, data);
1334 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
1339 ret = nfs_generic_pagein(desc, &head);
1341 put_lseg(desc->pg_lseg);
1342 desc->pg_lseg = NULL;
1345 pnfs_do_multiple_reads(desc, &head);
1348 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
1351 * There can be multiple RW segments.
1353 static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
1355 struct pnfs_layout_segment *lseg;
1357 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
1358 if (lseg->pls_range.iomode == IOMODE_RW &&
1359 test_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
1360 list_add(&lseg->pls_lc_list, listp);
1364 void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
1366 if (lseg->pls_range.iomode == IOMODE_RW) {
1367 dprintk("%s Setting layout IOMODE_RW fail bit\n", __func__);
1368 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
1370 dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
1371 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
1374 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
1377 pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1379 struct nfs_inode *nfsi = NFS_I(wdata->inode);
1380 loff_t end_pos = wdata->mds_offset + wdata->res.count;
1381 bool mark_as_dirty = false;
1383 spin_lock(&nfsi->vfs_inode.i_lock);
1384 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1385 mark_as_dirty = true;
1386 dprintk("%s: Set layoutcommit for inode %lu ",
1387 __func__, wdata->inode->i_ino);
1389 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &wdata->lseg->pls_flags)) {
1390 /* references matched in nfs4_layoutcommit_release */
1391 get_lseg(wdata->lseg);
1393 if (end_pos > nfsi->layout->plh_lwb)
1394 nfsi->layout->plh_lwb = end_pos;
1395 spin_unlock(&nfsi->vfs_inode.i_lock);
1396 dprintk("%s: lseg %p end_pos %llu\n",
1397 __func__, wdata->lseg, nfsi->layout->plh_lwb);
1399 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1400 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1402 mark_inode_dirty_sync(wdata->inode);
1404 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1406 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
1408 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
1410 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
1411 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
1415 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1416 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1417 * data to disk to allow the server to recover the data if it crashes.
1418 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1419 * is off, and a COMMIT is sent to a data server, or
1420 * if WRITEs to a data server return NFS_DATA_SYNC.
1423 pnfs_layoutcommit_inode(struct inode *inode, bool sync)
1425 struct nfs4_layoutcommit_data *data;
1426 struct nfs_inode *nfsi = NFS_I(inode);
1430 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1432 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1435 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1436 data = kzalloc(sizeof(*data), GFP_NOFS);
1442 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1445 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
1450 status = wait_on_bit_lock(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING,
1451 nfs_wait_bit_killable, TASK_KILLABLE);
1456 INIT_LIST_HEAD(&data->lseg_list);
1457 spin_lock(&inode->i_lock);
1458 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1459 clear_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags);
1460 spin_unlock(&inode->i_lock);
1461 wake_up_bit(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING);
1465 pnfs_list_write_lseg(inode, &data->lseg_list);
1467 end_pos = nfsi->layout->plh_lwb;
1468 nfsi->layout->plh_lwb = 0;
1470 memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data,
1471 sizeof(nfsi->layout->plh_stateid.data));
1472 spin_unlock(&inode->i_lock);
1474 data->args.inode = inode;
1475 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
1476 nfs_fattr_init(&data->fattr);
1477 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1478 data->res.fattr = &data->fattr;
1479 data->args.lastbytewritten = end_pos - 1;
1480 data->res.server = NFS_SERVER(inode);
1482 status = nfs4_proc_layoutcommit(data, sync);
1485 mark_inode_dirty_sync(inode);
1486 dprintk("<-- %s status %d\n", __func__, status);