1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Extent allocs and frees
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
30 #include <linux/swap.h>
32 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33 #include <cluster/masklog.h>
40 #include "extent_map.h"
43 #include "localalloc.h"
50 #include "buffer_head_io.h"
53 * ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract
54 * the b-tree operations in ocfs2. Now all the b-tree operations are not
55 * limited to ocfs2_dinode only. Any data which need to allocate clusters
56 * to store can use b-tree. And it only needs to implement its ocfs2_extent_tree
59 * ocfs2_extent_tree contains info for the root of the b-tree, it must have a
60 * root ocfs2_extent_list and a root_bh so that they can be used in the b-tree
62 * ocfs2_extent_tree_operations abstract the normal operations we do for
63 * the root of extent b-tree.
65 struct ocfs2_extent_tree
;
67 struct ocfs2_extent_tree_operations
{
68 void (*eo_set_last_eb_blk
)(struct ocfs2_extent_tree
*et
,
70 u64 (*eo_get_last_eb_blk
)(struct ocfs2_extent_tree
*et
);
71 void (*eo_update_clusters
)(struct inode
*inode
,
72 struct ocfs2_extent_tree
*et
,
74 int (*eo_sanity_check
)(struct inode
*inode
, struct ocfs2_extent_tree
*et
);
77 struct ocfs2_extent_tree
{
78 enum ocfs2_extent_tree_type et_type
;
79 struct ocfs2_extent_tree_operations
*et_ops
;
80 struct buffer_head
*et_root_bh
;
81 struct ocfs2_extent_list
*et_root_el
;
83 unsigned int et_max_leaf_clusters
;
86 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
89 struct ocfs2_dinode
*di
= et
->et_object
;
91 BUG_ON(et
->et_type
!= OCFS2_DINODE_EXTENT
);
92 di
->i_last_eb_blk
= cpu_to_le64(blkno
);
95 static u64
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
97 struct ocfs2_dinode
*di
= et
->et_object
;
99 BUG_ON(et
->et_type
!= OCFS2_DINODE_EXTENT
);
100 return le64_to_cpu(di
->i_last_eb_blk
);
103 static void ocfs2_dinode_update_clusters(struct inode
*inode
,
104 struct ocfs2_extent_tree
*et
,
107 struct ocfs2_dinode
*di
= et
->et_object
;
109 le32_add_cpu(&di
->i_clusters
, clusters
);
110 spin_lock(&OCFS2_I(inode
)->ip_lock
);
111 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(di
->i_clusters
);
112 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
115 static int ocfs2_dinode_sanity_check(struct inode
*inode
,
116 struct ocfs2_extent_tree
*et
)
119 struct ocfs2_dinode
*di
;
121 BUG_ON(et
->et_type
!= OCFS2_DINODE_EXTENT
);
124 if (!OCFS2_IS_VALID_DINODE(di
)) {
126 ocfs2_error(inode
->i_sb
,
127 "Inode %llu has invalid path root",
128 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
134 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops
= {
135 .eo_set_last_eb_blk
= ocfs2_dinode_set_last_eb_blk
,
136 .eo_get_last_eb_blk
= ocfs2_dinode_get_last_eb_blk
,
137 .eo_update_clusters
= ocfs2_dinode_update_clusters
,
138 .eo_sanity_check
= ocfs2_dinode_sanity_check
,
141 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
144 struct ocfs2_xattr_value_root
*xv
=
145 (struct ocfs2_xattr_value_root
*)et
->et_object
;
147 xv
->xr_last_eb_blk
= cpu_to_le64(blkno
);
150 static u64
ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
152 struct ocfs2_xattr_value_root
*xv
=
153 (struct ocfs2_xattr_value_root
*) et
->et_object
;
155 return le64_to_cpu(xv
->xr_last_eb_blk
);
158 static void ocfs2_xattr_value_update_clusters(struct inode
*inode
,
159 struct ocfs2_extent_tree
*et
,
162 struct ocfs2_xattr_value_root
*xv
=
163 (struct ocfs2_xattr_value_root
*)et
->et_object
;
165 le32_add_cpu(&xv
->xr_clusters
, clusters
);
168 static int ocfs2_xattr_value_sanity_check(struct inode
*inode
,
169 struct ocfs2_extent_tree
*et
)
174 static struct ocfs2_extent_tree_operations ocfs2_xattr_et_ops
= {
175 .eo_set_last_eb_blk
= ocfs2_xattr_value_set_last_eb_blk
,
176 .eo_get_last_eb_blk
= ocfs2_xattr_value_get_last_eb_blk
,
177 .eo_update_clusters
= ocfs2_xattr_value_update_clusters
,
178 .eo_sanity_check
= ocfs2_xattr_value_sanity_check
,
181 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
184 struct ocfs2_xattr_block
*xb
= et
->et_object
;
185 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
187 xt
->xt_last_eb_blk
= cpu_to_le64(blkno
);
190 static u64
ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
192 struct ocfs2_xattr_block
*xb
= et
->et_object
;
193 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
195 return le64_to_cpu(xt
->xt_last_eb_blk
);
198 static void ocfs2_xattr_tree_update_clusters(struct inode
*inode
,
199 struct ocfs2_extent_tree
*et
,
202 struct ocfs2_xattr_block
*xb
= et
->et_object
;
204 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, clusters
);
207 static int ocfs2_xattr_tree_sanity_check(struct inode
*inode
,
208 struct ocfs2_extent_tree
*et
)
213 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops
= {
214 .eo_set_last_eb_blk
= ocfs2_xattr_tree_set_last_eb_blk
,
215 .eo_get_last_eb_blk
= ocfs2_xattr_tree_get_last_eb_blk
,
216 .eo_update_clusters
= ocfs2_xattr_tree_update_clusters
,
217 .eo_sanity_check
= ocfs2_xattr_tree_sanity_check
,
220 static void ocfs2_get_extent_tree(struct ocfs2_extent_tree
*et
,
222 struct buffer_head
*bh
,
223 enum ocfs2_extent_tree_type et_type
,
226 et
->et_type
= et_type
;
229 et
->et_max_leaf_clusters
= 0;
231 obj
= (void *)bh
->b_data
;
234 if (et_type
== OCFS2_DINODE_EXTENT
) {
236 &((struct ocfs2_dinode
*)obj
)->id2
.i_list
;
237 et
->et_ops
= &ocfs2_dinode_et_ops
;
238 } else if (et_type
== OCFS2_XATTR_VALUE_EXTENT
) {
239 struct ocfs2_xattr_value_root
*xv
=
240 (struct ocfs2_xattr_value_root
*)obj
;
241 et
->et_root_el
= &xv
->xr_list
;
242 et
->et_ops
= &ocfs2_xattr_et_ops
;
243 } else if (et_type
== OCFS2_XATTR_TREE_EXTENT
) {
244 struct ocfs2_xattr_block
*xb
=
245 (struct ocfs2_xattr_block
*)obj
;
246 et
->et_root_el
= &xb
->xb_attrs
.xb_root
.xt_list
;
247 et
->et_ops
= &ocfs2_xattr_tree_et_ops
;
248 et
->et_max_leaf_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
249 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
);
253 static void ocfs2_put_extent_tree(struct ocfs2_extent_tree
*et
)
255 brelse(et
->et_root_bh
);
258 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
261 et
->et_ops
->eo_set_last_eb_blk(et
, new_last_eb_blk
);
264 static inline u64
ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
266 return et
->et_ops
->eo_get_last_eb_blk(et
);
269 static inline void ocfs2_et_update_clusters(struct inode
*inode
,
270 struct ocfs2_extent_tree
*et
,
273 et
->et_ops
->eo_update_clusters(inode
, et
, clusters
);
276 static inline int ocfs2_et_sanity_check(struct inode
*inode
,
277 struct ocfs2_extent_tree
*et
)
279 return et
->et_ops
->eo_sanity_check(inode
, et
);
282 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
);
283 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
284 struct ocfs2_extent_block
*eb
);
287 * Structures which describe a path through a btree, and functions to
290 * The idea here is to be as generic as possible with the tree
293 struct ocfs2_path_item
{
294 struct buffer_head
*bh
;
295 struct ocfs2_extent_list
*el
;
298 #define OCFS2_MAX_PATH_DEPTH 5
302 struct ocfs2_path_item p_node
[OCFS2_MAX_PATH_DEPTH
];
305 #define path_root_bh(_path) ((_path)->p_node[0].bh)
306 #define path_root_el(_path) ((_path)->p_node[0].el)
307 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
308 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
309 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
312 * Reset the actual path elements so that we can re-use the structure
313 * to build another path. Generally, this involves freeing the buffer
316 static void ocfs2_reinit_path(struct ocfs2_path
*path
, int keep_root
)
318 int i
, start
= 0, depth
= 0;
319 struct ocfs2_path_item
*node
;
324 for(i
= start
; i
< path_num_items(path
); i
++) {
325 node
= &path
->p_node
[i
];
333 * Tree depth may change during truncate, or insert. If we're
334 * keeping the root extent list, then make sure that our path
335 * structure reflects the proper depth.
338 depth
= le16_to_cpu(path_root_el(path
)->l_tree_depth
);
340 path
->p_tree_depth
= depth
;
343 static void ocfs2_free_path(struct ocfs2_path
*path
)
346 ocfs2_reinit_path(path
, 0);
352 * All the elements of src into dest. After this call, src could be freed
353 * without affecting dest.
355 * Both paths should have the same root. Any non-root elements of dest
358 static void ocfs2_cp_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
362 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
363 BUG_ON(path_root_el(dest
) != path_root_el(src
));
365 ocfs2_reinit_path(dest
, 1);
367 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
368 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
369 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
371 if (dest
->p_node
[i
].bh
)
372 get_bh(dest
->p_node
[i
].bh
);
377 * Make the *dest path the same as src and re-initialize src path to
380 static void ocfs2_mv_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
384 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
386 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
387 brelse(dest
->p_node
[i
].bh
);
389 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
390 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
392 src
->p_node
[i
].bh
= NULL
;
393 src
->p_node
[i
].el
= NULL
;
398 * Insert an extent block at given index.
400 * This will not take an additional reference on eb_bh.
402 static inline void ocfs2_path_insert_eb(struct ocfs2_path
*path
, int index
,
403 struct buffer_head
*eb_bh
)
405 struct ocfs2_extent_block
*eb
= (struct ocfs2_extent_block
*)eb_bh
->b_data
;
408 * Right now, no root bh is an extent block, so this helps
409 * catch code errors with dinode trees. The assertion can be
410 * safely removed if we ever need to insert extent block
411 * structures at the root.
415 path
->p_node
[index
].bh
= eb_bh
;
416 path
->p_node
[index
].el
= &eb
->h_list
;
419 static struct ocfs2_path
*ocfs2_new_path(struct buffer_head
*root_bh
,
420 struct ocfs2_extent_list
*root_el
)
422 struct ocfs2_path
*path
;
424 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) >= OCFS2_MAX_PATH_DEPTH
);
426 path
= kzalloc(sizeof(*path
), GFP_NOFS
);
428 path
->p_tree_depth
= le16_to_cpu(root_el
->l_tree_depth
);
430 path_root_bh(path
) = root_bh
;
431 path_root_el(path
) = root_el
;
438 * Convenience function to journal all components in a path.
440 static int ocfs2_journal_access_path(struct inode
*inode
, handle_t
*handle
,
441 struct ocfs2_path
*path
)
448 for(i
= 0; i
< path_num_items(path
); i
++) {
449 ret
= ocfs2_journal_access(handle
, inode
, path
->p_node
[i
].bh
,
450 OCFS2_JOURNAL_ACCESS_WRITE
);
462 * Return the index of the extent record which contains cluster #v_cluster.
463 * -1 is returned if it was not found.
465 * Should work fine on interior and exterior nodes.
467 int ocfs2_search_extent_list(struct ocfs2_extent_list
*el
, u32 v_cluster
)
471 struct ocfs2_extent_rec
*rec
;
472 u32 rec_end
, rec_start
, clusters
;
474 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
475 rec
= &el
->l_recs
[i
];
477 rec_start
= le32_to_cpu(rec
->e_cpos
);
478 clusters
= ocfs2_rec_clusters(el
, rec
);
480 rec_end
= rec_start
+ clusters
;
482 if (v_cluster
>= rec_start
&& v_cluster
< rec_end
) {
491 enum ocfs2_contig_type
{
500 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
501 * ocfs2_extent_contig only work properly against leaf nodes!
503 static int ocfs2_block_extent_contig(struct super_block
*sb
,
504 struct ocfs2_extent_rec
*ext
,
507 u64 blk_end
= le64_to_cpu(ext
->e_blkno
);
509 blk_end
+= ocfs2_clusters_to_blocks(sb
,
510 le16_to_cpu(ext
->e_leaf_clusters
));
512 return blkno
== blk_end
;
515 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec
*left
,
516 struct ocfs2_extent_rec
*right
)
520 left_range
= le32_to_cpu(left
->e_cpos
) +
521 le16_to_cpu(left
->e_leaf_clusters
);
523 return (left_range
== le32_to_cpu(right
->e_cpos
));
526 static enum ocfs2_contig_type
527 ocfs2_extent_contig(struct inode
*inode
,
528 struct ocfs2_extent_rec
*ext
,
529 struct ocfs2_extent_rec
*insert_rec
)
531 u64 blkno
= le64_to_cpu(insert_rec
->e_blkno
);
534 * Refuse to coalesce extent records with different flag
535 * fields - we don't want to mix unwritten extents with user
538 if (ext
->e_flags
!= insert_rec
->e_flags
)
541 if (ocfs2_extents_adjacent(ext
, insert_rec
) &&
542 ocfs2_block_extent_contig(inode
->i_sb
, ext
, blkno
))
545 blkno
= le64_to_cpu(ext
->e_blkno
);
546 if (ocfs2_extents_adjacent(insert_rec
, ext
) &&
547 ocfs2_block_extent_contig(inode
->i_sb
, insert_rec
, blkno
))
554 * NOTE: We can have pretty much any combination of contiguousness and
557 * The usefulness of APPEND_TAIL is more in that it lets us know that
558 * we'll have to update the path to that leaf.
560 enum ocfs2_append_type
{
565 enum ocfs2_split_type
{
571 struct ocfs2_insert_type
{
572 enum ocfs2_split_type ins_split
;
573 enum ocfs2_append_type ins_appending
;
574 enum ocfs2_contig_type ins_contig
;
575 int ins_contig_index
;
579 struct ocfs2_merge_ctxt
{
580 enum ocfs2_contig_type c_contig_type
;
581 int c_has_empty_extent
;
582 int c_split_covers_rec
;
586 * How many free extents have we got before we need more meta data?
588 int ocfs2_num_free_extents(struct ocfs2_super
*osb
,
590 struct buffer_head
*root_bh
,
591 enum ocfs2_extent_tree_type type
,
595 struct ocfs2_extent_list
*el
= NULL
;
596 struct ocfs2_extent_block
*eb
;
597 struct buffer_head
*eb_bh
= NULL
;
602 if (type
== OCFS2_DINODE_EXTENT
) {
603 struct ocfs2_dinode
*fe
=
604 (struct ocfs2_dinode
*)root_bh
->b_data
;
605 if (!OCFS2_IS_VALID_DINODE(fe
)) {
606 OCFS2_RO_ON_INVALID_DINODE(inode
->i_sb
, fe
);
611 if (fe
->i_last_eb_blk
)
612 last_eb_blk
= le64_to_cpu(fe
->i_last_eb_blk
);
613 el
= &fe
->id2
.i_list
;
614 } else if (type
== OCFS2_XATTR_VALUE_EXTENT
) {
615 struct ocfs2_xattr_value_root
*xv
=
616 (struct ocfs2_xattr_value_root
*) obj
;
618 last_eb_blk
= le64_to_cpu(xv
->xr_last_eb_blk
);
620 } else if (type
== OCFS2_XATTR_TREE_EXTENT
) {
621 struct ocfs2_xattr_block
*xb
=
622 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
624 last_eb_blk
= le64_to_cpu(xb
->xb_attrs
.xb_root
.xt_last_eb_blk
);
625 el
= &xb
->xb_attrs
.xb_root
.xt_list
;
629 retval
= ocfs2_read_block(osb
, last_eb_blk
,
630 &eb_bh
, OCFS2_BH_CACHED
, inode
);
635 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
639 BUG_ON(el
->l_tree_depth
!= 0);
641 retval
= le16_to_cpu(el
->l_count
) - le16_to_cpu(el
->l_next_free_rec
);
650 /* expects array to already be allocated
652 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
655 static int ocfs2_create_new_meta_bhs(struct ocfs2_super
*osb
,
659 struct ocfs2_alloc_context
*meta_ac
,
660 struct buffer_head
*bhs
[])
662 int count
, status
, i
;
663 u16 suballoc_bit_start
;
666 struct ocfs2_extent_block
*eb
;
671 while (count
< wanted
) {
672 status
= ocfs2_claim_metadata(osb
,
684 for(i
= count
; i
< (num_got
+ count
); i
++) {
685 bhs
[i
] = sb_getblk(osb
->sb
, first_blkno
);
686 if (bhs
[i
] == NULL
) {
691 ocfs2_set_new_buffer_uptodate(inode
, bhs
[i
]);
693 status
= ocfs2_journal_access(handle
, inode
, bhs
[i
],
694 OCFS2_JOURNAL_ACCESS_CREATE
);
700 memset(bhs
[i
]->b_data
, 0, osb
->sb
->s_blocksize
);
701 eb
= (struct ocfs2_extent_block
*) bhs
[i
]->b_data
;
702 /* Ok, setup the minimal stuff here. */
703 strcpy(eb
->h_signature
, OCFS2_EXTENT_BLOCK_SIGNATURE
);
704 eb
->h_blkno
= cpu_to_le64(first_blkno
);
705 eb
->h_fs_generation
= cpu_to_le32(osb
->fs_generation
);
706 eb
->h_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
707 eb
->h_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
709 cpu_to_le16(ocfs2_extent_recs_per_eb(osb
->sb
));
711 suballoc_bit_start
++;
714 /* We'll also be dirtied by the caller, so
715 * this isn't absolutely necessary. */
716 status
= ocfs2_journal_dirty(handle
, bhs
[i
]);
729 for(i
= 0; i
< wanted
; i
++) {
740 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
742 * Returns the sum of the rightmost extent rec logical offset and
745 * ocfs2_add_branch() uses this to determine what logical cluster
746 * value should be populated into the leftmost new branch records.
748 * ocfs2_shift_tree_depth() uses this to determine the # clusters
749 * value for the new topmost tree record.
751 static inline u32
ocfs2_sum_rightmost_rec(struct ocfs2_extent_list
*el
)
755 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
757 return le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
758 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
762 * Add an entire tree branch to our inode. eb_bh is the extent block
763 * to start at, if we don't want to start the branch at the dinode
766 * last_eb_bh is required as we have to update it's next_leaf pointer
767 * for the new last extent block.
769 * the new branch will be 'empty' in the sense that every block will
770 * contain a single record with cluster count == 0.
772 static int ocfs2_add_branch(struct ocfs2_super
*osb
,
775 struct ocfs2_extent_tree
*et
,
776 struct buffer_head
*eb_bh
,
777 struct buffer_head
**last_eb_bh
,
778 struct ocfs2_alloc_context
*meta_ac
)
780 int status
, new_blocks
, i
;
781 u64 next_blkno
, new_last_eb_blk
;
782 struct buffer_head
*bh
;
783 struct buffer_head
**new_eb_bhs
= NULL
;
784 struct ocfs2_extent_block
*eb
;
785 struct ocfs2_extent_list
*eb_el
;
786 struct ocfs2_extent_list
*el
;
791 BUG_ON(!last_eb_bh
|| !*last_eb_bh
);
794 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
799 /* we never add a branch to a leaf. */
800 BUG_ON(!el
->l_tree_depth
);
802 new_blocks
= le16_to_cpu(el
->l_tree_depth
);
804 /* allocate the number of new eb blocks we need */
805 new_eb_bhs
= kcalloc(new_blocks
, sizeof(struct buffer_head
*),
813 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, new_blocks
,
814 meta_ac
, new_eb_bhs
);
820 eb
= (struct ocfs2_extent_block
*)(*last_eb_bh
)->b_data
;
821 new_cpos
= ocfs2_sum_rightmost_rec(&eb
->h_list
);
823 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
824 * linked with the rest of the tree.
825 * conversly, new_eb_bhs[0] is the new bottommost leaf.
827 * when we leave the loop, new_last_eb_blk will point to the
828 * newest leaf, and next_blkno will point to the topmost extent
830 next_blkno
= new_last_eb_blk
= 0;
831 for(i
= 0; i
< new_blocks
; i
++) {
833 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
834 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
835 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
841 status
= ocfs2_journal_access(handle
, inode
, bh
,
842 OCFS2_JOURNAL_ACCESS_CREATE
);
848 eb
->h_next_leaf_blk
= 0;
849 eb_el
->l_tree_depth
= cpu_to_le16(i
);
850 eb_el
->l_next_free_rec
= cpu_to_le16(1);
852 * This actually counts as an empty extent as
855 eb_el
->l_recs
[0].e_cpos
= cpu_to_le32(new_cpos
);
856 eb_el
->l_recs
[0].e_blkno
= cpu_to_le64(next_blkno
);
858 * eb_el isn't always an interior node, but even leaf
859 * nodes want a zero'd flags and reserved field so
860 * this gets the whole 32 bits regardless of use.
862 eb_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(0);
863 if (!eb_el
->l_tree_depth
)
864 new_last_eb_blk
= le64_to_cpu(eb
->h_blkno
);
866 status
= ocfs2_journal_dirty(handle
, bh
);
872 next_blkno
= le64_to_cpu(eb
->h_blkno
);
875 /* This is a bit hairy. We want to update up to three blocks
876 * here without leaving any of them in an inconsistent state
877 * in case of error. We don't have to worry about
878 * journal_dirty erroring as it won't unless we've aborted the
879 * handle (in which case we would never be here) so reserving
880 * the write with journal_access is all we need to do. */
881 status
= ocfs2_journal_access(handle
, inode
, *last_eb_bh
,
882 OCFS2_JOURNAL_ACCESS_WRITE
);
887 status
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
888 OCFS2_JOURNAL_ACCESS_WRITE
);
894 status
= ocfs2_journal_access(handle
, inode
, eb_bh
,
895 OCFS2_JOURNAL_ACCESS_WRITE
);
902 /* Link the new branch into the rest of the tree (el will
903 * either be on the root_bh, or the extent block passed in. */
904 i
= le16_to_cpu(el
->l_next_free_rec
);
905 el
->l_recs
[i
].e_blkno
= cpu_to_le64(next_blkno
);
906 el
->l_recs
[i
].e_cpos
= cpu_to_le32(new_cpos
);
907 el
->l_recs
[i
].e_int_clusters
= 0;
908 le16_add_cpu(&el
->l_next_free_rec
, 1);
910 /* fe needs a new last extent block pointer, as does the
911 * next_leaf on the previously last-extent-block. */
912 ocfs2_et_set_last_eb_blk(et
, new_last_eb_blk
);
914 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
915 eb
->h_next_leaf_blk
= cpu_to_le64(new_last_eb_blk
);
917 status
= ocfs2_journal_dirty(handle
, *last_eb_bh
);
920 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
924 status
= ocfs2_journal_dirty(handle
, eb_bh
);
930 * Some callers want to track the rightmost leaf so pass it
934 get_bh(new_eb_bhs
[0]);
935 *last_eb_bh
= new_eb_bhs
[0];
940 for (i
= 0; i
< new_blocks
; i
++)
942 brelse(new_eb_bhs
[i
]);
951 * adds another level to the allocation tree.
952 * returns back the new extent block so you can add a branch to it
955 static int ocfs2_shift_tree_depth(struct ocfs2_super
*osb
,
958 struct ocfs2_extent_tree
*et
,
959 struct ocfs2_alloc_context
*meta_ac
,
960 struct buffer_head
**ret_new_eb_bh
)
964 struct buffer_head
*new_eb_bh
= NULL
;
965 struct ocfs2_extent_block
*eb
;
966 struct ocfs2_extent_list
*root_el
;
967 struct ocfs2_extent_list
*eb_el
;
971 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, 1, meta_ac
,
978 eb
= (struct ocfs2_extent_block
*) new_eb_bh
->b_data
;
979 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
980 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
986 root_el
= et
->et_root_el
;
988 status
= ocfs2_journal_access(handle
, inode
, new_eb_bh
,
989 OCFS2_JOURNAL_ACCESS_CREATE
);
995 /* copy the root extent list data into the new extent block */
996 eb_el
->l_tree_depth
= root_el
->l_tree_depth
;
997 eb_el
->l_next_free_rec
= root_el
->l_next_free_rec
;
998 for (i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
999 eb_el
->l_recs
[i
] = root_el
->l_recs
[i
];
1001 status
= ocfs2_journal_dirty(handle
, new_eb_bh
);
1007 status
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
1008 OCFS2_JOURNAL_ACCESS_WRITE
);
1014 new_clusters
= ocfs2_sum_rightmost_rec(eb_el
);
1016 /* update root_bh now */
1017 le16_add_cpu(&root_el
->l_tree_depth
, 1);
1018 root_el
->l_recs
[0].e_cpos
= 0;
1019 root_el
->l_recs
[0].e_blkno
= eb
->h_blkno
;
1020 root_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(new_clusters
);
1021 for (i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1022 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
1023 root_el
->l_next_free_rec
= cpu_to_le16(1);
1025 /* If this is our 1st tree depth shift, then last_eb_blk
1026 * becomes the allocated extent block */
1027 if (root_el
->l_tree_depth
== cpu_to_le16(1))
1028 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
1030 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1036 *ret_new_eb_bh
= new_eb_bh
;
1048 * Should only be called when there is no space left in any of the
1049 * leaf nodes. What we want to do is find the lowest tree depth
1050 * non-leaf extent block with room for new records. There are three
1051 * valid results of this search:
1053 * 1) a lowest extent block is found, then we pass it back in
1054 * *lowest_eb_bh and return '0'
1056 * 2) the search fails to find anything, but the root_el has room. We
1057 * pass NULL back in *lowest_eb_bh, but still return '0'
1059 * 3) the search fails to find anything AND the root_el is full, in
1060 * which case we return > 0
1062 * return status < 0 indicates an error.
1064 static int ocfs2_find_branch_target(struct ocfs2_super
*osb
,
1065 struct inode
*inode
,
1066 struct ocfs2_extent_tree
*et
,
1067 struct buffer_head
**target_bh
)
1071 struct ocfs2_extent_block
*eb
;
1072 struct ocfs2_extent_list
*el
;
1073 struct buffer_head
*bh
= NULL
;
1074 struct buffer_head
*lowest_bh
= NULL
;
1080 el
= et
->et_root_el
;
1082 while(le16_to_cpu(el
->l_tree_depth
) > 1) {
1083 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1084 ocfs2_error(inode
->i_sb
, "Dinode %llu has empty "
1085 "extent list (next_free_rec == 0)",
1086 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1090 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1091 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1093 ocfs2_error(inode
->i_sb
, "Dinode %llu has extent "
1094 "list where extent # %d has no physical "
1096 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, i
);
1106 status
= ocfs2_read_block(osb
, blkno
, &bh
, OCFS2_BH_CACHED
,
1113 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1114 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
1115 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
1121 if (le16_to_cpu(el
->l_next_free_rec
) <
1122 le16_to_cpu(el
->l_count
)) {
1130 /* If we didn't find one and the fe doesn't have any room,
1131 * then return '1' */
1132 el
= et
->et_root_el
;
1133 if (!lowest_bh
&& (el
->l_next_free_rec
== el
->l_count
))
1136 *target_bh
= lowest_bh
;
1146 * Grow a b-tree so that it has more records.
1148 * We might shift the tree depth in which case existing paths should
1149 * be considered invalid.
1151 * Tree depth after the grow is returned via *final_depth.
1153 * *last_eb_bh will be updated by ocfs2_add_branch().
1155 static int ocfs2_grow_tree(struct inode
*inode
, handle_t
*handle
,
1156 struct ocfs2_extent_tree
*et
, int *final_depth
,
1157 struct buffer_head
**last_eb_bh
,
1158 struct ocfs2_alloc_context
*meta_ac
)
1161 struct ocfs2_extent_list
*el
= et
->et_root_el
;
1162 int depth
= le16_to_cpu(el
->l_tree_depth
);
1163 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1164 struct buffer_head
*bh
= NULL
;
1166 BUG_ON(meta_ac
== NULL
);
1168 shift
= ocfs2_find_branch_target(osb
, inode
, et
, &bh
);
1175 /* We traveled all the way to the bottom of the allocation tree
1176 * and didn't find room for any more extents - we need to add
1177 * another tree level */
1180 mlog(0, "need to shift tree depth (current = %d)\n", depth
);
1182 /* ocfs2_shift_tree_depth will return us a buffer with
1183 * the new extent block (so we can pass that to
1184 * ocfs2_add_branch). */
1185 ret
= ocfs2_shift_tree_depth(osb
, handle
, inode
, et
,
1194 * Special case: we have room now if we shifted from
1195 * tree_depth 0, so no more work needs to be done.
1197 * We won't be calling add_branch, so pass
1198 * back *last_eb_bh as the new leaf. At depth
1199 * zero, it should always be null so there's
1200 * no reason to brelse.
1202 BUG_ON(*last_eb_bh
);
1209 /* call ocfs2_add_branch to add the final part of the tree with
1211 mlog(0, "add branch. bh = %p\n", bh
);
1212 ret
= ocfs2_add_branch(osb
, handle
, inode
, et
, bh
, last_eb_bh
,
1221 *final_depth
= depth
;
1227 * This function will discard the rightmost extent record.
1229 static void ocfs2_shift_records_right(struct ocfs2_extent_list
*el
)
1231 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1232 int count
= le16_to_cpu(el
->l_count
);
1233 unsigned int num_bytes
;
1236 /* This will cause us to go off the end of our extent list. */
1237 BUG_ON(next_free
>= count
);
1239 num_bytes
= sizeof(struct ocfs2_extent_rec
) * next_free
;
1241 memmove(&el
->l_recs
[1], &el
->l_recs
[0], num_bytes
);
1244 static void ocfs2_rotate_leaf(struct ocfs2_extent_list
*el
,
1245 struct ocfs2_extent_rec
*insert_rec
)
1247 int i
, insert_index
, next_free
, has_empty
, num_bytes
;
1248 u32 insert_cpos
= le32_to_cpu(insert_rec
->e_cpos
);
1249 struct ocfs2_extent_rec
*rec
;
1251 next_free
= le16_to_cpu(el
->l_next_free_rec
);
1252 has_empty
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
1256 /* The tree code before us didn't allow enough room in the leaf. */
1257 BUG_ON(el
->l_next_free_rec
== el
->l_count
&& !has_empty
);
1260 * The easiest way to approach this is to just remove the
1261 * empty extent and temporarily decrement next_free.
1265 * If next_free was 1 (only an empty extent), this
1266 * loop won't execute, which is fine. We still want
1267 * the decrement above to happen.
1269 for(i
= 0; i
< (next_free
- 1); i
++)
1270 el
->l_recs
[i
] = el
->l_recs
[i
+1];
1276 * Figure out what the new record index should be.
1278 for(i
= 0; i
< next_free
; i
++) {
1279 rec
= &el
->l_recs
[i
];
1281 if (insert_cpos
< le32_to_cpu(rec
->e_cpos
))
1286 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1287 insert_cpos
, insert_index
, has_empty
, next_free
, le16_to_cpu(el
->l_count
));
1289 BUG_ON(insert_index
< 0);
1290 BUG_ON(insert_index
>= le16_to_cpu(el
->l_count
));
1291 BUG_ON(insert_index
> next_free
);
1294 * No need to memmove if we're just adding to the tail.
1296 if (insert_index
!= next_free
) {
1297 BUG_ON(next_free
>= le16_to_cpu(el
->l_count
));
1299 num_bytes
= next_free
- insert_index
;
1300 num_bytes
*= sizeof(struct ocfs2_extent_rec
);
1301 memmove(&el
->l_recs
[insert_index
+ 1],
1302 &el
->l_recs
[insert_index
],
1307 * Either we had an empty extent, and need to re-increment or
1308 * there was no empty extent on a non full rightmost leaf node,
1309 * in which case we still need to increment.
1312 el
->l_next_free_rec
= cpu_to_le16(next_free
);
1314 * Make sure none of the math above just messed up our tree.
1316 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) > le16_to_cpu(el
->l_count
));
1318 el
->l_recs
[insert_index
] = *insert_rec
;
1322 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list
*el
)
1324 int size
, num_recs
= le16_to_cpu(el
->l_next_free_rec
);
1326 BUG_ON(num_recs
== 0);
1328 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
1330 size
= num_recs
* sizeof(struct ocfs2_extent_rec
);
1331 memmove(&el
->l_recs
[0], &el
->l_recs
[1], size
);
1332 memset(&el
->l_recs
[num_recs
], 0,
1333 sizeof(struct ocfs2_extent_rec
));
1334 el
->l_next_free_rec
= cpu_to_le16(num_recs
);
1339 * Create an empty extent record .
1341 * l_next_free_rec may be updated.
1343 * If an empty extent already exists do nothing.
1345 static void ocfs2_create_empty_extent(struct ocfs2_extent_list
*el
)
1347 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1349 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
1354 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
1357 mlog_bug_on_msg(el
->l_count
== el
->l_next_free_rec
,
1358 "Asked to create an empty extent in a full list:\n"
1359 "count = %u, tree depth = %u",
1360 le16_to_cpu(el
->l_count
),
1361 le16_to_cpu(el
->l_tree_depth
));
1363 ocfs2_shift_records_right(el
);
1366 le16_add_cpu(&el
->l_next_free_rec
, 1);
1367 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1371 * For a rotation which involves two leaf nodes, the "root node" is
1372 * the lowest level tree node which contains a path to both leafs. This
1373 * resulting set of information can be used to form a complete "subtree"
1375 * This function is passed two full paths from the dinode down to a
1376 * pair of adjacent leaves. It's task is to figure out which path
1377 * index contains the subtree root - this can be the root index itself
1378 * in a worst-case rotation.
1380 * The array index of the subtree root is passed back.
1382 static int ocfs2_find_subtree_root(struct inode
*inode
,
1383 struct ocfs2_path
*left
,
1384 struct ocfs2_path
*right
)
1389 * Check that the caller passed in two paths from the same tree.
1391 BUG_ON(path_root_bh(left
) != path_root_bh(right
));
1397 * The caller didn't pass two adjacent paths.
1399 mlog_bug_on_msg(i
> left
->p_tree_depth
,
1400 "Inode %lu, left depth %u, right depth %u\n"
1401 "left leaf blk %llu, right leaf blk %llu\n",
1402 inode
->i_ino
, left
->p_tree_depth
,
1403 right
->p_tree_depth
,
1404 (unsigned long long)path_leaf_bh(left
)->b_blocknr
,
1405 (unsigned long long)path_leaf_bh(right
)->b_blocknr
);
1406 } while (left
->p_node
[i
].bh
->b_blocknr
==
1407 right
->p_node
[i
].bh
->b_blocknr
);
1412 typedef void (path_insert_t
)(void *, struct buffer_head
*);
1415 * Traverse a btree path in search of cpos, starting at root_el.
1417 * This code can be called with a cpos larger than the tree, in which
1418 * case it will return the rightmost path.
1420 static int __ocfs2_find_path(struct inode
*inode
,
1421 struct ocfs2_extent_list
*root_el
, u32 cpos
,
1422 path_insert_t
*func
, void *data
)
1427 struct buffer_head
*bh
= NULL
;
1428 struct ocfs2_extent_block
*eb
;
1429 struct ocfs2_extent_list
*el
;
1430 struct ocfs2_extent_rec
*rec
;
1431 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1434 while (el
->l_tree_depth
) {
1435 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1436 ocfs2_error(inode
->i_sb
,
1437 "Inode %llu has empty extent list at "
1439 (unsigned long long)oi
->ip_blkno
,
1440 le16_to_cpu(el
->l_tree_depth
));
1446 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
) - 1; i
++) {
1447 rec
= &el
->l_recs
[i
];
1450 * In the case that cpos is off the allocation
1451 * tree, this should just wind up returning the
1454 range
= le32_to_cpu(rec
->e_cpos
) +
1455 ocfs2_rec_clusters(el
, rec
);
1456 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
1460 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1462 ocfs2_error(inode
->i_sb
,
1463 "Inode %llu has bad blkno in extent list "
1464 "at depth %u (index %d)\n",
1465 (unsigned long long)oi
->ip_blkno
,
1466 le16_to_cpu(el
->l_tree_depth
), i
);
1473 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
), blkno
,
1474 &bh
, OCFS2_BH_CACHED
, inode
);
1480 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1482 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
1483 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
1488 if (le16_to_cpu(el
->l_next_free_rec
) >
1489 le16_to_cpu(el
->l_count
)) {
1490 ocfs2_error(inode
->i_sb
,
1491 "Inode %llu has bad count in extent list "
1492 "at block %llu (next free=%u, count=%u)\n",
1493 (unsigned long long)oi
->ip_blkno
,
1494 (unsigned long long)bh
->b_blocknr
,
1495 le16_to_cpu(el
->l_next_free_rec
),
1496 le16_to_cpu(el
->l_count
));
1507 * Catch any trailing bh that the loop didn't handle.
1515 * Given an initialized path (that is, it has a valid root extent
1516 * list), this function will traverse the btree in search of the path
1517 * which would contain cpos.
1519 * The path traveled is recorded in the path structure.
1521 * Note that this will not do any comparisons on leaf node extent
1522 * records, so it will work fine in the case that we just added a tree
1525 struct find_path_data
{
1527 struct ocfs2_path
*path
;
1529 static void find_path_ins(void *data
, struct buffer_head
*bh
)
1531 struct find_path_data
*fp
= data
;
1534 ocfs2_path_insert_eb(fp
->path
, fp
->index
, bh
);
1537 static int ocfs2_find_path(struct inode
*inode
, struct ocfs2_path
*path
,
1540 struct find_path_data data
;
1544 return __ocfs2_find_path(inode
, path_root_el(path
), cpos
,
1545 find_path_ins
, &data
);
1548 static void find_leaf_ins(void *data
, struct buffer_head
*bh
)
1550 struct ocfs2_extent_block
*eb
=(struct ocfs2_extent_block
*)bh
->b_data
;
1551 struct ocfs2_extent_list
*el
= &eb
->h_list
;
1552 struct buffer_head
**ret
= data
;
1554 /* We want to retain only the leaf block. */
1555 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
1561 * Find the leaf block in the tree which would contain cpos. No
1562 * checking of the actual leaf is done.
1564 * Some paths want to call this instead of allocating a path structure
1565 * and calling ocfs2_find_path().
1567 * This function doesn't handle non btree extent lists.
1569 int ocfs2_find_leaf(struct inode
*inode
, struct ocfs2_extent_list
*root_el
,
1570 u32 cpos
, struct buffer_head
**leaf_bh
)
1573 struct buffer_head
*bh
= NULL
;
1575 ret
= __ocfs2_find_path(inode
, root_el
, cpos
, find_leaf_ins
, &bh
);
1587 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1589 * Basically, we've moved stuff around at the bottom of the tree and
1590 * we need to fix up the extent records above the changes to reflect
1593 * left_rec: the record on the left.
1594 * left_child_el: is the child list pointed to by left_rec
1595 * right_rec: the record to the right of left_rec
1596 * right_child_el: is the child list pointed to by right_rec
1598 * By definition, this only works on interior nodes.
1600 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec
*left_rec
,
1601 struct ocfs2_extent_list
*left_child_el
,
1602 struct ocfs2_extent_rec
*right_rec
,
1603 struct ocfs2_extent_list
*right_child_el
)
1605 u32 left_clusters
, right_end
;
1608 * Interior nodes never have holes. Their cpos is the cpos of
1609 * the leftmost record in their child list. Their cluster
1610 * count covers the full theoretical range of their child list
1611 * - the range between their cpos and the cpos of the record
1612 * immediately to their right.
1614 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[0].e_cpos
);
1615 if (ocfs2_is_empty_extent(&right_child_el
->l_recs
[0])) {
1616 BUG_ON(le16_to_cpu(right_child_el
->l_next_free_rec
) <= 1);
1617 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[1].e_cpos
);
1619 left_clusters
-= le32_to_cpu(left_rec
->e_cpos
);
1620 left_rec
->e_int_clusters
= cpu_to_le32(left_clusters
);
1623 * Calculate the rightmost cluster count boundary before
1624 * moving cpos - we will need to adjust clusters after
1625 * updating e_cpos to keep the same highest cluster count.
1627 right_end
= le32_to_cpu(right_rec
->e_cpos
);
1628 right_end
+= le32_to_cpu(right_rec
->e_int_clusters
);
1630 right_rec
->e_cpos
= left_rec
->e_cpos
;
1631 le32_add_cpu(&right_rec
->e_cpos
, left_clusters
);
1633 right_end
-= le32_to_cpu(right_rec
->e_cpos
);
1634 right_rec
->e_int_clusters
= cpu_to_le32(right_end
);
1638 * Adjust the adjacent root node records involved in a
1639 * rotation. left_el_blkno is passed in as a key so that we can easily
1640 * find it's index in the root list.
1642 static void ocfs2_adjust_root_records(struct ocfs2_extent_list
*root_el
,
1643 struct ocfs2_extent_list
*left_el
,
1644 struct ocfs2_extent_list
*right_el
,
1649 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) <=
1650 le16_to_cpu(left_el
->l_tree_depth
));
1652 for(i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
) - 1; i
++) {
1653 if (le64_to_cpu(root_el
->l_recs
[i
].e_blkno
) == left_el_blkno
)
1658 * The path walking code should have never returned a root and
1659 * two paths which are not adjacent.
1661 BUG_ON(i
>= (le16_to_cpu(root_el
->l_next_free_rec
) - 1));
1663 ocfs2_adjust_adjacent_records(&root_el
->l_recs
[i
], left_el
,
1664 &root_el
->l_recs
[i
+ 1], right_el
);
1668 * We've changed a leaf block (in right_path) and need to reflect that
1669 * change back up the subtree.
1671 * This happens in multiple places:
1672 * - When we've moved an extent record from the left path leaf to the right
1673 * path leaf to make room for an empty extent in the left path leaf.
1674 * - When our insert into the right path leaf is at the leftmost edge
1675 * and requires an update of the path immediately to it's left. This
1676 * can occur at the end of some types of rotation and appending inserts.
1677 * - When we've adjusted the last extent record in the left path leaf and the
1678 * 1st extent record in the right path leaf during cross extent block merge.
1680 static void ocfs2_complete_edge_insert(struct inode
*inode
, handle_t
*handle
,
1681 struct ocfs2_path
*left_path
,
1682 struct ocfs2_path
*right_path
,
1686 struct ocfs2_extent_list
*el
, *left_el
, *right_el
;
1687 struct ocfs2_extent_rec
*left_rec
, *right_rec
;
1688 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
1691 * Update the counts and position values within all the
1692 * interior nodes to reflect the leaf rotation we just did.
1694 * The root node is handled below the loop.
1696 * We begin the loop with right_el and left_el pointing to the
1697 * leaf lists and work our way up.
1699 * NOTE: within this loop, left_el and right_el always refer
1700 * to the *child* lists.
1702 left_el
= path_leaf_el(left_path
);
1703 right_el
= path_leaf_el(right_path
);
1704 for(i
= left_path
->p_tree_depth
- 1; i
> subtree_index
; i
--) {
1705 mlog(0, "Adjust records at index %u\n", i
);
1708 * One nice property of knowing that all of these
1709 * nodes are below the root is that we only deal with
1710 * the leftmost right node record and the rightmost
1713 el
= left_path
->p_node
[i
].el
;
1714 idx
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
1715 left_rec
= &el
->l_recs
[idx
];
1717 el
= right_path
->p_node
[i
].el
;
1718 right_rec
= &el
->l_recs
[0];
1720 ocfs2_adjust_adjacent_records(left_rec
, left_el
, right_rec
,
1723 ret
= ocfs2_journal_dirty(handle
, left_path
->p_node
[i
].bh
);
1727 ret
= ocfs2_journal_dirty(handle
, right_path
->p_node
[i
].bh
);
1732 * Setup our list pointers now so that the current
1733 * parents become children in the next iteration.
1735 left_el
= left_path
->p_node
[i
].el
;
1736 right_el
= right_path
->p_node
[i
].el
;
1740 * At the root node, adjust the two adjacent records which
1741 * begin our path to the leaves.
1744 el
= left_path
->p_node
[subtree_index
].el
;
1745 left_el
= left_path
->p_node
[subtree_index
+ 1].el
;
1746 right_el
= right_path
->p_node
[subtree_index
+ 1].el
;
1748 ocfs2_adjust_root_records(el
, left_el
, right_el
,
1749 left_path
->p_node
[subtree_index
+ 1].bh
->b_blocknr
);
1751 root_bh
= left_path
->p_node
[subtree_index
].bh
;
1753 ret
= ocfs2_journal_dirty(handle
, root_bh
);
1758 static int ocfs2_rotate_subtree_right(struct inode
*inode
,
1760 struct ocfs2_path
*left_path
,
1761 struct ocfs2_path
*right_path
,
1765 struct buffer_head
*right_leaf_bh
;
1766 struct buffer_head
*left_leaf_bh
= NULL
;
1767 struct buffer_head
*root_bh
;
1768 struct ocfs2_extent_list
*right_el
, *left_el
;
1769 struct ocfs2_extent_rec move_rec
;
1771 left_leaf_bh
= path_leaf_bh(left_path
);
1772 left_el
= path_leaf_el(left_path
);
1774 if (left_el
->l_next_free_rec
!= left_el
->l_count
) {
1775 ocfs2_error(inode
->i_sb
,
1776 "Inode %llu has non-full interior leaf node %llu"
1778 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1779 (unsigned long long)left_leaf_bh
->b_blocknr
,
1780 le16_to_cpu(left_el
->l_next_free_rec
));
1785 * This extent block may already have an empty record, so we
1786 * return early if so.
1788 if (ocfs2_is_empty_extent(&left_el
->l_recs
[0]))
1791 root_bh
= left_path
->p_node
[subtree_index
].bh
;
1792 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
1794 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
1795 OCFS2_JOURNAL_ACCESS_WRITE
);
1801 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
1802 ret
= ocfs2_journal_access(handle
, inode
,
1803 right_path
->p_node
[i
].bh
,
1804 OCFS2_JOURNAL_ACCESS_WRITE
);
1810 ret
= ocfs2_journal_access(handle
, inode
,
1811 left_path
->p_node
[i
].bh
,
1812 OCFS2_JOURNAL_ACCESS_WRITE
);
1819 right_leaf_bh
= path_leaf_bh(right_path
);
1820 right_el
= path_leaf_el(right_path
);
1822 /* This is a code error, not a disk corruption. */
1823 mlog_bug_on_msg(!right_el
->l_next_free_rec
, "Inode %llu: Rotate fails "
1824 "because rightmost leaf block %llu is empty\n",
1825 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1826 (unsigned long long)right_leaf_bh
->b_blocknr
);
1828 ocfs2_create_empty_extent(right_el
);
1830 ret
= ocfs2_journal_dirty(handle
, right_leaf_bh
);
1836 /* Do the copy now. */
1837 i
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
1838 move_rec
= left_el
->l_recs
[i
];
1839 right_el
->l_recs
[0] = move_rec
;
1842 * Clear out the record we just copied and shift everything
1843 * over, leaving an empty extent in the left leaf.
1845 * We temporarily subtract from next_free_rec so that the
1846 * shift will lose the tail record (which is now defunct).
1848 le16_add_cpu(&left_el
->l_next_free_rec
, -1);
1849 ocfs2_shift_records_right(left_el
);
1850 memset(&left_el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1851 le16_add_cpu(&left_el
->l_next_free_rec
, 1);
1853 ret
= ocfs2_journal_dirty(handle
, left_leaf_bh
);
1859 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
1867 * Given a full path, determine what cpos value would return us a path
1868 * containing the leaf immediately to the left of the current one.
1870 * Will return zero if the path passed in is already the leftmost path.
1872 static int ocfs2_find_cpos_for_left_leaf(struct super_block
*sb
,
1873 struct ocfs2_path
*path
, u32
*cpos
)
1877 struct ocfs2_extent_list
*el
;
1879 BUG_ON(path
->p_tree_depth
== 0);
1883 blkno
= path_leaf_bh(path
)->b_blocknr
;
1885 /* Start at the tree node just above the leaf and work our way up. */
1886 i
= path
->p_tree_depth
- 1;
1888 el
= path
->p_node
[i
].el
;
1891 * Find the extent record just before the one in our
1894 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
1895 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
1899 * We've determined that the
1900 * path specified is already
1901 * the leftmost one - return a
1907 * The leftmost record points to our
1908 * leaf - we need to travel up the
1914 *cpos
= le32_to_cpu(el
->l_recs
[j
- 1].e_cpos
);
1915 *cpos
= *cpos
+ ocfs2_rec_clusters(el
,
1916 &el
->l_recs
[j
- 1]);
1923 * If we got here, we never found a valid node where
1924 * the tree indicated one should be.
1927 "Invalid extent tree at extent block %llu\n",
1928 (unsigned long long)blkno
);
1933 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
1942 * Extend the transaction by enough credits to complete the rotation,
1943 * and still leave at least the original number of credits allocated
1944 * to this transaction.
1946 static int ocfs2_extend_rotate_transaction(handle_t
*handle
, int subtree_depth
,
1948 struct ocfs2_path
*path
)
1950 int credits
= (path
->p_tree_depth
- subtree_depth
) * 2 + 1 + op_credits
;
1952 if (handle
->h_buffer_credits
< credits
)
1953 return ocfs2_extend_trans(handle
, credits
);
1959 * Trap the case where we're inserting into the theoretical range past
1960 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1961 * whose cpos is less than ours into the right leaf.
1963 * It's only necessary to look at the rightmost record of the left
1964 * leaf because the logic that calls us should ensure that the
1965 * theoretical ranges in the path components above the leaves are
1968 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path
*left_path
,
1971 struct ocfs2_extent_list
*left_el
;
1972 struct ocfs2_extent_rec
*rec
;
1975 left_el
= path_leaf_el(left_path
);
1976 next_free
= le16_to_cpu(left_el
->l_next_free_rec
);
1977 rec
= &left_el
->l_recs
[next_free
- 1];
1979 if (insert_cpos
> le32_to_cpu(rec
->e_cpos
))
1984 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list
*el
, u32 cpos
)
1986 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1988 struct ocfs2_extent_rec
*rec
;
1993 rec
= &el
->l_recs
[0];
1994 if (ocfs2_is_empty_extent(rec
)) {
1998 rec
= &el
->l_recs
[1];
2001 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2002 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
2008 * Rotate all the records in a btree right one record, starting at insert_cpos.
2010 * The path to the rightmost leaf should be passed in.
2012 * The array is assumed to be large enough to hold an entire path (tree depth).
2014 * Upon succesful return from this function:
2016 * - The 'right_path' array will contain a path to the leaf block
2017 * whose range contains e_cpos.
2018 * - That leaf block will have a single empty extent in list index 0.
2019 * - In the case that the rotation requires a post-insert update,
2020 * *ret_left_path will contain a valid path which can be passed to
2021 * ocfs2_insert_path().
2023 static int ocfs2_rotate_tree_right(struct inode
*inode
,
2025 enum ocfs2_split_type split
,
2027 struct ocfs2_path
*right_path
,
2028 struct ocfs2_path
**ret_left_path
)
2030 int ret
, start
, orig_credits
= handle
->h_buffer_credits
;
2032 struct ocfs2_path
*left_path
= NULL
;
2034 *ret_left_path
= NULL
;
2036 left_path
= ocfs2_new_path(path_root_bh(right_path
),
2037 path_root_el(right_path
));
2044 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
, &cpos
);
2050 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos
, cpos
);
2053 * What we want to do here is:
2055 * 1) Start with the rightmost path.
2057 * 2) Determine a path to the leaf block directly to the left
2060 * 3) Determine the 'subtree root' - the lowest level tree node
2061 * which contains a path to both leaves.
2063 * 4) Rotate the subtree.
2065 * 5) Find the next subtree by considering the left path to be
2066 * the new right path.
2068 * The check at the top of this while loop also accepts
2069 * insert_cpos == cpos because cpos is only a _theoretical_
2070 * value to get us the left path - insert_cpos might very well
2071 * be filling that hole.
2073 * Stop at a cpos of '0' because we either started at the
2074 * leftmost branch (i.e., a tree with one branch and a
2075 * rotation inside of it), or we've gone as far as we can in
2076 * rotating subtrees.
2078 while (cpos
&& insert_cpos
<= cpos
) {
2079 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2082 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2088 mlog_bug_on_msg(path_leaf_bh(left_path
) ==
2089 path_leaf_bh(right_path
),
2090 "Inode %lu: error during insert of %u "
2091 "(left path cpos %u) results in two identical "
2092 "paths ending at %llu\n",
2093 inode
->i_ino
, insert_cpos
, cpos
,
2094 (unsigned long long)
2095 path_leaf_bh(left_path
)->b_blocknr
);
2097 if (split
== SPLIT_NONE
&&
2098 ocfs2_rotate_requires_path_adjustment(left_path
,
2102 * We've rotated the tree as much as we
2103 * should. The rest is up to
2104 * ocfs2_insert_path() to complete, after the
2105 * record insertion. We indicate this
2106 * situation by returning the left path.
2108 * The reason we don't adjust the records here
2109 * before the record insert is that an error
2110 * later might break the rule where a parent
2111 * record e_cpos will reflect the actual
2112 * e_cpos of the 1st nonempty record of the
2115 *ret_left_path
= left_path
;
2119 start
= ocfs2_find_subtree_root(inode
, left_path
, right_path
);
2121 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2123 (unsigned long long) right_path
->p_node
[start
].bh
->b_blocknr
,
2124 right_path
->p_tree_depth
);
2126 ret
= ocfs2_extend_rotate_transaction(handle
, start
,
2127 orig_credits
, right_path
);
2133 ret
= ocfs2_rotate_subtree_right(inode
, handle
, left_path
,
2140 if (split
!= SPLIT_NONE
&&
2141 ocfs2_leftmost_rec_contains(path_leaf_el(right_path
),
2144 * A rotate moves the rightmost left leaf
2145 * record over to the leftmost right leaf
2146 * slot. If we're doing an extent split
2147 * instead of a real insert, then we have to
2148 * check that the extent to be split wasn't
2149 * just moved over. If it was, then we can
2150 * exit here, passing left_path back -
2151 * ocfs2_split_extent() is smart enough to
2152 * search both leaves.
2154 *ret_left_path
= left_path
;
2159 * There is no need to re-read the next right path
2160 * as we know that it'll be our current left
2161 * path. Optimize by copying values instead.
2163 ocfs2_mv_path(right_path
, left_path
);
2165 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
2174 ocfs2_free_path(left_path
);
2180 static void ocfs2_update_edge_lengths(struct inode
*inode
, handle_t
*handle
,
2181 struct ocfs2_path
*path
)
2184 struct ocfs2_extent_rec
*rec
;
2185 struct ocfs2_extent_list
*el
;
2186 struct ocfs2_extent_block
*eb
;
2189 /* Path should always be rightmost. */
2190 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2191 BUG_ON(eb
->h_next_leaf_blk
!= 0ULL);
2194 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
2195 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2196 rec
= &el
->l_recs
[idx
];
2197 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2199 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
2200 el
= path
->p_node
[i
].el
;
2201 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2202 rec
= &el
->l_recs
[idx
];
2204 rec
->e_int_clusters
= cpu_to_le32(range
);
2205 le32_add_cpu(&rec
->e_int_clusters
, -le32_to_cpu(rec
->e_cpos
));
2207 ocfs2_journal_dirty(handle
, path
->p_node
[i
].bh
);
2211 static void ocfs2_unlink_path(struct inode
*inode
, handle_t
*handle
,
2212 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2213 struct ocfs2_path
*path
, int unlink_start
)
2216 struct ocfs2_extent_block
*eb
;
2217 struct ocfs2_extent_list
*el
;
2218 struct buffer_head
*bh
;
2220 for(i
= unlink_start
; i
< path_num_items(path
); i
++) {
2221 bh
= path
->p_node
[i
].bh
;
2223 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
2225 * Not all nodes might have had their final count
2226 * decremented by the caller - handle this here.
2229 if (le16_to_cpu(el
->l_next_free_rec
) > 1) {
2231 "Inode %llu, attempted to remove extent block "
2232 "%llu with %u records\n",
2233 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2234 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
2235 le16_to_cpu(el
->l_next_free_rec
));
2237 ocfs2_journal_dirty(handle
, bh
);
2238 ocfs2_remove_from_cache(inode
, bh
);
2242 el
->l_next_free_rec
= 0;
2243 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2245 ocfs2_journal_dirty(handle
, bh
);
2247 ret
= ocfs2_cache_extent_block_free(dealloc
, eb
);
2251 ocfs2_remove_from_cache(inode
, bh
);
2255 static void ocfs2_unlink_subtree(struct inode
*inode
, handle_t
*handle
,
2256 struct ocfs2_path
*left_path
,
2257 struct ocfs2_path
*right_path
,
2259 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2262 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
2263 struct ocfs2_extent_list
*root_el
= left_path
->p_node
[subtree_index
].el
;
2264 struct ocfs2_extent_list
*el
;
2265 struct ocfs2_extent_block
*eb
;
2267 el
= path_leaf_el(left_path
);
2269 eb
= (struct ocfs2_extent_block
*)right_path
->p_node
[subtree_index
+ 1].bh
->b_data
;
2271 for(i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
2272 if (root_el
->l_recs
[i
].e_blkno
== eb
->h_blkno
)
2275 BUG_ON(i
>= le16_to_cpu(root_el
->l_next_free_rec
));
2277 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
2278 le16_add_cpu(&root_el
->l_next_free_rec
, -1);
2280 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2281 eb
->h_next_leaf_blk
= 0;
2283 ocfs2_journal_dirty(handle
, root_bh
);
2284 ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2286 ocfs2_unlink_path(inode
, handle
, dealloc
, right_path
,
2290 static int ocfs2_rotate_subtree_left(struct inode
*inode
, handle_t
*handle
,
2291 struct ocfs2_path
*left_path
,
2292 struct ocfs2_path
*right_path
,
2294 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2296 struct ocfs2_extent_tree
*et
)
2298 int ret
, i
, del_right_subtree
= 0, right_has_empty
= 0;
2299 struct buffer_head
*root_bh
, *et_root_bh
= path_root_bh(right_path
);
2300 struct ocfs2_extent_list
*right_leaf_el
, *left_leaf_el
;
2301 struct ocfs2_extent_block
*eb
;
2305 right_leaf_el
= path_leaf_el(right_path
);
2306 left_leaf_el
= path_leaf_el(left_path
);
2307 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2308 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2310 if (!ocfs2_is_empty_extent(&left_leaf_el
->l_recs
[0]))
2313 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(right_path
)->b_data
;
2314 if (ocfs2_is_empty_extent(&right_leaf_el
->l_recs
[0])) {
2316 * It's legal for us to proceed if the right leaf is
2317 * the rightmost one and it has an empty extent. There
2318 * are two cases to handle - whether the leaf will be
2319 * empty after removal or not. If the leaf isn't empty
2320 * then just remove the empty extent up front. The
2321 * next block will handle empty leaves by flagging
2324 * Non rightmost leaves will throw -EAGAIN and the
2325 * caller can manually move the subtree and retry.
2328 if (eb
->h_next_leaf_blk
!= 0ULL)
2331 if (le16_to_cpu(right_leaf_el
->l_next_free_rec
) > 1) {
2332 ret
= ocfs2_journal_access(handle
, inode
,
2333 path_leaf_bh(right_path
),
2334 OCFS2_JOURNAL_ACCESS_WRITE
);
2340 ocfs2_remove_empty_extent(right_leaf_el
);
2342 right_has_empty
= 1;
2345 if (eb
->h_next_leaf_blk
== 0ULL &&
2346 le16_to_cpu(right_leaf_el
->l_next_free_rec
) == 1) {
2348 * We have to update i_last_eb_blk during the meta
2351 ret
= ocfs2_journal_access(handle
, inode
, et_root_bh
,
2352 OCFS2_JOURNAL_ACCESS_WRITE
);
2358 del_right_subtree
= 1;
2362 * Getting here with an empty extent in the right path implies
2363 * that it's the rightmost path and will be deleted.
2365 BUG_ON(right_has_empty
&& !del_right_subtree
);
2367 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
2368 OCFS2_JOURNAL_ACCESS_WRITE
);
2374 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2375 ret
= ocfs2_journal_access(handle
, inode
,
2376 right_path
->p_node
[i
].bh
,
2377 OCFS2_JOURNAL_ACCESS_WRITE
);
2383 ret
= ocfs2_journal_access(handle
, inode
,
2384 left_path
->p_node
[i
].bh
,
2385 OCFS2_JOURNAL_ACCESS_WRITE
);
2392 if (!right_has_empty
) {
2394 * Only do this if we're moving a real
2395 * record. Otherwise, the action is delayed until
2396 * after removal of the right path in which case we
2397 * can do a simple shift to remove the empty extent.
2399 ocfs2_rotate_leaf(left_leaf_el
, &right_leaf_el
->l_recs
[0]);
2400 memset(&right_leaf_el
->l_recs
[0], 0,
2401 sizeof(struct ocfs2_extent_rec
));
2403 if (eb
->h_next_leaf_blk
== 0ULL) {
2405 * Move recs over to get rid of empty extent, decrease
2406 * next_free. This is allowed to remove the last
2407 * extent in our leaf (setting l_next_free_rec to
2408 * zero) - the delete code below won't care.
2410 ocfs2_remove_empty_extent(right_leaf_el
);
2413 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2416 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
2420 if (del_right_subtree
) {
2421 ocfs2_unlink_subtree(inode
, handle
, left_path
, right_path
,
2422 subtree_index
, dealloc
);
2423 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2425 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2426 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2429 * Removal of the extent in the left leaf was skipped
2430 * above so we could delete the right path
2433 if (right_has_empty
)
2434 ocfs2_remove_empty_extent(left_leaf_el
);
2436 ret
= ocfs2_journal_dirty(handle
, et_root_bh
);
2442 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
2450 * Given a full path, determine what cpos value would return us a path
2451 * containing the leaf immediately to the right of the current one.
2453 * Will return zero if the path passed in is already the rightmost path.
2455 * This looks similar, but is subtly different to
2456 * ocfs2_find_cpos_for_left_leaf().
2458 static int ocfs2_find_cpos_for_right_leaf(struct super_block
*sb
,
2459 struct ocfs2_path
*path
, u32
*cpos
)
2463 struct ocfs2_extent_list
*el
;
2467 if (path
->p_tree_depth
== 0)
2470 blkno
= path_leaf_bh(path
)->b_blocknr
;
2472 /* Start at the tree node just above the leaf and work our way up. */
2473 i
= path
->p_tree_depth
- 1;
2477 el
= path
->p_node
[i
].el
;
2480 * Find the extent record just after the one in our
2483 next_free
= le16_to_cpu(el
->l_next_free_rec
);
2484 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2485 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2486 if (j
== (next_free
- 1)) {
2489 * We've determined that the
2490 * path specified is already
2491 * the rightmost one - return a
2497 * The rightmost record points to our
2498 * leaf - we need to travel up the
2504 *cpos
= le32_to_cpu(el
->l_recs
[j
+ 1].e_cpos
);
2510 * If we got here, we never found a valid node where
2511 * the tree indicated one should be.
2514 "Invalid extent tree at extent block %llu\n",
2515 (unsigned long long)blkno
);
2520 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2528 static int ocfs2_rotate_rightmost_leaf_left(struct inode
*inode
,
2530 struct buffer_head
*bh
,
2531 struct ocfs2_extent_list
*el
)
2535 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2538 ret
= ocfs2_journal_access(handle
, inode
, bh
,
2539 OCFS2_JOURNAL_ACCESS_WRITE
);
2545 ocfs2_remove_empty_extent(el
);
2547 ret
= ocfs2_journal_dirty(handle
, bh
);
2555 static int __ocfs2_rotate_tree_left(struct inode
*inode
,
2556 handle_t
*handle
, int orig_credits
,
2557 struct ocfs2_path
*path
,
2558 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2559 struct ocfs2_path
**empty_extent_path
,
2560 struct ocfs2_extent_tree
*et
)
2562 int ret
, subtree_root
, deleted
;
2564 struct ocfs2_path
*left_path
= NULL
;
2565 struct ocfs2_path
*right_path
= NULL
;
2567 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path
)->l_recs
[0])));
2569 *empty_extent_path
= NULL
;
2571 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, path
,
2578 left_path
= ocfs2_new_path(path_root_bh(path
),
2579 path_root_el(path
));
2586 ocfs2_cp_path(left_path
, path
);
2588 right_path
= ocfs2_new_path(path_root_bh(path
),
2589 path_root_el(path
));
2596 while (right_cpos
) {
2597 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
2603 subtree_root
= ocfs2_find_subtree_root(inode
, left_path
,
2606 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2608 (unsigned long long)
2609 right_path
->p_node
[subtree_root
].bh
->b_blocknr
,
2610 right_path
->p_tree_depth
);
2612 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_root
,
2613 orig_credits
, left_path
);
2620 * Caller might still want to make changes to the
2621 * tree root, so re-add it to the journal here.
2623 ret
= ocfs2_journal_access(handle
, inode
,
2624 path_root_bh(left_path
),
2625 OCFS2_JOURNAL_ACCESS_WRITE
);
2631 ret
= ocfs2_rotate_subtree_left(inode
, handle
, left_path
,
2632 right_path
, subtree_root
,
2633 dealloc
, &deleted
, et
);
2634 if (ret
== -EAGAIN
) {
2636 * The rotation has to temporarily stop due to
2637 * the right subtree having an empty
2638 * extent. Pass it back to the caller for a
2641 *empty_extent_path
= right_path
;
2651 * The subtree rotate might have removed records on
2652 * the rightmost edge. If so, then rotation is
2658 ocfs2_mv_path(left_path
, right_path
);
2660 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
2669 ocfs2_free_path(right_path
);
2670 ocfs2_free_path(left_path
);
2675 static int ocfs2_remove_rightmost_path(struct inode
*inode
, handle_t
*handle
,
2676 struct ocfs2_path
*path
,
2677 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2678 struct ocfs2_extent_tree
*et
)
2680 int ret
, subtree_index
;
2682 struct ocfs2_path
*left_path
= NULL
;
2683 struct ocfs2_extent_block
*eb
;
2684 struct ocfs2_extent_list
*el
;
2687 ret
= ocfs2_et_sanity_check(inode
, et
);
2691 * There's two ways we handle this depending on
2692 * whether path is the only existing one.
2694 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
2695 handle
->h_buffer_credits
,
2702 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
2708 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
2716 * We have a path to the left of this one - it needs
2719 left_path
= ocfs2_new_path(path_root_bh(path
),
2720 path_root_el(path
));
2727 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2733 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
2739 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
2741 ocfs2_unlink_subtree(inode
, handle
, left_path
, path
,
2742 subtree_index
, dealloc
);
2743 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2745 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2746 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2749 * 'path' is also the leftmost path which
2750 * means it must be the only one. This gets
2751 * handled differently because we want to
2752 * revert the inode back to having extents
2755 ocfs2_unlink_path(inode
, handle
, dealloc
, path
, 1);
2757 el
= et
->et_root_el
;
2758 el
->l_tree_depth
= 0;
2759 el
->l_next_free_rec
= 0;
2760 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2762 ocfs2_et_set_last_eb_blk(et
, 0);
2765 ocfs2_journal_dirty(handle
, path_root_bh(path
));
2768 ocfs2_free_path(left_path
);
2773 * Left rotation of btree records.
2775 * In many ways, this is (unsurprisingly) the opposite of right
2776 * rotation. We start at some non-rightmost path containing an empty
2777 * extent in the leaf block. The code works its way to the rightmost
2778 * path by rotating records to the left in every subtree.
2780 * This is used by any code which reduces the number of extent records
2781 * in a leaf. After removal, an empty record should be placed in the
2782 * leftmost list position.
2784 * This won't handle a length update of the rightmost path records if
2785 * the rightmost tree leaf record is removed so the caller is
2786 * responsible for detecting and correcting that.
2788 static int ocfs2_rotate_tree_left(struct inode
*inode
, handle_t
*handle
,
2789 struct ocfs2_path
*path
,
2790 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2791 struct ocfs2_extent_tree
*et
)
2793 int ret
, orig_credits
= handle
->h_buffer_credits
;
2794 struct ocfs2_path
*tmp_path
= NULL
, *restart_path
= NULL
;
2795 struct ocfs2_extent_block
*eb
;
2796 struct ocfs2_extent_list
*el
;
2798 el
= path_leaf_el(path
);
2799 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2802 if (path
->p_tree_depth
== 0) {
2803 rightmost_no_delete
:
2805 * Inline extents. This is trivially handled, so do
2808 ret
= ocfs2_rotate_rightmost_leaf_left(inode
, handle
,
2810 path_leaf_el(path
));
2817 * Handle rightmost branch now. There's several cases:
2818 * 1) simple rotation leaving records in there. That's trivial.
2819 * 2) rotation requiring a branch delete - there's no more
2820 * records left. Two cases of this:
2821 * a) There are branches to the left.
2822 * b) This is also the leftmost (the only) branch.
2824 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2825 * 2a) we need the left branch so that we can update it with the unlink
2826 * 2b) we need to bring the inode back to inline extents.
2829 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2831 if (eb
->h_next_leaf_blk
== 0) {
2833 * This gets a bit tricky if we're going to delete the
2834 * rightmost path. Get the other cases out of the way
2837 if (le16_to_cpu(el
->l_next_free_rec
) > 1)
2838 goto rightmost_no_delete
;
2840 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
2842 ocfs2_error(inode
->i_sb
,
2843 "Inode %llu has empty extent block at %llu",
2844 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2845 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
2850 * XXX: The caller can not trust "path" any more after
2851 * this as it will have been deleted. What do we do?
2853 * In theory the rotate-for-merge code will never get
2854 * here because it'll always ask for a rotate in a
2858 ret
= ocfs2_remove_rightmost_path(inode
, handle
, path
,
2866 * Now we can loop, remembering the path we get from -EAGAIN
2867 * and restarting from there.
2870 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
, path
,
2871 dealloc
, &restart_path
, et
);
2872 if (ret
&& ret
!= -EAGAIN
) {
2877 while (ret
== -EAGAIN
) {
2878 tmp_path
= restart_path
;
2879 restart_path
= NULL
;
2881 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
,
2884 if (ret
&& ret
!= -EAGAIN
) {
2889 ocfs2_free_path(tmp_path
);
2897 ocfs2_free_path(tmp_path
);
2898 ocfs2_free_path(restart_path
);
2902 static void ocfs2_cleanup_merge(struct ocfs2_extent_list
*el
,
2905 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[index
];
2908 if (rec
->e_leaf_clusters
== 0) {
2910 * We consumed all of the merged-from record. An empty
2911 * extent cannot exist anywhere but the 1st array
2912 * position, so move things over if the merged-from
2913 * record doesn't occupy that position.
2915 * This creates a new empty extent so the caller
2916 * should be smart enough to have removed any existing
2920 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
2921 size
= index
* sizeof(struct ocfs2_extent_rec
);
2922 memmove(&el
->l_recs
[1], &el
->l_recs
[0], size
);
2926 * Always memset - the caller doesn't check whether it
2927 * created an empty extent, so there could be junk in
2930 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2934 static int ocfs2_get_right_path(struct inode
*inode
,
2935 struct ocfs2_path
*left_path
,
2936 struct ocfs2_path
**ret_right_path
)
2940 struct ocfs2_path
*right_path
= NULL
;
2941 struct ocfs2_extent_list
*left_el
;
2943 *ret_right_path
= NULL
;
2945 /* This function shouldn't be called for non-trees. */
2946 BUG_ON(left_path
->p_tree_depth
== 0);
2948 left_el
= path_leaf_el(left_path
);
2949 BUG_ON(left_el
->l_next_free_rec
!= left_el
->l_count
);
2951 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
2958 /* This function shouldn't be called for the rightmost leaf. */
2959 BUG_ON(right_cpos
== 0);
2961 right_path
= ocfs2_new_path(path_root_bh(left_path
),
2962 path_root_el(left_path
));
2969 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
2975 *ret_right_path
= right_path
;
2978 ocfs2_free_path(right_path
);
2983 * Remove split_rec clusters from the record at index and merge them
2984 * onto the beginning of the record "next" to it.
2985 * For index < l_count - 1, the next means the extent rec at index + 1.
2986 * For index == l_count - 1, the "next" means the 1st extent rec of the
2987 * next extent block.
2989 static int ocfs2_merge_rec_right(struct inode
*inode
,
2990 struct ocfs2_path
*left_path
,
2992 struct ocfs2_extent_rec
*split_rec
,
2995 int ret
, next_free
, i
;
2996 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
2997 struct ocfs2_extent_rec
*left_rec
;
2998 struct ocfs2_extent_rec
*right_rec
;
2999 struct ocfs2_extent_list
*right_el
;
3000 struct ocfs2_path
*right_path
= NULL
;
3001 int subtree_index
= 0;
3002 struct ocfs2_extent_list
*el
= path_leaf_el(left_path
);
3003 struct buffer_head
*bh
= path_leaf_bh(left_path
);
3004 struct buffer_head
*root_bh
= NULL
;
3006 BUG_ON(index
>= le16_to_cpu(el
->l_next_free_rec
));
3007 left_rec
= &el
->l_recs
[index
];
3009 if (index
== le16_to_cpu(el
->l_next_free_rec
) - 1 &&
3010 le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
)) {
3011 /* we meet with a cross extent block merge. */
3012 ret
= ocfs2_get_right_path(inode
, left_path
, &right_path
);
3018 right_el
= path_leaf_el(right_path
);
3019 next_free
= le16_to_cpu(right_el
->l_next_free_rec
);
3020 BUG_ON(next_free
<= 0);
3021 right_rec
= &right_el
->l_recs
[0];
3022 if (ocfs2_is_empty_extent(right_rec
)) {
3023 BUG_ON(next_free
<= 1);
3024 right_rec
= &right_el
->l_recs
[1];
3027 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3028 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3029 le32_to_cpu(right_rec
->e_cpos
));
3031 subtree_index
= ocfs2_find_subtree_root(inode
,
3032 left_path
, right_path
);
3034 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3035 handle
->h_buffer_credits
,
3042 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3043 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3045 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
3046 OCFS2_JOURNAL_ACCESS_WRITE
);
3052 for (i
= subtree_index
+ 1;
3053 i
< path_num_items(right_path
); i
++) {
3054 ret
= ocfs2_journal_access(handle
, inode
,
3055 right_path
->p_node
[i
].bh
,
3056 OCFS2_JOURNAL_ACCESS_WRITE
);
3062 ret
= ocfs2_journal_access(handle
, inode
,
3063 left_path
->p_node
[i
].bh
,
3064 OCFS2_JOURNAL_ACCESS_WRITE
);
3072 BUG_ON(index
== le16_to_cpu(el
->l_next_free_rec
) - 1);
3073 right_rec
= &el
->l_recs
[index
+ 1];
3076 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3077 OCFS2_JOURNAL_ACCESS_WRITE
);
3083 le16_add_cpu(&left_rec
->e_leaf_clusters
, -split_clusters
);
3085 le32_add_cpu(&right_rec
->e_cpos
, -split_clusters
);
3086 le64_add_cpu(&right_rec
->e_blkno
,
3087 -ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3088 le16_add_cpu(&right_rec
->e_leaf_clusters
, split_clusters
);
3090 ocfs2_cleanup_merge(el
, index
);
3092 ret
= ocfs2_journal_dirty(handle
, bh
);
3097 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
3101 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3102 right_path
, subtree_index
);
3106 ocfs2_free_path(right_path
);
3110 static int ocfs2_get_left_path(struct inode
*inode
,
3111 struct ocfs2_path
*right_path
,
3112 struct ocfs2_path
**ret_left_path
)
3116 struct ocfs2_path
*left_path
= NULL
;
3118 *ret_left_path
= NULL
;
3120 /* This function shouldn't be called for non-trees. */
3121 BUG_ON(right_path
->p_tree_depth
== 0);
3123 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
3124 right_path
, &left_cpos
);
3130 /* This function shouldn't be called for the leftmost leaf. */
3131 BUG_ON(left_cpos
== 0);
3133 left_path
= ocfs2_new_path(path_root_bh(right_path
),
3134 path_root_el(right_path
));
3141 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3147 *ret_left_path
= left_path
;
3150 ocfs2_free_path(left_path
);
3155 * Remove split_rec clusters from the record at index and merge them
3156 * onto the tail of the record "before" it.
3157 * For index > 0, the "before" means the extent rec at index - 1.
3159 * For index == 0, the "before" means the last record of the previous
3160 * extent block. And there is also a situation that we may need to
3161 * remove the rightmost leaf extent block in the right_path and change
3162 * the right path to indicate the new rightmost path.
3164 static int ocfs2_merge_rec_left(struct inode
*inode
,
3165 struct ocfs2_path
*right_path
,
3167 struct ocfs2_extent_rec
*split_rec
,
3168 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3169 struct ocfs2_extent_tree
*et
,
3172 int ret
, i
, subtree_index
= 0, has_empty_extent
= 0;
3173 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3174 struct ocfs2_extent_rec
*left_rec
;
3175 struct ocfs2_extent_rec
*right_rec
;
3176 struct ocfs2_extent_list
*el
= path_leaf_el(right_path
);
3177 struct buffer_head
*bh
= path_leaf_bh(right_path
);
3178 struct buffer_head
*root_bh
= NULL
;
3179 struct ocfs2_path
*left_path
= NULL
;
3180 struct ocfs2_extent_list
*left_el
;
3184 right_rec
= &el
->l_recs
[index
];
3186 /* we meet with a cross extent block merge. */
3187 ret
= ocfs2_get_left_path(inode
, right_path
, &left_path
);
3193 left_el
= path_leaf_el(left_path
);
3194 BUG_ON(le16_to_cpu(left_el
->l_next_free_rec
) !=
3195 le16_to_cpu(left_el
->l_count
));
3197 left_rec
= &left_el
->l_recs
[
3198 le16_to_cpu(left_el
->l_next_free_rec
) - 1];
3199 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3200 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3201 le32_to_cpu(split_rec
->e_cpos
));
3203 subtree_index
= ocfs2_find_subtree_root(inode
,
3204 left_path
, right_path
);
3206 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3207 handle
->h_buffer_credits
,
3214 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3215 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3217 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
3218 OCFS2_JOURNAL_ACCESS_WRITE
);
3224 for (i
= subtree_index
+ 1;
3225 i
< path_num_items(right_path
); i
++) {
3226 ret
= ocfs2_journal_access(handle
, inode
,
3227 right_path
->p_node
[i
].bh
,
3228 OCFS2_JOURNAL_ACCESS_WRITE
);
3234 ret
= ocfs2_journal_access(handle
, inode
,
3235 left_path
->p_node
[i
].bh
,
3236 OCFS2_JOURNAL_ACCESS_WRITE
);
3243 left_rec
= &el
->l_recs
[index
- 1];
3244 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
3245 has_empty_extent
= 1;
3248 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3249 OCFS2_JOURNAL_ACCESS_WRITE
);
3255 if (has_empty_extent
&& index
== 1) {
3257 * The easy case - we can just plop the record right in.
3259 *left_rec
= *split_rec
;
3261 has_empty_extent
= 0;
3263 le16_add_cpu(&left_rec
->e_leaf_clusters
, split_clusters
);
3265 le32_add_cpu(&right_rec
->e_cpos
, split_clusters
);
3266 le64_add_cpu(&right_rec
->e_blkno
,
3267 ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3268 le16_add_cpu(&right_rec
->e_leaf_clusters
, -split_clusters
);
3270 ocfs2_cleanup_merge(el
, index
);
3272 ret
= ocfs2_journal_dirty(handle
, bh
);
3277 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
3282 * In the situation that the right_rec is empty and the extent
3283 * block is empty also, ocfs2_complete_edge_insert can't handle
3284 * it and we need to delete the right extent block.
3286 if (le16_to_cpu(right_rec
->e_leaf_clusters
) == 0 &&
3287 le16_to_cpu(el
->l_next_free_rec
) == 1) {
3289 ret
= ocfs2_remove_rightmost_path(inode
, handle
,
3297 /* Now the rightmost extent block has been deleted.
3298 * So we use the new rightmost path.
3300 ocfs2_mv_path(right_path
, left_path
);
3303 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3304 right_path
, subtree_index
);
3308 ocfs2_free_path(left_path
);
3312 static int ocfs2_try_to_merge_extent(struct inode
*inode
,
3314 struct ocfs2_path
*path
,
3316 struct ocfs2_extent_rec
*split_rec
,
3317 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3318 struct ocfs2_merge_ctxt
*ctxt
,
3319 struct ocfs2_extent_tree
*et
)
3323 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
3324 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
3326 BUG_ON(ctxt
->c_contig_type
== CONTIG_NONE
);
3328 if (ctxt
->c_split_covers_rec
&& ctxt
->c_has_empty_extent
) {
3330 * The merge code will need to create an empty
3331 * extent to take the place of the newly
3332 * emptied slot. Remove any pre-existing empty
3333 * extents - having more than one in a leaf is
3336 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3343 rec
= &el
->l_recs
[split_index
];
3346 if (ctxt
->c_contig_type
== CONTIG_LEFTRIGHT
) {
3348 * Left-right contig implies this.
3350 BUG_ON(!ctxt
->c_split_covers_rec
);
3353 * Since the leftright insert always covers the entire
3354 * extent, this call will delete the insert record
3355 * entirely, resulting in an empty extent record added to
3358 * Since the adding of an empty extent shifts
3359 * everything back to the right, there's no need to
3360 * update split_index here.
3362 * When the split_index is zero, we need to merge it to the
3363 * prevoius extent block. It is more efficient and easier
3364 * if we do merge_right first and merge_left later.
3366 ret
= ocfs2_merge_rec_right(inode
, path
,
3375 * We can only get this from logic error above.
3377 BUG_ON(!ocfs2_is_empty_extent(&el
->l_recs
[0]));
3379 /* The merge left us with an empty extent, remove it. */
3380 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3387 rec
= &el
->l_recs
[split_index
];
3390 * Note that we don't pass split_rec here on purpose -
3391 * we've merged it into the rec already.
3393 ret
= ocfs2_merge_rec_left(inode
, path
,
3403 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3406 * Error from this last rotate is not critical, so
3407 * print but don't bubble it up.
3414 * Merge a record to the left or right.
3416 * 'contig_type' is relative to the existing record,
3417 * so for example, if we're "right contig", it's to
3418 * the record on the left (hence the left merge).
3420 if (ctxt
->c_contig_type
== CONTIG_RIGHT
) {
3421 ret
= ocfs2_merge_rec_left(inode
,
3431 ret
= ocfs2_merge_rec_right(inode
,
3441 if (ctxt
->c_split_covers_rec
) {
3443 * The merge may have left an empty extent in
3444 * our leaf. Try to rotate it away.
3446 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3458 static void ocfs2_subtract_from_rec(struct super_block
*sb
,
3459 enum ocfs2_split_type split
,
3460 struct ocfs2_extent_rec
*rec
,
3461 struct ocfs2_extent_rec
*split_rec
)
3465 len_blocks
= ocfs2_clusters_to_blocks(sb
,
3466 le16_to_cpu(split_rec
->e_leaf_clusters
));
3468 if (split
== SPLIT_LEFT
) {
3470 * Region is on the left edge of the existing
3473 le32_add_cpu(&rec
->e_cpos
,
3474 le16_to_cpu(split_rec
->e_leaf_clusters
));
3475 le64_add_cpu(&rec
->e_blkno
, len_blocks
);
3476 le16_add_cpu(&rec
->e_leaf_clusters
,
3477 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3480 * Region is on the right edge of the existing
3483 le16_add_cpu(&rec
->e_leaf_clusters
,
3484 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3489 * Do the final bits of extent record insertion at the target leaf
3490 * list. If this leaf is part of an allocation tree, it is assumed
3491 * that the tree above has been prepared.
3493 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec
*insert_rec
,
3494 struct ocfs2_extent_list
*el
,
3495 struct ocfs2_insert_type
*insert
,
3496 struct inode
*inode
)
3498 int i
= insert
->ins_contig_index
;
3500 struct ocfs2_extent_rec
*rec
;
3502 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
3504 if (insert
->ins_split
!= SPLIT_NONE
) {
3505 i
= ocfs2_search_extent_list(el
, le32_to_cpu(insert_rec
->e_cpos
));
3507 rec
= &el
->l_recs
[i
];
3508 ocfs2_subtract_from_rec(inode
->i_sb
, insert
->ins_split
, rec
,
3514 * Contiguous insert - either left or right.
3516 if (insert
->ins_contig
!= CONTIG_NONE
) {
3517 rec
= &el
->l_recs
[i
];
3518 if (insert
->ins_contig
== CONTIG_LEFT
) {
3519 rec
->e_blkno
= insert_rec
->e_blkno
;
3520 rec
->e_cpos
= insert_rec
->e_cpos
;
3522 le16_add_cpu(&rec
->e_leaf_clusters
,
3523 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3528 * Handle insert into an empty leaf.
3530 if (le16_to_cpu(el
->l_next_free_rec
) == 0 ||
3531 ((le16_to_cpu(el
->l_next_free_rec
) == 1) &&
3532 ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3533 el
->l_recs
[0] = *insert_rec
;
3534 el
->l_next_free_rec
= cpu_to_le16(1);
3541 if (insert
->ins_appending
== APPEND_TAIL
) {
3542 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
3543 rec
= &el
->l_recs
[i
];
3544 range
= le32_to_cpu(rec
->e_cpos
)
3545 + le16_to_cpu(rec
->e_leaf_clusters
);
3546 BUG_ON(le32_to_cpu(insert_rec
->e_cpos
) < range
);
3548 mlog_bug_on_msg(le16_to_cpu(el
->l_next_free_rec
) >=
3549 le16_to_cpu(el
->l_count
),
3550 "inode %lu, depth %u, count %u, next free %u, "
3551 "rec.cpos %u, rec.clusters %u, "
3552 "insert.cpos %u, insert.clusters %u\n",
3554 le16_to_cpu(el
->l_tree_depth
),
3555 le16_to_cpu(el
->l_count
),
3556 le16_to_cpu(el
->l_next_free_rec
),
3557 le32_to_cpu(el
->l_recs
[i
].e_cpos
),
3558 le16_to_cpu(el
->l_recs
[i
].e_leaf_clusters
),
3559 le32_to_cpu(insert_rec
->e_cpos
),
3560 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3562 el
->l_recs
[i
] = *insert_rec
;
3563 le16_add_cpu(&el
->l_next_free_rec
, 1);
3569 * Ok, we have to rotate.
3571 * At this point, it is safe to assume that inserting into an
3572 * empty leaf and appending to a leaf have both been handled
3575 * This leaf needs to have space, either by the empty 1st
3576 * extent record, or by virtue of an l_next_rec < l_count.
3578 ocfs2_rotate_leaf(el
, insert_rec
);
3581 static void ocfs2_adjust_rightmost_records(struct inode
*inode
,
3583 struct ocfs2_path
*path
,
3584 struct ocfs2_extent_rec
*insert_rec
)
3586 int ret
, i
, next_free
;
3587 struct buffer_head
*bh
;
3588 struct ocfs2_extent_list
*el
;
3589 struct ocfs2_extent_rec
*rec
;
3592 * Update everything except the leaf block.
3594 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
3595 bh
= path
->p_node
[i
].bh
;
3596 el
= path
->p_node
[i
].el
;
3598 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3599 if (next_free
== 0) {
3600 ocfs2_error(inode
->i_sb
,
3601 "Dinode %llu has a bad extent list",
3602 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3607 rec
= &el
->l_recs
[next_free
- 1];
3609 rec
->e_int_clusters
= insert_rec
->e_cpos
;
3610 le32_add_cpu(&rec
->e_int_clusters
,
3611 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3612 le32_add_cpu(&rec
->e_int_clusters
,
3613 -le32_to_cpu(rec
->e_cpos
));
3615 ret
= ocfs2_journal_dirty(handle
, bh
);
3622 static int ocfs2_append_rec_to_path(struct inode
*inode
, handle_t
*handle
,
3623 struct ocfs2_extent_rec
*insert_rec
,
3624 struct ocfs2_path
*right_path
,
3625 struct ocfs2_path
**ret_left_path
)
3628 struct ocfs2_extent_list
*el
;
3629 struct ocfs2_path
*left_path
= NULL
;
3631 *ret_left_path
= NULL
;
3634 * This shouldn't happen for non-trees. The extent rec cluster
3635 * count manipulation below only works for interior nodes.
3637 BUG_ON(right_path
->p_tree_depth
== 0);
3640 * If our appending insert is at the leftmost edge of a leaf,
3641 * then we might need to update the rightmost records of the
3644 el
= path_leaf_el(right_path
);
3645 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3646 if (next_free
== 0 ||
3647 (next_free
== 1 && ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3650 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
3657 mlog(0, "Append may need a left path update. cpos: %u, "
3658 "left_cpos: %u\n", le32_to_cpu(insert_rec
->e_cpos
),
3662 * No need to worry if the append is already in the
3666 left_path
= ocfs2_new_path(path_root_bh(right_path
),
3667 path_root_el(right_path
));
3674 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3681 * ocfs2_insert_path() will pass the left_path to the
3687 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
3693 ocfs2_adjust_rightmost_records(inode
, handle
, right_path
, insert_rec
);
3695 *ret_left_path
= left_path
;
3699 ocfs2_free_path(left_path
);
3704 static void ocfs2_split_record(struct inode
*inode
,
3705 struct ocfs2_path
*left_path
,
3706 struct ocfs2_path
*right_path
,
3707 struct ocfs2_extent_rec
*split_rec
,
3708 enum ocfs2_split_type split
)
3711 u32 cpos
= le32_to_cpu(split_rec
->e_cpos
);
3712 struct ocfs2_extent_list
*left_el
= NULL
, *right_el
, *insert_el
, *el
;
3713 struct ocfs2_extent_rec
*rec
, *tmprec
;
3715 right_el
= path_leaf_el(right_path
);;
3717 left_el
= path_leaf_el(left_path
);
3720 insert_el
= right_el
;
3721 index
= ocfs2_search_extent_list(el
, cpos
);
3723 if (index
== 0 && left_path
) {
3724 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3727 * This typically means that the record
3728 * started in the left path but moved to the
3729 * right as a result of rotation. We either
3730 * move the existing record to the left, or we
3731 * do the later insert there.
3733 * In this case, the left path should always
3734 * exist as the rotate code will have passed
3735 * it back for a post-insert update.
3738 if (split
== SPLIT_LEFT
) {
3740 * It's a left split. Since we know
3741 * that the rotate code gave us an
3742 * empty extent in the left path, we
3743 * can just do the insert there.
3745 insert_el
= left_el
;
3748 * Right split - we have to move the
3749 * existing record over to the left
3750 * leaf. The insert will be into the
3751 * newly created empty extent in the
3754 tmprec
= &right_el
->l_recs
[index
];
3755 ocfs2_rotate_leaf(left_el
, tmprec
);
3758 memset(tmprec
, 0, sizeof(*tmprec
));
3759 index
= ocfs2_search_extent_list(left_el
, cpos
);
3760 BUG_ON(index
== -1);
3765 BUG_ON(!ocfs2_is_empty_extent(&left_el
->l_recs
[0]));
3767 * Left path is easy - we can just allow the insert to
3771 insert_el
= left_el
;
3772 index
= ocfs2_search_extent_list(el
, cpos
);
3773 BUG_ON(index
== -1);
3776 rec
= &el
->l_recs
[index
];
3777 ocfs2_subtract_from_rec(inode
->i_sb
, split
, rec
, split_rec
);
3778 ocfs2_rotate_leaf(insert_el
, split_rec
);
3782 * This function only does inserts on an allocation b-tree. For tree
3783 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3785 * right_path is the path we want to do the actual insert
3786 * in. left_path should only be passed in if we need to update that
3787 * portion of the tree after an edge insert.
3789 static int ocfs2_insert_path(struct inode
*inode
,
3791 struct ocfs2_path
*left_path
,
3792 struct ocfs2_path
*right_path
,
3793 struct ocfs2_extent_rec
*insert_rec
,
3794 struct ocfs2_insert_type
*insert
)
3796 int ret
, subtree_index
;
3797 struct buffer_head
*leaf_bh
= path_leaf_bh(right_path
);
3800 int credits
= handle
->h_buffer_credits
;
3803 * There's a chance that left_path got passed back to
3804 * us without being accounted for in the
3805 * journal. Extend our transaction here to be sure we
3806 * can change those blocks.
3808 credits
+= left_path
->p_tree_depth
;
3810 ret
= ocfs2_extend_trans(handle
, credits
);
3816 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
3824 * Pass both paths to the journal. The majority of inserts
3825 * will be touching all components anyway.
3827 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
3833 if (insert
->ins_split
!= SPLIT_NONE
) {
3835 * We could call ocfs2_insert_at_leaf() for some types
3836 * of splits, but it's easier to just let one separate
3837 * function sort it all out.
3839 ocfs2_split_record(inode
, left_path
, right_path
,
3840 insert_rec
, insert
->ins_split
);
3843 * Split might have modified either leaf and we don't
3844 * have a guarantee that the later edge insert will
3845 * dirty this for us.
3848 ret
= ocfs2_journal_dirty(handle
,
3849 path_leaf_bh(left_path
));
3853 ocfs2_insert_at_leaf(insert_rec
, path_leaf_el(right_path
),
3856 ret
= ocfs2_journal_dirty(handle
, leaf_bh
);
3862 * The rotate code has indicated that we need to fix
3863 * up portions of the tree after the insert.
3865 * XXX: Should we extend the transaction here?
3867 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
,
3869 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3870 right_path
, subtree_index
);
3878 static int ocfs2_do_insert_extent(struct inode
*inode
,
3880 struct ocfs2_extent_tree
*et
,
3881 struct ocfs2_extent_rec
*insert_rec
,
3882 struct ocfs2_insert_type
*type
)
3884 int ret
, rotate
= 0;
3886 struct ocfs2_path
*right_path
= NULL
;
3887 struct ocfs2_path
*left_path
= NULL
;
3888 struct ocfs2_extent_list
*el
;
3890 el
= et
->et_root_el
;
3892 ret
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
3893 OCFS2_JOURNAL_ACCESS_WRITE
);
3899 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
3900 ocfs2_insert_at_leaf(insert_rec
, el
, type
, inode
);
3901 goto out_update_clusters
;
3904 right_path
= ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
);
3912 * Determine the path to start with. Rotations need the
3913 * rightmost path, everything else can go directly to the
3916 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
3917 if (type
->ins_appending
== APPEND_NONE
&&
3918 type
->ins_contig
== CONTIG_NONE
) {
3923 ret
= ocfs2_find_path(inode
, right_path
, cpos
);
3930 * Rotations and appends need special treatment - they modify
3931 * parts of the tree's above them.
3933 * Both might pass back a path immediate to the left of the
3934 * one being inserted to. This will be cause
3935 * ocfs2_insert_path() to modify the rightmost records of
3936 * left_path to account for an edge insert.
3938 * XXX: When modifying this code, keep in mind that an insert
3939 * can wind up skipping both of these two special cases...
3942 ret
= ocfs2_rotate_tree_right(inode
, handle
, type
->ins_split
,
3943 le32_to_cpu(insert_rec
->e_cpos
),
3944 right_path
, &left_path
);
3951 * ocfs2_rotate_tree_right() might have extended the
3952 * transaction without re-journaling our tree root.
3954 ret
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
3955 OCFS2_JOURNAL_ACCESS_WRITE
);
3960 } else if (type
->ins_appending
== APPEND_TAIL
3961 && type
->ins_contig
!= CONTIG_LEFT
) {
3962 ret
= ocfs2_append_rec_to_path(inode
, handle
, insert_rec
,
3963 right_path
, &left_path
);
3970 ret
= ocfs2_insert_path(inode
, handle
, left_path
, right_path
,
3977 out_update_clusters
:
3978 if (type
->ins_split
== SPLIT_NONE
)
3979 ocfs2_et_update_clusters(inode
, et
,
3980 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3982 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
3987 ocfs2_free_path(left_path
);
3988 ocfs2_free_path(right_path
);
3993 static enum ocfs2_contig_type
3994 ocfs2_figure_merge_contig_type(struct inode
*inode
, struct ocfs2_path
*path
,
3995 struct ocfs2_extent_list
*el
, int index
,
3996 struct ocfs2_extent_rec
*split_rec
)
3999 enum ocfs2_contig_type ret
= CONTIG_NONE
;
4000 u32 left_cpos
, right_cpos
;
4001 struct ocfs2_extent_rec
*rec
= NULL
;
4002 struct ocfs2_extent_list
*new_el
;
4003 struct ocfs2_path
*left_path
= NULL
, *right_path
= NULL
;
4004 struct buffer_head
*bh
;
4005 struct ocfs2_extent_block
*eb
;
4008 rec
= &el
->l_recs
[index
- 1];
4009 } else if (path
->p_tree_depth
> 0) {
4010 status
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
4015 if (left_cpos
!= 0) {
4016 left_path
= ocfs2_new_path(path_root_bh(path
),
4017 path_root_el(path
));
4021 status
= ocfs2_find_path(inode
, left_path
, left_cpos
);
4025 new_el
= path_leaf_el(left_path
);
4027 if (le16_to_cpu(new_el
->l_next_free_rec
) !=
4028 le16_to_cpu(new_el
->l_count
)) {
4029 bh
= path_leaf_bh(left_path
);
4030 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4031 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
,
4035 rec
= &new_el
->l_recs
[
4036 le16_to_cpu(new_el
->l_next_free_rec
) - 1];
4041 * We're careful to check for an empty extent record here -
4042 * the merge code will know what to do if it sees one.
4045 if (index
== 1 && ocfs2_is_empty_extent(rec
)) {
4046 if (split_rec
->e_cpos
== el
->l_recs
[index
].e_cpos
)
4049 ret
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4054 if (index
< (le16_to_cpu(el
->l_next_free_rec
) - 1))
4055 rec
= &el
->l_recs
[index
+ 1];
4056 else if (le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
) &&
4057 path
->p_tree_depth
> 0) {
4058 status
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
,
4063 if (right_cpos
== 0)
4066 right_path
= ocfs2_new_path(path_root_bh(path
),
4067 path_root_el(path
));
4071 status
= ocfs2_find_path(inode
, right_path
, right_cpos
);
4075 new_el
= path_leaf_el(right_path
);
4076 rec
= &new_el
->l_recs
[0];
4077 if (ocfs2_is_empty_extent(rec
)) {
4078 if (le16_to_cpu(new_el
->l_next_free_rec
) <= 1) {
4079 bh
= path_leaf_bh(right_path
);
4080 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4081 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
,
4085 rec
= &new_el
->l_recs
[1];
4090 enum ocfs2_contig_type contig_type
;
4092 contig_type
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4094 if (contig_type
== CONTIG_LEFT
&& ret
== CONTIG_RIGHT
)
4095 ret
= CONTIG_LEFTRIGHT
;
4096 else if (ret
== CONTIG_NONE
)
4102 ocfs2_free_path(left_path
);
4104 ocfs2_free_path(right_path
);
4109 static void ocfs2_figure_contig_type(struct inode
*inode
,
4110 struct ocfs2_insert_type
*insert
,
4111 struct ocfs2_extent_list
*el
,
4112 struct ocfs2_extent_rec
*insert_rec
,
4113 struct ocfs2_extent_tree
*et
)
4116 enum ocfs2_contig_type contig_type
= CONTIG_NONE
;
4118 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4120 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
4121 contig_type
= ocfs2_extent_contig(inode
, &el
->l_recs
[i
],
4123 if (contig_type
!= CONTIG_NONE
) {
4124 insert
->ins_contig_index
= i
;
4128 insert
->ins_contig
= contig_type
;
4130 if (insert
->ins_contig
!= CONTIG_NONE
) {
4131 struct ocfs2_extent_rec
*rec
=
4132 &el
->l_recs
[insert
->ins_contig_index
];
4133 unsigned int len
= le16_to_cpu(rec
->e_leaf_clusters
) +
4134 le16_to_cpu(insert_rec
->e_leaf_clusters
);
4137 * Caller might want us to limit the size of extents, don't
4138 * calculate contiguousness if we might exceed that limit.
4140 if (et
->et_max_leaf_clusters
&&
4141 (len
> et
->et_max_leaf_clusters
))
4142 insert
->ins_contig
= CONTIG_NONE
;
4147 * This should only be called against the righmost leaf extent list.
4149 * ocfs2_figure_appending_type() will figure out whether we'll have to
4150 * insert at the tail of the rightmost leaf.
4152 * This should also work against the root extent list for tree's with 0
4153 * depth. If we consider the root extent list to be the rightmost leaf node
4154 * then the logic here makes sense.
4156 static void ocfs2_figure_appending_type(struct ocfs2_insert_type
*insert
,
4157 struct ocfs2_extent_list
*el
,
4158 struct ocfs2_extent_rec
*insert_rec
)
4161 u32 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4162 struct ocfs2_extent_rec
*rec
;
4164 insert
->ins_appending
= APPEND_NONE
;
4166 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4168 if (!el
->l_next_free_rec
)
4169 goto set_tail_append
;
4171 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
4172 /* Were all records empty? */
4173 if (le16_to_cpu(el
->l_next_free_rec
) == 1)
4174 goto set_tail_append
;
4177 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
4178 rec
= &el
->l_recs
[i
];
4181 (le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)))
4182 goto set_tail_append
;
4187 insert
->ins_appending
= APPEND_TAIL
;
4191 * Helper function called at the begining of an insert.
4193 * This computes a few things that are commonly used in the process of
4194 * inserting into the btree:
4195 * - Whether the new extent is contiguous with an existing one.
4196 * - The current tree depth.
4197 * - Whether the insert is an appending one.
4198 * - The total # of free records in the tree.
4200 * All of the information is stored on the ocfs2_insert_type
4203 static int ocfs2_figure_insert_type(struct inode
*inode
,
4204 struct ocfs2_extent_tree
*et
,
4205 struct buffer_head
**last_eb_bh
,
4206 struct ocfs2_extent_rec
*insert_rec
,
4208 struct ocfs2_insert_type
*insert
)
4211 struct ocfs2_extent_block
*eb
;
4212 struct ocfs2_extent_list
*el
;
4213 struct ocfs2_path
*path
= NULL
;
4214 struct buffer_head
*bh
= NULL
;
4216 insert
->ins_split
= SPLIT_NONE
;
4218 el
= et
->et_root_el
;
4219 insert
->ins_tree_depth
= le16_to_cpu(el
->l_tree_depth
);
4221 if (el
->l_tree_depth
) {
4223 * If we have tree depth, we read in the
4224 * rightmost extent block ahead of time as
4225 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4226 * may want it later.
4228 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
),
4229 ocfs2_et_get_last_eb_blk(et
), &bh
,
4230 OCFS2_BH_CACHED
, inode
);
4235 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
4240 * Unless we have a contiguous insert, we'll need to know if
4241 * there is room left in our allocation tree for another
4244 * XXX: This test is simplistic, we can search for empty
4245 * extent records too.
4247 *free_records
= le16_to_cpu(el
->l_count
) -
4248 le16_to_cpu(el
->l_next_free_rec
);
4250 if (!insert
->ins_tree_depth
) {
4251 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4252 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4256 path
= ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
);
4264 * In the case that we're inserting past what the tree
4265 * currently accounts for, ocfs2_find_path() will return for
4266 * us the rightmost tree path. This is accounted for below in
4267 * the appending code.
4269 ret
= ocfs2_find_path(inode
, path
, le32_to_cpu(insert_rec
->e_cpos
));
4275 el
= path_leaf_el(path
);
4278 * Now that we have the path, there's two things we want to determine:
4279 * 1) Contiguousness (also set contig_index if this is so)
4281 * 2) Are we doing an append? We can trivially break this up
4282 * into two types of appends: simple record append, or a
4283 * rotate inside the tail leaf.
4285 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4288 * The insert code isn't quite ready to deal with all cases of
4289 * left contiguousness. Specifically, if it's an insert into
4290 * the 1st record in a leaf, it will require the adjustment of
4291 * cluster count on the last record of the path directly to it's
4292 * left. For now, just catch that case and fool the layers
4293 * above us. This works just fine for tree_depth == 0, which
4294 * is why we allow that above.
4296 if (insert
->ins_contig
== CONTIG_LEFT
&&
4297 insert
->ins_contig_index
== 0)
4298 insert
->ins_contig
= CONTIG_NONE
;
4301 * Ok, so we can simply compare against last_eb to figure out
4302 * whether the path doesn't exist. This will only happen in
4303 * the case that we're doing a tail append, so maybe we can
4304 * take advantage of that information somehow.
4306 if (ocfs2_et_get_last_eb_blk(et
) ==
4307 path_leaf_bh(path
)->b_blocknr
) {
4309 * Ok, ocfs2_find_path() returned us the rightmost
4310 * tree path. This might be an appending insert. There are
4312 * 1) We're doing a true append at the tail:
4313 * -This might even be off the end of the leaf
4314 * 2) We're "appending" by rotating in the tail
4316 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4320 ocfs2_free_path(path
);
4330 * Insert an extent into an inode btree.
4332 * The caller needs to update fe->i_clusters
4334 static int ocfs2_insert_extent(struct ocfs2_super
*osb
,
4336 struct inode
*inode
,
4337 struct buffer_head
*root_bh
,
4342 struct ocfs2_alloc_context
*meta_ac
,
4343 struct ocfs2_extent_tree
*et
)
4346 int uninitialized_var(free_records
);
4347 struct buffer_head
*last_eb_bh
= NULL
;
4348 struct ocfs2_insert_type insert
= {0, };
4349 struct ocfs2_extent_rec rec
;
4351 BUG_ON(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
);
4353 mlog(0, "add %u clusters at position %u to inode %llu\n",
4354 new_clusters
, cpos
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4356 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb
) &&
4357 (OCFS2_I(inode
)->ip_clusters
!= cpos
),
4358 "Device %s, asking for sparse allocation: inode %llu, "
4359 "cpos %u, clusters %u\n",
4361 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
,
4362 OCFS2_I(inode
)->ip_clusters
);
4364 memset(&rec
, 0, sizeof(rec
));
4365 rec
.e_cpos
= cpu_to_le32(cpos
);
4366 rec
.e_blkno
= cpu_to_le64(start_blk
);
4367 rec
.e_leaf_clusters
= cpu_to_le16(new_clusters
);
4368 rec
.e_flags
= flags
;
4370 status
= ocfs2_figure_insert_type(inode
, et
, &last_eb_bh
, &rec
,
4371 &free_records
, &insert
);
4377 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4378 "Insert.contig_index: %d, Insert.free_records: %d, "
4379 "Insert.tree_depth: %d\n",
4380 insert
.ins_appending
, insert
.ins_contig
, insert
.ins_contig_index
,
4381 free_records
, insert
.ins_tree_depth
);
4383 if (insert
.ins_contig
== CONTIG_NONE
&& free_records
== 0) {
4384 status
= ocfs2_grow_tree(inode
, handle
, et
,
4385 &insert
.ins_tree_depth
, &last_eb_bh
,
4393 /* Finally, we can add clusters. This might rotate the tree for us. */
4394 status
= ocfs2_do_insert_extent(inode
, handle
, et
, &rec
, &insert
);
4397 else if (et
->et_type
== OCFS2_DINODE_EXTENT
)
4398 ocfs2_extent_map_insert_rec(inode
, &rec
);
4408 int ocfs2_dinode_insert_extent(struct ocfs2_super
*osb
,
4410 struct inode
*inode
,
4411 struct buffer_head
*root_bh
,
4416 struct ocfs2_alloc_context
*meta_ac
)
4419 struct ocfs2_extent_tree et
;
4421 ocfs2_get_extent_tree(&et
, inode
, root_bh
, OCFS2_DINODE_EXTENT
,
4423 status
= ocfs2_insert_extent(osb
, handle
, inode
, root_bh
,
4424 cpos
, start_blk
, new_clusters
,
4425 flags
, meta_ac
, &et
);
4426 ocfs2_put_extent_tree(&et
);
4431 int ocfs2_xattr_value_insert_extent(struct ocfs2_super
*osb
,
4433 struct inode
*inode
,
4434 struct buffer_head
*root_bh
,
4439 struct ocfs2_alloc_context
*meta_ac
,
4443 struct ocfs2_extent_tree et
;
4445 ocfs2_get_extent_tree(&et
, inode
, root_bh
,
4446 OCFS2_XATTR_VALUE_EXTENT
, obj
);
4447 status
= ocfs2_insert_extent(osb
, handle
, inode
, root_bh
,
4448 cpos
, start_blk
, new_clusters
,
4449 flags
, meta_ac
, &et
);
4450 ocfs2_put_extent_tree(&et
);
4455 int ocfs2_xattr_tree_insert_extent(struct ocfs2_super
*osb
,
4457 struct inode
*inode
,
4458 struct buffer_head
*root_bh
,
4463 struct ocfs2_alloc_context
*meta_ac
)
4466 struct ocfs2_extent_tree et
;
4468 ocfs2_get_extent_tree(&et
, inode
, root_bh
, OCFS2_XATTR_TREE_EXTENT
,
4470 status
= ocfs2_insert_extent(osb
, handle
, inode
, root_bh
,
4471 cpos
, start_blk
, new_clusters
,
4472 flags
, meta_ac
, &et
);
4473 ocfs2_put_extent_tree(&et
);
4479 * Allcate and add clusters into the extent b-tree.
4480 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4481 * The extent b-tree's root is root_el and it should be in root_bh, and
4482 * it is not limited to the file storage. Any extent tree can use this
4483 * function if it implements the proper ocfs2_extent_tree.
4485 int ocfs2_add_clusters_in_btree(struct ocfs2_super
*osb
,
4486 struct inode
*inode
,
4487 u32
*logical_offset
,
4488 u32 clusters_to_add
,
4490 struct buffer_head
*root_bh
,
4491 struct ocfs2_extent_list
*root_el
,
4493 struct ocfs2_alloc_context
*data_ac
,
4494 struct ocfs2_alloc_context
*meta_ac
,
4495 enum ocfs2_alloc_restarted
*reason_ret
,
4496 enum ocfs2_extent_tree_type type
,
4501 enum ocfs2_alloc_restarted reason
= RESTART_NONE
;
4502 u32 bit_off
, num_bits
;
4506 BUG_ON(!clusters_to_add
);
4509 flags
= OCFS2_EXT_UNWRITTEN
;
4511 free_extents
= ocfs2_num_free_extents(osb
, inode
, root_bh
, type
,
4513 if (free_extents
< 0) {
4514 status
= free_extents
;
4519 /* there are two cases which could cause us to EAGAIN in the
4520 * we-need-more-metadata case:
4521 * 1) we haven't reserved *any*
4522 * 2) we are so fragmented, we've needed to add metadata too
4524 if (!free_extents
&& !meta_ac
) {
4525 mlog(0, "we haven't reserved any metadata!\n");
4527 reason
= RESTART_META
;
4529 } else if ((!free_extents
)
4530 && (ocfs2_alloc_context_bits_left(meta_ac
)
4531 < ocfs2_extend_meta_needed(root_el
))) {
4532 mlog(0, "filesystem is really fragmented...\n");
4534 reason
= RESTART_META
;
4538 status
= __ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
4539 clusters_to_add
, &bit_off
, &num_bits
);
4541 if (status
!= -ENOSPC
)
4546 BUG_ON(num_bits
> clusters_to_add
);
4548 /* reserve our write early -- insert_extent may update the inode */
4549 status
= ocfs2_journal_access(handle
, inode
, root_bh
,
4550 OCFS2_JOURNAL_ACCESS_WRITE
);
4556 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
4557 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4558 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4559 if (type
== OCFS2_DINODE_EXTENT
)
4560 status
= ocfs2_dinode_insert_extent(osb
, handle
, inode
, root_bh
,
4561 *logical_offset
, block
,
4562 num_bits
, flags
, meta_ac
);
4563 else if (type
== OCFS2_XATTR_TREE_EXTENT
)
4564 status
= ocfs2_xattr_tree_insert_extent(osb
, handle
,
4567 block
, num_bits
, flags
,
4570 status
= ocfs2_xattr_value_insert_extent(osb
, handle
,
4573 block
, num_bits
, flags
,
4580 status
= ocfs2_journal_dirty(handle
, root_bh
);
4586 clusters_to_add
-= num_bits
;
4587 *logical_offset
+= num_bits
;
4589 if (clusters_to_add
) {
4590 mlog(0, "need to alloc once more, wanted = %u\n",
4593 reason
= RESTART_TRANS
;
4599 *reason_ret
= reason
;
4603 static void ocfs2_make_right_split_rec(struct super_block
*sb
,
4604 struct ocfs2_extent_rec
*split_rec
,
4606 struct ocfs2_extent_rec
*rec
)
4608 u32 rec_cpos
= le32_to_cpu(rec
->e_cpos
);
4609 u32 rec_range
= rec_cpos
+ le16_to_cpu(rec
->e_leaf_clusters
);
4611 memset(split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4613 split_rec
->e_cpos
= cpu_to_le32(cpos
);
4614 split_rec
->e_leaf_clusters
= cpu_to_le16(rec_range
- cpos
);
4616 split_rec
->e_blkno
= rec
->e_blkno
;
4617 le64_add_cpu(&split_rec
->e_blkno
,
4618 ocfs2_clusters_to_blocks(sb
, cpos
- rec_cpos
));
4620 split_rec
->e_flags
= rec
->e_flags
;
4623 static int ocfs2_split_and_insert(struct inode
*inode
,
4625 struct ocfs2_path
*path
,
4626 struct ocfs2_extent_tree
*et
,
4627 struct buffer_head
**last_eb_bh
,
4629 struct ocfs2_extent_rec
*orig_split_rec
,
4630 struct ocfs2_alloc_context
*meta_ac
)
4633 unsigned int insert_range
, rec_range
, do_leftright
= 0;
4634 struct ocfs2_extent_rec tmprec
;
4635 struct ocfs2_extent_list
*rightmost_el
;
4636 struct ocfs2_extent_rec rec
;
4637 struct ocfs2_extent_rec split_rec
= *orig_split_rec
;
4638 struct ocfs2_insert_type insert
;
4639 struct ocfs2_extent_block
*eb
;
4643 * Store a copy of the record on the stack - it might move
4644 * around as the tree is manipulated below.
4646 rec
= path_leaf_el(path
)->l_recs
[split_index
];
4648 rightmost_el
= et
->et_root_el
;
4650 depth
= le16_to_cpu(rightmost_el
->l_tree_depth
);
4652 BUG_ON(!(*last_eb_bh
));
4653 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
4654 rightmost_el
= &eb
->h_list
;
4657 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4658 le16_to_cpu(rightmost_el
->l_count
)) {
4659 ret
= ocfs2_grow_tree(inode
, handle
, et
,
4660 &depth
, last_eb_bh
, meta_ac
);
4667 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4668 insert
.ins_appending
= APPEND_NONE
;
4669 insert
.ins_contig
= CONTIG_NONE
;
4670 insert
.ins_tree_depth
= depth
;
4672 insert_range
= le32_to_cpu(split_rec
.e_cpos
) +
4673 le16_to_cpu(split_rec
.e_leaf_clusters
);
4674 rec_range
= le32_to_cpu(rec
.e_cpos
) +
4675 le16_to_cpu(rec
.e_leaf_clusters
);
4677 if (split_rec
.e_cpos
== rec
.e_cpos
) {
4678 insert
.ins_split
= SPLIT_LEFT
;
4679 } else if (insert_range
== rec_range
) {
4680 insert
.ins_split
= SPLIT_RIGHT
;
4683 * Left/right split. We fake this as a right split
4684 * first and then make a second pass as a left split.
4686 insert
.ins_split
= SPLIT_RIGHT
;
4688 ocfs2_make_right_split_rec(inode
->i_sb
, &tmprec
, insert_range
,
4693 BUG_ON(do_leftright
);
4697 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
4703 if (do_leftright
== 1) {
4705 struct ocfs2_extent_list
*el
;
4708 split_rec
= *orig_split_rec
;
4710 ocfs2_reinit_path(path
, 1);
4712 cpos
= le32_to_cpu(split_rec
.e_cpos
);
4713 ret
= ocfs2_find_path(inode
, path
, cpos
);
4719 el
= path_leaf_el(path
);
4720 split_index
= ocfs2_search_extent_list(el
, cpos
);
4729 * Mark part or all of the extent record at split_index in the leaf
4730 * pointed to by path as written. This removes the unwritten
4733 * Care is taken to handle contiguousness so as to not grow the tree.
4735 * meta_ac is not strictly necessary - we only truly need it if growth
4736 * of the tree is required. All other cases will degrade into a less
4737 * optimal tree layout.
4739 * last_eb_bh should be the rightmost leaf block for any extent
4740 * btree. Since a split may grow the tree or a merge might shrink it,
4741 * the caller cannot trust the contents of that buffer after this call.
4743 * This code is optimized for readability - several passes might be
4744 * made over certain portions of the tree. All of those blocks will
4745 * have been brought into cache (and pinned via the journal), so the
4746 * extra overhead is not expressed in terms of disk reads.
4748 static int __ocfs2_mark_extent_written(struct inode
*inode
,
4749 struct ocfs2_extent_tree
*et
,
4751 struct ocfs2_path
*path
,
4753 struct ocfs2_extent_rec
*split_rec
,
4754 struct ocfs2_alloc_context
*meta_ac
,
4755 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
4758 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
4759 struct buffer_head
*last_eb_bh
= NULL
;
4760 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
4761 struct ocfs2_merge_ctxt ctxt
;
4762 struct ocfs2_extent_list
*rightmost_el
;
4764 if (!(rec
->e_flags
& OCFS2_EXT_UNWRITTEN
)) {
4770 if (le32_to_cpu(rec
->e_cpos
) > le32_to_cpu(split_rec
->e_cpos
) ||
4771 ((le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)) <
4772 (le32_to_cpu(split_rec
->e_cpos
) + le16_to_cpu(split_rec
->e_leaf_clusters
)))) {
4778 ctxt
.c_contig_type
= ocfs2_figure_merge_contig_type(inode
, path
, el
,
4783 * The core merge / split code wants to know how much room is
4784 * left in this inodes allocation tree, so we pass the
4785 * rightmost extent list.
4787 if (path
->p_tree_depth
) {
4788 struct ocfs2_extent_block
*eb
;
4790 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
),
4791 ocfs2_et_get_last_eb_blk(et
),
4792 &last_eb_bh
, OCFS2_BH_CACHED
, inode
);
4798 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
4799 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
4800 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
4805 rightmost_el
= &eb
->h_list
;
4807 rightmost_el
= path_root_el(path
);
4809 if (rec
->e_cpos
== split_rec
->e_cpos
&&
4810 rec
->e_leaf_clusters
== split_rec
->e_leaf_clusters
)
4811 ctxt
.c_split_covers_rec
= 1;
4813 ctxt
.c_split_covers_rec
= 0;
4815 ctxt
.c_has_empty_extent
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
4817 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4818 split_index
, ctxt
.c_contig_type
, ctxt
.c_has_empty_extent
,
4819 ctxt
.c_split_covers_rec
);
4821 if (ctxt
.c_contig_type
== CONTIG_NONE
) {
4822 if (ctxt
.c_split_covers_rec
)
4823 el
->l_recs
[split_index
] = *split_rec
;
4825 ret
= ocfs2_split_and_insert(inode
, handle
, path
, et
,
4826 &last_eb_bh
, split_index
,
4827 split_rec
, meta_ac
);
4831 ret
= ocfs2_try_to_merge_extent(inode
, handle
, path
,
4832 split_index
, split_rec
,
4833 dealloc
, &ctxt
, et
);
4844 * Mark the already-existing extent at cpos as written for len clusters.
4846 * If the existing extent is larger than the request, initiate a
4847 * split. An attempt will be made at merging with adjacent extents.
4849 * The caller is responsible for passing down meta_ac if we'll need it.
4851 int ocfs2_mark_extent_written(struct inode
*inode
, struct buffer_head
*root_bh
,
4852 handle_t
*handle
, u32 cpos
, u32 len
, u32 phys
,
4853 struct ocfs2_alloc_context
*meta_ac
,
4854 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
4855 enum ocfs2_extent_tree_type et_type
,
4859 u64 start_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys
);
4860 struct ocfs2_extent_rec split_rec
;
4861 struct ocfs2_path
*left_path
= NULL
;
4862 struct ocfs2_extent_list
*el
;
4863 struct ocfs2_extent_tree et
;
4865 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4866 inode
->i_ino
, cpos
, len
, phys
, (unsigned long long)start_blkno
);
4868 ocfs2_get_extent_tree(&et
, inode
, root_bh
, et_type
, obj
);
4870 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode
->i_sb
))) {
4871 ocfs2_error(inode
->i_sb
, "Inode %llu has unwritten extents "
4872 "that are being written to, but the feature bit "
4873 "is not set in the super block.",
4874 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4880 * XXX: This should be fixed up so that we just re-insert the
4881 * next extent records.
4883 if (et_type
== OCFS2_DINODE_EXTENT
)
4884 ocfs2_extent_map_trunc(inode
, 0);
4886 left_path
= ocfs2_new_path(et
.et_root_bh
, et
.et_root_el
);
4893 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
4898 el
= path_leaf_el(left_path
);
4900 index
= ocfs2_search_extent_list(el
, cpos
);
4901 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
4902 ocfs2_error(inode
->i_sb
,
4903 "Inode %llu has an extent at cpos %u which can no "
4904 "longer be found.\n",
4905 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
4910 memset(&split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4911 split_rec
.e_cpos
= cpu_to_le32(cpos
);
4912 split_rec
.e_leaf_clusters
= cpu_to_le16(len
);
4913 split_rec
.e_blkno
= cpu_to_le64(start_blkno
);
4914 split_rec
.e_flags
= path_leaf_el(left_path
)->l_recs
[index
].e_flags
;
4915 split_rec
.e_flags
&= ~OCFS2_EXT_UNWRITTEN
;
4917 ret
= __ocfs2_mark_extent_written(inode
, &et
, handle
, left_path
,
4918 index
, &split_rec
, meta_ac
,
4924 ocfs2_free_path(left_path
);
4925 ocfs2_put_extent_tree(&et
);
4929 static int ocfs2_split_tree(struct inode
*inode
, struct ocfs2_extent_tree
*et
,
4930 handle_t
*handle
, struct ocfs2_path
*path
,
4931 int index
, u32 new_range
,
4932 struct ocfs2_alloc_context
*meta_ac
)
4934 int ret
, depth
, credits
= handle
->h_buffer_credits
;
4935 struct buffer_head
*last_eb_bh
= NULL
;
4936 struct ocfs2_extent_block
*eb
;
4937 struct ocfs2_extent_list
*rightmost_el
, *el
;
4938 struct ocfs2_extent_rec split_rec
;
4939 struct ocfs2_extent_rec
*rec
;
4940 struct ocfs2_insert_type insert
;
4943 * Setup the record to split before we grow the tree.
4945 el
= path_leaf_el(path
);
4946 rec
= &el
->l_recs
[index
];
4947 ocfs2_make_right_split_rec(inode
->i_sb
, &split_rec
, new_range
, rec
);
4949 depth
= path
->p_tree_depth
;
4951 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
),
4952 ocfs2_et_get_last_eb_blk(et
),
4953 &last_eb_bh
, OCFS2_BH_CACHED
, inode
);
4959 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
4960 rightmost_el
= &eb
->h_list
;
4962 rightmost_el
= path_leaf_el(path
);
4964 credits
+= path
->p_tree_depth
+
4965 ocfs2_extend_meta_needed(et
->et_root_el
);
4966 ret
= ocfs2_extend_trans(handle
, credits
);
4972 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4973 le16_to_cpu(rightmost_el
->l_count
)) {
4974 ret
= ocfs2_grow_tree(inode
, handle
, et
, &depth
, &last_eb_bh
,
4982 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4983 insert
.ins_appending
= APPEND_NONE
;
4984 insert
.ins_contig
= CONTIG_NONE
;
4985 insert
.ins_split
= SPLIT_RIGHT
;
4986 insert
.ins_tree_depth
= depth
;
4988 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
4997 static int ocfs2_truncate_rec(struct inode
*inode
, handle_t
*handle
,
4998 struct ocfs2_path
*path
, int index
,
4999 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5001 struct ocfs2_extent_tree
*et
)
5004 u32 left_cpos
, rec_range
, trunc_range
;
5005 int wants_rotate
= 0, is_rightmost_tree_rec
= 0;
5006 struct super_block
*sb
= inode
->i_sb
;
5007 struct ocfs2_path
*left_path
= NULL
;
5008 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
5009 struct ocfs2_extent_rec
*rec
;
5010 struct ocfs2_extent_block
*eb
;
5012 if (ocfs2_is_empty_extent(&el
->l_recs
[0]) && index
> 0) {
5013 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5022 if (index
== (le16_to_cpu(el
->l_next_free_rec
) - 1) &&
5023 path
->p_tree_depth
) {
5025 * Check whether this is the rightmost tree record. If
5026 * we remove all of this record or part of its right
5027 * edge then an update of the record lengths above it
5030 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
5031 if (eb
->h_next_leaf_blk
== 0)
5032 is_rightmost_tree_rec
= 1;
5035 rec
= &el
->l_recs
[index
];
5036 if (index
== 0 && path
->p_tree_depth
&&
5037 le32_to_cpu(rec
->e_cpos
) == cpos
) {
5039 * Changing the leftmost offset (via partial or whole
5040 * record truncate) of an interior (or rightmost) path
5041 * means we have to update the subtree that is formed
5042 * by this leaf and the one to it's left.
5044 * There are two cases we can skip:
5045 * 1) Path is the leftmost one in our inode tree.
5046 * 2) The leaf is rightmost and will be empty after
5047 * we remove the extent record - the rotate code
5048 * knows how to update the newly formed edge.
5051 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
,
5058 if (left_cpos
&& le16_to_cpu(el
->l_next_free_rec
) > 1) {
5059 left_path
= ocfs2_new_path(path_root_bh(path
),
5060 path_root_el(path
));
5067 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
5075 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
5076 handle
->h_buffer_credits
,
5083 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
5089 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
5095 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5096 trunc_range
= cpos
+ len
;
5098 if (le32_to_cpu(rec
->e_cpos
) == cpos
&& rec_range
== trunc_range
) {
5101 memset(rec
, 0, sizeof(*rec
));
5102 ocfs2_cleanup_merge(el
, index
);
5105 next_free
= le16_to_cpu(el
->l_next_free_rec
);
5106 if (is_rightmost_tree_rec
&& next_free
> 1) {
5108 * We skip the edge update if this path will
5109 * be deleted by the rotate code.
5111 rec
= &el
->l_recs
[next_free
- 1];
5112 ocfs2_adjust_rightmost_records(inode
, handle
, path
,
5115 } else if (le32_to_cpu(rec
->e_cpos
) == cpos
) {
5116 /* Remove leftmost portion of the record. */
5117 le32_add_cpu(&rec
->e_cpos
, len
);
5118 le64_add_cpu(&rec
->e_blkno
, ocfs2_clusters_to_blocks(sb
, len
));
5119 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5120 } else if (rec_range
== trunc_range
) {
5121 /* Remove rightmost portion of the record */
5122 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5123 if (is_rightmost_tree_rec
)
5124 ocfs2_adjust_rightmost_records(inode
, handle
, path
, rec
);
5126 /* Caller should have trapped this. */
5127 mlog(ML_ERROR
, "Inode %llu: Invalid record truncate: (%u, %u) "
5128 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5129 le32_to_cpu(rec
->e_cpos
),
5130 le16_to_cpu(rec
->e_leaf_clusters
), cpos
, len
);
5137 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
5138 ocfs2_complete_edge_insert(inode
, handle
, left_path
, path
,
5142 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
5144 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5151 ocfs2_free_path(left_path
);
5155 int ocfs2_remove_extent(struct inode
*inode
, struct buffer_head
*root_bh
,
5156 u32 cpos
, u32 len
, handle_t
*handle
,
5157 struct ocfs2_alloc_context
*meta_ac
,
5158 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5159 enum ocfs2_extent_tree_type et_type
,
5163 u32 rec_range
, trunc_range
;
5164 struct ocfs2_extent_rec
*rec
;
5165 struct ocfs2_extent_list
*el
;
5166 struct ocfs2_path
*path
= NULL
;
5167 struct ocfs2_extent_tree et
;
5169 ocfs2_get_extent_tree(&et
, inode
, root_bh
, et_type
, obj
);
5171 ocfs2_extent_map_trunc(inode
, 0);
5173 path
= ocfs2_new_path(et
.et_root_bh
, et
.et_root_el
);
5180 ret
= ocfs2_find_path(inode
, path
, cpos
);
5186 el
= path_leaf_el(path
);
5187 index
= ocfs2_search_extent_list(el
, cpos
);
5188 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5189 ocfs2_error(inode
->i_sb
,
5190 "Inode %llu has an extent at cpos %u which can no "
5191 "longer be found.\n",
5192 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
5198 * We have 3 cases of extent removal:
5199 * 1) Range covers the entire extent rec
5200 * 2) Range begins or ends on one edge of the extent rec
5201 * 3) Range is in the middle of the extent rec (no shared edges)
5203 * For case 1 we remove the extent rec and left rotate to
5206 * For case 2 we just shrink the existing extent rec, with a
5207 * tree update if the shrinking edge is also the edge of an
5210 * For case 3 we do a right split to turn the extent rec into
5211 * something case 2 can handle.
5213 rec
= &el
->l_recs
[index
];
5214 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5215 trunc_range
= cpos
+ len
;
5217 BUG_ON(cpos
< le32_to_cpu(rec
->e_cpos
) || trunc_range
> rec_range
);
5219 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5220 "(cpos %u, len %u)\n",
5221 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
, len
, index
,
5222 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
));
5224 if (le32_to_cpu(rec
->e_cpos
) == cpos
|| rec_range
== trunc_range
) {
5225 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5232 ret
= ocfs2_split_tree(inode
, &et
, handle
, path
, index
,
5233 trunc_range
, meta_ac
);
5240 * The split could have manipulated the tree enough to
5241 * move the record location, so we have to look for it again.
5243 ocfs2_reinit_path(path
, 1);
5245 ret
= ocfs2_find_path(inode
, path
, cpos
);
5251 el
= path_leaf_el(path
);
5252 index
= ocfs2_search_extent_list(el
, cpos
);
5253 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5254 ocfs2_error(inode
->i_sb
,
5255 "Inode %llu: split at cpos %u lost record.",
5256 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5263 * Double check our values here. If anything is fishy,
5264 * it's easier to catch it at the top level.
5266 rec
= &el
->l_recs
[index
];
5267 rec_range
= le32_to_cpu(rec
->e_cpos
) +
5268 ocfs2_rec_clusters(el
, rec
);
5269 if (rec_range
!= trunc_range
) {
5270 ocfs2_error(inode
->i_sb
,
5271 "Inode %llu: error after split at cpos %u"
5272 "trunc len %u, existing record is (%u,%u)",
5273 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5274 cpos
, len
, le32_to_cpu(rec
->e_cpos
),
5275 ocfs2_rec_clusters(el
, rec
));
5280 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5289 ocfs2_free_path(path
);
5290 ocfs2_put_extent_tree(&et
);
5294 int ocfs2_truncate_log_needs_flush(struct ocfs2_super
*osb
)
5296 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5297 struct ocfs2_dinode
*di
;
5298 struct ocfs2_truncate_log
*tl
;
5300 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5301 tl
= &di
->id2
.i_dealloc
;
5303 mlog_bug_on_msg(le16_to_cpu(tl
->tl_used
) > le16_to_cpu(tl
->tl_count
),
5304 "slot %d, invalid truncate log parameters: used = "
5305 "%u, count = %u\n", osb
->slot_num
,
5306 le16_to_cpu(tl
->tl_used
), le16_to_cpu(tl
->tl_count
));
5307 return le16_to_cpu(tl
->tl_used
) == le16_to_cpu(tl
->tl_count
);
5310 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log
*tl
,
5311 unsigned int new_start
)
5313 unsigned int tail_index
;
5314 unsigned int current_tail
;
5316 /* No records, nothing to coalesce */
5317 if (!le16_to_cpu(tl
->tl_used
))
5320 tail_index
= le16_to_cpu(tl
->tl_used
) - 1;
5321 current_tail
= le32_to_cpu(tl
->tl_recs
[tail_index
].t_start
);
5322 current_tail
+= le32_to_cpu(tl
->tl_recs
[tail_index
].t_clusters
);
5324 return current_tail
== new_start
;
5327 int ocfs2_truncate_log_append(struct ocfs2_super
*osb
,
5330 unsigned int num_clusters
)
5333 unsigned int start_cluster
, tl_count
;
5334 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5335 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5336 struct ocfs2_dinode
*di
;
5337 struct ocfs2_truncate_log
*tl
;
5339 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5340 (unsigned long long)start_blk
, num_clusters
);
5342 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5344 start_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, start_blk
);
5346 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5347 tl
= &di
->id2
.i_dealloc
;
5348 if (!OCFS2_IS_VALID_DINODE(di
)) {
5349 OCFS2_RO_ON_INVALID_DINODE(osb
->sb
, di
);
5354 tl_count
= le16_to_cpu(tl
->tl_count
);
5355 mlog_bug_on_msg(tl_count
> ocfs2_truncate_recs_per_inode(osb
->sb
) ||
5357 "Truncate record count on #%llu invalid "
5358 "wanted %u, actual %u\n",
5359 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
,
5360 ocfs2_truncate_recs_per_inode(osb
->sb
),
5361 le16_to_cpu(tl
->tl_count
));
5363 /* Caller should have known to flush before calling us. */
5364 index
= le16_to_cpu(tl
->tl_used
);
5365 if (index
>= tl_count
) {
5371 status
= ocfs2_journal_access(handle
, tl_inode
, tl_bh
,
5372 OCFS2_JOURNAL_ACCESS_WRITE
);
5378 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5379 "%llu (index = %d)\n", num_clusters
, start_cluster
,
5380 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
, index
);
5382 if (ocfs2_truncate_log_can_coalesce(tl
, start_cluster
)) {
5384 * Move index back to the record we are coalescing with.
5385 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5389 num_clusters
+= le32_to_cpu(tl
->tl_recs
[index
].t_clusters
);
5390 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5391 index
, le32_to_cpu(tl
->tl_recs
[index
].t_start
),
5394 tl
->tl_recs
[index
].t_start
= cpu_to_le32(start_cluster
);
5395 tl
->tl_used
= cpu_to_le16(index
+ 1);
5397 tl
->tl_recs
[index
].t_clusters
= cpu_to_le32(num_clusters
);
5399 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5410 static int ocfs2_replay_truncate_records(struct ocfs2_super
*osb
,
5412 struct inode
*data_alloc_inode
,
5413 struct buffer_head
*data_alloc_bh
)
5417 unsigned int num_clusters
;
5419 struct ocfs2_truncate_rec rec
;
5420 struct ocfs2_dinode
*di
;
5421 struct ocfs2_truncate_log
*tl
;
5422 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5423 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5427 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5428 tl
= &di
->id2
.i_dealloc
;
5429 i
= le16_to_cpu(tl
->tl_used
) - 1;
5431 /* Caller has given us at least enough credits to
5432 * update the truncate log dinode */
5433 status
= ocfs2_journal_access(handle
, tl_inode
, tl_bh
,
5434 OCFS2_JOURNAL_ACCESS_WRITE
);
5440 tl
->tl_used
= cpu_to_le16(i
);
5442 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5448 /* TODO: Perhaps we can calculate the bulk of the
5449 * credits up front rather than extending like
5451 status
= ocfs2_extend_trans(handle
,
5452 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC
);
5458 rec
= tl
->tl_recs
[i
];
5459 start_blk
= ocfs2_clusters_to_blocks(data_alloc_inode
->i_sb
,
5460 le32_to_cpu(rec
.t_start
));
5461 num_clusters
= le32_to_cpu(rec
.t_clusters
);
5463 /* if start_blk is not set, we ignore the record as
5466 mlog(0, "free record %d, start = %u, clusters = %u\n",
5467 i
, le32_to_cpu(rec
.t_start
), num_clusters
);
5469 status
= ocfs2_free_clusters(handle
, data_alloc_inode
,
5470 data_alloc_bh
, start_blk
,
5485 /* Expects you to already be holding tl_inode->i_mutex */
5486 int __ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5489 unsigned int num_to_flush
;
5491 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5492 struct inode
*data_alloc_inode
= NULL
;
5493 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5494 struct buffer_head
*data_alloc_bh
= NULL
;
5495 struct ocfs2_dinode
*di
;
5496 struct ocfs2_truncate_log
*tl
;
5500 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5502 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5503 tl
= &di
->id2
.i_dealloc
;
5504 if (!OCFS2_IS_VALID_DINODE(di
)) {
5505 OCFS2_RO_ON_INVALID_DINODE(osb
->sb
, di
);
5510 num_to_flush
= le16_to_cpu(tl
->tl_used
);
5511 mlog(0, "Flush %u records from truncate log #%llu\n",
5512 num_to_flush
, (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
);
5513 if (!num_to_flush
) {
5518 data_alloc_inode
= ocfs2_get_system_file_inode(osb
,
5519 GLOBAL_BITMAP_SYSTEM_INODE
,
5520 OCFS2_INVALID_SLOT
);
5521 if (!data_alloc_inode
) {
5523 mlog(ML_ERROR
, "Could not get bitmap inode!\n");
5527 mutex_lock(&data_alloc_inode
->i_mutex
);
5529 status
= ocfs2_inode_lock(data_alloc_inode
, &data_alloc_bh
, 1);
5535 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5536 if (IS_ERR(handle
)) {
5537 status
= PTR_ERR(handle
);
5542 status
= ocfs2_replay_truncate_records(osb
, handle
, data_alloc_inode
,
5547 ocfs2_commit_trans(osb
, handle
);
5550 brelse(data_alloc_bh
);
5551 ocfs2_inode_unlock(data_alloc_inode
, 1);
5554 mutex_unlock(&data_alloc_inode
->i_mutex
);
5555 iput(data_alloc_inode
);
5562 int ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5565 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5567 mutex_lock(&tl_inode
->i_mutex
);
5568 status
= __ocfs2_flush_truncate_log(osb
);
5569 mutex_unlock(&tl_inode
->i_mutex
);
5574 static void ocfs2_truncate_log_worker(struct work_struct
*work
)
5577 struct ocfs2_super
*osb
=
5578 container_of(work
, struct ocfs2_super
,
5579 osb_truncate_log_wq
.work
);
5583 status
= ocfs2_flush_truncate_log(osb
);
5587 ocfs2_init_inode_steal_slot(osb
);
5592 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5593 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super
*osb
,
5596 if (osb
->osb_tl_inode
) {
5597 /* We want to push off log flushes while truncates are
5600 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5602 queue_delayed_work(ocfs2_wq
, &osb
->osb_truncate_log_wq
,
5603 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL
);
5607 static int ocfs2_get_truncate_log_info(struct ocfs2_super
*osb
,
5609 struct inode
**tl_inode
,
5610 struct buffer_head
**tl_bh
)
5613 struct inode
*inode
= NULL
;
5614 struct buffer_head
*bh
= NULL
;
5616 inode
= ocfs2_get_system_file_inode(osb
,
5617 TRUNCATE_LOG_SYSTEM_INODE
,
5621 mlog(ML_ERROR
, "Could not get load truncate log inode!\n");
5625 status
= ocfs2_read_block(osb
, OCFS2_I(inode
)->ip_blkno
, &bh
,
5626 OCFS2_BH_CACHED
, inode
);
5640 /* called during the 1st stage of node recovery. we stamp a clean
5641 * truncate log and pass back a copy for processing later. if the
5642 * truncate log does not require processing, a *tl_copy is set to
5644 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super
*osb
,
5646 struct ocfs2_dinode
**tl_copy
)
5649 struct inode
*tl_inode
= NULL
;
5650 struct buffer_head
*tl_bh
= NULL
;
5651 struct ocfs2_dinode
*di
;
5652 struct ocfs2_truncate_log
*tl
;
5656 mlog(0, "recover truncate log from slot %d\n", slot_num
);
5658 status
= ocfs2_get_truncate_log_info(osb
, slot_num
, &tl_inode
, &tl_bh
);
5664 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5665 tl
= &di
->id2
.i_dealloc
;
5666 if (!OCFS2_IS_VALID_DINODE(di
)) {
5667 OCFS2_RO_ON_INVALID_DINODE(tl_inode
->i_sb
, di
);
5672 if (le16_to_cpu(tl
->tl_used
)) {
5673 mlog(0, "We'll have %u logs to recover\n",
5674 le16_to_cpu(tl
->tl_used
));
5676 *tl_copy
= kmalloc(tl_bh
->b_size
, GFP_KERNEL
);
5683 /* Assuming the write-out below goes well, this copy
5684 * will be passed back to recovery for processing. */
5685 memcpy(*tl_copy
, tl_bh
->b_data
, tl_bh
->b_size
);
5687 /* All we need to do to clear the truncate log is set
5691 status
= ocfs2_write_block(osb
, tl_bh
, tl_inode
);
5704 if (status
< 0 && (*tl_copy
)) {
5713 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super
*osb
,
5714 struct ocfs2_dinode
*tl_copy
)
5718 unsigned int clusters
, num_recs
, start_cluster
;
5721 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5722 struct ocfs2_truncate_log
*tl
;
5726 if (OCFS2_I(tl_inode
)->ip_blkno
== le64_to_cpu(tl_copy
->i_blkno
)) {
5727 mlog(ML_ERROR
, "Asked to recover my own truncate log!\n");
5731 tl
= &tl_copy
->id2
.i_dealloc
;
5732 num_recs
= le16_to_cpu(tl
->tl_used
);
5733 mlog(0, "cleanup %u records from %llu\n", num_recs
,
5734 (unsigned long long)le64_to_cpu(tl_copy
->i_blkno
));
5736 mutex_lock(&tl_inode
->i_mutex
);
5737 for(i
= 0; i
< num_recs
; i
++) {
5738 if (ocfs2_truncate_log_needs_flush(osb
)) {
5739 status
= __ocfs2_flush_truncate_log(osb
);
5746 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5747 if (IS_ERR(handle
)) {
5748 status
= PTR_ERR(handle
);
5753 clusters
= le32_to_cpu(tl
->tl_recs
[i
].t_clusters
);
5754 start_cluster
= le32_to_cpu(tl
->tl_recs
[i
].t_start
);
5755 start_blk
= ocfs2_clusters_to_blocks(osb
->sb
, start_cluster
);
5757 status
= ocfs2_truncate_log_append(osb
, handle
,
5758 start_blk
, clusters
);
5759 ocfs2_commit_trans(osb
, handle
);
5767 mutex_unlock(&tl_inode
->i_mutex
);
5773 void ocfs2_truncate_log_shutdown(struct ocfs2_super
*osb
)
5776 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5781 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5782 flush_workqueue(ocfs2_wq
);
5784 status
= ocfs2_flush_truncate_log(osb
);
5788 brelse(osb
->osb_tl_bh
);
5789 iput(osb
->osb_tl_inode
);
5795 int ocfs2_truncate_log_init(struct ocfs2_super
*osb
)
5798 struct inode
*tl_inode
= NULL
;
5799 struct buffer_head
*tl_bh
= NULL
;
5803 status
= ocfs2_get_truncate_log_info(osb
,
5810 /* ocfs2_truncate_log_shutdown keys on the existence of
5811 * osb->osb_tl_inode so we don't set any of the osb variables
5812 * until we're sure all is well. */
5813 INIT_DELAYED_WORK(&osb
->osb_truncate_log_wq
,
5814 ocfs2_truncate_log_worker
);
5815 osb
->osb_tl_bh
= tl_bh
;
5816 osb
->osb_tl_inode
= tl_inode
;
5823 * Delayed de-allocation of suballocator blocks.
5825 * Some sets of block de-allocations might involve multiple suballocator inodes.
5827 * The locking for this can get extremely complicated, especially when
5828 * the suballocator inodes to delete from aren't known until deep
5829 * within an unrelated codepath.
5831 * ocfs2_extent_block structures are a good example of this - an inode
5832 * btree could have been grown by any number of nodes each allocating
5833 * out of their own suballoc inode.
5835 * These structures allow the delay of block de-allocation until a
5836 * later time, when locking of multiple cluster inodes won't cause
5841 * Describes a single block free from a suballocator
5843 struct ocfs2_cached_block_free
{
5844 struct ocfs2_cached_block_free
*free_next
;
5846 unsigned int free_bit
;
5849 struct ocfs2_per_slot_free_list
{
5850 struct ocfs2_per_slot_free_list
*f_next_suballocator
;
5853 struct ocfs2_cached_block_free
*f_first
;
5856 static int ocfs2_free_cached_items(struct ocfs2_super
*osb
,
5859 struct ocfs2_cached_block_free
*head
)
5864 struct inode
*inode
;
5865 struct buffer_head
*di_bh
= NULL
;
5866 struct ocfs2_cached_block_free
*tmp
;
5868 inode
= ocfs2_get_system_file_inode(osb
, sysfile_type
, slot
);
5875 mutex_lock(&inode
->i_mutex
);
5877 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
5883 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
5884 if (IS_ERR(handle
)) {
5885 ret
= PTR_ERR(handle
);
5891 bg_blkno
= ocfs2_which_suballoc_group(head
->free_blk
,
5893 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5894 head
->free_bit
, (unsigned long long)head
->free_blk
);
5896 ret
= ocfs2_free_suballoc_bits(handle
, inode
, di_bh
,
5897 head
->free_bit
, bg_blkno
, 1);
5903 ret
= ocfs2_extend_trans(handle
, OCFS2_SUBALLOC_FREE
);
5910 head
= head
->free_next
;
5915 ocfs2_commit_trans(osb
, handle
);
5918 ocfs2_inode_unlock(inode
, 1);
5921 mutex_unlock(&inode
->i_mutex
);
5925 /* Premature exit may have left some dangling items. */
5927 head
= head
->free_next
;
5934 int ocfs2_run_deallocs(struct ocfs2_super
*osb
,
5935 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
5938 struct ocfs2_per_slot_free_list
*fl
;
5943 while (ctxt
->c_first_suballocator
) {
5944 fl
= ctxt
->c_first_suballocator
;
5947 mlog(0, "Free items: (type %u, slot %d)\n",
5948 fl
->f_inode_type
, fl
->f_slot
);
5949 ret2
= ocfs2_free_cached_items(osb
, fl
->f_inode_type
,
5950 fl
->f_slot
, fl
->f_first
);
5957 ctxt
->c_first_suballocator
= fl
->f_next_suballocator
;
5964 static struct ocfs2_per_slot_free_list
*
5965 ocfs2_find_per_slot_free_list(int type
,
5967 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
5969 struct ocfs2_per_slot_free_list
*fl
= ctxt
->c_first_suballocator
;
5972 if (fl
->f_inode_type
== type
&& fl
->f_slot
== slot
)
5975 fl
= fl
->f_next_suballocator
;
5978 fl
= kmalloc(sizeof(*fl
), GFP_NOFS
);
5980 fl
->f_inode_type
= type
;
5983 fl
->f_next_suballocator
= ctxt
->c_first_suballocator
;
5985 ctxt
->c_first_suballocator
= fl
;
5990 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
5991 int type
, int slot
, u64 blkno
,
5995 struct ocfs2_per_slot_free_list
*fl
;
5996 struct ocfs2_cached_block_free
*item
;
5998 fl
= ocfs2_find_per_slot_free_list(type
, slot
, ctxt
);
6005 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6012 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6013 type
, slot
, bit
, (unsigned long long)blkno
);
6015 item
->free_blk
= blkno
;
6016 item
->free_bit
= bit
;
6017 item
->free_next
= fl
->f_first
;
6026 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6027 struct ocfs2_extent_block
*eb
)
6029 return ocfs2_cache_block_dealloc(ctxt
, EXTENT_ALLOC_SYSTEM_INODE
,
6030 le16_to_cpu(eb
->h_suballoc_slot
),
6031 le64_to_cpu(eb
->h_blkno
),
6032 le16_to_cpu(eb
->h_suballoc_bit
));
6035 /* This function will figure out whether the currently last extent
6036 * block will be deleted, and if it will, what the new last extent
6037 * block will be so we can update his h_next_leaf_blk field, as well
6038 * as the dinodes i_last_eb_blk */
6039 static int ocfs2_find_new_last_ext_blk(struct inode
*inode
,
6040 unsigned int clusters_to_del
,
6041 struct ocfs2_path
*path
,
6042 struct buffer_head
**new_last_eb
)
6044 int next_free
, ret
= 0;
6046 struct ocfs2_extent_rec
*rec
;
6047 struct ocfs2_extent_block
*eb
;
6048 struct ocfs2_extent_list
*el
;
6049 struct buffer_head
*bh
= NULL
;
6051 *new_last_eb
= NULL
;
6053 /* we have no tree, so of course, no last_eb. */
6054 if (!path
->p_tree_depth
)
6057 /* trunc to zero special case - this makes tree_depth = 0
6058 * regardless of what it is. */
6059 if (OCFS2_I(inode
)->ip_clusters
== clusters_to_del
)
6062 el
= path_leaf_el(path
);
6063 BUG_ON(!el
->l_next_free_rec
);
6066 * Make sure that this extent list will actually be empty
6067 * after we clear away the data. We can shortcut out if
6068 * there's more than one non-empty extent in the
6069 * list. Otherwise, a check of the remaining extent is
6072 next_free
= le16_to_cpu(el
->l_next_free_rec
);
6074 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6078 /* We may have a valid extent in index 1, check it. */
6080 rec
= &el
->l_recs
[1];
6083 * Fall through - no more nonempty extents, so we want
6084 * to delete this leaf.
6090 rec
= &el
->l_recs
[0];
6095 * Check it we'll only be trimming off the end of this
6098 if (le16_to_cpu(rec
->e_leaf_clusters
) > clusters_to_del
)
6102 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
6108 ret
= ocfs2_find_leaf(inode
, path_root_el(path
), cpos
, &bh
);
6114 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
6116 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
6117 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
6123 get_bh(*new_last_eb
);
6124 mlog(0, "returning block %llu, (cpos: %u)\n",
6125 (unsigned long long)le64_to_cpu(eb
->h_blkno
), cpos
);
6133 * Trim some clusters off the rightmost edge of a tree. Only called
6136 * The caller needs to:
6137 * - start journaling of each path component.
6138 * - compute and fully set up any new last ext block
6140 static int ocfs2_trim_tree(struct inode
*inode
, struct ocfs2_path
*path
,
6141 handle_t
*handle
, struct ocfs2_truncate_context
*tc
,
6142 u32 clusters_to_del
, u64
*delete_start
)
6144 int ret
, i
, index
= path
->p_tree_depth
;
6147 struct buffer_head
*bh
;
6148 struct ocfs2_extent_list
*el
;
6149 struct ocfs2_extent_rec
*rec
;
6153 while (index
>= 0) {
6154 bh
= path
->p_node
[index
].bh
;
6155 el
= path
->p_node
[index
].el
;
6157 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6158 index
, (unsigned long long)bh
->b_blocknr
);
6160 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
6163 (path
->p_tree_depth
- le16_to_cpu(el
->l_tree_depth
))) {
6164 ocfs2_error(inode
->i_sb
,
6165 "Inode %lu has invalid ext. block %llu",
6167 (unsigned long long)bh
->b_blocknr
);
6173 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
6174 rec
= &el
->l_recs
[i
];
6176 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6177 "next = %u\n", i
, le32_to_cpu(rec
->e_cpos
),
6178 ocfs2_rec_clusters(el
, rec
),
6179 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6180 le16_to_cpu(el
->l_next_free_rec
));
6182 BUG_ON(ocfs2_rec_clusters(el
, rec
) < clusters_to_del
);
6184 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
6186 * If the leaf block contains a single empty
6187 * extent and no records, we can just remove
6190 if (i
== 0 && ocfs2_is_empty_extent(rec
)) {
6192 sizeof(struct ocfs2_extent_rec
));
6193 el
->l_next_free_rec
= cpu_to_le16(0);
6199 * Remove any empty extents by shifting things
6200 * left. That should make life much easier on
6201 * the code below. This condition is rare
6202 * enough that we shouldn't see a performance
6205 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6206 le16_add_cpu(&el
->l_next_free_rec
, -1);
6209 i
< le16_to_cpu(el
->l_next_free_rec
); i
++)
6210 el
->l_recs
[i
] = el
->l_recs
[i
+ 1];
6212 memset(&el
->l_recs
[i
], 0,
6213 sizeof(struct ocfs2_extent_rec
));
6216 * We've modified our extent list. The
6217 * simplest way to handle this change
6218 * is to being the search from the
6221 goto find_tail_record
;
6224 le16_add_cpu(&rec
->e_leaf_clusters
, -clusters_to_del
);
6227 * We'll use "new_edge" on our way back up the
6228 * tree to know what our rightmost cpos is.
6230 new_edge
= le16_to_cpu(rec
->e_leaf_clusters
);
6231 new_edge
+= le32_to_cpu(rec
->e_cpos
);
6234 * The caller will use this to delete data blocks.
6236 *delete_start
= le64_to_cpu(rec
->e_blkno
)
6237 + ocfs2_clusters_to_blocks(inode
->i_sb
,
6238 le16_to_cpu(rec
->e_leaf_clusters
));
6241 * If it's now empty, remove this record.
6243 if (le16_to_cpu(rec
->e_leaf_clusters
) == 0) {
6245 sizeof(struct ocfs2_extent_rec
));
6246 le16_add_cpu(&el
->l_next_free_rec
, -1);
6249 if (le64_to_cpu(rec
->e_blkno
) == deleted_eb
) {
6251 sizeof(struct ocfs2_extent_rec
));
6252 le16_add_cpu(&el
->l_next_free_rec
, -1);
6257 /* Can this actually happen? */
6258 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
6262 * We never actually deleted any clusters
6263 * because our leaf was empty. There's no
6264 * reason to adjust the rightmost edge then.
6269 rec
->e_int_clusters
= cpu_to_le32(new_edge
);
6270 le32_add_cpu(&rec
->e_int_clusters
,
6271 -le32_to_cpu(rec
->e_cpos
));
6274 * A deleted child record should have been
6277 BUG_ON(le32_to_cpu(rec
->e_int_clusters
) == 0);
6281 ret
= ocfs2_journal_dirty(handle
, bh
);
6287 mlog(0, "extent list container %llu, after: record %d: "
6288 "(%u, %u, %llu), next = %u.\n",
6289 (unsigned long long)bh
->b_blocknr
, i
,
6290 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
),
6291 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6292 le16_to_cpu(el
->l_next_free_rec
));
6295 * We must be careful to only attempt delete of an
6296 * extent block (and not the root inode block).
6298 if (index
> 0 && le16_to_cpu(el
->l_next_free_rec
) == 0) {
6299 struct ocfs2_extent_block
*eb
=
6300 (struct ocfs2_extent_block
*)bh
->b_data
;
6303 * Save this for use when processing the
6306 deleted_eb
= le64_to_cpu(eb
->h_blkno
);
6308 mlog(0, "deleting this extent block.\n");
6310 ocfs2_remove_from_cache(inode
, bh
);
6312 BUG_ON(ocfs2_rec_clusters(el
, &el
->l_recs
[0]));
6313 BUG_ON(le32_to_cpu(el
->l_recs
[0].e_cpos
));
6314 BUG_ON(le64_to_cpu(el
->l_recs
[0].e_blkno
));
6316 ret
= ocfs2_cache_extent_block_free(&tc
->tc_dealloc
, eb
);
6317 /* An error here is not fatal. */
6332 static int ocfs2_do_truncate(struct ocfs2_super
*osb
,
6333 unsigned int clusters_to_del
,
6334 struct inode
*inode
,
6335 struct buffer_head
*fe_bh
,
6337 struct ocfs2_truncate_context
*tc
,
6338 struct ocfs2_path
*path
)
6341 struct ocfs2_dinode
*fe
;
6342 struct ocfs2_extent_block
*last_eb
= NULL
;
6343 struct ocfs2_extent_list
*el
;
6344 struct buffer_head
*last_eb_bh
= NULL
;
6347 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
6349 status
= ocfs2_find_new_last_ext_blk(inode
, clusters_to_del
,
6357 * Each component will be touched, so we might as well journal
6358 * here to avoid having to handle errors later.
6360 status
= ocfs2_journal_access_path(inode
, handle
, path
);
6367 status
= ocfs2_journal_access(handle
, inode
, last_eb_bh
,
6368 OCFS2_JOURNAL_ACCESS_WRITE
);
6374 last_eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
6377 el
= &(fe
->id2
.i_list
);
6380 * Lower levels depend on this never happening, but it's best
6381 * to check it up here before changing the tree.
6383 if (el
->l_tree_depth
&& el
->l_recs
[0].e_int_clusters
== 0) {
6384 ocfs2_error(inode
->i_sb
,
6385 "Inode %lu has an empty extent record, depth %u\n",
6386 inode
->i_ino
, le16_to_cpu(el
->l_tree_depth
));
6391 spin_lock(&OCFS2_I(inode
)->ip_lock
);
6392 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
) -
6394 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
6395 le32_add_cpu(&fe
->i_clusters
, -clusters_to_del
);
6396 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6398 status
= ocfs2_trim_tree(inode
, path
, handle
, tc
,
6399 clusters_to_del
, &delete_blk
);
6405 if (le32_to_cpu(fe
->i_clusters
) == 0) {
6406 /* trunc to zero is a special case. */
6407 el
->l_tree_depth
= 0;
6408 fe
->i_last_eb_blk
= 0;
6410 fe
->i_last_eb_blk
= last_eb
->h_blkno
;
6412 status
= ocfs2_journal_dirty(handle
, fe_bh
);
6419 /* If there will be a new last extent block, then by
6420 * definition, there cannot be any leaves to the right of
6422 last_eb
->h_next_leaf_blk
= 0;
6423 status
= ocfs2_journal_dirty(handle
, last_eb_bh
);
6431 status
= ocfs2_truncate_log_append(osb
, handle
, delete_blk
,
6445 static int ocfs2_writeback_zero_func(handle_t
*handle
, struct buffer_head
*bh
)
6447 set_buffer_uptodate(bh
);
6448 mark_buffer_dirty(bh
);
6452 static int ocfs2_ordered_zero_func(handle_t
*handle
, struct buffer_head
*bh
)
6454 set_buffer_uptodate(bh
);
6455 mark_buffer_dirty(bh
);
6456 return ocfs2_journal_dirty_data(handle
, bh
);
6459 static void ocfs2_map_and_dirty_page(struct inode
*inode
, handle_t
*handle
,
6460 unsigned int from
, unsigned int to
,
6461 struct page
*page
, int zero
, u64
*phys
)
6463 int ret
, partial
= 0;
6465 ret
= ocfs2_map_page_blocks(page
, phys
, inode
, from
, to
, 0);
6470 zero_user_segment(page
, from
, to
);
6473 * Need to set the buffers we zero'd into uptodate
6474 * here if they aren't - ocfs2_map_page_blocks()
6475 * might've skipped some
6477 if (ocfs2_should_order_data(inode
)) {
6478 ret
= walk_page_buffers(handle
,
6481 ocfs2_ordered_zero_func
);
6485 ret
= walk_page_buffers(handle
, page_buffers(page
),
6487 ocfs2_writeback_zero_func
);
6493 SetPageUptodate(page
);
6495 flush_dcache_page(page
);
6498 static void ocfs2_zero_cluster_pages(struct inode
*inode
, loff_t start
,
6499 loff_t end
, struct page
**pages
,
6500 int numpages
, u64 phys
, handle_t
*handle
)
6504 unsigned int from
, to
= PAGE_CACHE_SIZE
;
6505 struct super_block
*sb
= inode
->i_sb
;
6507 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb
)));
6512 to
= PAGE_CACHE_SIZE
;
6513 for(i
= 0; i
< numpages
; i
++) {
6516 from
= start
& (PAGE_CACHE_SIZE
- 1);
6517 if ((end
>> PAGE_CACHE_SHIFT
) == page
->index
)
6518 to
= end
& (PAGE_CACHE_SIZE
- 1);
6520 BUG_ON(from
> PAGE_CACHE_SIZE
);
6521 BUG_ON(to
> PAGE_CACHE_SIZE
);
6523 ocfs2_map_and_dirty_page(inode
, handle
, from
, to
, page
, 1,
6526 start
= (page
->index
+ 1) << PAGE_CACHE_SHIFT
;
6530 ocfs2_unlock_and_free_pages(pages
, numpages
);
6533 static int ocfs2_grab_eof_pages(struct inode
*inode
, loff_t start
, loff_t end
,
6534 struct page
**pages
, int *num
)
6536 int numpages
, ret
= 0;
6537 struct super_block
*sb
= inode
->i_sb
;
6538 struct address_space
*mapping
= inode
->i_mapping
;
6539 unsigned long index
;
6540 loff_t last_page_bytes
;
6542 BUG_ON(start
> end
);
6544 BUG_ON(start
>> OCFS2_SB(sb
)->s_clustersize_bits
!=
6545 (end
- 1) >> OCFS2_SB(sb
)->s_clustersize_bits
);
6548 last_page_bytes
= PAGE_ALIGN(end
);
6549 index
= start
>> PAGE_CACHE_SHIFT
;
6551 pages
[numpages
] = grab_cache_page(mapping
, index
);
6552 if (!pages
[numpages
]) {
6560 } while (index
< (last_page_bytes
>> PAGE_CACHE_SHIFT
));
6565 ocfs2_unlock_and_free_pages(pages
, numpages
);
6575 * Zero the area past i_size but still within an allocated
6576 * cluster. This avoids exposing nonzero data on subsequent file
6579 * We need to call this before i_size is updated on the inode because
6580 * otherwise block_write_full_page() will skip writeout of pages past
6581 * i_size. The new_i_size parameter is passed for this reason.
6583 int ocfs2_zero_range_for_truncate(struct inode
*inode
, handle_t
*handle
,
6584 u64 range_start
, u64 range_end
)
6586 int ret
= 0, numpages
;
6587 struct page
**pages
= NULL
;
6589 unsigned int ext_flags
;
6590 struct super_block
*sb
= inode
->i_sb
;
6593 * File systems which don't support sparse files zero on every
6596 if (!ocfs2_sparse_alloc(OCFS2_SB(sb
)))
6599 pages
= kcalloc(ocfs2_pages_per_cluster(sb
),
6600 sizeof(struct page
*), GFP_NOFS
);
6601 if (pages
== NULL
) {
6607 if (range_start
== range_end
)
6610 ret
= ocfs2_extent_map_get_blocks(inode
,
6611 range_start
>> sb
->s_blocksize_bits
,
6612 &phys
, NULL
, &ext_flags
);
6619 * Tail is a hole, or is marked unwritten. In either case, we
6620 * can count on read and write to return/push zero's.
6622 if (phys
== 0 || ext_flags
& OCFS2_EXT_UNWRITTEN
)
6625 ret
= ocfs2_grab_eof_pages(inode
, range_start
, range_end
, pages
,
6632 ocfs2_zero_cluster_pages(inode
, range_start
, range_end
, pages
,
6633 numpages
, phys
, handle
);
6636 * Initiate writeout of the pages we zero'd here. We don't
6637 * wait on them - the truncate_inode_pages() call later will
6640 ret
= do_sync_mapping_range(inode
->i_mapping
, range_start
,
6641 range_end
- 1, SYNC_FILE_RANGE_WRITE
);
6652 static void ocfs2_zero_dinode_id2_with_xattr(struct inode
*inode
,
6653 struct ocfs2_dinode
*di
)
6655 unsigned int blocksize
= 1 << inode
->i_sb
->s_blocksize_bits
;
6656 unsigned int xattrsize
= le16_to_cpu(di
->i_xattr_inline_size
);
6658 if (le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_XATTR_FL
)
6659 memset(&di
->id2
, 0, blocksize
-
6660 offsetof(struct ocfs2_dinode
, id2
) -
6663 memset(&di
->id2
, 0, blocksize
-
6664 offsetof(struct ocfs2_dinode
, id2
));
6667 void ocfs2_dinode_new_extent_list(struct inode
*inode
,
6668 struct ocfs2_dinode
*di
)
6670 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
6671 di
->id2
.i_list
.l_tree_depth
= 0;
6672 di
->id2
.i_list
.l_next_free_rec
= 0;
6673 di
->id2
.i_list
.l_count
= cpu_to_le16(
6674 ocfs2_extent_recs_per_inode_with_xattr(inode
->i_sb
, di
));
6677 void ocfs2_set_inode_data_inline(struct inode
*inode
, struct ocfs2_dinode
*di
)
6679 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6680 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
6682 spin_lock(&oi
->ip_lock
);
6683 oi
->ip_dyn_features
|= OCFS2_INLINE_DATA_FL
;
6684 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
6685 spin_unlock(&oi
->ip_lock
);
6688 * We clear the entire i_data structure here so that all
6689 * fields can be properly initialized.
6691 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
6693 idata
->id_count
= cpu_to_le16(
6694 ocfs2_max_inline_data_with_xattr(inode
->i_sb
, di
));
6697 int ocfs2_convert_inline_data_to_extents(struct inode
*inode
,
6698 struct buffer_head
*di_bh
)
6700 int ret
, i
, has_data
, num_pages
= 0;
6702 u64
uninitialized_var(block
);
6703 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6704 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6705 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
6706 struct ocfs2_alloc_context
*data_ac
= NULL
;
6707 struct page
**pages
= NULL
;
6708 loff_t end
= osb
->s_clustersize
;
6710 has_data
= i_size_read(inode
) ? 1 : 0;
6713 pages
= kcalloc(ocfs2_pages_per_cluster(osb
->sb
),
6714 sizeof(struct page
*), GFP_NOFS
);
6715 if (pages
== NULL
) {
6721 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
6728 handle
= ocfs2_start_trans(osb
, OCFS2_INLINE_TO_EXTENTS_CREDITS
);
6729 if (IS_ERR(handle
)) {
6730 ret
= PTR_ERR(handle
);
6735 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
6736 OCFS2_JOURNAL_ACCESS_WRITE
);
6744 unsigned int page_end
;
6747 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
,
6755 * Save two copies, one for insert, and one that can
6756 * be changed by ocfs2_map_and_dirty_page() below.
6758 block
= phys
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
6761 * Non sparse file systems zero on extend, so no need
6764 if (!ocfs2_sparse_alloc(osb
) &&
6765 PAGE_CACHE_SIZE
< osb
->s_clustersize
)
6766 end
= PAGE_CACHE_SIZE
;
6768 ret
= ocfs2_grab_eof_pages(inode
, 0, end
, pages
, &num_pages
);
6775 * This should populate the 1st page for us and mark
6778 ret
= ocfs2_read_inline_data(inode
, pages
[0], di_bh
);
6784 page_end
= PAGE_CACHE_SIZE
;
6785 if (PAGE_CACHE_SIZE
> osb
->s_clustersize
)
6786 page_end
= osb
->s_clustersize
;
6788 for (i
= 0; i
< num_pages
; i
++)
6789 ocfs2_map_and_dirty_page(inode
, handle
, 0, page_end
,
6790 pages
[i
], i
> 0, &phys
);
6793 spin_lock(&oi
->ip_lock
);
6794 oi
->ip_dyn_features
&= ~OCFS2_INLINE_DATA_FL
;
6795 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
6796 spin_unlock(&oi
->ip_lock
);
6798 ocfs2_dinode_new_extent_list(inode
, di
);
6800 ocfs2_journal_dirty(handle
, di_bh
);
6804 * An error at this point should be extremely rare. If
6805 * this proves to be false, we could always re-build
6806 * the in-inode data from our pages.
6808 ret
= ocfs2_dinode_insert_extent(osb
, handle
, inode
, di_bh
,
6809 0, block
, 1, 0, NULL
);
6815 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6819 ocfs2_commit_trans(osb
, handle
);
6823 ocfs2_free_alloc_context(data_ac
);
6827 ocfs2_unlock_and_free_pages(pages
, num_pages
);
6835 * It is expected, that by the time you call this function,
6836 * inode->i_size and fe->i_size have been adjusted.
6838 * WARNING: This will kfree the truncate context
6840 int ocfs2_commit_truncate(struct ocfs2_super
*osb
,
6841 struct inode
*inode
,
6842 struct buffer_head
*fe_bh
,
6843 struct ocfs2_truncate_context
*tc
)
6845 int status
, i
, credits
, tl_sem
= 0;
6846 u32 clusters_to_del
, new_highest_cpos
, range
;
6847 struct ocfs2_extent_list
*el
;
6848 handle_t
*handle
= NULL
;
6849 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6850 struct ocfs2_path
*path
= NULL
;
6851 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
6855 new_highest_cpos
= ocfs2_clusters_for_bytes(osb
->sb
,
6856 i_size_read(inode
));
6858 path
= ocfs2_new_path(fe_bh
, &di
->id2
.i_list
);
6865 ocfs2_extent_map_trunc(inode
, new_highest_cpos
);
6869 * Check that we still have allocation to delete.
6871 if (OCFS2_I(inode
)->ip_clusters
== 0) {
6877 * Truncate always works against the rightmost tree branch.
6879 status
= ocfs2_find_path(inode
, path
, UINT_MAX
);
6885 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6886 OCFS2_I(inode
)->ip_clusters
, path
->p_tree_depth
);
6889 * By now, el will point to the extent list on the bottom most
6890 * portion of this tree. Only the tail record is considered in
6893 * We handle the following cases, in order:
6894 * - empty extent: delete the remaining branch
6895 * - remove the entire record
6896 * - remove a partial record
6897 * - no record needs to be removed (truncate has completed)
6899 el
= path_leaf_el(path
);
6900 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
6901 ocfs2_error(inode
->i_sb
,
6902 "Inode %llu has empty extent block at %llu\n",
6903 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
6904 (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
6909 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
6910 range
= le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
6911 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
6912 if (i
== 0 && ocfs2_is_empty_extent(&el
->l_recs
[i
])) {
6913 clusters_to_del
= 0;
6914 } else if (le32_to_cpu(el
->l_recs
[i
].e_cpos
) >= new_highest_cpos
) {
6915 clusters_to_del
= ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
6916 } else if (range
> new_highest_cpos
) {
6917 clusters_to_del
= (ocfs2_rec_clusters(el
, &el
->l_recs
[i
]) +
6918 le32_to_cpu(el
->l_recs
[i
].e_cpos
)) -
6925 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6926 clusters_to_del
, (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
6928 mutex_lock(&tl_inode
->i_mutex
);
6930 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6931 * record is free for use. If there isn't any, we flush to get
6932 * an empty truncate log. */
6933 if (ocfs2_truncate_log_needs_flush(osb
)) {
6934 status
= __ocfs2_flush_truncate_log(osb
);
6941 credits
= ocfs2_calc_tree_trunc_credits(osb
->sb
, clusters_to_del
,
6942 (struct ocfs2_dinode
*)fe_bh
->b_data
,
6944 handle
= ocfs2_start_trans(osb
, credits
);
6945 if (IS_ERR(handle
)) {
6946 status
= PTR_ERR(handle
);
6952 status
= ocfs2_do_truncate(osb
, clusters_to_del
, inode
, fe_bh
, handle
,
6959 mutex_unlock(&tl_inode
->i_mutex
);
6962 ocfs2_commit_trans(osb
, handle
);
6965 ocfs2_reinit_path(path
, 1);
6968 * The check above will catch the case where we've truncated
6969 * away all allocation.
6975 ocfs2_schedule_truncate_log_flush(osb
, 1);
6978 mutex_unlock(&tl_inode
->i_mutex
);
6981 ocfs2_commit_trans(osb
, handle
);
6983 ocfs2_run_deallocs(osb
, &tc
->tc_dealloc
);
6985 ocfs2_free_path(path
);
6987 /* This will drop the ext_alloc cluster lock for us */
6988 ocfs2_free_truncate_context(tc
);
6995 * Expects the inode to already be locked.
6997 int ocfs2_prepare_truncate(struct ocfs2_super
*osb
,
6998 struct inode
*inode
,
6999 struct buffer_head
*fe_bh
,
7000 struct ocfs2_truncate_context
**tc
)
7003 unsigned int new_i_clusters
;
7004 struct ocfs2_dinode
*fe
;
7005 struct ocfs2_extent_block
*eb
;
7006 struct buffer_head
*last_eb_bh
= NULL
;
7012 new_i_clusters
= ocfs2_clusters_for_bytes(osb
->sb
,
7013 i_size_read(inode
));
7014 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
7016 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7017 "%llu\n", le32_to_cpu(fe
->i_clusters
), new_i_clusters
,
7018 (unsigned long long)le64_to_cpu(fe
->i_size
));
7020 *tc
= kzalloc(sizeof(struct ocfs2_truncate_context
), GFP_KERNEL
);
7026 ocfs2_init_dealloc_ctxt(&(*tc
)->tc_dealloc
);
7028 if (fe
->id2
.i_list
.l_tree_depth
) {
7029 status
= ocfs2_read_block(osb
, le64_to_cpu(fe
->i_last_eb_blk
),
7030 &last_eb_bh
, OCFS2_BH_CACHED
, inode
);
7035 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
7036 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
7037 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
7045 (*tc
)->tc_last_eb_bh
= last_eb_bh
;
7051 ocfs2_free_truncate_context(*tc
);
7059 * 'start' is inclusive, 'end' is not.
7061 int ocfs2_truncate_inline(struct inode
*inode
, struct buffer_head
*di_bh
,
7062 unsigned int start
, unsigned int end
, int trunc
)
7065 unsigned int numbytes
;
7067 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7068 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
7069 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
7071 if (end
> i_size_read(inode
))
7072 end
= i_size_read(inode
);
7074 BUG_ON(start
>= end
);
7076 if (!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) ||
7077 !(le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_DATA_FL
) ||
7078 !ocfs2_supports_inline_data(osb
)) {
7079 ocfs2_error(inode
->i_sb
,
7080 "Inline data flags for inode %llu don't agree! "
7081 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7082 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7083 le16_to_cpu(di
->i_dyn_features
),
7084 OCFS2_I(inode
)->ip_dyn_features
,
7085 osb
->s_feature_incompat
);
7090 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
7091 if (IS_ERR(handle
)) {
7092 ret
= PTR_ERR(handle
);
7097 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
7098 OCFS2_JOURNAL_ACCESS_WRITE
);
7104 numbytes
= end
- start
;
7105 memset(idata
->id_data
+ start
, 0, numbytes
);
7108 * No need to worry about the data page here - it's been
7109 * truncated already and inline data doesn't need it for
7110 * pushing zero's to disk, so we'll let readpage pick it up
7114 i_size_write(inode
, start
);
7115 di
->i_size
= cpu_to_le64(start
);
7118 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7119 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
7121 di
->i_ctime
= di
->i_mtime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
7122 di
->i_ctime_nsec
= di
->i_mtime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
7124 ocfs2_journal_dirty(handle
, di_bh
);
7127 ocfs2_commit_trans(osb
, handle
);
7133 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
)
7136 * The caller is responsible for completing deallocation
7137 * before freeing the context.
7139 if (tc
->tc_dealloc
.c_first_suballocator
!= NULL
)
7141 "Truncate completion has non-empty dealloc context\n");
7143 if (tc
->tc_last_eb_bh
)
7144 brelse(tc
->tc_last_eb_bh
);