2 * linux/fs/nfs/blocklayout/blocklayout.h
4 * Module for the NFSv4.1 pNFS block layout driver.
6 * Copyright (c) 2006 The Regents of the University of Michigan.
9 * Andy Adamson <andros@citi.umich.edu>
10 * Fred Isaman <iisaman@umich.edu>
12 * permission is granted to use, copy, create derivative works and
13 * redistribute this software and such derivative works for any purpose,
14 * so long as the name of the university of michigan is not used in
15 * any advertising or publicity pertaining to the use or distribution
16 * of this software without specific, written prior authorization. if
17 * the above copyright notice or any other identification of the
18 * university of michigan is included in any copy of any portion of
19 * this software, then the disclaimer below must also be included.
21 * this software is provided as is, without representation from the
22 * university of michigan as to its fitness for any purpose, and without
23 * warranty by the university of michigan of any kind, either express
24 * or implied, including without limitation the implied warranties of
25 * merchantability and fitness for a particular purpose. the regents
26 * of the university of michigan shall not be liable for any damages,
27 * including special, indirect, incidental, or consequential damages,
28 * with respect to any claim arising out or in connection with the use
29 * of the software, even if it has been or is hereafter advised of the
30 * possibility of such damages.
33 #include "blocklayout.h"
34 #define NFSDBG_FACILITY NFSDBG_PNFS_LD
37 #define EXTENT_INITIALIZED 0
38 #define EXTENT_WRITTEN 1
39 #define EXTENT_IN_COMMIT 2
40 #define INTERNAL_EXISTS MY_MAX_TAGS
41 #define INTERNAL_MASK ((1 << INTERNAL_EXISTS) - 1)
43 /* Returns largest t<=s s.t. t%base==0 */
44 static inline sector_t
normalize(sector_t s
, int base
)
46 sector_t tmp
= s
; /* Since do_div modifies its argument */
47 return s
- do_div(tmp
, base
);
50 static inline sector_t
normalize_up(sector_t s
, int base
)
52 return normalize(s
+ base
- 1, base
);
55 /* Complete stub using list while determine API wanted */
57 /* Returns tags, or negative */
58 static int32_t _find_entry(struct my_tree
*tree
, u64 s
)
60 struct pnfs_inval_tracking
*pos
;
62 dprintk("%s(%llu) enter\n", __func__
, s
);
63 list_for_each_entry_reverse(pos
, &tree
->mtt_stub
, it_link
) {
64 if (pos
->it_sector
> s
)
66 else if (pos
->it_sector
== s
)
67 return pos
->it_tags
& INTERNAL_MASK
;
75 int _has_tag(struct my_tree
*tree
, u64 s
, int32_t tag
)
79 dprintk("%s(%llu, %i) enter\n", __func__
, s
, tag
);
80 s
= normalize(s
, tree
->mtt_step_size
);
81 tags
= _find_entry(tree
, s
);
82 if ((tags
< 0) || !(tags
& (1 << tag
)))
88 /* Creates entry with tag, or if entry already exists, unions tag to it.
89 * If storage is not NULL, newly created entry will use it.
90 * Returns number of entries added, or negative on error.
92 static int _add_entry(struct my_tree
*tree
, u64 s
, int32_t tag
,
93 struct pnfs_inval_tracking
*storage
)
96 struct pnfs_inval_tracking
*pos
;
98 dprintk("%s(%llu, %i, %p) enter\n", __func__
, s
, tag
, storage
);
99 list_for_each_entry_reverse(pos
, &tree
->mtt_stub
, it_link
) {
100 if (pos
->it_sector
> s
)
102 else if (pos
->it_sector
== s
) {
109 pos
->it_tags
|= (1 << tag
);
112 struct pnfs_inval_tracking
*new;
115 new->it_tags
= (1 << tag
);
116 list_add(&new->it_link
, &pos
->it_link
);
121 /* XXXX Really want option to not create */
122 /* Over range, unions tag with existing entries, else creates entry with tag */
123 static int _set_range(struct my_tree
*tree
, int32_t tag
, u64 s
, u64 length
)
127 dprintk("%s(%i, %llu, %llu) enter\n", __func__
, tag
, s
, length
);
128 for (i
= normalize(s
, tree
->mtt_step_size
); i
< s
+ length
;
129 i
+= tree
->mtt_step_size
)
130 if (_add_entry(tree
, i
, tag
, NULL
))
135 /* Ensure that future operations on given range of tree will not malloc */
136 static int _preload_range(struct pnfs_inval_markings
*marks
,
137 u64 offset
, u64 length
)
140 int count
, i
, used
= 0, status
= -ENOMEM
;
141 struct pnfs_inval_tracking
**storage
;
142 struct my_tree
*tree
= &marks
->im_tree
;
144 dprintk("%s(%llu, %llu) enter\n", __func__
, offset
, length
);
145 start
= normalize(offset
, tree
->mtt_step_size
);
146 end
= normalize_up(offset
+ length
, tree
->mtt_step_size
);
147 count
= (int)(end
- start
) / (int)tree
->mtt_step_size
;
149 /* Pre-malloc what memory we might need */
150 storage
= kmalloc(sizeof(*storage
) * count
, GFP_NOFS
);
153 for (i
= 0; i
< count
; i
++) {
154 storage
[i
] = kmalloc(sizeof(struct pnfs_inval_tracking
),
160 spin_lock_bh(&marks
->im_lock
);
161 for (s
= start
; s
< end
; s
+= tree
->mtt_step_size
)
162 used
+= _add_entry(tree
, s
, INTERNAL_EXISTS
, storage
[used
]);
163 spin_unlock_bh(&marks
->im_lock
);
168 for (i
= used
; i
< count
; i
++) {
177 /* We are relying on page lock to serialize this */
178 int bl_is_sector_init(struct pnfs_inval_markings
*marks
, sector_t isect
)
182 spin_lock_bh(&marks
->im_lock
);
183 rv
= _has_tag(&marks
->im_tree
, isect
, EXTENT_INITIALIZED
);
184 spin_unlock_bh(&marks
->im_lock
);
188 /* Assume start, end already sector aligned */
190 _range_has_tag(struct my_tree
*tree
, u64 start
, u64 end
, int32_t tag
)
192 struct pnfs_inval_tracking
*pos
;
195 dprintk("%s(%llu, %llu, %i) enter\n", __func__
, start
, end
, tag
);
196 list_for_each_entry_reverse(pos
, &tree
->mtt_stub
, it_link
) {
197 if (pos
->it_sector
>= end
)
200 if ((pos
->it_sector
== end
- tree
->mtt_step_size
) &&
201 (pos
->it_tags
& (1 << tag
))) {
202 expect
= pos
->it_sector
- tree
->mtt_step_size
;
203 if (pos
->it_sector
< tree
->mtt_step_size
|| expect
< start
)
210 if (pos
->it_sector
!= expect
|| !(pos
->it_tags
& (1 << tag
)))
212 expect
-= tree
->mtt_step_size
;
219 static int is_range_written(struct pnfs_inval_markings
*marks
,
220 sector_t start
, sector_t end
)
224 spin_lock_bh(&marks
->im_lock
);
225 rv
= _range_has_tag(&marks
->im_tree
, start
, end
, EXTENT_WRITTEN
);
226 spin_unlock_bh(&marks
->im_lock
);
230 /* Marks sectors in [offest, offset_length) as having been initialized.
231 * All lengths are step-aligned, where step is min(pagesize, blocksize).
232 * Currently assumes offset is page-aligned
234 int bl_mark_sectors_init(struct pnfs_inval_markings
*marks
,
235 sector_t offset
, sector_t length
)
239 dprintk("%s(offset=%llu,len=%llu) enter\n",
240 __func__
, (u64
)offset
, (u64
)length
);
242 start
= normalize(offset
, marks
->im_block_size
);
243 end
= normalize_up(offset
+ length
, marks
->im_block_size
);
244 if (_preload_range(marks
, start
, end
- start
))
247 spin_lock_bh(&marks
->im_lock
);
248 if (_set_range(&marks
->im_tree
, EXTENT_INITIALIZED
, offset
, length
))
250 spin_unlock_bh(&marks
->im_lock
);
255 spin_unlock_bh(&marks
->im_lock
);
260 /* Marks sectors in [offest, offset+length) as having been written to disk.
261 * All lengths should be block aligned.
263 static int mark_written_sectors(struct pnfs_inval_markings
*marks
,
264 sector_t offset
, sector_t length
)
268 dprintk("%s(offset=%llu,len=%llu) enter\n", __func__
,
269 (u64
)offset
, (u64
)length
);
270 spin_lock_bh(&marks
->im_lock
);
271 status
= _set_range(&marks
->im_tree
, EXTENT_WRITTEN
, offset
, length
);
272 spin_unlock_bh(&marks
->im_lock
);
276 static void print_short_extent(struct pnfs_block_short_extent
*be
)
278 dprintk("PRINT SHORT EXTENT extent %p\n", be
);
280 dprintk(" be_f_offset %llu\n", (u64
)be
->bse_f_offset
);
281 dprintk(" be_length %llu\n", (u64
)be
->bse_length
);
285 static void print_clist(struct list_head
*list
, unsigned int count
)
287 struct pnfs_block_short_extent
*be
;
291 printk(KERN_DEBUG
"****************\n");
292 printk(KERN_DEBUG
"Extent list looks like:\n");
293 list_for_each_entry(be
, list
, bse_node
) {
295 print_short_extent(be
);
298 printk(KERN_DEBUG
"\n\nExpected %u entries\n\n\n", count
);
299 printk(KERN_DEBUG
"****************\n");
303 /* Note: In theory, we should do more checking that devid's match between
304 * old and new, but if they don't, the lists are too corrupt to salvage anyway.
306 /* Note this is very similar to bl_add_merge_extent */
307 static void add_to_commitlist(struct pnfs_block_layout
*bl
,
308 struct pnfs_block_short_extent
*new)
310 struct list_head
*clist
= &bl
->bl_commit
;
311 struct pnfs_block_short_extent
*old
, *save
;
312 sector_t end
= new->bse_f_offset
+ new->bse_length
;
314 dprintk("%s enter\n", __func__
);
315 print_short_extent(new);
316 print_clist(clist
, bl
->bl_count
);
318 /* Scan for proper place to insert, extending new to the left
319 * as much as possible.
321 list_for_each_entry_safe(old
, save
, clist
, bse_node
) {
322 if (new->bse_f_offset
< old
->bse_f_offset
)
324 if (end
<= old
->bse_f_offset
+ old
->bse_length
) {
325 /* Range is already in list */
329 } else if (new->bse_f_offset
<=
330 old
->bse_f_offset
+ old
->bse_length
) {
331 /* new overlaps or abuts existing be */
332 if (new->bse_mdev
== old
->bse_mdev
) {
333 /* extend new to fully replace old */
334 new->bse_length
+= new->bse_f_offset
-
336 new->bse_f_offset
= old
->bse_f_offset
;
337 list_del(&old
->bse_node
);
343 /* Note that if we never hit the above break, old will not point to a
344 * valid extent. However, in that case &old->bse_node==list.
346 list_add_tail(&new->bse_node
, &old
->bse_node
);
347 /* Scan forward for overlaps. If we find any, extend new and
348 * remove the overlapped extent.
350 old
= list_prepare_entry(new, clist
, bse_node
);
351 list_for_each_entry_safe_continue(old
, save
, clist
, bse_node
) {
352 if (end
< old
->bse_f_offset
)
354 /* new overlaps or abuts old */
355 if (new->bse_mdev
== old
->bse_mdev
) {
356 if (end
< old
->bse_f_offset
+ old
->bse_length
) {
357 /* extend new to fully cover old */
358 end
= old
->bse_f_offset
+ old
->bse_length
;
359 new->bse_length
= end
- new->bse_f_offset
;
361 list_del(&old
->bse_node
);
366 dprintk("%s: after merging\n", __func__
);
367 print_clist(clist
, bl
->bl_count
);
370 /* Note the range described by offset, length is guaranteed to be contained
372 * new will be freed, either by this function or add_to_commitlist if they
373 * decide not to use it, or after LAYOUTCOMMIT uses it in the commitlist.
375 int bl_mark_for_commit(struct pnfs_block_extent
*be
,
376 sector_t offset
, sector_t length
,
377 struct pnfs_block_short_extent
*new)
379 sector_t new_end
, end
= offset
+ length
;
380 struct pnfs_block_layout
*bl
= container_of(be
->be_inval
,
381 struct pnfs_block_layout
,
384 mark_written_sectors(be
->be_inval
, offset
, length
);
385 /* We want to add the range to commit list, but it must be
386 * block-normalized, and verified that the normalized range has
387 * been entirely written to disk.
389 new->bse_f_offset
= offset
;
390 offset
= normalize(offset
, bl
->bl_blocksize
);
391 if (offset
< new->bse_f_offset
) {
392 if (is_range_written(be
->be_inval
, offset
, new->bse_f_offset
))
393 new->bse_f_offset
= offset
;
395 new->bse_f_offset
= offset
+ bl
->bl_blocksize
;
397 new_end
= normalize_up(end
, bl
->bl_blocksize
);
399 if (is_range_written(be
->be_inval
, end
, new_end
))
402 end
= new_end
- bl
->bl_blocksize
;
404 if (end
<= new->bse_f_offset
) {
408 new->bse_length
= end
- new->bse_f_offset
;
409 new->bse_devid
= be
->be_devid
;
410 new->bse_mdev
= be
->be_mdev
;
412 spin_lock(&bl
->bl_ext_lock
);
413 add_to_commitlist(bl
, new);
414 spin_unlock(&bl
->bl_ext_lock
);
418 static void print_bl_extent(struct pnfs_block_extent
*be
)
420 dprintk("PRINT EXTENT extent %p\n", be
);
422 dprintk(" be_f_offset %llu\n", (u64
)be
->be_f_offset
);
423 dprintk(" be_length %llu\n", (u64
)be
->be_length
);
424 dprintk(" be_v_offset %llu\n", (u64
)be
->be_v_offset
);
425 dprintk(" be_state %d\n", be
->be_state
);
430 destroy_extent(struct kref
*kref
)
432 struct pnfs_block_extent
*be
;
434 be
= container_of(kref
, struct pnfs_block_extent
, be_refcnt
);
435 dprintk("%s be=%p\n", __func__
, be
);
440 bl_put_extent(struct pnfs_block_extent
*be
)
443 dprintk("%s enter %p (%i)\n", __func__
, be
,
444 atomic_read(&be
->be_refcnt
.refcount
));
445 kref_put(&be
->be_refcnt
, destroy_extent
);
449 struct pnfs_block_extent
*bl_alloc_extent(void)
451 struct pnfs_block_extent
*be
;
453 be
= kmalloc(sizeof(struct pnfs_block_extent
), GFP_NOFS
);
456 INIT_LIST_HEAD(&be
->be_node
);
457 kref_init(&be
->be_refcnt
);
462 static void print_elist(struct list_head
*list
)
464 struct pnfs_block_extent
*be
;
465 dprintk("****************\n");
466 dprintk("Extent list looks like:\n");
467 list_for_each_entry(be
, list
, be_node
) {
470 dprintk("****************\n");
474 extents_consistent(struct pnfs_block_extent
*old
, struct pnfs_block_extent
*new)
476 /* Note this assumes new->be_f_offset >= old->be_f_offset */
477 return (new->be_state
== old
->be_state
) &&
478 ((new->be_state
== PNFS_BLOCK_NONE_DATA
) ||
479 ((new->be_v_offset
- old
->be_v_offset
==
480 new->be_f_offset
- old
->be_f_offset
) &&
481 new->be_mdev
== old
->be_mdev
));
484 /* Adds new to appropriate list in bl, modifying new and removing existing
485 * extents as appropriate to deal with overlaps.
487 * See bl_find_get_extent for list constraints.
489 * Refcount on new is already set. If end up not using it, or error out,
490 * need to put the reference.
492 * bl->bl_ext_lock is held by caller.
495 bl_add_merge_extent(struct pnfs_block_layout
*bl
,
496 struct pnfs_block_extent
*new)
498 struct pnfs_block_extent
*be
, *tmp
;
499 sector_t end
= new->be_f_offset
+ new->be_length
;
500 struct list_head
*list
;
502 dprintk("%s enter with be=%p\n", __func__
, new);
503 print_bl_extent(new);
504 list
= &bl
->bl_extents
[bl_choose_list(new->be_state
)];
507 /* Scan for proper place to insert, extending new to the left
508 * as much as possible.
510 list_for_each_entry_safe_reverse(be
, tmp
, list
, be_node
) {
511 if (new->be_f_offset
>= be
->be_f_offset
+ be
->be_length
)
513 if (new->be_f_offset
>= be
->be_f_offset
) {
514 if (end
<= be
->be_f_offset
+ be
->be_length
) {
515 /* new is a subset of existing be*/
516 if (extents_consistent(be
, new)) {
517 dprintk("%s: new is subset, ignoring\n",
527 if (extents_consistent(be
, new)) {
528 /* extend new to fully replace be */
529 new->be_length
+= new->be_f_offset
-
531 new->be_f_offset
= be
->be_f_offset
;
532 new->be_v_offset
= be
->be_v_offset
;
533 dprintk("%s: removing %p\n", __func__
, be
);
534 list_del(&be
->be_node
);
540 } else if (end
>= be
->be_f_offset
+ be
->be_length
) {
541 /* new extent overlap existing be */
542 if (extents_consistent(be
, new)) {
543 /* extend new to fully replace be */
544 dprintk("%s: removing %p\n", __func__
, be
);
545 list_del(&be
->be_node
);
550 } else if (end
> be
->be_f_offset
) {
553 if (extents_consistent(new, be
)) {
554 /* extend new to fully replace be */
555 new->be_length
+= be
->be_f_offset
+ be
->be_length
-
556 new->be_f_offset
- new->be_length
;
557 dprintk("%s: removing %p\n", __func__
, be
);
558 list_del(&be
->be_node
);
565 /* Note that if we never hit the above break, be will not point to a
566 * valid extent. However, in that case &be->be_node==list.
568 list_add(&new->be_node
, &be
->be_node
);
569 dprintk("%s: inserting new\n", __func__
);
571 /* FIXME - The per-list consistency checks have all been done,
572 * should now check cross-list consistency.
581 /* Returns extent, or NULL. If a second READ extent exists, it is returned
582 * in cow_read, if given.
584 * The extents are kept in two seperate ordered lists, one for READ and NONE,
585 * one for READWRITE and INVALID. Within each list, we assume:
586 * 1. Extents are ordered by file offset.
587 * 2. For any given isect, there is at most one extents that matches.
589 struct pnfs_block_extent
*
590 bl_find_get_extent(struct pnfs_block_layout
*bl
, sector_t isect
,
591 struct pnfs_block_extent
**cow_read
)
593 struct pnfs_block_extent
*be
, *cow
, *ret
;
596 dprintk("%s enter with isect %llu\n", __func__
, (u64
)isect
);
598 spin_lock(&bl
->bl_ext_lock
);
599 for (i
= 0; i
< EXTENT_LISTS
; i
++) {
600 list_for_each_entry_reverse(be
, &bl
->bl_extents
[i
], be_node
) {
601 if (isect
>= be
->be_f_offset
+ be
->be_length
)
603 if (isect
>= be
->be_f_offset
) {
604 /* We have found an extent */
605 dprintk("%s Get %p (%i)\n", __func__
, be
,
606 atomic_read(&be
->be_refcnt
.refcount
));
607 kref_get(&be
->be_refcnt
);
610 else if (be
->be_state
!= PNFS_BLOCK_READ_DATA
)
618 (!cow_read
|| ret
->be_state
!= PNFS_BLOCK_INVALID_DATA
))
621 spin_unlock(&bl
->bl_ext_lock
);
624 print_bl_extent(ret
);
628 /* Similar to bl_find_get_extent, but called with lock held, and ignores cow */
629 static struct pnfs_block_extent
*
630 bl_find_get_extent_locked(struct pnfs_block_layout
*bl
, sector_t isect
)
632 struct pnfs_block_extent
*be
, *ret
= NULL
;
635 dprintk("%s enter with isect %llu\n", __func__
, (u64
)isect
);
636 for (i
= 0; i
< EXTENT_LISTS
; i
++) {
639 list_for_each_entry_reverse(be
, &bl
->bl_extents
[i
], be_node
) {
640 if (isect
>= be
->be_f_offset
+ be
->be_length
)
642 if (isect
>= be
->be_f_offset
) {
643 /* We have found an extent */
644 dprintk("%s Get %p (%i)\n", __func__
, be
,
645 atomic_read(&be
->be_refcnt
.refcount
));
646 kref_get(&be
->be_refcnt
);
652 print_bl_extent(ret
);
657 encode_pnfs_block_layoutupdate(struct pnfs_block_layout
*bl
,
658 struct xdr_stream
*xdr
,
659 const struct nfs4_layoutcommit_args
*arg
)
661 struct pnfs_block_short_extent
*lce
, *save
;
662 unsigned int count
= 0;
663 __be32
*p
, *xdr_start
;
665 dprintk("%s enter\n", __func__
);
666 /* BUG - creation of bl_commit is buggy - need to wait for
667 * entire block to be marked WRITTEN before it can be added.
669 spin_lock(&bl
->bl_ext_lock
);
670 /* Want to adjust for possible truncate */
671 /* We now want to adjust argument range */
673 /* XDR encode the ranges found */
674 xdr_start
= xdr_reserve_space(xdr
, 8);
677 list_for_each_entry_safe(lce
, save
, &bl
->bl_commit
, bse_node
) {
678 p
= xdr_reserve_space(xdr
, 7 * 4 + sizeof(lce
->bse_devid
.data
));
681 p
= xdr_encode_opaque_fixed(p
, lce
->bse_devid
.data
, NFS4_DEVICEID4_SIZE
);
682 p
= xdr_encode_hyper(p
, lce
->bse_f_offset
<< SECTOR_SHIFT
);
683 p
= xdr_encode_hyper(p
, lce
->bse_length
<< SECTOR_SHIFT
);
684 p
= xdr_encode_hyper(p
, 0LL);
685 *p
++ = cpu_to_be32(PNFS_BLOCK_READWRITE_DATA
);
686 list_del(&lce
->bse_node
);
687 list_add_tail(&lce
->bse_node
, &bl
->bl_committing
);
691 xdr_start
[0] = cpu_to_be32((xdr
->p
- xdr_start
- 1) * 4);
692 xdr_start
[1] = cpu_to_be32(count
);
694 spin_unlock(&bl
->bl_ext_lock
);
695 dprintk("%s found %i ranges\n", __func__
, count
);
699 /* Helper function to set_to_rw that initialize a new extent */
701 _prep_new_extent(struct pnfs_block_extent
*new,
702 struct pnfs_block_extent
*orig
,
703 sector_t offset
, sector_t length
, int state
)
705 kref_init(&new->be_refcnt
);
706 /* don't need to INIT_LIST_HEAD(&new->be_node) */
707 memcpy(&new->be_devid
, &orig
->be_devid
, sizeof(struct nfs4_deviceid
));
708 new->be_mdev
= orig
->be_mdev
;
709 new->be_f_offset
= offset
;
710 new->be_length
= length
;
711 new->be_v_offset
= orig
->be_v_offset
- orig
->be_f_offset
+ offset
;
712 new->be_state
= state
;
713 new->be_inval
= orig
->be_inval
;
716 /* Tries to merge be with extent in front of it in list.
717 * Frees storage if not used.
719 static struct pnfs_block_extent
*
720 _front_merge(struct pnfs_block_extent
*be
, struct list_head
*head
,
721 struct pnfs_block_extent
*storage
)
723 struct pnfs_block_extent
*prev
;
727 if (&be
->be_node
== head
|| be
->be_node
.prev
== head
)
729 prev
= list_entry(be
->be_node
.prev
, struct pnfs_block_extent
, be_node
);
730 if ((prev
->be_f_offset
+ prev
->be_length
!= be
->be_f_offset
) ||
731 !extents_consistent(prev
, be
))
733 _prep_new_extent(storage
, prev
, prev
->be_f_offset
,
734 prev
->be_length
+ be
->be_length
, prev
->be_state
);
735 list_replace(&prev
->be_node
, &storage
->be_node
);
737 list_del(&be
->be_node
);
747 set_to_rw(struct pnfs_block_layout
*bl
, u64 offset
, u64 length
)
749 u64 rv
= offset
+ length
;
750 struct pnfs_block_extent
*be
, *e1
, *e2
, *e3
, *new, *old
;
751 struct pnfs_block_extent
*children
[3];
752 struct pnfs_block_extent
*merge1
= NULL
, *merge2
= NULL
;
755 dprintk("%s(%llu, %llu)\n", __func__
, offset
, length
);
756 /* Create storage for up to three new extents e1, e2, e3 */
757 e1
= kmalloc(sizeof(*e1
), GFP_ATOMIC
);
758 e2
= kmalloc(sizeof(*e2
), GFP_ATOMIC
);
759 e3
= kmalloc(sizeof(*e3
), GFP_ATOMIC
);
760 /* BUG - we are ignoring any failure */
761 if (!e1
|| !e2
|| !e3
)
764 spin_lock(&bl
->bl_ext_lock
);
765 be
= bl_find_get_extent_locked(bl
, offset
);
766 rv
= be
->be_f_offset
+ be
->be_length
;
767 if (be
->be_state
!= PNFS_BLOCK_INVALID_DATA
) {
768 spin_unlock(&bl
->bl_ext_lock
);
771 /* Add e* to children, bumping e*'s krefs */
772 if (be
->be_f_offset
!= offset
) {
773 _prep_new_extent(e1
, be
, be
->be_f_offset
,
774 offset
- be
->be_f_offset
,
775 PNFS_BLOCK_INVALID_DATA
);
780 _prep_new_extent(e2
, be
, offset
,
781 min(length
, be
->be_f_offset
+ be
->be_length
- offset
),
782 PNFS_BLOCK_READWRITE_DATA
);
785 if (offset
+ length
< be
->be_f_offset
+ be
->be_length
) {
786 _prep_new_extent(e3
, be
, e2
->be_f_offset
+ e2
->be_length
,
787 be
->be_f_offset
+ be
->be_length
-
789 PNFS_BLOCK_INVALID_DATA
);
795 /* Remove be from list, and insert the e* */
796 /* We don't get refs on e*, since this list is the base reference
802 list_replace(&be
->be_node
, &new->be_node
);
804 new = _front_merge(new, &bl
->bl_extents
[RW_EXTENT
], merge1
);
805 for (j
= 1; j
< i
; j
++) {
808 list_add(&new->be_node
, &old
->be_node
);
811 /* This is a HACK, should just create a _back_merge function */
812 new = list_entry(new->be_node
.next
,
813 struct pnfs_block_extent
, be_node
);
814 new = _front_merge(new, &bl
->bl_extents
[RW_EXTENT
], merge2
);
816 spin_unlock(&bl
->bl_ext_lock
);
818 /* Since we removed the base reference above, be is now scheduled for
822 dprintk("%s returns %llu after split\n", __func__
, rv
);
829 dprintk("%s returns %llu without splitting\n", __func__
, rv
);
834 clean_pnfs_block_layoutupdate(struct pnfs_block_layout
*bl
,
835 const struct nfs4_layoutcommit_args
*arg
,
838 struct pnfs_block_short_extent
*lce
, *save
;
840 dprintk("%s status %d\n", __func__
, status
);
841 list_for_each_entry_safe(lce
, save
, &bl
->bl_committing
, bse_node
) {
842 if (likely(!status
)) {
843 u64 offset
= lce
->bse_f_offset
;
844 u64 end
= offset
+ lce
->bse_length
;
847 offset
= set_to_rw(bl
, offset
, end
- offset
);
848 } while (offset
< end
);
849 list_del(&lce
->bse_node
);
853 list_del(&lce
->bse_node
);
854 spin_lock(&bl
->bl_ext_lock
);
855 add_to_commitlist(bl
, lce
);
856 spin_unlock(&bl
->bl_ext_lock
);
861 int bl_push_one_short_extent(struct pnfs_inval_markings
*marks
)
863 struct pnfs_block_short_extent
*new;
865 new = kmalloc(sizeof(*new), GFP_NOFS
);
869 spin_lock_bh(&marks
->im_lock
);
870 list_add(&new->bse_node
, &marks
->im_extents
);
871 spin_unlock_bh(&marks
->im_lock
);
876 struct pnfs_block_short_extent
*
877 bl_pop_one_short_extent(struct pnfs_inval_markings
*marks
)
879 struct pnfs_block_short_extent
*rv
= NULL
;
881 spin_lock_bh(&marks
->im_lock
);
882 if (!list_empty(&marks
->im_extents
)) {
883 rv
= list_entry((&marks
->im_extents
)->next
,
884 struct pnfs_block_short_extent
, bse_node
);
885 list_del_init(&rv
->bse_node
);
887 spin_unlock_bh(&marks
->im_lock
);
892 void bl_free_short_extents(struct pnfs_inval_markings
*marks
, int num_to_free
)
894 struct pnfs_block_short_extent
*se
= NULL
, *tmp
;
896 if (num_to_free
<= 0)
899 spin_lock(&marks
->im_lock
);
900 list_for_each_entry_safe(se
, tmp
, &marks
->im_extents
, bse_node
) {
901 list_del(&se
->bse_node
);
903 if (--num_to_free
== 0)
906 spin_unlock(&marks
->im_lock
);
908 BUG_ON(num_to_free
> 0);