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"
53 * ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract
54 * the b-tree operations in ocfs2. Now all the b-tree operations are not
55 * limited to ocfs2_dinode only. Any data which need to allocate clusters
56 * to store can use b-tree. And it only needs to implement its ocfs2_extent_tree
59 * ocfs2_extent_tree contains info for the root of the b-tree, it must have a
60 * root ocfs2_extent_list and a root_bh so that they can be used in the b-tree
62 * ocfs2_extent_tree_operations abstract the normal operations we do for
63 * the root of extent b-tree.
65 struct ocfs2_extent_tree;
67 struct ocfs2_extent_tree_operations {
68 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
70 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
71 void (*eo_update_clusters)(struct inode *inode,
72 struct ocfs2_extent_tree *et,
74 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
77 struct ocfs2_extent_tree {
78 enum ocfs2_extent_tree_type et_type;
79 struct ocfs2_extent_tree_operations *et_ops;
80 struct buffer_head *et_root_bh;
81 struct ocfs2_extent_list *et_root_el;
83 unsigned int et_max_leaf_clusters;
86 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
89 struct ocfs2_dinode *di =
90 (struct ocfs2_dinode *)et->et_root_bh->b_data;
92 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
93 di->i_last_eb_blk = cpu_to_le64(blkno);
96 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
98 struct ocfs2_dinode *di =
99 (struct ocfs2_dinode *)et->et_root_bh->b_data;
101 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
102 return le64_to_cpu(di->i_last_eb_blk);
105 static void ocfs2_dinode_update_clusters(struct inode *inode,
106 struct ocfs2_extent_tree *et,
109 struct ocfs2_dinode *di =
110 (struct ocfs2_dinode *)et->et_root_bh->b_data;
112 le32_add_cpu(&di->i_clusters, clusters);
113 spin_lock(&OCFS2_I(inode)->ip_lock);
114 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
115 spin_unlock(&OCFS2_I(inode)->ip_lock);
118 static int ocfs2_dinode_sanity_check(struct inode *inode,
119 struct ocfs2_extent_tree *et)
122 struct ocfs2_dinode *di;
124 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
126 di = (struct ocfs2_dinode *)et->et_root_bh->b_data;
127 if (!OCFS2_IS_VALID_DINODE(di)) {
129 ocfs2_error(inode->i_sb,
130 "Inode %llu has invalid path root",
131 (unsigned long long)OCFS2_I(inode)->ip_blkno);
137 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
138 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
139 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
140 .eo_update_clusters = ocfs2_dinode_update_clusters,
141 .eo_sanity_check = ocfs2_dinode_sanity_check,
144 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
147 struct ocfs2_xattr_value_root *xv =
148 (struct ocfs2_xattr_value_root *)et->et_private;
150 xv->xr_last_eb_blk = cpu_to_le64(blkno);
153 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
155 struct ocfs2_xattr_value_root *xv =
156 (struct ocfs2_xattr_value_root *) et->et_private;
158 return le64_to_cpu(xv->xr_last_eb_blk);
161 static void ocfs2_xattr_value_update_clusters(struct inode *inode,
162 struct ocfs2_extent_tree *et,
165 struct ocfs2_xattr_value_root *xv =
166 (struct ocfs2_xattr_value_root *)et->et_private;
168 le32_add_cpu(&xv->xr_clusters, clusters);
171 static int ocfs2_xattr_value_sanity_check(struct inode *inode,
172 struct ocfs2_extent_tree *et)
177 static struct ocfs2_extent_tree_operations ocfs2_xattr_et_ops = {
178 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
179 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
180 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
181 .eo_sanity_check = ocfs2_xattr_value_sanity_check,
184 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
187 struct ocfs2_xattr_block *xb =
188 (struct ocfs2_xattr_block *) et->et_root_bh->b_data;
189 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
191 xt->xt_last_eb_blk = cpu_to_le64(blkno);
194 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
196 struct ocfs2_xattr_block *xb =
197 (struct ocfs2_xattr_block *) et->et_root_bh->b_data;
198 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
200 return le64_to_cpu(xt->xt_last_eb_blk);
203 static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
204 struct ocfs2_extent_tree *et,
207 struct ocfs2_xattr_block *xb =
208 (struct ocfs2_xattr_block *)et->et_root_bh->b_data;
210 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
213 static int ocfs2_xattr_tree_sanity_check(struct inode *inode,
214 struct ocfs2_extent_tree *et)
219 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
220 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
221 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
222 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
223 .eo_sanity_check = ocfs2_xattr_tree_sanity_check,
226 static struct ocfs2_extent_tree*
227 ocfs2_new_extent_tree(struct inode *inode,
228 struct buffer_head *bh,
229 enum ocfs2_extent_tree_type et_type,
232 struct ocfs2_extent_tree *et;
234 et = kzalloc(sizeof(*et), GFP_NOFS);
238 et->et_type = et_type;
241 et->et_private = private;
243 if (et_type == OCFS2_DINODE_EXTENT) {
245 &((struct ocfs2_dinode *)bh->b_data)->id2.i_list;
246 et->et_ops = &ocfs2_dinode_et_ops;
247 } else if (et_type == OCFS2_XATTR_VALUE_EXTENT) {
248 struct ocfs2_xattr_value_root *xv =
249 (struct ocfs2_xattr_value_root *) private;
250 et->et_root_el = &xv->xr_list;
251 et->et_ops = &ocfs2_xattr_et_ops;
252 } else if (et_type == OCFS2_XATTR_TREE_EXTENT) {
253 struct ocfs2_xattr_block *xb =
254 (struct ocfs2_xattr_block *)bh->b_data;
255 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
256 et->et_ops = &ocfs2_xattr_tree_et_ops;
257 et->et_max_leaf_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
258 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
264 static void ocfs2_free_extent_tree(struct ocfs2_extent_tree *et)
267 brelse(et->et_root_bh);
272 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
275 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
278 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
280 return et->et_ops->eo_get_last_eb_blk(et);
283 static inline void ocfs2_et_update_clusters(struct inode *inode,
284 struct ocfs2_extent_tree *et,
287 et->et_ops->eo_update_clusters(inode, et, clusters);
290 static inline int ocfs2_et_sanity_check(struct inode *inode,
291 struct ocfs2_extent_tree *et)
293 return et->et_ops->eo_sanity_check(inode, et);
296 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
297 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
298 struct ocfs2_extent_block *eb);
301 * Structures which describe a path through a btree, and functions to
304 * The idea here is to be as generic as possible with the tree
307 struct ocfs2_path_item {
308 struct buffer_head *bh;
309 struct ocfs2_extent_list *el;
312 #define OCFS2_MAX_PATH_DEPTH 5
316 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
319 #define path_root_bh(_path) ((_path)->p_node[0].bh)
320 #define path_root_el(_path) ((_path)->p_node[0].el)
321 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
322 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
323 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
326 * Reset the actual path elements so that we can re-use the structure
327 * to build another path. Generally, this involves freeing the buffer
330 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
332 int i, start = 0, depth = 0;
333 struct ocfs2_path_item *node;
338 for(i = start; i < path_num_items(path); i++) {
339 node = &path->p_node[i];
347 * Tree depth may change during truncate, or insert. If we're
348 * keeping the root extent list, then make sure that our path
349 * structure reflects the proper depth.
352 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
354 path->p_tree_depth = depth;
357 static void ocfs2_free_path(struct ocfs2_path *path)
360 ocfs2_reinit_path(path, 0);
366 * All the elements of src into dest. After this call, src could be freed
367 * without affecting dest.
369 * Both paths should have the same root. Any non-root elements of dest
372 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
376 BUG_ON(path_root_bh(dest) != path_root_bh(src));
377 BUG_ON(path_root_el(dest) != path_root_el(src));
379 ocfs2_reinit_path(dest, 1);
381 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
382 dest->p_node[i].bh = src->p_node[i].bh;
383 dest->p_node[i].el = src->p_node[i].el;
385 if (dest->p_node[i].bh)
386 get_bh(dest->p_node[i].bh);
391 * Make the *dest path the same as src and re-initialize src path to
394 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
398 BUG_ON(path_root_bh(dest) != path_root_bh(src));
400 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
401 brelse(dest->p_node[i].bh);
403 dest->p_node[i].bh = src->p_node[i].bh;
404 dest->p_node[i].el = src->p_node[i].el;
406 src->p_node[i].bh = NULL;
407 src->p_node[i].el = NULL;
412 * Insert an extent block at given index.
414 * This will not take an additional reference on eb_bh.
416 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
417 struct buffer_head *eb_bh)
419 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
422 * Right now, no root bh is an extent block, so this helps
423 * catch code errors with dinode trees. The assertion can be
424 * safely removed if we ever need to insert extent block
425 * structures at the root.
429 path->p_node[index].bh = eb_bh;
430 path->p_node[index].el = &eb->h_list;
433 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
434 struct ocfs2_extent_list *root_el)
436 struct ocfs2_path *path;
438 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
440 path = kzalloc(sizeof(*path), GFP_NOFS);
442 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
444 path_root_bh(path) = root_bh;
445 path_root_el(path) = root_el;
452 * Convenience function to journal all components in a path.
454 static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
455 struct ocfs2_path *path)
462 for(i = 0; i < path_num_items(path); i++) {
463 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
464 OCFS2_JOURNAL_ACCESS_WRITE);
476 * Return the index of the extent record which contains cluster #v_cluster.
477 * -1 is returned if it was not found.
479 * Should work fine on interior and exterior nodes.
481 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
485 struct ocfs2_extent_rec *rec;
486 u32 rec_end, rec_start, clusters;
488 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
489 rec = &el->l_recs[i];
491 rec_start = le32_to_cpu(rec->e_cpos);
492 clusters = ocfs2_rec_clusters(el, rec);
494 rec_end = rec_start + clusters;
496 if (v_cluster >= rec_start && v_cluster < rec_end) {
505 enum ocfs2_contig_type {
514 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
515 * ocfs2_extent_contig only work properly against leaf nodes!
517 static int ocfs2_block_extent_contig(struct super_block *sb,
518 struct ocfs2_extent_rec *ext,
521 u64 blk_end = le64_to_cpu(ext->e_blkno);
523 blk_end += ocfs2_clusters_to_blocks(sb,
524 le16_to_cpu(ext->e_leaf_clusters));
526 return blkno == blk_end;
529 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
530 struct ocfs2_extent_rec *right)
534 left_range = le32_to_cpu(left->e_cpos) +
535 le16_to_cpu(left->e_leaf_clusters);
537 return (left_range == le32_to_cpu(right->e_cpos));
540 static enum ocfs2_contig_type
541 ocfs2_extent_contig(struct inode *inode,
542 struct ocfs2_extent_rec *ext,
543 struct ocfs2_extent_rec *insert_rec)
545 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
548 * Refuse to coalesce extent records with different flag
549 * fields - we don't want to mix unwritten extents with user
552 if (ext->e_flags != insert_rec->e_flags)
555 if (ocfs2_extents_adjacent(ext, insert_rec) &&
556 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
559 blkno = le64_to_cpu(ext->e_blkno);
560 if (ocfs2_extents_adjacent(insert_rec, ext) &&
561 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
568 * NOTE: We can have pretty much any combination of contiguousness and
571 * The usefulness of APPEND_TAIL is more in that it lets us know that
572 * we'll have to update the path to that leaf.
574 enum ocfs2_append_type {
579 enum ocfs2_split_type {
585 struct ocfs2_insert_type {
586 enum ocfs2_split_type ins_split;
587 enum ocfs2_append_type ins_appending;
588 enum ocfs2_contig_type ins_contig;
589 int ins_contig_index;
593 struct ocfs2_merge_ctxt {
594 enum ocfs2_contig_type c_contig_type;
595 int c_has_empty_extent;
596 int c_split_covers_rec;
600 * How many free extents have we got before we need more meta data?
602 int ocfs2_num_free_extents(struct ocfs2_super *osb,
604 struct buffer_head *root_bh,
605 enum ocfs2_extent_tree_type type,
609 struct ocfs2_extent_list *el = NULL;
610 struct ocfs2_extent_block *eb;
611 struct buffer_head *eb_bh = NULL;
616 if (type == OCFS2_DINODE_EXTENT) {
617 struct ocfs2_dinode *fe =
618 (struct ocfs2_dinode *)root_bh->b_data;
619 if (!OCFS2_IS_VALID_DINODE(fe)) {
620 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
625 if (fe->i_last_eb_blk)
626 last_eb_blk = le64_to_cpu(fe->i_last_eb_blk);
627 el = &fe->id2.i_list;
628 } else if (type == OCFS2_XATTR_VALUE_EXTENT) {
629 struct ocfs2_xattr_value_root *xv =
630 (struct ocfs2_xattr_value_root *) private;
632 last_eb_blk = le64_to_cpu(xv->xr_last_eb_blk);
634 } else if (type == OCFS2_XATTR_TREE_EXTENT) {
635 struct ocfs2_xattr_block *xb =
636 (struct ocfs2_xattr_block *)root_bh->b_data;
638 last_eb_blk = le64_to_cpu(xb->xb_attrs.xb_root.xt_last_eb_blk);
639 el = &xb->xb_attrs.xb_root.xt_list;
643 retval = ocfs2_read_block(osb, last_eb_blk,
644 &eb_bh, OCFS2_BH_CACHED, inode);
649 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
653 BUG_ON(el->l_tree_depth != 0);
655 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
664 /* expects array to already be allocated
666 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
669 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
673 struct ocfs2_alloc_context *meta_ac,
674 struct buffer_head *bhs[])
676 int count, status, i;
677 u16 suballoc_bit_start;
680 struct ocfs2_extent_block *eb;
685 while (count < wanted) {
686 status = ocfs2_claim_metadata(osb,
698 for(i = count; i < (num_got + count); i++) {
699 bhs[i] = sb_getblk(osb->sb, first_blkno);
700 if (bhs[i] == NULL) {
705 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
707 status = ocfs2_journal_access(handle, inode, bhs[i],
708 OCFS2_JOURNAL_ACCESS_CREATE);
714 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
715 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
716 /* Ok, setup the minimal stuff here. */
717 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
718 eb->h_blkno = cpu_to_le64(first_blkno);
719 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
720 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
721 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
723 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
725 suballoc_bit_start++;
728 /* We'll also be dirtied by the caller, so
729 * this isn't absolutely necessary. */
730 status = ocfs2_journal_dirty(handle, bhs[i]);
743 for(i = 0; i < wanted; i++) {
754 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
756 * Returns the sum of the rightmost extent rec logical offset and
759 * ocfs2_add_branch() uses this to determine what logical cluster
760 * value should be populated into the leftmost new branch records.
762 * ocfs2_shift_tree_depth() uses this to determine the # clusters
763 * value for the new topmost tree record.
765 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
769 i = le16_to_cpu(el->l_next_free_rec) - 1;
771 return le32_to_cpu(el->l_recs[i].e_cpos) +
772 ocfs2_rec_clusters(el, &el->l_recs[i]);
776 * Add an entire tree branch to our inode. eb_bh is the extent block
777 * to start at, if we don't want to start the branch at the dinode
780 * last_eb_bh is required as we have to update it's next_leaf pointer
781 * for the new last extent block.
783 * the new branch will be 'empty' in the sense that every block will
784 * contain a single record with cluster count == 0.
786 static int ocfs2_add_branch(struct ocfs2_super *osb,
789 struct ocfs2_extent_tree *et,
790 struct buffer_head *eb_bh,
791 struct buffer_head **last_eb_bh,
792 struct ocfs2_alloc_context *meta_ac)
794 int status, new_blocks, i;
795 u64 next_blkno, new_last_eb_blk;
796 struct buffer_head *bh;
797 struct buffer_head **new_eb_bhs = NULL;
798 struct ocfs2_extent_block *eb;
799 struct ocfs2_extent_list *eb_el;
800 struct ocfs2_extent_list *el;
805 BUG_ON(!last_eb_bh || !*last_eb_bh);
808 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
813 /* we never add a branch to a leaf. */
814 BUG_ON(!el->l_tree_depth);
816 new_blocks = le16_to_cpu(el->l_tree_depth);
818 /* allocate the number of new eb blocks we need */
819 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
827 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
828 meta_ac, new_eb_bhs);
834 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
835 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
837 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
838 * linked with the rest of the tree.
839 * conversly, new_eb_bhs[0] is the new bottommost leaf.
841 * when we leave the loop, new_last_eb_blk will point to the
842 * newest leaf, and next_blkno will point to the topmost extent
844 next_blkno = new_last_eb_blk = 0;
845 for(i = 0; i < new_blocks; i++) {
847 eb = (struct ocfs2_extent_block *) bh->b_data;
848 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
849 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
855 status = ocfs2_journal_access(handle, inode, bh,
856 OCFS2_JOURNAL_ACCESS_CREATE);
862 eb->h_next_leaf_blk = 0;
863 eb_el->l_tree_depth = cpu_to_le16(i);
864 eb_el->l_next_free_rec = cpu_to_le16(1);
866 * This actually counts as an empty extent as
869 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
870 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
872 * eb_el isn't always an interior node, but even leaf
873 * nodes want a zero'd flags and reserved field so
874 * this gets the whole 32 bits regardless of use.
876 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
877 if (!eb_el->l_tree_depth)
878 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
880 status = ocfs2_journal_dirty(handle, bh);
886 next_blkno = le64_to_cpu(eb->h_blkno);
889 /* This is a bit hairy. We want to update up to three blocks
890 * here without leaving any of them in an inconsistent state
891 * in case of error. We don't have to worry about
892 * journal_dirty erroring as it won't unless we've aborted the
893 * handle (in which case we would never be here) so reserving
894 * the write with journal_access is all we need to do. */
895 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
896 OCFS2_JOURNAL_ACCESS_WRITE);
901 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
902 OCFS2_JOURNAL_ACCESS_WRITE);
908 status = ocfs2_journal_access(handle, inode, eb_bh,
909 OCFS2_JOURNAL_ACCESS_WRITE);
916 /* Link the new branch into the rest of the tree (el will
917 * either be on the root_bh, or the extent block passed in. */
918 i = le16_to_cpu(el->l_next_free_rec);
919 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
920 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
921 el->l_recs[i].e_int_clusters = 0;
922 le16_add_cpu(&el->l_next_free_rec, 1);
924 /* fe needs a new last extent block pointer, as does the
925 * next_leaf on the previously last-extent-block. */
926 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
928 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
929 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
931 status = ocfs2_journal_dirty(handle, *last_eb_bh);
934 status = ocfs2_journal_dirty(handle, et->et_root_bh);
938 status = ocfs2_journal_dirty(handle, eb_bh);
944 * Some callers want to track the rightmost leaf so pass it
948 get_bh(new_eb_bhs[0]);
949 *last_eb_bh = new_eb_bhs[0];
954 for (i = 0; i < new_blocks; i++)
956 brelse(new_eb_bhs[i]);
965 * adds another level to the allocation tree.
966 * returns back the new extent block so you can add a branch to it
969 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
972 struct ocfs2_extent_tree *et,
973 struct ocfs2_alloc_context *meta_ac,
974 struct buffer_head **ret_new_eb_bh)
978 struct buffer_head *new_eb_bh = NULL;
979 struct ocfs2_extent_block *eb;
980 struct ocfs2_extent_list *root_el;
981 struct ocfs2_extent_list *eb_el;
985 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
992 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
993 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
994 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1000 root_el = et->et_root_el;
1002 status = ocfs2_journal_access(handle, inode, new_eb_bh,
1003 OCFS2_JOURNAL_ACCESS_CREATE);
1009 /* copy the root extent list data into the new extent block */
1010 eb_el->l_tree_depth = root_el->l_tree_depth;
1011 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1012 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1013 eb_el->l_recs[i] = root_el->l_recs[i];
1015 status = ocfs2_journal_dirty(handle, new_eb_bh);
1021 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
1022 OCFS2_JOURNAL_ACCESS_WRITE);
1028 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1030 /* update root_bh now */
1031 le16_add_cpu(&root_el->l_tree_depth, 1);
1032 root_el->l_recs[0].e_cpos = 0;
1033 root_el->l_recs[0].e_blkno = eb->h_blkno;
1034 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1035 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1036 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1037 root_el->l_next_free_rec = cpu_to_le16(1);
1039 /* If this is our 1st tree depth shift, then last_eb_blk
1040 * becomes the allocated extent block */
1041 if (root_el->l_tree_depth == cpu_to_le16(1))
1042 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1044 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1050 *ret_new_eb_bh = new_eb_bh;
1062 * Should only be called when there is no space left in any of the
1063 * leaf nodes. What we want to do is find the lowest tree depth
1064 * non-leaf extent block with room for new records. There are three
1065 * valid results of this search:
1067 * 1) a lowest extent block is found, then we pass it back in
1068 * *lowest_eb_bh and return '0'
1070 * 2) the search fails to find anything, but the root_el has room. We
1071 * pass NULL back in *lowest_eb_bh, but still return '0'
1073 * 3) the search fails to find anything AND the root_el is full, in
1074 * which case we return > 0
1076 * return status < 0 indicates an error.
1078 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1079 struct inode *inode,
1080 struct ocfs2_extent_tree *et,
1081 struct buffer_head **target_bh)
1085 struct ocfs2_extent_block *eb;
1086 struct ocfs2_extent_list *el;
1087 struct buffer_head *bh = NULL;
1088 struct buffer_head *lowest_bh = NULL;
1094 el = et->et_root_el;
1096 while(le16_to_cpu(el->l_tree_depth) > 1) {
1097 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1098 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
1099 "extent list (next_free_rec == 0)",
1100 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1104 i = le16_to_cpu(el->l_next_free_rec) - 1;
1105 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1107 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
1108 "list where extent # %d has no physical "
1110 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
1120 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
1127 eb = (struct ocfs2_extent_block *) bh->b_data;
1128 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1129 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1135 if (le16_to_cpu(el->l_next_free_rec) <
1136 le16_to_cpu(el->l_count)) {
1144 /* If we didn't find one and the fe doesn't have any room,
1145 * then return '1' */
1146 el = et->et_root_el;
1147 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1150 *target_bh = lowest_bh;
1160 * Grow a b-tree so that it has more records.
1162 * We might shift the tree depth in which case existing paths should
1163 * be considered invalid.
1165 * Tree depth after the grow is returned via *final_depth.
1167 * *last_eb_bh will be updated by ocfs2_add_branch().
1169 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
1170 struct ocfs2_extent_tree *et, int *final_depth,
1171 struct buffer_head **last_eb_bh,
1172 struct ocfs2_alloc_context *meta_ac)
1175 struct ocfs2_extent_list *el = et->et_root_el;
1176 int depth = le16_to_cpu(el->l_tree_depth);
1177 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1178 struct buffer_head *bh = NULL;
1180 BUG_ON(meta_ac == NULL);
1182 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
1189 /* We traveled all the way to the bottom of the allocation tree
1190 * and didn't find room for any more extents - we need to add
1191 * another tree level */
1194 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1196 /* ocfs2_shift_tree_depth will return us a buffer with
1197 * the new extent block (so we can pass that to
1198 * ocfs2_add_branch). */
1199 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
1208 * Special case: we have room now if we shifted from
1209 * tree_depth 0, so no more work needs to be done.
1211 * We won't be calling add_branch, so pass
1212 * back *last_eb_bh as the new leaf. At depth
1213 * zero, it should always be null so there's
1214 * no reason to brelse.
1216 BUG_ON(*last_eb_bh);
1223 /* call ocfs2_add_branch to add the final part of the tree with
1225 mlog(0, "add branch. bh = %p\n", bh);
1226 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
1235 *final_depth = depth;
1241 * This function will discard the rightmost extent record.
1243 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1245 int next_free = le16_to_cpu(el->l_next_free_rec);
1246 int count = le16_to_cpu(el->l_count);
1247 unsigned int num_bytes;
1250 /* This will cause us to go off the end of our extent list. */
1251 BUG_ON(next_free >= count);
1253 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1255 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1258 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1259 struct ocfs2_extent_rec *insert_rec)
1261 int i, insert_index, next_free, has_empty, num_bytes;
1262 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1263 struct ocfs2_extent_rec *rec;
1265 next_free = le16_to_cpu(el->l_next_free_rec);
1266 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1270 /* The tree code before us didn't allow enough room in the leaf. */
1271 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1274 * The easiest way to approach this is to just remove the
1275 * empty extent and temporarily decrement next_free.
1279 * If next_free was 1 (only an empty extent), this
1280 * loop won't execute, which is fine. We still want
1281 * the decrement above to happen.
1283 for(i = 0; i < (next_free - 1); i++)
1284 el->l_recs[i] = el->l_recs[i+1];
1290 * Figure out what the new record index should be.
1292 for(i = 0; i < next_free; i++) {
1293 rec = &el->l_recs[i];
1295 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1300 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1301 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1303 BUG_ON(insert_index < 0);
1304 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1305 BUG_ON(insert_index > next_free);
1308 * No need to memmove if we're just adding to the tail.
1310 if (insert_index != next_free) {
1311 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1313 num_bytes = next_free - insert_index;
1314 num_bytes *= sizeof(struct ocfs2_extent_rec);
1315 memmove(&el->l_recs[insert_index + 1],
1316 &el->l_recs[insert_index],
1321 * Either we had an empty extent, and need to re-increment or
1322 * there was no empty extent on a non full rightmost leaf node,
1323 * in which case we still need to increment.
1326 el->l_next_free_rec = cpu_to_le16(next_free);
1328 * Make sure none of the math above just messed up our tree.
1330 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1332 el->l_recs[insert_index] = *insert_rec;
1336 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1338 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1340 BUG_ON(num_recs == 0);
1342 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1344 size = num_recs * sizeof(struct ocfs2_extent_rec);
1345 memmove(&el->l_recs[0], &el->l_recs[1], size);
1346 memset(&el->l_recs[num_recs], 0,
1347 sizeof(struct ocfs2_extent_rec));
1348 el->l_next_free_rec = cpu_to_le16(num_recs);
1353 * Create an empty extent record .
1355 * l_next_free_rec may be updated.
1357 * If an empty extent already exists do nothing.
1359 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1361 int next_free = le16_to_cpu(el->l_next_free_rec);
1363 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1368 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1371 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1372 "Asked to create an empty extent in a full list:\n"
1373 "count = %u, tree depth = %u",
1374 le16_to_cpu(el->l_count),
1375 le16_to_cpu(el->l_tree_depth));
1377 ocfs2_shift_records_right(el);
1380 le16_add_cpu(&el->l_next_free_rec, 1);
1381 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1385 * For a rotation which involves two leaf nodes, the "root node" is
1386 * the lowest level tree node which contains a path to both leafs. This
1387 * resulting set of information can be used to form a complete "subtree"
1389 * This function is passed two full paths from the dinode down to a
1390 * pair of adjacent leaves. It's task is to figure out which path
1391 * index contains the subtree root - this can be the root index itself
1392 * in a worst-case rotation.
1394 * The array index of the subtree root is passed back.
1396 static int ocfs2_find_subtree_root(struct inode *inode,
1397 struct ocfs2_path *left,
1398 struct ocfs2_path *right)
1403 * Check that the caller passed in two paths from the same tree.
1405 BUG_ON(path_root_bh(left) != path_root_bh(right));
1411 * The caller didn't pass two adjacent paths.
1413 mlog_bug_on_msg(i > left->p_tree_depth,
1414 "Inode %lu, left depth %u, right depth %u\n"
1415 "left leaf blk %llu, right leaf blk %llu\n",
1416 inode->i_ino, left->p_tree_depth,
1417 right->p_tree_depth,
1418 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1419 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1420 } while (left->p_node[i].bh->b_blocknr ==
1421 right->p_node[i].bh->b_blocknr);
1426 typedef void (path_insert_t)(void *, struct buffer_head *);
1429 * Traverse a btree path in search of cpos, starting at root_el.
1431 * This code can be called with a cpos larger than the tree, in which
1432 * case it will return the rightmost path.
1434 static int __ocfs2_find_path(struct inode *inode,
1435 struct ocfs2_extent_list *root_el, u32 cpos,
1436 path_insert_t *func, void *data)
1441 struct buffer_head *bh = NULL;
1442 struct ocfs2_extent_block *eb;
1443 struct ocfs2_extent_list *el;
1444 struct ocfs2_extent_rec *rec;
1445 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1448 while (el->l_tree_depth) {
1449 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1450 ocfs2_error(inode->i_sb,
1451 "Inode %llu has empty extent list at "
1453 (unsigned long long)oi->ip_blkno,
1454 le16_to_cpu(el->l_tree_depth));
1460 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1461 rec = &el->l_recs[i];
1464 * In the case that cpos is off the allocation
1465 * tree, this should just wind up returning the
1468 range = le32_to_cpu(rec->e_cpos) +
1469 ocfs2_rec_clusters(el, rec);
1470 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1474 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1476 ocfs2_error(inode->i_sb,
1477 "Inode %llu has bad blkno in extent list "
1478 "at depth %u (index %d)\n",
1479 (unsigned long long)oi->ip_blkno,
1480 le16_to_cpu(el->l_tree_depth), i);
1487 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1488 &bh, OCFS2_BH_CACHED, inode);
1494 eb = (struct ocfs2_extent_block *) bh->b_data;
1496 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1497 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1502 if (le16_to_cpu(el->l_next_free_rec) >
1503 le16_to_cpu(el->l_count)) {
1504 ocfs2_error(inode->i_sb,
1505 "Inode %llu has bad count in extent list "
1506 "at block %llu (next free=%u, count=%u)\n",
1507 (unsigned long long)oi->ip_blkno,
1508 (unsigned long long)bh->b_blocknr,
1509 le16_to_cpu(el->l_next_free_rec),
1510 le16_to_cpu(el->l_count));
1521 * Catch any trailing bh that the loop didn't handle.
1529 * Given an initialized path (that is, it has a valid root extent
1530 * list), this function will traverse the btree in search of the path
1531 * which would contain cpos.
1533 * The path traveled is recorded in the path structure.
1535 * Note that this will not do any comparisons on leaf node extent
1536 * records, so it will work fine in the case that we just added a tree
1539 struct find_path_data {
1541 struct ocfs2_path *path;
1543 static void find_path_ins(void *data, struct buffer_head *bh)
1545 struct find_path_data *fp = data;
1548 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1551 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1554 struct find_path_data data;
1558 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1559 find_path_ins, &data);
1562 static void find_leaf_ins(void *data, struct buffer_head *bh)
1564 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1565 struct ocfs2_extent_list *el = &eb->h_list;
1566 struct buffer_head **ret = data;
1568 /* We want to retain only the leaf block. */
1569 if (le16_to_cpu(el->l_tree_depth) == 0) {
1575 * Find the leaf block in the tree which would contain cpos. No
1576 * checking of the actual leaf is done.
1578 * Some paths want to call this instead of allocating a path structure
1579 * and calling ocfs2_find_path().
1581 * This function doesn't handle non btree extent lists.
1583 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1584 u32 cpos, struct buffer_head **leaf_bh)
1587 struct buffer_head *bh = NULL;
1589 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1601 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1603 * Basically, we've moved stuff around at the bottom of the tree and
1604 * we need to fix up the extent records above the changes to reflect
1607 * left_rec: the record on the left.
1608 * left_child_el: is the child list pointed to by left_rec
1609 * right_rec: the record to the right of left_rec
1610 * right_child_el: is the child list pointed to by right_rec
1612 * By definition, this only works on interior nodes.
1614 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1615 struct ocfs2_extent_list *left_child_el,
1616 struct ocfs2_extent_rec *right_rec,
1617 struct ocfs2_extent_list *right_child_el)
1619 u32 left_clusters, right_end;
1622 * Interior nodes never have holes. Their cpos is the cpos of
1623 * the leftmost record in their child list. Their cluster
1624 * count covers the full theoretical range of their child list
1625 * - the range between their cpos and the cpos of the record
1626 * immediately to their right.
1628 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1629 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1630 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1631 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1633 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1634 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1637 * Calculate the rightmost cluster count boundary before
1638 * moving cpos - we will need to adjust clusters after
1639 * updating e_cpos to keep the same highest cluster count.
1641 right_end = le32_to_cpu(right_rec->e_cpos);
1642 right_end += le32_to_cpu(right_rec->e_int_clusters);
1644 right_rec->e_cpos = left_rec->e_cpos;
1645 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1647 right_end -= le32_to_cpu(right_rec->e_cpos);
1648 right_rec->e_int_clusters = cpu_to_le32(right_end);
1652 * Adjust the adjacent root node records involved in a
1653 * rotation. left_el_blkno is passed in as a key so that we can easily
1654 * find it's index in the root list.
1656 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1657 struct ocfs2_extent_list *left_el,
1658 struct ocfs2_extent_list *right_el,
1663 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1664 le16_to_cpu(left_el->l_tree_depth));
1666 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1667 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1672 * The path walking code should have never returned a root and
1673 * two paths which are not adjacent.
1675 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1677 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1678 &root_el->l_recs[i + 1], right_el);
1682 * We've changed a leaf block (in right_path) and need to reflect that
1683 * change back up the subtree.
1685 * This happens in multiple places:
1686 * - When we've moved an extent record from the left path leaf to the right
1687 * path leaf to make room for an empty extent in the left path leaf.
1688 * - When our insert into the right path leaf is at the leftmost edge
1689 * and requires an update of the path immediately to it's left. This
1690 * can occur at the end of some types of rotation and appending inserts.
1691 * - When we've adjusted the last extent record in the left path leaf and the
1692 * 1st extent record in the right path leaf during cross extent block merge.
1694 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1695 struct ocfs2_path *left_path,
1696 struct ocfs2_path *right_path,
1700 struct ocfs2_extent_list *el, *left_el, *right_el;
1701 struct ocfs2_extent_rec *left_rec, *right_rec;
1702 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1705 * Update the counts and position values within all the
1706 * interior nodes to reflect the leaf rotation we just did.
1708 * The root node is handled below the loop.
1710 * We begin the loop with right_el and left_el pointing to the
1711 * leaf lists and work our way up.
1713 * NOTE: within this loop, left_el and right_el always refer
1714 * to the *child* lists.
1716 left_el = path_leaf_el(left_path);
1717 right_el = path_leaf_el(right_path);
1718 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1719 mlog(0, "Adjust records at index %u\n", i);
1722 * One nice property of knowing that all of these
1723 * nodes are below the root is that we only deal with
1724 * the leftmost right node record and the rightmost
1727 el = left_path->p_node[i].el;
1728 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1729 left_rec = &el->l_recs[idx];
1731 el = right_path->p_node[i].el;
1732 right_rec = &el->l_recs[0];
1734 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1737 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1741 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1746 * Setup our list pointers now so that the current
1747 * parents become children in the next iteration.
1749 left_el = left_path->p_node[i].el;
1750 right_el = right_path->p_node[i].el;
1754 * At the root node, adjust the two adjacent records which
1755 * begin our path to the leaves.
1758 el = left_path->p_node[subtree_index].el;
1759 left_el = left_path->p_node[subtree_index + 1].el;
1760 right_el = right_path->p_node[subtree_index + 1].el;
1762 ocfs2_adjust_root_records(el, left_el, right_el,
1763 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1765 root_bh = left_path->p_node[subtree_index].bh;
1767 ret = ocfs2_journal_dirty(handle, root_bh);
1772 static int ocfs2_rotate_subtree_right(struct inode *inode,
1774 struct ocfs2_path *left_path,
1775 struct ocfs2_path *right_path,
1779 struct buffer_head *right_leaf_bh;
1780 struct buffer_head *left_leaf_bh = NULL;
1781 struct buffer_head *root_bh;
1782 struct ocfs2_extent_list *right_el, *left_el;
1783 struct ocfs2_extent_rec move_rec;
1785 left_leaf_bh = path_leaf_bh(left_path);
1786 left_el = path_leaf_el(left_path);
1788 if (left_el->l_next_free_rec != left_el->l_count) {
1789 ocfs2_error(inode->i_sb,
1790 "Inode %llu has non-full interior leaf node %llu"
1792 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1793 (unsigned long long)left_leaf_bh->b_blocknr,
1794 le16_to_cpu(left_el->l_next_free_rec));
1799 * This extent block may already have an empty record, so we
1800 * return early if so.
1802 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1805 root_bh = left_path->p_node[subtree_index].bh;
1806 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1808 ret = ocfs2_journal_access(handle, inode, root_bh,
1809 OCFS2_JOURNAL_ACCESS_WRITE);
1815 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1816 ret = ocfs2_journal_access(handle, inode,
1817 right_path->p_node[i].bh,
1818 OCFS2_JOURNAL_ACCESS_WRITE);
1824 ret = ocfs2_journal_access(handle, inode,
1825 left_path->p_node[i].bh,
1826 OCFS2_JOURNAL_ACCESS_WRITE);
1833 right_leaf_bh = path_leaf_bh(right_path);
1834 right_el = path_leaf_el(right_path);
1836 /* This is a code error, not a disk corruption. */
1837 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1838 "because rightmost leaf block %llu is empty\n",
1839 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1840 (unsigned long long)right_leaf_bh->b_blocknr);
1842 ocfs2_create_empty_extent(right_el);
1844 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1850 /* Do the copy now. */
1851 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1852 move_rec = left_el->l_recs[i];
1853 right_el->l_recs[0] = move_rec;
1856 * Clear out the record we just copied and shift everything
1857 * over, leaving an empty extent in the left leaf.
1859 * We temporarily subtract from next_free_rec so that the
1860 * shift will lose the tail record (which is now defunct).
1862 le16_add_cpu(&left_el->l_next_free_rec, -1);
1863 ocfs2_shift_records_right(left_el);
1864 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1865 le16_add_cpu(&left_el->l_next_free_rec, 1);
1867 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1873 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1881 * Given a full path, determine what cpos value would return us a path
1882 * containing the leaf immediately to the left of the current one.
1884 * Will return zero if the path passed in is already the leftmost path.
1886 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1887 struct ocfs2_path *path, u32 *cpos)
1891 struct ocfs2_extent_list *el;
1893 BUG_ON(path->p_tree_depth == 0);
1897 blkno = path_leaf_bh(path)->b_blocknr;
1899 /* Start at the tree node just above the leaf and work our way up. */
1900 i = path->p_tree_depth - 1;
1902 el = path->p_node[i].el;
1905 * Find the extent record just before the one in our
1908 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1909 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1913 * We've determined that the
1914 * path specified is already
1915 * the leftmost one - return a
1921 * The leftmost record points to our
1922 * leaf - we need to travel up the
1928 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
1929 *cpos = *cpos + ocfs2_rec_clusters(el,
1930 &el->l_recs[j - 1]);
1937 * If we got here, we never found a valid node where
1938 * the tree indicated one should be.
1941 "Invalid extent tree at extent block %llu\n",
1942 (unsigned long long)blkno);
1947 blkno = path->p_node[i].bh->b_blocknr;
1956 * Extend the transaction by enough credits to complete the rotation,
1957 * and still leave at least the original number of credits allocated
1958 * to this transaction.
1960 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
1962 struct ocfs2_path *path)
1964 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
1966 if (handle->h_buffer_credits < credits)
1967 return ocfs2_extend_trans(handle, credits);
1973 * Trap the case where we're inserting into the theoretical range past
1974 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1975 * whose cpos is less than ours into the right leaf.
1977 * It's only necessary to look at the rightmost record of the left
1978 * leaf because the logic that calls us should ensure that the
1979 * theoretical ranges in the path components above the leaves are
1982 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1985 struct ocfs2_extent_list *left_el;
1986 struct ocfs2_extent_rec *rec;
1989 left_el = path_leaf_el(left_path);
1990 next_free = le16_to_cpu(left_el->l_next_free_rec);
1991 rec = &left_el->l_recs[next_free - 1];
1993 if (insert_cpos > le32_to_cpu(rec->e_cpos))
1998 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2000 int next_free = le16_to_cpu(el->l_next_free_rec);
2002 struct ocfs2_extent_rec *rec;
2007 rec = &el->l_recs[0];
2008 if (ocfs2_is_empty_extent(rec)) {
2012 rec = &el->l_recs[1];
2015 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2016 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2022 * Rotate all the records in a btree right one record, starting at insert_cpos.
2024 * The path to the rightmost leaf should be passed in.
2026 * The array is assumed to be large enough to hold an entire path (tree depth).
2028 * Upon succesful return from this function:
2030 * - The 'right_path' array will contain a path to the leaf block
2031 * whose range contains e_cpos.
2032 * - That leaf block will have a single empty extent in list index 0.
2033 * - In the case that the rotation requires a post-insert update,
2034 * *ret_left_path will contain a valid path which can be passed to
2035 * ocfs2_insert_path().
2037 static int ocfs2_rotate_tree_right(struct inode *inode,
2039 enum ocfs2_split_type split,
2041 struct ocfs2_path *right_path,
2042 struct ocfs2_path **ret_left_path)
2044 int ret, start, orig_credits = handle->h_buffer_credits;
2046 struct ocfs2_path *left_path = NULL;
2048 *ret_left_path = NULL;
2050 left_path = ocfs2_new_path(path_root_bh(right_path),
2051 path_root_el(right_path));
2058 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2064 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2067 * What we want to do here is:
2069 * 1) Start with the rightmost path.
2071 * 2) Determine a path to the leaf block directly to the left
2074 * 3) Determine the 'subtree root' - the lowest level tree node
2075 * which contains a path to both leaves.
2077 * 4) Rotate the subtree.
2079 * 5) Find the next subtree by considering the left path to be
2080 * the new right path.
2082 * The check at the top of this while loop also accepts
2083 * insert_cpos == cpos because cpos is only a _theoretical_
2084 * value to get us the left path - insert_cpos might very well
2085 * be filling that hole.
2087 * Stop at a cpos of '0' because we either started at the
2088 * leftmost branch (i.e., a tree with one branch and a
2089 * rotation inside of it), or we've gone as far as we can in
2090 * rotating subtrees.
2092 while (cpos && insert_cpos <= cpos) {
2093 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2096 ret = ocfs2_find_path(inode, left_path, cpos);
2102 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2103 path_leaf_bh(right_path),
2104 "Inode %lu: error during insert of %u "
2105 "(left path cpos %u) results in two identical "
2106 "paths ending at %llu\n",
2107 inode->i_ino, insert_cpos, cpos,
2108 (unsigned long long)
2109 path_leaf_bh(left_path)->b_blocknr);
2111 if (split == SPLIT_NONE &&
2112 ocfs2_rotate_requires_path_adjustment(left_path,
2116 * We've rotated the tree as much as we
2117 * should. The rest is up to
2118 * ocfs2_insert_path() to complete, after the
2119 * record insertion. We indicate this
2120 * situation by returning the left path.
2122 * The reason we don't adjust the records here
2123 * before the record insert is that an error
2124 * later might break the rule where a parent
2125 * record e_cpos will reflect the actual
2126 * e_cpos of the 1st nonempty record of the
2129 *ret_left_path = left_path;
2133 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2135 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2137 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2138 right_path->p_tree_depth);
2140 ret = ocfs2_extend_rotate_transaction(handle, start,
2141 orig_credits, right_path);
2147 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2154 if (split != SPLIT_NONE &&
2155 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2158 * A rotate moves the rightmost left leaf
2159 * record over to the leftmost right leaf
2160 * slot. If we're doing an extent split
2161 * instead of a real insert, then we have to
2162 * check that the extent to be split wasn't
2163 * just moved over. If it was, then we can
2164 * exit here, passing left_path back -
2165 * ocfs2_split_extent() is smart enough to
2166 * search both leaves.
2168 *ret_left_path = left_path;
2173 * There is no need to re-read the next right path
2174 * as we know that it'll be our current left
2175 * path. Optimize by copying values instead.
2177 ocfs2_mv_path(right_path, left_path);
2179 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2188 ocfs2_free_path(left_path);
2194 static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2195 struct ocfs2_path *path)
2198 struct ocfs2_extent_rec *rec;
2199 struct ocfs2_extent_list *el;
2200 struct ocfs2_extent_block *eb;
2203 /* Path should always be rightmost. */
2204 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2205 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2208 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2209 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2210 rec = &el->l_recs[idx];
2211 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2213 for (i = 0; i < path->p_tree_depth; i++) {
2214 el = path->p_node[i].el;
2215 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2216 rec = &el->l_recs[idx];
2218 rec->e_int_clusters = cpu_to_le32(range);
2219 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2221 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2225 static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2226 struct ocfs2_cached_dealloc_ctxt *dealloc,
2227 struct ocfs2_path *path, int unlink_start)
2230 struct ocfs2_extent_block *eb;
2231 struct ocfs2_extent_list *el;
2232 struct buffer_head *bh;
2234 for(i = unlink_start; i < path_num_items(path); i++) {
2235 bh = path->p_node[i].bh;
2237 eb = (struct ocfs2_extent_block *)bh->b_data;
2239 * Not all nodes might have had their final count
2240 * decremented by the caller - handle this here.
2243 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2245 "Inode %llu, attempted to remove extent block "
2246 "%llu with %u records\n",
2247 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2248 (unsigned long long)le64_to_cpu(eb->h_blkno),
2249 le16_to_cpu(el->l_next_free_rec));
2251 ocfs2_journal_dirty(handle, bh);
2252 ocfs2_remove_from_cache(inode, bh);
2256 el->l_next_free_rec = 0;
2257 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2259 ocfs2_journal_dirty(handle, bh);
2261 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2265 ocfs2_remove_from_cache(inode, bh);
2269 static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2270 struct ocfs2_path *left_path,
2271 struct ocfs2_path *right_path,
2273 struct ocfs2_cached_dealloc_ctxt *dealloc)
2276 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2277 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2278 struct ocfs2_extent_list *el;
2279 struct ocfs2_extent_block *eb;
2281 el = path_leaf_el(left_path);
2283 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2285 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2286 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2289 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2291 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2292 le16_add_cpu(&root_el->l_next_free_rec, -1);
2294 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2295 eb->h_next_leaf_blk = 0;
2297 ocfs2_journal_dirty(handle, root_bh);
2298 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2300 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2304 static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2305 struct ocfs2_path *left_path,
2306 struct ocfs2_path *right_path,
2308 struct ocfs2_cached_dealloc_ctxt *dealloc,
2310 struct ocfs2_extent_tree *et)
2312 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2313 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2314 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2315 struct ocfs2_extent_block *eb;
2319 right_leaf_el = path_leaf_el(right_path);
2320 left_leaf_el = path_leaf_el(left_path);
2321 root_bh = left_path->p_node[subtree_index].bh;
2322 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2324 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2327 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2328 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2330 * It's legal for us to proceed if the right leaf is
2331 * the rightmost one and it has an empty extent. There
2332 * are two cases to handle - whether the leaf will be
2333 * empty after removal or not. If the leaf isn't empty
2334 * then just remove the empty extent up front. The
2335 * next block will handle empty leaves by flagging
2338 * Non rightmost leaves will throw -EAGAIN and the
2339 * caller can manually move the subtree and retry.
2342 if (eb->h_next_leaf_blk != 0ULL)
2345 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2346 ret = ocfs2_journal_access(handle, inode,
2347 path_leaf_bh(right_path),
2348 OCFS2_JOURNAL_ACCESS_WRITE);
2354 ocfs2_remove_empty_extent(right_leaf_el);
2356 right_has_empty = 1;
2359 if (eb->h_next_leaf_blk == 0ULL &&
2360 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2362 * We have to update i_last_eb_blk during the meta
2365 ret = ocfs2_journal_access(handle, inode, et_root_bh,
2366 OCFS2_JOURNAL_ACCESS_WRITE);
2372 del_right_subtree = 1;
2376 * Getting here with an empty extent in the right path implies
2377 * that it's the rightmost path and will be deleted.
2379 BUG_ON(right_has_empty && !del_right_subtree);
2381 ret = ocfs2_journal_access(handle, inode, root_bh,
2382 OCFS2_JOURNAL_ACCESS_WRITE);
2388 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2389 ret = ocfs2_journal_access(handle, inode,
2390 right_path->p_node[i].bh,
2391 OCFS2_JOURNAL_ACCESS_WRITE);
2397 ret = ocfs2_journal_access(handle, inode,
2398 left_path->p_node[i].bh,
2399 OCFS2_JOURNAL_ACCESS_WRITE);
2406 if (!right_has_empty) {
2408 * Only do this if we're moving a real
2409 * record. Otherwise, the action is delayed until
2410 * after removal of the right path in which case we
2411 * can do a simple shift to remove the empty extent.
2413 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2414 memset(&right_leaf_el->l_recs[0], 0,
2415 sizeof(struct ocfs2_extent_rec));
2417 if (eb->h_next_leaf_blk == 0ULL) {
2419 * Move recs over to get rid of empty extent, decrease
2420 * next_free. This is allowed to remove the last
2421 * extent in our leaf (setting l_next_free_rec to
2422 * zero) - the delete code below won't care.
2424 ocfs2_remove_empty_extent(right_leaf_el);
2427 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2430 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2434 if (del_right_subtree) {
2435 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2436 subtree_index, dealloc);
2437 ocfs2_update_edge_lengths(inode, handle, left_path);
2439 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2440 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2443 * Removal of the extent in the left leaf was skipped
2444 * above so we could delete the right path
2447 if (right_has_empty)
2448 ocfs2_remove_empty_extent(left_leaf_el);
2450 ret = ocfs2_journal_dirty(handle, et_root_bh);
2456 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2464 * Given a full path, determine what cpos value would return us a path
2465 * containing the leaf immediately to the right of the current one.
2467 * Will return zero if the path passed in is already the rightmost path.
2469 * This looks similar, but is subtly different to
2470 * ocfs2_find_cpos_for_left_leaf().
2472 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2473 struct ocfs2_path *path, u32 *cpos)
2477 struct ocfs2_extent_list *el;
2481 if (path->p_tree_depth == 0)
2484 blkno = path_leaf_bh(path)->b_blocknr;
2486 /* Start at the tree node just above the leaf and work our way up. */
2487 i = path->p_tree_depth - 1;
2491 el = path->p_node[i].el;
2494 * Find the extent record just after the one in our
2497 next_free = le16_to_cpu(el->l_next_free_rec);
2498 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2499 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2500 if (j == (next_free - 1)) {
2503 * We've determined that the
2504 * path specified is already
2505 * the rightmost one - return a
2511 * The rightmost record points to our
2512 * leaf - we need to travel up the
2518 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2524 * If we got here, we never found a valid node where
2525 * the tree indicated one should be.
2528 "Invalid extent tree at extent block %llu\n",
2529 (unsigned long long)blkno);
2534 blkno = path->p_node[i].bh->b_blocknr;
2542 static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2544 struct buffer_head *bh,
2545 struct ocfs2_extent_list *el)
2549 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2552 ret = ocfs2_journal_access(handle, inode, bh,
2553 OCFS2_JOURNAL_ACCESS_WRITE);
2559 ocfs2_remove_empty_extent(el);
2561 ret = ocfs2_journal_dirty(handle, bh);
2569 static int __ocfs2_rotate_tree_left(struct inode *inode,
2570 handle_t *handle, int orig_credits,
2571 struct ocfs2_path *path,
2572 struct ocfs2_cached_dealloc_ctxt *dealloc,
2573 struct ocfs2_path **empty_extent_path,
2574 struct ocfs2_extent_tree *et)
2576 int ret, subtree_root, deleted;
2578 struct ocfs2_path *left_path = NULL;
2579 struct ocfs2_path *right_path = NULL;
2581 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2583 *empty_extent_path = NULL;
2585 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2592 left_path = ocfs2_new_path(path_root_bh(path),
2593 path_root_el(path));
2600 ocfs2_cp_path(left_path, path);
2602 right_path = ocfs2_new_path(path_root_bh(path),
2603 path_root_el(path));
2610 while (right_cpos) {
2611 ret = ocfs2_find_path(inode, right_path, right_cpos);
2617 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2620 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2622 (unsigned long long)
2623 right_path->p_node[subtree_root].bh->b_blocknr,
2624 right_path->p_tree_depth);
2626 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2627 orig_credits, left_path);
2634 * Caller might still want to make changes to the
2635 * tree root, so re-add it to the journal here.
2637 ret = ocfs2_journal_access(handle, inode,
2638 path_root_bh(left_path),
2639 OCFS2_JOURNAL_ACCESS_WRITE);
2645 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2646 right_path, subtree_root,
2647 dealloc, &deleted, et);
2648 if (ret == -EAGAIN) {
2650 * The rotation has to temporarily stop due to
2651 * the right subtree having an empty
2652 * extent. Pass it back to the caller for a
2655 *empty_extent_path = right_path;
2665 * The subtree rotate might have removed records on
2666 * the rightmost edge. If so, then rotation is
2672 ocfs2_mv_path(left_path, right_path);
2674 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2683 ocfs2_free_path(right_path);
2684 ocfs2_free_path(left_path);
2689 static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2690 struct ocfs2_path *path,
2691 struct ocfs2_cached_dealloc_ctxt *dealloc,
2692 struct ocfs2_extent_tree *et)
2694 int ret, subtree_index;
2696 struct ocfs2_path *left_path = NULL;
2697 struct ocfs2_extent_block *eb;
2698 struct ocfs2_extent_list *el;
2701 ret = ocfs2_et_sanity_check(inode, et);
2705 * There's two ways we handle this depending on
2706 * whether path is the only existing one.
2708 ret = ocfs2_extend_rotate_transaction(handle, 0,
2709 handle->h_buffer_credits,
2716 ret = ocfs2_journal_access_path(inode, handle, path);
2722 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2730 * We have a path to the left of this one - it needs
2733 left_path = ocfs2_new_path(path_root_bh(path),
2734 path_root_el(path));
2741 ret = ocfs2_find_path(inode, left_path, cpos);
2747 ret = ocfs2_journal_access_path(inode, handle, left_path);
2753 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2755 ocfs2_unlink_subtree(inode, handle, left_path, path,
2756 subtree_index, dealloc);
2757 ocfs2_update_edge_lengths(inode, handle, left_path);
2759 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2760 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2763 * 'path' is also the leftmost path which
2764 * means it must be the only one. This gets
2765 * handled differently because we want to
2766 * revert the inode back to having extents
2769 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2771 el = et->et_root_el;
2772 el->l_tree_depth = 0;
2773 el->l_next_free_rec = 0;
2774 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2776 ocfs2_et_set_last_eb_blk(et, 0);
2779 ocfs2_journal_dirty(handle, path_root_bh(path));
2782 ocfs2_free_path(left_path);
2787 * Left rotation of btree records.
2789 * In many ways, this is (unsurprisingly) the opposite of right
2790 * rotation. We start at some non-rightmost path containing an empty
2791 * extent in the leaf block. The code works its way to the rightmost
2792 * path by rotating records to the left in every subtree.
2794 * This is used by any code which reduces the number of extent records
2795 * in a leaf. After removal, an empty record should be placed in the
2796 * leftmost list position.
2798 * This won't handle a length update of the rightmost path records if
2799 * the rightmost tree leaf record is removed so the caller is
2800 * responsible for detecting and correcting that.
2802 static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2803 struct ocfs2_path *path,
2804 struct ocfs2_cached_dealloc_ctxt *dealloc,
2805 struct ocfs2_extent_tree *et)
2807 int ret, orig_credits = handle->h_buffer_credits;
2808 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2809 struct ocfs2_extent_block *eb;
2810 struct ocfs2_extent_list *el;
2812 el = path_leaf_el(path);
2813 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2816 if (path->p_tree_depth == 0) {
2817 rightmost_no_delete:
2819 * Inline extents. This is trivially handled, so do
2822 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2824 path_leaf_el(path));
2831 * Handle rightmost branch now. There's several cases:
2832 * 1) simple rotation leaving records in there. That's trivial.
2833 * 2) rotation requiring a branch delete - there's no more
2834 * records left. Two cases of this:
2835 * a) There are branches to the left.
2836 * b) This is also the leftmost (the only) branch.
2838 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2839 * 2a) we need the left branch so that we can update it with the unlink
2840 * 2b) we need to bring the inode back to inline extents.
2843 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2845 if (eb->h_next_leaf_blk == 0) {
2847 * This gets a bit tricky if we're going to delete the
2848 * rightmost path. Get the other cases out of the way
2851 if (le16_to_cpu(el->l_next_free_rec) > 1)
2852 goto rightmost_no_delete;
2854 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2856 ocfs2_error(inode->i_sb,
2857 "Inode %llu has empty extent block at %llu",
2858 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2859 (unsigned long long)le64_to_cpu(eb->h_blkno));
2864 * XXX: The caller can not trust "path" any more after
2865 * this as it will have been deleted. What do we do?
2867 * In theory the rotate-for-merge code will never get
2868 * here because it'll always ask for a rotate in a
2872 ret = ocfs2_remove_rightmost_path(inode, handle, path,
2880 * Now we can loop, remembering the path we get from -EAGAIN
2881 * and restarting from there.
2884 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
2885 dealloc, &restart_path, et);
2886 if (ret && ret != -EAGAIN) {
2891 while (ret == -EAGAIN) {
2892 tmp_path = restart_path;
2893 restart_path = NULL;
2895 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2898 if (ret && ret != -EAGAIN) {
2903 ocfs2_free_path(tmp_path);
2911 ocfs2_free_path(tmp_path);
2912 ocfs2_free_path(restart_path);
2916 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2919 struct ocfs2_extent_rec *rec = &el->l_recs[index];
2922 if (rec->e_leaf_clusters == 0) {
2924 * We consumed all of the merged-from record. An empty
2925 * extent cannot exist anywhere but the 1st array
2926 * position, so move things over if the merged-from
2927 * record doesn't occupy that position.
2929 * This creates a new empty extent so the caller
2930 * should be smart enough to have removed any existing
2934 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
2935 size = index * sizeof(struct ocfs2_extent_rec);
2936 memmove(&el->l_recs[1], &el->l_recs[0], size);
2940 * Always memset - the caller doesn't check whether it
2941 * created an empty extent, so there could be junk in
2944 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2948 static int ocfs2_get_right_path(struct inode *inode,
2949 struct ocfs2_path *left_path,
2950 struct ocfs2_path **ret_right_path)
2954 struct ocfs2_path *right_path = NULL;
2955 struct ocfs2_extent_list *left_el;
2957 *ret_right_path = NULL;
2959 /* This function shouldn't be called for non-trees. */
2960 BUG_ON(left_path->p_tree_depth == 0);
2962 left_el = path_leaf_el(left_path);
2963 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
2965 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2972 /* This function shouldn't be called for the rightmost leaf. */
2973 BUG_ON(right_cpos == 0);
2975 right_path = ocfs2_new_path(path_root_bh(left_path),
2976 path_root_el(left_path));
2983 ret = ocfs2_find_path(inode, right_path, right_cpos);
2989 *ret_right_path = right_path;
2992 ocfs2_free_path(right_path);
2997 * Remove split_rec clusters from the record at index and merge them
2998 * onto the beginning of the record "next" to it.
2999 * For index < l_count - 1, the next means the extent rec at index + 1.
3000 * For index == l_count - 1, the "next" means the 1st extent rec of the
3001 * next extent block.
3003 static int ocfs2_merge_rec_right(struct inode *inode,
3004 struct ocfs2_path *left_path,
3006 struct ocfs2_extent_rec *split_rec,
3009 int ret, next_free, i;
3010 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3011 struct ocfs2_extent_rec *left_rec;
3012 struct ocfs2_extent_rec *right_rec;
3013 struct ocfs2_extent_list *right_el;
3014 struct ocfs2_path *right_path = NULL;
3015 int subtree_index = 0;
3016 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3017 struct buffer_head *bh = path_leaf_bh(left_path);
3018 struct buffer_head *root_bh = NULL;
3020 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3021 left_rec = &el->l_recs[index];
3023 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3024 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3025 /* we meet with a cross extent block merge. */
3026 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3032 right_el = path_leaf_el(right_path);
3033 next_free = le16_to_cpu(right_el->l_next_free_rec);
3034 BUG_ON(next_free <= 0);
3035 right_rec = &right_el->l_recs[0];
3036 if (ocfs2_is_empty_extent(right_rec)) {
3037 BUG_ON(next_free <= 1);
3038 right_rec = &right_el->l_recs[1];
3041 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3042 le16_to_cpu(left_rec->e_leaf_clusters) !=
3043 le32_to_cpu(right_rec->e_cpos));
3045 subtree_index = ocfs2_find_subtree_root(inode,
3046 left_path, right_path);
3048 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3049 handle->h_buffer_credits,
3056 root_bh = left_path->p_node[subtree_index].bh;
3057 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3059 ret = ocfs2_journal_access(handle, inode, root_bh,
3060 OCFS2_JOURNAL_ACCESS_WRITE);
3066 for (i = subtree_index + 1;
3067 i < path_num_items(right_path); i++) {
3068 ret = ocfs2_journal_access(handle, inode,
3069 right_path->p_node[i].bh,
3070 OCFS2_JOURNAL_ACCESS_WRITE);
3076 ret = ocfs2_journal_access(handle, inode,
3077 left_path->p_node[i].bh,
3078 OCFS2_JOURNAL_ACCESS_WRITE);
3086 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3087 right_rec = &el->l_recs[index + 1];
3090 ret = ocfs2_journal_access(handle, inode, bh,
3091 OCFS2_JOURNAL_ACCESS_WRITE);
3097 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3099 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3100 le64_add_cpu(&right_rec->e_blkno,
3101 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3102 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3104 ocfs2_cleanup_merge(el, index);
3106 ret = ocfs2_journal_dirty(handle, bh);
3111 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3115 ocfs2_complete_edge_insert(inode, handle, left_path,
3116 right_path, subtree_index);
3120 ocfs2_free_path(right_path);
3124 static int ocfs2_get_left_path(struct inode *inode,
3125 struct ocfs2_path *right_path,
3126 struct ocfs2_path **ret_left_path)
3130 struct ocfs2_path *left_path = NULL;
3132 *ret_left_path = NULL;
3134 /* This function shouldn't be called for non-trees. */
3135 BUG_ON(right_path->p_tree_depth == 0);
3137 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3138 right_path, &left_cpos);
3144 /* This function shouldn't be called for the leftmost leaf. */
3145 BUG_ON(left_cpos == 0);
3147 left_path = ocfs2_new_path(path_root_bh(right_path),
3148 path_root_el(right_path));
3155 ret = ocfs2_find_path(inode, left_path, left_cpos);
3161 *ret_left_path = left_path;
3164 ocfs2_free_path(left_path);
3169 * Remove split_rec clusters from the record at index and merge them
3170 * onto the tail of the record "before" it.
3171 * For index > 0, the "before" means the extent rec at index - 1.
3173 * For index == 0, the "before" means the last record of the previous
3174 * extent block. And there is also a situation that we may need to
3175 * remove the rightmost leaf extent block in the right_path and change
3176 * the right path to indicate the new rightmost path.
3178 static int ocfs2_merge_rec_left(struct inode *inode,
3179 struct ocfs2_path *right_path,
3181 struct ocfs2_extent_rec *split_rec,
3182 struct ocfs2_cached_dealloc_ctxt *dealloc,
3183 struct ocfs2_extent_tree *et,
3186 int ret, i, subtree_index = 0, has_empty_extent = 0;
3187 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3188 struct ocfs2_extent_rec *left_rec;
3189 struct ocfs2_extent_rec *right_rec;
3190 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3191 struct buffer_head *bh = path_leaf_bh(right_path);
3192 struct buffer_head *root_bh = NULL;
3193 struct ocfs2_path *left_path = NULL;
3194 struct ocfs2_extent_list *left_el;
3198 right_rec = &el->l_recs[index];
3200 /* we meet with a cross extent block merge. */
3201 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3207 left_el = path_leaf_el(left_path);
3208 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3209 le16_to_cpu(left_el->l_count));
3211 left_rec = &left_el->l_recs[
3212 le16_to_cpu(left_el->l_next_free_rec) - 1];
3213 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3214 le16_to_cpu(left_rec->e_leaf_clusters) !=
3215 le32_to_cpu(split_rec->e_cpos));
3217 subtree_index = ocfs2_find_subtree_root(inode,
3218 left_path, right_path);
3220 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3221 handle->h_buffer_credits,
3228 root_bh = left_path->p_node[subtree_index].bh;
3229 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3231 ret = ocfs2_journal_access(handle, inode, root_bh,
3232 OCFS2_JOURNAL_ACCESS_WRITE);
3238 for (i = subtree_index + 1;
3239 i < path_num_items(right_path); i++) {
3240 ret = ocfs2_journal_access(handle, inode,
3241 right_path->p_node[i].bh,
3242 OCFS2_JOURNAL_ACCESS_WRITE);
3248 ret = ocfs2_journal_access(handle, inode,
3249 left_path->p_node[i].bh,
3250 OCFS2_JOURNAL_ACCESS_WRITE);
3257 left_rec = &el->l_recs[index - 1];
3258 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3259 has_empty_extent = 1;
3262 ret = ocfs2_journal_access(handle, inode, bh,
3263 OCFS2_JOURNAL_ACCESS_WRITE);
3269 if (has_empty_extent && index == 1) {
3271 * The easy case - we can just plop the record right in.
3273 *left_rec = *split_rec;
3275 has_empty_extent = 0;
3277 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3279 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3280 le64_add_cpu(&right_rec->e_blkno,
3281 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3282 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3284 ocfs2_cleanup_merge(el, index);
3286 ret = ocfs2_journal_dirty(handle, bh);
3291 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3296 * In the situation that the right_rec is empty and the extent
3297 * block is empty also, ocfs2_complete_edge_insert can't handle
3298 * it and we need to delete the right extent block.
3300 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3301 le16_to_cpu(el->l_next_free_rec) == 1) {
3303 ret = ocfs2_remove_rightmost_path(inode, handle,
3311 /* Now the rightmost extent block has been deleted.
3312 * So we use the new rightmost path.
3314 ocfs2_mv_path(right_path, left_path);
3317 ocfs2_complete_edge_insert(inode, handle, left_path,
3318 right_path, subtree_index);
3322 ocfs2_free_path(left_path);
3326 static int ocfs2_try_to_merge_extent(struct inode *inode,
3328 struct ocfs2_path *path,
3330 struct ocfs2_extent_rec *split_rec,
3331 struct ocfs2_cached_dealloc_ctxt *dealloc,
3332 struct ocfs2_merge_ctxt *ctxt,
3333 struct ocfs2_extent_tree *et)
3337 struct ocfs2_extent_list *el = path_leaf_el(path);
3338 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3340 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3342 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3344 * The merge code will need to create an empty
3345 * extent to take the place of the newly
3346 * emptied slot. Remove any pre-existing empty
3347 * extents - having more than one in a leaf is
3350 ret = ocfs2_rotate_tree_left(inode, handle, path,
3357 rec = &el->l_recs[split_index];
3360 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3362 * Left-right contig implies this.
3364 BUG_ON(!ctxt->c_split_covers_rec);
3367 * Since the leftright insert always covers the entire
3368 * extent, this call will delete the insert record
3369 * entirely, resulting in an empty extent record added to
3372 * Since the adding of an empty extent shifts
3373 * everything back to the right, there's no need to
3374 * update split_index here.
3376 * When the split_index is zero, we need to merge it to the
3377 * prevoius extent block. It is more efficient and easier
3378 * if we do merge_right first and merge_left later.
3380 ret = ocfs2_merge_rec_right(inode, path,
3389 * We can only get this from logic error above.
3391 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3393 /* The merge left us with an empty extent, remove it. */
3394 ret = ocfs2_rotate_tree_left(inode, handle, path,
3401 rec = &el->l_recs[split_index];
3404 * Note that we don't pass split_rec here on purpose -
3405 * we've merged it into the rec already.
3407 ret = ocfs2_merge_rec_left(inode, path,
3417 ret = ocfs2_rotate_tree_left(inode, handle, path,
3420 * Error from this last rotate is not critical, so
3421 * print but don't bubble it up.
3428 * Merge a record to the left or right.
3430 * 'contig_type' is relative to the existing record,
3431 * so for example, if we're "right contig", it's to
3432 * the record on the left (hence the left merge).
3434 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3435 ret = ocfs2_merge_rec_left(inode,
3445 ret = ocfs2_merge_rec_right(inode,
3455 if (ctxt->c_split_covers_rec) {
3457 * The merge may have left an empty extent in
3458 * our leaf. Try to rotate it away.
3460 ret = ocfs2_rotate_tree_left(inode, handle, path,
3472 static void ocfs2_subtract_from_rec(struct super_block *sb,
3473 enum ocfs2_split_type split,
3474 struct ocfs2_extent_rec *rec,
3475 struct ocfs2_extent_rec *split_rec)
3479 len_blocks = ocfs2_clusters_to_blocks(sb,
3480 le16_to_cpu(split_rec->e_leaf_clusters));
3482 if (split == SPLIT_LEFT) {
3484 * Region is on the left edge of the existing
3487 le32_add_cpu(&rec->e_cpos,
3488 le16_to_cpu(split_rec->e_leaf_clusters));
3489 le64_add_cpu(&rec->e_blkno, len_blocks);
3490 le16_add_cpu(&rec->e_leaf_clusters,
3491 -le16_to_cpu(split_rec->e_leaf_clusters));
3494 * Region is on the right edge of the existing
3497 le16_add_cpu(&rec->e_leaf_clusters,
3498 -le16_to_cpu(split_rec->e_leaf_clusters));
3503 * Do the final bits of extent record insertion at the target leaf
3504 * list. If this leaf is part of an allocation tree, it is assumed
3505 * that the tree above has been prepared.
3507 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3508 struct ocfs2_extent_list *el,
3509 struct ocfs2_insert_type *insert,
3510 struct inode *inode)
3512 int i = insert->ins_contig_index;
3514 struct ocfs2_extent_rec *rec;
3516 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3518 if (insert->ins_split != SPLIT_NONE) {
3519 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3521 rec = &el->l_recs[i];
3522 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3528 * Contiguous insert - either left or right.
3530 if (insert->ins_contig != CONTIG_NONE) {
3531 rec = &el->l_recs[i];
3532 if (insert->ins_contig == CONTIG_LEFT) {
3533 rec->e_blkno = insert_rec->e_blkno;
3534 rec->e_cpos = insert_rec->e_cpos;
3536 le16_add_cpu(&rec->e_leaf_clusters,
3537 le16_to_cpu(insert_rec->e_leaf_clusters));
3542 * Handle insert into an empty leaf.
3544 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3545 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3546 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3547 el->l_recs[0] = *insert_rec;
3548 el->l_next_free_rec = cpu_to_le16(1);
3555 if (insert->ins_appending == APPEND_TAIL) {
3556 i = le16_to_cpu(el->l_next_free_rec) - 1;
3557 rec = &el->l_recs[i];
3558 range = le32_to_cpu(rec->e_cpos)
3559 + le16_to_cpu(rec->e_leaf_clusters);
3560 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3562 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3563 le16_to_cpu(el->l_count),
3564 "inode %lu, depth %u, count %u, next free %u, "
3565 "rec.cpos %u, rec.clusters %u, "
3566 "insert.cpos %u, insert.clusters %u\n",
3568 le16_to_cpu(el->l_tree_depth),
3569 le16_to_cpu(el->l_count),
3570 le16_to_cpu(el->l_next_free_rec),
3571 le32_to_cpu(el->l_recs[i].e_cpos),
3572 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3573 le32_to_cpu(insert_rec->e_cpos),
3574 le16_to_cpu(insert_rec->e_leaf_clusters));
3576 el->l_recs[i] = *insert_rec;
3577 le16_add_cpu(&el->l_next_free_rec, 1);
3583 * Ok, we have to rotate.
3585 * At this point, it is safe to assume that inserting into an
3586 * empty leaf and appending to a leaf have both been handled
3589 * This leaf needs to have space, either by the empty 1st
3590 * extent record, or by virtue of an l_next_rec < l_count.
3592 ocfs2_rotate_leaf(el, insert_rec);
3595 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3597 struct ocfs2_path *path,
3598 struct ocfs2_extent_rec *insert_rec)
3600 int ret, i, next_free;
3601 struct buffer_head *bh;
3602 struct ocfs2_extent_list *el;
3603 struct ocfs2_extent_rec *rec;
3606 * Update everything except the leaf block.
3608 for (i = 0; i < path->p_tree_depth; i++) {
3609 bh = path->p_node[i].bh;
3610 el = path->p_node[i].el;
3612 next_free = le16_to_cpu(el->l_next_free_rec);
3613 if (next_free == 0) {
3614 ocfs2_error(inode->i_sb,
3615 "Dinode %llu has a bad extent list",
3616 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3621 rec = &el->l_recs[next_free - 1];
3623 rec->e_int_clusters = insert_rec->e_cpos;
3624 le32_add_cpu(&rec->e_int_clusters,
3625 le16_to_cpu(insert_rec->e_leaf_clusters));
3626 le32_add_cpu(&rec->e_int_clusters,
3627 -le32_to_cpu(rec->e_cpos));
3629 ret = ocfs2_journal_dirty(handle, bh);
3636 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3637 struct ocfs2_extent_rec *insert_rec,
3638 struct ocfs2_path *right_path,
3639 struct ocfs2_path **ret_left_path)
3642 struct ocfs2_extent_list *el;
3643 struct ocfs2_path *left_path = NULL;
3645 *ret_left_path = NULL;
3648 * This shouldn't happen for non-trees. The extent rec cluster
3649 * count manipulation below only works for interior nodes.
3651 BUG_ON(right_path->p_tree_depth == 0);
3654 * If our appending insert is at the leftmost edge of a leaf,
3655 * then we might need to update the rightmost records of the
3658 el = path_leaf_el(right_path);
3659 next_free = le16_to_cpu(el->l_next_free_rec);
3660 if (next_free == 0 ||
3661 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3664 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3671 mlog(0, "Append may need a left path update. cpos: %u, "
3672 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3676 * No need to worry if the append is already in the
3680 left_path = ocfs2_new_path(path_root_bh(right_path),
3681 path_root_el(right_path));
3688 ret = ocfs2_find_path(inode, left_path, left_cpos);
3695 * ocfs2_insert_path() will pass the left_path to the
3701 ret = ocfs2_journal_access_path(inode, handle, right_path);
3707 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
3709 *ret_left_path = left_path;
3713 ocfs2_free_path(left_path);
3718 static void ocfs2_split_record(struct inode *inode,
3719 struct ocfs2_path *left_path,
3720 struct ocfs2_path *right_path,
3721 struct ocfs2_extent_rec *split_rec,
3722 enum ocfs2_split_type split)
3725 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3726 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3727 struct ocfs2_extent_rec *rec, *tmprec;
3729 right_el = path_leaf_el(right_path);;
3731 left_el = path_leaf_el(left_path);
3734 insert_el = right_el;
3735 index = ocfs2_search_extent_list(el, cpos);
3737 if (index == 0 && left_path) {
3738 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3741 * This typically means that the record
3742 * started in the left path but moved to the
3743 * right as a result of rotation. We either
3744 * move the existing record to the left, or we
3745 * do the later insert there.
3747 * In this case, the left path should always
3748 * exist as the rotate code will have passed
3749 * it back for a post-insert update.
3752 if (split == SPLIT_LEFT) {
3754 * It's a left split. Since we know
3755 * that the rotate code gave us an
3756 * empty extent in the left path, we
3757 * can just do the insert there.
3759 insert_el = left_el;
3762 * Right split - we have to move the
3763 * existing record over to the left
3764 * leaf. The insert will be into the
3765 * newly created empty extent in the
3768 tmprec = &right_el->l_recs[index];
3769 ocfs2_rotate_leaf(left_el, tmprec);
3772 memset(tmprec, 0, sizeof(*tmprec));
3773 index = ocfs2_search_extent_list(left_el, cpos);
3774 BUG_ON(index == -1);
3779 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3781 * Left path is easy - we can just allow the insert to
3785 insert_el = left_el;
3786 index = ocfs2_search_extent_list(el, cpos);
3787 BUG_ON(index == -1);
3790 rec = &el->l_recs[index];
3791 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3792 ocfs2_rotate_leaf(insert_el, split_rec);
3796 * This function only does inserts on an allocation b-tree. For tree
3797 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3799 * right_path is the path we want to do the actual insert
3800 * in. left_path should only be passed in if we need to update that
3801 * portion of the tree after an edge insert.
3803 static int ocfs2_insert_path(struct inode *inode,
3805 struct ocfs2_path *left_path,
3806 struct ocfs2_path *right_path,
3807 struct ocfs2_extent_rec *insert_rec,
3808 struct ocfs2_insert_type *insert)
3810 int ret, subtree_index;
3811 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
3814 int credits = handle->h_buffer_credits;
3817 * There's a chance that left_path got passed back to
3818 * us without being accounted for in the
3819 * journal. Extend our transaction here to be sure we
3820 * can change those blocks.
3822 credits += left_path->p_tree_depth;
3824 ret = ocfs2_extend_trans(handle, credits);
3830 ret = ocfs2_journal_access_path(inode, handle, left_path);
3838 * Pass both paths to the journal. The majority of inserts
3839 * will be touching all components anyway.
3841 ret = ocfs2_journal_access_path(inode, handle, right_path);
3847 if (insert->ins_split != SPLIT_NONE) {
3849 * We could call ocfs2_insert_at_leaf() for some types
3850 * of splits, but it's easier to just let one separate
3851 * function sort it all out.
3853 ocfs2_split_record(inode, left_path, right_path,
3854 insert_rec, insert->ins_split);
3857 * Split might have modified either leaf and we don't
3858 * have a guarantee that the later edge insert will
3859 * dirty this for us.
3862 ret = ocfs2_journal_dirty(handle,
3863 path_leaf_bh(left_path));
3867 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3870 ret = ocfs2_journal_dirty(handle, leaf_bh);
3876 * The rotate code has indicated that we need to fix
3877 * up portions of the tree after the insert.
3879 * XXX: Should we extend the transaction here?
3881 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3883 ocfs2_complete_edge_insert(inode, handle, left_path,
3884 right_path, subtree_index);
3892 static int ocfs2_do_insert_extent(struct inode *inode,
3894 struct ocfs2_extent_tree *et,
3895 struct ocfs2_extent_rec *insert_rec,
3896 struct ocfs2_insert_type *type)
3898 int ret, rotate = 0;
3900 struct ocfs2_path *right_path = NULL;
3901 struct ocfs2_path *left_path = NULL;
3902 struct ocfs2_extent_list *el;
3904 el = et->et_root_el;
3906 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
3907 OCFS2_JOURNAL_ACCESS_WRITE);
3913 if (le16_to_cpu(el->l_tree_depth) == 0) {
3914 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3915 goto out_update_clusters;
3918 right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
3926 * Determine the path to start with. Rotations need the
3927 * rightmost path, everything else can go directly to the
3930 cpos = le32_to_cpu(insert_rec->e_cpos);
3931 if (type->ins_appending == APPEND_NONE &&
3932 type->ins_contig == CONTIG_NONE) {
3937 ret = ocfs2_find_path(inode, right_path, cpos);
3944 * Rotations and appends need special treatment - they modify
3945 * parts of the tree's above them.
3947 * Both might pass back a path immediate to the left of the
3948 * one being inserted to. This will be cause
3949 * ocfs2_insert_path() to modify the rightmost records of
3950 * left_path to account for an edge insert.
3952 * XXX: When modifying this code, keep in mind that an insert
3953 * can wind up skipping both of these two special cases...
3956 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
3957 le32_to_cpu(insert_rec->e_cpos),
3958 right_path, &left_path);
3965 * ocfs2_rotate_tree_right() might have extended the
3966 * transaction without re-journaling our tree root.
3968 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
3969 OCFS2_JOURNAL_ACCESS_WRITE);
3974 } else if (type->ins_appending == APPEND_TAIL
3975 && type->ins_contig != CONTIG_LEFT) {
3976 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
3977 right_path, &left_path);
3984 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
3991 out_update_clusters:
3992 if (type->ins_split == SPLIT_NONE)
3993 ocfs2_et_update_clusters(inode, et,
3994 le16_to_cpu(insert_rec->e_leaf_clusters));
3996 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
4001 ocfs2_free_path(left_path);
4002 ocfs2_free_path(right_path);
4007 static enum ocfs2_contig_type
4008 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
4009 struct ocfs2_extent_list *el, int index,
4010 struct ocfs2_extent_rec *split_rec)
4013 enum ocfs2_contig_type ret = CONTIG_NONE;
4014 u32 left_cpos, right_cpos;
4015 struct ocfs2_extent_rec *rec = NULL;
4016 struct ocfs2_extent_list *new_el;
4017 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4018 struct buffer_head *bh;
4019 struct ocfs2_extent_block *eb;
4022 rec = &el->l_recs[index - 1];
4023 } else if (path->p_tree_depth > 0) {
4024 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4029 if (left_cpos != 0) {
4030 left_path = ocfs2_new_path(path_root_bh(path),
4031 path_root_el(path));
4035 status = ocfs2_find_path(inode, left_path, left_cpos);
4039 new_el = path_leaf_el(left_path);
4041 if (le16_to_cpu(new_el->l_next_free_rec) !=
4042 le16_to_cpu(new_el->l_count)) {
4043 bh = path_leaf_bh(left_path);
4044 eb = (struct ocfs2_extent_block *)bh->b_data;
4045 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4049 rec = &new_el->l_recs[
4050 le16_to_cpu(new_el->l_next_free_rec) - 1];
4055 * We're careful to check for an empty extent record here -
4056 * the merge code will know what to do if it sees one.
4059 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4060 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4063 ret = ocfs2_extent_contig(inode, rec, split_rec);
4068 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4069 rec = &el->l_recs[index + 1];
4070 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4071 path->p_tree_depth > 0) {
4072 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4077 if (right_cpos == 0)
4080 right_path = ocfs2_new_path(path_root_bh(path),
4081 path_root_el(path));
4085 status = ocfs2_find_path(inode, right_path, right_cpos);
4089 new_el = path_leaf_el(right_path);
4090 rec = &new_el->l_recs[0];
4091 if (ocfs2_is_empty_extent(rec)) {
4092 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4093 bh = path_leaf_bh(right_path);
4094 eb = (struct ocfs2_extent_block *)bh->b_data;
4095 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4099 rec = &new_el->l_recs[1];
4104 enum ocfs2_contig_type contig_type;
4106 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4108 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4109 ret = CONTIG_LEFTRIGHT;
4110 else if (ret == CONTIG_NONE)
4116 ocfs2_free_path(left_path);
4118 ocfs2_free_path(right_path);
4123 static void ocfs2_figure_contig_type(struct inode *inode,
4124 struct ocfs2_insert_type *insert,
4125 struct ocfs2_extent_list *el,
4126 struct ocfs2_extent_rec *insert_rec,
4127 struct ocfs2_extent_tree *et)
4130 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4132 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4134 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4135 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4137 if (contig_type != CONTIG_NONE) {
4138 insert->ins_contig_index = i;
4142 insert->ins_contig = contig_type;
4144 if (insert->ins_contig != CONTIG_NONE) {
4145 struct ocfs2_extent_rec *rec =
4146 &el->l_recs[insert->ins_contig_index];
4147 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4148 le16_to_cpu(insert_rec->e_leaf_clusters);
4151 * Caller might want us to limit the size of extents, don't
4152 * calculate contiguousness if we might exceed that limit.
4154 if (et->et_max_leaf_clusters &&
4155 (len > et->et_max_leaf_clusters))
4156 insert->ins_contig = CONTIG_NONE;
4161 * This should only be called against the righmost leaf extent list.
4163 * ocfs2_figure_appending_type() will figure out whether we'll have to
4164 * insert at the tail of the rightmost leaf.
4166 * This should also work against the root extent list for tree's with 0
4167 * depth. If we consider the root extent list to be the rightmost leaf node
4168 * then the logic here makes sense.
4170 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4171 struct ocfs2_extent_list *el,
4172 struct ocfs2_extent_rec *insert_rec)
4175 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4176 struct ocfs2_extent_rec *rec;
4178 insert->ins_appending = APPEND_NONE;
4180 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4182 if (!el->l_next_free_rec)
4183 goto set_tail_append;
4185 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4186 /* Were all records empty? */
4187 if (le16_to_cpu(el->l_next_free_rec) == 1)
4188 goto set_tail_append;
4191 i = le16_to_cpu(el->l_next_free_rec) - 1;
4192 rec = &el->l_recs[i];
4195 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4196 goto set_tail_append;
4201 insert->ins_appending = APPEND_TAIL;
4205 * Helper function called at the begining of an insert.
4207 * This computes a few things that are commonly used in the process of
4208 * inserting into the btree:
4209 * - Whether the new extent is contiguous with an existing one.
4210 * - The current tree depth.
4211 * - Whether the insert is an appending one.
4212 * - The total # of free records in the tree.
4214 * All of the information is stored on the ocfs2_insert_type
4217 static int ocfs2_figure_insert_type(struct inode *inode,
4218 struct ocfs2_extent_tree *et,
4219 struct buffer_head **last_eb_bh,
4220 struct ocfs2_extent_rec *insert_rec,
4222 struct ocfs2_insert_type *insert)
4225 struct ocfs2_extent_block *eb;
4226 struct ocfs2_extent_list *el;
4227 struct ocfs2_path *path = NULL;
4228 struct buffer_head *bh = NULL;
4230 insert->ins_split = SPLIT_NONE;
4232 el = et->et_root_el;
4233 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4235 if (el->l_tree_depth) {
4237 * If we have tree depth, we read in the
4238 * rightmost extent block ahead of time as
4239 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4240 * may want it later.
4242 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4243 ocfs2_et_get_last_eb_blk(et), &bh,
4244 OCFS2_BH_CACHED, inode);
4249 eb = (struct ocfs2_extent_block *) bh->b_data;
4254 * Unless we have a contiguous insert, we'll need to know if
4255 * there is room left in our allocation tree for another
4258 * XXX: This test is simplistic, we can search for empty
4259 * extent records too.
4261 *free_records = le16_to_cpu(el->l_count) -
4262 le16_to_cpu(el->l_next_free_rec);
4264 if (!insert->ins_tree_depth) {
4265 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4266 ocfs2_figure_appending_type(insert, el, insert_rec);
4270 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4278 * In the case that we're inserting past what the tree
4279 * currently accounts for, ocfs2_find_path() will return for
4280 * us the rightmost tree path. This is accounted for below in
4281 * the appending code.
4283 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4289 el = path_leaf_el(path);
4292 * Now that we have the path, there's two things we want to determine:
4293 * 1) Contiguousness (also set contig_index if this is so)
4295 * 2) Are we doing an append? We can trivially break this up
4296 * into two types of appends: simple record append, or a
4297 * rotate inside the tail leaf.
4299 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4302 * The insert code isn't quite ready to deal with all cases of
4303 * left contiguousness. Specifically, if it's an insert into
4304 * the 1st record in a leaf, it will require the adjustment of
4305 * cluster count on the last record of the path directly to it's
4306 * left. For now, just catch that case and fool the layers
4307 * above us. This works just fine for tree_depth == 0, which
4308 * is why we allow that above.
4310 if (insert->ins_contig == CONTIG_LEFT &&
4311 insert->ins_contig_index == 0)
4312 insert->ins_contig = CONTIG_NONE;
4315 * Ok, so we can simply compare against last_eb to figure out
4316 * whether the path doesn't exist. This will only happen in
4317 * the case that we're doing a tail append, so maybe we can
4318 * take advantage of that information somehow.
4320 if (ocfs2_et_get_last_eb_blk(et) ==
4321 path_leaf_bh(path)->b_blocknr) {
4323 * Ok, ocfs2_find_path() returned us the rightmost
4324 * tree path. This might be an appending insert. There are
4326 * 1) We're doing a true append at the tail:
4327 * -This might even be off the end of the leaf
4328 * 2) We're "appending" by rotating in the tail
4330 ocfs2_figure_appending_type(insert, el, insert_rec);
4334 ocfs2_free_path(path);
4344 * Insert an extent into an inode btree.
4346 * The caller needs to update fe->i_clusters
4348 static int ocfs2_insert_extent(struct ocfs2_super *osb,
4350 struct inode *inode,
4351 struct buffer_head *root_bh,
4356 struct ocfs2_alloc_context *meta_ac,
4357 struct ocfs2_extent_tree *et)
4360 int uninitialized_var(free_records);
4361 struct buffer_head *last_eb_bh = NULL;
4362 struct ocfs2_insert_type insert = {0, };
4363 struct ocfs2_extent_rec rec;
4365 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
4367 mlog(0, "add %u clusters at position %u to inode %llu\n",
4368 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4370 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
4371 (OCFS2_I(inode)->ip_clusters != cpos),
4372 "Device %s, asking for sparse allocation: inode %llu, "
4373 "cpos %u, clusters %u\n",
4375 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
4376 OCFS2_I(inode)->ip_clusters);
4378 memset(&rec, 0, sizeof(rec));
4379 rec.e_cpos = cpu_to_le32(cpos);
4380 rec.e_blkno = cpu_to_le64(start_blk);
4381 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4382 rec.e_flags = flags;
4384 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4385 &free_records, &insert);
4391 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4392 "Insert.contig_index: %d, Insert.free_records: %d, "
4393 "Insert.tree_depth: %d\n",
4394 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4395 free_records, insert.ins_tree_depth);
4397 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4398 status = ocfs2_grow_tree(inode, handle, et,
4399 &insert.ins_tree_depth, &last_eb_bh,
4407 /* Finally, we can add clusters. This might rotate the tree for us. */
4408 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4411 else if (et->et_type == OCFS2_DINODE_EXTENT)
4412 ocfs2_extent_map_insert_rec(inode, &rec);
4422 int ocfs2_dinode_insert_extent(struct ocfs2_super *osb,
4424 struct inode *inode,
4425 struct buffer_head *root_bh,
4430 struct ocfs2_alloc_context *meta_ac)
4433 struct ocfs2_extent_tree *et = NULL;
4435 et = ocfs2_new_extent_tree(inode, root_bh, OCFS2_DINODE_EXTENT, NULL);
4442 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4443 cpos, start_blk, new_clusters,
4444 flags, meta_ac, et);
4447 ocfs2_free_extent_tree(et);
4452 int ocfs2_xattr_value_insert_extent(struct ocfs2_super *osb,
4454 struct inode *inode,
4455 struct buffer_head *root_bh,
4460 struct ocfs2_alloc_context *meta_ac,
4464 struct ocfs2_extent_tree *et = NULL;
4466 et = ocfs2_new_extent_tree(inode, root_bh,
4467 OCFS2_XATTR_VALUE_EXTENT, private);
4474 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4475 cpos, start_blk, new_clusters,
4476 flags, meta_ac, et);
4479 ocfs2_free_extent_tree(et);
4484 int ocfs2_xattr_tree_insert_extent(struct ocfs2_super *osb,
4486 struct inode *inode,
4487 struct buffer_head *root_bh,
4492 struct ocfs2_alloc_context *meta_ac)
4495 struct ocfs2_extent_tree *et = NULL;
4497 et = ocfs2_new_extent_tree(inode, root_bh, OCFS2_XATTR_TREE_EXTENT,
4505 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4506 cpos, start_blk, new_clusters,
4507 flags, meta_ac, et);
4510 ocfs2_free_extent_tree(et);
4516 * Allcate and add clusters into the extent b-tree.
4517 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4518 * The extent b-tree's root is root_el and it should be in root_bh, and
4519 * it is not limited to the file storage. Any extent tree can use this
4520 * function if it implements the proper ocfs2_extent_tree.
4522 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4523 struct inode *inode,
4524 u32 *logical_offset,
4525 u32 clusters_to_add,
4527 struct buffer_head *root_bh,
4528 struct ocfs2_extent_list *root_el,
4530 struct ocfs2_alloc_context *data_ac,
4531 struct ocfs2_alloc_context *meta_ac,
4532 enum ocfs2_alloc_restarted *reason_ret,
4533 enum ocfs2_extent_tree_type type,
4538 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4539 u32 bit_off, num_bits;
4543 BUG_ON(!clusters_to_add);
4546 flags = OCFS2_EXT_UNWRITTEN;
4548 free_extents = ocfs2_num_free_extents(osb, inode, root_bh, type,
4550 if (free_extents < 0) {
4551 status = free_extents;
4556 /* there are two cases which could cause us to EAGAIN in the
4557 * we-need-more-metadata case:
4558 * 1) we haven't reserved *any*
4559 * 2) we are so fragmented, we've needed to add metadata too
4561 if (!free_extents && !meta_ac) {
4562 mlog(0, "we haven't reserved any metadata!\n");
4564 reason = RESTART_META;
4566 } else if ((!free_extents)
4567 && (ocfs2_alloc_context_bits_left(meta_ac)
4568 < ocfs2_extend_meta_needed(root_el))) {
4569 mlog(0, "filesystem is really fragmented...\n");
4571 reason = RESTART_META;
4575 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4576 clusters_to_add, &bit_off, &num_bits);
4578 if (status != -ENOSPC)
4583 BUG_ON(num_bits > clusters_to_add);
4585 /* reserve our write early -- insert_extent may update the inode */
4586 status = ocfs2_journal_access(handle, inode, root_bh,
4587 OCFS2_JOURNAL_ACCESS_WRITE);
4593 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4594 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4595 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4596 if (type == OCFS2_DINODE_EXTENT)
4597 status = ocfs2_dinode_insert_extent(osb, handle, inode, root_bh,
4598 *logical_offset, block,
4599 num_bits, flags, meta_ac);
4600 else if (type == OCFS2_XATTR_TREE_EXTENT)
4601 status = ocfs2_xattr_tree_insert_extent(osb, handle,
4604 block, num_bits, flags,
4607 status = ocfs2_xattr_value_insert_extent(osb, handle,
4610 block, num_bits, flags,
4617 status = ocfs2_journal_dirty(handle, root_bh);
4623 clusters_to_add -= num_bits;
4624 *logical_offset += num_bits;
4626 if (clusters_to_add) {
4627 mlog(0, "need to alloc once more, wanted = %u\n",
4630 reason = RESTART_TRANS;
4636 *reason_ret = reason;
4640 static void ocfs2_make_right_split_rec(struct super_block *sb,
4641 struct ocfs2_extent_rec *split_rec,
4643 struct ocfs2_extent_rec *rec)
4645 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4646 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4648 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4650 split_rec->e_cpos = cpu_to_le32(cpos);
4651 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4653 split_rec->e_blkno = rec->e_blkno;
4654 le64_add_cpu(&split_rec->e_blkno,
4655 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4657 split_rec->e_flags = rec->e_flags;
4660 static int ocfs2_split_and_insert(struct inode *inode,
4662 struct ocfs2_path *path,
4663 struct ocfs2_extent_tree *et,
4664 struct buffer_head **last_eb_bh,
4666 struct ocfs2_extent_rec *orig_split_rec,
4667 struct ocfs2_alloc_context *meta_ac)
4670 unsigned int insert_range, rec_range, do_leftright = 0;
4671 struct ocfs2_extent_rec tmprec;
4672 struct ocfs2_extent_list *rightmost_el;
4673 struct ocfs2_extent_rec rec;
4674 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4675 struct ocfs2_insert_type insert;
4676 struct ocfs2_extent_block *eb;
4680 * Store a copy of the record on the stack - it might move
4681 * around as the tree is manipulated below.
4683 rec = path_leaf_el(path)->l_recs[split_index];
4685 rightmost_el = et->et_root_el;
4687 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4689 BUG_ON(!(*last_eb_bh));
4690 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4691 rightmost_el = &eb->h_list;
4694 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4695 le16_to_cpu(rightmost_el->l_count)) {
4696 ret = ocfs2_grow_tree(inode, handle, et,
4697 &depth, last_eb_bh, meta_ac);
4704 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4705 insert.ins_appending = APPEND_NONE;
4706 insert.ins_contig = CONTIG_NONE;
4707 insert.ins_tree_depth = depth;
4709 insert_range = le32_to_cpu(split_rec.e_cpos) +
4710 le16_to_cpu(split_rec.e_leaf_clusters);
4711 rec_range = le32_to_cpu(rec.e_cpos) +
4712 le16_to_cpu(rec.e_leaf_clusters);
4714 if (split_rec.e_cpos == rec.e_cpos) {
4715 insert.ins_split = SPLIT_LEFT;
4716 } else if (insert_range == rec_range) {
4717 insert.ins_split = SPLIT_RIGHT;
4720 * Left/right split. We fake this as a right split
4721 * first and then make a second pass as a left split.
4723 insert.ins_split = SPLIT_RIGHT;
4725 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4730 BUG_ON(do_leftright);
4734 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4740 if (do_leftright == 1) {
4742 struct ocfs2_extent_list *el;
4745 split_rec = *orig_split_rec;
4747 ocfs2_reinit_path(path, 1);
4749 cpos = le32_to_cpu(split_rec.e_cpos);
4750 ret = ocfs2_find_path(inode, path, cpos);
4756 el = path_leaf_el(path);
4757 split_index = ocfs2_search_extent_list(el, cpos);
4766 * Mark part or all of the extent record at split_index in the leaf
4767 * pointed to by path as written. This removes the unwritten
4770 * Care is taken to handle contiguousness so as to not grow the tree.
4772 * meta_ac is not strictly necessary - we only truly need it if growth
4773 * of the tree is required. All other cases will degrade into a less
4774 * optimal tree layout.
4776 * last_eb_bh should be the rightmost leaf block for any extent
4777 * btree. Since a split may grow the tree or a merge might shrink it,
4778 * the caller cannot trust the contents of that buffer after this call.
4780 * This code is optimized for readability - several passes might be
4781 * made over certain portions of the tree. All of those blocks will
4782 * have been brought into cache (and pinned via the journal), so the
4783 * extra overhead is not expressed in terms of disk reads.
4785 static int __ocfs2_mark_extent_written(struct inode *inode,
4786 struct ocfs2_extent_tree *et,
4788 struct ocfs2_path *path,
4790 struct ocfs2_extent_rec *split_rec,
4791 struct ocfs2_alloc_context *meta_ac,
4792 struct ocfs2_cached_dealloc_ctxt *dealloc)
4795 struct ocfs2_extent_list *el = path_leaf_el(path);
4796 struct buffer_head *last_eb_bh = NULL;
4797 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4798 struct ocfs2_merge_ctxt ctxt;
4799 struct ocfs2_extent_list *rightmost_el;
4801 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
4807 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4808 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4809 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4815 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
4820 * The core merge / split code wants to know how much room is
4821 * left in this inodes allocation tree, so we pass the
4822 * rightmost extent list.
4824 if (path->p_tree_depth) {
4825 struct ocfs2_extent_block *eb;
4827 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4828 ocfs2_et_get_last_eb_blk(et),
4829 &last_eb_bh, OCFS2_BH_CACHED, inode);
4835 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4836 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4837 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4842 rightmost_el = &eb->h_list;
4844 rightmost_el = path_root_el(path);
4846 if (rec->e_cpos == split_rec->e_cpos &&
4847 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4848 ctxt.c_split_covers_rec = 1;
4850 ctxt.c_split_covers_rec = 0;
4852 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4854 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4855 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4856 ctxt.c_split_covers_rec);
4858 if (ctxt.c_contig_type == CONTIG_NONE) {
4859 if (ctxt.c_split_covers_rec)
4860 el->l_recs[split_index] = *split_rec;
4862 ret = ocfs2_split_and_insert(inode, handle, path, et,
4863 &last_eb_bh, split_index,
4864 split_rec, meta_ac);
4868 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4869 split_index, split_rec,
4870 dealloc, &ctxt, et);
4881 * Mark the already-existing extent at cpos as written for len clusters.
4883 * If the existing extent is larger than the request, initiate a
4884 * split. An attempt will be made at merging with adjacent extents.
4886 * The caller is responsible for passing down meta_ac if we'll need it.
4888 int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh,
4889 handle_t *handle, u32 cpos, u32 len, u32 phys,
4890 struct ocfs2_alloc_context *meta_ac,
4891 struct ocfs2_cached_dealloc_ctxt *dealloc,
4892 enum ocfs2_extent_tree_type et_type,
4896 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4897 struct ocfs2_extent_rec split_rec;
4898 struct ocfs2_path *left_path = NULL;
4899 struct ocfs2_extent_list *el;
4900 struct ocfs2_extent_tree *et = NULL;
4902 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4903 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4905 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4906 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4907 "that are being written to, but the feature bit "
4908 "is not set in the super block.",
4909 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4914 et = ocfs2_new_extent_tree(inode, root_bh, et_type, private);
4922 * XXX: This should be fixed up so that we just re-insert the
4923 * next extent records.
4925 if (et_type == OCFS2_DINODE_EXTENT)
4926 ocfs2_extent_map_trunc(inode, 0);
4928 left_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4935 ret = ocfs2_find_path(inode, left_path, cpos);
4940 el = path_leaf_el(left_path);
4942 index = ocfs2_search_extent_list(el, cpos);
4943 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4944 ocfs2_error(inode->i_sb,
4945 "Inode %llu has an extent at cpos %u which can no "
4946 "longer be found.\n",
4947 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4952 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4953 split_rec.e_cpos = cpu_to_le32(cpos);
4954 split_rec.e_leaf_clusters = cpu_to_le16(len);
4955 split_rec.e_blkno = cpu_to_le64(start_blkno);
4956 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4957 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4959 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
4960 index, &split_rec, meta_ac,
4966 ocfs2_free_path(left_path);
4968 ocfs2_free_extent_tree(et);
4972 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
4973 handle_t *handle, struct ocfs2_path *path,
4974 int index, u32 new_range,
4975 struct ocfs2_alloc_context *meta_ac)
4977 int ret, depth, credits = handle->h_buffer_credits;
4978 struct buffer_head *last_eb_bh = NULL;
4979 struct ocfs2_extent_block *eb;
4980 struct ocfs2_extent_list *rightmost_el, *el;
4981 struct ocfs2_extent_rec split_rec;
4982 struct ocfs2_extent_rec *rec;
4983 struct ocfs2_insert_type insert;
4986 * Setup the record to split before we grow the tree.
4988 el = path_leaf_el(path);
4989 rec = &el->l_recs[index];
4990 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4992 depth = path->p_tree_depth;
4994 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4995 ocfs2_et_get_last_eb_blk(et),
4996 &last_eb_bh, OCFS2_BH_CACHED, inode);
5002 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5003 rightmost_el = &eb->h_list;
5005 rightmost_el = path_leaf_el(path);
5007 credits += path->p_tree_depth +
5008 ocfs2_extend_meta_needed(et->et_root_el);
5009 ret = ocfs2_extend_trans(handle, credits);
5015 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5016 le16_to_cpu(rightmost_el->l_count)) {
5017 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
5025 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5026 insert.ins_appending = APPEND_NONE;
5027 insert.ins_contig = CONTIG_NONE;
5028 insert.ins_split = SPLIT_RIGHT;
5029 insert.ins_tree_depth = depth;
5031 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
5040 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5041 struct ocfs2_path *path, int index,
5042 struct ocfs2_cached_dealloc_ctxt *dealloc,
5044 struct ocfs2_extent_tree *et)
5047 u32 left_cpos, rec_range, trunc_range;
5048 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5049 struct super_block *sb = inode->i_sb;
5050 struct ocfs2_path *left_path = NULL;
5051 struct ocfs2_extent_list *el = path_leaf_el(path);
5052 struct ocfs2_extent_rec *rec;
5053 struct ocfs2_extent_block *eb;
5055 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5056 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5065 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5066 path->p_tree_depth) {
5068 * Check whether this is the rightmost tree record. If
5069 * we remove all of this record or part of its right
5070 * edge then an update of the record lengths above it
5073 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5074 if (eb->h_next_leaf_blk == 0)
5075 is_rightmost_tree_rec = 1;
5078 rec = &el->l_recs[index];
5079 if (index == 0 && path->p_tree_depth &&
5080 le32_to_cpu(rec->e_cpos) == cpos) {
5082 * Changing the leftmost offset (via partial or whole
5083 * record truncate) of an interior (or rightmost) path
5084 * means we have to update the subtree that is formed
5085 * by this leaf and the one to it's left.
5087 * There are two cases we can skip:
5088 * 1) Path is the leftmost one in our inode tree.
5089 * 2) The leaf is rightmost and will be empty after
5090 * we remove the extent record - the rotate code
5091 * knows how to update the newly formed edge.
5094 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5101 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5102 left_path = ocfs2_new_path(path_root_bh(path),
5103 path_root_el(path));
5110 ret = ocfs2_find_path(inode, left_path, left_cpos);
5118 ret = ocfs2_extend_rotate_transaction(handle, 0,
5119 handle->h_buffer_credits,
5126 ret = ocfs2_journal_access_path(inode, handle, path);
5132 ret = ocfs2_journal_access_path(inode, handle, left_path);
5138 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5139 trunc_range = cpos + len;
5141 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5144 memset(rec, 0, sizeof(*rec));
5145 ocfs2_cleanup_merge(el, index);
5148 next_free = le16_to_cpu(el->l_next_free_rec);
5149 if (is_rightmost_tree_rec && next_free > 1) {
5151 * We skip the edge update if this path will
5152 * be deleted by the rotate code.
5154 rec = &el->l_recs[next_free - 1];
5155 ocfs2_adjust_rightmost_records(inode, handle, path,
5158 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5159 /* Remove leftmost portion of the record. */
5160 le32_add_cpu(&rec->e_cpos, len);
5161 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5162 le16_add_cpu(&rec->e_leaf_clusters, -len);
5163 } else if (rec_range == trunc_range) {
5164 /* Remove rightmost portion of the record */
5165 le16_add_cpu(&rec->e_leaf_clusters, -len);
5166 if (is_rightmost_tree_rec)
5167 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5169 /* Caller should have trapped this. */
5170 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5171 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5172 le32_to_cpu(rec->e_cpos),
5173 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5180 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5181 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5185 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5187 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5194 ocfs2_free_path(left_path);
5198 int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
5199 u32 cpos, u32 len, handle_t *handle,
5200 struct ocfs2_alloc_context *meta_ac,
5201 struct ocfs2_cached_dealloc_ctxt *dealloc,
5202 enum ocfs2_extent_tree_type et_type,
5206 u32 rec_range, trunc_range;
5207 struct ocfs2_extent_rec *rec;
5208 struct ocfs2_extent_list *el;
5209 struct ocfs2_path *path = NULL;
5210 struct ocfs2_extent_tree *et = NULL;
5212 et = ocfs2_new_extent_tree(inode, root_bh, et_type, private);
5219 ocfs2_extent_map_trunc(inode, 0);
5221 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
5228 ret = ocfs2_find_path(inode, path, cpos);
5234 el = path_leaf_el(path);
5235 index = ocfs2_search_extent_list(el, cpos);
5236 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5237 ocfs2_error(inode->i_sb,
5238 "Inode %llu has an extent at cpos %u which can no "
5239 "longer be found.\n",
5240 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5246 * We have 3 cases of extent removal:
5247 * 1) Range covers the entire extent rec
5248 * 2) Range begins or ends on one edge of the extent rec
5249 * 3) Range is in the middle of the extent rec (no shared edges)
5251 * For case 1 we remove the extent rec and left rotate to
5254 * For case 2 we just shrink the existing extent rec, with a
5255 * tree update if the shrinking edge is also the edge of an
5258 * For case 3 we do a right split to turn the extent rec into
5259 * something case 2 can handle.
5261 rec = &el->l_recs[index];
5262 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5263 trunc_range = cpos + len;
5265 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5267 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5268 "(cpos %u, len %u)\n",
5269 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5270 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5272 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5273 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5280 ret = ocfs2_split_tree(inode, et, handle, path, index,
5281 trunc_range, meta_ac);
5288 * The split could have manipulated the tree enough to
5289 * move the record location, so we have to look for it again.
5291 ocfs2_reinit_path(path, 1);
5293 ret = ocfs2_find_path(inode, path, cpos);
5299 el = path_leaf_el(path);
5300 index = ocfs2_search_extent_list(el, cpos);
5301 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5302 ocfs2_error(inode->i_sb,
5303 "Inode %llu: split at cpos %u lost record.",
5304 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5311 * Double check our values here. If anything is fishy,
5312 * it's easier to catch it at the top level.
5314 rec = &el->l_recs[index];
5315 rec_range = le32_to_cpu(rec->e_cpos) +
5316 ocfs2_rec_clusters(el, rec);
5317 if (rec_range != trunc_range) {
5318 ocfs2_error(inode->i_sb,
5319 "Inode %llu: error after split at cpos %u"
5320 "trunc len %u, existing record is (%u,%u)",
5321 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5322 cpos, len, le32_to_cpu(rec->e_cpos),
5323 ocfs2_rec_clusters(el, rec));
5328 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5337 ocfs2_free_path(path);
5339 ocfs2_free_extent_tree(et);
5343 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5345 struct buffer_head *tl_bh = osb->osb_tl_bh;
5346 struct ocfs2_dinode *di;
5347 struct ocfs2_truncate_log *tl;
5349 di = (struct ocfs2_dinode *) tl_bh->b_data;
5350 tl = &di->id2.i_dealloc;
5352 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5353 "slot %d, invalid truncate log parameters: used = "
5354 "%u, count = %u\n", osb->slot_num,
5355 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5356 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5359 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5360 unsigned int new_start)
5362 unsigned int tail_index;
5363 unsigned int current_tail;
5365 /* No records, nothing to coalesce */
5366 if (!le16_to_cpu(tl->tl_used))
5369 tail_index = le16_to_cpu(tl->tl_used) - 1;
5370 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5371 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5373 return current_tail == new_start;
5376 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5379 unsigned int num_clusters)
5382 unsigned int start_cluster, tl_count;
5383 struct inode *tl_inode = osb->osb_tl_inode;
5384 struct buffer_head *tl_bh = osb->osb_tl_bh;
5385 struct ocfs2_dinode *di;
5386 struct ocfs2_truncate_log *tl;
5388 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5389 (unsigned long long)start_blk, num_clusters);
5391 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5393 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5395 di = (struct ocfs2_dinode *) tl_bh->b_data;
5396 tl = &di->id2.i_dealloc;
5397 if (!OCFS2_IS_VALID_DINODE(di)) {
5398 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5403 tl_count = le16_to_cpu(tl->tl_count);
5404 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5406 "Truncate record count on #%llu invalid "
5407 "wanted %u, actual %u\n",
5408 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5409 ocfs2_truncate_recs_per_inode(osb->sb),
5410 le16_to_cpu(tl->tl_count));
5412 /* Caller should have known to flush before calling us. */
5413 index = le16_to_cpu(tl->tl_used);
5414 if (index >= tl_count) {
5420 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5421 OCFS2_JOURNAL_ACCESS_WRITE);
5427 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5428 "%llu (index = %d)\n", num_clusters, start_cluster,
5429 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5431 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5433 * Move index back to the record we are coalescing with.
5434 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5438 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5439 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5440 index, le32_to_cpu(tl->tl_recs[index].t_start),
5443 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5444 tl->tl_used = cpu_to_le16(index + 1);
5446 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5448 status = ocfs2_journal_dirty(handle, tl_bh);
5459 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5461 struct inode *data_alloc_inode,
5462 struct buffer_head *data_alloc_bh)
5466 unsigned int num_clusters;
5468 struct ocfs2_truncate_rec rec;
5469 struct ocfs2_dinode *di;
5470 struct ocfs2_truncate_log *tl;
5471 struct inode *tl_inode = osb->osb_tl_inode;
5472 struct buffer_head *tl_bh = osb->osb_tl_bh;
5476 di = (struct ocfs2_dinode *) tl_bh->b_data;
5477 tl = &di->id2.i_dealloc;
5478 i = le16_to_cpu(tl->tl_used) - 1;
5480 /* Caller has given us at least enough credits to
5481 * update the truncate log dinode */
5482 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5483 OCFS2_JOURNAL_ACCESS_WRITE);
5489 tl->tl_used = cpu_to_le16(i);
5491 status = ocfs2_journal_dirty(handle, tl_bh);
5497 /* TODO: Perhaps we can calculate the bulk of the
5498 * credits up front rather than extending like
5500 status = ocfs2_extend_trans(handle,
5501 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5507 rec = tl->tl_recs[i];
5508 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5509 le32_to_cpu(rec.t_start));
5510 num_clusters = le32_to_cpu(rec.t_clusters);
5512 /* if start_blk is not set, we ignore the record as
5515 mlog(0, "free record %d, start = %u, clusters = %u\n",
5516 i, le32_to_cpu(rec.t_start), num_clusters);
5518 status = ocfs2_free_clusters(handle, data_alloc_inode,
5519 data_alloc_bh, start_blk,
5534 /* Expects you to already be holding tl_inode->i_mutex */
5535 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5538 unsigned int num_to_flush;
5540 struct inode *tl_inode = osb->osb_tl_inode;
5541 struct inode *data_alloc_inode = NULL;
5542 struct buffer_head *tl_bh = osb->osb_tl_bh;
5543 struct buffer_head *data_alloc_bh = NULL;
5544 struct ocfs2_dinode *di;
5545 struct ocfs2_truncate_log *tl;
5549 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5551 di = (struct ocfs2_dinode *) tl_bh->b_data;
5552 tl = &di->id2.i_dealloc;
5553 if (!OCFS2_IS_VALID_DINODE(di)) {
5554 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5559 num_to_flush = le16_to_cpu(tl->tl_used);
5560 mlog(0, "Flush %u records from truncate log #%llu\n",
5561 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5562 if (!num_to_flush) {
5567 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5568 GLOBAL_BITMAP_SYSTEM_INODE,
5569 OCFS2_INVALID_SLOT);
5570 if (!data_alloc_inode) {
5572 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5576 mutex_lock(&data_alloc_inode->i_mutex);
5578 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5584 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5585 if (IS_ERR(handle)) {
5586 status = PTR_ERR(handle);
5591 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5596 ocfs2_commit_trans(osb, handle);
5599 brelse(data_alloc_bh);
5600 ocfs2_inode_unlock(data_alloc_inode, 1);
5603 mutex_unlock(&data_alloc_inode->i_mutex);
5604 iput(data_alloc_inode);
5611 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5614 struct inode *tl_inode = osb->osb_tl_inode;
5616 mutex_lock(&tl_inode->i_mutex);
5617 status = __ocfs2_flush_truncate_log(osb);
5618 mutex_unlock(&tl_inode->i_mutex);
5623 static void ocfs2_truncate_log_worker(struct work_struct *work)
5626 struct ocfs2_super *osb =
5627 container_of(work, struct ocfs2_super,
5628 osb_truncate_log_wq.work);
5632 status = ocfs2_flush_truncate_log(osb);
5636 ocfs2_init_inode_steal_slot(osb);
5641 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5642 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5645 if (osb->osb_tl_inode) {
5646 /* We want to push off log flushes while truncates are
5649 cancel_delayed_work(&osb->osb_truncate_log_wq);
5651 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5652 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5656 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5658 struct inode **tl_inode,
5659 struct buffer_head **tl_bh)
5662 struct inode *inode = NULL;
5663 struct buffer_head *bh = NULL;
5665 inode = ocfs2_get_system_file_inode(osb,
5666 TRUNCATE_LOG_SYSTEM_INODE,
5670 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5674 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
5675 OCFS2_BH_CACHED, inode);
5689 /* called during the 1st stage of node recovery. we stamp a clean
5690 * truncate log and pass back a copy for processing later. if the
5691 * truncate log does not require processing, a *tl_copy is set to
5693 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5695 struct ocfs2_dinode **tl_copy)
5698 struct inode *tl_inode = NULL;
5699 struct buffer_head *tl_bh = NULL;
5700 struct ocfs2_dinode *di;
5701 struct ocfs2_truncate_log *tl;
5705 mlog(0, "recover truncate log from slot %d\n", slot_num);
5707 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5713 di = (struct ocfs2_dinode *) tl_bh->b_data;
5714 tl = &di->id2.i_dealloc;
5715 if (!OCFS2_IS_VALID_DINODE(di)) {
5716 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
5721 if (le16_to_cpu(tl->tl_used)) {
5722 mlog(0, "We'll have %u logs to recover\n",
5723 le16_to_cpu(tl->tl_used));
5725 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5732 /* Assuming the write-out below goes well, this copy
5733 * will be passed back to recovery for processing. */
5734 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5736 /* All we need to do to clear the truncate log is set
5740 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5753 if (status < 0 && (*tl_copy)) {
5762 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5763 struct ocfs2_dinode *tl_copy)
5767 unsigned int clusters, num_recs, start_cluster;
5770 struct inode *tl_inode = osb->osb_tl_inode;
5771 struct ocfs2_truncate_log *tl;
5775 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5776 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5780 tl = &tl_copy->id2.i_dealloc;
5781 num_recs = le16_to_cpu(tl->tl_used);
5782 mlog(0, "cleanup %u records from %llu\n", num_recs,
5783 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
5785 mutex_lock(&tl_inode->i_mutex);
5786 for(i = 0; i < num_recs; i++) {
5787 if (ocfs2_truncate_log_needs_flush(osb)) {
5788 status = __ocfs2_flush_truncate_log(osb);
5795 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5796 if (IS_ERR(handle)) {
5797 status = PTR_ERR(handle);
5802 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5803 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5804 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5806 status = ocfs2_truncate_log_append(osb, handle,
5807 start_blk, clusters);
5808 ocfs2_commit_trans(osb, handle);
5816 mutex_unlock(&tl_inode->i_mutex);
5822 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5825 struct inode *tl_inode = osb->osb_tl_inode;
5830 cancel_delayed_work(&osb->osb_truncate_log_wq);
5831 flush_workqueue(ocfs2_wq);
5833 status = ocfs2_flush_truncate_log(osb);
5837 brelse(osb->osb_tl_bh);
5838 iput(osb->osb_tl_inode);
5844 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5847 struct inode *tl_inode = NULL;
5848 struct buffer_head *tl_bh = NULL;
5852 status = ocfs2_get_truncate_log_info(osb,
5859 /* ocfs2_truncate_log_shutdown keys on the existence of
5860 * osb->osb_tl_inode so we don't set any of the osb variables
5861 * until we're sure all is well. */
5862 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5863 ocfs2_truncate_log_worker);
5864 osb->osb_tl_bh = tl_bh;
5865 osb->osb_tl_inode = tl_inode;
5872 * Delayed de-allocation of suballocator blocks.
5874 * Some sets of block de-allocations might involve multiple suballocator inodes.
5876 * The locking for this can get extremely complicated, especially when
5877 * the suballocator inodes to delete from aren't known until deep
5878 * within an unrelated codepath.
5880 * ocfs2_extent_block structures are a good example of this - an inode
5881 * btree could have been grown by any number of nodes each allocating
5882 * out of their own suballoc inode.
5884 * These structures allow the delay of block de-allocation until a
5885 * later time, when locking of multiple cluster inodes won't cause
5890 * Describes a single block free from a suballocator
5892 struct ocfs2_cached_block_free {
5893 struct ocfs2_cached_block_free *free_next;
5895 unsigned int free_bit;
5898 struct ocfs2_per_slot_free_list {
5899 struct ocfs2_per_slot_free_list *f_next_suballocator;
5902 struct ocfs2_cached_block_free *f_first;
5905 static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5908 struct ocfs2_cached_block_free *head)
5913 struct inode *inode;
5914 struct buffer_head *di_bh = NULL;
5915 struct ocfs2_cached_block_free *tmp;
5917 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5924 mutex_lock(&inode->i_mutex);
5926 ret = ocfs2_inode_lock(inode, &di_bh, 1);
5932 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5933 if (IS_ERR(handle)) {
5934 ret = PTR_ERR(handle);
5940 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5942 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5943 head->free_bit, (unsigned long long)head->free_blk);
5945 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5946 head->free_bit, bg_blkno, 1);
5952 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5959 head = head->free_next;
5964 ocfs2_commit_trans(osb, handle);
5967 ocfs2_inode_unlock(inode, 1);
5970 mutex_unlock(&inode->i_mutex);
5974 /* Premature exit may have left some dangling items. */
5976 head = head->free_next;
5983 int ocfs2_run_deallocs(struct ocfs2_super *osb,
5984 struct ocfs2_cached_dealloc_ctxt *ctxt)
5987 struct ocfs2_per_slot_free_list *fl;
5992 while (ctxt->c_first_suballocator) {
5993 fl = ctxt->c_first_suballocator;
5996 mlog(0, "Free items: (type %u, slot %d)\n",
5997 fl->f_inode_type, fl->f_slot);
5998 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
5999 fl->f_slot, fl->f_first);
6006 ctxt->c_first_suballocator = fl->f_next_suballocator;
6013 static struct ocfs2_per_slot_free_list *
6014 ocfs2_find_per_slot_free_list(int type,
6016 struct ocfs2_cached_dealloc_ctxt *ctxt)
6018 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6021 if (fl->f_inode_type == type && fl->f_slot == slot)
6024 fl = fl->f_next_suballocator;
6027 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6029 fl->f_inode_type = type;
6032 fl->f_next_suballocator = ctxt->c_first_suballocator;
6034 ctxt->c_first_suballocator = fl;
6039 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6040 int type, int slot, u64 blkno,
6044 struct ocfs2_per_slot_free_list *fl;
6045 struct ocfs2_cached_block_free *item;
6047 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6054 item = kmalloc(sizeof(*item), GFP_NOFS);
6061 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6062 type, slot, bit, (unsigned long long)blkno);
6064 item->free_blk = blkno;
6065 item->free_bit = bit;
6066 item->free_next = fl->f_first;
6075 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6076 struct ocfs2_extent_block *eb)
6078 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6079 le16_to_cpu(eb->h_suballoc_slot),
6080 le64_to_cpu(eb->h_blkno),
6081 le16_to_cpu(eb->h_suballoc_bit));
6084 /* This function will figure out whether the currently last extent
6085 * block will be deleted, and if it will, what the new last extent
6086 * block will be so we can update his h_next_leaf_blk field, as well
6087 * as the dinodes i_last_eb_blk */
6088 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6089 unsigned int clusters_to_del,
6090 struct ocfs2_path *path,
6091 struct buffer_head **new_last_eb)
6093 int next_free, ret = 0;
6095 struct ocfs2_extent_rec *rec;
6096 struct ocfs2_extent_block *eb;
6097 struct ocfs2_extent_list *el;
6098 struct buffer_head *bh = NULL;
6100 *new_last_eb = NULL;
6102 /* we have no tree, so of course, no last_eb. */
6103 if (!path->p_tree_depth)
6106 /* trunc to zero special case - this makes tree_depth = 0
6107 * regardless of what it is. */
6108 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6111 el = path_leaf_el(path);
6112 BUG_ON(!el->l_next_free_rec);
6115 * Make sure that this extent list will actually be empty
6116 * after we clear away the data. We can shortcut out if
6117 * there's more than one non-empty extent in the
6118 * list. Otherwise, a check of the remaining extent is
6121 next_free = le16_to_cpu(el->l_next_free_rec);
6123 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6127 /* We may have a valid extent in index 1, check it. */
6129 rec = &el->l_recs[1];
6132 * Fall through - no more nonempty extents, so we want
6133 * to delete this leaf.
6139 rec = &el->l_recs[0];
6144 * Check it we'll only be trimming off the end of this
6147 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6151 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6157 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6163 eb = (struct ocfs2_extent_block *) bh->b_data;
6165 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6166 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6172 get_bh(*new_last_eb);
6173 mlog(0, "returning block %llu, (cpos: %u)\n",
6174 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6182 * Trim some clusters off the rightmost edge of a tree. Only called
6185 * The caller needs to:
6186 * - start journaling of each path component.
6187 * - compute and fully set up any new last ext block
6189 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6190 handle_t *handle, struct ocfs2_truncate_context *tc,
6191 u32 clusters_to_del, u64 *delete_start)
6193 int ret, i, index = path->p_tree_depth;
6196 struct buffer_head *bh;
6197 struct ocfs2_extent_list *el;
6198 struct ocfs2_extent_rec *rec;
6202 while (index >= 0) {
6203 bh = path->p_node[index].bh;
6204 el = path->p_node[index].el;
6206 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6207 index, (unsigned long long)bh->b_blocknr);
6209 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6212 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6213 ocfs2_error(inode->i_sb,
6214 "Inode %lu has invalid ext. block %llu",
6216 (unsigned long long)bh->b_blocknr);
6222 i = le16_to_cpu(el->l_next_free_rec) - 1;
6223 rec = &el->l_recs[i];
6225 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6226 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6227 ocfs2_rec_clusters(el, rec),
6228 (unsigned long long)le64_to_cpu(rec->e_blkno),
6229 le16_to_cpu(el->l_next_free_rec));
6231 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6233 if (le16_to_cpu(el->l_tree_depth) == 0) {
6235 * If the leaf block contains a single empty
6236 * extent and no records, we can just remove
6239 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6241 sizeof(struct ocfs2_extent_rec));
6242 el->l_next_free_rec = cpu_to_le16(0);
6248 * Remove any empty extents by shifting things
6249 * left. That should make life much easier on
6250 * the code below. This condition is rare
6251 * enough that we shouldn't see a performance
6254 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6255 le16_add_cpu(&el->l_next_free_rec, -1);
6258 i < le16_to_cpu(el->l_next_free_rec); i++)
6259 el->l_recs[i] = el->l_recs[i + 1];
6261 memset(&el->l_recs[i], 0,
6262 sizeof(struct ocfs2_extent_rec));
6265 * We've modified our extent list. The
6266 * simplest way to handle this change
6267 * is to being the search from the
6270 goto find_tail_record;
6273 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6276 * We'll use "new_edge" on our way back up the
6277 * tree to know what our rightmost cpos is.
6279 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6280 new_edge += le32_to_cpu(rec->e_cpos);
6283 * The caller will use this to delete data blocks.
6285 *delete_start = le64_to_cpu(rec->e_blkno)
6286 + ocfs2_clusters_to_blocks(inode->i_sb,
6287 le16_to_cpu(rec->e_leaf_clusters));
6290 * If it's now empty, remove this record.
6292 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6294 sizeof(struct ocfs2_extent_rec));
6295 le16_add_cpu(&el->l_next_free_rec, -1);
6298 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6300 sizeof(struct ocfs2_extent_rec));
6301 le16_add_cpu(&el->l_next_free_rec, -1);
6306 /* Can this actually happen? */
6307 if (le16_to_cpu(el->l_next_free_rec) == 0)
6311 * We never actually deleted any clusters
6312 * because our leaf was empty. There's no
6313 * reason to adjust the rightmost edge then.
6318 rec->e_int_clusters = cpu_to_le32(new_edge);
6319 le32_add_cpu(&rec->e_int_clusters,
6320 -le32_to_cpu(rec->e_cpos));
6323 * A deleted child record should have been
6326 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6330 ret = ocfs2_journal_dirty(handle, bh);
6336 mlog(0, "extent list container %llu, after: record %d: "
6337 "(%u, %u, %llu), next = %u.\n",
6338 (unsigned long long)bh->b_blocknr, i,
6339 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6340 (unsigned long long)le64_to_cpu(rec->e_blkno),
6341 le16_to_cpu(el->l_next_free_rec));
6344 * We must be careful to only attempt delete of an
6345 * extent block (and not the root inode block).
6347 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6348 struct ocfs2_extent_block *eb =
6349 (struct ocfs2_extent_block *)bh->b_data;
6352 * Save this for use when processing the
6355 deleted_eb = le64_to_cpu(eb->h_blkno);
6357 mlog(0, "deleting this extent block.\n");
6359 ocfs2_remove_from_cache(inode, bh);
6361 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6362 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6363 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6365 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6366 /* An error here is not fatal. */
6381 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6382 unsigned int clusters_to_del,
6383 struct inode *inode,
6384 struct buffer_head *fe_bh,
6386 struct ocfs2_truncate_context *tc,
6387 struct ocfs2_path *path)
6390 struct ocfs2_dinode *fe;
6391 struct ocfs2_extent_block *last_eb = NULL;
6392 struct ocfs2_extent_list *el;
6393 struct buffer_head *last_eb_bh = NULL;
6396 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6398 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6406 * Each component will be touched, so we might as well journal
6407 * here to avoid having to handle errors later.
6409 status = ocfs2_journal_access_path(inode, handle, path);
6416 status = ocfs2_journal_access(handle, inode, last_eb_bh,
6417 OCFS2_JOURNAL_ACCESS_WRITE);
6423 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6426 el = &(fe->id2.i_list);
6429 * Lower levels depend on this never happening, but it's best
6430 * to check it up here before changing the tree.
6432 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6433 ocfs2_error(inode->i_sb,
6434 "Inode %lu has an empty extent record, depth %u\n",
6435 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6440 spin_lock(&OCFS2_I(inode)->ip_lock);
6441 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6443 spin_unlock(&OCFS2_I(inode)->ip_lock);
6444 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6445 inode->i_blocks = ocfs2_inode_sector_count(inode);
6447 status = ocfs2_trim_tree(inode, path, handle, tc,
6448 clusters_to_del, &delete_blk);
6454 if (le32_to_cpu(fe->i_clusters) == 0) {
6455 /* trunc to zero is a special case. */
6456 el->l_tree_depth = 0;
6457 fe->i_last_eb_blk = 0;
6459 fe->i_last_eb_blk = last_eb->h_blkno;
6461 status = ocfs2_journal_dirty(handle, fe_bh);
6468 /* If there will be a new last extent block, then by
6469 * definition, there cannot be any leaves to the right of
6471 last_eb->h_next_leaf_blk = 0;
6472 status = ocfs2_journal_dirty(handle, last_eb_bh);
6480 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6494 static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
6496 set_buffer_uptodate(bh);
6497 mark_buffer_dirty(bh);
6501 static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
6503 set_buffer_uptodate(bh);
6504 mark_buffer_dirty(bh);
6505 return ocfs2_journal_dirty_data(handle, bh);
6508 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6509 unsigned int from, unsigned int to,
6510 struct page *page, int zero, u64 *phys)
6512 int ret, partial = 0;
6514 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6519 zero_user_segment(page, from, to);
6522 * Need to set the buffers we zero'd into uptodate
6523 * here if they aren't - ocfs2_map_page_blocks()
6524 * might've skipped some
6526 if (ocfs2_should_order_data(inode)) {
6527 ret = walk_page_buffers(handle,
6530 ocfs2_ordered_zero_func);
6534 ret = walk_page_buffers(handle, page_buffers(page),
6536 ocfs2_writeback_zero_func);
6542 SetPageUptodate(page);
6544 flush_dcache_page(page);
6547 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6548 loff_t end, struct page **pages,
6549 int numpages, u64 phys, handle_t *handle)
6553 unsigned int from, to = PAGE_CACHE_SIZE;
6554 struct super_block *sb = inode->i_sb;
6556 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6561 to = PAGE_CACHE_SIZE;
6562 for(i = 0; i < numpages; i++) {
6565 from = start & (PAGE_CACHE_SIZE - 1);
6566 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6567 to = end & (PAGE_CACHE_SIZE - 1);
6569 BUG_ON(from > PAGE_CACHE_SIZE);
6570 BUG_ON(to > PAGE_CACHE_SIZE);
6572 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6575 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6579 ocfs2_unlock_and_free_pages(pages, numpages);
6582 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6583 struct page **pages, int *num)
6585 int numpages, ret = 0;
6586 struct super_block *sb = inode->i_sb;
6587 struct address_space *mapping = inode->i_mapping;
6588 unsigned long index;
6589 loff_t last_page_bytes;
6591 BUG_ON(start > end);
6593 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6594 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6597 last_page_bytes = PAGE_ALIGN(end);
6598 index = start >> PAGE_CACHE_SHIFT;
6600 pages[numpages] = grab_cache_page(mapping, index);
6601 if (!pages[numpages]) {
6609 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6614 ocfs2_unlock_and_free_pages(pages, numpages);
6624 * Zero the area past i_size but still within an allocated
6625 * cluster. This avoids exposing nonzero data on subsequent file
6628 * We need to call this before i_size is updated on the inode because
6629 * otherwise block_write_full_page() will skip writeout of pages past
6630 * i_size. The new_i_size parameter is passed for this reason.
6632 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6633 u64 range_start, u64 range_end)
6635 int ret = 0, numpages;
6636 struct page **pages = NULL;
6638 unsigned int ext_flags;
6639 struct super_block *sb = inode->i_sb;
6642 * File systems which don't support sparse files zero on every
6645 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6648 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6649 sizeof(struct page *), GFP_NOFS);
6650 if (pages == NULL) {
6656 if (range_start == range_end)
6659 ret = ocfs2_extent_map_get_blocks(inode,
6660 range_start >> sb->s_blocksize_bits,
6661 &phys, NULL, &ext_flags);
6668 * Tail is a hole, or is marked unwritten. In either case, we
6669 * can count on read and write to return/push zero's.
6671 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6674 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6681 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6682 numpages, phys, handle);
6685 * Initiate writeout of the pages we zero'd here. We don't
6686 * wait on them - the truncate_inode_pages() call later will
6689 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6690 range_end - 1, SYNC_FILE_RANGE_WRITE);
6701 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6702 struct ocfs2_dinode *di)
6704 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6705 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6707 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6708 memset(&di->id2, 0, blocksize -
6709 offsetof(struct ocfs2_dinode, id2) -
6712 memset(&di->id2, 0, blocksize -
6713 offsetof(struct ocfs2_dinode, id2));
6716 void ocfs2_dinode_new_extent_list(struct inode *inode,
6717 struct ocfs2_dinode *di)
6719 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6720 di->id2.i_list.l_tree_depth = 0;
6721 di->id2.i_list.l_next_free_rec = 0;
6722 di->id2.i_list.l_count = cpu_to_le16(
6723 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6726 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6728 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6729 struct ocfs2_inline_data *idata = &di->id2.i_data;
6731 spin_lock(&oi->ip_lock);
6732 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6733 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6734 spin_unlock(&oi->ip_lock);
6737 * We clear the entire i_data structure here so that all
6738 * fields can be properly initialized.
6740 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6742 idata->id_count = cpu_to_le16(
6743 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6746 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6747 struct buffer_head *di_bh)
6749 int ret, i, has_data, num_pages = 0;
6751 u64 uninitialized_var(block);
6752 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6753 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6754 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6755 struct ocfs2_alloc_context *data_ac = NULL;
6756 struct page **pages = NULL;
6757 loff_t end = osb->s_clustersize;
6759 has_data = i_size_read(inode) ? 1 : 0;
6762 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6763 sizeof(struct page *), GFP_NOFS);
6764 if (pages == NULL) {
6770 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6777 handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
6778 if (IS_ERR(handle)) {
6779 ret = PTR_ERR(handle);
6784 ret = ocfs2_journal_access(handle, inode, di_bh,
6785 OCFS2_JOURNAL_ACCESS_WRITE);
6793 unsigned int page_end;
6796 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6804 * Save two copies, one for insert, and one that can
6805 * be changed by ocfs2_map_and_dirty_page() below.
6807 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6810 * Non sparse file systems zero on extend, so no need
6813 if (!ocfs2_sparse_alloc(osb) &&
6814 PAGE_CACHE_SIZE < osb->s_clustersize)
6815 end = PAGE_CACHE_SIZE;
6817 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6824 * This should populate the 1st page for us and mark
6827 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6833 page_end = PAGE_CACHE_SIZE;
6834 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6835 page_end = osb->s_clustersize;
6837 for (i = 0; i < num_pages; i++)
6838 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6839 pages[i], i > 0, &phys);
6842 spin_lock(&oi->ip_lock);
6843 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6844 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6845 spin_unlock(&oi->ip_lock);
6847 ocfs2_dinode_new_extent_list(inode, di);
6849 ocfs2_journal_dirty(handle, di_bh);
6853 * An error at this point should be extremely rare. If
6854 * this proves to be false, we could always re-build
6855 * the in-inode data from our pages.
6857 ret = ocfs2_dinode_insert_extent(osb, handle, inode, di_bh,
6858 0, block, 1, 0, NULL);
6864 inode->i_blocks = ocfs2_inode_sector_count(inode);
6868 ocfs2_commit_trans(osb, handle);
6872 ocfs2_free_alloc_context(data_ac);
6876 ocfs2_unlock_and_free_pages(pages, num_pages);
6884 * It is expected, that by the time you call this function,
6885 * inode->i_size and fe->i_size have been adjusted.
6887 * WARNING: This will kfree the truncate context
6889 int ocfs2_commit_truncate(struct ocfs2_super *osb,
6890 struct inode *inode,
6891 struct buffer_head *fe_bh,
6892 struct ocfs2_truncate_context *tc)
6894 int status, i, credits, tl_sem = 0;
6895 u32 clusters_to_del, new_highest_cpos, range;
6896 struct ocfs2_extent_list *el;
6897 handle_t *handle = NULL;
6898 struct inode *tl_inode = osb->osb_tl_inode;
6899 struct ocfs2_path *path = NULL;
6900 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6904 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
6905 i_size_read(inode));
6907 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
6914 ocfs2_extent_map_trunc(inode, new_highest_cpos);
6918 * Check that we still have allocation to delete.
6920 if (OCFS2_I(inode)->ip_clusters == 0) {
6926 * Truncate always works against the rightmost tree branch.
6928 status = ocfs2_find_path(inode, path, UINT_MAX);
6934 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6935 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
6938 * By now, el will point to the extent list on the bottom most
6939 * portion of this tree. Only the tail record is considered in
6942 * We handle the following cases, in order:
6943 * - empty extent: delete the remaining branch
6944 * - remove the entire record
6945 * - remove a partial record
6946 * - no record needs to be removed (truncate has completed)
6948 el = path_leaf_el(path);
6949 if (le16_to_cpu(el->l_next_free_rec) == 0) {
6950 ocfs2_error(inode->i_sb,
6951 "Inode %llu has empty extent block at %llu\n",
6952 (unsigned long long)OCFS2_I(inode)->ip_blkno,
6953 (unsigned long long)path_leaf_bh(path)->b_blocknr);
6958 i = le16_to_cpu(el->l_next_free_rec) - 1;
6959 range = le32_to_cpu(el->l_recs[i].e_cpos) +
6960 ocfs2_rec_clusters(el, &el->l_recs[i]);
6961 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
6962 clusters_to_del = 0;
6963 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
6964 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
6965 } else if (range > new_highest_cpos) {
6966 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
6967 le32_to_cpu(el->l_recs[i].e_cpos)) -
6974 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6975 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
6977 mutex_lock(&tl_inode->i_mutex);
6979 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6980 * record is free for use. If there isn't any, we flush to get
6981 * an empty truncate log. */
6982 if (ocfs2_truncate_log_needs_flush(osb)) {
6983 status = __ocfs2_flush_truncate_log(osb);
6990 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
6991 (struct ocfs2_dinode *)fe_bh->b_data,
6993 handle = ocfs2_start_trans(osb, credits);
6994 if (IS_ERR(handle)) {
6995 status = PTR_ERR(handle);
7001 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7008 mutex_unlock(&tl_inode->i_mutex);
7011 ocfs2_commit_trans(osb, handle);
7014 ocfs2_reinit_path(path, 1);
7017 * The check above will catch the case where we've truncated
7018 * away all allocation.
7024 ocfs2_schedule_truncate_log_flush(osb, 1);
7027 mutex_unlock(&tl_inode->i_mutex);
7030 ocfs2_commit_trans(osb, handle);
7032 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7034 ocfs2_free_path(path);
7036 /* This will drop the ext_alloc cluster lock for us */
7037 ocfs2_free_truncate_context(tc);
7044 * Expects the inode to already be locked.
7046 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7047 struct inode *inode,
7048 struct buffer_head *fe_bh,
7049 struct ocfs2_truncate_context **tc)
7052 unsigned int new_i_clusters;
7053 struct ocfs2_dinode *fe;
7054 struct ocfs2_extent_block *eb;
7055 struct buffer_head *last_eb_bh = NULL;
7061 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7062 i_size_read(inode));
7063 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7065 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7066 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7067 (unsigned long long)le64_to_cpu(fe->i_size));
7069 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7075 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7077 if (fe->id2.i_list.l_tree_depth) {
7078 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
7079 &last_eb_bh, OCFS2_BH_CACHED, inode);
7084 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7085 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
7086 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
7094 (*tc)->tc_last_eb_bh = last_eb_bh;
7100 ocfs2_free_truncate_context(*tc);
7108 * 'start' is inclusive, 'end' is not.
7110 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7111 unsigned int start, unsigned int end, int trunc)
7114 unsigned int numbytes;
7116 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7117 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7118 struct ocfs2_inline_data *idata = &di->id2.i_data;
7120 if (end > i_size_read(inode))
7121 end = i_size_read(inode);
7123 BUG_ON(start >= end);
7125 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7126 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7127 !ocfs2_supports_inline_data(osb)) {
7128 ocfs2_error(inode->i_sb,
7129 "Inline data flags for inode %llu don't agree! "
7130 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7131 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7132 le16_to_cpu(di->i_dyn_features),
7133 OCFS2_I(inode)->ip_dyn_features,
7134 osb->s_feature_incompat);
7139 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7140 if (IS_ERR(handle)) {
7141 ret = PTR_ERR(handle);
7146 ret = ocfs2_journal_access(handle, inode, di_bh,
7147 OCFS2_JOURNAL_ACCESS_WRITE);
7153 numbytes = end - start;
7154 memset(idata->id_data + start, 0, numbytes);
7157 * No need to worry about the data page here - it's been
7158 * truncated already and inline data doesn't need it for
7159 * pushing zero's to disk, so we'll let readpage pick it up
7163 i_size_write(inode, start);
7164 di->i_size = cpu_to_le64(start);
7167 inode->i_blocks = ocfs2_inode_sector_count(inode);
7168 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7170 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7171 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7173 ocfs2_journal_dirty(handle, di_bh);
7176 ocfs2_commit_trans(osb, handle);
7182 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7185 * The caller is responsible for completing deallocation
7186 * before freeing the context.
7188 if (tc->tc_dealloc.c_first_suballocator != NULL)
7190 "Truncate completion has non-empty dealloc context\n");
7192 if (tc->tc_last_eb_bh)
7193 brelse(tc->tc_last_eb_bh);