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 module_put(nfss
->pnfs_curr_ld
->owner
);
80 nfss
->pnfs_curr_ld
= NULL
;
84 * Try to set the server's pnfs module to the pnfs layout type specified by id.
85 * Currently only one pNFS layout driver per filesystem is supported.
87 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
90 set_pnfs_layoutdriver(struct nfs_server
*server
, u32 id
)
92 struct pnfs_layoutdriver_type
*ld_type
= NULL
;
96 if (!(server
->nfs_client
->cl_exchange_flags
&
97 (EXCHGID4_FLAG_USE_NON_PNFS
| EXCHGID4_FLAG_USE_PNFS_MDS
))) {
98 printk(KERN_ERR
"%s: id %u cl_exchange_flags 0x%x\n", __func__
,
99 id
, server
->nfs_client
->cl_exchange_flags
);
102 ld_type
= find_pnfs_driver(id
);
104 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX
, id
);
105 ld_type
= find_pnfs_driver(id
);
107 dprintk("%s: No pNFS module found for %u.\n",
112 if (!try_module_get(ld_type
->owner
)) {
113 dprintk("%s: Could not grab reference on module\n", __func__
);
116 server
->pnfs_curr_ld
= ld_type
;
118 dprintk("%s: pNFS module for %u set\n", __func__
, id
);
122 dprintk("%s: Using NFSv4 I/O\n", __func__
);
123 server
->pnfs_curr_ld
= NULL
;
127 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type
*ld_type
)
129 int status
= -EINVAL
;
130 struct pnfs_layoutdriver_type
*tmp
;
132 if (ld_type
->id
== 0) {
133 printk(KERN_ERR
"%s id 0 is reserved\n", __func__
);
136 if (!ld_type
->alloc_lseg
|| !ld_type
->free_lseg
) {
137 printk(KERN_ERR
"%s Layout driver must provide "
138 "alloc_lseg and free_lseg.\n", __func__
);
142 spin_lock(&pnfs_spinlock
);
143 tmp
= find_pnfs_driver_locked(ld_type
->id
);
145 list_add(&ld_type
->pnfs_tblid
, &pnfs_modules_tbl
);
147 dprintk("%s Registering id:%u name:%s\n", __func__
, ld_type
->id
,
150 printk(KERN_ERR
"%s Module with id %d already loaded!\n",
151 __func__
, ld_type
->id
);
153 spin_unlock(&pnfs_spinlock
);
157 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver
);
160 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type
*ld_type
)
162 dprintk("%s Deregistering id:%u\n", __func__
, ld_type
->id
);
163 spin_lock(&pnfs_spinlock
);
164 list_del(&ld_type
->pnfs_tblid
);
165 spin_unlock(&pnfs_spinlock
);
167 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver
);
170 * pNFS client layout cache
173 /* Need to hold i_lock if caller does not already hold reference */
175 get_layout_hdr(struct pnfs_layout_hdr
*lo
)
177 atomic_inc(&lo
->plh_refcount
);
180 static struct pnfs_layout_hdr
*
181 pnfs_alloc_layout_hdr(struct inode
*ino
, gfp_t gfp_flags
)
183 struct pnfs_layoutdriver_type
*ld
= NFS_SERVER(ino
)->pnfs_curr_ld
;
184 return ld
->alloc_layout_hdr
? ld
->alloc_layout_hdr(ino
, gfp_flags
) :
185 kzalloc(sizeof(struct pnfs_layout_hdr
), gfp_flags
);
189 pnfs_free_layout_hdr(struct pnfs_layout_hdr
*lo
)
191 struct pnfs_layoutdriver_type
*ld
= NFS_SERVER(lo
->plh_inode
)->pnfs_curr_ld
;
192 return ld
->alloc_layout_hdr
? ld
->free_layout_hdr(lo
) : kfree(lo
);
196 destroy_layout_hdr(struct pnfs_layout_hdr
*lo
)
198 dprintk("%s: freeing layout cache %p\n", __func__
, lo
);
199 BUG_ON(!list_empty(&lo
->plh_layouts
));
200 NFS_I(lo
->plh_inode
)->layout
= NULL
;
201 pnfs_free_layout_hdr(lo
);
205 put_layout_hdr_locked(struct pnfs_layout_hdr
*lo
)
207 if (atomic_dec_and_test(&lo
->plh_refcount
))
208 destroy_layout_hdr(lo
);
212 put_layout_hdr(struct pnfs_layout_hdr
*lo
)
214 struct inode
*inode
= lo
->plh_inode
;
216 if (atomic_dec_and_lock(&lo
->plh_refcount
, &inode
->i_lock
)) {
217 destroy_layout_hdr(lo
);
218 spin_unlock(&inode
->i_lock
);
223 init_lseg(struct pnfs_layout_hdr
*lo
, struct pnfs_layout_segment
*lseg
)
225 INIT_LIST_HEAD(&lseg
->pls_list
);
226 atomic_set(&lseg
->pls_refcount
, 1);
228 set_bit(NFS_LSEG_VALID
, &lseg
->pls_flags
);
229 lseg
->pls_layout
= lo
;
232 static void free_lseg(struct pnfs_layout_segment
*lseg
)
234 struct inode
*ino
= lseg
->pls_layout
->plh_inode
;
236 NFS_SERVER(ino
)->pnfs_curr_ld
->free_lseg(lseg
);
237 /* Matched by get_layout_hdr in pnfs_insert_layout */
238 put_layout_hdr(NFS_I(ino
)->layout
);
242 put_lseg_common(struct pnfs_layout_segment
*lseg
)
244 struct inode
*inode
= lseg
->pls_layout
->plh_inode
;
246 WARN_ON(test_bit(NFS_LSEG_VALID
, &lseg
->pls_flags
));
247 list_del_init(&lseg
->pls_list
);
248 if (list_empty(&lseg
->pls_layout
->plh_segs
)) {
249 set_bit(NFS_LAYOUT_DESTROYED
, &lseg
->pls_layout
->plh_flags
);
250 /* Matched by initial refcount set in alloc_init_layout_hdr */
251 put_layout_hdr_locked(lseg
->pls_layout
);
253 rpc_wake_up(&NFS_SERVER(inode
)->roc_rpcwaitq
);
257 put_lseg(struct pnfs_layout_segment
*lseg
)
264 dprintk("%s: lseg %p ref %d valid %d\n", __func__
, lseg
,
265 atomic_read(&lseg
->pls_refcount
),
266 test_bit(NFS_LSEG_VALID
, &lseg
->pls_flags
));
267 inode
= lseg
->pls_layout
->plh_inode
;
268 if (atomic_dec_and_lock(&lseg
->pls_refcount
, &inode
->i_lock
)) {
271 put_lseg_common(lseg
);
272 list_add(&lseg
->pls_list
, &free_me
);
273 spin_unlock(&inode
->i_lock
);
274 pnfs_free_lseg_list(&free_me
);
277 EXPORT_SYMBOL_GPL(put_lseg
);
280 end_offset(u64 start
, u64 len
)
285 return end
>= start
? end
: NFS4_MAX_UINT64
;
288 /* last octet in a range */
290 last_byte_offset(u64 start
, u64 len
)
296 return end
> start
? end
- 1 : NFS4_MAX_UINT64
;
300 * is l2 fully contained in l1?
302 * [----------------------------------)
307 lo_seg_contained(struct pnfs_layout_range
*l1
,
308 struct pnfs_layout_range
*l2
)
310 u64 start1
= l1
->offset
;
311 u64 end1
= end_offset(start1
, l1
->length
);
312 u64 start2
= l2
->offset
;
313 u64 end2
= end_offset(start2
, l2
->length
);
315 return (start1
<= start2
) && (end1
>= end2
);
319 * is l1 and l2 intersecting?
321 * [----------------------------------)
326 lo_seg_intersecting(struct pnfs_layout_range
*l1
,
327 struct pnfs_layout_range
*l2
)
329 u64 start1
= l1
->offset
;
330 u64 end1
= end_offset(start1
, l1
->length
);
331 u64 start2
= l2
->offset
;
332 u64 end2
= end_offset(start2
, l2
->length
);
334 return (end1
== NFS4_MAX_UINT64
|| end1
> start2
) &&
335 (end2
== NFS4_MAX_UINT64
|| end2
> start1
);
339 should_free_lseg(struct pnfs_layout_range
*lseg_range
,
340 struct pnfs_layout_range
*recall_range
)
342 return (recall_range
->iomode
== IOMODE_ANY
||
343 lseg_range
->iomode
== recall_range
->iomode
) &&
344 lo_seg_intersecting(lseg_range
, recall_range
);
347 /* Returns 1 if lseg is removed from list, 0 otherwise */
348 static int mark_lseg_invalid(struct pnfs_layout_segment
*lseg
,
349 struct list_head
*tmp_list
)
353 if (test_and_clear_bit(NFS_LSEG_VALID
, &lseg
->pls_flags
)) {
354 /* Remove the reference keeping the lseg in the
355 * list. It will now be removed when all
356 * outstanding io is finished.
358 dprintk("%s: lseg %p ref %d\n", __func__
, lseg
,
359 atomic_read(&lseg
->pls_refcount
));
360 if (atomic_dec_and_test(&lseg
->pls_refcount
)) {
361 put_lseg_common(lseg
);
362 list_add(&lseg
->pls_list
, tmp_list
);
369 /* Returns count of number of matching invalid lsegs remaining in list
373 mark_matching_lsegs_invalid(struct pnfs_layout_hdr
*lo
,
374 struct list_head
*tmp_list
,
375 struct pnfs_layout_range
*recall_range
)
377 struct pnfs_layout_segment
*lseg
, *next
;
378 int invalid
= 0, removed
= 0;
380 dprintk("%s:Begin lo %p\n", __func__
, lo
);
382 if (list_empty(&lo
->plh_segs
)) {
383 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED
, &lo
->plh_flags
))
384 put_layout_hdr_locked(lo
);
387 list_for_each_entry_safe(lseg
, next
, &lo
->plh_segs
, pls_list
)
389 should_free_lseg(&lseg
->pls_range
, recall_range
)) {
390 dprintk("%s: freeing lseg %p iomode %d "
391 "offset %llu length %llu\n", __func__
,
392 lseg
, lseg
->pls_range
.iomode
, lseg
->pls_range
.offset
,
393 lseg
->pls_range
.length
);
395 removed
+= mark_lseg_invalid(lseg
, tmp_list
);
397 dprintk("%s:Return %i\n", __func__
, invalid
- removed
);
398 return invalid
- removed
;
401 /* note free_me must contain lsegs from a single layout_hdr */
403 pnfs_free_lseg_list(struct list_head
*free_me
)
405 struct pnfs_layout_segment
*lseg
, *tmp
;
406 struct pnfs_layout_hdr
*lo
;
408 if (list_empty(free_me
))
411 lo
= list_first_entry(free_me
, struct pnfs_layout_segment
,
412 pls_list
)->pls_layout
;
414 if (test_bit(NFS_LAYOUT_DESTROYED
, &lo
->plh_flags
)) {
415 struct nfs_client
*clp
;
417 clp
= NFS_SERVER(lo
->plh_inode
)->nfs_client
;
418 spin_lock(&clp
->cl_lock
);
419 list_del_init(&lo
->plh_layouts
);
420 spin_unlock(&clp
->cl_lock
);
422 list_for_each_entry_safe(lseg
, tmp
, free_me
, pls_list
) {
423 list_del(&lseg
->pls_list
);
429 pnfs_destroy_layout(struct nfs_inode
*nfsi
)
431 struct pnfs_layout_hdr
*lo
;
434 spin_lock(&nfsi
->vfs_inode
.i_lock
);
437 lo
->plh_block_lgets
++; /* permanently block new LAYOUTGETs */
438 mark_matching_lsegs_invalid(lo
, &tmp_list
, NULL
);
440 spin_unlock(&nfsi
->vfs_inode
.i_lock
);
441 pnfs_free_lseg_list(&tmp_list
);
445 * Called by the state manger to remove all layouts established under an
449 pnfs_destroy_all_layouts(struct nfs_client
*clp
)
451 struct pnfs_layout_hdr
*lo
;
454 spin_lock(&clp
->cl_lock
);
455 list_splice_init(&clp
->cl_layouts
, &tmp_list
);
456 spin_unlock(&clp
->cl_lock
);
458 while (!list_empty(&tmp_list
)) {
459 lo
= list_entry(tmp_list
.next
, struct pnfs_layout_hdr
,
461 dprintk("%s freeing layout for inode %lu\n", __func__
,
462 lo
->plh_inode
->i_ino
);
463 list_del_init(&lo
->plh_layouts
);
464 pnfs_destroy_layout(NFS_I(lo
->plh_inode
));
468 /* update lo->plh_stateid with new if is more recent */
470 pnfs_set_layout_stateid(struct pnfs_layout_hdr
*lo
, const nfs4_stateid
*new,
475 oldseq
= be32_to_cpu(lo
->plh_stateid
.stateid
.seqid
);
476 newseq
= be32_to_cpu(new->stateid
.seqid
);
477 if ((int)(newseq
- oldseq
) > 0) {
478 memcpy(&lo
->plh_stateid
, &new->stateid
, sizeof(new->stateid
));
479 if (update_barrier
) {
480 u32 new_barrier
= be32_to_cpu(new->stateid
.seqid
);
482 if ((int)(new_barrier
- lo
->plh_barrier
))
483 lo
->plh_barrier
= new_barrier
;
485 /* Because of wraparound, we want to keep the barrier
486 * "close" to the current seqids. It needs to be
487 * within 2**31 to count as "behind", so if it
488 * gets too near that limit, give us a litle leeway
489 * and bring it to within 2**30.
490 * NOTE - and yes, this is all unsigned arithmetic.
492 if (unlikely((newseq
- lo
->plh_barrier
) > (3 << 29)))
493 lo
->plh_barrier
= newseq
- (1 << 30);
498 /* lget is set to 1 if called from inside send_layoutget call chain */
500 pnfs_layoutgets_blocked(struct pnfs_layout_hdr
*lo
, nfs4_stateid
*stateid
,
504 (int)(lo
->plh_barrier
- be32_to_cpu(stateid
->stateid
.seqid
)) >= 0)
506 return lo
->plh_block_lgets
||
507 test_bit(NFS_LAYOUT_DESTROYED
, &lo
->plh_flags
) ||
508 test_bit(NFS_LAYOUT_BULK_RECALL
, &lo
->plh_flags
) ||
509 (list_empty(&lo
->plh_segs
) &&
510 (atomic_read(&lo
->plh_outstanding
) > lget
));
514 pnfs_choose_layoutget_stateid(nfs4_stateid
*dst
, struct pnfs_layout_hdr
*lo
,
515 struct nfs4_state
*open_state
)
519 dprintk("--> %s\n", __func__
);
520 spin_lock(&lo
->plh_inode
->i_lock
);
521 if (pnfs_layoutgets_blocked(lo
, NULL
, 1)) {
523 } else if (list_empty(&lo
->plh_segs
)) {
527 seq
= read_seqbegin(&open_state
->seqlock
);
528 memcpy(dst
->data
, open_state
->stateid
.data
,
529 sizeof(open_state
->stateid
.data
));
530 } while (read_seqretry(&open_state
->seqlock
, seq
));
532 memcpy(dst
->data
, lo
->plh_stateid
.data
, sizeof(lo
->plh_stateid
.data
));
533 spin_unlock(&lo
->plh_inode
->i_lock
);
534 dprintk("<-- %s\n", __func__
);
539 * Get layout from server.
540 * for now, assume that whole file layouts are requested.
542 * arg->length: all ones
544 static struct pnfs_layout_segment
*
545 send_layoutget(struct pnfs_layout_hdr
*lo
,
546 struct nfs_open_context
*ctx
,
547 struct pnfs_layout_range
*range
,
550 struct inode
*ino
= lo
->plh_inode
;
551 struct nfs_server
*server
= NFS_SERVER(ino
);
552 struct nfs4_layoutget
*lgp
;
553 struct pnfs_layout_segment
*lseg
= NULL
;
554 struct page
**pages
= NULL
;
556 u32 max_resp_sz
, max_pages
;
558 dprintk("--> %s\n", __func__
);
561 lgp
= kzalloc(sizeof(*lgp
), gfp_flags
);
565 /* allocate pages for xdr post processing */
566 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
567 max_pages
= max_resp_sz
>> PAGE_SHIFT
;
569 pages
= kzalloc(max_pages
* sizeof(struct page
*), gfp_flags
);
573 for (i
= 0; i
< max_pages
; i
++) {
574 pages
[i
] = alloc_page(gfp_flags
);
579 lgp
->args
.minlength
= PAGE_CACHE_SIZE
;
580 if (lgp
->args
.minlength
> range
->length
)
581 lgp
->args
.minlength
= range
->length
;
582 lgp
->args
.maxcount
= PNFS_LAYOUT_MAXSIZE
;
583 lgp
->args
.range
= *range
;
584 lgp
->args
.type
= server
->pnfs_curr_ld
->id
;
585 lgp
->args
.inode
= ino
;
586 lgp
->args
.ctx
= get_nfs_open_context(ctx
);
587 lgp
->args
.layout
.pages
= pages
;
588 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
590 lgp
->gfp_flags
= gfp_flags
;
592 /* Synchronously retrieve layout information from server and
595 nfs4_proc_layoutget(lgp
);
597 /* remember that LAYOUTGET failed and suspend trying */
598 set_bit(lo_fail_bit(range
->iomode
), &lo
->plh_flags
);
602 for (i
= 0; i
< max_pages
; i
++)
603 __free_page(pages
[i
]);
609 /* free any allocated xdr pages, lgp as it's not used */
611 for (i
= 0; i
< max_pages
; i
++) {
614 __free_page(pages
[i
]);
622 /* Initiates a LAYOUTRETURN(FILE) */
624 _pnfs_return_layout(struct inode
*ino
)
626 struct pnfs_layout_hdr
*lo
= NULL
;
627 struct nfs_inode
*nfsi
= NFS_I(ino
);
629 struct nfs4_layoutreturn
*lrp
;
630 nfs4_stateid stateid
;
633 dprintk("--> %s\n", __func__
);
635 spin_lock(&ino
->i_lock
);
637 if (!lo
|| !mark_matching_lsegs_invalid(lo
, &tmp_list
, NULL
)) {
638 spin_unlock(&ino
->i_lock
);
639 dprintk("%s: no layout segments to return\n", __func__
);
642 stateid
= nfsi
->layout
->plh_stateid
;
643 /* Reference matched in nfs4_layoutreturn_release */
645 spin_unlock(&ino
->i_lock
);
646 pnfs_free_lseg_list(&tmp_list
);
648 WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT
, &nfsi
->flags
));
650 lrp
= kzalloc(sizeof(*lrp
), GFP_KERNEL
);
651 if (unlikely(lrp
== NULL
)) {
656 lrp
->args
.stateid
= stateid
;
657 lrp
->args
.layout_type
= NFS_SERVER(ino
)->pnfs_curr_ld
->id
;
658 lrp
->args
.inode
= ino
;
659 lrp
->clp
= NFS_SERVER(ino
)->nfs_client
;
661 status
= nfs4_proc_layoutreturn(lrp
);
663 dprintk("<-- %s status: %d\n", __func__
, status
);
667 bool pnfs_roc(struct inode
*ino
)
669 struct pnfs_layout_hdr
*lo
;
670 struct pnfs_layout_segment
*lseg
, *tmp
;
674 spin_lock(&ino
->i_lock
);
675 lo
= NFS_I(ino
)->layout
;
676 if (!lo
|| !test_and_clear_bit(NFS_LAYOUT_ROC
, &lo
->plh_flags
) ||
677 test_bit(NFS_LAYOUT_BULK_RECALL
, &lo
->plh_flags
))
679 list_for_each_entry_safe(lseg
, tmp
, &lo
->plh_segs
, pls_list
)
680 if (test_bit(NFS_LSEG_ROC
, &lseg
->pls_flags
)) {
681 mark_lseg_invalid(lseg
, &tmp_list
);
686 lo
->plh_block_lgets
++;
687 get_layout_hdr(lo
); /* matched in pnfs_roc_release */
688 spin_unlock(&ino
->i_lock
);
689 pnfs_free_lseg_list(&tmp_list
);
693 spin_unlock(&ino
->i_lock
);
697 void pnfs_roc_release(struct inode
*ino
)
699 struct pnfs_layout_hdr
*lo
;
701 spin_lock(&ino
->i_lock
);
702 lo
= NFS_I(ino
)->layout
;
703 lo
->plh_block_lgets
--;
704 put_layout_hdr_locked(lo
);
705 spin_unlock(&ino
->i_lock
);
708 void pnfs_roc_set_barrier(struct inode
*ino
, u32 barrier
)
710 struct pnfs_layout_hdr
*lo
;
712 spin_lock(&ino
->i_lock
);
713 lo
= NFS_I(ino
)->layout
;
714 if ((int)(barrier
- lo
->plh_barrier
) > 0)
715 lo
->plh_barrier
= barrier
;
716 spin_unlock(&ino
->i_lock
);
719 bool pnfs_roc_drain(struct inode
*ino
, u32
*barrier
)
721 struct nfs_inode
*nfsi
= NFS_I(ino
);
722 struct pnfs_layout_segment
*lseg
;
725 spin_lock(&ino
->i_lock
);
726 list_for_each_entry(lseg
, &nfsi
->layout
->plh_segs
, pls_list
)
727 if (test_bit(NFS_LSEG_ROC
, &lseg
->pls_flags
)) {
732 struct pnfs_layout_hdr
*lo
= nfsi
->layout
;
733 u32 current_seqid
= be32_to_cpu(lo
->plh_stateid
.stateid
.seqid
);
735 /* Since close does not return a layout stateid for use as
736 * a barrier, we choose the worst-case barrier.
738 *barrier
= current_seqid
+ atomic_read(&lo
->plh_outstanding
);
740 spin_unlock(&ino
->i_lock
);
745 * Compare two layout segments for sorting into layout cache.
746 * We want to preferentially return RW over RO layouts, so ensure those
750 cmp_layout(struct pnfs_layout_range
*l1
,
751 struct pnfs_layout_range
*l2
)
755 /* high offset > low offset */
756 d
= l1
->offset
- l2
->offset
;
760 /* short length > long length */
761 d
= l2
->length
- l1
->length
;
765 /* read > read/write */
766 return (int)(l1
->iomode
== IOMODE_READ
) - (int)(l2
->iomode
== IOMODE_READ
);
770 pnfs_insert_layout(struct pnfs_layout_hdr
*lo
,
771 struct pnfs_layout_segment
*lseg
)
773 struct pnfs_layout_segment
*lp
;
775 dprintk("%s:Begin\n", __func__
);
777 assert_spin_locked(&lo
->plh_inode
->i_lock
);
778 list_for_each_entry(lp
, &lo
->plh_segs
, pls_list
) {
779 if (cmp_layout(&lseg
->pls_range
, &lp
->pls_range
) > 0)
781 list_add_tail(&lseg
->pls_list
, &lp
->pls_list
);
782 dprintk("%s: inserted lseg %p "
783 "iomode %d offset %llu length %llu before "
784 "lp %p iomode %d offset %llu length %llu\n",
785 __func__
, lseg
, lseg
->pls_range
.iomode
,
786 lseg
->pls_range
.offset
, lseg
->pls_range
.length
,
787 lp
, lp
->pls_range
.iomode
, lp
->pls_range
.offset
,
788 lp
->pls_range
.length
);
791 list_add_tail(&lseg
->pls_list
, &lo
->plh_segs
);
792 dprintk("%s: inserted lseg %p "
793 "iomode %d offset %llu length %llu at tail\n",
794 __func__
, lseg
, lseg
->pls_range
.iomode
,
795 lseg
->pls_range
.offset
, lseg
->pls_range
.length
);
799 dprintk("%s:Return\n", __func__
);
802 static struct pnfs_layout_hdr
*
803 alloc_init_layout_hdr(struct inode
*ino
, gfp_t gfp_flags
)
805 struct pnfs_layout_hdr
*lo
;
807 lo
= pnfs_alloc_layout_hdr(ino
, gfp_flags
);
810 atomic_set(&lo
->plh_refcount
, 1);
811 INIT_LIST_HEAD(&lo
->plh_layouts
);
812 INIT_LIST_HEAD(&lo
->plh_segs
);
813 INIT_LIST_HEAD(&lo
->plh_bulk_recall
);
818 static struct pnfs_layout_hdr
*
819 pnfs_find_alloc_layout(struct inode
*ino
, gfp_t gfp_flags
)
821 struct nfs_inode
*nfsi
= NFS_I(ino
);
822 struct pnfs_layout_hdr
*new = NULL
;
824 dprintk("%s Begin ino=%p layout=%p\n", __func__
, ino
, nfsi
->layout
);
826 assert_spin_locked(&ino
->i_lock
);
828 if (test_bit(NFS_LAYOUT_DESTROYED
, &nfsi
->layout
->plh_flags
))
833 spin_unlock(&ino
->i_lock
);
834 new = alloc_init_layout_hdr(ino
, gfp_flags
);
835 spin_lock(&ino
->i_lock
);
837 if (likely(nfsi
->layout
== NULL
)) /* Won the race? */
840 pnfs_free_layout_hdr(new);
845 * iomode matching rules:
856 is_matching_lseg(struct pnfs_layout_range
*ls_range
,
857 struct pnfs_layout_range
*range
)
859 struct pnfs_layout_range range1
;
861 if ((range
->iomode
== IOMODE_RW
&&
862 ls_range
->iomode
!= IOMODE_RW
) ||
863 !lo_seg_intersecting(ls_range
, range
))
866 /* range1 covers only the first byte in the range */
869 return lo_seg_contained(ls_range
, &range1
);
873 * lookup range in layout
875 static struct pnfs_layout_segment
*
876 pnfs_find_lseg(struct pnfs_layout_hdr
*lo
,
877 struct pnfs_layout_range
*range
)
879 struct pnfs_layout_segment
*lseg
, *ret
= NULL
;
881 dprintk("%s:Begin\n", __func__
);
883 assert_spin_locked(&lo
->plh_inode
->i_lock
);
884 list_for_each_entry(lseg
, &lo
->plh_segs
, pls_list
) {
885 if (test_bit(NFS_LSEG_VALID
, &lseg
->pls_flags
) &&
886 is_matching_lseg(&lseg
->pls_range
, range
)) {
887 ret
= get_lseg(lseg
);
890 if (cmp_layout(range
, &lseg
->pls_range
) > 0)
894 dprintk("%s:Return lseg %p ref %d\n",
895 __func__
, ret
, ret
? atomic_read(&ret
->pls_refcount
) : 0);
900 * Layout segment is retreived from the server if not cached.
901 * The appropriate layout segment is referenced and returned to the caller.
903 struct pnfs_layout_segment
*
904 pnfs_update_layout(struct inode
*ino
,
905 struct nfs_open_context
*ctx
,
908 enum pnfs_iomode iomode
,
911 struct pnfs_layout_range arg
= {
917 struct nfs_inode
*nfsi
= NFS_I(ino
);
918 struct nfs_client
*clp
= NFS_SERVER(ino
)->nfs_client
;
919 struct pnfs_layout_hdr
*lo
;
920 struct pnfs_layout_segment
*lseg
= NULL
;
923 if (!pnfs_enabled_sb(NFS_SERVER(ino
)))
925 spin_lock(&ino
->i_lock
);
926 lo
= pnfs_find_alloc_layout(ino
, gfp_flags
);
928 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__
);
932 /* Do we even need to bother with this? */
933 if (test_bit(NFS4CLNT_LAYOUTRECALL
, &clp
->cl_state
) ||
934 test_bit(NFS_LAYOUT_BULK_RECALL
, &lo
->plh_flags
)) {
935 dprintk("%s matches recall, use MDS\n", __func__
);
939 /* if LAYOUTGET already failed once we don't try again */
940 if (test_bit(lo_fail_bit(iomode
), &nfsi
->layout
->plh_flags
))
943 /* Check to see if the layout for the given range already exists */
944 lseg
= pnfs_find_lseg(lo
, &arg
);
948 if (pnfs_layoutgets_blocked(lo
, NULL
, 0))
950 atomic_inc(&lo
->plh_outstanding
);
953 if (list_empty(&lo
->plh_segs
))
955 spin_unlock(&ino
->i_lock
);
957 /* The lo must be on the clp list if there is any
958 * chance of a CB_LAYOUTRECALL(FILE) coming in.
960 spin_lock(&clp
->cl_lock
);
961 BUG_ON(!list_empty(&lo
->plh_layouts
));
962 list_add_tail(&lo
->plh_layouts
, &clp
->cl_layouts
);
963 spin_unlock(&clp
->cl_lock
);
966 pg_offset
= arg
.offset
& ~PAGE_CACHE_MASK
;
968 arg
.offset
-= pg_offset
;
969 arg
.length
+= pg_offset
;
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
);
991 pnfs_layout_process(struct nfs4_layoutget
*lgp
)
993 struct pnfs_layout_hdr
*lo
= NFS_I(lgp
->args
.inode
)->layout
;
994 struct nfs4_layoutget_res
*res
= &lgp
->res
;
995 struct pnfs_layout_segment
*lseg
;
996 struct inode
*ino
= lo
->plh_inode
;
997 struct nfs_client
*clp
= NFS_SERVER(ino
)->nfs_client
;
1000 /* Inject layout blob into I/O device driver */
1001 lseg
= NFS_SERVER(ino
)->pnfs_curr_ld
->alloc_lseg(lo
, res
, lgp
->gfp_flags
);
1002 if (!lseg
|| IS_ERR(lseg
)) {
1006 status
= PTR_ERR(lseg
);
1007 dprintk("%s: Could not allocate layout: error %d\n",
1012 spin_lock(&ino
->i_lock
);
1013 if (test_bit(NFS4CLNT_LAYOUTRECALL
, &clp
->cl_state
) ||
1014 test_bit(NFS_LAYOUT_BULK_RECALL
, &lo
->plh_flags
)) {
1015 dprintk("%s forget reply due to recall\n", __func__
);
1016 goto out_forget_reply
;
1019 if (pnfs_layoutgets_blocked(lo
, &res
->stateid
, 1)) {
1020 dprintk("%s forget reply due to state\n", __func__
);
1021 goto out_forget_reply
;
1023 init_lseg(lo
, lseg
);
1024 lseg
->pls_range
= res
->range
;
1025 *lgp
->lsegpp
= get_lseg(lseg
);
1026 pnfs_insert_layout(lo
, lseg
);
1028 if (res
->return_on_close
) {
1029 set_bit(NFS_LSEG_ROC
, &lseg
->pls_flags
);
1030 set_bit(NFS_LAYOUT_ROC
, &lo
->plh_flags
);
1033 /* Done processing layoutget. Set the layout stateid */
1034 pnfs_set_layout_stateid(lo
, &res
->stateid
, false);
1035 spin_unlock(&ino
->i_lock
);
1040 spin_unlock(&ino
->i_lock
);
1041 lseg
->pls_layout
= lo
;
1042 NFS_SERVER(ino
)->pnfs_curr_ld
->free_lseg(lseg
);
1046 static int pnfs_read_pg_test(struct nfs_pageio_descriptor
*pgio
,
1047 struct nfs_page
*prev
,
1048 struct nfs_page
*req
)
1050 if (pgio
->pg_count
== prev
->wb_bytes
) {
1051 /* This is first coelesce call for a series of nfs_pages */
1052 pgio
->pg_lseg
= pnfs_update_layout(pgio
->pg_inode
,
1058 } else if (pgio
->pg_lseg
&&
1059 req_offset(req
) > end_offset(pgio
->pg_lseg
->pls_range
.offset
,
1060 pgio
->pg_lseg
->pls_range
.length
))
1062 return NFS_SERVER(pgio
->pg_inode
)->pnfs_curr_ld
->pg_test(pgio
, prev
, req
);
1066 pnfs_pageio_init_read(struct nfs_pageio_descriptor
*pgio
, struct inode
*inode
)
1068 struct pnfs_layoutdriver_type
*ld
;
1070 ld
= NFS_SERVER(inode
)->pnfs_curr_ld
;
1071 pgio
->pg_test
= (ld
&& ld
->pg_test
) ? pnfs_read_pg_test
: NULL
;
1074 static int pnfs_write_pg_test(struct nfs_pageio_descriptor
*pgio
,
1075 struct nfs_page
*prev
,
1076 struct nfs_page
*req
)
1078 if (pgio
->pg_count
== prev
->wb_bytes
) {
1079 /* This is first coelesce call for a series of nfs_pages */
1080 pgio
->pg_lseg
= pnfs_update_layout(pgio
->pg_inode
,
1086 } else if (pgio
->pg_lseg
&&
1087 req_offset(req
) > end_offset(pgio
->pg_lseg
->pls_range
.offset
,
1088 pgio
->pg_lseg
->pls_range
.length
))
1090 return NFS_SERVER(pgio
->pg_inode
)->pnfs_curr_ld
->pg_test(pgio
, prev
, req
);
1094 pnfs_pageio_init_write(struct nfs_pageio_descriptor
*pgio
, struct inode
*inode
)
1096 struct pnfs_layoutdriver_type
*ld
;
1098 ld
= NFS_SERVER(inode
)->pnfs_curr_ld
;
1099 pgio
->pg_test
= (ld
&& ld
->pg_test
) ? pnfs_write_pg_test
: NULL
;
1103 * Called by non rpc-based layout drivers
1106 pnfs_ld_write_done(struct nfs_write_data
*data
)
1110 if (!data
->pnfs_error
) {
1111 pnfs_set_layoutcommit(data
);
1112 data
->mds_ops
->rpc_call_done(&data
->task
, data
);
1113 data
->mds_ops
->rpc_release(data
);
1117 dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__
,
1119 status
= nfs_initiate_write(data
, NFS_CLIENT(data
->inode
),
1120 data
->mds_ops
, NFS_FILE_SYNC
);
1121 return status
? : -EAGAIN
;
1123 EXPORT_SYMBOL_GPL(pnfs_ld_write_done
);
1125 enum pnfs_try_status
1126 pnfs_try_to_write_data(struct nfs_write_data
*wdata
,
1127 const struct rpc_call_ops
*call_ops
, int how
)
1129 struct inode
*inode
= wdata
->inode
;
1130 enum pnfs_try_status trypnfs
;
1131 struct nfs_server
*nfss
= NFS_SERVER(inode
);
1133 wdata
->mds_ops
= call_ops
;
1135 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__
,
1136 inode
->i_ino
, wdata
->args
.count
, wdata
->args
.offset
, how
);
1138 trypnfs
= nfss
->pnfs_curr_ld
->write_pagelist(wdata
, how
);
1139 if (trypnfs
== PNFS_NOT_ATTEMPTED
) {
1140 put_lseg(wdata
->lseg
);
1143 nfs_inc_stats(inode
, NFSIOS_PNFS_WRITE
);
1145 dprintk("%s End (trypnfs:%d)\n", __func__
, trypnfs
);
1150 * Called by non rpc-based layout drivers
1153 pnfs_ld_read_done(struct nfs_read_data
*data
)
1157 if (!data
->pnfs_error
) {
1158 __nfs4_read_done_cb(data
);
1159 data
->mds_ops
->rpc_call_done(&data
->task
, data
);
1160 data
->mds_ops
->rpc_release(data
);
1164 dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__
,
1166 status
= nfs_initiate_read(data
, NFS_CLIENT(data
->inode
),
1168 return status
? : -EAGAIN
;
1170 EXPORT_SYMBOL_GPL(pnfs_ld_read_done
);
1173 * Call the appropriate parallel I/O subsystem read function.
1175 enum pnfs_try_status
1176 pnfs_try_to_read_data(struct nfs_read_data
*rdata
,
1177 const struct rpc_call_ops
*call_ops
)
1179 struct inode
*inode
= rdata
->inode
;
1180 struct nfs_server
*nfss
= NFS_SERVER(inode
);
1181 enum pnfs_try_status trypnfs
;
1183 rdata
->mds_ops
= call_ops
;
1185 dprintk("%s: Reading ino:%lu %u@%llu\n",
1186 __func__
, inode
->i_ino
, rdata
->args
.count
, rdata
->args
.offset
);
1188 trypnfs
= nfss
->pnfs_curr_ld
->read_pagelist(rdata
);
1189 if (trypnfs
== PNFS_NOT_ATTEMPTED
) {
1190 put_lseg(rdata
->lseg
);
1193 nfs_inc_stats(inode
, NFSIOS_PNFS_READ
);
1195 dprintk("%s End (trypnfs:%d)\n", __func__
, trypnfs
);
1200 * Currently there is only one (whole file) write lseg.
1202 static struct pnfs_layout_segment
*pnfs_list_write_lseg(struct inode
*inode
)
1204 struct pnfs_layout_segment
*lseg
, *rv
= NULL
;
1206 list_for_each_entry(lseg
, &NFS_I(inode
)->layout
->plh_segs
, pls_list
)
1207 if (lseg
->pls_range
.iomode
== IOMODE_RW
)
1213 pnfs_set_layoutcommit(struct nfs_write_data
*wdata
)
1215 struct nfs_inode
*nfsi
= NFS_I(wdata
->inode
);
1216 loff_t end_pos
= wdata
->args
.offset
+ wdata
->res
.count
;
1217 bool mark_as_dirty
= false;
1219 spin_lock(&nfsi
->vfs_inode
.i_lock
);
1220 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT
, &nfsi
->flags
)) {
1221 /* references matched in nfs4_layoutcommit_release */
1222 get_lseg(wdata
->lseg
);
1223 wdata
->lseg
->pls_lc_cred
=
1224 get_rpccred(wdata
->args
.context
->state
->owner
->so_cred
);
1225 mark_as_dirty
= true;
1226 dprintk("%s: Set layoutcommit for inode %lu ",
1227 __func__
, wdata
->inode
->i_ino
);
1229 if (end_pos
> wdata
->lseg
->pls_end_pos
)
1230 wdata
->lseg
->pls_end_pos
= end_pos
;
1231 spin_unlock(&nfsi
->vfs_inode
.i_lock
);
1233 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1234 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1236 mark_inode_dirty_sync(wdata
->inode
);
1238 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit
);
1241 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1242 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1243 * data to disk to allow the server to recover the data if it crashes.
1244 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1245 * is off, and a COMMIT is sent to a data server, or
1246 * if WRITEs to a data server return NFS_DATA_SYNC.
1249 pnfs_layoutcommit_inode(struct inode
*inode
, bool sync
)
1251 struct nfs4_layoutcommit_data
*data
;
1252 struct nfs_inode
*nfsi
= NFS_I(inode
);
1253 struct pnfs_layout_segment
*lseg
;
1254 struct rpc_cred
*cred
;
1258 dprintk("--> %s inode %lu\n", __func__
, inode
->i_ino
);
1260 if (!test_bit(NFS_INO_LAYOUTCOMMIT
, &nfsi
->flags
))
1263 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1264 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
1266 mark_inode_dirty_sync(inode
);
1271 spin_lock(&inode
->i_lock
);
1272 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT
, &nfsi
->flags
)) {
1273 spin_unlock(&inode
->i_lock
);
1278 * Currently only one (whole file) write lseg which is referenced
1279 * in pnfs_set_layoutcommit and will be found.
1281 lseg
= pnfs_list_write_lseg(inode
);
1283 end_pos
= lseg
->pls_end_pos
;
1284 cred
= lseg
->pls_lc_cred
;
1285 lseg
->pls_end_pos
= 0;
1286 lseg
->pls_lc_cred
= NULL
;
1288 memcpy(&data
->args
.stateid
.data
, nfsi
->layout
->plh_stateid
.data
,
1289 sizeof(nfsi
->layout
->plh_stateid
.data
));
1290 spin_unlock(&inode
->i_lock
);
1292 data
->args
.inode
= inode
;
1295 nfs_fattr_init(&data
->fattr
);
1296 data
->args
.bitmask
= NFS_SERVER(inode
)->cache_consistency_bitmask
;
1297 data
->res
.fattr
= &data
->fattr
;
1298 data
->args
.lastbytewritten
= end_pos
- 1;
1299 data
->res
.server
= NFS_SERVER(inode
);
1301 status
= nfs4_proc_layoutcommit(data
, sync
);
1303 dprintk("<-- %s status %d\n", __func__
, status
);