1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Extent allocs and frees
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
30 #include <linux/swap.h>
32 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33 #include <cluster/masklog.h>
40 #include "extent_map.h"
43 #include "localalloc.h"
50 #include "buffer_head_io.h"
52 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
53 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
54 struct ocfs2_extent_block *eb);
57 * Structures which describe a path through a btree, and functions to
60 * The idea here is to be as generic as possible with the tree
63 struct ocfs2_path_item {
64 struct buffer_head *bh;
65 struct ocfs2_extent_list *el;
68 #define OCFS2_MAX_PATH_DEPTH 5
72 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
75 #define path_root_bh(_path) ((_path)->p_node[0].bh)
76 #define path_root_el(_path) ((_path)->p_node[0].el)
77 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
78 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
79 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
82 * Reset the actual path elements so that we can re-use the structure
83 * to build another path. Generally, this involves freeing the buffer
86 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
88 int i, start = 0, depth = 0;
89 struct ocfs2_path_item *node;
94 for(i = start; i < path_num_items(path); i++) {
95 node = &path->p_node[i];
103 * Tree depth may change during truncate, or insert. If we're
104 * keeping the root extent list, then make sure that our path
105 * structure reflects the proper depth.
108 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
110 path->p_tree_depth = depth;
113 static void ocfs2_free_path(struct ocfs2_path *path)
116 ocfs2_reinit_path(path, 0);
122 * Make the *dest path the same as src and re-initialize src path to
125 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
129 BUG_ON(path_root_bh(dest) != path_root_bh(src));
131 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
132 brelse(dest->p_node[i].bh);
134 dest->p_node[i].bh = src->p_node[i].bh;
135 dest->p_node[i].el = src->p_node[i].el;
137 src->p_node[i].bh = NULL;
138 src->p_node[i].el = NULL;
143 * Insert an extent block at given index.
145 * This will not take an additional reference on eb_bh.
147 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
148 struct buffer_head *eb_bh)
150 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
153 * Right now, no root bh is an extent block, so this helps
154 * catch code errors with dinode trees. The assertion can be
155 * safely removed if we ever need to insert extent block
156 * structures at the root.
160 path->p_node[index].bh = eb_bh;
161 path->p_node[index].el = &eb->h_list;
164 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
165 struct ocfs2_extent_list *root_el)
167 struct ocfs2_path *path;
169 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
171 path = kzalloc(sizeof(*path), GFP_NOFS);
173 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
175 path_root_bh(path) = root_bh;
176 path_root_el(path) = root_el;
183 * Allocate and initialize a new path based on a disk inode tree.
185 static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh)
187 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
188 struct ocfs2_extent_list *el = &di->id2.i_list;
190 return ocfs2_new_path(di_bh, el);
194 * Convenience function to journal all components in a path.
196 static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
197 struct ocfs2_path *path)
204 for(i = 0; i < path_num_items(path); i++) {
205 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
206 OCFS2_JOURNAL_ACCESS_WRITE);
217 enum ocfs2_contig_type {
225 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
226 * ocfs2_extent_contig only work properly against leaf nodes!
228 static int ocfs2_block_extent_contig(struct super_block *sb,
229 struct ocfs2_extent_rec *ext,
232 u64 blk_end = le64_to_cpu(ext->e_blkno);
234 blk_end += ocfs2_clusters_to_blocks(sb,
235 le16_to_cpu(ext->e_leaf_clusters));
237 return blkno == blk_end;
240 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
241 struct ocfs2_extent_rec *right)
245 left_range = le32_to_cpu(left->e_cpos) +
246 le16_to_cpu(left->e_leaf_clusters);
248 return (left_range == le32_to_cpu(right->e_cpos));
251 static enum ocfs2_contig_type
252 ocfs2_extent_contig(struct inode *inode,
253 struct ocfs2_extent_rec *ext,
254 struct ocfs2_extent_rec *insert_rec)
256 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
258 if (ocfs2_extents_adjacent(ext, insert_rec) &&
259 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
262 blkno = le64_to_cpu(ext->e_blkno);
263 if (ocfs2_extents_adjacent(insert_rec, ext) &&
264 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
271 * NOTE: We can have pretty much any combination of contiguousness and
274 * The usefulness of APPEND_TAIL is more in that it lets us know that
275 * we'll have to update the path to that leaf.
277 enum ocfs2_append_type {
282 struct ocfs2_insert_type {
283 enum ocfs2_append_type ins_appending;
284 enum ocfs2_contig_type ins_contig;
285 int ins_contig_index;
286 int ins_free_records;
291 * How many free extents have we got before we need more meta data?
293 int ocfs2_num_free_extents(struct ocfs2_super *osb,
295 struct ocfs2_dinode *fe)
298 struct ocfs2_extent_list *el;
299 struct ocfs2_extent_block *eb;
300 struct buffer_head *eb_bh = NULL;
304 if (!OCFS2_IS_VALID_DINODE(fe)) {
305 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
310 if (fe->i_last_eb_blk) {
311 retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
312 &eb_bh, OCFS2_BH_CACHED, inode);
317 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
320 el = &fe->id2.i_list;
322 BUG_ON(el->l_tree_depth != 0);
324 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
333 /* expects array to already be allocated
335 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
338 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
342 struct ocfs2_alloc_context *meta_ac,
343 struct buffer_head *bhs[])
345 int count, status, i;
346 u16 suballoc_bit_start;
349 struct ocfs2_extent_block *eb;
354 while (count < wanted) {
355 status = ocfs2_claim_metadata(osb,
367 for(i = count; i < (num_got + count); i++) {
368 bhs[i] = sb_getblk(osb->sb, first_blkno);
369 if (bhs[i] == NULL) {
374 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
376 status = ocfs2_journal_access(handle, inode, bhs[i],
377 OCFS2_JOURNAL_ACCESS_CREATE);
383 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
384 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
385 /* Ok, setup the minimal stuff here. */
386 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
387 eb->h_blkno = cpu_to_le64(first_blkno);
388 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
389 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
390 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
392 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
394 suballoc_bit_start++;
397 /* We'll also be dirtied by the caller, so
398 * this isn't absolutely necessary. */
399 status = ocfs2_journal_dirty(handle, bhs[i]);
412 for(i = 0; i < wanted; i++) {
423 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
425 * Returns the sum of the rightmost extent rec logical offset and
428 * ocfs2_add_branch() uses this to determine what logical cluster
429 * value should be populated into the leftmost new branch records.
431 * ocfs2_shift_tree_depth() uses this to determine the # clusters
432 * value for the new topmost tree record.
434 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
438 i = le16_to_cpu(el->l_next_free_rec) - 1;
440 return le32_to_cpu(el->l_recs[i].e_cpos) +
441 ocfs2_rec_clusters(el, &el->l_recs[i]);
445 * Add an entire tree branch to our inode. eb_bh is the extent block
446 * to start at, if we don't want to start the branch at the dinode
449 * last_eb_bh is required as we have to update it's next_leaf pointer
450 * for the new last extent block.
452 * the new branch will be 'empty' in the sense that every block will
453 * contain a single record with cluster count == 0.
455 static int ocfs2_add_branch(struct ocfs2_super *osb,
458 struct buffer_head *fe_bh,
459 struct buffer_head *eb_bh,
460 struct buffer_head *last_eb_bh,
461 struct ocfs2_alloc_context *meta_ac)
463 int status, new_blocks, i;
464 u64 next_blkno, new_last_eb_blk;
465 struct buffer_head *bh;
466 struct buffer_head **new_eb_bhs = NULL;
467 struct ocfs2_dinode *fe;
468 struct ocfs2_extent_block *eb;
469 struct ocfs2_extent_list *eb_el;
470 struct ocfs2_extent_list *el;
477 fe = (struct ocfs2_dinode *) fe_bh->b_data;
480 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
483 el = &fe->id2.i_list;
485 /* we never add a branch to a leaf. */
486 BUG_ON(!el->l_tree_depth);
488 new_blocks = le16_to_cpu(el->l_tree_depth);
490 /* allocate the number of new eb blocks we need */
491 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
499 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
500 meta_ac, new_eb_bhs);
506 eb = (struct ocfs2_extent_block *)last_eb_bh->b_data;
507 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
509 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
510 * linked with the rest of the tree.
511 * conversly, new_eb_bhs[0] is the new bottommost leaf.
513 * when we leave the loop, new_last_eb_blk will point to the
514 * newest leaf, and next_blkno will point to the topmost extent
516 next_blkno = new_last_eb_blk = 0;
517 for(i = 0; i < new_blocks; i++) {
519 eb = (struct ocfs2_extent_block *) bh->b_data;
520 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
521 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
527 status = ocfs2_journal_access(handle, inode, bh,
528 OCFS2_JOURNAL_ACCESS_CREATE);
534 eb->h_next_leaf_blk = 0;
535 eb_el->l_tree_depth = cpu_to_le16(i);
536 eb_el->l_next_free_rec = cpu_to_le16(1);
538 * This actually counts as an empty extent as
541 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
542 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
544 * eb_el isn't always an interior node, but even leaf
545 * nodes want a zero'd flags and reserved field so
546 * this gets the whole 32 bits regardless of use.
548 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
549 if (!eb_el->l_tree_depth)
550 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
552 status = ocfs2_journal_dirty(handle, bh);
558 next_blkno = le64_to_cpu(eb->h_blkno);
561 /* This is a bit hairy. We want to update up to three blocks
562 * here without leaving any of them in an inconsistent state
563 * in case of error. We don't have to worry about
564 * journal_dirty erroring as it won't unless we've aborted the
565 * handle (in which case we would never be here) so reserving
566 * the write with journal_access is all we need to do. */
567 status = ocfs2_journal_access(handle, inode, last_eb_bh,
568 OCFS2_JOURNAL_ACCESS_WRITE);
573 status = ocfs2_journal_access(handle, inode, fe_bh,
574 OCFS2_JOURNAL_ACCESS_WRITE);
580 status = ocfs2_journal_access(handle, inode, eb_bh,
581 OCFS2_JOURNAL_ACCESS_WRITE);
588 /* Link the new branch into the rest of the tree (el will
589 * either be on the fe, or the extent block passed in. */
590 i = le16_to_cpu(el->l_next_free_rec);
591 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
592 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
593 el->l_recs[i].e_int_clusters = 0;
594 le16_add_cpu(&el->l_next_free_rec, 1);
596 /* fe needs a new last extent block pointer, as does the
597 * next_leaf on the previously last-extent-block. */
598 fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk);
600 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
601 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
603 status = ocfs2_journal_dirty(handle, last_eb_bh);
606 status = ocfs2_journal_dirty(handle, fe_bh);
610 status = ocfs2_journal_dirty(handle, eb_bh);
618 for (i = 0; i < new_blocks; i++)
620 brelse(new_eb_bhs[i]);
629 * adds another level to the allocation tree.
630 * returns back the new extent block so you can add a branch to it
633 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
636 struct buffer_head *fe_bh,
637 struct ocfs2_alloc_context *meta_ac,
638 struct buffer_head **ret_new_eb_bh)
642 struct buffer_head *new_eb_bh = NULL;
643 struct ocfs2_dinode *fe;
644 struct ocfs2_extent_block *eb;
645 struct ocfs2_extent_list *fe_el;
646 struct ocfs2_extent_list *eb_el;
650 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
657 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
658 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
659 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
665 fe = (struct ocfs2_dinode *) fe_bh->b_data;
666 fe_el = &fe->id2.i_list;
668 status = ocfs2_journal_access(handle, inode, new_eb_bh,
669 OCFS2_JOURNAL_ACCESS_CREATE);
675 /* copy the fe data into the new extent block */
676 eb_el->l_tree_depth = fe_el->l_tree_depth;
677 eb_el->l_next_free_rec = fe_el->l_next_free_rec;
678 for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
679 eb_el->l_recs[i] = fe_el->l_recs[i];
681 status = ocfs2_journal_dirty(handle, new_eb_bh);
687 status = ocfs2_journal_access(handle, inode, fe_bh,
688 OCFS2_JOURNAL_ACCESS_WRITE);
694 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
697 le16_add_cpu(&fe_el->l_tree_depth, 1);
698 fe_el->l_recs[0].e_cpos = 0;
699 fe_el->l_recs[0].e_blkno = eb->h_blkno;
700 fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
701 for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
702 memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
703 fe_el->l_next_free_rec = cpu_to_le16(1);
705 /* If this is our 1st tree depth shift, then last_eb_blk
706 * becomes the allocated extent block */
707 if (fe_el->l_tree_depth == cpu_to_le16(1))
708 fe->i_last_eb_blk = eb->h_blkno;
710 status = ocfs2_journal_dirty(handle, fe_bh);
716 *ret_new_eb_bh = new_eb_bh;
728 * Should only be called when there is no space left in any of the
729 * leaf nodes. What we want to do is find the lowest tree depth
730 * non-leaf extent block with room for new records. There are three
731 * valid results of this search:
733 * 1) a lowest extent block is found, then we pass it back in
734 * *lowest_eb_bh and return '0'
736 * 2) the search fails to find anything, but the dinode has room. We
737 * pass NULL back in *lowest_eb_bh, but still return '0'
739 * 3) the search fails to find anything AND the dinode is full, in
740 * which case we return > 0
742 * return status < 0 indicates an error.
744 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
746 struct buffer_head *fe_bh,
747 struct buffer_head **target_bh)
751 struct ocfs2_dinode *fe;
752 struct ocfs2_extent_block *eb;
753 struct ocfs2_extent_list *el;
754 struct buffer_head *bh = NULL;
755 struct buffer_head *lowest_bh = NULL;
761 fe = (struct ocfs2_dinode *) fe_bh->b_data;
762 el = &fe->id2.i_list;
764 while(le16_to_cpu(el->l_tree_depth) > 1) {
765 if (le16_to_cpu(el->l_next_free_rec) == 0) {
766 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
767 "extent list (next_free_rec == 0)",
768 (unsigned long long)OCFS2_I(inode)->ip_blkno);
772 i = le16_to_cpu(el->l_next_free_rec) - 1;
773 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
775 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
776 "list where extent # %d has no physical "
778 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
788 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
795 eb = (struct ocfs2_extent_block *) bh->b_data;
796 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
797 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
803 if (le16_to_cpu(el->l_next_free_rec) <
804 le16_to_cpu(el->l_count)) {
812 /* If we didn't find one and the fe doesn't have any room,
815 && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count))
818 *target_bh = lowest_bh;
828 * Grow a b-tree so that it has more records.
830 * We might shift the tree depth in which case existing paths should
831 * be considered invalid.
833 * Tree depth after the grow is returned via *final_depth.
835 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
836 struct buffer_head *di_bh, int *final_depth,
837 struct buffer_head *last_eb_bh,
838 struct ocfs2_alloc_context *meta_ac)
841 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
842 int depth = le16_to_cpu(di->id2.i_list.l_tree_depth);
843 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
844 struct buffer_head *bh = NULL;
846 BUG_ON(meta_ac == NULL);
848 shift = ocfs2_find_branch_target(osb, inode, di_bh, &bh);
855 /* We traveled all the way to the bottom of the allocation tree
856 * and didn't find room for any more extents - we need to add
857 * another tree level */
860 mlog(0, "need to shift tree depth (current = %d)\n", depth);
862 /* ocfs2_shift_tree_depth will return us a buffer with
863 * the new extent block (so we can pass that to
864 * ocfs2_add_branch). */
865 ret = ocfs2_shift_tree_depth(osb, handle, inode, di_bh,
872 /* Special case: we have room now if we shifted from
878 /* call ocfs2_add_branch to add the final part of the tree with
880 mlog(0, "add branch. bh = %p\n", bh);
881 ret = ocfs2_add_branch(osb, handle, inode, di_bh, bh, last_eb_bh,
890 *final_depth = depth;
896 * This is only valid for leaf nodes, which are the only ones that can
897 * have empty extents anyway.
899 static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
901 return !rec->e_leaf_clusters;
905 * This function will discard the rightmost extent record.
907 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
909 int next_free = le16_to_cpu(el->l_next_free_rec);
910 int count = le16_to_cpu(el->l_count);
911 unsigned int num_bytes;
914 /* This will cause us to go off the end of our extent list. */
915 BUG_ON(next_free >= count);
917 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
919 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
922 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
923 struct ocfs2_extent_rec *insert_rec)
925 int i, insert_index, next_free, has_empty, num_bytes;
926 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
927 struct ocfs2_extent_rec *rec;
929 next_free = le16_to_cpu(el->l_next_free_rec);
930 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
934 /* The tree code before us didn't allow enough room in the leaf. */
935 if (el->l_next_free_rec == el->l_count && !has_empty)
939 * The easiest way to approach this is to just remove the
940 * empty extent and temporarily decrement next_free.
944 * If next_free was 1 (only an empty extent), this
945 * loop won't execute, which is fine. We still want
946 * the decrement above to happen.
948 for(i = 0; i < (next_free - 1); i++)
949 el->l_recs[i] = el->l_recs[i+1];
955 * Figure out what the new record index should be.
957 for(i = 0; i < next_free; i++) {
958 rec = &el->l_recs[i];
960 if (insert_cpos < le32_to_cpu(rec->e_cpos))
965 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
966 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
968 BUG_ON(insert_index < 0);
969 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
970 BUG_ON(insert_index > next_free);
973 * No need to memmove if we're just adding to the tail.
975 if (insert_index != next_free) {
976 BUG_ON(next_free >= le16_to_cpu(el->l_count));
978 num_bytes = next_free - insert_index;
979 num_bytes *= sizeof(struct ocfs2_extent_rec);
980 memmove(&el->l_recs[insert_index + 1],
981 &el->l_recs[insert_index],
986 * Either we had an empty extent, and need to re-increment or
987 * there was no empty extent on a non full rightmost leaf node,
988 * in which case we still need to increment.
991 el->l_next_free_rec = cpu_to_le16(next_free);
993 * Make sure none of the math above just messed up our tree.
995 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
997 el->l_recs[insert_index] = *insert_rec;
1002 * Create an empty extent record .
1004 * l_next_free_rec may be updated.
1006 * If an empty extent already exists do nothing.
1008 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1010 int next_free = le16_to_cpu(el->l_next_free_rec);
1012 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1017 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1020 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1021 "Asked to create an empty extent in a full list:\n"
1022 "count = %u, tree depth = %u",
1023 le16_to_cpu(el->l_count),
1024 le16_to_cpu(el->l_tree_depth));
1026 ocfs2_shift_records_right(el);
1029 le16_add_cpu(&el->l_next_free_rec, 1);
1030 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1034 * For a rotation which involves two leaf nodes, the "root node" is
1035 * the lowest level tree node which contains a path to both leafs. This
1036 * resulting set of information can be used to form a complete "subtree"
1038 * This function is passed two full paths from the dinode down to a
1039 * pair of adjacent leaves. It's task is to figure out which path
1040 * index contains the subtree root - this can be the root index itself
1041 * in a worst-case rotation.
1043 * The array index of the subtree root is passed back.
1045 static int ocfs2_find_subtree_root(struct inode *inode,
1046 struct ocfs2_path *left,
1047 struct ocfs2_path *right)
1052 * Check that the caller passed in two paths from the same tree.
1054 BUG_ON(path_root_bh(left) != path_root_bh(right));
1060 * The caller didn't pass two adjacent paths.
1062 mlog_bug_on_msg(i > left->p_tree_depth,
1063 "Inode %lu, left depth %u, right depth %u\n"
1064 "left leaf blk %llu, right leaf blk %llu\n",
1065 inode->i_ino, left->p_tree_depth,
1066 right->p_tree_depth,
1067 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1068 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1069 } while (left->p_node[i].bh->b_blocknr ==
1070 right->p_node[i].bh->b_blocknr);
1075 typedef void (path_insert_t)(void *, struct buffer_head *);
1078 * Traverse a btree path in search of cpos, starting at root_el.
1080 * This code can be called with a cpos larger than the tree, in which
1081 * case it will return the rightmost path.
1083 static int __ocfs2_find_path(struct inode *inode,
1084 struct ocfs2_extent_list *root_el, u32 cpos,
1085 path_insert_t *func, void *data)
1090 struct buffer_head *bh = NULL;
1091 struct ocfs2_extent_block *eb;
1092 struct ocfs2_extent_list *el;
1093 struct ocfs2_extent_rec *rec;
1094 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1097 while (el->l_tree_depth) {
1098 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1099 ocfs2_error(inode->i_sb,
1100 "Inode %llu has empty extent list at "
1102 (unsigned long long)oi->ip_blkno,
1103 le16_to_cpu(el->l_tree_depth));
1109 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1110 rec = &el->l_recs[i];
1113 * In the case that cpos is off the allocation
1114 * tree, this should just wind up returning the
1117 range = le32_to_cpu(rec->e_cpos) +
1118 ocfs2_rec_clusters(el, rec);
1119 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1123 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1125 ocfs2_error(inode->i_sb,
1126 "Inode %llu has bad blkno in extent list "
1127 "at depth %u (index %d)\n",
1128 (unsigned long long)oi->ip_blkno,
1129 le16_to_cpu(el->l_tree_depth), i);
1136 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1137 &bh, OCFS2_BH_CACHED, inode);
1143 eb = (struct ocfs2_extent_block *) bh->b_data;
1145 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1146 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1151 if (le16_to_cpu(el->l_next_free_rec) >
1152 le16_to_cpu(el->l_count)) {
1153 ocfs2_error(inode->i_sb,
1154 "Inode %llu has bad count in extent list "
1155 "at block %llu (next free=%u, count=%u)\n",
1156 (unsigned long long)oi->ip_blkno,
1157 (unsigned long long)bh->b_blocknr,
1158 le16_to_cpu(el->l_next_free_rec),
1159 le16_to_cpu(el->l_count));
1170 * Catch any trailing bh that the loop didn't handle.
1178 * Given an initialized path (that is, it has a valid root extent
1179 * list), this function will traverse the btree in search of the path
1180 * which would contain cpos.
1182 * The path traveled is recorded in the path structure.
1184 * Note that this will not do any comparisons on leaf node extent
1185 * records, so it will work fine in the case that we just added a tree
1188 struct find_path_data {
1190 struct ocfs2_path *path;
1192 static void find_path_ins(void *data, struct buffer_head *bh)
1194 struct find_path_data *fp = data;
1197 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1200 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1203 struct find_path_data data;
1207 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1208 find_path_ins, &data);
1211 static void find_leaf_ins(void *data, struct buffer_head *bh)
1213 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1214 struct ocfs2_extent_list *el = &eb->h_list;
1215 struct buffer_head **ret = data;
1217 /* We want to retain only the leaf block. */
1218 if (le16_to_cpu(el->l_tree_depth) == 0) {
1224 * Find the leaf block in the tree which would contain cpos. No
1225 * checking of the actual leaf is done.
1227 * Some paths want to call this instead of allocating a path structure
1228 * and calling ocfs2_find_path().
1230 * This function doesn't handle non btree extent lists.
1232 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1233 u32 cpos, struct buffer_head **leaf_bh)
1236 struct buffer_head *bh = NULL;
1238 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1250 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1252 * Basically, we've moved stuff around at the bottom of the tree and
1253 * we need to fix up the extent records above the changes to reflect
1256 * left_rec: the record on the left.
1257 * left_child_el: is the child list pointed to by left_rec
1258 * right_rec: the record to the right of left_rec
1259 * right_child_el: is the child list pointed to by right_rec
1261 * By definition, this only works on interior nodes.
1263 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1264 struct ocfs2_extent_list *left_child_el,
1265 struct ocfs2_extent_rec *right_rec,
1266 struct ocfs2_extent_list *right_child_el)
1268 u32 left_clusters, right_end;
1271 * Interior nodes never have holes. Their cpos is the cpos of
1272 * the leftmost record in their child list. Their cluster
1273 * count covers the full theoretical range of their child list
1274 * - the range between their cpos and the cpos of the record
1275 * immediately to their right.
1277 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1278 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1279 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1282 * Calculate the rightmost cluster count boundary before
1283 * moving cpos - we will need to adjust clusters after
1284 * updating e_cpos to keep the same highest cluster count.
1286 right_end = le32_to_cpu(right_rec->e_cpos);
1287 right_end += le32_to_cpu(right_rec->e_int_clusters);
1289 right_rec->e_cpos = left_rec->e_cpos;
1290 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1292 right_end -= le32_to_cpu(right_rec->e_cpos);
1293 right_rec->e_int_clusters = cpu_to_le32(right_end);
1297 * Adjust the adjacent root node records involved in a
1298 * rotation. left_el_blkno is passed in as a key so that we can easily
1299 * find it's index in the root list.
1301 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1302 struct ocfs2_extent_list *left_el,
1303 struct ocfs2_extent_list *right_el,
1308 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1309 le16_to_cpu(left_el->l_tree_depth));
1311 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1312 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1317 * The path walking code should have never returned a root and
1318 * two paths which are not adjacent.
1320 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1322 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1323 &root_el->l_recs[i + 1], right_el);
1327 * We've changed a leaf block (in right_path) and need to reflect that
1328 * change back up the subtree.
1330 * This happens in multiple places:
1331 * - When we've moved an extent record from the left path leaf to the right
1332 * path leaf to make room for an empty extent in the left path leaf.
1333 * - When our insert into the right path leaf is at the leftmost edge
1334 * and requires an update of the path immediately to it's left. This
1335 * can occur at the end of some types of rotation and appending inserts.
1337 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1338 struct ocfs2_path *left_path,
1339 struct ocfs2_path *right_path,
1343 struct ocfs2_extent_list *el, *left_el, *right_el;
1344 struct ocfs2_extent_rec *left_rec, *right_rec;
1345 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1348 * Update the counts and position values within all the
1349 * interior nodes to reflect the leaf rotation we just did.
1351 * The root node is handled below the loop.
1353 * We begin the loop with right_el and left_el pointing to the
1354 * leaf lists and work our way up.
1356 * NOTE: within this loop, left_el and right_el always refer
1357 * to the *child* lists.
1359 left_el = path_leaf_el(left_path);
1360 right_el = path_leaf_el(right_path);
1361 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1362 mlog(0, "Adjust records at index %u\n", i);
1365 * One nice property of knowing that all of these
1366 * nodes are below the root is that we only deal with
1367 * the leftmost right node record and the rightmost
1370 el = left_path->p_node[i].el;
1371 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1372 left_rec = &el->l_recs[idx];
1374 el = right_path->p_node[i].el;
1375 right_rec = &el->l_recs[0];
1377 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1380 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1384 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1389 * Setup our list pointers now so that the current
1390 * parents become children in the next iteration.
1392 left_el = left_path->p_node[i].el;
1393 right_el = right_path->p_node[i].el;
1397 * At the root node, adjust the two adjacent records which
1398 * begin our path to the leaves.
1401 el = left_path->p_node[subtree_index].el;
1402 left_el = left_path->p_node[subtree_index + 1].el;
1403 right_el = right_path->p_node[subtree_index + 1].el;
1405 ocfs2_adjust_root_records(el, left_el, right_el,
1406 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1408 root_bh = left_path->p_node[subtree_index].bh;
1410 ret = ocfs2_journal_dirty(handle, root_bh);
1415 static int ocfs2_rotate_subtree_right(struct inode *inode,
1417 struct ocfs2_path *left_path,
1418 struct ocfs2_path *right_path,
1422 struct buffer_head *right_leaf_bh;
1423 struct buffer_head *left_leaf_bh = NULL;
1424 struct buffer_head *root_bh;
1425 struct ocfs2_extent_list *right_el, *left_el;
1426 struct ocfs2_extent_rec move_rec;
1428 left_leaf_bh = path_leaf_bh(left_path);
1429 left_el = path_leaf_el(left_path);
1431 if (left_el->l_next_free_rec != left_el->l_count) {
1432 ocfs2_error(inode->i_sb,
1433 "Inode %llu has non-full interior leaf node %llu"
1435 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1436 (unsigned long long)left_leaf_bh->b_blocknr,
1437 le16_to_cpu(left_el->l_next_free_rec));
1442 * This extent block may already have an empty record, so we
1443 * return early if so.
1445 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1448 root_bh = left_path->p_node[subtree_index].bh;
1449 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1451 ret = ocfs2_journal_access(handle, inode, root_bh,
1452 OCFS2_JOURNAL_ACCESS_WRITE);
1458 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1459 ret = ocfs2_journal_access(handle, inode,
1460 right_path->p_node[i].bh,
1461 OCFS2_JOURNAL_ACCESS_WRITE);
1467 ret = ocfs2_journal_access(handle, inode,
1468 left_path->p_node[i].bh,
1469 OCFS2_JOURNAL_ACCESS_WRITE);
1476 right_leaf_bh = path_leaf_bh(right_path);
1477 right_el = path_leaf_el(right_path);
1479 /* This is a code error, not a disk corruption. */
1480 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1481 "because rightmost leaf block %llu is empty\n",
1482 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1483 (unsigned long long)right_leaf_bh->b_blocknr);
1485 ocfs2_create_empty_extent(right_el);
1487 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1493 /* Do the copy now. */
1494 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1495 move_rec = left_el->l_recs[i];
1496 right_el->l_recs[0] = move_rec;
1499 * Clear out the record we just copied and shift everything
1500 * over, leaving an empty extent in the left leaf.
1502 * We temporarily subtract from next_free_rec so that the
1503 * shift will lose the tail record (which is now defunct).
1505 le16_add_cpu(&left_el->l_next_free_rec, -1);
1506 ocfs2_shift_records_right(left_el);
1507 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1508 le16_add_cpu(&left_el->l_next_free_rec, 1);
1510 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1516 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1524 * Given a full path, determine what cpos value would return us a path
1525 * containing the leaf immediately to the left of the current one.
1527 * Will return zero if the path passed in is already the leftmost path.
1529 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1530 struct ocfs2_path *path, u32 *cpos)
1534 struct ocfs2_extent_list *el;
1536 BUG_ON(path->p_tree_depth == 0);
1540 blkno = path_leaf_bh(path)->b_blocknr;
1542 /* Start at the tree node just above the leaf and work our way up. */
1543 i = path->p_tree_depth - 1;
1545 el = path->p_node[i].el;
1548 * Find the extent record just before the one in our
1551 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1552 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1556 * We've determined that the
1557 * path specified is already
1558 * the leftmost one - return a
1564 * The leftmost record points to our
1565 * leaf - we need to travel up the
1571 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
1572 *cpos = *cpos + ocfs2_rec_clusters(el,
1573 &el->l_recs[j - 1]);
1580 * If we got here, we never found a valid node where
1581 * the tree indicated one should be.
1584 "Invalid extent tree at extent block %llu\n",
1585 (unsigned long long)blkno);
1590 blkno = path->p_node[i].bh->b_blocknr;
1598 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
1599 struct ocfs2_path *path)
1601 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1;
1603 if (handle->h_buffer_credits < credits)
1604 return ocfs2_extend_trans(handle, credits);
1610 * Trap the case where we're inserting into the theoretical range past
1611 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1612 * whose cpos is less than ours into the right leaf.
1614 * It's only necessary to look at the rightmost record of the left
1615 * leaf because the logic that calls us should ensure that the
1616 * theoretical ranges in the path components above the leaves are
1619 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1622 struct ocfs2_extent_list *left_el;
1623 struct ocfs2_extent_rec *rec;
1626 left_el = path_leaf_el(left_path);
1627 next_free = le16_to_cpu(left_el->l_next_free_rec);
1628 rec = &left_el->l_recs[next_free - 1];
1630 if (insert_cpos > le32_to_cpu(rec->e_cpos))
1636 * Rotate all the records in a btree right one record, starting at insert_cpos.
1638 * The path to the rightmost leaf should be passed in.
1640 * The array is assumed to be large enough to hold an entire path (tree depth).
1642 * Upon succesful return from this function:
1644 * - The 'right_path' array will contain a path to the leaf block
1645 * whose range contains e_cpos.
1646 * - That leaf block will have a single empty extent in list index 0.
1647 * - In the case that the rotation requires a post-insert update,
1648 * *ret_left_path will contain a valid path which can be passed to
1649 * ocfs2_insert_path().
1651 static int ocfs2_rotate_tree_right(struct inode *inode,
1654 struct ocfs2_path *right_path,
1655 struct ocfs2_path **ret_left_path)
1659 struct ocfs2_path *left_path = NULL;
1661 *ret_left_path = NULL;
1663 left_path = ocfs2_new_path(path_root_bh(right_path),
1664 path_root_el(right_path));
1671 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
1677 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
1680 * What we want to do here is:
1682 * 1) Start with the rightmost path.
1684 * 2) Determine a path to the leaf block directly to the left
1687 * 3) Determine the 'subtree root' - the lowest level tree node
1688 * which contains a path to both leaves.
1690 * 4) Rotate the subtree.
1692 * 5) Find the next subtree by considering the left path to be
1693 * the new right path.
1695 * The check at the top of this while loop also accepts
1696 * insert_cpos == cpos because cpos is only a _theoretical_
1697 * value to get us the left path - insert_cpos might very well
1698 * be filling that hole.
1700 * Stop at a cpos of '0' because we either started at the
1701 * leftmost branch (i.e., a tree with one branch and a
1702 * rotation inside of it), or we've gone as far as we can in
1703 * rotating subtrees.
1705 while (cpos && insert_cpos <= cpos) {
1706 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
1709 ret = ocfs2_find_path(inode, left_path, cpos);
1715 mlog_bug_on_msg(path_leaf_bh(left_path) ==
1716 path_leaf_bh(right_path),
1717 "Inode %lu: error during insert of %u "
1718 "(left path cpos %u) results in two identical "
1719 "paths ending at %llu\n",
1720 inode->i_ino, insert_cpos, cpos,
1721 (unsigned long long)
1722 path_leaf_bh(left_path)->b_blocknr);
1724 if (ocfs2_rotate_requires_path_adjustment(left_path,
1726 mlog(0, "Path adjustment required\n");
1729 * We've rotated the tree as much as we
1730 * should. The rest is up to
1731 * ocfs2_insert_path() to complete, after the
1732 * record insertion. We indicate this
1733 * situation by returning the left path.
1735 * The reason we don't adjust the records here
1736 * before the record insert is that an error
1737 * later might break the rule where a parent
1738 * record e_cpos will reflect the actual
1739 * e_cpos of the 1st nonempty record of the
1742 *ret_left_path = left_path;
1746 start = ocfs2_find_subtree_root(inode, left_path, right_path);
1748 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
1750 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
1751 right_path->p_tree_depth);
1753 ret = ocfs2_extend_rotate_transaction(handle, start,
1760 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
1768 * There is no need to re-read the next right path
1769 * as we know that it'll be our current left
1770 * path. Optimize by copying values instead.
1772 ocfs2_mv_path(right_path, left_path);
1774 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
1783 ocfs2_free_path(left_path);
1790 * Do the final bits of extent record insertion at the target leaf
1791 * list. If this leaf is part of an allocation tree, it is assumed
1792 * that the tree above has been prepared.
1794 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
1795 struct ocfs2_extent_list *el,
1796 struct ocfs2_insert_type *insert,
1797 struct inode *inode)
1799 int i = insert->ins_contig_index;
1801 struct ocfs2_extent_rec *rec;
1803 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1806 * Contiguous insert - either left or right.
1808 if (insert->ins_contig != CONTIG_NONE) {
1809 rec = &el->l_recs[i];
1810 if (insert->ins_contig == CONTIG_LEFT) {
1811 rec->e_blkno = insert_rec->e_blkno;
1812 rec->e_cpos = insert_rec->e_cpos;
1814 le16_add_cpu(&rec->e_leaf_clusters,
1815 le16_to_cpu(insert_rec->e_leaf_clusters));
1820 * Handle insert into an empty leaf.
1822 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
1823 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
1824 ocfs2_is_empty_extent(&el->l_recs[0]))) {
1825 el->l_recs[0] = *insert_rec;
1826 el->l_next_free_rec = cpu_to_le16(1);
1833 if (insert->ins_appending == APPEND_TAIL) {
1834 i = le16_to_cpu(el->l_next_free_rec) - 1;
1835 rec = &el->l_recs[i];
1836 range = le32_to_cpu(rec->e_cpos)
1837 + le16_to_cpu(rec->e_leaf_clusters);
1838 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
1840 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
1841 le16_to_cpu(el->l_count),
1842 "inode %lu, depth %u, count %u, next free %u, "
1843 "rec.cpos %u, rec.clusters %u, "
1844 "insert.cpos %u, insert.clusters %u\n",
1846 le16_to_cpu(el->l_tree_depth),
1847 le16_to_cpu(el->l_count),
1848 le16_to_cpu(el->l_next_free_rec),
1849 le32_to_cpu(el->l_recs[i].e_cpos),
1850 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
1851 le32_to_cpu(insert_rec->e_cpos),
1852 le16_to_cpu(insert_rec->e_leaf_clusters));
1854 el->l_recs[i] = *insert_rec;
1855 le16_add_cpu(&el->l_next_free_rec, 1);
1860 * Ok, we have to rotate.
1862 * At this point, it is safe to assume that inserting into an
1863 * empty leaf and appending to a leaf have both been handled
1866 * This leaf needs to have space, either by the empty 1st
1867 * extent record, or by virtue of an l_next_rec < l_count.
1869 ocfs2_rotate_leaf(el, insert_rec);
1872 static inline void ocfs2_update_dinode_clusters(struct inode *inode,
1873 struct ocfs2_dinode *di,
1876 le32_add_cpu(&di->i_clusters, clusters);
1877 spin_lock(&OCFS2_I(inode)->ip_lock);
1878 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
1879 spin_unlock(&OCFS2_I(inode)->ip_lock);
1882 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
1883 struct ocfs2_extent_rec *insert_rec,
1884 struct ocfs2_path *right_path,
1885 struct ocfs2_path **ret_left_path)
1887 int ret, i, next_free;
1888 struct buffer_head *bh;
1889 struct ocfs2_extent_list *el;
1890 struct ocfs2_path *left_path = NULL;
1892 *ret_left_path = NULL;
1895 * This shouldn't happen for non-trees. The extent rec cluster
1896 * count manipulation below only works for interior nodes.
1898 BUG_ON(right_path->p_tree_depth == 0);
1901 * If our appending insert is at the leftmost edge of a leaf,
1902 * then we might need to update the rightmost records of the
1905 el = path_leaf_el(right_path);
1906 next_free = le16_to_cpu(el->l_next_free_rec);
1907 if (next_free == 0 ||
1908 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
1911 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
1918 mlog(0, "Append may need a left path update. cpos: %u, "
1919 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
1923 * No need to worry if the append is already in the
1927 left_path = ocfs2_new_path(path_root_bh(right_path),
1928 path_root_el(right_path));
1935 ret = ocfs2_find_path(inode, left_path, left_cpos);
1942 * ocfs2_insert_path() will pass the left_path to the
1948 ret = ocfs2_journal_access_path(inode, handle, right_path);
1954 el = path_root_el(right_path);
1955 bh = path_root_bh(right_path);
1958 struct ocfs2_extent_rec *rec;
1960 next_free = le16_to_cpu(el->l_next_free_rec);
1961 if (next_free == 0) {
1962 ocfs2_error(inode->i_sb,
1963 "Dinode %llu has a bad extent list",
1964 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1969 rec = &el->l_recs[next_free - 1];
1971 rec->e_int_clusters = insert_rec->e_cpos;
1972 le32_add_cpu(&rec->e_int_clusters,
1973 le16_to_cpu(insert_rec->e_leaf_clusters));
1974 le32_add_cpu(&rec->e_int_clusters,
1975 -le32_to_cpu(rec->e_cpos));
1977 ret = ocfs2_journal_dirty(handle, bh);
1981 /* Don't touch the leaf node */
1982 if (++i >= right_path->p_tree_depth)
1985 bh = right_path->p_node[i].bh;
1986 el = right_path->p_node[i].el;
1989 *ret_left_path = left_path;
1993 ocfs2_free_path(left_path);
1999 * This function only does inserts on an allocation b-tree. For dinode
2000 * lists, ocfs2_insert_at_leaf() is called directly.
2002 * right_path is the path we want to do the actual insert
2003 * in. left_path should only be passed in if we need to update that
2004 * portion of the tree after an edge insert.
2006 static int ocfs2_insert_path(struct inode *inode,
2008 struct ocfs2_path *left_path,
2009 struct ocfs2_path *right_path,
2010 struct ocfs2_extent_rec *insert_rec,
2011 struct ocfs2_insert_type *insert)
2013 int ret, subtree_index;
2014 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
2015 struct ocfs2_extent_list *el;
2018 * Pass both paths to the journal. The majority of inserts
2019 * will be touching all components anyway.
2021 ret = ocfs2_journal_access_path(inode, handle, right_path);
2028 int credits = handle->h_buffer_credits;
2031 * There's a chance that left_path got passed back to
2032 * us without being accounted for in the
2033 * journal. Extend our transaction here to be sure we
2034 * can change those blocks.
2036 credits += left_path->p_tree_depth;
2038 ret = ocfs2_extend_trans(handle, credits);
2044 ret = ocfs2_journal_access_path(inode, handle, left_path);
2051 el = path_leaf_el(right_path);
2053 ocfs2_insert_at_leaf(insert_rec, el, insert, inode);
2054 ret = ocfs2_journal_dirty(handle, leaf_bh);
2060 * The rotate code has indicated that we need to fix
2061 * up portions of the tree after the insert.
2063 * XXX: Should we extend the transaction here?
2065 subtree_index = ocfs2_find_subtree_root(inode, left_path,
2067 ocfs2_complete_edge_insert(inode, handle, left_path,
2068 right_path, subtree_index);
2076 static int ocfs2_do_insert_extent(struct inode *inode,
2078 struct buffer_head *di_bh,
2079 struct ocfs2_extent_rec *insert_rec,
2080 struct ocfs2_insert_type *type)
2082 int ret, rotate = 0;
2084 struct ocfs2_path *right_path = NULL;
2085 struct ocfs2_path *left_path = NULL;
2086 struct ocfs2_dinode *di;
2087 struct ocfs2_extent_list *el;
2089 di = (struct ocfs2_dinode *) di_bh->b_data;
2090 el = &di->id2.i_list;
2092 ret = ocfs2_journal_access(handle, inode, di_bh,
2093 OCFS2_JOURNAL_ACCESS_WRITE);
2099 if (le16_to_cpu(el->l_tree_depth) == 0) {
2100 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
2101 goto out_update_clusters;
2104 right_path = ocfs2_new_inode_path(di_bh);
2112 * Determine the path to start with. Rotations need the
2113 * rightmost path, everything else can go directly to the
2116 cpos = le32_to_cpu(insert_rec->e_cpos);
2117 if (type->ins_appending == APPEND_NONE &&
2118 type->ins_contig == CONTIG_NONE) {
2123 ret = ocfs2_find_path(inode, right_path, cpos);
2130 * Rotations and appends need special treatment - they modify
2131 * parts of the tree's above them.
2133 * Both might pass back a path immediate to the left of the
2134 * one being inserted to. This will be cause
2135 * ocfs2_insert_path() to modify the rightmost records of
2136 * left_path to account for an edge insert.
2138 * XXX: When modifying this code, keep in mind that an insert
2139 * can wind up skipping both of these two special cases...
2142 ret = ocfs2_rotate_tree_right(inode, handle,
2143 le32_to_cpu(insert_rec->e_cpos),
2144 right_path, &left_path);
2149 } else if (type->ins_appending == APPEND_TAIL
2150 && type->ins_contig != CONTIG_LEFT) {
2151 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
2152 right_path, &left_path);
2159 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
2166 out_update_clusters:
2167 ocfs2_update_dinode_clusters(inode, di,
2168 le16_to_cpu(insert_rec->e_leaf_clusters));
2170 ret = ocfs2_journal_dirty(handle, di_bh);
2175 ocfs2_free_path(left_path);
2176 ocfs2_free_path(right_path);
2181 static void ocfs2_figure_contig_type(struct inode *inode,
2182 struct ocfs2_insert_type *insert,
2183 struct ocfs2_extent_list *el,
2184 struct ocfs2_extent_rec *insert_rec)
2187 enum ocfs2_contig_type contig_type = CONTIG_NONE;
2189 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
2191 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
2192 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
2194 if (contig_type != CONTIG_NONE) {
2195 insert->ins_contig_index = i;
2199 insert->ins_contig = contig_type;
2203 * This should only be called against the righmost leaf extent list.
2205 * ocfs2_figure_appending_type() will figure out whether we'll have to
2206 * insert at the tail of the rightmost leaf.
2208 * This should also work against the dinode list for tree's with 0
2209 * depth. If we consider the dinode list to be the rightmost leaf node
2210 * then the logic here makes sense.
2212 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
2213 struct ocfs2_extent_list *el,
2214 struct ocfs2_extent_rec *insert_rec)
2217 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
2218 struct ocfs2_extent_rec *rec;
2220 insert->ins_appending = APPEND_NONE;
2222 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
2224 if (!el->l_next_free_rec)
2225 goto set_tail_append;
2227 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
2228 /* Were all records empty? */
2229 if (le16_to_cpu(el->l_next_free_rec) == 1)
2230 goto set_tail_append;
2233 i = le16_to_cpu(el->l_next_free_rec) - 1;
2234 rec = &el->l_recs[i];
2237 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
2238 goto set_tail_append;
2243 insert->ins_appending = APPEND_TAIL;
2247 * Helper function called at the begining of an insert.
2249 * This computes a few things that are commonly used in the process of
2250 * inserting into the btree:
2251 * - Whether the new extent is contiguous with an existing one.
2252 * - The current tree depth.
2253 * - Whether the insert is an appending one.
2254 * - The total # of free records in the tree.
2256 * All of the information is stored on the ocfs2_insert_type
2259 static int ocfs2_figure_insert_type(struct inode *inode,
2260 struct buffer_head *di_bh,
2261 struct buffer_head **last_eb_bh,
2262 struct ocfs2_extent_rec *insert_rec,
2263 struct ocfs2_insert_type *insert)
2266 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2267 struct ocfs2_extent_block *eb;
2268 struct ocfs2_extent_list *el;
2269 struct ocfs2_path *path = NULL;
2270 struct buffer_head *bh = NULL;
2272 el = &di->id2.i_list;
2273 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
2275 if (el->l_tree_depth) {
2277 * If we have tree depth, we read in the
2278 * rightmost extent block ahead of time as
2279 * ocfs2_figure_insert_type() and ocfs2_add_branch()
2280 * may want it later.
2282 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2283 le64_to_cpu(di->i_last_eb_blk), &bh,
2284 OCFS2_BH_CACHED, inode);
2289 eb = (struct ocfs2_extent_block *) bh->b_data;
2294 * Unless we have a contiguous insert, we'll need to know if
2295 * there is room left in our allocation tree for another
2298 * XXX: This test is simplistic, we can search for empty
2299 * extent records too.
2301 insert->ins_free_records = le16_to_cpu(el->l_count) -
2302 le16_to_cpu(el->l_next_free_rec);
2304 if (!insert->ins_tree_depth) {
2305 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
2306 ocfs2_figure_appending_type(insert, el, insert_rec);
2310 path = ocfs2_new_inode_path(di_bh);
2318 * In the case that we're inserting past what the tree
2319 * currently accounts for, ocfs2_find_path() will return for
2320 * us the rightmost tree path. This is accounted for below in
2321 * the appending code.
2323 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
2329 el = path_leaf_el(path);
2332 * Now that we have the path, there's two things we want to determine:
2333 * 1) Contiguousness (also set contig_index if this is so)
2335 * 2) Are we doing an append? We can trivially break this up
2336 * into two types of appends: simple record append, or a
2337 * rotate inside the tail leaf.
2339 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
2342 * The insert code isn't quite ready to deal with all cases of
2343 * left contiguousness. Specifically, if it's an insert into
2344 * the 1st record in a leaf, it will require the adjustment of
2345 * cluster count on the last record of the path directly to it's
2346 * left. For now, just catch that case and fool the layers
2347 * above us. This works just fine for tree_depth == 0, which
2348 * is why we allow that above.
2350 if (insert->ins_contig == CONTIG_LEFT &&
2351 insert->ins_contig_index == 0)
2352 insert->ins_contig = CONTIG_NONE;
2355 * Ok, so we can simply compare against last_eb to figure out
2356 * whether the path doesn't exist. This will only happen in
2357 * the case that we're doing a tail append, so maybe we can
2358 * take advantage of that information somehow.
2360 if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) {
2362 * Ok, ocfs2_find_path() returned us the rightmost
2363 * tree path. This might be an appending insert. There are
2365 * 1) We're doing a true append at the tail:
2366 * -This might even be off the end of the leaf
2367 * 2) We're "appending" by rotating in the tail
2369 ocfs2_figure_appending_type(insert, el, insert_rec);
2373 ocfs2_free_path(path);
2383 * Insert an extent into an inode btree.
2385 * The caller needs to update fe->i_clusters
2387 int ocfs2_insert_extent(struct ocfs2_super *osb,
2389 struct inode *inode,
2390 struct buffer_head *fe_bh,
2394 struct ocfs2_alloc_context *meta_ac)
2397 struct buffer_head *last_eb_bh = NULL;
2398 struct buffer_head *bh = NULL;
2399 struct ocfs2_insert_type insert = {0, };
2400 struct ocfs2_extent_rec rec;
2402 mlog(0, "add %u clusters at position %u to inode %llu\n",
2403 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
2405 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
2406 (OCFS2_I(inode)->ip_clusters != cpos),
2407 "Device %s, asking for sparse allocation: inode %llu, "
2408 "cpos %u, clusters %u\n",
2410 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
2411 OCFS2_I(inode)->ip_clusters);
2413 memset(&rec, 0, sizeof(rec));
2414 rec.e_cpos = cpu_to_le32(cpos);
2415 rec.e_blkno = cpu_to_le64(start_blk);
2416 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
2418 status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec,
2425 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
2426 "Insert.contig_index: %d, Insert.free_records: %d, "
2427 "Insert.tree_depth: %d\n",
2428 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
2429 insert.ins_free_records, insert.ins_tree_depth);
2431 if (insert.ins_contig == CONTIG_NONE && insert.ins_free_records == 0) {
2432 status = ocfs2_grow_tree(inode, handle, fe_bh,
2433 &insert.ins_tree_depth, last_eb_bh,
2441 /* Finally, we can add clusters. This might rotate the tree for us. */
2442 status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert);
2446 ocfs2_extent_map_insert_rec(inode, &rec);
2459 static inline int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
2461 struct buffer_head *tl_bh = osb->osb_tl_bh;
2462 struct ocfs2_dinode *di;
2463 struct ocfs2_truncate_log *tl;
2465 di = (struct ocfs2_dinode *) tl_bh->b_data;
2466 tl = &di->id2.i_dealloc;
2468 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
2469 "slot %d, invalid truncate log parameters: used = "
2470 "%u, count = %u\n", osb->slot_num,
2471 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
2472 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
2475 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
2476 unsigned int new_start)
2478 unsigned int tail_index;
2479 unsigned int current_tail;
2481 /* No records, nothing to coalesce */
2482 if (!le16_to_cpu(tl->tl_used))
2485 tail_index = le16_to_cpu(tl->tl_used) - 1;
2486 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
2487 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
2489 return current_tail == new_start;
2492 static int ocfs2_truncate_log_append(struct ocfs2_super *osb,
2495 unsigned int num_clusters)
2498 unsigned int start_cluster, tl_count;
2499 struct inode *tl_inode = osb->osb_tl_inode;
2500 struct buffer_head *tl_bh = osb->osb_tl_bh;
2501 struct ocfs2_dinode *di;
2502 struct ocfs2_truncate_log *tl;
2504 mlog_entry("start_blk = %llu, num_clusters = %u\n",
2505 (unsigned long long)start_blk, num_clusters);
2507 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
2509 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
2511 di = (struct ocfs2_dinode *) tl_bh->b_data;
2512 tl = &di->id2.i_dealloc;
2513 if (!OCFS2_IS_VALID_DINODE(di)) {
2514 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
2519 tl_count = le16_to_cpu(tl->tl_count);
2520 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
2522 "Truncate record count on #%llu invalid "
2523 "wanted %u, actual %u\n",
2524 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
2525 ocfs2_truncate_recs_per_inode(osb->sb),
2526 le16_to_cpu(tl->tl_count));
2528 /* Caller should have known to flush before calling us. */
2529 index = le16_to_cpu(tl->tl_used);
2530 if (index >= tl_count) {
2536 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
2537 OCFS2_JOURNAL_ACCESS_WRITE);
2543 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
2544 "%llu (index = %d)\n", num_clusters, start_cluster,
2545 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
2547 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
2549 * Move index back to the record we are coalescing with.
2550 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
2554 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
2555 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
2556 index, le32_to_cpu(tl->tl_recs[index].t_start),
2559 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
2560 tl->tl_used = cpu_to_le16(index + 1);
2562 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
2564 status = ocfs2_journal_dirty(handle, tl_bh);
2575 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
2577 struct inode *data_alloc_inode,
2578 struct buffer_head *data_alloc_bh)
2582 unsigned int num_clusters;
2584 struct ocfs2_truncate_rec rec;
2585 struct ocfs2_dinode *di;
2586 struct ocfs2_truncate_log *tl;
2587 struct inode *tl_inode = osb->osb_tl_inode;
2588 struct buffer_head *tl_bh = osb->osb_tl_bh;
2592 di = (struct ocfs2_dinode *) tl_bh->b_data;
2593 tl = &di->id2.i_dealloc;
2594 i = le16_to_cpu(tl->tl_used) - 1;
2596 /* Caller has given us at least enough credits to
2597 * update the truncate log dinode */
2598 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
2599 OCFS2_JOURNAL_ACCESS_WRITE);
2605 tl->tl_used = cpu_to_le16(i);
2607 status = ocfs2_journal_dirty(handle, tl_bh);
2613 /* TODO: Perhaps we can calculate the bulk of the
2614 * credits up front rather than extending like
2616 status = ocfs2_extend_trans(handle,
2617 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
2623 rec = tl->tl_recs[i];
2624 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
2625 le32_to_cpu(rec.t_start));
2626 num_clusters = le32_to_cpu(rec.t_clusters);
2628 /* if start_blk is not set, we ignore the record as
2631 mlog(0, "free record %d, start = %u, clusters = %u\n",
2632 i, le32_to_cpu(rec.t_start), num_clusters);
2634 status = ocfs2_free_clusters(handle, data_alloc_inode,
2635 data_alloc_bh, start_blk,
2650 /* Expects you to already be holding tl_inode->i_mutex */
2651 static int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
2654 unsigned int num_to_flush;
2656 struct inode *tl_inode = osb->osb_tl_inode;
2657 struct inode *data_alloc_inode = NULL;
2658 struct buffer_head *tl_bh = osb->osb_tl_bh;
2659 struct buffer_head *data_alloc_bh = NULL;
2660 struct ocfs2_dinode *di;
2661 struct ocfs2_truncate_log *tl;
2665 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
2667 di = (struct ocfs2_dinode *) tl_bh->b_data;
2668 tl = &di->id2.i_dealloc;
2669 if (!OCFS2_IS_VALID_DINODE(di)) {
2670 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
2675 num_to_flush = le16_to_cpu(tl->tl_used);
2676 mlog(0, "Flush %u records from truncate log #%llu\n",
2677 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
2678 if (!num_to_flush) {
2683 data_alloc_inode = ocfs2_get_system_file_inode(osb,
2684 GLOBAL_BITMAP_SYSTEM_INODE,
2685 OCFS2_INVALID_SLOT);
2686 if (!data_alloc_inode) {
2688 mlog(ML_ERROR, "Could not get bitmap inode!\n");
2692 mutex_lock(&data_alloc_inode->i_mutex);
2694 status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1);
2700 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
2701 if (IS_ERR(handle)) {
2702 status = PTR_ERR(handle);
2707 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
2712 ocfs2_commit_trans(osb, handle);
2715 brelse(data_alloc_bh);
2716 ocfs2_meta_unlock(data_alloc_inode, 1);
2719 mutex_unlock(&data_alloc_inode->i_mutex);
2720 iput(data_alloc_inode);
2727 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
2730 struct inode *tl_inode = osb->osb_tl_inode;
2732 mutex_lock(&tl_inode->i_mutex);
2733 status = __ocfs2_flush_truncate_log(osb);
2734 mutex_unlock(&tl_inode->i_mutex);
2739 static void ocfs2_truncate_log_worker(struct work_struct *work)
2742 struct ocfs2_super *osb =
2743 container_of(work, struct ocfs2_super,
2744 osb_truncate_log_wq.work);
2748 status = ocfs2_flush_truncate_log(osb);
2755 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
2756 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
2759 if (osb->osb_tl_inode) {
2760 /* We want to push off log flushes while truncates are
2763 cancel_delayed_work(&osb->osb_truncate_log_wq);
2765 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
2766 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
2770 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
2772 struct inode **tl_inode,
2773 struct buffer_head **tl_bh)
2776 struct inode *inode = NULL;
2777 struct buffer_head *bh = NULL;
2779 inode = ocfs2_get_system_file_inode(osb,
2780 TRUNCATE_LOG_SYSTEM_INODE,
2784 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
2788 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
2789 OCFS2_BH_CACHED, inode);
2803 /* called during the 1st stage of node recovery. we stamp a clean
2804 * truncate log and pass back a copy for processing later. if the
2805 * truncate log does not require processing, a *tl_copy is set to
2807 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
2809 struct ocfs2_dinode **tl_copy)
2812 struct inode *tl_inode = NULL;
2813 struct buffer_head *tl_bh = NULL;
2814 struct ocfs2_dinode *di;
2815 struct ocfs2_truncate_log *tl;
2819 mlog(0, "recover truncate log from slot %d\n", slot_num);
2821 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
2827 di = (struct ocfs2_dinode *) tl_bh->b_data;
2828 tl = &di->id2.i_dealloc;
2829 if (!OCFS2_IS_VALID_DINODE(di)) {
2830 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
2835 if (le16_to_cpu(tl->tl_used)) {
2836 mlog(0, "We'll have %u logs to recover\n",
2837 le16_to_cpu(tl->tl_used));
2839 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
2846 /* Assuming the write-out below goes well, this copy
2847 * will be passed back to recovery for processing. */
2848 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
2850 /* All we need to do to clear the truncate log is set
2854 status = ocfs2_write_block(osb, tl_bh, tl_inode);
2867 if (status < 0 && (*tl_copy)) {
2876 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
2877 struct ocfs2_dinode *tl_copy)
2881 unsigned int clusters, num_recs, start_cluster;
2884 struct inode *tl_inode = osb->osb_tl_inode;
2885 struct ocfs2_truncate_log *tl;
2889 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
2890 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
2894 tl = &tl_copy->id2.i_dealloc;
2895 num_recs = le16_to_cpu(tl->tl_used);
2896 mlog(0, "cleanup %u records from %llu\n", num_recs,
2897 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
2899 mutex_lock(&tl_inode->i_mutex);
2900 for(i = 0; i < num_recs; i++) {
2901 if (ocfs2_truncate_log_needs_flush(osb)) {
2902 status = __ocfs2_flush_truncate_log(osb);
2909 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
2910 if (IS_ERR(handle)) {
2911 status = PTR_ERR(handle);
2916 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
2917 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
2918 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
2920 status = ocfs2_truncate_log_append(osb, handle,
2921 start_blk, clusters);
2922 ocfs2_commit_trans(osb, handle);
2930 mutex_unlock(&tl_inode->i_mutex);
2936 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
2939 struct inode *tl_inode = osb->osb_tl_inode;
2944 cancel_delayed_work(&osb->osb_truncate_log_wq);
2945 flush_workqueue(ocfs2_wq);
2947 status = ocfs2_flush_truncate_log(osb);
2951 brelse(osb->osb_tl_bh);
2952 iput(osb->osb_tl_inode);
2958 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
2961 struct inode *tl_inode = NULL;
2962 struct buffer_head *tl_bh = NULL;
2966 status = ocfs2_get_truncate_log_info(osb,
2973 /* ocfs2_truncate_log_shutdown keys on the existence of
2974 * osb->osb_tl_inode so we don't set any of the osb variables
2975 * until we're sure all is well. */
2976 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
2977 ocfs2_truncate_log_worker);
2978 osb->osb_tl_bh = tl_bh;
2979 osb->osb_tl_inode = tl_inode;
2986 * Delayed de-allocation of suballocator blocks.
2988 * Some sets of block de-allocations might involve multiple suballocator inodes.
2990 * The locking for this can get extremely complicated, especially when
2991 * the suballocator inodes to delete from aren't known until deep
2992 * within an unrelated codepath.
2994 * ocfs2_extent_block structures are a good example of this - an inode
2995 * btree could have been grown by any number of nodes each allocating
2996 * out of their own suballoc inode.
2998 * These structures allow the delay of block de-allocation until a
2999 * later time, when locking of multiple cluster inodes won't cause
3004 * Describes a single block free from a suballocator
3006 struct ocfs2_cached_block_free {
3007 struct ocfs2_cached_block_free *free_next;
3009 unsigned int free_bit;
3012 struct ocfs2_per_slot_free_list {
3013 struct ocfs2_per_slot_free_list *f_next_suballocator;
3016 struct ocfs2_cached_block_free *f_first;
3019 static int ocfs2_free_cached_items(struct ocfs2_super *osb,
3022 struct ocfs2_cached_block_free *head)
3027 struct inode *inode;
3028 struct buffer_head *di_bh = NULL;
3029 struct ocfs2_cached_block_free *tmp;
3031 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
3038 mutex_lock(&inode->i_mutex);
3040 ret = ocfs2_meta_lock(inode, &di_bh, 1);
3046 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
3047 if (IS_ERR(handle)) {
3048 ret = PTR_ERR(handle);
3054 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
3056 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
3057 head->free_bit, (unsigned long long)head->free_blk);
3059 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
3060 head->free_bit, bg_blkno, 1);
3066 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
3073 head = head->free_next;
3078 ocfs2_commit_trans(osb, handle);
3081 ocfs2_meta_unlock(inode, 1);
3084 mutex_unlock(&inode->i_mutex);
3088 /* Premature exit may have left some dangling items. */
3090 head = head->free_next;
3097 int ocfs2_run_deallocs(struct ocfs2_super *osb,
3098 struct ocfs2_cached_dealloc_ctxt *ctxt)
3101 struct ocfs2_per_slot_free_list *fl;
3106 while (ctxt->c_first_suballocator) {
3107 fl = ctxt->c_first_suballocator;
3110 mlog(0, "Free items: (type %u, slot %d)\n",
3111 fl->f_inode_type, fl->f_slot);
3112 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
3113 fl->f_slot, fl->f_first);
3120 ctxt->c_first_suballocator = fl->f_next_suballocator;
3127 static struct ocfs2_per_slot_free_list *
3128 ocfs2_find_per_slot_free_list(int type,
3130 struct ocfs2_cached_dealloc_ctxt *ctxt)
3132 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
3135 if (fl->f_inode_type == type && fl->f_slot == slot)
3138 fl = fl->f_next_suballocator;
3141 fl = kmalloc(sizeof(*fl), GFP_NOFS);
3143 fl->f_inode_type = type;
3146 fl->f_next_suballocator = ctxt->c_first_suballocator;
3148 ctxt->c_first_suballocator = fl;
3153 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
3154 int type, int slot, u64 blkno,
3158 struct ocfs2_per_slot_free_list *fl;
3159 struct ocfs2_cached_block_free *item;
3161 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
3168 item = kmalloc(sizeof(*item), GFP_NOFS);
3175 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
3176 type, slot, bit, (unsigned long long)blkno);
3178 item->free_blk = blkno;
3179 item->free_bit = bit;
3180 item->free_next = fl->f_first;
3189 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
3190 struct ocfs2_extent_block *eb)
3192 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
3193 le16_to_cpu(eb->h_suballoc_slot),
3194 le64_to_cpu(eb->h_blkno),
3195 le16_to_cpu(eb->h_suballoc_bit));
3198 /* This function will figure out whether the currently last extent
3199 * block will be deleted, and if it will, what the new last extent
3200 * block will be so we can update his h_next_leaf_blk field, as well
3201 * as the dinodes i_last_eb_blk */
3202 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
3203 unsigned int clusters_to_del,
3204 struct ocfs2_path *path,
3205 struct buffer_head **new_last_eb)
3207 int next_free, ret = 0;
3209 struct ocfs2_extent_rec *rec;
3210 struct ocfs2_extent_block *eb;
3211 struct ocfs2_extent_list *el;
3212 struct buffer_head *bh = NULL;
3214 *new_last_eb = NULL;
3216 /* we have no tree, so of course, no last_eb. */
3217 if (!path->p_tree_depth)
3220 /* trunc to zero special case - this makes tree_depth = 0
3221 * regardless of what it is. */
3222 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
3225 el = path_leaf_el(path);
3226 BUG_ON(!el->l_next_free_rec);
3229 * Make sure that this extent list will actually be empty
3230 * after we clear away the data. We can shortcut out if
3231 * there's more than one non-empty extent in the
3232 * list. Otherwise, a check of the remaining extent is
3235 next_free = le16_to_cpu(el->l_next_free_rec);
3237 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
3241 /* We may have a valid extent in index 1, check it. */
3243 rec = &el->l_recs[1];
3246 * Fall through - no more nonempty extents, so we want
3247 * to delete this leaf.
3253 rec = &el->l_recs[0];
3258 * Check it we'll only be trimming off the end of this
3261 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
3265 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
3271 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
3277 eb = (struct ocfs2_extent_block *) bh->b_data;
3279 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
3280 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
3286 get_bh(*new_last_eb);
3287 mlog(0, "returning block %llu, (cpos: %u)\n",
3288 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
3296 * Trim some clusters off the rightmost edge of a tree. Only called
3299 * The caller needs to:
3300 * - start journaling of each path component.
3301 * - compute and fully set up any new last ext block
3303 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
3304 handle_t *handle, struct ocfs2_truncate_context *tc,
3305 u32 clusters_to_del, u64 *delete_start)
3307 int ret, i, index = path->p_tree_depth;
3310 struct buffer_head *bh;
3311 struct ocfs2_extent_list *el;
3312 struct ocfs2_extent_rec *rec;
3316 while (index >= 0) {
3317 bh = path->p_node[index].bh;
3318 el = path->p_node[index].el;
3320 mlog(0, "traveling tree (index = %d, block = %llu)\n",
3321 index, (unsigned long long)bh->b_blocknr);
3323 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
3326 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
3327 ocfs2_error(inode->i_sb,
3328 "Inode %lu has invalid ext. block %llu",
3330 (unsigned long long)bh->b_blocknr);
3336 i = le16_to_cpu(el->l_next_free_rec) - 1;
3337 rec = &el->l_recs[i];
3339 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
3340 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
3341 ocfs2_rec_clusters(el, rec),
3342 (unsigned long long)le64_to_cpu(rec->e_blkno),
3343 le16_to_cpu(el->l_next_free_rec));
3345 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
3347 if (le16_to_cpu(el->l_tree_depth) == 0) {
3349 * If the leaf block contains a single empty
3350 * extent and no records, we can just remove
3353 if (i == 0 && ocfs2_is_empty_extent(rec)) {
3355 sizeof(struct ocfs2_extent_rec));
3356 el->l_next_free_rec = cpu_to_le16(0);
3362 * Remove any empty extents by shifting things
3363 * left. That should make life much easier on
3364 * the code below. This condition is rare
3365 * enough that we shouldn't see a performance
3368 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
3369 le16_add_cpu(&el->l_next_free_rec, -1);
3372 i < le16_to_cpu(el->l_next_free_rec); i++)
3373 el->l_recs[i] = el->l_recs[i + 1];
3375 memset(&el->l_recs[i], 0,
3376 sizeof(struct ocfs2_extent_rec));
3379 * We've modified our extent list. The
3380 * simplest way to handle this change
3381 * is to being the search from the
3384 goto find_tail_record;
3387 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
3390 * We'll use "new_edge" on our way back up the
3391 * tree to know what our rightmost cpos is.
3393 new_edge = le16_to_cpu(rec->e_leaf_clusters);
3394 new_edge += le32_to_cpu(rec->e_cpos);
3397 * The caller will use this to delete data blocks.
3399 *delete_start = le64_to_cpu(rec->e_blkno)
3400 + ocfs2_clusters_to_blocks(inode->i_sb,
3401 le16_to_cpu(rec->e_leaf_clusters));
3404 * If it's now empty, remove this record.
3406 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
3408 sizeof(struct ocfs2_extent_rec));
3409 le16_add_cpu(&el->l_next_free_rec, -1);
3412 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
3414 sizeof(struct ocfs2_extent_rec));
3415 le16_add_cpu(&el->l_next_free_rec, -1);
3420 /* Can this actually happen? */
3421 if (le16_to_cpu(el->l_next_free_rec) == 0)
3425 * We never actually deleted any clusters
3426 * because our leaf was empty. There's no
3427 * reason to adjust the rightmost edge then.
3432 rec->e_int_clusters = cpu_to_le32(new_edge);
3433 le32_add_cpu(&rec->e_int_clusters,
3434 -le32_to_cpu(rec->e_cpos));
3437 * A deleted child record should have been
3440 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
3444 ret = ocfs2_journal_dirty(handle, bh);
3450 mlog(0, "extent list container %llu, after: record %d: "
3451 "(%u, %u, %llu), next = %u.\n",
3452 (unsigned long long)bh->b_blocknr, i,
3453 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
3454 (unsigned long long)le64_to_cpu(rec->e_blkno),
3455 le16_to_cpu(el->l_next_free_rec));
3458 * We must be careful to only attempt delete of an
3459 * extent block (and not the root inode block).
3461 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
3462 struct ocfs2_extent_block *eb =
3463 (struct ocfs2_extent_block *)bh->b_data;
3466 * Save this for use when processing the
3469 deleted_eb = le64_to_cpu(eb->h_blkno);
3471 mlog(0, "deleting this extent block.\n");
3473 ocfs2_remove_from_cache(inode, bh);
3475 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
3476 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
3477 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
3479 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
3480 /* An error here is not fatal. */
3495 static int ocfs2_do_truncate(struct ocfs2_super *osb,
3496 unsigned int clusters_to_del,
3497 struct inode *inode,
3498 struct buffer_head *fe_bh,
3500 struct ocfs2_truncate_context *tc,
3501 struct ocfs2_path *path)
3504 struct ocfs2_dinode *fe;
3505 struct ocfs2_extent_block *last_eb = NULL;
3506 struct ocfs2_extent_list *el;
3507 struct buffer_head *last_eb_bh = NULL;
3510 fe = (struct ocfs2_dinode *) fe_bh->b_data;
3512 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
3520 * Each component will be touched, so we might as well journal
3521 * here to avoid having to handle errors later.
3523 status = ocfs2_journal_access_path(inode, handle, path);
3530 status = ocfs2_journal_access(handle, inode, last_eb_bh,
3531 OCFS2_JOURNAL_ACCESS_WRITE);
3537 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
3540 el = &(fe->id2.i_list);
3543 * Lower levels depend on this never happening, but it's best
3544 * to check it up here before changing the tree.
3546 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
3547 ocfs2_error(inode->i_sb,
3548 "Inode %lu has an empty extent record, depth %u\n",
3549 inode->i_ino, le16_to_cpu(el->l_tree_depth));
3554 spin_lock(&OCFS2_I(inode)->ip_lock);
3555 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
3557 spin_unlock(&OCFS2_I(inode)->ip_lock);
3558 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
3560 status = ocfs2_trim_tree(inode, path, handle, tc,
3561 clusters_to_del, &delete_blk);
3567 if (le32_to_cpu(fe->i_clusters) == 0) {
3568 /* trunc to zero is a special case. */
3569 el->l_tree_depth = 0;
3570 fe->i_last_eb_blk = 0;
3572 fe->i_last_eb_blk = last_eb->h_blkno;
3574 status = ocfs2_journal_dirty(handle, fe_bh);
3581 /* If there will be a new last extent block, then by
3582 * definition, there cannot be any leaves to the right of
3584 last_eb->h_next_leaf_blk = 0;
3585 status = ocfs2_journal_dirty(handle, last_eb_bh);
3593 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
3607 static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
3609 set_buffer_uptodate(bh);
3610 mark_buffer_dirty(bh);
3614 static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
3616 set_buffer_uptodate(bh);
3617 mark_buffer_dirty(bh);
3618 return ocfs2_journal_dirty_data(handle, bh);
3621 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t isize,
3622 struct page **pages, int numpages,
3623 u64 phys, handle_t *handle)
3625 int i, ret, partial = 0;
3628 unsigned int from, to = PAGE_CACHE_SIZE;
3629 struct super_block *sb = inode->i_sb;
3631 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
3636 from = isize & (PAGE_CACHE_SIZE - 1); /* 1st page offset */
3637 if (PAGE_CACHE_SHIFT > OCFS2_SB(sb)->s_clustersize_bits) {
3639 * Since 'from' has been capped to a value below page
3640 * size, this calculation won't be able to overflow
3643 to = ocfs2_align_bytes_to_clusters(sb, from);
3646 * The truncate tail in this case should never contain
3647 * more than one page at maximum. The loop below also
3650 BUG_ON(numpages != 1);
3653 for(i = 0; i < numpages; i++) {
3656 BUG_ON(from > PAGE_CACHE_SIZE);
3657 BUG_ON(to > PAGE_CACHE_SIZE);
3659 ret = ocfs2_map_page_blocks(page, &phys, inode, from, to, 0);
3663 kaddr = kmap_atomic(page, KM_USER0);
3664 memset(kaddr + from, 0, to - from);
3665 kunmap_atomic(kaddr, KM_USER0);
3668 * Need to set the buffers we zero'd into uptodate
3669 * here if they aren't - ocfs2_map_page_blocks()
3670 * might've skipped some
3672 if (ocfs2_should_order_data(inode)) {
3673 ret = walk_page_buffers(handle,
3676 ocfs2_ordered_zero_func);
3680 ret = walk_page_buffers(handle, page_buffers(page),
3682 ocfs2_writeback_zero_func);
3688 SetPageUptodate(page);
3690 flush_dcache_page(page);
3693 * Every page after the 1st one should be completely zero'd.
3699 for (i = 0; i < numpages; i++) {
3702 mark_page_accessed(page);
3703 page_cache_release(page);
3708 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t isize, struct page **pages,
3709 int *num, u64 *phys)
3711 int i, numpages = 0, ret = 0;
3712 unsigned int csize = OCFS2_SB(inode->i_sb)->s_clustersize;
3713 unsigned int ext_flags;
3714 struct super_block *sb = inode->i_sb;
3715 struct address_space *mapping = inode->i_mapping;
3716 unsigned long index;
3717 u64 next_cluster_bytes;
3719 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
3721 /* Cluster boundary, so we don't need to grab any pages. */
3722 if ((isize & (csize - 1)) == 0)
3725 ret = ocfs2_extent_map_get_blocks(inode, isize >> sb->s_blocksize_bits,
3726 phys, NULL, &ext_flags);
3732 /* Tail is a hole. */
3736 /* Tail is marked as unwritten, we can count on write to zero
3738 if (ext_flags & OCFS2_EXT_UNWRITTEN)
3741 next_cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, isize);
3742 index = isize >> PAGE_CACHE_SHIFT;
3744 pages[numpages] = grab_cache_page(mapping, index);
3745 if (!pages[numpages]) {
3753 } while (index < (next_cluster_bytes >> PAGE_CACHE_SHIFT));
3758 for (i = 0; i < numpages; i++) {
3760 unlock_page(pages[i]);
3761 page_cache_release(pages[i]);
3774 * Zero the area past i_size but still within an allocated
3775 * cluster. This avoids exposing nonzero data on subsequent file
3778 * We need to call this before i_size is updated on the inode because
3779 * otherwise block_write_full_page() will skip writeout of pages past
3780 * i_size. The new_i_size parameter is passed for this reason.
3782 int ocfs2_zero_tail_for_truncate(struct inode *inode, handle_t *handle,
3787 struct page **pages = NULL;
3791 * File systems which don't support sparse files zero on every
3794 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
3797 pages = kcalloc(ocfs2_pages_per_cluster(inode->i_sb),
3798 sizeof(struct page *), GFP_NOFS);
3799 if (pages == NULL) {
3805 ret = ocfs2_grab_eof_pages(inode, new_i_size, pages, &numpages, &phys);
3814 ocfs2_zero_cluster_pages(inode, new_i_size, pages, numpages, phys,
3818 * Initiate writeout of the pages we zero'd here. We don't
3819 * wait on them - the truncate_inode_pages() call later will
3822 endbyte = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
3823 ret = do_sync_mapping_range(inode->i_mapping, new_i_size,
3824 endbyte - 1, SYNC_FILE_RANGE_WRITE);
3836 * It is expected, that by the time you call this function,
3837 * inode->i_size and fe->i_size have been adjusted.
3839 * WARNING: This will kfree the truncate context
3841 int ocfs2_commit_truncate(struct ocfs2_super *osb,
3842 struct inode *inode,
3843 struct buffer_head *fe_bh,
3844 struct ocfs2_truncate_context *tc)
3846 int status, i, credits, tl_sem = 0;
3847 u32 clusters_to_del, new_highest_cpos, range;
3848 struct ocfs2_extent_list *el;
3849 handle_t *handle = NULL;
3850 struct inode *tl_inode = osb->osb_tl_inode;
3851 struct ocfs2_path *path = NULL;
3855 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
3856 i_size_read(inode));
3858 path = ocfs2_new_inode_path(fe_bh);
3865 ocfs2_extent_map_trunc(inode, new_highest_cpos);
3869 * Check that we still have allocation to delete.
3871 if (OCFS2_I(inode)->ip_clusters == 0) {
3877 * Truncate always works against the rightmost tree branch.
3879 status = ocfs2_find_path(inode, path, UINT_MAX);
3885 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
3886 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
3889 * By now, el will point to the extent list on the bottom most
3890 * portion of this tree. Only the tail record is considered in
3893 * We handle the following cases, in order:
3894 * - empty extent: delete the remaining branch
3895 * - remove the entire record
3896 * - remove a partial record
3897 * - no record needs to be removed (truncate has completed)
3899 el = path_leaf_el(path);
3900 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3901 ocfs2_error(inode->i_sb,
3902 "Inode %llu has empty extent block at %llu\n",
3903 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3904 (unsigned long long)path_leaf_bh(path)->b_blocknr);
3909 i = le16_to_cpu(el->l_next_free_rec) - 1;
3910 range = le32_to_cpu(el->l_recs[i].e_cpos) +
3911 ocfs2_rec_clusters(el, &el->l_recs[i]);
3912 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
3913 clusters_to_del = 0;
3914 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
3915 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
3916 } else if (range > new_highest_cpos) {
3917 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
3918 le32_to_cpu(el->l_recs[i].e_cpos)) -
3925 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
3926 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
3928 BUG_ON(clusters_to_del == 0);
3930 mutex_lock(&tl_inode->i_mutex);
3932 /* ocfs2_truncate_log_needs_flush guarantees us at least one
3933 * record is free for use. If there isn't any, we flush to get
3934 * an empty truncate log. */
3935 if (ocfs2_truncate_log_needs_flush(osb)) {
3936 status = __ocfs2_flush_truncate_log(osb);
3943 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
3944 (struct ocfs2_dinode *)fe_bh->b_data,
3946 handle = ocfs2_start_trans(osb, credits);
3947 if (IS_ERR(handle)) {
3948 status = PTR_ERR(handle);
3954 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
3961 mutex_unlock(&tl_inode->i_mutex);
3964 ocfs2_commit_trans(osb, handle);
3967 ocfs2_reinit_path(path, 1);
3970 * The check above will catch the case where we've truncated
3971 * away all allocation.
3977 ocfs2_schedule_truncate_log_flush(osb, 1);
3980 mutex_unlock(&tl_inode->i_mutex);
3983 ocfs2_commit_trans(osb, handle);
3985 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
3987 ocfs2_free_path(path);
3989 /* This will drop the ext_alloc cluster lock for us */
3990 ocfs2_free_truncate_context(tc);
3997 * Expects the inode to already be locked.
3999 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
4000 struct inode *inode,
4001 struct buffer_head *fe_bh,
4002 struct ocfs2_truncate_context **tc)
4005 unsigned int new_i_clusters;
4006 struct ocfs2_dinode *fe;
4007 struct ocfs2_extent_block *eb;
4008 struct buffer_head *last_eb_bh = NULL;
4014 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
4015 i_size_read(inode));
4016 fe = (struct ocfs2_dinode *) fe_bh->b_data;
4018 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
4019 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
4020 (unsigned long long)le64_to_cpu(fe->i_size));
4022 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
4028 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
4030 if (fe->id2.i_list.l_tree_depth) {
4031 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
4032 &last_eb_bh, OCFS2_BH_CACHED, inode);
4037 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4038 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4039 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4047 (*tc)->tc_last_eb_bh = last_eb_bh;
4053 ocfs2_free_truncate_context(*tc);
4060 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
4063 * The caller is responsible for completing deallocation
4064 * before freeing the context.
4066 if (tc->tc_dealloc.c_first_suballocator != NULL)
4068 "Truncate completion has non-empty dealloc context\n");
4070 if (tc->tc_last_eb_bh)
4071 brelse(tc->tc_last_eb_bh);