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 * This is only valid for leaf nodes, which are the only ones that can
829 * have empty extents anyway.
831 static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
833 return !rec->e_leaf_clusters;
837 * This function will discard the rightmost extent record.
839 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
841 int next_free = le16_to_cpu(el->l_next_free_rec);
842 int count = le16_to_cpu(el->l_count);
843 unsigned int num_bytes;
846 /* This will cause us to go off the end of our extent list. */
847 BUG_ON(next_free >= count);
849 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
851 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
854 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
855 struct ocfs2_extent_rec *insert_rec)
857 int i, insert_index, next_free, has_empty, num_bytes;
858 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
859 struct ocfs2_extent_rec *rec;
861 next_free = le16_to_cpu(el->l_next_free_rec);
862 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
866 /* The tree code before us didn't allow enough room in the leaf. */
867 if (el->l_next_free_rec == el->l_count && !has_empty)
871 * The easiest way to approach this is to just remove the
872 * empty extent and temporarily decrement next_free.
876 * If next_free was 1 (only an empty extent), this
877 * loop won't execute, which is fine. We still want
878 * the decrement above to happen.
880 for(i = 0; i < (next_free - 1); i++)
881 el->l_recs[i] = el->l_recs[i+1];
887 * Figure out what the new record index should be.
889 for(i = 0; i < next_free; i++) {
890 rec = &el->l_recs[i];
892 if (insert_cpos < le32_to_cpu(rec->e_cpos))
897 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
898 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
900 BUG_ON(insert_index < 0);
901 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
902 BUG_ON(insert_index > next_free);
905 * No need to memmove if we're just adding to the tail.
907 if (insert_index != next_free) {
908 BUG_ON(next_free >= le16_to_cpu(el->l_count));
910 num_bytes = next_free - insert_index;
911 num_bytes *= sizeof(struct ocfs2_extent_rec);
912 memmove(&el->l_recs[insert_index + 1],
913 &el->l_recs[insert_index],
918 * Either we had an empty extent, and need to re-increment or
919 * there was no empty extent on a non full rightmost leaf node,
920 * in which case we still need to increment.
923 el->l_next_free_rec = cpu_to_le16(next_free);
925 * Make sure none of the math above just messed up our tree.
927 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
929 el->l_recs[insert_index] = *insert_rec;
934 * Create an empty extent record .
936 * l_next_free_rec may be updated.
938 * If an empty extent already exists do nothing.
940 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
942 int next_free = le16_to_cpu(el->l_next_free_rec);
944 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
949 if (ocfs2_is_empty_extent(&el->l_recs[0]))
952 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
953 "Asked to create an empty extent in a full list:\n"
954 "count = %u, tree depth = %u",
955 le16_to_cpu(el->l_count),
956 le16_to_cpu(el->l_tree_depth));
958 ocfs2_shift_records_right(el);
961 le16_add_cpu(&el->l_next_free_rec, 1);
962 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
966 * For a rotation which involves two leaf nodes, the "root node" is
967 * the lowest level tree node which contains a path to both leafs. This
968 * resulting set of information can be used to form a complete "subtree"
970 * This function is passed two full paths from the dinode down to a
971 * pair of adjacent leaves. It's task is to figure out which path
972 * index contains the subtree root - this can be the root index itself
973 * in a worst-case rotation.
975 * The array index of the subtree root is passed back.
977 static int ocfs2_find_subtree_root(struct inode *inode,
978 struct ocfs2_path *left,
979 struct ocfs2_path *right)
984 * Check that the caller passed in two paths from the same tree.
986 BUG_ON(path_root_bh(left) != path_root_bh(right));
992 * The caller didn't pass two adjacent paths.
994 mlog_bug_on_msg(i > left->p_tree_depth,
995 "Inode %lu, left depth %u, right depth %u\n"
996 "left leaf blk %llu, right leaf blk %llu\n",
997 inode->i_ino, left->p_tree_depth,
999 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1000 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1001 } while (left->p_node[i].bh->b_blocknr ==
1002 right->p_node[i].bh->b_blocknr);
1007 typedef void (path_insert_t)(void *, struct buffer_head *);
1010 * Traverse a btree path in search of cpos, starting at root_el.
1012 * This code can be called with a cpos larger than the tree, in which
1013 * case it will return the rightmost path.
1015 static int __ocfs2_find_path(struct inode *inode,
1016 struct ocfs2_extent_list *root_el, u32 cpos,
1017 path_insert_t *func, void *data)
1022 struct buffer_head *bh = NULL;
1023 struct ocfs2_extent_block *eb;
1024 struct ocfs2_extent_list *el;
1025 struct ocfs2_extent_rec *rec;
1026 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1029 while (el->l_tree_depth) {
1030 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1031 ocfs2_error(inode->i_sb,
1032 "Inode %llu has empty extent list at "
1034 (unsigned long long)oi->ip_blkno,
1035 le16_to_cpu(el->l_tree_depth));
1041 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1042 rec = &el->l_recs[i];
1045 * In the case that cpos is off the allocation
1046 * tree, this should just wind up returning the
1049 range = le32_to_cpu(rec->e_cpos) +
1050 ocfs2_rec_clusters(el, rec);
1051 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1055 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1057 ocfs2_error(inode->i_sb,
1058 "Inode %llu has bad blkno in extent list "
1059 "at depth %u (index %d)\n",
1060 (unsigned long long)oi->ip_blkno,
1061 le16_to_cpu(el->l_tree_depth), i);
1068 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1069 &bh, OCFS2_BH_CACHED, inode);
1075 eb = (struct ocfs2_extent_block *) bh->b_data;
1077 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1078 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1083 if (le16_to_cpu(el->l_next_free_rec) >
1084 le16_to_cpu(el->l_count)) {
1085 ocfs2_error(inode->i_sb,
1086 "Inode %llu has bad count in extent list "
1087 "at block %llu (next free=%u, count=%u)\n",
1088 (unsigned long long)oi->ip_blkno,
1089 (unsigned long long)bh->b_blocknr,
1090 le16_to_cpu(el->l_next_free_rec),
1091 le16_to_cpu(el->l_count));
1102 * Catch any trailing bh that the loop didn't handle.
1110 * Given an initialized path (that is, it has a valid root extent
1111 * list), this function will traverse the btree in search of the path
1112 * which would contain cpos.
1114 * The path traveled is recorded in the path structure.
1116 * Note that this will not do any comparisons on leaf node extent
1117 * records, so it will work fine in the case that we just added a tree
1120 struct find_path_data {
1122 struct ocfs2_path *path;
1124 static void find_path_ins(void *data, struct buffer_head *bh)
1126 struct find_path_data *fp = data;
1129 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1132 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1135 struct find_path_data data;
1139 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1140 find_path_ins, &data);
1143 static void find_leaf_ins(void *data, struct buffer_head *bh)
1145 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1146 struct ocfs2_extent_list *el = &eb->h_list;
1147 struct buffer_head **ret = data;
1149 /* We want to retain only the leaf block. */
1150 if (le16_to_cpu(el->l_tree_depth) == 0) {
1156 * Find the leaf block in the tree which would contain cpos. No
1157 * checking of the actual leaf is done.
1159 * Some paths want to call this instead of allocating a path structure
1160 * and calling ocfs2_find_path().
1162 * This function doesn't handle non btree extent lists.
1164 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1165 u32 cpos, struct buffer_head **leaf_bh)
1168 struct buffer_head *bh = NULL;
1170 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1182 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1184 * Basically, we've moved stuff around at the bottom of the tree and
1185 * we need to fix up the extent records above the changes to reflect
1188 * left_rec: the record on the left.
1189 * left_child_el: is the child list pointed to by left_rec
1190 * right_rec: the record to the right of left_rec
1191 * right_child_el: is the child list pointed to by right_rec
1193 * By definition, this only works on interior nodes.
1195 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1196 struct ocfs2_extent_list *left_child_el,
1197 struct ocfs2_extent_rec *right_rec,
1198 struct ocfs2_extent_list *right_child_el)
1200 u32 left_clusters, right_end;
1203 * Interior nodes never have holes. Their cpos is the cpos of
1204 * the leftmost record in their child list. Their cluster
1205 * count covers the full theoretical range of their child list
1206 * - the range between their cpos and the cpos of the record
1207 * immediately to their right.
1209 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1210 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1211 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1214 * Calculate the rightmost cluster count boundary before
1215 * moving cpos - we will need to adjust clusters after
1216 * updating e_cpos to keep the same highest cluster count.
1218 right_end = le32_to_cpu(right_rec->e_cpos);
1219 right_end += le32_to_cpu(right_rec->e_int_clusters);
1221 right_rec->e_cpos = left_rec->e_cpos;
1222 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1224 right_end -= le32_to_cpu(right_rec->e_cpos);
1225 right_rec->e_int_clusters = cpu_to_le32(right_end);
1229 * Adjust the adjacent root node records involved in a
1230 * rotation. left_el_blkno is passed in as a key so that we can easily
1231 * find it's index in the root list.
1233 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1234 struct ocfs2_extent_list *left_el,
1235 struct ocfs2_extent_list *right_el,
1240 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1241 le16_to_cpu(left_el->l_tree_depth));
1243 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1244 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1249 * The path walking code should have never returned a root and
1250 * two paths which are not adjacent.
1252 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1254 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1255 &root_el->l_recs[i + 1], right_el);
1259 * We've changed a leaf block (in right_path) and need to reflect that
1260 * change back up the subtree.
1262 * This happens in multiple places:
1263 * - When we've moved an extent record from the left path leaf to the right
1264 * path leaf to make room for an empty extent in the left path leaf.
1265 * - When our insert into the right path leaf is at the leftmost edge
1266 * and requires an update of the path immediately to it's left. This
1267 * can occur at the end of some types of rotation and appending inserts.
1269 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1270 struct ocfs2_path *left_path,
1271 struct ocfs2_path *right_path,
1275 struct ocfs2_extent_list *el, *left_el, *right_el;
1276 struct ocfs2_extent_rec *left_rec, *right_rec;
1277 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1280 * Update the counts and position values within all the
1281 * interior nodes to reflect the leaf rotation we just did.
1283 * The root node is handled below the loop.
1285 * We begin the loop with right_el and left_el pointing to the
1286 * leaf lists and work our way up.
1288 * NOTE: within this loop, left_el and right_el always refer
1289 * to the *child* lists.
1291 left_el = path_leaf_el(left_path);
1292 right_el = path_leaf_el(right_path);
1293 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1294 mlog(0, "Adjust records at index %u\n", i);
1297 * One nice property of knowing that all of these
1298 * nodes are below the root is that we only deal with
1299 * the leftmost right node record and the rightmost
1302 el = left_path->p_node[i].el;
1303 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1304 left_rec = &el->l_recs[idx];
1306 el = right_path->p_node[i].el;
1307 right_rec = &el->l_recs[0];
1309 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1312 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1316 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1321 * Setup our list pointers now so that the current
1322 * parents become children in the next iteration.
1324 left_el = left_path->p_node[i].el;
1325 right_el = right_path->p_node[i].el;
1329 * At the root node, adjust the two adjacent records which
1330 * begin our path to the leaves.
1333 el = left_path->p_node[subtree_index].el;
1334 left_el = left_path->p_node[subtree_index + 1].el;
1335 right_el = right_path->p_node[subtree_index + 1].el;
1337 ocfs2_adjust_root_records(el, left_el, right_el,
1338 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1340 root_bh = left_path->p_node[subtree_index].bh;
1342 ret = ocfs2_journal_dirty(handle, root_bh);
1347 static int ocfs2_rotate_subtree_right(struct inode *inode,
1349 struct ocfs2_path *left_path,
1350 struct ocfs2_path *right_path,
1354 struct buffer_head *right_leaf_bh;
1355 struct buffer_head *left_leaf_bh = NULL;
1356 struct buffer_head *root_bh;
1357 struct ocfs2_extent_list *right_el, *left_el;
1358 struct ocfs2_extent_rec move_rec;
1360 left_leaf_bh = path_leaf_bh(left_path);
1361 left_el = path_leaf_el(left_path);
1363 if (left_el->l_next_free_rec != left_el->l_count) {
1364 ocfs2_error(inode->i_sb,
1365 "Inode %llu has non-full interior leaf node %llu"
1367 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1368 (unsigned long long)left_leaf_bh->b_blocknr,
1369 le16_to_cpu(left_el->l_next_free_rec));
1374 * This extent block may already have an empty record, so we
1375 * return early if so.
1377 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1380 root_bh = left_path->p_node[subtree_index].bh;
1381 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1383 ret = ocfs2_journal_access(handle, inode, root_bh,
1384 OCFS2_JOURNAL_ACCESS_WRITE);
1390 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1391 ret = ocfs2_journal_access(handle, inode,
1392 right_path->p_node[i].bh,
1393 OCFS2_JOURNAL_ACCESS_WRITE);
1399 ret = ocfs2_journal_access(handle, inode,
1400 left_path->p_node[i].bh,
1401 OCFS2_JOURNAL_ACCESS_WRITE);
1408 right_leaf_bh = path_leaf_bh(right_path);
1409 right_el = path_leaf_el(right_path);
1411 /* This is a code error, not a disk corruption. */
1412 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1413 "because rightmost leaf block %llu is empty\n",
1414 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1415 (unsigned long long)right_leaf_bh->b_blocknr);
1417 ocfs2_create_empty_extent(right_el);
1419 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1425 /* Do the copy now. */
1426 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1427 move_rec = left_el->l_recs[i];
1428 right_el->l_recs[0] = move_rec;
1431 * Clear out the record we just copied and shift everything
1432 * over, leaving an empty extent in the left leaf.
1434 * We temporarily subtract from next_free_rec so that the
1435 * shift will lose the tail record (which is now defunct).
1437 le16_add_cpu(&left_el->l_next_free_rec, -1);
1438 ocfs2_shift_records_right(left_el);
1439 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1440 le16_add_cpu(&left_el->l_next_free_rec, 1);
1442 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1448 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1456 * Given a full path, determine what cpos value would return us a path
1457 * containing the leaf immediately to the left of the current one.
1459 * Will return zero if the path passed in is already the leftmost path.
1461 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1462 struct ocfs2_path *path, u32 *cpos)
1466 struct ocfs2_extent_list *el;
1468 BUG_ON(path->p_tree_depth == 0);
1472 blkno = path_leaf_bh(path)->b_blocknr;
1474 /* Start at the tree node just above the leaf and work our way up. */
1475 i = path->p_tree_depth - 1;
1477 el = path->p_node[i].el;
1480 * Find the extent record just before the one in our
1483 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1484 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1488 * We've determined that the
1489 * path specified is already
1490 * the leftmost one - return a
1496 * The leftmost record points to our
1497 * leaf - we need to travel up the
1503 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
1504 *cpos = *cpos + ocfs2_rec_clusters(el,
1505 &el->l_recs[j - 1]);
1512 * If we got here, we never found a valid node where
1513 * the tree indicated one should be.
1516 "Invalid extent tree at extent block %llu\n",
1517 (unsigned long long)blkno);
1522 blkno = path->p_node[i].bh->b_blocknr;
1530 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
1531 struct ocfs2_path *path)
1533 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1;
1535 if (handle->h_buffer_credits < credits)
1536 return ocfs2_extend_trans(handle, credits);
1542 * Trap the case where we're inserting into the theoretical range past
1543 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1544 * whose cpos is less than ours into the right leaf.
1546 * It's only necessary to look at the rightmost record of the left
1547 * leaf because the logic that calls us should ensure that the
1548 * theoretical ranges in the path components above the leaves are
1551 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1554 struct ocfs2_extent_list *left_el;
1555 struct ocfs2_extent_rec *rec;
1558 left_el = path_leaf_el(left_path);
1559 next_free = le16_to_cpu(left_el->l_next_free_rec);
1560 rec = &left_el->l_recs[next_free - 1];
1562 if (insert_cpos > le32_to_cpu(rec->e_cpos))
1568 * Rotate all the records in a btree right one record, starting at insert_cpos.
1570 * The path to the rightmost leaf should be passed in.
1572 * The array is assumed to be large enough to hold an entire path (tree depth).
1574 * Upon succesful return from this function:
1576 * - The 'right_path' array will contain a path to the leaf block
1577 * whose range contains e_cpos.
1578 * - That leaf block will have a single empty extent in list index 0.
1579 * - In the case that the rotation requires a post-insert update,
1580 * *ret_left_path will contain a valid path which can be passed to
1581 * ocfs2_insert_path().
1583 static int ocfs2_rotate_tree_right(struct inode *inode,
1586 struct ocfs2_path *right_path,
1587 struct ocfs2_path **ret_left_path)
1591 struct ocfs2_path *left_path = NULL;
1593 *ret_left_path = NULL;
1595 left_path = ocfs2_new_path(path_root_bh(right_path),
1596 path_root_el(right_path));
1603 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
1609 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
1612 * What we want to do here is:
1614 * 1) Start with the rightmost path.
1616 * 2) Determine a path to the leaf block directly to the left
1619 * 3) Determine the 'subtree root' - the lowest level tree node
1620 * which contains a path to both leaves.
1622 * 4) Rotate the subtree.
1624 * 5) Find the next subtree by considering the left path to be
1625 * the new right path.
1627 * The check at the top of this while loop also accepts
1628 * insert_cpos == cpos because cpos is only a _theoretical_
1629 * value to get us the left path - insert_cpos might very well
1630 * be filling that hole.
1632 * Stop at a cpos of '0' because we either started at the
1633 * leftmost branch (i.e., a tree with one branch and a
1634 * rotation inside of it), or we've gone as far as we can in
1635 * rotating subtrees.
1637 while (cpos && insert_cpos <= cpos) {
1638 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
1641 ret = ocfs2_find_path(inode, left_path, cpos);
1647 mlog_bug_on_msg(path_leaf_bh(left_path) ==
1648 path_leaf_bh(right_path),
1649 "Inode %lu: error during insert of %u "
1650 "(left path cpos %u) results in two identical "
1651 "paths ending at %llu\n",
1652 inode->i_ino, insert_cpos, cpos,
1653 (unsigned long long)
1654 path_leaf_bh(left_path)->b_blocknr);
1656 if (ocfs2_rotate_requires_path_adjustment(left_path,
1658 mlog(0, "Path adjustment required\n");
1661 * We've rotated the tree as much as we
1662 * should. The rest is up to
1663 * ocfs2_insert_path() to complete, after the
1664 * record insertion. We indicate this
1665 * situation by returning the left path.
1667 * The reason we don't adjust the records here
1668 * before the record insert is that an error
1669 * later might break the rule where a parent
1670 * record e_cpos will reflect the actual
1671 * e_cpos of the 1st nonempty record of the
1674 *ret_left_path = left_path;
1678 start = ocfs2_find_subtree_root(inode, left_path, right_path);
1680 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
1682 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
1683 right_path->p_tree_depth);
1685 ret = ocfs2_extend_rotate_transaction(handle, start,
1692 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
1700 * There is no need to re-read the next right path
1701 * as we know that it'll be our current left
1702 * path. Optimize by copying values instead.
1704 ocfs2_mv_path(right_path, left_path);
1706 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
1715 ocfs2_free_path(left_path);
1722 * Do the final bits of extent record insertion at the target leaf
1723 * list. If this leaf is part of an allocation tree, it is assumed
1724 * that the tree above has been prepared.
1726 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
1727 struct ocfs2_extent_list *el,
1728 struct ocfs2_insert_type *insert,
1729 struct inode *inode)
1731 int i = insert->ins_contig_index;
1733 struct ocfs2_extent_rec *rec;
1735 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1738 * Contiguous insert - either left or right.
1740 if (insert->ins_contig != CONTIG_NONE) {
1741 rec = &el->l_recs[i];
1742 if (insert->ins_contig == CONTIG_LEFT) {
1743 rec->e_blkno = insert_rec->e_blkno;
1744 rec->e_cpos = insert_rec->e_cpos;
1746 le16_add_cpu(&rec->e_leaf_clusters,
1747 le16_to_cpu(insert_rec->e_leaf_clusters));
1752 * Handle insert into an empty leaf.
1754 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
1755 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
1756 ocfs2_is_empty_extent(&el->l_recs[0]))) {
1757 el->l_recs[0] = *insert_rec;
1758 el->l_next_free_rec = cpu_to_le16(1);
1765 if (insert->ins_appending == APPEND_TAIL) {
1766 i = le16_to_cpu(el->l_next_free_rec) - 1;
1767 rec = &el->l_recs[i];
1768 range = le32_to_cpu(rec->e_cpos)
1769 + le16_to_cpu(rec->e_leaf_clusters);
1770 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
1772 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
1773 le16_to_cpu(el->l_count),
1774 "inode %lu, depth %u, count %u, next free %u, "
1775 "rec.cpos %u, rec.clusters %u, "
1776 "insert.cpos %u, insert.clusters %u\n",
1778 le16_to_cpu(el->l_tree_depth),
1779 le16_to_cpu(el->l_count),
1780 le16_to_cpu(el->l_next_free_rec),
1781 le32_to_cpu(el->l_recs[i].e_cpos),
1782 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
1783 le32_to_cpu(insert_rec->e_cpos),
1784 le16_to_cpu(insert_rec->e_leaf_clusters));
1786 el->l_recs[i] = *insert_rec;
1787 le16_add_cpu(&el->l_next_free_rec, 1);
1792 * Ok, we have to rotate.
1794 * At this point, it is safe to assume that inserting into an
1795 * empty leaf and appending to a leaf have both been handled
1798 * This leaf needs to have space, either by the empty 1st
1799 * extent record, or by virtue of an l_next_rec < l_count.
1801 ocfs2_rotate_leaf(el, insert_rec);
1804 static inline void ocfs2_update_dinode_clusters(struct inode *inode,
1805 struct ocfs2_dinode *di,
1808 le32_add_cpu(&di->i_clusters, clusters);
1809 spin_lock(&OCFS2_I(inode)->ip_lock);
1810 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
1811 spin_unlock(&OCFS2_I(inode)->ip_lock);
1814 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
1815 struct ocfs2_extent_rec *insert_rec,
1816 struct ocfs2_path *right_path,
1817 struct ocfs2_path **ret_left_path)
1819 int ret, i, next_free;
1820 struct buffer_head *bh;
1821 struct ocfs2_extent_list *el;
1822 struct ocfs2_path *left_path = NULL;
1824 *ret_left_path = NULL;
1827 * This shouldn't happen for non-trees. The extent rec cluster
1828 * count manipulation below only works for interior nodes.
1830 BUG_ON(right_path->p_tree_depth == 0);
1833 * If our appending insert is at the leftmost edge of a leaf,
1834 * then we might need to update the rightmost records of the
1837 el = path_leaf_el(right_path);
1838 next_free = le16_to_cpu(el->l_next_free_rec);
1839 if (next_free == 0 ||
1840 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
1843 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
1850 mlog(0, "Append may need a left path update. cpos: %u, "
1851 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
1855 * No need to worry if the append is already in the
1859 left_path = ocfs2_new_path(path_root_bh(right_path),
1860 path_root_el(right_path));
1867 ret = ocfs2_find_path(inode, left_path, left_cpos);
1874 * ocfs2_insert_path() will pass the left_path to the
1880 ret = ocfs2_journal_access_path(inode, handle, right_path);
1886 el = path_root_el(right_path);
1887 bh = path_root_bh(right_path);
1890 struct ocfs2_extent_rec *rec;
1892 next_free = le16_to_cpu(el->l_next_free_rec);
1893 if (next_free == 0) {
1894 ocfs2_error(inode->i_sb,
1895 "Dinode %llu has a bad extent list",
1896 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1901 rec = &el->l_recs[next_free - 1];
1903 rec->e_int_clusters = insert_rec->e_cpos;
1904 le32_add_cpu(&rec->e_int_clusters,
1905 le16_to_cpu(insert_rec->e_leaf_clusters));
1906 le32_add_cpu(&rec->e_int_clusters,
1907 -le32_to_cpu(rec->e_cpos));
1909 ret = ocfs2_journal_dirty(handle, bh);
1913 /* Don't touch the leaf node */
1914 if (++i >= right_path->p_tree_depth)
1917 bh = right_path->p_node[i].bh;
1918 el = right_path->p_node[i].el;
1921 *ret_left_path = left_path;
1925 ocfs2_free_path(left_path);
1931 * This function only does inserts on an allocation b-tree. For dinode
1932 * lists, ocfs2_insert_at_leaf() is called directly.
1934 * right_path is the path we want to do the actual insert
1935 * in. left_path should only be passed in if we need to update that
1936 * portion of the tree after an edge insert.
1938 static int ocfs2_insert_path(struct inode *inode,
1940 struct ocfs2_path *left_path,
1941 struct ocfs2_path *right_path,
1942 struct ocfs2_extent_rec *insert_rec,
1943 struct ocfs2_insert_type *insert)
1945 int ret, subtree_index;
1946 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
1947 struct ocfs2_extent_list *el;
1950 * Pass both paths to the journal. The majority of inserts
1951 * will be touching all components anyway.
1953 ret = ocfs2_journal_access_path(inode, handle, right_path);
1960 int credits = handle->h_buffer_credits;
1963 * There's a chance that left_path got passed back to
1964 * us without being accounted for in the
1965 * journal. Extend our transaction here to be sure we
1966 * can change those blocks.
1968 credits += left_path->p_tree_depth;
1970 ret = ocfs2_extend_trans(handle, credits);
1976 ret = ocfs2_journal_access_path(inode, handle, left_path);
1983 el = path_leaf_el(right_path);
1985 ocfs2_insert_at_leaf(insert_rec, el, insert, inode);
1986 ret = ocfs2_journal_dirty(handle, leaf_bh);
1992 * The rotate code has indicated that we need to fix
1993 * up portions of the tree after the insert.
1995 * XXX: Should we extend the transaction here?
1997 subtree_index = ocfs2_find_subtree_root(inode, left_path,
1999 ocfs2_complete_edge_insert(inode, handle, left_path,
2000 right_path, subtree_index);
2008 static int ocfs2_do_insert_extent(struct inode *inode,
2010 struct buffer_head *di_bh,
2011 struct ocfs2_extent_rec *insert_rec,
2012 struct ocfs2_insert_type *type)
2014 int ret, rotate = 0;
2016 struct ocfs2_path *right_path = NULL;
2017 struct ocfs2_path *left_path = NULL;
2018 struct ocfs2_dinode *di;
2019 struct ocfs2_extent_list *el;
2021 di = (struct ocfs2_dinode *) di_bh->b_data;
2022 el = &di->id2.i_list;
2024 ret = ocfs2_journal_access(handle, inode, di_bh,
2025 OCFS2_JOURNAL_ACCESS_WRITE);
2031 if (le16_to_cpu(el->l_tree_depth) == 0) {
2032 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
2033 goto out_update_clusters;
2036 right_path = ocfs2_new_inode_path(di_bh);
2044 * Determine the path to start with. Rotations need the
2045 * rightmost path, everything else can go directly to the
2048 cpos = le32_to_cpu(insert_rec->e_cpos);
2049 if (type->ins_appending == APPEND_NONE &&
2050 type->ins_contig == CONTIG_NONE) {
2055 ret = ocfs2_find_path(inode, right_path, cpos);
2062 * Rotations and appends need special treatment - they modify
2063 * parts of the tree's above them.
2065 * Both might pass back a path immediate to the left of the
2066 * one being inserted to. This will be cause
2067 * ocfs2_insert_path() to modify the rightmost records of
2068 * left_path to account for an edge insert.
2070 * XXX: When modifying this code, keep in mind that an insert
2071 * can wind up skipping both of these two special cases...
2074 ret = ocfs2_rotate_tree_right(inode, handle,
2075 le32_to_cpu(insert_rec->e_cpos),
2076 right_path, &left_path);
2081 } else if (type->ins_appending == APPEND_TAIL
2082 && type->ins_contig != CONTIG_LEFT) {
2083 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
2084 right_path, &left_path);
2091 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
2098 out_update_clusters:
2099 ocfs2_update_dinode_clusters(inode, di,
2100 le16_to_cpu(insert_rec->e_leaf_clusters));
2102 ret = ocfs2_journal_dirty(handle, di_bh);
2107 ocfs2_free_path(left_path);
2108 ocfs2_free_path(right_path);
2113 static void ocfs2_figure_contig_type(struct inode *inode,
2114 struct ocfs2_insert_type *insert,
2115 struct ocfs2_extent_list *el,
2116 struct ocfs2_extent_rec *insert_rec)
2119 enum ocfs2_contig_type contig_type = CONTIG_NONE;
2121 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
2123 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
2124 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
2126 if (contig_type != CONTIG_NONE) {
2127 insert->ins_contig_index = i;
2131 insert->ins_contig = contig_type;
2135 * This should only be called against the righmost leaf extent list.
2137 * ocfs2_figure_appending_type() will figure out whether we'll have to
2138 * insert at the tail of the rightmost leaf.
2140 * This should also work against the dinode list for tree's with 0
2141 * depth. If we consider the dinode list to be the rightmost leaf node
2142 * then the logic here makes sense.
2144 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
2145 struct ocfs2_extent_list *el,
2146 struct ocfs2_extent_rec *insert_rec)
2149 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
2150 struct ocfs2_extent_rec *rec;
2152 insert->ins_appending = APPEND_NONE;
2154 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
2156 if (!el->l_next_free_rec)
2157 goto set_tail_append;
2159 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
2160 /* Were all records empty? */
2161 if (le16_to_cpu(el->l_next_free_rec) == 1)
2162 goto set_tail_append;
2165 i = le16_to_cpu(el->l_next_free_rec) - 1;
2166 rec = &el->l_recs[i];
2169 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
2170 goto set_tail_append;
2175 insert->ins_appending = APPEND_TAIL;
2179 * Helper function called at the begining of an insert.
2181 * This computes a few things that are commonly used in the process of
2182 * inserting into the btree:
2183 * - Whether the new extent is contiguous with an existing one.
2184 * - The current tree depth.
2185 * - Whether the insert is an appending one.
2186 * - The total # of free records in the tree.
2188 * All of the information is stored on the ocfs2_insert_type
2191 static int ocfs2_figure_insert_type(struct inode *inode,
2192 struct buffer_head *di_bh,
2193 struct buffer_head **last_eb_bh,
2194 struct ocfs2_extent_rec *insert_rec,
2195 struct ocfs2_insert_type *insert)
2198 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2199 struct ocfs2_extent_block *eb;
2200 struct ocfs2_extent_list *el;
2201 struct ocfs2_path *path = NULL;
2202 struct buffer_head *bh = NULL;
2204 el = &di->id2.i_list;
2205 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
2207 if (el->l_tree_depth) {
2209 * If we have tree depth, we read in the
2210 * rightmost extent block ahead of time as
2211 * ocfs2_figure_insert_type() and ocfs2_add_branch()
2212 * may want it later.
2214 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2215 le64_to_cpu(di->i_last_eb_blk), &bh,
2216 OCFS2_BH_CACHED, inode);
2221 eb = (struct ocfs2_extent_block *) bh->b_data;
2226 * Unless we have a contiguous insert, we'll need to know if
2227 * there is room left in our allocation tree for another
2230 * XXX: This test is simplistic, we can search for empty
2231 * extent records too.
2233 insert->ins_free_records = le16_to_cpu(el->l_count) -
2234 le16_to_cpu(el->l_next_free_rec);
2236 if (!insert->ins_tree_depth) {
2237 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
2238 ocfs2_figure_appending_type(insert, el, insert_rec);
2242 path = ocfs2_new_inode_path(di_bh);
2250 * In the case that we're inserting past what the tree
2251 * currently accounts for, ocfs2_find_path() will return for
2252 * us the rightmost tree path. This is accounted for below in
2253 * the appending code.
2255 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
2261 el = path_leaf_el(path);
2264 * Now that we have the path, there's two things we want to determine:
2265 * 1) Contiguousness (also set contig_index if this is so)
2267 * 2) Are we doing an append? We can trivially break this up
2268 * into two types of appends: simple record append, or a
2269 * rotate inside the tail leaf.
2271 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
2274 * The insert code isn't quite ready to deal with all cases of
2275 * left contiguousness. Specifically, if it's an insert into
2276 * the 1st record in a leaf, it will require the adjustment of
2277 * cluster count on the last record of the path directly to it's
2278 * left. For now, just catch that case and fool the layers
2279 * above us. This works just fine for tree_depth == 0, which
2280 * is why we allow that above.
2282 if (insert->ins_contig == CONTIG_LEFT &&
2283 insert->ins_contig_index == 0)
2284 insert->ins_contig = CONTIG_NONE;
2287 * Ok, so we can simply compare against last_eb to figure out
2288 * whether the path doesn't exist. This will only happen in
2289 * the case that we're doing a tail append, so maybe we can
2290 * take advantage of that information somehow.
2292 if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) {
2294 * Ok, ocfs2_find_path() returned us the rightmost
2295 * tree path. This might be an appending insert. There are
2297 * 1) We're doing a true append at the tail:
2298 * -This might even be off the end of the leaf
2299 * 2) We're "appending" by rotating in the tail
2301 ocfs2_figure_appending_type(insert, el, insert_rec);
2305 ocfs2_free_path(path);
2315 * Insert an extent into an inode btree.
2317 * The caller needs to update fe->i_clusters
2319 int ocfs2_insert_extent(struct ocfs2_super *osb,
2321 struct inode *inode,
2322 struct buffer_head *fe_bh,
2326 struct ocfs2_alloc_context *meta_ac)
2329 struct buffer_head *last_eb_bh = NULL;
2330 struct buffer_head *bh = NULL;
2331 struct ocfs2_insert_type insert = {0, };
2332 struct ocfs2_extent_rec rec;
2334 mlog(0, "add %u clusters at position %u to inode %llu\n",
2335 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
2337 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
2338 (OCFS2_I(inode)->ip_clusters != cpos),
2339 "Device %s, asking for sparse allocation: inode %llu, "
2340 "cpos %u, clusters %u\n",
2342 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
2343 OCFS2_I(inode)->ip_clusters);
2345 memset(&rec, 0, sizeof(rec));
2346 rec.e_cpos = cpu_to_le32(cpos);
2347 rec.e_blkno = cpu_to_le64(start_blk);
2348 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
2350 status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec,
2357 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
2358 "Insert.contig_index: %d, Insert.free_records: %d, "
2359 "Insert.tree_depth: %d\n",
2360 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
2361 insert.ins_free_records, insert.ins_tree_depth);
2364 * Avoid growing the tree unless we're out of records and the
2365 * insert type requres one.
2367 if (insert.ins_contig != CONTIG_NONE || insert.ins_free_records)
2370 shift = ocfs2_find_branch_target(osb, inode, fe_bh, &bh);
2377 /* We traveled all the way to the bottom of the allocation tree
2378 * and didn't find room for any more extents - we need to add
2379 * another tree level */
2382 mlog(0, "need to shift tree depth "
2383 "(current = %d)\n", insert.ins_tree_depth);
2385 /* ocfs2_shift_tree_depth will return us a buffer with
2386 * the new extent block (so we can pass that to
2387 * ocfs2_add_branch). */
2388 status = ocfs2_shift_tree_depth(osb, handle, inode, fe_bh,
2394 insert.ins_tree_depth++;
2395 /* Special case: we have room now if we shifted from
2397 if (insert.ins_tree_depth == 1)
2401 /* call ocfs2_add_branch to add the final part of the tree with
2403 mlog(0, "add branch. bh = %p\n", bh);
2404 status = ocfs2_add_branch(osb, handle, inode, fe_bh, bh, last_eb_bh,
2412 /* Finally, we can add clusters. This might rotate the tree for us. */
2413 status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert);
2417 ocfs2_extent_map_insert_rec(inode, &rec);
2430 static inline int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
2432 struct buffer_head *tl_bh = osb->osb_tl_bh;
2433 struct ocfs2_dinode *di;
2434 struct ocfs2_truncate_log *tl;
2436 di = (struct ocfs2_dinode *) tl_bh->b_data;
2437 tl = &di->id2.i_dealloc;
2439 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
2440 "slot %d, invalid truncate log parameters: used = "
2441 "%u, count = %u\n", osb->slot_num,
2442 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
2443 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
2446 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
2447 unsigned int new_start)
2449 unsigned int tail_index;
2450 unsigned int current_tail;
2452 /* No records, nothing to coalesce */
2453 if (!le16_to_cpu(tl->tl_used))
2456 tail_index = le16_to_cpu(tl->tl_used) - 1;
2457 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
2458 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
2460 return current_tail == new_start;
2463 static int ocfs2_truncate_log_append(struct ocfs2_super *osb,
2466 unsigned int num_clusters)
2469 unsigned int start_cluster, tl_count;
2470 struct inode *tl_inode = osb->osb_tl_inode;
2471 struct buffer_head *tl_bh = osb->osb_tl_bh;
2472 struct ocfs2_dinode *di;
2473 struct ocfs2_truncate_log *tl;
2475 mlog_entry("start_blk = %llu, num_clusters = %u\n",
2476 (unsigned long long)start_blk, num_clusters);
2478 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
2480 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
2482 di = (struct ocfs2_dinode *) tl_bh->b_data;
2483 tl = &di->id2.i_dealloc;
2484 if (!OCFS2_IS_VALID_DINODE(di)) {
2485 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
2490 tl_count = le16_to_cpu(tl->tl_count);
2491 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
2493 "Truncate record count on #%llu invalid "
2494 "wanted %u, actual %u\n",
2495 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
2496 ocfs2_truncate_recs_per_inode(osb->sb),
2497 le16_to_cpu(tl->tl_count));
2499 /* Caller should have known to flush before calling us. */
2500 index = le16_to_cpu(tl->tl_used);
2501 if (index >= tl_count) {
2507 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
2508 OCFS2_JOURNAL_ACCESS_WRITE);
2514 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
2515 "%llu (index = %d)\n", num_clusters, start_cluster,
2516 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
2518 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
2520 * Move index back to the record we are coalescing with.
2521 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
2525 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
2526 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
2527 index, le32_to_cpu(tl->tl_recs[index].t_start),
2530 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
2531 tl->tl_used = cpu_to_le16(index + 1);
2533 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
2535 status = ocfs2_journal_dirty(handle, tl_bh);
2546 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
2548 struct inode *data_alloc_inode,
2549 struct buffer_head *data_alloc_bh)
2553 unsigned int num_clusters;
2555 struct ocfs2_truncate_rec rec;
2556 struct ocfs2_dinode *di;
2557 struct ocfs2_truncate_log *tl;
2558 struct inode *tl_inode = osb->osb_tl_inode;
2559 struct buffer_head *tl_bh = osb->osb_tl_bh;
2563 di = (struct ocfs2_dinode *) tl_bh->b_data;
2564 tl = &di->id2.i_dealloc;
2565 i = le16_to_cpu(tl->tl_used) - 1;
2567 /* Caller has given us at least enough credits to
2568 * update the truncate log dinode */
2569 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
2570 OCFS2_JOURNAL_ACCESS_WRITE);
2576 tl->tl_used = cpu_to_le16(i);
2578 status = ocfs2_journal_dirty(handle, tl_bh);
2584 /* TODO: Perhaps we can calculate the bulk of the
2585 * credits up front rather than extending like
2587 status = ocfs2_extend_trans(handle,
2588 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
2594 rec = tl->tl_recs[i];
2595 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
2596 le32_to_cpu(rec.t_start));
2597 num_clusters = le32_to_cpu(rec.t_clusters);
2599 /* if start_blk is not set, we ignore the record as
2602 mlog(0, "free record %d, start = %u, clusters = %u\n",
2603 i, le32_to_cpu(rec.t_start), num_clusters);
2605 status = ocfs2_free_clusters(handle, data_alloc_inode,
2606 data_alloc_bh, start_blk,
2621 /* Expects you to already be holding tl_inode->i_mutex */
2622 static int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
2625 unsigned int num_to_flush;
2627 struct inode *tl_inode = osb->osb_tl_inode;
2628 struct inode *data_alloc_inode = NULL;
2629 struct buffer_head *tl_bh = osb->osb_tl_bh;
2630 struct buffer_head *data_alloc_bh = NULL;
2631 struct ocfs2_dinode *di;
2632 struct ocfs2_truncate_log *tl;
2636 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
2638 di = (struct ocfs2_dinode *) tl_bh->b_data;
2639 tl = &di->id2.i_dealloc;
2640 if (!OCFS2_IS_VALID_DINODE(di)) {
2641 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
2646 num_to_flush = le16_to_cpu(tl->tl_used);
2647 mlog(0, "Flush %u records from truncate log #%llu\n",
2648 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
2649 if (!num_to_flush) {
2654 data_alloc_inode = ocfs2_get_system_file_inode(osb,
2655 GLOBAL_BITMAP_SYSTEM_INODE,
2656 OCFS2_INVALID_SLOT);
2657 if (!data_alloc_inode) {
2659 mlog(ML_ERROR, "Could not get bitmap inode!\n");
2663 mutex_lock(&data_alloc_inode->i_mutex);
2665 status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1);
2671 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
2672 if (IS_ERR(handle)) {
2673 status = PTR_ERR(handle);
2678 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
2683 ocfs2_commit_trans(osb, handle);
2686 brelse(data_alloc_bh);
2687 ocfs2_meta_unlock(data_alloc_inode, 1);
2690 mutex_unlock(&data_alloc_inode->i_mutex);
2691 iput(data_alloc_inode);
2698 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
2701 struct inode *tl_inode = osb->osb_tl_inode;
2703 mutex_lock(&tl_inode->i_mutex);
2704 status = __ocfs2_flush_truncate_log(osb);
2705 mutex_unlock(&tl_inode->i_mutex);
2710 static void ocfs2_truncate_log_worker(struct work_struct *work)
2713 struct ocfs2_super *osb =
2714 container_of(work, struct ocfs2_super,
2715 osb_truncate_log_wq.work);
2719 status = ocfs2_flush_truncate_log(osb);
2726 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
2727 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
2730 if (osb->osb_tl_inode) {
2731 /* We want to push off log flushes while truncates are
2734 cancel_delayed_work(&osb->osb_truncate_log_wq);
2736 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
2737 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
2741 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
2743 struct inode **tl_inode,
2744 struct buffer_head **tl_bh)
2747 struct inode *inode = NULL;
2748 struct buffer_head *bh = NULL;
2750 inode = ocfs2_get_system_file_inode(osb,
2751 TRUNCATE_LOG_SYSTEM_INODE,
2755 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
2759 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
2760 OCFS2_BH_CACHED, inode);
2774 /* called during the 1st stage of node recovery. we stamp a clean
2775 * truncate log and pass back a copy for processing later. if the
2776 * truncate log does not require processing, a *tl_copy is set to
2778 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
2780 struct ocfs2_dinode **tl_copy)
2783 struct inode *tl_inode = NULL;
2784 struct buffer_head *tl_bh = NULL;
2785 struct ocfs2_dinode *di;
2786 struct ocfs2_truncate_log *tl;
2790 mlog(0, "recover truncate log from slot %d\n", slot_num);
2792 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
2798 di = (struct ocfs2_dinode *) tl_bh->b_data;
2799 tl = &di->id2.i_dealloc;
2800 if (!OCFS2_IS_VALID_DINODE(di)) {
2801 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
2806 if (le16_to_cpu(tl->tl_used)) {
2807 mlog(0, "We'll have %u logs to recover\n",
2808 le16_to_cpu(tl->tl_used));
2810 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
2817 /* Assuming the write-out below goes well, this copy
2818 * will be passed back to recovery for processing. */
2819 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
2821 /* All we need to do to clear the truncate log is set
2825 status = ocfs2_write_block(osb, tl_bh, tl_inode);
2838 if (status < 0 && (*tl_copy)) {
2847 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
2848 struct ocfs2_dinode *tl_copy)
2852 unsigned int clusters, num_recs, start_cluster;
2855 struct inode *tl_inode = osb->osb_tl_inode;
2856 struct ocfs2_truncate_log *tl;
2860 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
2861 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
2865 tl = &tl_copy->id2.i_dealloc;
2866 num_recs = le16_to_cpu(tl->tl_used);
2867 mlog(0, "cleanup %u records from %llu\n", num_recs,
2868 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
2870 mutex_lock(&tl_inode->i_mutex);
2871 for(i = 0; i < num_recs; i++) {
2872 if (ocfs2_truncate_log_needs_flush(osb)) {
2873 status = __ocfs2_flush_truncate_log(osb);
2880 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
2881 if (IS_ERR(handle)) {
2882 status = PTR_ERR(handle);
2887 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
2888 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
2889 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
2891 status = ocfs2_truncate_log_append(osb, handle,
2892 start_blk, clusters);
2893 ocfs2_commit_trans(osb, handle);
2901 mutex_unlock(&tl_inode->i_mutex);
2907 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
2910 struct inode *tl_inode = osb->osb_tl_inode;
2915 cancel_delayed_work(&osb->osb_truncate_log_wq);
2916 flush_workqueue(ocfs2_wq);
2918 status = ocfs2_flush_truncate_log(osb);
2922 brelse(osb->osb_tl_bh);
2923 iput(osb->osb_tl_inode);
2929 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
2932 struct inode *tl_inode = NULL;
2933 struct buffer_head *tl_bh = NULL;
2937 status = ocfs2_get_truncate_log_info(osb,
2944 /* ocfs2_truncate_log_shutdown keys on the existence of
2945 * osb->osb_tl_inode so we don't set any of the osb variables
2946 * until we're sure all is well. */
2947 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
2948 ocfs2_truncate_log_worker);
2949 osb->osb_tl_bh = tl_bh;
2950 osb->osb_tl_inode = tl_inode;
2957 * Delayed de-allocation of suballocator blocks.
2959 * Some sets of block de-allocations might involve multiple suballocator inodes.
2961 * The locking for this can get extremely complicated, especially when
2962 * the suballocator inodes to delete from aren't known until deep
2963 * within an unrelated codepath.
2965 * ocfs2_extent_block structures are a good example of this - an inode
2966 * btree could have been grown by any number of nodes each allocating
2967 * out of their own suballoc inode.
2969 * These structures allow the delay of block de-allocation until a
2970 * later time, when locking of multiple cluster inodes won't cause
2975 * Describes a single block free from a suballocator
2977 struct ocfs2_cached_block_free {
2978 struct ocfs2_cached_block_free *free_next;
2980 unsigned int free_bit;
2983 struct ocfs2_per_slot_free_list {
2984 struct ocfs2_per_slot_free_list *f_next_suballocator;
2987 struct ocfs2_cached_block_free *f_first;
2990 static int ocfs2_free_cached_items(struct ocfs2_super *osb,
2993 struct ocfs2_cached_block_free *head)
2998 struct inode *inode;
2999 struct buffer_head *di_bh = NULL;
3000 struct ocfs2_cached_block_free *tmp;
3002 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
3009 mutex_lock(&inode->i_mutex);
3011 ret = ocfs2_meta_lock(inode, &di_bh, 1);
3017 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
3018 if (IS_ERR(handle)) {
3019 ret = PTR_ERR(handle);
3025 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
3027 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
3028 head->free_bit, (unsigned long long)head->free_blk);
3030 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
3031 head->free_bit, bg_blkno, 1);
3037 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
3044 head = head->free_next;
3049 ocfs2_commit_trans(osb, handle);
3052 ocfs2_meta_unlock(inode, 1);
3055 mutex_unlock(&inode->i_mutex);
3059 /* Premature exit may have left some dangling items. */
3061 head = head->free_next;
3068 int ocfs2_run_deallocs(struct ocfs2_super *osb,
3069 struct ocfs2_cached_dealloc_ctxt *ctxt)
3072 struct ocfs2_per_slot_free_list *fl;
3077 while (ctxt->c_first_suballocator) {
3078 fl = ctxt->c_first_suballocator;
3081 mlog(0, "Free items: (type %u, slot %d)\n",
3082 fl->f_inode_type, fl->f_slot);
3083 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
3084 fl->f_slot, fl->f_first);
3091 ctxt->c_first_suballocator = fl->f_next_suballocator;
3098 static struct ocfs2_per_slot_free_list *
3099 ocfs2_find_per_slot_free_list(int type,
3101 struct ocfs2_cached_dealloc_ctxt *ctxt)
3103 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
3106 if (fl->f_inode_type == type && fl->f_slot == slot)
3109 fl = fl->f_next_suballocator;
3112 fl = kmalloc(sizeof(*fl), GFP_NOFS);
3114 fl->f_inode_type = type;
3117 fl->f_next_suballocator = ctxt->c_first_suballocator;
3119 ctxt->c_first_suballocator = fl;
3124 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
3125 int type, int slot, u64 blkno,
3129 struct ocfs2_per_slot_free_list *fl;
3130 struct ocfs2_cached_block_free *item;
3132 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
3139 item = kmalloc(sizeof(*item), GFP_NOFS);
3146 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
3147 type, slot, bit, (unsigned long long)blkno);
3149 item->free_blk = blkno;
3150 item->free_bit = bit;
3151 item->free_next = fl->f_first;
3160 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
3161 struct ocfs2_extent_block *eb)
3163 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
3164 le16_to_cpu(eb->h_suballoc_slot),
3165 le64_to_cpu(eb->h_blkno),
3166 le16_to_cpu(eb->h_suballoc_bit));
3169 /* This function will figure out whether the currently last extent
3170 * block will be deleted, and if it will, what the new last extent
3171 * block will be so we can update his h_next_leaf_blk field, as well
3172 * as the dinodes i_last_eb_blk */
3173 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
3174 unsigned int clusters_to_del,
3175 struct ocfs2_path *path,
3176 struct buffer_head **new_last_eb)
3178 int next_free, ret = 0;
3180 struct ocfs2_extent_rec *rec;
3181 struct ocfs2_extent_block *eb;
3182 struct ocfs2_extent_list *el;
3183 struct buffer_head *bh = NULL;
3185 *new_last_eb = NULL;
3187 /* we have no tree, so of course, no last_eb. */
3188 if (!path->p_tree_depth)
3191 /* trunc to zero special case - this makes tree_depth = 0
3192 * regardless of what it is. */
3193 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
3196 el = path_leaf_el(path);
3197 BUG_ON(!el->l_next_free_rec);
3200 * Make sure that this extent list will actually be empty
3201 * after we clear away the data. We can shortcut out if
3202 * there's more than one non-empty extent in the
3203 * list. Otherwise, a check of the remaining extent is
3206 next_free = le16_to_cpu(el->l_next_free_rec);
3208 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
3212 /* We may have a valid extent in index 1, check it. */
3214 rec = &el->l_recs[1];
3217 * Fall through - no more nonempty extents, so we want
3218 * to delete this leaf.
3224 rec = &el->l_recs[0];
3229 * Check it we'll only be trimming off the end of this
3232 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
3236 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
3242 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
3248 eb = (struct ocfs2_extent_block *) bh->b_data;
3250 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
3251 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
3257 get_bh(*new_last_eb);
3258 mlog(0, "returning block %llu, (cpos: %u)\n",
3259 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
3267 * Trim some clusters off the rightmost edge of a tree. Only called
3270 * The caller needs to:
3271 * - start journaling of each path component.
3272 * - compute and fully set up any new last ext block
3274 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
3275 handle_t *handle, struct ocfs2_truncate_context *tc,
3276 u32 clusters_to_del, u64 *delete_start)
3278 int ret, i, index = path->p_tree_depth;
3281 struct buffer_head *bh;
3282 struct ocfs2_extent_list *el;
3283 struct ocfs2_extent_rec *rec;
3287 while (index >= 0) {
3288 bh = path->p_node[index].bh;
3289 el = path->p_node[index].el;
3291 mlog(0, "traveling tree (index = %d, block = %llu)\n",
3292 index, (unsigned long long)bh->b_blocknr);
3294 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
3297 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
3298 ocfs2_error(inode->i_sb,
3299 "Inode %lu has invalid ext. block %llu",
3301 (unsigned long long)bh->b_blocknr);
3307 i = le16_to_cpu(el->l_next_free_rec) - 1;
3308 rec = &el->l_recs[i];
3310 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
3311 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
3312 ocfs2_rec_clusters(el, rec),
3313 (unsigned long long)le64_to_cpu(rec->e_blkno),
3314 le16_to_cpu(el->l_next_free_rec));
3316 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
3318 if (le16_to_cpu(el->l_tree_depth) == 0) {
3320 * If the leaf block contains a single empty
3321 * extent and no records, we can just remove
3324 if (i == 0 && ocfs2_is_empty_extent(rec)) {
3326 sizeof(struct ocfs2_extent_rec));
3327 el->l_next_free_rec = cpu_to_le16(0);
3333 * Remove any empty extents by shifting things
3334 * left. That should make life much easier on
3335 * the code below. This condition is rare
3336 * enough that we shouldn't see a performance
3339 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
3340 le16_add_cpu(&el->l_next_free_rec, -1);
3343 i < le16_to_cpu(el->l_next_free_rec); i++)
3344 el->l_recs[i] = el->l_recs[i + 1];
3346 memset(&el->l_recs[i], 0,
3347 sizeof(struct ocfs2_extent_rec));
3350 * We've modified our extent list. The
3351 * simplest way to handle this change
3352 * is to being the search from the
3355 goto find_tail_record;
3358 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
3361 * We'll use "new_edge" on our way back up the
3362 * tree to know what our rightmost cpos is.
3364 new_edge = le16_to_cpu(rec->e_leaf_clusters);
3365 new_edge += le32_to_cpu(rec->e_cpos);
3368 * The caller will use this to delete data blocks.
3370 *delete_start = le64_to_cpu(rec->e_blkno)
3371 + ocfs2_clusters_to_blocks(inode->i_sb,
3372 le16_to_cpu(rec->e_leaf_clusters));
3375 * If it's now empty, remove this record.
3377 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
3379 sizeof(struct ocfs2_extent_rec));
3380 le16_add_cpu(&el->l_next_free_rec, -1);
3383 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
3385 sizeof(struct ocfs2_extent_rec));
3386 le16_add_cpu(&el->l_next_free_rec, -1);
3391 /* Can this actually happen? */
3392 if (le16_to_cpu(el->l_next_free_rec) == 0)
3396 * We never actually deleted any clusters
3397 * because our leaf was empty. There's no
3398 * reason to adjust the rightmost edge then.
3403 rec->e_int_clusters = cpu_to_le32(new_edge);
3404 le32_add_cpu(&rec->e_int_clusters,
3405 -le32_to_cpu(rec->e_cpos));
3408 * A deleted child record should have been
3411 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
3415 ret = ocfs2_journal_dirty(handle, bh);
3421 mlog(0, "extent list container %llu, after: record %d: "
3422 "(%u, %u, %llu), next = %u.\n",
3423 (unsigned long long)bh->b_blocknr, i,
3424 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
3425 (unsigned long long)le64_to_cpu(rec->e_blkno),
3426 le16_to_cpu(el->l_next_free_rec));
3429 * We must be careful to only attempt delete of an
3430 * extent block (and not the root inode block).
3432 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
3433 struct ocfs2_extent_block *eb =
3434 (struct ocfs2_extent_block *)bh->b_data;
3437 * Save this for use when processing the
3440 deleted_eb = le64_to_cpu(eb->h_blkno);
3442 mlog(0, "deleting this extent block.\n");
3444 ocfs2_remove_from_cache(inode, bh);
3446 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
3447 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
3448 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
3450 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
3451 /* An error here is not fatal. */
3466 static int ocfs2_do_truncate(struct ocfs2_super *osb,
3467 unsigned int clusters_to_del,
3468 struct inode *inode,
3469 struct buffer_head *fe_bh,
3471 struct ocfs2_truncate_context *tc,
3472 struct ocfs2_path *path)
3475 struct ocfs2_dinode *fe;
3476 struct ocfs2_extent_block *last_eb = NULL;
3477 struct ocfs2_extent_list *el;
3478 struct buffer_head *last_eb_bh = NULL;
3481 fe = (struct ocfs2_dinode *) fe_bh->b_data;
3483 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
3491 * Each component will be touched, so we might as well journal
3492 * here to avoid having to handle errors later.
3494 status = ocfs2_journal_access_path(inode, handle, path);
3501 status = ocfs2_journal_access(handle, inode, last_eb_bh,
3502 OCFS2_JOURNAL_ACCESS_WRITE);
3508 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
3511 el = &(fe->id2.i_list);
3514 * Lower levels depend on this never happening, but it's best
3515 * to check it up here before changing the tree.
3517 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
3518 ocfs2_error(inode->i_sb,
3519 "Inode %lu has an empty extent record, depth %u\n",
3520 inode->i_ino, le16_to_cpu(el->l_tree_depth));
3525 spin_lock(&OCFS2_I(inode)->ip_lock);
3526 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
3528 spin_unlock(&OCFS2_I(inode)->ip_lock);
3529 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
3531 status = ocfs2_trim_tree(inode, path, handle, tc,
3532 clusters_to_del, &delete_blk);
3538 if (le32_to_cpu(fe->i_clusters) == 0) {
3539 /* trunc to zero is a special case. */
3540 el->l_tree_depth = 0;
3541 fe->i_last_eb_blk = 0;
3543 fe->i_last_eb_blk = last_eb->h_blkno;
3545 status = ocfs2_journal_dirty(handle, fe_bh);
3552 /* If there will be a new last extent block, then by
3553 * definition, there cannot be any leaves to the right of
3555 last_eb->h_next_leaf_blk = 0;
3556 status = ocfs2_journal_dirty(handle, last_eb_bh);
3564 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
3578 static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
3580 set_buffer_uptodate(bh);
3581 mark_buffer_dirty(bh);
3585 static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
3587 set_buffer_uptodate(bh);
3588 mark_buffer_dirty(bh);
3589 return ocfs2_journal_dirty_data(handle, bh);
3592 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t isize,
3593 struct page **pages, int numpages,
3594 u64 phys, handle_t *handle)
3596 int i, ret, partial = 0;
3599 unsigned int from, to = PAGE_CACHE_SIZE;
3600 struct super_block *sb = inode->i_sb;
3602 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
3607 from = isize & (PAGE_CACHE_SIZE - 1); /* 1st page offset */
3608 if (PAGE_CACHE_SHIFT > OCFS2_SB(sb)->s_clustersize_bits) {
3610 * Since 'from' has been capped to a value below page
3611 * size, this calculation won't be able to overflow
3614 to = ocfs2_align_bytes_to_clusters(sb, from);
3617 * The truncate tail in this case should never contain
3618 * more than one page at maximum. The loop below also
3621 BUG_ON(numpages != 1);
3624 for(i = 0; i < numpages; i++) {
3627 BUG_ON(from > PAGE_CACHE_SIZE);
3628 BUG_ON(to > PAGE_CACHE_SIZE);
3630 ret = ocfs2_map_page_blocks(page, &phys, inode, from, to, 0);
3634 kaddr = kmap_atomic(page, KM_USER0);
3635 memset(kaddr + from, 0, to - from);
3636 kunmap_atomic(kaddr, KM_USER0);
3639 * Need to set the buffers we zero'd into uptodate
3640 * here if they aren't - ocfs2_map_page_blocks()
3641 * might've skipped some
3643 if (ocfs2_should_order_data(inode)) {
3644 ret = walk_page_buffers(handle,
3647 ocfs2_ordered_zero_func);
3651 ret = walk_page_buffers(handle, page_buffers(page),
3653 ocfs2_writeback_zero_func);
3659 SetPageUptodate(page);
3661 flush_dcache_page(page);
3664 * Every page after the 1st one should be completely zero'd.
3670 for (i = 0; i < numpages; i++) {
3673 mark_page_accessed(page);
3674 page_cache_release(page);
3679 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t isize, struct page **pages,
3680 int *num, u64 *phys)
3682 int i, numpages = 0, ret = 0;
3683 unsigned int csize = OCFS2_SB(inode->i_sb)->s_clustersize;
3684 unsigned int ext_flags;
3685 struct super_block *sb = inode->i_sb;
3686 struct address_space *mapping = inode->i_mapping;
3687 unsigned long index;
3688 u64 next_cluster_bytes;
3690 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
3692 /* Cluster boundary, so we don't need to grab any pages. */
3693 if ((isize & (csize - 1)) == 0)
3696 ret = ocfs2_extent_map_get_blocks(inode, isize >> sb->s_blocksize_bits,
3697 phys, NULL, &ext_flags);
3703 /* Tail is a hole. */
3707 /* Tail is marked as unwritten, we can count on write to zero
3709 if (ext_flags & OCFS2_EXT_UNWRITTEN)
3712 next_cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, isize);
3713 index = isize >> PAGE_CACHE_SHIFT;
3715 pages[numpages] = grab_cache_page(mapping, index);
3716 if (!pages[numpages]) {
3724 } while (index < (next_cluster_bytes >> PAGE_CACHE_SHIFT));
3729 for (i = 0; i < numpages; i++) {
3731 unlock_page(pages[i]);
3732 page_cache_release(pages[i]);
3745 * Zero the area past i_size but still within an allocated
3746 * cluster. This avoids exposing nonzero data on subsequent file
3749 * We need to call this before i_size is updated on the inode because
3750 * otherwise block_write_full_page() will skip writeout of pages past
3751 * i_size. The new_i_size parameter is passed for this reason.
3753 int ocfs2_zero_tail_for_truncate(struct inode *inode, handle_t *handle,
3758 struct page **pages = NULL;
3762 * File systems which don't support sparse files zero on every
3765 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
3768 pages = kcalloc(ocfs2_pages_per_cluster(inode->i_sb),
3769 sizeof(struct page *), GFP_NOFS);
3770 if (pages == NULL) {
3776 ret = ocfs2_grab_eof_pages(inode, new_i_size, pages, &numpages, &phys);
3785 ocfs2_zero_cluster_pages(inode, new_i_size, pages, numpages, phys,
3789 * Initiate writeout of the pages we zero'd here. We don't
3790 * wait on them - the truncate_inode_pages() call later will
3793 endbyte = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
3794 ret = do_sync_mapping_range(inode->i_mapping, new_i_size,
3795 endbyte - 1, SYNC_FILE_RANGE_WRITE);
3807 * It is expected, that by the time you call this function,
3808 * inode->i_size and fe->i_size have been adjusted.
3810 * WARNING: This will kfree the truncate context
3812 int ocfs2_commit_truncate(struct ocfs2_super *osb,
3813 struct inode *inode,
3814 struct buffer_head *fe_bh,
3815 struct ocfs2_truncate_context *tc)
3817 int status, i, credits, tl_sem = 0;
3818 u32 clusters_to_del, new_highest_cpos, range;
3819 struct ocfs2_extent_list *el;
3820 handle_t *handle = NULL;
3821 struct inode *tl_inode = osb->osb_tl_inode;
3822 struct ocfs2_path *path = NULL;
3826 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
3827 i_size_read(inode));
3829 path = ocfs2_new_inode_path(fe_bh);
3836 ocfs2_extent_map_trunc(inode, new_highest_cpos);
3840 * Check that we still have allocation to delete.
3842 if (OCFS2_I(inode)->ip_clusters == 0) {
3848 * Truncate always works against the rightmost tree branch.
3850 status = ocfs2_find_path(inode, path, UINT_MAX);
3856 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
3857 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
3860 * By now, el will point to the extent list on the bottom most
3861 * portion of this tree. Only the tail record is considered in
3864 * We handle the following cases, in order:
3865 * - empty extent: delete the remaining branch
3866 * - remove the entire record
3867 * - remove a partial record
3868 * - no record needs to be removed (truncate has completed)
3870 el = path_leaf_el(path);
3871 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3872 ocfs2_error(inode->i_sb,
3873 "Inode %llu has empty extent block at %llu\n",
3874 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3875 (unsigned long long)path_leaf_bh(path)->b_blocknr);
3880 i = le16_to_cpu(el->l_next_free_rec) - 1;
3881 range = le32_to_cpu(el->l_recs[i].e_cpos) +
3882 ocfs2_rec_clusters(el, &el->l_recs[i]);
3883 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
3884 clusters_to_del = 0;
3885 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
3886 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
3887 } else if (range > new_highest_cpos) {
3888 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
3889 le32_to_cpu(el->l_recs[i].e_cpos)) -
3896 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
3897 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
3899 BUG_ON(clusters_to_del == 0);
3901 mutex_lock(&tl_inode->i_mutex);
3903 /* ocfs2_truncate_log_needs_flush guarantees us at least one
3904 * record is free for use. If there isn't any, we flush to get
3905 * an empty truncate log. */
3906 if (ocfs2_truncate_log_needs_flush(osb)) {
3907 status = __ocfs2_flush_truncate_log(osb);
3914 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
3915 (struct ocfs2_dinode *)fe_bh->b_data,
3917 handle = ocfs2_start_trans(osb, credits);
3918 if (IS_ERR(handle)) {
3919 status = PTR_ERR(handle);
3925 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
3932 mutex_unlock(&tl_inode->i_mutex);
3935 ocfs2_commit_trans(osb, handle);
3938 ocfs2_reinit_path(path, 1);
3941 * The check above will catch the case where we've truncated
3942 * away all allocation.
3948 ocfs2_schedule_truncate_log_flush(osb, 1);
3951 mutex_unlock(&tl_inode->i_mutex);
3954 ocfs2_commit_trans(osb, handle);
3956 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
3958 ocfs2_free_path(path);
3960 /* This will drop the ext_alloc cluster lock for us */
3961 ocfs2_free_truncate_context(tc);
3968 * Expects the inode to already be locked.
3970 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
3971 struct inode *inode,
3972 struct buffer_head *fe_bh,
3973 struct ocfs2_truncate_context **tc)
3976 unsigned int new_i_clusters;
3977 struct ocfs2_dinode *fe;
3978 struct ocfs2_extent_block *eb;
3979 struct buffer_head *last_eb_bh = NULL;
3985 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
3986 i_size_read(inode));
3987 fe = (struct ocfs2_dinode *) fe_bh->b_data;
3989 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
3990 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
3991 (unsigned long long)le64_to_cpu(fe->i_size));
3993 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
3999 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
4001 if (fe->id2.i_list.l_tree_depth) {
4002 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
4003 &last_eb_bh, OCFS2_BH_CACHED, inode);
4008 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4009 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4010 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4018 (*tc)->tc_last_eb_bh = last_eb_bh;
4024 ocfs2_free_truncate_context(*tc);
4031 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
4034 * The caller is responsible for completing deallocation
4035 * before freeing the context.
4037 if (tc->tc_dealloc.c_first_suballocator != NULL)
4039 "Truncate completion has non-empty dealloc context\n");
4041 if (tc->tc_last_eb_bh)
4042 brelse(tc->tc_last_eb_bh);