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>
31 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
32 #include <cluster/masklog.h>
38 #include "extent_map.h"
41 #include "localalloc.h"
48 #include "buffer_head_io.h"
50 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
53 * Structures which describe a path through a btree, and functions to
56 * The idea here is to be as generic as possible with the tree
59 struct ocfs2_path_item {
60 struct buffer_head *bh;
61 struct ocfs2_extent_list *el;
64 #define OCFS2_MAX_PATH_DEPTH 5
68 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
71 #define path_root_bh(_path) ((_path)->p_node[0].bh)
72 #define path_root_el(_path) ((_path)->p_node[0].el)
73 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
74 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
75 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
78 * Reset the actual path elements so that we can re-use the structure
79 * to build another path. Generally, this involves freeing the buffer
82 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
84 int i, start = 0, depth = 0;
85 struct ocfs2_path_item *node;
90 for(i = start; i < path_num_items(path); i++) {
91 node = &path->p_node[i];
99 * Tree depth may change during truncate, or insert. If we're
100 * keeping the root extent list, then make sure that our path
101 * structure reflects the proper depth.
104 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
106 path->p_tree_depth = depth;
109 static void ocfs2_free_path(struct ocfs2_path *path)
112 ocfs2_reinit_path(path, 0);
118 * Make the *dest path the same as src and re-initialize src path to
121 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
125 BUG_ON(path_root_bh(dest) != path_root_bh(src));
127 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
128 brelse(dest->p_node[i].bh);
130 dest->p_node[i].bh = src->p_node[i].bh;
131 dest->p_node[i].el = src->p_node[i].el;
133 src->p_node[i].bh = NULL;
134 src->p_node[i].el = NULL;
139 * Insert an extent block at given index.
141 * This will not take an additional reference on eb_bh.
143 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
144 struct buffer_head *eb_bh)
146 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
149 * Right now, no root bh is an extent block, so this helps
150 * catch code errors with dinode trees. The assertion can be
151 * safely removed if we ever need to insert extent block
152 * structures at the root.
156 path->p_node[index].bh = eb_bh;
157 path->p_node[index].el = &eb->h_list;
160 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
161 struct ocfs2_extent_list *root_el)
163 struct ocfs2_path *path;
165 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
167 path = kzalloc(sizeof(*path), GFP_NOFS);
169 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
171 path_root_bh(path) = root_bh;
172 path_root_el(path) = root_el;
179 * Allocate and initialize a new path based on a disk inode tree.
181 static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh)
183 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
184 struct ocfs2_extent_list *el = &di->id2.i_list;
186 return ocfs2_new_path(di_bh, el);
190 * Convenience function to journal all components in a path.
192 static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
193 struct ocfs2_path *path)
200 for(i = 0; i < path_num_items(path); i++) {
201 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
202 OCFS2_JOURNAL_ACCESS_WRITE);
213 enum ocfs2_contig_type {
219 static int ocfs2_block_extent_contig(struct super_block *sb,
220 struct ocfs2_extent_rec *ext,
223 return blkno == (le64_to_cpu(ext->e_blkno) +
224 ocfs2_clusters_to_blocks(sb,
225 le32_to_cpu(ext->e_clusters)));
228 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
229 struct ocfs2_extent_rec *right)
231 return (le32_to_cpu(left->e_cpos) + le32_to_cpu(left->e_clusters) ==
232 le32_to_cpu(right->e_cpos));
235 static enum ocfs2_contig_type
236 ocfs2_extent_contig(struct inode *inode,
237 struct ocfs2_extent_rec *ext,
238 struct ocfs2_extent_rec *insert_rec)
240 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
242 if (ocfs2_extents_adjacent(ext, insert_rec) &&
243 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
246 blkno = le64_to_cpu(ext->e_blkno);
247 if (ocfs2_extents_adjacent(insert_rec, ext) &&
248 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
255 * NOTE: We can have pretty much any combination of contiguousness and
258 * The usefulness of APPEND_TAIL is more in that it lets us know that
259 * we'll have to update the path to that leaf.
261 enum ocfs2_append_type {
266 struct ocfs2_insert_type {
267 enum ocfs2_append_type ins_appending;
268 enum ocfs2_contig_type ins_contig;
269 int ins_contig_index;
270 int ins_free_records;
275 * How many free extents have we got before we need more meta data?
277 int ocfs2_num_free_extents(struct ocfs2_super *osb,
279 struct ocfs2_dinode *fe)
282 struct ocfs2_extent_list *el;
283 struct ocfs2_extent_block *eb;
284 struct buffer_head *eb_bh = NULL;
288 if (!OCFS2_IS_VALID_DINODE(fe)) {
289 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
294 if (fe->i_last_eb_blk) {
295 retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
296 &eb_bh, OCFS2_BH_CACHED, inode);
301 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
304 el = &fe->id2.i_list;
306 BUG_ON(el->l_tree_depth != 0);
308 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
317 /* expects array to already be allocated
319 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
322 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
326 struct ocfs2_alloc_context *meta_ac,
327 struct buffer_head *bhs[])
329 int count, status, i;
330 u16 suballoc_bit_start;
333 struct ocfs2_extent_block *eb;
338 while (count < wanted) {
339 status = ocfs2_claim_metadata(osb,
351 for(i = count; i < (num_got + count); i++) {
352 bhs[i] = sb_getblk(osb->sb, first_blkno);
353 if (bhs[i] == NULL) {
358 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
360 status = ocfs2_journal_access(handle, inode, bhs[i],
361 OCFS2_JOURNAL_ACCESS_CREATE);
367 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
368 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
369 /* Ok, setup the minimal stuff here. */
370 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
371 eb->h_blkno = cpu_to_le64(first_blkno);
372 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
374 #ifndef OCFS2_USE_ALL_METADATA_SUBALLOCATORS
375 /* we always use slot zero's suballocator */
376 eb->h_suballoc_slot = 0;
378 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
380 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
382 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
384 suballoc_bit_start++;
387 /* We'll also be dirtied by the caller, so
388 * this isn't absolutely necessary. */
389 status = ocfs2_journal_dirty(handle, bhs[i]);
402 for(i = 0; i < wanted; i++) {
413 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
415 * Returns the sum of the rightmost extent rec logical offset and
418 * ocfs2_add_branch() uses this to determine what logical cluster
419 * value should be populated into the leftmost new branch records.
421 * ocfs2_shift_tree_depth() uses this to determine the # clusters
422 * value for the new topmost tree record.
424 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
428 i = le16_to_cpu(el->l_next_free_rec) - 1;
430 return le32_to_cpu(el->l_recs[i].e_cpos) +
431 le32_to_cpu(el->l_recs[i].e_clusters);
435 * Add an entire tree branch to our inode. eb_bh is the extent block
436 * to start at, if we don't want to start the branch at the dinode
439 * last_eb_bh is required as we have to update it's next_leaf pointer
440 * for the new last extent block.
442 * the new branch will be 'empty' in the sense that every block will
443 * contain a single record with e_clusters == 0.
445 static int ocfs2_add_branch(struct ocfs2_super *osb,
448 struct buffer_head *fe_bh,
449 struct buffer_head *eb_bh,
450 struct buffer_head *last_eb_bh,
451 struct ocfs2_alloc_context *meta_ac)
453 int status, new_blocks, i;
454 u64 next_blkno, new_last_eb_blk;
455 struct buffer_head *bh;
456 struct buffer_head **new_eb_bhs = NULL;
457 struct ocfs2_dinode *fe;
458 struct ocfs2_extent_block *eb;
459 struct ocfs2_extent_list *eb_el;
460 struct ocfs2_extent_list *el;
467 fe = (struct ocfs2_dinode *) fe_bh->b_data;
470 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
473 el = &fe->id2.i_list;
475 /* we never add a branch to a leaf. */
476 BUG_ON(!el->l_tree_depth);
478 new_blocks = le16_to_cpu(el->l_tree_depth);
480 /* allocate the number of new eb blocks we need */
481 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
489 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
490 meta_ac, new_eb_bhs);
496 eb = (struct ocfs2_extent_block *)last_eb_bh->b_data;
497 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
499 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
500 * linked with the rest of the tree.
501 * conversly, new_eb_bhs[0] is the new bottommost leaf.
503 * when we leave the loop, new_last_eb_blk will point to the
504 * newest leaf, and next_blkno will point to the topmost extent
506 next_blkno = new_last_eb_blk = 0;
507 for(i = 0; i < new_blocks; i++) {
509 eb = (struct ocfs2_extent_block *) bh->b_data;
510 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
511 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
517 status = ocfs2_journal_access(handle, inode, bh,
518 OCFS2_JOURNAL_ACCESS_CREATE);
524 eb->h_next_leaf_blk = 0;
525 eb_el->l_tree_depth = cpu_to_le16(i);
526 eb_el->l_next_free_rec = cpu_to_le16(1);
528 * This actually counts as an empty extent as
531 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
532 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
533 eb_el->l_recs[0].e_clusters = cpu_to_le32(0);
534 if (!eb_el->l_tree_depth)
535 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
537 status = ocfs2_journal_dirty(handle, bh);
543 next_blkno = le64_to_cpu(eb->h_blkno);
546 /* This is a bit hairy. We want to update up to three blocks
547 * here without leaving any of them in an inconsistent state
548 * in case of error. We don't have to worry about
549 * journal_dirty erroring as it won't unless we've aborted the
550 * handle (in which case we would never be here) so reserving
551 * the write with journal_access is all we need to do. */
552 status = ocfs2_journal_access(handle, inode, last_eb_bh,
553 OCFS2_JOURNAL_ACCESS_WRITE);
558 status = ocfs2_journal_access(handle, inode, fe_bh,
559 OCFS2_JOURNAL_ACCESS_WRITE);
565 status = ocfs2_journal_access(handle, inode, eb_bh,
566 OCFS2_JOURNAL_ACCESS_WRITE);
573 /* Link the new branch into the rest of the tree (el will
574 * either be on the fe, or the extent block passed in. */
575 i = le16_to_cpu(el->l_next_free_rec);
576 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
577 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
578 el->l_recs[i].e_clusters = 0;
579 le16_add_cpu(&el->l_next_free_rec, 1);
581 /* fe needs a new last extent block pointer, as does the
582 * next_leaf on the previously last-extent-block. */
583 fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk);
585 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
586 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
588 status = ocfs2_journal_dirty(handle, last_eb_bh);
591 status = ocfs2_journal_dirty(handle, fe_bh);
595 status = ocfs2_journal_dirty(handle, eb_bh);
603 for (i = 0; i < new_blocks; i++)
605 brelse(new_eb_bhs[i]);
614 * adds another level to the allocation tree.
615 * returns back the new extent block so you can add a branch to it
618 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
621 struct buffer_head *fe_bh,
622 struct ocfs2_alloc_context *meta_ac,
623 struct buffer_head **ret_new_eb_bh)
627 struct buffer_head *new_eb_bh = NULL;
628 struct ocfs2_dinode *fe;
629 struct ocfs2_extent_block *eb;
630 struct ocfs2_extent_list *fe_el;
631 struct ocfs2_extent_list *eb_el;
635 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
642 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
643 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
644 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
650 fe = (struct ocfs2_dinode *) fe_bh->b_data;
651 fe_el = &fe->id2.i_list;
653 status = ocfs2_journal_access(handle, inode, new_eb_bh,
654 OCFS2_JOURNAL_ACCESS_CREATE);
660 /* copy the fe data into the new extent block */
661 eb_el->l_tree_depth = fe_el->l_tree_depth;
662 eb_el->l_next_free_rec = fe_el->l_next_free_rec;
663 for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++) {
664 eb_el->l_recs[i].e_cpos = fe_el->l_recs[i].e_cpos;
665 eb_el->l_recs[i].e_clusters = fe_el->l_recs[i].e_clusters;
666 eb_el->l_recs[i].e_blkno = fe_el->l_recs[i].e_blkno;
669 status = ocfs2_journal_dirty(handle, new_eb_bh);
675 status = ocfs2_journal_access(handle, inode, fe_bh,
676 OCFS2_JOURNAL_ACCESS_WRITE);
682 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
685 le16_add_cpu(&fe_el->l_tree_depth, 1);
686 fe_el->l_recs[0].e_cpos = 0;
687 fe_el->l_recs[0].e_blkno = eb->h_blkno;
688 fe_el->l_recs[0].e_clusters = cpu_to_le32(new_clusters);
689 for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++) {
690 fe_el->l_recs[i].e_cpos = 0;
691 fe_el->l_recs[i].e_clusters = 0;
692 fe_el->l_recs[i].e_blkno = 0;
694 fe_el->l_next_free_rec = cpu_to_le16(1);
696 /* If this is our 1st tree depth shift, then last_eb_blk
697 * becomes the allocated extent block */
698 if (fe_el->l_tree_depth == cpu_to_le16(1))
699 fe->i_last_eb_blk = eb->h_blkno;
701 status = ocfs2_journal_dirty(handle, fe_bh);
707 *ret_new_eb_bh = new_eb_bh;
719 * Should only be called when there is no space left in any of the
720 * leaf nodes. What we want to do is find the lowest tree depth
721 * non-leaf extent block with room for new records. There are three
722 * valid results of this search:
724 * 1) a lowest extent block is found, then we pass it back in
725 * *lowest_eb_bh and return '0'
727 * 2) the search fails to find anything, but the dinode has room. We
728 * pass NULL back in *lowest_eb_bh, but still return '0'
730 * 3) the search fails to find anything AND the dinode is full, in
731 * which case we return > 0
733 * return status < 0 indicates an error.
735 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
737 struct buffer_head *fe_bh,
738 struct buffer_head **target_bh)
742 struct ocfs2_dinode *fe;
743 struct ocfs2_extent_block *eb;
744 struct ocfs2_extent_list *el;
745 struct buffer_head *bh = NULL;
746 struct buffer_head *lowest_bh = NULL;
752 fe = (struct ocfs2_dinode *) fe_bh->b_data;
753 el = &fe->id2.i_list;
755 while(le16_to_cpu(el->l_tree_depth) > 1) {
756 if (le16_to_cpu(el->l_next_free_rec) == 0) {
757 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
758 "extent list (next_free_rec == 0)",
759 (unsigned long long)OCFS2_I(inode)->ip_blkno);
763 i = le16_to_cpu(el->l_next_free_rec) - 1;
764 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
766 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
767 "list where extent # %d has no physical "
769 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
779 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
786 eb = (struct ocfs2_extent_block *) bh->b_data;
787 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
788 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
794 if (le16_to_cpu(el->l_next_free_rec) <
795 le16_to_cpu(el->l_count)) {
803 /* If we didn't find one and the fe doesn't have any room,
806 && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count))
809 *target_bh = lowest_bh;
818 static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
820 return !rec->e_clusters;
824 * This function will discard the rightmost extent record.
826 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
828 int next_free = le16_to_cpu(el->l_next_free_rec);
829 int count = le16_to_cpu(el->l_count);
830 unsigned int num_bytes;
833 /* This will cause us to go off the end of our extent list. */
834 BUG_ON(next_free >= count);
836 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
838 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
841 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
842 struct ocfs2_extent_rec *insert_rec)
844 int i, insert_index, next_free, has_empty, num_bytes;
845 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
846 struct ocfs2_extent_rec *rec;
848 next_free = le16_to_cpu(el->l_next_free_rec);
849 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
853 /* The tree code before us didn't allow enough room in the leaf. */
854 if (el->l_next_free_rec == el->l_count && !has_empty)
858 * The easiest way to approach this is to just remove the
859 * empty extent and temporarily decrement next_free.
863 * If next_free was 1 (only an empty extent), this
864 * loop won't execute, which is fine. We still want
865 * the decrement above to happen.
867 for(i = 0; i < (next_free - 1); i++)
868 el->l_recs[i] = el->l_recs[i+1];
874 * Figure out what the new record index should be.
876 for(i = 0; i < next_free; i++) {
877 rec = &el->l_recs[i];
879 if (insert_cpos < le32_to_cpu(rec->e_cpos))
884 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
885 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
887 BUG_ON(insert_index < 0);
888 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
889 BUG_ON(insert_index > next_free);
892 * No need to memmove if we're just adding to the tail.
894 if (insert_index != next_free) {
895 BUG_ON(next_free >= le16_to_cpu(el->l_count));
897 num_bytes = next_free - insert_index;
898 num_bytes *= sizeof(struct ocfs2_extent_rec);
899 memmove(&el->l_recs[insert_index + 1],
900 &el->l_recs[insert_index],
905 * Either we had an empty extent, and need to re-increment or
906 * there was no empty extent on a non full rightmost leaf node,
907 * in which case we still need to increment.
910 el->l_next_free_rec = cpu_to_le16(next_free);
912 * Make sure none of the math above just messed up our tree.
914 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
916 el->l_recs[insert_index] = *insert_rec;
921 * Create an empty extent record .
923 * l_next_free_rec may be updated.
925 * If an empty extent already exists do nothing.
927 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
929 int next_free = le16_to_cpu(el->l_next_free_rec);
934 if (ocfs2_is_empty_extent(&el->l_recs[0]))
937 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
938 "Asked to create an empty extent in a full list:\n"
939 "count = %u, tree depth = %u",
940 le16_to_cpu(el->l_count),
941 le16_to_cpu(el->l_tree_depth));
943 ocfs2_shift_records_right(el);
946 le16_add_cpu(&el->l_next_free_rec, 1);
947 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
951 * For a rotation which involves two leaf nodes, the "root node" is
952 * the lowest level tree node which contains a path to both leafs. This
953 * resulting set of information can be used to form a complete "subtree"
955 * This function is passed two full paths from the dinode down to a
956 * pair of adjacent leaves. It's task is to figure out which path
957 * index contains the subtree root - this can be the root index itself
958 * in a worst-case rotation.
960 * The array index of the subtree root is passed back.
962 static int ocfs2_find_subtree_root(struct inode *inode,
963 struct ocfs2_path *left,
964 struct ocfs2_path *right)
969 * Check that the caller passed in two paths from the same tree.
971 BUG_ON(path_root_bh(left) != path_root_bh(right));
977 * The caller didn't pass two adjacent paths.
979 mlog_bug_on_msg(i > left->p_tree_depth,
980 "Inode %lu, left depth %u, right depth %u\n"
981 "left leaf blk %llu, right leaf blk %llu\n",
982 inode->i_ino, left->p_tree_depth,
984 (unsigned long long)path_leaf_bh(left)->b_blocknr,
985 (unsigned long long)path_leaf_bh(right)->b_blocknr);
986 } while (left->p_node[i].bh->b_blocknr ==
987 right->p_node[i].bh->b_blocknr);
992 typedef void (path_insert_t)(void *, struct buffer_head *);
995 * Traverse a btree path in search of cpos, starting at root_el.
997 * This code can be called with a cpos larger than the tree, in which
998 * case it will return the rightmost path.
1000 static int __ocfs2_find_path(struct inode *inode,
1001 struct ocfs2_extent_list *root_el, u32 cpos,
1002 path_insert_t *func, void *data)
1007 struct buffer_head *bh = NULL;
1008 struct ocfs2_extent_block *eb;
1009 struct ocfs2_extent_list *el;
1010 struct ocfs2_extent_rec *rec;
1011 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1014 while (el->l_tree_depth) {
1015 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1016 ocfs2_error(inode->i_sb,
1017 "Inode %llu has empty extent list at "
1019 (unsigned long long)oi->ip_blkno,
1020 le16_to_cpu(el->l_tree_depth));
1026 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1027 rec = &el->l_recs[i];
1030 * In the case that cpos is off the allocation
1031 * tree, this should just wind up returning the
1034 range = le32_to_cpu(rec->e_cpos) +
1035 le32_to_cpu(rec->e_clusters);
1036 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1040 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1042 ocfs2_error(inode->i_sb,
1043 "Inode %llu has bad blkno in extent list "
1044 "at depth %u (index %d)\n",
1045 (unsigned long long)oi->ip_blkno,
1046 le16_to_cpu(el->l_tree_depth), i);
1053 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1054 &bh, OCFS2_BH_CACHED, inode);
1060 eb = (struct ocfs2_extent_block *) bh->b_data;
1062 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1063 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1068 if (le16_to_cpu(el->l_next_free_rec) >
1069 le16_to_cpu(el->l_count)) {
1070 ocfs2_error(inode->i_sb,
1071 "Inode %llu has bad count in extent list "
1072 "at block %llu (next free=%u, count=%u)\n",
1073 (unsigned long long)oi->ip_blkno,
1074 (unsigned long long)bh->b_blocknr,
1075 le16_to_cpu(el->l_next_free_rec),
1076 le16_to_cpu(el->l_count));
1087 * Catch any trailing bh that the loop didn't handle.
1095 * Given an initialized path (that is, it has a valid root extent
1096 * list), this function will traverse the btree in search of the path
1097 * which would contain cpos.
1099 * The path traveled is recorded in the path structure.
1101 * Note that this will not do any comparisons on leaf node extent
1102 * records, so it will work fine in the case that we just added a tree
1105 struct find_path_data {
1107 struct ocfs2_path *path;
1109 static void find_path_ins(void *data, struct buffer_head *bh)
1111 struct find_path_data *fp = data;
1114 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1117 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1120 struct find_path_data data;
1124 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1125 find_path_ins, &data);
1128 static void find_leaf_ins(void *data, struct buffer_head *bh)
1130 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1131 struct ocfs2_extent_list *el = &eb->h_list;
1132 struct buffer_head **ret = data;
1134 /* We want to retain only the leaf block. */
1135 if (le16_to_cpu(el->l_tree_depth) == 0) {
1141 * Find the leaf block in the tree which would contain cpos. No
1142 * checking of the actual leaf is done.
1144 * Some paths want to call this instead of allocating a path structure
1145 * and calling ocfs2_find_path().
1147 * This function doesn't handle non btree extent lists.
1149 static int ocfs2_find_leaf(struct inode *inode,
1150 struct ocfs2_extent_list *root_el, u32 cpos,
1151 struct buffer_head **leaf_bh)
1154 struct buffer_head *bh = NULL;
1156 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1168 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1170 * Basically, we've moved stuff around at the bottom of the tree and
1171 * we need to fix up the extent records above the changes to reflect
1174 * left_rec: the record on the left.
1175 * left_child_el: is the child list pointed to by left_rec
1176 * right_rec: the record to the right of left_rec
1177 * right_child_el: is the child list pointed to by right_rec
1179 * By definition, this only works on interior nodes.
1181 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1182 struct ocfs2_extent_list *left_child_el,
1183 struct ocfs2_extent_rec *right_rec,
1184 struct ocfs2_extent_list *right_child_el)
1186 u32 left_clusters, right_end;
1189 * Interior nodes never have holes. Their cpos is the cpos of
1190 * the leftmost record in their child list. Their cluster
1191 * count covers the full theoretical range of their child list
1192 * - the range between their cpos and the cpos of the record
1193 * immediately to their right.
1195 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1196 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1197 left_rec->e_clusters = cpu_to_le32(left_clusters);
1200 * Calculate the rightmost cluster count boundary before
1201 * moving cpos - we will need to adjust e_clusters after
1202 * updating e_cpos to keep the same highest cluster count.
1204 right_end = le32_to_cpu(right_rec->e_cpos);
1205 right_end += le32_to_cpu(right_rec->e_clusters);
1207 right_rec->e_cpos = left_rec->e_cpos;
1208 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1210 right_end -= le32_to_cpu(right_rec->e_cpos);
1211 right_rec->e_clusters = cpu_to_le32(right_end);
1215 * Adjust the adjacent root node records involved in a
1216 * rotation. left_el_blkno is passed in as a key so that we can easily
1217 * find it's index in the root list.
1219 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1220 struct ocfs2_extent_list *left_el,
1221 struct ocfs2_extent_list *right_el,
1226 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1227 le16_to_cpu(left_el->l_tree_depth));
1229 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1230 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1235 * The path walking code should have never returned a root and
1236 * two paths which are not adjacent.
1238 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1240 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1241 &root_el->l_recs[i + 1], right_el);
1245 * We've changed a leaf block (in right_path) and need to reflect that
1246 * change back up the subtree.
1248 * This happens in multiple places:
1249 * - When we've moved an extent record from the left path leaf to the right
1250 * path leaf to make room for an empty extent in the left path leaf.
1251 * - When our insert into the right path leaf is at the leftmost edge
1252 * and requires an update of the path immediately to it's left. This
1253 * can occur at the end of some types of rotation and appending inserts.
1255 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1256 struct ocfs2_path *left_path,
1257 struct ocfs2_path *right_path,
1261 struct ocfs2_extent_list *el, *left_el, *right_el;
1262 struct ocfs2_extent_rec *left_rec, *right_rec;
1263 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1266 * Update the counts and position values within all the
1267 * interior nodes to reflect the leaf rotation we just did.
1269 * The root node is handled below the loop.
1271 * We begin the loop with right_el and left_el pointing to the
1272 * leaf lists and work our way up.
1274 * NOTE: within this loop, left_el and right_el always refer
1275 * to the *child* lists.
1277 left_el = path_leaf_el(left_path);
1278 right_el = path_leaf_el(right_path);
1279 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1280 mlog(0, "Adjust records at index %u\n", i);
1283 * One nice property of knowing that all of these
1284 * nodes are below the root is that we only deal with
1285 * the leftmost right node record and the rightmost
1288 el = left_path->p_node[i].el;
1289 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1290 left_rec = &el->l_recs[idx];
1292 el = right_path->p_node[i].el;
1293 right_rec = &el->l_recs[0];
1295 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1298 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1302 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1307 * Setup our list pointers now so that the current
1308 * parents become children in the next iteration.
1310 left_el = left_path->p_node[i].el;
1311 right_el = right_path->p_node[i].el;
1315 * At the root node, adjust the two adjacent records which
1316 * begin our path to the leaves.
1319 el = left_path->p_node[subtree_index].el;
1320 left_el = left_path->p_node[subtree_index + 1].el;
1321 right_el = right_path->p_node[subtree_index + 1].el;
1323 ocfs2_adjust_root_records(el, left_el, right_el,
1324 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1326 root_bh = left_path->p_node[subtree_index].bh;
1328 ret = ocfs2_journal_dirty(handle, root_bh);
1333 static int ocfs2_rotate_subtree_right(struct inode *inode,
1335 struct ocfs2_path *left_path,
1336 struct ocfs2_path *right_path,
1340 struct buffer_head *right_leaf_bh;
1341 struct buffer_head *left_leaf_bh = NULL;
1342 struct buffer_head *root_bh;
1343 struct ocfs2_extent_list *right_el, *left_el;
1344 struct ocfs2_extent_rec move_rec;
1346 left_leaf_bh = path_leaf_bh(left_path);
1347 left_el = path_leaf_el(left_path);
1349 if (left_el->l_next_free_rec != left_el->l_count) {
1350 ocfs2_error(inode->i_sb,
1351 "Inode %llu has non-full interior leaf node %llu"
1353 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1354 (unsigned long long)left_leaf_bh->b_blocknr,
1355 le16_to_cpu(left_el->l_next_free_rec));
1360 * This extent block may already have an empty record, so we
1361 * return early if so.
1363 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1366 root_bh = left_path->p_node[subtree_index].bh;
1367 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1369 ret = ocfs2_journal_access(handle, inode, root_bh,
1370 OCFS2_JOURNAL_ACCESS_WRITE);
1376 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1377 ret = ocfs2_journal_access(handle, inode,
1378 right_path->p_node[i].bh,
1379 OCFS2_JOURNAL_ACCESS_WRITE);
1385 ret = ocfs2_journal_access(handle, inode,
1386 left_path->p_node[i].bh,
1387 OCFS2_JOURNAL_ACCESS_WRITE);
1394 right_leaf_bh = path_leaf_bh(right_path);
1395 right_el = path_leaf_el(right_path);
1397 /* This is a code error, not a disk corruption. */
1398 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1399 "because rightmost leaf block %llu is empty\n",
1400 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1401 (unsigned long long)right_leaf_bh->b_blocknr);
1403 ocfs2_create_empty_extent(right_el);
1405 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1411 /* Do the copy now. */
1412 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1413 move_rec = left_el->l_recs[i];
1414 right_el->l_recs[0] = move_rec;
1417 * Clear out the record we just copied and shift everything
1418 * over, leaving an empty extent in the left leaf.
1420 * We temporarily subtract from next_free_rec so that the
1421 * shift will lose the tail record (which is now defunct).
1423 le16_add_cpu(&left_el->l_next_free_rec, -1);
1424 ocfs2_shift_records_right(left_el);
1425 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1426 le16_add_cpu(&left_el->l_next_free_rec, 1);
1428 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1434 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1442 * Given a full path, determine what cpos value would return us a path
1443 * containing the leaf immediately to the left of the current one.
1445 * Will return zero if the path passed in is already the leftmost path.
1447 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1448 struct ocfs2_path *path, u32 *cpos)
1452 struct ocfs2_extent_list *el;
1456 blkno = path_leaf_bh(path)->b_blocknr;
1458 /* Start at the tree node just above the leaf and work our way up. */
1459 i = path->p_tree_depth - 1;
1461 el = path->p_node[i].el;
1464 * Find the extent record just before the one in our
1467 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1468 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1472 * We've determined that the
1473 * path specified is already
1474 * the leftmost one - return a
1480 * The leftmost record points to our
1481 * leaf - we need to travel up the
1487 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
1488 *cpos = *cpos + le32_to_cpu(el->l_recs[j - 1].e_clusters) - 1;
1494 * If we got here, we never found a valid node where
1495 * the tree indicated one should be.
1498 "Invalid extent tree at extent block %llu\n",
1499 (unsigned long long)blkno);
1504 blkno = path->p_node[i].bh->b_blocknr;
1512 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
1513 struct ocfs2_path *path)
1515 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1;
1517 if (handle->h_buffer_credits < credits)
1518 return ocfs2_extend_trans(handle, credits);
1524 * Trap the case where we're inserting into the theoretical range past
1525 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1526 * whose cpos is less than ours into the right leaf.
1528 * It's only necessary to look at the rightmost record of the left
1529 * leaf because the logic that calls us should ensure that the
1530 * theoretical ranges in the path components above the leaves are
1533 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1536 struct ocfs2_extent_list *left_el;
1537 struct ocfs2_extent_rec *rec;
1540 left_el = path_leaf_el(left_path);
1541 next_free = le16_to_cpu(left_el->l_next_free_rec);
1542 rec = &left_el->l_recs[next_free - 1];
1544 if (insert_cpos > le32_to_cpu(rec->e_cpos))
1550 * Rotate all the records in a btree right one record, starting at insert_cpos.
1552 * The path to the rightmost leaf should be passed in.
1554 * The array is assumed to be large enough to hold an entire path (tree depth).
1556 * Upon succesful return from this function:
1558 * - The 'right_path' array will contain a path to the leaf block
1559 * whose range contains e_cpos.
1560 * - That leaf block will have a single empty extent in list index 0.
1561 * - In the case that the rotation requires a post-insert update,
1562 * *ret_left_path will contain a valid path which can be passed to
1563 * ocfs2_insert_path().
1565 static int ocfs2_rotate_tree_right(struct inode *inode,
1568 struct ocfs2_path *right_path,
1569 struct ocfs2_path **ret_left_path)
1573 struct ocfs2_path *left_path = NULL;
1575 *ret_left_path = NULL;
1577 left_path = ocfs2_new_path(path_root_bh(right_path),
1578 path_root_el(right_path));
1585 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
1591 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
1594 * What we want to do here is:
1596 * 1) Start with the rightmost path.
1598 * 2) Determine a path to the leaf block directly to the left
1601 * 3) Determine the 'subtree root' - the lowest level tree node
1602 * which contains a path to both leaves.
1604 * 4) Rotate the subtree.
1606 * 5) Find the next subtree by considering the left path to be
1607 * the new right path.
1609 * The check at the top of this while loop also accepts
1610 * insert_cpos == cpos because cpos is only a _theoretical_
1611 * value to get us the left path - insert_cpos might very well
1612 * be filling that hole.
1614 * Stop at a cpos of '0' because we either started at the
1615 * leftmost branch (i.e., a tree with one branch and a
1616 * rotation inside of it), or we've gone as far as we can in
1617 * rotating subtrees.
1619 while (cpos && insert_cpos <= cpos) {
1620 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
1623 ret = ocfs2_find_path(inode, left_path, cpos);
1629 mlog_bug_on_msg(path_leaf_bh(left_path) ==
1630 path_leaf_bh(right_path),
1631 "Inode %lu: error during insert of %u "
1632 "(left path cpos %u) results in two identical "
1633 "paths ending at %llu\n",
1634 inode->i_ino, insert_cpos, cpos,
1635 (unsigned long long)
1636 path_leaf_bh(left_path)->b_blocknr);
1638 if (ocfs2_rotate_requires_path_adjustment(left_path,
1640 mlog(0, "Path adjustment required\n");
1643 * We've rotated the tree as much as we
1644 * should. The rest is up to
1645 * ocfs2_insert_path() to complete, after the
1646 * record insertion. We indicate this
1647 * situation by returning the left path.
1649 * The reason we don't adjust the records here
1650 * before the record insert is that an error
1651 * later might break the rule where a parent
1652 * record e_cpos will reflect the actual
1653 * e_cpos of the 1st nonempty record of the
1656 *ret_left_path = left_path;
1660 start = ocfs2_find_subtree_root(inode, left_path, right_path);
1662 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
1664 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
1665 right_path->p_tree_depth);
1667 ret = ocfs2_extend_rotate_transaction(handle, start,
1674 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
1682 * There is no need to re-read the next right path
1683 * as we know that it'll be our current left
1684 * path. Optimize by copying values instead.
1686 ocfs2_mv_path(right_path, left_path);
1688 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
1697 ocfs2_free_path(left_path);
1704 * Do the final bits of extent record insertion at the target leaf
1705 * list. If this leaf is part of an allocation tree, it is assumed
1706 * that the tree above has been prepared.
1708 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
1709 struct ocfs2_extent_list *el,
1710 struct ocfs2_insert_type *insert,
1711 struct inode *inode)
1713 int i = insert->ins_contig_index;
1715 struct ocfs2_extent_rec *rec;
1717 BUG_ON(el->l_tree_depth);
1720 * Contiguous insert - either left or right.
1722 if (insert->ins_contig != CONTIG_NONE) {
1723 rec = &el->l_recs[i];
1724 if (insert->ins_contig == CONTIG_LEFT) {
1725 rec->e_blkno = insert_rec->e_blkno;
1726 rec->e_cpos = insert_rec->e_cpos;
1728 le32_add_cpu(&rec->e_clusters,
1729 le32_to_cpu(insert_rec->e_clusters));
1734 * Handle insert into an empty leaf.
1736 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
1737 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
1738 ocfs2_is_empty_extent(&el->l_recs[0]))) {
1739 el->l_recs[0] = *insert_rec;
1740 el->l_next_free_rec = cpu_to_le16(1);
1747 if (insert->ins_appending == APPEND_TAIL) {
1748 i = le16_to_cpu(el->l_next_free_rec) - 1;
1749 rec = &el->l_recs[i];
1750 range = le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters);
1751 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
1753 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
1754 le16_to_cpu(el->l_count),
1755 "inode %lu, depth %u, count %u, next free %u, "
1756 "rec.cpos %u, rec.clusters %u, "
1757 "insert.cpos %u, insert.clusters %u\n",
1759 le16_to_cpu(el->l_tree_depth),
1760 le16_to_cpu(el->l_count),
1761 le16_to_cpu(el->l_next_free_rec),
1762 le32_to_cpu(el->l_recs[i].e_cpos),
1763 le32_to_cpu(el->l_recs[i].e_clusters),
1764 le32_to_cpu(insert_rec->e_cpos),
1765 le32_to_cpu(insert_rec->e_clusters));
1767 el->l_recs[i] = *insert_rec;
1768 le16_add_cpu(&el->l_next_free_rec, 1);
1773 * Ok, we have to rotate.
1775 * At this point, it is safe to assume that inserting into an
1776 * empty leaf and appending to a leaf have both been handled
1779 * This leaf needs to have space, either by the empty 1st
1780 * extent record, or by virtue of an l_next_rec < l_count.
1782 ocfs2_rotate_leaf(el, insert_rec);
1785 static inline void ocfs2_update_dinode_clusters(struct inode *inode,
1786 struct ocfs2_dinode *di,
1789 le32_add_cpu(&di->i_clusters, clusters);
1790 spin_lock(&OCFS2_I(inode)->ip_lock);
1791 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
1792 spin_unlock(&OCFS2_I(inode)->ip_lock);
1795 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
1796 struct ocfs2_extent_rec *insert_rec,
1797 struct ocfs2_path *right_path,
1798 struct ocfs2_path **ret_left_path)
1800 int ret, i, next_free;
1801 struct buffer_head *bh;
1802 struct ocfs2_extent_list *el;
1803 struct ocfs2_path *left_path = NULL;
1805 *ret_left_path = NULL;
1808 * If our appending insert is at the leftmost edge of a leaf,
1809 * then we might need to update the rightmost records of the
1812 el = path_leaf_el(right_path);
1813 next_free = le16_to_cpu(el->l_next_free_rec);
1814 if (next_free == 0 ||
1815 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
1818 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
1825 mlog(0, "Append may need a left path update. cpos: %u, "
1826 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
1830 * No need to worry if the append is already in the
1834 left_path = ocfs2_new_path(path_root_bh(right_path),
1835 path_root_el(right_path));
1842 ret = ocfs2_find_path(inode, left_path, left_cpos);
1849 * ocfs2_insert_path() will pass the left_path to the
1855 ret = ocfs2_journal_access_path(inode, handle, right_path);
1861 el = path_root_el(right_path);
1862 bh = path_root_bh(right_path);
1865 next_free = le16_to_cpu(el->l_next_free_rec);
1866 if (next_free == 0) {
1867 ocfs2_error(inode->i_sb,
1868 "Dinode %llu has a bad extent list",
1869 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1874 el->l_recs[next_free - 1].e_clusters = insert_rec->e_cpos;
1875 le32_add_cpu(&el->l_recs[next_free - 1].e_clusters,
1876 le32_to_cpu(insert_rec->e_clusters));
1877 le32_add_cpu(&el->l_recs[next_free - 1].e_clusters,
1878 -le32_to_cpu(el->l_recs[next_free - 1].e_cpos));
1880 ret = ocfs2_journal_dirty(handle, bh);
1884 if (++i >= right_path->p_tree_depth)
1887 bh = right_path->p_node[i].bh;
1888 el = right_path->p_node[i].el;
1891 *ret_left_path = left_path;
1895 ocfs2_free_path(left_path);
1901 * This function only does inserts on an allocation b-tree. For dinode
1902 * lists, ocfs2_insert_at_leaf() is called directly.
1904 * right_path is the path we want to do the actual insert
1905 * in. left_path should only be passed in if we need to update that
1906 * portion of the tree after an edge insert.
1908 static int ocfs2_insert_path(struct inode *inode,
1910 struct ocfs2_path *left_path,
1911 struct ocfs2_path *right_path,
1912 struct ocfs2_extent_rec *insert_rec,
1913 struct ocfs2_insert_type *insert)
1915 int ret, subtree_index;
1916 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
1917 struct ocfs2_extent_list *el;
1920 * Pass both paths to the journal. The majority of inserts
1921 * will be touching all components anyway.
1923 ret = ocfs2_journal_access_path(inode, handle, right_path);
1930 int credits = handle->h_buffer_credits;
1933 * There's a chance that left_path got passed back to
1934 * us without being accounted for in the
1935 * journal. Extend our transaction here to be sure we
1936 * can change those blocks.
1938 credits += left_path->p_tree_depth;
1940 ret = ocfs2_extend_trans(handle, credits);
1946 ret = ocfs2_journal_access_path(inode, handle, left_path);
1953 el = path_leaf_el(right_path);
1955 ocfs2_insert_at_leaf(insert_rec, el, insert, inode);
1956 ret = ocfs2_journal_dirty(handle, leaf_bh);
1962 * The rotate code has indicated that we need to fix
1963 * up portions of the tree after the insert.
1965 * XXX: Should we extend the transaction here?
1967 subtree_index = ocfs2_find_subtree_root(inode, left_path,
1969 ocfs2_complete_edge_insert(inode, handle, left_path,
1970 right_path, subtree_index);
1978 static int ocfs2_do_insert_extent(struct inode *inode,
1980 struct buffer_head *di_bh,
1981 struct ocfs2_extent_rec *insert_rec,
1982 struct ocfs2_insert_type *type)
1984 int ret, rotate = 0;
1986 struct ocfs2_path *right_path = NULL;
1987 struct ocfs2_path *left_path = NULL;
1988 struct ocfs2_dinode *di;
1989 struct ocfs2_extent_list *el;
1991 di = (struct ocfs2_dinode *) di_bh->b_data;
1992 el = &di->id2.i_list;
1994 ret = ocfs2_journal_access(handle, inode, di_bh,
1995 OCFS2_JOURNAL_ACCESS_WRITE);
2001 if (le16_to_cpu(el->l_tree_depth) == 0) {
2002 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
2003 goto out_update_clusters;
2006 right_path = ocfs2_new_inode_path(di_bh);
2014 * Determine the path to start with. Rotations need the
2015 * rightmost path, everything else can go directly to the
2018 cpos = le32_to_cpu(insert_rec->e_cpos);
2019 if (type->ins_appending == APPEND_NONE &&
2020 type->ins_contig == CONTIG_NONE) {
2025 ret = ocfs2_find_path(inode, right_path, cpos);
2032 * Rotations and appends need special treatment - they modify
2033 * parts of the tree's above them.
2035 * Both might pass back a path immediate to the left of the
2036 * one being inserted to. This will be cause
2037 * ocfs2_insert_path() to modify the rightmost records of
2038 * left_path to account for an edge insert.
2040 * XXX: When modifying this code, keep in mind that an insert
2041 * can wind up skipping both of these two special cases...
2044 ret = ocfs2_rotate_tree_right(inode, handle,
2045 le32_to_cpu(insert_rec->e_cpos),
2046 right_path, &left_path);
2051 } else if (type->ins_appending == APPEND_TAIL
2052 && type->ins_contig != CONTIG_LEFT) {
2053 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
2054 right_path, &left_path);
2061 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
2068 out_update_clusters:
2069 ocfs2_update_dinode_clusters(inode, di,
2070 le32_to_cpu(insert_rec->e_clusters));
2072 ret = ocfs2_journal_dirty(handle, di_bh);
2077 ocfs2_free_path(left_path);
2078 ocfs2_free_path(right_path);
2083 static void ocfs2_figure_contig_type(struct inode *inode,
2084 struct ocfs2_insert_type *insert,
2085 struct ocfs2_extent_list *el,
2086 struct ocfs2_extent_rec *insert_rec)
2089 enum ocfs2_contig_type contig_type = CONTIG_NONE;
2091 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
2092 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
2094 if (contig_type != CONTIG_NONE) {
2095 insert->ins_contig_index = i;
2099 insert->ins_contig = contig_type;
2103 * This should only be called against the righmost leaf extent list.
2105 * ocfs2_figure_appending_type() will figure out whether we'll have to
2106 * insert at the tail of the rightmost leaf.
2108 * This should also work against the dinode list for tree's with 0
2109 * depth. If we consider the dinode list to be the rightmost leaf node
2110 * then the logic here makes sense.
2112 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
2113 struct ocfs2_extent_list *el,
2114 struct ocfs2_extent_rec *insert_rec)
2117 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
2118 struct ocfs2_extent_rec *rec;
2120 insert->ins_appending = APPEND_NONE;
2122 BUG_ON(el->l_tree_depth);
2124 if (!el->l_next_free_rec)
2125 goto set_tail_append;
2127 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
2128 /* Were all records empty? */
2129 if (le16_to_cpu(el->l_next_free_rec) == 1)
2130 goto set_tail_append;
2133 i = le16_to_cpu(el->l_next_free_rec) - 1;
2134 rec = &el->l_recs[i];
2136 if (cpos >= (le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)))
2137 goto set_tail_append;
2142 insert->ins_appending = APPEND_TAIL;
2146 * Helper function called at the begining of an insert.
2148 * This computes a few things that are commonly used in the process of
2149 * inserting into the btree:
2150 * - Whether the new extent is contiguous with an existing one.
2151 * - The current tree depth.
2152 * - Whether the insert is an appending one.
2153 * - The total # of free records in the tree.
2155 * All of the information is stored on the ocfs2_insert_type
2158 static int ocfs2_figure_insert_type(struct inode *inode,
2159 struct buffer_head *di_bh,
2160 struct buffer_head **last_eb_bh,
2161 struct ocfs2_extent_rec *insert_rec,
2162 struct ocfs2_insert_type *insert)
2165 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2166 struct ocfs2_extent_block *eb;
2167 struct ocfs2_extent_list *el;
2168 struct ocfs2_path *path = NULL;
2169 struct buffer_head *bh = NULL;
2171 el = &di->id2.i_list;
2172 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
2174 if (el->l_tree_depth) {
2176 * If we have tree depth, we read in the
2177 * rightmost extent block ahead of time as
2178 * ocfs2_figure_insert_type() and ocfs2_add_branch()
2179 * may want it later.
2181 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2182 le64_to_cpu(di->i_last_eb_blk), &bh,
2183 OCFS2_BH_CACHED, inode);
2188 eb = (struct ocfs2_extent_block *) bh->b_data;
2193 * Unless we have a contiguous insert, we'll need to know if
2194 * there is room left in our allocation tree for another
2197 * XXX: This test is simplistic, we can search for empty
2198 * extent records too.
2200 insert->ins_free_records = le16_to_cpu(el->l_count) -
2201 le16_to_cpu(el->l_next_free_rec);
2203 if (!insert->ins_tree_depth) {
2204 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
2205 ocfs2_figure_appending_type(insert, el, insert_rec);
2209 path = ocfs2_new_inode_path(di_bh);
2217 * In the case that we're inserting past what the tree
2218 * currently accounts for, ocfs2_find_path() will return for
2219 * us the rightmost tree path. This is accounted for below in
2220 * the appending code.
2222 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
2228 el = path_leaf_el(path);
2231 * Now that we have the path, there's two things we want to determine:
2232 * 1) Contiguousness (also set contig_index if this is so)
2234 * 2) Are we doing an append? We can trivially break this up
2235 * into two types of appends: simple record append, or a
2236 * rotate inside the tail leaf.
2238 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
2241 * The insert code isn't quite ready to deal with all cases of
2242 * left contiguousness. Specifically, if it's an insert into
2243 * the 1st record in a leaf, it will require the adjustment of
2244 * e_clusters on the last record of the path directly to it's
2245 * left. For now, just catch that case and fool the layers
2246 * above us. This works just fine for tree_depth == 0, which
2247 * is why we allow that above.
2249 if (insert->ins_contig == CONTIG_LEFT &&
2250 insert->ins_contig_index == 0)
2251 insert->ins_contig = CONTIG_NONE;
2254 * Ok, so we can simply compare against last_eb to figure out
2255 * whether the path doesn't exist. This will only happen in
2256 * the case that we're doing a tail append, so maybe we can
2257 * take advantage of that information somehow.
2259 if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) {
2261 * Ok, ocfs2_find_path() returned us the rightmost
2262 * tree path. This might be an appending insert. There are
2264 * 1) We're doing a true append at the tail:
2265 * -This might even be off the end of the leaf
2266 * 2) We're "appending" by rotating in the tail
2268 ocfs2_figure_appending_type(insert, el, insert_rec);
2272 ocfs2_free_path(path);
2282 * Insert an extent into an inode btree.
2284 * The caller needs to update fe->i_clusters
2286 int ocfs2_insert_extent(struct ocfs2_super *osb,
2288 struct inode *inode,
2289 struct buffer_head *fe_bh,
2293 struct ocfs2_alloc_context *meta_ac)
2296 struct buffer_head *last_eb_bh = NULL;
2297 struct buffer_head *bh = NULL;
2298 struct ocfs2_insert_type insert = {0, };
2299 struct ocfs2_extent_rec rec;
2301 mlog(0, "add %u clusters at position %u to inode %llu\n",
2302 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
2304 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
2305 (OCFS2_I(inode)->ip_clusters != cpos),
2306 "Device %s, asking for sparse allocation: inode %llu, "
2307 "cpos %u, clusters %u\n",
2309 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
2310 OCFS2_I(inode)->ip_clusters);
2312 rec.e_cpos = cpu_to_le32(cpos);
2313 rec.e_blkno = cpu_to_le64(start_blk);
2314 rec.e_clusters = cpu_to_le32(new_clusters);
2316 status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec,
2323 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
2324 "Insert.contig_index: %d, Insert.free_records: %d, "
2325 "Insert.tree_depth: %d\n",
2326 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
2327 insert.ins_free_records, insert.ins_tree_depth);
2330 * Avoid growing the tree unless we're out of records and the
2331 * insert type requres one.
2333 if (insert.ins_contig != CONTIG_NONE || insert.ins_free_records)
2336 shift = ocfs2_find_branch_target(osb, inode, fe_bh, &bh);
2343 /* We traveled all the way to the bottom of the allocation tree
2344 * and didn't find room for any more extents - we need to add
2345 * another tree level */
2348 mlog(0, "need to shift tree depth "
2349 "(current = %d)\n", insert.ins_tree_depth);
2351 /* ocfs2_shift_tree_depth will return us a buffer with
2352 * the new extent block (so we can pass that to
2353 * ocfs2_add_branch). */
2354 status = ocfs2_shift_tree_depth(osb, handle, inode, fe_bh,
2360 insert.ins_tree_depth++;
2361 /* Special case: we have room now if we shifted from
2363 if (insert.ins_tree_depth == 1)
2367 /* call ocfs2_add_branch to add the final part of the tree with
2369 mlog(0, "add branch. bh = %p\n", bh);
2370 status = ocfs2_add_branch(osb, handle, inode, fe_bh, bh, last_eb_bh,
2378 /* Finally, we can add clusters. This might rotate the tree for us. */
2379 status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert);
2394 static inline int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
2396 struct buffer_head *tl_bh = osb->osb_tl_bh;
2397 struct ocfs2_dinode *di;
2398 struct ocfs2_truncate_log *tl;
2400 di = (struct ocfs2_dinode *) tl_bh->b_data;
2401 tl = &di->id2.i_dealloc;
2403 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
2404 "slot %d, invalid truncate log parameters: used = "
2405 "%u, count = %u\n", osb->slot_num,
2406 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
2407 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
2410 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
2411 unsigned int new_start)
2413 unsigned int tail_index;
2414 unsigned int current_tail;
2416 /* No records, nothing to coalesce */
2417 if (!le16_to_cpu(tl->tl_used))
2420 tail_index = le16_to_cpu(tl->tl_used) - 1;
2421 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
2422 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
2424 return current_tail == new_start;
2427 static int ocfs2_truncate_log_append(struct ocfs2_super *osb,
2430 unsigned int num_clusters)
2433 unsigned int start_cluster, tl_count;
2434 struct inode *tl_inode = osb->osb_tl_inode;
2435 struct buffer_head *tl_bh = osb->osb_tl_bh;
2436 struct ocfs2_dinode *di;
2437 struct ocfs2_truncate_log *tl;
2439 mlog_entry("start_blk = %llu, num_clusters = %u\n",
2440 (unsigned long long)start_blk, num_clusters);
2442 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
2444 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
2446 di = (struct ocfs2_dinode *) tl_bh->b_data;
2447 tl = &di->id2.i_dealloc;
2448 if (!OCFS2_IS_VALID_DINODE(di)) {
2449 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
2454 tl_count = le16_to_cpu(tl->tl_count);
2455 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
2457 "Truncate record count on #%llu invalid "
2458 "wanted %u, actual %u\n",
2459 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
2460 ocfs2_truncate_recs_per_inode(osb->sb),
2461 le16_to_cpu(tl->tl_count));
2463 /* Caller should have known to flush before calling us. */
2464 index = le16_to_cpu(tl->tl_used);
2465 if (index >= tl_count) {
2471 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
2472 OCFS2_JOURNAL_ACCESS_WRITE);
2478 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
2479 "%llu (index = %d)\n", num_clusters, start_cluster,
2480 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
2482 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
2484 * Move index back to the record we are coalescing with.
2485 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
2489 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
2490 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
2491 index, le32_to_cpu(tl->tl_recs[index].t_start),
2494 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
2495 tl->tl_used = cpu_to_le16(index + 1);
2497 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
2499 status = ocfs2_journal_dirty(handle, tl_bh);
2510 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
2512 struct inode *data_alloc_inode,
2513 struct buffer_head *data_alloc_bh)
2517 unsigned int num_clusters;
2519 struct ocfs2_truncate_rec rec;
2520 struct ocfs2_dinode *di;
2521 struct ocfs2_truncate_log *tl;
2522 struct inode *tl_inode = osb->osb_tl_inode;
2523 struct buffer_head *tl_bh = osb->osb_tl_bh;
2527 di = (struct ocfs2_dinode *) tl_bh->b_data;
2528 tl = &di->id2.i_dealloc;
2529 i = le16_to_cpu(tl->tl_used) - 1;
2531 /* Caller has given us at least enough credits to
2532 * update the truncate log dinode */
2533 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
2534 OCFS2_JOURNAL_ACCESS_WRITE);
2540 tl->tl_used = cpu_to_le16(i);
2542 status = ocfs2_journal_dirty(handle, tl_bh);
2548 /* TODO: Perhaps we can calculate the bulk of the
2549 * credits up front rather than extending like
2551 status = ocfs2_extend_trans(handle,
2552 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
2558 rec = tl->tl_recs[i];
2559 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
2560 le32_to_cpu(rec.t_start));
2561 num_clusters = le32_to_cpu(rec.t_clusters);
2563 /* if start_blk is not set, we ignore the record as
2566 mlog(0, "free record %d, start = %u, clusters = %u\n",
2567 i, le32_to_cpu(rec.t_start), num_clusters);
2569 status = ocfs2_free_clusters(handle, data_alloc_inode,
2570 data_alloc_bh, start_blk,
2585 /* Expects you to already be holding tl_inode->i_mutex */
2586 static int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
2589 unsigned int num_to_flush;
2591 struct inode *tl_inode = osb->osb_tl_inode;
2592 struct inode *data_alloc_inode = NULL;
2593 struct buffer_head *tl_bh = osb->osb_tl_bh;
2594 struct buffer_head *data_alloc_bh = NULL;
2595 struct ocfs2_dinode *di;
2596 struct ocfs2_truncate_log *tl;
2600 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
2602 di = (struct ocfs2_dinode *) tl_bh->b_data;
2603 tl = &di->id2.i_dealloc;
2604 if (!OCFS2_IS_VALID_DINODE(di)) {
2605 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
2610 num_to_flush = le16_to_cpu(tl->tl_used);
2611 mlog(0, "Flush %u records from truncate log #%llu\n",
2612 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
2613 if (!num_to_flush) {
2618 data_alloc_inode = ocfs2_get_system_file_inode(osb,
2619 GLOBAL_BITMAP_SYSTEM_INODE,
2620 OCFS2_INVALID_SLOT);
2621 if (!data_alloc_inode) {
2623 mlog(ML_ERROR, "Could not get bitmap inode!\n");
2627 mutex_lock(&data_alloc_inode->i_mutex);
2629 status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1);
2635 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
2636 if (IS_ERR(handle)) {
2637 status = PTR_ERR(handle);
2642 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
2647 ocfs2_commit_trans(osb, handle);
2650 brelse(data_alloc_bh);
2651 ocfs2_meta_unlock(data_alloc_inode, 1);
2654 mutex_unlock(&data_alloc_inode->i_mutex);
2655 iput(data_alloc_inode);
2662 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
2665 struct inode *tl_inode = osb->osb_tl_inode;
2667 mutex_lock(&tl_inode->i_mutex);
2668 status = __ocfs2_flush_truncate_log(osb);
2669 mutex_unlock(&tl_inode->i_mutex);
2674 static void ocfs2_truncate_log_worker(struct work_struct *work)
2677 struct ocfs2_super *osb =
2678 container_of(work, struct ocfs2_super,
2679 osb_truncate_log_wq.work);
2683 status = ocfs2_flush_truncate_log(osb);
2690 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
2691 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
2694 if (osb->osb_tl_inode) {
2695 /* We want to push off log flushes while truncates are
2698 cancel_delayed_work(&osb->osb_truncate_log_wq);
2700 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
2701 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
2705 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
2707 struct inode **tl_inode,
2708 struct buffer_head **tl_bh)
2711 struct inode *inode = NULL;
2712 struct buffer_head *bh = NULL;
2714 inode = ocfs2_get_system_file_inode(osb,
2715 TRUNCATE_LOG_SYSTEM_INODE,
2719 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
2723 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
2724 OCFS2_BH_CACHED, inode);
2738 /* called during the 1st stage of node recovery. we stamp a clean
2739 * truncate log and pass back a copy for processing later. if the
2740 * truncate log does not require processing, a *tl_copy is set to
2742 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
2744 struct ocfs2_dinode **tl_copy)
2747 struct inode *tl_inode = NULL;
2748 struct buffer_head *tl_bh = NULL;
2749 struct ocfs2_dinode *di;
2750 struct ocfs2_truncate_log *tl;
2754 mlog(0, "recover truncate log from slot %d\n", slot_num);
2756 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
2762 di = (struct ocfs2_dinode *) tl_bh->b_data;
2763 tl = &di->id2.i_dealloc;
2764 if (!OCFS2_IS_VALID_DINODE(di)) {
2765 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
2770 if (le16_to_cpu(tl->tl_used)) {
2771 mlog(0, "We'll have %u logs to recover\n",
2772 le16_to_cpu(tl->tl_used));
2774 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
2781 /* Assuming the write-out below goes well, this copy
2782 * will be passed back to recovery for processing. */
2783 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
2785 /* All we need to do to clear the truncate log is set
2789 status = ocfs2_write_block(osb, tl_bh, tl_inode);
2802 if (status < 0 && (*tl_copy)) {
2811 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
2812 struct ocfs2_dinode *tl_copy)
2816 unsigned int clusters, num_recs, start_cluster;
2819 struct inode *tl_inode = osb->osb_tl_inode;
2820 struct ocfs2_truncate_log *tl;
2824 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
2825 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
2829 tl = &tl_copy->id2.i_dealloc;
2830 num_recs = le16_to_cpu(tl->tl_used);
2831 mlog(0, "cleanup %u records from %llu\n", num_recs,
2832 (unsigned long long)tl_copy->i_blkno);
2834 mutex_lock(&tl_inode->i_mutex);
2835 for(i = 0; i < num_recs; i++) {
2836 if (ocfs2_truncate_log_needs_flush(osb)) {
2837 status = __ocfs2_flush_truncate_log(osb);
2844 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
2845 if (IS_ERR(handle)) {
2846 status = PTR_ERR(handle);
2851 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
2852 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
2853 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
2855 status = ocfs2_truncate_log_append(osb, handle,
2856 start_blk, clusters);
2857 ocfs2_commit_trans(osb, handle);
2865 mutex_unlock(&tl_inode->i_mutex);
2871 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
2874 struct inode *tl_inode = osb->osb_tl_inode;
2879 cancel_delayed_work(&osb->osb_truncate_log_wq);
2880 flush_workqueue(ocfs2_wq);
2882 status = ocfs2_flush_truncate_log(osb);
2886 brelse(osb->osb_tl_bh);
2887 iput(osb->osb_tl_inode);
2893 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
2896 struct inode *tl_inode = NULL;
2897 struct buffer_head *tl_bh = NULL;
2901 status = ocfs2_get_truncate_log_info(osb,
2908 /* ocfs2_truncate_log_shutdown keys on the existence of
2909 * osb->osb_tl_inode so we don't set any of the osb variables
2910 * until we're sure all is well. */
2911 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
2912 ocfs2_truncate_log_worker);
2913 osb->osb_tl_bh = tl_bh;
2914 osb->osb_tl_inode = tl_inode;
2920 /* This function will figure out whether the currently last extent
2921 * block will be deleted, and if it will, what the new last extent
2922 * block will be so we can update his h_next_leaf_blk field, as well
2923 * as the dinodes i_last_eb_blk */
2924 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
2926 struct ocfs2_path *path,
2927 struct buffer_head **new_last_eb)
2931 struct ocfs2_extent_block *eb;
2932 struct ocfs2_extent_list *el;
2933 struct buffer_head *bh = NULL;
2935 *new_last_eb = NULL;
2937 /* we have no tree, so of course, no last_eb. */
2938 if (!path->p_tree_depth)
2941 /* trunc to zero special case - this makes tree_depth = 0
2942 * regardless of what it is. */
2943 if (!new_i_clusters)
2946 el = path_leaf_el(path);
2947 BUG_ON(!el->l_next_free_rec);
2949 /* Make sure that this guy will actually be empty after we
2950 * clear away the data. */
2951 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
2952 if (le16_to_cpu(el->l_next_free_rec) > 1 &&
2953 le32_to_cpu(el->l_recs[1].e_cpos) < new_i_clusters)
2955 } else if (le32_to_cpu(el->l_recs[0].e_cpos) < new_i_clusters)
2958 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2964 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
2970 eb = (struct ocfs2_extent_block *) bh->b_data;
2972 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
2973 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
2979 get_bh(*new_last_eb);
2980 mlog(0, "returning block %llu, (cpos: %u)\n",
2981 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
2988 static int ocfs2_do_truncate(struct ocfs2_super *osb,
2989 unsigned int clusters_to_del,
2990 struct inode *inode,
2991 struct buffer_head *fe_bh,
2993 struct ocfs2_truncate_context *tc,
2994 struct ocfs2_path *path)
2996 int status, i, index;
2997 struct ocfs2_dinode *fe;
2998 struct ocfs2_extent_block *eb;
2999 struct ocfs2_extent_block *last_eb = NULL;
3000 struct ocfs2_extent_list *el;
3001 struct buffer_head *eb_bh = NULL;
3002 struct buffer_head *last_eb_bh = NULL;
3005 fe = (struct ocfs2_dinode *) fe_bh->b_data;
3007 status = ocfs2_find_new_last_ext_blk(inode,
3008 le32_to_cpu(fe->i_clusters) -
3017 * Each component will be touched, so we might as well journal
3018 * here to avoid having to handle errors later.
3020 for (i = 0; i < path_num_items(path); i++) {
3021 status = ocfs2_journal_access(handle, inode,
3023 OCFS2_JOURNAL_ACCESS_WRITE);
3031 status = ocfs2_journal_access(handle, inode, last_eb_bh,
3032 OCFS2_JOURNAL_ACCESS_WRITE);
3038 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
3041 el = &(fe->id2.i_list);
3044 * Lower levels depend on this never happening, but it's best
3045 * to check it up here before changing the tree.
3047 if (el->l_tree_depth && ocfs2_is_empty_extent(&el->l_recs[0])) {
3048 ocfs2_error(inode->i_sb,
3049 "Inode %lu has an empty extent record, depth %u\n",
3050 inode->i_ino, le16_to_cpu(el->l_tree_depth));
3054 spin_lock(&OCFS2_I(inode)->ip_lock);
3055 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
3057 spin_unlock(&OCFS2_I(inode)->ip_lock);
3058 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
3060 i = le16_to_cpu(el->l_next_free_rec) - 1;
3062 BUG_ON(le32_to_cpu(el->l_recs[i].e_clusters) < clusters_to_del);
3063 le32_add_cpu(&el->l_recs[i].e_clusters, -clusters_to_del);
3064 /* tree depth zero, we can just delete the clusters, otherwise
3065 * we need to record the offset of the next level extent block
3066 * as we may overwrite it. */
3067 if (!el->l_tree_depth) {
3068 delete_blk = le64_to_cpu(el->l_recs[i].e_blkno)
3069 + ocfs2_clusters_to_blocks(osb->sb,
3070 le32_to_cpu(el->l_recs[i].e_clusters));
3072 if (!el->l_recs[i].e_clusters) {
3073 /* if we deleted the whole extent record, then clear
3074 * out the other fields and update the extent
3077 el->l_recs[i].e_cpos = 0;
3078 el->l_recs[i].e_blkno = 0;
3079 BUG_ON(!el->l_next_free_rec);
3080 le16_add_cpu(&el->l_next_free_rec, -1);
3083 * The leftmost record might be an empty extent -
3084 * delete it here too.
3086 if (i == 1 && ocfs2_is_empty_extent(&el->l_recs[0])) {
3087 el->l_recs[0].e_cpos = 0;
3088 el->l_recs[0].e_blkno = 0;
3089 el->l_next_free_rec = 0;
3094 if (le32_to_cpu(fe->i_clusters) == 0) {
3095 /* trunc to zero is a special case. */
3096 el->l_tree_depth = 0;
3097 fe->i_last_eb_blk = 0;
3099 fe->i_last_eb_blk = last_eb->h_blkno;
3101 status = ocfs2_journal_dirty(handle, fe_bh);
3108 /* If there will be a new last extent block, then by
3109 * definition, there cannot be any leaves to the right of
3111 last_eb->h_next_leaf_blk = 0;
3112 status = ocfs2_journal_dirty(handle, last_eb_bh);
3120 /* if our tree depth > 0, update all the tree blocks below us. */
3121 while (index <= path->p_tree_depth) {
3122 eb_bh = path->p_node[index].bh;
3123 eb = (struct ocfs2_extent_block *)eb_bh->b_data;
3124 el = path->p_node[index].el;
3126 mlog(0, "traveling tree (index = %d, extent block: %llu)\n",
3127 index, (unsigned long long)eb_bh->b_blocknr);
3129 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
3131 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
3132 ocfs2_error(inode->i_sb,
3133 "Inode %lu has invalid ext. block %llu\n",
3135 (unsigned long long)eb_bh->b_blocknr);
3139 i = le16_to_cpu(el->l_next_free_rec) - 1;
3141 mlog(0, "extent block %llu, before: record %d: "
3142 "(%u, %u, %llu), next = %u\n",
3143 (unsigned long long)le64_to_cpu(eb->h_blkno), i,
3144 le32_to_cpu(el->l_recs[i].e_cpos),
3145 le32_to_cpu(el->l_recs[i].e_clusters),
3146 (unsigned long long)le64_to_cpu(el->l_recs[i].e_blkno),
3147 le16_to_cpu(el->l_next_free_rec));
3149 BUG_ON(le32_to_cpu(el->l_recs[i].e_clusters) < clusters_to_del);
3150 le32_add_cpu(&el->l_recs[i].e_clusters, -clusters_to_del);
3152 /* bottom-most block requires us to delete data.*/
3153 if (!el->l_tree_depth)
3154 delete_blk = le64_to_cpu(el->l_recs[i].e_blkno)
3155 + ocfs2_clusters_to_blocks(osb->sb,
3156 le32_to_cpu(el->l_recs[i].e_clusters));
3157 if (!el->l_recs[i].e_clusters) {
3158 el->l_recs[i].e_cpos = 0;
3159 el->l_recs[i].e_blkno = 0;
3160 BUG_ON(!el->l_next_free_rec);
3161 le16_add_cpu(&el->l_next_free_rec, -1);
3163 if (i == 1 && ocfs2_is_empty_extent(&el->l_recs[0])) {
3164 el->l_recs[0].e_cpos = 0;
3165 el->l_recs[0].e_blkno = 0;
3166 el->l_next_free_rec = 0;
3169 mlog(0, "extent block %llu, after: record %d: "
3170 "(%u, %u, %llu), next = %u\n",
3171 (unsigned long long)le64_to_cpu(eb->h_blkno), i,
3172 le32_to_cpu(el->l_recs[i].e_cpos),
3173 le32_to_cpu(el->l_recs[i].e_clusters),
3174 (unsigned long long)le64_to_cpu(el->l_recs[i].e_blkno),
3175 le16_to_cpu(el->l_next_free_rec));
3177 status = ocfs2_journal_dirty(handle, eb_bh);
3183 if (!el->l_next_free_rec) {
3184 mlog(0, "deleting this extent block.\n");
3186 ocfs2_remove_from_cache(inode, eb_bh);
3188 BUG_ON(el->l_recs[0].e_clusters);
3189 BUG_ON(el->l_recs[0].e_cpos);
3190 BUG_ON(el->l_recs[0].e_blkno);
3193 * We need to remove this extent block from
3194 * the list above it.
3196 * Since we've passed it already in this loop,
3197 * no need to worry about journaling.
3199 el = path->p_node[index - 1].el;
3200 i = le16_to_cpu(el->l_next_free_rec) - 1;
3202 el->l_recs[i].e_cpos = 0;
3203 el->l_recs[i].e_clusters = 0;
3204 el->l_recs[i].e_blkno = 0;
3205 le16_add_cpu(&el->l_next_free_rec, -1);
3207 if (eb->h_suballoc_slot == 0) {
3209 * This code only understands how to
3210 * lock the suballocator in slot 0,
3211 * which is fine because allocation is
3212 * only ever done out of that
3213 * suballocator too. A future version
3214 * might change that however, so avoid
3215 * a free if we don't know how to
3216 * handle it. This way an fs incompat
3217 * bit will not be necessary.
3219 status = ocfs2_free_extent_block(handle,
3220 tc->tc_ext_alloc_inode,
3221 tc->tc_ext_alloc_bh,
3232 BUG_ON(!delete_blk);
3233 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
3247 * It is expected, that by the time you call this function,
3248 * inode->i_size and fe->i_size have been adjusted.
3250 * WARNING: This will kfree the truncate context
3252 int ocfs2_commit_truncate(struct ocfs2_super *osb,
3253 struct inode *inode,
3254 struct buffer_head *fe_bh,
3255 struct ocfs2_truncate_context *tc)
3257 int status, i, credits, tl_sem = 0;
3258 u32 clusters_to_del, new_highest_cpos, range;
3259 struct ocfs2_extent_list *el;
3260 handle_t *handle = NULL;
3261 struct inode *tl_inode = osb->osb_tl_inode;
3262 struct ocfs2_path *path = NULL;
3266 down_write(&OCFS2_I(inode)->ip_alloc_sem);
3268 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
3269 i_size_read(inode));
3271 path = ocfs2_new_inode_path(fe_bh);
3279 * Truncate always works against the rightmost tree branch.
3281 status = ocfs2_find_path(inode, path, UINT_MAX);
3287 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
3288 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
3291 * By now, el will point to the extent list on the bottom most
3292 * portion of this tree. Only the tail record is considered in
3295 * We handle the following cases, in order:
3296 * - empty extent: delete the remaining branch
3297 * - remove the entire record
3298 * - remove a partial record
3299 * - no record needs to be removed (truncate has completed)
3301 el = path_leaf_el(path);
3302 i = le16_to_cpu(el->l_next_free_rec) - 1;
3303 range = le32_to_cpu(el->l_recs[i].e_cpos) +
3304 le32_to_cpu(el->l_recs[i].e_clusters);
3305 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
3306 clusters_to_del = 0;
3307 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
3308 clusters_to_del = le32_to_cpu(el->l_recs[i].e_clusters);
3309 } else if (range > new_highest_cpos) {
3310 clusters_to_del = (le32_to_cpu(el->l_recs[i].e_clusters) +
3311 le32_to_cpu(el->l_recs[i].e_cpos)) -
3318 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
3319 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
3321 BUG_ON(clusters_to_del == 0);
3323 mutex_lock(&tl_inode->i_mutex);
3325 /* ocfs2_truncate_log_needs_flush guarantees us at least one
3326 * record is free for use. If there isn't any, we flush to get
3327 * an empty truncate log. */
3328 if (ocfs2_truncate_log_needs_flush(osb)) {
3329 status = __ocfs2_flush_truncate_log(osb);
3336 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
3337 (struct ocfs2_dinode *)fe_bh->b_data,
3339 handle = ocfs2_start_trans(osb, credits);
3340 if (IS_ERR(handle)) {
3341 status = PTR_ERR(handle);
3347 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
3354 mutex_unlock(&tl_inode->i_mutex);
3357 ocfs2_commit_trans(osb, handle);
3360 ocfs2_reinit_path(path, 1);
3363 * Only loop if we still have allocation.
3365 if (OCFS2_I(inode)->ip_clusters)
3368 up_write(&OCFS2_I(inode)->ip_alloc_sem);
3370 ocfs2_schedule_truncate_log_flush(osb, 1);
3373 mutex_unlock(&tl_inode->i_mutex);
3376 ocfs2_commit_trans(osb, handle);
3378 ocfs2_free_path(path);
3380 /* This will drop the ext_alloc cluster lock for us */
3381 ocfs2_free_truncate_context(tc);
3388 * Expects the inode to already be locked. This will figure out which
3389 * inodes need to be locked and will put them on the returned truncate
3392 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
3393 struct inode *inode,
3394 struct buffer_head *fe_bh,
3395 struct ocfs2_truncate_context **tc)
3397 int status, metadata_delete, i;
3398 unsigned int new_i_clusters;
3399 struct ocfs2_dinode *fe;
3400 struct ocfs2_extent_block *eb;
3401 struct ocfs2_extent_list *el;
3402 struct buffer_head *last_eb_bh = NULL;
3403 struct inode *ext_alloc_inode = NULL;
3404 struct buffer_head *ext_alloc_bh = NULL;
3410 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
3411 i_size_read(inode));
3412 fe = (struct ocfs2_dinode *) fe_bh->b_data;
3414 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
3415 "%llu\n", fe->i_clusters, new_i_clusters,
3416 (unsigned long long)fe->i_size);
3418 if (!ocfs2_sparse_alloc(osb) &&
3419 le32_to_cpu(fe->i_clusters) <= new_i_clusters) {
3420 ocfs2_error(inode->i_sb, "Dinode %llu has cluster count "
3421 "%u and size %llu whereas struct inode has "
3422 "cluster count %u and size %llu which caused an "
3423 "invalid truncate to %u clusters.",
3424 (unsigned long long)le64_to_cpu(fe->i_blkno),
3425 le32_to_cpu(fe->i_clusters),
3426 (unsigned long long)le64_to_cpu(fe->i_size),
3427 OCFS2_I(inode)->ip_clusters, i_size_read(inode),
3429 mlog_meta_lvb(ML_ERROR, &OCFS2_I(inode)->ip_meta_lockres);
3434 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
3441 metadata_delete = 0;
3442 if (fe->id2.i_list.l_tree_depth) {
3443 /* If we have a tree, then the truncate may result in
3444 * metadata deletes. Figure this out from the
3445 * rightmost leaf block.*/
3446 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
3447 &last_eb_bh, OCFS2_BH_CACHED, inode);
3452 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
3453 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
3454 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
3463 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3466 * XXX: Should we check that next_free_rec contains
3469 if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_i_clusters)
3470 metadata_delete = 1;
3473 (*tc)->tc_last_eb_bh = last_eb_bh;
3475 if (metadata_delete) {
3476 mlog(0, "Will have to delete metadata for this trunc. "
3477 "locking allocator.\n");
3478 ext_alloc_inode = ocfs2_get_system_file_inode(osb, EXTENT_ALLOC_SYSTEM_INODE, 0);
3479 if (!ext_alloc_inode) {
3485 mutex_lock(&ext_alloc_inode->i_mutex);
3486 (*tc)->tc_ext_alloc_inode = ext_alloc_inode;
3488 status = ocfs2_meta_lock(ext_alloc_inode, &ext_alloc_bh, 1);
3493 (*tc)->tc_ext_alloc_bh = ext_alloc_bh;
3494 (*tc)->tc_ext_alloc_locked = 1;
3501 ocfs2_free_truncate_context(*tc);
3508 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
3510 if (tc->tc_ext_alloc_inode) {
3511 if (tc->tc_ext_alloc_locked)
3512 ocfs2_meta_unlock(tc->tc_ext_alloc_inode, 1);
3514 mutex_unlock(&tc->tc_ext_alloc_inode->i_mutex);
3515 iput(tc->tc_ext_alloc_inode);
3518 if (tc->tc_ext_alloc_bh)
3519 brelse(tc->tc_ext_alloc_bh);
3521 if (tc->tc_last_eb_bh)
3522 brelse(tc->tc_last_eb_bh);