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);
55 * Structures which describe a path through a btree, and functions to
58 * The idea here is to be as generic as possible with the tree
61 struct ocfs2_path_item {
62 struct buffer_head *bh;
63 struct ocfs2_extent_list *el;
66 #define OCFS2_MAX_PATH_DEPTH 5
70 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
73 #define path_root_bh(_path) ((_path)->p_node[0].bh)
74 #define path_root_el(_path) ((_path)->p_node[0].el)
75 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
76 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
77 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
80 * Reset the actual path elements so that we can re-use the structure
81 * to build another path. Generally, this involves freeing the buffer
84 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
86 int i, start = 0, depth = 0;
87 struct ocfs2_path_item *node;
92 for(i = start; i < path_num_items(path); i++) {
93 node = &path->p_node[i];
101 * Tree depth may change during truncate, or insert. If we're
102 * keeping the root extent list, then make sure that our path
103 * structure reflects the proper depth.
106 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
108 path->p_tree_depth = depth;
111 static void ocfs2_free_path(struct ocfs2_path *path)
114 ocfs2_reinit_path(path, 0);
120 * Make the *dest path the same as src and re-initialize src path to
123 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
127 BUG_ON(path_root_bh(dest) != path_root_bh(src));
129 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
130 brelse(dest->p_node[i].bh);
132 dest->p_node[i].bh = src->p_node[i].bh;
133 dest->p_node[i].el = src->p_node[i].el;
135 src->p_node[i].bh = NULL;
136 src->p_node[i].el = NULL;
141 * Insert an extent block at given index.
143 * This will not take an additional reference on eb_bh.
145 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
146 struct buffer_head *eb_bh)
148 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
151 * Right now, no root bh is an extent block, so this helps
152 * catch code errors with dinode trees. The assertion can be
153 * safely removed if we ever need to insert extent block
154 * structures at the root.
158 path->p_node[index].bh = eb_bh;
159 path->p_node[index].el = &eb->h_list;
162 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
163 struct ocfs2_extent_list *root_el)
165 struct ocfs2_path *path;
167 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
169 path = kzalloc(sizeof(*path), GFP_NOFS);
171 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
173 path_root_bh(path) = root_bh;
174 path_root_el(path) = root_el;
181 * Allocate and initialize a new path based on a disk inode tree.
183 static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh)
185 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
186 struct ocfs2_extent_list *el = &di->id2.i_list;
188 return ocfs2_new_path(di_bh, el);
192 * Convenience function to journal all components in a path.
194 static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
195 struct ocfs2_path *path)
202 for(i = 0; i < path_num_items(path); i++) {
203 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
204 OCFS2_JOURNAL_ACCESS_WRITE);
215 enum ocfs2_contig_type {
223 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
224 * ocfs2_extent_contig only work properly against leaf nodes!
226 static int ocfs2_block_extent_contig(struct super_block *sb,
227 struct ocfs2_extent_rec *ext,
230 u64 blk_end = le64_to_cpu(ext->e_blkno);
232 blk_end += ocfs2_clusters_to_blocks(sb,
233 le16_to_cpu(ext->e_leaf_clusters));
235 return blkno == blk_end;
238 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
239 struct ocfs2_extent_rec *right)
243 left_range = le32_to_cpu(left->e_cpos) +
244 le16_to_cpu(left->e_leaf_clusters);
246 return (left_range == le32_to_cpu(right->e_cpos));
249 static enum ocfs2_contig_type
250 ocfs2_extent_contig(struct inode *inode,
251 struct ocfs2_extent_rec *ext,
252 struct ocfs2_extent_rec *insert_rec)
254 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
256 if (ocfs2_extents_adjacent(ext, insert_rec) &&
257 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
260 blkno = le64_to_cpu(ext->e_blkno);
261 if (ocfs2_extents_adjacent(insert_rec, ext) &&
262 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
269 * NOTE: We can have pretty much any combination of contiguousness and
272 * The usefulness of APPEND_TAIL is more in that it lets us know that
273 * we'll have to update the path to that leaf.
275 enum ocfs2_append_type {
280 struct ocfs2_insert_type {
281 enum ocfs2_append_type ins_appending;
282 enum ocfs2_contig_type ins_contig;
283 int ins_contig_index;
284 int ins_free_records;
289 * How many free extents have we got before we need more meta data?
291 int ocfs2_num_free_extents(struct ocfs2_super *osb,
293 struct ocfs2_dinode *fe)
296 struct ocfs2_extent_list *el;
297 struct ocfs2_extent_block *eb;
298 struct buffer_head *eb_bh = NULL;
302 if (!OCFS2_IS_VALID_DINODE(fe)) {
303 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
308 if (fe->i_last_eb_blk) {
309 retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
310 &eb_bh, OCFS2_BH_CACHED, inode);
315 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
318 el = &fe->id2.i_list;
320 BUG_ON(el->l_tree_depth != 0);
322 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
331 /* expects array to already be allocated
333 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
336 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
340 struct ocfs2_alloc_context *meta_ac,
341 struct buffer_head *bhs[])
343 int count, status, i;
344 u16 suballoc_bit_start;
347 struct ocfs2_extent_block *eb;
352 while (count < wanted) {
353 status = ocfs2_claim_metadata(osb,
365 for(i = count; i < (num_got + count); i++) {
366 bhs[i] = sb_getblk(osb->sb, first_blkno);
367 if (bhs[i] == NULL) {
372 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
374 status = ocfs2_journal_access(handle, inode, bhs[i],
375 OCFS2_JOURNAL_ACCESS_CREATE);
381 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
382 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
383 /* Ok, setup the minimal stuff here. */
384 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
385 eb->h_blkno = cpu_to_le64(first_blkno);
386 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
388 #ifndef OCFS2_USE_ALL_METADATA_SUBALLOCATORS
389 /* we always use slot zero's suballocator */
390 eb->h_suballoc_slot = 0;
392 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
394 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
396 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
398 suballoc_bit_start++;
401 /* We'll also be dirtied by the caller, so
402 * this isn't absolutely necessary. */
403 status = ocfs2_journal_dirty(handle, bhs[i]);
416 for(i = 0; i < wanted; i++) {
427 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
429 * Returns the sum of the rightmost extent rec logical offset and
432 * ocfs2_add_branch() uses this to determine what logical cluster
433 * value should be populated into the leftmost new branch records.
435 * ocfs2_shift_tree_depth() uses this to determine the # clusters
436 * value for the new topmost tree record.
438 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
442 i = le16_to_cpu(el->l_next_free_rec) - 1;
444 return le32_to_cpu(el->l_recs[i].e_cpos) +
445 ocfs2_rec_clusters(el, &el->l_recs[i]);
449 * Add an entire tree branch to our inode. eb_bh is the extent block
450 * to start at, if we don't want to start the branch at the dinode
453 * last_eb_bh is required as we have to update it's next_leaf pointer
454 * for the new last extent block.
456 * the new branch will be 'empty' in the sense that every block will
457 * contain a single record with cluster count == 0.
459 static int ocfs2_add_branch(struct ocfs2_super *osb,
462 struct buffer_head *fe_bh,
463 struct buffer_head *eb_bh,
464 struct buffer_head *last_eb_bh,
465 struct ocfs2_alloc_context *meta_ac)
467 int status, new_blocks, i;
468 u64 next_blkno, new_last_eb_blk;
469 struct buffer_head *bh;
470 struct buffer_head **new_eb_bhs = NULL;
471 struct ocfs2_dinode *fe;
472 struct ocfs2_extent_block *eb;
473 struct ocfs2_extent_list *eb_el;
474 struct ocfs2_extent_list *el;
481 fe = (struct ocfs2_dinode *) fe_bh->b_data;
484 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
487 el = &fe->id2.i_list;
489 /* we never add a branch to a leaf. */
490 BUG_ON(!el->l_tree_depth);
492 new_blocks = le16_to_cpu(el->l_tree_depth);
494 /* allocate the number of new eb blocks we need */
495 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
503 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
504 meta_ac, new_eb_bhs);
510 eb = (struct ocfs2_extent_block *)last_eb_bh->b_data;
511 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
513 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
514 * linked with the rest of the tree.
515 * conversly, new_eb_bhs[0] is the new bottommost leaf.
517 * when we leave the loop, new_last_eb_blk will point to the
518 * newest leaf, and next_blkno will point to the topmost extent
520 next_blkno = new_last_eb_blk = 0;
521 for(i = 0; i < new_blocks; i++) {
523 eb = (struct ocfs2_extent_block *) bh->b_data;
524 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
525 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
531 status = ocfs2_journal_access(handle, inode, bh,
532 OCFS2_JOURNAL_ACCESS_CREATE);
538 eb->h_next_leaf_blk = 0;
539 eb_el->l_tree_depth = cpu_to_le16(i);
540 eb_el->l_next_free_rec = cpu_to_le16(1);
542 * This actually counts as an empty extent as
545 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
546 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
548 * eb_el isn't always an interior node, but even leaf
549 * nodes want a zero'd flags and reserved field so
550 * this gets the whole 32 bits regardless of use.
552 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
553 if (!eb_el->l_tree_depth)
554 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
556 status = ocfs2_journal_dirty(handle, bh);
562 next_blkno = le64_to_cpu(eb->h_blkno);
565 /* This is a bit hairy. We want to update up to three blocks
566 * here without leaving any of them in an inconsistent state
567 * in case of error. We don't have to worry about
568 * journal_dirty erroring as it won't unless we've aborted the
569 * handle (in which case we would never be here) so reserving
570 * the write with journal_access is all we need to do. */
571 status = ocfs2_journal_access(handle, inode, last_eb_bh,
572 OCFS2_JOURNAL_ACCESS_WRITE);
577 status = ocfs2_journal_access(handle, inode, fe_bh,
578 OCFS2_JOURNAL_ACCESS_WRITE);
584 status = ocfs2_journal_access(handle, inode, eb_bh,
585 OCFS2_JOURNAL_ACCESS_WRITE);
592 /* Link the new branch into the rest of the tree (el will
593 * either be on the fe, or the extent block passed in. */
594 i = le16_to_cpu(el->l_next_free_rec);
595 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
596 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
597 el->l_recs[i].e_int_clusters = 0;
598 le16_add_cpu(&el->l_next_free_rec, 1);
600 /* fe needs a new last extent block pointer, as does the
601 * next_leaf on the previously last-extent-block. */
602 fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk);
604 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
605 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
607 status = ocfs2_journal_dirty(handle, last_eb_bh);
610 status = ocfs2_journal_dirty(handle, fe_bh);
614 status = ocfs2_journal_dirty(handle, eb_bh);
622 for (i = 0; i < new_blocks; i++)
624 brelse(new_eb_bhs[i]);
633 * adds another level to the allocation tree.
634 * returns back the new extent block so you can add a branch to it
637 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
640 struct buffer_head *fe_bh,
641 struct ocfs2_alloc_context *meta_ac,
642 struct buffer_head **ret_new_eb_bh)
646 struct buffer_head *new_eb_bh = NULL;
647 struct ocfs2_dinode *fe;
648 struct ocfs2_extent_block *eb;
649 struct ocfs2_extent_list *fe_el;
650 struct ocfs2_extent_list *eb_el;
654 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
661 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
662 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
663 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
669 fe = (struct ocfs2_dinode *) fe_bh->b_data;
670 fe_el = &fe->id2.i_list;
672 status = ocfs2_journal_access(handle, inode, new_eb_bh,
673 OCFS2_JOURNAL_ACCESS_CREATE);
679 /* copy the fe data into the new extent block */
680 eb_el->l_tree_depth = fe_el->l_tree_depth;
681 eb_el->l_next_free_rec = fe_el->l_next_free_rec;
682 for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
683 eb_el->l_recs[i] = fe_el->l_recs[i];
685 status = ocfs2_journal_dirty(handle, new_eb_bh);
691 status = ocfs2_journal_access(handle, inode, fe_bh,
692 OCFS2_JOURNAL_ACCESS_WRITE);
698 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
701 le16_add_cpu(&fe_el->l_tree_depth, 1);
702 fe_el->l_recs[0].e_cpos = 0;
703 fe_el->l_recs[0].e_blkno = eb->h_blkno;
704 fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
705 for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
706 memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
707 fe_el->l_next_free_rec = cpu_to_le16(1);
709 /* If this is our 1st tree depth shift, then last_eb_blk
710 * becomes the allocated extent block */
711 if (fe_el->l_tree_depth == cpu_to_le16(1))
712 fe->i_last_eb_blk = eb->h_blkno;
714 status = ocfs2_journal_dirty(handle, fe_bh);
720 *ret_new_eb_bh = new_eb_bh;
732 * Should only be called when there is no space left in any of the
733 * leaf nodes. What we want to do is find the lowest tree depth
734 * non-leaf extent block with room for new records. There are three
735 * valid results of this search:
737 * 1) a lowest extent block is found, then we pass it back in
738 * *lowest_eb_bh and return '0'
740 * 2) the search fails to find anything, but the dinode has room. We
741 * pass NULL back in *lowest_eb_bh, but still return '0'
743 * 3) the search fails to find anything AND the dinode is full, in
744 * which case we return > 0
746 * return status < 0 indicates an error.
748 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
750 struct buffer_head *fe_bh,
751 struct buffer_head **target_bh)
755 struct ocfs2_dinode *fe;
756 struct ocfs2_extent_block *eb;
757 struct ocfs2_extent_list *el;
758 struct buffer_head *bh = NULL;
759 struct buffer_head *lowest_bh = NULL;
765 fe = (struct ocfs2_dinode *) fe_bh->b_data;
766 el = &fe->id2.i_list;
768 while(le16_to_cpu(el->l_tree_depth) > 1) {
769 if (le16_to_cpu(el->l_next_free_rec) == 0) {
770 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
771 "extent list (next_free_rec == 0)",
772 (unsigned long long)OCFS2_I(inode)->ip_blkno);
776 i = le16_to_cpu(el->l_next_free_rec) - 1;
777 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
779 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
780 "list where extent # %d has no physical "
782 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
792 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
799 eb = (struct ocfs2_extent_block *) bh->b_data;
800 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
801 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
807 if (le16_to_cpu(el->l_next_free_rec) <
808 le16_to_cpu(el->l_count)) {
816 /* If we didn't find one and the fe doesn't have any room,
819 && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count))
822 *target_bh = lowest_bh;
832 * This is only valid for leaf nodes, which are the only ones that can
833 * have empty extents anyway.
835 static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
837 return !rec->e_leaf_clusters;
841 * This function will discard the rightmost extent record.
843 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
845 int next_free = le16_to_cpu(el->l_next_free_rec);
846 int count = le16_to_cpu(el->l_count);
847 unsigned int num_bytes;
850 /* This will cause us to go off the end of our extent list. */
851 BUG_ON(next_free >= count);
853 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
855 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
858 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
859 struct ocfs2_extent_rec *insert_rec)
861 int i, insert_index, next_free, has_empty, num_bytes;
862 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
863 struct ocfs2_extent_rec *rec;
865 next_free = le16_to_cpu(el->l_next_free_rec);
866 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
870 /* The tree code before us didn't allow enough room in the leaf. */
871 if (el->l_next_free_rec == el->l_count && !has_empty)
875 * The easiest way to approach this is to just remove the
876 * empty extent and temporarily decrement next_free.
880 * If next_free was 1 (only an empty extent), this
881 * loop won't execute, which is fine. We still want
882 * the decrement above to happen.
884 for(i = 0; i < (next_free - 1); i++)
885 el->l_recs[i] = el->l_recs[i+1];
891 * Figure out what the new record index should be.
893 for(i = 0; i < next_free; i++) {
894 rec = &el->l_recs[i];
896 if (insert_cpos < le32_to_cpu(rec->e_cpos))
901 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
902 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
904 BUG_ON(insert_index < 0);
905 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
906 BUG_ON(insert_index > next_free);
909 * No need to memmove if we're just adding to the tail.
911 if (insert_index != next_free) {
912 BUG_ON(next_free >= le16_to_cpu(el->l_count));
914 num_bytes = next_free - insert_index;
915 num_bytes *= sizeof(struct ocfs2_extent_rec);
916 memmove(&el->l_recs[insert_index + 1],
917 &el->l_recs[insert_index],
922 * Either we had an empty extent, and need to re-increment or
923 * there was no empty extent on a non full rightmost leaf node,
924 * in which case we still need to increment.
927 el->l_next_free_rec = cpu_to_le16(next_free);
929 * Make sure none of the math above just messed up our tree.
931 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
933 el->l_recs[insert_index] = *insert_rec;
938 * Create an empty extent record .
940 * l_next_free_rec may be updated.
942 * If an empty extent already exists do nothing.
944 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
946 int next_free = le16_to_cpu(el->l_next_free_rec);
948 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
953 if (ocfs2_is_empty_extent(&el->l_recs[0]))
956 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
957 "Asked to create an empty extent in a full list:\n"
958 "count = %u, tree depth = %u",
959 le16_to_cpu(el->l_count),
960 le16_to_cpu(el->l_tree_depth));
962 ocfs2_shift_records_right(el);
965 le16_add_cpu(&el->l_next_free_rec, 1);
966 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
970 * For a rotation which involves two leaf nodes, the "root node" is
971 * the lowest level tree node which contains a path to both leafs. This
972 * resulting set of information can be used to form a complete "subtree"
974 * This function is passed two full paths from the dinode down to a
975 * pair of adjacent leaves. It's task is to figure out which path
976 * index contains the subtree root - this can be the root index itself
977 * in a worst-case rotation.
979 * The array index of the subtree root is passed back.
981 static int ocfs2_find_subtree_root(struct inode *inode,
982 struct ocfs2_path *left,
983 struct ocfs2_path *right)
988 * Check that the caller passed in two paths from the same tree.
990 BUG_ON(path_root_bh(left) != path_root_bh(right));
996 * The caller didn't pass two adjacent paths.
998 mlog_bug_on_msg(i > left->p_tree_depth,
999 "Inode %lu, left depth %u, right depth %u\n"
1000 "left leaf blk %llu, right leaf blk %llu\n",
1001 inode->i_ino, left->p_tree_depth,
1002 right->p_tree_depth,
1003 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1004 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1005 } while (left->p_node[i].bh->b_blocknr ==
1006 right->p_node[i].bh->b_blocknr);
1011 typedef void (path_insert_t)(void *, struct buffer_head *);
1014 * Traverse a btree path in search of cpos, starting at root_el.
1016 * This code can be called with a cpos larger than the tree, in which
1017 * case it will return the rightmost path.
1019 static int __ocfs2_find_path(struct inode *inode,
1020 struct ocfs2_extent_list *root_el, u32 cpos,
1021 path_insert_t *func, void *data)
1026 struct buffer_head *bh = NULL;
1027 struct ocfs2_extent_block *eb;
1028 struct ocfs2_extent_list *el;
1029 struct ocfs2_extent_rec *rec;
1030 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1033 while (el->l_tree_depth) {
1034 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1035 ocfs2_error(inode->i_sb,
1036 "Inode %llu has empty extent list at "
1038 (unsigned long long)oi->ip_blkno,
1039 le16_to_cpu(el->l_tree_depth));
1045 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1046 rec = &el->l_recs[i];
1049 * In the case that cpos is off the allocation
1050 * tree, this should just wind up returning the
1053 range = le32_to_cpu(rec->e_cpos) +
1054 ocfs2_rec_clusters(el, rec);
1055 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1059 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1061 ocfs2_error(inode->i_sb,
1062 "Inode %llu has bad blkno in extent list "
1063 "at depth %u (index %d)\n",
1064 (unsigned long long)oi->ip_blkno,
1065 le16_to_cpu(el->l_tree_depth), i);
1072 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1073 &bh, OCFS2_BH_CACHED, inode);
1079 eb = (struct ocfs2_extent_block *) bh->b_data;
1081 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1082 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1087 if (le16_to_cpu(el->l_next_free_rec) >
1088 le16_to_cpu(el->l_count)) {
1089 ocfs2_error(inode->i_sb,
1090 "Inode %llu has bad count in extent list "
1091 "at block %llu (next free=%u, count=%u)\n",
1092 (unsigned long long)oi->ip_blkno,
1093 (unsigned long long)bh->b_blocknr,
1094 le16_to_cpu(el->l_next_free_rec),
1095 le16_to_cpu(el->l_count));
1106 * Catch any trailing bh that the loop didn't handle.
1114 * Given an initialized path (that is, it has a valid root extent
1115 * list), this function will traverse the btree in search of the path
1116 * which would contain cpos.
1118 * The path traveled is recorded in the path structure.
1120 * Note that this will not do any comparisons on leaf node extent
1121 * records, so it will work fine in the case that we just added a tree
1124 struct find_path_data {
1126 struct ocfs2_path *path;
1128 static void find_path_ins(void *data, struct buffer_head *bh)
1130 struct find_path_data *fp = data;
1133 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1136 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1139 struct find_path_data data;
1143 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1144 find_path_ins, &data);
1147 static void find_leaf_ins(void *data, struct buffer_head *bh)
1149 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1150 struct ocfs2_extent_list *el = &eb->h_list;
1151 struct buffer_head **ret = data;
1153 /* We want to retain only the leaf block. */
1154 if (le16_to_cpu(el->l_tree_depth) == 0) {
1160 * Find the leaf block in the tree which would contain cpos. No
1161 * checking of the actual leaf is done.
1163 * Some paths want to call this instead of allocating a path structure
1164 * and calling ocfs2_find_path().
1166 * This function doesn't handle non btree extent lists.
1168 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1169 u32 cpos, struct buffer_head **leaf_bh)
1172 struct buffer_head *bh = NULL;
1174 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1186 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1188 * Basically, we've moved stuff around at the bottom of the tree and
1189 * we need to fix up the extent records above the changes to reflect
1192 * left_rec: the record on the left.
1193 * left_child_el: is the child list pointed to by left_rec
1194 * right_rec: the record to the right of left_rec
1195 * right_child_el: is the child list pointed to by right_rec
1197 * By definition, this only works on interior nodes.
1199 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1200 struct ocfs2_extent_list *left_child_el,
1201 struct ocfs2_extent_rec *right_rec,
1202 struct ocfs2_extent_list *right_child_el)
1204 u32 left_clusters, right_end;
1207 * Interior nodes never have holes. Their cpos is the cpos of
1208 * the leftmost record in their child list. Their cluster
1209 * count covers the full theoretical range of their child list
1210 * - the range between their cpos and the cpos of the record
1211 * immediately to their right.
1213 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1214 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1215 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1218 * Calculate the rightmost cluster count boundary before
1219 * moving cpos - we will need to adjust clusters after
1220 * updating e_cpos to keep the same highest cluster count.
1222 right_end = le32_to_cpu(right_rec->e_cpos);
1223 right_end += le32_to_cpu(right_rec->e_int_clusters);
1225 right_rec->e_cpos = left_rec->e_cpos;
1226 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1228 right_end -= le32_to_cpu(right_rec->e_cpos);
1229 right_rec->e_int_clusters = cpu_to_le32(right_end);
1233 * Adjust the adjacent root node records involved in a
1234 * rotation. left_el_blkno is passed in as a key so that we can easily
1235 * find it's index in the root list.
1237 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1238 struct ocfs2_extent_list *left_el,
1239 struct ocfs2_extent_list *right_el,
1244 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1245 le16_to_cpu(left_el->l_tree_depth));
1247 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1248 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1253 * The path walking code should have never returned a root and
1254 * two paths which are not adjacent.
1256 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1258 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1259 &root_el->l_recs[i + 1], right_el);
1263 * We've changed a leaf block (in right_path) and need to reflect that
1264 * change back up the subtree.
1266 * This happens in multiple places:
1267 * - When we've moved an extent record from the left path leaf to the right
1268 * path leaf to make room for an empty extent in the left path leaf.
1269 * - When our insert into the right path leaf is at the leftmost edge
1270 * and requires an update of the path immediately to it's left. This
1271 * can occur at the end of some types of rotation and appending inserts.
1273 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1274 struct ocfs2_path *left_path,
1275 struct ocfs2_path *right_path,
1279 struct ocfs2_extent_list *el, *left_el, *right_el;
1280 struct ocfs2_extent_rec *left_rec, *right_rec;
1281 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1284 * Update the counts and position values within all the
1285 * interior nodes to reflect the leaf rotation we just did.
1287 * The root node is handled below the loop.
1289 * We begin the loop with right_el and left_el pointing to the
1290 * leaf lists and work our way up.
1292 * NOTE: within this loop, left_el and right_el always refer
1293 * to the *child* lists.
1295 left_el = path_leaf_el(left_path);
1296 right_el = path_leaf_el(right_path);
1297 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1298 mlog(0, "Adjust records at index %u\n", i);
1301 * One nice property of knowing that all of these
1302 * nodes are below the root is that we only deal with
1303 * the leftmost right node record and the rightmost
1306 el = left_path->p_node[i].el;
1307 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1308 left_rec = &el->l_recs[idx];
1310 el = right_path->p_node[i].el;
1311 right_rec = &el->l_recs[0];
1313 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1316 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1320 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1325 * Setup our list pointers now so that the current
1326 * parents become children in the next iteration.
1328 left_el = left_path->p_node[i].el;
1329 right_el = right_path->p_node[i].el;
1333 * At the root node, adjust the two adjacent records which
1334 * begin our path to the leaves.
1337 el = left_path->p_node[subtree_index].el;
1338 left_el = left_path->p_node[subtree_index + 1].el;
1339 right_el = right_path->p_node[subtree_index + 1].el;
1341 ocfs2_adjust_root_records(el, left_el, right_el,
1342 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1344 root_bh = left_path->p_node[subtree_index].bh;
1346 ret = ocfs2_journal_dirty(handle, root_bh);
1351 static int ocfs2_rotate_subtree_right(struct inode *inode,
1353 struct ocfs2_path *left_path,
1354 struct ocfs2_path *right_path,
1358 struct buffer_head *right_leaf_bh;
1359 struct buffer_head *left_leaf_bh = NULL;
1360 struct buffer_head *root_bh;
1361 struct ocfs2_extent_list *right_el, *left_el;
1362 struct ocfs2_extent_rec move_rec;
1364 left_leaf_bh = path_leaf_bh(left_path);
1365 left_el = path_leaf_el(left_path);
1367 if (left_el->l_next_free_rec != left_el->l_count) {
1368 ocfs2_error(inode->i_sb,
1369 "Inode %llu has non-full interior leaf node %llu"
1371 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1372 (unsigned long long)left_leaf_bh->b_blocknr,
1373 le16_to_cpu(left_el->l_next_free_rec));
1378 * This extent block may already have an empty record, so we
1379 * return early if so.
1381 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1384 root_bh = left_path->p_node[subtree_index].bh;
1385 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1387 ret = ocfs2_journal_access(handle, inode, root_bh,
1388 OCFS2_JOURNAL_ACCESS_WRITE);
1394 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1395 ret = ocfs2_journal_access(handle, inode,
1396 right_path->p_node[i].bh,
1397 OCFS2_JOURNAL_ACCESS_WRITE);
1403 ret = ocfs2_journal_access(handle, inode,
1404 left_path->p_node[i].bh,
1405 OCFS2_JOURNAL_ACCESS_WRITE);
1412 right_leaf_bh = path_leaf_bh(right_path);
1413 right_el = path_leaf_el(right_path);
1415 /* This is a code error, not a disk corruption. */
1416 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1417 "because rightmost leaf block %llu is empty\n",
1418 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1419 (unsigned long long)right_leaf_bh->b_blocknr);
1421 ocfs2_create_empty_extent(right_el);
1423 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1429 /* Do the copy now. */
1430 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1431 move_rec = left_el->l_recs[i];
1432 right_el->l_recs[0] = move_rec;
1435 * Clear out the record we just copied and shift everything
1436 * over, leaving an empty extent in the left leaf.
1438 * We temporarily subtract from next_free_rec so that the
1439 * shift will lose the tail record (which is now defunct).
1441 le16_add_cpu(&left_el->l_next_free_rec, -1);
1442 ocfs2_shift_records_right(left_el);
1443 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1444 le16_add_cpu(&left_el->l_next_free_rec, 1);
1446 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1452 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1460 * Given a full path, determine what cpos value would return us a path
1461 * containing the leaf immediately to the left of the current one.
1463 * Will return zero if the path passed in is already the leftmost path.
1465 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1466 struct ocfs2_path *path, u32 *cpos)
1470 struct ocfs2_extent_list *el;
1472 BUG_ON(path->p_tree_depth == 0);
1476 blkno = path_leaf_bh(path)->b_blocknr;
1478 /* Start at the tree node just above the leaf and work our way up. */
1479 i = path->p_tree_depth - 1;
1481 el = path->p_node[i].el;
1484 * Find the extent record just before the one in our
1487 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1488 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1492 * We've determined that the
1493 * path specified is already
1494 * the leftmost one - return a
1500 * The leftmost record points to our
1501 * leaf - we need to travel up the
1507 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
1508 *cpos = *cpos + ocfs2_rec_clusters(el,
1509 &el->l_recs[j - 1]);
1516 * If we got here, we never found a valid node where
1517 * the tree indicated one should be.
1520 "Invalid extent tree at extent block %llu\n",
1521 (unsigned long long)blkno);
1526 blkno = path->p_node[i].bh->b_blocknr;
1534 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
1535 struct ocfs2_path *path)
1537 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1;
1539 if (handle->h_buffer_credits < credits)
1540 return ocfs2_extend_trans(handle, credits);
1546 * Trap the case where we're inserting into the theoretical range past
1547 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1548 * whose cpos is less than ours into the right leaf.
1550 * It's only necessary to look at the rightmost record of the left
1551 * leaf because the logic that calls us should ensure that the
1552 * theoretical ranges in the path components above the leaves are
1555 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1558 struct ocfs2_extent_list *left_el;
1559 struct ocfs2_extent_rec *rec;
1562 left_el = path_leaf_el(left_path);
1563 next_free = le16_to_cpu(left_el->l_next_free_rec);
1564 rec = &left_el->l_recs[next_free - 1];
1566 if (insert_cpos > le32_to_cpu(rec->e_cpos))
1572 * Rotate all the records in a btree right one record, starting at insert_cpos.
1574 * The path to the rightmost leaf should be passed in.
1576 * The array is assumed to be large enough to hold an entire path (tree depth).
1578 * Upon succesful return from this function:
1580 * - The 'right_path' array will contain a path to the leaf block
1581 * whose range contains e_cpos.
1582 * - That leaf block will have a single empty extent in list index 0.
1583 * - In the case that the rotation requires a post-insert update,
1584 * *ret_left_path will contain a valid path which can be passed to
1585 * ocfs2_insert_path().
1587 static int ocfs2_rotate_tree_right(struct inode *inode,
1590 struct ocfs2_path *right_path,
1591 struct ocfs2_path **ret_left_path)
1595 struct ocfs2_path *left_path = NULL;
1597 *ret_left_path = NULL;
1599 left_path = ocfs2_new_path(path_root_bh(right_path),
1600 path_root_el(right_path));
1607 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
1613 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
1616 * What we want to do here is:
1618 * 1) Start with the rightmost path.
1620 * 2) Determine a path to the leaf block directly to the left
1623 * 3) Determine the 'subtree root' - the lowest level tree node
1624 * which contains a path to both leaves.
1626 * 4) Rotate the subtree.
1628 * 5) Find the next subtree by considering the left path to be
1629 * the new right path.
1631 * The check at the top of this while loop also accepts
1632 * insert_cpos == cpos because cpos is only a _theoretical_
1633 * value to get us the left path - insert_cpos might very well
1634 * be filling that hole.
1636 * Stop at a cpos of '0' because we either started at the
1637 * leftmost branch (i.e., a tree with one branch and a
1638 * rotation inside of it), or we've gone as far as we can in
1639 * rotating subtrees.
1641 while (cpos && insert_cpos <= cpos) {
1642 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
1645 ret = ocfs2_find_path(inode, left_path, cpos);
1651 mlog_bug_on_msg(path_leaf_bh(left_path) ==
1652 path_leaf_bh(right_path),
1653 "Inode %lu: error during insert of %u "
1654 "(left path cpos %u) results in two identical "
1655 "paths ending at %llu\n",
1656 inode->i_ino, insert_cpos, cpos,
1657 (unsigned long long)
1658 path_leaf_bh(left_path)->b_blocknr);
1660 if (ocfs2_rotate_requires_path_adjustment(left_path,
1662 mlog(0, "Path adjustment required\n");
1665 * We've rotated the tree as much as we
1666 * should. The rest is up to
1667 * ocfs2_insert_path() to complete, after the
1668 * record insertion. We indicate this
1669 * situation by returning the left path.
1671 * The reason we don't adjust the records here
1672 * before the record insert is that an error
1673 * later might break the rule where a parent
1674 * record e_cpos will reflect the actual
1675 * e_cpos of the 1st nonempty record of the
1678 *ret_left_path = left_path;
1682 start = ocfs2_find_subtree_root(inode, left_path, right_path);
1684 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
1686 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
1687 right_path->p_tree_depth);
1689 ret = ocfs2_extend_rotate_transaction(handle, start,
1696 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
1704 * There is no need to re-read the next right path
1705 * as we know that it'll be our current left
1706 * path. Optimize by copying values instead.
1708 ocfs2_mv_path(right_path, left_path);
1710 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
1719 ocfs2_free_path(left_path);
1726 * Do the final bits of extent record insertion at the target leaf
1727 * list. If this leaf is part of an allocation tree, it is assumed
1728 * that the tree above has been prepared.
1730 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
1731 struct ocfs2_extent_list *el,
1732 struct ocfs2_insert_type *insert,
1733 struct inode *inode)
1735 int i = insert->ins_contig_index;
1737 struct ocfs2_extent_rec *rec;
1739 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1742 * Contiguous insert - either left or right.
1744 if (insert->ins_contig != CONTIG_NONE) {
1745 rec = &el->l_recs[i];
1746 if (insert->ins_contig == CONTIG_LEFT) {
1747 rec->e_blkno = insert_rec->e_blkno;
1748 rec->e_cpos = insert_rec->e_cpos;
1750 le16_add_cpu(&rec->e_leaf_clusters,
1751 le16_to_cpu(insert_rec->e_leaf_clusters));
1756 * Handle insert into an empty leaf.
1758 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
1759 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
1760 ocfs2_is_empty_extent(&el->l_recs[0]))) {
1761 el->l_recs[0] = *insert_rec;
1762 el->l_next_free_rec = cpu_to_le16(1);
1769 if (insert->ins_appending == APPEND_TAIL) {
1770 i = le16_to_cpu(el->l_next_free_rec) - 1;
1771 rec = &el->l_recs[i];
1772 range = le32_to_cpu(rec->e_cpos)
1773 + le16_to_cpu(rec->e_leaf_clusters);
1774 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
1776 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
1777 le16_to_cpu(el->l_count),
1778 "inode %lu, depth %u, count %u, next free %u, "
1779 "rec.cpos %u, rec.clusters %u, "
1780 "insert.cpos %u, insert.clusters %u\n",
1782 le16_to_cpu(el->l_tree_depth),
1783 le16_to_cpu(el->l_count),
1784 le16_to_cpu(el->l_next_free_rec),
1785 le32_to_cpu(el->l_recs[i].e_cpos),
1786 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
1787 le32_to_cpu(insert_rec->e_cpos),
1788 le16_to_cpu(insert_rec->e_leaf_clusters));
1790 el->l_recs[i] = *insert_rec;
1791 le16_add_cpu(&el->l_next_free_rec, 1);
1796 * Ok, we have to rotate.
1798 * At this point, it is safe to assume that inserting into an
1799 * empty leaf and appending to a leaf have both been handled
1802 * This leaf needs to have space, either by the empty 1st
1803 * extent record, or by virtue of an l_next_rec < l_count.
1805 ocfs2_rotate_leaf(el, insert_rec);
1808 static inline void ocfs2_update_dinode_clusters(struct inode *inode,
1809 struct ocfs2_dinode *di,
1812 le32_add_cpu(&di->i_clusters, clusters);
1813 spin_lock(&OCFS2_I(inode)->ip_lock);
1814 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
1815 spin_unlock(&OCFS2_I(inode)->ip_lock);
1818 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
1819 struct ocfs2_extent_rec *insert_rec,
1820 struct ocfs2_path *right_path,
1821 struct ocfs2_path **ret_left_path)
1823 int ret, i, next_free;
1824 struct buffer_head *bh;
1825 struct ocfs2_extent_list *el;
1826 struct ocfs2_path *left_path = NULL;
1828 *ret_left_path = NULL;
1831 * This shouldn't happen for non-trees. The extent rec cluster
1832 * count manipulation below only works for interior nodes.
1834 BUG_ON(right_path->p_tree_depth == 0);
1837 * If our appending insert is at the leftmost edge of a leaf,
1838 * then we might need to update the rightmost records of the
1841 el = path_leaf_el(right_path);
1842 next_free = le16_to_cpu(el->l_next_free_rec);
1843 if (next_free == 0 ||
1844 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
1847 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
1854 mlog(0, "Append may need a left path update. cpos: %u, "
1855 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
1859 * No need to worry if the append is already in the
1863 left_path = ocfs2_new_path(path_root_bh(right_path),
1864 path_root_el(right_path));
1871 ret = ocfs2_find_path(inode, left_path, left_cpos);
1878 * ocfs2_insert_path() will pass the left_path to the
1884 ret = ocfs2_journal_access_path(inode, handle, right_path);
1890 el = path_root_el(right_path);
1891 bh = path_root_bh(right_path);
1894 struct ocfs2_extent_rec *rec;
1896 next_free = le16_to_cpu(el->l_next_free_rec);
1897 if (next_free == 0) {
1898 ocfs2_error(inode->i_sb,
1899 "Dinode %llu has a bad extent list",
1900 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1905 rec = &el->l_recs[next_free - 1];
1907 rec->e_int_clusters = insert_rec->e_cpos;
1908 le32_add_cpu(&rec->e_int_clusters,
1909 le16_to_cpu(insert_rec->e_leaf_clusters));
1910 le32_add_cpu(&rec->e_int_clusters,
1911 -le32_to_cpu(rec->e_cpos));
1913 ret = ocfs2_journal_dirty(handle, bh);
1917 /* Don't touch the leaf node */
1918 if (++i >= right_path->p_tree_depth)
1921 bh = right_path->p_node[i].bh;
1922 el = right_path->p_node[i].el;
1925 *ret_left_path = left_path;
1929 ocfs2_free_path(left_path);
1935 * This function only does inserts on an allocation b-tree. For dinode
1936 * lists, ocfs2_insert_at_leaf() is called directly.
1938 * right_path is the path we want to do the actual insert
1939 * in. left_path should only be passed in if we need to update that
1940 * portion of the tree after an edge insert.
1942 static int ocfs2_insert_path(struct inode *inode,
1944 struct ocfs2_path *left_path,
1945 struct ocfs2_path *right_path,
1946 struct ocfs2_extent_rec *insert_rec,
1947 struct ocfs2_insert_type *insert)
1949 int ret, subtree_index;
1950 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
1951 struct ocfs2_extent_list *el;
1954 * Pass both paths to the journal. The majority of inserts
1955 * will be touching all components anyway.
1957 ret = ocfs2_journal_access_path(inode, handle, right_path);
1964 int credits = handle->h_buffer_credits;
1967 * There's a chance that left_path got passed back to
1968 * us without being accounted for in the
1969 * journal. Extend our transaction here to be sure we
1970 * can change those blocks.
1972 credits += left_path->p_tree_depth;
1974 ret = ocfs2_extend_trans(handle, credits);
1980 ret = ocfs2_journal_access_path(inode, handle, left_path);
1987 el = path_leaf_el(right_path);
1989 ocfs2_insert_at_leaf(insert_rec, el, insert, inode);
1990 ret = ocfs2_journal_dirty(handle, leaf_bh);
1996 * The rotate code has indicated that we need to fix
1997 * up portions of the tree after the insert.
1999 * XXX: Should we extend the transaction here?
2001 subtree_index = ocfs2_find_subtree_root(inode, left_path,
2003 ocfs2_complete_edge_insert(inode, handle, left_path,
2004 right_path, subtree_index);
2012 static int ocfs2_do_insert_extent(struct inode *inode,
2014 struct buffer_head *di_bh,
2015 struct ocfs2_extent_rec *insert_rec,
2016 struct ocfs2_insert_type *type)
2018 int ret, rotate = 0;
2020 struct ocfs2_path *right_path = NULL;
2021 struct ocfs2_path *left_path = NULL;
2022 struct ocfs2_dinode *di;
2023 struct ocfs2_extent_list *el;
2025 di = (struct ocfs2_dinode *) di_bh->b_data;
2026 el = &di->id2.i_list;
2028 ret = ocfs2_journal_access(handle, inode, di_bh,
2029 OCFS2_JOURNAL_ACCESS_WRITE);
2035 if (le16_to_cpu(el->l_tree_depth) == 0) {
2036 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
2037 goto out_update_clusters;
2040 right_path = ocfs2_new_inode_path(di_bh);
2048 * Determine the path to start with. Rotations need the
2049 * rightmost path, everything else can go directly to the
2052 cpos = le32_to_cpu(insert_rec->e_cpos);
2053 if (type->ins_appending == APPEND_NONE &&
2054 type->ins_contig == CONTIG_NONE) {
2059 ret = ocfs2_find_path(inode, right_path, cpos);
2066 * Rotations and appends need special treatment - they modify
2067 * parts of the tree's above them.
2069 * Both might pass back a path immediate to the left of the
2070 * one being inserted to. This will be cause
2071 * ocfs2_insert_path() to modify the rightmost records of
2072 * left_path to account for an edge insert.
2074 * XXX: When modifying this code, keep in mind that an insert
2075 * can wind up skipping both of these two special cases...
2078 ret = ocfs2_rotate_tree_right(inode, handle,
2079 le32_to_cpu(insert_rec->e_cpos),
2080 right_path, &left_path);
2085 } else if (type->ins_appending == APPEND_TAIL
2086 && type->ins_contig != CONTIG_LEFT) {
2087 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
2088 right_path, &left_path);
2095 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
2102 out_update_clusters:
2103 ocfs2_update_dinode_clusters(inode, di,
2104 le16_to_cpu(insert_rec->e_leaf_clusters));
2106 ret = ocfs2_journal_dirty(handle, di_bh);
2111 ocfs2_free_path(left_path);
2112 ocfs2_free_path(right_path);
2117 static void ocfs2_figure_contig_type(struct inode *inode,
2118 struct ocfs2_insert_type *insert,
2119 struct ocfs2_extent_list *el,
2120 struct ocfs2_extent_rec *insert_rec)
2123 enum ocfs2_contig_type contig_type = CONTIG_NONE;
2125 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
2127 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
2128 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
2130 if (contig_type != CONTIG_NONE) {
2131 insert->ins_contig_index = i;
2135 insert->ins_contig = contig_type;
2139 * This should only be called against the righmost leaf extent list.
2141 * ocfs2_figure_appending_type() will figure out whether we'll have to
2142 * insert at the tail of the rightmost leaf.
2144 * This should also work against the dinode list for tree's with 0
2145 * depth. If we consider the dinode list to be the rightmost leaf node
2146 * then the logic here makes sense.
2148 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
2149 struct ocfs2_extent_list *el,
2150 struct ocfs2_extent_rec *insert_rec)
2153 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
2154 struct ocfs2_extent_rec *rec;
2156 insert->ins_appending = APPEND_NONE;
2158 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
2160 if (!el->l_next_free_rec)
2161 goto set_tail_append;
2163 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
2164 /* Were all records empty? */
2165 if (le16_to_cpu(el->l_next_free_rec) == 1)
2166 goto set_tail_append;
2169 i = le16_to_cpu(el->l_next_free_rec) - 1;
2170 rec = &el->l_recs[i];
2173 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
2174 goto set_tail_append;
2179 insert->ins_appending = APPEND_TAIL;
2183 * Helper function called at the begining of an insert.
2185 * This computes a few things that are commonly used in the process of
2186 * inserting into the btree:
2187 * - Whether the new extent is contiguous with an existing one.
2188 * - The current tree depth.
2189 * - Whether the insert is an appending one.
2190 * - The total # of free records in the tree.
2192 * All of the information is stored on the ocfs2_insert_type
2195 static int ocfs2_figure_insert_type(struct inode *inode,
2196 struct buffer_head *di_bh,
2197 struct buffer_head **last_eb_bh,
2198 struct ocfs2_extent_rec *insert_rec,
2199 struct ocfs2_insert_type *insert)
2202 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2203 struct ocfs2_extent_block *eb;
2204 struct ocfs2_extent_list *el;
2205 struct ocfs2_path *path = NULL;
2206 struct buffer_head *bh = NULL;
2208 el = &di->id2.i_list;
2209 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
2211 if (el->l_tree_depth) {
2213 * If we have tree depth, we read in the
2214 * rightmost extent block ahead of time as
2215 * ocfs2_figure_insert_type() and ocfs2_add_branch()
2216 * may want it later.
2218 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2219 le64_to_cpu(di->i_last_eb_blk), &bh,
2220 OCFS2_BH_CACHED, inode);
2225 eb = (struct ocfs2_extent_block *) bh->b_data;
2230 * Unless we have a contiguous insert, we'll need to know if
2231 * there is room left in our allocation tree for another
2234 * XXX: This test is simplistic, we can search for empty
2235 * extent records too.
2237 insert->ins_free_records = le16_to_cpu(el->l_count) -
2238 le16_to_cpu(el->l_next_free_rec);
2240 if (!insert->ins_tree_depth) {
2241 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
2242 ocfs2_figure_appending_type(insert, el, insert_rec);
2246 path = ocfs2_new_inode_path(di_bh);
2254 * In the case that we're inserting past what the tree
2255 * currently accounts for, ocfs2_find_path() will return for
2256 * us the rightmost tree path. This is accounted for below in
2257 * the appending code.
2259 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
2265 el = path_leaf_el(path);
2268 * Now that we have the path, there's two things we want to determine:
2269 * 1) Contiguousness (also set contig_index if this is so)
2271 * 2) Are we doing an append? We can trivially break this up
2272 * into two types of appends: simple record append, or a
2273 * rotate inside the tail leaf.
2275 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
2278 * The insert code isn't quite ready to deal with all cases of
2279 * left contiguousness. Specifically, if it's an insert into
2280 * the 1st record in a leaf, it will require the adjustment of
2281 * cluster count on the last record of the path directly to it's
2282 * left. For now, just catch that case and fool the layers
2283 * above us. This works just fine for tree_depth == 0, which
2284 * is why we allow that above.
2286 if (insert->ins_contig == CONTIG_LEFT &&
2287 insert->ins_contig_index == 0)
2288 insert->ins_contig = CONTIG_NONE;
2291 * Ok, so we can simply compare against last_eb to figure out
2292 * whether the path doesn't exist. This will only happen in
2293 * the case that we're doing a tail append, so maybe we can
2294 * take advantage of that information somehow.
2296 if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) {
2298 * Ok, ocfs2_find_path() returned us the rightmost
2299 * tree path. This might be an appending insert. There are
2301 * 1) We're doing a true append at the tail:
2302 * -This might even be off the end of the leaf
2303 * 2) We're "appending" by rotating in the tail
2305 ocfs2_figure_appending_type(insert, el, insert_rec);
2309 ocfs2_free_path(path);
2319 * Insert an extent into an inode btree.
2321 * The caller needs to update fe->i_clusters
2323 int ocfs2_insert_extent(struct ocfs2_super *osb,
2325 struct inode *inode,
2326 struct buffer_head *fe_bh,
2330 struct ocfs2_alloc_context *meta_ac)
2333 struct buffer_head *last_eb_bh = NULL;
2334 struct buffer_head *bh = NULL;
2335 struct ocfs2_insert_type insert = {0, };
2336 struct ocfs2_extent_rec rec;
2338 mlog(0, "add %u clusters at position %u to inode %llu\n",
2339 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
2341 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
2342 (OCFS2_I(inode)->ip_clusters != cpos),
2343 "Device %s, asking for sparse allocation: inode %llu, "
2344 "cpos %u, clusters %u\n",
2346 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
2347 OCFS2_I(inode)->ip_clusters);
2349 memset(&rec, 0, sizeof(rec));
2350 rec.e_cpos = cpu_to_le32(cpos);
2351 rec.e_blkno = cpu_to_le64(start_blk);
2352 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
2354 status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec,
2361 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
2362 "Insert.contig_index: %d, Insert.free_records: %d, "
2363 "Insert.tree_depth: %d\n",
2364 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
2365 insert.ins_free_records, insert.ins_tree_depth);
2368 * Avoid growing the tree unless we're out of records and the
2369 * insert type requres one.
2371 if (insert.ins_contig != CONTIG_NONE || insert.ins_free_records)
2374 shift = ocfs2_find_branch_target(osb, inode, fe_bh, &bh);
2381 /* We traveled all the way to the bottom of the allocation tree
2382 * and didn't find room for any more extents - we need to add
2383 * another tree level */
2386 mlog(0, "need to shift tree depth "
2387 "(current = %d)\n", insert.ins_tree_depth);
2389 /* ocfs2_shift_tree_depth will return us a buffer with
2390 * the new extent block (so we can pass that to
2391 * ocfs2_add_branch). */
2392 status = ocfs2_shift_tree_depth(osb, handle, inode, fe_bh,
2398 insert.ins_tree_depth++;
2399 /* Special case: we have room now if we shifted from
2401 if (insert.ins_tree_depth == 1)
2405 /* call ocfs2_add_branch to add the final part of the tree with
2407 mlog(0, "add branch. bh = %p\n", bh);
2408 status = ocfs2_add_branch(osb, handle, inode, fe_bh, bh, last_eb_bh,
2416 /* Finally, we can add clusters. This might rotate the tree for us. */
2417 status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert);
2421 ocfs2_extent_map_insert_rec(inode, &rec);
2434 static inline int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
2436 struct buffer_head *tl_bh = osb->osb_tl_bh;
2437 struct ocfs2_dinode *di;
2438 struct ocfs2_truncate_log *tl;
2440 di = (struct ocfs2_dinode *) tl_bh->b_data;
2441 tl = &di->id2.i_dealloc;
2443 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
2444 "slot %d, invalid truncate log parameters: used = "
2445 "%u, count = %u\n", osb->slot_num,
2446 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
2447 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
2450 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
2451 unsigned int new_start)
2453 unsigned int tail_index;
2454 unsigned int current_tail;
2456 /* No records, nothing to coalesce */
2457 if (!le16_to_cpu(tl->tl_used))
2460 tail_index = le16_to_cpu(tl->tl_used) - 1;
2461 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
2462 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
2464 return current_tail == new_start;
2467 static int ocfs2_truncate_log_append(struct ocfs2_super *osb,
2470 unsigned int num_clusters)
2473 unsigned int start_cluster, tl_count;
2474 struct inode *tl_inode = osb->osb_tl_inode;
2475 struct buffer_head *tl_bh = osb->osb_tl_bh;
2476 struct ocfs2_dinode *di;
2477 struct ocfs2_truncate_log *tl;
2479 mlog_entry("start_blk = %llu, num_clusters = %u\n",
2480 (unsigned long long)start_blk, num_clusters);
2482 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
2484 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
2486 di = (struct ocfs2_dinode *) tl_bh->b_data;
2487 tl = &di->id2.i_dealloc;
2488 if (!OCFS2_IS_VALID_DINODE(di)) {
2489 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
2494 tl_count = le16_to_cpu(tl->tl_count);
2495 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
2497 "Truncate record count on #%llu invalid "
2498 "wanted %u, actual %u\n",
2499 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
2500 ocfs2_truncate_recs_per_inode(osb->sb),
2501 le16_to_cpu(tl->tl_count));
2503 /* Caller should have known to flush before calling us. */
2504 index = le16_to_cpu(tl->tl_used);
2505 if (index >= tl_count) {
2511 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
2512 OCFS2_JOURNAL_ACCESS_WRITE);
2518 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
2519 "%llu (index = %d)\n", num_clusters, start_cluster,
2520 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
2522 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
2524 * Move index back to the record we are coalescing with.
2525 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
2529 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
2530 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
2531 index, le32_to_cpu(tl->tl_recs[index].t_start),
2534 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
2535 tl->tl_used = cpu_to_le16(index + 1);
2537 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
2539 status = ocfs2_journal_dirty(handle, tl_bh);
2550 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
2552 struct inode *data_alloc_inode,
2553 struct buffer_head *data_alloc_bh)
2557 unsigned int num_clusters;
2559 struct ocfs2_truncate_rec rec;
2560 struct ocfs2_dinode *di;
2561 struct ocfs2_truncate_log *tl;
2562 struct inode *tl_inode = osb->osb_tl_inode;
2563 struct buffer_head *tl_bh = osb->osb_tl_bh;
2567 di = (struct ocfs2_dinode *) tl_bh->b_data;
2568 tl = &di->id2.i_dealloc;
2569 i = le16_to_cpu(tl->tl_used) - 1;
2571 /* Caller has given us at least enough credits to
2572 * update the truncate log dinode */
2573 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
2574 OCFS2_JOURNAL_ACCESS_WRITE);
2580 tl->tl_used = cpu_to_le16(i);
2582 status = ocfs2_journal_dirty(handle, tl_bh);
2588 /* TODO: Perhaps we can calculate the bulk of the
2589 * credits up front rather than extending like
2591 status = ocfs2_extend_trans(handle,
2592 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
2598 rec = tl->tl_recs[i];
2599 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
2600 le32_to_cpu(rec.t_start));
2601 num_clusters = le32_to_cpu(rec.t_clusters);
2603 /* if start_blk is not set, we ignore the record as
2606 mlog(0, "free record %d, start = %u, clusters = %u\n",
2607 i, le32_to_cpu(rec.t_start), num_clusters);
2609 status = ocfs2_free_clusters(handle, data_alloc_inode,
2610 data_alloc_bh, start_blk,
2625 /* Expects you to already be holding tl_inode->i_mutex */
2626 static int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
2629 unsigned int num_to_flush;
2631 struct inode *tl_inode = osb->osb_tl_inode;
2632 struct inode *data_alloc_inode = NULL;
2633 struct buffer_head *tl_bh = osb->osb_tl_bh;
2634 struct buffer_head *data_alloc_bh = NULL;
2635 struct ocfs2_dinode *di;
2636 struct ocfs2_truncate_log *tl;
2640 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
2642 di = (struct ocfs2_dinode *) tl_bh->b_data;
2643 tl = &di->id2.i_dealloc;
2644 if (!OCFS2_IS_VALID_DINODE(di)) {
2645 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
2650 num_to_flush = le16_to_cpu(tl->tl_used);
2651 mlog(0, "Flush %u records from truncate log #%llu\n",
2652 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
2653 if (!num_to_flush) {
2658 data_alloc_inode = ocfs2_get_system_file_inode(osb,
2659 GLOBAL_BITMAP_SYSTEM_INODE,
2660 OCFS2_INVALID_SLOT);
2661 if (!data_alloc_inode) {
2663 mlog(ML_ERROR, "Could not get bitmap inode!\n");
2667 mutex_lock(&data_alloc_inode->i_mutex);
2669 status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1);
2675 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
2676 if (IS_ERR(handle)) {
2677 status = PTR_ERR(handle);
2682 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
2687 ocfs2_commit_trans(osb, handle);
2690 brelse(data_alloc_bh);
2691 ocfs2_meta_unlock(data_alloc_inode, 1);
2694 mutex_unlock(&data_alloc_inode->i_mutex);
2695 iput(data_alloc_inode);
2702 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
2705 struct inode *tl_inode = osb->osb_tl_inode;
2707 mutex_lock(&tl_inode->i_mutex);
2708 status = __ocfs2_flush_truncate_log(osb);
2709 mutex_unlock(&tl_inode->i_mutex);
2714 static void ocfs2_truncate_log_worker(struct work_struct *work)
2717 struct ocfs2_super *osb =
2718 container_of(work, struct ocfs2_super,
2719 osb_truncate_log_wq.work);
2723 status = ocfs2_flush_truncate_log(osb);
2730 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
2731 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
2734 if (osb->osb_tl_inode) {
2735 /* We want to push off log flushes while truncates are
2738 cancel_delayed_work(&osb->osb_truncate_log_wq);
2740 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
2741 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
2745 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
2747 struct inode **tl_inode,
2748 struct buffer_head **tl_bh)
2751 struct inode *inode = NULL;
2752 struct buffer_head *bh = NULL;
2754 inode = ocfs2_get_system_file_inode(osb,
2755 TRUNCATE_LOG_SYSTEM_INODE,
2759 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
2763 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
2764 OCFS2_BH_CACHED, inode);
2778 /* called during the 1st stage of node recovery. we stamp a clean
2779 * truncate log and pass back a copy for processing later. if the
2780 * truncate log does not require processing, a *tl_copy is set to
2782 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
2784 struct ocfs2_dinode **tl_copy)
2787 struct inode *tl_inode = NULL;
2788 struct buffer_head *tl_bh = NULL;
2789 struct ocfs2_dinode *di;
2790 struct ocfs2_truncate_log *tl;
2794 mlog(0, "recover truncate log from slot %d\n", slot_num);
2796 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
2802 di = (struct ocfs2_dinode *) tl_bh->b_data;
2803 tl = &di->id2.i_dealloc;
2804 if (!OCFS2_IS_VALID_DINODE(di)) {
2805 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
2810 if (le16_to_cpu(tl->tl_used)) {
2811 mlog(0, "We'll have %u logs to recover\n",
2812 le16_to_cpu(tl->tl_used));
2814 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
2821 /* Assuming the write-out below goes well, this copy
2822 * will be passed back to recovery for processing. */
2823 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
2825 /* All we need to do to clear the truncate log is set
2829 status = ocfs2_write_block(osb, tl_bh, tl_inode);
2842 if (status < 0 && (*tl_copy)) {
2851 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
2852 struct ocfs2_dinode *tl_copy)
2856 unsigned int clusters, num_recs, start_cluster;
2859 struct inode *tl_inode = osb->osb_tl_inode;
2860 struct ocfs2_truncate_log *tl;
2864 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
2865 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
2869 tl = &tl_copy->id2.i_dealloc;
2870 num_recs = le16_to_cpu(tl->tl_used);
2871 mlog(0, "cleanup %u records from %llu\n", num_recs,
2872 (unsigned long long)tl_copy->i_blkno);
2874 mutex_lock(&tl_inode->i_mutex);
2875 for(i = 0; i < num_recs; i++) {
2876 if (ocfs2_truncate_log_needs_flush(osb)) {
2877 status = __ocfs2_flush_truncate_log(osb);
2884 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
2885 if (IS_ERR(handle)) {
2886 status = PTR_ERR(handle);
2891 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
2892 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
2893 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
2895 status = ocfs2_truncate_log_append(osb, handle,
2896 start_blk, clusters);
2897 ocfs2_commit_trans(osb, handle);
2905 mutex_unlock(&tl_inode->i_mutex);
2911 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
2914 struct inode *tl_inode = osb->osb_tl_inode;
2919 cancel_delayed_work(&osb->osb_truncate_log_wq);
2920 flush_workqueue(ocfs2_wq);
2922 status = ocfs2_flush_truncate_log(osb);
2926 brelse(osb->osb_tl_bh);
2927 iput(osb->osb_tl_inode);
2933 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
2936 struct inode *tl_inode = NULL;
2937 struct buffer_head *tl_bh = NULL;
2941 status = ocfs2_get_truncate_log_info(osb,
2948 /* ocfs2_truncate_log_shutdown keys on the existence of
2949 * osb->osb_tl_inode so we don't set any of the osb variables
2950 * until we're sure all is well. */
2951 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
2952 ocfs2_truncate_log_worker);
2953 osb->osb_tl_bh = tl_bh;
2954 osb->osb_tl_inode = tl_inode;
2960 /* This function will figure out whether the currently last extent
2961 * block will be deleted, and if it will, what the new last extent
2962 * block will be so we can update his h_next_leaf_blk field, as well
2963 * as the dinodes i_last_eb_blk */
2964 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
2965 unsigned int clusters_to_del,
2966 struct ocfs2_path *path,
2967 struct buffer_head **new_last_eb)
2969 int next_free, ret = 0;
2971 struct ocfs2_extent_rec *rec;
2972 struct ocfs2_extent_block *eb;
2973 struct ocfs2_extent_list *el;
2974 struct buffer_head *bh = NULL;
2976 *new_last_eb = NULL;
2978 /* we have no tree, so of course, no last_eb. */
2979 if (!path->p_tree_depth)
2982 /* trunc to zero special case - this makes tree_depth = 0
2983 * regardless of what it is. */
2984 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
2987 el = path_leaf_el(path);
2988 BUG_ON(!el->l_next_free_rec);
2991 * Make sure that this extent list will actually be empty
2992 * after we clear away the data. We can shortcut out if
2993 * there's more than one non-empty extent in the
2994 * list. Otherwise, a check of the remaining extent is
2997 next_free = le16_to_cpu(el->l_next_free_rec);
2999 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
3003 /* We may have a valid extent in index 1, check it. */
3005 rec = &el->l_recs[1];
3008 * Fall through - no more nonempty extents, so we want
3009 * to delete this leaf.
3015 rec = &el->l_recs[0];
3020 * Check it we'll only be trimming off the end of this
3023 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
3027 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
3033 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
3039 eb = (struct ocfs2_extent_block *) bh->b_data;
3041 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
3042 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
3048 get_bh(*new_last_eb);
3049 mlog(0, "returning block %llu, (cpos: %u)\n",
3050 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
3058 * Trim some clusters off the rightmost edge of a tree. Only called
3061 * The caller needs to:
3062 * - start journaling of each path component.
3063 * - compute and fully set up any new last ext block
3065 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
3066 handle_t *handle, struct ocfs2_truncate_context *tc,
3067 u32 clusters_to_del, u64 *delete_start)
3069 int ret, i, index = path->p_tree_depth;
3072 struct buffer_head *bh;
3073 struct ocfs2_extent_list *el;
3074 struct ocfs2_extent_rec *rec;
3078 while (index >= 0) {
3079 bh = path->p_node[index].bh;
3080 el = path->p_node[index].el;
3082 mlog(0, "traveling tree (index = %d, block = %llu)\n",
3083 index, (unsigned long long)bh->b_blocknr);
3085 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
3088 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
3089 ocfs2_error(inode->i_sb,
3090 "Inode %lu has invalid ext. block %llu",
3092 (unsigned long long)bh->b_blocknr);
3098 i = le16_to_cpu(el->l_next_free_rec) - 1;
3099 rec = &el->l_recs[i];
3101 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
3102 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
3103 ocfs2_rec_clusters(el, rec),
3104 (unsigned long long)le64_to_cpu(rec->e_blkno),
3105 le16_to_cpu(el->l_next_free_rec));
3107 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
3109 if (le16_to_cpu(el->l_tree_depth) == 0) {
3111 * If the leaf block contains a single empty
3112 * extent and no records, we can just remove
3115 if (i == 0 && ocfs2_is_empty_extent(rec)) {
3117 sizeof(struct ocfs2_extent_rec));
3118 el->l_next_free_rec = cpu_to_le16(0);
3124 * Remove any empty extents by shifting things
3125 * left. That should make life much easier on
3126 * the code below. This condition is rare
3127 * enough that we shouldn't see a performance
3130 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
3131 le16_add_cpu(&el->l_next_free_rec, -1);
3134 i < le16_to_cpu(el->l_next_free_rec); i++)
3135 el->l_recs[i] = el->l_recs[i + 1];
3137 memset(&el->l_recs[i], 0,
3138 sizeof(struct ocfs2_extent_rec));
3141 * We've modified our extent list. The
3142 * simplest way to handle this change
3143 * is to being the search from the
3146 goto find_tail_record;
3149 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
3152 * We'll use "new_edge" on our way back up the
3153 * tree to know what our rightmost cpos is.
3155 new_edge = le16_to_cpu(rec->e_leaf_clusters);
3156 new_edge += le32_to_cpu(rec->e_cpos);
3159 * The caller will use this to delete data blocks.
3161 *delete_start = le64_to_cpu(rec->e_blkno)
3162 + ocfs2_clusters_to_blocks(inode->i_sb,
3163 le16_to_cpu(rec->e_leaf_clusters));
3166 * If it's now empty, remove this record.
3168 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
3170 sizeof(struct ocfs2_extent_rec));
3171 le16_add_cpu(&el->l_next_free_rec, -1);
3174 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
3176 sizeof(struct ocfs2_extent_rec));
3177 le16_add_cpu(&el->l_next_free_rec, -1);
3182 /* Can this actually happen? */
3183 if (le16_to_cpu(el->l_next_free_rec) == 0)
3187 * We never actually deleted any clusters
3188 * because our leaf was empty. There's no
3189 * reason to adjust the rightmost edge then.
3194 rec->e_int_clusters = cpu_to_le32(new_edge);
3195 le32_add_cpu(&rec->e_int_clusters,
3196 -le32_to_cpu(rec->e_cpos));
3199 * A deleted child record should have been
3202 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
3206 ret = ocfs2_journal_dirty(handle, bh);
3212 mlog(0, "extent list container %llu, after: record %d: "
3213 "(%u, %u, %llu), next = %u.\n",
3214 (unsigned long long)bh->b_blocknr, i,
3215 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
3216 (unsigned long long)le64_to_cpu(rec->e_blkno),
3217 le16_to_cpu(el->l_next_free_rec));
3220 * We must be careful to only attempt delete of an
3221 * extent block (and not the root inode block).
3223 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
3224 struct ocfs2_extent_block *eb =
3225 (struct ocfs2_extent_block *)bh->b_data;
3228 * Save this for use when processing the
3231 deleted_eb = le64_to_cpu(eb->h_blkno);
3233 mlog(0, "deleting this extent block.\n");
3235 ocfs2_remove_from_cache(inode, bh);
3237 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
3238 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
3239 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
3241 if (le16_to_cpu(eb->h_suballoc_slot) == 0) {
3243 * This code only understands how to
3244 * lock the suballocator in slot 0,
3245 * which is fine because allocation is
3246 * only ever done out of that
3247 * suballocator too. A future version
3248 * might change that however, so avoid
3249 * a free if we don't know how to
3250 * handle it. This way an fs incompat
3251 * bit will not be necessary.
3253 ret = ocfs2_free_extent_block(handle,
3254 tc->tc_ext_alloc_inode,
3255 tc->tc_ext_alloc_bh,
3258 /* An error here is not fatal. */
3274 static int ocfs2_do_truncate(struct ocfs2_super *osb,
3275 unsigned int clusters_to_del,
3276 struct inode *inode,
3277 struct buffer_head *fe_bh,
3279 struct ocfs2_truncate_context *tc,
3280 struct ocfs2_path *path)
3283 struct ocfs2_dinode *fe;
3284 struct ocfs2_extent_block *last_eb = NULL;
3285 struct ocfs2_extent_list *el;
3286 struct buffer_head *last_eb_bh = NULL;
3289 fe = (struct ocfs2_dinode *) fe_bh->b_data;
3291 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
3299 * Each component will be touched, so we might as well journal
3300 * here to avoid having to handle errors later.
3302 status = ocfs2_journal_access_path(inode, handle, path);
3309 status = ocfs2_journal_access(handle, inode, last_eb_bh,
3310 OCFS2_JOURNAL_ACCESS_WRITE);
3316 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
3319 el = &(fe->id2.i_list);
3322 * Lower levels depend on this never happening, but it's best
3323 * to check it up here before changing the tree.
3325 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
3326 ocfs2_error(inode->i_sb,
3327 "Inode %lu has an empty extent record, depth %u\n",
3328 inode->i_ino, le16_to_cpu(el->l_tree_depth));
3333 spin_lock(&OCFS2_I(inode)->ip_lock);
3334 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
3336 spin_unlock(&OCFS2_I(inode)->ip_lock);
3337 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
3339 status = ocfs2_trim_tree(inode, path, handle, tc,
3340 clusters_to_del, &delete_blk);
3346 if (le32_to_cpu(fe->i_clusters) == 0) {
3347 /* trunc to zero is a special case. */
3348 el->l_tree_depth = 0;
3349 fe->i_last_eb_blk = 0;
3351 fe->i_last_eb_blk = last_eb->h_blkno;
3353 status = ocfs2_journal_dirty(handle, fe_bh);
3360 /* If there will be a new last extent block, then by
3361 * definition, there cannot be any leaves to the right of
3363 last_eb->h_next_leaf_blk = 0;
3364 status = ocfs2_journal_dirty(handle, last_eb_bh);
3372 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
3386 static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
3388 set_buffer_uptodate(bh);
3389 mark_buffer_dirty(bh);
3393 static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
3395 set_buffer_uptodate(bh);
3396 mark_buffer_dirty(bh);
3397 return ocfs2_journal_dirty_data(handle, bh);
3400 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t isize,
3401 struct page **pages, int numpages,
3402 u64 phys, handle_t *handle)
3404 int i, ret, partial = 0;
3407 unsigned int from, to = PAGE_CACHE_SIZE;
3408 struct super_block *sb = inode->i_sb;
3410 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
3415 from = isize & (PAGE_CACHE_SIZE - 1); /* 1st page offset */
3416 if (PAGE_CACHE_SHIFT > OCFS2_SB(sb)->s_clustersize_bits) {
3418 * Since 'from' has been capped to a value below page
3419 * size, this calculation won't be able to overflow
3422 to = ocfs2_align_bytes_to_clusters(sb, from);
3425 * The truncate tail in this case should never contain
3426 * more than one page at maximum. The loop below also
3429 BUG_ON(numpages != 1);
3432 for(i = 0; i < numpages; i++) {
3435 BUG_ON(from > PAGE_CACHE_SIZE);
3436 BUG_ON(to > PAGE_CACHE_SIZE);
3438 ret = ocfs2_map_page_blocks(page, &phys, inode, from, to, 0);
3442 kaddr = kmap_atomic(page, KM_USER0);
3443 memset(kaddr + from, 0, to - from);
3444 kunmap_atomic(kaddr, KM_USER0);
3447 * Need to set the buffers we zero'd into uptodate
3448 * here if they aren't - ocfs2_map_page_blocks()
3449 * might've skipped some
3451 if (ocfs2_should_order_data(inode)) {
3452 ret = walk_page_buffers(handle,
3455 ocfs2_ordered_zero_func);
3459 ret = walk_page_buffers(handle, page_buffers(page),
3461 ocfs2_writeback_zero_func);
3467 SetPageUptodate(page);
3469 flush_dcache_page(page);
3472 * Every page after the 1st one should be completely zero'd.
3478 for (i = 0; i < numpages; i++) {
3481 mark_page_accessed(page);
3482 page_cache_release(page);
3487 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t isize, struct page **pages,
3488 int *num, u64 *phys)
3490 int i, numpages = 0, ret = 0;
3491 unsigned int csize = OCFS2_SB(inode->i_sb)->s_clustersize;
3492 unsigned int ext_flags;
3493 struct super_block *sb = inode->i_sb;
3494 struct address_space *mapping = inode->i_mapping;
3495 unsigned long index;
3496 u64 next_cluster_bytes;
3498 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
3500 /* Cluster boundary, so we don't need to grab any pages. */
3501 if ((isize & (csize - 1)) == 0)
3504 ret = ocfs2_extent_map_get_blocks(inode, isize >> sb->s_blocksize_bits,
3505 phys, NULL, &ext_flags);
3511 /* Tail is a hole. */
3515 /* Tail is marked as unwritten, we can count on write to zero
3517 if (ext_flags & OCFS2_EXT_UNWRITTEN)
3520 next_cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, isize);
3521 index = isize >> PAGE_CACHE_SHIFT;
3523 pages[numpages] = grab_cache_page(mapping, index);
3524 if (!pages[numpages]) {
3532 } while (index < (next_cluster_bytes >> PAGE_CACHE_SHIFT));
3537 for (i = 0; i < numpages; i++) {
3539 unlock_page(pages[i]);
3540 page_cache_release(pages[i]);
3553 * Zero the area past i_size but still within an allocated
3554 * cluster. This avoids exposing nonzero data on subsequent file
3557 * We need to call this before i_size is updated on the inode because
3558 * otherwise block_write_full_page() will skip writeout of pages past
3559 * i_size. The new_i_size parameter is passed for this reason.
3561 int ocfs2_zero_tail_for_truncate(struct inode *inode, handle_t *handle,
3566 struct page **pages = NULL;
3570 * File systems which don't support sparse files zero on every
3573 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
3576 pages = kcalloc(ocfs2_pages_per_cluster(inode->i_sb),
3577 sizeof(struct page *), GFP_NOFS);
3578 if (pages == NULL) {
3584 ret = ocfs2_grab_eof_pages(inode, new_i_size, pages, &numpages, &phys);
3593 ocfs2_zero_cluster_pages(inode, new_i_size, pages, numpages, phys,
3597 * Initiate writeout of the pages we zero'd here. We don't
3598 * wait on them - the truncate_inode_pages() call later will
3601 endbyte = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
3602 ret = do_sync_mapping_range(inode->i_mapping, new_i_size,
3603 endbyte - 1, SYNC_FILE_RANGE_WRITE);
3615 * It is expected, that by the time you call this function,
3616 * inode->i_size and fe->i_size have been adjusted.
3618 * WARNING: This will kfree the truncate context
3620 int ocfs2_commit_truncate(struct ocfs2_super *osb,
3621 struct inode *inode,
3622 struct buffer_head *fe_bh,
3623 struct ocfs2_truncate_context *tc)
3625 int status, i, credits, tl_sem = 0;
3626 u32 clusters_to_del, new_highest_cpos, range;
3627 struct ocfs2_extent_list *el;
3628 handle_t *handle = NULL;
3629 struct inode *tl_inode = osb->osb_tl_inode;
3630 struct ocfs2_path *path = NULL;
3634 down_write(&OCFS2_I(inode)->ip_alloc_sem);
3636 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
3637 i_size_read(inode));
3639 path = ocfs2_new_inode_path(fe_bh);
3646 ocfs2_extent_map_trunc(inode, new_highest_cpos);
3650 * Check that we still have allocation to delete.
3652 if (OCFS2_I(inode)->ip_clusters == 0) {
3658 * Truncate always works against the rightmost tree branch.
3660 status = ocfs2_find_path(inode, path, UINT_MAX);
3666 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
3667 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
3670 * By now, el will point to the extent list on the bottom most
3671 * portion of this tree. Only the tail record is considered in
3674 * We handle the following cases, in order:
3675 * - empty extent: delete the remaining branch
3676 * - remove the entire record
3677 * - remove a partial record
3678 * - no record needs to be removed (truncate has completed)
3680 el = path_leaf_el(path);
3681 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3682 ocfs2_error(inode->i_sb,
3683 "Inode %llu has empty extent block at %llu\n",
3684 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3685 (unsigned long long)path_leaf_bh(path)->b_blocknr);
3690 i = le16_to_cpu(el->l_next_free_rec) - 1;
3691 range = le32_to_cpu(el->l_recs[i].e_cpos) +
3692 ocfs2_rec_clusters(el, &el->l_recs[i]);
3693 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
3694 clusters_to_del = 0;
3695 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
3696 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
3697 } else if (range > new_highest_cpos) {
3698 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
3699 le32_to_cpu(el->l_recs[i].e_cpos)) -
3706 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
3707 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
3709 BUG_ON(clusters_to_del == 0);
3711 mutex_lock(&tl_inode->i_mutex);
3713 /* ocfs2_truncate_log_needs_flush guarantees us at least one
3714 * record is free for use. If there isn't any, we flush to get
3715 * an empty truncate log. */
3716 if (ocfs2_truncate_log_needs_flush(osb)) {
3717 status = __ocfs2_flush_truncate_log(osb);
3724 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
3725 (struct ocfs2_dinode *)fe_bh->b_data,
3727 handle = ocfs2_start_trans(osb, credits);
3728 if (IS_ERR(handle)) {
3729 status = PTR_ERR(handle);
3735 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
3742 mutex_unlock(&tl_inode->i_mutex);
3745 ocfs2_commit_trans(osb, handle);
3748 ocfs2_reinit_path(path, 1);
3751 * The check above will catch the case where we've truncated
3752 * away all allocation.
3757 up_write(&OCFS2_I(inode)->ip_alloc_sem);
3759 ocfs2_schedule_truncate_log_flush(osb, 1);
3762 mutex_unlock(&tl_inode->i_mutex);
3765 ocfs2_commit_trans(osb, handle);
3767 ocfs2_free_path(path);
3769 /* This will drop the ext_alloc cluster lock for us */
3770 ocfs2_free_truncate_context(tc);
3777 * Expects the inode to already be locked. This will figure out which
3778 * inodes need to be locked and will put them on the returned truncate
3781 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
3782 struct inode *inode,
3783 struct buffer_head *fe_bh,
3784 struct ocfs2_truncate_context **tc)
3786 int status, metadata_delete, i;
3787 unsigned int new_i_clusters;
3788 struct ocfs2_dinode *fe;
3789 struct ocfs2_extent_block *eb;
3790 struct ocfs2_extent_list *el;
3791 struct buffer_head *last_eb_bh = NULL;
3792 struct inode *ext_alloc_inode = NULL;
3793 struct buffer_head *ext_alloc_bh = NULL;
3799 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
3800 i_size_read(inode));
3801 fe = (struct ocfs2_dinode *) fe_bh->b_data;
3803 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
3804 "%llu\n", fe->i_clusters, new_i_clusters,
3805 (unsigned long long)fe->i_size);
3807 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
3814 metadata_delete = 0;
3815 if (fe->id2.i_list.l_tree_depth) {
3816 /* If we have a tree, then the truncate may result in
3817 * metadata deletes. Figure this out from the
3818 * rightmost leaf block.*/
3819 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
3820 &last_eb_bh, OCFS2_BH_CACHED, inode);
3825 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
3826 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
3827 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
3836 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3839 * XXX: Should we check that next_free_rec contains
3842 if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_i_clusters)
3843 metadata_delete = 1;
3846 (*tc)->tc_last_eb_bh = last_eb_bh;
3848 if (metadata_delete) {
3849 mlog(0, "Will have to delete metadata for this trunc. "
3850 "locking allocator.\n");
3851 ext_alloc_inode = ocfs2_get_system_file_inode(osb, EXTENT_ALLOC_SYSTEM_INODE, 0);
3852 if (!ext_alloc_inode) {
3858 mutex_lock(&ext_alloc_inode->i_mutex);
3859 (*tc)->tc_ext_alloc_inode = ext_alloc_inode;
3861 status = ocfs2_meta_lock(ext_alloc_inode, &ext_alloc_bh, 1);
3866 (*tc)->tc_ext_alloc_bh = ext_alloc_bh;
3867 (*tc)->tc_ext_alloc_locked = 1;
3874 ocfs2_free_truncate_context(*tc);
3881 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
3883 if (tc->tc_ext_alloc_inode) {
3884 if (tc->tc_ext_alloc_locked)
3885 ocfs2_meta_unlock(tc->tc_ext_alloc_inode, 1);
3887 mutex_unlock(&tc->tc_ext_alloc_inode->i_mutex);
3888 iput(tc->tc_ext_alloc_inode);
3891 if (tc->tc_ext_alloc_bh)
3892 brelse(tc->tc_ext_alloc_bh);
3894 if (tc->tc_last_eb_bh)
3895 brelse(tc->tc_last_eb_bh);