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);
76 /* These are internal to ocfs2_extent_tree and don't have
77 * accessor functions */
78 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
79 void (*eo_fill_max_leaf_clusters)(struct inode *inode,
80 struct ocfs2_extent_tree *et);
83 struct ocfs2_extent_tree {
84 enum ocfs2_extent_tree_type et_type;
85 struct ocfs2_extent_tree_operations *et_ops;
86 struct buffer_head *et_root_bh;
87 struct ocfs2_extent_list *et_root_el;
89 unsigned int et_max_leaf_clusters;
92 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
94 struct ocfs2_dinode *di = et->et_object;
96 et->et_root_el = &di->id2.i_list;
99 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
102 struct ocfs2_dinode *di = et->et_object;
104 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
105 di->i_last_eb_blk = cpu_to_le64(blkno);
108 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
110 struct ocfs2_dinode *di = et->et_object;
112 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
113 return le64_to_cpu(di->i_last_eb_blk);
116 static void ocfs2_dinode_update_clusters(struct inode *inode,
117 struct ocfs2_extent_tree *et,
120 struct ocfs2_dinode *di = et->et_object;
122 le32_add_cpu(&di->i_clusters, clusters);
123 spin_lock(&OCFS2_I(inode)->ip_lock);
124 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
125 spin_unlock(&OCFS2_I(inode)->ip_lock);
128 static int ocfs2_dinode_sanity_check(struct inode *inode,
129 struct ocfs2_extent_tree *et)
132 struct ocfs2_dinode *di;
134 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
137 if (!OCFS2_IS_VALID_DINODE(di)) {
139 ocfs2_error(inode->i_sb,
140 "Inode %llu has invalid path root",
141 (unsigned long long)OCFS2_I(inode)->ip_blkno);
147 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
148 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
149 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
150 .eo_update_clusters = ocfs2_dinode_update_clusters,
151 .eo_sanity_check = ocfs2_dinode_sanity_check,
152 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
155 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
157 struct ocfs2_xattr_value_root *xv = et->et_object;
159 et->et_root_el = &xv->xr_list;
162 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
165 struct ocfs2_xattr_value_root *xv =
166 (struct ocfs2_xattr_value_root *)et->et_object;
168 xv->xr_last_eb_blk = cpu_to_le64(blkno);
171 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
173 struct ocfs2_xattr_value_root *xv =
174 (struct ocfs2_xattr_value_root *) et->et_object;
176 return le64_to_cpu(xv->xr_last_eb_blk);
179 static void ocfs2_xattr_value_update_clusters(struct inode *inode,
180 struct ocfs2_extent_tree *et,
183 struct ocfs2_xattr_value_root *xv =
184 (struct ocfs2_xattr_value_root *)et->et_object;
186 le32_add_cpu(&xv->xr_clusters, clusters);
189 static int ocfs2_xattr_value_sanity_check(struct inode *inode,
190 struct ocfs2_extent_tree *et)
195 static struct ocfs2_extent_tree_operations ocfs2_xattr_et_ops = {
196 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
197 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
198 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
199 .eo_sanity_check = ocfs2_xattr_value_sanity_check,
200 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
203 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
205 struct ocfs2_xattr_block *xb = et->et_object;
207 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
210 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
211 struct ocfs2_extent_tree *et)
213 et->et_max_leaf_clusters =
214 ocfs2_clusters_for_bytes(inode->i_sb,
215 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
218 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
221 struct ocfs2_xattr_block *xb = et->et_object;
222 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
224 xt->xt_last_eb_blk = cpu_to_le64(blkno);
227 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
229 struct ocfs2_xattr_block *xb = et->et_object;
230 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
232 return le64_to_cpu(xt->xt_last_eb_blk);
235 static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
236 struct ocfs2_extent_tree *et,
239 struct ocfs2_xattr_block *xb = et->et_object;
241 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
244 static int ocfs2_xattr_tree_sanity_check(struct inode *inode,
245 struct ocfs2_extent_tree *et)
250 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
251 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
252 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
253 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
254 .eo_sanity_check = ocfs2_xattr_tree_sanity_check,
255 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
256 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
259 static void ocfs2_get_extent_tree(struct ocfs2_extent_tree *et,
261 struct buffer_head *bh,
262 enum ocfs2_extent_tree_type et_type,
265 et->et_type = et_type;
269 obj = (void *)bh->b_data;
272 if (et_type == OCFS2_DINODE_EXTENT) {
273 et->et_ops = &ocfs2_dinode_et_ops;
274 } else if (et_type == OCFS2_XATTR_VALUE_EXTENT) {
275 et->et_ops = &ocfs2_xattr_et_ops;
276 } else if (et_type == OCFS2_XATTR_TREE_EXTENT) {
277 et->et_ops = &ocfs2_xattr_tree_et_ops;
280 et->et_ops->eo_fill_root_el(et);
281 if (!et->et_ops->eo_fill_max_leaf_clusters)
282 et->et_max_leaf_clusters = 0;
284 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
287 static void ocfs2_put_extent_tree(struct ocfs2_extent_tree *et)
289 brelse(et->et_root_bh);
292 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
295 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
298 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
300 return et->et_ops->eo_get_last_eb_blk(et);
303 static inline void ocfs2_et_update_clusters(struct inode *inode,
304 struct ocfs2_extent_tree *et,
307 et->et_ops->eo_update_clusters(inode, et, clusters);
310 static inline int ocfs2_et_sanity_check(struct inode *inode,
311 struct ocfs2_extent_tree *et)
313 return et->et_ops->eo_sanity_check(inode, et);
316 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
317 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
318 struct ocfs2_extent_block *eb);
321 * Structures which describe a path through a btree, and functions to
324 * The idea here is to be as generic as possible with the tree
327 struct ocfs2_path_item {
328 struct buffer_head *bh;
329 struct ocfs2_extent_list *el;
332 #define OCFS2_MAX_PATH_DEPTH 5
336 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
339 #define path_root_bh(_path) ((_path)->p_node[0].bh)
340 #define path_root_el(_path) ((_path)->p_node[0].el)
341 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
342 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
343 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
346 * Reset the actual path elements so that we can re-use the structure
347 * to build another path. Generally, this involves freeing the buffer
350 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
352 int i, start = 0, depth = 0;
353 struct ocfs2_path_item *node;
358 for(i = start; i < path_num_items(path); i++) {
359 node = &path->p_node[i];
367 * Tree depth may change during truncate, or insert. If we're
368 * keeping the root extent list, then make sure that our path
369 * structure reflects the proper depth.
372 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
374 path->p_tree_depth = depth;
377 static void ocfs2_free_path(struct ocfs2_path *path)
380 ocfs2_reinit_path(path, 0);
386 * All the elements of src into dest. After this call, src could be freed
387 * without affecting dest.
389 * Both paths should have the same root. Any non-root elements of dest
392 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
396 BUG_ON(path_root_bh(dest) != path_root_bh(src));
397 BUG_ON(path_root_el(dest) != path_root_el(src));
399 ocfs2_reinit_path(dest, 1);
401 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
402 dest->p_node[i].bh = src->p_node[i].bh;
403 dest->p_node[i].el = src->p_node[i].el;
405 if (dest->p_node[i].bh)
406 get_bh(dest->p_node[i].bh);
411 * Make the *dest path the same as src and re-initialize src path to
414 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
418 BUG_ON(path_root_bh(dest) != path_root_bh(src));
420 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
421 brelse(dest->p_node[i].bh);
423 dest->p_node[i].bh = src->p_node[i].bh;
424 dest->p_node[i].el = src->p_node[i].el;
426 src->p_node[i].bh = NULL;
427 src->p_node[i].el = NULL;
432 * Insert an extent block at given index.
434 * This will not take an additional reference on eb_bh.
436 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
437 struct buffer_head *eb_bh)
439 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
442 * Right now, no root bh is an extent block, so this helps
443 * catch code errors with dinode trees. The assertion can be
444 * safely removed if we ever need to insert extent block
445 * structures at the root.
449 path->p_node[index].bh = eb_bh;
450 path->p_node[index].el = &eb->h_list;
453 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
454 struct ocfs2_extent_list *root_el)
456 struct ocfs2_path *path;
458 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
460 path = kzalloc(sizeof(*path), GFP_NOFS);
462 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
464 path_root_bh(path) = root_bh;
465 path_root_el(path) = root_el;
472 * Convenience function to journal all components in a path.
474 static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
475 struct ocfs2_path *path)
482 for(i = 0; i < path_num_items(path); i++) {
483 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
484 OCFS2_JOURNAL_ACCESS_WRITE);
496 * Return the index of the extent record which contains cluster #v_cluster.
497 * -1 is returned if it was not found.
499 * Should work fine on interior and exterior nodes.
501 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
505 struct ocfs2_extent_rec *rec;
506 u32 rec_end, rec_start, clusters;
508 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
509 rec = &el->l_recs[i];
511 rec_start = le32_to_cpu(rec->e_cpos);
512 clusters = ocfs2_rec_clusters(el, rec);
514 rec_end = rec_start + clusters;
516 if (v_cluster >= rec_start && v_cluster < rec_end) {
525 enum ocfs2_contig_type {
534 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
535 * ocfs2_extent_contig only work properly against leaf nodes!
537 static int ocfs2_block_extent_contig(struct super_block *sb,
538 struct ocfs2_extent_rec *ext,
541 u64 blk_end = le64_to_cpu(ext->e_blkno);
543 blk_end += ocfs2_clusters_to_blocks(sb,
544 le16_to_cpu(ext->e_leaf_clusters));
546 return blkno == blk_end;
549 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
550 struct ocfs2_extent_rec *right)
554 left_range = le32_to_cpu(left->e_cpos) +
555 le16_to_cpu(left->e_leaf_clusters);
557 return (left_range == le32_to_cpu(right->e_cpos));
560 static enum ocfs2_contig_type
561 ocfs2_extent_contig(struct inode *inode,
562 struct ocfs2_extent_rec *ext,
563 struct ocfs2_extent_rec *insert_rec)
565 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
568 * Refuse to coalesce extent records with different flag
569 * fields - we don't want to mix unwritten extents with user
572 if (ext->e_flags != insert_rec->e_flags)
575 if (ocfs2_extents_adjacent(ext, insert_rec) &&
576 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
579 blkno = le64_to_cpu(ext->e_blkno);
580 if (ocfs2_extents_adjacent(insert_rec, ext) &&
581 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
588 * NOTE: We can have pretty much any combination of contiguousness and
591 * The usefulness of APPEND_TAIL is more in that it lets us know that
592 * we'll have to update the path to that leaf.
594 enum ocfs2_append_type {
599 enum ocfs2_split_type {
605 struct ocfs2_insert_type {
606 enum ocfs2_split_type ins_split;
607 enum ocfs2_append_type ins_appending;
608 enum ocfs2_contig_type ins_contig;
609 int ins_contig_index;
613 struct ocfs2_merge_ctxt {
614 enum ocfs2_contig_type c_contig_type;
615 int c_has_empty_extent;
616 int c_split_covers_rec;
620 * How many free extents have we got before we need more meta data?
622 int ocfs2_num_free_extents(struct ocfs2_super *osb,
624 struct buffer_head *root_bh,
625 enum ocfs2_extent_tree_type type,
629 struct ocfs2_extent_list *el = NULL;
630 struct ocfs2_extent_block *eb;
631 struct buffer_head *eb_bh = NULL;
633 struct ocfs2_extent_tree et;
637 ocfs2_get_extent_tree(&et, inode, root_bh, type, obj);
639 last_eb_blk = ocfs2_et_get_last_eb_blk(&et);
642 retval = ocfs2_read_block(osb, last_eb_blk,
643 &eb_bh, OCFS2_BH_CACHED, inode);
648 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
652 BUG_ON(el->l_tree_depth != 0);
654 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
659 ocfs2_put_extent_tree(&et);
664 /* expects array to already be allocated
666 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
669 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
673 struct ocfs2_alloc_context *meta_ac,
674 struct buffer_head *bhs[])
676 int count, status, i;
677 u16 suballoc_bit_start;
680 struct ocfs2_extent_block *eb;
685 while (count < wanted) {
686 status = ocfs2_claim_metadata(osb,
698 for(i = count; i < (num_got + count); i++) {
699 bhs[i] = sb_getblk(osb->sb, first_blkno);
700 if (bhs[i] == NULL) {
705 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
707 status = ocfs2_journal_access(handle, inode, bhs[i],
708 OCFS2_JOURNAL_ACCESS_CREATE);
714 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
715 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
716 /* Ok, setup the minimal stuff here. */
717 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
718 eb->h_blkno = cpu_to_le64(first_blkno);
719 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
720 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
721 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
723 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
725 suballoc_bit_start++;
728 /* We'll also be dirtied by the caller, so
729 * this isn't absolutely necessary. */
730 status = ocfs2_journal_dirty(handle, bhs[i]);
743 for(i = 0; i < wanted; i++) {
754 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
756 * Returns the sum of the rightmost extent rec logical offset and
759 * ocfs2_add_branch() uses this to determine what logical cluster
760 * value should be populated into the leftmost new branch records.
762 * ocfs2_shift_tree_depth() uses this to determine the # clusters
763 * value for the new topmost tree record.
765 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
769 i = le16_to_cpu(el->l_next_free_rec) - 1;
771 return le32_to_cpu(el->l_recs[i].e_cpos) +
772 ocfs2_rec_clusters(el, &el->l_recs[i]);
776 * Add an entire tree branch to our inode. eb_bh is the extent block
777 * to start at, if we don't want to start the branch at the dinode
780 * last_eb_bh is required as we have to update it's next_leaf pointer
781 * for the new last extent block.
783 * the new branch will be 'empty' in the sense that every block will
784 * contain a single record with cluster count == 0.
786 static int ocfs2_add_branch(struct ocfs2_super *osb,
789 struct ocfs2_extent_tree *et,
790 struct buffer_head *eb_bh,
791 struct buffer_head **last_eb_bh,
792 struct ocfs2_alloc_context *meta_ac)
794 int status, new_blocks, i;
795 u64 next_blkno, new_last_eb_blk;
796 struct buffer_head *bh;
797 struct buffer_head **new_eb_bhs = NULL;
798 struct ocfs2_extent_block *eb;
799 struct ocfs2_extent_list *eb_el;
800 struct ocfs2_extent_list *el;
805 BUG_ON(!last_eb_bh || !*last_eb_bh);
808 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
813 /* we never add a branch to a leaf. */
814 BUG_ON(!el->l_tree_depth);
816 new_blocks = le16_to_cpu(el->l_tree_depth);
818 /* allocate the number of new eb blocks we need */
819 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
827 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
828 meta_ac, new_eb_bhs);
834 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
835 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
837 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
838 * linked with the rest of the tree.
839 * conversly, new_eb_bhs[0] is the new bottommost leaf.
841 * when we leave the loop, new_last_eb_blk will point to the
842 * newest leaf, and next_blkno will point to the topmost extent
844 next_blkno = new_last_eb_blk = 0;
845 for(i = 0; i < new_blocks; i++) {
847 eb = (struct ocfs2_extent_block *) bh->b_data;
848 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
849 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
855 status = ocfs2_journal_access(handle, inode, bh,
856 OCFS2_JOURNAL_ACCESS_CREATE);
862 eb->h_next_leaf_blk = 0;
863 eb_el->l_tree_depth = cpu_to_le16(i);
864 eb_el->l_next_free_rec = cpu_to_le16(1);
866 * This actually counts as an empty extent as
869 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
870 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
872 * eb_el isn't always an interior node, but even leaf
873 * nodes want a zero'd flags and reserved field so
874 * this gets the whole 32 bits regardless of use.
876 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
877 if (!eb_el->l_tree_depth)
878 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
880 status = ocfs2_journal_dirty(handle, bh);
886 next_blkno = le64_to_cpu(eb->h_blkno);
889 /* This is a bit hairy. We want to update up to three blocks
890 * here without leaving any of them in an inconsistent state
891 * in case of error. We don't have to worry about
892 * journal_dirty erroring as it won't unless we've aborted the
893 * handle (in which case we would never be here) so reserving
894 * the write with journal_access is all we need to do. */
895 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
896 OCFS2_JOURNAL_ACCESS_WRITE);
901 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
902 OCFS2_JOURNAL_ACCESS_WRITE);
908 status = ocfs2_journal_access(handle, inode, eb_bh,
909 OCFS2_JOURNAL_ACCESS_WRITE);
916 /* Link the new branch into the rest of the tree (el will
917 * either be on the root_bh, or the extent block passed in. */
918 i = le16_to_cpu(el->l_next_free_rec);
919 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
920 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
921 el->l_recs[i].e_int_clusters = 0;
922 le16_add_cpu(&el->l_next_free_rec, 1);
924 /* fe needs a new last extent block pointer, as does the
925 * next_leaf on the previously last-extent-block. */
926 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
928 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
929 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
931 status = ocfs2_journal_dirty(handle, *last_eb_bh);
934 status = ocfs2_journal_dirty(handle, et->et_root_bh);
938 status = ocfs2_journal_dirty(handle, eb_bh);
944 * Some callers want to track the rightmost leaf so pass it
948 get_bh(new_eb_bhs[0]);
949 *last_eb_bh = new_eb_bhs[0];
954 for (i = 0; i < new_blocks; i++)
956 brelse(new_eb_bhs[i]);
965 * adds another level to the allocation tree.
966 * returns back the new extent block so you can add a branch to it
969 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
972 struct ocfs2_extent_tree *et,
973 struct ocfs2_alloc_context *meta_ac,
974 struct buffer_head **ret_new_eb_bh)
978 struct buffer_head *new_eb_bh = NULL;
979 struct ocfs2_extent_block *eb;
980 struct ocfs2_extent_list *root_el;
981 struct ocfs2_extent_list *eb_el;
985 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
992 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
993 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
994 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1000 root_el = et->et_root_el;
1002 status = ocfs2_journal_access(handle, inode, new_eb_bh,
1003 OCFS2_JOURNAL_ACCESS_CREATE);
1009 /* copy the root extent list data into the new extent block */
1010 eb_el->l_tree_depth = root_el->l_tree_depth;
1011 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1012 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1013 eb_el->l_recs[i] = root_el->l_recs[i];
1015 status = ocfs2_journal_dirty(handle, new_eb_bh);
1021 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
1022 OCFS2_JOURNAL_ACCESS_WRITE);
1028 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1030 /* update root_bh now */
1031 le16_add_cpu(&root_el->l_tree_depth, 1);
1032 root_el->l_recs[0].e_cpos = 0;
1033 root_el->l_recs[0].e_blkno = eb->h_blkno;
1034 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1035 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1036 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1037 root_el->l_next_free_rec = cpu_to_le16(1);
1039 /* If this is our 1st tree depth shift, then last_eb_blk
1040 * becomes the allocated extent block */
1041 if (root_el->l_tree_depth == cpu_to_le16(1))
1042 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1044 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1050 *ret_new_eb_bh = new_eb_bh;
1062 * Should only be called when there is no space left in any of the
1063 * leaf nodes. What we want to do is find the lowest tree depth
1064 * non-leaf extent block with room for new records. There are three
1065 * valid results of this search:
1067 * 1) a lowest extent block is found, then we pass it back in
1068 * *lowest_eb_bh and return '0'
1070 * 2) the search fails to find anything, but the root_el has room. We
1071 * pass NULL back in *lowest_eb_bh, but still return '0'
1073 * 3) the search fails to find anything AND the root_el is full, in
1074 * which case we return > 0
1076 * return status < 0 indicates an error.
1078 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1079 struct inode *inode,
1080 struct ocfs2_extent_tree *et,
1081 struct buffer_head **target_bh)
1085 struct ocfs2_extent_block *eb;
1086 struct ocfs2_extent_list *el;
1087 struct buffer_head *bh = NULL;
1088 struct buffer_head *lowest_bh = NULL;
1094 el = et->et_root_el;
1096 while(le16_to_cpu(el->l_tree_depth) > 1) {
1097 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1098 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
1099 "extent list (next_free_rec == 0)",
1100 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1104 i = le16_to_cpu(el->l_next_free_rec) - 1;
1105 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1107 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
1108 "list where extent # %d has no physical "
1110 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
1120 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
1127 eb = (struct ocfs2_extent_block *) bh->b_data;
1128 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1129 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1135 if (le16_to_cpu(el->l_next_free_rec) <
1136 le16_to_cpu(el->l_count)) {
1144 /* If we didn't find one and the fe doesn't have any room,
1145 * then return '1' */
1146 el = et->et_root_el;
1147 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1150 *target_bh = lowest_bh;
1160 * Grow a b-tree so that it has more records.
1162 * We might shift the tree depth in which case existing paths should
1163 * be considered invalid.
1165 * Tree depth after the grow is returned via *final_depth.
1167 * *last_eb_bh will be updated by ocfs2_add_branch().
1169 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
1170 struct ocfs2_extent_tree *et, int *final_depth,
1171 struct buffer_head **last_eb_bh,
1172 struct ocfs2_alloc_context *meta_ac)
1175 struct ocfs2_extent_list *el = et->et_root_el;
1176 int depth = le16_to_cpu(el->l_tree_depth);
1177 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1178 struct buffer_head *bh = NULL;
1180 BUG_ON(meta_ac == NULL);
1182 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
1189 /* We traveled all the way to the bottom of the allocation tree
1190 * and didn't find room for any more extents - we need to add
1191 * another tree level */
1194 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1196 /* ocfs2_shift_tree_depth will return us a buffer with
1197 * the new extent block (so we can pass that to
1198 * ocfs2_add_branch). */
1199 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
1208 * Special case: we have room now if we shifted from
1209 * tree_depth 0, so no more work needs to be done.
1211 * We won't be calling add_branch, so pass
1212 * back *last_eb_bh as the new leaf. At depth
1213 * zero, it should always be null so there's
1214 * no reason to brelse.
1216 BUG_ON(*last_eb_bh);
1223 /* call ocfs2_add_branch to add the final part of the tree with
1225 mlog(0, "add branch. bh = %p\n", bh);
1226 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
1235 *final_depth = depth;
1241 * This function will discard the rightmost extent record.
1243 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1245 int next_free = le16_to_cpu(el->l_next_free_rec);
1246 int count = le16_to_cpu(el->l_count);
1247 unsigned int num_bytes;
1250 /* This will cause us to go off the end of our extent list. */
1251 BUG_ON(next_free >= count);
1253 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1255 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1258 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1259 struct ocfs2_extent_rec *insert_rec)
1261 int i, insert_index, next_free, has_empty, num_bytes;
1262 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1263 struct ocfs2_extent_rec *rec;
1265 next_free = le16_to_cpu(el->l_next_free_rec);
1266 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1270 /* The tree code before us didn't allow enough room in the leaf. */
1271 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1274 * The easiest way to approach this is to just remove the
1275 * empty extent and temporarily decrement next_free.
1279 * If next_free was 1 (only an empty extent), this
1280 * loop won't execute, which is fine. We still want
1281 * the decrement above to happen.
1283 for(i = 0; i < (next_free - 1); i++)
1284 el->l_recs[i] = el->l_recs[i+1];
1290 * Figure out what the new record index should be.
1292 for(i = 0; i < next_free; i++) {
1293 rec = &el->l_recs[i];
1295 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1300 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1301 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1303 BUG_ON(insert_index < 0);
1304 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1305 BUG_ON(insert_index > next_free);
1308 * No need to memmove if we're just adding to the tail.
1310 if (insert_index != next_free) {
1311 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1313 num_bytes = next_free - insert_index;
1314 num_bytes *= sizeof(struct ocfs2_extent_rec);
1315 memmove(&el->l_recs[insert_index + 1],
1316 &el->l_recs[insert_index],
1321 * Either we had an empty extent, and need to re-increment or
1322 * there was no empty extent on a non full rightmost leaf node,
1323 * in which case we still need to increment.
1326 el->l_next_free_rec = cpu_to_le16(next_free);
1328 * Make sure none of the math above just messed up our tree.
1330 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1332 el->l_recs[insert_index] = *insert_rec;
1336 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1338 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1340 BUG_ON(num_recs == 0);
1342 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1344 size = num_recs * sizeof(struct ocfs2_extent_rec);
1345 memmove(&el->l_recs[0], &el->l_recs[1], size);
1346 memset(&el->l_recs[num_recs], 0,
1347 sizeof(struct ocfs2_extent_rec));
1348 el->l_next_free_rec = cpu_to_le16(num_recs);
1353 * Create an empty extent record .
1355 * l_next_free_rec may be updated.
1357 * If an empty extent already exists do nothing.
1359 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1361 int next_free = le16_to_cpu(el->l_next_free_rec);
1363 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1368 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1371 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1372 "Asked to create an empty extent in a full list:\n"
1373 "count = %u, tree depth = %u",
1374 le16_to_cpu(el->l_count),
1375 le16_to_cpu(el->l_tree_depth));
1377 ocfs2_shift_records_right(el);
1380 le16_add_cpu(&el->l_next_free_rec, 1);
1381 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1385 * For a rotation which involves two leaf nodes, the "root node" is
1386 * the lowest level tree node which contains a path to both leafs. This
1387 * resulting set of information can be used to form a complete "subtree"
1389 * This function is passed two full paths from the dinode down to a
1390 * pair of adjacent leaves. It's task is to figure out which path
1391 * index contains the subtree root - this can be the root index itself
1392 * in a worst-case rotation.
1394 * The array index of the subtree root is passed back.
1396 static int ocfs2_find_subtree_root(struct inode *inode,
1397 struct ocfs2_path *left,
1398 struct ocfs2_path *right)
1403 * Check that the caller passed in two paths from the same tree.
1405 BUG_ON(path_root_bh(left) != path_root_bh(right));
1411 * The caller didn't pass two adjacent paths.
1413 mlog_bug_on_msg(i > left->p_tree_depth,
1414 "Inode %lu, left depth %u, right depth %u\n"
1415 "left leaf blk %llu, right leaf blk %llu\n",
1416 inode->i_ino, left->p_tree_depth,
1417 right->p_tree_depth,
1418 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1419 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1420 } while (left->p_node[i].bh->b_blocknr ==
1421 right->p_node[i].bh->b_blocknr);
1426 typedef void (path_insert_t)(void *, struct buffer_head *);
1429 * Traverse a btree path in search of cpos, starting at root_el.
1431 * This code can be called with a cpos larger than the tree, in which
1432 * case it will return the rightmost path.
1434 static int __ocfs2_find_path(struct inode *inode,
1435 struct ocfs2_extent_list *root_el, u32 cpos,
1436 path_insert_t *func, void *data)
1441 struct buffer_head *bh = NULL;
1442 struct ocfs2_extent_block *eb;
1443 struct ocfs2_extent_list *el;
1444 struct ocfs2_extent_rec *rec;
1445 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1448 while (el->l_tree_depth) {
1449 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1450 ocfs2_error(inode->i_sb,
1451 "Inode %llu has empty extent list at "
1453 (unsigned long long)oi->ip_blkno,
1454 le16_to_cpu(el->l_tree_depth));
1460 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1461 rec = &el->l_recs[i];
1464 * In the case that cpos is off the allocation
1465 * tree, this should just wind up returning the
1468 range = le32_to_cpu(rec->e_cpos) +
1469 ocfs2_rec_clusters(el, rec);
1470 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1474 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1476 ocfs2_error(inode->i_sb,
1477 "Inode %llu has bad blkno in extent list "
1478 "at depth %u (index %d)\n",
1479 (unsigned long long)oi->ip_blkno,
1480 le16_to_cpu(el->l_tree_depth), i);
1487 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1488 &bh, OCFS2_BH_CACHED, inode);
1494 eb = (struct ocfs2_extent_block *) bh->b_data;
1496 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1497 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1502 if (le16_to_cpu(el->l_next_free_rec) >
1503 le16_to_cpu(el->l_count)) {
1504 ocfs2_error(inode->i_sb,
1505 "Inode %llu has bad count in extent list "
1506 "at block %llu (next free=%u, count=%u)\n",
1507 (unsigned long long)oi->ip_blkno,
1508 (unsigned long long)bh->b_blocknr,
1509 le16_to_cpu(el->l_next_free_rec),
1510 le16_to_cpu(el->l_count));
1521 * Catch any trailing bh that the loop didn't handle.
1529 * Given an initialized path (that is, it has a valid root extent
1530 * list), this function will traverse the btree in search of the path
1531 * which would contain cpos.
1533 * The path traveled is recorded in the path structure.
1535 * Note that this will not do any comparisons on leaf node extent
1536 * records, so it will work fine in the case that we just added a tree
1539 struct find_path_data {
1541 struct ocfs2_path *path;
1543 static void find_path_ins(void *data, struct buffer_head *bh)
1545 struct find_path_data *fp = data;
1548 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1551 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1554 struct find_path_data data;
1558 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1559 find_path_ins, &data);
1562 static void find_leaf_ins(void *data, struct buffer_head *bh)
1564 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1565 struct ocfs2_extent_list *el = &eb->h_list;
1566 struct buffer_head **ret = data;
1568 /* We want to retain only the leaf block. */
1569 if (le16_to_cpu(el->l_tree_depth) == 0) {
1575 * Find the leaf block in the tree which would contain cpos. No
1576 * checking of the actual leaf is done.
1578 * Some paths want to call this instead of allocating a path structure
1579 * and calling ocfs2_find_path().
1581 * This function doesn't handle non btree extent lists.
1583 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1584 u32 cpos, struct buffer_head **leaf_bh)
1587 struct buffer_head *bh = NULL;
1589 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1601 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1603 * Basically, we've moved stuff around at the bottom of the tree and
1604 * we need to fix up the extent records above the changes to reflect
1607 * left_rec: the record on the left.
1608 * left_child_el: is the child list pointed to by left_rec
1609 * right_rec: the record to the right of left_rec
1610 * right_child_el: is the child list pointed to by right_rec
1612 * By definition, this only works on interior nodes.
1614 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1615 struct ocfs2_extent_list *left_child_el,
1616 struct ocfs2_extent_rec *right_rec,
1617 struct ocfs2_extent_list *right_child_el)
1619 u32 left_clusters, right_end;
1622 * Interior nodes never have holes. Their cpos is the cpos of
1623 * the leftmost record in their child list. Their cluster
1624 * count covers the full theoretical range of their child list
1625 * - the range between their cpos and the cpos of the record
1626 * immediately to their right.
1628 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1629 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1630 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1631 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1633 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1634 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1637 * Calculate the rightmost cluster count boundary before
1638 * moving cpos - we will need to adjust clusters after
1639 * updating e_cpos to keep the same highest cluster count.
1641 right_end = le32_to_cpu(right_rec->e_cpos);
1642 right_end += le32_to_cpu(right_rec->e_int_clusters);
1644 right_rec->e_cpos = left_rec->e_cpos;
1645 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1647 right_end -= le32_to_cpu(right_rec->e_cpos);
1648 right_rec->e_int_clusters = cpu_to_le32(right_end);
1652 * Adjust the adjacent root node records involved in a
1653 * rotation. left_el_blkno is passed in as a key so that we can easily
1654 * find it's index in the root list.
1656 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1657 struct ocfs2_extent_list *left_el,
1658 struct ocfs2_extent_list *right_el,
1663 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1664 le16_to_cpu(left_el->l_tree_depth));
1666 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1667 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1672 * The path walking code should have never returned a root and
1673 * two paths which are not adjacent.
1675 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1677 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1678 &root_el->l_recs[i + 1], right_el);
1682 * We've changed a leaf block (in right_path) and need to reflect that
1683 * change back up the subtree.
1685 * This happens in multiple places:
1686 * - When we've moved an extent record from the left path leaf to the right
1687 * path leaf to make room for an empty extent in the left path leaf.
1688 * - When our insert into the right path leaf is at the leftmost edge
1689 * and requires an update of the path immediately to it's left. This
1690 * can occur at the end of some types of rotation and appending inserts.
1691 * - When we've adjusted the last extent record in the left path leaf and the
1692 * 1st extent record in the right path leaf during cross extent block merge.
1694 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1695 struct ocfs2_path *left_path,
1696 struct ocfs2_path *right_path,
1700 struct ocfs2_extent_list *el, *left_el, *right_el;
1701 struct ocfs2_extent_rec *left_rec, *right_rec;
1702 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1705 * Update the counts and position values within all the
1706 * interior nodes to reflect the leaf rotation we just did.
1708 * The root node is handled below the loop.
1710 * We begin the loop with right_el and left_el pointing to the
1711 * leaf lists and work our way up.
1713 * NOTE: within this loop, left_el and right_el always refer
1714 * to the *child* lists.
1716 left_el = path_leaf_el(left_path);
1717 right_el = path_leaf_el(right_path);
1718 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1719 mlog(0, "Adjust records at index %u\n", i);
1722 * One nice property of knowing that all of these
1723 * nodes are below the root is that we only deal with
1724 * the leftmost right node record and the rightmost
1727 el = left_path->p_node[i].el;
1728 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1729 left_rec = &el->l_recs[idx];
1731 el = right_path->p_node[i].el;
1732 right_rec = &el->l_recs[0];
1734 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1737 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1741 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1746 * Setup our list pointers now so that the current
1747 * parents become children in the next iteration.
1749 left_el = left_path->p_node[i].el;
1750 right_el = right_path->p_node[i].el;
1754 * At the root node, adjust the two adjacent records which
1755 * begin our path to the leaves.
1758 el = left_path->p_node[subtree_index].el;
1759 left_el = left_path->p_node[subtree_index + 1].el;
1760 right_el = right_path->p_node[subtree_index + 1].el;
1762 ocfs2_adjust_root_records(el, left_el, right_el,
1763 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1765 root_bh = left_path->p_node[subtree_index].bh;
1767 ret = ocfs2_journal_dirty(handle, root_bh);
1772 static int ocfs2_rotate_subtree_right(struct inode *inode,
1774 struct ocfs2_path *left_path,
1775 struct ocfs2_path *right_path,
1779 struct buffer_head *right_leaf_bh;
1780 struct buffer_head *left_leaf_bh = NULL;
1781 struct buffer_head *root_bh;
1782 struct ocfs2_extent_list *right_el, *left_el;
1783 struct ocfs2_extent_rec move_rec;
1785 left_leaf_bh = path_leaf_bh(left_path);
1786 left_el = path_leaf_el(left_path);
1788 if (left_el->l_next_free_rec != left_el->l_count) {
1789 ocfs2_error(inode->i_sb,
1790 "Inode %llu has non-full interior leaf node %llu"
1792 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1793 (unsigned long long)left_leaf_bh->b_blocknr,
1794 le16_to_cpu(left_el->l_next_free_rec));
1799 * This extent block may already have an empty record, so we
1800 * return early if so.
1802 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1805 root_bh = left_path->p_node[subtree_index].bh;
1806 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1808 ret = ocfs2_journal_access(handle, inode, root_bh,
1809 OCFS2_JOURNAL_ACCESS_WRITE);
1815 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1816 ret = ocfs2_journal_access(handle, inode,
1817 right_path->p_node[i].bh,
1818 OCFS2_JOURNAL_ACCESS_WRITE);
1824 ret = ocfs2_journal_access(handle, inode,
1825 left_path->p_node[i].bh,
1826 OCFS2_JOURNAL_ACCESS_WRITE);
1833 right_leaf_bh = path_leaf_bh(right_path);
1834 right_el = path_leaf_el(right_path);
1836 /* This is a code error, not a disk corruption. */
1837 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1838 "because rightmost leaf block %llu is empty\n",
1839 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1840 (unsigned long long)right_leaf_bh->b_blocknr);
1842 ocfs2_create_empty_extent(right_el);
1844 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1850 /* Do the copy now. */
1851 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1852 move_rec = left_el->l_recs[i];
1853 right_el->l_recs[0] = move_rec;
1856 * Clear out the record we just copied and shift everything
1857 * over, leaving an empty extent in the left leaf.
1859 * We temporarily subtract from next_free_rec so that the
1860 * shift will lose the tail record (which is now defunct).
1862 le16_add_cpu(&left_el->l_next_free_rec, -1);
1863 ocfs2_shift_records_right(left_el);
1864 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1865 le16_add_cpu(&left_el->l_next_free_rec, 1);
1867 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1873 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1881 * Given a full path, determine what cpos value would return us a path
1882 * containing the leaf immediately to the left of the current one.
1884 * Will return zero if the path passed in is already the leftmost path.
1886 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1887 struct ocfs2_path *path, u32 *cpos)
1891 struct ocfs2_extent_list *el;
1893 BUG_ON(path->p_tree_depth == 0);
1897 blkno = path_leaf_bh(path)->b_blocknr;
1899 /* Start at the tree node just above the leaf and work our way up. */
1900 i = path->p_tree_depth - 1;
1902 el = path->p_node[i].el;
1905 * Find the extent record just before the one in our
1908 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1909 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1913 * We've determined that the
1914 * path specified is already
1915 * the leftmost one - return a
1921 * The leftmost record points to our
1922 * leaf - we need to travel up the
1928 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
1929 *cpos = *cpos + ocfs2_rec_clusters(el,
1930 &el->l_recs[j - 1]);
1937 * If we got here, we never found a valid node where
1938 * the tree indicated one should be.
1941 "Invalid extent tree at extent block %llu\n",
1942 (unsigned long long)blkno);
1947 blkno = path->p_node[i].bh->b_blocknr;
1956 * Extend the transaction by enough credits to complete the rotation,
1957 * and still leave at least the original number of credits allocated
1958 * to this transaction.
1960 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
1962 struct ocfs2_path *path)
1964 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
1966 if (handle->h_buffer_credits < credits)
1967 return ocfs2_extend_trans(handle, credits);
1973 * Trap the case where we're inserting into the theoretical range past
1974 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1975 * whose cpos is less than ours into the right leaf.
1977 * It's only necessary to look at the rightmost record of the left
1978 * leaf because the logic that calls us should ensure that the
1979 * theoretical ranges in the path components above the leaves are
1982 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1985 struct ocfs2_extent_list *left_el;
1986 struct ocfs2_extent_rec *rec;
1989 left_el = path_leaf_el(left_path);
1990 next_free = le16_to_cpu(left_el->l_next_free_rec);
1991 rec = &left_el->l_recs[next_free - 1];
1993 if (insert_cpos > le32_to_cpu(rec->e_cpos))
1998 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2000 int next_free = le16_to_cpu(el->l_next_free_rec);
2002 struct ocfs2_extent_rec *rec;
2007 rec = &el->l_recs[0];
2008 if (ocfs2_is_empty_extent(rec)) {
2012 rec = &el->l_recs[1];
2015 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2016 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2022 * Rotate all the records in a btree right one record, starting at insert_cpos.
2024 * The path to the rightmost leaf should be passed in.
2026 * The array is assumed to be large enough to hold an entire path (tree depth).
2028 * Upon succesful return from this function:
2030 * - The 'right_path' array will contain a path to the leaf block
2031 * whose range contains e_cpos.
2032 * - That leaf block will have a single empty extent in list index 0.
2033 * - In the case that the rotation requires a post-insert update,
2034 * *ret_left_path will contain a valid path which can be passed to
2035 * ocfs2_insert_path().
2037 static int ocfs2_rotate_tree_right(struct inode *inode,
2039 enum ocfs2_split_type split,
2041 struct ocfs2_path *right_path,
2042 struct ocfs2_path **ret_left_path)
2044 int ret, start, orig_credits = handle->h_buffer_credits;
2046 struct ocfs2_path *left_path = NULL;
2048 *ret_left_path = NULL;
2050 left_path = ocfs2_new_path(path_root_bh(right_path),
2051 path_root_el(right_path));
2058 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2064 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2067 * What we want to do here is:
2069 * 1) Start with the rightmost path.
2071 * 2) Determine a path to the leaf block directly to the left
2074 * 3) Determine the 'subtree root' - the lowest level tree node
2075 * which contains a path to both leaves.
2077 * 4) Rotate the subtree.
2079 * 5) Find the next subtree by considering the left path to be
2080 * the new right path.
2082 * The check at the top of this while loop also accepts
2083 * insert_cpos == cpos because cpos is only a _theoretical_
2084 * value to get us the left path - insert_cpos might very well
2085 * be filling that hole.
2087 * Stop at a cpos of '0' because we either started at the
2088 * leftmost branch (i.e., a tree with one branch and a
2089 * rotation inside of it), or we've gone as far as we can in
2090 * rotating subtrees.
2092 while (cpos && insert_cpos <= cpos) {
2093 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2096 ret = ocfs2_find_path(inode, left_path, cpos);
2102 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2103 path_leaf_bh(right_path),
2104 "Inode %lu: error during insert of %u "
2105 "(left path cpos %u) results in two identical "
2106 "paths ending at %llu\n",
2107 inode->i_ino, insert_cpos, cpos,
2108 (unsigned long long)
2109 path_leaf_bh(left_path)->b_blocknr);
2111 if (split == SPLIT_NONE &&
2112 ocfs2_rotate_requires_path_adjustment(left_path,
2116 * We've rotated the tree as much as we
2117 * should. The rest is up to
2118 * ocfs2_insert_path() to complete, after the
2119 * record insertion. We indicate this
2120 * situation by returning the left path.
2122 * The reason we don't adjust the records here
2123 * before the record insert is that an error
2124 * later might break the rule where a parent
2125 * record e_cpos will reflect the actual
2126 * e_cpos of the 1st nonempty record of the
2129 *ret_left_path = left_path;
2133 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2135 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2137 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2138 right_path->p_tree_depth);
2140 ret = ocfs2_extend_rotate_transaction(handle, start,
2141 orig_credits, right_path);
2147 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2154 if (split != SPLIT_NONE &&
2155 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2158 * A rotate moves the rightmost left leaf
2159 * record over to the leftmost right leaf
2160 * slot. If we're doing an extent split
2161 * instead of a real insert, then we have to
2162 * check that the extent to be split wasn't
2163 * just moved over. If it was, then we can
2164 * exit here, passing left_path back -
2165 * ocfs2_split_extent() is smart enough to
2166 * search both leaves.
2168 *ret_left_path = left_path;
2173 * There is no need to re-read the next right path
2174 * as we know that it'll be our current left
2175 * path. Optimize by copying values instead.
2177 ocfs2_mv_path(right_path, left_path);
2179 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2188 ocfs2_free_path(left_path);
2194 static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2195 struct ocfs2_path *path)
2198 struct ocfs2_extent_rec *rec;
2199 struct ocfs2_extent_list *el;
2200 struct ocfs2_extent_block *eb;
2203 /* Path should always be rightmost. */
2204 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2205 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2208 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2209 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2210 rec = &el->l_recs[idx];
2211 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2213 for (i = 0; i < path->p_tree_depth; i++) {
2214 el = path->p_node[i].el;
2215 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2216 rec = &el->l_recs[idx];
2218 rec->e_int_clusters = cpu_to_le32(range);
2219 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2221 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2225 static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2226 struct ocfs2_cached_dealloc_ctxt *dealloc,
2227 struct ocfs2_path *path, int unlink_start)
2230 struct ocfs2_extent_block *eb;
2231 struct ocfs2_extent_list *el;
2232 struct buffer_head *bh;
2234 for(i = unlink_start; i < path_num_items(path); i++) {
2235 bh = path->p_node[i].bh;
2237 eb = (struct ocfs2_extent_block *)bh->b_data;
2239 * Not all nodes might have had their final count
2240 * decremented by the caller - handle this here.
2243 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2245 "Inode %llu, attempted to remove extent block "
2246 "%llu with %u records\n",
2247 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2248 (unsigned long long)le64_to_cpu(eb->h_blkno),
2249 le16_to_cpu(el->l_next_free_rec));
2251 ocfs2_journal_dirty(handle, bh);
2252 ocfs2_remove_from_cache(inode, bh);
2256 el->l_next_free_rec = 0;
2257 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2259 ocfs2_journal_dirty(handle, bh);
2261 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2265 ocfs2_remove_from_cache(inode, bh);
2269 static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2270 struct ocfs2_path *left_path,
2271 struct ocfs2_path *right_path,
2273 struct ocfs2_cached_dealloc_ctxt *dealloc)
2276 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2277 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2278 struct ocfs2_extent_list *el;
2279 struct ocfs2_extent_block *eb;
2281 el = path_leaf_el(left_path);
2283 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2285 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2286 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2289 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2291 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2292 le16_add_cpu(&root_el->l_next_free_rec, -1);
2294 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2295 eb->h_next_leaf_blk = 0;
2297 ocfs2_journal_dirty(handle, root_bh);
2298 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2300 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2304 static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2305 struct ocfs2_path *left_path,
2306 struct ocfs2_path *right_path,
2308 struct ocfs2_cached_dealloc_ctxt *dealloc,
2310 struct ocfs2_extent_tree *et)
2312 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2313 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2314 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2315 struct ocfs2_extent_block *eb;
2319 right_leaf_el = path_leaf_el(right_path);
2320 left_leaf_el = path_leaf_el(left_path);
2321 root_bh = left_path->p_node[subtree_index].bh;
2322 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2324 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2327 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2328 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2330 * It's legal for us to proceed if the right leaf is
2331 * the rightmost one and it has an empty extent. There
2332 * are two cases to handle - whether the leaf will be
2333 * empty after removal or not. If the leaf isn't empty
2334 * then just remove the empty extent up front. The
2335 * next block will handle empty leaves by flagging
2338 * Non rightmost leaves will throw -EAGAIN and the
2339 * caller can manually move the subtree and retry.
2342 if (eb->h_next_leaf_blk != 0ULL)
2345 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2346 ret = ocfs2_journal_access(handle, inode,
2347 path_leaf_bh(right_path),
2348 OCFS2_JOURNAL_ACCESS_WRITE);
2354 ocfs2_remove_empty_extent(right_leaf_el);
2356 right_has_empty = 1;
2359 if (eb->h_next_leaf_blk == 0ULL &&
2360 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2362 * We have to update i_last_eb_blk during the meta
2365 ret = ocfs2_journal_access(handle, inode, et_root_bh,
2366 OCFS2_JOURNAL_ACCESS_WRITE);
2372 del_right_subtree = 1;
2376 * Getting here with an empty extent in the right path implies
2377 * that it's the rightmost path and will be deleted.
2379 BUG_ON(right_has_empty && !del_right_subtree);
2381 ret = ocfs2_journal_access(handle, inode, root_bh,
2382 OCFS2_JOURNAL_ACCESS_WRITE);
2388 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2389 ret = ocfs2_journal_access(handle, inode,
2390 right_path->p_node[i].bh,
2391 OCFS2_JOURNAL_ACCESS_WRITE);
2397 ret = ocfs2_journal_access(handle, inode,
2398 left_path->p_node[i].bh,
2399 OCFS2_JOURNAL_ACCESS_WRITE);
2406 if (!right_has_empty) {
2408 * Only do this if we're moving a real
2409 * record. Otherwise, the action is delayed until
2410 * after removal of the right path in which case we
2411 * can do a simple shift to remove the empty extent.
2413 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2414 memset(&right_leaf_el->l_recs[0], 0,
2415 sizeof(struct ocfs2_extent_rec));
2417 if (eb->h_next_leaf_blk == 0ULL) {
2419 * Move recs over to get rid of empty extent, decrease
2420 * next_free. This is allowed to remove the last
2421 * extent in our leaf (setting l_next_free_rec to
2422 * zero) - the delete code below won't care.
2424 ocfs2_remove_empty_extent(right_leaf_el);
2427 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2430 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2434 if (del_right_subtree) {
2435 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2436 subtree_index, dealloc);
2437 ocfs2_update_edge_lengths(inode, handle, left_path);
2439 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2440 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2443 * Removal of the extent in the left leaf was skipped
2444 * above so we could delete the right path
2447 if (right_has_empty)
2448 ocfs2_remove_empty_extent(left_leaf_el);
2450 ret = ocfs2_journal_dirty(handle, et_root_bh);
2456 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2464 * Given a full path, determine what cpos value would return us a path
2465 * containing the leaf immediately to the right of the current one.
2467 * Will return zero if the path passed in is already the rightmost path.
2469 * This looks similar, but is subtly different to
2470 * ocfs2_find_cpos_for_left_leaf().
2472 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2473 struct ocfs2_path *path, u32 *cpos)
2477 struct ocfs2_extent_list *el;
2481 if (path->p_tree_depth == 0)
2484 blkno = path_leaf_bh(path)->b_blocknr;
2486 /* Start at the tree node just above the leaf and work our way up. */
2487 i = path->p_tree_depth - 1;
2491 el = path->p_node[i].el;
2494 * Find the extent record just after the one in our
2497 next_free = le16_to_cpu(el->l_next_free_rec);
2498 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2499 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2500 if (j == (next_free - 1)) {
2503 * We've determined that the
2504 * path specified is already
2505 * the rightmost one - return a
2511 * The rightmost record points to our
2512 * leaf - we need to travel up the
2518 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2524 * If we got here, we never found a valid node where
2525 * the tree indicated one should be.
2528 "Invalid extent tree at extent block %llu\n",
2529 (unsigned long long)blkno);
2534 blkno = path->p_node[i].bh->b_blocknr;
2542 static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2544 struct buffer_head *bh,
2545 struct ocfs2_extent_list *el)
2549 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2552 ret = ocfs2_journal_access(handle, inode, bh,
2553 OCFS2_JOURNAL_ACCESS_WRITE);
2559 ocfs2_remove_empty_extent(el);
2561 ret = ocfs2_journal_dirty(handle, bh);
2569 static int __ocfs2_rotate_tree_left(struct inode *inode,
2570 handle_t *handle, int orig_credits,
2571 struct ocfs2_path *path,
2572 struct ocfs2_cached_dealloc_ctxt *dealloc,
2573 struct ocfs2_path **empty_extent_path,
2574 struct ocfs2_extent_tree *et)
2576 int ret, subtree_root, deleted;
2578 struct ocfs2_path *left_path = NULL;
2579 struct ocfs2_path *right_path = NULL;
2581 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2583 *empty_extent_path = NULL;
2585 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2592 left_path = ocfs2_new_path(path_root_bh(path),
2593 path_root_el(path));
2600 ocfs2_cp_path(left_path, path);
2602 right_path = ocfs2_new_path(path_root_bh(path),
2603 path_root_el(path));
2610 while (right_cpos) {
2611 ret = ocfs2_find_path(inode, right_path, right_cpos);
2617 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2620 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2622 (unsigned long long)
2623 right_path->p_node[subtree_root].bh->b_blocknr,
2624 right_path->p_tree_depth);
2626 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2627 orig_credits, left_path);
2634 * Caller might still want to make changes to the
2635 * tree root, so re-add it to the journal here.
2637 ret = ocfs2_journal_access(handle, inode,
2638 path_root_bh(left_path),
2639 OCFS2_JOURNAL_ACCESS_WRITE);
2645 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2646 right_path, subtree_root,
2647 dealloc, &deleted, et);
2648 if (ret == -EAGAIN) {
2650 * The rotation has to temporarily stop due to
2651 * the right subtree having an empty
2652 * extent. Pass it back to the caller for a
2655 *empty_extent_path = right_path;
2665 * The subtree rotate might have removed records on
2666 * the rightmost edge. If so, then rotation is
2672 ocfs2_mv_path(left_path, right_path);
2674 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2683 ocfs2_free_path(right_path);
2684 ocfs2_free_path(left_path);
2689 static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2690 struct ocfs2_path *path,
2691 struct ocfs2_cached_dealloc_ctxt *dealloc,
2692 struct ocfs2_extent_tree *et)
2694 int ret, subtree_index;
2696 struct ocfs2_path *left_path = NULL;
2697 struct ocfs2_extent_block *eb;
2698 struct ocfs2_extent_list *el;
2701 ret = ocfs2_et_sanity_check(inode, et);
2705 * There's two ways we handle this depending on
2706 * whether path is the only existing one.
2708 ret = ocfs2_extend_rotate_transaction(handle, 0,
2709 handle->h_buffer_credits,
2716 ret = ocfs2_journal_access_path(inode, handle, path);
2722 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2730 * We have a path to the left of this one - it needs
2733 left_path = ocfs2_new_path(path_root_bh(path),
2734 path_root_el(path));
2741 ret = ocfs2_find_path(inode, left_path, cpos);
2747 ret = ocfs2_journal_access_path(inode, handle, left_path);
2753 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2755 ocfs2_unlink_subtree(inode, handle, left_path, path,
2756 subtree_index, dealloc);
2757 ocfs2_update_edge_lengths(inode, handle, left_path);
2759 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2760 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2763 * 'path' is also the leftmost path which
2764 * means it must be the only one. This gets
2765 * handled differently because we want to
2766 * revert the inode back to having extents
2769 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2771 el = et->et_root_el;
2772 el->l_tree_depth = 0;
2773 el->l_next_free_rec = 0;
2774 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2776 ocfs2_et_set_last_eb_blk(et, 0);
2779 ocfs2_journal_dirty(handle, path_root_bh(path));
2782 ocfs2_free_path(left_path);
2787 * Left rotation of btree records.
2789 * In many ways, this is (unsurprisingly) the opposite of right
2790 * rotation. We start at some non-rightmost path containing an empty
2791 * extent in the leaf block. The code works its way to the rightmost
2792 * path by rotating records to the left in every subtree.
2794 * This is used by any code which reduces the number of extent records
2795 * in a leaf. After removal, an empty record should be placed in the
2796 * leftmost list position.
2798 * This won't handle a length update of the rightmost path records if
2799 * the rightmost tree leaf record is removed so the caller is
2800 * responsible for detecting and correcting that.
2802 static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2803 struct ocfs2_path *path,
2804 struct ocfs2_cached_dealloc_ctxt *dealloc,
2805 struct ocfs2_extent_tree *et)
2807 int ret, orig_credits = handle->h_buffer_credits;
2808 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2809 struct ocfs2_extent_block *eb;
2810 struct ocfs2_extent_list *el;
2812 el = path_leaf_el(path);
2813 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2816 if (path->p_tree_depth == 0) {
2817 rightmost_no_delete:
2819 * Inline extents. This is trivially handled, so do
2822 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2824 path_leaf_el(path));
2831 * Handle rightmost branch now. There's several cases:
2832 * 1) simple rotation leaving records in there. That's trivial.
2833 * 2) rotation requiring a branch delete - there's no more
2834 * records left. Two cases of this:
2835 * a) There are branches to the left.
2836 * b) This is also the leftmost (the only) branch.
2838 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2839 * 2a) we need the left branch so that we can update it with the unlink
2840 * 2b) we need to bring the inode back to inline extents.
2843 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2845 if (eb->h_next_leaf_blk == 0) {
2847 * This gets a bit tricky if we're going to delete the
2848 * rightmost path. Get the other cases out of the way
2851 if (le16_to_cpu(el->l_next_free_rec) > 1)
2852 goto rightmost_no_delete;
2854 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2856 ocfs2_error(inode->i_sb,
2857 "Inode %llu has empty extent block at %llu",
2858 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2859 (unsigned long long)le64_to_cpu(eb->h_blkno));
2864 * XXX: The caller can not trust "path" any more after
2865 * this as it will have been deleted. What do we do?
2867 * In theory the rotate-for-merge code will never get
2868 * here because it'll always ask for a rotate in a
2872 ret = ocfs2_remove_rightmost_path(inode, handle, path,
2880 * Now we can loop, remembering the path we get from -EAGAIN
2881 * and restarting from there.
2884 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
2885 dealloc, &restart_path, et);
2886 if (ret && ret != -EAGAIN) {
2891 while (ret == -EAGAIN) {
2892 tmp_path = restart_path;
2893 restart_path = NULL;
2895 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2898 if (ret && ret != -EAGAIN) {
2903 ocfs2_free_path(tmp_path);
2911 ocfs2_free_path(tmp_path);
2912 ocfs2_free_path(restart_path);
2916 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2919 struct ocfs2_extent_rec *rec = &el->l_recs[index];
2922 if (rec->e_leaf_clusters == 0) {
2924 * We consumed all of the merged-from record. An empty
2925 * extent cannot exist anywhere but the 1st array
2926 * position, so move things over if the merged-from
2927 * record doesn't occupy that position.
2929 * This creates a new empty extent so the caller
2930 * should be smart enough to have removed any existing
2934 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
2935 size = index * sizeof(struct ocfs2_extent_rec);
2936 memmove(&el->l_recs[1], &el->l_recs[0], size);
2940 * Always memset - the caller doesn't check whether it
2941 * created an empty extent, so there could be junk in
2944 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2948 static int ocfs2_get_right_path(struct inode *inode,
2949 struct ocfs2_path *left_path,
2950 struct ocfs2_path **ret_right_path)
2954 struct ocfs2_path *right_path = NULL;
2955 struct ocfs2_extent_list *left_el;
2957 *ret_right_path = NULL;
2959 /* This function shouldn't be called for non-trees. */
2960 BUG_ON(left_path->p_tree_depth == 0);
2962 left_el = path_leaf_el(left_path);
2963 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
2965 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2972 /* This function shouldn't be called for the rightmost leaf. */
2973 BUG_ON(right_cpos == 0);
2975 right_path = ocfs2_new_path(path_root_bh(left_path),
2976 path_root_el(left_path));
2983 ret = ocfs2_find_path(inode, right_path, right_cpos);
2989 *ret_right_path = right_path;
2992 ocfs2_free_path(right_path);
2997 * Remove split_rec clusters from the record at index and merge them
2998 * onto the beginning of the record "next" to it.
2999 * For index < l_count - 1, the next means the extent rec at index + 1.
3000 * For index == l_count - 1, the "next" means the 1st extent rec of the
3001 * next extent block.
3003 static int ocfs2_merge_rec_right(struct inode *inode,
3004 struct ocfs2_path *left_path,
3006 struct ocfs2_extent_rec *split_rec,
3009 int ret, next_free, i;
3010 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3011 struct ocfs2_extent_rec *left_rec;
3012 struct ocfs2_extent_rec *right_rec;
3013 struct ocfs2_extent_list *right_el;
3014 struct ocfs2_path *right_path = NULL;
3015 int subtree_index = 0;
3016 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3017 struct buffer_head *bh = path_leaf_bh(left_path);
3018 struct buffer_head *root_bh = NULL;
3020 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3021 left_rec = &el->l_recs[index];
3023 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3024 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3025 /* we meet with a cross extent block merge. */
3026 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3032 right_el = path_leaf_el(right_path);
3033 next_free = le16_to_cpu(right_el->l_next_free_rec);
3034 BUG_ON(next_free <= 0);
3035 right_rec = &right_el->l_recs[0];
3036 if (ocfs2_is_empty_extent(right_rec)) {
3037 BUG_ON(next_free <= 1);
3038 right_rec = &right_el->l_recs[1];
3041 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3042 le16_to_cpu(left_rec->e_leaf_clusters) !=
3043 le32_to_cpu(right_rec->e_cpos));
3045 subtree_index = ocfs2_find_subtree_root(inode,
3046 left_path, right_path);
3048 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3049 handle->h_buffer_credits,
3056 root_bh = left_path->p_node[subtree_index].bh;
3057 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3059 ret = ocfs2_journal_access(handle, inode, root_bh,
3060 OCFS2_JOURNAL_ACCESS_WRITE);
3066 for (i = subtree_index + 1;
3067 i < path_num_items(right_path); i++) {
3068 ret = ocfs2_journal_access(handle, inode,
3069 right_path->p_node[i].bh,
3070 OCFS2_JOURNAL_ACCESS_WRITE);
3076 ret = ocfs2_journal_access(handle, inode,
3077 left_path->p_node[i].bh,
3078 OCFS2_JOURNAL_ACCESS_WRITE);
3086 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3087 right_rec = &el->l_recs[index + 1];
3090 ret = ocfs2_journal_access(handle, inode, bh,
3091 OCFS2_JOURNAL_ACCESS_WRITE);
3097 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3099 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3100 le64_add_cpu(&right_rec->e_blkno,
3101 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3102 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3104 ocfs2_cleanup_merge(el, index);
3106 ret = ocfs2_journal_dirty(handle, bh);
3111 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3115 ocfs2_complete_edge_insert(inode, handle, left_path,
3116 right_path, subtree_index);
3120 ocfs2_free_path(right_path);
3124 static int ocfs2_get_left_path(struct inode *inode,
3125 struct ocfs2_path *right_path,
3126 struct ocfs2_path **ret_left_path)
3130 struct ocfs2_path *left_path = NULL;
3132 *ret_left_path = NULL;
3134 /* This function shouldn't be called for non-trees. */
3135 BUG_ON(right_path->p_tree_depth == 0);
3137 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3138 right_path, &left_cpos);
3144 /* This function shouldn't be called for the leftmost leaf. */
3145 BUG_ON(left_cpos == 0);
3147 left_path = ocfs2_new_path(path_root_bh(right_path),
3148 path_root_el(right_path));
3155 ret = ocfs2_find_path(inode, left_path, left_cpos);
3161 *ret_left_path = left_path;
3164 ocfs2_free_path(left_path);
3169 * Remove split_rec clusters from the record at index and merge them
3170 * onto the tail of the record "before" it.
3171 * For index > 0, the "before" means the extent rec at index - 1.
3173 * For index == 0, the "before" means the last record of the previous
3174 * extent block. And there is also a situation that we may need to
3175 * remove the rightmost leaf extent block in the right_path and change
3176 * the right path to indicate the new rightmost path.
3178 static int ocfs2_merge_rec_left(struct inode *inode,
3179 struct ocfs2_path *right_path,
3181 struct ocfs2_extent_rec *split_rec,
3182 struct ocfs2_cached_dealloc_ctxt *dealloc,
3183 struct ocfs2_extent_tree *et,
3186 int ret, i, subtree_index = 0, has_empty_extent = 0;
3187 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3188 struct ocfs2_extent_rec *left_rec;
3189 struct ocfs2_extent_rec *right_rec;
3190 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3191 struct buffer_head *bh = path_leaf_bh(right_path);
3192 struct buffer_head *root_bh = NULL;
3193 struct ocfs2_path *left_path = NULL;
3194 struct ocfs2_extent_list *left_el;
3198 right_rec = &el->l_recs[index];
3200 /* we meet with a cross extent block merge. */
3201 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3207 left_el = path_leaf_el(left_path);
3208 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3209 le16_to_cpu(left_el->l_count));
3211 left_rec = &left_el->l_recs[
3212 le16_to_cpu(left_el->l_next_free_rec) - 1];
3213 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3214 le16_to_cpu(left_rec->e_leaf_clusters) !=
3215 le32_to_cpu(split_rec->e_cpos));
3217 subtree_index = ocfs2_find_subtree_root(inode,
3218 left_path, right_path);
3220 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3221 handle->h_buffer_credits,
3228 root_bh = left_path->p_node[subtree_index].bh;
3229 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3231 ret = ocfs2_journal_access(handle, inode, root_bh,
3232 OCFS2_JOURNAL_ACCESS_WRITE);
3238 for (i = subtree_index + 1;
3239 i < path_num_items(right_path); i++) {
3240 ret = ocfs2_journal_access(handle, inode,
3241 right_path->p_node[i].bh,
3242 OCFS2_JOURNAL_ACCESS_WRITE);
3248 ret = ocfs2_journal_access(handle, inode,
3249 left_path->p_node[i].bh,
3250 OCFS2_JOURNAL_ACCESS_WRITE);
3257 left_rec = &el->l_recs[index - 1];
3258 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3259 has_empty_extent = 1;
3262 ret = ocfs2_journal_access(handle, inode, bh,
3263 OCFS2_JOURNAL_ACCESS_WRITE);
3269 if (has_empty_extent && index == 1) {
3271 * The easy case - we can just plop the record right in.
3273 *left_rec = *split_rec;
3275 has_empty_extent = 0;
3277 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3279 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3280 le64_add_cpu(&right_rec->e_blkno,
3281 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3282 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3284 ocfs2_cleanup_merge(el, index);
3286 ret = ocfs2_journal_dirty(handle, bh);
3291 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3296 * In the situation that the right_rec is empty and the extent
3297 * block is empty also, ocfs2_complete_edge_insert can't handle
3298 * it and we need to delete the right extent block.
3300 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3301 le16_to_cpu(el->l_next_free_rec) == 1) {
3303 ret = ocfs2_remove_rightmost_path(inode, handle,
3311 /* Now the rightmost extent block has been deleted.
3312 * So we use the new rightmost path.
3314 ocfs2_mv_path(right_path, left_path);
3317 ocfs2_complete_edge_insert(inode, handle, left_path,
3318 right_path, subtree_index);
3322 ocfs2_free_path(left_path);
3326 static int ocfs2_try_to_merge_extent(struct inode *inode,
3328 struct ocfs2_path *path,
3330 struct ocfs2_extent_rec *split_rec,
3331 struct ocfs2_cached_dealloc_ctxt *dealloc,
3332 struct ocfs2_merge_ctxt *ctxt,
3333 struct ocfs2_extent_tree *et)
3337 struct ocfs2_extent_list *el = path_leaf_el(path);
3338 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3340 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3342 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3344 * The merge code will need to create an empty
3345 * extent to take the place of the newly
3346 * emptied slot. Remove any pre-existing empty
3347 * extents - having more than one in a leaf is
3350 ret = ocfs2_rotate_tree_left(inode, handle, path,
3357 rec = &el->l_recs[split_index];
3360 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3362 * Left-right contig implies this.
3364 BUG_ON(!ctxt->c_split_covers_rec);
3367 * Since the leftright insert always covers the entire
3368 * extent, this call will delete the insert record
3369 * entirely, resulting in an empty extent record added to
3372 * Since the adding of an empty extent shifts
3373 * everything back to the right, there's no need to
3374 * update split_index here.
3376 * When the split_index is zero, we need to merge it to the
3377 * prevoius extent block. It is more efficient and easier
3378 * if we do merge_right first and merge_left later.
3380 ret = ocfs2_merge_rec_right(inode, path,
3389 * We can only get this from logic error above.
3391 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3393 /* The merge left us with an empty extent, remove it. */
3394 ret = ocfs2_rotate_tree_left(inode, handle, path,
3401 rec = &el->l_recs[split_index];
3404 * Note that we don't pass split_rec here on purpose -
3405 * we've merged it into the rec already.
3407 ret = ocfs2_merge_rec_left(inode, path,
3417 ret = ocfs2_rotate_tree_left(inode, handle, path,
3420 * Error from this last rotate is not critical, so
3421 * print but don't bubble it up.
3428 * Merge a record to the left or right.
3430 * 'contig_type' is relative to the existing record,
3431 * so for example, if we're "right contig", it's to
3432 * the record on the left (hence the left merge).
3434 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3435 ret = ocfs2_merge_rec_left(inode,
3445 ret = ocfs2_merge_rec_right(inode,
3455 if (ctxt->c_split_covers_rec) {
3457 * The merge may have left an empty extent in
3458 * our leaf. Try to rotate it away.
3460 ret = ocfs2_rotate_tree_left(inode, handle, path,
3472 static void ocfs2_subtract_from_rec(struct super_block *sb,
3473 enum ocfs2_split_type split,
3474 struct ocfs2_extent_rec *rec,
3475 struct ocfs2_extent_rec *split_rec)
3479 len_blocks = ocfs2_clusters_to_blocks(sb,
3480 le16_to_cpu(split_rec->e_leaf_clusters));
3482 if (split == SPLIT_LEFT) {
3484 * Region is on the left edge of the existing
3487 le32_add_cpu(&rec->e_cpos,
3488 le16_to_cpu(split_rec->e_leaf_clusters));
3489 le64_add_cpu(&rec->e_blkno, len_blocks);
3490 le16_add_cpu(&rec->e_leaf_clusters,
3491 -le16_to_cpu(split_rec->e_leaf_clusters));
3494 * Region is on the right edge of the existing
3497 le16_add_cpu(&rec->e_leaf_clusters,
3498 -le16_to_cpu(split_rec->e_leaf_clusters));
3503 * Do the final bits of extent record insertion at the target leaf
3504 * list. If this leaf is part of an allocation tree, it is assumed
3505 * that the tree above has been prepared.
3507 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3508 struct ocfs2_extent_list *el,
3509 struct ocfs2_insert_type *insert,
3510 struct inode *inode)
3512 int i = insert->ins_contig_index;
3514 struct ocfs2_extent_rec *rec;
3516 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3518 if (insert->ins_split != SPLIT_NONE) {
3519 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3521 rec = &el->l_recs[i];
3522 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3528 * Contiguous insert - either left or right.
3530 if (insert->ins_contig != CONTIG_NONE) {
3531 rec = &el->l_recs[i];
3532 if (insert->ins_contig == CONTIG_LEFT) {
3533 rec->e_blkno = insert_rec->e_blkno;
3534 rec->e_cpos = insert_rec->e_cpos;
3536 le16_add_cpu(&rec->e_leaf_clusters,
3537 le16_to_cpu(insert_rec->e_leaf_clusters));
3542 * Handle insert into an empty leaf.
3544 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3545 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3546 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3547 el->l_recs[0] = *insert_rec;
3548 el->l_next_free_rec = cpu_to_le16(1);
3555 if (insert->ins_appending == APPEND_TAIL) {
3556 i = le16_to_cpu(el->l_next_free_rec) - 1;
3557 rec = &el->l_recs[i];
3558 range = le32_to_cpu(rec->e_cpos)
3559 + le16_to_cpu(rec->e_leaf_clusters);
3560 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3562 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3563 le16_to_cpu(el->l_count),
3564 "inode %lu, depth %u, count %u, next free %u, "
3565 "rec.cpos %u, rec.clusters %u, "
3566 "insert.cpos %u, insert.clusters %u\n",
3568 le16_to_cpu(el->l_tree_depth),
3569 le16_to_cpu(el->l_count),
3570 le16_to_cpu(el->l_next_free_rec),
3571 le32_to_cpu(el->l_recs[i].e_cpos),
3572 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3573 le32_to_cpu(insert_rec->e_cpos),
3574 le16_to_cpu(insert_rec->e_leaf_clusters));
3576 el->l_recs[i] = *insert_rec;
3577 le16_add_cpu(&el->l_next_free_rec, 1);
3583 * Ok, we have to rotate.
3585 * At this point, it is safe to assume that inserting into an
3586 * empty leaf and appending to a leaf have both been handled
3589 * This leaf needs to have space, either by the empty 1st
3590 * extent record, or by virtue of an l_next_rec < l_count.
3592 ocfs2_rotate_leaf(el, insert_rec);
3595 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3597 struct ocfs2_path *path,
3598 struct ocfs2_extent_rec *insert_rec)
3600 int ret, i, next_free;
3601 struct buffer_head *bh;
3602 struct ocfs2_extent_list *el;
3603 struct ocfs2_extent_rec *rec;
3606 * Update everything except the leaf block.
3608 for (i = 0; i < path->p_tree_depth; i++) {
3609 bh = path->p_node[i].bh;
3610 el = path->p_node[i].el;
3612 next_free = le16_to_cpu(el->l_next_free_rec);
3613 if (next_free == 0) {
3614 ocfs2_error(inode->i_sb,
3615 "Dinode %llu has a bad extent list",
3616 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3621 rec = &el->l_recs[next_free - 1];
3623 rec->e_int_clusters = insert_rec->e_cpos;
3624 le32_add_cpu(&rec->e_int_clusters,
3625 le16_to_cpu(insert_rec->e_leaf_clusters));
3626 le32_add_cpu(&rec->e_int_clusters,
3627 -le32_to_cpu(rec->e_cpos));
3629 ret = ocfs2_journal_dirty(handle, bh);
3636 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3637 struct ocfs2_extent_rec *insert_rec,
3638 struct ocfs2_path *right_path,
3639 struct ocfs2_path **ret_left_path)
3642 struct ocfs2_extent_list *el;
3643 struct ocfs2_path *left_path = NULL;
3645 *ret_left_path = NULL;
3648 * This shouldn't happen for non-trees. The extent rec cluster
3649 * count manipulation below only works for interior nodes.
3651 BUG_ON(right_path->p_tree_depth == 0);
3654 * If our appending insert is at the leftmost edge of a leaf,
3655 * then we might need to update the rightmost records of the
3658 el = path_leaf_el(right_path);
3659 next_free = le16_to_cpu(el->l_next_free_rec);
3660 if (next_free == 0 ||
3661 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3664 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3671 mlog(0, "Append may need a left path update. cpos: %u, "
3672 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3676 * No need to worry if the append is already in the
3680 left_path = ocfs2_new_path(path_root_bh(right_path),
3681 path_root_el(right_path));
3688 ret = ocfs2_find_path(inode, left_path, left_cpos);
3695 * ocfs2_insert_path() will pass the left_path to the
3701 ret = ocfs2_journal_access_path(inode, handle, right_path);
3707 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
3709 *ret_left_path = left_path;
3713 ocfs2_free_path(left_path);
3718 static void ocfs2_split_record(struct inode *inode,
3719 struct ocfs2_path *left_path,
3720 struct ocfs2_path *right_path,
3721 struct ocfs2_extent_rec *split_rec,
3722 enum ocfs2_split_type split)
3725 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3726 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3727 struct ocfs2_extent_rec *rec, *tmprec;
3729 right_el = path_leaf_el(right_path);;
3731 left_el = path_leaf_el(left_path);
3734 insert_el = right_el;
3735 index = ocfs2_search_extent_list(el, cpos);
3737 if (index == 0 && left_path) {
3738 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3741 * This typically means that the record
3742 * started in the left path but moved to the
3743 * right as a result of rotation. We either
3744 * move the existing record to the left, or we
3745 * do the later insert there.
3747 * In this case, the left path should always
3748 * exist as the rotate code will have passed
3749 * it back for a post-insert update.
3752 if (split == SPLIT_LEFT) {
3754 * It's a left split. Since we know
3755 * that the rotate code gave us an
3756 * empty extent in the left path, we
3757 * can just do the insert there.
3759 insert_el = left_el;
3762 * Right split - we have to move the
3763 * existing record over to the left
3764 * leaf. The insert will be into the
3765 * newly created empty extent in the
3768 tmprec = &right_el->l_recs[index];
3769 ocfs2_rotate_leaf(left_el, tmprec);
3772 memset(tmprec, 0, sizeof(*tmprec));
3773 index = ocfs2_search_extent_list(left_el, cpos);
3774 BUG_ON(index == -1);
3779 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3781 * Left path is easy - we can just allow the insert to
3785 insert_el = left_el;
3786 index = ocfs2_search_extent_list(el, cpos);
3787 BUG_ON(index == -1);
3790 rec = &el->l_recs[index];
3791 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3792 ocfs2_rotate_leaf(insert_el, split_rec);
3796 * This function only does inserts on an allocation b-tree. For tree
3797 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3799 * right_path is the path we want to do the actual insert
3800 * in. left_path should only be passed in if we need to update that
3801 * portion of the tree after an edge insert.
3803 static int ocfs2_insert_path(struct inode *inode,
3805 struct ocfs2_path *left_path,
3806 struct ocfs2_path *right_path,
3807 struct ocfs2_extent_rec *insert_rec,
3808 struct ocfs2_insert_type *insert)
3810 int ret, subtree_index;
3811 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
3814 int credits = handle->h_buffer_credits;
3817 * There's a chance that left_path got passed back to
3818 * us without being accounted for in the
3819 * journal. Extend our transaction here to be sure we
3820 * can change those blocks.
3822 credits += left_path->p_tree_depth;
3824 ret = ocfs2_extend_trans(handle, credits);
3830 ret = ocfs2_journal_access_path(inode, handle, left_path);
3838 * Pass both paths to the journal. The majority of inserts
3839 * will be touching all components anyway.
3841 ret = ocfs2_journal_access_path(inode, handle, right_path);
3847 if (insert->ins_split != SPLIT_NONE) {
3849 * We could call ocfs2_insert_at_leaf() for some types
3850 * of splits, but it's easier to just let one separate
3851 * function sort it all out.
3853 ocfs2_split_record(inode, left_path, right_path,
3854 insert_rec, insert->ins_split);
3857 * Split might have modified either leaf and we don't
3858 * have a guarantee that the later edge insert will
3859 * dirty this for us.
3862 ret = ocfs2_journal_dirty(handle,
3863 path_leaf_bh(left_path));
3867 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3870 ret = ocfs2_journal_dirty(handle, leaf_bh);
3876 * The rotate code has indicated that we need to fix
3877 * up portions of the tree after the insert.
3879 * XXX: Should we extend the transaction here?
3881 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3883 ocfs2_complete_edge_insert(inode, handle, left_path,
3884 right_path, subtree_index);
3892 static int ocfs2_do_insert_extent(struct inode *inode,
3894 struct ocfs2_extent_tree *et,
3895 struct ocfs2_extent_rec *insert_rec,
3896 struct ocfs2_insert_type *type)
3898 int ret, rotate = 0;
3900 struct ocfs2_path *right_path = NULL;
3901 struct ocfs2_path *left_path = NULL;
3902 struct ocfs2_extent_list *el;
3904 el = et->et_root_el;
3906 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
3907 OCFS2_JOURNAL_ACCESS_WRITE);
3913 if (le16_to_cpu(el->l_tree_depth) == 0) {
3914 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3915 goto out_update_clusters;
3918 right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
3926 * Determine the path to start with. Rotations need the
3927 * rightmost path, everything else can go directly to the
3930 cpos = le32_to_cpu(insert_rec->e_cpos);
3931 if (type->ins_appending == APPEND_NONE &&
3932 type->ins_contig == CONTIG_NONE) {
3937 ret = ocfs2_find_path(inode, right_path, cpos);
3944 * Rotations and appends need special treatment - they modify
3945 * parts of the tree's above them.
3947 * Both might pass back a path immediate to the left of the
3948 * one being inserted to. This will be cause
3949 * ocfs2_insert_path() to modify the rightmost records of
3950 * left_path to account for an edge insert.
3952 * XXX: When modifying this code, keep in mind that an insert
3953 * can wind up skipping both of these two special cases...
3956 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
3957 le32_to_cpu(insert_rec->e_cpos),
3958 right_path, &left_path);
3965 * ocfs2_rotate_tree_right() might have extended the
3966 * transaction without re-journaling our tree root.
3968 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
3969 OCFS2_JOURNAL_ACCESS_WRITE);
3974 } else if (type->ins_appending == APPEND_TAIL
3975 && type->ins_contig != CONTIG_LEFT) {
3976 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
3977 right_path, &left_path);
3984 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
3991 out_update_clusters:
3992 if (type->ins_split == SPLIT_NONE)
3993 ocfs2_et_update_clusters(inode, et,
3994 le16_to_cpu(insert_rec->e_leaf_clusters));
3996 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
4001 ocfs2_free_path(left_path);
4002 ocfs2_free_path(right_path);
4007 static enum ocfs2_contig_type
4008 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
4009 struct ocfs2_extent_list *el, int index,
4010 struct ocfs2_extent_rec *split_rec)
4013 enum ocfs2_contig_type ret = CONTIG_NONE;
4014 u32 left_cpos, right_cpos;
4015 struct ocfs2_extent_rec *rec = NULL;
4016 struct ocfs2_extent_list *new_el;
4017 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4018 struct buffer_head *bh;
4019 struct ocfs2_extent_block *eb;
4022 rec = &el->l_recs[index - 1];
4023 } else if (path->p_tree_depth > 0) {
4024 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4029 if (left_cpos != 0) {
4030 left_path = ocfs2_new_path(path_root_bh(path),
4031 path_root_el(path));
4035 status = ocfs2_find_path(inode, left_path, left_cpos);
4039 new_el = path_leaf_el(left_path);
4041 if (le16_to_cpu(new_el->l_next_free_rec) !=
4042 le16_to_cpu(new_el->l_count)) {
4043 bh = path_leaf_bh(left_path);
4044 eb = (struct ocfs2_extent_block *)bh->b_data;
4045 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4049 rec = &new_el->l_recs[
4050 le16_to_cpu(new_el->l_next_free_rec) - 1];
4055 * We're careful to check for an empty extent record here -
4056 * the merge code will know what to do if it sees one.
4059 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4060 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4063 ret = ocfs2_extent_contig(inode, rec, split_rec);
4068 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4069 rec = &el->l_recs[index + 1];
4070 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4071 path->p_tree_depth > 0) {
4072 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4077 if (right_cpos == 0)
4080 right_path = ocfs2_new_path(path_root_bh(path),
4081 path_root_el(path));
4085 status = ocfs2_find_path(inode, right_path, right_cpos);
4089 new_el = path_leaf_el(right_path);
4090 rec = &new_el->l_recs[0];
4091 if (ocfs2_is_empty_extent(rec)) {
4092 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4093 bh = path_leaf_bh(right_path);
4094 eb = (struct ocfs2_extent_block *)bh->b_data;
4095 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4099 rec = &new_el->l_recs[1];
4104 enum ocfs2_contig_type contig_type;
4106 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4108 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4109 ret = CONTIG_LEFTRIGHT;
4110 else if (ret == CONTIG_NONE)
4116 ocfs2_free_path(left_path);
4118 ocfs2_free_path(right_path);
4123 static void ocfs2_figure_contig_type(struct inode *inode,
4124 struct ocfs2_insert_type *insert,
4125 struct ocfs2_extent_list *el,
4126 struct ocfs2_extent_rec *insert_rec,
4127 struct ocfs2_extent_tree *et)
4130 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4132 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4134 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4135 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4137 if (contig_type != CONTIG_NONE) {
4138 insert->ins_contig_index = i;
4142 insert->ins_contig = contig_type;
4144 if (insert->ins_contig != CONTIG_NONE) {
4145 struct ocfs2_extent_rec *rec =
4146 &el->l_recs[insert->ins_contig_index];
4147 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4148 le16_to_cpu(insert_rec->e_leaf_clusters);
4151 * Caller might want us to limit the size of extents, don't
4152 * calculate contiguousness if we might exceed that limit.
4154 if (et->et_max_leaf_clusters &&
4155 (len > et->et_max_leaf_clusters))
4156 insert->ins_contig = CONTIG_NONE;
4161 * This should only be called against the righmost leaf extent list.
4163 * ocfs2_figure_appending_type() will figure out whether we'll have to
4164 * insert at the tail of the rightmost leaf.
4166 * This should also work against the root extent list for tree's with 0
4167 * depth. If we consider the root extent list to be the rightmost leaf node
4168 * then the logic here makes sense.
4170 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4171 struct ocfs2_extent_list *el,
4172 struct ocfs2_extent_rec *insert_rec)
4175 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4176 struct ocfs2_extent_rec *rec;
4178 insert->ins_appending = APPEND_NONE;
4180 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4182 if (!el->l_next_free_rec)
4183 goto set_tail_append;
4185 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4186 /* Were all records empty? */
4187 if (le16_to_cpu(el->l_next_free_rec) == 1)
4188 goto set_tail_append;
4191 i = le16_to_cpu(el->l_next_free_rec) - 1;
4192 rec = &el->l_recs[i];
4195 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4196 goto set_tail_append;
4201 insert->ins_appending = APPEND_TAIL;
4205 * Helper function called at the begining of an insert.
4207 * This computes a few things that are commonly used in the process of
4208 * inserting into the btree:
4209 * - Whether the new extent is contiguous with an existing one.
4210 * - The current tree depth.
4211 * - Whether the insert is an appending one.
4212 * - The total # of free records in the tree.
4214 * All of the information is stored on the ocfs2_insert_type
4217 static int ocfs2_figure_insert_type(struct inode *inode,
4218 struct ocfs2_extent_tree *et,
4219 struct buffer_head **last_eb_bh,
4220 struct ocfs2_extent_rec *insert_rec,
4222 struct ocfs2_insert_type *insert)
4225 struct ocfs2_extent_block *eb;
4226 struct ocfs2_extent_list *el;
4227 struct ocfs2_path *path = NULL;
4228 struct buffer_head *bh = NULL;
4230 insert->ins_split = SPLIT_NONE;
4232 el = et->et_root_el;
4233 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4235 if (el->l_tree_depth) {
4237 * If we have tree depth, we read in the
4238 * rightmost extent block ahead of time as
4239 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4240 * may want it later.
4242 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4243 ocfs2_et_get_last_eb_blk(et), &bh,
4244 OCFS2_BH_CACHED, inode);
4249 eb = (struct ocfs2_extent_block *) bh->b_data;
4254 * Unless we have a contiguous insert, we'll need to know if
4255 * there is room left in our allocation tree for another
4258 * XXX: This test is simplistic, we can search for empty
4259 * extent records too.
4261 *free_records = le16_to_cpu(el->l_count) -
4262 le16_to_cpu(el->l_next_free_rec);
4264 if (!insert->ins_tree_depth) {
4265 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4266 ocfs2_figure_appending_type(insert, el, insert_rec);
4270 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4278 * In the case that we're inserting past what the tree
4279 * currently accounts for, ocfs2_find_path() will return for
4280 * us the rightmost tree path. This is accounted for below in
4281 * the appending code.
4283 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4289 el = path_leaf_el(path);
4292 * Now that we have the path, there's two things we want to determine:
4293 * 1) Contiguousness (also set contig_index if this is so)
4295 * 2) Are we doing an append? We can trivially break this up
4296 * into two types of appends: simple record append, or a
4297 * rotate inside the tail leaf.
4299 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4302 * The insert code isn't quite ready to deal with all cases of
4303 * left contiguousness. Specifically, if it's an insert into
4304 * the 1st record in a leaf, it will require the adjustment of
4305 * cluster count on the last record of the path directly to it's
4306 * left. For now, just catch that case and fool the layers
4307 * above us. This works just fine for tree_depth == 0, which
4308 * is why we allow that above.
4310 if (insert->ins_contig == CONTIG_LEFT &&
4311 insert->ins_contig_index == 0)
4312 insert->ins_contig = CONTIG_NONE;
4315 * Ok, so we can simply compare against last_eb to figure out
4316 * whether the path doesn't exist. This will only happen in
4317 * the case that we're doing a tail append, so maybe we can
4318 * take advantage of that information somehow.
4320 if (ocfs2_et_get_last_eb_blk(et) ==
4321 path_leaf_bh(path)->b_blocknr) {
4323 * Ok, ocfs2_find_path() returned us the rightmost
4324 * tree path. This might be an appending insert. There are
4326 * 1) We're doing a true append at the tail:
4327 * -This might even be off the end of the leaf
4328 * 2) We're "appending" by rotating in the tail
4330 ocfs2_figure_appending_type(insert, el, insert_rec);
4334 ocfs2_free_path(path);
4344 * Insert an extent into an inode btree.
4346 * The caller needs to update fe->i_clusters
4348 static int ocfs2_insert_extent(struct ocfs2_super *osb,
4350 struct inode *inode,
4351 struct buffer_head *root_bh,
4356 struct ocfs2_alloc_context *meta_ac,
4357 struct ocfs2_extent_tree *et)
4360 int uninitialized_var(free_records);
4361 struct buffer_head *last_eb_bh = NULL;
4362 struct ocfs2_insert_type insert = {0, };
4363 struct ocfs2_extent_rec rec;
4365 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
4367 mlog(0, "add %u clusters at position %u to inode %llu\n",
4368 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4370 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
4371 (OCFS2_I(inode)->ip_clusters != cpos),
4372 "Device %s, asking for sparse allocation: inode %llu, "
4373 "cpos %u, clusters %u\n",
4375 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
4376 OCFS2_I(inode)->ip_clusters);
4378 memset(&rec, 0, sizeof(rec));
4379 rec.e_cpos = cpu_to_le32(cpos);
4380 rec.e_blkno = cpu_to_le64(start_blk);
4381 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4382 rec.e_flags = flags;
4384 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4385 &free_records, &insert);
4391 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4392 "Insert.contig_index: %d, Insert.free_records: %d, "
4393 "Insert.tree_depth: %d\n",
4394 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4395 free_records, insert.ins_tree_depth);
4397 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4398 status = ocfs2_grow_tree(inode, handle, et,
4399 &insert.ins_tree_depth, &last_eb_bh,
4407 /* Finally, we can add clusters. This might rotate the tree for us. */
4408 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4411 else if (et->et_type == OCFS2_DINODE_EXTENT)
4412 ocfs2_extent_map_insert_rec(inode, &rec);
4422 int ocfs2_dinode_insert_extent(struct ocfs2_super *osb,
4424 struct inode *inode,
4425 struct buffer_head *root_bh,
4430 struct ocfs2_alloc_context *meta_ac)
4433 struct ocfs2_extent_tree et;
4435 ocfs2_get_extent_tree(&et, inode, root_bh, OCFS2_DINODE_EXTENT,
4437 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4438 cpos, start_blk, new_clusters,
4439 flags, meta_ac, &et);
4440 ocfs2_put_extent_tree(&et);
4445 int ocfs2_xattr_value_insert_extent(struct ocfs2_super *osb,
4447 struct inode *inode,
4448 struct buffer_head *root_bh,
4453 struct ocfs2_alloc_context *meta_ac,
4457 struct ocfs2_extent_tree et;
4459 ocfs2_get_extent_tree(&et, inode, root_bh,
4460 OCFS2_XATTR_VALUE_EXTENT, obj);
4461 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4462 cpos, start_blk, new_clusters,
4463 flags, meta_ac, &et);
4464 ocfs2_put_extent_tree(&et);
4469 int ocfs2_xattr_tree_insert_extent(struct ocfs2_super *osb,
4471 struct inode *inode,
4472 struct buffer_head *root_bh,
4477 struct ocfs2_alloc_context *meta_ac)
4480 struct ocfs2_extent_tree et;
4482 ocfs2_get_extent_tree(&et, inode, root_bh, OCFS2_XATTR_TREE_EXTENT,
4484 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4485 cpos, start_blk, new_clusters,
4486 flags, meta_ac, &et);
4487 ocfs2_put_extent_tree(&et);
4493 * Allcate and add clusters into the extent b-tree.
4494 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4495 * The extent b-tree's root is root_el and it should be in root_bh, and
4496 * it is not limited to the file storage. Any extent tree can use this
4497 * function if it implements the proper ocfs2_extent_tree.
4499 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4500 struct inode *inode,
4501 u32 *logical_offset,
4502 u32 clusters_to_add,
4504 struct buffer_head *root_bh,
4505 struct ocfs2_extent_list *root_el,
4507 struct ocfs2_alloc_context *data_ac,
4508 struct ocfs2_alloc_context *meta_ac,
4509 enum ocfs2_alloc_restarted *reason_ret,
4510 enum ocfs2_extent_tree_type type,
4515 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4516 u32 bit_off, num_bits;
4520 BUG_ON(!clusters_to_add);
4523 flags = OCFS2_EXT_UNWRITTEN;
4525 free_extents = ocfs2_num_free_extents(osb, inode, root_bh, type,
4527 if (free_extents < 0) {
4528 status = free_extents;
4533 /* there are two cases which could cause us to EAGAIN in the
4534 * we-need-more-metadata case:
4535 * 1) we haven't reserved *any*
4536 * 2) we are so fragmented, we've needed to add metadata too
4538 if (!free_extents && !meta_ac) {
4539 mlog(0, "we haven't reserved any metadata!\n");
4541 reason = RESTART_META;
4543 } else if ((!free_extents)
4544 && (ocfs2_alloc_context_bits_left(meta_ac)
4545 < ocfs2_extend_meta_needed(root_el))) {
4546 mlog(0, "filesystem is really fragmented...\n");
4548 reason = RESTART_META;
4552 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4553 clusters_to_add, &bit_off, &num_bits);
4555 if (status != -ENOSPC)
4560 BUG_ON(num_bits > clusters_to_add);
4562 /* reserve our write early -- insert_extent may update the inode */
4563 status = ocfs2_journal_access(handle, inode, root_bh,
4564 OCFS2_JOURNAL_ACCESS_WRITE);
4570 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4571 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4572 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4573 if (type == OCFS2_DINODE_EXTENT)
4574 status = ocfs2_dinode_insert_extent(osb, handle, inode, root_bh,
4575 *logical_offset, block,
4576 num_bits, flags, meta_ac);
4577 else if (type == OCFS2_XATTR_TREE_EXTENT)
4578 status = ocfs2_xattr_tree_insert_extent(osb, handle,
4581 block, num_bits, flags,
4584 status = ocfs2_xattr_value_insert_extent(osb, handle,
4587 block, num_bits, flags,
4594 status = ocfs2_journal_dirty(handle, root_bh);
4600 clusters_to_add -= num_bits;
4601 *logical_offset += num_bits;
4603 if (clusters_to_add) {
4604 mlog(0, "need to alloc once more, wanted = %u\n",
4607 reason = RESTART_TRANS;
4613 *reason_ret = reason;
4617 static void ocfs2_make_right_split_rec(struct super_block *sb,
4618 struct ocfs2_extent_rec *split_rec,
4620 struct ocfs2_extent_rec *rec)
4622 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4623 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4625 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4627 split_rec->e_cpos = cpu_to_le32(cpos);
4628 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4630 split_rec->e_blkno = rec->e_blkno;
4631 le64_add_cpu(&split_rec->e_blkno,
4632 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4634 split_rec->e_flags = rec->e_flags;
4637 static int ocfs2_split_and_insert(struct inode *inode,
4639 struct ocfs2_path *path,
4640 struct ocfs2_extent_tree *et,
4641 struct buffer_head **last_eb_bh,
4643 struct ocfs2_extent_rec *orig_split_rec,
4644 struct ocfs2_alloc_context *meta_ac)
4647 unsigned int insert_range, rec_range, do_leftright = 0;
4648 struct ocfs2_extent_rec tmprec;
4649 struct ocfs2_extent_list *rightmost_el;
4650 struct ocfs2_extent_rec rec;
4651 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4652 struct ocfs2_insert_type insert;
4653 struct ocfs2_extent_block *eb;
4657 * Store a copy of the record on the stack - it might move
4658 * around as the tree is manipulated below.
4660 rec = path_leaf_el(path)->l_recs[split_index];
4662 rightmost_el = et->et_root_el;
4664 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4666 BUG_ON(!(*last_eb_bh));
4667 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4668 rightmost_el = &eb->h_list;
4671 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4672 le16_to_cpu(rightmost_el->l_count)) {
4673 ret = ocfs2_grow_tree(inode, handle, et,
4674 &depth, last_eb_bh, meta_ac);
4681 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4682 insert.ins_appending = APPEND_NONE;
4683 insert.ins_contig = CONTIG_NONE;
4684 insert.ins_tree_depth = depth;
4686 insert_range = le32_to_cpu(split_rec.e_cpos) +
4687 le16_to_cpu(split_rec.e_leaf_clusters);
4688 rec_range = le32_to_cpu(rec.e_cpos) +
4689 le16_to_cpu(rec.e_leaf_clusters);
4691 if (split_rec.e_cpos == rec.e_cpos) {
4692 insert.ins_split = SPLIT_LEFT;
4693 } else if (insert_range == rec_range) {
4694 insert.ins_split = SPLIT_RIGHT;
4697 * Left/right split. We fake this as a right split
4698 * first and then make a second pass as a left split.
4700 insert.ins_split = SPLIT_RIGHT;
4702 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4707 BUG_ON(do_leftright);
4711 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4717 if (do_leftright == 1) {
4719 struct ocfs2_extent_list *el;
4722 split_rec = *orig_split_rec;
4724 ocfs2_reinit_path(path, 1);
4726 cpos = le32_to_cpu(split_rec.e_cpos);
4727 ret = ocfs2_find_path(inode, path, cpos);
4733 el = path_leaf_el(path);
4734 split_index = ocfs2_search_extent_list(el, cpos);
4743 * Mark part or all of the extent record at split_index in the leaf
4744 * pointed to by path as written. This removes the unwritten
4747 * Care is taken to handle contiguousness so as to not grow the tree.
4749 * meta_ac is not strictly necessary - we only truly need it if growth
4750 * of the tree is required. All other cases will degrade into a less
4751 * optimal tree layout.
4753 * last_eb_bh should be the rightmost leaf block for any extent
4754 * btree. Since a split may grow the tree or a merge might shrink it,
4755 * the caller cannot trust the contents of that buffer after this call.
4757 * This code is optimized for readability - several passes might be
4758 * made over certain portions of the tree. All of those blocks will
4759 * have been brought into cache (and pinned via the journal), so the
4760 * extra overhead is not expressed in terms of disk reads.
4762 static int __ocfs2_mark_extent_written(struct inode *inode,
4763 struct ocfs2_extent_tree *et,
4765 struct ocfs2_path *path,
4767 struct ocfs2_extent_rec *split_rec,
4768 struct ocfs2_alloc_context *meta_ac,
4769 struct ocfs2_cached_dealloc_ctxt *dealloc)
4772 struct ocfs2_extent_list *el = path_leaf_el(path);
4773 struct buffer_head *last_eb_bh = NULL;
4774 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4775 struct ocfs2_merge_ctxt ctxt;
4776 struct ocfs2_extent_list *rightmost_el;
4778 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
4784 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4785 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4786 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4792 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
4797 * The core merge / split code wants to know how much room is
4798 * left in this inodes allocation tree, so we pass the
4799 * rightmost extent list.
4801 if (path->p_tree_depth) {
4802 struct ocfs2_extent_block *eb;
4804 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4805 ocfs2_et_get_last_eb_blk(et),
4806 &last_eb_bh, OCFS2_BH_CACHED, inode);
4812 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4813 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4814 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4819 rightmost_el = &eb->h_list;
4821 rightmost_el = path_root_el(path);
4823 if (rec->e_cpos == split_rec->e_cpos &&
4824 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4825 ctxt.c_split_covers_rec = 1;
4827 ctxt.c_split_covers_rec = 0;
4829 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4831 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4832 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4833 ctxt.c_split_covers_rec);
4835 if (ctxt.c_contig_type == CONTIG_NONE) {
4836 if (ctxt.c_split_covers_rec)
4837 el->l_recs[split_index] = *split_rec;
4839 ret = ocfs2_split_and_insert(inode, handle, path, et,
4840 &last_eb_bh, split_index,
4841 split_rec, meta_ac);
4845 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4846 split_index, split_rec,
4847 dealloc, &ctxt, et);
4858 * Mark the already-existing extent at cpos as written for len clusters.
4860 * If the existing extent is larger than the request, initiate a
4861 * split. An attempt will be made at merging with adjacent extents.
4863 * The caller is responsible for passing down meta_ac if we'll need it.
4865 int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh,
4866 handle_t *handle, u32 cpos, u32 len, u32 phys,
4867 struct ocfs2_alloc_context *meta_ac,
4868 struct ocfs2_cached_dealloc_ctxt *dealloc,
4869 enum ocfs2_extent_tree_type et_type,
4873 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4874 struct ocfs2_extent_rec split_rec;
4875 struct ocfs2_path *left_path = NULL;
4876 struct ocfs2_extent_list *el;
4877 struct ocfs2_extent_tree et;
4879 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4880 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4882 ocfs2_get_extent_tree(&et, inode, root_bh, et_type, obj);
4884 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4885 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4886 "that are being written to, but the feature bit "
4887 "is not set in the super block.",
4888 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4894 * XXX: This should be fixed up so that we just re-insert the
4895 * next extent records.
4897 if (et_type == OCFS2_DINODE_EXTENT)
4898 ocfs2_extent_map_trunc(inode, 0);
4900 left_path = ocfs2_new_path(et.et_root_bh, et.et_root_el);
4907 ret = ocfs2_find_path(inode, left_path, cpos);
4912 el = path_leaf_el(left_path);
4914 index = ocfs2_search_extent_list(el, cpos);
4915 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4916 ocfs2_error(inode->i_sb,
4917 "Inode %llu has an extent at cpos %u which can no "
4918 "longer be found.\n",
4919 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4924 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4925 split_rec.e_cpos = cpu_to_le32(cpos);
4926 split_rec.e_leaf_clusters = cpu_to_le16(len);
4927 split_rec.e_blkno = cpu_to_le64(start_blkno);
4928 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4929 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4931 ret = __ocfs2_mark_extent_written(inode, &et, handle, left_path,
4932 index, &split_rec, meta_ac,
4938 ocfs2_free_path(left_path);
4939 ocfs2_put_extent_tree(&et);
4943 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
4944 handle_t *handle, struct ocfs2_path *path,
4945 int index, u32 new_range,
4946 struct ocfs2_alloc_context *meta_ac)
4948 int ret, depth, credits = handle->h_buffer_credits;
4949 struct buffer_head *last_eb_bh = NULL;
4950 struct ocfs2_extent_block *eb;
4951 struct ocfs2_extent_list *rightmost_el, *el;
4952 struct ocfs2_extent_rec split_rec;
4953 struct ocfs2_extent_rec *rec;
4954 struct ocfs2_insert_type insert;
4957 * Setup the record to split before we grow the tree.
4959 el = path_leaf_el(path);
4960 rec = &el->l_recs[index];
4961 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4963 depth = path->p_tree_depth;
4965 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4966 ocfs2_et_get_last_eb_blk(et),
4967 &last_eb_bh, OCFS2_BH_CACHED, inode);
4973 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4974 rightmost_el = &eb->h_list;
4976 rightmost_el = path_leaf_el(path);
4978 credits += path->p_tree_depth +
4979 ocfs2_extend_meta_needed(et->et_root_el);
4980 ret = ocfs2_extend_trans(handle, credits);
4986 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4987 le16_to_cpu(rightmost_el->l_count)) {
4988 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
4996 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4997 insert.ins_appending = APPEND_NONE;
4998 insert.ins_contig = CONTIG_NONE;
4999 insert.ins_split = SPLIT_RIGHT;
5000 insert.ins_tree_depth = depth;
5002 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
5011 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5012 struct ocfs2_path *path, int index,
5013 struct ocfs2_cached_dealloc_ctxt *dealloc,
5015 struct ocfs2_extent_tree *et)
5018 u32 left_cpos, rec_range, trunc_range;
5019 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5020 struct super_block *sb = inode->i_sb;
5021 struct ocfs2_path *left_path = NULL;
5022 struct ocfs2_extent_list *el = path_leaf_el(path);
5023 struct ocfs2_extent_rec *rec;
5024 struct ocfs2_extent_block *eb;
5026 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5027 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5036 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5037 path->p_tree_depth) {
5039 * Check whether this is the rightmost tree record. If
5040 * we remove all of this record or part of its right
5041 * edge then an update of the record lengths above it
5044 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5045 if (eb->h_next_leaf_blk == 0)
5046 is_rightmost_tree_rec = 1;
5049 rec = &el->l_recs[index];
5050 if (index == 0 && path->p_tree_depth &&
5051 le32_to_cpu(rec->e_cpos) == cpos) {
5053 * Changing the leftmost offset (via partial or whole
5054 * record truncate) of an interior (or rightmost) path
5055 * means we have to update the subtree that is formed
5056 * by this leaf and the one to it's left.
5058 * There are two cases we can skip:
5059 * 1) Path is the leftmost one in our inode tree.
5060 * 2) The leaf is rightmost and will be empty after
5061 * we remove the extent record - the rotate code
5062 * knows how to update the newly formed edge.
5065 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5072 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5073 left_path = ocfs2_new_path(path_root_bh(path),
5074 path_root_el(path));
5081 ret = ocfs2_find_path(inode, left_path, left_cpos);
5089 ret = ocfs2_extend_rotate_transaction(handle, 0,
5090 handle->h_buffer_credits,
5097 ret = ocfs2_journal_access_path(inode, handle, path);
5103 ret = ocfs2_journal_access_path(inode, handle, left_path);
5109 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5110 trunc_range = cpos + len;
5112 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5115 memset(rec, 0, sizeof(*rec));
5116 ocfs2_cleanup_merge(el, index);
5119 next_free = le16_to_cpu(el->l_next_free_rec);
5120 if (is_rightmost_tree_rec && next_free > 1) {
5122 * We skip the edge update if this path will
5123 * be deleted by the rotate code.
5125 rec = &el->l_recs[next_free - 1];
5126 ocfs2_adjust_rightmost_records(inode, handle, path,
5129 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5130 /* Remove leftmost portion of the record. */
5131 le32_add_cpu(&rec->e_cpos, len);
5132 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5133 le16_add_cpu(&rec->e_leaf_clusters, -len);
5134 } else if (rec_range == trunc_range) {
5135 /* Remove rightmost portion of the record */
5136 le16_add_cpu(&rec->e_leaf_clusters, -len);
5137 if (is_rightmost_tree_rec)
5138 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5140 /* Caller should have trapped this. */
5141 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5142 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5143 le32_to_cpu(rec->e_cpos),
5144 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5151 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5152 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5156 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5158 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5165 ocfs2_free_path(left_path);
5169 int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
5170 u32 cpos, u32 len, handle_t *handle,
5171 struct ocfs2_alloc_context *meta_ac,
5172 struct ocfs2_cached_dealloc_ctxt *dealloc,
5173 enum ocfs2_extent_tree_type et_type,
5177 u32 rec_range, trunc_range;
5178 struct ocfs2_extent_rec *rec;
5179 struct ocfs2_extent_list *el;
5180 struct ocfs2_path *path = NULL;
5181 struct ocfs2_extent_tree et;
5183 ocfs2_get_extent_tree(&et, inode, root_bh, et_type, obj);
5185 ocfs2_extent_map_trunc(inode, 0);
5187 path = ocfs2_new_path(et.et_root_bh, et.et_root_el);
5194 ret = ocfs2_find_path(inode, path, cpos);
5200 el = path_leaf_el(path);
5201 index = ocfs2_search_extent_list(el, cpos);
5202 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5203 ocfs2_error(inode->i_sb,
5204 "Inode %llu has an extent at cpos %u which can no "
5205 "longer be found.\n",
5206 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5212 * We have 3 cases of extent removal:
5213 * 1) Range covers the entire extent rec
5214 * 2) Range begins or ends on one edge of the extent rec
5215 * 3) Range is in the middle of the extent rec (no shared edges)
5217 * For case 1 we remove the extent rec and left rotate to
5220 * For case 2 we just shrink the existing extent rec, with a
5221 * tree update if the shrinking edge is also the edge of an
5224 * For case 3 we do a right split to turn the extent rec into
5225 * something case 2 can handle.
5227 rec = &el->l_recs[index];
5228 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5229 trunc_range = cpos + len;
5231 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5233 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5234 "(cpos %u, len %u)\n",
5235 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5236 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5238 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5239 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5246 ret = ocfs2_split_tree(inode, &et, handle, path, index,
5247 trunc_range, meta_ac);
5254 * The split could have manipulated the tree enough to
5255 * move the record location, so we have to look for it again.
5257 ocfs2_reinit_path(path, 1);
5259 ret = ocfs2_find_path(inode, path, cpos);
5265 el = path_leaf_el(path);
5266 index = ocfs2_search_extent_list(el, cpos);
5267 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5268 ocfs2_error(inode->i_sb,
5269 "Inode %llu: split at cpos %u lost record.",
5270 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5277 * Double check our values here. If anything is fishy,
5278 * it's easier to catch it at the top level.
5280 rec = &el->l_recs[index];
5281 rec_range = le32_to_cpu(rec->e_cpos) +
5282 ocfs2_rec_clusters(el, rec);
5283 if (rec_range != trunc_range) {
5284 ocfs2_error(inode->i_sb,
5285 "Inode %llu: error after split at cpos %u"
5286 "trunc len %u, existing record is (%u,%u)",
5287 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5288 cpos, len, le32_to_cpu(rec->e_cpos),
5289 ocfs2_rec_clusters(el, rec));
5294 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5303 ocfs2_free_path(path);
5304 ocfs2_put_extent_tree(&et);
5308 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5310 struct buffer_head *tl_bh = osb->osb_tl_bh;
5311 struct ocfs2_dinode *di;
5312 struct ocfs2_truncate_log *tl;
5314 di = (struct ocfs2_dinode *) tl_bh->b_data;
5315 tl = &di->id2.i_dealloc;
5317 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5318 "slot %d, invalid truncate log parameters: used = "
5319 "%u, count = %u\n", osb->slot_num,
5320 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5321 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5324 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5325 unsigned int new_start)
5327 unsigned int tail_index;
5328 unsigned int current_tail;
5330 /* No records, nothing to coalesce */
5331 if (!le16_to_cpu(tl->tl_used))
5334 tail_index = le16_to_cpu(tl->tl_used) - 1;
5335 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5336 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5338 return current_tail == new_start;
5341 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5344 unsigned int num_clusters)
5347 unsigned int start_cluster, tl_count;
5348 struct inode *tl_inode = osb->osb_tl_inode;
5349 struct buffer_head *tl_bh = osb->osb_tl_bh;
5350 struct ocfs2_dinode *di;
5351 struct ocfs2_truncate_log *tl;
5353 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5354 (unsigned long long)start_blk, num_clusters);
5356 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5358 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5360 di = (struct ocfs2_dinode *) tl_bh->b_data;
5361 tl = &di->id2.i_dealloc;
5362 if (!OCFS2_IS_VALID_DINODE(di)) {
5363 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5368 tl_count = le16_to_cpu(tl->tl_count);
5369 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5371 "Truncate record count on #%llu invalid "
5372 "wanted %u, actual %u\n",
5373 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5374 ocfs2_truncate_recs_per_inode(osb->sb),
5375 le16_to_cpu(tl->tl_count));
5377 /* Caller should have known to flush before calling us. */
5378 index = le16_to_cpu(tl->tl_used);
5379 if (index >= tl_count) {
5385 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5386 OCFS2_JOURNAL_ACCESS_WRITE);
5392 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5393 "%llu (index = %d)\n", num_clusters, start_cluster,
5394 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5396 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5398 * Move index back to the record we are coalescing with.
5399 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5403 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5404 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5405 index, le32_to_cpu(tl->tl_recs[index].t_start),
5408 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5409 tl->tl_used = cpu_to_le16(index + 1);
5411 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5413 status = ocfs2_journal_dirty(handle, tl_bh);
5424 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5426 struct inode *data_alloc_inode,
5427 struct buffer_head *data_alloc_bh)
5431 unsigned int num_clusters;
5433 struct ocfs2_truncate_rec rec;
5434 struct ocfs2_dinode *di;
5435 struct ocfs2_truncate_log *tl;
5436 struct inode *tl_inode = osb->osb_tl_inode;
5437 struct buffer_head *tl_bh = osb->osb_tl_bh;
5441 di = (struct ocfs2_dinode *) tl_bh->b_data;
5442 tl = &di->id2.i_dealloc;
5443 i = le16_to_cpu(tl->tl_used) - 1;
5445 /* Caller has given us at least enough credits to
5446 * update the truncate log dinode */
5447 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5448 OCFS2_JOURNAL_ACCESS_WRITE);
5454 tl->tl_used = cpu_to_le16(i);
5456 status = ocfs2_journal_dirty(handle, tl_bh);
5462 /* TODO: Perhaps we can calculate the bulk of the
5463 * credits up front rather than extending like
5465 status = ocfs2_extend_trans(handle,
5466 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5472 rec = tl->tl_recs[i];
5473 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5474 le32_to_cpu(rec.t_start));
5475 num_clusters = le32_to_cpu(rec.t_clusters);
5477 /* if start_blk is not set, we ignore the record as
5480 mlog(0, "free record %d, start = %u, clusters = %u\n",
5481 i, le32_to_cpu(rec.t_start), num_clusters);
5483 status = ocfs2_free_clusters(handle, data_alloc_inode,
5484 data_alloc_bh, start_blk,
5499 /* Expects you to already be holding tl_inode->i_mutex */
5500 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5503 unsigned int num_to_flush;
5505 struct inode *tl_inode = osb->osb_tl_inode;
5506 struct inode *data_alloc_inode = NULL;
5507 struct buffer_head *tl_bh = osb->osb_tl_bh;
5508 struct buffer_head *data_alloc_bh = NULL;
5509 struct ocfs2_dinode *di;
5510 struct ocfs2_truncate_log *tl;
5514 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5516 di = (struct ocfs2_dinode *) tl_bh->b_data;
5517 tl = &di->id2.i_dealloc;
5518 if (!OCFS2_IS_VALID_DINODE(di)) {
5519 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5524 num_to_flush = le16_to_cpu(tl->tl_used);
5525 mlog(0, "Flush %u records from truncate log #%llu\n",
5526 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5527 if (!num_to_flush) {
5532 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5533 GLOBAL_BITMAP_SYSTEM_INODE,
5534 OCFS2_INVALID_SLOT);
5535 if (!data_alloc_inode) {
5537 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5541 mutex_lock(&data_alloc_inode->i_mutex);
5543 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5549 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5550 if (IS_ERR(handle)) {
5551 status = PTR_ERR(handle);
5556 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5561 ocfs2_commit_trans(osb, handle);
5564 brelse(data_alloc_bh);
5565 ocfs2_inode_unlock(data_alloc_inode, 1);
5568 mutex_unlock(&data_alloc_inode->i_mutex);
5569 iput(data_alloc_inode);
5576 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5579 struct inode *tl_inode = osb->osb_tl_inode;
5581 mutex_lock(&tl_inode->i_mutex);
5582 status = __ocfs2_flush_truncate_log(osb);
5583 mutex_unlock(&tl_inode->i_mutex);
5588 static void ocfs2_truncate_log_worker(struct work_struct *work)
5591 struct ocfs2_super *osb =
5592 container_of(work, struct ocfs2_super,
5593 osb_truncate_log_wq.work);
5597 status = ocfs2_flush_truncate_log(osb);
5601 ocfs2_init_inode_steal_slot(osb);
5606 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5607 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5610 if (osb->osb_tl_inode) {
5611 /* We want to push off log flushes while truncates are
5614 cancel_delayed_work(&osb->osb_truncate_log_wq);
5616 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5617 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5621 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5623 struct inode **tl_inode,
5624 struct buffer_head **tl_bh)
5627 struct inode *inode = NULL;
5628 struct buffer_head *bh = NULL;
5630 inode = ocfs2_get_system_file_inode(osb,
5631 TRUNCATE_LOG_SYSTEM_INODE,
5635 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5639 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
5640 OCFS2_BH_CACHED, inode);
5654 /* called during the 1st stage of node recovery. we stamp a clean
5655 * truncate log and pass back a copy for processing later. if the
5656 * truncate log does not require processing, a *tl_copy is set to
5658 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5660 struct ocfs2_dinode **tl_copy)
5663 struct inode *tl_inode = NULL;
5664 struct buffer_head *tl_bh = NULL;
5665 struct ocfs2_dinode *di;
5666 struct ocfs2_truncate_log *tl;
5670 mlog(0, "recover truncate log from slot %d\n", slot_num);
5672 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5678 di = (struct ocfs2_dinode *) tl_bh->b_data;
5679 tl = &di->id2.i_dealloc;
5680 if (!OCFS2_IS_VALID_DINODE(di)) {
5681 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
5686 if (le16_to_cpu(tl->tl_used)) {
5687 mlog(0, "We'll have %u logs to recover\n",
5688 le16_to_cpu(tl->tl_used));
5690 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5697 /* Assuming the write-out below goes well, this copy
5698 * will be passed back to recovery for processing. */
5699 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5701 /* All we need to do to clear the truncate log is set
5705 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5718 if (status < 0 && (*tl_copy)) {
5727 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5728 struct ocfs2_dinode *tl_copy)
5732 unsigned int clusters, num_recs, start_cluster;
5735 struct inode *tl_inode = osb->osb_tl_inode;
5736 struct ocfs2_truncate_log *tl;
5740 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5741 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5745 tl = &tl_copy->id2.i_dealloc;
5746 num_recs = le16_to_cpu(tl->tl_used);
5747 mlog(0, "cleanup %u records from %llu\n", num_recs,
5748 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
5750 mutex_lock(&tl_inode->i_mutex);
5751 for(i = 0; i < num_recs; i++) {
5752 if (ocfs2_truncate_log_needs_flush(osb)) {
5753 status = __ocfs2_flush_truncate_log(osb);
5760 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5761 if (IS_ERR(handle)) {
5762 status = PTR_ERR(handle);
5767 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5768 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5769 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5771 status = ocfs2_truncate_log_append(osb, handle,
5772 start_blk, clusters);
5773 ocfs2_commit_trans(osb, handle);
5781 mutex_unlock(&tl_inode->i_mutex);
5787 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5790 struct inode *tl_inode = osb->osb_tl_inode;
5795 cancel_delayed_work(&osb->osb_truncate_log_wq);
5796 flush_workqueue(ocfs2_wq);
5798 status = ocfs2_flush_truncate_log(osb);
5802 brelse(osb->osb_tl_bh);
5803 iput(osb->osb_tl_inode);
5809 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5812 struct inode *tl_inode = NULL;
5813 struct buffer_head *tl_bh = NULL;
5817 status = ocfs2_get_truncate_log_info(osb,
5824 /* ocfs2_truncate_log_shutdown keys on the existence of
5825 * osb->osb_tl_inode so we don't set any of the osb variables
5826 * until we're sure all is well. */
5827 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5828 ocfs2_truncate_log_worker);
5829 osb->osb_tl_bh = tl_bh;
5830 osb->osb_tl_inode = tl_inode;
5837 * Delayed de-allocation of suballocator blocks.
5839 * Some sets of block de-allocations might involve multiple suballocator inodes.
5841 * The locking for this can get extremely complicated, especially when
5842 * the suballocator inodes to delete from aren't known until deep
5843 * within an unrelated codepath.
5845 * ocfs2_extent_block structures are a good example of this - an inode
5846 * btree could have been grown by any number of nodes each allocating
5847 * out of their own suballoc inode.
5849 * These structures allow the delay of block de-allocation until a
5850 * later time, when locking of multiple cluster inodes won't cause
5855 * Describes a single block free from a suballocator
5857 struct ocfs2_cached_block_free {
5858 struct ocfs2_cached_block_free *free_next;
5860 unsigned int free_bit;
5863 struct ocfs2_per_slot_free_list {
5864 struct ocfs2_per_slot_free_list *f_next_suballocator;
5867 struct ocfs2_cached_block_free *f_first;
5870 static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5873 struct ocfs2_cached_block_free *head)
5878 struct inode *inode;
5879 struct buffer_head *di_bh = NULL;
5880 struct ocfs2_cached_block_free *tmp;
5882 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5889 mutex_lock(&inode->i_mutex);
5891 ret = ocfs2_inode_lock(inode, &di_bh, 1);
5897 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5898 if (IS_ERR(handle)) {
5899 ret = PTR_ERR(handle);
5905 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5907 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5908 head->free_bit, (unsigned long long)head->free_blk);
5910 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5911 head->free_bit, bg_blkno, 1);
5917 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5924 head = head->free_next;
5929 ocfs2_commit_trans(osb, handle);
5932 ocfs2_inode_unlock(inode, 1);
5935 mutex_unlock(&inode->i_mutex);
5939 /* Premature exit may have left some dangling items. */
5941 head = head->free_next;
5948 int ocfs2_run_deallocs(struct ocfs2_super *osb,
5949 struct ocfs2_cached_dealloc_ctxt *ctxt)
5952 struct ocfs2_per_slot_free_list *fl;
5957 while (ctxt->c_first_suballocator) {
5958 fl = ctxt->c_first_suballocator;
5961 mlog(0, "Free items: (type %u, slot %d)\n",
5962 fl->f_inode_type, fl->f_slot);
5963 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
5964 fl->f_slot, fl->f_first);
5971 ctxt->c_first_suballocator = fl->f_next_suballocator;
5978 static struct ocfs2_per_slot_free_list *
5979 ocfs2_find_per_slot_free_list(int type,
5981 struct ocfs2_cached_dealloc_ctxt *ctxt)
5983 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
5986 if (fl->f_inode_type == type && fl->f_slot == slot)
5989 fl = fl->f_next_suballocator;
5992 fl = kmalloc(sizeof(*fl), GFP_NOFS);
5994 fl->f_inode_type = type;
5997 fl->f_next_suballocator = ctxt->c_first_suballocator;
5999 ctxt->c_first_suballocator = fl;
6004 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6005 int type, int slot, u64 blkno,
6009 struct ocfs2_per_slot_free_list *fl;
6010 struct ocfs2_cached_block_free *item;
6012 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6019 item = kmalloc(sizeof(*item), GFP_NOFS);
6026 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6027 type, slot, bit, (unsigned long long)blkno);
6029 item->free_blk = blkno;
6030 item->free_bit = bit;
6031 item->free_next = fl->f_first;
6040 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6041 struct ocfs2_extent_block *eb)
6043 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6044 le16_to_cpu(eb->h_suballoc_slot),
6045 le64_to_cpu(eb->h_blkno),
6046 le16_to_cpu(eb->h_suballoc_bit));
6049 /* This function will figure out whether the currently last extent
6050 * block will be deleted, and if it will, what the new last extent
6051 * block will be so we can update his h_next_leaf_blk field, as well
6052 * as the dinodes i_last_eb_blk */
6053 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6054 unsigned int clusters_to_del,
6055 struct ocfs2_path *path,
6056 struct buffer_head **new_last_eb)
6058 int next_free, ret = 0;
6060 struct ocfs2_extent_rec *rec;
6061 struct ocfs2_extent_block *eb;
6062 struct ocfs2_extent_list *el;
6063 struct buffer_head *bh = NULL;
6065 *new_last_eb = NULL;
6067 /* we have no tree, so of course, no last_eb. */
6068 if (!path->p_tree_depth)
6071 /* trunc to zero special case - this makes tree_depth = 0
6072 * regardless of what it is. */
6073 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6076 el = path_leaf_el(path);
6077 BUG_ON(!el->l_next_free_rec);
6080 * Make sure that this extent list will actually be empty
6081 * after we clear away the data. We can shortcut out if
6082 * there's more than one non-empty extent in the
6083 * list. Otherwise, a check of the remaining extent is
6086 next_free = le16_to_cpu(el->l_next_free_rec);
6088 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6092 /* We may have a valid extent in index 1, check it. */
6094 rec = &el->l_recs[1];
6097 * Fall through - no more nonempty extents, so we want
6098 * to delete this leaf.
6104 rec = &el->l_recs[0];
6109 * Check it we'll only be trimming off the end of this
6112 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6116 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6122 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6128 eb = (struct ocfs2_extent_block *) bh->b_data;
6130 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6131 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6137 get_bh(*new_last_eb);
6138 mlog(0, "returning block %llu, (cpos: %u)\n",
6139 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6147 * Trim some clusters off the rightmost edge of a tree. Only called
6150 * The caller needs to:
6151 * - start journaling of each path component.
6152 * - compute and fully set up any new last ext block
6154 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6155 handle_t *handle, struct ocfs2_truncate_context *tc,
6156 u32 clusters_to_del, u64 *delete_start)
6158 int ret, i, index = path->p_tree_depth;
6161 struct buffer_head *bh;
6162 struct ocfs2_extent_list *el;
6163 struct ocfs2_extent_rec *rec;
6167 while (index >= 0) {
6168 bh = path->p_node[index].bh;
6169 el = path->p_node[index].el;
6171 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6172 index, (unsigned long long)bh->b_blocknr);
6174 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6177 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6178 ocfs2_error(inode->i_sb,
6179 "Inode %lu has invalid ext. block %llu",
6181 (unsigned long long)bh->b_blocknr);
6187 i = le16_to_cpu(el->l_next_free_rec) - 1;
6188 rec = &el->l_recs[i];
6190 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6191 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6192 ocfs2_rec_clusters(el, rec),
6193 (unsigned long long)le64_to_cpu(rec->e_blkno),
6194 le16_to_cpu(el->l_next_free_rec));
6196 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6198 if (le16_to_cpu(el->l_tree_depth) == 0) {
6200 * If the leaf block contains a single empty
6201 * extent and no records, we can just remove
6204 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6206 sizeof(struct ocfs2_extent_rec));
6207 el->l_next_free_rec = cpu_to_le16(0);
6213 * Remove any empty extents by shifting things
6214 * left. That should make life much easier on
6215 * the code below. This condition is rare
6216 * enough that we shouldn't see a performance
6219 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6220 le16_add_cpu(&el->l_next_free_rec, -1);
6223 i < le16_to_cpu(el->l_next_free_rec); i++)
6224 el->l_recs[i] = el->l_recs[i + 1];
6226 memset(&el->l_recs[i], 0,
6227 sizeof(struct ocfs2_extent_rec));
6230 * We've modified our extent list. The
6231 * simplest way to handle this change
6232 * is to being the search from the
6235 goto find_tail_record;
6238 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6241 * We'll use "new_edge" on our way back up the
6242 * tree to know what our rightmost cpos is.
6244 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6245 new_edge += le32_to_cpu(rec->e_cpos);
6248 * The caller will use this to delete data blocks.
6250 *delete_start = le64_to_cpu(rec->e_blkno)
6251 + ocfs2_clusters_to_blocks(inode->i_sb,
6252 le16_to_cpu(rec->e_leaf_clusters));
6255 * If it's now empty, remove this record.
6257 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6259 sizeof(struct ocfs2_extent_rec));
6260 le16_add_cpu(&el->l_next_free_rec, -1);
6263 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6265 sizeof(struct ocfs2_extent_rec));
6266 le16_add_cpu(&el->l_next_free_rec, -1);
6271 /* Can this actually happen? */
6272 if (le16_to_cpu(el->l_next_free_rec) == 0)
6276 * We never actually deleted any clusters
6277 * because our leaf was empty. There's no
6278 * reason to adjust the rightmost edge then.
6283 rec->e_int_clusters = cpu_to_le32(new_edge);
6284 le32_add_cpu(&rec->e_int_clusters,
6285 -le32_to_cpu(rec->e_cpos));
6288 * A deleted child record should have been
6291 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6295 ret = ocfs2_journal_dirty(handle, bh);
6301 mlog(0, "extent list container %llu, after: record %d: "
6302 "(%u, %u, %llu), next = %u.\n",
6303 (unsigned long long)bh->b_blocknr, i,
6304 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6305 (unsigned long long)le64_to_cpu(rec->e_blkno),
6306 le16_to_cpu(el->l_next_free_rec));
6309 * We must be careful to only attempt delete of an
6310 * extent block (and not the root inode block).
6312 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6313 struct ocfs2_extent_block *eb =
6314 (struct ocfs2_extent_block *)bh->b_data;
6317 * Save this for use when processing the
6320 deleted_eb = le64_to_cpu(eb->h_blkno);
6322 mlog(0, "deleting this extent block.\n");
6324 ocfs2_remove_from_cache(inode, bh);
6326 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6327 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6328 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6330 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6331 /* An error here is not fatal. */
6346 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6347 unsigned int clusters_to_del,
6348 struct inode *inode,
6349 struct buffer_head *fe_bh,
6351 struct ocfs2_truncate_context *tc,
6352 struct ocfs2_path *path)
6355 struct ocfs2_dinode *fe;
6356 struct ocfs2_extent_block *last_eb = NULL;
6357 struct ocfs2_extent_list *el;
6358 struct buffer_head *last_eb_bh = NULL;
6361 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6363 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6371 * Each component will be touched, so we might as well journal
6372 * here to avoid having to handle errors later.
6374 status = ocfs2_journal_access_path(inode, handle, path);
6381 status = ocfs2_journal_access(handle, inode, last_eb_bh,
6382 OCFS2_JOURNAL_ACCESS_WRITE);
6388 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6391 el = &(fe->id2.i_list);
6394 * Lower levels depend on this never happening, but it's best
6395 * to check it up here before changing the tree.
6397 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6398 ocfs2_error(inode->i_sb,
6399 "Inode %lu has an empty extent record, depth %u\n",
6400 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6405 spin_lock(&OCFS2_I(inode)->ip_lock);
6406 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6408 spin_unlock(&OCFS2_I(inode)->ip_lock);
6409 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6410 inode->i_blocks = ocfs2_inode_sector_count(inode);
6412 status = ocfs2_trim_tree(inode, path, handle, tc,
6413 clusters_to_del, &delete_blk);
6419 if (le32_to_cpu(fe->i_clusters) == 0) {
6420 /* trunc to zero is a special case. */
6421 el->l_tree_depth = 0;
6422 fe->i_last_eb_blk = 0;
6424 fe->i_last_eb_blk = last_eb->h_blkno;
6426 status = ocfs2_journal_dirty(handle, fe_bh);
6433 /* If there will be a new last extent block, then by
6434 * definition, there cannot be any leaves to the right of
6436 last_eb->h_next_leaf_blk = 0;
6437 status = ocfs2_journal_dirty(handle, last_eb_bh);
6445 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6459 static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
6461 set_buffer_uptodate(bh);
6462 mark_buffer_dirty(bh);
6466 static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
6468 set_buffer_uptodate(bh);
6469 mark_buffer_dirty(bh);
6470 return ocfs2_journal_dirty_data(handle, bh);
6473 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6474 unsigned int from, unsigned int to,
6475 struct page *page, int zero, u64 *phys)
6477 int ret, partial = 0;
6479 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6484 zero_user_segment(page, from, to);
6487 * Need to set the buffers we zero'd into uptodate
6488 * here if they aren't - ocfs2_map_page_blocks()
6489 * might've skipped some
6491 if (ocfs2_should_order_data(inode)) {
6492 ret = walk_page_buffers(handle,
6495 ocfs2_ordered_zero_func);
6499 ret = walk_page_buffers(handle, page_buffers(page),
6501 ocfs2_writeback_zero_func);
6507 SetPageUptodate(page);
6509 flush_dcache_page(page);
6512 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6513 loff_t end, struct page **pages,
6514 int numpages, u64 phys, handle_t *handle)
6518 unsigned int from, to = PAGE_CACHE_SIZE;
6519 struct super_block *sb = inode->i_sb;
6521 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6526 to = PAGE_CACHE_SIZE;
6527 for(i = 0; i < numpages; i++) {
6530 from = start & (PAGE_CACHE_SIZE - 1);
6531 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6532 to = end & (PAGE_CACHE_SIZE - 1);
6534 BUG_ON(from > PAGE_CACHE_SIZE);
6535 BUG_ON(to > PAGE_CACHE_SIZE);
6537 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6540 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6544 ocfs2_unlock_and_free_pages(pages, numpages);
6547 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6548 struct page **pages, int *num)
6550 int numpages, ret = 0;
6551 struct super_block *sb = inode->i_sb;
6552 struct address_space *mapping = inode->i_mapping;
6553 unsigned long index;
6554 loff_t last_page_bytes;
6556 BUG_ON(start > end);
6558 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6559 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6562 last_page_bytes = PAGE_ALIGN(end);
6563 index = start >> PAGE_CACHE_SHIFT;
6565 pages[numpages] = grab_cache_page(mapping, index);
6566 if (!pages[numpages]) {
6574 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6579 ocfs2_unlock_and_free_pages(pages, numpages);
6589 * Zero the area past i_size but still within an allocated
6590 * cluster. This avoids exposing nonzero data on subsequent file
6593 * We need to call this before i_size is updated on the inode because
6594 * otherwise block_write_full_page() will skip writeout of pages past
6595 * i_size. The new_i_size parameter is passed for this reason.
6597 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6598 u64 range_start, u64 range_end)
6600 int ret = 0, numpages;
6601 struct page **pages = NULL;
6603 unsigned int ext_flags;
6604 struct super_block *sb = inode->i_sb;
6607 * File systems which don't support sparse files zero on every
6610 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6613 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6614 sizeof(struct page *), GFP_NOFS);
6615 if (pages == NULL) {
6621 if (range_start == range_end)
6624 ret = ocfs2_extent_map_get_blocks(inode,
6625 range_start >> sb->s_blocksize_bits,
6626 &phys, NULL, &ext_flags);
6633 * Tail is a hole, or is marked unwritten. In either case, we
6634 * can count on read and write to return/push zero's.
6636 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6639 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6646 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6647 numpages, phys, handle);
6650 * Initiate writeout of the pages we zero'd here. We don't
6651 * wait on them - the truncate_inode_pages() call later will
6654 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6655 range_end - 1, SYNC_FILE_RANGE_WRITE);
6666 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6667 struct ocfs2_dinode *di)
6669 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6670 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6672 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6673 memset(&di->id2, 0, blocksize -
6674 offsetof(struct ocfs2_dinode, id2) -
6677 memset(&di->id2, 0, blocksize -
6678 offsetof(struct ocfs2_dinode, id2));
6681 void ocfs2_dinode_new_extent_list(struct inode *inode,
6682 struct ocfs2_dinode *di)
6684 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6685 di->id2.i_list.l_tree_depth = 0;
6686 di->id2.i_list.l_next_free_rec = 0;
6687 di->id2.i_list.l_count = cpu_to_le16(
6688 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6691 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6693 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6694 struct ocfs2_inline_data *idata = &di->id2.i_data;
6696 spin_lock(&oi->ip_lock);
6697 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6698 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6699 spin_unlock(&oi->ip_lock);
6702 * We clear the entire i_data structure here so that all
6703 * fields can be properly initialized.
6705 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6707 idata->id_count = cpu_to_le16(
6708 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6711 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6712 struct buffer_head *di_bh)
6714 int ret, i, has_data, num_pages = 0;
6716 u64 uninitialized_var(block);
6717 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6718 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6719 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6720 struct ocfs2_alloc_context *data_ac = NULL;
6721 struct page **pages = NULL;
6722 loff_t end = osb->s_clustersize;
6724 has_data = i_size_read(inode) ? 1 : 0;
6727 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6728 sizeof(struct page *), GFP_NOFS);
6729 if (pages == NULL) {
6735 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6742 handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
6743 if (IS_ERR(handle)) {
6744 ret = PTR_ERR(handle);
6749 ret = ocfs2_journal_access(handle, inode, di_bh,
6750 OCFS2_JOURNAL_ACCESS_WRITE);
6758 unsigned int page_end;
6761 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6769 * Save two copies, one for insert, and one that can
6770 * be changed by ocfs2_map_and_dirty_page() below.
6772 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6775 * Non sparse file systems zero on extend, so no need
6778 if (!ocfs2_sparse_alloc(osb) &&
6779 PAGE_CACHE_SIZE < osb->s_clustersize)
6780 end = PAGE_CACHE_SIZE;
6782 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6789 * This should populate the 1st page for us and mark
6792 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6798 page_end = PAGE_CACHE_SIZE;
6799 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6800 page_end = osb->s_clustersize;
6802 for (i = 0; i < num_pages; i++)
6803 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6804 pages[i], i > 0, &phys);
6807 spin_lock(&oi->ip_lock);
6808 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6809 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6810 spin_unlock(&oi->ip_lock);
6812 ocfs2_dinode_new_extent_list(inode, di);
6814 ocfs2_journal_dirty(handle, di_bh);
6818 * An error at this point should be extremely rare. If
6819 * this proves to be false, we could always re-build
6820 * the in-inode data from our pages.
6822 ret = ocfs2_dinode_insert_extent(osb, handle, inode, di_bh,
6823 0, block, 1, 0, NULL);
6829 inode->i_blocks = ocfs2_inode_sector_count(inode);
6833 ocfs2_commit_trans(osb, handle);
6837 ocfs2_free_alloc_context(data_ac);
6841 ocfs2_unlock_and_free_pages(pages, num_pages);
6849 * It is expected, that by the time you call this function,
6850 * inode->i_size and fe->i_size have been adjusted.
6852 * WARNING: This will kfree the truncate context
6854 int ocfs2_commit_truncate(struct ocfs2_super *osb,
6855 struct inode *inode,
6856 struct buffer_head *fe_bh,
6857 struct ocfs2_truncate_context *tc)
6859 int status, i, credits, tl_sem = 0;
6860 u32 clusters_to_del, new_highest_cpos, range;
6861 struct ocfs2_extent_list *el;
6862 handle_t *handle = NULL;
6863 struct inode *tl_inode = osb->osb_tl_inode;
6864 struct ocfs2_path *path = NULL;
6865 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6869 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
6870 i_size_read(inode));
6872 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
6879 ocfs2_extent_map_trunc(inode, new_highest_cpos);
6883 * Check that we still have allocation to delete.
6885 if (OCFS2_I(inode)->ip_clusters == 0) {
6891 * Truncate always works against the rightmost tree branch.
6893 status = ocfs2_find_path(inode, path, UINT_MAX);
6899 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6900 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
6903 * By now, el will point to the extent list on the bottom most
6904 * portion of this tree. Only the tail record is considered in
6907 * We handle the following cases, in order:
6908 * - empty extent: delete the remaining branch
6909 * - remove the entire record
6910 * - remove a partial record
6911 * - no record needs to be removed (truncate has completed)
6913 el = path_leaf_el(path);
6914 if (le16_to_cpu(el->l_next_free_rec) == 0) {
6915 ocfs2_error(inode->i_sb,
6916 "Inode %llu has empty extent block at %llu\n",
6917 (unsigned long long)OCFS2_I(inode)->ip_blkno,
6918 (unsigned long long)path_leaf_bh(path)->b_blocknr);
6923 i = le16_to_cpu(el->l_next_free_rec) - 1;
6924 range = le32_to_cpu(el->l_recs[i].e_cpos) +
6925 ocfs2_rec_clusters(el, &el->l_recs[i]);
6926 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
6927 clusters_to_del = 0;
6928 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
6929 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
6930 } else if (range > new_highest_cpos) {
6931 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
6932 le32_to_cpu(el->l_recs[i].e_cpos)) -
6939 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6940 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
6942 mutex_lock(&tl_inode->i_mutex);
6944 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6945 * record is free for use. If there isn't any, we flush to get
6946 * an empty truncate log. */
6947 if (ocfs2_truncate_log_needs_flush(osb)) {
6948 status = __ocfs2_flush_truncate_log(osb);
6955 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
6956 (struct ocfs2_dinode *)fe_bh->b_data,
6958 handle = ocfs2_start_trans(osb, credits);
6959 if (IS_ERR(handle)) {
6960 status = PTR_ERR(handle);
6966 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
6973 mutex_unlock(&tl_inode->i_mutex);
6976 ocfs2_commit_trans(osb, handle);
6979 ocfs2_reinit_path(path, 1);
6982 * The check above will catch the case where we've truncated
6983 * away all allocation.
6989 ocfs2_schedule_truncate_log_flush(osb, 1);
6992 mutex_unlock(&tl_inode->i_mutex);
6995 ocfs2_commit_trans(osb, handle);
6997 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
6999 ocfs2_free_path(path);
7001 /* This will drop the ext_alloc cluster lock for us */
7002 ocfs2_free_truncate_context(tc);
7009 * Expects the inode to already be locked.
7011 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7012 struct inode *inode,
7013 struct buffer_head *fe_bh,
7014 struct ocfs2_truncate_context **tc)
7017 unsigned int new_i_clusters;
7018 struct ocfs2_dinode *fe;
7019 struct ocfs2_extent_block *eb;
7020 struct buffer_head *last_eb_bh = NULL;
7026 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7027 i_size_read(inode));
7028 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7030 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7031 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7032 (unsigned long long)le64_to_cpu(fe->i_size));
7034 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7040 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7042 if (fe->id2.i_list.l_tree_depth) {
7043 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
7044 &last_eb_bh, OCFS2_BH_CACHED, inode);
7049 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7050 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
7051 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
7059 (*tc)->tc_last_eb_bh = last_eb_bh;
7065 ocfs2_free_truncate_context(*tc);
7073 * 'start' is inclusive, 'end' is not.
7075 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7076 unsigned int start, unsigned int end, int trunc)
7079 unsigned int numbytes;
7081 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7082 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7083 struct ocfs2_inline_data *idata = &di->id2.i_data;
7085 if (end > i_size_read(inode))
7086 end = i_size_read(inode);
7088 BUG_ON(start >= end);
7090 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7091 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7092 !ocfs2_supports_inline_data(osb)) {
7093 ocfs2_error(inode->i_sb,
7094 "Inline data flags for inode %llu don't agree! "
7095 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7096 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7097 le16_to_cpu(di->i_dyn_features),
7098 OCFS2_I(inode)->ip_dyn_features,
7099 osb->s_feature_incompat);
7104 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7105 if (IS_ERR(handle)) {
7106 ret = PTR_ERR(handle);
7111 ret = ocfs2_journal_access(handle, inode, di_bh,
7112 OCFS2_JOURNAL_ACCESS_WRITE);
7118 numbytes = end - start;
7119 memset(idata->id_data + start, 0, numbytes);
7122 * No need to worry about the data page here - it's been
7123 * truncated already and inline data doesn't need it for
7124 * pushing zero's to disk, so we'll let readpage pick it up
7128 i_size_write(inode, start);
7129 di->i_size = cpu_to_le64(start);
7132 inode->i_blocks = ocfs2_inode_sector_count(inode);
7133 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7135 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7136 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7138 ocfs2_journal_dirty(handle, di_bh);
7141 ocfs2_commit_trans(osb, handle);
7147 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7150 * The caller is responsible for completing deallocation
7151 * before freeing the context.
7153 if (tc->tc_dealloc.c_first_suballocator != NULL)
7155 "Truncate completion has non-empty dealloc context\n");
7157 if (tc->tc_last_eb_bh)
7158 brelse(tc->tc_last_eb_bh);