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 type
;
79 struct ocfs2_extent_tree_operations
*eops
;
80 struct buffer_head
*root_bh
;
81 struct ocfs2_extent_list
*root_el
;
83 unsigned int max_leaf_clusters
;
86 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
89 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)et
->root_bh
->b_data
;
91 BUG_ON(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
= (struct ocfs2_dinode
*)et
->root_bh
->b_data
;
99 BUG_ON(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
=
108 (struct ocfs2_dinode
*)et
->root_bh
->b_data
;
110 le32_add_cpu(&di
->i_clusters
, clusters
);
111 spin_lock(&OCFS2_I(inode
)->ip_lock
);
112 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(di
->i_clusters
);
113 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
116 static int ocfs2_dinode_sanity_check(struct inode
*inode
,
117 struct ocfs2_extent_tree
*et
)
120 struct ocfs2_dinode
*di
;
122 BUG_ON(et
->type
!= OCFS2_DINODE_EXTENT
);
124 di
= (struct ocfs2_dinode
*)et
->root_bh
->b_data
;
125 if (!OCFS2_IS_VALID_DINODE(di
)) {
127 ocfs2_error(inode
->i_sb
,
128 "Inode %llu has invalid path root",
129 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
135 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops
= {
136 .eo_set_last_eb_blk
= ocfs2_dinode_set_last_eb_blk
,
137 .eo_get_last_eb_blk
= ocfs2_dinode_get_last_eb_blk
,
138 .eo_update_clusters
= ocfs2_dinode_update_clusters
,
139 .eo_sanity_check
= ocfs2_dinode_sanity_check
,
142 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
145 struct ocfs2_xattr_value_root
*xv
=
146 (struct ocfs2_xattr_value_root
*)et
->private;
148 xv
->xr_last_eb_blk
= cpu_to_le64(blkno
);
151 static u64
ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
153 struct ocfs2_xattr_value_root
*xv
=
154 (struct ocfs2_xattr_value_root
*) et
->private;
156 return le64_to_cpu(xv
->xr_last_eb_blk
);
159 static void ocfs2_xattr_value_update_clusters(struct inode
*inode
,
160 struct ocfs2_extent_tree
*et
,
163 struct ocfs2_xattr_value_root
*xv
=
164 (struct ocfs2_xattr_value_root
*)et
->private;
166 le32_add_cpu(&xv
->xr_clusters
, clusters
);
169 static int ocfs2_xattr_value_sanity_check(struct inode
*inode
,
170 struct ocfs2_extent_tree
*et
)
175 static struct ocfs2_extent_tree_operations ocfs2_xattr_et_ops
= {
176 .eo_set_last_eb_blk
= ocfs2_xattr_value_set_last_eb_blk
,
177 .eo_get_last_eb_blk
= ocfs2_xattr_value_get_last_eb_blk
,
178 .eo_update_clusters
= ocfs2_xattr_value_update_clusters
,
179 .eo_sanity_check
= ocfs2_xattr_value_sanity_check
,
182 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
185 struct ocfs2_xattr_block
*xb
=
186 (struct ocfs2_xattr_block
*) et
->root_bh
->b_data
;
187 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
189 xt
->xt_last_eb_blk
= cpu_to_le64(blkno
);
192 static u64
ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
194 struct ocfs2_xattr_block
*xb
=
195 (struct ocfs2_xattr_block
*) et
->root_bh
->b_data
;
196 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
198 return le64_to_cpu(xt
->xt_last_eb_blk
);
201 static void ocfs2_xattr_tree_update_clusters(struct inode
*inode
,
202 struct ocfs2_extent_tree
*et
,
205 struct ocfs2_xattr_block
*xb
=
206 (struct ocfs2_xattr_block
*)et
->root_bh
->b_data
;
208 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, clusters
);
211 static int ocfs2_xattr_tree_sanity_check(struct inode
*inode
,
212 struct ocfs2_extent_tree
*et
)
217 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops
= {
218 .eo_set_last_eb_blk
= ocfs2_xattr_tree_set_last_eb_blk
,
219 .eo_get_last_eb_blk
= ocfs2_xattr_tree_get_last_eb_blk
,
220 .eo_update_clusters
= ocfs2_xattr_tree_update_clusters
,
221 .eo_sanity_check
= ocfs2_xattr_tree_sanity_check
,
224 static struct ocfs2_extent_tree
*
225 ocfs2_new_extent_tree(struct inode
*inode
,
226 struct buffer_head
*bh
,
227 enum ocfs2_extent_tree_type et_type
,
230 struct ocfs2_extent_tree
*et
;
232 et
= kzalloc(sizeof(*et
), GFP_NOFS
);
239 et
->private = private;
241 if (et_type
== OCFS2_DINODE_EXTENT
) {
242 et
->root_el
= &((struct ocfs2_dinode
*)bh
->b_data
)->id2
.i_list
;
243 et
->eops
= &ocfs2_dinode_et_ops
;
244 } else if (et_type
== OCFS2_XATTR_VALUE_EXTENT
) {
245 struct ocfs2_xattr_value_root
*xv
=
246 (struct ocfs2_xattr_value_root
*) private;
247 et
->root_el
= &xv
->xr_list
;
248 et
->eops
= &ocfs2_xattr_et_ops
;
249 } else if (et_type
== OCFS2_XATTR_TREE_EXTENT
) {
250 struct ocfs2_xattr_block
*xb
=
251 (struct ocfs2_xattr_block
*)bh
->b_data
;
252 et
->root_el
= &xb
->xb_attrs
.xb_root
.xt_list
;
253 et
->eops
= &ocfs2_xattr_tree_et_ops
;
254 et
->max_leaf_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
255 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
);
261 static void ocfs2_free_extent_tree(struct ocfs2_extent_tree
*et
)
269 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
272 et
->eops
->eo_set_last_eb_blk(et
, new_last_eb_blk
);
275 static inline u64
ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
277 return et
->eops
->eo_get_last_eb_blk(et
);
280 static inline void ocfs2_et_update_clusters(struct inode
*inode
,
281 struct ocfs2_extent_tree
*et
,
284 et
->eops
->eo_update_clusters(inode
, et
, clusters
);
287 static inline int ocfs2_et_sanity_check(struct inode
*inode
,
288 struct ocfs2_extent_tree
*et
)
290 return et
->eops
->eo_sanity_check(inode
, et
);
293 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
);
294 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
295 struct ocfs2_extent_block
*eb
);
298 * Structures which describe a path through a btree, and functions to
301 * The idea here is to be as generic as possible with the tree
304 struct ocfs2_path_item
{
305 struct buffer_head
*bh
;
306 struct ocfs2_extent_list
*el
;
309 #define OCFS2_MAX_PATH_DEPTH 5
313 struct ocfs2_path_item p_node
[OCFS2_MAX_PATH_DEPTH
];
316 #define path_root_bh(_path) ((_path)->p_node[0].bh)
317 #define path_root_el(_path) ((_path)->p_node[0].el)
318 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
319 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
320 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
323 * Reset the actual path elements so that we can re-use the structure
324 * to build another path. Generally, this involves freeing the buffer
327 static void ocfs2_reinit_path(struct ocfs2_path
*path
, int keep_root
)
329 int i
, start
= 0, depth
= 0;
330 struct ocfs2_path_item
*node
;
335 for(i
= start
; i
< path_num_items(path
); i
++) {
336 node
= &path
->p_node
[i
];
344 * Tree depth may change during truncate, or insert. If we're
345 * keeping the root extent list, then make sure that our path
346 * structure reflects the proper depth.
349 depth
= le16_to_cpu(path_root_el(path
)->l_tree_depth
);
351 path
->p_tree_depth
= depth
;
354 static void ocfs2_free_path(struct ocfs2_path
*path
)
357 ocfs2_reinit_path(path
, 0);
363 * All the elements of src into dest. After this call, src could be freed
364 * without affecting dest.
366 * Both paths should have the same root. Any non-root elements of dest
369 static void ocfs2_cp_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
373 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
374 BUG_ON(path_root_el(dest
) != path_root_el(src
));
376 ocfs2_reinit_path(dest
, 1);
378 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
379 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
380 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
382 if (dest
->p_node
[i
].bh
)
383 get_bh(dest
->p_node
[i
].bh
);
388 * Make the *dest path the same as src and re-initialize src path to
391 static void ocfs2_mv_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
395 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
397 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
398 brelse(dest
->p_node
[i
].bh
);
400 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
401 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
403 src
->p_node
[i
].bh
= NULL
;
404 src
->p_node
[i
].el
= NULL
;
409 * Insert an extent block at given index.
411 * This will not take an additional reference on eb_bh.
413 static inline void ocfs2_path_insert_eb(struct ocfs2_path
*path
, int index
,
414 struct buffer_head
*eb_bh
)
416 struct ocfs2_extent_block
*eb
= (struct ocfs2_extent_block
*)eb_bh
->b_data
;
419 * Right now, no root bh is an extent block, so this helps
420 * catch code errors with dinode trees. The assertion can be
421 * safely removed if we ever need to insert extent block
422 * structures at the root.
426 path
->p_node
[index
].bh
= eb_bh
;
427 path
->p_node
[index
].el
= &eb
->h_list
;
430 static struct ocfs2_path
*ocfs2_new_path(struct buffer_head
*root_bh
,
431 struct ocfs2_extent_list
*root_el
)
433 struct ocfs2_path
*path
;
435 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) >= OCFS2_MAX_PATH_DEPTH
);
437 path
= kzalloc(sizeof(*path
), GFP_NOFS
);
439 path
->p_tree_depth
= le16_to_cpu(root_el
->l_tree_depth
);
441 path_root_bh(path
) = root_bh
;
442 path_root_el(path
) = root_el
;
449 * Convenience function to journal all components in a path.
451 static int ocfs2_journal_access_path(struct inode
*inode
, handle_t
*handle
,
452 struct ocfs2_path
*path
)
459 for(i
= 0; i
< path_num_items(path
); i
++) {
460 ret
= ocfs2_journal_access(handle
, inode
, path
->p_node
[i
].bh
,
461 OCFS2_JOURNAL_ACCESS_WRITE
);
473 * Return the index of the extent record which contains cluster #v_cluster.
474 * -1 is returned if it was not found.
476 * Should work fine on interior and exterior nodes.
478 int ocfs2_search_extent_list(struct ocfs2_extent_list
*el
, u32 v_cluster
)
482 struct ocfs2_extent_rec
*rec
;
483 u32 rec_end
, rec_start
, clusters
;
485 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
486 rec
= &el
->l_recs
[i
];
488 rec_start
= le32_to_cpu(rec
->e_cpos
);
489 clusters
= ocfs2_rec_clusters(el
, rec
);
491 rec_end
= rec_start
+ clusters
;
493 if (v_cluster
>= rec_start
&& v_cluster
< rec_end
) {
502 enum ocfs2_contig_type
{
511 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
512 * ocfs2_extent_contig only work properly against leaf nodes!
514 static int ocfs2_block_extent_contig(struct super_block
*sb
,
515 struct ocfs2_extent_rec
*ext
,
518 u64 blk_end
= le64_to_cpu(ext
->e_blkno
);
520 blk_end
+= ocfs2_clusters_to_blocks(sb
,
521 le16_to_cpu(ext
->e_leaf_clusters
));
523 return blkno
== blk_end
;
526 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec
*left
,
527 struct ocfs2_extent_rec
*right
)
531 left_range
= le32_to_cpu(left
->e_cpos
) +
532 le16_to_cpu(left
->e_leaf_clusters
);
534 return (left_range
== le32_to_cpu(right
->e_cpos
));
537 static enum ocfs2_contig_type
538 ocfs2_extent_contig(struct inode
*inode
,
539 struct ocfs2_extent_rec
*ext
,
540 struct ocfs2_extent_rec
*insert_rec
)
542 u64 blkno
= le64_to_cpu(insert_rec
->e_blkno
);
545 * Refuse to coalesce extent records with different flag
546 * fields - we don't want to mix unwritten extents with user
549 if (ext
->e_flags
!= insert_rec
->e_flags
)
552 if (ocfs2_extents_adjacent(ext
, insert_rec
) &&
553 ocfs2_block_extent_contig(inode
->i_sb
, ext
, blkno
))
556 blkno
= le64_to_cpu(ext
->e_blkno
);
557 if (ocfs2_extents_adjacent(insert_rec
, ext
) &&
558 ocfs2_block_extent_contig(inode
->i_sb
, insert_rec
, blkno
))
565 * NOTE: We can have pretty much any combination of contiguousness and
568 * The usefulness of APPEND_TAIL is more in that it lets us know that
569 * we'll have to update the path to that leaf.
571 enum ocfs2_append_type
{
576 enum ocfs2_split_type
{
582 struct ocfs2_insert_type
{
583 enum ocfs2_split_type ins_split
;
584 enum ocfs2_append_type ins_appending
;
585 enum ocfs2_contig_type ins_contig
;
586 int ins_contig_index
;
590 struct ocfs2_merge_ctxt
{
591 enum ocfs2_contig_type c_contig_type
;
592 int c_has_empty_extent
;
593 int c_split_covers_rec
;
597 * How many free extents have we got before we need more meta data?
599 int ocfs2_num_free_extents(struct ocfs2_super
*osb
,
601 struct buffer_head
*root_bh
,
602 enum ocfs2_extent_tree_type type
,
606 struct ocfs2_extent_list
*el
= NULL
;
607 struct ocfs2_extent_block
*eb
;
608 struct buffer_head
*eb_bh
= NULL
;
613 if (type
== OCFS2_DINODE_EXTENT
) {
614 struct ocfs2_dinode
*fe
=
615 (struct ocfs2_dinode
*)root_bh
->b_data
;
616 if (!OCFS2_IS_VALID_DINODE(fe
)) {
617 OCFS2_RO_ON_INVALID_DINODE(inode
->i_sb
, fe
);
622 if (fe
->i_last_eb_blk
)
623 last_eb_blk
= le64_to_cpu(fe
->i_last_eb_blk
);
624 el
= &fe
->id2
.i_list
;
625 } else if (type
== OCFS2_XATTR_VALUE_EXTENT
) {
626 struct ocfs2_xattr_value_root
*xv
=
627 (struct ocfs2_xattr_value_root
*) private;
629 last_eb_blk
= le64_to_cpu(xv
->xr_last_eb_blk
);
631 } else if (type
== OCFS2_XATTR_TREE_EXTENT
) {
632 struct ocfs2_xattr_block
*xb
=
633 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
635 last_eb_blk
= le64_to_cpu(xb
->xb_attrs
.xb_root
.xt_last_eb_blk
);
636 el
= &xb
->xb_attrs
.xb_root
.xt_list
;
640 retval
= ocfs2_read_block(osb
, last_eb_blk
,
641 &eb_bh
, OCFS2_BH_CACHED
, inode
);
646 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
650 BUG_ON(el
->l_tree_depth
!= 0);
652 retval
= le16_to_cpu(el
->l_count
) - le16_to_cpu(el
->l_next_free_rec
);
661 /* expects array to already be allocated
663 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
666 static int ocfs2_create_new_meta_bhs(struct ocfs2_super
*osb
,
670 struct ocfs2_alloc_context
*meta_ac
,
671 struct buffer_head
*bhs
[])
673 int count
, status
, i
;
674 u16 suballoc_bit_start
;
677 struct ocfs2_extent_block
*eb
;
682 while (count
< wanted
) {
683 status
= ocfs2_claim_metadata(osb
,
695 for(i
= count
; i
< (num_got
+ count
); i
++) {
696 bhs
[i
] = sb_getblk(osb
->sb
, first_blkno
);
697 if (bhs
[i
] == NULL
) {
702 ocfs2_set_new_buffer_uptodate(inode
, bhs
[i
]);
704 status
= ocfs2_journal_access(handle
, inode
, bhs
[i
],
705 OCFS2_JOURNAL_ACCESS_CREATE
);
711 memset(bhs
[i
]->b_data
, 0, osb
->sb
->s_blocksize
);
712 eb
= (struct ocfs2_extent_block
*) bhs
[i
]->b_data
;
713 /* Ok, setup the minimal stuff here. */
714 strcpy(eb
->h_signature
, OCFS2_EXTENT_BLOCK_SIGNATURE
);
715 eb
->h_blkno
= cpu_to_le64(first_blkno
);
716 eb
->h_fs_generation
= cpu_to_le32(osb
->fs_generation
);
717 eb
->h_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
718 eb
->h_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
720 cpu_to_le16(ocfs2_extent_recs_per_eb(osb
->sb
));
722 suballoc_bit_start
++;
725 /* We'll also be dirtied by the caller, so
726 * this isn't absolutely necessary. */
727 status
= ocfs2_journal_dirty(handle
, bhs
[i
]);
740 for(i
= 0; i
< wanted
; i
++) {
751 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
753 * Returns the sum of the rightmost extent rec logical offset and
756 * ocfs2_add_branch() uses this to determine what logical cluster
757 * value should be populated into the leftmost new branch records.
759 * ocfs2_shift_tree_depth() uses this to determine the # clusters
760 * value for the new topmost tree record.
762 static inline u32
ocfs2_sum_rightmost_rec(struct ocfs2_extent_list
*el
)
766 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
768 return le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
769 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
773 * Add an entire tree branch to our inode. eb_bh is the extent block
774 * to start at, if we don't want to start the branch at the dinode
777 * last_eb_bh is required as we have to update it's next_leaf pointer
778 * for the new last extent block.
780 * the new branch will be 'empty' in the sense that every block will
781 * contain a single record with cluster count == 0.
783 static int ocfs2_add_branch(struct ocfs2_super
*osb
,
786 struct ocfs2_extent_tree
*et
,
787 struct buffer_head
*eb_bh
,
788 struct buffer_head
**last_eb_bh
,
789 struct ocfs2_alloc_context
*meta_ac
)
791 int status
, new_blocks
, i
;
792 u64 next_blkno
, new_last_eb_blk
;
793 struct buffer_head
*bh
;
794 struct buffer_head
**new_eb_bhs
= NULL
;
795 struct ocfs2_extent_block
*eb
;
796 struct ocfs2_extent_list
*eb_el
;
797 struct ocfs2_extent_list
*el
;
802 BUG_ON(!last_eb_bh
|| !*last_eb_bh
);
805 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
810 /* we never add a branch to a leaf. */
811 BUG_ON(!el
->l_tree_depth
);
813 new_blocks
= le16_to_cpu(el
->l_tree_depth
);
815 /* allocate the number of new eb blocks we need */
816 new_eb_bhs
= kcalloc(new_blocks
, sizeof(struct buffer_head
*),
824 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, new_blocks
,
825 meta_ac
, new_eb_bhs
);
831 eb
= (struct ocfs2_extent_block
*)(*last_eb_bh
)->b_data
;
832 new_cpos
= ocfs2_sum_rightmost_rec(&eb
->h_list
);
834 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
835 * linked with the rest of the tree.
836 * conversly, new_eb_bhs[0] is the new bottommost leaf.
838 * when we leave the loop, new_last_eb_blk will point to the
839 * newest leaf, and next_blkno will point to the topmost extent
841 next_blkno
= new_last_eb_blk
= 0;
842 for(i
= 0; i
< new_blocks
; i
++) {
844 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
845 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
846 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
852 status
= ocfs2_journal_access(handle
, inode
, bh
,
853 OCFS2_JOURNAL_ACCESS_CREATE
);
859 eb
->h_next_leaf_blk
= 0;
860 eb_el
->l_tree_depth
= cpu_to_le16(i
);
861 eb_el
->l_next_free_rec
= cpu_to_le16(1);
863 * This actually counts as an empty extent as
866 eb_el
->l_recs
[0].e_cpos
= cpu_to_le32(new_cpos
);
867 eb_el
->l_recs
[0].e_blkno
= cpu_to_le64(next_blkno
);
869 * eb_el isn't always an interior node, but even leaf
870 * nodes want a zero'd flags and reserved field so
871 * this gets the whole 32 bits regardless of use.
873 eb_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(0);
874 if (!eb_el
->l_tree_depth
)
875 new_last_eb_blk
= le64_to_cpu(eb
->h_blkno
);
877 status
= ocfs2_journal_dirty(handle
, bh
);
883 next_blkno
= le64_to_cpu(eb
->h_blkno
);
886 /* This is a bit hairy. We want to update up to three blocks
887 * here without leaving any of them in an inconsistent state
888 * in case of error. We don't have to worry about
889 * journal_dirty erroring as it won't unless we've aborted the
890 * handle (in which case we would never be here) so reserving
891 * the write with journal_access is all we need to do. */
892 status
= ocfs2_journal_access(handle
, inode
, *last_eb_bh
,
893 OCFS2_JOURNAL_ACCESS_WRITE
);
898 status
= ocfs2_journal_access(handle
, inode
, et
->root_bh
,
899 OCFS2_JOURNAL_ACCESS_WRITE
);
905 status
= ocfs2_journal_access(handle
, inode
, eb_bh
,
906 OCFS2_JOURNAL_ACCESS_WRITE
);
913 /* Link the new branch into the rest of the tree (el will
914 * either be on the root_bh, or the extent block passed in. */
915 i
= le16_to_cpu(el
->l_next_free_rec
);
916 el
->l_recs
[i
].e_blkno
= cpu_to_le64(next_blkno
);
917 el
->l_recs
[i
].e_cpos
= cpu_to_le32(new_cpos
);
918 el
->l_recs
[i
].e_int_clusters
= 0;
919 le16_add_cpu(&el
->l_next_free_rec
, 1);
921 /* fe needs a new last extent block pointer, as does the
922 * next_leaf on the previously last-extent-block. */
923 ocfs2_et_set_last_eb_blk(et
, new_last_eb_blk
);
925 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
926 eb
->h_next_leaf_blk
= cpu_to_le64(new_last_eb_blk
);
928 status
= ocfs2_journal_dirty(handle
, *last_eb_bh
);
931 status
= ocfs2_journal_dirty(handle
, et
->root_bh
);
935 status
= ocfs2_journal_dirty(handle
, eb_bh
);
941 * Some callers want to track the rightmost leaf so pass it
945 get_bh(new_eb_bhs
[0]);
946 *last_eb_bh
= new_eb_bhs
[0];
951 for (i
= 0; i
< new_blocks
; i
++)
953 brelse(new_eb_bhs
[i
]);
962 * adds another level to the allocation tree.
963 * returns back the new extent block so you can add a branch to it
966 static int ocfs2_shift_tree_depth(struct ocfs2_super
*osb
,
969 struct ocfs2_extent_tree
*et
,
970 struct ocfs2_alloc_context
*meta_ac
,
971 struct buffer_head
**ret_new_eb_bh
)
975 struct buffer_head
*new_eb_bh
= NULL
;
976 struct ocfs2_extent_block
*eb
;
977 struct ocfs2_extent_list
*root_el
;
978 struct ocfs2_extent_list
*eb_el
;
982 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, 1, meta_ac
,
989 eb
= (struct ocfs2_extent_block
*) new_eb_bh
->b_data
;
990 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
991 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
997 root_el
= et
->root_el
;
999 status
= ocfs2_journal_access(handle
, inode
, new_eb_bh
,
1000 OCFS2_JOURNAL_ACCESS_CREATE
);
1006 /* copy the root extent list data into the new extent block */
1007 eb_el
->l_tree_depth
= root_el
->l_tree_depth
;
1008 eb_el
->l_next_free_rec
= root_el
->l_next_free_rec
;
1009 for (i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1010 eb_el
->l_recs
[i
] = root_el
->l_recs
[i
];
1012 status
= ocfs2_journal_dirty(handle
, new_eb_bh
);
1018 status
= ocfs2_journal_access(handle
, inode
, et
->root_bh
,
1019 OCFS2_JOURNAL_ACCESS_WRITE
);
1025 new_clusters
= ocfs2_sum_rightmost_rec(eb_el
);
1027 /* update root_bh now */
1028 le16_add_cpu(&root_el
->l_tree_depth
, 1);
1029 root_el
->l_recs
[0].e_cpos
= 0;
1030 root_el
->l_recs
[0].e_blkno
= eb
->h_blkno
;
1031 root_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(new_clusters
);
1032 for (i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1033 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
1034 root_el
->l_next_free_rec
= cpu_to_le16(1);
1036 /* If this is our 1st tree depth shift, then last_eb_blk
1037 * becomes the allocated extent block */
1038 if (root_el
->l_tree_depth
== cpu_to_le16(1))
1039 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
1041 status
= ocfs2_journal_dirty(handle
, et
->root_bh
);
1047 *ret_new_eb_bh
= new_eb_bh
;
1059 * Should only be called when there is no space left in any of the
1060 * leaf nodes. What we want to do is find the lowest tree depth
1061 * non-leaf extent block with room for new records. There are three
1062 * valid results of this search:
1064 * 1) a lowest extent block is found, then we pass it back in
1065 * *lowest_eb_bh and return '0'
1067 * 2) the search fails to find anything, but the root_el has room. We
1068 * pass NULL back in *lowest_eb_bh, but still return '0'
1070 * 3) the search fails to find anything AND the root_el is full, in
1071 * which case we return > 0
1073 * return status < 0 indicates an error.
1075 static int ocfs2_find_branch_target(struct ocfs2_super
*osb
,
1076 struct inode
*inode
,
1077 struct ocfs2_extent_tree
*et
,
1078 struct buffer_head
**target_bh
)
1082 struct ocfs2_extent_block
*eb
;
1083 struct ocfs2_extent_list
*el
;
1084 struct buffer_head
*bh
= NULL
;
1085 struct buffer_head
*lowest_bh
= NULL
;
1093 while(le16_to_cpu(el
->l_tree_depth
) > 1) {
1094 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1095 ocfs2_error(inode
->i_sb
, "Dinode %llu has empty "
1096 "extent list (next_free_rec == 0)",
1097 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1101 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1102 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1104 ocfs2_error(inode
->i_sb
, "Dinode %llu has extent "
1105 "list where extent # %d has no physical "
1107 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, i
);
1117 status
= ocfs2_read_block(osb
, blkno
, &bh
, OCFS2_BH_CACHED
,
1124 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1125 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
1126 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
1132 if (le16_to_cpu(el
->l_next_free_rec
) <
1133 le16_to_cpu(el
->l_count
)) {
1141 /* If we didn't find one and the fe doesn't have any room,
1142 * then return '1' */
1144 if (!lowest_bh
&& (el
->l_next_free_rec
== el
->l_count
))
1147 *target_bh
= lowest_bh
;
1157 * Grow a b-tree so that it has more records.
1159 * We might shift the tree depth in which case existing paths should
1160 * be considered invalid.
1162 * Tree depth after the grow is returned via *final_depth.
1164 * *last_eb_bh will be updated by ocfs2_add_branch().
1166 static int ocfs2_grow_tree(struct inode
*inode
, handle_t
*handle
,
1167 struct ocfs2_extent_tree
*et
, int *final_depth
,
1168 struct buffer_head
**last_eb_bh
,
1169 struct ocfs2_alloc_context
*meta_ac
)
1172 struct ocfs2_extent_list
*el
= et
->root_el
;
1173 int depth
= le16_to_cpu(el
->l_tree_depth
);
1174 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1175 struct buffer_head
*bh
= NULL
;
1177 BUG_ON(meta_ac
== NULL
);
1179 shift
= ocfs2_find_branch_target(osb
, inode
, et
, &bh
);
1186 /* We traveled all the way to the bottom of the allocation tree
1187 * and didn't find room for any more extents - we need to add
1188 * another tree level */
1191 mlog(0, "need to shift tree depth (current = %d)\n", depth
);
1193 /* ocfs2_shift_tree_depth will return us a buffer with
1194 * the new extent block (so we can pass that to
1195 * ocfs2_add_branch). */
1196 ret
= ocfs2_shift_tree_depth(osb
, handle
, inode
, et
,
1205 * Special case: we have room now if we shifted from
1206 * tree_depth 0, so no more work needs to be done.
1208 * We won't be calling add_branch, so pass
1209 * back *last_eb_bh as the new leaf. At depth
1210 * zero, it should always be null so there's
1211 * no reason to brelse.
1213 BUG_ON(*last_eb_bh
);
1220 /* call ocfs2_add_branch to add the final part of the tree with
1222 mlog(0, "add branch. bh = %p\n", bh
);
1223 ret
= ocfs2_add_branch(osb
, handle
, inode
, et
, bh
, last_eb_bh
,
1232 *final_depth
= depth
;
1238 * This function will discard the rightmost extent record.
1240 static void ocfs2_shift_records_right(struct ocfs2_extent_list
*el
)
1242 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1243 int count
= le16_to_cpu(el
->l_count
);
1244 unsigned int num_bytes
;
1247 /* This will cause us to go off the end of our extent list. */
1248 BUG_ON(next_free
>= count
);
1250 num_bytes
= sizeof(struct ocfs2_extent_rec
) * next_free
;
1252 memmove(&el
->l_recs
[1], &el
->l_recs
[0], num_bytes
);
1255 static void ocfs2_rotate_leaf(struct ocfs2_extent_list
*el
,
1256 struct ocfs2_extent_rec
*insert_rec
)
1258 int i
, insert_index
, next_free
, has_empty
, num_bytes
;
1259 u32 insert_cpos
= le32_to_cpu(insert_rec
->e_cpos
);
1260 struct ocfs2_extent_rec
*rec
;
1262 next_free
= le16_to_cpu(el
->l_next_free_rec
);
1263 has_empty
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
1267 /* The tree code before us didn't allow enough room in the leaf. */
1268 BUG_ON(el
->l_next_free_rec
== el
->l_count
&& !has_empty
);
1271 * The easiest way to approach this is to just remove the
1272 * empty extent and temporarily decrement next_free.
1276 * If next_free was 1 (only an empty extent), this
1277 * loop won't execute, which is fine. We still want
1278 * the decrement above to happen.
1280 for(i
= 0; i
< (next_free
- 1); i
++)
1281 el
->l_recs
[i
] = el
->l_recs
[i
+1];
1287 * Figure out what the new record index should be.
1289 for(i
= 0; i
< next_free
; i
++) {
1290 rec
= &el
->l_recs
[i
];
1292 if (insert_cpos
< le32_to_cpu(rec
->e_cpos
))
1297 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1298 insert_cpos
, insert_index
, has_empty
, next_free
, le16_to_cpu(el
->l_count
));
1300 BUG_ON(insert_index
< 0);
1301 BUG_ON(insert_index
>= le16_to_cpu(el
->l_count
));
1302 BUG_ON(insert_index
> next_free
);
1305 * No need to memmove if we're just adding to the tail.
1307 if (insert_index
!= next_free
) {
1308 BUG_ON(next_free
>= le16_to_cpu(el
->l_count
));
1310 num_bytes
= next_free
- insert_index
;
1311 num_bytes
*= sizeof(struct ocfs2_extent_rec
);
1312 memmove(&el
->l_recs
[insert_index
+ 1],
1313 &el
->l_recs
[insert_index
],
1318 * Either we had an empty extent, and need to re-increment or
1319 * there was no empty extent on a non full rightmost leaf node,
1320 * in which case we still need to increment.
1323 el
->l_next_free_rec
= cpu_to_le16(next_free
);
1325 * Make sure none of the math above just messed up our tree.
1327 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) > le16_to_cpu(el
->l_count
));
1329 el
->l_recs
[insert_index
] = *insert_rec
;
1333 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list
*el
)
1335 int size
, num_recs
= le16_to_cpu(el
->l_next_free_rec
);
1337 BUG_ON(num_recs
== 0);
1339 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
1341 size
= num_recs
* sizeof(struct ocfs2_extent_rec
);
1342 memmove(&el
->l_recs
[0], &el
->l_recs
[1], size
);
1343 memset(&el
->l_recs
[num_recs
], 0,
1344 sizeof(struct ocfs2_extent_rec
));
1345 el
->l_next_free_rec
= cpu_to_le16(num_recs
);
1350 * Create an empty extent record .
1352 * l_next_free_rec may be updated.
1354 * If an empty extent already exists do nothing.
1356 static void ocfs2_create_empty_extent(struct ocfs2_extent_list
*el
)
1358 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1360 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
1365 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
1368 mlog_bug_on_msg(el
->l_count
== el
->l_next_free_rec
,
1369 "Asked to create an empty extent in a full list:\n"
1370 "count = %u, tree depth = %u",
1371 le16_to_cpu(el
->l_count
),
1372 le16_to_cpu(el
->l_tree_depth
));
1374 ocfs2_shift_records_right(el
);
1377 le16_add_cpu(&el
->l_next_free_rec
, 1);
1378 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1382 * For a rotation which involves two leaf nodes, the "root node" is
1383 * the lowest level tree node which contains a path to both leafs. This
1384 * resulting set of information can be used to form a complete "subtree"
1386 * This function is passed two full paths from the dinode down to a
1387 * pair of adjacent leaves. It's task is to figure out which path
1388 * index contains the subtree root - this can be the root index itself
1389 * in a worst-case rotation.
1391 * The array index of the subtree root is passed back.
1393 static int ocfs2_find_subtree_root(struct inode
*inode
,
1394 struct ocfs2_path
*left
,
1395 struct ocfs2_path
*right
)
1400 * Check that the caller passed in two paths from the same tree.
1402 BUG_ON(path_root_bh(left
) != path_root_bh(right
));
1408 * The caller didn't pass two adjacent paths.
1410 mlog_bug_on_msg(i
> left
->p_tree_depth
,
1411 "Inode %lu, left depth %u, right depth %u\n"
1412 "left leaf blk %llu, right leaf blk %llu\n",
1413 inode
->i_ino
, left
->p_tree_depth
,
1414 right
->p_tree_depth
,
1415 (unsigned long long)path_leaf_bh(left
)->b_blocknr
,
1416 (unsigned long long)path_leaf_bh(right
)->b_blocknr
);
1417 } while (left
->p_node
[i
].bh
->b_blocknr
==
1418 right
->p_node
[i
].bh
->b_blocknr
);
1423 typedef void (path_insert_t
)(void *, struct buffer_head
*);
1426 * Traverse a btree path in search of cpos, starting at root_el.
1428 * This code can be called with a cpos larger than the tree, in which
1429 * case it will return the rightmost path.
1431 static int __ocfs2_find_path(struct inode
*inode
,
1432 struct ocfs2_extent_list
*root_el
, u32 cpos
,
1433 path_insert_t
*func
, void *data
)
1438 struct buffer_head
*bh
= NULL
;
1439 struct ocfs2_extent_block
*eb
;
1440 struct ocfs2_extent_list
*el
;
1441 struct ocfs2_extent_rec
*rec
;
1442 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1445 while (el
->l_tree_depth
) {
1446 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1447 ocfs2_error(inode
->i_sb
,
1448 "Inode %llu has empty extent list at "
1450 (unsigned long long)oi
->ip_blkno
,
1451 le16_to_cpu(el
->l_tree_depth
));
1457 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
) - 1; i
++) {
1458 rec
= &el
->l_recs
[i
];
1461 * In the case that cpos is off the allocation
1462 * tree, this should just wind up returning the
1465 range
= le32_to_cpu(rec
->e_cpos
) +
1466 ocfs2_rec_clusters(el
, rec
);
1467 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
1471 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1473 ocfs2_error(inode
->i_sb
,
1474 "Inode %llu has bad blkno in extent list "
1475 "at depth %u (index %d)\n",
1476 (unsigned long long)oi
->ip_blkno
,
1477 le16_to_cpu(el
->l_tree_depth
), i
);
1484 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
), blkno
,
1485 &bh
, OCFS2_BH_CACHED
, inode
);
1491 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1493 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
1494 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
1499 if (le16_to_cpu(el
->l_next_free_rec
) >
1500 le16_to_cpu(el
->l_count
)) {
1501 ocfs2_error(inode
->i_sb
,
1502 "Inode %llu has bad count in extent list "
1503 "at block %llu (next free=%u, count=%u)\n",
1504 (unsigned long long)oi
->ip_blkno
,
1505 (unsigned long long)bh
->b_blocknr
,
1506 le16_to_cpu(el
->l_next_free_rec
),
1507 le16_to_cpu(el
->l_count
));
1518 * Catch any trailing bh that the loop didn't handle.
1526 * Given an initialized path (that is, it has a valid root extent
1527 * list), this function will traverse the btree in search of the path
1528 * which would contain cpos.
1530 * The path traveled is recorded in the path structure.
1532 * Note that this will not do any comparisons on leaf node extent
1533 * records, so it will work fine in the case that we just added a tree
1536 struct find_path_data
{
1538 struct ocfs2_path
*path
;
1540 static void find_path_ins(void *data
, struct buffer_head
*bh
)
1542 struct find_path_data
*fp
= data
;
1545 ocfs2_path_insert_eb(fp
->path
, fp
->index
, bh
);
1548 static int ocfs2_find_path(struct inode
*inode
, struct ocfs2_path
*path
,
1551 struct find_path_data data
;
1555 return __ocfs2_find_path(inode
, path_root_el(path
), cpos
,
1556 find_path_ins
, &data
);
1559 static void find_leaf_ins(void *data
, struct buffer_head
*bh
)
1561 struct ocfs2_extent_block
*eb
=(struct ocfs2_extent_block
*)bh
->b_data
;
1562 struct ocfs2_extent_list
*el
= &eb
->h_list
;
1563 struct buffer_head
**ret
= data
;
1565 /* We want to retain only the leaf block. */
1566 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
1572 * Find the leaf block in the tree which would contain cpos. No
1573 * checking of the actual leaf is done.
1575 * Some paths want to call this instead of allocating a path structure
1576 * and calling ocfs2_find_path().
1578 * This function doesn't handle non btree extent lists.
1580 int ocfs2_find_leaf(struct inode
*inode
, struct ocfs2_extent_list
*root_el
,
1581 u32 cpos
, struct buffer_head
**leaf_bh
)
1584 struct buffer_head
*bh
= NULL
;
1586 ret
= __ocfs2_find_path(inode
, root_el
, cpos
, find_leaf_ins
, &bh
);
1598 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1600 * Basically, we've moved stuff around at the bottom of the tree and
1601 * we need to fix up the extent records above the changes to reflect
1604 * left_rec: the record on the left.
1605 * left_child_el: is the child list pointed to by left_rec
1606 * right_rec: the record to the right of left_rec
1607 * right_child_el: is the child list pointed to by right_rec
1609 * By definition, this only works on interior nodes.
1611 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec
*left_rec
,
1612 struct ocfs2_extent_list
*left_child_el
,
1613 struct ocfs2_extent_rec
*right_rec
,
1614 struct ocfs2_extent_list
*right_child_el
)
1616 u32 left_clusters
, right_end
;
1619 * Interior nodes never have holes. Their cpos is the cpos of
1620 * the leftmost record in their child list. Their cluster
1621 * count covers the full theoretical range of their child list
1622 * - the range between their cpos and the cpos of the record
1623 * immediately to their right.
1625 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[0].e_cpos
);
1626 if (ocfs2_is_empty_extent(&right_child_el
->l_recs
[0])) {
1627 BUG_ON(le16_to_cpu(right_child_el
->l_next_free_rec
) <= 1);
1628 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[1].e_cpos
);
1630 left_clusters
-= le32_to_cpu(left_rec
->e_cpos
);
1631 left_rec
->e_int_clusters
= cpu_to_le32(left_clusters
);
1634 * Calculate the rightmost cluster count boundary before
1635 * moving cpos - we will need to adjust clusters after
1636 * updating e_cpos to keep the same highest cluster count.
1638 right_end
= le32_to_cpu(right_rec
->e_cpos
);
1639 right_end
+= le32_to_cpu(right_rec
->e_int_clusters
);
1641 right_rec
->e_cpos
= left_rec
->e_cpos
;
1642 le32_add_cpu(&right_rec
->e_cpos
, left_clusters
);
1644 right_end
-= le32_to_cpu(right_rec
->e_cpos
);
1645 right_rec
->e_int_clusters
= cpu_to_le32(right_end
);
1649 * Adjust the adjacent root node records involved in a
1650 * rotation. left_el_blkno is passed in as a key so that we can easily
1651 * find it's index in the root list.
1653 static void ocfs2_adjust_root_records(struct ocfs2_extent_list
*root_el
,
1654 struct ocfs2_extent_list
*left_el
,
1655 struct ocfs2_extent_list
*right_el
,
1660 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) <=
1661 le16_to_cpu(left_el
->l_tree_depth
));
1663 for(i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
) - 1; i
++) {
1664 if (le64_to_cpu(root_el
->l_recs
[i
].e_blkno
) == left_el_blkno
)
1669 * The path walking code should have never returned a root and
1670 * two paths which are not adjacent.
1672 BUG_ON(i
>= (le16_to_cpu(root_el
->l_next_free_rec
) - 1));
1674 ocfs2_adjust_adjacent_records(&root_el
->l_recs
[i
], left_el
,
1675 &root_el
->l_recs
[i
+ 1], right_el
);
1679 * We've changed a leaf block (in right_path) and need to reflect that
1680 * change back up the subtree.
1682 * This happens in multiple places:
1683 * - When we've moved an extent record from the left path leaf to the right
1684 * path leaf to make room for an empty extent in the left path leaf.
1685 * - When our insert into the right path leaf is at the leftmost edge
1686 * and requires an update of the path immediately to it's left. This
1687 * can occur at the end of some types of rotation and appending inserts.
1688 * - When we've adjusted the last extent record in the left path leaf and the
1689 * 1st extent record in the right path leaf during cross extent block merge.
1691 static void ocfs2_complete_edge_insert(struct inode
*inode
, handle_t
*handle
,
1692 struct ocfs2_path
*left_path
,
1693 struct ocfs2_path
*right_path
,
1697 struct ocfs2_extent_list
*el
, *left_el
, *right_el
;
1698 struct ocfs2_extent_rec
*left_rec
, *right_rec
;
1699 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
1702 * Update the counts and position values within all the
1703 * interior nodes to reflect the leaf rotation we just did.
1705 * The root node is handled below the loop.
1707 * We begin the loop with right_el and left_el pointing to the
1708 * leaf lists and work our way up.
1710 * NOTE: within this loop, left_el and right_el always refer
1711 * to the *child* lists.
1713 left_el
= path_leaf_el(left_path
);
1714 right_el
= path_leaf_el(right_path
);
1715 for(i
= left_path
->p_tree_depth
- 1; i
> subtree_index
; i
--) {
1716 mlog(0, "Adjust records at index %u\n", i
);
1719 * One nice property of knowing that all of these
1720 * nodes are below the root is that we only deal with
1721 * the leftmost right node record and the rightmost
1724 el
= left_path
->p_node
[i
].el
;
1725 idx
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
1726 left_rec
= &el
->l_recs
[idx
];
1728 el
= right_path
->p_node
[i
].el
;
1729 right_rec
= &el
->l_recs
[0];
1731 ocfs2_adjust_adjacent_records(left_rec
, left_el
, right_rec
,
1734 ret
= ocfs2_journal_dirty(handle
, left_path
->p_node
[i
].bh
);
1738 ret
= ocfs2_journal_dirty(handle
, right_path
->p_node
[i
].bh
);
1743 * Setup our list pointers now so that the current
1744 * parents become children in the next iteration.
1746 left_el
= left_path
->p_node
[i
].el
;
1747 right_el
= right_path
->p_node
[i
].el
;
1751 * At the root node, adjust the two adjacent records which
1752 * begin our path to the leaves.
1755 el
= left_path
->p_node
[subtree_index
].el
;
1756 left_el
= left_path
->p_node
[subtree_index
+ 1].el
;
1757 right_el
= right_path
->p_node
[subtree_index
+ 1].el
;
1759 ocfs2_adjust_root_records(el
, left_el
, right_el
,
1760 left_path
->p_node
[subtree_index
+ 1].bh
->b_blocknr
);
1762 root_bh
= left_path
->p_node
[subtree_index
].bh
;
1764 ret
= ocfs2_journal_dirty(handle
, root_bh
);
1769 static int ocfs2_rotate_subtree_right(struct inode
*inode
,
1771 struct ocfs2_path
*left_path
,
1772 struct ocfs2_path
*right_path
,
1776 struct buffer_head
*right_leaf_bh
;
1777 struct buffer_head
*left_leaf_bh
= NULL
;
1778 struct buffer_head
*root_bh
;
1779 struct ocfs2_extent_list
*right_el
, *left_el
;
1780 struct ocfs2_extent_rec move_rec
;
1782 left_leaf_bh
= path_leaf_bh(left_path
);
1783 left_el
= path_leaf_el(left_path
);
1785 if (left_el
->l_next_free_rec
!= left_el
->l_count
) {
1786 ocfs2_error(inode
->i_sb
,
1787 "Inode %llu has non-full interior leaf node %llu"
1789 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1790 (unsigned long long)left_leaf_bh
->b_blocknr
,
1791 le16_to_cpu(left_el
->l_next_free_rec
));
1796 * This extent block may already have an empty record, so we
1797 * return early if so.
1799 if (ocfs2_is_empty_extent(&left_el
->l_recs
[0]))
1802 root_bh
= left_path
->p_node
[subtree_index
].bh
;
1803 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
1805 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
1806 OCFS2_JOURNAL_ACCESS_WRITE
);
1812 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
1813 ret
= ocfs2_journal_access(handle
, inode
,
1814 right_path
->p_node
[i
].bh
,
1815 OCFS2_JOURNAL_ACCESS_WRITE
);
1821 ret
= ocfs2_journal_access(handle
, inode
,
1822 left_path
->p_node
[i
].bh
,
1823 OCFS2_JOURNAL_ACCESS_WRITE
);
1830 right_leaf_bh
= path_leaf_bh(right_path
);
1831 right_el
= path_leaf_el(right_path
);
1833 /* This is a code error, not a disk corruption. */
1834 mlog_bug_on_msg(!right_el
->l_next_free_rec
, "Inode %llu: Rotate fails "
1835 "because rightmost leaf block %llu is empty\n",
1836 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1837 (unsigned long long)right_leaf_bh
->b_blocknr
);
1839 ocfs2_create_empty_extent(right_el
);
1841 ret
= ocfs2_journal_dirty(handle
, right_leaf_bh
);
1847 /* Do the copy now. */
1848 i
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
1849 move_rec
= left_el
->l_recs
[i
];
1850 right_el
->l_recs
[0] = move_rec
;
1853 * Clear out the record we just copied and shift everything
1854 * over, leaving an empty extent in the left leaf.
1856 * We temporarily subtract from next_free_rec so that the
1857 * shift will lose the tail record (which is now defunct).
1859 le16_add_cpu(&left_el
->l_next_free_rec
, -1);
1860 ocfs2_shift_records_right(left_el
);
1861 memset(&left_el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1862 le16_add_cpu(&left_el
->l_next_free_rec
, 1);
1864 ret
= ocfs2_journal_dirty(handle
, left_leaf_bh
);
1870 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
1878 * Given a full path, determine what cpos value would return us a path
1879 * containing the leaf immediately to the left of the current one.
1881 * Will return zero if the path passed in is already the leftmost path.
1883 static int ocfs2_find_cpos_for_left_leaf(struct super_block
*sb
,
1884 struct ocfs2_path
*path
, u32
*cpos
)
1888 struct ocfs2_extent_list
*el
;
1890 BUG_ON(path
->p_tree_depth
== 0);
1894 blkno
= path_leaf_bh(path
)->b_blocknr
;
1896 /* Start at the tree node just above the leaf and work our way up. */
1897 i
= path
->p_tree_depth
- 1;
1899 el
= path
->p_node
[i
].el
;
1902 * Find the extent record just before the one in our
1905 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
1906 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
1910 * We've determined that the
1911 * path specified is already
1912 * the leftmost one - return a
1918 * The leftmost record points to our
1919 * leaf - we need to travel up the
1925 *cpos
= le32_to_cpu(el
->l_recs
[j
- 1].e_cpos
);
1926 *cpos
= *cpos
+ ocfs2_rec_clusters(el
,
1927 &el
->l_recs
[j
- 1]);
1934 * If we got here, we never found a valid node where
1935 * the tree indicated one should be.
1938 "Invalid extent tree at extent block %llu\n",
1939 (unsigned long long)blkno
);
1944 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
1953 * Extend the transaction by enough credits to complete the rotation,
1954 * and still leave at least the original number of credits allocated
1955 * to this transaction.
1957 static int ocfs2_extend_rotate_transaction(handle_t
*handle
, int subtree_depth
,
1959 struct ocfs2_path
*path
)
1961 int credits
= (path
->p_tree_depth
- subtree_depth
) * 2 + 1 + op_credits
;
1963 if (handle
->h_buffer_credits
< credits
)
1964 return ocfs2_extend_trans(handle
, credits
);
1970 * Trap the case where we're inserting into the theoretical range past
1971 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1972 * whose cpos is less than ours into the right leaf.
1974 * It's only necessary to look at the rightmost record of the left
1975 * leaf because the logic that calls us should ensure that the
1976 * theoretical ranges in the path components above the leaves are
1979 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path
*left_path
,
1982 struct ocfs2_extent_list
*left_el
;
1983 struct ocfs2_extent_rec
*rec
;
1986 left_el
= path_leaf_el(left_path
);
1987 next_free
= le16_to_cpu(left_el
->l_next_free_rec
);
1988 rec
= &left_el
->l_recs
[next_free
- 1];
1990 if (insert_cpos
> le32_to_cpu(rec
->e_cpos
))
1995 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list
*el
, u32 cpos
)
1997 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1999 struct ocfs2_extent_rec
*rec
;
2004 rec
= &el
->l_recs
[0];
2005 if (ocfs2_is_empty_extent(rec
)) {
2009 rec
= &el
->l_recs
[1];
2012 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2013 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
2019 * Rotate all the records in a btree right one record, starting at insert_cpos.
2021 * The path to the rightmost leaf should be passed in.
2023 * The array is assumed to be large enough to hold an entire path (tree depth).
2025 * Upon succesful return from this function:
2027 * - The 'right_path' array will contain a path to the leaf block
2028 * whose range contains e_cpos.
2029 * - That leaf block will have a single empty extent in list index 0.
2030 * - In the case that the rotation requires a post-insert update,
2031 * *ret_left_path will contain a valid path which can be passed to
2032 * ocfs2_insert_path().
2034 static int ocfs2_rotate_tree_right(struct inode
*inode
,
2036 enum ocfs2_split_type split
,
2038 struct ocfs2_path
*right_path
,
2039 struct ocfs2_path
**ret_left_path
)
2041 int ret
, start
, orig_credits
= handle
->h_buffer_credits
;
2043 struct ocfs2_path
*left_path
= NULL
;
2045 *ret_left_path
= NULL
;
2047 left_path
= ocfs2_new_path(path_root_bh(right_path
),
2048 path_root_el(right_path
));
2055 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
, &cpos
);
2061 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos
, cpos
);
2064 * What we want to do here is:
2066 * 1) Start with the rightmost path.
2068 * 2) Determine a path to the leaf block directly to the left
2071 * 3) Determine the 'subtree root' - the lowest level tree node
2072 * which contains a path to both leaves.
2074 * 4) Rotate the subtree.
2076 * 5) Find the next subtree by considering the left path to be
2077 * the new right path.
2079 * The check at the top of this while loop also accepts
2080 * insert_cpos == cpos because cpos is only a _theoretical_
2081 * value to get us the left path - insert_cpos might very well
2082 * be filling that hole.
2084 * Stop at a cpos of '0' because we either started at the
2085 * leftmost branch (i.e., a tree with one branch and a
2086 * rotation inside of it), or we've gone as far as we can in
2087 * rotating subtrees.
2089 while (cpos
&& insert_cpos
<= cpos
) {
2090 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2093 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2099 mlog_bug_on_msg(path_leaf_bh(left_path
) ==
2100 path_leaf_bh(right_path
),
2101 "Inode %lu: error during insert of %u "
2102 "(left path cpos %u) results in two identical "
2103 "paths ending at %llu\n",
2104 inode
->i_ino
, insert_cpos
, cpos
,
2105 (unsigned long long)
2106 path_leaf_bh(left_path
)->b_blocknr
);
2108 if (split
== SPLIT_NONE
&&
2109 ocfs2_rotate_requires_path_adjustment(left_path
,
2113 * We've rotated the tree as much as we
2114 * should. The rest is up to
2115 * ocfs2_insert_path() to complete, after the
2116 * record insertion. We indicate this
2117 * situation by returning the left path.
2119 * The reason we don't adjust the records here
2120 * before the record insert is that an error
2121 * later might break the rule where a parent
2122 * record e_cpos will reflect the actual
2123 * e_cpos of the 1st nonempty record of the
2126 *ret_left_path
= left_path
;
2130 start
= ocfs2_find_subtree_root(inode
, left_path
, right_path
);
2132 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2134 (unsigned long long) right_path
->p_node
[start
].bh
->b_blocknr
,
2135 right_path
->p_tree_depth
);
2137 ret
= ocfs2_extend_rotate_transaction(handle
, start
,
2138 orig_credits
, right_path
);
2144 ret
= ocfs2_rotate_subtree_right(inode
, handle
, left_path
,
2151 if (split
!= SPLIT_NONE
&&
2152 ocfs2_leftmost_rec_contains(path_leaf_el(right_path
),
2155 * A rotate moves the rightmost left leaf
2156 * record over to the leftmost right leaf
2157 * slot. If we're doing an extent split
2158 * instead of a real insert, then we have to
2159 * check that the extent to be split wasn't
2160 * just moved over. If it was, then we can
2161 * exit here, passing left_path back -
2162 * ocfs2_split_extent() is smart enough to
2163 * search both leaves.
2165 *ret_left_path
= left_path
;
2170 * There is no need to re-read the next right path
2171 * as we know that it'll be our current left
2172 * path. Optimize by copying values instead.
2174 ocfs2_mv_path(right_path
, left_path
);
2176 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
2185 ocfs2_free_path(left_path
);
2191 static void ocfs2_update_edge_lengths(struct inode
*inode
, handle_t
*handle
,
2192 struct ocfs2_path
*path
)
2195 struct ocfs2_extent_rec
*rec
;
2196 struct ocfs2_extent_list
*el
;
2197 struct ocfs2_extent_block
*eb
;
2200 /* Path should always be rightmost. */
2201 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2202 BUG_ON(eb
->h_next_leaf_blk
!= 0ULL);
2205 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
2206 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2207 rec
= &el
->l_recs
[idx
];
2208 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2210 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
2211 el
= path
->p_node
[i
].el
;
2212 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2213 rec
= &el
->l_recs
[idx
];
2215 rec
->e_int_clusters
= cpu_to_le32(range
);
2216 le32_add_cpu(&rec
->e_int_clusters
, -le32_to_cpu(rec
->e_cpos
));
2218 ocfs2_journal_dirty(handle
, path
->p_node
[i
].bh
);
2222 static void ocfs2_unlink_path(struct inode
*inode
, handle_t
*handle
,
2223 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2224 struct ocfs2_path
*path
, int unlink_start
)
2227 struct ocfs2_extent_block
*eb
;
2228 struct ocfs2_extent_list
*el
;
2229 struct buffer_head
*bh
;
2231 for(i
= unlink_start
; i
< path_num_items(path
); i
++) {
2232 bh
= path
->p_node
[i
].bh
;
2234 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
2236 * Not all nodes might have had their final count
2237 * decremented by the caller - handle this here.
2240 if (le16_to_cpu(el
->l_next_free_rec
) > 1) {
2242 "Inode %llu, attempted to remove extent block "
2243 "%llu with %u records\n",
2244 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2245 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
2246 le16_to_cpu(el
->l_next_free_rec
));
2248 ocfs2_journal_dirty(handle
, bh
);
2249 ocfs2_remove_from_cache(inode
, bh
);
2253 el
->l_next_free_rec
= 0;
2254 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2256 ocfs2_journal_dirty(handle
, bh
);
2258 ret
= ocfs2_cache_extent_block_free(dealloc
, eb
);
2262 ocfs2_remove_from_cache(inode
, bh
);
2266 static void ocfs2_unlink_subtree(struct inode
*inode
, handle_t
*handle
,
2267 struct ocfs2_path
*left_path
,
2268 struct ocfs2_path
*right_path
,
2270 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2273 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
2274 struct ocfs2_extent_list
*root_el
= left_path
->p_node
[subtree_index
].el
;
2275 struct ocfs2_extent_list
*el
;
2276 struct ocfs2_extent_block
*eb
;
2278 el
= path_leaf_el(left_path
);
2280 eb
= (struct ocfs2_extent_block
*)right_path
->p_node
[subtree_index
+ 1].bh
->b_data
;
2282 for(i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
2283 if (root_el
->l_recs
[i
].e_blkno
== eb
->h_blkno
)
2286 BUG_ON(i
>= le16_to_cpu(root_el
->l_next_free_rec
));
2288 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
2289 le16_add_cpu(&root_el
->l_next_free_rec
, -1);
2291 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2292 eb
->h_next_leaf_blk
= 0;
2294 ocfs2_journal_dirty(handle
, root_bh
);
2295 ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2297 ocfs2_unlink_path(inode
, handle
, dealloc
, right_path
,
2301 static int ocfs2_rotate_subtree_left(struct inode
*inode
, handle_t
*handle
,
2302 struct ocfs2_path
*left_path
,
2303 struct ocfs2_path
*right_path
,
2305 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2307 struct ocfs2_extent_tree
*et
)
2309 int ret
, i
, del_right_subtree
= 0, right_has_empty
= 0;
2310 struct buffer_head
*root_bh
, *et_root_bh
= path_root_bh(right_path
);
2311 struct ocfs2_extent_list
*right_leaf_el
, *left_leaf_el
;
2312 struct ocfs2_extent_block
*eb
;
2316 right_leaf_el
= path_leaf_el(right_path
);
2317 left_leaf_el
= path_leaf_el(left_path
);
2318 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2319 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2321 if (!ocfs2_is_empty_extent(&left_leaf_el
->l_recs
[0]))
2324 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(right_path
)->b_data
;
2325 if (ocfs2_is_empty_extent(&right_leaf_el
->l_recs
[0])) {
2327 * It's legal for us to proceed if the right leaf is
2328 * the rightmost one and it has an empty extent. There
2329 * are two cases to handle - whether the leaf will be
2330 * empty after removal or not. If the leaf isn't empty
2331 * then just remove the empty extent up front. The
2332 * next block will handle empty leaves by flagging
2335 * Non rightmost leaves will throw -EAGAIN and the
2336 * caller can manually move the subtree and retry.
2339 if (eb
->h_next_leaf_blk
!= 0ULL)
2342 if (le16_to_cpu(right_leaf_el
->l_next_free_rec
) > 1) {
2343 ret
= ocfs2_journal_access(handle
, inode
,
2344 path_leaf_bh(right_path
),
2345 OCFS2_JOURNAL_ACCESS_WRITE
);
2351 ocfs2_remove_empty_extent(right_leaf_el
);
2353 right_has_empty
= 1;
2356 if (eb
->h_next_leaf_blk
== 0ULL &&
2357 le16_to_cpu(right_leaf_el
->l_next_free_rec
) == 1) {
2359 * We have to update i_last_eb_blk during the meta
2362 ret
= ocfs2_journal_access(handle
, inode
, et_root_bh
,
2363 OCFS2_JOURNAL_ACCESS_WRITE
);
2369 del_right_subtree
= 1;
2373 * Getting here with an empty extent in the right path implies
2374 * that it's the rightmost path and will be deleted.
2376 BUG_ON(right_has_empty
&& !del_right_subtree
);
2378 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
2379 OCFS2_JOURNAL_ACCESS_WRITE
);
2385 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2386 ret
= ocfs2_journal_access(handle
, inode
,
2387 right_path
->p_node
[i
].bh
,
2388 OCFS2_JOURNAL_ACCESS_WRITE
);
2394 ret
= ocfs2_journal_access(handle
, inode
,
2395 left_path
->p_node
[i
].bh
,
2396 OCFS2_JOURNAL_ACCESS_WRITE
);
2403 if (!right_has_empty
) {
2405 * Only do this if we're moving a real
2406 * record. Otherwise, the action is delayed until
2407 * after removal of the right path in which case we
2408 * can do a simple shift to remove the empty extent.
2410 ocfs2_rotate_leaf(left_leaf_el
, &right_leaf_el
->l_recs
[0]);
2411 memset(&right_leaf_el
->l_recs
[0], 0,
2412 sizeof(struct ocfs2_extent_rec
));
2414 if (eb
->h_next_leaf_blk
== 0ULL) {
2416 * Move recs over to get rid of empty extent, decrease
2417 * next_free. This is allowed to remove the last
2418 * extent in our leaf (setting l_next_free_rec to
2419 * zero) - the delete code below won't care.
2421 ocfs2_remove_empty_extent(right_leaf_el
);
2424 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2427 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
2431 if (del_right_subtree
) {
2432 ocfs2_unlink_subtree(inode
, handle
, left_path
, right_path
,
2433 subtree_index
, dealloc
);
2434 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2436 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2437 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2440 * Removal of the extent in the left leaf was skipped
2441 * above so we could delete the right path
2444 if (right_has_empty
)
2445 ocfs2_remove_empty_extent(left_leaf_el
);
2447 ret
= ocfs2_journal_dirty(handle
, et_root_bh
);
2453 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
2461 * Given a full path, determine what cpos value would return us a path
2462 * containing the leaf immediately to the right of the current one.
2464 * Will return zero if the path passed in is already the rightmost path.
2466 * This looks similar, but is subtly different to
2467 * ocfs2_find_cpos_for_left_leaf().
2469 static int ocfs2_find_cpos_for_right_leaf(struct super_block
*sb
,
2470 struct ocfs2_path
*path
, u32
*cpos
)
2474 struct ocfs2_extent_list
*el
;
2478 if (path
->p_tree_depth
== 0)
2481 blkno
= path_leaf_bh(path
)->b_blocknr
;
2483 /* Start at the tree node just above the leaf and work our way up. */
2484 i
= path
->p_tree_depth
- 1;
2488 el
= path
->p_node
[i
].el
;
2491 * Find the extent record just after the one in our
2494 next_free
= le16_to_cpu(el
->l_next_free_rec
);
2495 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2496 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2497 if (j
== (next_free
- 1)) {
2500 * We've determined that the
2501 * path specified is already
2502 * the rightmost one - return a
2508 * The rightmost record points to our
2509 * leaf - we need to travel up the
2515 *cpos
= le32_to_cpu(el
->l_recs
[j
+ 1].e_cpos
);
2521 * If we got here, we never found a valid node where
2522 * the tree indicated one should be.
2525 "Invalid extent tree at extent block %llu\n",
2526 (unsigned long long)blkno
);
2531 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2539 static int ocfs2_rotate_rightmost_leaf_left(struct inode
*inode
,
2541 struct buffer_head
*bh
,
2542 struct ocfs2_extent_list
*el
)
2546 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2549 ret
= ocfs2_journal_access(handle
, inode
, bh
,
2550 OCFS2_JOURNAL_ACCESS_WRITE
);
2556 ocfs2_remove_empty_extent(el
);
2558 ret
= ocfs2_journal_dirty(handle
, bh
);
2566 static int __ocfs2_rotate_tree_left(struct inode
*inode
,
2567 handle_t
*handle
, int orig_credits
,
2568 struct ocfs2_path
*path
,
2569 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2570 struct ocfs2_path
**empty_extent_path
,
2571 struct ocfs2_extent_tree
*et
)
2573 int ret
, subtree_root
, deleted
;
2575 struct ocfs2_path
*left_path
= NULL
;
2576 struct ocfs2_path
*right_path
= NULL
;
2578 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path
)->l_recs
[0])));
2580 *empty_extent_path
= NULL
;
2582 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, path
,
2589 left_path
= ocfs2_new_path(path_root_bh(path
),
2590 path_root_el(path
));
2597 ocfs2_cp_path(left_path
, path
);
2599 right_path
= ocfs2_new_path(path_root_bh(path
),
2600 path_root_el(path
));
2607 while (right_cpos
) {
2608 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
2614 subtree_root
= ocfs2_find_subtree_root(inode
, left_path
,
2617 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2619 (unsigned long long)
2620 right_path
->p_node
[subtree_root
].bh
->b_blocknr
,
2621 right_path
->p_tree_depth
);
2623 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_root
,
2624 orig_credits
, left_path
);
2631 * Caller might still want to make changes to the
2632 * tree root, so re-add it to the journal here.
2634 ret
= ocfs2_journal_access(handle
, inode
,
2635 path_root_bh(left_path
),
2636 OCFS2_JOURNAL_ACCESS_WRITE
);
2642 ret
= ocfs2_rotate_subtree_left(inode
, handle
, left_path
,
2643 right_path
, subtree_root
,
2644 dealloc
, &deleted
, et
);
2645 if (ret
== -EAGAIN
) {
2647 * The rotation has to temporarily stop due to
2648 * the right subtree having an empty
2649 * extent. Pass it back to the caller for a
2652 *empty_extent_path
= right_path
;
2662 * The subtree rotate might have removed records on
2663 * the rightmost edge. If so, then rotation is
2669 ocfs2_mv_path(left_path
, right_path
);
2671 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
2680 ocfs2_free_path(right_path
);
2681 ocfs2_free_path(left_path
);
2686 static int ocfs2_remove_rightmost_path(struct inode
*inode
, handle_t
*handle
,
2687 struct ocfs2_path
*path
,
2688 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2689 struct ocfs2_extent_tree
*et
)
2691 int ret
, subtree_index
;
2693 struct ocfs2_path
*left_path
= NULL
;
2694 struct ocfs2_extent_block
*eb
;
2695 struct ocfs2_extent_list
*el
;
2698 ret
= ocfs2_et_sanity_check(inode
, et
);
2702 * There's two ways we handle this depending on
2703 * whether path is the only existing one.
2705 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
2706 handle
->h_buffer_credits
,
2713 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
2719 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
2727 * We have a path to the left of this one - it needs
2730 left_path
= ocfs2_new_path(path_root_bh(path
),
2731 path_root_el(path
));
2738 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2744 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
2750 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
2752 ocfs2_unlink_subtree(inode
, handle
, left_path
, path
,
2753 subtree_index
, dealloc
);
2754 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2756 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2757 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2760 * 'path' is also the leftmost path which
2761 * means it must be the only one. This gets
2762 * handled differently because we want to
2763 * revert the inode back to having extents
2766 ocfs2_unlink_path(inode
, handle
, dealloc
, path
, 1);
2769 el
->l_tree_depth
= 0;
2770 el
->l_next_free_rec
= 0;
2771 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2773 ocfs2_et_set_last_eb_blk(et
, 0);
2776 ocfs2_journal_dirty(handle
, path_root_bh(path
));
2779 ocfs2_free_path(left_path
);
2784 * Left rotation of btree records.
2786 * In many ways, this is (unsurprisingly) the opposite of right
2787 * rotation. We start at some non-rightmost path containing an empty
2788 * extent in the leaf block. The code works its way to the rightmost
2789 * path by rotating records to the left in every subtree.
2791 * This is used by any code which reduces the number of extent records
2792 * in a leaf. After removal, an empty record should be placed in the
2793 * leftmost list position.
2795 * This won't handle a length update of the rightmost path records if
2796 * the rightmost tree leaf record is removed so the caller is
2797 * responsible for detecting and correcting that.
2799 static int ocfs2_rotate_tree_left(struct inode
*inode
, handle_t
*handle
,
2800 struct ocfs2_path
*path
,
2801 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2802 struct ocfs2_extent_tree
*et
)
2804 int ret
, orig_credits
= handle
->h_buffer_credits
;
2805 struct ocfs2_path
*tmp_path
= NULL
, *restart_path
= NULL
;
2806 struct ocfs2_extent_block
*eb
;
2807 struct ocfs2_extent_list
*el
;
2809 el
= path_leaf_el(path
);
2810 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2813 if (path
->p_tree_depth
== 0) {
2814 rightmost_no_delete
:
2816 * Inline extents. This is trivially handled, so do
2819 ret
= ocfs2_rotate_rightmost_leaf_left(inode
, handle
,
2821 path_leaf_el(path
));
2828 * Handle rightmost branch now. There's several cases:
2829 * 1) simple rotation leaving records in there. That's trivial.
2830 * 2) rotation requiring a branch delete - there's no more
2831 * records left. Two cases of this:
2832 * a) There are branches to the left.
2833 * b) This is also the leftmost (the only) branch.
2835 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2836 * 2a) we need the left branch so that we can update it with the unlink
2837 * 2b) we need to bring the inode back to inline extents.
2840 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2842 if (eb
->h_next_leaf_blk
== 0) {
2844 * This gets a bit tricky if we're going to delete the
2845 * rightmost path. Get the other cases out of the way
2848 if (le16_to_cpu(el
->l_next_free_rec
) > 1)
2849 goto rightmost_no_delete
;
2851 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
2853 ocfs2_error(inode
->i_sb
,
2854 "Inode %llu has empty extent block at %llu",
2855 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2856 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
2861 * XXX: The caller can not trust "path" any more after
2862 * this as it will have been deleted. What do we do?
2864 * In theory the rotate-for-merge code will never get
2865 * here because it'll always ask for a rotate in a
2869 ret
= ocfs2_remove_rightmost_path(inode
, handle
, path
,
2877 * Now we can loop, remembering the path we get from -EAGAIN
2878 * and restarting from there.
2881 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
, path
,
2882 dealloc
, &restart_path
, et
);
2883 if (ret
&& ret
!= -EAGAIN
) {
2888 while (ret
== -EAGAIN
) {
2889 tmp_path
= restart_path
;
2890 restart_path
= NULL
;
2892 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
,
2895 if (ret
&& ret
!= -EAGAIN
) {
2900 ocfs2_free_path(tmp_path
);
2908 ocfs2_free_path(tmp_path
);
2909 ocfs2_free_path(restart_path
);
2913 static void ocfs2_cleanup_merge(struct ocfs2_extent_list
*el
,
2916 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[index
];
2919 if (rec
->e_leaf_clusters
== 0) {
2921 * We consumed all of the merged-from record. An empty
2922 * extent cannot exist anywhere but the 1st array
2923 * position, so move things over if the merged-from
2924 * record doesn't occupy that position.
2926 * This creates a new empty extent so the caller
2927 * should be smart enough to have removed any existing
2931 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
2932 size
= index
* sizeof(struct ocfs2_extent_rec
);
2933 memmove(&el
->l_recs
[1], &el
->l_recs
[0], size
);
2937 * Always memset - the caller doesn't check whether it
2938 * created an empty extent, so there could be junk in
2941 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2945 static int ocfs2_get_right_path(struct inode
*inode
,
2946 struct ocfs2_path
*left_path
,
2947 struct ocfs2_path
**ret_right_path
)
2951 struct ocfs2_path
*right_path
= NULL
;
2952 struct ocfs2_extent_list
*left_el
;
2954 *ret_right_path
= NULL
;
2956 /* This function shouldn't be called for non-trees. */
2957 BUG_ON(left_path
->p_tree_depth
== 0);
2959 left_el
= path_leaf_el(left_path
);
2960 BUG_ON(left_el
->l_next_free_rec
!= left_el
->l_count
);
2962 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
2969 /* This function shouldn't be called for the rightmost leaf. */
2970 BUG_ON(right_cpos
== 0);
2972 right_path
= ocfs2_new_path(path_root_bh(left_path
),
2973 path_root_el(left_path
));
2980 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
2986 *ret_right_path
= right_path
;
2989 ocfs2_free_path(right_path
);
2994 * Remove split_rec clusters from the record at index and merge them
2995 * onto the beginning of the record "next" to it.
2996 * For index < l_count - 1, the next means the extent rec at index + 1.
2997 * For index == l_count - 1, the "next" means the 1st extent rec of the
2998 * next extent block.
3000 static int ocfs2_merge_rec_right(struct inode
*inode
,
3001 struct ocfs2_path
*left_path
,
3003 struct ocfs2_extent_rec
*split_rec
,
3006 int ret
, next_free
, i
;
3007 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3008 struct ocfs2_extent_rec
*left_rec
;
3009 struct ocfs2_extent_rec
*right_rec
;
3010 struct ocfs2_extent_list
*right_el
;
3011 struct ocfs2_path
*right_path
= NULL
;
3012 int subtree_index
= 0;
3013 struct ocfs2_extent_list
*el
= path_leaf_el(left_path
);
3014 struct buffer_head
*bh
= path_leaf_bh(left_path
);
3015 struct buffer_head
*root_bh
= NULL
;
3017 BUG_ON(index
>= le16_to_cpu(el
->l_next_free_rec
));
3018 left_rec
= &el
->l_recs
[index
];
3020 if (index
== le16_to_cpu(el
->l_next_free_rec
) - 1 &&
3021 le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
)) {
3022 /* we meet with a cross extent block merge. */
3023 ret
= ocfs2_get_right_path(inode
, left_path
, &right_path
);
3029 right_el
= path_leaf_el(right_path
);
3030 next_free
= le16_to_cpu(right_el
->l_next_free_rec
);
3031 BUG_ON(next_free
<= 0);
3032 right_rec
= &right_el
->l_recs
[0];
3033 if (ocfs2_is_empty_extent(right_rec
)) {
3034 BUG_ON(next_free
<= 1);
3035 right_rec
= &right_el
->l_recs
[1];
3038 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3039 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3040 le32_to_cpu(right_rec
->e_cpos
));
3042 subtree_index
= ocfs2_find_subtree_root(inode
,
3043 left_path
, right_path
);
3045 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3046 handle
->h_buffer_credits
,
3053 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3054 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3056 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
3057 OCFS2_JOURNAL_ACCESS_WRITE
);
3063 for (i
= subtree_index
+ 1;
3064 i
< path_num_items(right_path
); i
++) {
3065 ret
= ocfs2_journal_access(handle
, inode
,
3066 right_path
->p_node
[i
].bh
,
3067 OCFS2_JOURNAL_ACCESS_WRITE
);
3073 ret
= ocfs2_journal_access(handle
, inode
,
3074 left_path
->p_node
[i
].bh
,
3075 OCFS2_JOURNAL_ACCESS_WRITE
);
3083 BUG_ON(index
== le16_to_cpu(el
->l_next_free_rec
) - 1);
3084 right_rec
= &el
->l_recs
[index
+ 1];
3087 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3088 OCFS2_JOURNAL_ACCESS_WRITE
);
3094 le16_add_cpu(&left_rec
->e_leaf_clusters
, -split_clusters
);
3096 le32_add_cpu(&right_rec
->e_cpos
, -split_clusters
);
3097 le64_add_cpu(&right_rec
->e_blkno
,
3098 -ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3099 le16_add_cpu(&right_rec
->e_leaf_clusters
, split_clusters
);
3101 ocfs2_cleanup_merge(el
, index
);
3103 ret
= ocfs2_journal_dirty(handle
, bh
);
3108 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
3112 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3113 right_path
, subtree_index
);
3117 ocfs2_free_path(right_path
);
3121 static int ocfs2_get_left_path(struct inode
*inode
,
3122 struct ocfs2_path
*right_path
,
3123 struct ocfs2_path
**ret_left_path
)
3127 struct ocfs2_path
*left_path
= NULL
;
3129 *ret_left_path
= NULL
;
3131 /* This function shouldn't be called for non-trees. */
3132 BUG_ON(right_path
->p_tree_depth
== 0);
3134 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
3135 right_path
, &left_cpos
);
3141 /* This function shouldn't be called for the leftmost leaf. */
3142 BUG_ON(left_cpos
== 0);
3144 left_path
= ocfs2_new_path(path_root_bh(right_path
),
3145 path_root_el(right_path
));
3152 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3158 *ret_left_path
= left_path
;
3161 ocfs2_free_path(left_path
);
3166 * Remove split_rec clusters from the record at index and merge them
3167 * onto the tail of the record "before" it.
3168 * For index > 0, the "before" means the extent rec at index - 1.
3170 * For index == 0, the "before" means the last record of the previous
3171 * extent block. And there is also a situation that we may need to
3172 * remove the rightmost leaf extent block in the right_path and change
3173 * the right path to indicate the new rightmost path.
3175 static int ocfs2_merge_rec_left(struct inode
*inode
,
3176 struct ocfs2_path
*right_path
,
3178 struct ocfs2_extent_rec
*split_rec
,
3179 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3180 struct ocfs2_extent_tree
*et
,
3183 int ret
, i
, subtree_index
= 0, has_empty_extent
= 0;
3184 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3185 struct ocfs2_extent_rec
*left_rec
;
3186 struct ocfs2_extent_rec
*right_rec
;
3187 struct ocfs2_extent_list
*el
= path_leaf_el(right_path
);
3188 struct buffer_head
*bh
= path_leaf_bh(right_path
);
3189 struct buffer_head
*root_bh
= NULL
;
3190 struct ocfs2_path
*left_path
= NULL
;
3191 struct ocfs2_extent_list
*left_el
;
3195 right_rec
= &el
->l_recs
[index
];
3197 /* we meet with a cross extent block merge. */
3198 ret
= ocfs2_get_left_path(inode
, right_path
, &left_path
);
3204 left_el
= path_leaf_el(left_path
);
3205 BUG_ON(le16_to_cpu(left_el
->l_next_free_rec
) !=
3206 le16_to_cpu(left_el
->l_count
));
3208 left_rec
= &left_el
->l_recs
[
3209 le16_to_cpu(left_el
->l_next_free_rec
) - 1];
3210 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3211 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3212 le32_to_cpu(split_rec
->e_cpos
));
3214 subtree_index
= ocfs2_find_subtree_root(inode
,
3215 left_path
, right_path
);
3217 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3218 handle
->h_buffer_credits
,
3225 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3226 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3228 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
3229 OCFS2_JOURNAL_ACCESS_WRITE
);
3235 for (i
= subtree_index
+ 1;
3236 i
< path_num_items(right_path
); i
++) {
3237 ret
= ocfs2_journal_access(handle
, inode
,
3238 right_path
->p_node
[i
].bh
,
3239 OCFS2_JOURNAL_ACCESS_WRITE
);
3245 ret
= ocfs2_journal_access(handle
, inode
,
3246 left_path
->p_node
[i
].bh
,
3247 OCFS2_JOURNAL_ACCESS_WRITE
);
3254 left_rec
= &el
->l_recs
[index
- 1];
3255 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
3256 has_empty_extent
= 1;
3259 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3260 OCFS2_JOURNAL_ACCESS_WRITE
);
3266 if (has_empty_extent
&& index
== 1) {
3268 * The easy case - we can just plop the record right in.
3270 *left_rec
= *split_rec
;
3272 has_empty_extent
= 0;
3274 le16_add_cpu(&left_rec
->e_leaf_clusters
, split_clusters
);
3276 le32_add_cpu(&right_rec
->e_cpos
, split_clusters
);
3277 le64_add_cpu(&right_rec
->e_blkno
,
3278 ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3279 le16_add_cpu(&right_rec
->e_leaf_clusters
, -split_clusters
);
3281 ocfs2_cleanup_merge(el
, index
);
3283 ret
= ocfs2_journal_dirty(handle
, bh
);
3288 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
3293 * In the situation that the right_rec is empty and the extent
3294 * block is empty also, ocfs2_complete_edge_insert can't handle
3295 * it and we need to delete the right extent block.
3297 if (le16_to_cpu(right_rec
->e_leaf_clusters
) == 0 &&
3298 le16_to_cpu(el
->l_next_free_rec
) == 1) {
3300 ret
= ocfs2_remove_rightmost_path(inode
, handle
,
3308 /* Now the rightmost extent block has been deleted.
3309 * So we use the new rightmost path.
3311 ocfs2_mv_path(right_path
, left_path
);
3314 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3315 right_path
, subtree_index
);
3319 ocfs2_free_path(left_path
);
3323 static int ocfs2_try_to_merge_extent(struct inode
*inode
,
3325 struct ocfs2_path
*path
,
3327 struct ocfs2_extent_rec
*split_rec
,
3328 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3329 struct ocfs2_merge_ctxt
*ctxt
,
3330 struct ocfs2_extent_tree
*et
)
3334 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
3335 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
3337 BUG_ON(ctxt
->c_contig_type
== CONTIG_NONE
);
3339 if (ctxt
->c_split_covers_rec
&& ctxt
->c_has_empty_extent
) {
3341 * The merge code will need to create an empty
3342 * extent to take the place of the newly
3343 * emptied slot. Remove any pre-existing empty
3344 * extents - having more than one in a leaf is
3347 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3354 rec
= &el
->l_recs
[split_index
];
3357 if (ctxt
->c_contig_type
== CONTIG_LEFTRIGHT
) {
3359 * Left-right contig implies this.
3361 BUG_ON(!ctxt
->c_split_covers_rec
);
3364 * Since the leftright insert always covers the entire
3365 * extent, this call will delete the insert record
3366 * entirely, resulting in an empty extent record added to
3369 * Since the adding of an empty extent shifts
3370 * everything back to the right, there's no need to
3371 * update split_index here.
3373 * When the split_index is zero, we need to merge it to the
3374 * prevoius extent block. It is more efficient and easier
3375 * if we do merge_right first and merge_left later.
3377 ret
= ocfs2_merge_rec_right(inode
, path
,
3386 * We can only get this from logic error above.
3388 BUG_ON(!ocfs2_is_empty_extent(&el
->l_recs
[0]));
3390 /* The merge left us with an empty extent, remove it. */
3391 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3398 rec
= &el
->l_recs
[split_index
];
3401 * Note that we don't pass split_rec here on purpose -
3402 * we've merged it into the rec already.
3404 ret
= ocfs2_merge_rec_left(inode
, path
,
3414 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3417 * Error from this last rotate is not critical, so
3418 * print but don't bubble it up.
3425 * Merge a record to the left or right.
3427 * 'contig_type' is relative to the existing record,
3428 * so for example, if we're "right contig", it's to
3429 * the record on the left (hence the left merge).
3431 if (ctxt
->c_contig_type
== CONTIG_RIGHT
) {
3432 ret
= ocfs2_merge_rec_left(inode
,
3442 ret
= ocfs2_merge_rec_right(inode
,
3452 if (ctxt
->c_split_covers_rec
) {
3454 * The merge may have left an empty extent in
3455 * our leaf. Try to rotate it away.
3457 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3469 static void ocfs2_subtract_from_rec(struct super_block
*sb
,
3470 enum ocfs2_split_type split
,
3471 struct ocfs2_extent_rec
*rec
,
3472 struct ocfs2_extent_rec
*split_rec
)
3476 len_blocks
= ocfs2_clusters_to_blocks(sb
,
3477 le16_to_cpu(split_rec
->e_leaf_clusters
));
3479 if (split
== SPLIT_LEFT
) {
3481 * Region is on the left edge of the existing
3484 le32_add_cpu(&rec
->e_cpos
,
3485 le16_to_cpu(split_rec
->e_leaf_clusters
));
3486 le64_add_cpu(&rec
->e_blkno
, len_blocks
);
3487 le16_add_cpu(&rec
->e_leaf_clusters
,
3488 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3491 * Region is on the right edge of the existing
3494 le16_add_cpu(&rec
->e_leaf_clusters
,
3495 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3500 * Do the final bits of extent record insertion at the target leaf
3501 * list. If this leaf is part of an allocation tree, it is assumed
3502 * that the tree above has been prepared.
3504 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec
*insert_rec
,
3505 struct ocfs2_extent_list
*el
,
3506 struct ocfs2_insert_type
*insert
,
3507 struct inode
*inode
)
3509 int i
= insert
->ins_contig_index
;
3511 struct ocfs2_extent_rec
*rec
;
3513 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
3515 if (insert
->ins_split
!= SPLIT_NONE
) {
3516 i
= ocfs2_search_extent_list(el
, le32_to_cpu(insert_rec
->e_cpos
));
3518 rec
= &el
->l_recs
[i
];
3519 ocfs2_subtract_from_rec(inode
->i_sb
, insert
->ins_split
, rec
,
3525 * Contiguous insert - either left or right.
3527 if (insert
->ins_contig
!= CONTIG_NONE
) {
3528 rec
= &el
->l_recs
[i
];
3529 if (insert
->ins_contig
== CONTIG_LEFT
) {
3530 rec
->e_blkno
= insert_rec
->e_blkno
;
3531 rec
->e_cpos
= insert_rec
->e_cpos
;
3533 le16_add_cpu(&rec
->e_leaf_clusters
,
3534 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3539 * Handle insert into an empty leaf.
3541 if (le16_to_cpu(el
->l_next_free_rec
) == 0 ||
3542 ((le16_to_cpu(el
->l_next_free_rec
) == 1) &&
3543 ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3544 el
->l_recs
[0] = *insert_rec
;
3545 el
->l_next_free_rec
= cpu_to_le16(1);
3552 if (insert
->ins_appending
== APPEND_TAIL
) {
3553 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
3554 rec
= &el
->l_recs
[i
];
3555 range
= le32_to_cpu(rec
->e_cpos
)
3556 + le16_to_cpu(rec
->e_leaf_clusters
);
3557 BUG_ON(le32_to_cpu(insert_rec
->e_cpos
) < range
);
3559 mlog_bug_on_msg(le16_to_cpu(el
->l_next_free_rec
) >=
3560 le16_to_cpu(el
->l_count
),
3561 "inode %lu, depth %u, count %u, next free %u, "
3562 "rec.cpos %u, rec.clusters %u, "
3563 "insert.cpos %u, insert.clusters %u\n",
3565 le16_to_cpu(el
->l_tree_depth
),
3566 le16_to_cpu(el
->l_count
),
3567 le16_to_cpu(el
->l_next_free_rec
),
3568 le32_to_cpu(el
->l_recs
[i
].e_cpos
),
3569 le16_to_cpu(el
->l_recs
[i
].e_leaf_clusters
),
3570 le32_to_cpu(insert_rec
->e_cpos
),
3571 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3573 el
->l_recs
[i
] = *insert_rec
;
3574 le16_add_cpu(&el
->l_next_free_rec
, 1);
3580 * Ok, we have to rotate.
3582 * At this point, it is safe to assume that inserting into an
3583 * empty leaf and appending to a leaf have both been handled
3586 * This leaf needs to have space, either by the empty 1st
3587 * extent record, or by virtue of an l_next_rec < l_count.
3589 ocfs2_rotate_leaf(el
, insert_rec
);
3592 static void ocfs2_adjust_rightmost_records(struct inode
*inode
,
3594 struct ocfs2_path
*path
,
3595 struct ocfs2_extent_rec
*insert_rec
)
3597 int ret
, i
, next_free
;
3598 struct buffer_head
*bh
;
3599 struct ocfs2_extent_list
*el
;
3600 struct ocfs2_extent_rec
*rec
;
3603 * Update everything except the leaf block.
3605 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
3606 bh
= path
->p_node
[i
].bh
;
3607 el
= path
->p_node
[i
].el
;
3609 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3610 if (next_free
== 0) {
3611 ocfs2_error(inode
->i_sb
,
3612 "Dinode %llu has a bad extent list",
3613 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3618 rec
= &el
->l_recs
[next_free
- 1];
3620 rec
->e_int_clusters
= insert_rec
->e_cpos
;
3621 le32_add_cpu(&rec
->e_int_clusters
,
3622 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3623 le32_add_cpu(&rec
->e_int_clusters
,
3624 -le32_to_cpu(rec
->e_cpos
));
3626 ret
= ocfs2_journal_dirty(handle
, bh
);
3633 static int ocfs2_append_rec_to_path(struct inode
*inode
, handle_t
*handle
,
3634 struct ocfs2_extent_rec
*insert_rec
,
3635 struct ocfs2_path
*right_path
,
3636 struct ocfs2_path
**ret_left_path
)
3639 struct ocfs2_extent_list
*el
;
3640 struct ocfs2_path
*left_path
= NULL
;
3642 *ret_left_path
= NULL
;
3645 * This shouldn't happen for non-trees. The extent rec cluster
3646 * count manipulation below only works for interior nodes.
3648 BUG_ON(right_path
->p_tree_depth
== 0);
3651 * If our appending insert is at the leftmost edge of a leaf,
3652 * then we might need to update the rightmost records of the
3655 el
= path_leaf_el(right_path
);
3656 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3657 if (next_free
== 0 ||
3658 (next_free
== 1 && ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3661 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
3668 mlog(0, "Append may need a left path update. cpos: %u, "
3669 "left_cpos: %u\n", le32_to_cpu(insert_rec
->e_cpos
),
3673 * No need to worry if the append is already in the
3677 left_path
= ocfs2_new_path(path_root_bh(right_path
),
3678 path_root_el(right_path
));
3685 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3692 * ocfs2_insert_path() will pass the left_path to the
3698 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
3704 ocfs2_adjust_rightmost_records(inode
, handle
, right_path
, insert_rec
);
3706 *ret_left_path
= left_path
;
3710 ocfs2_free_path(left_path
);
3715 static void ocfs2_split_record(struct inode
*inode
,
3716 struct ocfs2_path
*left_path
,
3717 struct ocfs2_path
*right_path
,
3718 struct ocfs2_extent_rec
*split_rec
,
3719 enum ocfs2_split_type split
)
3722 u32 cpos
= le32_to_cpu(split_rec
->e_cpos
);
3723 struct ocfs2_extent_list
*left_el
= NULL
, *right_el
, *insert_el
, *el
;
3724 struct ocfs2_extent_rec
*rec
, *tmprec
;
3726 right_el
= path_leaf_el(right_path
);;
3728 left_el
= path_leaf_el(left_path
);
3731 insert_el
= right_el
;
3732 index
= ocfs2_search_extent_list(el
, cpos
);
3734 if (index
== 0 && left_path
) {
3735 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3738 * This typically means that the record
3739 * started in the left path but moved to the
3740 * right as a result of rotation. We either
3741 * move the existing record to the left, or we
3742 * do the later insert there.
3744 * In this case, the left path should always
3745 * exist as the rotate code will have passed
3746 * it back for a post-insert update.
3749 if (split
== SPLIT_LEFT
) {
3751 * It's a left split. Since we know
3752 * that the rotate code gave us an
3753 * empty extent in the left path, we
3754 * can just do the insert there.
3756 insert_el
= left_el
;
3759 * Right split - we have to move the
3760 * existing record over to the left
3761 * leaf. The insert will be into the
3762 * newly created empty extent in the
3765 tmprec
= &right_el
->l_recs
[index
];
3766 ocfs2_rotate_leaf(left_el
, tmprec
);
3769 memset(tmprec
, 0, sizeof(*tmprec
));
3770 index
= ocfs2_search_extent_list(left_el
, cpos
);
3771 BUG_ON(index
== -1);
3776 BUG_ON(!ocfs2_is_empty_extent(&left_el
->l_recs
[0]));
3778 * Left path is easy - we can just allow the insert to
3782 insert_el
= left_el
;
3783 index
= ocfs2_search_extent_list(el
, cpos
);
3784 BUG_ON(index
== -1);
3787 rec
= &el
->l_recs
[index
];
3788 ocfs2_subtract_from_rec(inode
->i_sb
, split
, rec
, split_rec
);
3789 ocfs2_rotate_leaf(insert_el
, split_rec
);
3793 * This function only does inserts on an allocation b-tree. For tree
3794 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3796 * right_path is the path we want to do the actual insert
3797 * in. left_path should only be passed in if we need to update that
3798 * portion of the tree after an edge insert.
3800 static int ocfs2_insert_path(struct inode
*inode
,
3802 struct ocfs2_path
*left_path
,
3803 struct ocfs2_path
*right_path
,
3804 struct ocfs2_extent_rec
*insert_rec
,
3805 struct ocfs2_insert_type
*insert
)
3807 int ret
, subtree_index
;
3808 struct buffer_head
*leaf_bh
= path_leaf_bh(right_path
);
3811 int credits
= handle
->h_buffer_credits
;
3814 * There's a chance that left_path got passed back to
3815 * us without being accounted for in the
3816 * journal. Extend our transaction here to be sure we
3817 * can change those blocks.
3819 credits
+= left_path
->p_tree_depth
;
3821 ret
= ocfs2_extend_trans(handle
, credits
);
3827 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
3835 * Pass both paths to the journal. The majority of inserts
3836 * will be touching all components anyway.
3838 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
3844 if (insert
->ins_split
!= SPLIT_NONE
) {
3846 * We could call ocfs2_insert_at_leaf() for some types
3847 * of splits, but it's easier to just let one separate
3848 * function sort it all out.
3850 ocfs2_split_record(inode
, left_path
, right_path
,
3851 insert_rec
, insert
->ins_split
);
3854 * Split might have modified either leaf and we don't
3855 * have a guarantee that the later edge insert will
3856 * dirty this for us.
3859 ret
= ocfs2_journal_dirty(handle
,
3860 path_leaf_bh(left_path
));
3864 ocfs2_insert_at_leaf(insert_rec
, path_leaf_el(right_path
),
3867 ret
= ocfs2_journal_dirty(handle
, leaf_bh
);
3873 * The rotate code has indicated that we need to fix
3874 * up portions of the tree after the insert.
3876 * XXX: Should we extend the transaction here?
3878 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
,
3880 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3881 right_path
, subtree_index
);
3889 static int ocfs2_do_insert_extent(struct inode
*inode
,
3891 struct ocfs2_extent_tree
*et
,
3892 struct ocfs2_extent_rec
*insert_rec
,
3893 struct ocfs2_insert_type
*type
)
3895 int ret
, rotate
= 0;
3897 struct ocfs2_path
*right_path
= NULL
;
3898 struct ocfs2_path
*left_path
= NULL
;
3899 struct ocfs2_extent_list
*el
;
3903 ret
= ocfs2_journal_access(handle
, inode
, et
->root_bh
,
3904 OCFS2_JOURNAL_ACCESS_WRITE
);
3910 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
3911 ocfs2_insert_at_leaf(insert_rec
, el
, type
, inode
);
3912 goto out_update_clusters
;
3915 right_path
= ocfs2_new_path(et
->root_bh
, et
->root_el
);
3923 * Determine the path to start with. Rotations need the
3924 * rightmost path, everything else can go directly to the
3927 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
3928 if (type
->ins_appending
== APPEND_NONE
&&
3929 type
->ins_contig
== CONTIG_NONE
) {
3934 ret
= ocfs2_find_path(inode
, right_path
, cpos
);
3941 * Rotations and appends need special treatment - they modify
3942 * parts of the tree's above them.
3944 * Both might pass back a path immediate to the left of the
3945 * one being inserted to. This will be cause
3946 * ocfs2_insert_path() to modify the rightmost records of
3947 * left_path to account for an edge insert.
3949 * XXX: When modifying this code, keep in mind that an insert
3950 * can wind up skipping both of these two special cases...
3953 ret
= ocfs2_rotate_tree_right(inode
, handle
, type
->ins_split
,
3954 le32_to_cpu(insert_rec
->e_cpos
),
3955 right_path
, &left_path
);
3962 * ocfs2_rotate_tree_right() might have extended the
3963 * transaction without re-journaling our tree root.
3965 ret
= ocfs2_journal_access(handle
, inode
, et
->root_bh
,
3966 OCFS2_JOURNAL_ACCESS_WRITE
);
3971 } else if (type
->ins_appending
== APPEND_TAIL
3972 && type
->ins_contig
!= CONTIG_LEFT
) {
3973 ret
= ocfs2_append_rec_to_path(inode
, handle
, insert_rec
,
3974 right_path
, &left_path
);
3981 ret
= ocfs2_insert_path(inode
, handle
, left_path
, right_path
,
3988 out_update_clusters
:
3989 if (type
->ins_split
== SPLIT_NONE
)
3990 ocfs2_et_update_clusters(inode
, et
,
3991 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3993 ret
= ocfs2_journal_dirty(handle
, et
->root_bh
);
3998 ocfs2_free_path(left_path
);
3999 ocfs2_free_path(right_path
);
4004 static enum ocfs2_contig_type
4005 ocfs2_figure_merge_contig_type(struct inode
*inode
, struct ocfs2_path
*path
,
4006 struct ocfs2_extent_list
*el
, int index
,
4007 struct ocfs2_extent_rec
*split_rec
)
4010 enum ocfs2_contig_type ret
= CONTIG_NONE
;
4011 u32 left_cpos
, right_cpos
;
4012 struct ocfs2_extent_rec
*rec
= NULL
;
4013 struct ocfs2_extent_list
*new_el
;
4014 struct ocfs2_path
*left_path
= NULL
, *right_path
= NULL
;
4015 struct buffer_head
*bh
;
4016 struct ocfs2_extent_block
*eb
;
4019 rec
= &el
->l_recs
[index
- 1];
4020 } else if (path
->p_tree_depth
> 0) {
4021 status
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
4026 if (left_cpos
!= 0) {
4027 left_path
= ocfs2_new_path(path_root_bh(path
),
4028 path_root_el(path
));
4032 status
= ocfs2_find_path(inode
, left_path
, left_cpos
);
4036 new_el
= path_leaf_el(left_path
);
4038 if (le16_to_cpu(new_el
->l_next_free_rec
) !=
4039 le16_to_cpu(new_el
->l_count
)) {
4040 bh
= path_leaf_bh(left_path
);
4041 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4042 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
,
4046 rec
= &new_el
->l_recs
[
4047 le16_to_cpu(new_el
->l_next_free_rec
) - 1];
4052 * We're careful to check for an empty extent record here -
4053 * the merge code will know what to do if it sees one.
4056 if (index
== 1 && ocfs2_is_empty_extent(rec
)) {
4057 if (split_rec
->e_cpos
== el
->l_recs
[index
].e_cpos
)
4060 ret
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4065 if (index
< (le16_to_cpu(el
->l_next_free_rec
) - 1))
4066 rec
= &el
->l_recs
[index
+ 1];
4067 else if (le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
) &&
4068 path
->p_tree_depth
> 0) {
4069 status
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
,
4074 if (right_cpos
== 0)
4077 right_path
= ocfs2_new_path(path_root_bh(path
),
4078 path_root_el(path
));
4082 status
= ocfs2_find_path(inode
, right_path
, right_cpos
);
4086 new_el
= path_leaf_el(right_path
);
4087 rec
= &new_el
->l_recs
[0];
4088 if (ocfs2_is_empty_extent(rec
)) {
4089 if (le16_to_cpu(new_el
->l_next_free_rec
) <= 1) {
4090 bh
= path_leaf_bh(right_path
);
4091 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4092 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
,
4096 rec
= &new_el
->l_recs
[1];
4101 enum ocfs2_contig_type contig_type
;
4103 contig_type
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4105 if (contig_type
== CONTIG_LEFT
&& ret
== CONTIG_RIGHT
)
4106 ret
= CONTIG_LEFTRIGHT
;
4107 else if (ret
== CONTIG_NONE
)
4113 ocfs2_free_path(left_path
);
4115 ocfs2_free_path(right_path
);
4120 static void ocfs2_figure_contig_type(struct inode
*inode
,
4121 struct ocfs2_insert_type
*insert
,
4122 struct ocfs2_extent_list
*el
,
4123 struct ocfs2_extent_rec
*insert_rec
,
4124 struct ocfs2_extent_tree
*et
)
4127 enum ocfs2_contig_type contig_type
= CONTIG_NONE
;
4129 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4131 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
4132 contig_type
= ocfs2_extent_contig(inode
, &el
->l_recs
[i
],
4134 if (contig_type
!= CONTIG_NONE
) {
4135 insert
->ins_contig_index
= i
;
4139 insert
->ins_contig
= contig_type
;
4141 if (insert
->ins_contig
!= CONTIG_NONE
) {
4142 struct ocfs2_extent_rec
*rec
=
4143 &el
->l_recs
[insert
->ins_contig_index
];
4144 unsigned int len
= le16_to_cpu(rec
->e_leaf_clusters
) +
4145 le16_to_cpu(insert_rec
->e_leaf_clusters
);
4148 * Caller might want us to limit the size of extents, don't
4149 * calculate contiguousness if we might exceed that limit.
4151 if (et
->max_leaf_clusters
&& len
> et
->max_leaf_clusters
)
4152 insert
->ins_contig
= CONTIG_NONE
;
4157 * This should only be called against the righmost leaf extent list.
4159 * ocfs2_figure_appending_type() will figure out whether we'll have to
4160 * insert at the tail of the rightmost leaf.
4162 * This should also work against the root extent list for tree's with 0
4163 * depth. If we consider the root extent list to be the rightmost leaf node
4164 * then the logic here makes sense.
4166 static void ocfs2_figure_appending_type(struct ocfs2_insert_type
*insert
,
4167 struct ocfs2_extent_list
*el
,
4168 struct ocfs2_extent_rec
*insert_rec
)
4171 u32 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4172 struct ocfs2_extent_rec
*rec
;
4174 insert
->ins_appending
= APPEND_NONE
;
4176 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4178 if (!el
->l_next_free_rec
)
4179 goto set_tail_append
;
4181 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
4182 /* Were all records empty? */
4183 if (le16_to_cpu(el
->l_next_free_rec
) == 1)
4184 goto set_tail_append
;
4187 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
4188 rec
= &el
->l_recs
[i
];
4191 (le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)))
4192 goto set_tail_append
;
4197 insert
->ins_appending
= APPEND_TAIL
;
4201 * Helper function called at the begining of an insert.
4203 * This computes a few things that are commonly used in the process of
4204 * inserting into the btree:
4205 * - Whether the new extent is contiguous with an existing one.
4206 * - The current tree depth.
4207 * - Whether the insert is an appending one.
4208 * - The total # of free records in the tree.
4210 * All of the information is stored on the ocfs2_insert_type
4213 static int ocfs2_figure_insert_type(struct inode
*inode
,
4214 struct ocfs2_extent_tree
*et
,
4215 struct buffer_head
**last_eb_bh
,
4216 struct ocfs2_extent_rec
*insert_rec
,
4218 struct ocfs2_insert_type
*insert
)
4221 struct ocfs2_extent_block
*eb
;
4222 struct ocfs2_extent_list
*el
;
4223 struct ocfs2_path
*path
= NULL
;
4224 struct buffer_head
*bh
= NULL
;
4226 insert
->ins_split
= SPLIT_NONE
;
4229 insert
->ins_tree_depth
= le16_to_cpu(el
->l_tree_depth
);
4231 if (el
->l_tree_depth
) {
4233 * If we have tree depth, we read in the
4234 * rightmost extent block ahead of time as
4235 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4236 * may want it later.
4238 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
),
4239 ocfs2_et_get_last_eb_blk(et
), &bh
,
4240 OCFS2_BH_CACHED
, inode
);
4245 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
4250 * Unless we have a contiguous insert, we'll need to know if
4251 * there is room left in our allocation tree for another
4254 * XXX: This test is simplistic, we can search for empty
4255 * extent records too.
4257 *free_records
= le16_to_cpu(el
->l_count
) -
4258 le16_to_cpu(el
->l_next_free_rec
);
4260 if (!insert
->ins_tree_depth
) {
4261 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4262 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4266 path
= ocfs2_new_path(et
->root_bh
, et
->root_el
);
4274 * In the case that we're inserting past what the tree
4275 * currently accounts for, ocfs2_find_path() will return for
4276 * us the rightmost tree path. This is accounted for below in
4277 * the appending code.
4279 ret
= ocfs2_find_path(inode
, path
, le32_to_cpu(insert_rec
->e_cpos
));
4285 el
= path_leaf_el(path
);
4288 * Now that we have the path, there's two things we want to determine:
4289 * 1) Contiguousness (also set contig_index if this is so)
4291 * 2) Are we doing an append? We can trivially break this up
4292 * into two types of appends: simple record append, or a
4293 * rotate inside the tail leaf.
4295 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4298 * The insert code isn't quite ready to deal with all cases of
4299 * left contiguousness. Specifically, if it's an insert into
4300 * the 1st record in a leaf, it will require the adjustment of
4301 * cluster count on the last record of the path directly to it's
4302 * left. For now, just catch that case and fool the layers
4303 * above us. This works just fine for tree_depth == 0, which
4304 * is why we allow that above.
4306 if (insert
->ins_contig
== CONTIG_LEFT
&&
4307 insert
->ins_contig_index
== 0)
4308 insert
->ins_contig
= CONTIG_NONE
;
4311 * Ok, so we can simply compare against last_eb to figure out
4312 * whether the path doesn't exist. This will only happen in
4313 * the case that we're doing a tail append, so maybe we can
4314 * take advantage of that information somehow.
4316 if (ocfs2_et_get_last_eb_blk(et
) ==
4317 path_leaf_bh(path
)->b_blocknr
) {
4319 * Ok, ocfs2_find_path() returned us the rightmost
4320 * tree path. This might be an appending insert. There are
4322 * 1) We're doing a true append at the tail:
4323 * -This might even be off the end of the leaf
4324 * 2) We're "appending" by rotating in the tail
4326 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4330 ocfs2_free_path(path
);
4340 * Insert an extent into an inode btree.
4342 * The caller needs to update fe->i_clusters
4344 static int ocfs2_insert_extent(struct ocfs2_super
*osb
,
4346 struct inode
*inode
,
4347 struct buffer_head
*root_bh
,
4352 struct ocfs2_alloc_context
*meta_ac
,
4353 struct ocfs2_extent_tree
*et
)
4356 int uninitialized_var(free_records
);
4357 struct buffer_head
*last_eb_bh
= NULL
;
4358 struct ocfs2_insert_type insert
= {0, };
4359 struct ocfs2_extent_rec rec
;
4361 BUG_ON(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
);
4363 mlog(0, "add %u clusters at position %u to inode %llu\n",
4364 new_clusters
, cpos
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4366 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb
) &&
4367 (OCFS2_I(inode
)->ip_clusters
!= cpos
),
4368 "Device %s, asking for sparse allocation: inode %llu, "
4369 "cpos %u, clusters %u\n",
4371 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
,
4372 OCFS2_I(inode
)->ip_clusters
);
4374 memset(&rec
, 0, sizeof(rec
));
4375 rec
.e_cpos
= cpu_to_le32(cpos
);
4376 rec
.e_blkno
= cpu_to_le64(start_blk
);
4377 rec
.e_leaf_clusters
= cpu_to_le16(new_clusters
);
4378 rec
.e_flags
= flags
;
4380 status
= ocfs2_figure_insert_type(inode
, et
, &last_eb_bh
, &rec
,
4381 &free_records
, &insert
);
4387 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4388 "Insert.contig_index: %d, Insert.free_records: %d, "
4389 "Insert.tree_depth: %d\n",
4390 insert
.ins_appending
, insert
.ins_contig
, insert
.ins_contig_index
,
4391 free_records
, insert
.ins_tree_depth
);
4393 if (insert
.ins_contig
== CONTIG_NONE
&& free_records
== 0) {
4394 status
= ocfs2_grow_tree(inode
, handle
, et
,
4395 &insert
.ins_tree_depth
, &last_eb_bh
,
4403 /* Finally, we can add clusters. This might rotate the tree for us. */
4404 status
= ocfs2_do_insert_extent(inode
, handle
, et
, &rec
, &insert
);
4407 else if (et
->type
== OCFS2_DINODE_EXTENT
)
4408 ocfs2_extent_map_insert_rec(inode
, &rec
);
4418 int ocfs2_dinode_insert_extent(struct ocfs2_super
*osb
,
4420 struct inode
*inode
,
4421 struct buffer_head
*root_bh
,
4426 struct ocfs2_alloc_context
*meta_ac
)
4429 struct ocfs2_extent_tree
*et
= NULL
;
4431 et
= ocfs2_new_extent_tree(inode
, root_bh
, OCFS2_DINODE_EXTENT
, NULL
);
4438 status
= ocfs2_insert_extent(osb
, handle
, inode
, root_bh
,
4439 cpos
, start_blk
, new_clusters
,
4440 flags
, meta_ac
, et
);
4443 ocfs2_free_extent_tree(et
);
4448 int ocfs2_xattr_value_insert_extent(struct ocfs2_super
*osb
,
4450 struct inode
*inode
,
4451 struct buffer_head
*root_bh
,
4456 struct ocfs2_alloc_context
*meta_ac
,
4460 struct ocfs2_extent_tree
*et
= NULL
;
4462 et
= ocfs2_new_extent_tree(inode
, root_bh
,
4463 OCFS2_XATTR_VALUE_EXTENT
, private);
4470 status
= ocfs2_insert_extent(osb
, handle
, inode
, root_bh
,
4471 cpos
, start_blk
, new_clusters
,
4472 flags
, meta_ac
, et
);
4475 ocfs2_free_extent_tree(et
);
4480 int ocfs2_xattr_tree_insert_extent(struct ocfs2_super
*osb
,
4482 struct inode
*inode
,
4483 struct buffer_head
*root_bh
,
4488 struct ocfs2_alloc_context
*meta_ac
)
4491 struct ocfs2_extent_tree
*et
= NULL
;
4493 et
= ocfs2_new_extent_tree(inode
, root_bh
, OCFS2_XATTR_TREE_EXTENT
,
4501 status
= ocfs2_insert_extent(osb
, handle
, inode
, root_bh
,
4502 cpos
, start_blk
, new_clusters
,
4503 flags
, meta_ac
, et
);
4506 ocfs2_free_extent_tree(et
);
4512 * Allcate and add clusters into the extent b-tree.
4513 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4514 * The extent b-tree's root is root_el and it should be in root_bh, and
4515 * it is not limited to the file storage. Any extent tree can use this
4516 * function if it implements the proper ocfs2_extent_tree.
4518 int ocfs2_add_clusters_in_btree(struct ocfs2_super
*osb
,
4519 struct inode
*inode
,
4520 u32
*logical_offset
,
4521 u32 clusters_to_add
,
4523 struct buffer_head
*root_bh
,
4524 struct ocfs2_extent_list
*root_el
,
4526 struct ocfs2_alloc_context
*data_ac
,
4527 struct ocfs2_alloc_context
*meta_ac
,
4528 enum ocfs2_alloc_restarted
*reason_ret
,
4529 enum ocfs2_extent_tree_type type
,
4534 enum ocfs2_alloc_restarted reason
= RESTART_NONE
;
4535 u32 bit_off
, num_bits
;
4539 BUG_ON(!clusters_to_add
);
4542 flags
= OCFS2_EXT_UNWRITTEN
;
4544 free_extents
= ocfs2_num_free_extents(osb
, inode
, root_bh
, type
,
4546 if (free_extents
< 0) {
4547 status
= free_extents
;
4552 /* there are two cases which could cause us to EAGAIN in the
4553 * we-need-more-metadata case:
4554 * 1) we haven't reserved *any*
4555 * 2) we are so fragmented, we've needed to add metadata too
4557 if (!free_extents
&& !meta_ac
) {
4558 mlog(0, "we haven't reserved any metadata!\n");
4560 reason
= RESTART_META
;
4562 } else if ((!free_extents
)
4563 && (ocfs2_alloc_context_bits_left(meta_ac
)
4564 < ocfs2_extend_meta_needed(root_el
))) {
4565 mlog(0, "filesystem is really fragmented...\n");
4567 reason
= RESTART_META
;
4571 status
= __ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
4572 clusters_to_add
, &bit_off
, &num_bits
);
4574 if (status
!= -ENOSPC
)
4579 BUG_ON(num_bits
> clusters_to_add
);
4581 /* reserve our write early -- insert_extent may update the inode */
4582 status
= ocfs2_journal_access(handle
, inode
, root_bh
,
4583 OCFS2_JOURNAL_ACCESS_WRITE
);
4589 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
4590 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4591 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4592 if (type
== OCFS2_DINODE_EXTENT
)
4593 status
= ocfs2_dinode_insert_extent(osb
, handle
, inode
, root_bh
,
4594 *logical_offset
, block
,
4595 num_bits
, flags
, meta_ac
);
4596 else if (type
== OCFS2_XATTR_TREE_EXTENT
)
4597 status
= ocfs2_xattr_tree_insert_extent(osb
, handle
,
4600 block
, num_bits
, flags
,
4603 status
= ocfs2_xattr_value_insert_extent(osb
, handle
,
4606 block
, num_bits
, flags
,
4613 status
= ocfs2_journal_dirty(handle
, root_bh
);
4619 clusters_to_add
-= num_bits
;
4620 *logical_offset
+= num_bits
;
4622 if (clusters_to_add
) {
4623 mlog(0, "need to alloc once more, wanted = %u\n",
4626 reason
= RESTART_TRANS
;
4632 *reason_ret
= reason
;
4636 static void ocfs2_make_right_split_rec(struct super_block
*sb
,
4637 struct ocfs2_extent_rec
*split_rec
,
4639 struct ocfs2_extent_rec
*rec
)
4641 u32 rec_cpos
= le32_to_cpu(rec
->e_cpos
);
4642 u32 rec_range
= rec_cpos
+ le16_to_cpu(rec
->e_leaf_clusters
);
4644 memset(split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4646 split_rec
->e_cpos
= cpu_to_le32(cpos
);
4647 split_rec
->e_leaf_clusters
= cpu_to_le16(rec_range
- cpos
);
4649 split_rec
->e_blkno
= rec
->e_blkno
;
4650 le64_add_cpu(&split_rec
->e_blkno
,
4651 ocfs2_clusters_to_blocks(sb
, cpos
- rec_cpos
));
4653 split_rec
->e_flags
= rec
->e_flags
;
4656 static int ocfs2_split_and_insert(struct inode
*inode
,
4658 struct ocfs2_path
*path
,
4659 struct ocfs2_extent_tree
*et
,
4660 struct buffer_head
**last_eb_bh
,
4662 struct ocfs2_extent_rec
*orig_split_rec
,
4663 struct ocfs2_alloc_context
*meta_ac
)
4666 unsigned int insert_range
, rec_range
, do_leftright
= 0;
4667 struct ocfs2_extent_rec tmprec
;
4668 struct ocfs2_extent_list
*rightmost_el
;
4669 struct ocfs2_extent_rec rec
;
4670 struct ocfs2_extent_rec split_rec
= *orig_split_rec
;
4671 struct ocfs2_insert_type insert
;
4672 struct ocfs2_extent_block
*eb
;
4676 * Store a copy of the record on the stack - it might move
4677 * around as the tree is manipulated below.
4679 rec
= path_leaf_el(path
)->l_recs
[split_index
];
4681 rightmost_el
= et
->root_el
;
4683 depth
= le16_to_cpu(rightmost_el
->l_tree_depth
);
4685 BUG_ON(!(*last_eb_bh
));
4686 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
4687 rightmost_el
= &eb
->h_list
;
4690 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4691 le16_to_cpu(rightmost_el
->l_count
)) {
4692 ret
= ocfs2_grow_tree(inode
, handle
, et
,
4693 &depth
, last_eb_bh
, meta_ac
);
4700 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4701 insert
.ins_appending
= APPEND_NONE
;
4702 insert
.ins_contig
= CONTIG_NONE
;
4703 insert
.ins_tree_depth
= depth
;
4705 insert_range
= le32_to_cpu(split_rec
.e_cpos
) +
4706 le16_to_cpu(split_rec
.e_leaf_clusters
);
4707 rec_range
= le32_to_cpu(rec
.e_cpos
) +
4708 le16_to_cpu(rec
.e_leaf_clusters
);
4710 if (split_rec
.e_cpos
== rec
.e_cpos
) {
4711 insert
.ins_split
= SPLIT_LEFT
;
4712 } else if (insert_range
== rec_range
) {
4713 insert
.ins_split
= SPLIT_RIGHT
;
4716 * Left/right split. We fake this as a right split
4717 * first and then make a second pass as a left split.
4719 insert
.ins_split
= SPLIT_RIGHT
;
4721 ocfs2_make_right_split_rec(inode
->i_sb
, &tmprec
, insert_range
,
4726 BUG_ON(do_leftright
);
4730 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
4736 if (do_leftright
== 1) {
4738 struct ocfs2_extent_list
*el
;
4741 split_rec
= *orig_split_rec
;
4743 ocfs2_reinit_path(path
, 1);
4745 cpos
= le32_to_cpu(split_rec
.e_cpos
);
4746 ret
= ocfs2_find_path(inode
, path
, cpos
);
4752 el
= path_leaf_el(path
);
4753 split_index
= ocfs2_search_extent_list(el
, cpos
);
4762 * Mark part or all of the extent record at split_index in the leaf
4763 * pointed to by path as written. This removes the unwritten
4766 * Care is taken to handle contiguousness so as to not grow the tree.
4768 * meta_ac is not strictly necessary - we only truly need it if growth
4769 * of the tree is required. All other cases will degrade into a less
4770 * optimal tree layout.
4772 * last_eb_bh should be the rightmost leaf block for any extent
4773 * btree. Since a split may grow the tree or a merge might shrink it,
4774 * the caller cannot trust the contents of that buffer after this call.
4776 * This code is optimized for readability - several passes might be
4777 * made over certain portions of the tree. All of those blocks will
4778 * have been brought into cache (and pinned via the journal), so the
4779 * extra overhead is not expressed in terms of disk reads.
4781 static int __ocfs2_mark_extent_written(struct inode
*inode
,
4782 struct ocfs2_extent_tree
*et
,
4784 struct ocfs2_path
*path
,
4786 struct ocfs2_extent_rec
*split_rec
,
4787 struct ocfs2_alloc_context
*meta_ac
,
4788 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
4791 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
4792 struct buffer_head
*last_eb_bh
= NULL
;
4793 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
4794 struct ocfs2_merge_ctxt ctxt
;
4795 struct ocfs2_extent_list
*rightmost_el
;
4797 if (!(rec
->e_flags
& OCFS2_EXT_UNWRITTEN
)) {
4803 if (le32_to_cpu(rec
->e_cpos
) > le32_to_cpu(split_rec
->e_cpos
) ||
4804 ((le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)) <
4805 (le32_to_cpu(split_rec
->e_cpos
) + le16_to_cpu(split_rec
->e_leaf_clusters
)))) {
4811 ctxt
.c_contig_type
= ocfs2_figure_merge_contig_type(inode
, path
, el
,
4816 * The core merge / split code wants to know how much room is
4817 * left in this inodes allocation tree, so we pass the
4818 * rightmost extent list.
4820 if (path
->p_tree_depth
) {
4821 struct ocfs2_extent_block
*eb
;
4823 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
),
4824 ocfs2_et_get_last_eb_blk(et
),
4825 &last_eb_bh
, OCFS2_BH_CACHED
, inode
);
4831 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
4832 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
4833 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
4838 rightmost_el
= &eb
->h_list
;
4840 rightmost_el
= path_root_el(path
);
4842 if (rec
->e_cpos
== split_rec
->e_cpos
&&
4843 rec
->e_leaf_clusters
== split_rec
->e_leaf_clusters
)
4844 ctxt
.c_split_covers_rec
= 1;
4846 ctxt
.c_split_covers_rec
= 0;
4848 ctxt
.c_has_empty_extent
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
4850 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4851 split_index
, ctxt
.c_contig_type
, ctxt
.c_has_empty_extent
,
4852 ctxt
.c_split_covers_rec
);
4854 if (ctxt
.c_contig_type
== CONTIG_NONE
) {
4855 if (ctxt
.c_split_covers_rec
)
4856 el
->l_recs
[split_index
] = *split_rec
;
4858 ret
= ocfs2_split_and_insert(inode
, handle
, path
, et
,
4859 &last_eb_bh
, split_index
,
4860 split_rec
, meta_ac
);
4864 ret
= ocfs2_try_to_merge_extent(inode
, handle
, path
,
4865 split_index
, split_rec
,
4866 dealloc
, &ctxt
, et
);
4877 * Mark the already-existing extent at cpos as written for len clusters.
4879 * If the existing extent is larger than the request, initiate a
4880 * split. An attempt will be made at merging with adjacent extents.
4882 * The caller is responsible for passing down meta_ac if we'll need it.
4884 int ocfs2_mark_extent_written(struct inode
*inode
, struct buffer_head
*root_bh
,
4885 handle_t
*handle
, u32 cpos
, u32 len
, u32 phys
,
4886 struct ocfs2_alloc_context
*meta_ac
,
4887 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
4888 enum ocfs2_extent_tree_type et_type
,
4892 u64 start_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys
);
4893 struct ocfs2_extent_rec split_rec
;
4894 struct ocfs2_path
*left_path
= NULL
;
4895 struct ocfs2_extent_list
*el
;
4896 struct ocfs2_extent_tree
*et
= NULL
;
4898 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4899 inode
->i_ino
, cpos
, len
, phys
, (unsigned long long)start_blkno
);
4901 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode
->i_sb
))) {
4902 ocfs2_error(inode
->i_sb
, "Inode %llu has unwritten extents "
4903 "that are being written to, but the feature bit "
4904 "is not set in the super block.",
4905 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4910 et
= ocfs2_new_extent_tree(inode
, root_bh
, et_type
, private);
4918 * XXX: This should be fixed up so that we just re-insert the
4919 * next extent records.
4921 if (et_type
== OCFS2_DINODE_EXTENT
)
4922 ocfs2_extent_map_trunc(inode
, 0);
4924 left_path
= ocfs2_new_path(et
->root_bh
, et
->root_el
);
4931 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
4936 el
= path_leaf_el(left_path
);
4938 index
= ocfs2_search_extent_list(el
, cpos
);
4939 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
4940 ocfs2_error(inode
->i_sb
,
4941 "Inode %llu has an extent at cpos %u which can no "
4942 "longer be found.\n",
4943 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
4948 memset(&split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4949 split_rec
.e_cpos
= cpu_to_le32(cpos
);
4950 split_rec
.e_leaf_clusters
= cpu_to_le16(len
);
4951 split_rec
.e_blkno
= cpu_to_le64(start_blkno
);
4952 split_rec
.e_flags
= path_leaf_el(left_path
)->l_recs
[index
].e_flags
;
4953 split_rec
.e_flags
&= ~OCFS2_EXT_UNWRITTEN
;
4955 ret
= __ocfs2_mark_extent_written(inode
, et
, handle
, left_path
,
4956 index
, &split_rec
, meta_ac
,
4962 ocfs2_free_path(left_path
);
4964 ocfs2_free_extent_tree(et
);
4968 static int ocfs2_split_tree(struct inode
*inode
, struct ocfs2_extent_tree
*et
,
4969 handle_t
*handle
, struct ocfs2_path
*path
,
4970 int index
, u32 new_range
,
4971 struct ocfs2_alloc_context
*meta_ac
)
4973 int ret
, depth
, credits
= handle
->h_buffer_credits
;
4974 struct buffer_head
*last_eb_bh
= NULL
;
4975 struct ocfs2_extent_block
*eb
;
4976 struct ocfs2_extent_list
*rightmost_el
, *el
;
4977 struct ocfs2_extent_rec split_rec
;
4978 struct ocfs2_extent_rec
*rec
;
4979 struct ocfs2_insert_type insert
;
4982 * Setup the record to split before we grow the tree.
4984 el
= path_leaf_el(path
);
4985 rec
= &el
->l_recs
[index
];
4986 ocfs2_make_right_split_rec(inode
->i_sb
, &split_rec
, new_range
, rec
);
4988 depth
= path
->p_tree_depth
;
4990 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
),
4991 ocfs2_et_get_last_eb_blk(et
),
4992 &last_eb_bh
, OCFS2_BH_CACHED
, inode
);
4998 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
4999 rightmost_el
= &eb
->h_list
;
5001 rightmost_el
= path_leaf_el(path
);
5003 credits
+= path
->p_tree_depth
+
5004 ocfs2_extend_meta_needed(et
->root_el
);
5005 ret
= ocfs2_extend_trans(handle
, credits
);
5011 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
5012 le16_to_cpu(rightmost_el
->l_count
)) {
5013 ret
= ocfs2_grow_tree(inode
, handle
, et
, &depth
, &last_eb_bh
,
5021 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
5022 insert
.ins_appending
= APPEND_NONE
;
5023 insert
.ins_contig
= CONTIG_NONE
;
5024 insert
.ins_split
= SPLIT_RIGHT
;
5025 insert
.ins_tree_depth
= depth
;
5027 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
5036 static int ocfs2_truncate_rec(struct inode
*inode
, handle_t
*handle
,
5037 struct ocfs2_path
*path
, int index
,
5038 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5040 struct ocfs2_extent_tree
*et
)
5043 u32 left_cpos
, rec_range
, trunc_range
;
5044 int wants_rotate
= 0, is_rightmost_tree_rec
= 0;
5045 struct super_block
*sb
= inode
->i_sb
;
5046 struct ocfs2_path
*left_path
= NULL
;
5047 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
5048 struct ocfs2_extent_rec
*rec
;
5049 struct ocfs2_extent_block
*eb
;
5051 if (ocfs2_is_empty_extent(&el
->l_recs
[0]) && index
> 0) {
5052 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5061 if (index
== (le16_to_cpu(el
->l_next_free_rec
) - 1) &&
5062 path
->p_tree_depth
) {
5064 * Check whether this is the rightmost tree record. If
5065 * we remove all of this record or part of its right
5066 * edge then an update of the record lengths above it
5069 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
5070 if (eb
->h_next_leaf_blk
== 0)
5071 is_rightmost_tree_rec
= 1;
5074 rec
= &el
->l_recs
[index
];
5075 if (index
== 0 && path
->p_tree_depth
&&
5076 le32_to_cpu(rec
->e_cpos
) == cpos
) {
5078 * Changing the leftmost offset (via partial or whole
5079 * record truncate) of an interior (or rightmost) path
5080 * means we have to update the subtree that is formed
5081 * by this leaf and the one to it's left.
5083 * There are two cases we can skip:
5084 * 1) Path is the leftmost one in our inode tree.
5085 * 2) The leaf is rightmost and will be empty after
5086 * we remove the extent record - the rotate code
5087 * knows how to update the newly formed edge.
5090 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
,
5097 if (left_cpos
&& le16_to_cpu(el
->l_next_free_rec
) > 1) {
5098 left_path
= ocfs2_new_path(path_root_bh(path
),
5099 path_root_el(path
));
5106 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
5114 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
5115 handle
->h_buffer_credits
,
5122 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
5128 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
5134 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5135 trunc_range
= cpos
+ len
;
5137 if (le32_to_cpu(rec
->e_cpos
) == cpos
&& rec_range
== trunc_range
) {
5140 memset(rec
, 0, sizeof(*rec
));
5141 ocfs2_cleanup_merge(el
, index
);
5144 next_free
= le16_to_cpu(el
->l_next_free_rec
);
5145 if (is_rightmost_tree_rec
&& next_free
> 1) {
5147 * We skip the edge update if this path will
5148 * be deleted by the rotate code.
5150 rec
= &el
->l_recs
[next_free
- 1];
5151 ocfs2_adjust_rightmost_records(inode
, handle
, path
,
5154 } else if (le32_to_cpu(rec
->e_cpos
) == cpos
) {
5155 /* Remove leftmost portion of the record. */
5156 le32_add_cpu(&rec
->e_cpos
, len
);
5157 le64_add_cpu(&rec
->e_blkno
, ocfs2_clusters_to_blocks(sb
, len
));
5158 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5159 } else if (rec_range
== trunc_range
) {
5160 /* Remove rightmost portion of the record */
5161 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5162 if (is_rightmost_tree_rec
)
5163 ocfs2_adjust_rightmost_records(inode
, handle
, path
, rec
);
5165 /* Caller should have trapped this. */
5166 mlog(ML_ERROR
, "Inode %llu: Invalid record truncate: (%u, %u) "
5167 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5168 le32_to_cpu(rec
->e_cpos
),
5169 le16_to_cpu(rec
->e_leaf_clusters
), cpos
, len
);
5176 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
5177 ocfs2_complete_edge_insert(inode
, handle
, left_path
, path
,
5181 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
5183 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5190 ocfs2_free_path(left_path
);
5194 int ocfs2_remove_extent(struct inode
*inode
, struct buffer_head
*root_bh
,
5195 u32 cpos
, u32 len
, handle_t
*handle
,
5196 struct ocfs2_alloc_context
*meta_ac
,
5197 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5198 enum ocfs2_extent_tree_type et_type
,
5202 u32 rec_range
, trunc_range
;
5203 struct ocfs2_extent_rec
*rec
;
5204 struct ocfs2_extent_list
*el
;
5205 struct ocfs2_path
*path
= NULL
;
5206 struct ocfs2_extent_tree
*et
= NULL
;
5208 et
= ocfs2_new_extent_tree(inode
, root_bh
, et_type
, private);
5215 ocfs2_extent_map_trunc(inode
, 0);
5217 path
= ocfs2_new_path(et
->root_bh
, et
->root_el
);
5224 ret
= ocfs2_find_path(inode
, path
, cpos
);
5230 el
= path_leaf_el(path
);
5231 index
= ocfs2_search_extent_list(el
, cpos
);
5232 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5233 ocfs2_error(inode
->i_sb
,
5234 "Inode %llu has an extent at cpos %u which can no "
5235 "longer be found.\n",
5236 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
5242 * We have 3 cases of extent removal:
5243 * 1) Range covers the entire extent rec
5244 * 2) Range begins or ends on one edge of the extent rec
5245 * 3) Range is in the middle of the extent rec (no shared edges)
5247 * For case 1 we remove the extent rec and left rotate to
5250 * For case 2 we just shrink the existing extent rec, with a
5251 * tree update if the shrinking edge is also the edge of an
5254 * For case 3 we do a right split to turn the extent rec into
5255 * something case 2 can handle.
5257 rec
= &el
->l_recs
[index
];
5258 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5259 trunc_range
= cpos
+ len
;
5261 BUG_ON(cpos
< le32_to_cpu(rec
->e_cpos
) || trunc_range
> rec_range
);
5263 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5264 "(cpos %u, len %u)\n",
5265 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
, len
, index
,
5266 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
));
5268 if (le32_to_cpu(rec
->e_cpos
) == cpos
|| rec_range
== trunc_range
) {
5269 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5276 ret
= ocfs2_split_tree(inode
, et
, handle
, path
, index
,
5277 trunc_range
, meta_ac
);
5284 * The split could have manipulated the tree enough to
5285 * move the record location, so we have to look for it again.
5287 ocfs2_reinit_path(path
, 1);
5289 ret
= ocfs2_find_path(inode
, path
, cpos
);
5295 el
= path_leaf_el(path
);
5296 index
= ocfs2_search_extent_list(el
, cpos
);
5297 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5298 ocfs2_error(inode
->i_sb
,
5299 "Inode %llu: split at cpos %u lost record.",
5300 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5307 * Double check our values here. If anything is fishy,
5308 * it's easier to catch it at the top level.
5310 rec
= &el
->l_recs
[index
];
5311 rec_range
= le32_to_cpu(rec
->e_cpos
) +
5312 ocfs2_rec_clusters(el
, rec
);
5313 if (rec_range
!= trunc_range
) {
5314 ocfs2_error(inode
->i_sb
,
5315 "Inode %llu: error after split at cpos %u"
5316 "trunc len %u, existing record is (%u,%u)",
5317 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5318 cpos
, len
, le32_to_cpu(rec
->e_cpos
),
5319 ocfs2_rec_clusters(el
, rec
));
5324 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5333 ocfs2_free_path(path
);
5335 ocfs2_free_extent_tree(et
);
5339 int ocfs2_truncate_log_needs_flush(struct ocfs2_super
*osb
)
5341 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5342 struct ocfs2_dinode
*di
;
5343 struct ocfs2_truncate_log
*tl
;
5345 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5346 tl
= &di
->id2
.i_dealloc
;
5348 mlog_bug_on_msg(le16_to_cpu(tl
->tl_used
) > le16_to_cpu(tl
->tl_count
),
5349 "slot %d, invalid truncate log parameters: used = "
5350 "%u, count = %u\n", osb
->slot_num
,
5351 le16_to_cpu(tl
->tl_used
), le16_to_cpu(tl
->tl_count
));
5352 return le16_to_cpu(tl
->tl_used
) == le16_to_cpu(tl
->tl_count
);
5355 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log
*tl
,
5356 unsigned int new_start
)
5358 unsigned int tail_index
;
5359 unsigned int current_tail
;
5361 /* No records, nothing to coalesce */
5362 if (!le16_to_cpu(tl
->tl_used
))
5365 tail_index
= le16_to_cpu(tl
->tl_used
) - 1;
5366 current_tail
= le32_to_cpu(tl
->tl_recs
[tail_index
].t_start
);
5367 current_tail
+= le32_to_cpu(tl
->tl_recs
[tail_index
].t_clusters
);
5369 return current_tail
== new_start
;
5372 int ocfs2_truncate_log_append(struct ocfs2_super
*osb
,
5375 unsigned int num_clusters
)
5378 unsigned int start_cluster
, tl_count
;
5379 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5380 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5381 struct ocfs2_dinode
*di
;
5382 struct ocfs2_truncate_log
*tl
;
5384 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5385 (unsigned long long)start_blk
, num_clusters
);
5387 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5389 start_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, start_blk
);
5391 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5392 tl
= &di
->id2
.i_dealloc
;
5393 if (!OCFS2_IS_VALID_DINODE(di
)) {
5394 OCFS2_RO_ON_INVALID_DINODE(osb
->sb
, di
);
5399 tl_count
= le16_to_cpu(tl
->tl_count
);
5400 mlog_bug_on_msg(tl_count
> ocfs2_truncate_recs_per_inode(osb
->sb
) ||
5402 "Truncate record count on #%llu invalid "
5403 "wanted %u, actual %u\n",
5404 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
,
5405 ocfs2_truncate_recs_per_inode(osb
->sb
),
5406 le16_to_cpu(tl
->tl_count
));
5408 /* Caller should have known to flush before calling us. */
5409 index
= le16_to_cpu(tl
->tl_used
);
5410 if (index
>= tl_count
) {
5416 status
= ocfs2_journal_access(handle
, tl_inode
, tl_bh
,
5417 OCFS2_JOURNAL_ACCESS_WRITE
);
5423 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5424 "%llu (index = %d)\n", num_clusters
, start_cluster
,
5425 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
, index
);
5427 if (ocfs2_truncate_log_can_coalesce(tl
, start_cluster
)) {
5429 * Move index back to the record we are coalescing with.
5430 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5434 num_clusters
+= le32_to_cpu(tl
->tl_recs
[index
].t_clusters
);
5435 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5436 index
, le32_to_cpu(tl
->tl_recs
[index
].t_start
),
5439 tl
->tl_recs
[index
].t_start
= cpu_to_le32(start_cluster
);
5440 tl
->tl_used
= cpu_to_le16(index
+ 1);
5442 tl
->tl_recs
[index
].t_clusters
= cpu_to_le32(num_clusters
);
5444 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5455 static int ocfs2_replay_truncate_records(struct ocfs2_super
*osb
,
5457 struct inode
*data_alloc_inode
,
5458 struct buffer_head
*data_alloc_bh
)
5462 unsigned int num_clusters
;
5464 struct ocfs2_truncate_rec rec
;
5465 struct ocfs2_dinode
*di
;
5466 struct ocfs2_truncate_log
*tl
;
5467 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5468 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5472 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5473 tl
= &di
->id2
.i_dealloc
;
5474 i
= le16_to_cpu(tl
->tl_used
) - 1;
5476 /* Caller has given us at least enough credits to
5477 * update the truncate log dinode */
5478 status
= ocfs2_journal_access(handle
, tl_inode
, tl_bh
,
5479 OCFS2_JOURNAL_ACCESS_WRITE
);
5485 tl
->tl_used
= cpu_to_le16(i
);
5487 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5493 /* TODO: Perhaps we can calculate the bulk of the
5494 * credits up front rather than extending like
5496 status
= ocfs2_extend_trans(handle
,
5497 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC
);
5503 rec
= tl
->tl_recs
[i
];
5504 start_blk
= ocfs2_clusters_to_blocks(data_alloc_inode
->i_sb
,
5505 le32_to_cpu(rec
.t_start
));
5506 num_clusters
= le32_to_cpu(rec
.t_clusters
);
5508 /* if start_blk is not set, we ignore the record as
5511 mlog(0, "free record %d, start = %u, clusters = %u\n",
5512 i
, le32_to_cpu(rec
.t_start
), num_clusters
);
5514 status
= ocfs2_free_clusters(handle
, data_alloc_inode
,
5515 data_alloc_bh
, start_blk
,
5530 /* Expects you to already be holding tl_inode->i_mutex */
5531 int __ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5534 unsigned int num_to_flush
;
5536 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5537 struct inode
*data_alloc_inode
= NULL
;
5538 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5539 struct buffer_head
*data_alloc_bh
= NULL
;
5540 struct ocfs2_dinode
*di
;
5541 struct ocfs2_truncate_log
*tl
;
5545 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5547 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5548 tl
= &di
->id2
.i_dealloc
;
5549 if (!OCFS2_IS_VALID_DINODE(di
)) {
5550 OCFS2_RO_ON_INVALID_DINODE(osb
->sb
, di
);
5555 num_to_flush
= le16_to_cpu(tl
->tl_used
);
5556 mlog(0, "Flush %u records from truncate log #%llu\n",
5557 num_to_flush
, (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
);
5558 if (!num_to_flush
) {
5563 data_alloc_inode
= ocfs2_get_system_file_inode(osb
,
5564 GLOBAL_BITMAP_SYSTEM_INODE
,
5565 OCFS2_INVALID_SLOT
);
5566 if (!data_alloc_inode
) {
5568 mlog(ML_ERROR
, "Could not get bitmap inode!\n");
5572 mutex_lock(&data_alloc_inode
->i_mutex
);
5574 status
= ocfs2_inode_lock(data_alloc_inode
, &data_alloc_bh
, 1);
5580 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5581 if (IS_ERR(handle
)) {
5582 status
= PTR_ERR(handle
);
5587 status
= ocfs2_replay_truncate_records(osb
, handle
, data_alloc_inode
,
5592 ocfs2_commit_trans(osb
, handle
);
5595 brelse(data_alloc_bh
);
5596 ocfs2_inode_unlock(data_alloc_inode
, 1);
5599 mutex_unlock(&data_alloc_inode
->i_mutex
);
5600 iput(data_alloc_inode
);
5607 int ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5610 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5612 mutex_lock(&tl_inode
->i_mutex
);
5613 status
= __ocfs2_flush_truncate_log(osb
);
5614 mutex_unlock(&tl_inode
->i_mutex
);
5619 static void ocfs2_truncate_log_worker(struct work_struct
*work
)
5622 struct ocfs2_super
*osb
=
5623 container_of(work
, struct ocfs2_super
,
5624 osb_truncate_log_wq
.work
);
5628 status
= ocfs2_flush_truncate_log(osb
);
5632 ocfs2_init_inode_steal_slot(osb
);
5637 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5638 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super
*osb
,
5641 if (osb
->osb_tl_inode
) {
5642 /* We want to push off log flushes while truncates are
5645 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5647 queue_delayed_work(ocfs2_wq
, &osb
->osb_truncate_log_wq
,
5648 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL
);
5652 static int ocfs2_get_truncate_log_info(struct ocfs2_super
*osb
,
5654 struct inode
**tl_inode
,
5655 struct buffer_head
**tl_bh
)
5658 struct inode
*inode
= NULL
;
5659 struct buffer_head
*bh
= NULL
;
5661 inode
= ocfs2_get_system_file_inode(osb
,
5662 TRUNCATE_LOG_SYSTEM_INODE
,
5666 mlog(ML_ERROR
, "Could not get load truncate log inode!\n");
5670 status
= ocfs2_read_block(osb
, OCFS2_I(inode
)->ip_blkno
, &bh
,
5671 OCFS2_BH_CACHED
, inode
);
5685 /* called during the 1st stage of node recovery. we stamp a clean
5686 * truncate log and pass back a copy for processing later. if the
5687 * truncate log does not require processing, a *tl_copy is set to
5689 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super
*osb
,
5691 struct ocfs2_dinode
**tl_copy
)
5694 struct inode
*tl_inode
= NULL
;
5695 struct buffer_head
*tl_bh
= NULL
;
5696 struct ocfs2_dinode
*di
;
5697 struct ocfs2_truncate_log
*tl
;
5701 mlog(0, "recover truncate log from slot %d\n", slot_num
);
5703 status
= ocfs2_get_truncate_log_info(osb
, slot_num
, &tl_inode
, &tl_bh
);
5709 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5710 tl
= &di
->id2
.i_dealloc
;
5711 if (!OCFS2_IS_VALID_DINODE(di
)) {
5712 OCFS2_RO_ON_INVALID_DINODE(tl_inode
->i_sb
, di
);
5717 if (le16_to_cpu(tl
->tl_used
)) {
5718 mlog(0, "We'll have %u logs to recover\n",
5719 le16_to_cpu(tl
->tl_used
));
5721 *tl_copy
= kmalloc(tl_bh
->b_size
, GFP_KERNEL
);
5728 /* Assuming the write-out below goes well, this copy
5729 * will be passed back to recovery for processing. */
5730 memcpy(*tl_copy
, tl_bh
->b_data
, tl_bh
->b_size
);
5732 /* All we need to do to clear the truncate log is set
5736 status
= ocfs2_write_block(osb
, tl_bh
, tl_inode
);
5749 if (status
< 0 && (*tl_copy
)) {
5758 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super
*osb
,
5759 struct ocfs2_dinode
*tl_copy
)
5763 unsigned int clusters
, num_recs
, start_cluster
;
5766 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5767 struct ocfs2_truncate_log
*tl
;
5771 if (OCFS2_I(tl_inode
)->ip_blkno
== le64_to_cpu(tl_copy
->i_blkno
)) {
5772 mlog(ML_ERROR
, "Asked to recover my own truncate log!\n");
5776 tl
= &tl_copy
->id2
.i_dealloc
;
5777 num_recs
= le16_to_cpu(tl
->tl_used
);
5778 mlog(0, "cleanup %u records from %llu\n", num_recs
,
5779 (unsigned long long)le64_to_cpu(tl_copy
->i_blkno
));
5781 mutex_lock(&tl_inode
->i_mutex
);
5782 for(i
= 0; i
< num_recs
; i
++) {
5783 if (ocfs2_truncate_log_needs_flush(osb
)) {
5784 status
= __ocfs2_flush_truncate_log(osb
);
5791 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5792 if (IS_ERR(handle
)) {
5793 status
= PTR_ERR(handle
);
5798 clusters
= le32_to_cpu(tl
->tl_recs
[i
].t_clusters
);
5799 start_cluster
= le32_to_cpu(tl
->tl_recs
[i
].t_start
);
5800 start_blk
= ocfs2_clusters_to_blocks(osb
->sb
, start_cluster
);
5802 status
= ocfs2_truncate_log_append(osb
, handle
,
5803 start_blk
, clusters
);
5804 ocfs2_commit_trans(osb
, handle
);
5812 mutex_unlock(&tl_inode
->i_mutex
);
5818 void ocfs2_truncate_log_shutdown(struct ocfs2_super
*osb
)
5821 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5826 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5827 flush_workqueue(ocfs2_wq
);
5829 status
= ocfs2_flush_truncate_log(osb
);
5833 brelse(osb
->osb_tl_bh
);
5834 iput(osb
->osb_tl_inode
);
5840 int ocfs2_truncate_log_init(struct ocfs2_super
*osb
)
5843 struct inode
*tl_inode
= NULL
;
5844 struct buffer_head
*tl_bh
= NULL
;
5848 status
= ocfs2_get_truncate_log_info(osb
,
5855 /* ocfs2_truncate_log_shutdown keys on the existence of
5856 * osb->osb_tl_inode so we don't set any of the osb variables
5857 * until we're sure all is well. */
5858 INIT_DELAYED_WORK(&osb
->osb_truncate_log_wq
,
5859 ocfs2_truncate_log_worker
);
5860 osb
->osb_tl_bh
= tl_bh
;
5861 osb
->osb_tl_inode
= tl_inode
;
5868 * Delayed de-allocation of suballocator blocks.
5870 * Some sets of block de-allocations might involve multiple suballocator inodes.
5872 * The locking for this can get extremely complicated, especially when
5873 * the suballocator inodes to delete from aren't known until deep
5874 * within an unrelated codepath.
5876 * ocfs2_extent_block structures are a good example of this - an inode
5877 * btree could have been grown by any number of nodes each allocating
5878 * out of their own suballoc inode.
5880 * These structures allow the delay of block de-allocation until a
5881 * later time, when locking of multiple cluster inodes won't cause
5886 * Describes a single block free from a suballocator
5888 struct ocfs2_cached_block_free
{
5889 struct ocfs2_cached_block_free
*free_next
;
5891 unsigned int free_bit
;
5894 struct ocfs2_per_slot_free_list
{
5895 struct ocfs2_per_slot_free_list
*f_next_suballocator
;
5898 struct ocfs2_cached_block_free
*f_first
;
5901 static int ocfs2_free_cached_items(struct ocfs2_super
*osb
,
5904 struct ocfs2_cached_block_free
*head
)
5909 struct inode
*inode
;
5910 struct buffer_head
*di_bh
= NULL
;
5911 struct ocfs2_cached_block_free
*tmp
;
5913 inode
= ocfs2_get_system_file_inode(osb
, sysfile_type
, slot
);
5920 mutex_lock(&inode
->i_mutex
);
5922 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
5928 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
5929 if (IS_ERR(handle
)) {
5930 ret
= PTR_ERR(handle
);
5936 bg_blkno
= ocfs2_which_suballoc_group(head
->free_blk
,
5938 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5939 head
->free_bit
, (unsigned long long)head
->free_blk
);
5941 ret
= ocfs2_free_suballoc_bits(handle
, inode
, di_bh
,
5942 head
->free_bit
, bg_blkno
, 1);
5948 ret
= ocfs2_extend_trans(handle
, OCFS2_SUBALLOC_FREE
);
5955 head
= head
->free_next
;
5960 ocfs2_commit_trans(osb
, handle
);
5963 ocfs2_inode_unlock(inode
, 1);
5966 mutex_unlock(&inode
->i_mutex
);
5970 /* Premature exit may have left some dangling items. */
5972 head
= head
->free_next
;
5979 int ocfs2_run_deallocs(struct ocfs2_super
*osb
,
5980 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
5983 struct ocfs2_per_slot_free_list
*fl
;
5988 while (ctxt
->c_first_suballocator
) {
5989 fl
= ctxt
->c_first_suballocator
;
5992 mlog(0, "Free items: (type %u, slot %d)\n",
5993 fl
->f_inode_type
, fl
->f_slot
);
5994 ret2
= ocfs2_free_cached_items(osb
, fl
->f_inode_type
,
5995 fl
->f_slot
, fl
->f_first
);
6002 ctxt
->c_first_suballocator
= fl
->f_next_suballocator
;
6009 static struct ocfs2_per_slot_free_list
*
6010 ocfs2_find_per_slot_free_list(int type
,
6012 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6014 struct ocfs2_per_slot_free_list
*fl
= ctxt
->c_first_suballocator
;
6017 if (fl
->f_inode_type
== type
&& fl
->f_slot
== slot
)
6020 fl
= fl
->f_next_suballocator
;
6023 fl
= kmalloc(sizeof(*fl
), GFP_NOFS
);
6025 fl
->f_inode_type
= type
;
6028 fl
->f_next_suballocator
= ctxt
->c_first_suballocator
;
6030 ctxt
->c_first_suballocator
= fl
;
6035 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6036 int type
, int slot
, u64 blkno
,
6040 struct ocfs2_per_slot_free_list
*fl
;
6041 struct ocfs2_cached_block_free
*item
;
6043 fl
= ocfs2_find_per_slot_free_list(type
, slot
, ctxt
);
6050 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6057 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6058 type
, slot
, bit
, (unsigned long long)blkno
);
6060 item
->free_blk
= blkno
;
6061 item
->free_bit
= bit
;
6062 item
->free_next
= fl
->f_first
;
6071 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6072 struct ocfs2_extent_block
*eb
)
6074 return ocfs2_cache_block_dealloc(ctxt
, EXTENT_ALLOC_SYSTEM_INODE
,
6075 le16_to_cpu(eb
->h_suballoc_slot
),
6076 le64_to_cpu(eb
->h_blkno
),
6077 le16_to_cpu(eb
->h_suballoc_bit
));
6080 /* This function will figure out whether the currently last extent
6081 * block will be deleted, and if it will, what the new last extent
6082 * block will be so we can update his h_next_leaf_blk field, as well
6083 * as the dinodes i_last_eb_blk */
6084 static int ocfs2_find_new_last_ext_blk(struct inode
*inode
,
6085 unsigned int clusters_to_del
,
6086 struct ocfs2_path
*path
,
6087 struct buffer_head
**new_last_eb
)
6089 int next_free
, ret
= 0;
6091 struct ocfs2_extent_rec
*rec
;
6092 struct ocfs2_extent_block
*eb
;
6093 struct ocfs2_extent_list
*el
;
6094 struct buffer_head
*bh
= NULL
;
6096 *new_last_eb
= NULL
;
6098 /* we have no tree, so of course, no last_eb. */
6099 if (!path
->p_tree_depth
)
6102 /* trunc to zero special case - this makes tree_depth = 0
6103 * regardless of what it is. */
6104 if (OCFS2_I(inode
)->ip_clusters
== clusters_to_del
)
6107 el
= path_leaf_el(path
);
6108 BUG_ON(!el
->l_next_free_rec
);
6111 * Make sure that this extent list will actually be empty
6112 * after we clear away the data. We can shortcut out if
6113 * there's more than one non-empty extent in the
6114 * list. Otherwise, a check of the remaining extent is
6117 next_free
= le16_to_cpu(el
->l_next_free_rec
);
6119 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6123 /* We may have a valid extent in index 1, check it. */
6125 rec
= &el
->l_recs
[1];
6128 * Fall through - no more nonempty extents, so we want
6129 * to delete this leaf.
6135 rec
= &el
->l_recs
[0];
6140 * Check it we'll only be trimming off the end of this
6143 if (le16_to_cpu(rec
->e_leaf_clusters
) > clusters_to_del
)
6147 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
6153 ret
= ocfs2_find_leaf(inode
, path_root_el(path
), cpos
, &bh
);
6159 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
6161 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
6162 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
6168 get_bh(*new_last_eb
);
6169 mlog(0, "returning block %llu, (cpos: %u)\n",
6170 (unsigned long long)le64_to_cpu(eb
->h_blkno
), cpos
);
6178 * Trim some clusters off the rightmost edge of a tree. Only called
6181 * The caller needs to:
6182 * - start journaling of each path component.
6183 * - compute and fully set up any new last ext block
6185 static int ocfs2_trim_tree(struct inode
*inode
, struct ocfs2_path
*path
,
6186 handle_t
*handle
, struct ocfs2_truncate_context
*tc
,
6187 u32 clusters_to_del
, u64
*delete_start
)
6189 int ret
, i
, index
= path
->p_tree_depth
;
6192 struct buffer_head
*bh
;
6193 struct ocfs2_extent_list
*el
;
6194 struct ocfs2_extent_rec
*rec
;
6198 while (index
>= 0) {
6199 bh
= path
->p_node
[index
].bh
;
6200 el
= path
->p_node
[index
].el
;
6202 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6203 index
, (unsigned long long)bh
->b_blocknr
);
6205 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
6208 (path
->p_tree_depth
- le16_to_cpu(el
->l_tree_depth
))) {
6209 ocfs2_error(inode
->i_sb
,
6210 "Inode %lu has invalid ext. block %llu",
6212 (unsigned long long)bh
->b_blocknr
);
6218 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
6219 rec
= &el
->l_recs
[i
];
6221 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6222 "next = %u\n", i
, le32_to_cpu(rec
->e_cpos
),
6223 ocfs2_rec_clusters(el
, rec
),
6224 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6225 le16_to_cpu(el
->l_next_free_rec
));
6227 BUG_ON(ocfs2_rec_clusters(el
, rec
) < clusters_to_del
);
6229 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
6231 * If the leaf block contains a single empty
6232 * extent and no records, we can just remove
6235 if (i
== 0 && ocfs2_is_empty_extent(rec
)) {
6237 sizeof(struct ocfs2_extent_rec
));
6238 el
->l_next_free_rec
= cpu_to_le16(0);
6244 * Remove any empty extents by shifting things
6245 * left. That should make life much easier on
6246 * the code below. This condition is rare
6247 * enough that we shouldn't see a performance
6250 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6251 le16_add_cpu(&el
->l_next_free_rec
, -1);
6254 i
< le16_to_cpu(el
->l_next_free_rec
); i
++)
6255 el
->l_recs
[i
] = el
->l_recs
[i
+ 1];
6257 memset(&el
->l_recs
[i
], 0,
6258 sizeof(struct ocfs2_extent_rec
));
6261 * We've modified our extent list. The
6262 * simplest way to handle this change
6263 * is to being the search from the
6266 goto find_tail_record
;
6269 le16_add_cpu(&rec
->e_leaf_clusters
, -clusters_to_del
);
6272 * We'll use "new_edge" on our way back up the
6273 * tree to know what our rightmost cpos is.
6275 new_edge
= le16_to_cpu(rec
->e_leaf_clusters
);
6276 new_edge
+= le32_to_cpu(rec
->e_cpos
);
6279 * The caller will use this to delete data blocks.
6281 *delete_start
= le64_to_cpu(rec
->e_blkno
)
6282 + ocfs2_clusters_to_blocks(inode
->i_sb
,
6283 le16_to_cpu(rec
->e_leaf_clusters
));
6286 * If it's now empty, remove this record.
6288 if (le16_to_cpu(rec
->e_leaf_clusters
) == 0) {
6290 sizeof(struct ocfs2_extent_rec
));
6291 le16_add_cpu(&el
->l_next_free_rec
, -1);
6294 if (le64_to_cpu(rec
->e_blkno
) == deleted_eb
) {
6296 sizeof(struct ocfs2_extent_rec
));
6297 le16_add_cpu(&el
->l_next_free_rec
, -1);
6302 /* Can this actually happen? */
6303 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
6307 * We never actually deleted any clusters
6308 * because our leaf was empty. There's no
6309 * reason to adjust the rightmost edge then.
6314 rec
->e_int_clusters
= cpu_to_le32(new_edge
);
6315 le32_add_cpu(&rec
->e_int_clusters
,
6316 -le32_to_cpu(rec
->e_cpos
));
6319 * A deleted child record should have been
6322 BUG_ON(le32_to_cpu(rec
->e_int_clusters
) == 0);
6326 ret
= ocfs2_journal_dirty(handle
, bh
);
6332 mlog(0, "extent list container %llu, after: record %d: "
6333 "(%u, %u, %llu), next = %u.\n",
6334 (unsigned long long)bh
->b_blocknr
, i
,
6335 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
),
6336 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6337 le16_to_cpu(el
->l_next_free_rec
));
6340 * We must be careful to only attempt delete of an
6341 * extent block (and not the root inode block).
6343 if (index
> 0 && le16_to_cpu(el
->l_next_free_rec
) == 0) {
6344 struct ocfs2_extent_block
*eb
=
6345 (struct ocfs2_extent_block
*)bh
->b_data
;
6348 * Save this for use when processing the
6351 deleted_eb
= le64_to_cpu(eb
->h_blkno
);
6353 mlog(0, "deleting this extent block.\n");
6355 ocfs2_remove_from_cache(inode
, bh
);
6357 BUG_ON(ocfs2_rec_clusters(el
, &el
->l_recs
[0]));
6358 BUG_ON(le32_to_cpu(el
->l_recs
[0].e_cpos
));
6359 BUG_ON(le64_to_cpu(el
->l_recs
[0].e_blkno
));
6361 ret
= ocfs2_cache_extent_block_free(&tc
->tc_dealloc
, eb
);
6362 /* An error here is not fatal. */
6377 static int ocfs2_do_truncate(struct ocfs2_super
*osb
,
6378 unsigned int clusters_to_del
,
6379 struct inode
*inode
,
6380 struct buffer_head
*fe_bh
,
6382 struct ocfs2_truncate_context
*tc
,
6383 struct ocfs2_path
*path
)
6386 struct ocfs2_dinode
*fe
;
6387 struct ocfs2_extent_block
*last_eb
= NULL
;
6388 struct ocfs2_extent_list
*el
;
6389 struct buffer_head
*last_eb_bh
= NULL
;
6392 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
6394 status
= ocfs2_find_new_last_ext_blk(inode
, clusters_to_del
,
6402 * Each component will be touched, so we might as well journal
6403 * here to avoid having to handle errors later.
6405 status
= ocfs2_journal_access_path(inode
, handle
, path
);
6412 status
= ocfs2_journal_access(handle
, inode
, last_eb_bh
,
6413 OCFS2_JOURNAL_ACCESS_WRITE
);
6419 last_eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
6422 el
= &(fe
->id2
.i_list
);
6425 * Lower levels depend on this never happening, but it's best
6426 * to check it up here before changing the tree.
6428 if (el
->l_tree_depth
&& el
->l_recs
[0].e_int_clusters
== 0) {
6429 ocfs2_error(inode
->i_sb
,
6430 "Inode %lu has an empty extent record, depth %u\n",
6431 inode
->i_ino
, le16_to_cpu(el
->l_tree_depth
));
6436 spin_lock(&OCFS2_I(inode
)->ip_lock
);
6437 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
) -
6439 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
6440 le32_add_cpu(&fe
->i_clusters
, -clusters_to_del
);
6441 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6443 status
= ocfs2_trim_tree(inode
, path
, handle
, tc
,
6444 clusters_to_del
, &delete_blk
);
6450 if (le32_to_cpu(fe
->i_clusters
) == 0) {
6451 /* trunc to zero is a special case. */
6452 el
->l_tree_depth
= 0;
6453 fe
->i_last_eb_blk
= 0;
6455 fe
->i_last_eb_blk
= last_eb
->h_blkno
;
6457 status
= ocfs2_journal_dirty(handle
, fe_bh
);
6464 /* If there will be a new last extent block, then by
6465 * definition, there cannot be any leaves to the right of
6467 last_eb
->h_next_leaf_blk
= 0;
6468 status
= ocfs2_journal_dirty(handle
, last_eb_bh
);
6476 status
= ocfs2_truncate_log_append(osb
, handle
, delete_blk
,
6490 static int ocfs2_writeback_zero_func(handle_t
*handle
, struct buffer_head
*bh
)
6492 set_buffer_uptodate(bh
);
6493 mark_buffer_dirty(bh
);
6497 static int ocfs2_ordered_zero_func(handle_t
*handle
, struct buffer_head
*bh
)
6499 set_buffer_uptodate(bh
);
6500 mark_buffer_dirty(bh
);
6501 return ocfs2_journal_dirty_data(handle
, bh
);
6504 static void ocfs2_map_and_dirty_page(struct inode
*inode
, handle_t
*handle
,
6505 unsigned int from
, unsigned int to
,
6506 struct page
*page
, int zero
, u64
*phys
)
6508 int ret
, partial
= 0;
6510 ret
= ocfs2_map_page_blocks(page
, phys
, inode
, from
, to
, 0);
6515 zero_user_segment(page
, from
, to
);
6518 * Need to set the buffers we zero'd into uptodate
6519 * here if they aren't - ocfs2_map_page_blocks()
6520 * might've skipped some
6522 if (ocfs2_should_order_data(inode
)) {
6523 ret
= walk_page_buffers(handle
,
6526 ocfs2_ordered_zero_func
);
6530 ret
= walk_page_buffers(handle
, page_buffers(page
),
6532 ocfs2_writeback_zero_func
);
6538 SetPageUptodate(page
);
6540 flush_dcache_page(page
);
6543 static void ocfs2_zero_cluster_pages(struct inode
*inode
, loff_t start
,
6544 loff_t end
, struct page
**pages
,
6545 int numpages
, u64 phys
, handle_t
*handle
)
6549 unsigned int from
, to
= PAGE_CACHE_SIZE
;
6550 struct super_block
*sb
= inode
->i_sb
;
6552 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb
)));
6557 to
= PAGE_CACHE_SIZE
;
6558 for(i
= 0; i
< numpages
; i
++) {
6561 from
= start
& (PAGE_CACHE_SIZE
- 1);
6562 if ((end
>> PAGE_CACHE_SHIFT
) == page
->index
)
6563 to
= end
& (PAGE_CACHE_SIZE
- 1);
6565 BUG_ON(from
> PAGE_CACHE_SIZE
);
6566 BUG_ON(to
> PAGE_CACHE_SIZE
);
6568 ocfs2_map_and_dirty_page(inode
, handle
, from
, to
, page
, 1,
6571 start
= (page
->index
+ 1) << PAGE_CACHE_SHIFT
;
6575 ocfs2_unlock_and_free_pages(pages
, numpages
);
6578 static int ocfs2_grab_eof_pages(struct inode
*inode
, loff_t start
, loff_t end
,
6579 struct page
**pages
, int *num
)
6581 int numpages
, ret
= 0;
6582 struct super_block
*sb
= inode
->i_sb
;
6583 struct address_space
*mapping
= inode
->i_mapping
;
6584 unsigned long index
;
6585 loff_t last_page_bytes
;
6587 BUG_ON(start
> end
);
6589 BUG_ON(start
>> OCFS2_SB(sb
)->s_clustersize_bits
!=
6590 (end
- 1) >> OCFS2_SB(sb
)->s_clustersize_bits
);
6593 last_page_bytes
= PAGE_ALIGN(end
);
6594 index
= start
>> PAGE_CACHE_SHIFT
;
6596 pages
[numpages
] = grab_cache_page(mapping
, index
);
6597 if (!pages
[numpages
]) {
6605 } while (index
< (last_page_bytes
>> PAGE_CACHE_SHIFT
));
6610 ocfs2_unlock_and_free_pages(pages
, numpages
);
6620 * Zero the area past i_size but still within an allocated
6621 * cluster. This avoids exposing nonzero data on subsequent file
6624 * We need to call this before i_size is updated on the inode because
6625 * otherwise block_write_full_page() will skip writeout of pages past
6626 * i_size. The new_i_size parameter is passed for this reason.
6628 int ocfs2_zero_range_for_truncate(struct inode
*inode
, handle_t
*handle
,
6629 u64 range_start
, u64 range_end
)
6631 int ret
= 0, numpages
;
6632 struct page
**pages
= NULL
;
6634 unsigned int ext_flags
;
6635 struct super_block
*sb
= inode
->i_sb
;
6638 * File systems which don't support sparse files zero on every
6641 if (!ocfs2_sparse_alloc(OCFS2_SB(sb
)))
6644 pages
= kcalloc(ocfs2_pages_per_cluster(sb
),
6645 sizeof(struct page
*), GFP_NOFS
);
6646 if (pages
== NULL
) {
6652 if (range_start
== range_end
)
6655 ret
= ocfs2_extent_map_get_blocks(inode
,
6656 range_start
>> sb
->s_blocksize_bits
,
6657 &phys
, NULL
, &ext_flags
);
6664 * Tail is a hole, or is marked unwritten. In either case, we
6665 * can count on read and write to return/push zero's.
6667 if (phys
== 0 || ext_flags
& OCFS2_EXT_UNWRITTEN
)
6670 ret
= ocfs2_grab_eof_pages(inode
, range_start
, range_end
, pages
,
6677 ocfs2_zero_cluster_pages(inode
, range_start
, range_end
, pages
,
6678 numpages
, phys
, handle
);
6681 * Initiate writeout of the pages we zero'd here. We don't
6682 * wait on them - the truncate_inode_pages() call later will
6685 ret
= do_sync_mapping_range(inode
->i_mapping
, range_start
,
6686 range_end
- 1, SYNC_FILE_RANGE_WRITE
);
6697 static void ocfs2_zero_dinode_id2_with_xattr(struct inode
*inode
,
6698 struct ocfs2_dinode
*di
)
6700 unsigned int blocksize
= 1 << inode
->i_sb
->s_blocksize_bits
;
6701 unsigned int xattrsize
= le16_to_cpu(di
->i_xattr_inline_size
);
6703 if (le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_XATTR_FL
)
6704 memset(&di
->id2
, 0, blocksize
-
6705 offsetof(struct ocfs2_dinode
, id2
) -
6708 memset(&di
->id2
, 0, blocksize
-
6709 offsetof(struct ocfs2_dinode
, id2
));
6712 void ocfs2_dinode_new_extent_list(struct inode
*inode
,
6713 struct ocfs2_dinode
*di
)
6715 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
6716 di
->id2
.i_list
.l_tree_depth
= 0;
6717 di
->id2
.i_list
.l_next_free_rec
= 0;
6718 di
->id2
.i_list
.l_count
= cpu_to_le16(
6719 ocfs2_extent_recs_per_inode_with_xattr(inode
->i_sb
, di
));
6722 void ocfs2_set_inode_data_inline(struct inode
*inode
, struct ocfs2_dinode
*di
)
6724 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6725 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
6727 spin_lock(&oi
->ip_lock
);
6728 oi
->ip_dyn_features
|= OCFS2_INLINE_DATA_FL
;
6729 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
6730 spin_unlock(&oi
->ip_lock
);
6733 * We clear the entire i_data structure here so that all
6734 * fields can be properly initialized.
6736 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
6738 idata
->id_count
= cpu_to_le16(
6739 ocfs2_max_inline_data_with_xattr(inode
->i_sb
, di
));
6742 int ocfs2_convert_inline_data_to_extents(struct inode
*inode
,
6743 struct buffer_head
*di_bh
)
6745 int ret
, i
, has_data
, num_pages
= 0;
6747 u64
uninitialized_var(block
);
6748 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6749 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6750 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
6751 struct ocfs2_alloc_context
*data_ac
= NULL
;
6752 struct page
**pages
= NULL
;
6753 loff_t end
= osb
->s_clustersize
;
6755 has_data
= i_size_read(inode
) ? 1 : 0;
6758 pages
= kcalloc(ocfs2_pages_per_cluster(osb
->sb
),
6759 sizeof(struct page
*), GFP_NOFS
);
6760 if (pages
== NULL
) {
6766 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
6773 handle
= ocfs2_start_trans(osb
, OCFS2_INLINE_TO_EXTENTS_CREDITS
);
6774 if (IS_ERR(handle
)) {
6775 ret
= PTR_ERR(handle
);
6780 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
6781 OCFS2_JOURNAL_ACCESS_WRITE
);
6789 unsigned int page_end
;
6792 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
,
6800 * Save two copies, one for insert, and one that can
6801 * be changed by ocfs2_map_and_dirty_page() below.
6803 block
= phys
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
6806 * Non sparse file systems zero on extend, so no need
6809 if (!ocfs2_sparse_alloc(osb
) &&
6810 PAGE_CACHE_SIZE
< osb
->s_clustersize
)
6811 end
= PAGE_CACHE_SIZE
;
6813 ret
= ocfs2_grab_eof_pages(inode
, 0, end
, pages
, &num_pages
);
6820 * This should populate the 1st page for us and mark
6823 ret
= ocfs2_read_inline_data(inode
, pages
[0], di_bh
);
6829 page_end
= PAGE_CACHE_SIZE
;
6830 if (PAGE_CACHE_SIZE
> osb
->s_clustersize
)
6831 page_end
= osb
->s_clustersize
;
6833 for (i
= 0; i
< num_pages
; i
++)
6834 ocfs2_map_and_dirty_page(inode
, handle
, 0, page_end
,
6835 pages
[i
], i
> 0, &phys
);
6838 spin_lock(&oi
->ip_lock
);
6839 oi
->ip_dyn_features
&= ~OCFS2_INLINE_DATA_FL
;
6840 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
6841 spin_unlock(&oi
->ip_lock
);
6843 ocfs2_dinode_new_extent_list(inode
, di
);
6845 ocfs2_journal_dirty(handle
, di_bh
);
6849 * An error at this point should be extremely rare. If
6850 * this proves to be false, we could always re-build
6851 * the in-inode data from our pages.
6853 ret
= ocfs2_dinode_insert_extent(osb
, handle
, inode
, di_bh
,
6854 0, block
, 1, 0, NULL
);
6860 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6864 ocfs2_commit_trans(osb
, handle
);
6868 ocfs2_free_alloc_context(data_ac
);
6872 ocfs2_unlock_and_free_pages(pages
, num_pages
);
6880 * It is expected, that by the time you call this function,
6881 * inode->i_size and fe->i_size have been adjusted.
6883 * WARNING: This will kfree the truncate context
6885 int ocfs2_commit_truncate(struct ocfs2_super
*osb
,
6886 struct inode
*inode
,
6887 struct buffer_head
*fe_bh
,
6888 struct ocfs2_truncate_context
*tc
)
6890 int status
, i
, credits
, tl_sem
= 0;
6891 u32 clusters_to_del
, new_highest_cpos
, range
;
6892 struct ocfs2_extent_list
*el
;
6893 handle_t
*handle
= NULL
;
6894 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6895 struct ocfs2_path
*path
= NULL
;
6896 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
6900 new_highest_cpos
= ocfs2_clusters_for_bytes(osb
->sb
,
6901 i_size_read(inode
));
6903 path
= ocfs2_new_path(fe_bh
, &di
->id2
.i_list
);
6910 ocfs2_extent_map_trunc(inode
, new_highest_cpos
);
6914 * Check that we still have allocation to delete.
6916 if (OCFS2_I(inode
)->ip_clusters
== 0) {
6922 * Truncate always works against the rightmost tree branch.
6924 status
= ocfs2_find_path(inode
, path
, UINT_MAX
);
6930 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6931 OCFS2_I(inode
)->ip_clusters
, path
->p_tree_depth
);
6934 * By now, el will point to the extent list on the bottom most
6935 * portion of this tree. Only the tail record is considered in
6938 * We handle the following cases, in order:
6939 * - empty extent: delete the remaining branch
6940 * - remove the entire record
6941 * - remove a partial record
6942 * - no record needs to be removed (truncate has completed)
6944 el
= path_leaf_el(path
);
6945 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
6946 ocfs2_error(inode
->i_sb
,
6947 "Inode %llu has empty extent block at %llu\n",
6948 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
6949 (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
6954 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
6955 range
= le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
6956 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
6957 if (i
== 0 && ocfs2_is_empty_extent(&el
->l_recs
[i
])) {
6958 clusters_to_del
= 0;
6959 } else if (le32_to_cpu(el
->l_recs
[i
].e_cpos
) >= new_highest_cpos
) {
6960 clusters_to_del
= ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
6961 } else if (range
> new_highest_cpos
) {
6962 clusters_to_del
= (ocfs2_rec_clusters(el
, &el
->l_recs
[i
]) +
6963 le32_to_cpu(el
->l_recs
[i
].e_cpos
)) -
6970 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6971 clusters_to_del
, (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
6973 mutex_lock(&tl_inode
->i_mutex
);
6975 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6976 * record is free for use. If there isn't any, we flush to get
6977 * an empty truncate log. */
6978 if (ocfs2_truncate_log_needs_flush(osb
)) {
6979 status
= __ocfs2_flush_truncate_log(osb
);
6986 credits
= ocfs2_calc_tree_trunc_credits(osb
->sb
, clusters_to_del
,
6987 (struct ocfs2_dinode
*)fe_bh
->b_data
,
6989 handle
= ocfs2_start_trans(osb
, credits
);
6990 if (IS_ERR(handle
)) {
6991 status
= PTR_ERR(handle
);
6997 status
= ocfs2_do_truncate(osb
, clusters_to_del
, inode
, fe_bh
, handle
,
7004 mutex_unlock(&tl_inode
->i_mutex
);
7007 ocfs2_commit_trans(osb
, handle
);
7010 ocfs2_reinit_path(path
, 1);
7013 * The check above will catch the case where we've truncated
7014 * away all allocation.
7020 ocfs2_schedule_truncate_log_flush(osb
, 1);
7023 mutex_unlock(&tl_inode
->i_mutex
);
7026 ocfs2_commit_trans(osb
, handle
);
7028 ocfs2_run_deallocs(osb
, &tc
->tc_dealloc
);
7030 ocfs2_free_path(path
);
7032 /* This will drop the ext_alloc cluster lock for us */
7033 ocfs2_free_truncate_context(tc
);
7040 * Expects the inode to already be locked.
7042 int ocfs2_prepare_truncate(struct ocfs2_super
*osb
,
7043 struct inode
*inode
,
7044 struct buffer_head
*fe_bh
,
7045 struct ocfs2_truncate_context
**tc
)
7048 unsigned int new_i_clusters
;
7049 struct ocfs2_dinode
*fe
;
7050 struct ocfs2_extent_block
*eb
;
7051 struct buffer_head
*last_eb_bh
= NULL
;
7057 new_i_clusters
= ocfs2_clusters_for_bytes(osb
->sb
,
7058 i_size_read(inode
));
7059 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
7061 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7062 "%llu\n", le32_to_cpu(fe
->i_clusters
), new_i_clusters
,
7063 (unsigned long long)le64_to_cpu(fe
->i_size
));
7065 *tc
= kzalloc(sizeof(struct ocfs2_truncate_context
), GFP_KERNEL
);
7071 ocfs2_init_dealloc_ctxt(&(*tc
)->tc_dealloc
);
7073 if (fe
->id2
.i_list
.l_tree_depth
) {
7074 status
= ocfs2_read_block(osb
, le64_to_cpu(fe
->i_last_eb_blk
),
7075 &last_eb_bh
, OCFS2_BH_CACHED
, inode
);
7080 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
7081 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
7082 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
7090 (*tc
)->tc_last_eb_bh
= last_eb_bh
;
7096 ocfs2_free_truncate_context(*tc
);
7104 * 'start' is inclusive, 'end' is not.
7106 int ocfs2_truncate_inline(struct inode
*inode
, struct buffer_head
*di_bh
,
7107 unsigned int start
, unsigned int end
, int trunc
)
7110 unsigned int numbytes
;
7112 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7113 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
7114 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
7116 if (end
> i_size_read(inode
))
7117 end
= i_size_read(inode
);
7119 BUG_ON(start
>= end
);
7121 if (!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) ||
7122 !(le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_DATA_FL
) ||
7123 !ocfs2_supports_inline_data(osb
)) {
7124 ocfs2_error(inode
->i_sb
,
7125 "Inline data flags for inode %llu don't agree! "
7126 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7127 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7128 le16_to_cpu(di
->i_dyn_features
),
7129 OCFS2_I(inode
)->ip_dyn_features
,
7130 osb
->s_feature_incompat
);
7135 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
7136 if (IS_ERR(handle
)) {
7137 ret
= PTR_ERR(handle
);
7142 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
7143 OCFS2_JOURNAL_ACCESS_WRITE
);
7149 numbytes
= end
- start
;
7150 memset(idata
->id_data
+ start
, 0, numbytes
);
7153 * No need to worry about the data page here - it's been
7154 * truncated already and inline data doesn't need it for
7155 * pushing zero's to disk, so we'll let readpage pick it up
7159 i_size_write(inode
, start
);
7160 di
->i_size
= cpu_to_le64(start
);
7163 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7164 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
7166 di
->i_ctime
= di
->i_mtime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
7167 di
->i_ctime_nsec
= di
->i_mtime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
7169 ocfs2_journal_dirty(handle
, di_bh
);
7172 ocfs2_commit_trans(osb
, handle
);
7178 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
)
7181 * The caller is responsible for completing deallocation
7182 * before freeing the context.
7184 if (tc
->tc_dealloc
.c_first_suballocator
!= NULL
)
7186 "Truncate completion has non-empty dealloc context\n");
7188 if (tc
->tc_last_eb_bh
)
7189 brelse(tc
->tc_last_eb_bh
);