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>
35 #define NFSDBG_FACILITY NFSDBG_PNFS
40 * protects pnfs_modules_tbl.
42 static DEFINE_SPINLOCK(pnfs_spinlock);
45 * pnfs_modules_tbl holds all pnfs modules
47 static LIST_HEAD(pnfs_modules_tbl);
49 /* Return the registered pnfs layout driver module matching given id */
50 static struct pnfs_layoutdriver_type *
51 find_pnfs_driver_locked(u32 id)
53 struct pnfs_layoutdriver_type *local;
55 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
60 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
64 static struct pnfs_layoutdriver_type *
65 find_pnfs_driver(u32 id)
67 struct pnfs_layoutdriver_type *local;
69 spin_lock(&pnfs_spinlock);
70 local = find_pnfs_driver_locked(id);
71 spin_unlock(&pnfs_spinlock);
76 unset_pnfs_layoutdriver(struct nfs_server *nfss)
78 if (nfss->pnfs_curr_ld) {
79 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
80 module_put(nfss->pnfs_curr_ld->owner);
82 nfss->pnfs_curr_ld = NULL;
86 * Try to set the server's pnfs module to the pnfs layout type specified by id.
87 * Currently only one pNFS layout driver per filesystem is supported.
89 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
92 set_pnfs_layoutdriver(struct nfs_server *server, u32 id)
94 struct pnfs_layoutdriver_type *ld_type = NULL;
98 if (!(server->nfs_client->cl_exchange_flags &
99 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
100 printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__,
101 id, server->nfs_client->cl_exchange_flags);
104 ld_type = find_pnfs_driver(id);
106 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
107 ld_type = find_pnfs_driver(id);
109 dprintk("%s: No pNFS module found for %u.\n",
114 if (!try_module_get(ld_type->owner)) {
115 dprintk("%s: Could not grab reference on module\n", __func__);
118 server->pnfs_curr_ld = ld_type;
119 if (ld_type->set_layoutdriver(server)) {
121 "%s: Error initializing mount point for layout driver %u.\n",
123 module_put(ld_type->owner);
126 dprintk("%s: pNFS module for %u set\n", __func__, id);
130 dprintk("%s: Using NFSv4 I/O\n", __func__);
131 server->pnfs_curr_ld = NULL;
135 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
137 int status = -EINVAL;
138 struct pnfs_layoutdriver_type *tmp;
140 if (ld_type->id == 0) {
141 printk(KERN_ERR "%s id 0 is reserved\n", __func__);
144 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
145 printk(KERN_ERR "%s Layout driver must provide "
146 "alloc_lseg and free_lseg.\n", __func__);
150 spin_lock(&pnfs_spinlock);
151 tmp = find_pnfs_driver_locked(ld_type->id);
153 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
155 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
158 printk(KERN_ERR "%s Module with id %d already loaded!\n",
159 __func__, ld_type->id);
161 spin_unlock(&pnfs_spinlock);
165 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
168 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
170 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
171 spin_lock(&pnfs_spinlock);
172 list_del(&ld_type->pnfs_tblid);
173 spin_unlock(&pnfs_spinlock);
175 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
178 * pNFS client layout cache
181 /* Need to hold i_lock if caller does not already hold reference */
183 get_layout_hdr(struct pnfs_layout_hdr *lo)
185 atomic_inc(&lo->plh_refcount);
189 destroy_layout_hdr(struct pnfs_layout_hdr *lo)
191 dprintk("%s: freeing layout cache %p\n", __func__, lo);
192 BUG_ON(!list_empty(&lo->plh_layouts));
193 NFS_I(lo->plh_inode)->layout = NULL;
198 put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
200 if (atomic_dec_and_test(&lo->plh_refcount))
201 destroy_layout_hdr(lo);
205 put_layout_hdr(struct pnfs_layout_hdr *lo)
207 struct inode *inode = lo->plh_inode;
209 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
210 destroy_layout_hdr(lo);
211 spin_unlock(&inode->i_lock);
216 init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
218 INIT_LIST_HEAD(&lseg->pls_list);
219 atomic_set(&lseg->pls_refcount, 1);
221 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
222 lseg->pls_layout = lo;
225 static void free_lseg(struct pnfs_layout_segment *lseg)
227 struct inode *ino = lseg->pls_layout->plh_inode;
229 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
230 /* Matched by get_layout_hdr in pnfs_insert_layout */
231 put_layout_hdr(NFS_I(ino)->layout);
235 put_lseg_common(struct pnfs_layout_segment *lseg)
237 struct inode *inode = lseg->pls_layout->plh_inode;
239 BUG_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
240 list_del_init(&lseg->pls_list);
241 if (list_empty(&lseg->pls_layout->plh_segs)) {
242 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
243 /* Matched by initial refcount set in alloc_init_layout_hdr */
244 put_layout_hdr_locked(lseg->pls_layout);
246 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
250 put_lseg(struct pnfs_layout_segment *lseg)
257 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
258 atomic_read(&lseg->pls_refcount),
259 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
260 inode = lseg->pls_layout->plh_inode;
261 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
264 put_lseg_common(lseg);
265 list_add(&lseg->pls_list, &free_me);
266 spin_unlock(&inode->i_lock);
267 pnfs_free_lseg_list(&free_me);
272 should_free_lseg(u32 lseg_iomode, u32 recall_iomode)
274 return (recall_iomode == IOMODE_ANY ||
275 lseg_iomode == recall_iomode);
278 /* Returns 1 if lseg is removed from list, 0 otherwise */
279 static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
280 struct list_head *tmp_list)
284 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
285 /* Remove the reference keeping the lseg in the
286 * list. It will now be removed when all
287 * outstanding io is finished.
289 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
290 atomic_read(&lseg->pls_refcount));
291 if (atomic_dec_and_test(&lseg->pls_refcount)) {
292 put_lseg_common(lseg);
293 list_add(&lseg->pls_list, tmp_list);
300 /* Returns count of number of matching invalid lsegs remaining in list
304 mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
305 struct list_head *tmp_list,
308 struct pnfs_layout_segment *lseg, *next;
309 int invalid = 0, removed = 0;
311 dprintk("%s:Begin lo %p\n", __func__, lo);
313 if (list_empty(&lo->plh_segs)) {
314 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
315 put_layout_hdr_locked(lo);
318 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
319 if (should_free_lseg(lseg->pls_range.iomode, iomode)) {
320 dprintk("%s: freeing lseg %p iomode %d "
321 "offset %llu length %llu\n", __func__,
322 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
323 lseg->pls_range.length);
325 removed += mark_lseg_invalid(lseg, tmp_list);
327 dprintk("%s:Return %i\n", __func__, invalid - removed);
328 return invalid - removed;
331 /* note free_me must contain lsegs from a single layout_hdr */
333 pnfs_free_lseg_list(struct list_head *free_me)
335 struct pnfs_layout_segment *lseg, *tmp;
336 struct pnfs_layout_hdr *lo;
338 if (list_empty(free_me))
341 lo = list_first_entry(free_me, struct pnfs_layout_segment,
342 pls_list)->pls_layout;
344 if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
345 struct nfs_client *clp;
347 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
348 spin_lock(&clp->cl_lock);
349 list_del_init(&lo->plh_layouts);
350 spin_unlock(&clp->cl_lock);
352 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
353 list_del(&lseg->pls_list);
359 pnfs_destroy_layout(struct nfs_inode *nfsi)
361 struct pnfs_layout_hdr *lo;
364 spin_lock(&nfsi->vfs_inode.i_lock);
367 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
368 mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY);
370 spin_unlock(&nfsi->vfs_inode.i_lock);
371 pnfs_free_lseg_list(&tmp_list);
375 * Called by the state manger to remove all layouts established under an
379 pnfs_destroy_all_layouts(struct nfs_client *clp)
381 struct pnfs_layout_hdr *lo;
384 spin_lock(&clp->cl_lock);
385 list_splice_init(&clp->cl_layouts, &tmp_list);
386 spin_unlock(&clp->cl_lock);
388 while (!list_empty(&tmp_list)) {
389 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
391 dprintk("%s freeing layout for inode %lu\n", __func__,
392 lo->plh_inode->i_ino);
393 pnfs_destroy_layout(NFS_I(lo->plh_inode));
397 /* update lo->plh_stateid with new if is more recent */
399 pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
404 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
405 newseq = be32_to_cpu(new->stateid.seqid);
406 if ((int)(newseq - oldseq) > 0) {
407 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
408 if (update_barrier) {
409 u32 new_barrier = be32_to_cpu(new->stateid.seqid);
411 if ((int)(new_barrier - lo->plh_barrier))
412 lo->plh_barrier = new_barrier;
414 /* Because of wraparound, we want to keep the barrier
415 * "close" to the current seqids. It needs to be
416 * within 2**31 to count as "behind", so if it
417 * gets too near that limit, give us a litle leeway
418 * and bring it to within 2**30.
419 * NOTE - and yes, this is all unsigned arithmetic.
421 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
422 lo->plh_barrier = newseq - (1 << 30);
427 /* lget is set to 1 if called from inside send_layoutget call chain */
429 pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
433 (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
435 return lo->plh_block_lgets ||
436 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
437 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
438 (list_empty(&lo->plh_segs) &&
439 (atomic_read(&lo->plh_outstanding) > lget));
443 pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
444 struct nfs4_state *open_state)
448 dprintk("--> %s\n", __func__);
449 spin_lock(&lo->plh_inode->i_lock);
450 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
452 } else if (list_empty(&lo->plh_segs)) {
456 seq = read_seqbegin(&open_state->seqlock);
457 memcpy(dst->data, open_state->stateid.data,
458 sizeof(open_state->stateid.data));
459 } while (read_seqretry(&open_state->seqlock, seq));
461 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
462 spin_unlock(&lo->plh_inode->i_lock);
463 dprintk("<-- %s\n", __func__);
468 * Get layout from server.
469 * for now, assume that whole file layouts are requested.
471 * arg->length: all ones
473 static struct pnfs_layout_segment *
474 send_layoutget(struct pnfs_layout_hdr *lo,
475 struct nfs_open_context *ctx,
478 struct inode *ino = lo->plh_inode;
479 struct nfs_server *server = NFS_SERVER(ino);
480 struct nfs4_layoutget *lgp;
481 struct pnfs_layout_segment *lseg = NULL;
483 dprintk("--> %s\n", __func__);
486 lgp = kzalloc(sizeof(*lgp), GFP_KERNEL);
489 lgp->args.minlength = NFS4_MAX_UINT64;
490 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
491 lgp->args.range.iomode = iomode;
492 lgp->args.range.offset = 0;
493 lgp->args.range.length = NFS4_MAX_UINT64;
494 lgp->args.type = server->pnfs_curr_ld->id;
495 lgp->args.inode = ino;
496 lgp->args.ctx = get_nfs_open_context(ctx);
499 /* Synchronously retrieve layout information from server and
502 nfs4_proc_layoutget(lgp);
504 /* remember that LAYOUTGET failed and suspend trying */
505 set_bit(lo_fail_bit(iomode), &lo->plh_flags);
510 bool pnfs_roc(struct inode *ino)
512 struct pnfs_layout_hdr *lo;
513 struct pnfs_layout_segment *lseg, *tmp;
517 spin_lock(&ino->i_lock);
518 lo = NFS_I(ino)->layout;
519 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
520 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
522 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
523 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
524 mark_lseg_invalid(lseg, &tmp_list);
529 lo->plh_block_lgets++;
530 get_layout_hdr(lo); /* matched in pnfs_roc_release */
531 spin_unlock(&ino->i_lock);
532 pnfs_free_lseg_list(&tmp_list);
536 spin_unlock(&ino->i_lock);
540 void pnfs_roc_release(struct inode *ino)
542 struct pnfs_layout_hdr *lo;
544 spin_lock(&ino->i_lock);
545 lo = NFS_I(ino)->layout;
546 lo->plh_block_lgets--;
547 put_layout_hdr_locked(lo);
548 spin_unlock(&ino->i_lock);
551 void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
553 struct pnfs_layout_hdr *lo;
555 spin_lock(&ino->i_lock);
556 lo = NFS_I(ino)->layout;
557 if ((int)(barrier - lo->plh_barrier) > 0)
558 lo->plh_barrier = barrier;
559 spin_unlock(&ino->i_lock);
562 bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
564 struct nfs_inode *nfsi = NFS_I(ino);
565 struct pnfs_layout_segment *lseg;
568 spin_lock(&ino->i_lock);
569 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
570 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
575 struct pnfs_layout_hdr *lo = nfsi->layout;
576 u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
578 /* Since close does not return a layout stateid for use as
579 * a barrier, we choose the worst-case barrier.
581 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
583 spin_unlock(&ino->i_lock);
588 * Compare two layout segments for sorting into layout cache.
589 * We want to preferentially return RW over RO layouts, so ensure those
593 cmp_layout(u32 iomode1, u32 iomode2)
595 /* read > read/write */
596 return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ);
600 pnfs_insert_layout(struct pnfs_layout_hdr *lo,
601 struct pnfs_layout_segment *lseg)
603 struct pnfs_layout_segment *lp;
606 dprintk("%s:Begin\n", __func__);
608 assert_spin_locked(&lo->plh_inode->i_lock);
609 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
610 if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0)
612 list_add_tail(&lseg->pls_list, &lp->pls_list);
613 dprintk("%s: inserted lseg %p "
614 "iomode %d offset %llu length %llu before "
615 "lp %p iomode %d offset %llu length %llu\n",
616 __func__, lseg, lseg->pls_range.iomode,
617 lseg->pls_range.offset, lseg->pls_range.length,
618 lp, lp->pls_range.iomode, lp->pls_range.offset,
619 lp->pls_range.length);
624 list_add_tail(&lseg->pls_list, &lo->plh_segs);
625 dprintk("%s: inserted lseg %p "
626 "iomode %d offset %llu length %llu at tail\n",
627 __func__, lseg, lseg->pls_range.iomode,
628 lseg->pls_range.offset, lseg->pls_range.length);
632 dprintk("%s:Return\n", __func__);
635 static struct pnfs_layout_hdr *
636 alloc_init_layout_hdr(struct inode *ino)
638 struct pnfs_layout_hdr *lo;
640 lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
643 atomic_set(&lo->plh_refcount, 1);
644 INIT_LIST_HEAD(&lo->plh_layouts);
645 INIT_LIST_HEAD(&lo->plh_segs);
646 INIT_LIST_HEAD(&lo->plh_bulk_recall);
651 static struct pnfs_layout_hdr *
652 pnfs_find_alloc_layout(struct inode *ino)
654 struct nfs_inode *nfsi = NFS_I(ino);
655 struct pnfs_layout_hdr *new = NULL;
657 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
659 assert_spin_locked(&ino->i_lock);
661 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
666 spin_unlock(&ino->i_lock);
667 new = alloc_init_layout_hdr(ino);
668 spin_lock(&ino->i_lock);
670 if (likely(nfsi->layout == NULL)) /* Won the race? */
678 * iomode matching rules:
689 is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode)
691 return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW);
695 * lookup range in layout
697 static struct pnfs_layout_segment *
698 pnfs_find_lseg(struct pnfs_layout_hdr *lo, u32 iomode)
700 struct pnfs_layout_segment *lseg, *ret = NULL;
702 dprintk("%s:Begin\n", __func__);
704 assert_spin_locked(&lo->plh_inode->i_lock);
705 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
706 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
707 is_matching_lseg(lseg, iomode)) {
708 ret = get_lseg(lseg);
711 if (cmp_layout(iomode, lseg->pls_range.iomode) > 0)
715 dprintk("%s:Return lseg %p ref %d\n",
716 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
721 * Layout segment is retreived from the server if not cached.
722 * The appropriate layout segment is referenced and returned to the caller.
724 struct pnfs_layout_segment *
725 pnfs_update_layout(struct inode *ino,
726 struct nfs_open_context *ctx,
727 enum pnfs_iomode iomode)
729 struct nfs_inode *nfsi = NFS_I(ino);
730 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
731 struct pnfs_layout_hdr *lo;
732 struct pnfs_layout_segment *lseg = NULL;
735 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
737 spin_lock(&ino->i_lock);
738 lo = pnfs_find_alloc_layout(ino);
740 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
744 /* Do we even need to bother with this? */
745 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
746 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
747 dprintk("%s matches recall, use MDS\n", __func__);
750 /* Check to see if the layout for the given range already exists */
751 lseg = pnfs_find_lseg(lo, iomode);
755 /* if LAYOUTGET already failed once we don't try again */
756 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
759 if (pnfs_layoutgets_blocked(lo, NULL, 0))
761 atomic_inc(&lo->plh_outstanding);
764 if (list_empty(&lo->plh_segs))
766 spin_unlock(&ino->i_lock);
768 /* The lo must be on the clp list if there is any
769 * chance of a CB_LAYOUTRECALL(FILE) coming in.
771 spin_lock(&clp->cl_lock);
772 BUG_ON(!list_empty(&lo->plh_layouts));
773 list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
774 spin_unlock(&clp->cl_lock);
777 lseg = send_layoutget(lo, ctx, iomode);
778 if (!lseg && first) {
779 spin_lock(&clp->cl_lock);
780 list_del_init(&lo->plh_layouts);
781 spin_unlock(&clp->cl_lock);
783 atomic_dec(&lo->plh_outstanding);
786 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
787 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
790 spin_unlock(&ino->i_lock);
795 pnfs_layout_process(struct nfs4_layoutget *lgp)
797 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
798 struct nfs4_layoutget_res *res = &lgp->res;
799 struct pnfs_layout_segment *lseg;
800 struct inode *ino = lo->plh_inode;
801 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
804 /* Verify we got what we asked for.
805 * Note that because the xdr parsing only accepts a single
806 * element array, this can fail even if the server is behaving
809 if (lgp->args.range.iomode > res->range.iomode ||
810 res->range.offset != 0 ||
811 res->range.length != NFS4_MAX_UINT64) {
815 /* Inject layout blob into I/O device driver */
816 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res);
817 if (!lseg || IS_ERR(lseg)) {
821 status = PTR_ERR(lseg);
822 dprintk("%s: Could not allocate layout: error %d\n",
827 spin_lock(&ino->i_lock);
828 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
829 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
830 dprintk("%s forget reply due to recall\n", __func__);
831 goto out_forget_reply;
834 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
835 dprintk("%s forget reply due to state\n", __func__);
836 goto out_forget_reply;
839 lseg->pls_range = res->range;
840 *lgp->lsegpp = get_lseg(lseg);
841 pnfs_insert_layout(lo, lseg);
843 if (res->return_on_close) {
844 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
845 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
848 /* Done processing layoutget. Set the layout stateid */
849 pnfs_set_layout_stateid(lo, &res->stateid, false);
850 spin_unlock(&ino->i_lock);
855 spin_unlock(&ino->i_lock);
856 lseg->pls_layout = lo;
857 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
861 static int pnfs_read_pg_test(struct nfs_pageio_descriptor *pgio,
862 struct nfs_page *prev,
863 struct nfs_page *req)
865 if (pgio->pg_count == prev->wb_bytes) {
866 /* This is first coelesce call for a series of nfs_pages */
867 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
871 return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req);
875 pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode)
877 struct pnfs_layoutdriver_type *ld;
879 ld = NFS_SERVER(inode)->pnfs_curr_ld;
880 pgio->pg_test = (ld && ld->pg_test) ? pnfs_read_pg_test : NULL;
884 * Call the appropriate parallel I/O subsystem read function.
887 pnfs_try_to_read_data(struct nfs_read_data *rdata,
888 const struct rpc_call_ops *call_ops)
890 struct inode *inode = rdata->inode;
891 struct nfs_server *nfss = NFS_SERVER(inode);
892 enum pnfs_try_status trypnfs;
894 rdata->mds_ops = call_ops;
896 dprintk("%s: Reading ino:%lu %u@%llu\n",
897 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
899 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
900 if (trypnfs == PNFS_NOT_ATTEMPTED) {
901 put_lseg(rdata->lseg);
904 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
906 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
911 * Device ID cache. Currently supports one layout type per struct nfs_client.
912 * Add layout type to the lookup key to expand to support multiple types.
915 pnfs_alloc_init_deviceid_cache(struct nfs_client *clp,
916 void (*free_callback)(struct pnfs_deviceid_node *))
918 struct pnfs_deviceid_cache *c;
920 c = kzalloc(sizeof(struct pnfs_deviceid_cache), GFP_KERNEL);
923 spin_lock(&clp->cl_lock);
924 if (clp->cl_devid_cache != NULL) {
925 atomic_inc(&clp->cl_devid_cache->dc_ref);
926 dprintk("%s [kref [%d]]\n", __func__,
927 atomic_read(&clp->cl_devid_cache->dc_ref));
930 /* kzalloc initializes hlists */
931 spin_lock_init(&c->dc_lock);
932 atomic_set(&c->dc_ref, 1);
933 c->dc_free_callback = free_callback;
934 clp->cl_devid_cache = c;
935 dprintk("%s [new]\n", __func__);
937 spin_unlock(&clp->cl_lock);
940 EXPORT_SYMBOL_GPL(pnfs_alloc_init_deviceid_cache);
943 * Called from pnfs_layoutdriver_type->free_lseg
944 * last layout segment reference frees deviceid
947 pnfs_put_deviceid(struct pnfs_deviceid_cache *c,
948 struct pnfs_deviceid_node *devid)
950 struct nfs4_deviceid *id = &devid->de_id;
951 struct pnfs_deviceid_node *d;
952 struct hlist_node *n;
953 long h = nfs4_deviceid_hash(id);
955 dprintk("%s [%d]\n", __func__, atomic_read(&devid->de_ref));
956 if (!atomic_dec_and_lock(&devid->de_ref, &c->dc_lock))
959 hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[h], de_node)
960 if (!memcmp(&d->de_id, id, sizeof(*id))) {
961 hlist_del_rcu(&d->de_node);
962 spin_unlock(&c->dc_lock);
964 c->dc_free_callback(devid);
967 spin_unlock(&c->dc_lock);
968 /* Why wasn't it found in the list? */
971 EXPORT_SYMBOL_GPL(pnfs_put_deviceid);
973 /* Find and reference a deviceid */
974 struct pnfs_deviceid_node *
975 pnfs_find_get_deviceid(struct pnfs_deviceid_cache *c, struct nfs4_deviceid *id)
977 struct pnfs_deviceid_node *d;
978 struct hlist_node *n;
979 long hash = nfs4_deviceid_hash(id);
981 dprintk("--> %s hash %ld\n", __func__, hash);
983 hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[hash], de_node) {
984 if (!memcmp(&d->de_id, id, sizeof(*id))) {
985 if (!atomic_inc_not_zero(&d->de_ref)) {
997 EXPORT_SYMBOL_GPL(pnfs_find_get_deviceid);
1000 * Add a deviceid to the cache.
1001 * GETDEVICEINFOs for same deviceid can race. If deviceid is found, discard new
1003 struct pnfs_deviceid_node *
1004 pnfs_add_deviceid(struct pnfs_deviceid_cache *c, struct pnfs_deviceid_node *new)
1006 struct pnfs_deviceid_node *d;
1007 long hash = nfs4_deviceid_hash(&new->de_id);
1009 dprintk("--> %s hash %ld\n", __func__, hash);
1010 spin_lock(&c->dc_lock);
1011 d = pnfs_find_get_deviceid(c, &new->de_id);
1013 spin_unlock(&c->dc_lock);
1014 dprintk("%s [discard]\n", __func__);
1015 c->dc_free_callback(new);
1018 INIT_HLIST_NODE(&new->de_node);
1019 atomic_set(&new->de_ref, 1);
1020 hlist_add_head_rcu(&new->de_node, &c->dc_deviceids[hash]);
1021 spin_unlock(&c->dc_lock);
1022 dprintk("%s [new]\n", __func__);
1025 EXPORT_SYMBOL_GPL(pnfs_add_deviceid);
1028 pnfs_put_deviceid_cache(struct nfs_client *clp)
1030 struct pnfs_deviceid_cache *local = clp->cl_devid_cache;
1032 dprintk("--> %s ({%d})\n", __func__, atomic_read(&local->dc_ref));
1033 if (atomic_dec_and_lock(&local->dc_ref, &clp->cl_lock)) {
1035 /* Verify cache is empty */
1036 for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i++)
1037 BUG_ON(!hlist_empty(&local->dc_deviceids[i]));
1038 clp->cl_devid_cache = NULL;
1039 spin_unlock(&clp->cl_lock);
1043 EXPORT_SYMBOL_GPL(pnfs_put_deviceid_cache);