2 * Copyright (c) 2017 Christoph Hellwig.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #include <linux/cache.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
18 #include "xfs_format.h"
20 #include "xfs_log_format.h"
21 #include "xfs_inode.h"
22 #include "xfs_inode_fork.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_mount.h"
25 #include "xfs_trace.h"
28 * In-core extent record layout:
30 * +-------+----------------------------+
31 * | 00:53 | all 54 bits of startoff |
32 * | 54:63 | low 10 bits of startblock |
33 * +-------+----------------------------+
34 * | 00:20 | all 21 bits of length |
35 * | 21 | unwritten extent bit |
36 * | 22:63 | high 42 bits of startblock |
37 * +-------+----------------------------+
39 #define XFS_IEXT_STARTOFF_MASK xfs_mask64lo(BMBT_STARTOFF_BITLEN)
40 #define XFS_IEXT_LENGTH_MASK xfs_mask64lo(BMBT_BLOCKCOUNT_BITLEN)
41 #define XFS_IEXT_STARTBLOCK_MASK xfs_mask64lo(BMBT_STARTBLOCK_BITLEN)
49 * Given that the length can't be a zero, only an empty hi value indicates an
52 static bool xfs_iext_rec_is_empty(struct xfs_iext_rec
*rec
)
57 static inline void xfs_iext_rec_clear(struct xfs_iext_rec
*rec
)
65 struct xfs_iext_rec
*rec
,
66 struct xfs_bmbt_irec
*irec
)
68 ASSERT((irec
->br_startoff
& ~XFS_IEXT_STARTOFF_MASK
) == 0);
69 ASSERT((irec
->br_blockcount
& ~XFS_IEXT_LENGTH_MASK
) == 0);
70 ASSERT((irec
->br_startblock
& ~XFS_IEXT_STARTBLOCK_MASK
) == 0);
72 rec
->lo
= irec
->br_startoff
& XFS_IEXT_STARTOFF_MASK
;
73 rec
->hi
= irec
->br_blockcount
& XFS_IEXT_LENGTH_MASK
;
75 rec
->lo
|= (irec
->br_startblock
<< 54);
76 rec
->hi
|= ((irec
->br_startblock
& ~xfs_mask64lo(10)) << (22 - 10));
78 if (irec
->br_state
== XFS_EXT_UNWRITTEN
)
84 struct xfs_bmbt_irec
*irec
,
85 struct xfs_iext_rec
*rec
)
87 irec
->br_startoff
= rec
->lo
& XFS_IEXT_STARTOFF_MASK
;
88 irec
->br_blockcount
= rec
->hi
& XFS_IEXT_LENGTH_MASK
;
90 irec
->br_startblock
= rec
->lo
>> 54;
91 irec
->br_startblock
|= (rec
->hi
& xfs_mask64hi(42)) >> (22 - 10);
93 if (rec
->hi
& (1 << 21))
94 irec
->br_state
= XFS_EXT_UNWRITTEN
;
96 irec
->br_state
= XFS_EXT_NORM
;
101 KEYS_PER_NODE
= NODE_SIZE
/ (sizeof(uint64_t) + sizeof(void *)),
102 RECS_PER_LEAF
= (NODE_SIZE
- (2 * sizeof(struct xfs_iext_leaf
*))) /
103 sizeof(struct xfs_iext_rec
),
107 * In-core extent btree block layout:
109 * There are two types of blocks in the btree: leaf and inner (non-leaf) blocks.
111 * The leaf blocks are made up by %KEYS_PER_NODE extent records, which each
112 * contain the startoffset, blockcount, startblock and unwritten extent flag.
113 * See above for the exact format, followed by pointers to the previous and next
114 * leaf blocks (if there are any).
116 * The inner (non-leaf) blocks first contain KEYS_PER_NODE lookup keys, followed
117 * by an equal number of pointers to the btree blocks at the next lower level.
119 * +-------+-------+-------+-------+-------+----------+----------+
120 * Leaf: | rec 1 | rec 2 | rec 3 | rec 4 | rec N | prev-ptr | next-ptr |
121 * +-------+-------+-------+-------+-------+----------+----------+
123 * +-------+-------+-------+-------+-------+-------+------+-------+
124 * Inner: | key 1 | key 2 | key 3 | key N | ptr 1 | ptr 2 | ptr3 | ptr N |
125 * +-------+-------+-------+-------+-------+-------+------+-------+
127 struct xfs_iext_node
{
128 uint64_t keys
[KEYS_PER_NODE
];
129 #define XFS_IEXT_KEY_INVALID (1ULL << 63)
130 void *ptrs
[KEYS_PER_NODE
];
133 struct xfs_iext_leaf
{
134 struct xfs_iext_rec recs
[RECS_PER_LEAF
];
135 struct xfs_iext_leaf
*prev
;
136 struct xfs_iext_leaf
*next
;
139 inline xfs_extnum_t
xfs_iext_count(struct xfs_ifork
*ifp
)
141 return ifp
->if_bytes
/ sizeof(struct xfs_iext_rec
);
144 static inline int xfs_iext_max_recs(struct xfs_ifork
*ifp
)
146 if (ifp
->if_height
== 1)
147 return xfs_iext_count(ifp
);
148 return RECS_PER_LEAF
;
151 static inline struct xfs_iext_rec
*cur_rec(struct xfs_iext_cursor
*cur
)
153 return &cur
->leaf
->recs
[cur
->pos
];
156 static inline bool xfs_iext_valid(struct xfs_ifork
*ifp
,
157 struct xfs_iext_cursor
*cur
)
161 if (cur
->pos
< 0 || cur
->pos
>= xfs_iext_max_recs(ifp
))
163 if (xfs_iext_rec_is_empty(cur_rec(cur
)))
169 xfs_iext_find_first_leaf(
170 struct xfs_ifork
*ifp
)
172 struct xfs_iext_node
*node
= ifp
->if_u1
.if_root
;
178 for (height
= ifp
->if_height
; height
> 1; height
--) {
179 node
= node
->ptrs
[0];
187 xfs_iext_find_last_leaf(
188 struct xfs_ifork
*ifp
)
190 struct xfs_iext_node
*node
= ifp
->if_u1
.if_root
;
196 for (height
= ifp
->if_height
; height
> 1; height
--) {
197 for (i
= 1; i
< KEYS_PER_NODE
; i
++)
200 node
= node
->ptrs
[i
- 1];
209 struct xfs_ifork
*ifp
,
210 struct xfs_iext_cursor
*cur
)
213 cur
->leaf
= xfs_iext_find_first_leaf(ifp
);
218 struct xfs_ifork
*ifp
,
219 struct xfs_iext_cursor
*cur
)
223 cur
->leaf
= xfs_iext_find_last_leaf(ifp
);
229 for (i
= 1; i
< xfs_iext_max_recs(ifp
); i
++) {
230 if (xfs_iext_rec_is_empty(&cur
->leaf
->recs
[i
]))
238 struct xfs_ifork
*ifp
,
239 struct xfs_iext_cursor
*cur
)
242 ASSERT(cur
->pos
<= 0 || cur
->pos
>= RECS_PER_LEAF
);
243 xfs_iext_first(ifp
, cur
);
247 ASSERT(cur
->pos
>= 0);
248 ASSERT(cur
->pos
< xfs_iext_max_recs(ifp
));
251 if (ifp
->if_height
> 1 && !xfs_iext_valid(ifp
, cur
) &&
253 cur
->leaf
= cur
->leaf
->next
;
260 struct xfs_ifork
*ifp
,
261 struct xfs_iext_cursor
*cur
)
264 ASSERT(cur
->pos
<= 0 || cur
->pos
>= RECS_PER_LEAF
);
265 xfs_iext_last(ifp
, cur
);
269 ASSERT(cur
->pos
>= 0);
270 ASSERT(cur
->pos
<= RECS_PER_LEAF
);
275 if (xfs_iext_valid(ifp
, cur
))
277 } while (cur
->pos
> 0);
279 if (ifp
->if_height
> 1 && cur
->leaf
->prev
) {
280 cur
->leaf
= cur
->leaf
->prev
;
281 cur
->pos
= RECS_PER_LEAF
;
288 struct xfs_iext_node
*node
,
290 xfs_fileoff_t offset
)
292 if (node
->keys
[n
] > offset
)
294 if (node
->keys
[n
] < offset
)
301 struct xfs_iext_rec
*rec
,
302 xfs_fileoff_t offset
)
304 uint64_t rec_offset
= rec
->lo
& XFS_IEXT_STARTOFF_MASK
;
305 uint32_t rec_len
= rec
->hi
& XFS_IEXT_LENGTH_MASK
;
307 if (rec_offset
> offset
)
309 if (rec_offset
+ rec_len
<= offset
)
316 struct xfs_ifork
*ifp
,
317 xfs_fileoff_t offset
,
320 struct xfs_iext_node
*node
= ifp
->if_u1
.if_root
;
326 for (height
= ifp
->if_height
; height
> level
; height
--) {
327 for (i
= 1; i
< KEYS_PER_NODE
; i
++)
328 if (xfs_iext_key_cmp(node
, i
, offset
) > 0)
331 node
= node
->ptrs
[i
- 1];
341 struct xfs_iext_node
*node
,
342 xfs_fileoff_t offset
)
346 for (i
= 1; i
< KEYS_PER_NODE
; i
++) {
347 if (xfs_iext_key_cmp(node
, i
, offset
) > 0)
355 xfs_iext_node_insert_pos(
356 struct xfs_iext_node
*node
,
357 xfs_fileoff_t offset
)
361 for (i
= 0; i
< KEYS_PER_NODE
; i
++) {
362 if (xfs_iext_key_cmp(node
, i
, offset
) > 0)
366 return KEYS_PER_NODE
;
370 xfs_iext_node_nr_entries(
371 struct xfs_iext_node
*node
,
376 for (i
= start
; i
< KEYS_PER_NODE
; i
++) {
377 if (node
->keys
[i
] == XFS_IEXT_KEY_INVALID
)
385 xfs_iext_leaf_nr_entries(
386 struct xfs_ifork
*ifp
,
387 struct xfs_iext_leaf
*leaf
,
392 for (i
= start
; i
< xfs_iext_max_recs(ifp
); i
++) {
393 if (xfs_iext_rec_is_empty(&leaf
->recs
[i
]))
400 static inline uint64_t
402 struct xfs_iext_leaf
*leaf
,
405 return leaf
->recs
[n
].lo
& XFS_IEXT_STARTOFF_MASK
;
410 struct xfs_ifork
*ifp
)
412 struct xfs_iext_node
*node
= kmem_zalloc(NODE_SIZE
, KM_NOFS
);
415 if (ifp
->if_height
== 1) {
416 struct xfs_iext_leaf
*prev
= ifp
->if_u1
.if_root
;
418 node
->keys
[0] = xfs_iext_leaf_key(prev
, 0);
419 node
->ptrs
[0] = prev
;
421 struct xfs_iext_node
*prev
= ifp
->if_u1
.if_root
;
423 ASSERT(ifp
->if_height
> 1);
425 node
->keys
[0] = prev
->keys
[0];
426 node
->ptrs
[0] = prev
;
429 for (i
= 1; i
< KEYS_PER_NODE
; i
++)
430 node
->keys
[i
] = XFS_IEXT_KEY_INVALID
;
432 ifp
->if_u1
.if_root
= node
;
437 xfs_iext_update_node(
438 struct xfs_ifork
*ifp
,
439 xfs_fileoff_t old_offset
,
440 xfs_fileoff_t new_offset
,
444 struct xfs_iext_node
*node
= ifp
->if_u1
.if_root
;
447 for (height
= ifp
->if_height
; height
> level
; height
--) {
448 for (i
= 0; i
< KEYS_PER_NODE
; i
++) {
449 if (i
> 0 && xfs_iext_key_cmp(node
, i
, old_offset
) > 0)
451 if (node
->keys
[i
] == old_offset
)
452 node
->keys
[i
] = new_offset
;
454 node
= node
->ptrs
[i
- 1];
461 static struct xfs_iext_node
*
463 struct xfs_iext_node
**nodep
,
467 struct xfs_iext_node
*node
= *nodep
;
468 struct xfs_iext_node
*new = kmem_zalloc(NODE_SIZE
, KM_NOFS
);
469 const int nr_move
= KEYS_PER_NODE
/ 2;
470 int nr_keep
= nr_move
+ (KEYS_PER_NODE
& 1);
473 /* for sequential append operations just spill over into the new node */
474 if (*pos
== KEYS_PER_NODE
) {
482 for (i
= 0; i
< nr_move
; i
++) {
483 new->keys
[i
] = node
->keys
[nr_keep
+ i
];
484 new->ptrs
[i
] = node
->ptrs
[nr_keep
+ i
];
486 node
->keys
[nr_keep
+ i
] = XFS_IEXT_KEY_INVALID
;
487 node
->ptrs
[nr_keep
+ i
] = NULL
;
490 if (*pos
>= nr_keep
) {
493 *nr_entries
= nr_move
;
495 *nr_entries
= nr_keep
;
498 for (; i
< KEYS_PER_NODE
; i
++)
499 new->keys
[i
] = XFS_IEXT_KEY_INVALID
;
504 xfs_iext_insert_node(
505 struct xfs_ifork
*ifp
,
510 struct xfs_iext_node
*node
, *new;
511 int i
, pos
, nr_entries
;
514 if (ifp
->if_height
< level
)
518 node
= xfs_iext_find_level(ifp
, offset
, level
);
519 pos
= xfs_iext_node_insert_pos(node
, offset
);
520 nr_entries
= xfs_iext_node_nr_entries(node
, pos
);
522 ASSERT(pos
>= nr_entries
|| xfs_iext_key_cmp(node
, pos
, offset
) != 0);
523 ASSERT(nr_entries
<= KEYS_PER_NODE
);
525 if (nr_entries
== KEYS_PER_NODE
)
526 new = xfs_iext_split_node(&node
, &pos
, &nr_entries
);
529 * Update the pointers in higher levels if the first entry changes
530 * in an existing node.
532 if (node
!= new && pos
== 0 && nr_entries
> 0)
533 xfs_iext_update_node(ifp
, node
->keys
[0], offset
, level
, node
);
535 for (i
= nr_entries
; i
> pos
; i
--) {
536 node
->keys
[i
] = node
->keys
[i
- 1];
537 node
->ptrs
[i
] = node
->ptrs
[i
- 1];
539 node
->keys
[pos
] = offset
;
540 node
->ptrs
[pos
] = ptr
;
543 offset
= new->keys
[0];
550 static struct xfs_iext_leaf
*
552 struct xfs_iext_cursor
*cur
,
555 struct xfs_iext_leaf
*leaf
= cur
->leaf
;
556 struct xfs_iext_leaf
*new = kmem_zalloc(NODE_SIZE
, KM_NOFS
);
557 const int nr_move
= RECS_PER_LEAF
/ 2;
558 int nr_keep
= nr_move
+ (RECS_PER_LEAF
& 1);
561 /* for sequential append operations just spill over into the new node */
562 if (cur
->pos
== RECS_PER_LEAF
) {
569 for (i
= 0; i
< nr_move
; i
++) {
570 new->recs
[i
] = leaf
->recs
[nr_keep
+ i
];
571 xfs_iext_rec_clear(&leaf
->recs
[nr_keep
+ i
]);
574 if (cur
->pos
>= nr_keep
) {
577 *nr_entries
= nr_move
;
579 *nr_entries
= nr_keep
;
583 leaf
->next
->prev
= new;
584 new->next
= leaf
->next
;
592 struct xfs_ifork
*ifp
,
593 struct xfs_iext_cursor
*cur
)
595 ASSERT(ifp
->if_bytes
== 0);
597 ifp
->if_u1
.if_root
= kmem_zalloc(sizeof(struct xfs_iext_rec
), KM_NOFS
);
600 /* now that we have a node step into it */
601 cur
->leaf
= ifp
->if_u1
.if_root
;
606 xfs_iext_realloc_root(
607 struct xfs_ifork
*ifp
,
608 struct xfs_iext_cursor
*cur
)
610 size_t new_size
= ifp
->if_bytes
+ sizeof(struct xfs_iext_rec
);
613 /* account for the prev/next pointers */
614 if (new_size
/ sizeof(struct xfs_iext_rec
) == RECS_PER_LEAF
)
615 new_size
= NODE_SIZE
;
617 new = kmem_realloc(ifp
->if_u1
.if_root
, new_size
, KM_NOFS
);
618 memset(new + ifp
->if_bytes
, 0, new_size
- ifp
->if_bytes
);
619 ifp
->if_u1
.if_root
= new;
625 struct xfs_inode
*ip
,
626 struct xfs_iext_cursor
*cur
,
627 struct xfs_bmbt_irec
*irec
,
630 struct xfs_ifork
*ifp
= xfs_iext_state_to_fork(ip
, state
);
631 xfs_fileoff_t offset
= irec
->br_startoff
;
632 struct xfs_iext_leaf
*new = NULL
;
635 trace_xfs_iext_insert(ip
, cur
, state
, _RET_IP_
);
637 if (ifp
->if_height
== 0)
638 xfs_iext_alloc_root(ifp
, cur
);
639 else if (ifp
->if_height
== 1)
640 xfs_iext_realloc_root(ifp
, cur
);
642 nr_entries
= xfs_iext_leaf_nr_entries(ifp
, cur
->leaf
, cur
->pos
);
643 ASSERT(nr_entries
<= RECS_PER_LEAF
);
644 ASSERT(cur
->pos
>= nr_entries
||
645 xfs_iext_rec_cmp(cur_rec(cur
), irec
->br_startoff
) != 0);
647 if (nr_entries
== RECS_PER_LEAF
)
648 new = xfs_iext_split_leaf(cur
, &nr_entries
);
651 * Update the pointers in higher levels if the first entry changes
652 * in an existing node.
654 if (cur
->leaf
!= new && cur
->pos
== 0 && nr_entries
> 0) {
655 xfs_iext_update_node(ifp
, xfs_iext_leaf_key(cur
->leaf
, 0),
656 offset
, 1, cur
->leaf
);
659 for (i
= nr_entries
; i
> cur
->pos
; i
--)
660 cur
->leaf
->recs
[i
] = cur
->leaf
->recs
[i
- 1];
661 xfs_iext_set(cur_rec(cur
), irec
);
662 ifp
->if_bytes
+= sizeof(struct xfs_iext_rec
);
665 xfs_iext_insert_node(ifp
, xfs_iext_leaf_key(new, 0), new, 2);
668 static struct xfs_iext_node
*
669 xfs_iext_rebalance_node(
670 struct xfs_iext_node
*parent
,
672 struct xfs_iext_node
*node
,
676 * If the neighbouring nodes are completely full, or have different
677 * parents, we might never be able to merge our node, and will only
678 * delete it once the number of entries hits zero.
684 struct xfs_iext_node
*prev
= parent
->ptrs
[*pos
- 1];
685 int nr_prev
= xfs_iext_node_nr_entries(prev
, 0), i
;
687 if (nr_prev
+ nr_entries
<= KEYS_PER_NODE
) {
688 for (i
= 0; i
< nr_entries
; i
++) {
689 prev
->keys
[nr_prev
+ i
] = node
->keys
[i
];
690 prev
->ptrs
[nr_prev
+ i
] = node
->ptrs
[i
];
696 if (*pos
+ 1 < xfs_iext_node_nr_entries(parent
, *pos
)) {
697 struct xfs_iext_node
*next
= parent
->ptrs
[*pos
+ 1];
698 int nr_next
= xfs_iext_node_nr_entries(next
, 0), i
;
700 if (nr_entries
+ nr_next
<= KEYS_PER_NODE
) {
702 * Merge the next node into this node so that we don't
703 * have to do an additional update of the keys in the
706 for (i
= 0; i
< nr_next
; i
++) {
707 node
->keys
[nr_entries
+ i
] = next
->keys
[i
];
708 node
->ptrs
[nr_entries
+ i
] = next
->ptrs
[i
];
720 xfs_iext_remove_node(
721 struct xfs_ifork
*ifp
,
722 xfs_fileoff_t offset
,
725 struct xfs_iext_node
*node
, *parent
;
726 int level
= 2, pos
, nr_entries
, i
;
728 ASSERT(level
<= ifp
->if_height
);
729 node
= xfs_iext_find_level(ifp
, offset
, level
);
730 pos
= xfs_iext_node_pos(node
, offset
);
732 ASSERT(node
->ptrs
[pos
]);
733 ASSERT(node
->ptrs
[pos
] == victim
);
736 nr_entries
= xfs_iext_node_nr_entries(node
, pos
) - 1;
737 offset
= node
->keys
[0];
738 for (i
= pos
; i
< nr_entries
; i
++) {
739 node
->keys
[i
] = node
->keys
[i
+ 1];
740 node
->ptrs
[i
] = node
->ptrs
[i
+ 1];
742 node
->keys
[nr_entries
] = XFS_IEXT_KEY_INVALID
;
743 node
->ptrs
[nr_entries
] = NULL
;
745 if (pos
== 0 && nr_entries
> 0) {
746 xfs_iext_update_node(ifp
, offset
, node
->keys
[0], level
, node
);
747 offset
= node
->keys
[0];
750 if (nr_entries
>= KEYS_PER_NODE
/ 2)
753 if (level
< ifp
->if_height
) {
755 * If we aren't at the root yet try to find a neighbour node to
756 * merge with (or delete the node if it is empty), and then
757 * recurse up to the next level.
760 parent
= xfs_iext_find_level(ifp
, offset
, level
);
761 pos
= xfs_iext_node_pos(parent
, offset
);
763 ASSERT(pos
!= KEYS_PER_NODE
);
764 ASSERT(parent
->ptrs
[pos
] == node
);
766 node
= xfs_iext_rebalance_node(parent
, &pos
, node
, nr_entries
);
772 } else if (nr_entries
== 1) {
774 * If we are at the root and only one entry is left we can just
775 * free this node and update the root pointer.
777 ASSERT(node
== ifp
->if_u1
.if_root
);
778 ifp
->if_u1
.if_root
= node
->ptrs
[0];
785 xfs_iext_rebalance_leaf(
786 struct xfs_ifork
*ifp
,
787 struct xfs_iext_cursor
*cur
,
788 struct xfs_iext_leaf
*leaf
,
789 xfs_fileoff_t offset
,
793 * If the neighbouring nodes are completely full we might never be able
794 * to merge our node, and will only delete it once the number of
801 int nr_prev
= xfs_iext_leaf_nr_entries(ifp
, leaf
->prev
, 0), i
;
803 if (nr_prev
+ nr_entries
<= RECS_PER_LEAF
) {
804 for (i
= 0; i
< nr_entries
; i
++)
805 leaf
->prev
->recs
[nr_prev
+ i
] = leaf
->recs
[i
];
807 if (cur
->leaf
== leaf
) {
808 cur
->leaf
= leaf
->prev
;
816 int nr_next
= xfs_iext_leaf_nr_entries(ifp
, leaf
->next
, 0), i
;
818 if (nr_entries
+ nr_next
<= RECS_PER_LEAF
) {
820 * Merge the next node into this node so that we don't
821 * have to do an additional update of the keys in the
824 for (i
= 0; i
< nr_next
; i
++) {
825 leaf
->recs
[nr_entries
+ i
] =
829 if (cur
->leaf
== leaf
->next
) {
831 cur
->pos
+= nr_entries
;
834 offset
= xfs_iext_leaf_key(leaf
->next
, 0);
843 leaf
->prev
->next
= leaf
->next
;
845 leaf
->next
->prev
= leaf
->prev
;
846 xfs_iext_remove_node(ifp
, offset
, leaf
);
850 xfs_iext_free_last_leaf(
851 struct xfs_ifork
*ifp
)
853 ifp
->if_u1
.if_root
= NULL
;
855 kmem_free(ifp
->if_u1
.if_root
);
860 struct xfs_inode
*ip
,
861 struct xfs_iext_cursor
*cur
,
864 struct xfs_ifork
*ifp
= xfs_iext_state_to_fork(ip
, state
);
865 struct xfs_iext_leaf
*leaf
= cur
->leaf
;
866 xfs_fileoff_t offset
= xfs_iext_leaf_key(leaf
, 0);
869 trace_xfs_iext_remove(ip
, cur
, state
, _RET_IP_
);
871 ASSERT(ifp
->if_height
> 0);
872 ASSERT(ifp
->if_u1
.if_root
!= NULL
);
873 ASSERT(xfs_iext_valid(ifp
, cur
));
875 nr_entries
= xfs_iext_leaf_nr_entries(ifp
, leaf
, cur
->pos
) - 1;
876 for (i
= cur
->pos
; i
< nr_entries
; i
++)
877 leaf
->recs
[i
] = leaf
->recs
[i
+ 1];
878 xfs_iext_rec_clear(&leaf
->recs
[nr_entries
]);
879 ifp
->if_bytes
-= sizeof(struct xfs_iext_rec
);
881 if (cur
->pos
== 0 && nr_entries
> 0) {
882 xfs_iext_update_node(ifp
, offset
, xfs_iext_leaf_key(leaf
, 0), 1,
884 offset
= xfs_iext_leaf_key(leaf
, 0);
885 } else if (cur
->pos
== nr_entries
) {
886 if (ifp
->if_height
> 1 && leaf
->next
)
887 cur
->leaf
= leaf
->next
;
893 if (nr_entries
>= RECS_PER_LEAF
/ 2)
896 if (ifp
->if_height
> 1)
897 xfs_iext_rebalance_leaf(ifp
, cur
, leaf
, offset
, nr_entries
);
898 else if (nr_entries
== 0)
899 xfs_iext_free_last_leaf(ifp
);
903 * Lookup the extent covering bno.
905 * If there is an extent covering bno return the extent index, and store the
906 * expanded extent structure in *gotp, and the extent cursor in *cur.
907 * If there is no extent covering bno, but there is an extent after it (e.g.
908 * it lies in a hole) return that extent in *gotp and its cursor in *cur
910 * If bno is beyond the last extent return false, and return an invalid
914 xfs_iext_lookup_extent(
915 struct xfs_inode
*ip
,
916 struct xfs_ifork
*ifp
,
917 xfs_fileoff_t offset
,
918 struct xfs_iext_cursor
*cur
,
919 struct xfs_bmbt_irec
*gotp
)
921 XFS_STATS_INC(ip
->i_mount
, xs_look_exlist
);
923 cur
->leaf
= xfs_iext_find_level(ifp
, offset
, 1);
929 for (cur
->pos
= 0; cur
->pos
< xfs_iext_max_recs(ifp
); cur
->pos
++) {
930 struct xfs_iext_rec
*rec
= cur_rec(cur
);
932 if (xfs_iext_rec_is_empty(rec
))
934 if (xfs_iext_rec_cmp(rec
, offset
) >= 0)
938 /* Try looking in the next node for an entry > offset */
939 if (ifp
->if_height
== 1 || !cur
->leaf
->next
)
941 cur
->leaf
= cur
->leaf
->next
;
943 if (!xfs_iext_valid(ifp
, cur
))
946 xfs_iext_get(gotp
, cur_rec(cur
));
951 * Returns the last extent before end, and if this extent doesn't cover
952 * end, update end to the end of the extent.
955 xfs_iext_lookup_extent_before(
956 struct xfs_inode
*ip
,
957 struct xfs_ifork
*ifp
,
959 struct xfs_iext_cursor
*cur
,
960 struct xfs_bmbt_irec
*gotp
)
962 /* could be optimized to not even look up the next on a match.. */
963 if (xfs_iext_lookup_extent(ip
, ifp
, *end
- 1, cur
, gotp
) &&
964 gotp
->br_startoff
<= *end
- 1)
966 if (!xfs_iext_prev_extent(ifp
, cur
, gotp
))
968 *end
= gotp
->br_startoff
+ gotp
->br_blockcount
;
973 xfs_iext_update_extent(
974 struct xfs_inode
*ip
,
976 struct xfs_iext_cursor
*cur
,
977 struct xfs_bmbt_irec
*new)
979 struct xfs_ifork
*ifp
= xfs_iext_state_to_fork(ip
, state
);
982 struct xfs_bmbt_irec old
;
984 xfs_iext_get(&old
, cur_rec(cur
));
985 if (new->br_startoff
!= old
.br_startoff
) {
986 xfs_iext_update_node(ifp
, old
.br_startoff
,
987 new->br_startoff
, 1, cur
->leaf
);
991 trace_xfs_bmap_pre_update(ip
, cur
, state
, _RET_IP_
);
992 xfs_iext_set(cur_rec(cur
), new);
993 trace_xfs_bmap_post_update(ip
, cur
, state
, _RET_IP_
);
997 * Return true if the cursor points at an extent and return the extent structure
998 * in gotp. Else return false.
1001 xfs_iext_get_extent(
1002 struct xfs_ifork
*ifp
,
1003 struct xfs_iext_cursor
*cur
,
1004 struct xfs_bmbt_irec
*gotp
)
1006 if (!xfs_iext_valid(ifp
, cur
))
1008 xfs_iext_get(gotp
, cur_rec(cur
));
1013 * This is a recursive function, because of that we need to be extremely
1014 * careful with stack usage.
1017 xfs_iext_destroy_node(
1018 struct xfs_iext_node
*node
,
1024 for (i
= 0; i
< KEYS_PER_NODE
; i
++) {
1025 if (node
->keys
[i
] == XFS_IEXT_KEY_INVALID
)
1027 xfs_iext_destroy_node(node
->ptrs
[i
], level
- 1);
1036 struct xfs_ifork
*ifp
)
1038 xfs_iext_destroy_node(ifp
->if_u1
.if_root
, ifp
->if_height
);
1042 ifp
->if_u1
.if_root
= NULL
;