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"
54 * Operations for a specific extent tree type.
56 * To implement an on-disk btree (extent tree) type in ocfs2, add
57 * an ocfs2_extent_tree_operations structure and the matching
58 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
59 * for the allocation portion of the extent tree.
61 struct ocfs2_extent_tree_operations {
63 * last_eb_blk is the block number of the right most leaf extent
64 * block. Most on-disk structures containing an extent tree store
65 * this value for fast access. The ->eo_set_last_eb_blk() and
66 * ->eo_get_last_eb_blk() operations access this value. They are
69 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
71 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
74 * The on-disk structure usually keeps track of how many total
75 * clusters are stored in this extent tree. This function updates
76 * that value. new_clusters is the delta, and must be
77 * added to the total. Required.
79 void (*eo_update_clusters)(struct inode *inode,
80 struct ocfs2_extent_tree *et,
84 * If ->eo_insert_check() exists, it is called before rec is
85 * inserted into the extent tree. It is optional.
87 int (*eo_insert_check)(struct inode *inode,
88 struct ocfs2_extent_tree *et,
89 struct ocfs2_extent_rec *rec);
90 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
93 * --------------------------------------------------------------
94 * The remaining are internal to ocfs2_extent_tree and don't have
99 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
102 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
105 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
106 * it exists. If it does not, et->et_max_leaf_clusters is set
107 * to 0 (unlimited). Optional.
109 void (*eo_fill_max_leaf_clusters)(struct inode *inode,
110 struct ocfs2_extent_tree *et);
115 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
118 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
119 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
121 static void ocfs2_dinode_update_clusters(struct inode *inode,
122 struct ocfs2_extent_tree *et,
124 static int ocfs2_dinode_insert_check(struct inode *inode,
125 struct ocfs2_extent_tree *et,
126 struct ocfs2_extent_rec *rec);
127 static int ocfs2_dinode_sanity_check(struct inode *inode,
128 struct ocfs2_extent_tree *et);
129 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
130 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
131 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
132 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
133 .eo_update_clusters = ocfs2_dinode_update_clusters,
134 .eo_insert_check = ocfs2_dinode_insert_check,
135 .eo_sanity_check = ocfs2_dinode_sanity_check,
136 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
139 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
142 struct ocfs2_dinode *di = et->et_object;
144 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
145 di->i_last_eb_blk = cpu_to_le64(blkno);
148 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
150 struct ocfs2_dinode *di = et->et_object;
152 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
153 return le64_to_cpu(di->i_last_eb_blk);
156 static void ocfs2_dinode_update_clusters(struct inode *inode,
157 struct ocfs2_extent_tree *et,
160 struct ocfs2_dinode *di = et->et_object;
162 le32_add_cpu(&di->i_clusters, clusters);
163 spin_lock(&OCFS2_I(inode)->ip_lock);
164 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
165 spin_unlock(&OCFS2_I(inode)->ip_lock);
168 static int ocfs2_dinode_insert_check(struct inode *inode,
169 struct ocfs2_extent_tree *et,
170 struct ocfs2_extent_rec *rec)
172 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
174 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
175 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
176 (OCFS2_I(inode)->ip_clusters != rec->e_cpos),
177 "Device %s, asking for sparse allocation: inode %llu, "
178 "cpos %u, clusters %u\n",
180 (unsigned long long)OCFS2_I(inode)->ip_blkno,
182 OCFS2_I(inode)->ip_clusters);
187 static int ocfs2_dinode_sanity_check(struct inode *inode,
188 struct ocfs2_extent_tree *et)
190 struct ocfs2_dinode *di = et->et_object;
192 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
193 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
198 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
200 struct ocfs2_dinode *di = et->et_object;
202 et->et_root_el = &di->id2.i_list;
206 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
208 struct ocfs2_xattr_value_root *xv = et->et_object;
210 et->et_root_el = &xv->xr_list;
213 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
216 struct ocfs2_xattr_value_root *xv =
217 (struct ocfs2_xattr_value_root *)et->et_object;
219 xv->xr_last_eb_blk = cpu_to_le64(blkno);
222 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
224 struct ocfs2_xattr_value_root *xv =
225 (struct ocfs2_xattr_value_root *) et->et_object;
227 return le64_to_cpu(xv->xr_last_eb_blk);
230 static void ocfs2_xattr_value_update_clusters(struct inode *inode,
231 struct ocfs2_extent_tree *et,
234 struct ocfs2_xattr_value_root *xv =
235 (struct ocfs2_xattr_value_root *)et->et_object;
237 le32_add_cpu(&xv->xr_clusters, clusters);
240 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
241 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
242 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
243 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
244 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
247 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
249 struct ocfs2_xattr_block *xb = et->et_object;
251 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
254 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
255 struct ocfs2_extent_tree *et)
257 et->et_max_leaf_clusters =
258 ocfs2_clusters_for_bytes(inode->i_sb,
259 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
262 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
265 struct ocfs2_xattr_block *xb = et->et_object;
266 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
268 xt->xt_last_eb_blk = cpu_to_le64(blkno);
271 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
273 struct ocfs2_xattr_block *xb = et->et_object;
274 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
276 return le64_to_cpu(xt->xt_last_eb_blk);
279 static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
280 struct ocfs2_extent_tree *et,
283 struct ocfs2_xattr_block *xb = et->et_object;
285 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
288 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
289 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
290 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
291 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
292 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
293 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
296 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
298 struct buffer_head *bh,
300 struct ocfs2_extent_tree_operations *ops)
305 obj = (void *)bh->b_data;
308 et->et_ops->eo_fill_root_el(et);
309 if (!et->et_ops->eo_fill_max_leaf_clusters)
310 et->et_max_leaf_clusters = 0;
312 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
315 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
317 struct buffer_head *bh)
319 __ocfs2_init_extent_tree(et, inode, bh, NULL, &ocfs2_dinode_et_ops);
322 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
324 struct buffer_head *bh)
326 __ocfs2_init_extent_tree(et, inode, bh, NULL,
327 &ocfs2_xattr_tree_et_ops);
330 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
332 struct buffer_head *bh,
333 struct ocfs2_xattr_value_root *xv)
335 __ocfs2_init_extent_tree(et, inode, bh, xv,
336 &ocfs2_xattr_value_et_ops);
339 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
342 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
345 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
347 return et->et_ops->eo_get_last_eb_blk(et);
350 static inline void ocfs2_et_update_clusters(struct inode *inode,
351 struct ocfs2_extent_tree *et,
354 et->et_ops->eo_update_clusters(inode, et, clusters);
357 static inline int ocfs2_et_insert_check(struct inode *inode,
358 struct ocfs2_extent_tree *et,
359 struct ocfs2_extent_rec *rec)
363 if (et->et_ops->eo_insert_check)
364 ret = et->et_ops->eo_insert_check(inode, et, rec);
368 static inline int ocfs2_et_sanity_check(struct inode *inode,
369 struct ocfs2_extent_tree *et)
373 if (et->et_ops->eo_sanity_check)
374 ret = et->et_ops->eo_sanity_check(inode, et);
378 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
379 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
380 struct ocfs2_extent_block *eb);
383 * Structures which describe a path through a btree, and functions to
386 * The idea here is to be as generic as possible with the tree
389 struct ocfs2_path_item {
390 struct buffer_head *bh;
391 struct ocfs2_extent_list *el;
394 #define OCFS2_MAX_PATH_DEPTH 5
398 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
401 #define path_root_bh(_path) ((_path)->p_node[0].bh)
402 #define path_root_el(_path) ((_path)->p_node[0].el)
403 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
404 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
405 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
408 * Reset the actual path elements so that we can re-use the structure
409 * to build another path. Generally, this involves freeing the buffer
412 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
414 int i, start = 0, depth = 0;
415 struct ocfs2_path_item *node;
420 for(i = start; i < path_num_items(path); i++) {
421 node = &path->p_node[i];
429 * Tree depth may change during truncate, or insert. If we're
430 * keeping the root extent list, then make sure that our path
431 * structure reflects the proper depth.
434 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
436 path->p_tree_depth = depth;
439 static void ocfs2_free_path(struct ocfs2_path *path)
442 ocfs2_reinit_path(path, 0);
448 * All the elements of src into dest. After this call, src could be freed
449 * without affecting dest.
451 * Both paths should have the same root. Any non-root elements of dest
454 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
458 BUG_ON(path_root_bh(dest) != path_root_bh(src));
459 BUG_ON(path_root_el(dest) != path_root_el(src));
461 ocfs2_reinit_path(dest, 1);
463 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
464 dest->p_node[i].bh = src->p_node[i].bh;
465 dest->p_node[i].el = src->p_node[i].el;
467 if (dest->p_node[i].bh)
468 get_bh(dest->p_node[i].bh);
473 * Make the *dest path the same as src and re-initialize src path to
476 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
480 BUG_ON(path_root_bh(dest) != path_root_bh(src));
482 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
483 brelse(dest->p_node[i].bh);
485 dest->p_node[i].bh = src->p_node[i].bh;
486 dest->p_node[i].el = src->p_node[i].el;
488 src->p_node[i].bh = NULL;
489 src->p_node[i].el = NULL;
494 * Insert an extent block at given index.
496 * This will not take an additional reference on eb_bh.
498 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
499 struct buffer_head *eb_bh)
501 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
504 * Right now, no root bh is an extent block, so this helps
505 * catch code errors with dinode trees. The assertion can be
506 * safely removed if we ever need to insert extent block
507 * structures at the root.
511 path->p_node[index].bh = eb_bh;
512 path->p_node[index].el = &eb->h_list;
515 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
516 struct ocfs2_extent_list *root_el)
518 struct ocfs2_path *path;
520 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
522 path = kzalloc(sizeof(*path), GFP_NOFS);
524 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
526 path_root_bh(path) = root_bh;
527 path_root_el(path) = root_el;
534 * Convenience function to journal all components in a path.
536 static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
537 struct ocfs2_path *path)
544 for(i = 0; i < path_num_items(path); i++) {
545 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
546 OCFS2_JOURNAL_ACCESS_WRITE);
558 * Return the index of the extent record which contains cluster #v_cluster.
559 * -1 is returned if it was not found.
561 * Should work fine on interior and exterior nodes.
563 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
567 struct ocfs2_extent_rec *rec;
568 u32 rec_end, rec_start, clusters;
570 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
571 rec = &el->l_recs[i];
573 rec_start = le32_to_cpu(rec->e_cpos);
574 clusters = ocfs2_rec_clusters(el, rec);
576 rec_end = rec_start + clusters;
578 if (v_cluster >= rec_start && v_cluster < rec_end) {
587 enum ocfs2_contig_type {
596 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
597 * ocfs2_extent_contig only work properly against leaf nodes!
599 static int ocfs2_block_extent_contig(struct super_block *sb,
600 struct ocfs2_extent_rec *ext,
603 u64 blk_end = le64_to_cpu(ext->e_blkno);
605 blk_end += ocfs2_clusters_to_blocks(sb,
606 le16_to_cpu(ext->e_leaf_clusters));
608 return blkno == blk_end;
611 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
612 struct ocfs2_extent_rec *right)
616 left_range = le32_to_cpu(left->e_cpos) +
617 le16_to_cpu(left->e_leaf_clusters);
619 return (left_range == le32_to_cpu(right->e_cpos));
622 static enum ocfs2_contig_type
623 ocfs2_extent_contig(struct inode *inode,
624 struct ocfs2_extent_rec *ext,
625 struct ocfs2_extent_rec *insert_rec)
627 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
630 * Refuse to coalesce extent records with different flag
631 * fields - we don't want to mix unwritten extents with user
634 if (ext->e_flags != insert_rec->e_flags)
637 if (ocfs2_extents_adjacent(ext, insert_rec) &&
638 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
641 blkno = le64_to_cpu(ext->e_blkno);
642 if (ocfs2_extents_adjacent(insert_rec, ext) &&
643 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
650 * NOTE: We can have pretty much any combination of contiguousness and
653 * The usefulness of APPEND_TAIL is more in that it lets us know that
654 * we'll have to update the path to that leaf.
656 enum ocfs2_append_type {
661 enum ocfs2_split_type {
667 struct ocfs2_insert_type {
668 enum ocfs2_split_type ins_split;
669 enum ocfs2_append_type ins_appending;
670 enum ocfs2_contig_type ins_contig;
671 int ins_contig_index;
675 struct ocfs2_merge_ctxt {
676 enum ocfs2_contig_type c_contig_type;
677 int c_has_empty_extent;
678 int c_split_covers_rec;
681 static int ocfs2_validate_extent_block(struct super_block *sb,
682 struct buffer_head *bh)
684 struct ocfs2_extent_block *eb =
685 (struct ocfs2_extent_block *)bh->b_data;
687 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
689 "Extent block #%llu has bad signature %.*s",
690 (unsigned long long)bh->b_blocknr, 7,
695 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
697 "Extent block #%llu has an invalid h_blkno "
699 (unsigned long long)bh->b_blocknr,
700 (unsigned long long)le64_to_cpu(eb->h_blkno));
704 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
706 "Extent block #%llu has an invalid "
707 "h_fs_generation of #%u",
708 (unsigned long long)bh->b_blocknr,
709 le32_to_cpu(eb->h_fs_generation));
716 int ocfs2_read_extent_block(struct inode *inode, u64 eb_blkno,
717 struct buffer_head **bh)
720 struct buffer_head *tmp = *bh;
722 rc = ocfs2_read_block(inode, eb_blkno, &tmp);
726 rc = ocfs2_validate_extent_block(inode->i_sb, tmp);
732 /* If ocfs2_read_block() got us a new bh, pass it up. */
742 * How many free extents have we got before we need more meta data?
744 int ocfs2_num_free_extents(struct ocfs2_super *osb,
746 struct ocfs2_extent_tree *et)
749 struct ocfs2_extent_list *el = NULL;
750 struct ocfs2_extent_block *eb;
751 struct buffer_head *eb_bh = NULL;
757 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
760 retval = ocfs2_read_extent_block(inode, last_eb_blk, &eb_bh);
765 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
769 BUG_ON(el->l_tree_depth != 0);
771 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
779 /* expects array to already be allocated
781 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
784 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
788 struct ocfs2_alloc_context *meta_ac,
789 struct buffer_head *bhs[])
791 int count, status, i;
792 u16 suballoc_bit_start;
795 struct ocfs2_extent_block *eb;
800 while (count < wanted) {
801 status = ocfs2_claim_metadata(osb,
813 for(i = count; i < (num_got + count); i++) {
814 bhs[i] = sb_getblk(osb->sb, first_blkno);
815 if (bhs[i] == NULL) {
820 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
822 status = ocfs2_journal_access(handle, inode, bhs[i],
823 OCFS2_JOURNAL_ACCESS_CREATE);
829 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
830 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
831 /* Ok, setup the minimal stuff here. */
832 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
833 eb->h_blkno = cpu_to_le64(first_blkno);
834 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
835 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
836 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
838 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
840 suballoc_bit_start++;
843 /* We'll also be dirtied by the caller, so
844 * this isn't absolutely necessary. */
845 status = ocfs2_journal_dirty(handle, bhs[i]);
858 for(i = 0; i < wanted; i++) {
868 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
870 * Returns the sum of the rightmost extent rec logical offset and
873 * ocfs2_add_branch() uses this to determine what logical cluster
874 * value should be populated into the leftmost new branch records.
876 * ocfs2_shift_tree_depth() uses this to determine the # clusters
877 * value for the new topmost tree record.
879 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
883 i = le16_to_cpu(el->l_next_free_rec) - 1;
885 return le32_to_cpu(el->l_recs[i].e_cpos) +
886 ocfs2_rec_clusters(el, &el->l_recs[i]);
890 * Add an entire tree branch to our inode. eb_bh is the extent block
891 * to start at, if we don't want to start the branch at the dinode
894 * last_eb_bh is required as we have to update it's next_leaf pointer
895 * for the new last extent block.
897 * the new branch will be 'empty' in the sense that every block will
898 * contain a single record with cluster count == 0.
900 static int ocfs2_add_branch(struct ocfs2_super *osb,
903 struct ocfs2_extent_tree *et,
904 struct buffer_head *eb_bh,
905 struct buffer_head **last_eb_bh,
906 struct ocfs2_alloc_context *meta_ac)
908 int status, new_blocks, i;
909 u64 next_blkno, new_last_eb_blk;
910 struct buffer_head *bh;
911 struct buffer_head **new_eb_bhs = NULL;
912 struct ocfs2_extent_block *eb;
913 struct ocfs2_extent_list *eb_el;
914 struct ocfs2_extent_list *el;
919 BUG_ON(!last_eb_bh || !*last_eb_bh);
922 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
927 /* we never add a branch to a leaf. */
928 BUG_ON(!el->l_tree_depth);
930 new_blocks = le16_to_cpu(el->l_tree_depth);
932 /* allocate the number of new eb blocks we need */
933 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
941 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
942 meta_ac, new_eb_bhs);
948 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
949 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
951 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
952 * linked with the rest of the tree.
953 * conversly, new_eb_bhs[0] is the new bottommost leaf.
955 * when we leave the loop, new_last_eb_blk will point to the
956 * newest leaf, and next_blkno will point to the topmost extent
958 next_blkno = new_last_eb_blk = 0;
959 for(i = 0; i < new_blocks; i++) {
961 eb = (struct ocfs2_extent_block *) bh->b_data;
962 /* ocfs2_create_new_meta_bhs() should create it right! */
963 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
966 status = ocfs2_journal_access(handle, inode, bh,
967 OCFS2_JOURNAL_ACCESS_CREATE);
973 eb->h_next_leaf_blk = 0;
974 eb_el->l_tree_depth = cpu_to_le16(i);
975 eb_el->l_next_free_rec = cpu_to_le16(1);
977 * This actually counts as an empty extent as
980 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
981 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
983 * eb_el isn't always an interior node, but even leaf
984 * nodes want a zero'd flags and reserved field so
985 * this gets the whole 32 bits regardless of use.
987 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
988 if (!eb_el->l_tree_depth)
989 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
991 status = ocfs2_journal_dirty(handle, bh);
997 next_blkno = le64_to_cpu(eb->h_blkno);
1000 /* This is a bit hairy. We want to update up to three blocks
1001 * here without leaving any of them in an inconsistent state
1002 * in case of error. We don't have to worry about
1003 * journal_dirty erroring as it won't unless we've aborted the
1004 * handle (in which case we would never be here) so reserving
1005 * the write with journal_access is all we need to do. */
1006 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
1007 OCFS2_JOURNAL_ACCESS_WRITE);
1012 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
1013 OCFS2_JOURNAL_ACCESS_WRITE);
1019 status = ocfs2_journal_access(handle, inode, eb_bh,
1020 OCFS2_JOURNAL_ACCESS_WRITE);
1027 /* Link the new branch into the rest of the tree (el will
1028 * either be on the root_bh, or the extent block passed in. */
1029 i = le16_to_cpu(el->l_next_free_rec);
1030 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1031 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1032 el->l_recs[i].e_int_clusters = 0;
1033 le16_add_cpu(&el->l_next_free_rec, 1);
1035 /* fe needs a new last extent block pointer, as does the
1036 * next_leaf on the previously last-extent-block. */
1037 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1039 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1040 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1042 status = ocfs2_journal_dirty(handle, *last_eb_bh);
1045 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1049 status = ocfs2_journal_dirty(handle, eb_bh);
1055 * Some callers want to track the rightmost leaf so pass it
1058 brelse(*last_eb_bh);
1059 get_bh(new_eb_bhs[0]);
1060 *last_eb_bh = new_eb_bhs[0];
1065 for (i = 0; i < new_blocks; i++)
1066 brelse(new_eb_bhs[i]);
1075 * adds another level to the allocation tree.
1076 * returns back the new extent block so you can add a branch to it
1079 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
1081 struct inode *inode,
1082 struct ocfs2_extent_tree *et,
1083 struct ocfs2_alloc_context *meta_ac,
1084 struct buffer_head **ret_new_eb_bh)
1088 struct buffer_head *new_eb_bh = NULL;
1089 struct ocfs2_extent_block *eb;
1090 struct ocfs2_extent_list *root_el;
1091 struct ocfs2_extent_list *eb_el;
1095 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1102 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1103 /* ocfs2_create_new_meta_bhs() should create it right! */
1104 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1106 eb_el = &eb->h_list;
1107 root_el = et->et_root_el;
1109 status = ocfs2_journal_access(handle, inode, new_eb_bh,
1110 OCFS2_JOURNAL_ACCESS_CREATE);
1116 /* copy the root extent list data into the new extent block */
1117 eb_el->l_tree_depth = root_el->l_tree_depth;
1118 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1119 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1120 eb_el->l_recs[i] = root_el->l_recs[i];
1122 status = ocfs2_journal_dirty(handle, new_eb_bh);
1128 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
1129 OCFS2_JOURNAL_ACCESS_WRITE);
1135 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1137 /* update root_bh now */
1138 le16_add_cpu(&root_el->l_tree_depth, 1);
1139 root_el->l_recs[0].e_cpos = 0;
1140 root_el->l_recs[0].e_blkno = eb->h_blkno;
1141 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1142 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1143 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1144 root_el->l_next_free_rec = cpu_to_le16(1);
1146 /* If this is our 1st tree depth shift, then last_eb_blk
1147 * becomes the allocated extent block */
1148 if (root_el->l_tree_depth == cpu_to_le16(1))
1149 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1151 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1157 *ret_new_eb_bh = new_eb_bh;
1168 * Should only be called when there is no space left in any of the
1169 * leaf nodes. What we want to do is find the lowest tree depth
1170 * non-leaf extent block with room for new records. There are three
1171 * valid results of this search:
1173 * 1) a lowest extent block is found, then we pass it back in
1174 * *lowest_eb_bh and return '0'
1176 * 2) the search fails to find anything, but the root_el has room. We
1177 * pass NULL back in *lowest_eb_bh, but still return '0'
1179 * 3) the search fails to find anything AND the root_el is full, in
1180 * which case we return > 0
1182 * return status < 0 indicates an error.
1184 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1185 struct inode *inode,
1186 struct ocfs2_extent_tree *et,
1187 struct buffer_head **target_bh)
1191 struct ocfs2_extent_block *eb;
1192 struct ocfs2_extent_list *el;
1193 struct buffer_head *bh = NULL;
1194 struct buffer_head *lowest_bh = NULL;
1200 el = et->et_root_el;
1202 while(le16_to_cpu(el->l_tree_depth) > 1) {
1203 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1204 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
1205 "extent list (next_free_rec == 0)",
1206 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1210 i = le16_to_cpu(el->l_next_free_rec) - 1;
1211 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1213 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
1214 "list where extent # %d has no physical "
1216 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
1224 status = ocfs2_read_extent_block(inode, blkno, &bh);
1230 eb = (struct ocfs2_extent_block *) bh->b_data;
1233 if (le16_to_cpu(el->l_next_free_rec) <
1234 le16_to_cpu(el->l_count)) {
1241 /* If we didn't find one and the fe doesn't have any room,
1242 * then return '1' */
1243 el = et->et_root_el;
1244 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1247 *target_bh = lowest_bh;
1256 * Grow a b-tree so that it has more records.
1258 * We might shift the tree depth in which case existing paths should
1259 * be considered invalid.
1261 * Tree depth after the grow is returned via *final_depth.
1263 * *last_eb_bh will be updated by ocfs2_add_branch().
1265 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
1266 struct ocfs2_extent_tree *et, int *final_depth,
1267 struct buffer_head **last_eb_bh,
1268 struct ocfs2_alloc_context *meta_ac)
1271 struct ocfs2_extent_list *el = et->et_root_el;
1272 int depth = le16_to_cpu(el->l_tree_depth);
1273 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1274 struct buffer_head *bh = NULL;
1276 BUG_ON(meta_ac == NULL);
1278 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
1285 /* We traveled all the way to the bottom of the allocation tree
1286 * and didn't find room for any more extents - we need to add
1287 * another tree level */
1290 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1292 /* ocfs2_shift_tree_depth will return us a buffer with
1293 * the new extent block (so we can pass that to
1294 * ocfs2_add_branch). */
1295 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
1304 * Special case: we have room now if we shifted from
1305 * tree_depth 0, so no more work needs to be done.
1307 * We won't be calling add_branch, so pass
1308 * back *last_eb_bh as the new leaf. At depth
1309 * zero, it should always be null so there's
1310 * no reason to brelse.
1312 BUG_ON(*last_eb_bh);
1319 /* call ocfs2_add_branch to add the final part of the tree with
1321 mlog(0, "add branch. bh = %p\n", bh);
1322 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
1331 *final_depth = depth;
1337 * This function will discard the rightmost extent record.
1339 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1341 int next_free = le16_to_cpu(el->l_next_free_rec);
1342 int count = le16_to_cpu(el->l_count);
1343 unsigned int num_bytes;
1346 /* This will cause us to go off the end of our extent list. */
1347 BUG_ON(next_free >= count);
1349 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1351 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1354 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1355 struct ocfs2_extent_rec *insert_rec)
1357 int i, insert_index, next_free, has_empty, num_bytes;
1358 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1359 struct ocfs2_extent_rec *rec;
1361 next_free = le16_to_cpu(el->l_next_free_rec);
1362 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1366 /* The tree code before us didn't allow enough room in the leaf. */
1367 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1370 * The easiest way to approach this is to just remove the
1371 * empty extent and temporarily decrement next_free.
1375 * If next_free was 1 (only an empty extent), this
1376 * loop won't execute, which is fine. We still want
1377 * the decrement above to happen.
1379 for(i = 0; i < (next_free - 1); i++)
1380 el->l_recs[i] = el->l_recs[i+1];
1386 * Figure out what the new record index should be.
1388 for(i = 0; i < next_free; i++) {
1389 rec = &el->l_recs[i];
1391 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1396 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1397 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1399 BUG_ON(insert_index < 0);
1400 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1401 BUG_ON(insert_index > next_free);
1404 * No need to memmove if we're just adding to the tail.
1406 if (insert_index != next_free) {
1407 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1409 num_bytes = next_free - insert_index;
1410 num_bytes *= sizeof(struct ocfs2_extent_rec);
1411 memmove(&el->l_recs[insert_index + 1],
1412 &el->l_recs[insert_index],
1417 * Either we had an empty extent, and need to re-increment or
1418 * there was no empty extent on a non full rightmost leaf node,
1419 * in which case we still need to increment.
1422 el->l_next_free_rec = cpu_to_le16(next_free);
1424 * Make sure none of the math above just messed up our tree.
1426 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1428 el->l_recs[insert_index] = *insert_rec;
1432 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1434 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1436 BUG_ON(num_recs == 0);
1438 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1440 size = num_recs * sizeof(struct ocfs2_extent_rec);
1441 memmove(&el->l_recs[0], &el->l_recs[1], size);
1442 memset(&el->l_recs[num_recs], 0,
1443 sizeof(struct ocfs2_extent_rec));
1444 el->l_next_free_rec = cpu_to_le16(num_recs);
1449 * Create an empty extent record .
1451 * l_next_free_rec may be updated.
1453 * If an empty extent already exists do nothing.
1455 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1457 int next_free = le16_to_cpu(el->l_next_free_rec);
1459 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1464 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1467 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1468 "Asked to create an empty extent in a full list:\n"
1469 "count = %u, tree depth = %u",
1470 le16_to_cpu(el->l_count),
1471 le16_to_cpu(el->l_tree_depth));
1473 ocfs2_shift_records_right(el);
1476 le16_add_cpu(&el->l_next_free_rec, 1);
1477 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1481 * For a rotation which involves two leaf nodes, the "root node" is
1482 * the lowest level tree node which contains a path to both leafs. This
1483 * resulting set of information can be used to form a complete "subtree"
1485 * This function is passed two full paths from the dinode down to a
1486 * pair of adjacent leaves. It's task is to figure out which path
1487 * index contains the subtree root - this can be the root index itself
1488 * in a worst-case rotation.
1490 * The array index of the subtree root is passed back.
1492 static int ocfs2_find_subtree_root(struct inode *inode,
1493 struct ocfs2_path *left,
1494 struct ocfs2_path *right)
1499 * Check that the caller passed in two paths from the same tree.
1501 BUG_ON(path_root_bh(left) != path_root_bh(right));
1507 * The caller didn't pass two adjacent paths.
1509 mlog_bug_on_msg(i > left->p_tree_depth,
1510 "Inode %lu, left depth %u, right depth %u\n"
1511 "left leaf blk %llu, right leaf blk %llu\n",
1512 inode->i_ino, left->p_tree_depth,
1513 right->p_tree_depth,
1514 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1515 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1516 } while (left->p_node[i].bh->b_blocknr ==
1517 right->p_node[i].bh->b_blocknr);
1522 typedef void (path_insert_t)(void *, struct buffer_head *);
1525 * Traverse a btree path in search of cpos, starting at root_el.
1527 * This code can be called with a cpos larger than the tree, in which
1528 * case it will return the rightmost path.
1530 static int __ocfs2_find_path(struct inode *inode,
1531 struct ocfs2_extent_list *root_el, u32 cpos,
1532 path_insert_t *func, void *data)
1537 struct buffer_head *bh = NULL;
1538 struct ocfs2_extent_block *eb;
1539 struct ocfs2_extent_list *el;
1540 struct ocfs2_extent_rec *rec;
1541 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1544 while (el->l_tree_depth) {
1545 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1546 ocfs2_error(inode->i_sb,
1547 "Inode %llu has empty extent list at "
1549 (unsigned long long)oi->ip_blkno,
1550 le16_to_cpu(el->l_tree_depth));
1556 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1557 rec = &el->l_recs[i];
1560 * In the case that cpos is off the allocation
1561 * tree, this should just wind up returning the
1564 range = le32_to_cpu(rec->e_cpos) +
1565 ocfs2_rec_clusters(el, rec);
1566 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1570 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1572 ocfs2_error(inode->i_sb,
1573 "Inode %llu has bad blkno in extent list "
1574 "at depth %u (index %d)\n",
1575 (unsigned long long)oi->ip_blkno,
1576 le16_to_cpu(el->l_tree_depth), i);
1583 ret = ocfs2_read_extent_block(inode, blkno, &bh);
1589 eb = (struct ocfs2_extent_block *) bh->b_data;
1592 if (le16_to_cpu(el->l_next_free_rec) >
1593 le16_to_cpu(el->l_count)) {
1594 ocfs2_error(inode->i_sb,
1595 "Inode %llu has bad count in extent list "
1596 "at block %llu (next free=%u, count=%u)\n",
1597 (unsigned long long)oi->ip_blkno,
1598 (unsigned long long)bh->b_blocknr,
1599 le16_to_cpu(el->l_next_free_rec),
1600 le16_to_cpu(el->l_count));
1611 * Catch any trailing bh that the loop didn't handle.
1619 * Given an initialized path (that is, it has a valid root extent
1620 * list), this function will traverse the btree in search of the path
1621 * which would contain cpos.
1623 * The path traveled is recorded in the path structure.
1625 * Note that this will not do any comparisons on leaf node extent
1626 * records, so it will work fine in the case that we just added a tree
1629 struct find_path_data {
1631 struct ocfs2_path *path;
1633 static void find_path_ins(void *data, struct buffer_head *bh)
1635 struct find_path_data *fp = data;
1638 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1641 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1644 struct find_path_data data;
1648 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1649 find_path_ins, &data);
1652 static void find_leaf_ins(void *data, struct buffer_head *bh)
1654 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1655 struct ocfs2_extent_list *el = &eb->h_list;
1656 struct buffer_head **ret = data;
1658 /* We want to retain only the leaf block. */
1659 if (le16_to_cpu(el->l_tree_depth) == 0) {
1665 * Find the leaf block in the tree which would contain cpos. No
1666 * checking of the actual leaf is done.
1668 * Some paths want to call this instead of allocating a path structure
1669 * and calling ocfs2_find_path().
1671 * This function doesn't handle non btree extent lists.
1673 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1674 u32 cpos, struct buffer_head **leaf_bh)
1677 struct buffer_head *bh = NULL;
1679 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1691 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1693 * Basically, we've moved stuff around at the bottom of the tree and
1694 * we need to fix up the extent records above the changes to reflect
1697 * left_rec: the record on the left.
1698 * left_child_el: is the child list pointed to by left_rec
1699 * right_rec: the record to the right of left_rec
1700 * right_child_el: is the child list pointed to by right_rec
1702 * By definition, this only works on interior nodes.
1704 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1705 struct ocfs2_extent_list *left_child_el,
1706 struct ocfs2_extent_rec *right_rec,
1707 struct ocfs2_extent_list *right_child_el)
1709 u32 left_clusters, right_end;
1712 * Interior nodes never have holes. Their cpos is the cpos of
1713 * the leftmost record in their child list. Their cluster
1714 * count covers the full theoretical range of their child list
1715 * - the range between their cpos and the cpos of the record
1716 * immediately to their right.
1718 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1719 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1720 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1721 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1723 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1724 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1727 * Calculate the rightmost cluster count boundary before
1728 * moving cpos - we will need to adjust clusters after
1729 * updating e_cpos to keep the same highest cluster count.
1731 right_end = le32_to_cpu(right_rec->e_cpos);
1732 right_end += le32_to_cpu(right_rec->e_int_clusters);
1734 right_rec->e_cpos = left_rec->e_cpos;
1735 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1737 right_end -= le32_to_cpu(right_rec->e_cpos);
1738 right_rec->e_int_clusters = cpu_to_le32(right_end);
1742 * Adjust the adjacent root node records involved in a
1743 * rotation. left_el_blkno is passed in as a key so that we can easily
1744 * find it's index in the root list.
1746 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1747 struct ocfs2_extent_list *left_el,
1748 struct ocfs2_extent_list *right_el,
1753 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1754 le16_to_cpu(left_el->l_tree_depth));
1756 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1757 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1762 * The path walking code should have never returned a root and
1763 * two paths which are not adjacent.
1765 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1767 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1768 &root_el->l_recs[i + 1], right_el);
1772 * We've changed a leaf block (in right_path) and need to reflect that
1773 * change back up the subtree.
1775 * This happens in multiple places:
1776 * - When we've moved an extent record from the left path leaf to the right
1777 * path leaf to make room for an empty extent in the left path leaf.
1778 * - When our insert into the right path leaf is at the leftmost edge
1779 * and requires an update of the path immediately to it's left. This
1780 * can occur at the end of some types of rotation and appending inserts.
1781 * - When we've adjusted the last extent record in the left path leaf and the
1782 * 1st extent record in the right path leaf during cross extent block merge.
1784 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1785 struct ocfs2_path *left_path,
1786 struct ocfs2_path *right_path,
1790 struct ocfs2_extent_list *el, *left_el, *right_el;
1791 struct ocfs2_extent_rec *left_rec, *right_rec;
1792 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1795 * Update the counts and position values within all the
1796 * interior nodes to reflect the leaf rotation we just did.
1798 * The root node is handled below the loop.
1800 * We begin the loop with right_el and left_el pointing to the
1801 * leaf lists and work our way up.
1803 * NOTE: within this loop, left_el and right_el always refer
1804 * to the *child* lists.
1806 left_el = path_leaf_el(left_path);
1807 right_el = path_leaf_el(right_path);
1808 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1809 mlog(0, "Adjust records at index %u\n", i);
1812 * One nice property of knowing that all of these
1813 * nodes are below the root is that we only deal with
1814 * the leftmost right node record and the rightmost
1817 el = left_path->p_node[i].el;
1818 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1819 left_rec = &el->l_recs[idx];
1821 el = right_path->p_node[i].el;
1822 right_rec = &el->l_recs[0];
1824 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1827 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1831 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1836 * Setup our list pointers now so that the current
1837 * parents become children in the next iteration.
1839 left_el = left_path->p_node[i].el;
1840 right_el = right_path->p_node[i].el;
1844 * At the root node, adjust the two adjacent records which
1845 * begin our path to the leaves.
1848 el = left_path->p_node[subtree_index].el;
1849 left_el = left_path->p_node[subtree_index + 1].el;
1850 right_el = right_path->p_node[subtree_index + 1].el;
1852 ocfs2_adjust_root_records(el, left_el, right_el,
1853 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1855 root_bh = left_path->p_node[subtree_index].bh;
1857 ret = ocfs2_journal_dirty(handle, root_bh);
1862 static int ocfs2_rotate_subtree_right(struct inode *inode,
1864 struct ocfs2_path *left_path,
1865 struct ocfs2_path *right_path,
1869 struct buffer_head *right_leaf_bh;
1870 struct buffer_head *left_leaf_bh = NULL;
1871 struct buffer_head *root_bh;
1872 struct ocfs2_extent_list *right_el, *left_el;
1873 struct ocfs2_extent_rec move_rec;
1875 left_leaf_bh = path_leaf_bh(left_path);
1876 left_el = path_leaf_el(left_path);
1878 if (left_el->l_next_free_rec != left_el->l_count) {
1879 ocfs2_error(inode->i_sb,
1880 "Inode %llu has non-full interior leaf node %llu"
1882 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1883 (unsigned long long)left_leaf_bh->b_blocknr,
1884 le16_to_cpu(left_el->l_next_free_rec));
1889 * This extent block may already have an empty record, so we
1890 * return early if so.
1892 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1895 root_bh = left_path->p_node[subtree_index].bh;
1896 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1898 ret = ocfs2_journal_access(handle, inode, root_bh,
1899 OCFS2_JOURNAL_ACCESS_WRITE);
1905 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1906 ret = ocfs2_journal_access(handle, inode,
1907 right_path->p_node[i].bh,
1908 OCFS2_JOURNAL_ACCESS_WRITE);
1914 ret = ocfs2_journal_access(handle, inode,
1915 left_path->p_node[i].bh,
1916 OCFS2_JOURNAL_ACCESS_WRITE);
1923 right_leaf_bh = path_leaf_bh(right_path);
1924 right_el = path_leaf_el(right_path);
1926 /* This is a code error, not a disk corruption. */
1927 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1928 "because rightmost leaf block %llu is empty\n",
1929 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1930 (unsigned long long)right_leaf_bh->b_blocknr);
1932 ocfs2_create_empty_extent(right_el);
1934 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1940 /* Do the copy now. */
1941 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1942 move_rec = left_el->l_recs[i];
1943 right_el->l_recs[0] = move_rec;
1946 * Clear out the record we just copied and shift everything
1947 * over, leaving an empty extent in the left leaf.
1949 * We temporarily subtract from next_free_rec so that the
1950 * shift will lose the tail record (which is now defunct).
1952 le16_add_cpu(&left_el->l_next_free_rec, -1);
1953 ocfs2_shift_records_right(left_el);
1954 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1955 le16_add_cpu(&left_el->l_next_free_rec, 1);
1957 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1963 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1971 * Given a full path, determine what cpos value would return us a path
1972 * containing the leaf immediately to the left of the current one.
1974 * Will return zero if the path passed in is already the leftmost path.
1976 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1977 struct ocfs2_path *path, u32 *cpos)
1981 struct ocfs2_extent_list *el;
1983 BUG_ON(path->p_tree_depth == 0);
1987 blkno = path_leaf_bh(path)->b_blocknr;
1989 /* Start at the tree node just above the leaf and work our way up. */
1990 i = path->p_tree_depth - 1;
1992 el = path->p_node[i].el;
1995 * Find the extent record just before the one in our
1998 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1999 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2003 * We've determined that the
2004 * path specified is already
2005 * the leftmost one - return a
2011 * The leftmost record points to our
2012 * leaf - we need to travel up the
2018 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2019 *cpos = *cpos + ocfs2_rec_clusters(el,
2020 &el->l_recs[j - 1]);
2027 * If we got here, we never found a valid node where
2028 * the tree indicated one should be.
2031 "Invalid extent tree at extent block %llu\n",
2032 (unsigned long long)blkno);
2037 blkno = path->p_node[i].bh->b_blocknr;
2046 * Extend the transaction by enough credits to complete the rotation,
2047 * and still leave at least the original number of credits allocated
2048 * to this transaction.
2050 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2052 struct ocfs2_path *path)
2054 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2056 if (handle->h_buffer_credits < credits)
2057 return ocfs2_extend_trans(handle, credits);
2063 * Trap the case where we're inserting into the theoretical range past
2064 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2065 * whose cpos is less than ours into the right leaf.
2067 * It's only necessary to look at the rightmost record of the left
2068 * leaf because the logic that calls us should ensure that the
2069 * theoretical ranges in the path components above the leaves are
2072 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2075 struct ocfs2_extent_list *left_el;
2076 struct ocfs2_extent_rec *rec;
2079 left_el = path_leaf_el(left_path);
2080 next_free = le16_to_cpu(left_el->l_next_free_rec);
2081 rec = &left_el->l_recs[next_free - 1];
2083 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2088 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2090 int next_free = le16_to_cpu(el->l_next_free_rec);
2092 struct ocfs2_extent_rec *rec;
2097 rec = &el->l_recs[0];
2098 if (ocfs2_is_empty_extent(rec)) {
2102 rec = &el->l_recs[1];
2105 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2106 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2112 * Rotate all the records in a btree right one record, starting at insert_cpos.
2114 * The path to the rightmost leaf should be passed in.
2116 * The array is assumed to be large enough to hold an entire path (tree depth).
2118 * Upon succesful return from this function:
2120 * - The 'right_path' array will contain a path to the leaf block
2121 * whose range contains e_cpos.
2122 * - That leaf block will have a single empty extent in list index 0.
2123 * - In the case that the rotation requires a post-insert update,
2124 * *ret_left_path will contain a valid path which can be passed to
2125 * ocfs2_insert_path().
2127 static int ocfs2_rotate_tree_right(struct inode *inode,
2129 enum ocfs2_split_type split,
2131 struct ocfs2_path *right_path,
2132 struct ocfs2_path **ret_left_path)
2134 int ret, start, orig_credits = handle->h_buffer_credits;
2136 struct ocfs2_path *left_path = NULL;
2138 *ret_left_path = NULL;
2140 left_path = ocfs2_new_path(path_root_bh(right_path),
2141 path_root_el(right_path));
2148 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2154 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2157 * What we want to do here is:
2159 * 1) Start with the rightmost path.
2161 * 2) Determine a path to the leaf block directly to the left
2164 * 3) Determine the 'subtree root' - the lowest level tree node
2165 * which contains a path to both leaves.
2167 * 4) Rotate the subtree.
2169 * 5) Find the next subtree by considering the left path to be
2170 * the new right path.
2172 * The check at the top of this while loop also accepts
2173 * insert_cpos == cpos because cpos is only a _theoretical_
2174 * value to get us the left path - insert_cpos might very well
2175 * be filling that hole.
2177 * Stop at a cpos of '0' because we either started at the
2178 * leftmost branch (i.e., a tree with one branch and a
2179 * rotation inside of it), or we've gone as far as we can in
2180 * rotating subtrees.
2182 while (cpos && insert_cpos <= cpos) {
2183 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2186 ret = ocfs2_find_path(inode, left_path, cpos);
2192 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2193 path_leaf_bh(right_path),
2194 "Inode %lu: error during insert of %u "
2195 "(left path cpos %u) results in two identical "
2196 "paths ending at %llu\n",
2197 inode->i_ino, insert_cpos, cpos,
2198 (unsigned long long)
2199 path_leaf_bh(left_path)->b_blocknr);
2201 if (split == SPLIT_NONE &&
2202 ocfs2_rotate_requires_path_adjustment(left_path,
2206 * We've rotated the tree as much as we
2207 * should. The rest is up to
2208 * ocfs2_insert_path() to complete, after the
2209 * record insertion. We indicate this
2210 * situation by returning the left path.
2212 * The reason we don't adjust the records here
2213 * before the record insert is that an error
2214 * later might break the rule where a parent
2215 * record e_cpos will reflect the actual
2216 * e_cpos of the 1st nonempty record of the
2219 *ret_left_path = left_path;
2223 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2225 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2227 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2228 right_path->p_tree_depth);
2230 ret = ocfs2_extend_rotate_transaction(handle, start,
2231 orig_credits, right_path);
2237 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2244 if (split != SPLIT_NONE &&
2245 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2248 * A rotate moves the rightmost left leaf
2249 * record over to the leftmost right leaf
2250 * slot. If we're doing an extent split
2251 * instead of a real insert, then we have to
2252 * check that the extent to be split wasn't
2253 * just moved over. If it was, then we can
2254 * exit here, passing left_path back -
2255 * ocfs2_split_extent() is smart enough to
2256 * search both leaves.
2258 *ret_left_path = left_path;
2263 * There is no need to re-read the next right path
2264 * as we know that it'll be our current left
2265 * path. Optimize by copying values instead.
2267 ocfs2_mv_path(right_path, left_path);
2269 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2278 ocfs2_free_path(left_path);
2284 static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2285 struct ocfs2_path *path)
2288 struct ocfs2_extent_rec *rec;
2289 struct ocfs2_extent_list *el;
2290 struct ocfs2_extent_block *eb;
2293 /* Path should always be rightmost. */
2294 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2295 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2298 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2299 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2300 rec = &el->l_recs[idx];
2301 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2303 for (i = 0; i < path->p_tree_depth; i++) {
2304 el = path->p_node[i].el;
2305 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2306 rec = &el->l_recs[idx];
2308 rec->e_int_clusters = cpu_to_le32(range);
2309 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2311 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2315 static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2316 struct ocfs2_cached_dealloc_ctxt *dealloc,
2317 struct ocfs2_path *path, int unlink_start)
2320 struct ocfs2_extent_block *eb;
2321 struct ocfs2_extent_list *el;
2322 struct buffer_head *bh;
2324 for(i = unlink_start; i < path_num_items(path); i++) {
2325 bh = path->p_node[i].bh;
2327 eb = (struct ocfs2_extent_block *)bh->b_data;
2329 * Not all nodes might have had their final count
2330 * decremented by the caller - handle this here.
2333 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2335 "Inode %llu, attempted to remove extent block "
2336 "%llu with %u records\n",
2337 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2338 (unsigned long long)le64_to_cpu(eb->h_blkno),
2339 le16_to_cpu(el->l_next_free_rec));
2341 ocfs2_journal_dirty(handle, bh);
2342 ocfs2_remove_from_cache(inode, bh);
2346 el->l_next_free_rec = 0;
2347 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2349 ocfs2_journal_dirty(handle, bh);
2351 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2355 ocfs2_remove_from_cache(inode, bh);
2359 static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2360 struct ocfs2_path *left_path,
2361 struct ocfs2_path *right_path,
2363 struct ocfs2_cached_dealloc_ctxt *dealloc)
2366 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2367 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2368 struct ocfs2_extent_list *el;
2369 struct ocfs2_extent_block *eb;
2371 el = path_leaf_el(left_path);
2373 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2375 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2376 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2379 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2381 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2382 le16_add_cpu(&root_el->l_next_free_rec, -1);
2384 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2385 eb->h_next_leaf_blk = 0;
2387 ocfs2_journal_dirty(handle, root_bh);
2388 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2390 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2394 static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2395 struct ocfs2_path *left_path,
2396 struct ocfs2_path *right_path,
2398 struct ocfs2_cached_dealloc_ctxt *dealloc,
2400 struct ocfs2_extent_tree *et)
2402 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2403 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2404 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2405 struct ocfs2_extent_block *eb;
2409 right_leaf_el = path_leaf_el(right_path);
2410 left_leaf_el = path_leaf_el(left_path);
2411 root_bh = left_path->p_node[subtree_index].bh;
2412 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2414 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2417 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2418 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2420 * It's legal for us to proceed if the right leaf is
2421 * the rightmost one and it has an empty extent. There
2422 * are two cases to handle - whether the leaf will be
2423 * empty after removal or not. If the leaf isn't empty
2424 * then just remove the empty extent up front. The
2425 * next block will handle empty leaves by flagging
2428 * Non rightmost leaves will throw -EAGAIN and the
2429 * caller can manually move the subtree and retry.
2432 if (eb->h_next_leaf_blk != 0ULL)
2435 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2436 ret = ocfs2_journal_access(handle, inode,
2437 path_leaf_bh(right_path),
2438 OCFS2_JOURNAL_ACCESS_WRITE);
2444 ocfs2_remove_empty_extent(right_leaf_el);
2446 right_has_empty = 1;
2449 if (eb->h_next_leaf_blk == 0ULL &&
2450 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2452 * We have to update i_last_eb_blk during the meta
2455 ret = ocfs2_journal_access(handle, inode, et_root_bh,
2456 OCFS2_JOURNAL_ACCESS_WRITE);
2462 del_right_subtree = 1;
2466 * Getting here with an empty extent in the right path implies
2467 * that it's the rightmost path and will be deleted.
2469 BUG_ON(right_has_empty && !del_right_subtree);
2471 ret = ocfs2_journal_access(handle, inode, root_bh,
2472 OCFS2_JOURNAL_ACCESS_WRITE);
2478 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2479 ret = ocfs2_journal_access(handle, inode,
2480 right_path->p_node[i].bh,
2481 OCFS2_JOURNAL_ACCESS_WRITE);
2487 ret = ocfs2_journal_access(handle, inode,
2488 left_path->p_node[i].bh,
2489 OCFS2_JOURNAL_ACCESS_WRITE);
2496 if (!right_has_empty) {
2498 * Only do this if we're moving a real
2499 * record. Otherwise, the action is delayed until
2500 * after removal of the right path in which case we
2501 * can do a simple shift to remove the empty extent.
2503 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2504 memset(&right_leaf_el->l_recs[0], 0,
2505 sizeof(struct ocfs2_extent_rec));
2507 if (eb->h_next_leaf_blk == 0ULL) {
2509 * Move recs over to get rid of empty extent, decrease
2510 * next_free. This is allowed to remove the last
2511 * extent in our leaf (setting l_next_free_rec to
2512 * zero) - the delete code below won't care.
2514 ocfs2_remove_empty_extent(right_leaf_el);
2517 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2520 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2524 if (del_right_subtree) {
2525 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2526 subtree_index, dealloc);
2527 ocfs2_update_edge_lengths(inode, handle, left_path);
2529 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2530 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2533 * Removal of the extent in the left leaf was skipped
2534 * above so we could delete the right path
2537 if (right_has_empty)
2538 ocfs2_remove_empty_extent(left_leaf_el);
2540 ret = ocfs2_journal_dirty(handle, et_root_bh);
2546 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2554 * Given a full path, determine what cpos value would return us a path
2555 * containing the leaf immediately to the right of the current one.
2557 * Will return zero if the path passed in is already the rightmost path.
2559 * This looks similar, but is subtly different to
2560 * ocfs2_find_cpos_for_left_leaf().
2562 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2563 struct ocfs2_path *path, u32 *cpos)
2567 struct ocfs2_extent_list *el;
2571 if (path->p_tree_depth == 0)
2574 blkno = path_leaf_bh(path)->b_blocknr;
2576 /* Start at the tree node just above the leaf and work our way up. */
2577 i = path->p_tree_depth - 1;
2581 el = path->p_node[i].el;
2584 * Find the extent record just after the one in our
2587 next_free = le16_to_cpu(el->l_next_free_rec);
2588 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2589 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2590 if (j == (next_free - 1)) {
2593 * We've determined that the
2594 * path specified is already
2595 * the rightmost one - return a
2601 * The rightmost record points to our
2602 * leaf - we need to travel up the
2608 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2614 * If we got here, we never found a valid node where
2615 * the tree indicated one should be.
2618 "Invalid extent tree at extent block %llu\n",
2619 (unsigned long long)blkno);
2624 blkno = path->p_node[i].bh->b_blocknr;
2632 static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2634 struct buffer_head *bh,
2635 struct ocfs2_extent_list *el)
2639 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2642 ret = ocfs2_journal_access(handle, inode, bh,
2643 OCFS2_JOURNAL_ACCESS_WRITE);
2649 ocfs2_remove_empty_extent(el);
2651 ret = ocfs2_journal_dirty(handle, bh);
2659 static int __ocfs2_rotate_tree_left(struct inode *inode,
2660 handle_t *handle, int orig_credits,
2661 struct ocfs2_path *path,
2662 struct ocfs2_cached_dealloc_ctxt *dealloc,
2663 struct ocfs2_path **empty_extent_path,
2664 struct ocfs2_extent_tree *et)
2666 int ret, subtree_root, deleted;
2668 struct ocfs2_path *left_path = NULL;
2669 struct ocfs2_path *right_path = NULL;
2671 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2673 *empty_extent_path = NULL;
2675 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2682 left_path = ocfs2_new_path(path_root_bh(path),
2683 path_root_el(path));
2690 ocfs2_cp_path(left_path, path);
2692 right_path = ocfs2_new_path(path_root_bh(path),
2693 path_root_el(path));
2700 while (right_cpos) {
2701 ret = ocfs2_find_path(inode, right_path, right_cpos);
2707 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2710 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2712 (unsigned long long)
2713 right_path->p_node[subtree_root].bh->b_blocknr,
2714 right_path->p_tree_depth);
2716 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2717 orig_credits, left_path);
2724 * Caller might still want to make changes to the
2725 * tree root, so re-add it to the journal here.
2727 ret = ocfs2_journal_access(handle, inode,
2728 path_root_bh(left_path),
2729 OCFS2_JOURNAL_ACCESS_WRITE);
2735 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2736 right_path, subtree_root,
2737 dealloc, &deleted, et);
2738 if (ret == -EAGAIN) {
2740 * The rotation has to temporarily stop due to
2741 * the right subtree having an empty
2742 * extent. Pass it back to the caller for a
2745 *empty_extent_path = right_path;
2755 * The subtree rotate might have removed records on
2756 * the rightmost edge. If so, then rotation is
2762 ocfs2_mv_path(left_path, right_path);
2764 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2773 ocfs2_free_path(right_path);
2774 ocfs2_free_path(left_path);
2779 static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2780 struct ocfs2_path *path,
2781 struct ocfs2_cached_dealloc_ctxt *dealloc,
2782 struct ocfs2_extent_tree *et)
2784 int ret, subtree_index;
2786 struct ocfs2_path *left_path = NULL;
2787 struct ocfs2_extent_block *eb;
2788 struct ocfs2_extent_list *el;
2791 ret = ocfs2_et_sanity_check(inode, et);
2795 * There's two ways we handle this depending on
2796 * whether path is the only existing one.
2798 ret = ocfs2_extend_rotate_transaction(handle, 0,
2799 handle->h_buffer_credits,
2806 ret = ocfs2_journal_access_path(inode, handle, path);
2812 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2820 * We have a path to the left of this one - it needs
2823 left_path = ocfs2_new_path(path_root_bh(path),
2824 path_root_el(path));
2831 ret = ocfs2_find_path(inode, left_path, cpos);
2837 ret = ocfs2_journal_access_path(inode, handle, left_path);
2843 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2845 ocfs2_unlink_subtree(inode, handle, left_path, path,
2846 subtree_index, dealloc);
2847 ocfs2_update_edge_lengths(inode, handle, left_path);
2849 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2850 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2853 * 'path' is also the leftmost path which
2854 * means it must be the only one. This gets
2855 * handled differently because we want to
2856 * revert the inode back to having extents
2859 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2861 el = et->et_root_el;
2862 el->l_tree_depth = 0;
2863 el->l_next_free_rec = 0;
2864 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2866 ocfs2_et_set_last_eb_blk(et, 0);
2869 ocfs2_journal_dirty(handle, path_root_bh(path));
2872 ocfs2_free_path(left_path);
2877 * Left rotation of btree records.
2879 * In many ways, this is (unsurprisingly) the opposite of right
2880 * rotation. We start at some non-rightmost path containing an empty
2881 * extent in the leaf block. The code works its way to the rightmost
2882 * path by rotating records to the left in every subtree.
2884 * This is used by any code which reduces the number of extent records
2885 * in a leaf. After removal, an empty record should be placed in the
2886 * leftmost list position.
2888 * This won't handle a length update of the rightmost path records if
2889 * the rightmost tree leaf record is removed so the caller is
2890 * responsible for detecting and correcting that.
2892 static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2893 struct ocfs2_path *path,
2894 struct ocfs2_cached_dealloc_ctxt *dealloc,
2895 struct ocfs2_extent_tree *et)
2897 int ret, orig_credits = handle->h_buffer_credits;
2898 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2899 struct ocfs2_extent_block *eb;
2900 struct ocfs2_extent_list *el;
2902 el = path_leaf_el(path);
2903 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2906 if (path->p_tree_depth == 0) {
2907 rightmost_no_delete:
2909 * Inline extents. This is trivially handled, so do
2912 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2914 path_leaf_el(path));
2921 * Handle rightmost branch now. There's several cases:
2922 * 1) simple rotation leaving records in there. That's trivial.
2923 * 2) rotation requiring a branch delete - there's no more
2924 * records left. Two cases of this:
2925 * a) There are branches to the left.
2926 * b) This is also the leftmost (the only) branch.
2928 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2929 * 2a) we need the left branch so that we can update it with the unlink
2930 * 2b) we need to bring the inode back to inline extents.
2933 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2935 if (eb->h_next_leaf_blk == 0) {
2937 * This gets a bit tricky if we're going to delete the
2938 * rightmost path. Get the other cases out of the way
2941 if (le16_to_cpu(el->l_next_free_rec) > 1)
2942 goto rightmost_no_delete;
2944 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2946 ocfs2_error(inode->i_sb,
2947 "Inode %llu has empty extent block at %llu",
2948 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2949 (unsigned long long)le64_to_cpu(eb->h_blkno));
2954 * XXX: The caller can not trust "path" any more after
2955 * this as it will have been deleted. What do we do?
2957 * In theory the rotate-for-merge code will never get
2958 * here because it'll always ask for a rotate in a
2962 ret = ocfs2_remove_rightmost_path(inode, handle, path,
2970 * Now we can loop, remembering the path we get from -EAGAIN
2971 * and restarting from there.
2974 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
2975 dealloc, &restart_path, et);
2976 if (ret && ret != -EAGAIN) {
2981 while (ret == -EAGAIN) {
2982 tmp_path = restart_path;
2983 restart_path = NULL;
2985 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2988 if (ret && ret != -EAGAIN) {
2993 ocfs2_free_path(tmp_path);
3001 ocfs2_free_path(tmp_path);
3002 ocfs2_free_path(restart_path);
3006 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3009 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3012 if (rec->e_leaf_clusters == 0) {
3014 * We consumed all of the merged-from record. An empty
3015 * extent cannot exist anywhere but the 1st array
3016 * position, so move things over if the merged-from
3017 * record doesn't occupy that position.
3019 * This creates a new empty extent so the caller
3020 * should be smart enough to have removed any existing
3024 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3025 size = index * sizeof(struct ocfs2_extent_rec);
3026 memmove(&el->l_recs[1], &el->l_recs[0], size);
3030 * Always memset - the caller doesn't check whether it
3031 * created an empty extent, so there could be junk in
3034 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3038 static int ocfs2_get_right_path(struct inode *inode,
3039 struct ocfs2_path *left_path,
3040 struct ocfs2_path **ret_right_path)
3044 struct ocfs2_path *right_path = NULL;
3045 struct ocfs2_extent_list *left_el;
3047 *ret_right_path = NULL;
3049 /* This function shouldn't be called for non-trees. */
3050 BUG_ON(left_path->p_tree_depth == 0);
3052 left_el = path_leaf_el(left_path);
3053 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3055 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3062 /* This function shouldn't be called for the rightmost leaf. */
3063 BUG_ON(right_cpos == 0);
3065 right_path = ocfs2_new_path(path_root_bh(left_path),
3066 path_root_el(left_path));
3073 ret = ocfs2_find_path(inode, right_path, right_cpos);
3079 *ret_right_path = right_path;
3082 ocfs2_free_path(right_path);
3087 * Remove split_rec clusters from the record at index and merge them
3088 * onto the beginning of the record "next" to it.
3089 * For index < l_count - 1, the next means the extent rec at index + 1.
3090 * For index == l_count - 1, the "next" means the 1st extent rec of the
3091 * next extent block.
3093 static int ocfs2_merge_rec_right(struct inode *inode,
3094 struct ocfs2_path *left_path,
3096 struct ocfs2_extent_rec *split_rec,
3099 int ret, next_free, i;
3100 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3101 struct ocfs2_extent_rec *left_rec;
3102 struct ocfs2_extent_rec *right_rec;
3103 struct ocfs2_extent_list *right_el;
3104 struct ocfs2_path *right_path = NULL;
3105 int subtree_index = 0;
3106 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3107 struct buffer_head *bh = path_leaf_bh(left_path);
3108 struct buffer_head *root_bh = NULL;
3110 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3111 left_rec = &el->l_recs[index];
3113 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3114 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3115 /* we meet with a cross extent block merge. */
3116 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3122 right_el = path_leaf_el(right_path);
3123 next_free = le16_to_cpu(right_el->l_next_free_rec);
3124 BUG_ON(next_free <= 0);
3125 right_rec = &right_el->l_recs[0];
3126 if (ocfs2_is_empty_extent(right_rec)) {
3127 BUG_ON(next_free <= 1);
3128 right_rec = &right_el->l_recs[1];
3131 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3132 le16_to_cpu(left_rec->e_leaf_clusters) !=
3133 le32_to_cpu(right_rec->e_cpos));
3135 subtree_index = ocfs2_find_subtree_root(inode,
3136 left_path, right_path);
3138 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3139 handle->h_buffer_credits,
3146 root_bh = left_path->p_node[subtree_index].bh;
3147 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3149 ret = ocfs2_journal_access(handle, inode, root_bh,
3150 OCFS2_JOURNAL_ACCESS_WRITE);
3156 for (i = subtree_index + 1;
3157 i < path_num_items(right_path); i++) {
3158 ret = ocfs2_journal_access(handle, inode,
3159 right_path->p_node[i].bh,
3160 OCFS2_JOURNAL_ACCESS_WRITE);
3166 ret = ocfs2_journal_access(handle, inode,
3167 left_path->p_node[i].bh,
3168 OCFS2_JOURNAL_ACCESS_WRITE);
3176 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3177 right_rec = &el->l_recs[index + 1];
3180 ret = ocfs2_journal_access(handle, inode, bh,
3181 OCFS2_JOURNAL_ACCESS_WRITE);
3187 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3189 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3190 le64_add_cpu(&right_rec->e_blkno,
3191 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3192 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3194 ocfs2_cleanup_merge(el, index);
3196 ret = ocfs2_journal_dirty(handle, bh);
3201 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3205 ocfs2_complete_edge_insert(inode, handle, left_path,
3206 right_path, subtree_index);
3210 ocfs2_free_path(right_path);
3214 static int ocfs2_get_left_path(struct inode *inode,
3215 struct ocfs2_path *right_path,
3216 struct ocfs2_path **ret_left_path)
3220 struct ocfs2_path *left_path = NULL;
3222 *ret_left_path = NULL;
3224 /* This function shouldn't be called for non-trees. */
3225 BUG_ON(right_path->p_tree_depth == 0);
3227 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3228 right_path, &left_cpos);
3234 /* This function shouldn't be called for the leftmost leaf. */
3235 BUG_ON(left_cpos == 0);
3237 left_path = ocfs2_new_path(path_root_bh(right_path),
3238 path_root_el(right_path));
3245 ret = ocfs2_find_path(inode, left_path, left_cpos);
3251 *ret_left_path = left_path;
3254 ocfs2_free_path(left_path);
3259 * Remove split_rec clusters from the record at index and merge them
3260 * onto the tail of the record "before" it.
3261 * For index > 0, the "before" means the extent rec at index - 1.
3263 * For index == 0, the "before" means the last record of the previous
3264 * extent block. And there is also a situation that we may need to
3265 * remove the rightmost leaf extent block in the right_path and change
3266 * the right path to indicate the new rightmost path.
3268 static int ocfs2_merge_rec_left(struct inode *inode,
3269 struct ocfs2_path *right_path,
3271 struct ocfs2_extent_rec *split_rec,
3272 struct ocfs2_cached_dealloc_ctxt *dealloc,
3273 struct ocfs2_extent_tree *et,
3276 int ret, i, subtree_index = 0, has_empty_extent = 0;
3277 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3278 struct ocfs2_extent_rec *left_rec;
3279 struct ocfs2_extent_rec *right_rec;
3280 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3281 struct buffer_head *bh = path_leaf_bh(right_path);
3282 struct buffer_head *root_bh = NULL;
3283 struct ocfs2_path *left_path = NULL;
3284 struct ocfs2_extent_list *left_el;
3288 right_rec = &el->l_recs[index];
3290 /* we meet with a cross extent block merge. */
3291 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3297 left_el = path_leaf_el(left_path);
3298 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3299 le16_to_cpu(left_el->l_count));
3301 left_rec = &left_el->l_recs[
3302 le16_to_cpu(left_el->l_next_free_rec) - 1];
3303 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3304 le16_to_cpu(left_rec->e_leaf_clusters) !=
3305 le32_to_cpu(split_rec->e_cpos));
3307 subtree_index = ocfs2_find_subtree_root(inode,
3308 left_path, right_path);
3310 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3311 handle->h_buffer_credits,
3318 root_bh = left_path->p_node[subtree_index].bh;
3319 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3321 ret = ocfs2_journal_access(handle, inode, root_bh,
3322 OCFS2_JOURNAL_ACCESS_WRITE);
3328 for (i = subtree_index + 1;
3329 i < path_num_items(right_path); i++) {
3330 ret = ocfs2_journal_access(handle, inode,
3331 right_path->p_node[i].bh,
3332 OCFS2_JOURNAL_ACCESS_WRITE);
3338 ret = ocfs2_journal_access(handle, inode,
3339 left_path->p_node[i].bh,
3340 OCFS2_JOURNAL_ACCESS_WRITE);
3347 left_rec = &el->l_recs[index - 1];
3348 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3349 has_empty_extent = 1;
3352 ret = ocfs2_journal_access(handle, inode, bh,
3353 OCFS2_JOURNAL_ACCESS_WRITE);
3359 if (has_empty_extent && index == 1) {
3361 * The easy case - we can just plop the record right in.
3363 *left_rec = *split_rec;
3365 has_empty_extent = 0;
3367 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3369 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3370 le64_add_cpu(&right_rec->e_blkno,
3371 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3372 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3374 ocfs2_cleanup_merge(el, index);
3376 ret = ocfs2_journal_dirty(handle, bh);
3381 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3386 * In the situation that the right_rec is empty and the extent
3387 * block is empty also, ocfs2_complete_edge_insert can't handle
3388 * it and we need to delete the right extent block.
3390 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3391 le16_to_cpu(el->l_next_free_rec) == 1) {
3393 ret = ocfs2_remove_rightmost_path(inode, handle,
3401 /* Now the rightmost extent block has been deleted.
3402 * So we use the new rightmost path.
3404 ocfs2_mv_path(right_path, left_path);
3407 ocfs2_complete_edge_insert(inode, handle, left_path,
3408 right_path, subtree_index);
3412 ocfs2_free_path(left_path);
3416 static int ocfs2_try_to_merge_extent(struct inode *inode,
3418 struct ocfs2_path *path,
3420 struct ocfs2_extent_rec *split_rec,
3421 struct ocfs2_cached_dealloc_ctxt *dealloc,
3422 struct ocfs2_merge_ctxt *ctxt,
3423 struct ocfs2_extent_tree *et)
3427 struct ocfs2_extent_list *el = path_leaf_el(path);
3428 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3430 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3432 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3434 * The merge code will need to create an empty
3435 * extent to take the place of the newly
3436 * emptied slot. Remove any pre-existing empty
3437 * extents - having more than one in a leaf is
3440 ret = ocfs2_rotate_tree_left(inode, handle, path,
3447 rec = &el->l_recs[split_index];
3450 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3452 * Left-right contig implies this.
3454 BUG_ON(!ctxt->c_split_covers_rec);
3457 * Since the leftright insert always covers the entire
3458 * extent, this call will delete the insert record
3459 * entirely, resulting in an empty extent record added to
3462 * Since the adding of an empty extent shifts
3463 * everything back to the right, there's no need to
3464 * update split_index here.
3466 * When the split_index is zero, we need to merge it to the
3467 * prevoius extent block. It is more efficient and easier
3468 * if we do merge_right first and merge_left later.
3470 ret = ocfs2_merge_rec_right(inode, path,
3479 * We can only get this from logic error above.
3481 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3483 /* The merge left us with an empty extent, remove it. */
3484 ret = ocfs2_rotate_tree_left(inode, handle, path,
3491 rec = &el->l_recs[split_index];
3494 * Note that we don't pass split_rec here on purpose -
3495 * we've merged it into the rec already.
3497 ret = ocfs2_merge_rec_left(inode, path,
3507 ret = ocfs2_rotate_tree_left(inode, handle, path,
3510 * Error from this last rotate is not critical, so
3511 * print but don't bubble it up.
3518 * Merge a record to the left or right.
3520 * 'contig_type' is relative to the existing record,
3521 * so for example, if we're "right contig", it's to
3522 * the record on the left (hence the left merge).
3524 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3525 ret = ocfs2_merge_rec_left(inode,
3535 ret = ocfs2_merge_rec_right(inode,
3545 if (ctxt->c_split_covers_rec) {
3547 * The merge may have left an empty extent in
3548 * our leaf. Try to rotate it away.
3550 ret = ocfs2_rotate_tree_left(inode, handle, path,
3562 static void ocfs2_subtract_from_rec(struct super_block *sb,
3563 enum ocfs2_split_type split,
3564 struct ocfs2_extent_rec *rec,
3565 struct ocfs2_extent_rec *split_rec)
3569 len_blocks = ocfs2_clusters_to_blocks(sb,
3570 le16_to_cpu(split_rec->e_leaf_clusters));
3572 if (split == SPLIT_LEFT) {
3574 * Region is on the left edge of the existing
3577 le32_add_cpu(&rec->e_cpos,
3578 le16_to_cpu(split_rec->e_leaf_clusters));
3579 le64_add_cpu(&rec->e_blkno, len_blocks);
3580 le16_add_cpu(&rec->e_leaf_clusters,
3581 -le16_to_cpu(split_rec->e_leaf_clusters));
3584 * Region is on the right edge of the existing
3587 le16_add_cpu(&rec->e_leaf_clusters,
3588 -le16_to_cpu(split_rec->e_leaf_clusters));
3593 * Do the final bits of extent record insertion at the target leaf
3594 * list. If this leaf is part of an allocation tree, it is assumed
3595 * that the tree above has been prepared.
3597 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3598 struct ocfs2_extent_list *el,
3599 struct ocfs2_insert_type *insert,
3600 struct inode *inode)
3602 int i = insert->ins_contig_index;
3604 struct ocfs2_extent_rec *rec;
3606 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3608 if (insert->ins_split != SPLIT_NONE) {
3609 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3611 rec = &el->l_recs[i];
3612 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3618 * Contiguous insert - either left or right.
3620 if (insert->ins_contig != CONTIG_NONE) {
3621 rec = &el->l_recs[i];
3622 if (insert->ins_contig == CONTIG_LEFT) {
3623 rec->e_blkno = insert_rec->e_blkno;
3624 rec->e_cpos = insert_rec->e_cpos;
3626 le16_add_cpu(&rec->e_leaf_clusters,
3627 le16_to_cpu(insert_rec->e_leaf_clusters));
3632 * Handle insert into an empty leaf.
3634 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3635 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3636 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3637 el->l_recs[0] = *insert_rec;
3638 el->l_next_free_rec = cpu_to_le16(1);
3645 if (insert->ins_appending == APPEND_TAIL) {
3646 i = le16_to_cpu(el->l_next_free_rec) - 1;
3647 rec = &el->l_recs[i];
3648 range = le32_to_cpu(rec->e_cpos)
3649 + le16_to_cpu(rec->e_leaf_clusters);
3650 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3652 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3653 le16_to_cpu(el->l_count),
3654 "inode %lu, depth %u, count %u, next free %u, "
3655 "rec.cpos %u, rec.clusters %u, "
3656 "insert.cpos %u, insert.clusters %u\n",
3658 le16_to_cpu(el->l_tree_depth),
3659 le16_to_cpu(el->l_count),
3660 le16_to_cpu(el->l_next_free_rec),
3661 le32_to_cpu(el->l_recs[i].e_cpos),
3662 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3663 le32_to_cpu(insert_rec->e_cpos),
3664 le16_to_cpu(insert_rec->e_leaf_clusters));
3666 el->l_recs[i] = *insert_rec;
3667 le16_add_cpu(&el->l_next_free_rec, 1);
3673 * Ok, we have to rotate.
3675 * At this point, it is safe to assume that inserting into an
3676 * empty leaf and appending to a leaf have both been handled
3679 * This leaf needs to have space, either by the empty 1st
3680 * extent record, or by virtue of an l_next_rec < l_count.
3682 ocfs2_rotate_leaf(el, insert_rec);
3685 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3687 struct ocfs2_path *path,
3688 struct ocfs2_extent_rec *insert_rec)
3690 int ret, i, next_free;
3691 struct buffer_head *bh;
3692 struct ocfs2_extent_list *el;
3693 struct ocfs2_extent_rec *rec;
3696 * Update everything except the leaf block.
3698 for (i = 0; i < path->p_tree_depth; i++) {
3699 bh = path->p_node[i].bh;
3700 el = path->p_node[i].el;
3702 next_free = le16_to_cpu(el->l_next_free_rec);
3703 if (next_free == 0) {
3704 ocfs2_error(inode->i_sb,
3705 "Dinode %llu has a bad extent list",
3706 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3711 rec = &el->l_recs[next_free - 1];
3713 rec->e_int_clusters = insert_rec->e_cpos;
3714 le32_add_cpu(&rec->e_int_clusters,
3715 le16_to_cpu(insert_rec->e_leaf_clusters));
3716 le32_add_cpu(&rec->e_int_clusters,
3717 -le32_to_cpu(rec->e_cpos));
3719 ret = ocfs2_journal_dirty(handle, bh);
3726 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3727 struct ocfs2_extent_rec *insert_rec,
3728 struct ocfs2_path *right_path,
3729 struct ocfs2_path **ret_left_path)
3732 struct ocfs2_extent_list *el;
3733 struct ocfs2_path *left_path = NULL;
3735 *ret_left_path = NULL;
3738 * This shouldn't happen for non-trees. The extent rec cluster
3739 * count manipulation below only works for interior nodes.
3741 BUG_ON(right_path->p_tree_depth == 0);
3744 * If our appending insert is at the leftmost edge of a leaf,
3745 * then we might need to update the rightmost records of the
3748 el = path_leaf_el(right_path);
3749 next_free = le16_to_cpu(el->l_next_free_rec);
3750 if (next_free == 0 ||
3751 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3754 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3761 mlog(0, "Append may need a left path update. cpos: %u, "
3762 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3766 * No need to worry if the append is already in the
3770 left_path = ocfs2_new_path(path_root_bh(right_path),
3771 path_root_el(right_path));
3778 ret = ocfs2_find_path(inode, left_path, left_cpos);
3785 * ocfs2_insert_path() will pass the left_path to the
3791 ret = ocfs2_journal_access_path(inode, handle, right_path);
3797 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
3799 *ret_left_path = left_path;
3803 ocfs2_free_path(left_path);
3808 static void ocfs2_split_record(struct inode *inode,
3809 struct ocfs2_path *left_path,
3810 struct ocfs2_path *right_path,
3811 struct ocfs2_extent_rec *split_rec,
3812 enum ocfs2_split_type split)
3815 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3816 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3817 struct ocfs2_extent_rec *rec, *tmprec;
3819 right_el = path_leaf_el(right_path);;
3821 left_el = path_leaf_el(left_path);
3824 insert_el = right_el;
3825 index = ocfs2_search_extent_list(el, cpos);
3827 if (index == 0 && left_path) {
3828 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3831 * This typically means that the record
3832 * started in the left path but moved to the
3833 * right as a result of rotation. We either
3834 * move the existing record to the left, or we
3835 * do the later insert there.
3837 * In this case, the left path should always
3838 * exist as the rotate code will have passed
3839 * it back for a post-insert update.
3842 if (split == SPLIT_LEFT) {
3844 * It's a left split. Since we know
3845 * that the rotate code gave us an
3846 * empty extent in the left path, we
3847 * can just do the insert there.
3849 insert_el = left_el;
3852 * Right split - we have to move the
3853 * existing record over to the left
3854 * leaf. The insert will be into the
3855 * newly created empty extent in the
3858 tmprec = &right_el->l_recs[index];
3859 ocfs2_rotate_leaf(left_el, tmprec);
3862 memset(tmprec, 0, sizeof(*tmprec));
3863 index = ocfs2_search_extent_list(left_el, cpos);
3864 BUG_ON(index == -1);
3869 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3871 * Left path is easy - we can just allow the insert to
3875 insert_el = left_el;
3876 index = ocfs2_search_extent_list(el, cpos);
3877 BUG_ON(index == -1);
3880 rec = &el->l_recs[index];
3881 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3882 ocfs2_rotate_leaf(insert_el, split_rec);
3886 * This function only does inserts on an allocation b-tree. For tree
3887 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3889 * right_path is the path we want to do the actual insert
3890 * in. left_path should only be passed in if we need to update that
3891 * portion of the tree after an edge insert.
3893 static int ocfs2_insert_path(struct inode *inode,
3895 struct ocfs2_path *left_path,
3896 struct ocfs2_path *right_path,
3897 struct ocfs2_extent_rec *insert_rec,
3898 struct ocfs2_insert_type *insert)
3900 int ret, subtree_index;
3901 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
3904 int credits = handle->h_buffer_credits;
3907 * There's a chance that left_path got passed back to
3908 * us without being accounted for in the
3909 * journal. Extend our transaction here to be sure we
3910 * can change those blocks.
3912 credits += left_path->p_tree_depth;
3914 ret = ocfs2_extend_trans(handle, credits);
3920 ret = ocfs2_journal_access_path(inode, handle, left_path);
3928 * Pass both paths to the journal. The majority of inserts
3929 * will be touching all components anyway.
3931 ret = ocfs2_journal_access_path(inode, handle, right_path);
3937 if (insert->ins_split != SPLIT_NONE) {
3939 * We could call ocfs2_insert_at_leaf() for some types
3940 * of splits, but it's easier to just let one separate
3941 * function sort it all out.
3943 ocfs2_split_record(inode, left_path, right_path,
3944 insert_rec, insert->ins_split);
3947 * Split might have modified either leaf and we don't
3948 * have a guarantee that the later edge insert will
3949 * dirty this for us.
3952 ret = ocfs2_journal_dirty(handle,
3953 path_leaf_bh(left_path));
3957 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3960 ret = ocfs2_journal_dirty(handle, leaf_bh);
3966 * The rotate code has indicated that we need to fix
3967 * up portions of the tree after the insert.
3969 * XXX: Should we extend the transaction here?
3971 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3973 ocfs2_complete_edge_insert(inode, handle, left_path,
3974 right_path, subtree_index);
3982 static int ocfs2_do_insert_extent(struct inode *inode,
3984 struct ocfs2_extent_tree *et,
3985 struct ocfs2_extent_rec *insert_rec,
3986 struct ocfs2_insert_type *type)
3988 int ret, rotate = 0;
3990 struct ocfs2_path *right_path = NULL;
3991 struct ocfs2_path *left_path = NULL;
3992 struct ocfs2_extent_list *el;
3994 el = et->et_root_el;
3996 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
3997 OCFS2_JOURNAL_ACCESS_WRITE);
4003 if (le16_to_cpu(el->l_tree_depth) == 0) {
4004 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4005 goto out_update_clusters;
4008 right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4016 * Determine the path to start with. Rotations need the
4017 * rightmost path, everything else can go directly to the
4020 cpos = le32_to_cpu(insert_rec->e_cpos);
4021 if (type->ins_appending == APPEND_NONE &&
4022 type->ins_contig == CONTIG_NONE) {
4027 ret = ocfs2_find_path(inode, right_path, cpos);
4034 * Rotations and appends need special treatment - they modify
4035 * parts of the tree's above them.
4037 * Both might pass back a path immediate to the left of the
4038 * one being inserted to. This will be cause
4039 * ocfs2_insert_path() to modify the rightmost records of
4040 * left_path to account for an edge insert.
4042 * XXX: When modifying this code, keep in mind that an insert
4043 * can wind up skipping both of these two special cases...
4046 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
4047 le32_to_cpu(insert_rec->e_cpos),
4048 right_path, &left_path);
4055 * ocfs2_rotate_tree_right() might have extended the
4056 * transaction without re-journaling our tree root.
4058 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
4059 OCFS2_JOURNAL_ACCESS_WRITE);
4064 } else if (type->ins_appending == APPEND_TAIL
4065 && type->ins_contig != CONTIG_LEFT) {
4066 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4067 right_path, &left_path);
4074 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4081 out_update_clusters:
4082 if (type->ins_split == SPLIT_NONE)
4083 ocfs2_et_update_clusters(inode, et,
4084 le16_to_cpu(insert_rec->e_leaf_clusters));
4086 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
4091 ocfs2_free_path(left_path);
4092 ocfs2_free_path(right_path);
4097 static enum ocfs2_contig_type
4098 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
4099 struct ocfs2_extent_list *el, int index,
4100 struct ocfs2_extent_rec *split_rec)
4103 enum ocfs2_contig_type ret = CONTIG_NONE;
4104 u32 left_cpos, right_cpos;
4105 struct ocfs2_extent_rec *rec = NULL;
4106 struct ocfs2_extent_list *new_el;
4107 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4108 struct buffer_head *bh;
4109 struct ocfs2_extent_block *eb;
4112 rec = &el->l_recs[index - 1];
4113 } else if (path->p_tree_depth > 0) {
4114 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4119 if (left_cpos != 0) {
4120 left_path = ocfs2_new_path(path_root_bh(path),
4121 path_root_el(path));
4125 status = ocfs2_find_path(inode, left_path, left_cpos);
4129 new_el = path_leaf_el(left_path);
4131 if (le16_to_cpu(new_el->l_next_free_rec) !=
4132 le16_to_cpu(new_el->l_count)) {
4133 bh = path_leaf_bh(left_path);
4134 eb = (struct ocfs2_extent_block *)bh->b_data;
4135 ocfs2_error(inode->i_sb,
4136 "Extent block #%llu has an "
4137 "invalid l_next_free_rec of "
4138 "%d. It should have "
4139 "matched the l_count of %d",
4140 (unsigned long long)le64_to_cpu(eb->h_blkno),
4141 le16_to_cpu(new_el->l_next_free_rec),
4142 le16_to_cpu(new_el->l_count));
4146 rec = &new_el->l_recs[
4147 le16_to_cpu(new_el->l_next_free_rec) - 1];
4152 * We're careful to check for an empty extent record here -
4153 * the merge code will know what to do if it sees one.
4156 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4157 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4160 ret = ocfs2_extent_contig(inode, rec, split_rec);
4165 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4166 rec = &el->l_recs[index + 1];
4167 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4168 path->p_tree_depth > 0) {
4169 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4174 if (right_cpos == 0)
4177 right_path = ocfs2_new_path(path_root_bh(path),
4178 path_root_el(path));
4182 status = ocfs2_find_path(inode, right_path, right_cpos);
4186 new_el = path_leaf_el(right_path);
4187 rec = &new_el->l_recs[0];
4188 if (ocfs2_is_empty_extent(rec)) {
4189 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4190 bh = path_leaf_bh(right_path);
4191 eb = (struct ocfs2_extent_block *)bh->b_data;
4192 ocfs2_error(inode->i_sb,
4193 "Extent block #%llu has an "
4194 "invalid l_next_free_rec of %d",
4195 (unsigned long long)le64_to_cpu(eb->h_blkno),
4196 le16_to_cpu(new_el->l_next_free_rec));
4200 rec = &new_el->l_recs[1];
4205 enum ocfs2_contig_type contig_type;
4207 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4209 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4210 ret = CONTIG_LEFTRIGHT;
4211 else if (ret == CONTIG_NONE)
4217 ocfs2_free_path(left_path);
4219 ocfs2_free_path(right_path);
4224 static void ocfs2_figure_contig_type(struct inode *inode,
4225 struct ocfs2_insert_type *insert,
4226 struct ocfs2_extent_list *el,
4227 struct ocfs2_extent_rec *insert_rec,
4228 struct ocfs2_extent_tree *et)
4231 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4233 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4235 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4236 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4238 if (contig_type != CONTIG_NONE) {
4239 insert->ins_contig_index = i;
4243 insert->ins_contig = contig_type;
4245 if (insert->ins_contig != CONTIG_NONE) {
4246 struct ocfs2_extent_rec *rec =
4247 &el->l_recs[insert->ins_contig_index];
4248 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4249 le16_to_cpu(insert_rec->e_leaf_clusters);
4252 * Caller might want us to limit the size of extents, don't
4253 * calculate contiguousness if we might exceed that limit.
4255 if (et->et_max_leaf_clusters &&
4256 (len > et->et_max_leaf_clusters))
4257 insert->ins_contig = CONTIG_NONE;
4262 * This should only be called against the righmost leaf extent list.
4264 * ocfs2_figure_appending_type() will figure out whether we'll have to
4265 * insert at the tail of the rightmost leaf.
4267 * This should also work against the root extent list for tree's with 0
4268 * depth. If we consider the root extent list to be the rightmost leaf node
4269 * then the logic here makes sense.
4271 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4272 struct ocfs2_extent_list *el,
4273 struct ocfs2_extent_rec *insert_rec)
4276 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4277 struct ocfs2_extent_rec *rec;
4279 insert->ins_appending = APPEND_NONE;
4281 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4283 if (!el->l_next_free_rec)
4284 goto set_tail_append;
4286 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4287 /* Were all records empty? */
4288 if (le16_to_cpu(el->l_next_free_rec) == 1)
4289 goto set_tail_append;
4292 i = le16_to_cpu(el->l_next_free_rec) - 1;
4293 rec = &el->l_recs[i];
4296 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4297 goto set_tail_append;
4302 insert->ins_appending = APPEND_TAIL;
4306 * Helper function called at the begining of an insert.
4308 * This computes a few things that are commonly used in the process of
4309 * inserting into the btree:
4310 * - Whether the new extent is contiguous with an existing one.
4311 * - The current tree depth.
4312 * - Whether the insert is an appending one.
4313 * - The total # of free records in the tree.
4315 * All of the information is stored on the ocfs2_insert_type
4318 static int ocfs2_figure_insert_type(struct inode *inode,
4319 struct ocfs2_extent_tree *et,
4320 struct buffer_head **last_eb_bh,
4321 struct ocfs2_extent_rec *insert_rec,
4323 struct ocfs2_insert_type *insert)
4326 struct ocfs2_extent_block *eb;
4327 struct ocfs2_extent_list *el;
4328 struct ocfs2_path *path = NULL;
4329 struct buffer_head *bh = NULL;
4331 insert->ins_split = SPLIT_NONE;
4333 el = et->et_root_el;
4334 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4336 if (el->l_tree_depth) {
4338 * If we have tree depth, we read in the
4339 * rightmost extent block ahead of time as
4340 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4341 * may want it later.
4343 ret = ocfs2_read_extent_block(inode,
4344 ocfs2_et_get_last_eb_blk(et),
4350 eb = (struct ocfs2_extent_block *) bh->b_data;
4355 * Unless we have a contiguous insert, we'll need to know if
4356 * there is room left in our allocation tree for another
4359 * XXX: This test is simplistic, we can search for empty
4360 * extent records too.
4362 *free_records = le16_to_cpu(el->l_count) -
4363 le16_to_cpu(el->l_next_free_rec);
4365 if (!insert->ins_tree_depth) {
4366 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4367 ocfs2_figure_appending_type(insert, el, insert_rec);
4371 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4379 * In the case that we're inserting past what the tree
4380 * currently accounts for, ocfs2_find_path() will return for
4381 * us the rightmost tree path. This is accounted for below in
4382 * the appending code.
4384 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4390 el = path_leaf_el(path);
4393 * Now that we have the path, there's two things we want to determine:
4394 * 1) Contiguousness (also set contig_index if this is so)
4396 * 2) Are we doing an append? We can trivially break this up
4397 * into two types of appends: simple record append, or a
4398 * rotate inside the tail leaf.
4400 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4403 * The insert code isn't quite ready to deal with all cases of
4404 * left contiguousness. Specifically, if it's an insert into
4405 * the 1st record in a leaf, it will require the adjustment of
4406 * cluster count on the last record of the path directly to it's
4407 * left. For now, just catch that case and fool the layers
4408 * above us. This works just fine for tree_depth == 0, which
4409 * is why we allow that above.
4411 if (insert->ins_contig == CONTIG_LEFT &&
4412 insert->ins_contig_index == 0)
4413 insert->ins_contig = CONTIG_NONE;
4416 * Ok, so we can simply compare against last_eb to figure out
4417 * whether the path doesn't exist. This will only happen in
4418 * the case that we're doing a tail append, so maybe we can
4419 * take advantage of that information somehow.
4421 if (ocfs2_et_get_last_eb_blk(et) ==
4422 path_leaf_bh(path)->b_blocknr) {
4424 * Ok, ocfs2_find_path() returned us the rightmost
4425 * tree path. This might be an appending insert. There are
4427 * 1) We're doing a true append at the tail:
4428 * -This might even be off the end of the leaf
4429 * 2) We're "appending" by rotating in the tail
4431 ocfs2_figure_appending_type(insert, el, insert_rec);
4435 ocfs2_free_path(path);
4445 * Insert an extent into an inode btree.
4447 * The caller needs to update fe->i_clusters
4449 int ocfs2_insert_extent(struct ocfs2_super *osb,
4451 struct inode *inode,
4452 struct ocfs2_extent_tree *et,
4457 struct ocfs2_alloc_context *meta_ac)
4460 int uninitialized_var(free_records);
4461 struct buffer_head *last_eb_bh = NULL;
4462 struct ocfs2_insert_type insert = {0, };
4463 struct ocfs2_extent_rec rec;
4465 mlog(0, "add %u clusters at position %u to inode %llu\n",
4466 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4468 memset(&rec, 0, sizeof(rec));
4469 rec.e_cpos = cpu_to_le32(cpos);
4470 rec.e_blkno = cpu_to_le64(start_blk);
4471 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4472 rec.e_flags = flags;
4473 status = ocfs2_et_insert_check(inode, et, &rec);
4479 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4480 &free_records, &insert);
4486 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4487 "Insert.contig_index: %d, Insert.free_records: %d, "
4488 "Insert.tree_depth: %d\n",
4489 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4490 free_records, insert.ins_tree_depth);
4492 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4493 status = ocfs2_grow_tree(inode, handle, et,
4494 &insert.ins_tree_depth, &last_eb_bh,
4502 /* Finally, we can add clusters. This might rotate the tree for us. */
4503 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4506 else if (et->et_ops == &ocfs2_dinode_et_ops)
4507 ocfs2_extent_map_insert_rec(inode, &rec);
4517 * Allcate and add clusters into the extent b-tree.
4518 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4519 * The extent b-tree's root is specified by et, and
4520 * it is not limited to the file storage. Any extent tree can use this
4521 * function if it implements the proper ocfs2_extent_tree.
4523 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4524 struct inode *inode,
4525 u32 *logical_offset,
4526 u32 clusters_to_add,
4528 struct ocfs2_extent_tree *et,
4530 struct ocfs2_alloc_context *data_ac,
4531 struct ocfs2_alloc_context *meta_ac,
4532 enum ocfs2_alloc_restarted *reason_ret)
4536 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4537 u32 bit_off, num_bits;
4541 BUG_ON(!clusters_to_add);
4544 flags = OCFS2_EXT_UNWRITTEN;
4546 free_extents = ocfs2_num_free_extents(osb, inode, et);
4547 if (free_extents < 0) {
4548 status = free_extents;
4553 /* there are two cases which could cause us to EAGAIN in the
4554 * we-need-more-metadata case:
4555 * 1) we haven't reserved *any*
4556 * 2) we are so fragmented, we've needed to add metadata too
4558 if (!free_extents && !meta_ac) {
4559 mlog(0, "we haven't reserved any metadata!\n");
4561 reason = RESTART_META;
4563 } else if ((!free_extents)
4564 && (ocfs2_alloc_context_bits_left(meta_ac)
4565 < ocfs2_extend_meta_needed(et->et_root_el))) {
4566 mlog(0, "filesystem is really fragmented...\n");
4568 reason = RESTART_META;
4572 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4573 clusters_to_add, &bit_off, &num_bits);
4575 if (status != -ENOSPC)
4580 BUG_ON(num_bits > clusters_to_add);
4582 /* reserve our write early -- insert_extent may update the inode */
4583 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
4584 OCFS2_JOURNAL_ACCESS_WRITE);
4590 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4591 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4592 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4593 status = ocfs2_insert_extent(osb, handle, inode, et,
4594 *logical_offset, block,
4595 num_bits, flags, meta_ac);
4601 status = ocfs2_journal_dirty(handle, et->et_root_bh);
4607 clusters_to_add -= num_bits;
4608 *logical_offset += num_bits;
4610 if (clusters_to_add) {
4611 mlog(0, "need to alloc once more, wanted = %u\n",
4614 reason = RESTART_TRANS;
4620 *reason_ret = reason;
4624 static void ocfs2_make_right_split_rec(struct super_block *sb,
4625 struct ocfs2_extent_rec *split_rec,
4627 struct ocfs2_extent_rec *rec)
4629 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4630 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4632 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4634 split_rec->e_cpos = cpu_to_le32(cpos);
4635 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4637 split_rec->e_blkno = rec->e_blkno;
4638 le64_add_cpu(&split_rec->e_blkno,
4639 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4641 split_rec->e_flags = rec->e_flags;
4644 static int ocfs2_split_and_insert(struct inode *inode,
4646 struct ocfs2_path *path,
4647 struct ocfs2_extent_tree *et,
4648 struct buffer_head **last_eb_bh,
4650 struct ocfs2_extent_rec *orig_split_rec,
4651 struct ocfs2_alloc_context *meta_ac)
4654 unsigned int insert_range, rec_range, do_leftright = 0;
4655 struct ocfs2_extent_rec tmprec;
4656 struct ocfs2_extent_list *rightmost_el;
4657 struct ocfs2_extent_rec rec;
4658 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4659 struct ocfs2_insert_type insert;
4660 struct ocfs2_extent_block *eb;
4664 * Store a copy of the record on the stack - it might move
4665 * around as the tree is manipulated below.
4667 rec = path_leaf_el(path)->l_recs[split_index];
4669 rightmost_el = et->et_root_el;
4671 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4673 BUG_ON(!(*last_eb_bh));
4674 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4675 rightmost_el = &eb->h_list;
4678 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4679 le16_to_cpu(rightmost_el->l_count)) {
4680 ret = ocfs2_grow_tree(inode, handle, et,
4681 &depth, last_eb_bh, meta_ac);
4688 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4689 insert.ins_appending = APPEND_NONE;
4690 insert.ins_contig = CONTIG_NONE;
4691 insert.ins_tree_depth = depth;
4693 insert_range = le32_to_cpu(split_rec.e_cpos) +
4694 le16_to_cpu(split_rec.e_leaf_clusters);
4695 rec_range = le32_to_cpu(rec.e_cpos) +
4696 le16_to_cpu(rec.e_leaf_clusters);
4698 if (split_rec.e_cpos == rec.e_cpos) {
4699 insert.ins_split = SPLIT_LEFT;
4700 } else if (insert_range == rec_range) {
4701 insert.ins_split = SPLIT_RIGHT;
4704 * Left/right split. We fake this as a right split
4705 * first and then make a second pass as a left split.
4707 insert.ins_split = SPLIT_RIGHT;
4709 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4714 BUG_ON(do_leftright);
4718 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4724 if (do_leftright == 1) {
4726 struct ocfs2_extent_list *el;
4729 split_rec = *orig_split_rec;
4731 ocfs2_reinit_path(path, 1);
4733 cpos = le32_to_cpu(split_rec.e_cpos);
4734 ret = ocfs2_find_path(inode, path, cpos);
4740 el = path_leaf_el(path);
4741 split_index = ocfs2_search_extent_list(el, cpos);
4750 * Mark part or all of the extent record at split_index in the leaf
4751 * pointed to by path as written. This removes the unwritten
4754 * Care is taken to handle contiguousness so as to not grow the tree.
4756 * meta_ac is not strictly necessary - we only truly need it if growth
4757 * of the tree is required. All other cases will degrade into a less
4758 * optimal tree layout.
4760 * last_eb_bh should be the rightmost leaf block for any extent
4761 * btree. Since a split may grow the tree or a merge might shrink it,
4762 * the caller cannot trust the contents of that buffer after this call.
4764 * This code is optimized for readability - several passes might be
4765 * made over certain portions of the tree. All of those blocks will
4766 * have been brought into cache (and pinned via the journal), so the
4767 * extra overhead is not expressed in terms of disk reads.
4769 static int __ocfs2_mark_extent_written(struct inode *inode,
4770 struct ocfs2_extent_tree *et,
4772 struct ocfs2_path *path,
4774 struct ocfs2_extent_rec *split_rec,
4775 struct ocfs2_alloc_context *meta_ac,
4776 struct ocfs2_cached_dealloc_ctxt *dealloc)
4779 struct ocfs2_extent_list *el = path_leaf_el(path);
4780 struct buffer_head *last_eb_bh = NULL;
4781 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4782 struct ocfs2_merge_ctxt ctxt;
4783 struct ocfs2_extent_list *rightmost_el;
4785 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
4791 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4792 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4793 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4799 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
4804 * The core merge / split code wants to know how much room is
4805 * left in this inodes allocation tree, so we pass the
4806 * rightmost extent list.
4808 if (path->p_tree_depth) {
4809 struct ocfs2_extent_block *eb;
4811 ret = ocfs2_read_extent_block(inode,
4812 ocfs2_et_get_last_eb_blk(et),
4819 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4820 rightmost_el = &eb->h_list;
4822 rightmost_el = path_root_el(path);
4824 if (rec->e_cpos == split_rec->e_cpos &&
4825 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4826 ctxt.c_split_covers_rec = 1;
4828 ctxt.c_split_covers_rec = 0;
4830 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4832 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4833 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4834 ctxt.c_split_covers_rec);
4836 if (ctxt.c_contig_type == CONTIG_NONE) {
4837 if (ctxt.c_split_covers_rec)
4838 el->l_recs[split_index] = *split_rec;
4840 ret = ocfs2_split_and_insert(inode, handle, path, et,
4841 &last_eb_bh, split_index,
4842 split_rec, meta_ac);
4846 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4847 split_index, split_rec,
4848 dealloc, &ctxt, et);
4859 * Mark the already-existing extent at cpos as written for len clusters.
4861 * If the existing extent is larger than the request, initiate a
4862 * split. An attempt will be made at merging with adjacent extents.
4864 * The caller is responsible for passing down meta_ac if we'll need it.
4866 int ocfs2_mark_extent_written(struct inode *inode,
4867 struct ocfs2_extent_tree *et,
4868 handle_t *handle, u32 cpos, u32 len, u32 phys,
4869 struct ocfs2_alloc_context *meta_ac,
4870 struct ocfs2_cached_dealloc_ctxt *dealloc)
4873 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4874 struct ocfs2_extent_rec split_rec;
4875 struct ocfs2_path *left_path = NULL;
4876 struct ocfs2_extent_list *el;
4878 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4879 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4881 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4882 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4883 "that are being written to, but the feature bit "
4884 "is not set in the super block.",
4885 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4891 * XXX: This should be fixed up so that we just re-insert the
4892 * next extent records.
4894 * XXX: This is a hack on the extent tree, maybe it should be
4897 if (et->et_ops == &ocfs2_dinode_et_ops)
4898 ocfs2_extent_map_trunc(inode, 0);
4900 left_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4907 ret = ocfs2_find_path(inode, left_path, cpos);
4912 el = path_leaf_el(left_path);
4914 index = ocfs2_search_extent_list(el, cpos);
4915 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4916 ocfs2_error(inode->i_sb,
4917 "Inode %llu has an extent at cpos %u which can no "
4918 "longer be found.\n",
4919 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4924 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4925 split_rec.e_cpos = cpu_to_le32(cpos);
4926 split_rec.e_leaf_clusters = cpu_to_le16(len);
4927 split_rec.e_blkno = cpu_to_le64(start_blkno);
4928 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4929 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4931 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
4932 index, &split_rec, meta_ac,
4938 ocfs2_free_path(left_path);
4942 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
4943 handle_t *handle, struct ocfs2_path *path,
4944 int index, u32 new_range,
4945 struct ocfs2_alloc_context *meta_ac)
4947 int ret, depth, credits = handle->h_buffer_credits;
4948 struct buffer_head *last_eb_bh = NULL;
4949 struct ocfs2_extent_block *eb;
4950 struct ocfs2_extent_list *rightmost_el, *el;
4951 struct ocfs2_extent_rec split_rec;
4952 struct ocfs2_extent_rec *rec;
4953 struct ocfs2_insert_type insert;
4956 * Setup the record to split before we grow the tree.
4958 el = path_leaf_el(path);
4959 rec = &el->l_recs[index];
4960 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4962 depth = path->p_tree_depth;
4964 ret = ocfs2_read_extent_block(inode,
4965 ocfs2_et_get_last_eb_blk(et),
4972 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4973 rightmost_el = &eb->h_list;
4975 rightmost_el = path_leaf_el(path);
4977 credits += path->p_tree_depth +
4978 ocfs2_extend_meta_needed(et->et_root_el);
4979 ret = ocfs2_extend_trans(handle, credits);
4985 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4986 le16_to_cpu(rightmost_el->l_count)) {
4987 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
4995 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4996 insert.ins_appending = APPEND_NONE;
4997 insert.ins_contig = CONTIG_NONE;
4998 insert.ins_split = SPLIT_RIGHT;
4999 insert.ins_tree_depth = depth;
5001 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
5010 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5011 struct ocfs2_path *path, int index,
5012 struct ocfs2_cached_dealloc_ctxt *dealloc,
5014 struct ocfs2_extent_tree *et)
5017 u32 left_cpos, rec_range, trunc_range;
5018 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5019 struct super_block *sb = inode->i_sb;
5020 struct ocfs2_path *left_path = NULL;
5021 struct ocfs2_extent_list *el = path_leaf_el(path);
5022 struct ocfs2_extent_rec *rec;
5023 struct ocfs2_extent_block *eb;
5025 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5026 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5035 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5036 path->p_tree_depth) {
5038 * Check whether this is the rightmost tree record. If
5039 * we remove all of this record or part of its right
5040 * edge then an update of the record lengths above it
5043 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5044 if (eb->h_next_leaf_blk == 0)
5045 is_rightmost_tree_rec = 1;
5048 rec = &el->l_recs[index];
5049 if (index == 0 && path->p_tree_depth &&
5050 le32_to_cpu(rec->e_cpos) == cpos) {
5052 * Changing the leftmost offset (via partial or whole
5053 * record truncate) of an interior (or rightmost) path
5054 * means we have to update the subtree that is formed
5055 * by this leaf and the one to it's left.
5057 * There are two cases we can skip:
5058 * 1) Path is the leftmost one in our inode tree.
5059 * 2) The leaf is rightmost and will be empty after
5060 * we remove the extent record - the rotate code
5061 * knows how to update the newly formed edge.
5064 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5071 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5072 left_path = ocfs2_new_path(path_root_bh(path),
5073 path_root_el(path));
5080 ret = ocfs2_find_path(inode, left_path, left_cpos);
5088 ret = ocfs2_extend_rotate_transaction(handle, 0,
5089 handle->h_buffer_credits,
5096 ret = ocfs2_journal_access_path(inode, handle, path);
5102 ret = ocfs2_journal_access_path(inode, handle, left_path);
5108 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5109 trunc_range = cpos + len;
5111 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5114 memset(rec, 0, sizeof(*rec));
5115 ocfs2_cleanup_merge(el, index);
5118 next_free = le16_to_cpu(el->l_next_free_rec);
5119 if (is_rightmost_tree_rec && next_free > 1) {
5121 * We skip the edge update if this path will
5122 * be deleted by the rotate code.
5124 rec = &el->l_recs[next_free - 1];
5125 ocfs2_adjust_rightmost_records(inode, handle, path,
5128 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5129 /* Remove leftmost portion of the record. */
5130 le32_add_cpu(&rec->e_cpos, len);
5131 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5132 le16_add_cpu(&rec->e_leaf_clusters, -len);
5133 } else if (rec_range == trunc_range) {
5134 /* Remove rightmost portion of the record */
5135 le16_add_cpu(&rec->e_leaf_clusters, -len);
5136 if (is_rightmost_tree_rec)
5137 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5139 /* Caller should have trapped this. */
5140 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5141 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5142 le32_to_cpu(rec->e_cpos),
5143 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5150 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5151 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5155 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5157 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5164 ocfs2_free_path(left_path);
5168 int ocfs2_remove_extent(struct inode *inode,
5169 struct ocfs2_extent_tree *et,
5170 u32 cpos, u32 len, handle_t *handle,
5171 struct ocfs2_alloc_context *meta_ac,
5172 struct ocfs2_cached_dealloc_ctxt *dealloc)
5175 u32 rec_range, trunc_range;
5176 struct ocfs2_extent_rec *rec;
5177 struct ocfs2_extent_list *el;
5178 struct ocfs2_path *path = NULL;
5180 ocfs2_extent_map_trunc(inode, 0);
5182 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
5189 ret = ocfs2_find_path(inode, path, cpos);
5195 el = path_leaf_el(path);
5196 index = ocfs2_search_extent_list(el, cpos);
5197 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5198 ocfs2_error(inode->i_sb,
5199 "Inode %llu has an extent at cpos %u which can no "
5200 "longer be found.\n",
5201 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5207 * We have 3 cases of extent removal:
5208 * 1) Range covers the entire extent rec
5209 * 2) Range begins or ends on one edge of the extent rec
5210 * 3) Range is in the middle of the extent rec (no shared edges)
5212 * For case 1 we remove the extent rec and left rotate to
5215 * For case 2 we just shrink the existing extent rec, with a
5216 * tree update if the shrinking edge is also the edge of an
5219 * For case 3 we do a right split to turn the extent rec into
5220 * something case 2 can handle.
5222 rec = &el->l_recs[index];
5223 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5224 trunc_range = cpos + len;
5226 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5228 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5229 "(cpos %u, len %u)\n",
5230 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5231 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5233 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5234 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5241 ret = ocfs2_split_tree(inode, et, handle, path, index,
5242 trunc_range, meta_ac);
5249 * The split could have manipulated the tree enough to
5250 * move the record location, so we have to look for it again.
5252 ocfs2_reinit_path(path, 1);
5254 ret = ocfs2_find_path(inode, path, cpos);
5260 el = path_leaf_el(path);
5261 index = ocfs2_search_extent_list(el, cpos);
5262 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5263 ocfs2_error(inode->i_sb,
5264 "Inode %llu: split at cpos %u lost record.",
5265 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5272 * Double check our values here. If anything is fishy,
5273 * it's easier to catch it at the top level.
5275 rec = &el->l_recs[index];
5276 rec_range = le32_to_cpu(rec->e_cpos) +
5277 ocfs2_rec_clusters(el, rec);
5278 if (rec_range != trunc_range) {
5279 ocfs2_error(inode->i_sb,
5280 "Inode %llu: error after split at cpos %u"
5281 "trunc len %u, existing record is (%u,%u)",
5282 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5283 cpos, len, le32_to_cpu(rec->e_cpos),
5284 ocfs2_rec_clusters(el, rec));
5289 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5298 ocfs2_free_path(path);
5302 int ocfs2_remove_btree_range(struct inode *inode,
5303 struct ocfs2_extent_tree *et,
5304 u32 cpos, u32 phys_cpos, u32 len,
5305 struct ocfs2_cached_dealloc_ctxt *dealloc)
5308 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5309 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5310 struct inode *tl_inode = osb->osb_tl_inode;
5312 struct ocfs2_alloc_context *meta_ac = NULL;
5314 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5320 mutex_lock(&tl_inode->i_mutex);
5322 if (ocfs2_truncate_log_needs_flush(osb)) {
5323 ret = __ocfs2_flush_truncate_log(osb);
5330 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
5331 if (IS_ERR(handle)) {
5332 ret = PTR_ERR(handle);
5337 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
5338 OCFS2_JOURNAL_ACCESS_WRITE);
5344 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5351 ocfs2_et_update_clusters(inode, et, -len);
5353 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5359 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5364 ocfs2_commit_trans(osb, handle);
5366 mutex_unlock(&tl_inode->i_mutex);
5369 ocfs2_free_alloc_context(meta_ac);
5374 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5376 struct buffer_head *tl_bh = osb->osb_tl_bh;
5377 struct ocfs2_dinode *di;
5378 struct ocfs2_truncate_log *tl;
5380 di = (struct ocfs2_dinode *) tl_bh->b_data;
5381 tl = &di->id2.i_dealloc;
5383 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5384 "slot %d, invalid truncate log parameters: used = "
5385 "%u, count = %u\n", osb->slot_num,
5386 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5387 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5390 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5391 unsigned int new_start)
5393 unsigned int tail_index;
5394 unsigned int current_tail;
5396 /* No records, nothing to coalesce */
5397 if (!le16_to_cpu(tl->tl_used))
5400 tail_index = le16_to_cpu(tl->tl_used) - 1;
5401 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5402 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5404 return current_tail == new_start;
5407 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5410 unsigned int num_clusters)
5413 unsigned int start_cluster, tl_count;
5414 struct inode *tl_inode = osb->osb_tl_inode;
5415 struct buffer_head *tl_bh = osb->osb_tl_bh;
5416 struct ocfs2_dinode *di;
5417 struct ocfs2_truncate_log *tl;
5419 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5420 (unsigned long long)start_blk, num_clusters);
5422 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5424 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5426 di = (struct ocfs2_dinode *) tl_bh->b_data;
5428 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5429 * by the underlying call to ocfs2_read_inode_block(), so any
5430 * corruption is a code bug */
5431 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5433 tl = &di->id2.i_dealloc;
5434 tl_count = le16_to_cpu(tl->tl_count);
5435 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5437 "Truncate record count on #%llu invalid "
5438 "wanted %u, actual %u\n",
5439 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5440 ocfs2_truncate_recs_per_inode(osb->sb),
5441 le16_to_cpu(tl->tl_count));
5443 /* Caller should have known to flush before calling us. */
5444 index = le16_to_cpu(tl->tl_used);
5445 if (index >= tl_count) {
5451 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5452 OCFS2_JOURNAL_ACCESS_WRITE);
5458 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5459 "%llu (index = %d)\n", num_clusters, start_cluster,
5460 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5462 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5464 * Move index back to the record we are coalescing with.
5465 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5469 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5470 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5471 index, le32_to_cpu(tl->tl_recs[index].t_start),
5474 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5475 tl->tl_used = cpu_to_le16(index + 1);
5477 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5479 status = ocfs2_journal_dirty(handle, tl_bh);
5490 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5492 struct inode *data_alloc_inode,
5493 struct buffer_head *data_alloc_bh)
5497 unsigned int num_clusters;
5499 struct ocfs2_truncate_rec rec;
5500 struct ocfs2_dinode *di;
5501 struct ocfs2_truncate_log *tl;
5502 struct inode *tl_inode = osb->osb_tl_inode;
5503 struct buffer_head *tl_bh = osb->osb_tl_bh;
5507 di = (struct ocfs2_dinode *) tl_bh->b_data;
5508 tl = &di->id2.i_dealloc;
5509 i = le16_to_cpu(tl->tl_used) - 1;
5511 /* Caller has given us at least enough credits to
5512 * update the truncate log dinode */
5513 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5514 OCFS2_JOURNAL_ACCESS_WRITE);
5520 tl->tl_used = cpu_to_le16(i);
5522 status = ocfs2_journal_dirty(handle, tl_bh);
5528 /* TODO: Perhaps we can calculate the bulk of the
5529 * credits up front rather than extending like
5531 status = ocfs2_extend_trans(handle,
5532 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5538 rec = tl->tl_recs[i];
5539 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5540 le32_to_cpu(rec.t_start));
5541 num_clusters = le32_to_cpu(rec.t_clusters);
5543 /* if start_blk is not set, we ignore the record as
5546 mlog(0, "free record %d, start = %u, clusters = %u\n",
5547 i, le32_to_cpu(rec.t_start), num_clusters);
5549 status = ocfs2_free_clusters(handle, data_alloc_inode,
5550 data_alloc_bh, start_blk,
5565 /* Expects you to already be holding tl_inode->i_mutex */
5566 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5569 unsigned int num_to_flush;
5571 struct inode *tl_inode = osb->osb_tl_inode;
5572 struct inode *data_alloc_inode = NULL;
5573 struct buffer_head *tl_bh = osb->osb_tl_bh;
5574 struct buffer_head *data_alloc_bh = NULL;
5575 struct ocfs2_dinode *di;
5576 struct ocfs2_truncate_log *tl;
5580 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5582 di = (struct ocfs2_dinode *) tl_bh->b_data;
5584 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5585 * by the underlying call to ocfs2_read_inode_block(), so any
5586 * corruption is a code bug */
5587 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5589 tl = &di->id2.i_dealloc;
5590 num_to_flush = le16_to_cpu(tl->tl_used);
5591 mlog(0, "Flush %u records from truncate log #%llu\n",
5592 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5593 if (!num_to_flush) {
5598 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5599 GLOBAL_BITMAP_SYSTEM_INODE,
5600 OCFS2_INVALID_SLOT);
5601 if (!data_alloc_inode) {
5603 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5607 mutex_lock(&data_alloc_inode->i_mutex);
5609 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5615 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5616 if (IS_ERR(handle)) {
5617 status = PTR_ERR(handle);
5622 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5627 ocfs2_commit_trans(osb, handle);
5630 brelse(data_alloc_bh);
5631 ocfs2_inode_unlock(data_alloc_inode, 1);
5634 mutex_unlock(&data_alloc_inode->i_mutex);
5635 iput(data_alloc_inode);
5642 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5645 struct inode *tl_inode = osb->osb_tl_inode;
5647 mutex_lock(&tl_inode->i_mutex);
5648 status = __ocfs2_flush_truncate_log(osb);
5649 mutex_unlock(&tl_inode->i_mutex);
5654 static void ocfs2_truncate_log_worker(struct work_struct *work)
5657 struct ocfs2_super *osb =
5658 container_of(work, struct ocfs2_super,
5659 osb_truncate_log_wq.work);
5663 status = ocfs2_flush_truncate_log(osb);
5667 ocfs2_init_inode_steal_slot(osb);
5672 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5673 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5676 if (osb->osb_tl_inode) {
5677 /* We want to push off log flushes while truncates are
5680 cancel_delayed_work(&osb->osb_truncate_log_wq);
5682 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5683 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5687 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5689 struct inode **tl_inode,
5690 struct buffer_head **tl_bh)
5693 struct inode *inode = NULL;
5694 struct buffer_head *bh = NULL;
5696 inode = ocfs2_get_system_file_inode(osb,
5697 TRUNCATE_LOG_SYSTEM_INODE,
5701 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5705 status = ocfs2_read_inode_block(inode, &bh);
5719 /* called during the 1st stage of node recovery. we stamp a clean
5720 * truncate log and pass back a copy for processing later. if the
5721 * truncate log does not require processing, a *tl_copy is set to
5723 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5725 struct ocfs2_dinode **tl_copy)
5728 struct inode *tl_inode = NULL;
5729 struct buffer_head *tl_bh = NULL;
5730 struct ocfs2_dinode *di;
5731 struct ocfs2_truncate_log *tl;
5735 mlog(0, "recover truncate log from slot %d\n", slot_num);
5737 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5743 di = (struct ocfs2_dinode *) tl_bh->b_data;
5745 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5746 * validated by the underlying call to ocfs2_read_inode_block(),
5747 * so any corruption is a code bug */
5748 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5750 tl = &di->id2.i_dealloc;
5751 if (le16_to_cpu(tl->tl_used)) {
5752 mlog(0, "We'll have %u logs to recover\n",
5753 le16_to_cpu(tl->tl_used));
5755 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5762 /* Assuming the write-out below goes well, this copy
5763 * will be passed back to recovery for processing. */
5764 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5766 /* All we need to do to clear the truncate log is set
5770 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5782 if (status < 0 && (*tl_copy)) {
5791 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5792 struct ocfs2_dinode *tl_copy)
5796 unsigned int clusters, num_recs, start_cluster;
5799 struct inode *tl_inode = osb->osb_tl_inode;
5800 struct ocfs2_truncate_log *tl;
5804 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5805 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5809 tl = &tl_copy->id2.i_dealloc;
5810 num_recs = le16_to_cpu(tl->tl_used);
5811 mlog(0, "cleanup %u records from %llu\n", num_recs,
5812 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
5814 mutex_lock(&tl_inode->i_mutex);
5815 for(i = 0; i < num_recs; i++) {
5816 if (ocfs2_truncate_log_needs_flush(osb)) {
5817 status = __ocfs2_flush_truncate_log(osb);
5824 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5825 if (IS_ERR(handle)) {
5826 status = PTR_ERR(handle);
5831 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5832 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5833 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5835 status = ocfs2_truncate_log_append(osb, handle,
5836 start_blk, clusters);
5837 ocfs2_commit_trans(osb, handle);
5845 mutex_unlock(&tl_inode->i_mutex);
5851 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5854 struct inode *tl_inode = osb->osb_tl_inode;
5859 cancel_delayed_work(&osb->osb_truncate_log_wq);
5860 flush_workqueue(ocfs2_wq);
5862 status = ocfs2_flush_truncate_log(osb);
5866 brelse(osb->osb_tl_bh);
5867 iput(osb->osb_tl_inode);
5873 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5876 struct inode *tl_inode = NULL;
5877 struct buffer_head *tl_bh = NULL;
5881 status = ocfs2_get_truncate_log_info(osb,
5888 /* ocfs2_truncate_log_shutdown keys on the existence of
5889 * osb->osb_tl_inode so we don't set any of the osb variables
5890 * until we're sure all is well. */
5891 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5892 ocfs2_truncate_log_worker);
5893 osb->osb_tl_bh = tl_bh;
5894 osb->osb_tl_inode = tl_inode;
5901 * Delayed de-allocation of suballocator blocks.
5903 * Some sets of block de-allocations might involve multiple suballocator inodes.
5905 * The locking for this can get extremely complicated, especially when
5906 * the suballocator inodes to delete from aren't known until deep
5907 * within an unrelated codepath.
5909 * ocfs2_extent_block structures are a good example of this - an inode
5910 * btree could have been grown by any number of nodes each allocating
5911 * out of their own suballoc inode.
5913 * These structures allow the delay of block de-allocation until a
5914 * later time, when locking of multiple cluster inodes won't cause
5919 * Describe a single bit freed from a suballocator. For the block
5920 * suballocators, it represents one block. For the global cluster
5921 * allocator, it represents some clusters and free_bit indicates
5924 struct ocfs2_cached_block_free {
5925 struct ocfs2_cached_block_free *free_next;
5927 unsigned int free_bit;
5930 struct ocfs2_per_slot_free_list {
5931 struct ocfs2_per_slot_free_list *f_next_suballocator;
5934 struct ocfs2_cached_block_free *f_first;
5937 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
5940 struct ocfs2_cached_block_free *head)
5945 struct inode *inode;
5946 struct buffer_head *di_bh = NULL;
5947 struct ocfs2_cached_block_free *tmp;
5949 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5956 mutex_lock(&inode->i_mutex);
5958 ret = ocfs2_inode_lock(inode, &di_bh, 1);
5964 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5965 if (IS_ERR(handle)) {
5966 ret = PTR_ERR(handle);
5972 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5974 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5975 head->free_bit, (unsigned long long)head->free_blk);
5977 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5978 head->free_bit, bg_blkno, 1);
5984 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5991 head = head->free_next;
5996 ocfs2_commit_trans(osb, handle);
5999 ocfs2_inode_unlock(inode, 1);
6002 mutex_unlock(&inode->i_mutex);
6006 /* Premature exit may have left some dangling items. */
6008 head = head->free_next;
6015 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6016 u64 blkno, unsigned int bit)
6019 struct ocfs2_cached_block_free *item;
6021 item = kmalloc(sizeof(*item), GFP_NOFS);
6028 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6029 bit, (unsigned long long)blkno);
6031 item->free_blk = blkno;
6032 item->free_bit = bit;
6033 item->free_next = ctxt->c_global_allocator;
6035 ctxt->c_global_allocator = item;
6039 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6040 struct ocfs2_cached_block_free *head)
6042 struct ocfs2_cached_block_free *tmp;
6043 struct inode *tl_inode = osb->osb_tl_inode;
6047 mutex_lock(&tl_inode->i_mutex);
6050 if (ocfs2_truncate_log_needs_flush(osb)) {
6051 ret = __ocfs2_flush_truncate_log(osb);
6058 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6059 if (IS_ERR(handle)) {
6060 ret = PTR_ERR(handle);
6065 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6068 ocfs2_commit_trans(osb, handle);
6070 head = head->free_next;
6079 mutex_unlock(&tl_inode->i_mutex);
6082 /* Premature exit may have left some dangling items. */
6084 head = head->free_next;
6091 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6092 struct ocfs2_cached_dealloc_ctxt *ctxt)
6095 struct ocfs2_per_slot_free_list *fl;
6100 while (ctxt->c_first_suballocator) {
6101 fl = ctxt->c_first_suballocator;
6104 mlog(0, "Free items: (type %u, slot %d)\n",
6105 fl->f_inode_type, fl->f_slot);
6106 ret2 = ocfs2_free_cached_blocks(osb,
6116 ctxt->c_first_suballocator = fl->f_next_suballocator;
6120 if (ctxt->c_global_allocator) {
6121 ret2 = ocfs2_free_cached_clusters(osb,
6122 ctxt->c_global_allocator);
6128 ctxt->c_global_allocator = NULL;
6134 static struct ocfs2_per_slot_free_list *
6135 ocfs2_find_per_slot_free_list(int type,
6137 struct ocfs2_cached_dealloc_ctxt *ctxt)
6139 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6142 if (fl->f_inode_type == type && fl->f_slot == slot)
6145 fl = fl->f_next_suballocator;
6148 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6150 fl->f_inode_type = type;
6153 fl->f_next_suballocator = ctxt->c_first_suballocator;
6155 ctxt->c_first_suballocator = fl;
6160 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6161 int type, int slot, u64 blkno,
6165 struct ocfs2_per_slot_free_list *fl;
6166 struct ocfs2_cached_block_free *item;
6168 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6175 item = kmalloc(sizeof(*item), GFP_NOFS);
6182 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6183 type, slot, bit, (unsigned long long)blkno);
6185 item->free_blk = blkno;
6186 item->free_bit = bit;
6187 item->free_next = fl->f_first;
6196 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6197 struct ocfs2_extent_block *eb)
6199 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6200 le16_to_cpu(eb->h_suballoc_slot),
6201 le64_to_cpu(eb->h_blkno),
6202 le16_to_cpu(eb->h_suballoc_bit));
6205 /* This function will figure out whether the currently last extent
6206 * block will be deleted, and if it will, what the new last extent
6207 * block will be so we can update his h_next_leaf_blk field, as well
6208 * as the dinodes i_last_eb_blk */
6209 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6210 unsigned int clusters_to_del,
6211 struct ocfs2_path *path,
6212 struct buffer_head **new_last_eb)
6214 int next_free, ret = 0;
6216 struct ocfs2_extent_rec *rec;
6217 struct ocfs2_extent_block *eb;
6218 struct ocfs2_extent_list *el;
6219 struct buffer_head *bh = NULL;
6221 *new_last_eb = NULL;
6223 /* we have no tree, so of course, no last_eb. */
6224 if (!path->p_tree_depth)
6227 /* trunc to zero special case - this makes tree_depth = 0
6228 * regardless of what it is. */
6229 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6232 el = path_leaf_el(path);
6233 BUG_ON(!el->l_next_free_rec);
6236 * Make sure that this extent list will actually be empty
6237 * after we clear away the data. We can shortcut out if
6238 * there's more than one non-empty extent in the
6239 * list. Otherwise, a check of the remaining extent is
6242 next_free = le16_to_cpu(el->l_next_free_rec);
6244 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6248 /* We may have a valid extent in index 1, check it. */
6250 rec = &el->l_recs[1];
6253 * Fall through - no more nonempty extents, so we want
6254 * to delete this leaf.
6260 rec = &el->l_recs[0];
6265 * Check it we'll only be trimming off the end of this
6268 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6272 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6278 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6284 eb = (struct ocfs2_extent_block *) bh->b_data;
6287 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6288 * Any corruption is a code bug. */
6289 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
6292 get_bh(*new_last_eb);
6293 mlog(0, "returning block %llu, (cpos: %u)\n",
6294 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6302 * Trim some clusters off the rightmost edge of a tree. Only called
6305 * The caller needs to:
6306 * - start journaling of each path component.
6307 * - compute and fully set up any new last ext block
6309 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6310 handle_t *handle, struct ocfs2_truncate_context *tc,
6311 u32 clusters_to_del, u64 *delete_start)
6313 int ret, i, index = path->p_tree_depth;
6316 struct buffer_head *bh;
6317 struct ocfs2_extent_list *el;
6318 struct ocfs2_extent_rec *rec;
6322 while (index >= 0) {
6323 bh = path->p_node[index].bh;
6324 el = path->p_node[index].el;
6326 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6327 index, (unsigned long long)bh->b_blocknr);
6329 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6332 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6333 ocfs2_error(inode->i_sb,
6334 "Inode %lu has invalid ext. block %llu",
6336 (unsigned long long)bh->b_blocknr);
6342 i = le16_to_cpu(el->l_next_free_rec) - 1;
6343 rec = &el->l_recs[i];
6345 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6346 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6347 ocfs2_rec_clusters(el, rec),
6348 (unsigned long long)le64_to_cpu(rec->e_blkno),
6349 le16_to_cpu(el->l_next_free_rec));
6351 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6353 if (le16_to_cpu(el->l_tree_depth) == 0) {
6355 * If the leaf block contains a single empty
6356 * extent and no records, we can just remove
6359 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6361 sizeof(struct ocfs2_extent_rec));
6362 el->l_next_free_rec = cpu_to_le16(0);
6368 * Remove any empty extents by shifting things
6369 * left. That should make life much easier on
6370 * the code below. This condition is rare
6371 * enough that we shouldn't see a performance
6374 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6375 le16_add_cpu(&el->l_next_free_rec, -1);
6378 i < le16_to_cpu(el->l_next_free_rec); i++)
6379 el->l_recs[i] = el->l_recs[i + 1];
6381 memset(&el->l_recs[i], 0,
6382 sizeof(struct ocfs2_extent_rec));
6385 * We've modified our extent list. The
6386 * simplest way to handle this change
6387 * is to being the search from the
6390 goto find_tail_record;
6393 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6396 * We'll use "new_edge" on our way back up the
6397 * tree to know what our rightmost cpos is.
6399 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6400 new_edge += le32_to_cpu(rec->e_cpos);
6403 * The caller will use this to delete data blocks.
6405 *delete_start = le64_to_cpu(rec->e_blkno)
6406 + ocfs2_clusters_to_blocks(inode->i_sb,
6407 le16_to_cpu(rec->e_leaf_clusters));
6410 * If it's now empty, remove this record.
6412 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6414 sizeof(struct ocfs2_extent_rec));
6415 le16_add_cpu(&el->l_next_free_rec, -1);
6418 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6420 sizeof(struct ocfs2_extent_rec));
6421 le16_add_cpu(&el->l_next_free_rec, -1);
6426 /* Can this actually happen? */
6427 if (le16_to_cpu(el->l_next_free_rec) == 0)
6431 * We never actually deleted any clusters
6432 * because our leaf was empty. There's no
6433 * reason to adjust the rightmost edge then.
6438 rec->e_int_clusters = cpu_to_le32(new_edge);
6439 le32_add_cpu(&rec->e_int_clusters,
6440 -le32_to_cpu(rec->e_cpos));
6443 * A deleted child record should have been
6446 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6450 ret = ocfs2_journal_dirty(handle, bh);
6456 mlog(0, "extent list container %llu, after: record %d: "
6457 "(%u, %u, %llu), next = %u.\n",
6458 (unsigned long long)bh->b_blocknr, i,
6459 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6460 (unsigned long long)le64_to_cpu(rec->e_blkno),
6461 le16_to_cpu(el->l_next_free_rec));
6464 * We must be careful to only attempt delete of an
6465 * extent block (and not the root inode block).
6467 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6468 struct ocfs2_extent_block *eb =
6469 (struct ocfs2_extent_block *)bh->b_data;
6472 * Save this for use when processing the
6475 deleted_eb = le64_to_cpu(eb->h_blkno);
6477 mlog(0, "deleting this extent block.\n");
6479 ocfs2_remove_from_cache(inode, bh);
6481 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6482 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6483 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6485 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6486 /* An error here is not fatal. */
6501 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6502 unsigned int clusters_to_del,
6503 struct inode *inode,
6504 struct buffer_head *fe_bh,
6506 struct ocfs2_truncate_context *tc,
6507 struct ocfs2_path *path)
6510 struct ocfs2_dinode *fe;
6511 struct ocfs2_extent_block *last_eb = NULL;
6512 struct ocfs2_extent_list *el;
6513 struct buffer_head *last_eb_bh = NULL;
6516 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6518 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6526 * Each component will be touched, so we might as well journal
6527 * here to avoid having to handle errors later.
6529 status = ocfs2_journal_access_path(inode, handle, path);
6536 status = ocfs2_journal_access(handle, inode, last_eb_bh,
6537 OCFS2_JOURNAL_ACCESS_WRITE);
6543 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6546 el = &(fe->id2.i_list);
6549 * Lower levels depend on this never happening, but it's best
6550 * to check it up here before changing the tree.
6552 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6553 ocfs2_error(inode->i_sb,
6554 "Inode %lu has an empty extent record, depth %u\n",
6555 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6560 spin_lock(&OCFS2_I(inode)->ip_lock);
6561 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6563 spin_unlock(&OCFS2_I(inode)->ip_lock);
6564 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6565 inode->i_blocks = ocfs2_inode_sector_count(inode);
6567 status = ocfs2_trim_tree(inode, path, handle, tc,
6568 clusters_to_del, &delete_blk);
6574 if (le32_to_cpu(fe->i_clusters) == 0) {
6575 /* trunc to zero is a special case. */
6576 el->l_tree_depth = 0;
6577 fe->i_last_eb_blk = 0;
6579 fe->i_last_eb_blk = last_eb->h_blkno;
6581 status = ocfs2_journal_dirty(handle, fe_bh);
6588 /* If there will be a new last extent block, then by
6589 * definition, there cannot be any leaves to the right of
6591 last_eb->h_next_leaf_blk = 0;
6592 status = ocfs2_journal_dirty(handle, last_eb_bh);
6600 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6614 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6616 set_buffer_uptodate(bh);
6617 mark_buffer_dirty(bh);
6621 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6622 unsigned int from, unsigned int to,
6623 struct page *page, int zero, u64 *phys)
6625 int ret, partial = 0;
6627 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6632 zero_user_segment(page, from, to);
6635 * Need to set the buffers we zero'd into uptodate
6636 * here if they aren't - ocfs2_map_page_blocks()
6637 * might've skipped some
6639 ret = walk_page_buffers(handle, page_buffers(page),
6644 else if (ocfs2_should_order_data(inode)) {
6645 ret = ocfs2_jbd2_file_inode(handle, inode);
6646 #ifdef CONFIG_OCFS2_COMPAT_JBD
6647 ret = walk_page_buffers(handle, page_buffers(page),
6649 ocfs2_journal_dirty_data);
6656 SetPageUptodate(page);
6658 flush_dcache_page(page);
6661 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6662 loff_t end, struct page **pages,
6663 int numpages, u64 phys, handle_t *handle)
6667 unsigned int from, to = PAGE_CACHE_SIZE;
6668 struct super_block *sb = inode->i_sb;
6670 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6675 to = PAGE_CACHE_SIZE;
6676 for(i = 0; i < numpages; i++) {
6679 from = start & (PAGE_CACHE_SIZE - 1);
6680 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6681 to = end & (PAGE_CACHE_SIZE - 1);
6683 BUG_ON(from > PAGE_CACHE_SIZE);
6684 BUG_ON(to > PAGE_CACHE_SIZE);
6686 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6689 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6693 ocfs2_unlock_and_free_pages(pages, numpages);
6696 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6697 struct page **pages, int *num)
6699 int numpages, ret = 0;
6700 struct super_block *sb = inode->i_sb;
6701 struct address_space *mapping = inode->i_mapping;
6702 unsigned long index;
6703 loff_t last_page_bytes;
6705 BUG_ON(start > end);
6707 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6708 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6711 last_page_bytes = PAGE_ALIGN(end);
6712 index = start >> PAGE_CACHE_SHIFT;
6714 pages[numpages] = grab_cache_page(mapping, index);
6715 if (!pages[numpages]) {
6723 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6728 ocfs2_unlock_and_free_pages(pages, numpages);
6738 * Zero the area past i_size but still within an allocated
6739 * cluster. This avoids exposing nonzero data on subsequent file
6742 * We need to call this before i_size is updated on the inode because
6743 * otherwise block_write_full_page() will skip writeout of pages past
6744 * i_size. The new_i_size parameter is passed for this reason.
6746 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6747 u64 range_start, u64 range_end)
6749 int ret = 0, numpages;
6750 struct page **pages = NULL;
6752 unsigned int ext_flags;
6753 struct super_block *sb = inode->i_sb;
6756 * File systems which don't support sparse files zero on every
6759 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6762 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6763 sizeof(struct page *), GFP_NOFS);
6764 if (pages == NULL) {
6770 if (range_start == range_end)
6773 ret = ocfs2_extent_map_get_blocks(inode,
6774 range_start >> sb->s_blocksize_bits,
6775 &phys, NULL, &ext_flags);
6782 * Tail is a hole, or is marked unwritten. In either case, we
6783 * can count on read and write to return/push zero's.
6785 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6788 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6795 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6796 numpages, phys, handle);
6799 * Initiate writeout of the pages we zero'd here. We don't
6800 * wait on them - the truncate_inode_pages() call later will
6803 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6804 range_end - 1, SYNC_FILE_RANGE_WRITE);
6815 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6816 struct ocfs2_dinode *di)
6818 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6819 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6821 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6822 memset(&di->id2, 0, blocksize -
6823 offsetof(struct ocfs2_dinode, id2) -
6826 memset(&di->id2, 0, blocksize -
6827 offsetof(struct ocfs2_dinode, id2));
6830 void ocfs2_dinode_new_extent_list(struct inode *inode,
6831 struct ocfs2_dinode *di)
6833 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6834 di->id2.i_list.l_tree_depth = 0;
6835 di->id2.i_list.l_next_free_rec = 0;
6836 di->id2.i_list.l_count = cpu_to_le16(
6837 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6840 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6842 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6843 struct ocfs2_inline_data *idata = &di->id2.i_data;
6845 spin_lock(&oi->ip_lock);
6846 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6847 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6848 spin_unlock(&oi->ip_lock);
6851 * We clear the entire i_data structure here so that all
6852 * fields can be properly initialized.
6854 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6856 idata->id_count = cpu_to_le16(
6857 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6860 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6861 struct buffer_head *di_bh)
6863 int ret, i, has_data, num_pages = 0;
6865 u64 uninitialized_var(block);
6866 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6867 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6868 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6869 struct ocfs2_alloc_context *data_ac = NULL;
6870 struct page **pages = NULL;
6871 loff_t end = osb->s_clustersize;
6872 struct ocfs2_extent_tree et;
6874 has_data = i_size_read(inode) ? 1 : 0;
6877 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6878 sizeof(struct page *), GFP_NOFS);
6879 if (pages == NULL) {
6885 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6892 handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
6893 if (IS_ERR(handle)) {
6894 ret = PTR_ERR(handle);
6899 ret = ocfs2_journal_access(handle, inode, di_bh,
6900 OCFS2_JOURNAL_ACCESS_WRITE);
6908 unsigned int page_end;
6911 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6919 * Save two copies, one for insert, and one that can
6920 * be changed by ocfs2_map_and_dirty_page() below.
6922 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6925 * Non sparse file systems zero on extend, so no need
6928 if (!ocfs2_sparse_alloc(osb) &&
6929 PAGE_CACHE_SIZE < osb->s_clustersize)
6930 end = PAGE_CACHE_SIZE;
6932 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6939 * This should populate the 1st page for us and mark
6942 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6948 page_end = PAGE_CACHE_SIZE;
6949 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6950 page_end = osb->s_clustersize;
6952 for (i = 0; i < num_pages; i++)
6953 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6954 pages[i], i > 0, &phys);
6957 spin_lock(&oi->ip_lock);
6958 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6959 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6960 spin_unlock(&oi->ip_lock);
6962 ocfs2_dinode_new_extent_list(inode, di);
6964 ocfs2_journal_dirty(handle, di_bh);
6968 * An error at this point should be extremely rare. If
6969 * this proves to be false, we could always re-build
6970 * the in-inode data from our pages.
6972 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
6973 ret = ocfs2_insert_extent(osb, handle, inode, &et,
6974 0, block, 1, 0, NULL);
6980 inode->i_blocks = ocfs2_inode_sector_count(inode);
6984 ocfs2_commit_trans(osb, handle);
6988 ocfs2_free_alloc_context(data_ac);
6992 ocfs2_unlock_and_free_pages(pages, num_pages);
7000 * It is expected, that by the time you call this function,
7001 * inode->i_size and fe->i_size have been adjusted.
7003 * WARNING: This will kfree the truncate context
7005 int ocfs2_commit_truncate(struct ocfs2_super *osb,
7006 struct inode *inode,
7007 struct buffer_head *fe_bh,
7008 struct ocfs2_truncate_context *tc)
7010 int status, i, credits, tl_sem = 0;
7011 u32 clusters_to_del, new_highest_cpos, range;
7012 struct ocfs2_extent_list *el;
7013 handle_t *handle = NULL;
7014 struct inode *tl_inode = osb->osb_tl_inode;
7015 struct ocfs2_path *path = NULL;
7016 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
7020 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
7021 i_size_read(inode));
7023 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
7030 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7034 * Check that we still have allocation to delete.
7036 if (OCFS2_I(inode)->ip_clusters == 0) {
7042 * Truncate always works against the rightmost tree branch.
7044 status = ocfs2_find_path(inode, path, UINT_MAX);
7050 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7051 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7054 * By now, el will point to the extent list on the bottom most
7055 * portion of this tree. Only the tail record is considered in
7058 * We handle the following cases, in order:
7059 * - empty extent: delete the remaining branch
7060 * - remove the entire record
7061 * - remove a partial record
7062 * - no record needs to be removed (truncate has completed)
7064 el = path_leaf_el(path);
7065 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7066 ocfs2_error(inode->i_sb,
7067 "Inode %llu has empty extent block at %llu\n",
7068 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7069 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7074 i = le16_to_cpu(el->l_next_free_rec) - 1;
7075 range = le32_to_cpu(el->l_recs[i].e_cpos) +
7076 ocfs2_rec_clusters(el, &el->l_recs[i]);
7077 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7078 clusters_to_del = 0;
7079 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
7080 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
7081 } else if (range > new_highest_cpos) {
7082 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
7083 le32_to_cpu(el->l_recs[i].e_cpos)) -
7090 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7091 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7093 mutex_lock(&tl_inode->i_mutex);
7095 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7096 * record is free for use. If there isn't any, we flush to get
7097 * an empty truncate log. */
7098 if (ocfs2_truncate_log_needs_flush(osb)) {
7099 status = __ocfs2_flush_truncate_log(osb);
7106 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
7107 (struct ocfs2_dinode *)fe_bh->b_data,
7109 handle = ocfs2_start_trans(osb, credits);
7110 if (IS_ERR(handle)) {
7111 status = PTR_ERR(handle);
7117 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7124 mutex_unlock(&tl_inode->i_mutex);
7127 ocfs2_commit_trans(osb, handle);
7130 ocfs2_reinit_path(path, 1);
7133 * The check above will catch the case where we've truncated
7134 * away all allocation.
7140 ocfs2_schedule_truncate_log_flush(osb, 1);
7143 mutex_unlock(&tl_inode->i_mutex);
7146 ocfs2_commit_trans(osb, handle);
7148 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7150 ocfs2_free_path(path);
7152 /* This will drop the ext_alloc cluster lock for us */
7153 ocfs2_free_truncate_context(tc);
7160 * Expects the inode to already be locked.
7162 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7163 struct inode *inode,
7164 struct buffer_head *fe_bh,
7165 struct ocfs2_truncate_context **tc)
7168 unsigned int new_i_clusters;
7169 struct ocfs2_dinode *fe;
7170 struct ocfs2_extent_block *eb;
7171 struct buffer_head *last_eb_bh = NULL;
7177 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7178 i_size_read(inode));
7179 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7181 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7182 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7183 (unsigned long long)le64_to_cpu(fe->i_size));
7185 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7191 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7193 if (fe->id2.i_list.l_tree_depth) {
7194 status = ocfs2_read_extent_block(inode,
7195 le64_to_cpu(fe->i_last_eb_blk),
7201 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7204 (*tc)->tc_last_eb_bh = last_eb_bh;
7210 ocfs2_free_truncate_context(*tc);
7218 * 'start' is inclusive, 'end' is not.
7220 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7221 unsigned int start, unsigned int end, int trunc)
7224 unsigned int numbytes;
7226 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7227 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7228 struct ocfs2_inline_data *idata = &di->id2.i_data;
7230 if (end > i_size_read(inode))
7231 end = i_size_read(inode);
7233 BUG_ON(start >= end);
7235 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7236 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7237 !ocfs2_supports_inline_data(osb)) {
7238 ocfs2_error(inode->i_sb,
7239 "Inline data flags for inode %llu don't agree! "
7240 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7241 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7242 le16_to_cpu(di->i_dyn_features),
7243 OCFS2_I(inode)->ip_dyn_features,
7244 osb->s_feature_incompat);
7249 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7250 if (IS_ERR(handle)) {
7251 ret = PTR_ERR(handle);
7256 ret = ocfs2_journal_access(handle, inode, di_bh,
7257 OCFS2_JOURNAL_ACCESS_WRITE);
7263 numbytes = end - start;
7264 memset(idata->id_data + start, 0, numbytes);
7267 * No need to worry about the data page here - it's been
7268 * truncated already and inline data doesn't need it for
7269 * pushing zero's to disk, so we'll let readpage pick it up
7273 i_size_write(inode, start);
7274 di->i_size = cpu_to_le64(start);
7277 inode->i_blocks = ocfs2_inode_sector_count(inode);
7278 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7280 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7281 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7283 ocfs2_journal_dirty(handle, di_bh);
7286 ocfs2_commit_trans(osb, handle);
7292 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7295 * The caller is responsible for completing deallocation
7296 * before freeing the context.
7298 if (tc->tc_dealloc.c_first_suballocator != NULL)
7300 "Truncate completion has non-empty dealloc context\n");
7302 brelse(tc->tc_last_eb_bh);