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 (*set_last_eb_blk) (struct ocfs2_extent_tree *et, u64 blkno);
69 u64 (*get_last_eb_blk) (struct ocfs2_extent_tree *et);
70 void (*update_clusters) (struct inode *inode,
71 struct ocfs2_extent_tree *et,
73 int (*sanity_check) (struct inode *inode, struct ocfs2_extent_tree *et);
76 struct ocfs2_extent_tree {
77 enum ocfs2_extent_tree_type type;
78 struct ocfs2_extent_tree_operations *eops;
79 struct buffer_head *root_bh;
80 struct ocfs2_extent_list *root_el;
84 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
87 struct ocfs2_dinode *di = (struct ocfs2_dinode *)et->root_bh->b_data;
89 BUG_ON(et->type != OCFS2_DINODE_EXTENT);
90 di->i_last_eb_blk = cpu_to_le64(blkno);
93 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
95 struct ocfs2_dinode *di = (struct ocfs2_dinode *)et->root_bh->b_data;
97 BUG_ON(et->type != OCFS2_DINODE_EXTENT);
98 return le64_to_cpu(di->i_last_eb_blk);
101 static void ocfs2_dinode_update_clusters(struct inode *inode,
102 struct ocfs2_extent_tree *et,
105 struct ocfs2_dinode *di =
106 (struct ocfs2_dinode *)et->root_bh->b_data;
108 le32_add_cpu(&di->i_clusters, clusters);
109 spin_lock(&OCFS2_I(inode)->ip_lock);
110 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
111 spin_unlock(&OCFS2_I(inode)->ip_lock);
114 static int ocfs2_dinode_sanity_check(struct inode *inode,
115 struct ocfs2_extent_tree *et)
118 struct ocfs2_dinode *di;
120 BUG_ON(et->type != OCFS2_DINODE_EXTENT);
122 di = (struct ocfs2_dinode *)et->root_bh->b_data;
123 if (!OCFS2_IS_VALID_DINODE(di)) {
125 ocfs2_error(inode->i_sb,
126 "Inode %llu has invalid path root",
127 (unsigned long long)OCFS2_I(inode)->ip_blkno);
133 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
134 .set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
135 .get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
136 .update_clusters = ocfs2_dinode_update_clusters,
137 .sanity_check = ocfs2_dinode_sanity_check,
140 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
143 struct ocfs2_xattr_value_root *xv =
144 (struct ocfs2_xattr_value_root *)et->private;
146 xv->xr_last_eb_blk = cpu_to_le64(blkno);
149 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
151 struct ocfs2_xattr_value_root *xv =
152 (struct ocfs2_xattr_value_root *) et->private;
154 return le64_to_cpu(xv->xr_last_eb_blk);
157 static void ocfs2_xattr_value_update_clusters(struct inode *inode,
158 struct ocfs2_extent_tree *et,
161 struct ocfs2_xattr_value_root *xv =
162 (struct ocfs2_xattr_value_root *)et->private;
164 le32_add_cpu(&xv->xr_clusters, clusters);
167 static int ocfs2_xattr_value_sanity_check(struct inode *inode,
168 struct ocfs2_extent_tree *et)
173 static struct ocfs2_extent_tree_operations ocfs2_xattr_et_ops = {
174 .set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
175 .get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
176 .update_clusters = ocfs2_xattr_value_update_clusters,
177 .sanity_check = ocfs2_xattr_value_sanity_check,
180 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
183 struct ocfs2_xattr_block *xb =
184 (struct ocfs2_xattr_block *) et->root_bh->b_data;
185 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
187 xt->xt_last_eb_blk = cpu_to_le64(blkno);
190 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
192 struct ocfs2_xattr_block *xb =
193 (struct ocfs2_xattr_block *) et->root_bh->b_data;
194 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
196 return le64_to_cpu(xt->xt_last_eb_blk);
199 static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
200 struct ocfs2_extent_tree *et,
203 struct ocfs2_xattr_block *xb =
204 (struct ocfs2_xattr_block *)et->root_bh->b_data;
206 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
209 static int ocfs2_xattr_tree_sanity_check(struct inode *inode,
210 struct ocfs2_extent_tree *et)
215 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
216 .set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
217 .get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
218 .update_clusters = ocfs2_xattr_tree_update_clusters,
219 .sanity_check = ocfs2_xattr_tree_sanity_check,
222 static struct ocfs2_extent_tree*
223 ocfs2_new_extent_tree(struct buffer_head *bh,
224 enum ocfs2_extent_tree_type et_type,
227 struct ocfs2_extent_tree *et;
229 et = kzalloc(sizeof(*et), GFP_NOFS);
236 et->private = private;
238 if (et_type == OCFS2_DINODE_EXTENT) {
239 et->root_el = &((struct ocfs2_dinode *)bh->b_data)->id2.i_list;
240 et->eops = &ocfs2_dinode_et_ops;
241 } else if (et_type == OCFS2_XATTR_VALUE_EXTENT) {
242 struct ocfs2_xattr_value_root *xv =
243 (struct ocfs2_xattr_value_root *) private;
244 et->root_el = &xv->xr_list;
245 et->eops = &ocfs2_xattr_et_ops;
246 } else if (et_type == OCFS2_XATTR_TREE_EXTENT) {
247 struct ocfs2_xattr_block *xb =
248 (struct ocfs2_xattr_block *)bh->b_data;
249 et->root_el = &xb->xb_attrs.xb_root.xt_list;
250 et->eops = &ocfs2_xattr_tree_et_ops;
256 static void ocfs2_free_extent_tree(struct ocfs2_extent_tree *et)
264 static inline void ocfs2_set_last_eb_blk(struct ocfs2_extent_tree *et,
267 et->eops->set_last_eb_blk(et, new_last_eb_blk);
270 static inline u64 ocfs2_get_last_eb_blk(struct ocfs2_extent_tree *et)
272 return et->eops->get_last_eb_blk(et);
275 static inline void ocfs2_update_clusters(struct inode *inode,
276 struct ocfs2_extent_tree *et,
279 et->eops->update_clusters(inode, et, clusters);
282 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
283 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
284 struct ocfs2_extent_block *eb);
287 * Structures which describe a path through a btree, and functions to
290 * The idea here is to be as generic as possible with the tree
293 struct ocfs2_path_item {
294 struct buffer_head *bh;
295 struct ocfs2_extent_list *el;
298 #define OCFS2_MAX_PATH_DEPTH 5
302 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
305 #define path_root_bh(_path) ((_path)->p_node[0].bh)
306 #define path_root_el(_path) ((_path)->p_node[0].el)
307 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
308 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
309 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
312 * Reset the actual path elements so that we can re-use the structure
313 * to build another path. Generally, this involves freeing the buffer
316 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
318 int i, start = 0, depth = 0;
319 struct ocfs2_path_item *node;
324 for(i = start; i < path_num_items(path); i++) {
325 node = &path->p_node[i];
333 * Tree depth may change during truncate, or insert. If we're
334 * keeping the root extent list, then make sure that our path
335 * structure reflects the proper depth.
338 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
340 path->p_tree_depth = depth;
343 static void ocfs2_free_path(struct ocfs2_path *path)
346 ocfs2_reinit_path(path, 0);
352 * All the elements of src into dest. After this call, src could be freed
353 * without affecting dest.
355 * Both paths should have the same root. Any non-root elements of dest
358 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
362 BUG_ON(path_root_bh(dest) != path_root_bh(src));
363 BUG_ON(path_root_el(dest) != path_root_el(src));
365 ocfs2_reinit_path(dest, 1);
367 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
368 dest->p_node[i].bh = src->p_node[i].bh;
369 dest->p_node[i].el = src->p_node[i].el;
371 if (dest->p_node[i].bh)
372 get_bh(dest->p_node[i].bh);
377 * Make the *dest path the same as src and re-initialize src path to
380 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
384 BUG_ON(path_root_bh(dest) != path_root_bh(src));
386 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
387 brelse(dest->p_node[i].bh);
389 dest->p_node[i].bh = src->p_node[i].bh;
390 dest->p_node[i].el = src->p_node[i].el;
392 src->p_node[i].bh = NULL;
393 src->p_node[i].el = NULL;
398 * Insert an extent block at given index.
400 * This will not take an additional reference on eb_bh.
402 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
403 struct buffer_head *eb_bh)
405 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
408 * Right now, no root bh is an extent block, so this helps
409 * catch code errors with dinode trees. The assertion can be
410 * safely removed if we ever need to insert extent block
411 * structures at the root.
415 path->p_node[index].bh = eb_bh;
416 path->p_node[index].el = &eb->h_list;
419 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
420 struct ocfs2_extent_list *root_el)
422 struct ocfs2_path *path;
424 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
426 path = kzalloc(sizeof(*path), GFP_NOFS);
428 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
430 path_root_bh(path) = root_bh;
431 path_root_el(path) = root_el;
438 * Convenience function to journal all components in a path.
440 static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
441 struct ocfs2_path *path)
448 for(i = 0; i < path_num_items(path); i++) {
449 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
450 OCFS2_JOURNAL_ACCESS_WRITE);
462 * Return the index of the extent record which contains cluster #v_cluster.
463 * -1 is returned if it was not found.
465 * Should work fine on interior and exterior nodes.
467 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
471 struct ocfs2_extent_rec *rec;
472 u32 rec_end, rec_start, clusters;
474 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
475 rec = &el->l_recs[i];
477 rec_start = le32_to_cpu(rec->e_cpos);
478 clusters = ocfs2_rec_clusters(el, rec);
480 rec_end = rec_start + clusters;
482 if (v_cluster >= rec_start && v_cluster < rec_end) {
491 enum ocfs2_contig_type {
500 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
501 * ocfs2_extent_contig only work properly against leaf nodes!
503 static int ocfs2_block_extent_contig(struct super_block *sb,
504 struct ocfs2_extent_rec *ext,
507 u64 blk_end = le64_to_cpu(ext->e_blkno);
509 blk_end += ocfs2_clusters_to_blocks(sb,
510 le16_to_cpu(ext->e_leaf_clusters));
512 return blkno == blk_end;
515 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
516 struct ocfs2_extent_rec *right)
520 left_range = le32_to_cpu(left->e_cpos) +
521 le16_to_cpu(left->e_leaf_clusters);
523 return (left_range == le32_to_cpu(right->e_cpos));
526 static enum ocfs2_contig_type
527 ocfs2_extent_contig(struct inode *inode,
528 struct ocfs2_extent_rec *ext,
529 struct ocfs2_extent_rec *insert_rec)
531 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
534 * Refuse to coalesce extent records with different flag
535 * fields - we don't want to mix unwritten extents with user
538 if (ext->e_flags != insert_rec->e_flags)
541 if (ocfs2_extents_adjacent(ext, insert_rec) &&
542 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
545 blkno = le64_to_cpu(ext->e_blkno);
546 if (ocfs2_extents_adjacent(insert_rec, ext) &&
547 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
554 * NOTE: We can have pretty much any combination of contiguousness and
557 * The usefulness of APPEND_TAIL is more in that it lets us know that
558 * we'll have to update the path to that leaf.
560 enum ocfs2_append_type {
565 enum ocfs2_split_type {
571 struct ocfs2_insert_type {
572 enum ocfs2_split_type ins_split;
573 enum ocfs2_append_type ins_appending;
574 enum ocfs2_contig_type ins_contig;
575 int ins_contig_index;
579 struct ocfs2_merge_ctxt {
580 enum ocfs2_contig_type c_contig_type;
581 int c_has_empty_extent;
582 int c_split_covers_rec;
586 * How many free extents have we got before we need more meta data?
588 int ocfs2_num_free_extents(struct ocfs2_super *osb,
590 struct buffer_head *root_bh,
591 enum ocfs2_extent_tree_type type,
595 struct ocfs2_extent_list *el = NULL;
596 struct ocfs2_extent_block *eb;
597 struct buffer_head *eb_bh = NULL;
602 if (type == OCFS2_DINODE_EXTENT) {
603 struct ocfs2_dinode *fe =
604 (struct ocfs2_dinode *)root_bh->b_data;
605 if (!OCFS2_IS_VALID_DINODE(fe)) {
606 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
611 if (fe->i_last_eb_blk)
612 last_eb_blk = le64_to_cpu(fe->i_last_eb_blk);
613 el = &fe->id2.i_list;
614 } else if (type == OCFS2_XATTR_VALUE_EXTENT) {
615 struct ocfs2_xattr_value_root *xv =
616 (struct ocfs2_xattr_value_root *) private;
618 last_eb_blk = le64_to_cpu(xv->xr_last_eb_blk);
620 } else if (type == OCFS2_XATTR_TREE_EXTENT) {
621 struct ocfs2_xattr_block *xb =
622 (struct ocfs2_xattr_block *)root_bh->b_data;
624 last_eb_blk = le64_to_cpu(xb->xb_attrs.xb_root.xt_last_eb_blk);
625 el = &xb->xb_attrs.xb_root.xt_list;
629 retval = ocfs2_read_block(osb, last_eb_blk,
630 &eb_bh, OCFS2_BH_CACHED, inode);
635 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
639 BUG_ON(el->l_tree_depth != 0);
641 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
650 /* expects array to already be allocated
652 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
655 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
659 struct ocfs2_alloc_context *meta_ac,
660 struct buffer_head *bhs[])
662 int count, status, i;
663 u16 suballoc_bit_start;
666 struct ocfs2_extent_block *eb;
671 while (count < wanted) {
672 status = ocfs2_claim_metadata(osb,
684 for(i = count; i < (num_got + count); i++) {
685 bhs[i] = sb_getblk(osb->sb, first_blkno);
686 if (bhs[i] == NULL) {
691 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
693 status = ocfs2_journal_access(handle, inode, bhs[i],
694 OCFS2_JOURNAL_ACCESS_CREATE);
700 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
701 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
702 /* Ok, setup the minimal stuff here. */
703 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
704 eb->h_blkno = cpu_to_le64(first_blkno);
705 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
706 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
707 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
709 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
711 suballoc_bit_start++;
714 /* We'll also be dirtied by the caller, so
715 * this isn't absolutely necessary. */
716 status = ocfs2_journal_dirty(handle, bhs[i]);
729 for(i = 0; i < wanted; i++) {
740 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
742 * Returns the sum of the rightmost extent rec logical offset and
745 * ocfs2_add_branch() uses this to determine what logical cluster
746 * value should be populated into the leftmost new branch records.
748 * ocfs2_shift_tree_depth() uses this to determine the # clusters
749 * value for the new topmost tree record.
751 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
755 i = le16_to_cpu(el->l_next_free_rec) - 1;
757 return le32_to_cpu(el->l_recs[i].e_cpos) +
758 ocfs2_rec_clusters(el, &el->l_recs[i]);
762 * Add an entire tree branch to our inode. eb_bh is the extent block
763 * to start at, if we don't want to start the branch at the dinode
766 * last_eb_bh is required as we have to update it's next_leaf pointer
767 * for the new last extent block.
769 * the new branch will be 'empty' in the sense that every block will
770 * contain a single record with cluster count == 0.
772 static int ocfs2_add_branch(struct ocfs2_super *osb,
775 struct ocfs2_extent_tree *et,
776 struct buffer_head *eb_bh,
777 struct buffer_head **last_eb_bh,
778 struct ocfs2_alloc_context *meta_ac)
780 int status, new_blocks, i;
781 u64 next_blkno, new_last_eb_blk;
782 struct buffer_head *bh;
783 struct buffer_head **new_eb_bhs = NULL;
784 struct ocfs2_extent_block *eb;
785 struct ocfs2_extent_list *eb_el;
786 struct ocfs2_extent_list *el;
791 BUG_ON(!last_eb_bh || !*last_eb_bh);
794 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
799 /* we never add a branch to a leaf. */
800 BUG_ON(!el->l_tree_depth);
802 new_blocks = le16_to_cpu(el->l_tree_depth);
804 /* allocate the number of new eb blocks we need */
805 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
813 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
814 meta_ac, new_eb_bhs);
820 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
821 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
823 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
824 * linked with the rest of the tree.
825 * conversly, new_eb_bhs[0] is the new bottommost leaf.
827 * when we leave the loop, new_last_eb_blk will point to the
828 * newest leaf, and next_blkno will point to the topmost extent
830 next_blkno = new_last_eb_blk = 0;
831 for(i = 0; i < new_blocks; i++) {
833 eb = (struct ocfs2_extent_block *) bh->b_data;
834 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
835 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
841 status = ocfs2_journal_access(handle, inode, bh,
842 OCFS2_JOURNAL_ACCESS_CREATE);
848 eb->h_next_leaf_blk = 0;
849 eb_el->l_tree_depth = cpu_to_le16(i);
850 eb_el->l_next_free_rec = cpu_to_le16(1);
852 * This actually counts as an empty extent as
855 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
856 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
858 * eb_el isn't always an interior node, but even leaf
859 * nodes want a zero'd flags and reserved field so
860 * this gets the whole 32 bits regardless of use.
862 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
863 if (!eb_el->l_tree_depth)
864 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
866 status = ocfs2_journal_dirty(handle, bh);
872 next_blkno = le64_to_cpu(eb->h_blkno);
875 /* This is a bit hairy. We want to update up to three blocks
876 * here without leaving any of them in an inconsistent state
877 * in case of error. We don't have to worry about
878 * journal_dirty erroring as it won't unless we've aborted the
879 * handle (in which case we would never be here) so reserving
880 * the write with journal_access is all we need to do. */
881 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
882 OCFS2_JOURNAL_ACCESS_WRITE);
887 status = ocfs2_journal_access(handle, inode, et->root_bh,
888 OCFS2_JOURNAL_ACCESS_WRITE);
894 status = ocfs2_journal_access(handle, inode, eb_bh,
895 OCFS2_JOURNAL_ACCESS_WRITE);
902 /* Link the new branch into the rest of the tree (el will
903 * either be on the root_bh, or the extent block passed in. */
904 i = le16_to_cpu(el->l_next_free_rec);
905 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
906 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
907 el->l_recs[i].e_int_clusters = 0;
908 le16_add_cpu(&el->l_next_free_rec, 1);
910 /* fe needs a new last extent block pointer, as does the
911 * next_leaf on the previously last-extent-block. */
912 ocfs2_set_last_eb_blk(et, new_last_eb_blk);
914 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
915 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
917 status = ocfs2_journal_dirty(handle, *last_eb_bh);
920 status = ocfs2_journal_dirty(handle, et->root_bh);
924 status = ocfs2_journal_dirty(handle, eb_bh);
930 * Some callers want to track the rightmost leaf so pass it
934 get_bh(new_eb_bhs[0]);
935 *last_eb_bh = new_eb_bhs[0];
940 for (i = 0; i < new_blocks; i++)
942 brelse(new_eb_bhs[i]);
951 * adds another level to the allocation tree.
952 * returns back the new extent block so you can add a branch to it
955 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
958 struct ocfs2_extent_tree *et,
959 struct ocfs2_alloc_context *meta_ac,
960 struct buffer_head **ret_new_eb_bh)
964 struct buffer_head *new_eb_bh = NULL;
965 struct ocfs2_extent_block *eb;
966 struct ocfs2_extent_list *root_el;
967 struct ocfs2_extent_list *eb_el;
971 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
978 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
979 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
980 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
986 root_el = et->root_el;
988 status = ocfs2_journal_access(handle, inode, new_eb_bh,
989 OCFS2_JOURNAL_ACCESS_CREATE);
995 /* copy the root extent list data into the new extent block */
996 eb_el->l_tree_depth = root_el->l_tree_depth;
997 eb_el->l_next_free_rec = root_el->l_next_free_rec;
998 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
999 eb_el->l_recs[i] = root_el->l_recs[i];
1001 status = ocfs2_journal_dirty(handle, new_eb_bh);
1007 status = ocfs2_journal_access(handle, inode, et->root_bh,
1008 OCFS2_JOURNAL_ACCESS_WRITE);
1014 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1016 /* update root_bh now */
1017 le16_add_cpu(&root_el->l_tree_depth, 1);
1018 root_el->l_recs[0].e_cpos = 0;
1019 root_el->l_recs[0].e_blkno = eb->h_blkno;
1020 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1021 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1022 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1023 root_el->l_next_free_rec = cpu_to_le16(1);
1025 /* If this is our 1st tree depth shift, then last_eb_blk
1026 * becomes the allocated extent block */
1027 if (root_el->l_tree_depth == cpu_to_le16(1))
1028 ocfs2_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1030 status = ocfs2_journal_dirty(handle, et->root_bh);
1036 *ret_new_eb_bh = new_eb_bh;
1048 * Should only be called when there is no space left in any of the
1049 * leaf nodes. What we want to do is find the lowest tree depth
1050 * non-leaf extent block with room for new records. There are three
1051 * valid results of this search:
1053 * 1) a lowest extent block is found, then we pass it back in
1054 * *lowest_eb_bh and return '0'
1056 * 2) the search fails to find anything, but the root_el has room. We
1057 * pass NULL back in *lowest_eb_bh, but still return '0'
1059 * 3) the search fails to find anything AND the root_el is full, in
1060 * which case we return > 0
1062 * return status < 0 indicates an error.
1064 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1065 struct inode *inode,
1066 struct ocfs2_extent_tree *et,
1067 struct buffer_head **target_bh)
1071 struct ocfs2_extent_block *eb;
1072 struct ocfs2_extent_list *el;
1073 struct buffer_head *bh = NULL;
1074 struct buffer_head *lowest_bh = NULL;
1082 while(le16_to_cpu(el->l_tree_depth) > 1) {
1083 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1084 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
1085 "extent list (next_free_rec == 0)",
1086 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1090 i = le16_to_cpu(el->l_next_free_rec) - 1;
1091 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1093 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
1094 "list where extent # %d has no physical "
1096 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
1106 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
1113 eb = (struct ocfs2_extent_block *) bh->b_data;
1114 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1115 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1121 if (le16_to_cpu(el->l_next_free_rec) <
1122 le16_to_cpu(el->l_count)) {
1130 /* If we didn't find one and the fe doesn't have any room,
1131 * then return '1' */
1133 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1136 *target_bh = lowest_bh;
1146 * Grow a b-tree so that it has more records.
1148 * We might shift the tree depth in which case existing paths should
1149 * be considered invalid.
1151 * Tree depth after the grow is returned via *final_depth.
1153 * *last_eb_bh will be updated by ocfs2_add_branch().
1155 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
1156 struct ocfs2_extent_tree *et, int *final_depth,
1157 struct buffer_head **last_eb_bh,
1158 struct ocfs2_alloc_context *meta_ac)
1161 struct ocfs2_extent_list *el = et->root_el;
1162 int depth = le16_to_cpu(el->l_tree_depth);
1163 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1164 struct buffer_head *bh = NULL;
1166 BUG_ON(meta_ac == NULL);
1168 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
1175 /* We traveled all the way to the bottom of the allocation tree
1176 * and didn't find room for any more extents - we need to add
1177 * another tree level */
1180 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1182 /* ocfs2_shift_tree_depth will return us a buffer with
1183 * the new extent block (so we can pass that to
1184 * ocfs2_add_branch). */
1185 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
1194 * Special case: we have room now if we shifted from
1195 * tree_depth 0, so no more work needs to be done.
1197 * We won't be calling add_branch, so pass
1198 * back *last_eb_bh as the new leaf. At depth
1199 * zero, it should always be null so there's
1200 * no reason to brelse.
1202 BUG_ON(*last_eb_bh);
1209 /* call ocfs2_add_branch to add the final part of the tree with
1211 mlog(0, "add branch. bh = %p\n", bh);
1212 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
1221 *final_depth = depth;
1227 * This function will discard the rightmost extent record.
1229 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1231 int next_free = le16_to_cpu(el->l_next_free_rec);
1232 int count = le16_to_cpu(el->l_count);
1233 unsigned int num_bytes;
1236 /* This will cause us to go off the end of our extent list. */
1237 BUG_ON(next_free >= count);
1239 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1241 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1244 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1245 struct ocfs2_extent_rec *insert_rec)
1247 int i, insert_index, next_free, has_empty, num_bytes;
1248 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1249 struct ocfs2_extent_rec *rec;
1251 next_free = le16_to_cpu(el->l_next_free_rec);
1252 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1256 /* The tree code before us didn't allow enough room in the leaf. */
1257 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1260 * The easiest way to approach this is to just remove the
1261 * empty extent and temporarily decrement next_free.
1265 * If next_free was 1 (only an empty extent), this
1266 * loop won't execute, which is fine. We still want
1267 * the decrement above to happen.
1269 for(i = 0; i < (next_free - 1); i++)
1270 el->l_recs[i] = el->l_recs[i+1];
1276 * Figure out what the new record index should be.
1278 for(i = 0; i < next_free; i++) {
1279 rec = &el->l_recs[i];
1281 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1286 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1287 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1289 BUG_ON(insert_index < 0);
1290 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1291 BUG_ON(insert_index > next_free);
1294 * No need to memmove if we're just adding to the tail.
1296 if (insert_index != next_free) {
1297 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1299 num_bytes = next_free - insert_index;
1300 num_bytes *= sizeof(struct ocfs2_extent_rec);
1301 memmove(&el->l_recs[insert_index + 1],
1302 &el->l_recs[insert_index],
1307 * Either we had an empty extent, and need to re-increment or
1308 * there was no empty extent on a non full rightmost leaf node,
1309 * in which case we still need to increment.
1312 el->l_next_free_rec = cpu_to_le16(next_free);
1314 * Make sure none of the math above just messed up our tree.
1316 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1318 el->l_recs[insert_index] = *insert_rec;
1322 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1324 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1326 BUG_ON(num_recs == 0);
1328 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1330 size = num_recs * sizeof(struct ocfs2_extent_rec);
1331 memmove(&el->l_recs[0], &el->l_recs[1], size);
1332 memset(&el->l_recs[num_recs], 0,
1333 sizeof(struct ocfs2_extent_rec));
1334 el->l_next_free_rec = cpu_to_le16(num_recs);
1339 * Create an empty extent record .
1341 * l_next_free_rec may be updated.
1343 * If an empty extent already exists do nothing.
1345 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1347 int next_free = le16_to_cpu(el->l_next_free_rec);
1349 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1354 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1357 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1358 "Asked to create an empty extent in a full list:\n"
1359 "count = %u, tree depth = %u",
1360 le16_to_cpu(el->l_count),
1361 le16_to_cpu(el->l_tree_depth));
1363 ocfs2_shift_records_right(el);
1366 le16_add_cpu(&el->l_next_free_rec, 1);
1367 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1371 * For a rotation which involves two leaf nodes, the "root node" is
1372 * the lowest level tree node which contains a path to both leafs. This
1373 * resulting set of information can be used to form a complete "subtree"
1375 * This function is passed two full paths from the dinode down to a
1376 * pair of adjacent leaves. It's task is to figure out which path
1377 * index contains the subtree root - this can be the root index itself
1378 * in a worst-case rotation.
1380 * The array index of the subtree root is passed back.
1382 static int ocfs2_find_subtree_root(struct inode *inode,
1383 struct ocfs2_path *left,
1384 struct ocfs2_path *right)
1389 * Check that the caller passed in two paths from the same tree.
1391 BUG_ON(path_root_bh(left) != path_root_bh(right));
1397 * The caller didn't pass two adjacent paths.
1399 mlog_bug_on_msg(i > left->p_tree_depth,
1400 "Inode %lu, left depth %u, right depth %u\n"
1401 "left leaf blk %llu, right leaf blk %llu\n",
1402 inode->i_ino, left->p_tree_depth,
1403 right->p_tree_depth,
1404 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1405 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1406 } while (left->p_node[i].bh->b_blocknr ==
1407 right->p_node[i].bh->b_blocknr);
1412 typedef void (path_insert_t)(void *, struct buffer_head *);
1415 * Traverse a btree path in search of cpos, starting at root_el.
1417 * This code can be called with a cpos larger than the tree, in which
1418 * case it will return the rightmost path.
1420 static int __ocfs2_find_path(struct inode *inode,
1421 struct ocfs2_extent_list *root_el, u32 cpos,
1422 path_insert_t *func, void *data)
1427 struct buffer_head *bh = NULL;
1428 struct ocfs2_extent_block *eb;
1429 struct ocfs2_extent_list *el;
1430 struct ocfs2_extent_rec *rec;
1431 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1434 while (el->l_tree_depth) {
1435 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1436 ocfs2_error(inode->i_sb,
1437 "Inode %llu has empty extent list at "
1439 (unsigned long long)oi->ip_blkno,
1440 le16_to_cpu(el->l_tree_depth));
1446 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1447 rec = &el->l_recs[i];
1450 * In the case that cpos is off the allocation
1451 * tree, this should just wind up returning the
1454 range = le32_to_cpu(rec->e_cpos) +
1455 ocfs2_rec_clusters(el, rec);
1456 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1460 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1462 ocfs2_error(inode->i_sb,
1463 "Inode %llu has bad blkno in extent list "
1464 "at depth %u (index %d)\n",
1465 (unsigned long long)oi->ip_blkno,
1466 le16_to_cpu(el->l_tree_depth), i);
1473 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1474 &bh, OCFS2_BH_CACHED, inode);
1480 eb = (struct ocfs2_extent_block *) bh->b_data;
1482 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1483 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1488 if (le16_to_cpu(el->l_next_free_rec) >
1489 le16_to_cpu(el->l_count)) {
1490 ocfs2_error(inode->i_sb,
1491 "Inode %llu has bad count in extent list "
1492 "at block %llu (next free=%u, count=%u)\n",
1493 (unsigned long long)oi->ip_blkno,
1494 (unsigned long long)bh->b_blocknr,
1495 le16_to_cpu(el->l_next_free_rec),
1496 le16_to_cpu(el->l_count));
1507 * Catch any trailing bh that the loop didn't handle.
1515 * Given an initialized path (that is, it has a valid root extent
1516 * list), this function will traverse the btree in search of the path
1517 * which would contain cpos.
1519 * The path traveled is recorded in the path structure.
1521 * Note that this will not do any comparisons on leaf node extent
1522 * records, so it will work fine in the case that we just added a tree
1525 struct find_path_data {
1527 struct ocfs2_path *path;
1529 static void find_path_ins(void *data, struct buffer_head *bh)
1531 struct find_path_data *fp = data;
1534 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1537 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1540 struct find_path_data data;
1544 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1545 find_path_ins, &data);
1548 static void find_leaf_ins(void *data, struct buffer_head *bh)
1550 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1551 struct ocfs2_extent_list *el = &eb->h_list;
1552 struct buffer_head **ret = data;
1554 /* We want to retain only the leaf block. */
1555 if (le16_to_cpu(el->l_tree_depth) == 0) {
1561 * Find the leaf block in the tree which would contain cpos. No
1562 * checking of the actual leaf is done.
1564 * Some paths want to call this instead of allocating a path structure
1565 * and calling ocfs2_find_path().
1567 * This function doesn't handle non btree extent lists.
1569 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1570 u32 cpos, struct buffer_head **leaf_bh)
1573 struct buffer_head *bh = NULL;
1575 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1587 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1589 * Basically, we've moved stuff around at the bottom of the tree and
1590 * we need to fix up the extent records above the changes to reflect
1593 * left_rec: the record on the left.
1594 * left_child_el: is the child list pointed to by left_rec
1595 * right_rec: the record to the right of left_rec
1596 * right_child_el: is the child list pointed to by right_rec
1598 * By definition, this only works on interior nodes.
1600 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1601 struct ocfs2_extent_list *left_child_el,
1602 struct ocfs2_extent_rec *right_rec,
1603 struct ocfs2_extent_list *right_child_el)
1605 u32 left_clusters, right_end;
1608 * Interior nodes never have holes. Their cpos is the cpos of
1609 * the leftmost record in their child list. Their cluster
1610 * count covers the full theoretical range of their child list
1611 * - the range between their cpos and the cpos of the record
1612 * immediately to their right.
1614 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1615 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1616 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1617 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1619 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1620 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1623 * Calculate the rightmost cluster count boundary before
1624 * moving cpos - we will need to adjust clusters after
1625 * updating e_cpos to keep the same highest cluster count.
1627 right_end = le32_to_cpu(right_rec->e_cpos);
1628 right_end += le32_to_cpu(right_rec->e_int_clusters);
1630 right_rec->e_cpos = left_rec->e_cpos;
1631 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1633 right_end -= le32_to_cpu(right_rec->e_cpos);
1634 right_rec->e_int_clusters = cpu_to_le32(right_end);
1638 * Adjust the adjacent root node records involved in a
1639 * rotation. left_el_blkno is passed in as a key so that we can easily
1640 * find it's index in the root list.
1642 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1643 struct ocfs2_extent_list *left_el,
1644 struct ocfs2_extent_list *right_el,
1649 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1650 le16_to_cpu(left_el->l_tree_depth));
1652 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1653 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1658 * The path walking code should have never returned a root and
1659 * two paths which are not adjacent.
1661 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1663 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1664 &root_el->l_recs[i + 1], right_el);
1668 * We've changed a leaf block (in right_path) and need to reflect that
1669 * change back up the subtree.
1671 * This happens in multiple places:
1672 * - When we've moved an extent record from the left path leaf to the right
1673 * path leaf to make room for an empty extent in the left path leaf.
1674 * - When our insert into the right path leaf is at the leftmost edge
1675 * and requires an update of the path immediately to it's left. This
1676 * can occur at the end of some types of rotation and appending inserts.
1677 * - When we've adjusted the last extent record in the left path leaf and the
1678 * 1st extent record in the right path leaf during cross extent block merge.
1680 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1681 struct ocfs2_path *left_path,
1682 struct ocfs2_path *right_path,
1686 struct ocfs2_extent_list *el, *left_el, *right_el;
1687 struct ocfs2_extent_rec *left_rec, *right_rec;
1688 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1691 * Update the counts and position values within all the
1692 * interior nodes to reflect the leaf rotation we just did.
1694 * The root node is handled below the loop.
1696 * We begin the loop with right_el and left_el pointing to the
1697 * leaf lists and work our way up.
1699 * NOTE: within this loop, left_el and right_el always refer
1700 * to the *child* lists.
1702 left_el = path_leaf_el(left_path);
1703 right_el = path_leaf_el(right_path);
1704 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1705 mlog(0, "Adjust records at index %u\n", i);
1708 * One nice property of knowing that all of these
1709 * nodes are below the root is that we only deal with
1710 * the leftmost right node record and the rightmost
1713 el = left_path->p_node[i].el;
1714 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1715 left_rec = &el->l_recs[idx];
1717 el = right_path->p_node[i].el;
1718 right_rec = &el->l_recs[0];
1720 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1723 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1727 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1732 * Setup our list pointers now so that the current
1733 * parents become children in the next iteration.
1735 left_el = left_path->p_node[i].el;
1736 right_el = right_path->p_node[i].el;
1740 * At the root node, adjust the two adjacent records which
1741 * begin our path to the leaves.
1744 el = left_path->p_node[subtree_index].el;
1745 left_el = left_path->p_node[subtree_index + 1].el;
1746 right_el = right_path->p_node[subtree_index + 1].el;
1748 ocfs2_adjust_root_records(el, left_el, right_el,
1749 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1751 root_bh = left_path->p_node[subtree_index].bh;
1753 ret = ocfs2_journal_dirty(handle, root_bh);
1758 static int ocfs2_rotate_subtree_right(struct inode *inode,
1760 struct ocfs2_path *left_path,
1761 struct ocfs2_path *right_path,
1765 struct buffer_head *right_leaf_bh;
1766 struct buffer_head *left_leaf_bh = NULL;
1767 struct buffer_head *root_bh;
1768 struct ocfs2_extent_list *right_el, *left_el;
1769 struct ocfs2_extent_rec move_rec;
1771 left_leaf_bh = path_leaf_bh(left_path);
1772 left_el = path_leaf_el(left_path);
1774 if (left_el->l_next_free_rec != left_el->l_count) {
1775 ocfs2_error(inode->i_sb,
1776 "Inode %llu has non-full interior leaf node %llu"
1778 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1779 (unsigned long long)left_leaf_bh->b_blocknr,
1780 le16_to_cpu(left_el->l_next_free_rec));
1785 * This extent block may already have an empty record, so we
1786 * return early if so.
1788 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1791 root_bh = left_path->p_node[subtree_index].bh;
1792 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1794 ret = ocfs2_journal_access(handle, inode, root_bh,
1795 OCFS2_JOURNAL_ACCESS_WRITE);
1801 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1802 ret = ocfs2_journal_access(handle, inode,
1803 right_path->p_node[i].bh,
1804 OCFS2_JOURNAL_ACCESS_WRITE);
1810 ret = ocfs2_journal_access(handle, inode,
1811 left_path->p_node[i].bh,
1812 OCFS2_JOURNAL_ACCESS_WRITE);
1819 right_leaf_bh = path_leaf_bh(right_path);
1820 right_el = path_leaf_el(right_path);
1822 /* This is a code error, not a disk corruption. */
1823 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1824 "because rightmost leaf block %llu is empty\n",
1825 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1826 (unsigned long long)right_leaf_bh->b_blocknr);
1828 ocfs2_create_empty_extent(right_el);
1830 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1836 /* Do the copy now. */
1837 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1838 move_rec = left_el->l_recs[i];
1839 right_el->l_recs[0] = move_rec;
1842 * Clear out the record we just copied and shift everything
1843 * over, leaving an empty extent in the left leaf.
1845 * We temporarily subtract from next_free_rec so that the
1846 * shift will lose the tail record (which is now defunct).
1848 le16_add_cpu(&left_el->l_next_free_rec, -1);
1849 ocfs2_shift_records_right(left_el);
1850 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1851 le16_add_cpu(&left_el->l_next_free_rec, 1);
1853 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1859 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1867 * Given a full path, determine what cpos value would return us a path
1868 * containing the leaf immediately to the left of the current one.
1870 * Will return zero if the path passed in is already the leftmost path.
1872 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1873 struct ocfs2_path *path, u32 *cpos)
1877 struct ocfs2_extent_list *el;
1879 BUG_ON(path->p_tree_depth == 0);
1883 blkno = path_leaf_bh(path)->b_blocknr;
1885 /* Start at the tree node just above the leaf and work our way up. */
1886 i = path->p_tree_depth - 1;
1888 el = path->p_node[i].el;
1891 * Find the extent record just before the one in our
1894 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1895 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1899 * We've determined that the
1900 * path specified is already
1901 * the leftmost one - return a
1907 * The leftmost record points to our
1908 * leaf - we need to travel up the
1914 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
1915 *cpos = *cpos + ocfs2_rec_clusters(el,
1916 &el->l_recs[j - 1]);
1923 * If we got here, we never found a valid node where
1924 * the tree indicated one should be.
1927 "Invalid extent tree at extent block %llu\n",
1928 (unsigned long long)blkno);
1933 blkno = path->p_node[i].bh->b_blocknr;
1942 * Extend the transaction by enough credits to complete the rotation,
1943 * and still leave at least the original number of credits allocated
1944 * to this transaction.
1946 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
1948 struct ocfs2_path *path)
1950 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
1952 if (handle->h_buffer_credits < credits)
1953 return ocfs2_extend_trans(handle, credits);
1959 * Trap the case where we're inserting into the theoretical range past
1960 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1961 * whose cpos is less than ours into the right leaf.
1963 * It's only necessary to look at the rightmost record of the left
1964 * leaf because the logic that calls us should ensure that the
1965 * theoretical ranges in the path components above the leaves are
1968 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1971 struct ocfs2_extent_list *left_el;
1972 struct ocfs2_extent_rec *rec;
1975 left_el = path_leaf_el(left_path);
1976 next_free = le16_to_cpu(left_el->l_next_free_rec);
1977 rec = &left_el->l_recs[next_free - 1];
1979 if (insert_cpos > le32_to_cpu(rec->e_cpos))
1984 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
1986 int next_free = le16_to_cpu(el->l_next_free_rec);
1988 struct ocfs2_extent_rec *rec;
1993 rec = &el->l_recs[0];
1994 if (ocfs2_is_empty_extent(rec)) {
1998 rec = &el->l_recs[1];
2001 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2002 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2008 * Rotate all the records in a btree right one record, starting at insert_cpos.
2010 * The path to the rightmost leaf should be passed in.
2012 * The array is assumed to be large enough to hold an entire path (tree depth).
2014 * Upon succesful return from this function:
2016 * - The 'right_path' array will contain a path to the leaf block
2017 * whose range contains e_cpos.
2018 * - That leaf block will have a single empty extent in list index 0.
2019 * - In the case that the rotation requires a post-insert update,
2020 * *ret_left_path will contain a valid path which can be passed to
2021 * ocfs2_insert_path().
2023 static int ocfs2_rotate_tree_right(struct inode *inode,
2025 enum ocfs2_split_type split,
2027 struct ocfs2_path *right_path,
2028 struct ocfs2_path **ret_left_path)
2030 int ret, start, orig_credits = handle->h_buffer_credits;
2032 struct ocfs2_path *left_path = NULL;
2034 *ret_left_path = NULL;
2036 left_path = ocfs2_new_path(path_root_bh(right_path),
2037 path_root_el(right_path));
2044 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2050 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2053 * What we want to do here is:
2055 * 1) Start with the rightmost path.
2057 * 2) Determine a path to the leaf block directly to the left
2060 * 3) Determine the 'subtree root' - the lowest level tree node
2061 * which contains a path to both leaves.
2063 * 4) Rotate the subtree.
2065 * 5) Find the next subtree by considering the left path to be
2066 * the new right path.
2068 * The check at the top of this while loop also accepts
2069 * insert_cpos == cpos because cpos is only a _theoretical_
2070 * value to get us the left path - insert_cpos might very well
2071 * be filling that hole.
2073 * Stop at a cpos of '0' because we either started at the
2074 * leftmost branch (i.e., a tree with one branch and a
2075 * rotation inside of it), or we've gone as far as we can in
2076 * rotating subtrees.
2078 while (cpos && insert_cpos <= cpos) {
2079 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2082 ret = ocfs2_find_path(inode, left_path, cpos);
2088 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2089 path_leaf_bh(right_path),
2090 "Inode %lu: error during insert of %u "
2091 "(left path cpos %u) results in two identical "
2092 "paths ending at %llu\n",
2093 inode->i_ino, insert_cpos, cpos,
2094 (unsigned long long)
2095 path_leaf_bh(left_path)->b_blocknr);
2097 if (split == SPLIT_NONE &&
2098 ocfs2_rotate_requires_path_adjustment(left_path,
2102 * We've rotated the tree as much as we
2103 * should. The rest is up to
2104 * ocfs2_insert_path() to complete, after the
2105 * record insertion. We indicate this
2106 * situation by returning the left path.
2108 * The reason we don't adjust the records here
2109 * before the record insert is that an error
2110 * later might break the rule where a parent
2111 * record e_cpos will reflect the actual
2112 * e_cpos of the 1st nonempty record of the
2115 *ret_left_path = left_path;
2119 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2121 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2123 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2124 right_path->p_tree_depth);
2126 ret = ocfs2_extend_rotate_transaction(handle, start,
2127 orig_credits, right_path);
2133 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2140 if (split != SPLIT_NONE &&
2141 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2144 * A rotate moves the rightmost left leaf
2145 * record over to the leftmost right leaf
2146 * slot. If we're doing an extent split
2147 * instead of a real insert, then we have to
2148 * check that the extent to be split wasn't
2149 * just moved over. If it was, then we can
2150 * exit here, passing left_path back -
2151 * ocfs2_split_extent() is smart enough to
2152 * search both leaves.
2154 *ret_left_path = left_path;
2159 * There is no need to re-read the next right path
2160 * as we know that it'll be our current left
2161 * path. Optimize by copying values instead.
2163 ocfs2_mv_path(right_path, left_path);
2165 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2174 ocfs2_free_path(left_path);
2180 static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2181 struct ocfs2_path *path)
2184 struct ocfs2_extent_rec *rec;
2185 struct ocfs2_extent_list *el;
2186 struct ocfs2_extent_block *eb;
2189 /* Path should always be rightmost. */
2190 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2191 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2194 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2195 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2196 rec = &el->l_recs[idx];
2197 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2199 for (i = 0; i < path->p_tree_depth; i++) {
2200 el = path->p_node[i].el;
2201 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2202 rec = &el->l_recs[idx];
2204 rec->e_int_clusters = cpu_to_le32(range);
2205 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2207 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2211 static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2212 struct ocfs2_cached_dealloc_ctxt *dealloc,
2213 struct ocfs2_path *path, int unlink_start)
2216 struct ocfs2_extent_block *eb;
2217 struct ocfs2_extent_list *el;
2218 struct buffer_head *bh;
2220 for(i = unlink_start; i < path_num_items(path); i++) {
2221 bh = path->p_node[i].bh;
2223 eb = (struct ocfs2_extent_block *)bh->b_data;
2225 * Not all nodes might have had their final count
2226 * decremented by the caller - handle this here.
2229 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2231 "Inode %llu, attempted to remove extent block "
2232 "%llu with %u records\n",
2233 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2234 (unsigned long long)le64_to_cpu(eb->h_blkno),
2235 le16_to_cpu(el->l_next_free_rec));
2237 ocfs2_journal_dirty(handle, bh);
2238 ocfs2_remove_from_cache(inode, bh);
2242 el->l_next_free_rec = 0;
2243 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2245 ocfs2_journal_dirty(handle, bh);
2247 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2251 ocfs2_remove_from_cache(inode, bh);
2255 static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2256 struct ocfs2_path *left_path,
2257 struct ocfs2_path *right_path,
2259 struct ocfs2_cached_dealloc_ctxt *dealloc)
2262 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2263 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2264 struct ocfs2_extent_list *el;
2265 struct ocfs2_extent_block *eb;
2267 el = path_leaf_el(left_path);
2269 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2271 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2272 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2275 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2277 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2278 le16_add_cpu(&root_el->l_next_free_rec, -1);
2280 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2281 eb->h_next_leaf_blk = 0;
2283 ocfs2_journal_dirty(handle, root_bh);
2284 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2286 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2290 static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2291 struct ocfs2_path *left_path,
2292 struct ocfs2_path *right_path,
2294 struct ocfs2_cached_dealloc_ctxt *dealloc,
2296 struct ocfs2_extent_tree *et)
2298 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2299 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2300 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2301 struct ocfs2_extent_block *eb;
2305 right_leaf_el = path_leaf_el(right_path);
2306 left_leaf_el = path_leaf_el(left_path);
2307 root_bh = left_path->p_node[subtree_index].bh;
2308 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2310 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2313 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2314 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2316 * It's legal for us to proceed if the right leaf is
2317 * the rightmost one and it has an empty extent. There
2318 * are two cases to handle - whether the leaf will be
2319 * empty after removal or not. If the leaf isn't empty
2320 * then just remove the empty extent up front. The
2321 * next block will handle empty leaves by flagging
2324 * Non rightmost leaves will throw -EAGAIN and the
2325 * caller can manually move the subtree and retry.
2328 if (eb->h_next_leaf_blk != 0ULL)
2331 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2332 ret = ocfs2_journal_access(handle, inode,
2333 path_leaf_bh(right_path),
2334 OCFS2_JOURNAL_ACCESS_WRITE);
2340 ocfs2_remove_empty_extent(right_leaf_el);
2342 right_has_empty = 1;
2345 if (eb->h_next_leaf_blk == 0ULL &&
2346 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2348 * We have to update i_last_eb_blk during the meta
2351 ret = ocfs2_journal_access(handle, inode, et_root_bh,
2352 OCFS2_JOURNAL_ACCESS_WRITE);
2358 del_right_subtree = 1;
2362 * Getting here with an empty extent in the right path implies
2363 * that it's the rightmost path and will be deleted.
2365 BUG_ON(right_has_empty && !del_right_subtree);
2367 ret = ocfs2_journal_access(handle, inode, root_bh,
2368 OCFS2_JOURNAL_ACCESS_WRITE);
2374 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2375 ret = ocfs2_journal_access(handle, inode,
2376 right_path->p_node[i].bh,
2377 OCFS2_JOURNAL_ACCESS_WRITE);
2383 ret = ocfs2_journal_access(handle, inode,
2384 left_path->p_node[i].bh,
2385 OCFS2_JOURNAL_ACCESS_WRITE);
2392 if (!right_has_empty) {
2394 * Only do this if we're moving a real
2395 * record. Otherwise, the action is delayed until
2396 * after removal of the right path in which case we
2397 * can do a simple shift to remove the empty extent.
2399 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2400 memset(&right_leaf_el->l_recs[0], 0,
2401 sizeof(struct ocfs2_extent_rec));
2403 if (eb->h_next_leaf_blk == 0ULL) {
2405 * Move recs over to get rid of empty extent, decrease
2406 * next_free. This is allowed to remove the last
2407 * extent in our leaf (setting l_next_free_rec to
2408 * zero) - the delete code below won't care.
2410 ocfs2_remove_empty_extent(right_leaf_el);
2413 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2416 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2420 if (del_right_subtree) {
2421 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2422 subtree_index, dealloc);
2423 ocfs2_update_edge_lengths(inode, handle, left_path);
2425 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2426 ocfs2_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2429 * Removal of the extent in the left leaf was skipped
2430 * above so we could delete the right path
2433 if (right_has_empty)
2434 ocfs2_remove_empty_extent(left_leaf_el);
2436 ret = ocfs2_journal_dirty(handle, et_root_bh);
2442 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2450 * Given a full path, determine what cpos value would return us a path
2451 * containing the leaf immediately to the right of the current one.
2453 * Will return zero if the path passed in is already the rightmost path.
2455 * This looks similar, but is subtly different to
2456 * ocfs2_find_cpos_for_left_leaf().
2458 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2459 struct ocfs2_path *path, u32 *cpos)
2463 struct ocfs2_extent_list *el;
2467 if (path->p_tree_depth == 0)
2470 blkno = path_leaf_bh(path)->b_blocknr;
2472 /* Start at the tree node just above the leaf and work our way up. */
2473 i = path->p_tree_depth - 1;
2477 el = path->p_node[i].el;
2480 * Find the extent record just after the one in our
2483 next_free = le16_to_cpu(el->l_next_free_rec);
2484 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2485 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2486 if (j == (next_free - 1)) {
2489 * We've determined that the
2490 * path specified is already
2491 * the rightmost one - return a
2497 * The rightmost record points to our
2498 * leaf - we need to travel up the
2504 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2510 * If we got here, we never found a valid node where
2511 * the tree indicated one should be.
2514 "Invalid extent tree at extent block %llu\n",
2515 (unsigned long long)blkno);
2520 blkno = path->p_node[i].bh->b_blocknr;
2528 static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2530 struct buffer_head *bh,
2531 struct ocfs2_extent_list *el)
2535 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2538 ret = ocfs2_journal_access(handle, inode, bh,
2539 OCFS2_JOURNAL_ACCESS_WRITE);
2545 ocfs2_remove_empty_extent(el);
2547 ret = ocfs2_journal_dirty(handle, bh);
2555 static int __ocfs2_rotate_tree_left(struct inode *inode,
2556 handle_t *handle, int orig_credits,
2557 struct ocfs2_path *path,
2558 struct ocfs2_cached_dealloc_ctxt *dealloc,
2559 struct ocfs2_path **empty_extent_path,
2560 struct ocfs2_extent_tree *et)
2562 int ret, subtree_root, deleted;
2564 struct ocfs2_path *left_path = NULL;
2565 struct ocfs2_path *right_path = NULL;
2567 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2569 *empty_extent_path = NULL;
2571 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2578 left_path = ocfs2_new_path(path_root_bh(path),
2579 path_root_el(path));
2586 ocfs2_cp_path(left_path, path);
2588 right_path = ocfs2_new_path(path_root_bh(path),
2589 path_root_el(path));
2596 while (right_cpos) {
2597 ret = ocfs2_find_path(inode, right_path, right_cpos);
2603 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2606 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2608 (unsigned long long)
2609 right_path->p_node[subtree_root].bh->b_blocknr,
2610 right_path->p_tree_depth);
2612 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2613 orig_credits, left_path);
2620 * Caller might still want to make changes to the
2621 * tree root, so re-add it to the journal here.
2623 ret = ocfs2_journal_access(handle, inode,
2624 path_root_bh(left_path),
2625 OCFS2_JOURNAL_ACCESS_WRITE);
2631 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2632 right_path, subtree_root,
2633 dealloc, &deleted, et);
2634 if (ret == -EAGAIN) {
2636 * The rotation has to temporarily stop due to
2637 * the right subtree having an empty
2638 * extent. Pass it back to the caller for a
2641 *empty_extent_path = right_path;
2651 * The subtree rotate might have removed records on
2652 * the rightmost edge. If so, then rotation is
2658 ocfs2_mv_path(left_path, right_path);
2660 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2669 ocfs2_free_path(right_path);
2670 ocfs2_free_path(left_path);
2675 static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2676 struct ocfs2_path *path,
2677 struct ocfs2_cached_dealloc_ctxt *dealloc,
2678 struct ocfs2_extent_tree *et)
2680 int ret, subtree_index;
2682 struct ocfs2_path *left_path = NULL;
2683 struct ocfs2_extent_block *eb;
2684 struct ocfs2_extent_list *el;
2687 ret = et->eops->sanity_check(inode, et);
2691 * There's two ways we handle this depending on
2692 * whether path is the only existing one.
2694 ret = ocfs2_extend_rotate_transaction(handle, 0,
2695 handle->h_buffer_credits,
2702 ret = ocfs2_journal_access_path(inode, handle, path);
2708 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2716 * We have a path to the left of this one - it needs
2719 left_path = ocfs2_new_path(path_root_bh(path),
2720 path_root_el(path));
2727 ret = ocfs2_find_path(inode, left_path, cpos);
2733 ret = ocfs2_journal_access_path(inode, handle, left_path);
2739 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2741 ocfs2_unlink_subtree(inode, handle, left_path, path,
2742 subtree_index, dealloc);
2743 ocfs2_update_edge_lengths(inode, handle, left_path);
2745 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2746 ocfs2_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2749 * 'path' is also the leftmost path which
2750 * means it must be the only one. This gets
2751 * handled differently because we want to
2752 * revert the inode back to having extents
2755 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2758 el->l_tree_depth = 0;
2759 el->l_next_free_rec = 0;
2760 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2762 ocfs2_set_last_eb_blk(et, 0);
2765 ocfs2_journal_dirty(handle, path_root_bh(path));
2768 ocfs2_free_path(left_path);
2773 * Left rotation of btree records.
2775 * In many ways, this is (unsurprisingly) the opposite of right
2776 * rotation. We start at some non-rightmost path containing an empty
2777 * extent in the leaf block. The code works its way to the rightmost
2778 * path by rotating records to the left in every subtree.
2780 * This is used by any code which reduces the number of extent records
2781 * in a leaf. After removal, an empty record should be placed in the
2782 * leftmost list position.
2784 * This won't handle a length update of the rightmost path records if
2785 * the rightmost tree leaf record is removed so the caller is
2786 * responsible for detecting and correcting that.
2788 static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2789 struct ocfs2_path *path,
2790 struct ocfs2_cached_dealloc_ctxt *dealloc,
2791 struct ocfs2_extent_tree *et)
2793 int ret, orig_credits = handle->h_buffer_credits;
2794 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2795 struct ocfs2_extent_block *eb;
2796 struct ocfs2_extent_list *el;
2798 el = path_leaf_el(path);
2799 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2802 if (path->p_tree_depth == 0) {
2803 rightmost_no_delete:
2805 * Inline extents. This is trivially handled, so do
2808 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2810 path_leaf_el(path));
2817 * Handle rightmost branch now. There's several cases:
2818 * 1) simple rotation leaving records in there. That's trivial.
2819 * 2) rotation requiring a branch delete - there's no more
2820 * records left. Two cases of this:
2821 * a) There are branches to the left.
2822 * b) This is also the leftmost (the only) branch.
2824 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2825 * 2a) we need the left branch so that we can update it with the unlink
2826 * 2b) we need to bring the inode back to inline extents.
2829 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2831 if (eb->h_next_leaf_blk == 0) {
2833 * This gets a bit tricky if we're going to delete the
2834 * rightmost path. Get the other cases out of the way
2837 if (le16_to_cpu(el->l_next_free_rec) > 1)
2838 goto rightmost_no_delete;
2840 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2842 ocfs2_error(inode->i_sb,
2843 "Inode %llu has empty extent block at %llu",
2844 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2845 (unsigned long long)le64_to_cpu(eb->h_blkno));
2850 * XXX: The caller can not trust "path" any more after
2851 * this as it will have been deleted. What do we do?
2853 * In theory the rotate-for-merge code will never get
2854 * here because it'll always ask for a rotate in a
2858 ret = ocfs2_remove_rightmost_path(inode, handle, path,
2866 * Now we can loop, remembering the path we get from -EAGAIN
2867 * and restarting from there.
2870 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
2871 dealloc, &restart_path, et);
2872 if (ret && ret != -EAGAIN) {
2877 while (ret == -EAGAIN) {
2878 tmp_path = restart_path;
2879 restart_path = NULL;
2881 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2884 if (ret && ret != -EAGAIN) {
2889 ocfs2_free_path(tmp_path);
2897 ocfs2_free_path(tmp_path);
2898 ocfs2_free_path(restart_path);
2902 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2905 struct ocfs2_extent_rec *rec = &el->l_recs[index];
2908 if (rec->e_leaf_clusters == 0) {
2910 * We consumed all of the merged-from record. An empty
2911 * extent cannot exist anywhere but the 1st array
2912 * position, so move things over if the merged-from
2913 * record doesn't occupy that position.
2915 * This creates a new empty extent so the caller
2916 * should be smart enough to have removed any existing
2920 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
2921 size = index * sizeof(struct ocfs2_extent_rec);
2922 memmove(&el->l_recs[1], &el->l_recs[0], size);
2926 * Always memset - the caller doesn't check whether it
2927 * created an empty extent, so there could be junk in
2930 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2934 static int ocfs2_get_right_path(struct inode *inode,
2935 struct ocfs2_path *left_path,
2936 struct ocfs2_path **ret_right_path)
2940 struct ocfs2_path *right_path = NULL;
2941 struct ocfs2_extent_list *left_el;
2943 *ret_right_path = NULL;
2945 /* This function shouldn't be called for non-trees. */
2946 BUG_ON(left_path->p_tree_depth == 0);
2948 left_el = path_leaf_el(left_path);
2949 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
2951 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2958 /* This function shouldn't be called for the rightmost leaf. */
2959 BUG_ON(right_cpos == 0);
2961 right_path = ocfs2_new_path(path_root_bh(left_path),
2962 path_root_el(left_path));
2969 ret = ocfs2_find_path(inode, right_path, right_cpos);
2975 *ret_right_path = right_path;
2978 ocfs2_free_path(right_path);
2983 * Remove split_rec clusters from the record at index and merge them
2984 * onto the beginning of the record "next" to it.
2985 * For index < l_count - 1, the next means the extent rec at index + 1.
2986 * For index == l_count - 1, the "next" means the 1st extent rec of the
2987 * next extent block.
2989 static int ocfs2_merge_rec_right(struct inode *inode,
2990 struct ocfs2_path *left_path,
2992 struct ocfs2_extent_rec *split_rec,
2995 int ret, next_free, i;
2996 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
2997 struct ocfs2_extent_rec *left_rec;
2998 struct ocfs2_extent_rec *right_rec;
2999 struct ocfs2_extent_list *right_el;
3000 struct ocfs2_path *right_path = NULL;
3001 int subtree_index = 0;
3002 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3003 struct buffer_head *bh = path_leaf_bh(left_path);
3004 struct buffer_head *root_bh = NULL;
3006 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3007 left_rec = &el->l_recs[index];
3009 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3010 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3011 /* we meet with a cross extent block merge. */
3012 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3018 right_el = path_leaf_el(right_path);
3019 next_free = le16_to_cpu(right_el->l_next_free_rec);
3020 BUG_ON(next_free <= 0);
3021 right_rec = &right_el->l_recs[0];
3022 if (ocfs2_is_empty_extent(right_rec)) {
3023 BUG_ON(next_free <= 1);
3024 right_rec = &right_el->l_recs[1];
3027 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3028 le16_to_cpu(left_rec->e_leaf_clusters) !=
3029 le32_to_cpu(right_rec->e_cpos));
3031 subtree_index = ocfs2_find_subtree_root(inode,
3032 left_path, right_path);
3034 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3035 handle->h_buffer_credits,
3042 root_bh = left_path->p_node[subtree_index].bh;
3043 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3045 ret = ocfs2_journal_access(handle, inode, root_bh,
3046 OCFS2_JOURNAL_ACCESS_WRITE);
3052 for (i = subtree_index + 1;
3053 i < path_num_items(right_path); i++) {
3054 ret = ocfs2_journal_access(handle, inode,
3055 right_path->p_node[i].bh,
3056 OCFS2_JOURNAL_ACCESS_WRITE);
3062 ret = ocfs2_journal_access(handle, inode,
3063 left_path->p_node[i].bh,
3064 OCFS2_JOURNAL_ACCESS_WRITE);
3072 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3073 right_rec = &el->l_recs[index + 1];
3076 ret = ocfs2_journal_access(handle, inode, bh,
3077 OCFS2_JOURNAL_ACCESS_WRITE);
3083 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3085 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3086 le64_add_cpu(&right_rec->e_blkno,
3087 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3088 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3090 ocfs2_cleanup_merge(el, index);
3092 ret = ocfs2_journal_dirty(handle, bh);
3097 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3101 ocfs2_complete_edge_insert(inode, handle, left_path,
3102 right_path, subtree_index);
3106 ocfs2_free_path(right_path);
3110 static int ocfs2_get_left_path(struct inode *inode,
3111 struct ocfs2_path *right_path,
3112 struct ocfs2_path **ret_left_path)
3116 struct ocfs2_path *left_path = NULL;
3118 *ret_left_path = NULL;
3120 /* This function shouldn't be called for non-trees. */
3121 BUG_ON(right_path->p_tree_depth == 0);
3123 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3124 right_path, &left_cpos);
3130 /* This function shouldn't be called for the leftmost leaf. */
3131 BUG_ON(left_cpos == 0);
3133 left_path = ocfs2_new_path(path_root_bh(right_path),
3134 path_root_el(right_path));
3141 ret = ocfs2_find_path(inode, left_path, left_cpos);
3147 *ret_left_path = left_path;
3150 ocfs2_free_path(left_path);
3155 * Remove split_rec clusters from the record at index and merge them
3156 * onto the tail of the record "before" it.
3157 * For index > 0, the "before" means the extent rec at index - 1.
3159 * For index == 0, the "before" means the last record of the previous
3160 * extent block. And there is also a situation that we may need to
3161 * remove the rightmost leaf extent block in the right_path and change
3162 * the right path to indicate the new rightmost path.
3164 static int ocfs2_merge_rec_left(struct inode *inode,
3165 struct ocfs2_path *right_path,
3167 struct ocfs2_extent_rec *split_rec,
3168 struct ocfs2_cached_dealloc_ctxt *dealloc,
3169 struct ocfs2_extent_tree *et,
3172 int ret, i, subtree_index = 0, has_empty_extent = 0;
3173 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3174 struct ocfs2_extent_rec *left_rec;
3175 struct ocfs2_extent_rec *right_rec;
3176 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3177 struct buffer_head *bh = path_leaf_bh(right_path);
3178 struct buffer_head *root_bh = NULL;
3179 struct ocfs2_path *left_path = NULL;
3180 struct ocfs2_extent_list *left_el;
3184 right_rec = &el->l_recs[index];
3186 /* we meet with a cross extent block merge. */
3187 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3193 left_el = path_leaf_el(left_path);
3194 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3195 le16_to_cpu(left_el->l_count));
3197 left_rec = &left_el->l_recs[
3198 le16_to_cpu(left_el->l_next_free_rec) - 1];
3199 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3200 le16_to_cpu(left_rec->e_leaf_clusters) !=
3201 le32_to_cpu(split_rec->e_cpos));
3203 subtree_index = ocfs2_find_subtree_root(inode,
3204 left_path, right_path);
3206 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3207 handle->h_buffer_credits,
3214 root_bh = left_path->p_node[subtree_index].bh;
3215 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3217 ret = ocfs2_journal_access(handle, inode, root_bh,
3218 OCFS2_JOURNAL_ACCESS_WRITE);
3224 for (i = subtree_index + 1;
3225 i < path_num_items(right_path); i++) {
3226 ret = ocfs2_journal_access(handle, inode,
3227 right_path->p_node[i].bh,
3228 OCFS2_JOURNAL_ACCESS_WRITE);
3234 ret = ocfs2_journal_access(handle, inode,
3235 left_path->p_node[i].bh,
3236 OCFS2_JOURNAL_ACCESS_WRITE);
3243 left_rec = &el->l_recs[index - 1];
3244 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3245 has_empty_extent = 1;
3248 ret = ocfs2_journal_access(handle, inode, bh,
3249 OCFS2_JOURNAL_ACCESS_WRITE);
3255 if (has_empty_extent && index == 1) {
3257 * The easy case - we can just plop the record right in.
3259 *left_rec = *split_rec;
3261 has_empty_extent = 0;
3263 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3265 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3266 le64_add_cpu(&right_rec->e_blkno,
3267 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3268 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3270 ocfs2_cleanup_merge(el, index);
3272 ret = ocfs2_journal_dirty(handle, bh);
3277 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3282 * In the situation that the right_rec is empty and the extent
3283 * block is empty also, ocfs2_complete_edge_insert can't handle
3284 * it and we need to delete the right extent block.
3286 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3287 le16_to_cpu(el->l_next_free_rec) == 1) {
3289 ret = ocfs2_remove_rightmost_path(inode, handle,
3297 /* Now the rightmost extent block has been deleted.
3298 * So we use the new rightmost path.
3300 ocfs2_mv_path(right_path, left_path);
3303 ocfs2_complete_edge_insert(inode, handle, left_path,
3304 right_path, subtree_index);
3308 ocfs2_free_path(left_path);
3312 static int ocfs2_try_to_merge_extent(struct inode *inode,
3314 struct ocfs2_path *path,
3316 struct ocfs2_extent_rec *split_rec,
3317 struct ocfs2_cached_dealloc_ctxt *dealloc,
3318 struct ocfs2_merge_ctxt *ctxt,
3319 struct ocfs2_extent_tree *et)
3323 struct ocfs2_extent_list *el = path_leaf_el(path);
3324 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3326 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3328 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3330 * The merge code will need to create an empty
3331 * extent to take the place of the newly
3332 * emptied slot. Remove any pre-existing empty
3333 * extents - having more than one in a leaf is
3336 ret = ocfs2_rotate_tree_left(inode, handle, path,
3343 rec = &el->l_recs[split_index];
3346 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3348 * Left-right contig implies this.
3350 BUG_ON(!ctxt->c_split_covers_rec);
3353 * Since the leftright insert always covers the entire
3354 * extent, this call will delete the insert record
3355 * entirely, resulting in an empty extent record added to
3358 * Since the adding of an empty extent shifts
3359 * everything back to the right, there's no need to
3360 * update split_index here.
3362 * When the split_index is zero, we need to merge it to the
3363 * prevoius extent block. It is more efficient and easier
3364 * if we do merge_right first and merge_left later.
3366 ret = ocfs2_merge_rec_right(inode, path,
3375 * We can only get this from logic error above.
3377 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3379 /* The merge left us with an empty extent, remove it. */
3380 ret = ocfs2_rotate_tree_left(inode, handle, path,
3387 rec = &el->l_recs[split_index];
3390 * Note that we don't pass split_rec here on purpose -
3391 * we've merged it into the rec already.
3393 ret = ocfs2_merge_rec_left(inode, path,
3403 ret = ocfs2_rotate_tree_left(inode, handle, path,
3406 * Error from this last rotate is not critical, so
3407 * print but don't bubble it up.
3414 * Merge a record to the left or right.
3416 * 'contig_type' is relative to the existing record,
3417 * so for example, if we're "right contig", it's to
3418 * the record on the left (hence the left merge).
3420 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3421 ret = ocfs2_merge_rec_left(inode,
3431 ret = ocfs2_merge_rec_right(inode,
3441 if (ctxt->c_split_covers_rec) {
3443 * The merge may have left an empty extent in
3444 * our leaf. Try to rotate it away.
3446 ret = ocfs2_rotate_tree_left(inode, handle, path,
3458 static void ocfs2_subtract_from_rec(struct super_block *sb,
3459 enum ocfs2_split_type split,
3460 struct ocfs2_extent_rec *rec,
3461 struct ocfs2_extent_rec *split_rec)
3465 len_blocks = ocfs2_clusters_to_blocks(sb,
3466 le16_to_cpu(split_rec->e_leaf_clusters));
3468 if (split == SPLIT_LEFT) {
3470 * Region is on the left edge of the existing
3473 le32_add_cpu(&rec->e_cpos,
3474 le16_to_cpu(split_rec->e_leaf_clusters));
3475 le64_add_cpu(&rec->e_blkno, len_blocks);
3476 le16_add_cpu(&rec->e_leaf_clusters,
3477 -le16_to_cpu(split_rec->e_leaf_clusters));
3480 * Region is on the right edge of the existing
3483 le16_add_cpu(&rec->e_leaf_clusters,
3484 -le16_to_cpu(split_rec->e_leaf_clusters));
3489 * Do the final bits of extent record insertion at the target leaf
3490 * list. If this leaf is part of an allocation tree, it is assumed
3491 * that the tree above has been prepared.
3493 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3494 struct ocfs2_extent_list *el,
3495 struct ocfs2_insert_type *insert,
3496 struct inode *inode)
3498 int i = insert->ins_contig_index;
3500 struct ocfs2_extent_rec *rec;
3502 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3504 if (insert->ins_split != SPLIT_NONE) {
3505 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3507 rec = &el->l_recs[i];
3508 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3514 * Contiguous insert - either left or right.
3516 if (insert->ins_contig != CONTIG_NONE) {
3517 rec = &el->l_recs[i];
3518 if (insert->ins_contig == CONTIG_LEFT) {
3519 rec->e_blkno = insert_rec->e_blkno;
3520 rec->e_cpos = insert_rec->e_cpos;
3522 le16_add_cpu(&rec->e_leaf_clusters,
3523 le16_to_cpu(insert_rec->e_leaf_clusters));
3528 * Handle insert into an empty leaf.
3530 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3531 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3532 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3533 el->l_recs[0] = *insert_rec;
3534 el->l_next_free_rec = cpu_to_le16(1);
3541 if (insert->ins_appending == APPEND_TAIL) {
3542 i = le16_to_cpu(el->l_next_free_rec) - 1;
3543 rec = &el->l_recs[i];
3544 range = le32_to_cpu(rec->e_cpos)
3545 + le16_to_cpu(rec->e_leaf_clusters);
3546 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3548 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3549 le16_to_cpu(el->l_count),
3550 "inode %lu, depth %u, count %u, next free %u, "
3551 "rec.cpos %u, rec.clusters %u, "
3552 "insert.cpos %u, insert.clusters %u\n",
3554 le16_to_cpu(el->l_tree_depth),
3555 le16_to_cpu(el->l_count),
3556 le16_to_cpu(el->l_next_free_rec),
3557 le32_to_cpu(el->l_recs[i].e_cpos),
3558 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3559 le32_to_cpu(insert_rec->e_cpos),
3560 le16_to_cpu(insert_rec->e_leaf_clusters));
3562 el->l_recs[i] = *insert_rec;
3563 le16_add_cpu(&el->l_next_free_rec, 1);
3569 * Ok, we have to rotate.
3571 * At this point, it is safe to assume that inserting into an
3572 * empty leaf and appending to a leaf have both been handled
3575 * This leaf needs to have space, either by the empty 1st
3576 * extent record, or by virtue of an l_next_rec < l_count.
3578 ocfs2_rotate_leaf(el, insert_rec);
3581 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3583 struct ocfs2_path *path,
3584 struct ocfs2_extent_rec *insert_rec)
3586 int ret, i, next_free;
3587 struct buffer_head *bh;
3588 struct ocfs2_extent_list *el;
3589 struct ocfs2_extent_rec *rec;
3592 * Update everything except the leaf block.
3594 for (i = 0; i < path->p_tree_depth; i++) {
3595 bh = path->p_node[i].bh;
3596 el = path->p_node[i].el;
3598 next_free = le16_to_cpu(el->l_next_free_rec);
3599 if (next_free == 0) {
3600 ocfs2_error(inode->i_sb,
3601 "Dinode %llu has a bad extent list",
3602 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3607 rec = &el->l_recs[next_free - 1];
3609 rec->e_int_clusters = insert_rec->e_cpos;
3610 le32_add_cpu(&rec->e_int_clusters,
3611 le16_to_cpu(insert_rec->e_leaf_clusters));
3612 le32_add_cpu(&rec->e_int_clusters,
3613 -le32_to_cpu(rec->e_cpos));
3615 ret = ocfs2_journal_dirty(handle, bh);
3622 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3623 struct ocfs2_extent_rec *insert_rec,
3624 struct ocfs2_path *right_path,
3625 struct ocfs2_path **ret_left_path)
3628 struct ocfs2_extent_list *el;
3629 struct ocfs2_path *left_path = NULL;
3631 *ret_left_path = NULL;
3634 * This shouldn't happen for non-trees. The extent rec cluster
3635 * count manipulation below only works for interior nodes.
3637 BUG_ON(right_path->p_tree_depth == 0);
3640 * If our appending insert is at the leftmost edge of a leaf,
3641 * then we might need to update the rightmost records of the
3644 el = path_leaf_el(right_path);
3645 next_free = le16_to_cpu(el->l_next_free_rec);
3646 if (next_free == 0 ||
3647 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3650 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3657 mlog(0, "Append may need a left path update. cpos: %u, "
3658 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3662 * No need to worry if the append is already in the
3666 left_path = ocfs2_new_path(path_root_bh(right_path),
3667 path_root_el(right_path));
3674 ret = ocfs2_find_path(inode, left_path, left_cpos);
3681 * ocfs2_insert_path() will pass the left_path to the
3687 ret = ocfs2_journal_access_path(inode, handle, right_path);
3693 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
3695 *ret_left_path = left_path;
3699 ocfs2_free_path(left_path);
3704 static void ocfs2_split_record(struct inode *inode,
3705 struct ocfs2_path *left_path,
3706 struct ocfs2_path *right_path,
3707 struct ocfs2_extent_rec *split_rec,
3708 enum ocfs2_split_type split)
3711 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3712 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3713 struct ocfs2_extent_rec *rec, *tmprec;
3715 right_el = path_leaf_el(right_path);;
3717 left_el = path_leaf_el(left_path);
3720 insert_el = right_el;
3721 index = ocfs2_search_extent_list(el, cpos);
3723 if (index == 0 && left_path) {
3724 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3727 * This typically means that the record
3728 * started in the left path but moved to the
3729 * right as a result of rotation. We either
3730 * move the existing record to the left, or we
3731 * do the later insert there.
3733 * In this case, the left path should always
3734 * exist as the rotate code will have passed
3735 * it back for a post-insert update.
3738 if (split == SPLIT_LEFT) {
3740 * It's a left split. Since we know
3741 * that the rotate code gave us an
3742 * empty extent in the left path, we
3743 * can just do the insert there.
3745 insert_el = left_el;
3748 * Right split - we have to move the
3749 * existing record over to the left
3750 * leaf. The insert will be into the
3751 * newly created empty extent in the
3754 tmprec = &right_el->l_recs[index];
3755 ocfs2_rotate_leaf(left_el, tmprec);
3758 memset(tmprec, 0, sizeof(*tmprec));
3759 index = ocfs2_search_extent_list(left_el, cpos);
3760 BUG_ON(index == -1);
3765 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3767 * Left path is easy - we can just allow the insert to
3771 insert_el = left_el;
3772 index = ocfs2_search_extent_list(el, cpos);
3773 BUG_ON(index == -1);
3776 rec = &el->l_recs[index];
3777 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3778 ocfs2_rotate_leaf(insert_el, split_rec);
3782 * This function only does inserts on an allocation b-tree. For tree
3783 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3785 * right_path is the path we want to do the actual insert
3786 * in. left_path should only be passed in if we need to update that
3787 * portion of the tree after an edge insert.
3789 static int ocfs2_insert_path(struct inode *inode,
3791 struct ocfs2_path *left_path,
3792 struct ocfs2_path *right_path,
3793 struct ocfs2_extent_rec *insert_rec,
3794 struct ocfs2_insert_type *insert)
3796 int ret, subtree_index;
3797 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
3800 int credits = handle->h_buffer_credits;
3803 * There's a chance that left_path got passed back to
3804 * us without being accounted for in the
3805 * journal. Extend our transaction here to be sure we
3806 * can change those blocks.
3808 credits += left_path->p_tree_depth;
3810 ret = ocfs2_extend_trans(handle, credits);
3816 ret = ocfs2_journal_access_path(inode, handle, left_path);
3824 * Pass both paths to the journal. The majority of inserts
3825 * will be touching all components anyway.
3827 ret = ocfs2_journal_access_path(inode, handle, right_path);
3833 if (insert->ins_split != SPLIT_NONE) {
3835 * We could call ocfs2_insert_at_leaf() for some types
3836 * of splits, but it's easier to just let one separate
3837 * function sort it all out.
3839 ocfs2_split_record(inode, left_path, right_path,
3840 insert_rec, insert->ins_split);
3843 * Split might have modified either leaf and we don't
3844 * have a guarantee that the later edge insert will
3845 * dirty this for us.
3848 ret = ocfs2_journal_dirty(handle,
3849 path_leaf_bh(left_path));
3853 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3856 ret = ocfs2_journal_dirty(handle, leaf_bh);
3862 * The rotate code has indicated that we need to fix
3863 * up portions of the tree after the insert.
3865 * XXX: Should we extend the transaction here?
3867 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3869 ocfs2_complete_edge_insert(inode, handle, left_path,
3870 right_path, subtree_index);
3878 static int ocfs2_do_insert_extent(struct inode *inode,
3880 struct ocfs2_extent_tree *et,
3881 struct ocfs2_extent_rec *insert_rec,
3882 struct ocfs2_insert_type *type)
3884 int ret, rotate = 0;
3886 struct ocfs2_path *right_path = NULL;
3887 struct ocfs2_path *left_path = NULL;
3888 struct ocfs2_extent_list *el;
3892 ret = ocfs2_journal_access(handle, inode, et->root_bh,
3893 OCFS2_JOURNAL_ACCESS_WRITE);
3899 if (le16_to_cpu(el->l_tree_depth) == 0) {
3900 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3901 goto out_update_clusters;
3904 right_path = ocfs2_new_path(et->root_bh, et->root_el);
3912 * Determine the path to start with. Rotations need the
3913 * rightmost path, everything else can go directly to the
3916 cpos = le32_to_cpu(insert_rec->e_cpos);
3917 if (type->ins_appending == APPEND_NONE &&
3918 type->ins_contig == CONTIG_NONE) {
3923 ret = ocfs2_find_path(inode, right_path, cpos);
3930 * Rotations and appends need special treatment - they modify
3931 * parts of the tree's above them.
3933 * Both might pass back a path immediate to the left of the
3934 * one being inserted to. This will be cause
3935 * ocfs2_insert_path() to modify the rightmost records of
3936 * left_path to account for an edge insert.
3938 * XXX: When modifying this code, keep in mind that an insert
3939 * can wind up skipping both of these two special cases...
3942 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
3943 le32_to_cpu(insert_rec->e_cpos),
3944 right_path, &left_path);
3951 * ocfs2_rotate_tree_right() might have extended the
3952 * transaction without re-journaling our tree root.
3954 ret = ocfs2_journal_access(handle, inode, et->root_bh,
3955 OCFS2_JOURNAL_ACCESS_WRITE);
3960 } else if (type->ins_appending == APPEND_TAIL
3961 && type->ins_contig != CONTIG_LEFT) {
3962 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
3963 right_path, &left_path);
3970 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
3977 out_update_clusters:
3978 if (type->ins_split == SPLIT_NONE)
3979 ocfs2_update_clusters(inode, et,
3980 le16_to_cpu(insert_rec->e_leaf_clusters));
3982 ret = ocfs2_journal_dirty(handle, et->root_bh);
3987 ocfs2_free_path(left_path);
3988 ocfs2_free_path(right_path);
3993 static enum ocfs2_contig_type
3994 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
3995 struct ocfs2_extent_list *el, int index,
3996 struct ocfs2_extent_rec *split_rec)
3999 enum ocfs2_contig_type ret = CONTIG_NONE;
4000 u32 left_cpos, right_cpos;
4001 struct ocfs2_extent_rec *rec = NULL;
4002 struct ocfs2_extent_list *new_el;
4003 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4004 struct buffer_head *bh;
4005 struct ocfs2_extent_block *eb;
4008 rec = &el->l_recs[index - 1];
4009 } else if (path->p_tree_depth > 0) {
4010 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4015 if (left_cpos != 0) {
4016 left_path = ocfs2_new_path(path_root_bh(path),
4017 path_root_el(path));
4021 status = ocfs2_find_path(inode, left_path, left_cpos);
4025 new_el = path_leaf_el(left_path);
4027 if (le16_to_cpu(new_el->l_next_free_rec) !=
4028 le16_to_cpu(new_el->l_count)) {
4029 bh = path_leaf_bh(left_path);
4030 eb = (struct ocfs2_extent_block *)bh->b_data;
4031 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4035 rec = &new_el->l_recs[
4036 le16_to_cpu(new_el->l_next_free_rec) - 1];
4041 * We're careful to check for an empty extent record here -
4042 * the merge code will know what to do if it sees one.
4045 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4046 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4049 ret = ocfs2_extent_contig(inode, rec, split_rec);
4054 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4055 rec = &el->l_recs[index + 1];
4056 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4057 path->p_tree_depth > 0) {
4058 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4063 if (right_cpos == 0)
4066 right_path = ocfs2_new_path(path_root_bh(path),
4067 path_root_el(path));
4071 status = ocfs2_find_path(inode, right_path, right_cpos);
4075 new_el = path_leaf_el(right_path);
4076 rec = &new_el->l_recs[0];
4077 if (ocfs2_is_empty_extent(rec)) {
4078 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4079 bh = path_leaf_bh(right_path);
4080 eb = (struct ocfs2_extent_block *)bh->b_data;
4081 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4085 rec = &new_el->l_recs[1];
4090 enum ocfs2_contig_type contig_type;
4092 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4094 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4095 ret = CONTIG_LEFTRIGHT;
4096 else if (ret == CONTIG_NONE)
4102 ocfs2_free_path(left_path);
4104 ocfs2_free_path(right_path);
4109 static void ocfs2_figure_contig_type(struct inode *inode,
4110 struct ocfs2_insert_type *insert,
4111 struct ocfs2_extent_list *el,
4112 struct ocfs2_extent_rec *insert_rec)
4115 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4117 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4119 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4120 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4122 if (contig_type != CONTIG_NONE) {
4123 insert->ins_contig_index = i;
4127 insert->ins_contig = contig_type;
4131 * This should only be called against the righmost leaf extent list.
4133 * ocfs2_figure_appending_type() will figure out whether we'll have to
4134 * insert at the tail of the rightmost leaf.
4136 * This should also work against the root extent list for tree's with 0
4137 * depth. If we consider the root extent list to be the rightmost leaf node
4138 * then the logic here makes sense.
4140 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4141 struct ocfs2_extent_list *el,
4142 struct ocfs2_extent_rec *insert_rec)
4145 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4146 struct ocfs2_extent_rec *rec;
4148 insert->ins_appending = APPEND_NONE;
4150 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4152 if (!el->l_next_free_rec)
4153 goto set_tail_append;
4155 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4156 /* Were all records empty? */
4157 if (le16_to_cpu(el->l_next_free_rec) == 1)
4158 goto set_tail_append;
4161 i = le16_to_cpu(el->l_next_free_rec) - 1;
4162 rec = &el->l_recs[i];
4165 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4166 goto set_tail_append;
4171 insert->ins_appending = APPEND_TAIL;
4175 * Helper function called at the begining of an insert.
4177 * This computes a few things that are commonly used in the process of
4178 * inserting into the btree:
4179 * - Whether the new extent is contiguous with an existing one.
4180 * - The current tree depth.
4181 * - Whether the insert is an appending one.
4182 * - The total # of free records in the tree.
4184 * All of the information is stored on the ocfs2_insert_type
4187 static int ocfs2_figure_insert_type(struct inode *inode,
4188 struct ocfs2_extent_tree *et,
4189 struct buffer_head **last_eb_bh,
4190 struct ocfs2_extent_rec *insert_rec,
4192 struct ocfs2_insert_type *insert)
4195 struct ocfs2_extent_block *eb;
4196 struct ocfs2_extent_list *el;
4197 struct ocfs2_path *path = NULL;
4198 struct buffer_head *bh = NULL;
4200 insert->ins_split = SPLIT_NONE;
4203 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4205 if (el->l_tree_depth) {
4207 * If we have tree depth, we read in the
4208 * rightmost extent block ahead of time as
4209 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4210 * may want it later.
4212 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4213 ocfs2_get_last_eb_blk(et), &bh,
4214 OCFS2_BH_CACHED, inode);
4219 eb = (struct ocfs2_extent_block *) bh->b_data;
4224 * Unless we have a contiguous insert, we'll need to know if
4225 * there is room left in our allocation tree for another
4228 * XXX: This test is simplistic, we can search for empty
4229 * extent records too.
4231 *free_records = le16_to_cpu(el->l_count) -
4232 le16_to_cpu(el->l_next_free_rec);
4234 if (!insert->ins_tree_depth) {
4235 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
4236 ocfs2_figure_appending_type(insert, el, insert_rec);
4240 path = ocfs2_new_path(et->root_bh, et->root_el);
4248 * In the case that we're inserting past what the tree
4249 * currently accounts for, ocfs2_find_path() will return for
4250 * us the rightmost tree path. This is accounted for below in
4251 * the appending code.
4253 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4259 el = path_leaf_el(path);
4262 * Now that we have the path, there's two things we want to determine:
4263 * 1) Contiguousness (also set contig_index if this is so)
4265 * 2) Are we doing an append? We can trivially break this up
4266 * into two types of appends: simple record append, or a
4267 * rotate inside the tail leaf.
4269 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
4272 * The insert code isn't quite ready to deal with all cases of
4273 * left contiguousness. Specifically, if it's an insert into
4274 * the 1st record in a leaf, it will require the adjustment of
4275 * cluster count on the last record of the path directly to it's
4276 * left. For now, just catch that case and fool the layers
4277 * above us. This works just fine for tree_depth == 0, which
4278 * is why we allow that above.
4280 if (insert->ins_contig == CONTIG_LEFT &&
4281 insert->ins_contig_index == 0)
4282 insert->ins_contig = CONTIG_NONE;
4285 * Ok, so we can simply compare against last_eb to figure out
4286 * whether the path doesn't exist. This will only happen in
4287 * the case that we're doing a tail append, so maybe we can
4288 * take advantage of that information somehow.
4290 if (ocfs2_get_last_eb_blk(et) ==
4291 path_leaf_bh(path)->b_blocknr) {
4293 * Ok, ocfs2_find_path() returned us the rightmost
4294 * tree path. This might be an appending insert. There are
4296 * 1) We're doing a true append at the tail:
4297 * -This might even be off the end of the leaf
4298 * 2) We're "appending" by rotating in the tail
4300 ocfs2_figure_appending_type(insert, el, insert_rec);
4304 ocfs2_free_path(path);
4314 * Insert an extent into an inode btree.
4316 * The caller needs to update fe->i_clusters
4318 static int ocfs2_insert_extent(struct ocfs2_super *osb,
4320 struct inode *inode,
4321 struct buffer_head *root_bh,
4326 struct ocfs2_alloc_context *meta_ac,
4327 struct ocfs2_extent_tree *et)
4330 int uninitialized_var(free_records);
4331 struct buffer_head *last_eb_bh = NULL;
4332 struct ocfs2_insert_type insert = {0, };
4333 struct ocfs2_extent_rec rec;
4335 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
4337 mlog(0, "add %u clusters at position %u to inode %llu\n",
4338 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4340 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
4341 (OCFS2_I(inode)->ip_clusters != cpos),
4342 "Device %s, asking for sparse allocation: inode %llu, "
4343 "cpos %u, clusters %u\n",
4345 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
4346 OCFS2_I(inode)->ip_clusters);
4348 memset(&rec, 0, sizeof(rec));
4349 rec.e_cpos = cpu_to_le32(cpos);
4350 rec.e_blkno = cpu_to_le64(start_blk);
4351 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4352 rec.e_flags = flags;
4354 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4355 &free_records, &insert);
4361 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4362 "Insert.contig_index: %d, Insert.free_records: %d, "
4363 "Insert.tree_depth: %d\n",
4364 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4365 free_records, insert.ins_tree_depth);
4367 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4368 status = ocfs2_grow_tree(inode, handle, et,
4369 &insert.ins_tree_depth, &last_eb_bh,
4377 /* Finally, we can add clusters. This might rotate the tree for us. */
4378 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4381 else if (et->type == OCFS2_DINODE_EXTENT)
4382 ocfs2_extent_map_insert_rec(inode, &rec);
4392 int ocfs2_dinode_insert_extent(struct ocfs2_super *osb,
4394 struct inode *inode,
4395 struct buffer_head *root_bh,
4400 struct ocfs2_alloc_context *meta_ac)
4403 struct ocfs2_extent_tree *et = NULL;
4405 et = ocfs2_new_extent_tree(root_bh, OCFS2_DINODE_EXTENT, NULL);
4412 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4413 cpos, start_blk, new_clusters,
4414 flags, meta_ac, et);
4417 ocfs2_free_extent_tree(et);
4422 int ocfs2_xattr_value_insert_extent(struct ocfs2_super *osb,
4424 struct inode *inode,
4425 struct buffer_head *root_bh,
4430 struct ocfs2_alloc_context *meta_ac,
4434 struct ocfs2_extent_tree *et = NULL;
4436 et = ocfs2_new_extent_tree(root_bh, OCFS2_XATTR_VALUE_EXTENT, private);
4443 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4444 cpos, start_blk, new_clusters,
4445 flags, meta_ac, et);
4448 ocfs2_free_extent_tree(et);
4453 int ocfs2_xattr_tree_insert_extent(struct ocfs2_super *osb,
4455 struct inode *inode,
4456 struct buffer_head *root_bh,
4461 struct ocfs2_alloc_context *meta_ac)
4464 struct ocfs2_extent_tree *et = NULL;
4466 et = ocfs2_new_extent_tree(root_bh, OCFS2_XATTR_TREE_EXTENT, NULL);
4473 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4474 cpos, start_blk, new_clusters,
4475 flags, meta_ac, et);
4478 ocfs2_free_extent_tree(et);
4484 * Allcate and add clusters into the extent b-tree.
4485 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4486 * The extent b-tree's root is root_el and it should be in root_bh, and
4487 * it is not limited to the file storage. Any extent tree can use this
4488 * function if it implements the proper ocfs2_extent_tree.
4490 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4491 struct inode *inode,
4492 u32 *logical_offset,
4493 u32 clusters_to_add,
4495 struct buffer_head *root_bh,
4496 struct ocfs2_extent_list *root_el,
4498 struct ocfs2_alloc_context *data_ac,
4499 struct ocfs2_alloc_context *meta_ac,
4500 enum ocfs2_alloc_restarted *reason_ret,
4501 enum ocfs2_extent_tree_type type,
4506 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4507 u32 bit_off, num_bits;
4511 BUG_ON(!clusters_to_add);
4514 flags = OCFS2_EXT_UNWRITTEN;
4516 free_extents = ocfs2_num_free_extents(osb, inode, root_bh, type,
4518 if (free_extents < 0) {
4519 status = free_extents;
4524 /* there are two cases which could cause us to EAGAIN in the
4525 * we-need-more-metadata case:
4526 * 1) we haven't reserved *any*
4527 * 2) we are so fragmented, we've needed to add metadata too
4529 if (!free_extents && !meta_ac) {
4530 mlog(0, "we haven't reserved any metadata!\n");
4532 reason = RESTART_META;
4534 } else if ((!free_extents)
4535 && (ocfs2_alloc_context_bits_left(meta_ac)
4536 < ocfs2_extend_meta_needed(root_el))) {
4537 mlog(0, "filesystem is really fragmented...\n");
4539 reason = RESTART_META;
4543 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4544 clusters_to_add, &bit_off, &num_bits);
4546 if (status != -ENOSPC)
4551 BUG_ON(num_bits > clusters_to_add);
4553 /* reserve our write early -- insert_extent may update the inode */
4554 status = ocfs2_journal_access(handle, inode, root_bh,
4555 OCFS2_JOURNAL_ACCESS_WRITE);
4561 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4562 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4563 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4564 if (type == OCFS2_DINODE_EXTENT)
4565 status = ocfs2_dinode_insert_extent(osb, handle, inode, root_bh,
4566 *logical_offset, block,
4567 num_bits, flags, meta_ac);
4568 else if (type == OCFS2_XATTR_TREE_EXTENT)
4569 status = ocfs2_xattr_tree_insert_extent(osb, handle,
4572 block, num_bits, flags,
4575 status = ocfs2_xattr_value_insert_extent(osb, handle,
4578 block, num_bits, flags,
4585 status = ocfs2_journal_dirty(handle, root_bh);
4591 clusters_to_add -= num_bits;
4592 *logical_offset += num_bits;
4594 if (clusters_to_add) {
4595 mlog(0, "need to alloc once more, wanted = %u\n",
4598 reason = RESTART_TRANS;
4604 *reason_ret = reason;
4608 static void ocfs2_make_right_split_rec(struct super_block *sb,
4609 struct ocfs2_extent_rec *split_rec,
4611 struct ocfs2_extent_rec *rec)
4613 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4614 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4616 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4618 split_rec->e_cpos = cpu_to_le32(cpos);
4619 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4621 split_rec->e_blkno = rec->e_blkno;
4622 le64_add_cpu(&split_rec->e_blkno,
4623 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4625 split_rec->e_flags = rec->e_flags;
4628 static int ocfs2_split_and_insert(struct inode *inode,
4630 struct ocfs2_path *path,
4631 struct ocfs2_extent_tree *et,
4632 struct buffer_head **last_eb_bh,
4634 struct ocfs2_extent_rec *orig_split_rec,
4635 struct ocfs2_alloc_context *meta_ac)
4638 unsigned int insert_range, rec_range, do_leftright = 0;
4639 struct ocfs2_extent_rec tmprec;
4640 struct ocfs2_extent_list *rightmost_el;
4641 struct ocfs2_extent_rec rec;
4642 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4643 struct ocfs2_insert_type insert;
4644 struct ocfs2_extent_block *eb;
4648 * Store a copy of the record on the stack - it might move
4649 * around as the tree is manipulated below.
4651 rec = path_leaf_el(path)->l_recs[split_index];
4653 rightmost_el = et->root_el;
4655 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4657 BUG_ON(!(*last_eb_bh));
4658 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4659 rightmost_el = &eb->h_list;
4662 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4663 le16_to_cpu(rightmost_el->l_count)) {
4664 ret = ocfs2_grow_tree(inode, handle, et,
4665 &depth, last_eb_bh, meta_ac);
4672 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4673 insert.ins_appending = APPEND_NONE;
4674 insert.ins_contig = CONTIG_NONE;
4675 insert.ins_tree_depth = depth;
4677 insert_range = le32_to_cpu(split_rec.e_cpos) +
4678 le16_to_cpu(split_rec.e_leaf_clusters);
4679 rec_range = le32_to_cpu(rec.e_cpos) +
4680 le16_to_cpu(rec.e_leaf_clusters);
4682 if (split_rec.e_cpos == rec.e_cpos) {
4683 insert.ins_split = SPLIT_LEFT;
4684 } else if (insert_range == rec_range) {
4685 insert.ins_split = SPLIT_RIGHT;
4688 * Left/right split. We fake this as a right split
4689 * first and then make a second pass as a left split.
4691 insert.ins_split = SPLIT_RIGHT;
4693 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4698 BUG_ON(do_leftright);
4702 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4708 if (do_leftright == 1) {
4710 struct ocfs2_extent_list *el;
4713 split_rec = *orig_split_rec;
4715 ocfs2_reinit_path(path, 1);
4717 cpos = le32_to_cpu(split_rec.e_cpos);
4718 ret = ocfs2_find_path(inode, path, cpos);
4724 el = path_leaf_el(path);
4725 split_index = ocfs2_search_extent_list(el, cpos);
4734 * Mark part or all of the extent record at split_index in the leaf
4735 * pointed to by path as written. This removes the unwritten
4738 * Care is taken to handle contiguousness so as to not grow the tree.
4740 * meta_ac is not strictly necessary - we only truly need it if growth
4741 * of the tree is required. All other cases will degrade into a less
4742 * optimal tree layout.
4744 * last_eb_bh should be the rightmost leaf block for any extent
4745 * btree. Since a split may grow the tree or a merge might shrink it,
4746 * the caller cannot trust the contents of that buffer after this call.
4748 * This code is optimized for readability - several passes might be
4749 * made over certain portions of the tree. All of those blocks will
4750 * have been brought into cache (and pinned via the journal), so the
4751 * extra overhead is not expressed in terms of disk reads.
4753 static int __ocfs2_mark_extent_written(struct inode *inode,
4754 struct ocfs2_extent_tree *et,
4756 struct ocfs2_path *path,
4758 struct ocfs2_extent_rec *split_rec,
4759 struct ocfs2_alloc_context *meta_ac,
4760 struct ocfs2_cached_dealloc_ctxt *dealloc)
4763 struct ocfs2_extent_list *el = path_leaf_el(path);
4764 struct buffer_head *last_eb_bh = NULL;
4765 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4766 struct ocfs2_merge_ctxt ctxt;
4767 struct ocfs2_extent_list *rightmost_el;
4769 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
4775 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4776 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4777 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4783 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
4788 * The core merge / split code wants to know how much room is
4789 * left in this inodes allocation tree, so we pass the
4790 * rightmost extent list.
4792 if (path->p_tree_depth) {
4793 struct ocfs2_extent_block *eb;
4795 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4796 ocfs2_get_last_eb_blk(et),
4797 &last_eb_bh, OCFS2_BH_CACHED, inode);
4803 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4804 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4805 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4810 rightmost_el = &eb->h_list;
4812 rightmost_el = path_root_el(path);
4814 if (rec->e_cpos == split_rec->e_cpos &&
4815 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4816 ctxt.c_split_covers_rec = 1;
4818 ctxt.c_split_covers_rec = 0;
4820 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4822 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4823 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4824 ctxt.c_split_covers_rec);
4826 if (ctxt.c_contig_type == CONTIG_NONE) {
4827 if (ctxt.c_split_covers_rec)
4828 el->l_recs[split_index] = *split_rec;
4830 ret = ocfs2_split_and_insert(inode, handle, path, et,
4831 &last_eb_bh, split_index,
4832 split_rec, meta_ac);
4836 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4837 split_index, split_rec,
4838 dealloc, &ctxt, et);
4849 * Mark the already-existing extent at cpos as written for len clusters.
4851 * If the existing extent is larger than the request, initiate a
4852 * split. An attempt will be made at merging with adjacent extents.
4854 * The caller is responsible for passing down meta_ac if we'll need it.
4856 int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh,
4857 handle_t *handle, u32 cpos, u32 len, u32 phys,
4858 struct ocfs2_alloc_context *meta_ac,
4859 struct ocfs2_cached_dealloc_ctxt *dealloc,
4860 enum ocfs2_extent_tree_type et_type,
4864 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4865 struct ocfs2_extent_rec split_rec;
4866 struct ocfs2_path *left_path = NULL;
4867 struct ocfs2_extent_list *el;
4868 struct ocfs2_extent_tree *et = NULL;
4870 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4871 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4873 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4874 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4875 "that are being written to, but the feature bit "
4876 "is not set in the super block.",
4877 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4882 et = ocfs2_new_extent_tree(root_bh, et_type, private);
4890 * XXX: This should be fixed up so that we just re-insert the
4891 * next extent records.
4893 if (et_type == OCFS2_DINODE_EXTENT)
4894 ocfs2_extent_map_trunc(inode, 0);
4896 left_path = ocfs2_new_path(et->root_bh, et->root_el);
4903 ret = ocfs2_find_path(inode, left_path, cpos);
4908 el = path_leaf_el(left_path);
4910 index = ocfs2_search_extent_list(el, cpos);
4911 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4912 ocfs2_error(inode->i_sb,
4913 "Inode %llu has an extent at cpos %u which can no "
4914 "longer be found.\n",
4915 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4920 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4921 split_rec.e_cpos = cpu_to_le32(cpos);
4922 split_rec.e_leaf_clusters = cpu_to_le16(len);
4923 split_rec.e_blkno = cpu_to_le64(start_blkno);
4924 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4925 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4927 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
4928 index, &split_rec, meta_ac,
4934 ocfs2_free_path(left_path);
4936 ocfs2_free_extent_tree(et);
4940 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
4941 handle_t *handle, struct ocfs2_path *path,
4942 int index, u32 new_range,
4943 struct ocfs2_alloc_context *meta_ac)
4945 int ret, depth, credits = handle->h_buffer_credits;
4946 struct buffer_head *last_eb_bh = NULL;
4947 struct ocfs2_extent_block *eb;
4948 struct ocfs2_extent_list *rightmost_el, *el;
4949 struct ocfs2_extent_rec split_rec;
4950 struct ocfs2_extent_rec *rec;
4951 struct ocfs2_insert_type insert;
4954 * Setup the record to split before we grow the tree.
4956 el = path_leaf_el(path);
4957 rec = &el->l_recs[index];
4958 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4960 depth = path->p_tree_depth;
4962 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4963 ocfs2_get_last_eb_blk(et),
4964 &last_eb_bh, OCFS2_BH_CACHED, inode);
4970 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4971 rightmost_el = &eb->h_list;
4973 rightmost_el = path_leaf_el(path);
4975 credits += path->p_tree_depth +
4976 ocfs2_extend_meta_needed(et->root_el);
4977 ret = ocfs2_extend_trans(handle, credits);
4983 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4984 le16_to_cpu(rightmost_el->l_count)) {
4985 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
4993 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4994 insert.ins_appending = APPEND_NONE;
4995 insert.ins_contig = CONTIG_NONE;
4996 insert.ins_split = SPLIT_RIGHT;
4997 insert.ins_tree_depth = depth;
4999 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
5008 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5009 struct ocfs2_path *path, int index,
5010 struct ocfs2_cached_dealloc_ctxt *dealloc,
5012 struct ocfs2_extent_tree *et)
5015 u32 left_cpos, rec_range, trunc_range;
5016 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5017 struct super_block *sb = inode->i_sb;
5018 struct ocfs2_path *left_path = NULL;
5019 struct ocfs2_extent_list *el = path_leaf_el(path);
5020 struct ocfs2_extent_rec *rec;
5021 struct ocfs2_extent_block *eb;
5023 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5024 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5033 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5034 path->p_tree_depth) {
5036 * Check whether this is the rightmost tree record. If
5037 * we remove all of this record or part of its right
5038 * edge then an update of the record lengths above it
5041 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5042 if (eb->h_next_leaf_blk == 0)
5043 is_rightmost_tree_rec = 1;
5046 rec = &el->l_recs[index];
5047 if (index == 0 && path->p_tree_depth &&
5048 le32_to_cpu(rec->e_cpos) == cpos) {
5050 * Changing the leftmost offset (via partial or whole
5051 * record truncate) of an interior (or rightmost) path
5052 * means we have to update the subtree that is formed
5053 * by this leaf and the one to it's left.
5055 * There are two cases we can skip:
5056 * 1) Path is the leftmost one in our inode tree.
5057 * 2) The leaf is rightmost and will be empty after
5058 * we remove the extent record - the rotate code
5059 * knows how to update the newly formed edge.
5062 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5069 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5070 left_path = ocfs2_new_path(path_root_bh(path),
5071 path_root_el(path));
5078 ret = ocfs2_find_path(inode, left_path, left_cpos);
5086 ret = ocfs2_extend_rotate_transaction(handle, 0,
5087 handle->h_buffer_credits,
5094 ret = ocfs2_journal_access_path(inode, handle, path);
5100 ret = ocfs2_journal_access_path(inode, handle, left_path);
5106 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5107 trunc_range = cpos + len;
5109 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5112 memset(rec, 0, sizeof(*rec));
5113 ocfs2_cleanup_merge(el, index);
5116 next_free = le16_to_cpu(el->l_next_free_rec);
5117 if (is_rightmost_tree_rec && next_free > 1) {
5119 * We skip the edge update if this path will
5120 * be deleted by the rotate code.
5122 rec = &el->l_recs[next_free - 1];
5123 ocfs2_adjust_rightmost_records(inode, handle, path,
5126 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5127 /* Remove leftmost portion of the record. */
5128 le32_add_cpu(&rec->e_cpos, len);
5129 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5130 le16_add_cpu(&rec->e_leaf_clusters, -len);
5131 } else if (rec_range == trunc_range) {
5132 /* Remove rightmost portion of the record */
5133 le16_add_cpu(&rec->e_leaf_clusters, -len);
5134 if (is_rightmost_tree_rec)
5135 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5137 /* Caller should have trapped this. */
5138 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5139 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5140 le32_to_cpu(rec->e_cpos),
5141 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5148 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5149 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5153 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5155 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5162 ocfs2_free_path(left_path);
5166 int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
5167 u32 cpos, u32 len, handle_t *handle,
5168 struct ocfs2_alloc_context *meta_ac,
5169 struct ocfs2_cached_dealloc_ctxt *dealloc,
5170 enum ocfs2_extent_tree_type et_type,
5174 u32 rec_range, trunc_range;
5175 struct ocfs2_extent_rec *rec;
5176 struct ocfs2_extent_list *el;
5177 struct ocfs2_path *path = NULL;
5178 struct ocfs2_extent_tree *et = NULL;
5180 et = ocfs2_new_extent_tree(root_bh, et_type, private);
5187 ocfs2_extent_map_trunc(inode, 0);
5189 path = ocfs2_new_path(et->root_bh, et->root_el);
5196 ret = ocfs2_find_path(inode, path, cpos);
5202 el = path_leaf_el(path);
5203 index = ocfs2_search_extent_list(el, cpos);
5204 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5205 ocfs2_error(inode->i_sb,
5206 "Inode %llu has an extent at cpos %u which can no "
5207 "longer be found.\n",
5208 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5214 * We have 3 cases of extent removal:
5215 * 1) Range covers the entire extent rec
5216 * 2) Range begins or ends on one edge of the extent rec
5217 * 3) Range is in the middle of the extent rec (no shared edges)
5219 * For case 1 we remove the extent rec and left rotate to
5222 * For case 2 we just shrink the existing extent rec, with a
5223 * tree update if the shrinking edge is also the edge of an
5226 * For case 3 we do a right split to turn the extent rec into
5227 * something case 2 can handle.
5229 rec = &el->l_recs[index];
5230 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5231 trunc_range = cpos + len;
5233 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5235 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5236 "(cpos %u, len %u)\n",
5237 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5238 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5240 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5241 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5248 ret = ocfs2_split_tree(inode, et, handle, path, index,
5249 trunc_range, meta_ac);
5256 * The split could have manipulated the tree enough to
5257 * move the record location, so we have to look for it again.
5259 ocfs2_reinit_path(path, 1);
5261 ret = ocfs2_find_path(inode, path, cpos);
5267 el = path_leaf_el(path);
5268 index = ocfs2_search_extent_list(el, cpos);
5269 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5270 ocfs2_error(inode->i_sb,
5271 "Inode %llu: split at cpos %u lost record.",
5272 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5279 * Double check our values here. If anything is fishy,
5280 * it's easier to catch it at the top level.
5282 rec = &el->l_recs[index];
5283 rec_range = le32_to_cpu(rec->e_cpos) +
5284 ocfs2_rec_clusters(el, rec);
5285 if (rec_range != trunc_range) {
5286 ocfs2_error(inode->i_sb,
5287 "Inode %llu: error after split at cpos %u"
5288 "trunc len %u, existing record is (%u,%u)",
5289 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5290 cpos, len, le32_to_cpu(rec->e_cpos),
5291 ocfs2_rec_clusters(el, rec));
5296 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5305 ocfs2_free_path(path);
5307 ocfs2_free_extent_tree(et);
5311 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5313 struct buffer_head *tl_bh = osb->osb_tl_bh;
5314 struct ocfs2_dinode *di;
5315 struct ocfs2_truncate_log *tl;
5317 di = (struct ocfs2_dinode *) tl_bh->b_data;
5318 tl = &di->id2.i_dealloc;
5320 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5321 "slot %d, invalid truncate log parameters: used = "
5322 "%u, count = %u\n", osb->slot_num,
5323 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5324 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5327 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5328 unsigned int new_start)
5330 unsigned int tail_index;
5331 unsigned int current_tail;
5333 /* No records, nothing to coalesce */
5334 if (!le16_to_cpu(tl->tl_used))
5337 tail_index = le16_to_cpu(tl->tl_used) - 1;
5338 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5339 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5341 return current_tail == new_start;
5344 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5347 unsigned int num_clusters)
5350 unsigned int start_cluster, tl_count;
5351 struct inode *tl_inode = osb->osb_tl_inode;
5352 struct buffer_head *tl_bh = osb->osb_tl_bh;
5353 struct ocfs2_dinode *di;
5354 struct ocfs2_truncate_log *tl;
5356 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5357 (unsigned long long)start_blk, num_clusters);
5359 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5361 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5363 di = (struct ocfs2_dinode *) tl_bh->b_data;
5364 tl = &di->id2.i_dealloc;
5365 if (!OCFS2_IS_VALID_DINODE(di)) {
5366 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5371 tl_count = le16_to_cpu(tl->tl_count);
5372 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5374 "Truncate record count on #%llu invalid "
5375 "wanted %u, actual %u\n",
5376 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5377 ocfs2_truncate_recs_per_inode(osb->sb),
5378 le16_to_cpu(tl->tl_count));
5380 /* Caller should have known to flush before calling us. */
5381 index = le16_to_cpu(tl->tl_used);
5382 if (index >= tl_count) {
5388 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5389 OCFS2_JOURNAL_ACCESS_WRITE);
5395 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5396 "%llu (index = %d)\n", num_clusters, start_cluster,
5397 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5399 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5401 * Move index back to the record we are coalescing with.
5402 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5406 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5407 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5408 index, le32_to_cpu(tl->tl_recs[index].t_start),
5411 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5412 tl->tl_used = cpu_to_le16(index + 1);
5414 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5416 status = ocfs2_journal_dirty(handle, tl_bh);
5427 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5429 struct inode *data_alloc_inode,
5430 struct buffer_head *data_alloc_bh)
5434 unsigned int num_clusters;
5436 struct ocfs2_truncate_rec rec;
5437 struct ocfs2_dinode *di;
5438 struct ocfs2_truncate_log *tl;
5439 struct inode *tl_inode = osb->osb_tl_inode;
5440 struct buffer_head *tl_bh = osb->osb_tl_bh;
5444 di = (struct ocfs2_dinode *) tl_bh->b_data;
5445 tl = &di->id2.i_dealloc;
5446 i = le16_to_cpu(tl->tl_used) - 1;
5448 /* Caller has given us at least enough credits to
5449 * update the truncate log dinode */
5450 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5451 OCFS2_JOURNAL_ACCESS_WRITE);
5457 tl->tl_used = cpu_to_le16(i);
5459 status = ocfs2_journal_dirty(handle, tl_bh);
5465 /* TODO: Perhaps we can calculate the bulk of the
5466 * credits up front rather than extending like
5468 status = ocfs2_extend_trans(handle,
5469 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5475 rec = tl->tl_recs[i];
5476 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5477 le32_to_cpu(rec.t_start));
5478 num_clusters = le32_to_cpu(rec.t_clusters);
5480 /* if start_blk is not set, we ignore the record as
5483 mlog(0, "free record %d, start = %u, clusters = %u\n",
5484 i, le32_to_cpu(rec.t_start), num_clusters);
5486 status = ocfs2_free_clusters(handle, data_alloc_inode,
5487 data_alloc_bh, start_blk,
5502 /* Expects you to already be holding tl_inode->i_mutex */
5503 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5506 unsigned int num_to_flush;
5508 struct inode *tl_inode = osb->osb_tl_inode;
5509 struct inode *data_alloc_inode = NULL;
5510 struct buffer_head *tl_bh = osb->osb_tl_bh;
5511 struct buffer_head *data_alloc_bh = NULL;
5512 struct ocfs2_dinode *di;
5513 struct ocfs2_truncate_log *tl;
5517 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5519 di = (struct ocfs2_dinode *) tl_bh->b_data;
5520 tl = &di->id2.i_dealloc;
5521 if (!OCFS2_IS_VALID_DINODE(di)) {
5522 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5527 num_to_flush = le16_to_cpu(tl->tl_used);
5528 mlog(0, "Flush %u records from truncate log #%llu\n",
5529 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5530 if (!num_to_flush) {
5535 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5536 GLOBAL_BITMAP_SYSTEM_INODE,
5537 OCFS2_INVALID_SLOT);
5538 if (!data_alloc_inode) {
5540 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5544 mutex_lock(&data_alloc_inode->i_mutex);
5546 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5552 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5553 if (IS_ERR(handle)) {
5554 status = PTR_ERR(handle);
5559 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5564 ocfs2_commit_trans(osb, handle);
5567 brelse(data_alloc_bh);
5568 ocfs2_inode_unlock(data_alloc_inode, 1);
5571 mutex_unlock(&data_alloc_inode->i_mutex);
5572 iput(data_alloc_inode);
5579 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5582 struct inode *tl_inode = osb->osb_tl_inode;
5584 mutex_lock(&tl_inode->i_mutex);
5585 status = __ocfs2_flush_truncate_log(osb);
5586 mutex_unlock(&tl_inode->i_mutex);
5591 static void ocfs2_truncate_log_worker(struct work_struct *work)
5594 struct ocfs2_super *osb =
5595 container_of(work, struct ocfs2_super,
5596 osb_truncate_log_wq.work);
5600 status = ocfs2_flush_truncate_log(osb);
5604 ocfs2_init_inode_steal_slot(osb);
5609 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5610 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5613 if (osb->osb_tl_inode) {
5614 /* We want to push off log flushes while truncates are
5617 cancel_delayed_work(&osb->osb_truncate_log_wq);
5619 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5620 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5624 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5626 struct inode **tl_inode,
5627 struct buffer_head **tl_bh)
5630 struct inode *inode = NULL;
5631 struct buffer_head *bh = NULL;
5633 inode = ocfs2_get_system_file_inode(osb,
5634 TRUNCATE_LOG_SYSTEM_INODE,
5638 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5642 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
5643 OCFS2_BH_CACHED, inode);
5657 /* called during the 1st stage of node recovery. we stamp a clean
5658 * truncate log and pass back a copy for processing later. if the
5659 * truncate log does not require processing, a *tl_copy is set to
5661 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5663 struct ocfs2_dinode **tl_copy)
5666 struct inode *tl_inode = NULL;
5667 struct buffer_head *tl_bh = NULL;
5668 struct ocfs2_dinode *di;
5669 struct ocfs2_truncate_log *tl;
5673 mlog(0, "recover truncate log from slot %d\n", slot_num);
5675 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5681 di = (struct ocfs2_dinode *) tl_bh->b_data;
5682 tl = &di->id2.i_dealloc;
5683 if (!OCFS2_IS_VALID_DINODE(di)) {
5684 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
5689 if (le16_to_cpu(tl->tl_used)) {
5690 mlog(0, "We'll have %u logs to recover\n",
5691 le16_to_cpu(tl->tl_used));
5693 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5700 /* Assuming the write-out below goes well, this copy
5701 * will be passed back to recovery for processing. */
5702 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5704 /* All we need to do to clear the truncate log is set
5708 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5721 if (status < 0 && (*tl_copy)) {
5730 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5731 struct ocfs2_dinode *tl_copy)
5735 unsigned int clusters, num_recs, start_cluster;
5738 struct inode *tl_inode = osb->osb_tl_inode;
5739 struct ocfs2_truncate_log *tl;
5743 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5744 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5748 tl = &tl_copy->id2.i_dealloc;
5749 num_recs = le16_to_cpu(tl->tl_used);
5750 mlog(0, "cleanup %u records from %llu\n", num_recs,
5751 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
5753 mutex_lock(&tl_inode->i_mutex);
5754 for(i = 0; i < num_recs; i++) {
5755 if (ocfs2_truncate_log_needs_flush(osb)) {
5756 status = __ocfs2_flush_truncate_log(osb);
5763 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5764 if (IS_ERR(handle)) {
5765 status = PTR_ERR(handle);
5770 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5771 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5772 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5774 status = ocfs2_truncate_log_append(osb, handle,
5775 start_blk, clusters);
5776 ocfs2_commit_trans(osb, handle);
5784 mutex_unlock(&tl_inode->i_mutex);
5790 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5793 struct inode *tl_inode = osb->osb_tl_inode;
5798 cancel_delayed_work(&osb->osb_truncate_log_wq);
5799 flush_workqueue(ocfs2_wq);
5801 status = ocfs2_flush_truncate_log(osb);
5805 brelse(osb->osb_tl_bh);
5806 iput(osb->osb_tl_inode);
5812 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5815 struct inode *tl_inode = NULL;
5816 struct buffer_head *tl_bh = NULL;
5820 status = ocfs2_get_truncate_log_info(osb,
5827 /* ocfs2_truncate_log_shutdown keys on the existence of
5828 * osb->osb_tl_inode so we don't set any of the osb variables
5829 * until we're sure all is well. */
5830 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5831 ocfs2_truncate_log_worker);
5832 osb->osb_tl_bh = tl_bh;
5833 osb->osb_tl_inode = tl_inode;
5840 * Delayed de-allocation of suballocator blocks.
5842 * Some sets of block de-allocations might involve multiple suballocator inodes.
5844 * The locking for this can get extremely complicated, especially when
5845 * the suballocator inodes to delete from aren't known until deep
5846 * within an unrelated codepath.
5848 * ocfs2_extent_block structures are a good example of this - an inode
5849 * btree could have been grown by any number of nodes each allocating
5850 * out of their own suballoc inode.
5852 * These structures allow the delay of block de-allocation until a
5853 * later time, when locking of multiple cluster inodes won't cause
5858 * Describes a single block free from a suballocator
5860 struct ocfs2_cached_block_free {
5861 struct ocfs2_cached_block_free *free_next;
5863 unsigned int free_bit;
5866 struct ocfs2_per_slot_free_list {
5867 struct ocfs2_per_slot_free_list *f_next_suballocator;
5870 struct ocfs2_cached_block_free *f_first;
5873 static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5876 struct ocfs2_cached_block_free *head)
5881 struct inode *inode;
5882 struct buffer_head *di_bh = NULL;
5883 struct ocfs2_cached_block_free *tmp;
5885 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5892 mutex_lock(&inode->i_mutex);
5894 ret = ocfs2_inode_lock(inode, &di_bh, 1);
5900 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5901 if (IS_ERR(handle)) {
5902 ret = PTR_ERR(handle);
5908 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5910 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5911 head->free_bit, (unsigned long long)head->free_blk);
5913 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5914 head->free_bit, bg_blkno, 1);
5920 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5927 head = head->free_next;
5932 ocfs2_commit_trans(osb, handle);
5935 ocfs2_inode_unlock(inode, 1);
5938 mutex_unlock(&inode->i_mutex);
5942 /* Premature exit may have left some dangling items. */
5944 head = head->free_next;
5951 int ocfs2_run_deallocs(struct ocfs2_super *osb,
5952 struct ocfs2_cached_dealloc_ctxt *ctxt)
5955 struct ocfs2_per_slot_free_list *fl;
5960 while (ctxt->c_first_suballocator) {
5961 fl = ctxt->c_first_suballocator;
5964 mlog(0, "Free items: (type %u, slot %d)\n",
5965 fl->f_inode_type, fl->f_slot);
5966 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
5967 fl->f_slot, fl->f_first);
5974 ctxt->c_first_suballocator = fl->f_next_suballocator;
5981 static struct ocfs2_per_slot_free_list *
5982 ocfs2_find_per_slot_free_list(int type,
5984 struct ocfs2_cached_dealloc_ctxt *ctxt)
5986 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
5989 if (fl->f_inode_type == type && fl->f_slot == slot)
5992 fl = fl->f_next_suballocator;
5995 fl = kmalloc(sizeof(*fl), GFP_NOFS);
5997 fl->f_inode_type = type;
6000 fl->f_next_suballocator = ctxt->c_first_suballocator;
6002 ctxt->c_first_suballocator = fl;
6007 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6008 int type, int slot, u64 blkno,
6012 struct ocfs2_per_slot_free_list *fl;
6013 struct ocfs2_cached_block_free *item;
6015 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6022 item = kmalloc(sizeof(*item), GFP_NOFS);
6029 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6030 type, slot, bit, (unsigned long long)blkno);
6032 item->free_blk = blkno;
6033 item->free_bit = bit;
6034 item->free_next = fl->f_first;
6043 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6044 struct ocfs2_extent_block *eb)
6046 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6047 le16_to_cpu(eb->h_suballoc_slot),
6048 le64_to_cpu(eb->h_blkno),
6049 le16_to_cpu(eb->h_suballoc_bit));
6052 /* This function will figure out whether the currently last extent
6053 * block will be deleted, and if it will, what the new last extent
6054 * block will be so we can update his h_next_leaf_blk field, as well
6055 * as the dinodes i_last_eb_blk */
6056 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6057 unsigned int clusters_to_del,
6058 struct ocfs2_path *path,
6059 struct buffer_head **new_last_eb)
6061 int next_free, ret = 0;
6063 struct ocfs2_extent_rec *rec;
6064 struct ocfs2_extent_block *eb;
6065 struct ocfs2_extent_list *el;
6066 struct buffer_head *bh = NULL;
6068 *new_last_eb = NULL;
6070 /* we have no tree, so of course, no last_eb. */
6071 if (!path->p_tree_depth)
6074 /* trunc to zero special case - this makes tree_depth = 0
6075 * regardless of what it is. */
6076 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6079 el = path_leaf_el(path);
6080 BUG_ON(!el->l_next_free_rec);
6083 * Make sure that this extent list will actually be empty
6084 * after we clear away the data. We can shortcut out if
6085 * there's more than one non-empty extent in the
6086 * list. Otherwise, a check of the remaining extent is
6089 next_free = le16_to_cpu(el->l_next_free_rec);
6091 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6095 /* We may have a valid extent in index 1, check it. */
6097 rec = &el->l_recs[1];
6100 * Fall through - no more nonempty extents, so we want
6101 * to delete this leaf.
6107 rec = &el->l_recs[0];
6112 * Check it we'll only be trimming off the end of this
6115 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6119 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6125 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6131 eb = (struct ocfs2_extent_block *) bh->b_data;
6133 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6134 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6140 get_bh(*new_last_eb);
6141 mlog(0, "returning block %llu, (cpos: %u)\n",
6142 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6150 * Trim some clusters off the rightmost edge of a tree. Only called
6153 * The caller needs to:
6154 * - start journaling of each path component.
6155 * - compute and fully set up any new last ext block
6157 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6158 handle_t *handle, struct ocfs2_truncate_context *tc,
6159 u32 clusters_to_del, u64 *delete_start)
6161 int ret, i, index = path->p_tree_depth;
6164 struct buffer_head *bh;
6165 struct ocfs2_extent_list *el;
6166 struct ocfs2_extent_rec *rec;
6170 while (index >= 0) {
6171 bh = path->p_node[index].bh;
6172 el = path->p_node[index].el;
6174 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6175 index, (unsigned long long)bh->b_blocknr);
6177 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6180 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6181 ocfs2_error(inode->i_sb,
6182 "Inode %lu has invalid ext. block %llu",
6184 (unsigned long long)bh->b_blocknr);
6190 i = le16_to_cpu(el->l_next_free_rec) - 1;
6191 rec = &el->l_recs[i];
6193 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6194 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6195 ocfs2_rec_clusters(el, rec),
6196 (unsigned long long)le64_to_cpu(rec->e_blkno),
6197 le16_to_cpu(el->l_next_free_rec));
6199 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6201 if (le16_to_cpu(el->l_tree_depth) == 0) {
6203 * If the leaf block contains a single empty
6204 * extent and no records, we can just remove
6207 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6209 sizeof(struct ocfs2_extent_rec));
6210 el->l_next_free_rec = cpu_to_le16(0);
6216 * Remove any empty extents by shifting things
6217 * left. That should make life much easier on
6218 * the code below. This condition is rare
6219 * enough that we shouldn't see a performance
6222 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6223 le16_add_cpu(&el->l_next_free_rec, -1);
6226 i < le16_to_cpu(el->l_next_free_rec); i++)
6227 el->l_recs[i] = el->l_recs[i + 1];
6229 memset(&el->l_recs[i], 0,
6230 sizeof(struct ocfs2_extent_rec));
6233 * We've modified our extent list. The
6234 * simplest way to handle this change
6235 * is to being the search from the
6238 goto find_tail_record;
6241 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6244 * We'll use "new_edge" on our way back up the
6245 * tree to know what our rightmost cpos is.
6247 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6248 new_edge += le32_to_cpu(rec->e_cpos);
6251 * The caller will use this to delete data blocks.
6253 *delete_start = le64_to_cpu(rec->e_blkno)
6254 + ocfs2_clusters_to_blocks(inode->i_sb,
6255 le16_to_cpu(rec->e_leaf_clusters));
6258 * If it's now empty, remove this record.
6260 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6262 sizeof(struct ocfs2_extent_rec));
6263 le16_add_cpu(&el->l_next_free_rec, -1);
6266 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6268 sizeof(struct ocfs2_extent_rec));
6269 le16_add_cpu(&el->l_next_free_rec, -1);
6274 /* Can this actually happen? */
6275 if (le16_to_cpu(el->l_next_free_rec) == 0)
6279 * We never actually deleted any clusters
6280 * because our leaf was empty. There's no
6281 * reason to adjust the rightmost edge then.
6286 rec->e_int_clusters = cpu_to_le32(new_edge);
6287 le32_add_cpu(&rec->e_int_clusters,
6288 -le32_to_cpu(rec->e_cpos));
6291 * A deleted child record should have been
6294 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6298 ret = ocfs2_journal_dirty(handle, bh);
6304 mlog(0, "extent list container %llu, after: record %d: "
6305 "(%u, %u, %llu), next = %u.\n",
6306 (unsigned long long)bh->b_blocknr, i,
6307 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6308 (unsigned long long)le64_to_cpu(rec->e_blkno),
6309 le16_to_cpu(el->l_next_free_rec));
6312 * We must be careful to only attempt delete of an
6313 * extent block (and not the root inode block).
6315 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6316 struct ocfs2_extent_block *eb =
6317 (struct ocfs2_extent_block *)bh->b_data;
6320 * Save this for use when processing the
6323 deleted_eb = le64_to_cpu(eb->h_blkno);
6325 mlog(0, "deleting this extent block.\n");
6327 ocfs2_remove_from_cache(inode, bh);
6329 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6330 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6331 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6333 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6334 /* An error here is not fatal. */
6349 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6350 unsigned int clusters_to_del,
6351 struct inode *inode,
6352 struct buffer_head *fe_bh,
6354 struct ocfs2_truncate_context *tc,
6355 struct ocfs2_path *path)
6358 struct ocfs2_dinode *fe;
6359 struct ocfs2_extent_block *last_eb = NULL;
6360 struct ocfs2_extent_list *el;
6361 struct buffer_head *last_eb_bh = NULL;
6364 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6366 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6374 * Each component will be touched, so we might as well journal
6375 * here to avoid having to handle errors later.
6377 status = ocfs2_journal_access_path(inode, handle, path);
6384 status = ocfs2_journal_access(handle, inode, last_eb_bh,
6385 OCFS2_JOURNAL_ACCESS_WRITE);
6391 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6394 el = &(fe->id2.i_list);
6397 * Lower levels depend on this never happening, but it's best
6398 * to check it up here before changing the tree.
6400 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6401 ocfs2_error(inode->i_sb,
6402 "Inode %lu has an empty extent record, depth %u\n",
6403 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6408 spin_lock(&OCFS2_I(inode)->ip_lock);
6409 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6411 spin_unlock(&OCFS2_I(inode)->ip_lock);
6412 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6413 inode->i_blocks = ocfs2_inode_sector_count(inode);
6415 status = ocfs2_trim_tree(inode, path, handle, tc,
6416 clusters_to_del, &delete_blk);
6422 if (le32_to_cpu(fe->i_clusters) == 0) {
6423 /* trunc to zero is a special case. */
6424 el->l_tree_depth = 0;
6425 fe->i_last_eb_blk = 0;
6427 fe->i_last_eb_blk = last_eb->h_blkno;
6429 status = ocfs2_journal_dirty(handle, fe_bh);
6436 /* If there will be a new last extent block, then by
6437 * definition, there cannot be any leaves to the right of
6439 last_eb->h_next_leaf_blk = 0;
6440 status = ocfs2_journal_dirty(handle, last_eb_bh);
6448 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6462 static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
6464 set_buffer_uptodate(bh);
6465 mark_buffer_dirty(bh);
6469 static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
6471 set_buffer_uptodate(bh);
6472 mark_buffer_dirty(bh);
6473 return ocfs2_journal_dirty_data(handle, bh);
6476 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6477 unsigned int from, unsigned int to,
6478 struct page *page, int zero, u64 *phys)
6480 int ret, partial = 0;
6482 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6487 zero_user_segment(page, from, to);
6490 * Need to set the buffers we zero'd into uptodate
6491 * here if they aren't - ocfs2_map_page_blocks()
6492 * might've skipped some
6494 if (ocfs2_should_order_data(inode)) {
6495 ret = walk_page_buffers(handle,
6498 ocfs2_ordered_zero_func);
6502 ret = walk_page_buffers(handle, page_buffers(page),
6504 ocfs2_writeback_zero_func);
6510 SetPageUptodate(page);
6512 flush_dcache_page(page);
6515 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6516 loff_t end, struct page **pages,
6517 int numpages, u64 phys, handle_t *handle)
6521 unsigned int from, to = PAGE_CACHE_SIZE;
6522 struct super_block *sb = inode->i_sb;
6524 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6529 to = PAGE_CACHE_SIZE;
6530 for(i = 0; i < numpages; i++) {
6533 from = start & (PAGE_CACHE_SIZE - 1);
6534 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6535 to = end & (PAGE_CACHE_SIZE - 1);
6537 BUG_ON(from > PAGE_CACHE_SIZE);
6538 BUG_ON(to > PAGE_CACHE_SIZE);
6540 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6543 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6547 ocfs2_unlock_and_free_pages(pages, numpages);
6550 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6551 struct page **pages, int *num)
6553 int numpages, ret = 0;
6554 struct super_block *sb = inode->i_sb;
6555 struct address_space *mapping = inode->i_mapping;
6556 unsigned long index;
6557 loff_t last_page_bytes;
6559 BUG_ON(start > end);
6561 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6562 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6565 last_page_bytes = PAGE_ALIGN(end);
6566 index = start >> PAGE_CACHE_SHIFT;
6568 pages[numpages] = grab_cache_page(mapping, index);
6569 if (!pages[numpages]) {
6577 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6582 ocfs2_unlock_and_free_pages(pages, numpages);
6592 * Zero the area past i_size but still within an allocated
6593 * cluster. This avoids exposing nonzero data on subsequent file
6596 * We need to call this before i_size is updated on the inode because
6597 * otherwise block_write_full_page() will skip writeout of pages past
6598 * i_size. The new_i_size parameter is passed for this reason.
6600 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6601 u64 range_start, u64 range_end)
6603 int ret = 0, numpages;
6604 struct page **pages = NULL;
6606 unsigned int ext_flags;
6607 struct super_block *sb = inode->i_sb;
6610 * File systems which don't support sparse files zero on every
6613 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6616 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6617 sizeof(struct page *), GFP_NOFS);
6618 if (pages == NULL) {
6624 if (range_start == range_end)
6627 ret = ocfs2_extent_map_get_blocks(inode,
6628 range_start >> sb->s_blocksize_bits,
6629 &phys, NULL, &ext_flags);
6636 * Tail is a hole, or is marked unwritten. In either case, we
6637 * can count on read and write to return/push zero's.
6639 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6642 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6649 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6650 numpages, phys, handle);
6653 * Initiate writeout of the pages we zero'd here. We don't
6654 * wait on them - the truncate_inode_pages() call later will
6657 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6658 range_end - 1, SYNC_FILE_RANGE_WRITE);
6669 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6670 struct ocfs2_dinode *di)
6672 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6673 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6675 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6676 memset(&di->id2, 0, blocksize -
6677 offsetof(struct ocfs2_dinode, id2) -
6680 memset(&di->id2, 0, blocksize -
6681 offsetof(struct ocfs2_dinode, id2));
6684 void ocfs2_dinode_new_extent_list(struct inode *inode,
6685 struct ocfs2_dinode *di)
6687 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6688 di->id2.i_list.l_tree_depth = 0;
6689 di->id2.i_list.l_next_free_rec = 0;
6690 di->id2.i_list.l_count = cpu_to_le16(
6691 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6694 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6696 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6697 struct ocfs2_inline_data *idata = &di->id2.i_data;
6699 spin_lock(&oi->ip_lock);
6700 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6701 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6702 spin_unlock(&oi->ip_lock);
6705 * We clear the entire i_data structure here so that all
6706 * fields can be properly initialized.
6708 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6710 idata->id_count = cpu_to_le16(
6711 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6714 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6715 struct buffer_head *di_bh)
6717 int ret, i, has_data, num_pages = 0;
6719 u64 uninitialized_var(block);
6720 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6721 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6722 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6723 struct ocfs2_alloc_context *data_ac = NULL;
6724 struct page **pages = NULL;
6725 loff_t end = osb->s_clustersize;
6727 has_data = i_size_read(inode) ? 1 : 0;
6730 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6731 sizeof(struct page *), GFP_NOFS);
6732 if (pages == NULL) {
6738 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6745 handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
6746 if (IS_ERR(handle)) {
6747 ret = PTR_ERR(handle);
6752 ret = ocfs2_journal_access(handle, inode, di_bh,
6753 OCFS2_JOURNAL_ACCESS_WRITE);
6761 unsigned int page_end;
6764 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6772 * Save two copies, one for insert, and one that can
6773 * be changed by ocfs2_map_and_dirty_page() below.
6775 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6778 * Non sparse file systems zero on extend, so no need
6781 if (!ocfs2_sparse_alloc(osb) &&
6782 PAGE_CACHE_SIZE < osb->s_clustersize)
6783 end = PAGE_CACHE_SIZE;
6785 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6792 * This should populate the 1st page for us and mark
6795 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6801 page_end = PAGE_CACHE_SIZE;
6802 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6803 page_end = osb->s_clustersize;
6805 for (i = 0; i < num_pages; i++)
6806 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6807 pages[i], i > 0, &phys);
6810 spin_lock(&oi->ip_lock);
6811 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6812 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6813 spin_unlock(&oi->ip_lock);
6815 ocfs2_dinode_new_extent_list(inode, di);
6817 ocfs2_journal_dirty(handle, di_bh);
6821 * An error at this point should be extremely rare. If
6822 * this proves to be false, we could always re-build
6823 * the in-inode data from our pages.
6825 ret = ocfs2_dinode_insert_extent(osb, handle, inode, di_bh,
6826 0, block, 1, 0, NULL);
6832 inode->i_blocks = ocfs2_inode_sector_count(inode);
6836 ocfs2_commit_trans(osb, handle);
6840 ocfs2_free_alloc_context(data_ac);
6844 ocfs2_unlock_and_free_pages(pages, num_pages);
6852 * It is expected, that by the time you call this function,
6853 * inode->i_size and fe->i_size have been adjusted.
6855 * WARNING: This will kfree the truncate context
6857 int ocfs2_commit_truncate(struct ocfs2_super *osb,
6858 struct inode *inode,
6859 struct buffer_head *fe_bh,
6860 struct ocfs2_truncate_context *tc)
6862 int status, i, credits, tl_sem = 0;
6863 u32 clusters_to_del, new_highest_cpos, range;
6864 struct ocfs2_extent_list *el;
6865 handle_t *handle = NULL;
6866 struct inode *tl_inode = osb->osb_tl_inode;
6867 struct ocfs2_path *path = NULL;
6868 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6872 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
6873 i_size_read(inode));
6875 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
6882 ocfs2_extent_map_trunc(inode, new_highest_cpos);
6886 * Check that we still have allocation to delete.
6888 if (OCFS2_I(inode)->ip_clusters == 0) {
6894 * Truncate always works against the rightmost tree branch.
6896 status = ocfs2_find_path(inode, path, UINT_MAX);
6902 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6903 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
6906 * By now, el will point to the extent list on the bottom most
6907 * portion of this tree. Only the tail record is considered in
6910 * We handle the following cases, in order:
6911 * - empty extent: delete the remaining branch
6912 * - remove the entire record
6913 * - remove a partial record
6914 * - no record needs to be removed (truncate has completed)
6916 el = path_leaf_el(path);
6917 if (le16_to_cpu(el->l_next_free_rec) == 0) {
6918 ocfs2_error(inode->i_sb,
6919 "Inode %llu has empty extent block at %llu\n",
6920 (unsigned long long)OCFS2_I(inode)->ip_blkno,
6921 (unsigned long long)path_leaf_bh(path)->b_blocknr);
6926 i = le16_to_cpu(el->l_next_free_rec) - 1;
6927 range = le32_to_cpu(el->l_recs[i].e_cpos) +
6928 ocfs2_rec_clusters(el, &el->l_recs[i]);
6929 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
6930 clusters_to_del = 0;
6931 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
6932 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
6933 } else if (range > new_highest_cpos) {
6934 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
6935 le32_to_cpu(el->l_recs[i].e_cpos)) -
6942 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6943 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
6945 mutex_lock(&tl_inode->i_mutex);
6947 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6948 * record is free for use. If there isn't any, we flush to get
6949 * an empty truncate log. */
6950 if (ocfs2_truncate_log_needs_flush(osb)) {
6951 status = __ocfs2_flush_truncate_log(osb);
6958 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
6959 (struct ocfs2_dinode *)fe_bh->b_data,
6961 handle = ocfs2_start_trans(osb, credits);
6962 if (IS_ERR(handle)) {
6963 status = PTR_ERR(handle);
6969 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
6976 mutex_unlock(&tl_inode->i_mutex);
6979 ocfs2_commit_trans(osb, handle);
6982 ocfs2_reinit_path(path, 1);
6985 * The check above will catch the case where we've truncated
6986 * away all allocation.
6992 ocfs2_schedule_truncate_log_flush(osb, 1);
6995 mutex_unlock(&tl_inode->i_mutex);
6998 ocfs2_commit_trans(osb, handle);
7000 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7002 ocfs2_free_path(path);
7004 /* This will drop the ext_alloc cluster lock for us */
7005 ocfs2_free_truncate_context(tc);
7012 * Expects the inode to already be locked.
7014 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7015 struct inode *inode,
7016 struct buffer_head *fe_bh,
7017 struct ocfs2_truncate_context **tc)
7020 unsigned int new_i_clusters;
7021 struct ocfs2_dinode *fe;
7022 struct ocfs2_extent_block *eb;
7023 struct buffer_head *last_eb_bh = NULL;
7029 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7030 i_size_read(inode));
7031 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7033 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7034 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7035 (unsigned long long)le64_to_cpu(fe->i_size));
7037 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7043 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7045 if (fe->id2.i_list.l_tree_depth) {
7046 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
7047 &last_eb_bh, OCFS2_BH_CACHED, inode);
7052 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7053 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
7054 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
7062 (*tc)->tc_last_eb_bh = last_eb_bh;
7068 ocfs2_free_truncate_context(*tc);
7076 * 'start' is inclusive, 'end' is not.
7078 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7079 unsigned int start, unsigned int end, int trunc)
7082 unsigned int numbytes;
7084 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7085 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7086 struct ocfs2_inline_data *idata = &di->id2.i_data;
7088 if (end > i_size_read(inode))
7089 end = i_size_read(inode);
7091 BUG_ON(start >= end);
7093 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7094 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7095 !ocfs2_supports_inline_data(osb)) {
7096 ocfs2_error(inode->i_sb,
7097 "Inline data flags for inode %llu don't agree! "
7098 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7099 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7100 le16_to_cpu(di->i_dyn_features),
7101 OCFS2_I(inode)->ip_dyn_features,
7102 osb->s_feature_incompat);
7107 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7108 if (IS_ERR(handle)) {
7109 ret = PTR_ERR(handle);
7114 ret = ocfs2_journal_access(handle, inode, di_bh,
7115 OCFS2_JOURNAL_ACCESS_WRITE);
7121 numbytes = end - start;
7122 memset(idata->id_data + start, 0, numbytes);
7125 * No need to worry about the data page here - it's been
7126 * truncated already and inline data doesn't need it for
7127 * pushing zero's to disk, so we'll let readpage pick it up
7131 i_size_write(inode, start);
7132 di->i_size = cpu_to_le64(start);
7135 inode->i_blocks = ocfs2_inode_sector_count(inode);
7136 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7138 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7139 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7141 ocfs2_journal_dirty(handle, di_bh);
7144 ocfs2_commit_trans(osb, handle);
7150 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7153 * The caller is responsible for completing deallocation
7154 * before freeing the context.
7156 if (tc->tc_dealloc.c_first_suballocator != NULL)
7158 "Truncate completion has non-empty dealloc context\n");
7160 if (tc->tc_last_eb_bh)
7161 brelse(tc->tc_last_eb_bh);