1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Extent allocs and frees
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
30 #include <linux/swap.h>
32 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33 #include <cluster/masklog.h>
40 #include "extent_map.h"
43 #include "localalloc.h"
50 #include "buffer_head_io.h"
53 * ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract
54 * the b-tree operations in ocfs2. Now all the b-tree operations are not
55 * limited to ocfs2_dinode only. Any data which need to allocate clusters
56 * to store can use b-tree. And it only needs to implement its ocfs2_extent_tree
59 * ocfs2_extent_tree contains info for the root of the b-tree, it must have a
60 * root ocfs2_extent_list and a root_bh so that they can be used in the b-tree
62 * ocfs2_extent_tree_operations abstract the normal operations we do for
63 * the root of extent b-tree.
65 struct ocfs2_extent_tree;
67 struct ocfs2_extent_tree_operations {
68 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
70 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
71 void (*eo_update_clusters)(struct inode *inode,
72 struct ocfs2_extent_tree *et,
74 int (*eo_insert_check)(struct inode *inode,
75 struct ocfs2_extent_tree *et,
76 struct ocfs2_extent_rec *rec);
77 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
79 /* These are internal to ocfs2_extent_tree and don't have
80 * accessor functions */
81 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
82 void (*eo_fill_max_leaf_clusters)(struct inode *inode,
83 struct ocfs2_extent_tree *et);
86 struct ocfs2_extent_tree {
87 enum ocfs2_extent_tree_type et_type;
88 struct ocfs2_extent_tree_operations *et_ops;
89 struct buffer_head *et_root_bh;
90 struct ocfs2_extent_list *et_root_el;
92 unsigned int et_max_leaf_clusters;
95 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
97 struct ocfs2_dinode *di = et->et_object;
99 et->et_root_el = &di->id2.i_list;
102 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
105 struct ocfs2_dinode *di = et->et_object;
107 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
108 di->i_last_eb_blk = cpu_to_le64(blkno);
111 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
113 struct ocfs2_dinode *di = et->et_object;
115 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
116 return le64_to_cpu(di->i_last_eb_blk);
119 static void ocfs2_dinode_update_clusters(struct inode *inode,
120 struct ocfs2_extent_tree *et,
123 struct ocfs2_dinode *di = et->et_object;
125 le32_add_cpu(&di->i_clusters, clusters);
126 spin_lock(&OCFS2_I(inode)->ip_lock);
127 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
128 spin_unlock(&OCFS2_I(inode)->ip_lock);
131 static int ocfs2_dinode_insert_check(struct inode *inode,
132 struct ocfs2_extent_tree *et,
133 struct ocfs2_extent_rec *rec)
135 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
137 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
138 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
139 (OCFS2_I(inode)->ip_clusters != rec->e_cpos),
140 "Device %s, asking for sparse allocation: inode %llu, "
141 "cpos %u, clusters %u\n",
143 (unsigned long long)OCFS2_I(inode)->ip_blkno,
145 OCFS2_I(inode)->ip_clusters);
150 static int ocfs2_dinode_sanity_check(struct inode *inode,
151 struct ocfs2_extent_tree *et)
154 struct ocfs2_dinode *di;
156 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
159 if (!OCFS2_IS_VALID_DINODE(di)) {
161 ocfs2_error(inode->i_sb,
162 "Inode %llu has invalid path root",
163 (unsigned long long)OCFS2_I(inode)->ip_blkno);
169 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
170 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
171 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
172 .eo_update_clusters = ocfs2_dinode_update_clusters,
173 .eo_insert_check = ocfs2_dinode_insert_check,
174 .eo_sanity_check = ocfs2_dinode_sanity_check,
175 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
178 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
180 struct ocfs2_xattr_value_root *xv = et->et_object;
182 et->et_root_el = &xv->xr_list;
185 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
188 struct ocfs2_xattr_value_root *xv =
189 (struct ocfs2_xattr_value_root *)et->et_object;
191 xv->xr_last_eb_blk = cpu_to_le64(blkno);
194 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
196 struct ocfs2_xattr_value_root *xv =
197 (struct ocfs2_xattr_value_root *) et->et_object;
199 return le64_to_cpu(xv->xr_last_eb_blk);
202 static void ocfs2_xattr_value_update_clusters(struct inode *inode,
203 struct ocfs2_extent_tree *et,
206 struct ocfs2_xattr_value_root *xv =
207 (struct ocfs2_xattr_value_root *)et->et_object;
209 le32_add_cpu(&xv->xr_clusters, clusters);
212 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
213 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
214 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
215 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
216 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
219 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
221 struct ocfs2_xattr_block *xb = et->et_object;
223 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
226 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
227 struct ocfs2_extent_tree *et)
229 et->et_max_leaf_clusters =
230 ocfs2_clusters_for_bytes(inode->i_sb,
231 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
234 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
237 struct ocfs2_xattr_block *xb = et->et_object;
238 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
240 xt->xt_last_eb_blk = cpu_to_le64(blkno);
243 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
245 struct ocfs2_xattr_block *xb = et->et_object;
246 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
248 return le64_to_cpu(xt->xt_last_eb_blk);
251 static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
252 struct ocfs2_extent_tree *et,
255 struct ocfs2_xattr_block *xb = et->et_object;
257 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
260 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
261 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
262 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
263 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
264 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
265 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
268 static void __ocfs2_get_extent_tree(struct ocfs2_extent_tree *et,
270 struct buffer_head *bh,
272 enum ocfs2_extent_tree_type et_type,
273 struct ocfs2_extent_tree_operations *ops)
275 et->et_type = et_type;
280 obj = (void *)bh->b_data;
283 et->et_ops->eo_fill_root_el(et);
284 if (!et->et_ops->eo_fill_max_leaf_clusters)
285 et->et_max_leaf_clusters = 0;
287 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
290 static void ocfs2_get_dinode_extent_tree(struct ocfs2_extent_tree *et,
292 struct buffer_head *bh)
294 __ocfs2_get_extent_tree(et, inode, bh, NULL, OCFS2_DINODE_EXTENT,
295 &ocfs2_dinode_et_ops);
298 static void ocfs2_get_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
300 struct buffer_head *bh)
302 __ocfs2_get_extent_tree(et, inode, bh, NULL,
303 OCFS2_XATTR_TREE_EXTENT,
304 &ocfs2_xattr_tree_et_ops);
307 static void ocfs2_get_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
309 struct buffer_head *bh,
310 struct ocfs2_xattr_value_root *xv)
312 __ocfs2_get_extent_tree(et, inode, bh, xv,
313 OCFS2_XATTR_VALUE_EXTENT,
314 &ocfs2_xattr_value_et_ops);
317 static void ocfs2_get_extent_tree(struct ocfs2_extent_tree *et,
319 struct buffer_head *bh,
320 enum ocfs2_extent_tree_type et_type,
323 if (et_type == OCFS2_DINODE_EXTENT)
324 ocfs2_get_dinode_extent_tree(et, inode, bh);
325 else if (et_type == OCFS2_XATTR_VALUE_EXTENT)
326 ocfs2_get_xattr_tree_extent_tree(et, inode, bh);
327 else if (et_type == OCFS2_XATTR_TREE_EXTENT)
328 ocfs2_get_xattr_value_extent_tree(et, inode, bh, obj);
333 static void ocfs2_put_extent_tree(struct ocfs2_extent_tree *et)
335 brelse(et->et_root_bh);
338 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
341 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
344 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
346 return et->et_ops->eo_get_last_eb_blk(et);
349 static inline void ocfs2_et_update_clusters(struct inode *inode,
350 struct ocfs2_extent_tree *et,
353 et->et_ops->eo_update_clusters(inode, et, clusters);
356 static inline int ocfs2_et_insert_check(struct inode *inode,
357 struct ocfs2_extent_tree *et,
358 struct ocfs2_extent_rec *rec)
362 if (et->et_ops->eo_insert_check)
363 ret = et->et_ops->eo_insert_check(inode, et, rec);
367 static inline int ocfs2_et_sanity_check(struct inode *inode,
368 struct ocfs2_extent_tree *et)
372 if (et->et_ops->eo_sanity_check)
373 ret = et->et_ops->eo_sanity_check(inode, et);
377 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
378 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
379 struct ocfs2_extent_block *eb);
382 * Structures which describe a path through a btree, and functions to
385 * The idea here is to be as generic as possible with the tree
388 struct ocfs2_path_item {
389 struct buffer_head *bh;
390 struct ocfs2_extent_list *el;
393 #define OCFS2_MAX_PATH_DEPTH 5
397 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
400 #define path_root_bh(_path) ((_path)->p_node[0].bh)
401 #define path_root_el(_path) ((_path)->p_node[0].el)
402 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
403 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
404 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
407 * Reset the actual path elements so that we can re-use the structure
408 * to build another path. Generally, this involves freeing the buffer
411 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
413 int i, start = 0, depth = 0;
414 struct ocfs2_path_item *node;
419 for(i = start; i < path_num_items(path); i++) {
420 node = &path->p_node[i];
428 * Tree depth may change during truncate, or insert. If we're
429 * keeping the root extent list, then make sure that our path
430 * structure reflects the proper depth.
433 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
435 path->p_tree_depth = depth;
438 static void ocfs2_free_path(struct ocfs2_path *path)
441 ocfs2_reinit_path(path, 0);
447 * All the elements of src into dest. After this call, src could be freed
448 * without affecting dest.
450 * Both paths should have the same root. Any non-root elements of dest
453 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
457 BUG_ON(path_root_bh(dest) != path_root_bh(src));
458 BUG_ON(path_root_el(dest) != path_root_el(src));
460 ocfs2_reinit_path(dest, 1);
462 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
463 dest->p_node[i].bh = src->p_node[i].bh;
464 dest->p_node[i].el = src->p_node[i].el;
466 if (dest->p_node[i].bh)
467 get_bh(dest->p_node[i].bh);
472 * Make the *dest path the same as src and re-initialize src path to
475 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
479 BUG_ON(path_root_bh(dest) != path_root_bh(src));
481 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
482 brelse(dest->p_node[i].bh);
484 dest->p_node[i].bh = src->p_node[i].bh;
485 dest->p_node[i].el = src->p_node[i].el;
487 src->p_node[i].bh = NULL;
488 src->p_node[i].el = NULL;
493 * Insert an extent block at given index.
495 * This will not take an additional reference on eb_bh.
497 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
498 struct buffer_head *eb_bh)
500 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
503 * Right now, no root bh is an extent block, so this helps
504 * catch code errors with dinode trees. The assertion can be
505 * safely removed if we ever need to insert extent block
506 * structures at the root.
510 path->p_node[index].bh = eb_bh;
511 path->p_node[index].el = &eb->h_list;
514 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
515 struct ocfs2_extent_list *root_el)
517 struct ocfs2_path *path;
519 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
521 path = kzalloc(sizeof(*path), GFP_NOFS);
523 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
525 path_root_bh(path) = root_bh;
526 path_root_el(path) = root_el;
533 * Convenience function to journal all components in a path.
535 static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
536 struct ocfs2_path *path)
543 for(i = 0; i < path_num_items(path); i++) {
544 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
545 OCFS2_JOURNAL_ACCESS_WRITE);
557 * Return the index of the extent record which contains cluster #v_cluster.
558 * -1 is returned if it was not found.
560 * Should work fine on interior and exterior nodes.
562 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
566 struct ocfs2_extent_rec *rec;
567 u32 rec_end, rec_start, clusters;
569 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
570 rec = &el->l_recs[i];
572 rec_start = le32_to_cpu(rec->e_cpos);
573 clusters = ocfs2_rec_clusters(el, rec);
575 rec_end = rec_start + clusters;
577 if (v_cluster >= rec_start && v_cluster < rec_end) {
586 enum ocfs2_contig_type {
595 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
596 * ocfs2_extent_contig only work properly against leaf nodes!
598 static int ocfs2_block_extent_contig(struct super_block *sb,
599 struct ocfs2_extent_rec *ext,
602 u64 blk_end = le64_to_cpu(ext->e_blkno);
604 blk_end += ocfs2_clusters_to_blocks(sb,
605 le16_to_cpu(ext->e_leaf_clusters));
607 return blkno == blk_end;
610 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
611 struct ocfs2_extent_rec *right)
615 left_range = le32_to_cpu(left->e_cpos) +
616 le16_to_cpu(left->e_leaf_clusters);
618 return (left_range == le32_to_cpu(right->e_cpos));
621 static enum ocfs2_contig_type
622 ocfs2_extent_contig(struct inode *inode,
623 struct ocfs2_extent_rec *ext,
624 struct ocfs2_extent_rec *insert_rec)
626 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
629 * Refuse to coalesce extent records with different flag
630 * fields - we don't want to mix unwritten extents with user
633 if (ext->e_flags != insert_rec->e_flags)
636 if (ocfs2_extents_adjacent(ext, insert_rec) &&
637 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
640 blkno = le64_to_cpu(ext->e_blkno);
641 if (ocfs2_extents_adjacent(insert_rec, ext) &&
642 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
649 * NOTE: We can have pretty much any combination of contiguousness and
652 * The usefulness of APPEND_TAIL is more in that it lets us know that
653 * we'll have to update the path to that leaf.
655 enum ocfs2_append_type {
660 enum ocfs2_split_type {
666 struct ocfs2_insert_type {
667 enum ocfs2_split_type ins_split;
668 enum ocfs2_append_type ins_appending;
669 enum ocfs2_contig_type ins_contig;
670 int ins_contig_index;
674 struct ocfs2_merge_ctxt {
675 enum ocfs2_contig_type c_contig_type;
676 int c_has_empty_extent;
677 int c_split_covers_rec;
681 * How many free extents have we got before we need more meta data?
683 int ocfs2_num_free_extents(struct ocfs2_super *osb,
685 struct buffer_head *root_bh,
686 enum ocfs2_extent_tree_type type,
690 struct ocfs2_extent_list *el = NULL;
691 struct ocfs2_extent_block *eb;
692 struct buffer_head *eb_bh = NULL;
694 struct ocfs2_extent_tree et;
698 ocfs2_get_extent_tree(&et, inode, root_bh, type, obj);
700 last_eb_blk = ocfs2_et_get_last_eb_blk(&et);
703 retval = ocfs2_read_block(osb, last_eb_blk,
704 &eb_bh, OCFS2_BH_CACHED, inode);
709 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
713 BUG_ON(el->l_tree_depth != 0);
715 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
720 ocfs2_put_extent_tree(&et);
725 /* expects array to already be allocated
727 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
730 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
734 struct ocfs2_alloc_context *meta_ac,
735 struct buffer_head *bhs[])
737 int count, status, i;
738 u16 suballoc_bit_start;
741 struct ocfs2_extent_block *eb;
746 while (count < wanted) {
747 status = ocfs2_claim_metadata(osb,
759 for(i = count; i < (num_got + count); i++) {
760 bhs[i] = sb_getblk(osb->sb, first_blkno);
761 if (bhs[i] == NULL) {
766 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
768 status = ocfs2_journal_access(handle, inode, bhs[i],
769 OCFS2_JOURNAL_ACCESS_CREATE);
775 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
776 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
777 /* Ok, setup the minimal stuff here. */
778 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
779 eb->h_blkno = cpu_to_le64(first_blkno);
780 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
781 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
782 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
784 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
786 suballoc_bit_start++;
789 /* We'll also be dirtied by the caller, so
790 * this isn't absolutely necessary. */
791 status = ocfs2_journal_dirty(handle, bhs[i]);
804 for(i = 0; i < wanted; i++) {
815 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
817 * Returns the sum of the rightmost extent rec logical offset and
820 * ocfs2_add_branch() uses this to determine what logical cluster
821 * value should be populated into the leftmost new branch records.
823 * ocfs2_shift_tree_depth() uses this to determine the # clusters
824 * value for the new topmost tree record.
826 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
830 i = le16_to_cpu(el->l_next_free_rec) - 1;
832 return le32_to_cpu(el->l_recs[i].e_cpos) +
833 ocfs2_rec_clusters(el, &el->l_recs[i]);
837 * Add an entire tree branch to our inode. eb_bh is the extent block
838 * to start at, if we don't want to start the branch at the dinode
841 * last_eb_bh is required as we have to update it's next_leaf pointer
842 * for the new last extent block.
844 * the new branch will be 'empty' in the sense that every block will
845 * contain a single record with cluster count == 0.
847 static int ocfs2_add_branch(struct ocfs2_super *osb,
850 struct ocfs2_extent_tree *et,
851 struct buffer_head *eb_bh,
852 struct buffer_head **last_eb_bh,
853 struct ocfs2_alloc_context *meta_ac)
855 int status, new_blocks, i;
856 u64 next_blkno, new_last_eb_blk;
857 struct buffer_head *bh;
858 struct buffer_head **new_eb_bhs = NULL;
859 struct ocfs2_extent_block *eb;
860 struct ocfs2_extent_list *eb_el;
861 struct ocfs2_extent_list *el;
866 BUG_ON(!last_eb_bh || !*last_eb_bh);
869 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
874 /* we never add a branch to a leaf. */
875 BUG_ON(!el->l_tree_depth);
877 new_blocks = le16_to_cpu(el->l_tree_depth);
879 /* allocate the number of new eb blocks we need */
880 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
888 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
889 meta_ac, new_eb_bhs);
895 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
896 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
898 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
899 * linked with the rest of the tree.
900 * conversly, new_eb_bhs[0] is the new bottommost leaf.
902 * when we leave the loop, new_last_eb_blk will point to the
903 * newest leaf, and next_blkno will point to the topmost extent
905 next_blkno = new_last_eb_blk = 0;
906 for(i = 0; i < new_blocks; i++) {
908 eb = (struct ocfs2_extent_block *) bh->b_data;
909 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
910 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
916 status = ocfs2_journal_access(handle, inode, bh,
917 OCFS2_JOURNAL_ACCESS_CREATE);
923 eb->h_next_leaf_blk = 0;
924 eb_el->l_tree_depth = cpu_to_le16(i);
925 eb_el->l_next_free_rec = cpu_to_le16(1);
927 * This actually counts as an empty extent as
930 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
931 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
933 * eb_el isn't always an interior node, but even leaf
934 * nodes want a zero'd flags and reserved field so
935 * this gets the whole 32 bits regardless of use.
937 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
938 if (!eb_el->l_tree_depth)
939 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
941 status = ocfs2_journal_dirty(handle, bh);
947 next_blkno = le64_to_cpu(eb->h_blkno);
950 /* This is a bit hairy. We want to update up to three blocks
951 * here without leaving any of them in an inconsistent state
952 * in case of error. We don't have to worry about
953 * journal_dirty erroring as it won't unless we've aborted the
954 * handle (in which case we would never be here) so reserving
955 * the write with journal_access is all we need to do. */
956 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
957 OCFS2_JOURNAL_ACCESS_WRITE);
962 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
963 OCFS2_JOURNAL_ACCESS_WRITE);
969 status = ocfs2_journal_access(handle, inode, eb_bh,
970 OCFS2_JOURNAL_ACCESS_WRITE);
977 /* Link the new branch into the rest of the tree (el will
978 * either be on the root_bh, or the extent block passed in. */
979 i = le16_to_cpu(el->l_next_free_rec);
980 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
981 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
982 el->l_recs[i].e_int_clusters = 0;
983 le16_add_cpu(&el->l_next_free_rec, 1);
985 /* fe needs a new last extent block pointer, as does the
986 * next_leaf on the previously last-extent-block. */
987 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
989 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
990 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
992 status = ocfs2_journal_dirty(handle, *last_eb_bh);
995 status = ocfs2_journal_dirty(handle, et->et_root_bh);
999 status = ocfs2_journal_dirty(handle, eb_bh);
1005 * Some callers want to track the rightmost leaf so pass it
1008 brelse(*last_eb_bh);
1009 get_bh(new_eb_bhs[0]);
1010 *last_eb_bh = new_eb_bhs[0];
1015 for (i = 0; i < new_blocks; i++)
1017 brelse(new_eb_bhs[i]);
1026 * adds another level to the allocation tree.
1027 * returns back the new extent block so you can add a branch to it
1030 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
1032 struct inode *inode,
1033 struct ocfs2_extent_tree *et,
1034 struct ocfs2_alloc_context *meta_ac,
1035 struct buffer_head **ret_new_eb_bh)
1039 struct buffer_head *new_eb_bh = NULL;
1040 struct ocfs2_extent_block *eb;
1041 struct ocfs2_extent_list *root_el;
1042 struct ocfs2_extent_list *eb_el;
1046 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1053 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1054 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1055 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1060 eb_el = &eb->h_list;
1061 root_el = et->et_root_el;
1063 status = ocfs2_journal_access(handle, inode, new_eb_bh,
1064 OCFS2_JOURNAL_ACCESS_CREATE);
1070 /* copy the root extent list data into the new extent block */
1071 eb_el->l_tree_depth = root_el->l_tree_depth;
1072 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1073 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1074 eb_el->l_recs[i] = root_el->l_recs[i];
1076 status = ocfs2_journal_dirty(handle, new_eb_bh);
1082 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
1083 OCFS2_JOURNAL_ACCESS_WRITE);
1089 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1091 /* update root_bh now */
1092 le16_add_cpu(&root_el->l_tree_depth, 1);
1093 root_el->l_recs[0].e_cpos = 0;
1094 root_el->l_recs[0].e_blkno = eb->h_blkno;
1095 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1096 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1097 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1098 root_el->l_next_free_rec = cpu_to_le16(1);
1100 /* If this is our 1st tree depth shift, then last_eb_blk
1101 * becomes the allocated extent block */
1102 if (root_el->l_tree_depth == cpu_to_le16(1))
1103 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1105 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1111 *ret_new_eb_bh = new_eb_bh;
1123 * Should only be called when there is no space left in any of the
1124 * leaf nodes. What we want to do is find the lowest tree depth
1125 * non-leaf extent block with room for new records. There are three
1126 * valid results of this search:
1128 * 1) a lowest extent block is found, then we pass it back in
1129 * *lowest_eb_bh and return '0'
1131 * 2) the search fails to find anything, but the root_el has room. We
1132 * pass NULL back in *lowest_eb_bh, but still return '0'
1134 * 3) the search fails to find anything AND the root_el is full, in
1135 * which case we return > 0
1137 * return status < 0 indicates an error.
1139 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1140 struct inode *inode,
1141 struct ocfs2_extent_tree *et,
1142 struct buffer_head **target_bh)
1146 struct ocfs2_extent_block *eb;
1147 struct ocfs2_extent_list *el;
1148 struct buffer_head *bh = NULL;
1149 struct buffer_head *lowest_bh = NULL;
1155 el = et->et_root_el;
1157 while(le16_to_cpu(el->l_tree_depth) > 1) {
1158 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1159 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
1160 "extent list (next_free_rec == 0)",
1161 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1165 i = le16_to_cpu(el->l_next_free_rec) - 1;
1166 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1168 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
1169 "list where extent # %d has no physical "
1171 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
1181 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
1188 eb = (struct ocfs2_extent_block *) bh->b_data;
1189 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1190 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1196 if (le16_to_cpu(el->l_next_free_rec) <
1197 le16_to_cpu(el->l_count)) {
1205 /* If we didn't find one and the fe doesn't have any room,
1206 * then return '1' */
1207 el = et->et_root_el;
1208 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1211 *target_bh = lowest_bh;
1221 * Grow a b-tree so that it has more records.
1223 * We might shift the tree depth in which case existing paths should
1224 * be considered invalid.
1226 * Tree depth after the grow is returned via *final_depth.
1228 * *last_eb_bh will be updated by ocfs2_add_branch().
1230 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
1231 struct ocfs2_extent_tree *et, int *final_depth,
1232 struct buffer_head **last_eb_bh,
1233 struct ocfs2_alloc_context *meta_ac)
1236 struct ocfs2_extent_list *el = et->et_root_el;
1237 int depth = le16_to_cpu(el->l_tree_depth);
1238 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1239 struct buffer_head *bh = NULL;
1241 BUG_ON(meta_ac == NULL);
1243 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
1250 /* We traveled all the way to the bottom of the allocation tree
1251 * and didn't find room for any more extents - we need to add
1252 * another tree level */
1255 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1257 /* ocfs2_shift_tree_depth will return us a buffer with
1258 * the new extent block (so we can pass that to
1259 * ocfs2_add_branch). */
1260 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
1269 * Special case: we have room now if we shifted from
1270 * tree_depth 0, so no more work needs to be done.
1272 * We won't be calling add_branch, so pass
1273 * back *last_eb_bh as the new leaf. At depth
1274 * zero, it should always be null so there's
1275 * no reason to brelse.
1277 BUG_ON(*last_eb_bh);
1284 /* call ocfs2_add_branch to add the final part of the tree with
1286 mlog(0, "add branch. bh = %p\n", bh);
1287 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
1296 *final_depth = depth;
1302 * This function will discard the rightmost extent record.
1304 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1306 int next_free = le16_to_cpu(el->l_next_free_rec);
1307 int count = le16_to_cpu(el->l_count);
1308 unsigned int num_bytes;
1311 /* This will cause us to go off the end of our extent list. */
1312 BUG_ON(next_free >= count);
1314 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1316 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1319 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1320 struct ocfs2_extent_rec *insert_rec)
1322 int i, insert_index, next_free, has_empty, num_bytes;
1323 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1324 struct ocfs2_extent_rec *rec;
1326 next_free = le16_to_cpu(el->l_next_free_rec);
1327 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1331 /* The tree code before us didn't allow enough room in the leaf. */
1332 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1335 * The easiest way to approach this is to just remove the
1336 * empty extent and temporarily decrement next_free.
1340 * If next_free was 1 (only an empty extent), this
1341 * loop won't execute, which is fine. We still want
1342 * the decrement above to happen.
1344 for(i = 0; i < (next_free - 1); i++)
1345 el->l_recs[i] = el->l_recs[i+1];
1351 * Figure out what the new record index should be.
1353 for(i = 0; i < next_free; i++) {
1354 rec = &el->l_recs[i];
1356 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1361 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1362 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1364 BUG_ON(insert_index < 0);
1365 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1366 BUG_ON(insert_index > next_free);
1369 * No need to memmove if we're just adding to the tail.
1371 if (insert_index != next_free) {
1372 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1374 num_bytes = next_free - insert_index;
1375 num_bytes *= sizeof(struct ocfs2_extent_rec);
1376 memmove(&el->l_recs[insert_index + 1],
1377 &el->l_recs[insert_index],
1382 * Either we had an empty extent, and need to re-increment or
1383 * there was no empty extent on a non full rightmost leaf node,
1384 * in which case we still need to increment.
1387 el->l_next_free_rec = cpu_to_le16(next_free);
1389 * Make sure none of the math above just messed up our tree.
1391 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1393 el->l_recs[insert_index] = *insert_rec;
1397 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1399 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1401 BUG_ON(num_recs == 0);
1403 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1405 size = num_recs * sizeof(struct ocfs2_extent_rec);
1406 memmove(&el->l_recs[0], &el->l_recs[1], size);
1407 memset(&el->l_recs[num_recs], 0,
1408 sizeof(struct ocfs2_extent_rec));
1409 el->l_next_free_rec = cpu_to_le16(num_recs);
1414 * Create an empty extent record .
1416 * l_next_free_rec may be updated.
1418 * If an empty extent already exists do nothing.
1420 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1422 int next_free = le16_to_cpu(el->l_next_free_rec);
1424 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1429 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1432 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1433 "Asked to create an empty extent in a full list:\n"
1434 "count = %u, tree depth = %u",
1435 le16_to_cpu(el->l_count),
1436 le16_to_cpu(el->l_tree_depth));
1438 ocfs2_shift_records_right(el);
1441 le16_add_cpu(&el->l_next_free_rec, 1);
1442 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1446 * For a rotation which involves two leaf nodes, the "root node" is
1447 * the lowest level tree node which contains a path to both leafs. This
1448 * resulting set of information can be used to form a complete "subtree"
1450 * This function is passed two full paths from the dinode down to a
1451 * pair of adjacent leaves. It's task is to figure out which path
1452 * index contains the subtree root - this can be the root index itself
1453 * in a worst-case rotation.
1455 * The array index of the subtree root is passed back.
1457 static int ocfs2_find_subtree_root(struct inode *inode,
1458 struct ocfs2_path *left,
1459 struct ocfs2_path *right)
1464 * Check that the caller passed in two paths from the same tree.
1466 BUG_ON(path_root_bh(left) != path_root_bh(right));
1472 * The caller didn't pass two adjacent paths.
1474 mlog_bug_on_msg(i > left->p_tree_depth,
1475 "Inode %lu, left depth %u, right depth %u\n"
1476 "left leaf blk %llu, right leaf blk %llu\n",
1477 inode->i_ino, left->p_tree_depth,
1478 right->p_tree_depth,
1479 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1480 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1481 } while (left->p_node[i].bh->b_blocknr ==
1482 right->p_node[i].bh->b_blocknr);
1487 typedef void (path_insert_t)(void *, struct buffer_head *);
1490 * Traverse a btree path in search of cpos, starting at root_el.
1492 * This code can be called with a cpos larger than the tree, in which
1493 * case it will return the rightmost path.
1495 static int __ocfs2_find_path(struct inode *inode,
1496 struct ocfs2_extent_list *root_el, u32 cpos,
1497 path_insert_t *func, void *data)
1502 struct buffer_head *bh = NULL;
1503 struct ocfs2_extent_block *eb;
1504 struct ocfs2_extent_list *el;
1505 struct ocfs2_extent_rec *rec;
1506 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1509 while (el->l_tree_depth) {
1510 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1511 ocfs2_error(inode->i_sb,
1512 "Inode %llu has empty extent list at "
1514 (unsigned long long)oi->ip_blkno,
1515 le16_to_cpu(el->l_tree_depth));
1521 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1522 rec = &el->l_recs[i];
1525 * In the case that cpos is off the allocation
1526 * tree, this should just wind up returning the
1529 range = le32_to_cpu(rec->e_cpos) +
1530 ocfs2_rec_clusters(el, rec);
1531 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1535 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1537 ocfs2_error(inode->i_sb,
1538 "Inode %llu has bad blkno in extent list "
1539 "at depth %u (index %d)\n",
1540 (unsigned long long)oi->ip_blkno,
1541 le16_to_cpu(el->l_tree_depth), i);
1548 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1549 &bh, OCFS2_BH_CACHED, inode);
1555 eb = (struct ocfs2_extent_block *) bh->b_data;
1557 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1558 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1563 if (le16_to_cpu(el->l_next_free_rec) >
1564 le16_to_cpu(el->l_count)) {
1565 ocfs2_error(inode->i_sb,
1566 "Inode %llu has bad count in extent list "
1567 "at block %llu (next free=%u, count=%u)\n",
1568 (unsigned long long)oi->ip_blkno,
1569 (unsigned long long)bh->b_blocknr,
1570 le16_to_cpu(el->l_next_free_rec),
1571 le16_to_cpu(el->l_count));
1582 * Catch any trailing bh that the loop didn't handle.
1590 * Given an initialized path (that is, it has a valid root extent
1591 * list), this function will traverse the btree in search of the path
1592 * which would contain cpos.
1594 * The path traveled is recorded in the path structure.
1596 * Note that this will not do any comparisons on leaf node extent
1597 * records, so it will work fine in the case that we just added a tree
1600 struct find_path_data {
1602 struct ocfs2_path *path;
1604 static void find_path_ins(void *data, struct buffer_head *bh)
1606 struct find_path_data *fp = data;
1609 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1612 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1615 struct find_path_data data;
1619 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1620 find_path_ins, &data);
1623 static void find_leaf_ins(void *data, struct buffer_head *bh)
1625 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1626 struct ocfs2_extent_list *el = &eb->h_list;
1627 struct buffer_head **ret = data;
1629 /* We want to retain only the leaf block. */
1630 if (le16_to_cpu(el->l_tree_depth) == 0) {
1636 * Find the leaf block in the tree which would contain cpos. No
1637 * checking of the actual leaf is done.
1639 * Some paths want to call this instead of allocating a path structure
1640 * and calling ocfs2_find_path().
1642 * This function doesn't handle non btree extent lists.
1644 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1645 u32 cpos, struct buffer_head **leaf_bh)
1648 struct buffer_head *bh = NULL;
1650 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1662 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1664 * Basically, we've moved stuff around at the bottom of the tree and
1665 * we need to fix up the extent records above the changes to reflect
1668 * left_rec: the record on the left.
1669 * left_child_el: is the child list pointed to by left_rec
1670 * right_rec: the record to the right of left_rec
1671 * right_child_el: is the child list pointed to by right_rec
1673 * By definition, this only works on interior nodes.
1675 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1676 struct ocfs2_extent_list *left_child_el,
1677 struct ocfs2_extent_rec *right_rec,
1678 struct ocfs2_extent_list *right_child_el)
1680 u32 left_clusters, right_end;
1683 * Interior nodes never have holes. Their cpos is the cpos of
1684 * the leftmost record in their child list. Their cluster
1685 * count covers the full theoretical range of their child list
1686 * - the range between their cpos and the cpos of the record
1687 * immediately to their right.
1689 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1690 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1691 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1692 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1694 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1695 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1698 * Calculate the rightmost cluster count boundary before
1699 * moving cpos - we will need to adjust clusters after
1700 * updating e_cpos to keep the same highest cluster count.
1702 right_end = le32_to_cpu(right_rec->e_cpos);
1703 right_end += le32_to_cpu(right_rec->e_int_clusters);
1705 right_rec->e_cpos = left_rec->e_cpos;
1706 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1708 right_end -= le32_to_cpu(right_rec->e_cpos);
1709 right_rec->e_int_clusters = cpu_to_le32(right_end);
1713 * Adjust the adjacent root node records involved in a
1714 * rotation. left_el_blkno is passed in as a key so that we can easily
1715 * find it's index in the root list.
1717 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1718 struct ocfs2_extent_list *left_el,
1719 struct ocfs2_extent_list *right_el,
1724 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1725 le16_to_cpu(left_el->l_tree_depth));
1727 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1728 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1733 * The path walking code should have never returned a root and
1734 * two paths which are not adjacent.
1736 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1738 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1739 &root_el->l_recs[i + 1], right_el);
1743 * We've changed a leaf block (in right_path) and need to reflect that
1744 * change back up the subtree.
1746 * This happens in multiple places:
1747 * - When we've moved an extent record from the left path leaf to the right
1748 * path leaf to make room for an empty extent in the left path leaf.
1749 * - When our insert into the right path leaf is at the leftmost edge
1750 * and requires an update of the path immediately to it's left. This
1751 * can occur at the end of some types of rotation and appending inserts.
1752 * - When we've adjusted the last extent record in the left path leaf and the
1753 * 1st extent record in the right path leaf during cross extent block merge.
1755 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1756 struct ocfs2_path *left_path,
1757 struct ocfs2_path *right_path,
1761 struct ocfs2_extent_list *el, *left_el, *right_el;
1762 struct ocfs2_extent_rec *left_rec, *right_rec;
1763 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1766 * Update the counts and position values within all the
1767 * interior nodes to reflect the leaf rotation we just did.
1769 * The root node is handled below the loop.
1771 * We begin the loop with right_el and left_el pointing to the
1772 * leaf lists and work our way up.
1774 * NOTE: within this loop, left_el and right_el always refer
1775 * to the *child* lists.
1777 left_el = path_leaf_el(left_path);
1778 right_el = path_leaf_el(right_path);
1779 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1780 mlog(0, "Adjust records at index %u\n", i);
1783 * One nice property of knowing that all of these
1784 * nodes are below the root is that we only deal with
1785 * the leftmost right node record and the rightmost
1788 el = left_path->p_node[i].el;
1789 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1790 left_rec = &el->l_recs[idx];
1792 el = right_path->p_node[i].el;
1793 right_rec = &el->l_recs[0];
1795 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1798 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1802 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1807 * Setup our list pointers now so that the current
1808 * parents become children in the next iteration.
1810 left_el = left_path->p_node[i].el;
1811 right_el = right_path->p_node[i].el;
1815 * At the root node, adjust the two adjacent records which
1816 * begin our path to the leaves.
1819 el = left_path->p_node[subtree_index].el;
1820 left_el = left_path->p_node[subtree_index + 1].el;
1821 right_el = right_path->p_node[subtree_index + 1].el;
1823 ocfs2_adjust_root_records(el, left_el, right_el,
1824 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1826 root_bh = left_path->p_node[subtree_index].bh;
1828 ret = ocfs2_journal_dirty(handle, root_bh);
1833 static int ocfs2_rotate_subtree_right(struct inode *inode,
1835 struct ocfs2_path *left_path,
1836 struct ocfs2_path *right_path,
1840 struct buffer_head *right_leaf_bh;
1841 struct buffer_head *left_leaf_bh = NULL;
1842 struct buffer_head *root_bh;
1843 struct ocfs2_extent_list *right_el, *left_el;
1844 struct ocfs2_extent_rec move_rec;
1846 left_leaf_bh = path_leaf_bh(left_path);
1847 left_el = path_leaf_el(left_path);
1849 if (left_el->l_next_free_rec != left_el->l_count) {
1850 ocfs2_error(inode->i_sb,
1851 "Inode %llu has non-full interior leaf node %llu"
1853 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1854 (unsigned long long)left_leaf_bh->b_blocknr,
1855 le16_to_cpu(left_el->l_next_free_rec));
1860 * This extent block may already have an empty record, so we
1861 * return early if so.
1863 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1866 root_bh = left_path->p_node[subtree_index].bh;
1867 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1869 ret = ocfs2_journal_access(handle, inode, root_bh,
1870 OCFS2_JOURNAL_ACCESS_WRITE);
1876 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1877 ret = ocfs2_journal_access(handle, inode,
1878 right_path->p_node[i].bh,
1879 OCFS2_JOURNAL_ACCESS_WRITE);
1885 ret = ocfs2_journal_access(handle, inode,
1886 left_path->p_node[i].bh,
1887 OCFS2_JOURNAL_ACCESS_WRITE);
1894 right_leaf_bh = path_leaf_bh(right_path);
1895 right_el = path_leaf_el(right_path);
1897 /* This is a code error, not a disk corruption. */
1898 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1899 "because rightmost leaf block %llu is empty\n",
1900 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1901 (unsigned long long)right_leaf_bh->b_blocknr);
1903 ocfs2_create_empty_extent(right_el);
1905 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1911 /* Do the copy now. */
1912 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1913 move_rec = left_el->l_recs[i];
1914 right_el->l_recs[0] = move_rec;
1917 * Clear out the record we just copied and shift everything
1918 * over, leaving an empty extent in the left leaf.
1920 * We temporarily subtract from next_free_rec so that the
1921 * shift will lose the tail record (which is now defunct).
1923 le16_add_cpu(&left_el->l_next_free_rec, -1);
1924 ocfs2_shift_records_right(left_el);
1925 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1926 le16_add_cpu(&left_el->l_next_free_rec, 1);
1928 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1934 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1942 * Given a full path, determine what cpos value would return us a path
1943 * containing the leaf immediately to the left of the current one.
1945 * Will return zero if the path passed in is already the leftmost path.
1947 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1948 struct ocfs2_path *path, u32 *cpos)
1952 struct ocfs2_extent_list *el;
1954 BUG_ON(path->p_tree_depth == 0);
1958 blkno = path_leaf_bh(path)->b_blocknr;
1960 /* Start at the tree node just above the leaf and work our way up. */
1961 i = path->p_tree_depth - 1;
1963 el = path->p_node[i].el;
1966 * Find the extent record just before the one in our
1969 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1970 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1974 * We've determined that the
1975 * path specified is already
1976 * the leftmost one - return a
1982 * The leftmost record points to our
1983 * leaf - we need to travel up the
1989 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
1990 *cpos = *cpos + ocfs2_rec_clusters(el,
1991 &el->l_recs[j - 1]);
1998 * If we got here, we never found a valid node where
1999 * the tree indicated one should be.
2002 "Invalid extent tree at extent block %llu\n",
2003 (unsigned long long)blkno);
2008 blkno = path->p_node[i].bh->b_blocknr;
2017 * Extend the transaction by enough credits to complete the rotation,
2018 * and still leave at least the original number of credits allocated
2019 * to this transaction.
2021 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2023 struct ocfs2_path *path)
2025 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2027 if (handle->h_buffer_credits < credits)
2028 return ocfs2_extend_trans(handle, credits);
2034 * Trap the case where we're inserting into the theoretical range past
2035 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2036 * whose cpos is less than ours into the right leaf.
2038 * It's only necessary to look at the rightmost record of the left
2039 * leaf because the logic that calls us should ensure that the
2040 * theoretical ranges in the path components above the leaves are
2043 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2046 struct ocfs2_extent_list *left_el;
2047 struct ocfs2_extent_rec *rec;
2050 left_el = path_leaf_el(left_path);
2051 next_free = le16_to_cpu(left_el->l_next_free_rec);
2052 rec = &left_el->l_recs[next_free - 1];
2054 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2059 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2061 int next_free = le16_to_cpu(el->l_next_free_rec);
2063 struct ocfs2_extent_rec *rec;
2068 rec = &el->l_recs[0];
2069 if (ocfs2_is_empty_extent(rec)) {
2073 rec = &el->l_recs[1];
2076 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2077 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2083 * Rotate all the records in a btree right one record, starting at insert_cpos.
2085 * The path to the rightmost leaf should be passed in.
2087 * The array is assumed to be large enough to hold an entire path (tree depth).
2089 * Upon succesful return from this function:
2091 * - The 'right_path' array will contain a path to the leaf block
2092 * whose range contains e_cpos.
2093 * - That leaf block will have a single empty extent in list index 0.
2094 * - In the case that the rotation requires a post-insert update,
2095 * *ret_left_path will contain a valid path which can be passed to
2096 * ocfs2_insert_path().
2098 static int ocfs2_rotate_tree_right(struct inode *inode,
2100 enum ocfs2_split_type split,
2102 struct ocfs2_path *right_path,
2103 struct ocfs2_path **ret_left_path)
2105 int ret, start, orig_credits = handle->h_buffer_credits;
2107 struct ocfs2_path *left_path = NULL;
2109 *ret_left_path = NULL;
2111 left_path = ocfs2_new_path(path_root_bh(right_path),
2112 path_root_el(right_path));
2119 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2125 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2128 * What we want to do here is:
2130 * 1) Start with the rightmost path.
2132 * 2) Determine a path to the leaf block directly to the left
2135 * 3) Determine the 'subtree root' - the lowest level tree node
2136 * which contains a path to both leaves.
2138 * 4) Rotate the subtree.
2140 * 5) Find the next subtree by considering the left path to be
2141 * the new right path.
2143 * The check at the top of this while loop also accepts
2144 * insert_cpos == cpos because cpos is only a _theoretical_
2145 * value to get us the left path - insert_cpos might very well
2146 * be filling that hole.
2148 * Stop at a cpos of '0' because we either started at the
2149 * leftmost branch (i.e., a tree with one branch and a
2150 * rotation inside of it), or we've gone as far as we can in
2151 * rotating subtrees.
2153 while (cpos && insert_cpos <= cpos) {
2154 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2157 ret = ocfs2_find_path(inode, left_path, cpos);
2163 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2164 path_leaf_bh(right_path),
2165 "Inode %lu: error during insert of %u "
2166 "(left path cpos %u) results in two identical "
2167 "paths ending at %llu\n",
2168 inode->i_ino, insert_cpos, cpos,
2169 (unsigned long long)
2170 path_leaf_bh(left_path)->b_blocknr);
2172 if (split == SPLIT_NONE &&
2173 ocfs2_rotate_requires_path_adjustment(left_path,
2177 * We've rotated the tree as much as we
2178 * should. The rest is up to
2179 * ocfs2_insert_path() to complete, after the
2180 * record insertion. We indicate this
2181 * situation by returning the left path.
2183 * The reason we don't adjust the records here
2184 * before the record insert is that an error
2185 * later might break the rule where a parent
2186 * record e_cpos will reflect the actual
2187 * e_cpos of the 1st nonempty record of the
2190 *ret_left_path = left_path;
2194 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2196 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2198 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2199 right_path->p_tree_depth);
2201 ret = ocfs2_extend_rotate_transaction(handle, start,
2202 orig_credits, right_path);
2208 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2215 if (split != SPLIT_NONE &&
2216 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2219 * A rotate moves the rightmost left leaf
2220 * record over to the leftmost right leaf
2221 * slot. If we're doing an extent split
2222 * instead of a real insert, then we have to
2223 * check that the extent to be split wasn't
2224 * just moved over. If it was, then we can
2225 * exit here, passing left_path back -
2226 * ocfs2_split_extent() is smart enough to
2227 * search both leaves.
2229 *ret_left_path = left_path;
2234 * There is no need to re-read the next right path
2235 * as we know that it'll be our current left
2236 * path. Optimize by copying values instead.
2238 ocfs2_mv_path(right_path, left_path);
2240 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2249 ocfs2_free_path(left_path);
2255 static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2256 struct ocfs2_path *path)
2259 struct ocfs2_extent_rec *rec;
2260 struct ocfs2_extent_list *el;
2261 struct ocfs2_extent_block *eb;
2264 /* Path should always be rightmost. */
2265 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2266 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2269 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2270 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2271 rec = &el->l_recs[idx];
2272 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2274 for (i = 0; i < path->p_tree_depth; i++) {
2275 el = path->p_node[i].el;
2276 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2277 rec = &el->l_recs[idx];
2279 rec->e_int_clusters = cpu_to_le32(range);
2280 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2282 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2286 static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2287 struct ocfs2_cached_dealloc_ctxt *dealloc,
2288 struct ocfs2_path *path, int unlink_start)
2291 struct ocfs2_extent_block *eb;
2292 struct ocfs2_extent_list *el;
2293 struct buffer_head *bh;
2295 for(i = unlink_start; i < path_num_items(path); i++) {
2296 bh = path->p_node[i].bh;
2298 eb = (struct ocfs2_extent_block *)bh->b_data;
2300 * Not all nodes might have had their final count
2301 * decremented by the caller - handle this here.
2304 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2306 "Inode %llu, attempted to remove extent block "
2307 "%llu with %u records\n",
2308 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2309 (unsigned long long)le64_to_cpu(eb->h_blkno),
2310 le16_to_cpu(el->l_next_free_rec));
2312 ocfs2_journal_dirty(handle, bh);
2313 ocfs2_remove_from_cache(inode, bh);
2317 el->l_next_free_rec = 0;
2318 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2320 ocfs2_journal_dirty(handle, bh);
2322 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2326 ocfs2_remove_from_cache(inode, bh);
2330 static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2331 struct ocfs2_path *left_path,
2332 struct ocfs2_path *right_path,
2334 struct ocfs2_cached_dealloc_ctxt *dealloc)
2337 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2338 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2339 struct ocfs2_extent_list *el;
2340 struct ocfs2_extent_block *eb;
2342 el = path_leaf_el(left_path);
2344 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2346 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2347 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2350 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2352 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2353 le16_add_cpu(&root_el->l_next_free_rec, -1);
2355 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2356 eb->h_next_leaf_blk = 0;
2358 ocfs2_journal_dirty(handle, root_bh);
2359 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2361 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2365 static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2366 struct ocfs2_path *left_path,
2367 struct ocfs2_path *right_path,
2369 struct ocfs2_cached_dealloc_ctxt *dealloc,
2371 struct ocfs2_extent_tree *et)
2373 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2374 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2375 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2376 struct ocfs2_extent_block *eb;
2380 right_leaf_el = path_leaf_el(right_path);
2381 left_leaf_el = path_leaf_el(left_path);
2382 root_bh = left_path->p_node[subtree_index].bh;
2383 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2385 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2388 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2389 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2391 * It's legal for us to proceed if the right leaf is
2392 * the rightmost one and it has an empty extent. There
2393 * are two cases to handle - whether the leaf will be
2394 * empty after removal or not. If the leaf isn't empty
2395 * then just remove the empty extent up front. The
2396 * next block will handle empty leaves by flagging
2399 * Non rightmost leaves will throw -EAGAIN and the
2400 * caller can manually move the subtree and retry.
2403 if (eb->h_next_leaf_blk != 0ULL)
2406 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2407 ret = ocfs2_journal_access(handle, inode,
2408 path_leaf_bh(right_path),
2409 OCFS2_JOURNAL_ACCESS_WRITE);
2415 ocfs2_remove_empty_extent(right_leaf_el);
2417 right_has_empty = 1;
2420 if (eb->h_next_leaf_blk == 0ULL &&
2421 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2423 * We have to update i_last_eb_blk during the meta
2426 ret = ocfs2_journal_access(handle, inode, et_root_bh,
2427 OCFS2_JOURNAL_ACCESS_WRITE);
2433 del_right_subtree = 1;
2437 * Getting here with an empty extent in the right path implies
2438 * that it's the rightmost path and will be deleted.
2440 BUG_ON(right_has_empty && !del_right_subtree);
2442 ret = ocfs2_journal_access(handle, inode, root_bh,
2443 OCFS2_JOURNAL_ACCESS_WRITE);
2449 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2450 ret = ocfs2_journal_access(handle, inode,
2451 right_path->p_node[i].bh,
2452 OCFS2_JOURNAL_ACCESS_WRITE);
2458 ret = ocfs2_journal_access(handle, inode,
2459 left_path->p_node[i].bh,
2460 OCFS2_JOURNAL_ACCESS_WRITE);
2467 if (!right_has_empty) {
2469 * Only do this if we're moving a real
2470 * record. Otherwise, the action is delayed until
2471 * after removal of the right path in which case we
2472 * can do a simple shift to remove the empty extent.
2474 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2475 memset(&right_leaf_el->l_recs[0], 0,
2476 sizeof(struct ocfs2_extent_rec));
2478 if (eb->h_next_leaf_blk == 0ULL) {
2480 * Move recs over to get rid of empty extent, decrease
2481 * next_free. This is allowed to remove the last
2482 * extent in our leaf (setting l_next_free_rec to
2483 * zero) - the delete code below won't care.
2485 ocfs2_remove_empty_extent(right_leaf_el);
2488 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2491 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2495 if (del_right_subtree) {
2496 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2497 subtree_index, dealloc);
2498 ocfs2_update_edge_lengths(inode, handle, left_path);
2500 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2501 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2504 * Removal of the extent in the left leaf was skipped
2505 * above so we could delete the right path
2508 if (right_has_empty)
2509 ocfs2_remove_empty_extent(left_leaf_el);
2511 ret = ocfs2_journal_dirty(handle, et_root_bh);
2517 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2525 * Given a full path, determine what cpos value would return us a path
2526 * containing the leaf immediately to the right of the current one.
2528 * Will return zero if the path passed in is already the rightmost path.
2530 * This looks similar, but is subtly different to
2531 * ocfs2_find_cpos_for_left_leaf().
2533 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2534 struct ocfs2_path *path, u32 *cpos)
2538 struct ocfs2_extent_list *el;
2542 if (path->p_tree_depth == 0)
2545 blkno = path_leaf_bh(path)->b_blocknr;
2547 /* Start at the tree node just above the leaf and work our way up. */
2548 i = path->p_tree_depth - 1;
2552 el = path->p_node[i].el;
2555 * Find the extent record just after the one in our
2558 next_free = le16_to_cpu(el->l_next_free_rec);
2559 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2560 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2561 if (j == (next_free - 1)) {
2564 * We've determined that the
2565 * path specified is already
2566 * the rightmost one - return a
2572 * The rightmost record points to our
2573 * leaf - we need to travel up the
2579 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2585 * If we got here, we never found a valid node where
2586 * the tree indicated one should be.
2589 "Invalid extent tree at extent block %llu\n",
2590 (unsigned long long)blkno);
2595 blkno = path->p_node[i].bh->b_blocknr;
2603 static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2605 struct buffer_head *bh,
2606 struct ocfs2_extent_list *el)
2610 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2613 ret = ocfs2_journal_access(handle, inode, bh,
2614 OCFS2_JOURNAL_ACCESS_WRITE);
2620 ocfs2_remove_empty_extent(el);
2622 ret = ocfs2_journal_dirty(handle, bh);
2630 static int __ocfs2_rotate_tree_left(struct inode *inode,
2631 handle_t *handle, int orig_credits,
2632 struct ocfs2_path *path,
2633 struct ocfs2_cached_dealloc_ctxt *dealloc,
2634 struct ocfs2_path **empty_extent_path,
2635 struct ocfs2_extent_tree *et)
2637 int ret, subtree_root, deleted;
2639 struct ocfs2_path *left_path = NULL;
2640 struct ocfs2_path *right_path = NULL;
2642 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2644 *empty_extent_path = NULL;
2646 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2653 left_path = ocfs2_new_path(path_root_bh(path),
2654 path_root_el(path));
2661 ocfs2_cp_path(left_path, path);
2663 right_path = ocfs2_new_path(path_root_bh(path),
2664 path_root_el(path));
2671 while (right_cpos) {
2672 ret = ocfs2_find_path(inode, right_path, right_cpos);
2678 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2681 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2683 (unsigned long long)
2684 right_path->p_node[subtree_root].bh->b_blocknr,
2685 right_path->p_tree_depth);
2687 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2688 orig_credits, left_path);
2695 * Caller might still want to make changes to the
2696 * tree root, so re-add it to the journal here.
2698 ret = ocfs2_journal_access(handle, inode,
2699 path_root_bh(left_path),
2700 OCFS2_JOURNAL_ACCESS_WRITE);
2706 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2707 right_path, subtree_root,
2708 dealloc, &deleted, et);
2709 if (ret == -EAGAIN) {
2711 * The rotation has to temporarily stop due to
2712 * the right subtree having an empty
2713 * extent. Pass it back to the caller for a
2716 *empty_extent_path = right_path;
2726 * The subtree rotate might have removed records on
2727 * the rightmost edge. If so, then rotation is
2733 ocfs2_mv_path(left_path, right_path);
2735 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2744 ocfs2_free_path(right_path);
2745 ocfs2_free_path(left_path);
2750 static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2751 struct ocfs2_path *path,
2752 struct ocfs2_cached_dealloc_ctxt *dealloc,
2753 struct ocfs2_extent_tree *et)
2755 int ret, subtree_index;
2757 struct ocfs2_path *left_path = NULL;
2758 struct ocfs2_extent_block *eb;
2759 struct ocfs2_extent_list *el;
2762 ret = ocfs2_et_sanity_check(inode, et);
2766 * There's two ways we handle this depending on
2767 * whether path is the only existing one.
2769 ret = ocfs2_extend_rotate_transaction(handle, 0,
2770 handle->h_buffer_credits,
2777 ret = ocfs2_journal_access_path(inode, handle, path);
2783 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2791 * We have a path to the left of this one - it needs
2794 left_path = ocfs2_new_path(path_root_bh(path),
2795 path_root_el(path));
2802 ret = ocfs2_find_path(inode, left_path, cpos);
2808 ret = ocfs2_journal_access_path(inode, handle, left_path);
2814 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2816 ocfs2_unlink_subtree(inode, handle, left_path, path,
2817 subtree_index, dealloc);
2818 ocfs2_update_edge_lengths(inode, handle, left_path);
2820 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2821 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2824 * 'path' is also the leftmost path which
2825 * means it must be the only one. This gets
2826 * handled differently because we want to
2827 * revert the inode back to having extents
2830 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2832 el = et->et_root_el;
2833 el->l_tree_depth = 0;
2834 el->l_next_free_rec = 0;
2835 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2837 ocfs2_et_set_last_eb_blk(et, 0);
2840 ocfs2_journal_dirty(handle, path_root_bh(path));
2843 ocfs2_free_path(left_path);
2848 * Left rotation of btree records.
2850 * In many ways, this is (unsurprisingly) the opposite of right
2851 * rotation. We start at some non-rightmost path containing an empty
2852 * extent in the leaf block. The code works its way to the rightmost
2853 * path by rotating records to the left in every subtree.
2855 * This is used by any code which reduces the number of extent records
2856 * in a leaf. After removal, an empty record should be placed in the
2857 * leftmost list position.
2859 * This won't handle a length update of the rightmost path records if
2860 * the rightmost tree leaf record is removed so the caller is
2861 * responsible for detecting and correcting that.
2863 static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2864 struct ocfs2_path *path,
2865 struct ocfs2_cached_dealloc_ctxt *dealloc,
2866 struct ocfs2_extent_tree *et)
2868 int ret, orig_credits = handle->h_buffer_credits;
2869 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2870 struct ocfs2_extent_block *eb;
2871 struct ocfs2_extent_list *el;
2873 el = path_leaf_el(path);
2874 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2877 if (path->p_tree_depth == 0) {
2878 rightmost_no_delete:
2880 * Inline extents. This is trivially handled, so do
2883 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2885 path_leaf_el(path));
2892 * Handle rightmost branch now. There's several cases:
2893 * 1) simple rotation leaving records in there. That's trivial.
2894 * 2) rotation requiring a branch delete - there's no more
2895 * records left. Two cases of this:
2896 * a) There are branches to the left.
2897 * b) This is also the leftmost (the only) branch.
2899 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2900 * 2a) we need the left branch so that we can update it with the unlink
2901 * 2b) we need to bring the inode back to inline extents.
2904 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2906 if (eb->h_next_leaf_blk == 0) {
2908 * This gets a bit tricky if we're going to delete the
2909 * rightmost path. Get the other cases out of the way
2912 if (le16_to_cpu(el->l_next_free_rec) > 1)
2913 goto rightmost_no_delete;
2915 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2917 ocfs2_error(inode->i_sb,
2918 "Inode %llu has empty extent block at %llu",
2919 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2920 (unsigned long long)le64_to_cpu(eb->h_blkno));
2925 * XXX: The caller can not trust "path" any more after
2926 * this as it will have been deleted. What do we do?
2928 * In theory the rotate-for-merge code will never get
2929 * here because it'll always ask for a rotate in a
2933 ret = ocfs2_remove_rightmost_path(inode, handle, path,
2941 * Now we can loop, remembering the path we get from -EAGAIN
2942 * and restarting from there.
2945 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
2946 dealloc, &restart_path, et);
2947 if (ret && ret != -EAGAIN) {
2952 while (ret == -EAGAIN) {
2953 tmp_path = restart_path;
2954 restart_path = NULL;
2956 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2959 if (ret && ret != -EAGAIN) {
2964 ocfs2_free_path(tmp_path);
2972 ocfs2_free_path(tmp_path);
2973 ocfs2_free_path(restart_path);
2977 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2980 struct ocfs2_extent_rec *rec = &el->l_recs[index];
2983 if (rec->e_leaf_clusters == 0) {
2985 * We consumed all of the merged-from record. An empty
2986 * extent cannot exist anywhere but the 1st array
2987 * position, so move things over if the merged-from
2988 * record doesn't occupy that position.
2990 * This creates a new empty extent so the caller
2991 * should be smart enough to have removed any existing
2995 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
2996 size = index * sizeof(struct ocfs2_extent_rec);
2997 memmove(&el->l_recs[1], &el->l_recs[0], size);
3001 * Always memset - the caller doesn't check whether it
3002 * created an empty extent, so there could be junk in
3005 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3009 static int ocfs2_get_right_path(struct inode *inode,
3010 struct ocfs2_path *left_path,
3011 struct ocfs2_path **ret_right_path)
3015 struct ocfs2_path *right_path = NULL;
3016 struct ocfs2_extent_list *left_el;
3018 *ret_right_path = NULL;
3020 /* This function shouldn't be called for non-trees. */
3021 BUG_ON(left_path->p_tree_depth == 0);
3023 left_el = path_leaf_el(left_path);
3024 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3026 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3033 /* This function shouldn't be called for the rightmost leaf. */
3034 BUG_ON(right_cpos == 0);
3036 right_path = ocfs2_new_path(path_root_bh(left_path),
3037 path_root_el(left_path));
3044 ret = ocfs2_find_path(inode, right_path, right_cpos);
3050 *ret_right_path = right_path;
3053 ocfs2_free_path(right_path);
3058 * Remove split_rec clusters from the record at index and merge them
3059 * onto the beginning of the record "next" to it.
3060 * For index < l_count - 1, the next means the extent rec at index + 1.
3061 * For index == l_count - 1, the "next" means the 1st extent rec of the
3062 * next extent block.
3064 static int ocfs2_merge_rec_right(struct inode *inode,
3065 struct ocfs2_path *left_path,
3067 struct ocfs2_extent_rec *split_rec,
3070 int ret, next_free, i;
3071 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3072 struct ocfs2_extent_rec *left_rec;
3073 struct ocfs2_extent_rec *right_rec;
3074 struct ocfs2_extent_list *right_el;
3075 struct ocfs2_path *right_path = NULL;
3076 int subtree_index = 0;
3077 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3078 struct buffer_head *bh = path_leaf_bh(left_path);
3079 struct buffer_head *root_bh = NULL;
3081 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3082 left_rec = &el->l_recs[index];
3084 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3085 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3086 /* we meet with a cross extent block merge. */
3087 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3093 right_el = path_leaf_el(right_path);
3094 next_free = le16_to_cpu(right_el->l_next_free_rec);
3095 BUG_ON(next_free <= 0);
3096 right_rec = &right_el->l_recs[0];
3097 if (ocfs2_is_empty_extent(right_rec)) {
3098 BUG_ON(next_free <= 1);
3099 right_rec = &right_el->l_recs[1];
3102 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3103 le16_to_cpu(left_rec->e_leaf_clusters) !=
3104 le32_to_cpu(right_rec->e_cpos));
3106 subtree_index = ocfs2_find_subtree_root(inode,
3107 left_path, right_path);
3109 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3110 handle->h_buffer_credits,
3117 root_bh = left_path->p_node[subtree_index].bh;
3118 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3120 ret = ocfs2_journal_access(handle, inode, root_bh,
3121 OCFS2_JOURNAL_ACCESS_WRITE);
3127 for (i = subtree_index + 1;
3128 i < path_num_items(right_path); i++) {
3129 ret = ocfs2_journal_access(handle, inode,
3130 right_path->p_node[i].bh,
3131 OCFS2_JOURNAL_ACCESS_WRITE);
3137 ret = ocfs2_journal_access(handle, inode,
3138 left_path->p_node[i].bh,
3139 OCFS2_JOURNAL_ACCESS_WRITE);
3147 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3148 right_rec = &el->l_recs[index + 1];
3151 ret = ocfs2_journal_access(handle, inode, bh,
3152 OCFS2_JOURNAL_ACCESS_WRITE);
3158 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3160 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3161 le64_add_cpu(&right_rec->e_blkno,
3162 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3163 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3165 ocfs2_cleanup_merge(el, index);
3167 ret = ocfs2_journal_dirty(handle, bh);
3172 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3176 ocfs2_complete_edge_insert(inode, handle, left_path,
3177 right_path, subtree_index);
3181 ocfs2_free_path(right_path);
3185 static int ocfs2_get_left_path(struct inode *inode,
3186 struct ocfs2_path *right_path,
3187 struct ocfs2_path **ret_left_path)
3191 struct ocfs2_path *left_path = NULL;
3193 *ret_left_path = NULL;
3195 /* This function shouldn't be called for non-trees. */
3196 BUG_ON(right_path->p_tree_depth == 0);
3198 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3199 right_path, &left_cpos);
3205 /* This function shouldn't be called for the leftmost leaf. */
3206 BUG_ON(left_cpos == 0);
3208 left_path = ocfs2_new_path(path_root_bh(right_path),
3209 path_root_el(right_path));
3216 ret = ocfs2_find_path(inode, left_path, left_cpos);
3222 *ret_left_path = left_path;
3225 ocfs2_free_path(left_path);
3230 * Remove split_rec clusters from the record at index and merge them
3231 * onto the tail of the record "before" it.
3232 * For index > 0, the "before" means the extent rec at index - 1.
3234 * For index == 0, the "before" means the last record of the previous
3235 * extent block. And there is also a situation that we may need to
3236 * remove the rightmost leaf extent block in the right_path and change
3237 * the right path to indicate the new rightmost path.
3239 static int ocfs2_merge_rec_left(struct inode *inode,
3240 struct ocfs2_path *right_path,
3242 struct ocfs2_extent_rec *split_rec,
3243 struct ocfs2_cached_dealloc_ctxt *dealloc,
3244 struct ocfs2_extent_tree *et,
3247 int ret, i, subtree_index = 0, has_empty_extent = 0;
3248 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3249 struct ocfs2_extent_rec *left_rec;
3250 struct ocfs2_extent_rec *right_rec;
3251 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3252 struct buffer_head *bh = path_leaf_bh(right_path);
3253 struct buffer_head *root_bh = NULL;
3254 struct ocfs2_path *left_path = NULL;
3255 struct ocfs2_extent_list *left_el;
3259 right_rec = &el->l_recs[index];
3261 /* we meet with a cross extent block merge. */
3262 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3268 left_el = path_leaf_el(left_path);
3269 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3270 le16_to_cpu(left_el->l_count));
3272 left_rec = &left_el->l_recs[
3273 le16_to_cpu(left_el->l_next_free_rec) - 1];
3274 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3275 le16_to_cpu(left_rec->e_leaf_clusters) !=
3276 le32_to_cpu(split_rec->e_cpos));
3278 subtree_index = ocfs2_find_subtree_root(inode,
3279 left_path, right_path);
3281 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3282 handle->h_buffer_credits,
3289 root_bh = left_path->p_node[subtree_index].bh;
3290 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3292 ret = ocfs2_journal_access(handle, inode, root_bh,
3293 OCFS2_JOURNAL_ACCESS_WRITE);
3299 for (i = subtree_index + 1;
3300 i < path_num_items(right_path); i++) {
3301 ret = ocfs2_journal_access(handle, inode,
3302 right_path->p_node[i].bh,
3303 OCFS2_JOURNAL_ACCESS_WRITE);
3309 ret = ocfs2_journal_access(handle, inode,
3310 left_path->p_node[i].bh,
3311 OCFS2_JOURNAL_ACCESS_WRITE);
3318 left_rec = &el->l_recs[index - 1];
3319 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3320 has_empty_extent = 1;
3323 ret = ocfs2_journal_access(handle, inode, bh,
3324 OCFS2_JOURNAL_ACCESS_WRITE);
3330 if (has_empty_extent && index == 1) {
3332 * The easy case - we can just plop the record right in.
3334 *left_rec = *split_rec;
3336 has_empty_extent = 0;
3338 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3340 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3341 le64_add_cpu(&right_rec->e_blkno,
3342 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3343 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3345 ocfs2_cleanup_merge(el, index);
3347 ret = ocfs2_journal_dirty(handle, bh);
3352 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3357 * In the situation that the right_rec is empty and the extent
3358 * block is empty also, ocfs2_complete_edge_insert can't handle
3359 * it and we need to delete the right extent block.
3361 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3362 le16_to_cpu(el->l_next_free_rec) == 1) {
3364 ret = ocfs2_remove_rightmost_path(inode, handle,
3372 /* Now the rightmost extent block has been deleted.
3373 * So we use the new rightmost path.
3375 ocfs2_mv_path(right_path, left_path);
3378 ocfs2_complete_edge_insert(inode, handle, left_path,
3379 right_path, subtree_index);
3383 ocfs2_free_path(left_path);
3387 static int ocfs2_try_to_merge_extent(struct inode *inode,
3389 struct ocfs2_path *path,
3391 struct ocfs2_extent_rec *split_rec,
3392 struct ocfs2_cached_dealloc_ctxt *dealloc,
3393 struct ocfs2_merge_ctxt *ctxt,
3394 struct ocfs2_extent_tree *et)
3398 struct ocfs2_extent_list *el = path_leaf_el(path);
3399 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3401 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3403 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3405 * The merge code will need to create an empty
3406 * extent to take the place of the newly
3407 * emptied slot. Remove any pre-existing empty
3408 * extents - having more than one in a leaf is
3411 ret = ocfs2_rotate_tree_left(inode, handle, path,
3418 rec = &el->l_recs[split_index];
3421 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3423 * Left-right contig implies this.
3425 BUG_ON(!ctxt->c_split_covers_rec);
3428 * Since the leftright insert always covers the entire
3429 * extent, this call will delete the insert record
3430 * entirely, resulting in an empty extent record added to
3433 * Since the adding of an empty extent shifts
3434 * everything back to the right, there's no need to
3435 * update split_index here.
3437 * When the split_index is zero, we need to merge it to the
3438 * prevoius extent block. It is more efficient and easier
3439 * if we do merge_right first and merge_left later.
3441 ret = ocfs2_merge_rec_right(inode, path,
3450 * We can only get this from logic error above.
3452 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3454 /* The merge left us with an empty extent, remove it. */
3455 ret = ocfs2_rotate_tree_left(inode, handle, path,
3462 rec = &el->l_recs[split_index];
3465 * Note that we don't pass split_rec here on purpose -
3466 * we've merged it into the rec already.
3468 ret = ocfs2_merge_rec_left(inode, path,
3478 ret = ocfs2_rotate_tree_left(inode, handle, path,
3481 * Error from this last rotate is not critical, so
3482 * print but don't bubble it up.
3489 * Merge a record to the left or right.
3491 * 'contig_type' is relative to the existing record,
3492 * so for example, if we're "right contig", it's to
3493 * the record on the left (hence the left merge).
3495 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3496 ret = ocfs2_merge_rec_left(inode,
3506 ret = ocfs2_merge_rec_right(inode,
3516 if (ctxt->c_split_covers_rec) {
3518 * The merge may have left an empty extent in
3519 * our leaf. Try to rotate it away.
3521 ret = ocfs2_rotate_tree_left(inode, handle, path,
3533 static void ocfs2_subtract_from_rec(struct super_block *sb,
3534 enum ocfs2_split_type split,
3535 struct ocfs2_extent_rec *rec,
3536 struct ocfs2_extent_rec *split_rec)
3540 len_blocks = ocfs2_clusters_to_blocks(sb,
3541 le16_to_cpu(split_rec->e_leaf_clusters));
3543 if (split == SPLIT_LEFT) {
3545 * Region is on the left edge of the existing
3548 le32_add_cpu(&rec->e_cpos,
3549 le16_to_cpu(split_rec->e_leaf_clusters));
3550 le64_add_cpu(&rec->e_blkno, len_blocks);
3551 le16_add_cpu(&rec->e_leaf_clusters,
3552 -le16_to_cpu(split_rec->e_leaf_clusters));
3555 * Region is on the right edge of the existing
3558 le16_add_cpu(&rec->e_leaf_clusters,
3559 -le16_to_cpu(split_rec->e_leaf_clusters));
3564 * Do the final bits of extent record insertion at the target leaf
3565 * list. If this leaf is part of an allocation tree, it is assumed
3566 * that the tree above has been prepared.
3568 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3569 struct ocfs2_extent_list *el,
3570 struct ocfs2_insert_type *insert,
3571 struct inode *inode)
3573 int i = insert->ins_contig_index;
3575 struct ocfs2_extent_rec *rec;
3577 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3579 if (insert->ins_split != SPLIT_NONE) {
3580 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3582 rec = &el->l_recs[i];
3583 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3589 * Contiguous insert - either left or right.
3591 if (insert->ins_contig != CONTIG_NONE) {
3592 rec = &el->l_recs[i];
3593 if (insert->ins_contig == CONTIG_LEFT) {
3594 rec->e_blkno = insert_rec->e_blkno;
3595 rec->e_cpos = insert_rec->e_cpos;
3597 le16_add_cpu(&rec->e_leaf_clusters,
3598 le16_to_cpu(insert_rec->e_leaf_clusters));
3603 * Handle insert into an empty leaf.
3605 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3606 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3607 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3608 el->l_recs[0] = *insert_rec;
3609 el->l_next_free_rec = cpu_to_le16(1);
3616 if (insert->ins_appending == APPEND_TAIL) {
3617 i = le16_to_cpu(el->l_next_free_rec) - 1;
3618 rec = &el->l_recs[i];
3619 range = le32_to_cpu(rec->e_cpos)
3620 + le16_to_cpu(rec->e_leaf_clusters);
3621 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3623 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3624 le16_to_cpu(el->l_count),
3625 "inode %lu, depth %u, count %u, next free %u, "
3626 "rec.cpos %u, rec.clusters %u, "
3627 "insert.cpos %u, insert.clusters %u\n",
3629 le16_to_cpu(el->l_tree_depth),
3630 le16_to_cpu(el->l_count),
3631 le16_to_cpu(el->l_next_free_rec),
3632 le32_to_cpu(el->l_recs[i].e_cpos),
3633 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3634 le32_to_cpu(insert_rec->e_cpos),
3635 le16_to_cpu(insert_rec->e_leaf_clusters));
3637 el->l_recs[i] = *insert_rec;
3638 le16_add_cpu(&el->l_next_free_rec, 1);
3644 * Ok, we have to rotate.
3646 * At this point, it is safe to assume that inserting into an
3647 * empty leaf and appending to a leaf have both been handled
3650 * This leaf needs to have space, either by the empty 1st
3651 * extent record, or by virtue of an l_next_rec < l_count.
3653 ocfs2_rotate_leaf(el, insert_rec);
3656 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3658 struct ocfs2_path *path,
3659 struct ocfs2_extent_rec *insert_rec)
3661 int ret, i, next_free;
3662 struct buffer_head *bh;
3663 struct ocfs2_extent_list *el;
3664 struct ocfs2_extent_rec *rec;
3667 * Update everything except the leaf block.
3669 for (i = 0; i < path->p_tree_depth; i++) {
3670 bh = path->p_node[i].bh;
3671 el = path->p_node[i].el;
3673 next_free = le16_to_cpu(el->l_next_free_rec);
3674 if (next_free == 0) {
3675 ocfs2_error(inode->i_sb,
3676 "Dinode %llu has a bad extent list",
3677 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3682 rec = &el->l_recs[next_free - 1];
3684 rec->e_int_clusters = insert_rec->e_cpos;
3685 le32_add_cpu(&rec->e_int_clusters,
3686 le16_to_cpu(insert_rec->e_leaf_clusters));
3687 le32_add_cpu(&rec->e_int_clusters,
3688 -le32_to_cpu(rec->e_cpos));
3690 ret = ocfs2_journal_dirty(handle, bh);
3697 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3698 struct ocfs2_extent_rec *insert_rec,
3699 struct ocfs2_path *right_path,
3700 struct ocfs2_path **ret_left_path)
3703 struct ocfs2_extent_list *el;
3704 struct ocfs2_path *left_path = NULL;
3706 *ret_left_path = NULL;
3709 * This shouldn't happen for non-trees. The extent rec cluster
3710 * count manipulation below only works for interior nodes.
3712 BUG_ON(right_path->p_tree_depth == 0);
3715 * If our appending insert is at the leftmost edge of a leaf,
3716 * then we might need to update the rightmost records of the
3719 el = path_leaf_el(right_path);
3720 next_free = le16_to_cpu(el->l_next_free_rec);
3721 if (next_free == 0 ||
3722 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3725 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3732 mlog(0, "Append may need a left path update. cpos: %u, "
3733 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3737 * No need to worry if the append is already in the
3741 left_path = ocfs2_new_path(path_root_bh(right_path),
3742 path_root_el(right_path));
3749 ret = ocfs2_find_path(inode, left_path, left_cpos);
3756 * ocfs2_insert_path() will pass the left_path to the
3762 ret = ocfs2_journal_access_path(inode, handle, right_path);
3768 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
3770 *ret_left_path = left_path;
3774 ocfs2_free_path(left_path);
3779 static void ocfs2_split_record(struct inode *inode,
3780 struct ocfs2_path *left_path,
3781 struct ocfs2_path *right_path,
3782 struct ocfs2_extent_rec *split_rec,
3783 enum ocfs2_split_type split)
3786 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3787 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3788 struct ocfs2_extent_rec *rec, *tmprec;
3790 right_el = path_leaf_el(right_path);;
3792 left_el = path_leaf_el(left_path);
3795 insert_el = right_el;
3796 index = ocfs2_search_extent_list(el, cpos);
3798 if (index == 0 && left_path) {
3799 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3802 * This typically means that the record
3803 * started in the left path but moved to the
3804 * right as a result of rotation. We either
3805 * move the existing record to the left, or we
3806 * do the later insert there.
3808 * In this case, the left path should always
3809 * exist as the rotate code will have passed
3810 * it back for a post-insert update.
3813 if (split == SPLIT_LEFT) {
3815 * It's a left split. Since we know
3816 * that the rotate code gave us an
3817 * empty extent in the left path, we
3818 * can just do the insert there.
3820 insert_el = left_el;
3823 * Right split - we have to move the
3824 * existing record over to the left
3825 * leaf. The insert will be into the
3826 * newly created empty extent in the
3829 tmprec = &right_el->l_recs[index];
3830 ocfs2_rotate_leaf(left_el, tmprec);
3833 memset(tmprec, 0, sizeof(*tmprec));
3834 index = ocfs2_search_extent_list(left_el, cpos);
3835 BUG_ON(index == -1);
3840 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3842 * Left path is easy - we can just allow the insert to
3846 insert_el = left_el;
3847 index = ocfs2_search_extent_list(el, cpos);
3848 BUG_ON(index == -1);
3851 rec = &el->l_recs[index];
3852 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3853 ocfs2_rotate_leaf(insert_el, split_rec);
3857 * This function only does inserts on an allocation b-tree. For tree
3858 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3860 * right_path is the path we want to do the actual insert
3861 * in. left_path should only be passed in if we need to update that
3862 * portion of the tree after an edge insert.
3864 static int ocfs2_insert_path(struct inode *inode,
3866 struct ocfs2_path *left_path,
3867 struct ocfs2_path *right_path,
3868 struct ocfs2_extent_rec *insert_rec,
3869 struct ocfs2_insert_type *insert)
3871 int ret, subtree_index;
3872 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
3875 int credits = handle->h_buffer_credits;
3878 * There's a chance that left_path got passed back to
3879 * us without being accounted for in the
3880 * journal. Extend our transaction here to be sure we
3881 * can change those blocks.
3883 credits += left_path->p_tree_depth;
3885 ret = ocfs2_extend_trans(handle, credits);
3891 ret = ocfs2_journal_access_path(inode, handle, left_path);
3899 * Pass both paths to the journal. The majority of inserts
3900 * will be touching all components anyway.
3902 ret = ocfs2_journal_access_path(inode, handle, right_path);
3908 if (insert->ins_split != SPLIT_NONE) {
3910 * We could call ocfs2_insert_at_leaf() for some types
3911 * of splits, but it's easier to just let one separate
3912 * function sort it all out.
3914 ocfs2_split_record(inode, left_path, right_path,
3915 insert_rec, insert->ins_split);
3918 * Split might have modified either leaf and we don't
3919 * have a guarantee that the later edge insert will
3920 * dirty this for us.
3923 ret = ocfs2_journal_dirty(handle,
3924 path_leaf_bh(left_path));
3928 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3931 ret = ocfs2_journal_dirty(handle, leaf_bh);
3937 * The rotate code has indicated that we need to fix
3938 * up portions of the tree after the insert.
3940 * XXX: Should we extend the transaction here?
3942 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3944 ocfs2_complete_edge_insert(inode, handle, left_path,
3945 right_path, subtree_index);
3953 static int ocfs2_do_insert_extent(struct inode *inode,
3955 struct ocfs2_extent_tree *et,
3956 struct ocfs2_extent_rec *insert_rec,
3957 struct ocfs2_insert_type *type)
3959 int ret, rotate = 0;
3961 struct ocfs2_path *right_path = NULL;
3962 struct ocfs2_path *left_path = NULL;
3963 struct ocfs2_extent_list *el;
3965 el = et->et_root_el;
3967 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
3968 OCFS2_JOURNAL_ACCESS_WRITE);
3974 if (le16_to_cpu(el->l_tree_depth) == 0) {
3975 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3976 goto out_update_clusters;
3979 right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
3987 * Determine the path to start with. Rotations need the
3988 * rightmost path, everything else can go directly to the
3991 cpos = le32_to_cpu(insert_rec->e_cpos);
3992 if (type->ins_appending == APPEND_NONE &&
3993 type->ins_contig == CONTIG_NONE) {
3998 ret = ocfs2_find_path(inode, right_path, cpos);
4005 * Rotations and appends need special treatment - they modify
4006 * parts of the tree's above them.
4008 * Both might pass back a path immediate to the left of the
4009 * one being inserted to. This will be cause
4010 * ocfs2_insert_path() to modify the rightmost records of
4011 * left_path to account for an edge insert.
4013 * XXX: When modifying this code, keep in mind that an insert
4014 * can wind up skipping both of these two special cases...
4017 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
4018 le32_to_cpu(insert_rec->e_cpos),
4019 right_path, &left_path);
4026 * ocfs2_rotate_tree_right() might have extended the
4027 * transaction without re-journaling our tree root.
4029 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
4030 OCFS2_JOURNAL_ACCESS_WRITE);
4035 } else if (type->ins_appending == APPEND_TAIL
4036 && type->ins_contig != CONTIG_LEFT) {
4037 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4038 right_path, &left_path);
4045 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4052 out_update_clusters:
4053 if (type->ins_split == SPLIT_NONE)
4054 ocfs2_et_update_clusters(inode, et,
4055 le16_to_cpu(insert_rec->e_leaf_clusters));
4057 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
4062 ocfs2_free_path(left_path);
4063 ocfs2_free_path(right_path);
4068 static enum ocfs2_contig_type
4069 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
4070 struct ocfs2_extent_list *el, int index,
4071 struct ocfs2_extent_rec *split_rec)
4074 enum ocfs2_contig_type ret = CONTIG_NONE;
4075 u32 left_cpos, right_cpos;
4076 struct ocfs2_extent_rec *rec = NULL;
4077 struct ocfs2_extent_list *new_el;
4078 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4079 struct buffer_head *bh;
4080 struct ocfs2_extent_block *eb;
4083 rec = &el->l_recs[index - 1];
4084 } else if (path->p_tree_depth > 0) {
4085 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4090 if (left_cpos != 0) {
4091 left_path = ocfs2_new_path(path_root_bh(path),
4092 path_root_el(path));
4096 status = ocfs2_find_path(inode, left_path, left_cpos);
4100 new_el = path_leaf_el(left_path);
4102 if (le16_to_cpu(new_el->l_next_free_rec) !=
4103 le16_to_cpu(new_el->l_count)) {
4104 bh = path_leaf_bh(left_path);
4105 eb = (struct ocfs2_extent_block *)bh->b_data;
4106 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4110 rec = &new_el->l_recs[
4111 le16_to_cpu(new_el->l_next_free_rec) - 1];
4116 * We're careful to check for an empty extent record here -
4117 * the merge code will know what to do if it sees one.
4120 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4121 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4124 ret = ocfs2_extent_contig(inode, rec, split_rec);
4129 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4130 rec = &el->l_recs[index + 1];
4131 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4132 path->p_tree_depth > 0) {
4133 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4138 if (right_cpos == 0)
4141 right_path = ocfs2_new_path(path_root_bh(path),
4142 path_root_el(path));
4146 status = ocfs2_find_path(inode, right_path, right_cpos);
4150 new_el = path_leaf_el(right_path);
4151 rec = &new_el->l_recs[0];
4152 if (ocfs2_is_empty_extent(rec)) {
4153 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4154 bh = path_leaf_bh(right_path);
4155 eb = (struct ocfs2_extent_block *)bh->b_data;
4156 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4160 rec = &new_el->l_recs[1];
4165 enum ocfs2_contig_type contig_type;
4167 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4169 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4170 ret = CONTIG_LEFTRIGHT;
4171 else if (ret == CONTIG_NONE)
4177 ocfs2_free_path(left_path);
4179 ocfs2_free_path(right_path);
4184 static void ocfs2_figure_contig_type(struct inode *inode,
4185 struct ocfs2_insert_type *insert,
4186 struct ocfs2_extent_list *el,
4187 struct ocfs2_extent_rec *insert_rec,
4188 struct ocfs2_extent_tree *et)
4191 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4193 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4195 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4196 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4198 if (contig_type != CONTIG_NONE) {
4199 insert->ins_contig_index = i;
4203 insert->ins_contig = contig_type;
4205 if (insert->ins_contig != CONTIG_NONE) {
4206 struct ocfs2_extent_rec *rec =
4207 &el->l_recs[insert->ins_contig_index];
4208 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4209 le16_to_cpu(insert_rec->e_leaf_clusters);
4212 * Caller might want us to limit the size of extents, don't
4213 * calculate contiguousness if we might exceed that limit.
4215 if (et->et_max_leaf_clusters &&
4216 (len > et->et_max_leaf_clusters))
4217 insert->ins_contig = CONTIG_NONE;
4222 * This should only be called against the righmost leaf extent list.
4224 * ocfs2_figure_appending_type() will figure out whether we'll have to
4225 * insert at the tail of the rightmost leaf.
4227 * This should also work against the root extent list for tree's with 0
4228 * depth. If we consider the root extent list to be the rightmost leaf node
4229 * then the logic here makes sense.
4231 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4232 struct ocfs2_extent_list *el,
4233 struct ocfs2_extent_rec *insert_rec)
4236 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4237 struct ocfs2_extent_rec *rec;
4239 insert->ins_appending = APPEND_NONE;
4241 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4243 if (!el->l_next_free_rec)
4244 goto set_tail_append;
4246 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4247 /* Were all records empty? */
4248 if (le16_to_cpu(el->l_next_free_rec) == 1)
4249 goto set_tail_append;
4252 i = le16_to_cpu(el->l_next_free_rec) - 1;
4253 rec = &el->l_recs[i];
4256 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4257 goto set_tail_append;
4262 insert->ins_appending = APPEND_TAIL;
4266 * Helper function called at the begining of an insert.
4268 * This computes a few things that are commonly used in the process of
4269 * inserting into the btree:
4270 * - Whether the new extent is contiguous with an existing one.
4271 * - The current tree depth.
4272 * - Whether the insert is an appending one.
4273 * - The total # of free records in the tree.
4275 * All of the information is stored on the ocfs2_insert_type
4278 static int ocfs2_figure_insert_type(struct inode *inode,
4279 struct ocfs2_extent_tree *et,
4280 struct buffer_head **last_eb_bh,
4281 struct ocfs2_extent_rec *insert_rec,
4283 struct ocfs2_insert_type *insert)
4286 struct ocfs2_extent_block *eb;
4287 struct ocfs2_extent_list *el;
4288 struct ocfs2_path *path = NULL;
4289 struct buffer_head *bh = NULL;
4291 insert->ins_split = SPLIT_NONE;
4293 el = et->et_root_el;
4294 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4296 if (el->l_tree_depth) {
4298 * If we have tree depth, we read in the
4299 * rightmost extent block ahead of time as
4300 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4301 * may want it later.
4303 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4304 ocfs2_et_get_last_eb_blk(et), &bh,
4305 OCFS2_BH_CACHED, inode);
4310 eb = (struct ocfs2_extent_block *) bh->b_data;
4315 * Unless we have a contiguous insert, we'll need to know if
4316 * there is room left in our allocation tree for another
4319 * XXX: This test is simplistic, we can search for empty
4320 * extent records too.
4322 *free_records = le16_to_cpu(el->l_count) -
4323 le16_to_cpu(el->l_next_free_rec);
4325 if (!insert->ins_tree_depth) {
4326 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4327 ocfs2_figure_appending_type(insert, el, insert_rec);
4331 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4339 * In the case that we're inserting past what the tree
4340 * currently accounts for, ocfs2_find_path() will return for
4341 * us the rightmost tree path. This is accounted for below in
4342 * the appending code.
4344 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4350 el = path_leaf_el(path);
4353 * Now that we have the path, there's two things we want to determine:
4354 * 1) Contiguousness (also set contig_index if this is so)
4356 * 2) Are we doing an append? We can trivially break this up
4357 * into two types of appends: simple record append, or a
4358 * rotate inside the tail leaf.
4360 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4363 * The insert code isn't quite ready to deal with all cases of
4364 * left contiguousness. Specifically, if it's an insert into
4365 * the 1st record in a leaf, it will require the adjustment of
4366 * cluster count on the last record of the path directly to it's
4367 * left. For now, just catch that case and fool the layers
4368 * above us. This works just fine for tree_depth == 0, which
4369 * is why we allow that above.
4371 if (insert->ins_contig == CONTIG_LEFT &&
4372 insert->ins_contig_index == 0)
4373 insert->ins_contig = CONTIG_NONE;
4376 * Ok, so we can simply compare against last_eb to figure out
4377 * whether the path doesn't exist. This will only happen in
4378 * the case that we're doing a tail append, so maybe we can
4379 * take advantage of that information somehow.
4381 if (ocfs2_et_get_last_eb_blk(et) ==
4382 path_leaf_bh(path)->b_blocknr) {
4384 * Ok, ocfs2_find_path() returned us the rightmost
4385 * tree path. This might be an appending insert. There are
4387 * 1) We're doing a true append at the tail:
4388 * -This might even be off the end of the leaf
4389 * 2) We're "appending" by rotating in the tail
4391 ocfs2_figure_appending_type(insert, el, insert_rec);
4395 ocfs2_free_path(path);
4405 * Insert an extent into an inode btree.
4407 * The caller needs to update fe->i_clusters
4409 static int ocfs2_insert_extent(struct ocfs2_super *osb,
4411 struct inode *inode,
4412 struct buffer_head *root_bh,
4417 struct ocfs2_alloc_context *meta_ac,
4418 struct ocfs2_extent_tree *et)
4421 int uninitialized_var(free_records);
4422 struct buffer_head *last_eb_bh = NULL;
4423 struct ocfs2_insert_type insert = {0, };
4424 struct ocfs2_extent_rec rec;
4426 mlog(0, "add %u clusters at position %u to inode %llu\n",
4427 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4429 memset(&rec, 0, sizeof(rec));
4430 rec.e_cpos = cpu_to_le32(cpos);
4431 rec.e_blkno = cpu_to_le64(start_blk);
4432 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4433 rec.e_flags = flags;
4434 status = ocfs2_et_insert_check(inode, et, &rec);
4440 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4441 &free_records, &insert);
4447 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4448 "Insert.contig_index: %d, Insert.free_records: %d, "
4449 "Insert.tree_depth: %d\n",
4450 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4451 free_records, insert.ins_tree_depth);
4453 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4454 status = ocfs2_grow_tree(inode, handle, et,
4455 &insert.ins_tree_depth, &last_eb_bh,
4463 /* Finally, we can add clusters. This might rotate the tree for us. */
4464 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4467 else if (et->et_type == OCFS2_DINODE_EXTENT)
4468 ocfs2_extent_map_insert_rec(inode, &rec);
4478 int ocfs2_dinode_insert_extent(struct ocfs2_super *osb,
4480 struct inode *inode,
4481 struct buffer_head *root_bh,
4486 struct ocfs2_alloc_context *meta_ac)
4489 struct ocfs2_extent_tree et;
4491 ocfs2_get_dinode_extent_tree(&et, inode, root_bh);
4492 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4493 cpos, start_blk, new_clusters,
4494 flags, meta_ac, &et);
4495 ocfs2_put_extent_tree(&et);
4500 int ocfs2_xattr_value_insert_extent(struct ocfs2_super *osb,
4502 struct inode *inode,
4503 struct buffer_head *root_bh,
4508 struct ocfs2_alloc_context *meta_ac,
4509 struct ocfs2_xattr_value_root *xv)
4512 struct ocfs2_extent_tree et;
4514 ocfs2_get_xattr_value_extent_tree(&et, inode, root_bh, xv);
4515 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4516 cpos, start_blk, new_clusters,
4517 flags, meta_ac, &et);
4518 ocfs2_put_extent_tree(&et);
4523 int ocfs2_xattr_tree_insert_extent(struct ocfs2_super *osb,
4525 struct inode *inode,
4526 struct buffer_head *root_bh,
4531 struct ocfs2_alloc_context *meta_ac)
4534 struct ocfs2_extent_tree et;
4536 ocfs2_get_xattr_tree_extent_tree(&et, inode, root_bh);
4537 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4538 cpos, start_blk, new_clusters,
4539 flags, meta_ac, &et);
4540 ocfs2_put_extent_tree(&et);
4546 * Allcate and add clusters into the extent b-tree.
4547 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4548 * The extent b-tree's root is root_el and it should be in root_bh, and
4549 * it is not limited to the file storage. Any extent tree can use this
4550 * function if it implements the proper ocfs2_extent_tree.
4552 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4553 struct inode *inode,
4554 u32 *logical_offset,
4555 u32 clusters_to_add,
4557 struct buffer_head *root_bh,
4558 struct ocfs2_extent_list *root_el,
4560 struct ocfs2_alloc_context *data_ac,
4561 struct ocfs2_alloc_context *meta_ac,
4562 enum ocfs2_alloc_restarted *reason_ret,
4563 enum ocfs2_extent_tree_type type,
4568 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4569 u32 bit_off, num_bits;
4573 BUG_ON(!clusters_to_add);
4576 flags = OCFS2_EXT_UNWRITTEN;
4578 free_extents = ocfs2_num_free_extents(osb, inode, root_bh, type,
4580 if (free_extents < 0) {
4581 status = free_extents;
4586 /* there are two cases which could cause us to EAGAIN in the
4587 * we-need-more-metadata case:
4588 * 1) we haven't reserved *any*
4589 * 2) we are so fragmented, we've needed to add metadata too
4591 if (!free_extents && !meta_ac) {
4592 mlog(0, "we haven't reserved any metadata!\n");
4594 reason = RESTART_META;
4596 } else if ((!free_extents)
4597 && (ocfs2_alloc_context_bits_left(meta_ac)
4598 < ocfs2_extend_meta_needed(root_el))) {
4599 mlog(0, "filesystem is really fragmented...\n");
4601 reason = RESTART_META;
4605 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4606 clusters_to_add, &bit_off, &num_bits);
4608 if (status != -ENOSPC)
4613 BUG_ON(num_bits > clusters_to_add);
4615 /* reserve our write early -- insert_extent may update the inode */
4616 status = ocfs2_journal_access(handle, inode, root_bh,
4617 OCFS2_JOURNAL_ACCESS_WRITE);
4623 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4624 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4625 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4626 if (type == OCFS2_DINODE_EXTENT)
4627 status = ocfs2_dinode_insert_extent(osb, handle, inode, root_bh,
4628 *logical_offset, block,
4629 num_bits, flags, meta_ac);
4630 else if (type == OCFS2_XATTR_TREE_EXTENT)
4631 status = ocfs2_xattr_tree_insert_extent(osb, handle,
4634 block, num_bits, flags,
4637 status = ocfs2_xattr_value_insert_extent(osb, handle,
4640 block, num_bits, flags,
4647 status = ocfs2_journal_dirty(handle, root_bh);
4653 clusters_to_add -= num_bits;
4654 *logical_offset += num_bits;
4656 if (clusters_to_add) {
4657 mlog(0, "need to alloc once more, wanted = %u\n",
4660 reason = RESTART_TRANS;
4666 *reason_ret = reason;
4670 static void ocfs2_make_right_split_rec(struct super_block *sb,
4671 struct ocfs2_extent_rec *split_rec,
4673 struct ocfs2_extent_rec *rec)
4675 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4676 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4678 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4680 split_rec->e_cpos = cpu_to_le32(cpos);
4681 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4683 split_rec->e_blkno = rec->e_blkno;
4684 le64_add_cpu(&split_rec->e_blkno,
4685 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4687 split_rec->e_flags = rec->e_flags;
4690 static int ocfs2_split_and_insert(struct inode *inode,
4692 struct ocfs2_path *path,
4693 struct ocfs2_extent_tree *et,
4694 struct buffer_head **last_eb_bh,
4696 struct ocfs2_extent_rec *orig_split_rec,
4697 struct ocfs2_alloc_context *meta_ac)
4700 unsigned int insert_range, rec_range, do_leftright = 0;
4701 struct ocfs2_extent_rec tmprec;
4702 struct ocfs2_extent_list *rightmost_el;
4703 struct ocfs2_extent_rec rec;
4704 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4705 struct ocfs2_insert_type insert;
4706 struct ocfs2_extent_block *eb;
4710 * Store a copy of the record on the stack - it might move
4711 * around as the tree is manipulated below.
4713 rec = path_leaf_el(path)->l_recs[split_index];
4715 rightmost_el = et->et_root_el;
4717 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4719 BUG_ON(!(*last_eb_bh));
4720 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4721 rightmost_el = &eb->h_list;
4724 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4725 le16_to_cpu(rightmost_el->l_count)) {
4726 ret = ocfs2_grow_tree(inode, handle, et,
4727 &depth, last_eb_bh, meta_ac);
4734 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4735 insert.ins_appending = APPEND_NONE;
4736 insert.ins_contig = CONTIG_NONE;
4737 insert.ins_tree_depth = depth;
4739 insert_range = le32_to_cpu(split_rec.e_cpos) +
4740 le16_to_cpu(split_rec.e_leaf_clusters);
4741 rec_range = le32_to_cpu(rec.e_cpos) +
4742 le16_to_cpu(rec.e_leaf_clusters);
4744 if (split_rec.e_cpos == rec.e_cpos) {
4745 insert.ins_split = SPLIT_LEFT;
4746 } else if (insert_range == rec_range) {
4747 insert.ins_split = SPLIT_RIGHT;
4750 * Left/right split. We fake this as a right split
4751 * first and then make a second pass as a left split.
4753 insert.ins_split = SPLIT_RIGHT;
4755 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4760 BUG_ON(do_leftright);
4764 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4770 if (do_leftright == 1) {
4772 struct ocfs2_extent_list *el;
4775 split_rec = *orig_split_rec;
4777 ocfs2_reinit_path(path, 1);
4779 cpos = le32_to_cpu(split_rec.e_cpos);
4780 ret = ocfs2_find_path(inode, path, cpos);
4786 el = path_leaf_el(path);
4787 split_index = ocfs2_search_extent_list(el, cpos);
4796 * Mark part or all of the extent record at split_index in the leaf
4797 * pointed to by path as written. This removes the unwritten
4800 * Care is taken to handle contiguousness so as to not grow the tree.
4802 * meta_ac is not strictly necessary - we only truly need it if growth
4803 * of the tree is required. All other cases will degrade into a less
4804 * optimal tree layout.
4806 * last_eb_bh should be the rightmost leaf block for any extent
4807 * btree. Since a split may grow the tree or a merge might shrink it,
4808 * the caller cannot trust the contents of that buffer after this call.
4810 * This code is optimized for readability - several passes might be
4811 * made over certain portions of the tree. All of those blocks will
4812 * have been brought into cache (and pinned via the journal), so the
4813 * extra overhead is not expressed in terms of disk reads.
4815 static int __ocfs2_mark_extent_written(struct inode *inode,
4816 struct ocfs2_extent_tree *et,
4818 struct ocfs2_path *path,
4820 struct ocfs2_extent_rec *split_rec,
4821 struct ocfs2_alloc_context *meta_ac,
4822 struct ocfs2_cached_dealloc_ctxt *dealloc)
4825 struct ocfs2_extent_list *el = path_leaf_el(path);
4826 struct buffer_head *last_eb_bh = NULL;
4827 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4828 struct ocfs2_merge_ctxt ctxt;
4829 struct ocfs2_extent_list *rightmost_el;
4831 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
4837 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4838 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4839 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4845 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
4850 * The core merge / split code wants to know how much room is
4851 * left in this inodes allocation tree, so we pass the
4852 * rightmost extent list.
4854 if (path->p_tree_depth) {
4855 struct ocfs2_extent_block *eb;
4857 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4858 ocfs2_et_get_last_eb_blk(et),
4859 &last_eb_bh, OCFS2_BH_CACHED, inode);
4865 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4866 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4867 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4872 rightmost_el = &eb->h_list;
4874 rightmost_el = path_root_el(path);
4876 if (rec->e_cpos == split_rec->e_cpos &&
4877 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4878 ctxt.c_split_covers_rec = 1;
4880 ctxt.c_split_covers_rec = 0;
4882 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4884 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4885 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4886 ctxt.c_split_covers_rec);
4888 if (ctxt.c_contig_type == CONTIG_NONE) {
4889 if (ctxt.c_split_covers_rec)
4890 el->l_recs[split_index] = *split_rec;
4892 ret = ocfs2_split_and_insert(inode, handle, path, et,
4893 &last_eb_bh, split_index,
4894 split_rec, meta_ac);
4898 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4899 split_index, split_rec,
4900 dealloc, &ctxt, et);
4911 * Mark the already-existing extent at cpos as written for len clusters.
4913 * If the existing extent is larger than the request, initiate a
4914 * split. An attempt will be made at merging with adjacent extents.
4916 * The caller is responsible for passing down meta_ac if we'll need it.
4918 int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh,
4919 handle_t *handle, u32 cpos, u32 len, u32 phys,
4920 struct ocfs2_alloc_context *meta_ac,
4921 struct ocfs2_cached_dealloc_ctxt *dealloc,
4922 enum ocfs2_extent_tree_type et_type,
4926 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4927 struct ocfs2_extent_rec split_rec;
4928 struct ocfs2_path *left_path = NULL;
4929 struct ocfs2_extent_list *el;
4930 struct ocfs2_extent_tree et;
4932 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4933 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4935 ocfs2_get_extent_tree(&et, inode, root_bh, et_type, obj);
4937 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4938 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4939 "that are being written to, but the feature bit "
4940 "is not set in the super block.",
4941 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4947 * XXX: This should be fixed up so that we just re-insert the
4948 * next extent records.
4950 if (et_type == OCFS2_DINODE_EXTENT)
4951 ocfs2_extent_map_trunc(inode, 0);
4953 left_path = ocfs2_new_path(et.et_root_bh, et.et_root_el);
4960 ret = ocfs2_find_path(inode, left_path, cpos);
4965 el = path_leaf_el(left_path);
4967 index = ocfs2_search_extent_list(el, cpos);
4968 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4969 ocfs2_error(inode->i_sb,
4970 "Inode %llu has an extent at cpos %u which can no "
4971 "longer be found.\n",
4972 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4977 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4978 split_rec.e_cpos = cpu_to_le32(cpos);
4979 split_rec.e_leaf_clusters = cpu_to_le16(len);
4980 split_rec.e_blkno = cpu_to_le64(start_blkno);
4981 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4982 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4984 ret = __ocfs2_mark_extent_written(inode, &et, handle, left_path,
4985 index, &split_rec, meta_ac,
4991 ocfs2_free_path(left_path);
4992 ocfs2_put_extent_tree(&et);
4996 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
4997 handle_t *handle, struct ocfs2_path *path,
4998 int index, u32 new_range,
4999 struct ocfs2_alloc_context *meta_ac)
5001 int ret, depth, credits = handle->h_buffer_credits;
5002 struct buffer_head *last_eb_bh = NULL;
5003 struct ocfs2_extent_block *eb;
5004 struct ocfs2_extent_list *rightmost_el, *el;
5005 struct ocfs2_extent_rec split_rec;
5006 struct ocfs2_extent_rec *rec;
5007 struct ocfs2_insert_type insert;
5010 * Setup the record to split before we grow the tree.
5012 el = path_leaf_el(path);
5013 rec = &el->l_recs[index];
5014 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5016 depth = path->p_tree_depth;
5018 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
5019 ocfs2_et_get_last_eb_blk(et),
5020 &last_eb_bh, OCFS2_BH_CACHED, inode);
5026 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5027 rightmost_el = &eb->h_list;
5029 rightmost_el = path_leaf_el(path);
5031 credits += path->p_tree_depth +
5032 ocfs2_extend_meta_needed(et->et_root_el);
5033 ret = ocfs2_extend_trans(handle, credits);
5039 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5040 le16_to_cpu(rightmost_el->l_count)) {
5041 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
5049 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5050 insert.ins_appending = APPEND_NONE;
5051 insert.ins_contig = CONTIG_NONE;
5052 insert.ins_split = SPLIT_RIGHT;
5053 insert.ins_tree_depth = depth;
5055 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
5064 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5065 struct ocfs2_path *path, int index,
5066 struct ocfs2_cached_dealloc_ctxt *dealloc,
5068 struct ocfs2_extent_tree *et)
5071 u32 left_cpos, rec_range, trunc_range;
5072 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5073 struct super_block *sb = inode->i_sb;
5074 struct ocfs2_path *left_path = NULL;
5075 struct ocfs2_extent_list *el = path_leaf_el(path);
5076 struct ocfs2_extent_rec *rec;
5077 struct ocfs2_extent_block *eb;
5079 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5080 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5089 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5090 path->p_tree_depth) {
5092 * Check whether this is the rightmost tree record. If
5093 * we remove all of this record or part of its right
5094 * edge then an update of the record lengths above it
5097 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5098 if (eb->h_next_leaf_blk == 0)
5099 is_rightmost_tree_rec = 1;
5102 rec = &el->l_recs[index];
5103 if (index == 0 && path->p_tree_depth &&
5104 le32_to_cpu(rec->e_cpos) == cpos) {
5106 * Changing the leftmost offset (via partial or whole
5107 * record truncate) of an interior (or rightmost) path
5108 * means we have to update the subtree that is formed
5109 * by this leaf and the one to it's left.
5111 * There are two cases we can skip:
5112 * 1) Path is the leftmost one in our inode tree.
5113 * 2) The leaf is rightmost and will be empty after
5114 * we remove the extent record - the rotate code
5115 * knows how to update the newly formed edge.
5118 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5125 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5126 left_path = ocfs2_new_path(path_root_bh(path),
5127 path_root_el(path));
5134 ret = ocfs2_find_path(inode, left_path, left_cpos);
5142 ret = ocfs2_extend_rotate_transaction(handle, 0,
5143 handle->h_buffer_credits,
5150 ret = ocfs2_journal_access_path(inode, handle, path);
5156 ret = ocfs2_journal_access_path(inode, handle, left_path);
5162 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5163 trunc_range = cpos + len;
5165 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5168 memset(rec, 0, sizeof(*rec));
5169 ocfs2_cleanup_merge(el, index);
5172 next_free = le16_to_cpu(el->l_next_free_rec);
5173 if (is_rightmost_tree_rec && next_free > 1) {
5175 * We skip the edge update if this path will
5176 * be deleted by the rotate code.
5178 rec = &el->l_recs[next_free - 1];
5179 ocfs2_adjust_rightmost_records(inode, handle, path,
5182 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5183 /* Remove leftmost portion of the record. */
5184 le32_add_cpu(&rec->e_cpos, len);
5185 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5186 le16_add_cpu(&rec->e_leaf_clusters, -len);
5187 } else if (rec_range == trunc_range) {
5188 /* Remove rightmost portion of the record */
5189 le16_add_cpu(&rec->e_leaf_clusters, -len);
5190 if (is_rightmost_tree_rec)
5191 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5193 /* Caller should have trapped this. */
5194 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5195 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5196 le32_to_cpu(rec->e_cpos),
5197 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5204 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5205 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5209 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5211 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5218 ocfs2_free_path(left_path);
5222 int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
5223 u32 cpos, u32 len, handle_t *handle,
5224 struct ocfs2_alloc_context *meta_ac,
5225 struct ocfs2_cached_dealloc_ctxt *dealloc,
5226 enum ocfs2_extent_tree_type et_type,
5230 u32 rec_range, trunc_range;
5231 struct ocfs2_extent_rec *rec;
5232 struct ocfs2_extent_list *el;
5233 struct ocfs2_path *path = NULL;
5234 struct ocfs2_extent_tree et;
5236 ocfs2_get_extent_tree(&et, inode, root_bh, et_type, obj);
5238 ocfs2_extent_map_trunc(inode, 0);
5240 path = ocfs2_new_path(et.et_root_bh, et.et_root_el);
5247 ret = ocfs2_find_path(inode, path, cpos);
5253 el = path_leaf_el(path);
5254 index = ocfs2_search_extent_list(el, cpos);
5255 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5256 ocfs2_error(inode->i_sb,
5257 "Inode %llu has an extent at cpos %u which can no "
5258 "longer be found.\n",
5259 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5265 * We have 3 cases of extent removal:
5266 * 1) Range covers the entire extent rec
5267 * 2) Range begins or ends on one edge of the extent rec
5268 * 3) Range is in the middle of the extent rec (no shared edges)
5270 * For case 1 we remove the extent rec and left rotate to
5273 * For case 2 we just shrink the existing extent rec, with a
5274 * tree update if the shrinking edge is also the edge of an
5277 * For case 3 we do a right split to turn the extent rec into
5278 * something case 2 can handle.
5280 rec = &el->l_recs[index];
5281 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5282 trunc_range = cpos + len;
5284 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5286 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5287 "(cpos %u, len %u)\n",
5288 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5289 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5291 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5292 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5299 ret = ocfs2_split_tree(inode, &et, handle, path, index,
5300 trunc_range, meta_ac);
5307 * The split could have manipulated the tree enough to
5308 * move the record location, so we have to look for it again.
5310 ocfs2_reinit_path(path, 1);
5312 ret = ocfs2_find_path(inode, path, cpos);
5318 el = path_leaf_el(path);
5319 index = ocfs2_search_extent_list(el, cpos);
5320 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5321 ocfs2_error(inode->i_sb,
5322 "Inode %llu: split at cpos %u lost record.",
5323 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5330 * Double check our values here. If anything is fishy,
5331 * it's easier to catch it at the top level.
5333 rec = &el->l_recs[index];
5334 rec_range = le32_to_cpu(rec->e_cpos) +
5335 ocfs2_rec_clusters(el, rec);
5336 if (rec_range != trunc_range) {
5337 ocfs2_error(inode->i_sb,
5338 "Inode %llu: error after split at cpos %u"
5339 "trunc len %u, existing record is (%u,%u)",
5340 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5341 cpos, len, le32_to_cpu(rec->e_cpos),
5342 ocfs2_rec_clusters(el, rec));
5347 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5356 ocfs2_free_path(path);
5357 ocfs2_put_extent_tree(&et);
5361 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5363 struct buffer_head *tl_bh = osb->osb_tl_bh;
5364 struct ocfs2_dinode *di;
5365 struct ocfs2_truncate_log *tl;
5367 di = (struct ocfs2_dinode *) tl_bh->b_data;
5368 tl = &di->id2.i_dealloc;
5370 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5371 "slot %d, invalid truncate log parameters: used = "
5372 "%u, count = %u\n", osb->slot_num,
5373 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5374 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5377 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5378 unsigned int new_start)
5380 unsigned int tail_index;
5381 unsigned int current_tail;
5383 /* No records, nothing to coalesce */
5384 if (!le16_to_cpu(tl->tl_used))
5387 tail_index = le16_to_cpu(tl->tl_used) - 1;
5388 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5389 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5391 return current_tail == new_start;
5394 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5397 unsigned int num_clusters)
5400 unsigned int start_cluster, tl_count;
5401 struct inode *tl_inode = osb->osb_tl_inode;
5402 struct buffer_head *tl_bh = osb->osb_tl_bh;
5403 struct ocfs2_dinode *di;
5404 struct ocfs2_truncate_log *tl;
5406 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5407 (unsigned long long)start_blk, num_clusters);
5409 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5411 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5413 di = (struct ocfs2_dinode *) tl_bh->b_data;
5414 tl = &di->id2.i_dealloc;
5415 if (!OCFS2_IS_VALID_DINODE(di)) {
5416 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5421 tl_count = le16_to_cpu(tl->tl_count);
5422 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5424 "Truncate record count on #%llu invalid "
5425 "wanted %u, actual %u\n",
5426 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5427 ocfs2_truncate_recs_per_inode(osb->sb),
5428 le16_to_cpu(tl->tl_count));
5430 /* Caller should have known to flush before calling us. */
5431 index = le16_to_cpu(tl->tl_used);
5432 if (index >= tl_count) {
5438 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5439 OCFS2_JOURNAL_ACCESS_WRITE);
5445 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5446 "%llu (index = %d)\n", num_clusters, start_cluster,
5447 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5449 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5451 * Move index back to the record we are coalescing with.
5452 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5456 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5457 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5458 index, le32_to_cpu(tl->tl_recs[index].t_start),
5461 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5462 tl->tl_used = cpu_to_le16(index + 1);
5464 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5466 status = ocfs2_journal_dirty(handle, tl_bh);
5477 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5479 struct inode *data_alloc_inode,
5480 struct buffer_head *data_alloc_bh)
5484 unsigned int num_clusters;
5486 struct ocfs2_truncate_rec rec;
5487 struct ocfs2_dinode *di;
5488 struct ocfs2_truncate_log *tl;
5489 struct inode *tl_inode = osb->osb_tl_inode;
5490 struct buffer_head *tl_bh = osb->osb_tl_bh;
5494 di = (struct ocfs2_dinode *) tl_bh->b_data;
5495 tl = &di->id2.i_dealloc;
5496 i = le16_to_cpu(tl->tl_used) - 1;
5498 /* Caller has given us at least enough credits to
5499 * update the truncate log dinode */
5500 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5501 OCFS2_JOURNAL_ACCESS_WRITE);
5507 tl->tl_used = cpu_to_le16(i);
5509 status = ocfs2_journal_dirty(handle, tl_bh);
5515 /* TODO: Perhaps we can calculate the bulk of the
5516 * credits up front rather than extending like
5518 status = ocfs2_extend_trans(handle,
5519 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5525 rec = tl->tl_recs[i];
5526 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5527 le32_to_cpu(rec.t_start));
5528 num_clusters = le32_to_cpu(rec.t_clusters);
5530 /* if start_blk is not set, we ignore the record as
5533 mlog(0, "free record %d, start = %u, clusters = %u\n",
5534 i, le32_to_cpu(rec.t_start), num_clusters);
5536 status = ocfs2_free_clusters(handle, data_alloc_inode,
5537 data_alloc_bh, start_blk,
5552 /* Expects you to already be holding tl_inode->i_mutex */
5553 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5556 unsigned int num_to_flush;
5558 struct inode *tl_inode = osb->osb_tl_inode;
5559 struct inode *data_alloc_inode = NULL;
5560 struct buffer_head *tl_bh = osb->osb_tl_bh;
5561 struct buffer_head *data_alloc_bh = NULL;
5562 struct ocfs2_dinode *di;
5563 struct ocfs2_truncate_log *tl;
5567 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5569 di = (struct ocfs2_dinode *) tl_bh->b_data;
5570 tl = &di->id2.i_dealloc;
5571 if (!OCFS2_IS_VALID_DINODE(di)) {
5572 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5577 num_to_flush = le16_to_cpu(tl->tl_used);
5578 mlog(0, "Flush %u records from truncate log #%llu\n",
5579 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5580 if (!num_to_flush) {
5585 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5586 GLOBAL_BITMAP_SYSTEM_INODE,
5587 OCFS2_INVALID_SLOT);
5588 if (!data_alloc_inode) {
5590 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5594 mutex_lock(&data_alloc_inode->i_mutex);
5596 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5602 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5603 if (IS_ERR(handle)) {
5604 status = PTR_ERR(handle);
5609 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5614 ocfs2_commit_trans(osb, handle);
5617 brelse(data_alloc_bh);
5618 ocfs2_inode_unlock(data_alloc_inode, 1);
5621 mutex_unlock(&data_alloc_inode->i_mutex);
5622 iput(data_alloc_inode);
5629 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5632 struct inode *tl_inode = osb->osb_tl_inode;
5634 mutex_lock(&tl_inode->i_mutex);
5635 status = __ocfs2_flush_truncate_log(osb);
5636 mutex_unlock(&tl_inode->i_mutex);
5641 static void ocfs2_truncate_log_worker(struct work_struct *work)
5644 struct ocfs2_super *osb =
5645 container_of(work, struct ocfs2_super,
5646 osb_truncate_log_wq.work);
5650 status = ocfs2_flush_truncate_log(osb);
5654 ocfs2_init_inode_steal_slot(osb);
5659 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5660 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5663 if (osb->osb_tl_inode) {
5664 /* We want to push off log flushes while truncates are
5667 cancel_delayed_work(&osb->osb_truncate_log_wq);
5669 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5670 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5674 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5676 struct inode **tl_inode,
5677 struct buffer_head **tl_bh)
5680 struct inode *inode = NULL;
5681 struct buffer_head *bh = NULL;
5683 inode = ocfs2_get_system_file_inode(osb,
5684 TRUNCATE_LOG_SYSTEM_INODE,
5688 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5692 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
5693 OCFS2_BH_CACHED, inode);
5707 /* called during the 1st stage of node recovery. we stamp a clean
5708 * truncate log and pass back a copy for processing later. if the
5709 * truncate log does not require processing, a *tl_copy is set to
5711 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5713 struct ocfs2_dinode **tl_copy)
5716 struct inode *tl_inode = NULL;
5717 struct buffer_head *tl_bh = NULL;
5718 struct ocfs2_dinode *di;
5719 struct ocfs2_truncate_log *tl;
5723 mlog(0, "recover truncate log from slot %d\n", slot_num);
5725 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5731 di = (struct ocfs2_dinode *) tl_bh->b_data;
5732 tl = &di->id2.i_dealloc;
5733 if (!OCFS2_IS_VALID_DINODE(di)) {
5734 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
5739 if (le16_to_cpu(tl->tl_used)) {
5740 mlog(0, "We'll have %u logs to recover\n",
5741 le16_to_cpu(tl->tl_used));
5743 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5750 /* Assuming the write-out below goes well, this copy
5751 * will be passed back to recovery for processing. */
5752 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5754 /* All we need to do to clear the truncate log is set
5758 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5771 if (status < 0 && (*tl_copy)) {
5780 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5781 struct ocfs2_dinode *tl_copy)
5785 unsigned int clusters, num_recs, start_cluster;
5788 struct inode *tl_inode = osb->osb_tl_inode;
5789 struct ocfs2_truncate_log *tl;
5793 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5794 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5798 tl = &tl_copy->id2.i_dealloc;
5799 num_recs = le16_to_cpu(tl->tl_used);
5800 mlog(0, "cleanup %u records from %llu\n", num_recs,
5801 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
5803 mutex_lock(&tl_inode->i_mutex);
5804 for(i = 0; i < num_recs; i++) {
5805 if (ocfs2_truncate_log_needs_flush(osb)) {
5806 status = __ocfs2_flush_truncate_log(osb);
5813 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5814 if (IS_ERR(handle)) {
5815 status = PTR_ERR(handle);
5820 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5821 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5822 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5824 status = ocfs2_truncate_log_append(osb, handle,
5825 start_blk, clusters);
5826 ocfs2_commit_trans(osb, handle);
5834 mutex_unlock(&tl_inode->i_mutex);
5840 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5843 struct inode *tl_inode = osb->osb_tl_inode;
5848 cancel_delayed_work(&osb->osb_truncate_log_wq);
5849 flush_workqueue(ocfs2_wq);
5851 status = ocfs2_flush_truncate_log(osb);
5855 brelse(osb->osb_tl_bh);
5856 iput(osb->osb_tl_inode);
5862 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5865 struct inode *tl_inode = NULL;
5866 struct buffer_head *tl_bh = NULL;
5870 status = ocfs2_get_truncate_log_info(osb,
5877 /* ocfs2_truncate_log_shutdown keys on the existence of
5878 * osb->osb_tl_inode so we don't set any of the osb variables
5879 * until we're sure all is well. */
5880 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5881 ocfs2_truncate_log_worker);
5882 osb->osb_tl_bh = tl_bh;
5883 osb->osb_tl_inode = tl_inode;
5890 * Delayed de-allocation of suballocator blocks.
5892 * Some sets of block de-allocations might involve multiple suballocator inodes.
5894 * The locking for this can get extremely complicated, especially when
5895 * the suballocator inodes to delete from aren't known until deep
5896 * within an unrelated codepath.
5898 * ocfs2_extent_block structures are a good example of this - an inode
5899 * btree could have been grown by any number of nodes each allocating
5900 * out of their own suballoc inode.
5902 * These structures allow the delay of block de-allocation until a
5903 * later time, when locking of multiple cluster inodes won't cause
5908 * Describes a single block free from a suballocator
5910 struct ocfs2_cached_block_free {
5911 struct ocfs2_cached_block_free *free_next;
5913 unsigned int free_bit;
5916 struct ocfs2_per_slot_free_list {
5917 struct ocfs2_per_slot_free_list *f_next_suballocator;
5920 struct ocfs2_cached_block_free *f_first;
5923 static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5926 struct ocfs2_cached_block_free *head)
5931 struct inode *inode;
5932 struct buffer_head *di_bh = NULL;
5933 struct ocfs2_cached_block_free *tmp;
5935 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5942 mutex_lock(&inode->i_mutex);
5944 ret = ocfs2_inode_lock(inode, &di_bh, 1);
5950 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5951 if (IS_ERR(handle)) {
5952 ret = PTR_ERR(handle);
5958 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5960 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5961 head->free_bit, (unsigned long long)head->free_blk);
5963 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5964 head->free_bit, bg_blkno, 1);
5970 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5977 head = head->free_next;
5982 ocfs2_commit_trans(osb, handle);
5985 ocfs2_inode_unlock(inode, 1);
5988 mutex_unlock(&inode->i_mutex);
5992 /* Premature exit may have left some dangling items. */
5994 head = head->free_next;
6001 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6002 struct ocfs2_cached_dealloc_ctxt *ctxt)
6005 struct ocfs2_per_slot_free_list *fl;
6010 while (ctxt->c_first_suballocator) {
6011 fl = ctxt->c_first_suballocator;
6014 mlog(0, "Free items: (type %u, slot %d)\n",
6015 fl->f_inode_type, fl->f_slot);
6016 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
6017 fl->f_slot, fl->f_first);
6024 ctxt->c_first_suballocator = fl->f_next_suballocator;
6031 static struct ocfs2_per_slot_free_list *
6032 ocfs2_find_per_slot_free_list(int type,
6034 struct ocfs2_cached_dealloc_ctxt *ctxt)
6036 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6039 if (fl->f_inode_type == type && fl->f_slot == slot)
6042 fl = fl->f_next_suballocator;
6045 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6047 fl->f_inode_type = type;
6050 fl->f_next_suballocator = ctxt->c_first_suballocator;
6052 ctxt->c_first_suballocator = fl;
6057 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6058 int type, int slot, u64 blkno,
6062 struct ocfs2_per_slot_free_list *fl;
6063 struct ocfs2_cached_block_free *item;
6065 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6072 item = kmalloc(sizeof(*item), GFP_NOFS);
6079 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6080 type, slot, bit, (unsigned long long)blkno);
6082 item->free_blk = blkno;
6083 item->free_bit = bit;
6084 item->free_next = fl->f_first;
6093 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6094 struct ocfs2_extent_block *eb)
6096 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6097 le16_to_cpu(eb->h_suballoc_slot),
6098 le64_to_cpu(eb->h_blkno),
6099 le16_to_cpu(eb->h_suballoc_bit));
6102 /* This function will figure out whether the currently last extent
6103 * block will be deleted, and if it will, what the new last extent
6104 * block will be so we can update his h_next_leaf_blk field, as well
6105 * as the dinodes i_last_eb_blk */
6106 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6107 unsigned int clusters_to_del,
6108 struct ocfs2_path *path,
6109 struct buffer_head **new_last_eb)
6111 int next_free, ret = 0;
6113 struct ocfs2_extent_rec *rec;
6114 struct ocfs2_extent_block *eb;
6115 struct ocfs2_extent_list *el;
6116 struct buffer_head *bh = NULL;
6118 *new_last_eb = NULL;
6120 /* we have no tree, so of course, no last_eb. */
6121 if (!path->p_tree_depth)
6124 /* trunc to zero special case - this makes tree_depth = 0
6125 * regardless of what it is. */
6126 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6129 el = path_leaf_el(path);
6130 BUG_ON(!el->l_next_free_rec);
6133 * Make sure that this extent list will actually be empty
6134 * after we clear away the data. We can shortcut out if
6135 * there's more than one non-empty extent in the
6136 * list. Otherwise, a check of the remaining extent is
6139 next_free = le16_to_cpu(el->l_next_free_rec);
6141 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6145 /* We may have a valid extent in index 1, check it. */
6147 rec = &el->l_recs[1];
6150 * Fall through - no more nonempty extents, so we want
6151 * to delete this leaf.
6157 rec = &el->l_recs[0];
6162 * Check it we'll only be trimming off the end of this
6165 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6169 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6175 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6181 eb = (struct ocfs2_extent_block *) bh->b_data;
6183 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6184 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6190 get_bh(*new_last_eb);
6191 mlog(0, "returning block %llu, (cpos: %u)\n",
6192 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6200 * Trim some clusters off the rightmost edge of a tree. Only called
6203 * The caller needs to:
6204 * - start journaling of each path component.
6205 * - compute and fully set up any new last ext block
6207 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6208 handle_t *handle, struct ocfs2_truncate_context *tc,
6209 u32 clusters_to_del, u64 *delete_start)
6211 int ret, i, index = path->p_tree_depth;
6214 struct buffer_head *bh;
6215 struct ocfs2_extent_list *el;
6216 struct ocfs2_extent_rec *rec;
6220 while (index >= 0) {
6221 bh = path->p_node[index].bh;
6222 el = path->p_node[index].el;
6224 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6225 index, (unsigned long long)bh->b_blocknr);
6227 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6230 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6231 ocfs2_error(inode->i_sb,
6232 "Inode %lu has invalid ext. block %llu",
6234 (unsigned long long)bh->b_blocknr);
6240 i = le16_to_cpu(el->l_next_free_rec) - 1;
6241 rec = &el->l_recs[i];
6243 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6244 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6245 ocfs2_rec_clusters(el, rec),
6246 (unsigned long long)le64_to_cpu(rec->e_blkno),
6247 le16_to_cpu(el->l_next_free_rec));
6249 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6251 if (le16_to_cpu(el->l_tree_depth) == 0) {
6253 * If the leaf block contains a single empty
6254 * extent and no records, we can just remove
6257 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6259 sizeof(struct ocfs2_extent_rec));
6260 el->l_next_free_rec = cpu_to_le16(0);
6266 * Remove any empty extents by shifting things
6267 * left. That should make life much easier on
6268 * the code below. This condition is rare
6269 * enough that we shouldn't see a performance
6272 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6273 le16_add_cpu(&el->l_next_free_rec, -1);
6276 i < le16_to_cpu(el->l_next_free_rec); i++)
6277 el->l_recs[i] = el->l_recs[i + 1];
6279 memset(&el->l_recs[i], 0,
6280 sizeof(struct ocfs2_extent_rec));
6283 * We've modified our extent list. The
6284 * simplest way to handle this change
6285 * is to being the search from the
6288 goto find_tail_record;
6291 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6294 * We'll use "new_edge" on our way back up the
6295 * tree to know what our rightmost cpos is.
6297 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6298 new_edge += le32_to_cpu(rec->e_cpos);
6301 * The caller will use this to delete data blocks.
6303 *delete_start = le64_to_cpu(rec->e_blkno)
6304 + ocfs2_clusters_to_blocks(inode->i_sb,
6305 le16_to_cpu(rec->e_leaf_clusters));
6308 * If it's now empty, remove this record.
6310 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6312 sizeof(struct ocfs2_extent_rec));
6313 le16_add_cpu(&el->l_next_free_rec, -1);
6316 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6318 sizeof(struct ocfs2_extent_rec));
6319 le16_add_cpu(&el->l_next_free_rec, -1);
6324 /* Can this actually happen? */
6325 if (le16_to_cpu(el->l_next_free_rec) == 0)
6329 * We never actually deleted any clusters
6330 * because our leaf was empty. There's no
6331 * reason to adjust the rightmost edge then.
6336 rec->e_int_clusters = cpu_to_le32(new_edge);
6337 le32_add_cpu(&rec->e_int_clusters,
6338 -le32_to_cpu(rec->e_cpos));
6341 * A deleted child record should have been
6344 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6348 ret = ocfs2_journal_dirty(handle, bh);
6354 mlog(0, "extent list container %llu, after: record %d: "
6355 "(%u, %u, %llu), next = %u.\n",
6356 (unsigned long long)bh->b_blocknr, i,
6357 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6358 (unsigned long long)le64_to_cpu(rec->e_blkno),
6359 le16_to_cpu(el->l_next_free_rec));
6362 * We must be careful to only attempt delete of an
6363 * extent block (and not the root inode block).
6365 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6366 struct ocfs2_extent_block *eb =
6367 (struct ocfs2_extent_block *)bh->b_data;
6370 * Save this for use when processing the
6373 deleted_eb = le64_to_cpu(eb->h_blkno);
6375 mlog(0, "deleting this extent block.\n");
6377 ocfs2_remove_from_cache(inode, bh);
6379 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6380 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6381 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6383 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6384 /* An error here is not fatal. */
6399 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6400 unsigned int clusters_to_del,
6401 struct inode *inode,
6402 struct buffer_head *fe_bh,
6404 struct ocfs2_truncate_context *tc,
6405 struct ocfs2_path *path)
6408 struct ocfs2_dinode *fe;
6409 struct ocfs2_extent_block *last_eb = NULL;
6410 struct ocfs2_extent_list *el;
6411 struct buffer_head *last_eb_bh = NULL;
6414 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6416 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6424 * Each component will be touched, so we might as well journal
6425 * here to avoid having to handle errors later.
6427 status = ocfs2_journal_access_path(inode, handle, path);
6434 status = ocfs2_journal_access(handle, inode, last_eb_bh,
6435 OCFS2_JOURNAL_ACCESS_WRITE);
6441 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6444 el = &(fe->id2.i_list);
6447 * Lower levels depend on this never happening, but it's best
6448 * to check it up here before changing the tree.
6450 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6451 ocfs2_error(inode->i_sb,
6452 "Inode %lu has an empty extent record, depth %u\n",
6453 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6458 spin_lock(&OCFS2_I(inode)->ip_lock);
6459 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6461 spin_unlock(&OCFS2_I(inode)->ip_lock);
6462 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6463 inode->i_blocks = ocfs2_inode_sector_count(inode);
6465 status = ocfs2_trim_tree(inode, path, handle, tc,
6466 clusters_to_del, &delete_blk);
6472 if (le32_to_cpu(fe->i_clusters) == 0) {
6473 /* trunc to zero is a special case. */
6474 el->l_tree_depth = 0;
6475 fe->i_last_eb_blk = 0;
6477 fe->i_last_eb_blk = last_eb->h_blkno;
6479 status = ocfs2_journal_dirty(handle, fe_bh);
6486 /* If there will be a new last extent block, then by
6487 * definition, there cannot be any leaves to the right of
6489 last_eb->h_next_leaf_blk = 0;
6490 status = ocfs2_journal_dirty(handle, last_eb_bh);
6498 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6512 static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
6514 set_buffer_uptodate(bh);
6515 mark_buffer_dirty(bh);
6519 static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
6521 set_buffer_uptodate(bh);
6522 mark_buffer_dirty(bh);
6523 return ocfs2_journal_dirty_data(handle, bh);
6526 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6527 unsigned int from, unsigned int to,
6528 struct page *page, int zero, u64 *phys)
6530 int ret, partial = 0;
6532 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6537 zero_user_segment(page, from, to);
6540 * Need to set the buffers we zero'd into uptodate
6541 * here if they aren't - ocfs2_map_page_blocks()
6542 * might've skipped some
6544 if (ocfs2_should_order_data(inode)) {
6545 ret = walk_page_buffers(handle,
6548 ocfs2_ordered_zero_func);
6552 ret = walk_page_buffers(handle, page_buffers(page),
6554 ocfs2_writeback_zero_func);
6560 SetPageUptodate(page);
6562 flush_dcache_page(page);
6565 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6566 loff_t end, struct page **pages,
6567 int numpages, u64 phys, handle_t *handle)
6571 unsigned int from, to = PAGE_CACHE_SIZE;
6572 struct super_block *sb = inode->i_sb;
6574 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6579 to = PAGE_CACHE_SIZE;
6580 for(i = 0; i < numpages; i++) {
6583 from = start & (PAGE_CACHE_SIZE - 1);
6584 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6585 to = end & (PAGE_CACHE_SIZE - 1);
6587 BUG_ON(from > PAGE_CACHE_SIZE);
6588 BUG_ON(to > PAGE_CACHE_SIZE);
6590 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6593 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6597 ocfs2_unlock_and_free_pages(pages, numpages);
6600 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6601 struct page **pages, int *num)
6603 int numpages, ret = 0;
6604 struct super_block *sb = inode->i_sb;
6605 struct address_space *mapping = inode->i_mapping;
6606 unsigned long index;
6607 loff_t last_page_bytes;
6609 BUG_ON(start > end);
6611 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6612 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6615 last_page_bytes = PAGE_ALIGN(end);
6616 index = start >> PAGE_CACHE_SHIFT;
6618 pages[numpages] = grab_cache_page(mapping, index);
6619 if (!pages[numpages]) {
6627 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6632 ocfs2_unlock_and_free_pages(pages, numpages);
6642 * Zero the area past i_size but still within an allocated
6643 * cluster. This avoids exposing nonzero data on subsequent file
6646 * We need to call this before i_size is updated on the inode because
6647 * otherwise block_write_full_page() will skip writeout of pages past
6648 * i_size. The new_i_size parameter is passed for this reason.
6650 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6651 u64 range_start, u64 range_end)
6653 int ret = 0, numpages;
6654 struct page **pages = NULL;
6656 unsigned int ext_flags;
6657 struct super_block *sb = inode->i_sb;
6660 * File systems which don't support sparse files zero on every
6663 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6666 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6667 sizeof(struct page *), GFP_NOFS);
6668 if (pages == NULL) {
6674 if (range_start == range_end)
6677 ret = ocfs2_extent_map_get_blocks(inode,
6678 range_start >> sb->s_blocksize_bits,
6679 &phys, NULL, &ext_flags);
6686 * Tail is a hole, or is marked unwritten. In either case, we
6687 * can count on read and write to return/push zero's.
6689 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6692 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6699 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6700 numpages, phys, handle);
6703 * Initiate writeout of the pages we zero'd here. We don't
6704 * wait on them - the truncate_inode_pages() call later will
6707 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6708 range_end - 1, SYNC_FILE_RANGE_WRITE);
6719 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6720 struct ocfs2_dinode *di)
6722 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6723 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6725 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6726 memset(&di->id2, 0, blocksize -
6727 offsetof(struct ocfs2_dinode, id2) -
6730 memset(&di->id2, 0, blocksize -
6731 offsetof(struct ocfs2_dinode, id2));
6734 void ocfs2_dinode_new_extent_list(struct inode *inode,
6735 struct ocfs2_dinode *di)
6737 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6738 di->id2.i_list.l_tree_depth = 0;
6739 di->id2.i_list.l_next_free_rec = 0;
6740 di->id2.i_list.l_count = cpu_to_le16(
6741 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6744 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6746 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6747 struct ocfs2_inline_data *idata = &di->id2.i_data;
6749 spin_lock(&oi->ip_lock);
6750 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6751 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6752 spin_unlock(&oi->ip_lock);
6755 * We clear the entire i_data structure here so that all
6756 * fields can be properly initialized.
6758 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6760 idata->id_count = cpu_to_le16(
6761 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6764 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6765 struct buffer_head *di_bh)
6767 int ret, i, has_data, num_pages = 0;
6769 u64 uninitialized_var(block);
6770 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6771 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6772 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6773 struct ocfs2_alloc_context *data_ac = NULL;
6774 struct page **pages = NULL;
6775 loff_t end = osb->s_clustersize;
6777 has_data = i_size_read(inode) ? 1 : 0;
6780 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6781 sizeof(struct page *), GFP_NOFS);
6782 if (pages == NULL) {
6788 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6795 handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
6796 if (IS_ERR(handle)) {
6797 ret = PTR_ERR(handle);
6802 ret = ocfs2_journal_access(handle, inode, di_bh,
6803 OCFS2_JOURNAL_ACCESS_WRITE);
6811 unsigned int page_end;
6814 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6822 * Save two copies, one for insert, and one that can
6823 * be changed by ocfs2_map_and_dirty_page() below.
6825 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6828 * Non sparse file systems zero on extend, so no need
6831 if (!ocfs2_sparse_alloc(osb) &&
6832 PAGE_CACHE_SIZE < osb->s_clustersize)
6833 end = PAGE_CACHE_SIZE;
6835 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6842 * This should populate the 1st page for us and mark
6845 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6851 page_end = PAGE_CACHE_SIZE;
6852 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6853 page_end = osb->s_clustersize;
6855 for (i = 0; i < num_pages; i++)
6856 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6857 pages[i], i > 0, &phys);
6860 spin_lock(&oi->ip_lock);
6861 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6862 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6863 spin_unlock(&oi->ip_lock);
6865 ocfs2_dinode_new_extent_list(inode, di);
6867 ocfs2_journal_dirty(handle, di_bh);
6871 * An error at this point should be extremely rare. If
6872 * this proves to be false, we could always re-build
6873 * the in-inode data from our pages.
6875 ret = ocfs2_dinode_insert_extent(osb, handle, inode, di_bh,
6876 0, block, 1, 0, NULL);
6882 inode->i_blocks = ocfs2_inode_sector_count(inode);
6886 ocfs2_commit_trans(osb, handle);
6890 ocfs2_free_alloc_context(data_ac);
6894 ocfs2_unlock_and_free_pages(pages, num_pages);
6902 * It is expected, that by the time you call this function,
6903 * inode->i_size and fe->i_size have been adjusted.
6905 * WARNING: This will kfree the truncate context
6907 int ocfs2_commit_truncate(struct ocfs2_super *osb,
6908 struct inode *inode,
6909 struct buffer_head *fe_bh,
6910 struct ocfs2_truncate_context *tc)
6912 int status, i, credits, tl_sem = 0;
6913 u32 clusters_to_del, new_highest_cpos, range;
6914 struct ocfs2_extent_list *el;
6915 handle_t *handle = NULL;
6916 struct inode *tl_inode = osb->osb_tl_inode;
6917 struct ocfs2_path *path = NULL;
6918 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6922 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
6923 i_size_read(inode));
6925 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
6932 ocfs2_extent_map_trunc(inode, new_highest_cpos);
6936 * Check that we still have allocation to delete.
6938 if (OCFS2_I(inode)->ip_clusters == 0) {
6944 * Truncate always works against the rightmost tree branch.
6946 status = ocfs2_find_path(inode, path, UINT_MAX);
6952 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6953 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
6956 * By now, el will point to the extent list on the bottom most
6957 * portion of this tree. Only the tail record is considered in
6960 * We handle the following cases, in order:
6961 * - empty extent: delete the remaining branch
6962 * - remove the entire record
6963 * - remove a partial record
6964 * - no record needs to be removed (truncate has completed)
6966 el = path_leaf_el(path);
6967 if (le16_to_cpu(el->l_next_free_rec) == 0) {
6968 ocfs2_error(inode->i_sb,
6969 "Inode %llu has empty extent block at %llu\n",
6970 (unsigned long long)OCFS2_I(inode)->ip_blkno,
6971 (unsigned long long)path_leaf_bh(path)->b_blocknr);
6976 i = le16_to_cpu(el->l_next_free_rec) - 1;
6977 range = le32_to_cpu(el->l_recs[i].e_cpos) +
6978 ocfs2_rec_clusters(el, &el->l_recs[i]);
6979 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
6980 clusters_to_del = 0;
6981 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
6982 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
6983 } else if (range > new_highest_cpos) {
6984 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
6985 le32_to_cpu(el->l_recs[i].e_cpos)) -
6992 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6993 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
6995 mutex_lock(&tl_inode->i_mutex);
6997 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6998 * record is free for use. If there isn't any, we flush to get
6999 * an empty truncate log. */
7000 if (ocfs2_truncate_log_needs_flush(osb)) {
7001 status = __ocfs2_flush_truncate_log(osb);
7008 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
7009 (struct ocfs2_dinode *)fe_bh->b_data,
7011 handle = ocfs2_start_trans(osb, credits);
7012 if (IS_ERR(handle)) {
7013 status = PTR_ERR(handle);
7019 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7026 mutex_unlock(&tl_inode->i_mutex);
7029 ocfs2_commit_trans(osb, handle);
7032 ocfs2_reinit_path(path, 1);
7035 * The check above will catch the case where we've truncated
7036 * away all allocation.
7042 ocfs2_schedule_truncate_log_flush(osb, 1);
7045 mutex_unlock(&tl_inode->i_mutex);
7048 ocfs2_commit_trans(osb, handle);
7050 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7052 ocfs2_free_path(path);
7054 /* This will drop the ext_alloc cluster lock for us */
7055 ocfs2_free_truncate_context(tc);
7062 * Expects the inode to already be locked.
7064 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7065 struct inode *inode,
7066 struct buffer_head *fe_bh,
7067 struct ocfs2_truncate_context **tc)
7070 unsigned int new_i_clusters;
7071 struct ocfs2_dinode *fe;
7072 struct ocfs2_extent_block *eb;
7073 struct buffer_head *last_eb_bh = NULL;
7079 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7080 i_size_read(inode));
7081 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7083 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7084 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7085 (unsigned long long)le64_to_cpu(fe->i_size));
7087 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7093 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7095 if (fe->id2.i_list.l_tree_depth) {
7096 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
7097 &last_eb_bh, OCFS2_BH_CACHED, inode);
7102 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7103 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
7104 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
7112 (*tc)->tc_last_eb_bh = last_eb_bh;
7118 ocfs2_free_truncate_context(*tc);
7126 * 'start' is inclusive, 'end' is not.
7128 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7129 unsigned int start, unsigned int end, int trunc)
7132 unsigned int numbytes;
7134 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7135 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7136 struct ocfs2_inline_data *idata = &di->id2.i_data;
7138 if (end > i_size_read(inode))
7139 end = i_size_read(inode);
7141 BUG_ON(start >= end);
7143 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7144 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7145 !ocfs2_supports_inline_data(osb)) {
7146 ocfs2_error(inode->i_sb,
7147 "Inline data flags for inode %llu don't agree! "
7148 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7149 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7150 le16_to_cpu(di->i_dyn_features),
7151 OCFS2_I(inode)->ip_dyn_features,
7152 osb->s_feature_incompat);
7157 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7158 if (IS_ERR(handle)) {
7159 ret = PTR_ERR(handle);
7164 ret = ocfs2_journal_access(handle, inode, di_bh,
7165 OCFS2_JOURNAL_ACCESS_WRITE);
7171 numbytes = end - start;
7172 memset(idata->id_data + start, 0, numbytes);
7175 * No need to worry about the data page here - it's been
7176 * truncated already and inline data doesn't need it for
7177 * pushing zero's to disk, so we'll let readpage pick it up
7181 i_size_write(inode, start);
7182 di->i_size = cpu_to_le64(start);
7185 inode->i_blocks = ocfs2_inode_sector_count(inode);
7186 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7188 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7189 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7191 ocfs2_journal_dirty(handle, di_bh);
7194 ocfs2_commit_trans(osb, handle);
7200 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7203 * The caller is responsible for completing deallocation
7204 * before freeing the context.
7206 if (tc->tc_dealloc.c_first_suballocator != NULL)
7208 "Truncate completion has non-empty dealloc context\n");
7210 if (tc->tc_last_eb_bh)
7211 brelse(tc->tc_last_eb_bh);