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"
52 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
53 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
54 struct ocfs2_extent_block *eb);
57 * Structures which describe a path through a btree, and functions to
60 * The idea here is to be as generic as possible with the tree
63 struct ocfs2_path_item {
64 struct buffer_head *bh;
65 struct ocfs2_extent_list *el;
68 #define OCFS2_MAX_PATH_DEPTH 5
72 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
75 #define path_root_bh(_path) ((_path)->p_node[0].bh)
76 #define path_root_el(_path) ((_path)->p_node[0].el)
77 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
78 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
79 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
82 * Reset the actual path elements so that we can re-use the structure
83 * to build another path. Generally, this involves freeing the buffer
86 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
88 int i, start = 0, depth = 0;
89 struct ocfs2_path_item *node;
94 for(i = start; i < path_num_items(path); i++) {
95 node = &path->p_node[i];
103 * Tree depth may change during truncate, or insert. If we're
104 * keeping the root extent list, then make sure that our path
105 * structure reflects the proper depth.
108 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
110 path->p_tree_depth = depth;
113 static void ocfs2_free_path(struct ocfs2_path *path)
116 ocfs2_reinit_path(path, 0);
122 * All the elements of src into dest. After this call, src could be freed
123 * without affecting dest.
125 * Both paths should have the same root. Any non-root elements of dest
128 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
132 BUG_ON(path_root_bh(dest) != path_root_bh(src));
133 BUG_ON(path_root_el(dest) != path_root_el(src));
135 ocfs2_reinit_path(dest, 1);
137 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
138 dest->p_node[i].bh = src->p_node[i].bh;
139 dest->p_node[i].el = src->p_node[i].el;
141 if (dest->p_node[i].bh)
142 get_bh(dest->p_node[i].bh);
147 * Make the *dest path the same as src and re-initialize src path to
150 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
154 BUG_ON(path_root_bh(dest) != path_root_bh(src));
156 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
157 brelse(dest->p_node[i].bh);
159 dest->p_node[i].bh = src->p_node[i].bh;
160 dest->p_node[i].el = src->p_node[i].el;
162 src->p_node[i].bh = NULL;
163 src->p_node[i].el = NULL;
168 * Insert an extent block at given index.
170 * This will not take an additional reference on eb_bh.
172 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
173 struct buffer_head *eb_bh)
175 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
178 * Right now, no root bh is an extent block, so this helps
179 * catch code errors with dinode trees. The assertion can be
180 * safely removed if we ever need to insert extent block
181 * structures at the root.
185 path->p_node[index].bh = eb_bh;
186 path->p_node[index].el = &eb->h_list;
189 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
190 struct ocfs2_extent_list *root_el)
192 struct ocfs2_path *path;
194 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
196 path = kzalloc(sizeof(*path), GFP_NOFS);
198 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
200 path_root_bh(path) = root_bh;
201 path_root_el(path) = root_el;
208 * Allocate and initialize a new path based on a disk inode tree.
210 static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh)
212 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
213 struct ocfs2_extent_list *el = &di->id2.i_list;
215 return ocfs2_new_path(di_bh, el);
219 * Convenience function to journal all components in a path.
221 static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
222 struct ocfs2_path *path)
229 for(i = 0; i < path_num_items(path); i++) {
230 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
231 OCFS2_JOURNAL_ACCESS_WRITE);
243 * Return the index of the extent record which contains cluster #v_cluster.
244 * -1 is returned if it was not found.
246 * Should work fine on interior and exterior nodes.
248 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
252 struct ocfs2_extent_rec *rec;
253 u32 rec_end, rec_start, clusters;
255 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
256 rec = &el->l_recs[i];
258 rec_start = le32_to_cpu(rec->e_cpos);
259 clusters = ocfs2_rec_clusters(el, rec);
261 rec_end = rec_start + clusters;
263 if (v_cluster >= rec_start && v_cluster < rec_end) {
272 enum ocfs2_contig_type {
281 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
282 * ocfs2_extent_contig only work properly against leaf nodes!
284 static int ocfs2_block_extent_contig(struct super_block *sb,
285 struct ocfs2_extent_rec *ext,
288 u64 blk_end = le64_to_cpu(ext->e_blkno);
290 blk_end += ocfs2_clusters_to_blocks(sb,
291 le16_to_cpu(ext->e_leaf_clusters));
293 return blkno == blk_end;
296 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
297 struct ocfs2_extent_rec *right)
301 left_range = le32_to_cpu(left->e_cpos) +
302 le16_to_cpu(left->e_leaf_clusters);
304 return (left_range == le32_to_cpu(right->e_cpos));
307 static enum ocfs2_contig_type
308 ocfs2_extent_contig(struct inode *inode,
309 struct ocfs2_extent_rec *ext,
310 struct ocfs2_extent_rec *insert_rec)
312 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
315 * Refuse to coalesce extent records with different flag
316 * fields - we don't want to mix unwritten extents with user
319 if (ext->e_flags != insert_rec->e_flags)
322 if (ocfs2_extents_adjacent(ext, insert_rec) &&
323 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
326 blkno = le64_to_cpu(ext->e_blkno);
327 if (ocfs2_extents_adjacent(insert_rec, ext) &&
328 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
335 * NOTE: We can have pretty much any combination of contiguousness and
338 * The usefulness of APPEND_TAIL is more in that it lets us know that
339 * we'll have to update the path to that leaf.
341 enum ocfs2_append_type {
346 enum ocfs2_split_type {
352 struct ocfs2_insert_type {
353 enum ocfs2_split_type ins_split;
354 enum ocfs2_append_type ins_appending;
355 enum ocfs2_contig_type ins_contig;
356 int ins_contig_index;
360 struct ocfs2_merge_ctxt {
361 enum ocfs2_contig_type c_contig_type;
362 int c_has_empty_extent;
363 int c_split_covers_rec;
367 * How many free extents have we got before we need more meta data?
369 int ocfs2_num_free_extents(struct ocfs2_super *osb,
371 struct ocfs2_dinode *fe)
374 struct ocfs2_extent_list *el;
375 struct ocfs2_extent_block *eb;
376 struct buffer_head *eb_bh = NULL;
380 if (!OCFS2_IS_VALID_DINODE(fe)) {
381 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
386 if (fe->i_last_eb_blk) {
387 retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
388 &eb_bh, OCFS2_BH_CACHED, inode);
393 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
396 el = &fe->id2.i_list;
398 BUG_ON(el->l_tree_depth != 0);
400 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
409 /* expects array to already be allocated
411 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
414 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
418 struct ocfs2_alloc_context *meta_ac,
419 struct buffer_head *bhs[])
421 int count, status, i;
422 u16 suballoc_bit_start;
425 struct ocfs2_extent_block *eb;
430 while (count < wanted) {
431 status = ocfs2_claim_metadata(osb,
443 for(i = count; i < (num_got + count); i++) {
444 bhs[i] = sb_getblk(osb->sb, first_blkno);
445 if (bhs[i] == NULL) {
450 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
452 status = ocfs2_journal_access(handle, inode, bhs[i],
453 OCFS2_JOURNAL_ACCESS_CREATE);
459 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
460 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
461 /* Ok, setup the minimal stuff here. */
462 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
463 eb->h_blkno = cpu_to_le64(first_blkno);
464 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
465 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
466 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
468 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
470 suballoc_bit_start++;
473 /* We'll also be dirtied by the caller, so
474 * this isn't absolutely necessary. */
475 status = ocfs2_journal_dirty(handle, bhs[i]);
488 for(i = 0; i < wanted; i++) {
499 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
501 * Returns the sum of the rightmost extent rec logical offset and
504 * ocfs2_add_branch() uses this to determine what logical cluster
505 * value should be populated into the leftmost new branch records.
507 * ocfs2_shift_tree_depth() uses this to determine the # clusters
508 * value for the new topmost tree record.
510 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
514 i = le16_to_cpu(el->l_next_free_rec) - 1;
516 return le32_to_cpu(el->l_recs[i].e_cpos) +
517 ocfs2_rec_clusters(el, &el->l_recs[i]);
521 * Add an entire tree branch to our inode. eb_bh is the extent block
522 * to start at, if we don't want to start the branch at the dinode
525 * last_eb_bh is required as we have to update it's next_leaf pointer
526 * for the new last extent block.
528 * the new branch will be 'empty' in the sense that every block will
529 * contain a single record with cluster count == 0.
531 static int ocfs2_add_branch(struct ocfs2_super *osb,
534 struct buffer_head *fe_bh,
535 struct buffer_head *eb_bh,
536 struct buffer_head **last_eb_bh,
537 struct ocfs2_alloc_context *meta_ac)
539 int status, new_blocks, i;
540 u64 next_blkno, new_last_eb_blk;
541 struct buffer_head *bh;
542 struct buffer_head **new_eb_bhs = NULL;
543 struct ocfs2_dinode *fe;
544 struct ocfs2_extent_block *eb;
545 struct ocfs2_extent_list *eb_el;
546 struct ocfs2_extent_list *el;
551 BUG_ON(!last_eb_bh || !*last_eb_bh);
553 fe = (struct ocfs2_dinode *) fe_bh->b_data;
556 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
559 el = &fe->id2.i_list;
561 /* we never add a branch to a leaf. */
562 BUG_ON(!el->l_tree_depth);
564 new_blocks = le16_to_cpu(el->l_tree_depth);
566 /* allocate the number of new eb blocks we need */
567 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
575 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
576 meta_ac, new_eb_bhs);
582 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
583 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
585 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
586 * linked with the rest of the tree.
587 * conversly, new_eb_bhs[0] is the new bottommost leaf.
589 * when we leave the loop, new_last_eb_blk will point to the
590 * newest leaf, and next_blkno will point to the topmost extent
592 next_blkno = new_last_eb_blk = 0;
593 for(i = 0; i < new_blocks; i++) {
595 eb = (struct ocfs2_extent_block *) bh->b_data;
596 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
597 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
603 status = ocfs2_journal_access(handle, inode, bh,
604 OCFS2_JOURNAL_ACCESS_CREATE);
610 eb->h_next_leaf_blk = 0;
611 eb_el->l_tree_depth = cpu_to_le16(i);
612 eb_el->l_next_free_rec = cpu_to_le16(1);
614 * This actually counts as an empty extent as
617 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
618 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
620 * eb_el isn't always an interior node, but even leaf
621 * nodes want a zero'd flags and reserved field so
622 * this gets the whole 32 bits regardless of use.
624 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
625 if (!eb_el->l_tree_depth)
626 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
628 status = ocfs2_journal_dirty(handle, bh);
634 next_blkno = le64_to_cpu(eb->h_blkno);
637 /* This is a bit hairy. We want to update up to three blocks
638 * here without leaving any of them in an inconsistent state
639 * in case of error. We don't have to worry about
640 * journal_dirty erroring as it won't unless we've aborted the
641 * handle (in which case we would never be here) so reserving
642 * the write with journal_access is all we need to do. */
643 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
644 OCFS2_JOURNAL_ACCESS_WRITE);
649 status = ocfs2_journal_access(handle, inode, fe_bh,
650 OCFS2_JOURNAL_ACCESS_WRITE);
656 status = ocfs2_journal_access(handle, inode, eb_bh,
657 OCFS2_JOURNAL_ACCESS_WRITE);
664 /* Link the new branch into the rest of the tree (el will
665 * either be on the fe, or the extent block passed in. */
666 i = le16_to_cpu(el->l_next_free_rec);
667 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
668 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
669 el->l_recs[i].e_int_clusters = 0;
670 le16_add_cpu(&el->l_next_free_rec, 1);
672 /* fe needs a new last extent block pointer, as does the
673 * next_leaf on the previously last-extent-block. */
674 fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk);
676 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
677 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
679 status = ocfs2_journal_dirty(handle, *last_eb_bh);
682 status = ocfs2_journal_dirty(handle, fe_bh);
686 status = ocfs2_journal_dirty(handle, eb_bh);
692 * Some callers want to track the rightmost leaf so pass it
696 get_bh(new_eb_bhs[0]);
697 *last_eb_bh = new_eb_bhs[0];
702 for (i = 0; i < new_blocks; i++)
704 brelse(new_eb_bhs[i]);
713 * adds another level to the allocation tree.
714 * returns back the new extent block so you can add a branch to it
717 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
720 struct buffer_head *fe_bh,
721 struct ocfs2_alloc_context *meta_ac,
722 struct buffer_head **ret_new_eb_bh)
726 struct buffer_head *new_eb_bh = NULL;
727 struct ocfs2_dinode *fe;
728 struct ocfs2_extent_block *eb;
729 struct ocfs2_extent_list *fe_el;
730 struct ocfs2_extent_list *eb_el;
734 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
741 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
742 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
743 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
749 fe = (struct ocfs2_dinode *) fe_bh->b_data;
750 fe_el = &fe->id2.i_list;
752 status = ocfs2_journal_access(handle, inode, new_eb_bh,
753 OCFS2_JOURNAL_ACCESS_CREATE);
759 /* copy the fe data into the new extent block */
760 eb_el->l_tree_depth = fe_el->l_tree_depth;
761 eb_el->l_next_free_rec = fe_el->l_next_free_rec;
762 for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
763 eb_el->l_recs[i] = fe_el->l_recs[i];
765 status = ocfs2_journal_dirty(handle, new_eb_bh);
771 status = ocfs2_journal_access(handle, inode, fe_bh,
772 OCFS2_JOURNAL_ACCESS_WRITE);
778 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
781 le16_add_cpu(&fe_el->l_tree_depth, 1);
782 fe_el->l_recs[0].e_cpos = 0;
783 fe_el->l_recs[0].e_blkno = eb->h_blkno;
784 fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
785 for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
786 memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
787 fe_el->l_next_free_rec = cpu_to_le16(1);
789 /* If this is our 1st tree depth shift, then last_eb_blk
790 * becomes the allocated extent block */
791 if (fe_el->l_tree_depth == cpu_to_le16(1))
792 fe->i_last_eb_blk = eb->h_blkno;
794 status = ocfs2_journal_dirty(handle, fe_bh);
800 *ret_new_eb_bh = new_eb_bh;
812 * Should only be called when there is no space left in any of the
813 * leaf nodes. What we want to do is find the lowest tree depth
814 * non-leaf extent block with room for new records. There are three
815 * valid results of this search:
817 * 1) a lowest extent block is found, then we pass it back in
818 * *lowest_eb_bh and return '0'
820 * 2) the search fails to find anything, but the dinode has room. We
821 * pass NULL back in *lowest_eb_bh, but still return '0'
823 * 3) the search fails to find anything AND the dinode is full, in
824 * which case we return > 0
826 * return status < 0 indicates an error.
828 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
830 struct buffer_head *fe_bh,
831 struct buffer_head **target_bh)
835 struct ocfs2_dinode *fe;
836 struct ocfs2_extent_block *eb;
837 struct ocfs2_extent_list *el;
838 struct buffer_head *bh = NULL;
839 struct buffer_head *lowest_bh = NULL;
845 fe = (struct ocfs2_dinode *) fe_bh->b_data;
846 el = &fe->id2.i_list;
848 while(le16_to_cpu(el->l_tree_depth) > 1) {
849 if (le16_to_cpu(el->l_next_free_rec) == 0) {
850 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
851 "extent list (next_free_rec == 0)",
852 (unsigned long long)OCFS2_I(inode)->ip_blkno);
856 i = le16_to_cpu(el->l_next_free_rec) - 1;
857 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
859 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
860 "list where extent # %d has no physical "
862 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
872 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
879 eb = (struct ocfs2_extent_block *) bh->b_data;
880 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
881 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
887 if (le16_to_cpu(el->l_next_free_rec) <
888 le16_to_cpu(el->l_count)) {
896 /* If we didn't find one and the fe doesn't have any room,
899 && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count))
902 *target_bh = lowest_bh;
912 * Grow a b-tree so that it has more records.
914 * We might shift the tree depth in which case existing paths should
915 * be considered invalid.
917 * Tree depth after the grow is returned via *final_depth.
919 * *last_eb_bh will be updated by ocfs2_add_branch().
921 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
922 struct buffer_head *di_bh, int *final_depth,
923 struct buffer_head **last_eb_bh,
924 struct ocfs2_alloc_context *meta_ac)
927 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
928 int depth = le16_to_cpu(di->id2.i_list.l_tree_depth);
929 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
930 struct buffer_head *bh = NULL;
932 BUG_ON(meta_ac == NULL);
934 shift = ocfs2_find_branch_target(osb, inode, di_bh, &bh);
941 /* We traveled all the way to the bottom of the allocation tree
942 * and didn't find room for any more extents - we need to add
943 * another tree level */
946 mlog(0, "need to shift tree depth (current = %d)\n", depth);
948 /* ocfs2_shift_tree_depth will return us a buffer with
949 * the new extent block (so we can pass that to
950 * ocfs2_add_branch). */
951 ret = ocfs2_shift_tree_depth(osb, handle, inode, di_bh,
960 * Special case: we have room now if we shifted from
961 * tree_depth 0, so no more work needs to be done.
963 * We won't be calling add_branch, so pass
964 * back *last_eb_bh as the new leaf. At depth
965 * zero, it should always be null so there's
966 * no reason to brelse.
975 /* call ocfs2_add_branch to add the final part of the tree with
977 mlog(0, "add branch. bh = %p\n", bh);
978 ret = ocfs2_add_branch(osb, handle, inode, di_bh, bh, last_eb_bh,
987 *final_depth = depth;
993 * This is only valid for leaf nodes, which are the only ones that can
994 * have empty extents anyway.
996 static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
998 return !rec->e_leaf_clusters;
1002 * This function will discard the rightmost extent record.
1004 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1006 int next_free = le16_to_cpu(el->l_next_free_rec);
1007 int count = le16_to_cpu(el->l_count);
1008 unsigned int num_bytes;
1011 /* This will cause us to go off the end of our extent list. */
1012 BUG_ON(next_free >= count);
1014 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1016 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1019 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1020 struct ocfs2_extent_rec *insert_rec)
1022 int i, insert_index, next_free, has_empty, num_bytes;
1023 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1024 struct ocfs2_extent_rec *rec;
1026 next_free = le16_to_cpu(el->l_next_free_rec);
1027 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1031 /* The tree code before us didn't allow enough room in the leaf. */
1032 if (el->l_next_free_rec == el->l_count && !has_empty)
1036 * The easiest way to approach this is to just remove the
1037 * empty extent and temporarily decrement next_free.
1041 * If next_free was 1 (only an empty extent), this
1042 * loop won't execute, which is fine. We still want
1043 * the decrement above to happen.
1045 for(i = 0; i < (next_free - 1); i++)
1046 el->l_recs[i] = el->l_recs[i+1];
1052 * Figure out what the new record index should be.
1054 for(i = 0; i < next_free; i++) {
1055 rec = &el->l_recs[i];
1057 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1062 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1063 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1065 BUG_ON(insert_index < 0);
1066 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1067 BUG_ON(insert_index > next_free);
1070 * No need to memmove if we're just adding to the tail.
1072 if (insert_index != next_free) {
1073 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1075 num_bytes = next_free - insert_index;
1076 num_bytes *= sizeof(struct ocfs2_extent_rec);
1077 memmove(&el->l_recs[insert_index + 1],
1078 &el->l_recs[insert_index],
1083 * Either we had an empty extent, and need to re-increment or
1084 * there was no empty extent on a non full rightmost leaf node,
1085 * in which case we still need to increment.
1088 el->l_next_free_rec = cpu_to_le16(next_free);
1090 * Make sure none of the math above just messed up our tree.
1092 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1094 el->l_recs[insert_index] = *insert_rec;
1098 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1100 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1102 BUG_ON(num_recs == 0);
1104 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1106 size = num_recs * sizeof(struct ocfs2_extent_rec);
1107 memmove(&el->l_recs[0], &el->l_recs[1], size);
1108 memset(&el->l_recs[num_recs], 0,
1109 sizeof(struct ocfs2_extent_rec));
1110 el->l_next_free_rec = cpu_to_le16(num_recs);
1115 * Create an empty extent record .
1117 * l_next_free_rec may be updated.
1119 * If an empty extent already exists do nothing.
1121 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1123 int next_free = le16_to_cpu(el->l_next_free_rec);
1125 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1130 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1133 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1134 "Asked to create an empty extent in a full list:\n"
1135 "count = %u, tree depth = %u",
1136 le16_to_cpu(el->l_count),
1137 le16_to_cpu(el->l_tree_depth));
1139 ocfs2_shift_records_right(el);
1142 le16_add_cpu(&el->l_next_free_rec, 1);
1143 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1147 * For a rotation which involves two leaf nodes, the "root node" is
1148 * the lowest level tree node which contains a path to both leafs. This
1149 * resulting set of information can be used to form a complete "subtree"
1151 * This function is passed two full paths from the dinode down to a
1152 * pair of adjacent leaves. It's task is to figure out which path
1153 * index contains the subtree root - this can be the root index itself
1154 * in a worst-case rotation.
1156 * The array index of the subtree root is passed back.
1158 static int ocfs2_find_subtree_root(struct inode *inode,
1159 struct ocfs2_path *left,
1160 struct ocfs2_path *right)
1165 * Check that the caller passed in two paths from the same tree.
1167 BUG_ON(path_root_bh(left) != path_root_bh(right));
1173 * The caller didn't pass two adjacent paths.
1175 mlog_bug_on_msg(i > left->p_tree_depth,
1176 "Inode %lu, left depth %u, right depth %u\n"
1177 "left leaf blk %llu, right leaf blk %llu\n",
1178 inode->i_ino, left->p_tree_depth,
1179 right->p_tree_depth,
1180 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1181 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1182 } while (left->p_node[i].bh->b_blocknr ==
1183 right->p_node[i].bh->b_blocknr);
1188 typedef void (path_insert_t)(void *, struct buffer_head *);
1191 * Traverse a btree path in search of cpos, starting at root_el.
1193 * This code can be called with a cpos larger than the tree, in which
1194 * case it will return the rightmost path.
1196 static int __ocfs2_find_path(struct inode *inode,
1197 struct ocfs2_extent_list *root_el, u32 cpos,
1198 path_insert_t *func, void *data)
1203 struct buffer_head *bh = NULL;
1204 struct ocfs2_extent_block *eb;
1205 struct ocfs2_extent_list *el;
1206 struct ocfs2_extent_rec *rec;
1207 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1210 while (el->l_tree_depth) {
1211 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1212 ocfs2_error(inode->i_sb,
1213 "Inode %llu has empty extent list at "
1215 (unsigned long long)oi->ip_blkno,
1216 le16_to_cpu(el->l_tree_depth));
1222 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1223 rec = &el->l_recs[i];
1226 * In the case that cpos is off the allocation
1227 * tree, this should just wind up returning the
1230 range = le32_to_cpu(rec->e_cpos) +
1231 ocfs2_rec_clusters(el, rec);
1232 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1236 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1238 ocfs2_error(inode->i_sb,
1239 "Inode %llu has bad blkno in extent list "
1240 "at depth %u (index %d)\n",
1241 (unsigned long long)oi->ip_blkno,
1242 le16_to_cpu(el->l_tree_depth), i);
1249 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1250 &bh, OCFS2_BH_CACHED, inode);
1256 eb = (struct ocfs2_extent_block *) bh->b_data;
1258 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1259 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1264 if (le16_to_cpu(el->l_next_free_rec) >
1265 le16_to_cpu(el->l_count)) {
1266 ocfs2_error(inode->i_sb,
1267 "Inode %llu has bad count in extent list "
1268 "at block %llu (next free=%u, count=%u)\n",
1269 (unsigned long long)oi->ip_blkno,
1270 (unsigned long long)bh->b_blocknr,
1271 le16_to_cpu(el->l_next_free_rec),
1272 le16_to_cpu(el->l_count));
1283 * Catch any trailing bh that the loop didn't handle.
1291 * Given an initialized path (that is, it has a valid root extent
1292 * list), this function will traverse the btree in search of the path
1293 * which would contain cpos.
1295 * The path traveled is recorded in the path structure.
1297 * Note that this will not do any comparisons on leaf node extent
1298 * records, so it will work fine in the case that we just added a tree
1301 struct find_path_data {
1303 struct ocfs2_path *path;
1305 static void find_path_ins(void *data, struct buffer_head *bh)
1307 struct find_path_data *fp = data;
1310 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1313 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1316 struct find_path_data data;
1320 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1321 find_path_ins, &data);
1324 static void find_leaf_ins(void *data, struct buffer_head *bh)
1326 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1327 struct ocfs2_extent_list *el = &eb->h_list;
1328 struct buffer_head **ret = data;
1330 /* We want to retain only the leaf block. */
1331 if (le16_to_cpu(el->l_tree_depth) == 0) {
1337 * Find the leaf block in the tree which would contain cpos. No
1338 * checking of the actual leaf is done.
1340 * Some paths want to call this instead of allocating a path structure
1341 * and calling ocfs2_find_path().
1343 * This function doesn't handle non btree extent lists.
1345 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1346 u32 cpos, struct buffer_head **leaf_bh)
1349 struct buffer_head *bh = NULL;
1351 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1363 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1365 * Basically, we've moved stuff around at the bottom of the tree and
1366 * we need to fix up the extent records above the changes to reflect
1369 * left_rec: the record on the left.
1370 * left_child_el: is the child list pointed to by left_rec
1371 * right_rec: the record to the right of left_rec
1372 * right_child_el: is the child list pointed to by right_rec
1374 * By definition, this only works on interior nodes.
1376 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1377 struct ocfs2_extent_list *left_child_el,
1378 struct ocfs2_extent_rec *right_rec,
1379 struct ocfs2_extent_list *right_child_el)
1381 u32 left_clusters, right_end;
1384 * Interior nodes never have holes. Their cpos is the cpos of
1385 * the leftmost record in their child list. Their cluster
1386 * count covers the full theoretical range of their child list
1387 * - the range between their cpos and the cpos of the record
1388 * immediately to their right.
1390 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1391 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1392 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1393 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1395 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1396 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1399 * Calculate the rightmost cluster count boundary before
1400 * moving cpos - we will need to adjust clusters after
1401 * updating e_cpos to keep the same highest cluster count.
1403 right_end = le32_to_cpu(right_rec->e_cpos);
1404 right_end += le32_to_cpu(right_rec->e_int_clusters);
1406 right_rec->e_cpos = left_rec->e_cpos;
1407 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1409 right_end -= le32_to_cpu(right_rec->e_cpos);
1410 right_rec->e_int_clusters = cpu_to_le32(right_end);
1414 * Adjust the adjacent root node records involved in a
1415 * rotation. left_el_blkno is passed in as a key so that we can easily
1416 * find it's index in the root list.
1418 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1419 struct ocfs2_extent_list *left_el,
1420 struct ocfs2_extent_list *right_el,
1425 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1426 le16_to_cpu(left_el->l_tree_depth));
1428 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1429 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1434 * The path walking code should have never returned a root and
1435 * two paths which are not adjacent.
1437 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1439 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1440 &root_el->l_recs[i + 1], right_el);
1444 * We've changed a leaf block (in right_path) and need to reflect that
1445 * change back up the subtree.
1447 * This happens in multiple places:
1448 * - When we've moved an extent record from the left path leaf to the right
1449 * path leaf to make room for an empty extent in the left path leaf.
1450 * - When our insert into the right path leaf is at the leftmost edge
1451 * and requires an update of the path immediately to it's left. This
1452 * can occur at the end of some types of rotation and appending inserts.
1454 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1455 struct ocfs2_path *left_path,
1456 struct ocfs2_path *right_path,
1460 struct ocfs2_extent_list *el, *left_el, *right_el;
1461 struct ocfs2_extent_rec *left_rec, *right_rec;
1462 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1465 * Update the counts and position values within all the
1466 * interior nodes to reflect the leaf rotation we just did.
1468 * The root node is handled below the loop.
1470 * We begin the loop with right_el and left_el pointing to the
1471 * leaf lists and work our way up.
1473 * NOTE: within this loop, left_el and right_el always refer
1474 * to the *child* lists.
1476 left_el = path_leaf_el(left_path);
1477 right_el = path_leaf_el(right_path);
1478 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1479 mlog(0, "Adjust records at index %u\n", i);
1482 * One nice property of knowing that all of these
1483 * nodes are below the root is that we only deal with
1484 * the leftmost right node record and the rightmost
1487 el = left_path->p_node[i].el;
1488 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1489 left_rec = &el->l_recs[idx];
1491 el = right_path->p_node[i].el;
1492 right_rec = &el->l_recs[0];
1494 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1497 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1501 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1506 * Setup our list pointers now so that the current
1507 * parents become children in the next iteration.
1509 left_el = left_path->p_node[i].el;
1510 right_el = right_path->p_node[i].el;
1514 * At the root node, adjust the two adjacent records which
1515 * begin our path to the leaves.
1518 el = left_path->p_node[subtree_index].el;
1519 left_el = left_path->p_node[subtree_index + 1].el;
1520 right_el = right_path->p_node[subtree_index + 1].el;
1522 ocfs2_adjust_root_records(el, left_el, right_el,
1523 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1525 root_bh = left_path->p_node[subtree_index].bh;
1527 ret = ocfs2_journal_dirty(handle, root_bh);
1532 static int ocfs2_rotate_subtree_right(struct inode *inode,
1534 struct ocfs2_path *left_path,
1535 struct ocfs2_path *right_path,
1539 struct buffer_head *right_leaf_bh;
1540 struct buffer_head *left_leaf_bh = NULL;
1541 struct buffer_head *root_bh;
1542 struct ocfs2_extent_list *right_el, *left_el;
1543 struct ocfs2_extent_rec move_rec;
1545 left_leaf_bh = path_leaf_bh(left_path);
1546 left_el = path_leaf_el(left_path);
1548 if (left_el->l_next_free_rec != left_el->l_count) {
1549 ocfs2_error(inode->i_sb,
1550 "Inode %llu has non-full interior leaf node %llu"
1552 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1553 (unsigned long long)left_leaf_bh->b_blocknr,
1554 le16_to_cpu(left_el->l_next_free_rec));
1559 * This extent block may already have an empty record, so we
1560 * return early if so.
1562 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1565 root_bh = left_path->p_node[subtree_index].bh;
1566 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1568 ret = ocfs2_journal_access(handle, inode, root_bh,
1569 OCFS2_JOURNAL_ACCESS_WRITE);
1575 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1576 ret = ocfs2_journal_access(handle, inode,
1577 right_path->p_node[i].bh,
1578 OCFS2_JOURNAL_ACCESS_WRITE);
1584 ret = ocfs2_journal_access(handle, inode,
1585 left_path->p_node[i].bh,
1586 OCFS2_JOURNAL_ACCESS_WRITE);
1593 right_leaf_bh = path_leaf_bh(right_path);
1594 right_el = path_leaf_el(right_path);
1596 /* This is a code error, not a disk corruption. */
1597 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1598 "because rightmost leaf block %llu is empty\n",
1599 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1600 (unsigned long long)right_leaf_bh->b_blocknr);
1602 ocfs2_create_empty_extent(right_el);
1604 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1610 /* Do the copy now. */
1611 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1612 move_rec = left_el->l_recs[i];
1613 right_el->l_recs[0] = move_rec;
1616 * Clear out the record we just copied and shift everything
1617 * over, leaving an empty extent in the left leaf.
1619 * We temporarily subtract from next_free_rec so that the
1620 * shift will lose the tail record (which is now defunct).
1622 le16_add_cpu(&left_el->l_next_free_rec, -1);
1623 ocfs2_shift_records_right(left_el);
1624 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1625 le16_add_cpu(&left_el->l_next_free_rec, 1);
1627 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1633 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1641 * Given a full path, determine what cpos value would return us a path
1642 * containing the leaf immediately to the left of the current one.
1644 * Will return zero if the path passed in is already the leftmost path.
1646 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1647 struct ocfs2_path *path, u32 *cpos)
1651 struct ocfs2_extent_list *el;
1653 BUG_ON(path->p_tree_depth == 0);
1657 blkno = path_leaf_bh(path)->b_blocknr;
1659 /* Start at the tree node just above the leaf and work our way up. */
1660 i = path->p_tree_depth - 1;
1662 el = path->p_node[i].el;
1665 * Find the extent record just before the one in our
1668 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1669 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1673 * We've determined that the
1674 * path specified is already
1675 * the leftmost one - return a
1681 * The leftmost record points to our
1682 * leaf - we need to travel up the
1688 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
1689 *cpos = *cpos + ocfs2_rec_clusters(el,
1690 &el->l_recs[j - 1]);
1697 * If we got here, we never found a valid node where
1698 * the tree indicated one should be.
1701 "Invalid extent tree at extent block %llu\n",
1702 (unsigned long long)blkno);
1707 blkno = path->p_node[i].bh->b_blocknr;
1716 * Extend the transaction by enough credits to complete the rotation,
1717 * and still leave at least the original number of credits allocated
1718 * to this transaction.
1720 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
1722 struct ocfs2_path *path)
1724 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
1726 if (handle->h_buffer_credits < credits)
1727 return ocfs2_extend_trans(handle, credits);
1733 * Trap the case where we're inserting into the theoretical range past
1734 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1735 * whose cpos is less than ours into the right leaf.
1737 * It's only necessary to look at the rightmost record of the left
1738 * leaf because the logic that calls us should ensure that the
1739 * theoretical ranges in the path components above the leaves are
1742 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1745 struct ocfs2_extent_list *left_el;
1746 struct ocfs2_extent_rec *rec;
1749 left_el = path_leaf_el(left_path);
1750 next_free = le16_to_cpu(left_el->l_next_free_rec);
1751 rec = &left_el->l_recs[next_free - 1];
1753 if (insert_cpos > le32_to_cpu(rec->e_cpos))
1758 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
1760 int next_free = le16_to_cpu(el->l_next_free_rec);
1762 struct ocfs2_extent_rec *rec;
1767 rec = &el->l_recs[0];
1768 if (ocfs2_is_empty_extent(rec)) {
1772 rec = &el->l_recs[1];
1775 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
1776 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1782 * Rotate all the records in a btree right one record, starting at insert_cpos.
1784 * The path to the rightmost leaf should be passed in.
1786 * The array is assumed to be large enough to hold an entire path (tree depth).
1788 * Upon succesful return from this function:
1790 * - The 'right_path' array will contain a path to the leaf block
1791 * whose range contains e_cpos.
1792 * - That leaf block will have a single empty extent in list index 0.
1793 * - In the case that the rotation requires a post-insert update,
1794 * *ret_left_path will contain a valid path which can be passed to
1795 * ocfs2_insert_path().
1797 static int ocfs2_rotate_tree_right(struct inode *inode,
1799 enum ocfs2_split_type split,
1801 struct ocfs2_path *right_path,
1802 struct ocfs2_path **ret_left_path)
1804 int ret, start, orig_credits = handle->h_buffer_credits;
1806 struct ocfs2_path *left_path = NULL;
1808 *ret_left_path = NULL;
1810 left_path = ocfs2_new_path(path_root_bh(right_path),
1811 path_root_el(right_path));
1818 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
1824 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
1827 * What we want to do here is:
1829 * 1) Start with the rightmost path.
1831 * 2) Determine a path to the leaf block directly to the left
1834 * 3) Determine the 'subtree root' - the lowest level tree node
1835 * which contains a path to both leaves.
1837 * 4) Rotate the subtree.
1839 * 5) Find the next subtree by considering the left path to be
1840 * the new right path.
1842 * The check at the top of this while loop also accepts
1843 * insert_cpos == cpos because cpos is only a _theoretical_
1844 * value to get us the left path - insert_cpos might very well
1845 * be filling that hole.
1847 * Stop at a cpos of '0' because we either started at the
1848 * leftmost branch (i.e., a tree with one branch and a
1849 * rotation inside of it), or we've gone as far as we can in
1850 * rotating subtrees.
1852 while (cpos && insert_cpos <= cpos) {
1853 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
1856 ret = ocfs2_find_path(inode, left_path, cpos);
1862 mlog_bug_on_msg(path_leaf_bh(left_path) ==
1863 path_leaf_bh(right_path),
1864 "Inode %lu: error during insert of %u "
1865 "(left path cpos %u) results in two identical "
1866 "paths ending at %llu\n",
1867 inode->i_ino, insert_cpos, cpos,
1868 (unsigned long long)
1869 path_leaf_bh(left_path)->b_blocknr);
1871 if (split == SPLIT_NONE &&
1872 ocfs2_rotate_requires_path_adjustment(left_path,
1876 * We've rotated the tree as much as we
1877 * should. The rest is up to
1878 * ocfs2_insert_path() to complete, after the
1879 * record insertion. We indicate this
1880 * situation by returning the left path.
1882 * The reason we don't adjust the records here
1883 * before the record insert is that an error
1884 * later might break the rule where a parent
1885 * record e_cpos will reflect the actual
1886 * e_cpos of the 1st nonempty record of the
1889 *ret_left_path = left_path;
1893 start = ocfs2_find_subtree_root(inode, left_path, right_path);
1895 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
1897 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
1898 right_path->p_tree_depth);
1900 ret = ocfs2_extend_rotate_transaction(handle, start,
1901 orig_credits, right_path);
1907 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
1914 if (split != SPLIT_NONE &&
1915 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
1918 * A rotate moves the rightmost left leaf
1919 * record over to the leftmost right leaf
1920 * slot. If we're doing an extent split
1921 * instead of a real insert, then we have to
1922 * check that the extent to be split wasn't
1923 * just moved over. If it was, then we can
1924 * exit here, passing left_path back -
1925 * ocfs2_split_extent() is smart enough to
1926 * search both leaves.
1928 *ret_left_path = left_path;
1933 * There is no need to re-read the next right path
1934 * as we know that it'll be our current left
1935 * path. Optimize by copying values instead.
1937 ocfs2_mv_path(right_path, left_path);
1939 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
1948 ocfs2_free_path(left_path);
1954 static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
1955 struct ocfs2_path *path)
1958 struct ocfs2_extent_rec *rec;
1959 struct ocfs2_extent_list *el;
1960 struct ocfs2_extent_block *eb;
1963 /* Path should always be rightmost. */
1964 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
1965 BUG_ON(eb->h_next_leaf_blk != 0ULL);
1968 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
1969 idx = le16_to_cpu(el->l_next_free_rec) - 1;
1970 rec = &el->l_recs[idx];
1971 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
1973 for (i = 0; i < path->p_tree_depth; i++) {
1974 el = path->p_node[i].el;
1975 idx = le16_to_cpu(el->l_next_free_rec) - 1;
1976 rec = &el->l_recs[idx];
1978 rec->e_int_clusters = cpu_to_le32(range);
1979 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
1981 ocfs2_journal_dirty(handle, path->p_node[i].bh);
1985 static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
1986 struct ocfs2_cached_dealloc_ctxt *dealloc,
1987 struct ocfs2_path *path, int unlink_start)
1990 struct ocfs2_extent_block *eb;
1991 struct ocfs2_extent_list *el;
1992 struct buffer_head *bh;
1994 for(i = unlink_start; i < path_num_items(path); i++) {
1995 bh = path->p_node[i].bh;
1997 eb = (struct ocfs2_extent_block *)bh->b_data;
1999 * Not all nodes might have had their final count
2000 * decremented by the caller - handle this here.
2003 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2005 "Inode %llu, attempted to remove extent block "
2006 "%llu with %u records\n",
2007 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2008 (unsigned long long)le64_to_cpu(eb->h_blkno),
2009 le16_to_cpu(el->l_next_free_rec));
2011 ocfs2_journal_dirty(handle, bh);
2012 ocfs2_remove_from_cache(inode, bh);
2016 el->l_next_free_rec = 0;
2017 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2019 ocfs2_journal_dirty(handle, bh);
2021 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2025 ocfs2_remove_from_cache(inode, bh);
2029 static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2030 struct ocfs2_path *left_path,
2031 struct ocfs2_path *right_path,
2033 struct ocfs2_cached_dealloc_ctxt *dealloc)
2036 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2037 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2038 struct ocfs2_extent_list *el;
2039 struct ocfs2_extent_block *eb;
2041 el = path_leaf_el(left_path);
2043 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2045 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2046 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2049 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2051 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2052 le16_add_cpu(&root_el->l_next_free_rec, -1);
2054 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2055 eb->h_next_leaf_blk = 0;
2057 ocfs2_journal_dirty(handle, root_bh);
2058 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2060 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2064 static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2065 struct ocfs2_path *left_path,
2066 struct ocfs2_path *right_path,
2068 struct ocfs2_cached_dealloc_ctxt *dealloc,
2071 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2072 struct buffer_head *root_bh, *di_bh = path_root_bh(right_path);
2073 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2074 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2075 struct ocfs2_extent_block *eb;
2079 right_leaf_el = path_leaf_el(right_path);
2080 left_leaf_el = path_leaf_el(left_path);
2081 root_bh = left_path->p_node[subtree_index].bh;
2082 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2084 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2087 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2088 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2090 * It's legal for us to proceed if the right leaf is
2091 * the rightmost one and it has an empty extent. There
2092 * are two cases to handle - whether the leaf will be
2093 * empty after removal or not. If the leaf isn't empty
2094 * then just remove the empty extent up front. The
2095 * next block will handle empty leaves by flagging
2098 * Non rightmost leaves will throw -EAGAIN and the
2099 * caller can manually move the subtree and retry.
2102 if (eb->h_next_leaf_blk != 0ULL)
2105 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2106 ret = ocfs2_journal_access(handle, inode,
2107 path_leaf_bh(right_path),
2108 OCFS2_JOURNAL_ACCESS_WRITE);
2114 ocfs2_remove_empty_extent(right_leaf_el);
2116 right_has_empty = 1;
2119 if (eb->h_next_leaf_blk == 0ULL &&
2120 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2122 * We have to update i_last_eb_blk during the meta
2125 ret = ocfs2_journal_access(handle, inode, di_bh,
2126 OCFS2_JOURNAL_ACCESS_WRITE);
2132 del_right_subtree = 1;
2136 * Getting here with an empty extent in the right path implies
2137 * that it's the rightmost path and will be deleted.
2139 BUG_ON(right_has_empty && !del_right_subtree);
2141 ret = ocfs2_journal_access(handle, inode, root_bh,
2142 OCFS2_JOURNAL_ACCESS_WRITE);
2148 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2149 ret = ocfs2_journal_access(handle, inode,
2150 right_path->p_node[i].bh,
2151 OCFS2_JOURNAL_ACCESS_WRITE);
2157 ret = ocfs2_journal_access(handle, inode,
2158 left_path->p_node[i].bh,
2159 OCFS2_JOURNAL_ACCESS_WRITE);
2166 if (!right_has_empty) {
2168 * Only do this if we're moving a real
2169 * record. Otherwise, the action is delayed until
2170 * after removal of the right path in which case we
2171 * can do a simple shift to remove the empty extent.
2173 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2174 memset(&right_leaf_el->l_recs[0], 0,
2175 sizeof(struct ocfs2_extent_rec));
2177 if (eb->h_next_leaf_blk == 0ULL) {
2179 * Move recs over to get rid of empty extent, decrease
2180 * next_free. This is allowed to remove the last
2181 * extent in our leaf (setting l_next_free_rec to
2182 * zero) - the delete code below won't care.
2184 ocfs2_remove_empty_extent(right_leaf_el);
2187 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2190 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2194 if (del_right_subtree) {
2195 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2196 subtree_index, dealloc);
2197 ocfs2_update_edge_lengths(inode, handle, left_path);
2199 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2200 di->i_last_eb_blk = eb->h_blkno;
2203 * Removal of the extent in the left leaf was skipped
2204 * above so we could delete the right path
2207 if (right_has_empty)
2208 ocfs2_remove_empty_extent(left_leaf_el);
2210 ret = ocfs2_journal_dirty(handle, di_bh);
2216 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2224 * Given a full path, determine what cpos value would return us a path
2225 * containing the leaf immediately to the right of the current one.
2227 * Will return zero if the path passed in is already the rightmost path.
2229 * This looks similar, but is subtly different to
2230 * ocfs2_find_cpos_for_left_leaf().
2232 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2233 struct ocfs2_path *path, u32 *cpos)
2237 struct ocfs2_extent_list *el;
2241 if (path->p_tree_depth == 0)
2244 blkno = path_leaf_bh(path)->b_blocknr;
2246 /* Start at the tree node just above the leaf and work our way up. */
2247 i = path->p_tree_depth - 1;
2251 el = path->p_node[i].el;
2254 * Find the extent record just after the one in our
2257 next_free = le16_to_cpu(el->l_next_free_rec);
2258 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2259 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2260 if (j == (next_free - 1)) {
2263 * We've determined that the
2264 * path specified is already
2265 * the rightmost one - return a
2271 * The rightmost record points to our
2272 * leaf - we need to travel up the
2278 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2284 * If we got here, we never found a valid node where
2285 * the tree indicated one should be.
2288 "Invalid extent tree at extent block %llu\n",
2289 (unsigned long long)blkno);
2294 blkno = path->p_node[i].bh->b_blocknr;
2302 static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2304 struct buffer_head *bh,
2305 struct ocfs2_extent_list *el)
2309 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2312 ret = ocfs2_journal_access(handle, inode, bh,
2313 OCFS2_JOURNAL_ACCESS_WRITE);
2319 ocfs2_remove_empty_extent(el);
2321 ret = ocfs2_journal_dirty(handle, bh);
2329 static int __ocfs2_rotate_tree_left(struct inode *inode,
2330 handle_t *handle, int orig_credits,
2331 struct ocfs2_path *path,
2332 struct ocfs2_cached_dealloc_ctxt *dealloc,
2333 struct ocfs2_path **empty_extent_path)
2335 int ret, subtree_root, deleted;
2337 struct ocfs2_path *left_path = NULL;
2338 struct ocfs2_path *right_path = NULL;
2340 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2342 *empty_extent_path = NULL;
2344 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2351 left_path = ocfs2_new_path(path_root_bh(path),
2352 path_root_el(path));
2359 ocfs2_cp_path(left_path, path);
2361 right_path = ocfs2_new_path(path_root_bh(path),
2362 path_root_el(path));
2369 while (right_cpos) {
2370 ret = ocfs2_find_path(inode, right_path, right_cpos);
2376 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2379 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2381 (unsigned long long)
2382 right_path->p_node[subtree_root].bh->b_blocknr,
2383 right_path->p_tree_depth);
2385 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2386 orig_credits, left_path);
2392 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2393 right_path, subtree_root,
2395 if (ret == -EAGAIN) {
2397 * The rotation has to temporarily stop due to
2398 * the right subtree having an empty
2399 * extent. Pass it back to the caller for a
2402 *empty_extent_path = right_path;
2412 * The subtree rotate might have removed records on
2413 * the rightmost edge. If so, then rotation is
2419 ocfs2_mv_path(left_path, right_path);
2421 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2430 ocfs2_free_path(right_path);
2431 ocfs2_free_path(left_path);
2436 static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2437 struct ocfs2_path *path,
2438 struct ocfs2_cached_dealloc_ctxt *dealloc)
2440 int ret, subtree_index;
2442 struct ocfs2_path *left_path = NULL;
2443 struct ocfs2_dinode *di;
2444 struct ocfs2_extent_block *eb;
2445 struct ocfs2_extent_list *el;
2448 * XXX: This code assumes that the root is an inode, which is
2449 * true for now but may change as tree code gets generic.
2451 di = (struct ocfs2_dinode *)path_root_bh(path)->b_data;
2452 if (!OCFS2_IS_VALID_DINODE(di)) {
2454 ocfs2_error(inode->i_sb,
2455 "Inode %llu has invalid path root",
2456 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2461 * There's two ways we handle this depending on
2462 * whether path is the only existing one.
2464 ret = ocfs2_extend_rotate_transaction(handle, 0,
2465 handle->h_buffer_credits,
2472 ret = ocfs2_journal_access_path(inode, handle, path);
2478 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2486 * We have a path to the left of this one - it needs
2489 left_path = ocfs2_new_path(path_root_bh(path),
2490 path_root_el(path));
2497 ret = ocfs2_find_path(inode, left_path, cpos);
2503 ret = ocfs2_journal_access_path(inode, handle, left_path);
2509 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2511 ocfs2_unlink_subtree(inode, handle, left_path, path,
2512 subtree_index, dealloc);
2513 ocfs2_update_edge_lengths(inode, handle, left_path);
2515 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2516 di->i_last_eb_blk = eb->h_blkno;
2519 * 'path' is also the leftmost path which
2520 * means it must be the only one. This gets
2521 * handled differently because we want to
2522 * revert the inode back to having extents
2525 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2527 el = &di->id2.i_list;
2528 el->l_tree_depth = 0;
2529 el->l_next_free_rec = 0;
2530 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2532 di->i_last_eb_blk = 0;
2535 ocfs2_journal_dirty(handle, path_root_bh(path));
2538 ocfs2_free_path(left_path);
2543 * Left rotation of btree records.
2545 * In many ways, this is (unsurprisingly) the opposite of right
2546 * rotation. We start at some non-rightmost path containing an empty
2547 * extent in the leaf block. The code works its way to the rightmost
2548 * path by rotating records to the left in every subtree.
2550 * This is used by any code which reduces the number of extent records
2551 * in a leaf. After removal, an empty record should be placed in the
2552 * leftmost list position.
2554 * This won't handle a length update of the rightmost path records if
2555 * the rightmost tree leaf record is removed so the caller is
2556 * responsible for detecting and correcting that.
2558 static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2559 struct ocfs2_path *path,
2560 struct ocfs2_cached_dealloc_ctxt *dealloc)
2562 int ret, orig_credits = handle->h_buffer_credits;
2563 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2564 struct ocfs2_extent_block *eb;
2565 struct ocfs2_extent_list *el;
2567 el = path_leaf_el(path);
2568 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2571 if (path->p_tree_depth == 0) {
2572 rightmost_no_delete:
2574 * In-inode extents. This is trivially handled, so do
2577 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2579 path_leaf_el(path));
2586 * Handle rightmost branch now. There's several cases:
2587 * 1) simple rotation leaving records in there. That's trivial.
2588 * 2) rotation requiring a branch delete - there's no more
2589 * records left. Two cases of this:
2590 * a) There are branches to the left.
2591 * b) This is also the leftmost (the only) branch.
2593 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2594 * 2a) we need the left branch so that we can update it with the unlink
2595 * 2b) we need to bring the inode back to inline extents.
2598 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2600 if (eb->h_next_leaf_blk == 0) {
2602 * This gets a bit tricky if we're going to delete the
2603 * rightmost path. Get the other cases out of the way
2606 if (le16_to_cpu(el->l_next_free_rec) > 1)
2607 goto rightmost_no_delete;
2609 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2611 ocfs2_error(inode->i_sb,
2612 "Inode %llu has empty extent block at %llu",
2613 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2614 (unsigned long long)le64_to_cpu(eb->h_blkno));
2619 * XXX: The caller can not trust "path" any more after
2620 * this as it will have been deleted. What do we do?
2622 * In theory the rotate-for-merge code will never get
2623 * here because it'll always ask for a rotate in a
2627 ret = ocfs2_remove_rightmost_path(inode, handle, path,
2635 * Now we can loop, remembering the path we get from -EAGAIN
2636 * and restarting from there.
2639 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
2640 dealloc, &restart_path);
2641 if (ret && ret != -EAGAIN) {
2646 while (ret == -EAGAIN) {
2647 tmp_path = restart_path;
2648 restart_path = NULL;
2650 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2653 if (ret && ret != -EAGAIN) {
2658 ocfs2_free_path(tmp_path);
2666 ocfs2_free_path(tmp_path);
2667 ocfs2_free_path(restart_path);
2671 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2674 struct ocfs2_extent_rec *rec = &el->l_recs[index];
2677 if (rec->e_leaf_clusters == 0) {
2679 * We consumed all of the merged-from record. An empty
2680 * extent cannot exist anywhere but the 1st array
2681 * position, so move things over if the merged-from
2682 * record doesn't occupy that position.
2684 * This creates a new empty extent so the caller
2685 * should be smart enough to have removed any existing
2689 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
2690 size = index * sizeof(struct ocfs2_extent_rec);
2691 memmove(&el->l_recs[1], &el->l_recs[0], size);
2695 * Always memset - the caller doesn't check whether it
2696 * created an empty extent, so there could be junk in
2699 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2704 * Remove split_rec clusters from the record at index and merge them
2705 * onto the beginning of the record at index + 1.
2707 static int ocfs2_merge_rec_right(struct inode *inode, struct buffer_head *bh,
2709 struct ocfs2_extent_rec *split_rec,
2710 struct ocfs2_extent_list *el, int index)
2713 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
2714 struct ocfs2_extent_rec *left_rec;
2715 struct ocfs2_extent_rec *right_rec;
2717 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
2719 left_rec = &el->l_recs[index];
2720 right_rec = &el->l_recs[index + 1];
2722 ret = ocfs2_journal_access(handle, inode, bh,
2723 OCFS2_JOURNAL_ACCESS_WRITE);
2729 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
2731 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
2732 le64_add_cpu(&right_rec->e_blkno,
2733 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
2734 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
2736 ocfs2_cleanup_merge(el, index);
2738 ret = ocfs2_journal_dirty(handle, bh);
2747 * Remove split_rec clusters from the record at index and merge them
2748 * onto the tail of the record at index - 1.
2750 static int ocfs2_merge_rec_left(struct inode *inode, struct buffer_head *bh,
2752 struct ocfs2_extent_rec *split_rec,
2753 struct ocfs2_extent_list *el, int index)
2755 int ret, has_empty_extent = 0;
2756 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
2757 struct ocfs2_extent_rec *left_rec;
2758 struct ocfs2_extent_rec *right_rec;
2762 left_rec = &el->l_recs[index - 1];
2763 right_rec = &el->l_recs[index];
2764 if (ocfs2_is_empty_extent(&el->l_recs[0]))
2765 has_empty_extent = 1;
2767 ret = ocfs2_journal_access(handle, inode, bh,
2768 OCFS2_JOURNAL_ACCESS_WRITE);
2774 if (has_empty_extent && index == 1) {
2776 * The easy case - we can just plop the record right in.
2778 *left_rec = *split_rec;
2780 has_empty_extent = 0;
2782 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
2785 le32_add_cpu(&right_rec->e_cpos, split_clusters);
2786 le64_add_cpu(&right_rec->e_blkno,
2787 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
2788 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
2790 ocfs2_cleanup_merge(el, index);
2792 ret = ocfs2_journal_dirty(handle, bh);
2800 static int ocfs2_try_to_merge_extent(struct inode *inode,
2802 struct ocfs2_path *left_path,
2804 struct ocfs2_extent_rec *split_rec,
2805 struct ocfs2_cached_dealloc_ctxt *dealloc,
2806 struct ocfs2_merge_ctxt *ctxt)
2810 struct ocfs2_extent_list *el = path_leaf_el(left_path);
2811 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
2813 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
2815 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
2817 * The merge code will need to create an empty
2818 * extent to take the place of the newly
2819 * emptied slot. Remove any pre-existing empty
2820 * extents - having more than one in a leaf is
2823 ret = ocfs2_rotate_tree_left(inode, handle, left_path,
2830 rec = &el->l_recs[split_index];
2833 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
2835 * Left-right contig implies this.
2837 BUG_ON(!ctxt->c_split_covers_rec);
2838 BUG_ON(split_index == 0);
2841 * Since the leftright insert always covers the entire
2842 * extent, this call will delete the insert record
2843 * entirely, resulting in an empty extent record added to
2846 * Since the adding of an empty extent shifts
2847 * everything back to the right, there's no need to
2848 * update split_index here.
2850 ret = ocfs2_merge_rec_left(inode, path_leaf_bh(left_path),
2851 handle, split_rec, el, split_index);
2858 * We can only get this from logic error above.
2860 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
2863 * The left merge left us with an empty extent, remove
2866 ret = ocfs2_rotate_tree_left(inode, handle, left_path, dealloc);
2872 rec = &el->l_recs[split_index];
2875 * Note that we don't pass split_rec here on purpose -
2876 * we've merged it into the left side.
2878 ret = ocfs2_merge_rec_right(inode, path_leaf_bh(left_path),
2879 handle, rec, el, split_index);
2885 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
2887 ret = ocfs2_rotate_tree_left(inode, handle, left_path,
2890 * Error from this last rotate is not critical, so
2891 * print but don't bubble it up.
2898 * Merge a record to the left or right.
2900 * 'contig_type' is relative to the existing record,
2901 * so for example, if we're "right contig", it's to
2902 * the record on the left (hence the left merge).
2904 if (ctxt->c_contig_type == CONTIG_RIGHT) {
2905 ret = ocfs2_merge_rec_left(inode,
2906 path_leaf_bh(left_path),
2907 handle, split_rec, el,
2914 ret = ocfs2_merge_rec_right(inode,
2915 path_leaf_bh(left_path),
2916 handle, split_rec, el,
2924 if (ctxt->c_split_covers_rec) {
2926 * The merge may have left an empty extent in
2927 * our leaf. Try to rotate it away.
2929 ret = ocfs2_rotate_tree_left(inode, handle, left_path,
2941 static void ocfs2_subtract_from_rec(struct super_block *sb,
2942 enum ocfs2_split_type split,
2943 struct ocfs2_extent_rec *rec,
2944 struct ocfs2_extent_rec *split_rec)
2948 len_blocks = ocfs2_clusters_to_blocks(sb,
2949 le16_to_cpu(split_rec->e_leaf_clusters));
2951 if (split == SPLIT_LEFT) {
2953 * Region is on the left edge of the existing
2956 le32_add_cpu(&rec->e_cpos,
2957 le16_to_cpu(split_rec->e_leaf_clusters));
2958 le64_add_cpu(&rec->e_blkno, len_blocks);
2959 le16_add_cpu(&rec->e_leaf_clusters,
2960 -le16_to_cpu(split_rec->e_leaf_clusters));
2963 * Region is on the right edge of the existing
2966 le16_add_cpu(&rec->e_leaf_clusters,
2967 -le16_to_cpu(split_rec->e_leaf_clusters));
2972 * Do the final bits of extent record insertion at the target leaf
2973 * list. If this leaf is part of an allocation tree, it is assumed
2974 * that the tree above has been prepared.
2976 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
2977 struct ocfs2_extent_list *el,
2978 struct ocfs2_insert_type *insert,
2979 struct inode *inode)
2981 int i = insert->ins_contig_index;
2983 struct ocfs2_extent_rec *rec;
2985 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
2987 if (insert->ins_split != SPLIT_NONE) {
2988 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
2990 rec = &el->l_recs[i];
2991 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
2997 * Contiguous insert - either left or right.
2999 if (insert->ins_contig != CONTIG_NONE) {
3000 rec = &el->l_recs[i];
3001 if (insert->ins_contig == CONTIG_LEFT) {
3002 rec->e_blkno = insert_rec->e_blkno;
3003 rec->e_cpos = insert_rec->e_cpos;
3005 le16_add_cpu(&rec->e_leaf_clusters,
3006 le16_to_cpu(insert_rec->e_leaf_clusters));
3011 * Handle insert into an empty leaf.
3013 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3014 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3015 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3016 el->l_recs[0] = *insert_rec;
3017 el->l_next_free_rec = cpu_to_le16(1);
3024 if (insert->ins_appending == APPEND_TAIL) {
3025 i = le16_to_cpu(el->l_next_free_rec) - 1;
3026 rec = &el->l_recs[i];
3027 range = le32_to_cpu(rec->e_cpos)
3028 + le16_to_cpu(rec->e_leaf_clusters);
3029 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3031 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3032 le16_to_cpu(el->l_count),
3033 "inode %lu, depth %u, count %u, next free %u, "
3034 "rec.cpos %u, rec.clusters %u, "
3035 "insert.cpos %u, insert.clusters %u\n",
3037 le16_to_cpu(el->l_tree_depth),
3038 le16_to_cpu(el->l_count),
3039 le16_to_cpu(el->l_next_free_rec),
3040 le32_to_cpu(el->l_recs[i].e_cpos),
3041 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3042 le32_to_cpu(insert_rec->e_cpos),
3043 le16_to_cpu(insert_rec->e_leaf_clusters));
3045 el->l_recs[i] = *insert_rec;
3046 le16_add_cpu(&el->l_next_free_rec, 1);
3052 * Ok, we have to rotate.
3054 * At this point, it is safe to assume that inserting into an
3055 * empty leaf and appending to a leaf have both been handled
3058 * This leaf needs to have space, either by the empty 1st
3059 * extent record, or by virtue of an l_next_rec < l_count.
3061 ocfs2_rotate_leaf(el, insert_rec);
3064 static inline void ocfs2_update_dinode_clusters(struct inode *inode,
3065 struct ocfs2_dinode *di,
3068 le32_add_cpu(&di->i_clusters, clusters);
3069 spin_lock(&OCFS2_I(inode)->ip_lock);
3070 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
3071 spin_unlock(&OCFS2_I(inode)->ip_lock);
3074 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3076 struct ocfs2_path *path,
3077 struct ocfs2_extent_rec *insert_rec)
3079 int ret, i, next_free;
3080 struct buffer_head *bh;
3081 struct ocfs2_extent_list *el;
3082 struct ocfs2_extent_rec *rec;
3085 * Update everything except the leaf block.
3087 for (i = 0; i < path->p_tree_depth; i++) {
3088 bh = path->p_node[i].bh;
3089 el = path->p_node[i].el;
3091 next_free = le16_to_cpu(el->l_next_free_rec);
3092 if (next_free == 0) {
3093 ocfs2_error(inode->i_sb,
3094 "Dinode %llu has a bad extent list",
3095 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3100 rec = &el->l_recs[next_free - 1];
3102 rec->e_int_clusters = insert_rec->e_cpos;
3103 le32_add_cpu(&rec->e_int_clusters,
3104 le16_to_cpu(insert_rec->e_leaf_clusters));
3105 le32_add_cpu(&rec->e_int_clusters,
3106 -le32_to_cpu(rec->e_cpos));
3108 ret = ocfs2_journal_dirty(handle, bh);
3115 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3116 struct ocfs2_extent_rec *insert_rec,
3117 struct ocfs2_path *right_path,
3118 struct ocfs2_path **ret_left_path)
3121 struct ocfs2_extent_list *el;
3122 struct ocfs2_path *left_path = NULL;
3124 *ret_left_path = NULL;
3127 * This shouldn't happen for non-trees. The extent rec cluster
3128 * count manipulation below only works for interior nodes.
3130 BUG_ON(right_path->p_tree_depth == 0);
3133 * If our appending insert is at the leftmost edge of a leaf,
3134 * then we might need to update the rightmost records of the
3137 el = path_leaf_el(right_path);
3138 next_free = le16_to_cpu(el->l_next_free_rec);
3139 if (next_free == 0 ||
3140 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3143 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3150 mlog(0, "Append may need a left path update. cpos: %u, "
3151 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3155 * No need to worry if the append is already in the
3159 left_path = ocfs2_new_path(path_root_bh(right_path),
3160 path_root_el(right_path));
3167 ret = ocfs2_find_path(inode, left_path, left_cpos);
3174 * ocfs2_insert_path() will pass the left_path to the
3180 ret = ocfs2_journal_access_path(inode, handle, right_path);
3186 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
3188 *ret_left_path = left_path;
3192 ocfs2_free_path(left_path);
3197 static void ocfs2_split_record(struct inode *inode,
3198 struct ocfs2_path *left_path,
3199 struct ocfs2_path *right_path,
3200 struct ocfs2_extent_rec *split_rec,
3201 enum ocfs2_split_type split)
3204 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3205 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3206 struct ocfs2_extent_rec *rec, *tmprec;
3208 right_el = path_leaf_el(right_path);;
3210 left_el = path_leaf_el(left_path);
3213 insert_el = right_el;
3214 index = ocfs2_search_extent_list(el, cpos);
3216 if (index == 0 && left_path) {
3217 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3220 * This typically means that the record
3221 * started in the left path but moved to the
3222 * right as a result of rotation. We either
3223 * move the existing record to the left, or we
3224 * do the later insert there.
3226 * In this case, the left path should always
3227 * exist as the rotate code will have passed
3228 * it back for a post-insert update.
3231 if (split == SPLIT_LEFT) {
3233 * It's a left split. Since we know
3234 * that the rotate code gave us an
3235 * empty extent in the left path, we
3236 * can just do the insert there.
3238 insert_el = left_el;
3241 * Right split - we have to move the
3242 * existing record over to the left
3243 * leaf. The insert will be into the
3244 * newly created empty extent in the
3247 tmprec = &right_el->l_recs[index];
3248 ocfs2_rotate_leaf(left_el, tmprec);
3251 memset(tmprec, 0, sizeof(*tmprec));
3252 index = ocfs2_search_extent_list(left_el, cpos);
3253 BUG_ON(index == -1);
3258 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3260 * Left path is easy - we can just allow the insert to
3264 insert_el = left_el;
3265 index = ocfs2_search_extent_list(el, cpos);
3266 BUG_ON(index == -1);
3269 rec = &el->l_recs[index];
3270 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3271 ocfs2_rotate_leaf(insert_el, split_rec);
3275 * This function only does inserts on an allocation b-tree. For dinode
3276 * lists, ocfs2_insert_at_leaf() is called directly.
3278 * right_path is the path we want to do the actual insert
3279 * in. left_path should only be passed in if we need to update that
3280 * portion of the tree after an edge insert.
3282 static int ocfs2_insert_path(struct inode *inode,
3284 struct ocfs2_path *left_path,
3285 struct ocfs2_path *right_path,
3286 struct ocfs2_extent_rec *insert_rec,
3287 struct ocfs2_insert_type *insert)
3289 int ret, subtree_index;
3290 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
3293 * Pass both paths to the journal. The majority of inserts
3294 * will be touching all components anyway.
3296 ret = ocfs2_journal_access_path(inode, handle, right_path);
3303 int credits = handle->h_buffer_credits;
3306 * There's a chance that left_path got passed back to
3307 * us without being accounted for in the
3308 * journal. Extend our transaction here to be sure we
3309 * can change those blocks.
3311 credits += left_path->p_tree_depth;
3313 ret = ocfs2_extend_trans(handle, credits);
3319 ret = ocfs2_journal_access_path(inode, handle, left_path);
3326 if (insert->ins_split != SPLIT_NONE) {
3328 * We could call ocfs2_insert_at_leaf() for some types
3329 * of splits, but it's easier to just let one seperate
3330 * function sort it all out.
3332 ocfs2_split_record(inode, left_path, right_path,
3333 insert_rec, insert->ins_split);
3335 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3338 ret = ocfs2_journal_dirty(handle, leaf_bh);
3344 * The rotate code has indicated that we need to fix
3345 * up portions of the tree after the insert.
3347 * XXX: Should we extend the transaction here?
3349 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3351 ocfs2_complete_edge_insert(inode, handle, left_path,
3352 right_path, subtree_index);
3360 static int ocfs2_do_insert_extent(struct inode *inode,
3362 struct buffer_head *di_bh,
3363 struct ocfs2_extent_rec *insert_rec,
3364 struct ocfs2_insert_type *type)
3366 int ret, rotate = 0;
3368 struct ocfs2_path *right_path = NULL;
3369 struct ocfs2_path *left_path = NULL;
3370 struct ocfs2_dinode *di;
3371 struct ocfs2_extent_list *el;
3373 di = (struct ocfs2_dinode *) di_bh->b_data;
3374 el = &di->id2.i_list;
3376 ret = ocfs2_journal_access(handle, inode, di_bh,
3377 OCFS2_JOURNAL_ACCESS_WRITE);
3383 if (le16_to_cpu(el->l_tree_depth) == 0) {
3384 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3385 goto out_update_clusters;
3388 right_path = ocfs2_new_inode_path(di_bh);
3396 * Determine the path to start with. Rotations need the
3397 * rightmost path, everything else can go directly to the
3400 cpos = le32_to_cpu(insert_rec->e_cpos);
3401 if (type->ins_appending == APPEND_NONE &&
3402 type->ins_contig == CONTIG_NONE) {
3407 ret = ocfs2_find_path(inode, right_path, cpos);
3414 * Rotations and appends need special treatment - they modify
3415 * parts of the tree's above them.
3417 * Both might pass back a path immediate to the left of the
3418 * one being inserted to. This will be cause
3419 * ocfs2_insert_path() to modify the rightmost records of
3420 * left_path to account for an edge insert.
3422 * XXX: When modifying this code, keep in mind that an insert
3423 * can wind up skipping both of these two special cases...
3426 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
3427 le32_to_cpu(insert_rec->e_cpos),
3428 right_path, &left_path);
3433 } else if (type->ins_appending == APPEND_TAIL
3434 && type->ins_contig != CONTIG_LEFT) {
3435 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
3436 right_path, &left_path);
3443 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
3450 out_update_clusters:
3451 if (type->ins_split == SPLIT_NONE)
3452 ocfs2_update_dinode_clusters(inode, di,
3453 le16_to_cpu(insert_rec->e_leaf_clusters));
3455 ret = ocfs2_journal_dirty(handle, di_bh);
3460 ocfs2_free_path(left_path);
3461 ocfs2_free_path(right_path);
3466 static enum ocfs2_contig_type
3467 ocfs2_figure_merge_contig_type(struct inode *inode,
3468 struct ocfs2_extent_list *el, int index,
3469 struct ocfs2_extent_rec *split_rec)
3471 struct ocfs2_extent_rec *rec;
3472 enum ocfs2_contig_type ret = CONTIG_NONE;
3475 * We're careful to check for an empty extent record here -
3476 * the merge code will know what to do if it sees one.
3480 rec = &el->l_recs[index - 1];
3481 if (index == 1 && ocfs2_is_empty_extent(rec)) {
3482 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
3485 ret = ocfs2_extent_contig(inode, rec, split_rec);
3489 if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) {
3490 enum ocfs2_contig_type contig_type;
3492 rec = &el->l_recs[index + 1];
3493 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
3495 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
3496 ret = CONTIG_LEFTRIGHT;
3497 else if (ret == CONTIG_NONE)
3504 static void ocfs2_figure_contig_type(struct inode *inode,
3505 struct ocfs2_insert_type *insert,
3506 struct ocfs2_extent_list *el,
3507 struct ocfs2_extent_rec *insert_rec)
3510 enum ocfs2_contig_type contig_type = CONTIG_NONE;
3512 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3514 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
3515 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
3517 if (contig_type != CONTIG_NONE) {
3518 insert->ins_contig_index = i;
3522 insert->ins_contig = contig_type;
3526 * This should only be called against the righmost leaf extent list.
3528 * ocfs2_figure_appending_type() will figure out whether we'll have to
3529 * insert at the tail of the rightmost leaf.
3531 * This should also work against the dinode list for tree's with 0
3532 * depth. If we consider the dinode list to be the rightmost leaf node
3533 * then the logic here makes sense.
3535 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
3536 struct ocfs2_extent_list *el,
3537 struct ocfs2_extent_rec *insert_rec)
3540 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
3541 struct ocfs2_extent_rec *rec;
3543 insert->ins_appending = APPEND_NONE;
3545 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3547 if (!el->l_next_free_rec)
3548 goto set_tail_append;
3550 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
3551 /* Were all records empty? */
3552 if (le16_to_cpu(el->l_next_free_rec) == 1)
3553 goto set_tail_append;
3556 i = le16_to_cpu(el->l_next_free_rec) - 1;
3557 rec = &el->l_recs[i];
3560 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
3561 goto set_tail_append;
3566 insert->ins_appending = APPEND_TAIL;
3570 * Helper function called at the begining of an insert.
3572 * This computes a few things that are commonly used in the process of
3573 * inserting into the btree:
3574 * - Whether the new extent is contiguous with an existing one.
3575 * - The current tree depth.
3576 * - Whether the insert is an appending one.
3577 * - The total # of free records in the tree.
3579 * All of the information is stored on the ocfs2_insert_type
3582 static int ocfs2_figure_insert_type(struct inode *inode,
3583 struct buffer_head *di_bh,
3584 struct buffer_head **last_eb_bh,
3585 struct ocfs2_extent_rec *insert_rec,
3587 struct ocfs2_insert_type *insert)
3590 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3591 struct ocfs2_extent_block *eb;
3592 struct ocfs2_extent_list *el;
3593 struct ocfs2_path *path = NULL;
3594 struct buffer_head *bh = NULL;
3596 insert->ins_split = SPLIT_NONE;
3598 el = &di->id2.i_list;
3599 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
3601 if (el->l_tree_depth) {
3603 * If we have tree depth, we read in the
3604 * rightmost extent block ahead of time as
3605 * ocfs2_figure_insert_type() and ocfs2_add_branch()
3606 * may want it later.
3608 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
3609 le64_to_cpu(di->i_last_eb_blk), &bh,
3610 OCFS2_BH_CACHED, inode);
3615 eb = (struct ocfs2_extent_block *) bh->b_data;
3620 * Unless we have a contiguous insert, we'll need to know if
3621 * there is room left in our allocation tree for another
3624 * XXX: This test is simplistic, we can search for empty
3625 * extent records too.
3627 *free_records = le16_to_cpu(el->l_count) -
3628 le16_to_cpu(el->l_next_free_rec);
3630 if (!insert->ins_tree_depth) {
3631 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
3632 ocfs2_figure_appending_type(insert, el, insert_rec);
3636 path = ocfs2_new_inode_path(di_bh);
3644 * In the case that we're inserting past what the tree
3645 * currently accounts for, ocfs2_find_path() will return for
3646 * us the rightmost tree path. This is accounted for below in
3647 * the appending code.
3649 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
3655 el = path_leaf_el(path);
3658 * Now that we have the path, there's two things we want to determine:
3659 * 1) Contiguousness (also set contig_index if this is so)
3661 * 2) Are we doing an append? We can trivially break this up
3662 * into two types of appends: simple record append, or a
3663 * rotate inside the tail leaf.
3665 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
3668 * The insert code isn't quite ready to deal with all cases of
3669 * left contiguousness. Specifically, if it's an insert into
3670 * the 1st record in a leaf, it will require the adjustment of
3671 * cluster count on the last record of the path directly to it's
3672 * left. For now, just catch that case and fool the layers
3673 * above us. This works just fine for tree_depth == 0, which
3674 * is why we allow that above.
3676 if (insert->ins_contig == CONTIG_LEFT &&
3677 insert->ins_contig_index == 0)
3678 insert->ins_contig = CONTIG_NONE;
3681 * Ok, so we can simply compare against last_eb to figure out
3682 * whether the path doesn't exist. This will only happen in
3683 * the case that we're doing a tail append, so maybe we can
3684 * take advantage of that information somehow.
3686 if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) {
3688 * Ok, ocfs2_find_path() returned us the rightmost
3689 * tree path. This might be an appending insert. There are
3691 * 1) We're doing a true append at the tail:
3692 * -This might even be off the end of the leaf
3693 * 2) We're "appending" by rotating in the tail
3695 ocfs2_figure_appending_type(insert, el, insert_rec);
3699 ocfs2_free_path(path);
3709 * Insert an extent into an inode btree.
3711 * The caller needs to update fe->i_clusters
3713 int ocfs2_insert_extent(struct ocfs2_super *osb,
3715 struct inode *inode,
3716 struct buffer_head *fe_bh,
3721 struct ocfs2_alloc_context *meta_ac)
3724 int uninitialized_var(free_records);
3725 struct buffer_head *last_eb_bh = NULL;
3726 struct ocfs2_insert_type insert = {0, };
3727 struct ocfs2_extent_rec rec;
3729 mlog(0, "add %u clusters at position %u to inode %llu\n",
3730 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3732 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
3733 (OCFS2_I(inode)->ip_clusters != cpos),
3734 "Device %s, asking for sparse allocation: inode %llu, "
3735 "cpos %u, clusters %u\n",
3737 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
3738 OCFS2_I(inode)->ip_clusters);
3740 memset(&rec, 0, sizeof(rec));
3741 rec.e_cpos = cpu_to_le32(cpos);
3742 rec.e_blkno = cpu_to_le64(start_blk);
3743 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
3744 rec.e_flags = flags;
3746 status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec,
3747 &free_records, &insert);
3753 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
3754 "Insert.contig_index: %d, Insert.free_records: %d, "
3755 "Insert.tree_depth: %d\n",
3756 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
3757 free_records, insert.ins_tree_depth);
3759 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
3760 status = ocfs2_grow_tree(inode, handle, fe_bh,
3761 &insert.ins_tree_depth, &last_eb_bh,
3769 /* Finally, we can add clusters. This might rotate the tree for us. */
3770 status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert);
3774 ocfs2_extent_map_insert_rec(inode, &rec);
3784 static void ocfs2_make_right_split_rec(struct super_block *sb,
3785 struct ocfs2_extent_rec *split_rec,
3787 struct ocfs2_extent_rec *rec)
3789 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
3790 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
3792 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
3794 split_rec->e_cpos = cpu_to_le32(cpos);
3795 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
3797 split_rec->e_blkno = rec->e_blkno;
3798 le64_add_cpu(&split_rec->e_blkno,
3799 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
3801 split_rec->e_flags = rec->e_flags;
3804 static int ocfs2_split_and_insert(struct inode *inode,
3806 struct ocfs2_path *path,
3807 struct buffer_head *di_bh,
3808 struct buffer_head **last_eb_bh,
3810 struct ocfs2_extent_rec *orig_split_rec,
3811 struct ocfs2_alloc_context *meta_ac)
3814 unsigned int insert_range, rec_range, do_leftright = 0;
3815 struct ocfs2_extent_rec tmprec;
3816 struct ocfs2_extent_list *rightmost_el;
3817 struct ocfs2_extent_rec rec;
3818 struct ocfs2_extent_rec split_rec = *orig_split_rec;
3819 struct ocfs2_insert_type insert;
3820 struct ocfs2_extent_block *eb;
3821 struct ocfs2_dinode *di;
3825 * Store a copy of the record on the stack - it might move
3826 * around as the tree is manipulated below.
3828 rec = path_leaf_el(path)->l_recs[split_index];
3830 di = (struct ocfs2_dinode *)di_bh->b_data;
3831 rightmost_el = &di->id2.i_list;
3833 depth = le16_to_cpu(rightmost_el->l_tree_depth);
3835 BUG_ON(!(*last_eb_bh));
3836 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
3837 rightmost_el = &eb->h_list;
3840 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
3841 le16_to_cpu(rightmost_el->l_count)) {
3842 ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, last_eb_bh,
3850 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
3851 insert.ins_appending = APPEND_NONE;
3852 insert.ins_contig = CONTIG_NONE;
3853 insert.ins_tree_depth = depth;
3855 insert_range = le32_to_cpu(split_rec.e_cpos) +
3856 le16_to_cpu(split_rec.e_leaf_clusters);
3857 rec_range = le32_to_cpu(rec.e_cpos) +
3858 le16_to_cpu(rec.e_leaf_clusters);
3860 if (split_rec.e_cpos == rec.e_cpos) {
3861 insert.ins_split = SPLIT_LEFT;
3862 } else if (insert_range == rec_range) {
3863 insert.ins_split = SPLIT_RIGHT;
3866 * Left/right split. We fake this as a right split
3867 * first and then make a second pass as a left split.
3869 insert.ins_split = SPLIT_RIGHT;
3871 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
3876 BUG_ON(do_leftright);
3880 ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec,
3887 if (do_leftright == 1) {
3889 struct ocfs2_extent_list *el;
3892 split_rec = *orig_split_rec;
3894 ocfs2_reinit_path(path, 1);
3896 cpos = le32_to_cpu(split_rec.e_cpos);
3897 ret = ocfs2_find_path(inode, path, cpos);
3903 el = path_leaf_el(path);
3904 split_index = ocfs2_search_extent_list(el, cpos);
3913 * Mark part or all of the extent record at split_index in the leaf
3914 * pointed to by path as written. This removes the unwritten
3917 * Care is taken to handle contiguousness so as to not grow the tree.
3919 * meta_ac is not strictly necessary - we only truly need it if growth
3920 * of the tree is required. All other cases will degrade into a less
3921 * optimal tree layout.
3923 * last_eb_bh should be the rightmost leaf block for any inode with a
3924 * btree. Since a split may grow the tree or a merge might shrink it, the caller cannot trust the contents of that buffer after this call.
3926 * This code is optimized for readability - several passes might be
3927 * made over certain portions of the tree. All of those blocks will
3928 * have been brought into cache (and pinned via the journal), so the
3929 * extra overhead is not expressed in terms of disk reads.
3931 static int __ocfs2_mark_extent_written(struct inode *inode,
3932 struct buffer_head *di_bh,
3934 struct ocfs2_path *path,
3936 struct ocfs2_extent_rec *split_rec,
3937 struct ocfs2_alloc_context *meta_ac,
3938 struct ocfs2_cached_dealloc_ctxt *dealloc)
3941 struct ocfs2_extent_list *el = path_leaf_el(path);
3942 struct buffer_head *eb_bh, *last_eb_bh = NULL;
3943 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3944 struct ocfs2_merge_ctxt ctxt;
3945 struct ocfs2_extent_list *rightmost_el;
3947 if (!rec->e_flags & OCFS2_EXT_UNWRITTEN) {
3953 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
3954 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
3955 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
3961 eb_bh = path_leaf_bh(path);
3962 ret = ocfs2_journal_access(handle, inode, eb_bh,
3963 OCFS2_JOURNAL_ACCESS_WRITE);
3969 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, el,
3974 * The core merge / split code wants to know how much room is
3975 * left in this inodes allocation tree, so we pass the
3976 * rightmost extent list.
3978 if (path->p_tree_depth) {
3979 struct ocfs2_extent_block *eb;
3980 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3982 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
3983 le64_to_cpu(di->i_last_eb_blk),
3984 &last_eb_bh, OCFS2_BH_CACHED, inode);
3990 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
3991 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
3992 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
3997 rightmost_el = &eb->h_list;
3999 rightmost_el = path_root_el(path);
4001 if (rec->e_cpos == split_rec->e_cpos &&
4002 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4003 ctxt.c_split_covers_rec = 1;
4005 ctxt.c_split_covers_rec = 0;
4007 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4009 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4010 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4011 ctxt.c_split_covers_rec);
4013 if (ctxt.c_contig_type == CONTIG_NONE) {
4014 if (ctxt.c_split_covers_rec)
4015 el->l_recs[split_index] = *split_rec;
4017 ret = ocfs2_split_and_insert(inode, handle, path, di_bh,
4018 &last_eb_bh, split_index,
4019 split_rec, meta_ac);
4023 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4024 split_index, split_rec,
4030 ocfs2_journal_dirty(handle, eb_bh);
4038 * Mark the already-existing extent at cpos as written for len clusters.
4040 * If the existing extent is larger than the request, initiate a
4041 * split. An attempt will be made at merging with adjacent extents.
4043 * The caller is responsible for passing down meta_ac if we'll need it.
4045 int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh,
4046 handle_t *handle, u32 cpos, u32 len, u32 phys,
4047 struct ocfs2_alloc_context *meta_ac,
4048 struct ocfs2_cached_dealloc_ctxt *dealloc)
4051 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4052 struct ocfs2_extent_rec split_rec;
4053 struct ocfs2_path *left_path = NULL;
4054 struct ocfs2_extent_list *el;
4056 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4057 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4059 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4060 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4061 "that are being written to, but the feature bit "
4062 "is not set in the super block.",
4063 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4069 * XXX: This should be fixed up so that we just re-insert the
4070 * next extent records.
4072 ocfs2_extent_map_trunc(inode, 0);
4074 left_path = ocfs2_new_inode_path(di_bh);
4081 ret = ocfs2_find_path(inode, left_path, cpos);
4086 el = path_leaf_el(left_path);
4088 index = ocfs2_search_extent_list(el, cpos);
4089 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4090 ocfs2_error(inode->i_sb,
4091 "Inode %llu has an extent at cpos %u which can no "
4092 "longer be found.\n",
4093 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4098 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4099 split_rec.e_cpos = cpu_to_le32(cpos);
4100 split_rec.e_leaf_clusters = cpu_to_le16(len);
4101 split_rec.e_blkno = cpu_to_le64(start_blkno);
4102 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4103 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4105 ret = __ocfs2_mark_extent_written(inode, di_bh, handle, left_path,
4106 index, &split_rec, meta_ac, dealloc);
4111 ocfs2_free_path(left_path);
4115 static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh,
4116 handle_t *handle, struct ocfs2_path *path,
4117 int index, u32 new_range,
4118 struct ocfs2_alloc_context *meta_ac)
4120 int ret, depth, credits = handle->h_buffer_credits;
4121 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4122 struct buffer_head *last_eb_bh = NULL;
4123 struct ocfs2_extent_block *eb;
4124 struct ocfs2_extent_list *rightmost_el, *el;
4125 struct ocfs2_extent_rec split_rec;
4126 struct ocfs2_extent_rec *rec;
4127 struct ocfs2_insert_type insert;
4130 * Setup the record to split before we grow the tree.
4132 el = path_leaf_el(path);
4133 rec = &el->l_recs[index];
4134 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4136 depth = path->p_tree_depth;
4138 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4139 le64_to_cpu(di->i_last_eb_blk),
4140 &last_eb_bh, OCFS2_BH_CACHED, inode);
4146 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4147 rightmost_el = &eb->h_list;
4149 rightmost_el = path_leaf_el(path);
4151 credits += path->p_tree_depth + ocfs2_extend_meta_needed(di);
4152 ret = ocfs2_extend_trans(handle, credits);
4158 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4159 le16_to_cpu(rightmost_el->l_count)) {
4160 ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, &last_eb_bh,
4168 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4169 insert.ins_appending = APPEND_NONE;
4170 insert.ins_contig = CONTIG_NONE;
4171 insert.ins_split = SPLIT_RIGHT;
4172 insert.ins_tree_depth = depth;
4174 ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec, &insert);
4183 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
4184 struct ocfs2_path *path, int index,
4185 struct ocfs2_cached_dealloc_ctxt *dealloc,
4189 u32 left_cpos, rec_range, trunc_range;
4190 int wants_rotate = 0, is_rightmost_tree_rec = 0;
4191 struct super_block *sb = inode->i_sb;
4192 struct ocfs2_path *left_path = NULL;
4193 struct ocfs2_extent_list *el = path_leaf_el(path);
4194 struct ocfs2_extent_rec *rec;
4195 struct ocfs2_extent_block *eb;
4197 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
4198 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
4207 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
4208 path->p_tree_depth) {
4210 * Check whether this is the rightmost tree record. If
4211 * we remove all of this record or part of its right
4212 * edge then an update of the record lengths above it
4215 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
4216 if (eb->h_next_leaf_blk == 0)
4217 is_rightmost_tree_rec = 1;
4220 rec = &el->l_recs[index];
4221 if (index == 0 && path->p_tree_depth &&
4222 le32_to_cpu(rec->e_cpos) == cpos) {
4224 * Changing the leftmost offset (via partial or whole
4225 * record truncate) of an interior (or rightmost) path
4226 * means we have to update the subtree that is formed
4227 * by this leaf and the one to it's left.
4229 * There are two cases we can skip:
4230 * 1) Path is the leftmost one in our inode tree.
4231 * 2) The leaf is rightmost and will be empty after
4232 * we remove the extent record - the rotate code
4233 * knows how to update the newly formed edge.
4236 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
4243 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
4244 left_path = ocfs2_new_path(path_root_bh(path),
4245 path_root_el(path));
4252 ret = ocfs2_find_path(inode, left_path, left_cpos);
4260 ret = ocfs2_extend_rotate_transaction(handle, 0,
4261 handle->h_buffer_credits,
4268 ret = ocfs2_journal_access_path(inode, handle, path);
4274 ret = ocfs2_journal_access_path(inode, handle, left_path);
4280 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
4281 trunc_range = cpos + len;
4283 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
4286 memset(rec, 0, sizeof(*rec));
4287 ocfs2_cleanup_merge(el, index);
4290 next_free = le16_to_cpu(el->l_next_free_rec);
4291 if (is_rightmost_tree_rec && next_free > 1) {
4293 * We skip the edge update if this path will
4294 * be deleted by the rotate code.
4296 rec = &el->l_recs[next_free - 1];
4297 ocfs2_adjust_rightmost_records(inode, handle, path,
4300 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
4301 /* Remove leftmost portion of the record. */
4302 le32_add_cpu(&rec->e_cpos, len);
4303 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
4304 le16_add_cpu(&rec->e_leaf_clusters, -len);
4305 } else if (rec_range == trunc_range) {
4306 /* Remove rightmost portion of the record */
4307 le16_add_cpu(&rec->e_leaf_clusters, -len);
4308 if (is_rightmost_tree_rec)
4309 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
4311 /* Caller should have trapped this. */
4312 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
4313 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
4314 le32_to_cpu(rec->e_cpos),
4315 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
4322 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
4323 ocfs2_complete_edge_insert(inode, handle, left_path, path,
4327 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4329 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
4336 ocfs2_free_path(left_path);
4340 int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh,
4341 u32 cpos, u32 len, handle_t *handle,
4342 struct ocfs2_alloc_context *meta_ac,
4343 struct ocfs2_cached_dealloc_ctxt *dealloc)
4346 u32 rec_range, trunc_range;
4347 struct ocfs2_extent_rec *rec;
4348 struct ocfs2_extent_list *el;
4349 struct ocfs2_path *path;
4351 ocfs2_extent_map_trunc(inode, 0);
4353 path = ocfs2_new_inode_path(di_bh);
4360 ret = ocfs2_find_path(inode, path, cpos);
4366 el = path_leaf_el(path);
4367 index = ocfs2_search_extent_list(el, cpos);
4368 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4369 ocfs2_error(inode->i_sb,
4370 "Inode %llu has an extent at cpos %u which can no "
4371 "longer be found.\n",
4372 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4378 * We have 3 cases of extent removal:
4379 * 1) Range covers the entire extent rec
4380 * 2) Range begins or ends on one edge of the extent rec
4381 * 3) Range is in the middle of the extent rec (no shared edges)
4383 * For case 1 we remove the extent rec and left rotate to
4386 * For case 2 we just shrink the existing extent rec, with a
4387 * tree update if the shrinking edge is also the edge of an
4390 * For case 3 we do a right split to turn the extent rec into
4391 * something case 2 can handle.
4393 rec = &el->l_recs[index];
4394 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
4395 trunc_range = cpos + len;
4397 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
4399 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
4400 "(cpos %u, len %u)\n",
4401 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
4402 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
4404 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
4405 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
4412 ret = ocfs2_split_tree(inode, di_bh, handle, path, index,
4413 trunc_range, meta_ac);
4420 * The split could have manipulated the tree enough to
4421 * move the record location, so we have to look for it again.
4423 ocfs2_reinit_path(path, 1);
4425 ret = ocfs2_find_path(inode, path, cpos);
4431 el = path_leaf_el(path);
4432 index = ocfs2_search_extent_list(el, cpos);
4433 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4434 ocfs2_error(inode->i_sb,
4435 "Inode %llu: split at cpos %u lost record.",
4436 (unsigned long long)OCFS2_I(inode)->ip_blkno,
4443 * Double check our values here. If anything is fishy,
4444 * it's easier to catch it at the top level.
4446 rec = &el->l_recs[index];
4447 rec_range = le32_to_cpu(rec->e_cpos) +
4448 ocfs2_rec_clusters(el, rec);
4449 if (rec_range != trunc_range) {
4450 ocfs2_error(inode->i_sb,
4451 "Inode %llu: error after split at cpos %u"
4452 "trunc len %u, existing record is (%u,%u)",
4453 (unsigned long long)OCFS2_I(inode)->ip_blkno,
4454 cpos, len, le32_to_cpu(rec->e_cpos),
4455 ocfs2_rec_clusters(el, rec));
4460 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
4469 ocfs2_free_path(path);
4473 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
4475 struct buffer_head *tl_bh = osb->osb_tl_bh;
4476 struct ocfs2_dinode *di;
4477 struct ocfs2_truncate_log *tl;
4479 di = (struct ocfs2_dinode *) tl_bh->b_data;
4480 tl = &di->id2.i_dealloc;
4482 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
4483 "slot %d, invalid truncate log parameters: used = "
4484 "%u, count = %u\n", osb->slot_num,
4485 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
4486 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
4489 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
4490 unsigned int new_start)
4492 unsigned int tail_index;
4493 unsigned int current_tail;
4495 /* No records, nothing to coalesce */
4496 if (!le16_to_cpu(tl->tl_used))
4499 tail_index = le16_to_cpu(tl->tl_used) - 1;
4500 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
4501 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
4503 return current_tail == new_start;
4506 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
4509 unsigned int num_clusters)
4512 unsigned int start_cluster, tl_count;
4513 struct inode *tl_inode = osb->osb_tl_inode;
4514 struct buffer_head *tl_bh = osb->osb_tl_bh;
4515 struct ocfs2_dinode *di;
4516 struct ocfs2_truncate_log *tl;
4518 mlog_entry("start_blk = %llu, num_clusters = %u\n",
4519 (unsigned long long)start_blk, num_clusters);
4521 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
4523 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
4525 di = (struct ocfs2_dinode *) tl_bh->b_data;
4526 tl = &di->id2.i_dealloc;
4527 if (!OCFS2_IS_VALID_DINODE(di)) {
4528 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
4533 tl_count = le16_to_cpu(tl->tl_count);
4534 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
4536 "Truncate record count on #%llu invalid "
4537 "wanted %u, actual %u\n",
4538 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
4539 ocfs2_truncate_recs_per_inode(osb->sb),
4540 le16_to_cpu(tl->tl_count));
4542 /* Caller should have known to flush before calling us. */
4543 index = le16_to_cpu(tl->tl_used);
4544 if (index >= tl_count) {
4550 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
4551 OCFS2_JOURNAL_ACCESS_WRITE);
4557 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
4558 "%llu (index = %d)\n", num_clusters, start_cluster,
4559 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
4561 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
4563 * Move index back to the record we are coalescing with.
4564 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
4568 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
4569 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
4570 index, le32_to_cpu(tl->tl_recs[index].t_start),
4573 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
4574 tl->tl_used = cpu_to_le16(index + 1);
4576 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
4578 status = ocfs2_journal_dirty(handle, tl_bh);
4589 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
4591 struct inode *data_alloc_inode,
4592 struct buffer_head *data_alloc_bh)
4596 unsigned int num_clusters;
4598 struct ocfs2_truncate_rec rec;
4599 struct ocfs2_dinode *di;
4600 struct ocfs2_truncate_log *tl;
4601 struct inode *tl_inode = osb->osb_tl_inode;
4602 struct buffer_head *tl_bh = osb->osb_tl_bh;
4606 di = (struct ocfs2_dinode *) tl_bh->b_data;
4607 tl = &di->id2.i_dealloc;
4608 i = le16_to_cpu(tl->tl_used) - 1;
4610 /* Caller has given us at least enough credits to
4611 * update the truncate log dinode */
4612 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
4613 OCFS2_JOURNAL_ACCESS_WRITE);
4619 tl->tl_used = cpu_to_le16(i);
4621 status = ocfs2_journal_dirty(handle, tl_bh);
4627 /* TODO: Perhaps we can calculate the bulk of the
4628 * credits up front rather than extending like
4630 status = ocfs2_extend_trans(handle,
4631 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
4637 rec = tl->tl_recs[i];
4638 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
4639 le32_to_cpu(rec.t_start));
4640 num_clusters = le32_to_cpu(rec.t_clusters);
4642 /* if start_blk is not set, we ignore the record as
4645 mlog(0, "free record %d, start = %u, clusters = %u\n",
4646 i, le32_to_cpu(rec.t_start), num_clusters);
4648 status = ocfs2_free_clusters(handle, data_alloc_inode,
4649 data_alloc_bh, start_blk,
4664 /* Expects you to already be holding tl_inode->i_mutex */
4665 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
4668 unsigned int num_to_flush;
4670 struct inode *tl_inode = osb->osb_tl_inode;
4671 struct inode *data_alloc_inode = NULL;
4672 struct buffer_head *tl_bh = osb->osb_tl_bh;
4673 struct buffer_head *data_alloc_bh = NULL;
4674 struct ocfs2_dinode *di;
4675 struct ocfs2_truncate_log *tl;
4679 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
4681 di = (struct ocfs2_dinode *) tl_bh->b_data;
4682 tl = &di->id2.i_dealloc;
4683 if (!OCFS2_IS_VALID_DINODE(di)) {
4684 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
4689 num_to_flush = le16_to_cpu(tl->tl_used);
4690 mlog(0, "Flush %u records from truncate log #%llu\n",
4691 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
4692 if (!num_to_flush) {
4697 data_alloc_inode = ocfs2_get_system_file_inode(osb,
4698 GLOBAL_BITMAP_SYSTEM_INODE,
4699 OCFS2_INVALID_SLOT);
4700 if (!data_alloc_inode) {
4702 mlog(ML_ERROR, "Could not get bitmap inode!\n");
4706 mutex_lock(&data_alloc_inode->i_mutex);
4708 status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1);
4714 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
4715 if (IS_ERR(handle)) {
4716 status = PTR_ERR(handle);
4721 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
4726 ocfs2_commit_trans(osb, handle);
4729 brelse(data_alloc_bh);
4730 ocfs2_meta_unlock(data_alloc_inode, 1);
4733 mutex_unlock(&data_alloc_inode->i_mutex);
4734 iput(data_alloc_inode);
4741 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
4744 struct inode *tl_inode = osb->osb_tl_inode;
4746 mutex_lock(&tl_inode->i_mutex);
4747 status = __ocfs2_flush_truncate_log(osb);
4748 mutex_unlock(&tl_inode->i_mutex);
4753 static void ocfs2_truncate_log_worker(struct work_struct *work)
4756 struct ocfs2_super *osb =
4757 container_of(work, struct ocfs2_super,
4758 osb_truncate_log_wq.work);
4762 status = ocfs2_flush_truncate_log(osb);
4769 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
4770 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
4773 if (osb->osb_tl_inode) {
4774 /* We want to push off log flushes while truncates are
4777 cancel_delayed_work(&osb->osb_truncate_log_wq);
4779 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
4780 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
4784 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
4786 struct inode **tl_inode,
4787 struct buffer_head **tl_bh)
4790 struct inode *inode = NULL;
4791 struct buffer_head *bh = NULL;
4793 inode = ocfs2_get_system_file_inode(osb,
4794 TRUNCATE_LOG_SYSTEM_INODE,
4798 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
4802 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
4803 OCFS2_BH_CACHED, inode);
4817 /* called during the 1st stage of node recovery. we stamp a clean
4818 * truncate log and pass back a copy for processing later. if the
4819 * truncate log does not require processing, a *tl_copy is set to
4821 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
4823 struct ocfs2_dinode **tl_copy)
4826 struct inode *tl_inode = NULL;
4827 struct buffer_head *tl_bh = NULL;
4828 struct ocfs2_dinode *di;
4829 struct ocfs2_truncate_log *tl;
4833 mlog(0, "recover truncate log from slot %d\n", slot_num);
4835 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
4841 di = (struct ocfs2_dinode *) tl_bh->b_data;
4842 tl = &di->id2.i_dealloc;
4843 if (!OCFS2_IS_VALID_DINODE(di)) {
4844 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
4849 if (le16_to_cpu(tl->tl_used)) {
4850 mlog(0, "We'll have %u logs to recover\n",
4851 le16_to_cpu(tl->tl_used));
4853 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
4860 /* Assuming the write-out below goes well, this copy
4861 * will be passed back to recovery for processing. */
4862 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
4864 /* All we need to do to clear the truncate log is set
4868 status = ocfs2_write_block(osb, tl_bh, tl_inode);
4881 if (status < 0 && (*tl_copy)) {
4890 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
4891 struct ocfs2_dinode *tl_copy)
4895 unsigned int clusters, num_recs, start_cluster;
4898 struct inode *tl_inode = osb->osb_tl_inode;
4899 struct ocfs2_truncate_log *tl;
4903 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
4904 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
4908 tl = &tl_copy->id2.i_dealloc;
4909 num_recs = le16_to_cpu(tl->tl_used);
4910 mlog(0, "cleanup %u records from %llu\n", num_recs,
4911 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
4913 mutex_lock(&tl_inode->i_mutex);
4914 for(i = 0; i < num_recs; i++) {
4915 if (ocfs2_truncate_log_needs_flush(osb)) {
4916 status = __ocfs2_flush_truncate_log(osb);
4923 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
4924 if (IS_ERR(handle)) {
4925 status = PTR_ERR(handle);
4930 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
4931 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
4932 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
4934 status = ocfs2_truncate_log_append(osb, handle,
4935 start_blk, clusters);
4936 ocfs2_commit_trans(osb, handle);
4944 mutex_unlock(&tl_inode->i_mutex);
4950 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
4953 struct inode *tl_inode = osb->osb_tl_inode;
4958 cancel_delayed_work(&osb->osb_truncate_log_wq);
4959 flush_workqueue(ocfs2_wq);
4961 status = ocfs2_flush_truncate_log(osb);
4965 brelse(osb->osb_tl_bh);
4966 iput(osb->osb_tl_inode);
4972 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
4975 struct inode *tl_inode = NULL;
4976 struct buffer_head *tl_bh = NULL;
4980 status = ocfs2_get_truncate_log_info(osb,
4987 /* ocfs2_truncate_log_shutdown keys on the existence of
4988 * osb->osb_tl_inode so we don't set any of the osb variables
4989 * until we're sure all is well. */
4990 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
4991 ocfs2_truncate_log_worker);
4992 osb->osb_tl_bh = tl_bh;
4993 osb->osb_tl_inode = tl_inode;
5000 * Delayed de-allocation of suballocator blocks.
5002 * Some sets of block de-allocations might involve multiple suballocator inodes.
5004 * The locking for this can get extremely complicated, especially when
5005 * the suballocator inodes to delete from aren't known until deep
5006 * within an unrelated codepath.
5008 * ocfs2_extent_block structures are a good example of this - an inode
5009 * btree could have been grown by any number of nodes each allocating
5010 * out of their own suballoc inode.
5012 * These structures allow the delay of block de-allocation until a
5013 * later time, when locking of multiple cluster inodes won't cause
5018 * Describes a single block free from a suballocator
5020 struct ocfs2_cached_block_free {
5021 struct ocfs2_cached_block_free *free_next;
5023 unsigned int free_bit;
5026 struct ocfs2_per_slot_free_list {
5027 struct ocfs2_per_slot_free_list *f_next_suballocator;
5030 struct ocfs2_cached_block_free *f_first;
5033 static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5036 struct ocfs2_cached_block_free *head)
5041 struct inode *inode;
5042 struct buffer_head *di_bh = NULL;
5043 struct ocfs2_cached_block_free *tmp;
5045 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5052 mutex_lock(&inode->i_mutex);
5054 ret = ocfs2_meta_lock(inode, &di_bh, 1);
5060 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5061 if (IS_ERR(handle)) {
5062 ret = PTR_ERR(handle);
5068 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5070 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5071 head->free_bit, (unsigned long long)head->free_blk);
5073 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5074 head->free_bit, bg_blkno, 1);
5080 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5087 head = head->free_next;
5092 ocfs2_commit_trans(osb, handle);
5095 ocfs2_meta_unlock(inode, 1);
5098 mutex_unlock(&inode->i_mutex);
5102 /* Premature exit may have left some dangling items. */
5104 head = head->free_next;
5111 int ocfs2_run_deallocs(struct ocfs2_super *osb,
5112 struct ocfs2_cached_dealloc_ctxt *ctxt)
5115 struct ocfs2_per_slot_free_list *fl;
5120 while (ctxt->c_first_suballocator) {
5121 fl = ctxt->c_first_suballocator;
5124 mlog(0, "Free items: (type %u, slot %d)\n",
5125 fl->f_inode_type, fl->f_slot);
5126 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
5127 fl->f_slot, fl->f_first);
5134 ctxt->c_first_suballocator = fl->f_next_suballocator;
5141 static struct ocfs2_per_slot_free_list *
5142 ocfs2_find_per_slot_free_list(int type,
5144 struct ocfs2_cached_dealloc_ctxt *ctxt)
5146 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
5149 if (fl->f_inode_type == type && fl->f_slot == slot)
5152 fl = fl->f_next_suballocator;
5155 fl = kmalloc(sizeof(*fl), GFP_NOFS);
5157 fl->f_inode_type = type;
5160 fl->f_next_suballocator = ctxt->c_first_suballocator;
5162 ctxt->c_first_suballocator = fl;
5167 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
5168 int type, int slot, u64 blkno,
5172 struct ocfs2_per_slot_free_list *fl;
5173 struct ocfs2_cached_block_free *item;
5175 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
5182 item = kmalloc(sizeof(*item), GFP_NOFS);
5189 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
5190 type, slot, bit, (unsigned long long)blkno);
5192 item->free_blk = blkno;
5193 item->free_bit = bit;
5194 item->free_next = fl->f_first;
5203 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
5204 struct ocfs2_extent_block *eb)
5206 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
5207 le16_to_cpu(eb->h_suballoc_slot),
5208 le64_to_cpu(eb->h_blkno),
5209 le16_to_cpu(eb->h_suballoc_bit));
5212 /* This function will figure out whether the currently last extent
5213 * block will be deleted, and if it will, what the new last extent
5214 * block will be so we can update his h_next_leaf_blk field, as well
5215 * as the dinodes i_last_eb_blk */
5216 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
5217 unsigned int clusters_to_del,
5218 struct ocfs2_path *path,
5219 struct buffer_head **new_last_eb)
5221 int next_free, ret = 0;
5223 struct ocfs2_extent_rec *rec;
5224 struct ocfs2_extent_block *eb;
5225 struct ocfs2_extent_list *el;
5226 struct buffer_head *bh = NULL;
5228 *new_last_eb = NULL;
5230 /* we have no tree, so of course, no last_eb. */
5231 if (!path->p_tree_depth)
5234 /* trunc to zero special case - this makes tree_depth = 0
5235 * regardless of what it is. */
5236 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
5239 el = path_leaf_el(path);
5240 BUG_ON(!el->l_next_free_rec);
5243 * Make sure that this extent list will actually be empty
5244 * after we clear away the data. We can shortcut out if
5245 * there's more than one non-empty extent in the
5246 * list. Otherwise, a check of the remaining extent is
5249 next_free = le16_to_cpu(el->l_next_free_rec);
5251 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
5255 /* We may have a valid extent in index 1, check it. */
5257 rec = &el->l_recs[1];
5260 * Fall through - no more nonempty extents, so we want
5261 * to delete this leaf.
5267 rec = &el->l_recs[0];
5272 * Check it we'll only be trimming off the end of this
5275 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
5279 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
5285 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
5291 eb = (struct ocfs2_extent_block *) bh->b_data;
5293 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
5294 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
5300 get_bh(*new_last_eb);
5301 mlog(0, "returning block %llu, (cpos: %u)\n",
5302 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
5310 * Trim some clusters off the rightmost edge of a tree. Only called
5313 * The caller needs to:
5314 * - start journaling of each path component.
5315 * - compute and fully set up any new last ext block
5317 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
5318 handle_t *handle, struct ocfs2_truncate_context *tc,
5319 u32 clusters_to_del, u64 *delete_start)
5321 int ret, i, index = path->p_tree_depth;
5324 struct buffer_head *bh;
5325 struct ocfs2_extent_list *el;
5326 struct ocfs2_extent_rec *rec;
5330 while (index >= 0) {
5331 bh = path->p_node[index].bh;
5332 el = path->p_node[index].el;
5334 mlog(0, "traveling tree (index = %d, block = %llu)\n",
5335 index, (unsigned long long)bh->b_blocknr);
5337 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
5340 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
5341 ocfs2_error(inode->i_sb,
5342 "Inode %lu has invalid ext. block %llu",
5344 (unsigned long long)bh->b_blocknr);
5350 i = le16_to_cpu(el->l_next_free_rec) - 1;
5351 rec = &el->l_recs[i];
5353 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
5354 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
5355 ocfs2_rec_clusters(el, rec),
5356 (unsigned long long)le64_to_cpu(rec->e_blkno),
5357 le16_to_cpu(el->l_next_free_rec));
5359 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
5361 if (le16_to_cpu(el->l_tree_depth) == 0) {
5363 * If the leaf block contains a single empty
5364 * extent and no records, we can just remove
5367 if (i == 0 && ocfs2_is_empty_extent(rec)) {
5369 sizeof(struct ocfs2_extent_rec));
5370 el->l_next_free_rec = cpu_to_le16(0);
5376 * Remove any empty extents by shifting things
5377 * left. That should make life much easier on
5378 * the code below. This condition is rare
5379 * enough that we shouldn't see a performance
5382 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
5383 le16_add_cpu(&el->l_next_free_rec, -1);
5386 i < le16_to_cpu(el->l_next_free_rec); i++)
5387 el->l_recs[i] = el->l_recs[i + 1];
5389 memset(&el->l_recs[i], 0,
5390 sizeof(struct ocfs2_extent_rec));
5393 * We've modified our extent list. The
5394 * simplest way to handle this change
5395 * is to being the search from the
5398 goto find_tail_record;
5401 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
5404 * We'll use "new_edge" on our way back up the
5405 * tree to know what our rightmost cpos is.
5407 new_edge = le16_to_cpu(rec->e_leaf_clusters);
5408 new_edge += le32_to_cpu(rec->e_cpos);
5411 * The caller will use this to delete data blocks.
5413 *delete_start = le64_to_cpu(rec->e_blkno)
5414 + ocfs2_clusters_to_blocks(inode->i_sb,
5415 le16_to_cpu(rec->e_leaf_clusters));
5418 * If it's now empty, remove this record.
5420 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
5422 sizeof(struct ocfs2_extent_rec));
5423 le16_add_cpu(&el->l_next_free_rec, -1);
5426 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
5428 sizeof(struct ocfs2_extent_rec));
5429 le16_add_cpu(&el->l_next_free_rec, -1);
5434 /* Can this actually happen? */
5435 if (le16_to_cpu(el->l_next_free_rec) == 0)
5439 * We never actually deleted any clusters
5440 * because our leaf was empty. There's no
5441 * reason to adjust the rightmost edge then.
5446 rec->e_int_clusters = cpu_to_le32(new_edge);
5447 le32_add_cpu(&rec->e_int_clusters,
5448 -le32_to_cpu(rec->e_cpos));
5451 * A deleted child record should have been
5454 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
5458 ret = ocfs2_journal_dirty(handle, bh);
5464 mlog(0, "extent list container %llu, after: record %d: "
5465 "(%u, %u, %llu), next = %u.\n",
5466 (unsigned long long)bh->b_blocknr, i,
5467 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
5468 (unsigned long long)le64_to_cpu(rec->e_blkno),
5469 le16_to_cpu(el->l_next_free_rec));
5472 * We must be careful to only attempt delete of an
5473 * extent block (and not the root inode block).
5475 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
5476 struct ocfs2_extent_block *eb =
5477 (struct ocfs2_extent_block *)bh->b_data;
5480 * Save this for use when processing the
5483 deleted_eb = le64_to_cpu(eb->h_blkno);
5485 mlog(0, "deleting this extent block.\n");
5487 ocfs2_remove_from_cache(inode, bh);
5489 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
5490 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
5491 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
5493 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
5494 /* An error here is not fatal. */
5509 static int ocfs2_do_truncate(struct ocfs2_super *osb,
5510 unsigned int clusters_to_del,
5511 struct inode *inode,
5512 struct buffer_head *fe_bh,
5514 struct ocfs2_truncate_context *tc,
5515 struct ocfs2_path *path)
5518 struct ocfs2_dinode *fe;
5519 struct ocfs2_extent_block *last_eb = NULL;
5520 struct ocfs2_extent_list *el;
5521 struct buffer_head *last_eb_bh = NULL;
5524 fe = (struct ocfs2_dinode *) fe_bh->b_data;
5526 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
5534 * Each component will be touched, so we might as well journal
5535 * here to avoid having to handle errors later.
5537 status = ocfs2_journal_access_path(inode, handle, path);
5544 status = ocfs2_journal_access(handle, inode, last_eb_bh,
5545 OCFS2_JOURNAL_ACCESS_WRITE);
5551 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5554 el = &(fe->id2.i_list);
5557 * Lower levels depend on this never happening, but it's best
5558 * to check it up here before changing the tree.
5560 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
5561 ocfs2_error(inode->i_sb,
5562 "Inode %lu has an empty extent record, depth %u\n",
5563 inode->i_ino, le16_to_cpu(el->l_tree_depth));
5568 spin_lock(&OCFS2_I(inode)->ip_lock);
5569 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
5571 spin_unlock(&OCFS2_I(inode)->ip_lock);
5572 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
5573 inode->i_blocks = ocfs2_inode_sector_count(inode);
5575 status = ocfs2_trim_tree(inode, path, handle, tc,
5576 clusters_to_del, &delete_blk);
5582 if (le32_to_cpu(fe->i_clusters) == 0) {
5583 /* trunc to zero is a special case. */
5584 el->l_tree_depth = 0;
5585 fe->i_last_eb_blk = 0;
5587 fe->i_last_eb_blk = last_eb->h_blkno;
5589 status = ocfs2_journal_dirty(handle, fe_bh);
5596 /* If there will be a new last extent block, then by
5597 * definition, there cannot be any leaves to the right of
5599 last_eb->h_next_leaf_blk = 0;
5600 status = ocfs2_journal_dirty(handle, last_eb_bh);
5608 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
5622 static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
5624 set_buffer_uptodate(bh);
5625 mark_buffer_dirty(bh);
5629 static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
5631 set_buffer_uptodate(bh);
5632 mark_buffer_dirty(bh);
5633 return ocfs2_journal_dirty_data(handle, bh);
5636 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
5637 loff_t end, struct page **pages,
5638 int numpages, u64 phys, handle_t *handle)
5640 int i, ret, partial = 0;
5643 unsigned int from, to = PAGE_CACHE_SIZE;
5644 struct super_block *sb = inode->i_sb;
5646 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
5651 to = PAGE_CACHE_SIZE;
5652 for(i = 0; i < numpages; i++) {
5655 from = start & (PAGE_CACHE_SIZE - 1);
5656 if ((end >> PAGE_CACHE_SHIFT) == page->index)
5657 to = end & (PAGE_CACHE_SIZE - 1);
5659 BUG_ON(from > PAGE_CACHE_SIZE);
5660 BUG_ON(to > PAGE_CACHE_SIZE);
5662 ret = ocfs2_map_page_blocks(page, &phys, inode, from, to, 0);
5666 kaddr = kmap_atomic(page, KM_USER0);
5667 memset(kaddr + from, 0, to - from);
5668 kunmap_atomic(kaddr, KM_USER0);
5671 * Need to set the buffers we zero'd into uptodate
5672 * here if they aren't - ocfs2_map_page_blocks()
5673 * might've skipped some
5675 if (ocfs2_should_order_data(inode)) {
5676 ret = walk_page_buffers(handle,
5679 ocfs2_ordered_zero_func);
5683 ret = walk_page_buffers(handle, page_buffers(page),
5685 ocfs2_writeback_zero_func);
5691 SetPageUptodate(page);
5693 flush_dcache_page(page);
5695 start = (page->index + 1) << PAGE_CACHE_SHIFT;
5699 for (i = 0; i < numpages; i++) {
5702 mark_page_accessed(page);
5703 page_cache_release(page);
5708 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
5709 struct page **pages, int *num, u64 *phys)
5711 int i, numpages = 0, ret = 0;
5712 unsigned int ext_flags;
5713 struct super_block *sb = inode->i_sb;
5714 struct address_space *mapping = inode->i_mapping;
5715 unsigned long index;
5716 loff_t last_page_bytes;
5718 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
5719 BUG_ON(start > end);
5724 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
5725 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
5727 ret = ocfs2_extent_map_get_blocks(inode, start >> sb->s_blocksize_bits,
5728 phys, NULL, &ext_flags);
5734 /* Tail is a hole. */
5738 /* Tail is marked as unwritten, we can count on write to zero
5740 if (ext_flags & OCFS2_EXT_UNWRITTEN)
5743 last_page_bytes = PAGE_ALIGN(end);
5744 index = start >> PAGE_CACHE_SHIFT;
5746 pages[numpages] = grab_cache_page(mapping, index);
5747 if (!pages[numpages]) {
5755 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
5760 for (i = 0; i < numpages; i++) {
5762 unlock_page(pages[i]);
5763 page_cache_release(pages[i]);
5776 * Zero the area past i_size but still within an allocated
5777 * cluster. This avoids exposing nonzero data on subsequent file
5780 * We need to call this before i_size is updated on the inode because
5781 * otherwise block_write_full_page() will skip writeout of pages past
5782 * i_size. The new_i_size parameter is passed for this reason.
5784 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
5785 u64 range_start, u64 range_end)
5788 struct page **pages = NULL;
5792 * File systems which don't support sparse files zero on every
5795 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
5798 pages = kcalloc(ocfs2_pages_per_cluster(inode->i_sb),
5799 sizeof(struct page *), GFP_NOFS);
5800 if (pages == NULL) {
5806 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
5816 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
5817 numpages, phys, handle);
5820 * Initiate writeout of the pages we zero'd here. We don't
5821 * wait on them - the truncate_inode_pages() call later will
5824 ret = do_sync_mapping_range(inode->i_mapping, range_start,
5825 range_end - 1, SYNC_FILE_RANGE_WRITE);
5837 * It is expected, that by the time you call this function,
5838 * inode->i_size and fe->i_size have been adjusted.
5840 * WARNING: This will kfree the truncate context
5842 int ocfs2_commit_truncate(struct ocfs2_super *osb,
5843 struct inode *inode,
5844 struct buffer_head *fe_bh,
5845 struct ocfs2_truncate_context *tc)
5847 int status, i, credits, tl_sem = 0;
5848 u32 clusters_to_del, new_highest_cpos, range;
5849 struct ocfs2_extent_list *el;
5850 handle_t *handle = NULL;
5851 struct inode *tl_inode = osb->osb_tl_inode;
5852 struct ocfs2_path *path = NULL;
5856 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
5857 i_size_read(inode));
5859 path = ocfs2_new_inode_path(fe_bh);
5866 ocfs2_extent_map_trunc(inode, new_highest_cpos);
5870 * Check that we still have allocation to delete.
5872 if (OCFS2_I(inode)->ip_clusters == 0) {
5878 * Truncate always works against the rightmost tree branch.
5880 status = ocfs2_find_path(inode, path, UINT_MAX);
5886 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
5887 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
5890 * By now, el will point to the extent list on the bottom most
5891 * portion of this tree. Only the tail record is considered in
5894 * We handle the following cases, in order:
5895 * - empty extent: delete the remaining branch
5896 * - remove the entire record
5897 * - remove a partial record
5898 * - no record needs to be removed (truncate has completed)
5900 el = path_leaf_el(path);
5901 if (le16_to_cpu(el->l_next_free_rec) == 0) {
5902 ocfs2_error(inode->i_sb,
5903 "Inode %llu has empty extent block at %llu\n",
5904 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5905 (unsigned long long)path_leaf_bh(path)->b_blocknr);
5910 i = le16_to_cpu(el->l_next_free_rec) - 1;
5911 range = le32_to_cpu(el->l_recs[i].e_cpos) +
5912 ocfs2_rec_clusters(el, &el->l_recs[i]);
5913 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
5914 clusters_to_del = 0;
5915 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
5916 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
5917 } else if (range > new_highest_cpos) {
5918 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
5919 le32_to_cpu(el->l_recs[i].e_cpos)) -
5926 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
5927 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
5929 BUG_ON(clusters_to_del == 0);
5931 mutex_lock(&tl_inode->i_mutex);
5933 /* ocfs2_truncate_log_needs_flush guarantees us at least one
5934 * record is free for use. If there isn't any, we flush to get
5935 * an empty truncate log. */
5936 if (ocfs2_truncate_log_needs_flush(osb)) {
5937 status = __ocfs2_flush_truncate_log(osb);
5944 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
5945 (struct ocfs2_dinode *)fe_bh->b_data,
5947 handle = ocfs2_start_trans(osb, credits);
5948 if (IS_ERR(handle)) {
5949 status = PTR_ERR(handle);
5955 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
5962 mutex_unlock(&tl_inode->i_mutex);
5965 ocfs2_commit_trans(osb, handle);
5968 ocfs2_reinit_path(path, 1);
5971 * The check above will catch the case where we've truncated
5972 * away all allocation.
5978 ocfs2_schedule_truncate_log_flush(osb, 1);
5981 mutex_unlock(&tl_inode->i_mutex);
5984 ocfs2_commit_trans(osb, handle);
5986 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
5988 ocfs2_free_path(path);
5990 /* This will drop the ext_alloc cluster lock for us */
5991 ocfs2_free_truncate_context(tc);
5998 * Expects the inode to already be locked.
6000 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
6001 struct inode *inode,
6002 struct buffer_head *fe_bh,
6003 struct ocfs2_truncate_context **tc)
6006 unsigned int new_i_clusters;
6007 struct ocfs2_dinode *fe;
6008 struct ocfs2_extent_block *eb;
6009 struct buffer_head *last_eb_bh = NULL;
6015 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
6016 i_size_read(inode));
6017 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6019 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
6020 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
6021 (unsigned long long)le64_to_cpu(fe->i_size));
6023 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
6029 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
6031 if (fe->id2.i_list.l_tree_depth) {
6032 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
6033 &last_eb_bh, OCFS2_BH_CACHED, inode);
6038 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6039 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6040 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6048 (*tc)->tc_last_eb_bh = last_eb_bh;
6054 ocfs2_free_truncate_context(*tc);
6061 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
6064 * The caller is responsible for completing deallocation
6065 * before freeing the context.
6067 if (tc->tc_dealloc.c_first_suballocator != NULL)
6069 "Truncate completion has non-empty dealloc context\n");
6071 if (tc->tc_last_eb_bh)
6072 brelse(tc->tc_last_eb_bh);