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>
31 #include <linux/quotaops.h>
33 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
34 #include <cluster/masklog.h>
40 #include "blockcheck.h"
42 #include "extent_map.h"
45 #include "localalloc.h"
53 #include "buffer_head_io.h"
57 * Operations for a specific extent tree type.
59 * To implement an on-disk btree (extent tree) type in ocfs2, add
60 * an ocfs2_extent_tree_operations structure and the matching
61 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
62 * for the allocation portion of the extent tree.
64 struct ocfs2_extent_tree_operations {
66 * last_eb_blk is the block number of the right most leaf extent
67 * block. Most on-disk structures containing an extent tree store
68 * this value for fast access. The ->eo_set_last_eb_blk() and
69 * ->eo_get_last_eb_blk() operations access this value. They are
72 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
74 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
77 * The on-disk structure usually keeps track of how many total
78 * clusters are stored in this extent tree. This function updates
79 * that value. new_clusters is the delta, and must be
80 * added to the total. Required.
82 void (*eo_update_clusters)(struct inode *inode,
83 struct ocfs2_extent_tree *et,
87 * If ->eo_insert_check() exists, it is called before rec is
88 * inserted into the extent tree. It is optional.
90 int (*eo_insert_check)(struct inode *inode,
91 struct ocfs2_extent_tree *et,
92 struct ocfs2_extent_rec *rec);
93 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
96 * --------------------------------------------------------------
97 * The remaining are internal to ocfs2_extent_tree and don't have
102 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
105 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
108 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
109 * it exists. If it does not, et->et_max_leaf_clusters is set
110 * to 0 (unlimited). Optional.
112 void (*eo_fill_max_leaf_clusters)(struct inode *inode,
113 struct ocfs2_extent_tree *et);
118 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
121 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
122 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
124 static void ocfs2_dinode_update_clusters(struct inode *inode,
125 struct ocfs2_extent_tree *et,
127 static int ocfs2_dinode_insert_check(struct inode *inode,
128 struct ocfs2_extent_tree *et,
129 struct ocfs2_extent_rec *rec);
130 static int ocfs2_dinode_sanity_check(struct inode *inode,
131 struct ocfs2_extent_tree *et);
132 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
133 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
134 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
135 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
136 .eo_update_clusters = ocfs2_dinode_update_clusters,
137 .eo_insert_check = ocfs2_dinode_insert_check,
138 .eo_sanity_check = ocfs2_dinode_sanity_check,
139 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
142 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
145 struct ocfs2_dinode *di = et->et_object;
147 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
148 di->i_last_eb_blk = cpu_to_le64(blkno);
151 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
153 struct ocfs2_dinode *di = et->et_object;
155 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
156 return le64_to_cpu(di->i_last_eb_blk);
159 static void ocfs2_dinode_update_clusters(struct inode *inode,
160 struct ocfs2_extent_tree *et,
163 struct ocfs2_dinode *di = et->et_object;
165 le32_add_cpu(&di->i_clusters, clusters);
166 spin_lock(&OCFS2_I(inode)->ip_lock);
167 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
168 spin_unlock(&OCFS2_I(inode)->ip_lock);
171 static int ocfs2_dinode_insert_check(struct inode *inode,
172 struct ocfs2_extent_tree *et,
173 struct ocfs2_extent_rec *rec)
175 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
177 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
178 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
179 (OCFS2_I(inode)->ip_clusters !=
180 le32_to_cpu(rec->e_cpos)),
181 "Device %s, asking for sparse allocation: inode %llu, "
182 "cpos %u, clusters %u\n",
184 (unsigned long long)OCFS2_I(inode)->ip_blkno,
186 OCFS2_I(inode)->ip_clusters);
191 static int ocfs2_dinode_sanity_check(struct inode *inode,
192 struct ocfs2_extent_tree *et)
194 struct ocfs2_dinode *di = et->et_object;
196 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
197 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
202 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
204 struct ocfs2_dinode *di = et->et_object;
206 et->et_root_el = &di->id2.i_list;
210 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
212 struct ocfs2_xattr_value_buf *vb = et->et_object;
214 et->et_root_el = &vb->vb_xv->xr_list;
217 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
220 struct ocfs2_xattr_value_buf *vb = et->et_object;
222 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
225 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
227 struct ocfs2_xattr_value_buf *vb = et->et_object;
229 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
232 static void ocfs2_xattr_value_update_clusters(struct inode *inode,
233 struct ocfs2_extent_tree *et,
236 struct ocfs2_xattr_value_buf *vb = et->et_object;
238 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
241 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
242 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
243 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
244 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
245 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
248 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
250 struct ocfs2_xattr_block *xb = et->et_object;
252 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
255 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
256 struct ocfs2_extent_tree *et)
258 et->et_max_leaf_clusters =
259 ocfs2_clusters_for_bytes(inode->i_sb,
260 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
263 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
266 struct ocfs2_xattr_block *xb = et->et_object;
267 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
269 xt->xt_last_eb_blk = cpu_to_le64(blkno);
272 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
274 struct ocfs2_xattr_block *xb = et->et_object;
275 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
277 return le64_to_cpu(xt->xt_last_eb_blk);
280 static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
281 struct ocfs2_extent_tree *et,
284 struct ocfs2_xattr_block *xb = et->et_object;
286 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
289 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
290 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
291 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
292 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
293 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
294 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
297 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
299 struct buffer_head *bh,
300 ocfs2_journal_access_func access,
302 struct ocfs2_extent_tree_operations *ops)
306 et->et_root_journal_access = access;
308 obj = (void *)bh->b_data;
311 et->et_ops->eo_fill_root_el(et);
312 if (!et->et_ops->eo_fill_max_leaf_clusters)
313 et->et_max_leaf_clusters = 0;
315 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
318 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
320 struct buffer_head *bh)
322 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di,
323 NULL, &ocfs2_dinode_et_ops);
326 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
328 struct buffer_head *bh)
330 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb,
331 NULL, &ocfs2_xattr_tree_et_ops);
334 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
336 struct ocfs2_xattr_value_buf *vb)
338 __ocfs2_init_extent_tree(et, inode, vb->vb_bh, vb->vb_access, vb,
339 &ocfs2_xattr_value_et_ops);
342 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
345 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
348 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
350 return et->et_ops->eo_get_last_eb_blk(et);
353 static inline void ocfs2_et_update_clusters(struct inode *inode,
354 struct ocfs2_extent_tree *et,
357 et->et_ops->eo_update_clusters(inode, et, clusters);
360 static inline int ocfs2_et_root_journal_access(handle_t *handle,
362 struct ocfs2_extent_tree *et,
365 return et->et_root_journal_access(handle, inode, et->et_root_bh,
369 static inline int ocfs2_et_insert_check(struct inode *inode,
370 struct ocfs2_extent_tree *et,
371 struct ocfs2_extent_rec *rec)
375 if (et->et_ops->eo_insert_check)
376 ret = et->et_ops->eo_insert_check(inode, et, rec);
380 static inline int ocfs2_et_sanity_check(struct inode *inode,
381 struct ocfs2_extent_tree *et)
385 if (et->et_ops->eo_sanity_check)
386 ret = et->et_ops->eo_sanity_check(inode, et);
390 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
391 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
392 struct ocfs2_extent_block *eb);
395 * Structures which describe a path through a btree, and functions to
398 * The idea here is to be as generic as possible with the tree
401 struct ocfs2_path_item {
402 struct buffer_head *bh;
403 struct ocfs2_extent_list *el;
406 #define OCFS2_MAX_PATH_DEPTH 5
410 ocfs2_journal_access_func p_root_access;
411 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
414 #define path_root_bh(_path) ((_path)->p_node[0].bh)
415 #define path_root_el(_path) ((_path)->p_node[0].el)
416 #define path_root_access(_path)((_path)->p_root_access)
417 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
418 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
419 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
422 * Reset the actual path elements so that we can re-use the structure
423 * to build another path. Generally, this involves freeing the buffer
426 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
428 int i, start = 0, depth = 0;
429 struct ocfs2_path_item *node;
434 for(i = start; i < path_num_items(path); i++) {
435 node = &path->p_node[i];
443 * Tree depth may change during truncate, or insert. If we're
444 * keeping the root extent list, then make sure that our path
445 * structure reflects the proper depth.
448 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
450 path_root_access(path) = NULL;
452 path->p_tree_depth = depth;
455 static void ocfs2_free_path(struct ocfs2_path *path)
458 ocfs2_reinit_path(path, 0);
464 * All the elements of src into dest. After this call, src could be freed
465 * without affecting dest.
467 * Both paths should have the same root. Any non-root elements of dest
470 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
474 BUG_ON(path_root_bh(dest) != path_root_bh(src));
475 BUG_ON(path_root_el(dest) != path_root_el(src));
476 BUG_ON(path_root_access(dest) != path_root_access(src));
478 ocfs2_reinit_path(dest, 1);
480 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
481 dest->p_node[i].bh = src->p_node[i].bh;
482 dest->p_node[i].el = src->p_node[i].el;
484 if (dest->p_node[i].bh)
485 get_bh(dest->p_node[i].bh);
490 * Make the *dest path the same as src and re-initialize src path to
493 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
497 BUG_ON(path_root_bh(dest) != path_root_bh(src));
498 BUG_ON(path_root_access(dest) != path_root_access(src));
500 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
501 brelse(dest->p_node[i].bh);
503 dest->p_node[i].bh = src->p_node[i].bh;
504 dest->p_node[i].el = src->p_node[i].el;
506 src->p_node[i].bh = NULL;
507 src->p_node[i].el = NULL;
512 * Insert an extent block at given index.
514 * This will not take an additional reference on eb_bh.
516 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
517 struct buffer_head *eb_bh)
519 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
522 * Right now, no root bh is an extent block, so this helps
523 * catch code errors with dinode trees. The assertion can be
524 * safely removed if we ever need to insert extent block
525 * structures at the root.
529 path->p_node[index].bh = eb_bh;
530 path->p_node[index].el = &eb->h_list;
533 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
534 struct ocfs2_extent_list *root_el,
535 ocfs2_journal_access_func access)
537 struct ocfs2_path *path;
539 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
541 path = kzalloc(sizeof(*path), GFP_NOFS);
543 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
545 path_root_bh(path) = root_bh;
546 path_root_el(path) = root_el;
547 path_root_access(path) = access;
553 static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
555 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
556 path_root_access(path));
559 static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
561 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
562 et->et_root_journal_access);
566 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
567 * otherwise it's the root_access function.
569 * I don't like the way this function's name looks next to
570 * ocfs2_journal_access_path(), but I don't have a better one.
572 static int ocfs2_path_bh_journal_access(handle_t *handle,
574 struct ocfs2_path *path,
577 ocfs2_journal_access_func access = path_root_access(path);
580 access = ocfs2_journal_access;
583 access = ocfs2_journal_access_eb;
585 return access(handle, inode, path->p_node[idx].bh,
586 OCFS2_JOURNAL_ACCESS_WRITE);
590 * Convenience function to journal all components in a path.
592 static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
593 struct ocfs2_path *path)
600 for(i = 0; i < path_num_items(path); i++) {
601 ret = ocfs2_path_bh_journal_access(handle, inode, path, i);
613 * Return the index of the extent record which contains cluster #v_cluster.
614 * -1 is returned if it was not found.
616 * Should work fine on interior and exterior nodes.
618 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
622 struct ocfs2_extent_rec *rec;
623 u32 rec_end, rec_start, clusters;
625 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
626 rec = &el->l_recs[i];
628 rec_start = le32_to_cpu(rec->e_cpos);
629 clusters = ocfs2_rec_clusters(el, rec);
631 rec_end = rec_start + clusters;
633 if (v_cluster >= rec_start && v_cluster < rec_end) {
642 enum ocfs2_contig_type {
651 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
652 * ocfs2_extent_contig only work properly against leaf nodes!
654 static int ocfs2_block_extent_contig(struct super_block *sb,
655 struct ocfs2_extent_rec *ext,
658 u64 blk_end = le64_to_cpu(ext->e_blkno);
660 blk_end += ocfs2_clusters_to_blocks(sb,
661 le16_to_cpu(ext->e_leaf_clusters));
663 return blkno == blk_end;
666 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
667 struct ocfs2_extent_rec *right)
671 left_range = le32_to_cpu(left->e_cpos) +
672 le16_to_cpu(left->e_leaf_clusters);
674 return (left_range == le32_to_cpu(right->e_cpos));
677 static enum ocfs2_contig_type
678 ocfs2_extent_contig(struct inode *inode,
679 struct ocfs2_extent_rec *ext,
680 struct ocfs2_extent_rec *insert_rec)
682 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
685 * Refuse to coalesce extent records with different flag
686 * fields - we don't want to mix unwritten extents with user
689 if (ext->e_flags != insert_rec->e_flags)
692 if (ocfs2_extents_adjacent(ext, insert_rec) &&
693 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
696 blkno = le64_to_cpu(ext->e_blkno);
697 if (ocfs2_extents_adjacent(insert_rec, ext) &&
698 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
705 * NOTE: We can have pretty much any combination of contiguousness and
708 * The usefulness of APPEND_TAIL is more in that it lets us know that
709 * we'll have to update the path to that leaf.
711 enum ocfs2_append_type {
716 enum ocfs2_split_type {
722 struct ocfs2_insert_type {
723 enum ocfs2_split_type ins_split;
724 enum ocfs2_append_type ins_appending;
725 enum ocfs2_contig_type ins_contig;
726 int ins_contig_index;
730 struct ocfs2_merge_ctxt {
731 enum ocfs2_contig_type c_contig_type;
732 int c_has_empty_extent;
733 int c_split_covers_rec;
736 static int ocfs2_validate_extent_block(struct super_block *sb,
737 struct buffer_head *bh)
740 struct ocfs2_extent_block *eb =
741 (struct ocfs2_extent_block *)bh->b_data;
743 mlog(0, "Validating extent block %llu\n",
744 (unsigned long long)bh->b_blocknr);
746 BUG_ON(!buffer_uptodate(bh));
749 * If the ecc fails, we return the error but otherwise
750 * leave the filesystem running. We know any error is
751 * local to this block.
753 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
755 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
756 (unsigned long long)bh->b_blocknr);
761 * Errors after here are fatal.
764 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
766 "Extent block #%llu has bad signature %.*s",
767 (unsigned long long)bh->b_blocknr, 7,
772 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
774 "Extent block #%llu has an invalid h_blkno "
776 (unsigned long long)bh->b_blocknr,
777 (unsigned long long)le64_to_cpu(eb->h_blkno));
781 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
783 "Extent block #%llu has an invalid "
784 "h_fs_generation of #%u",
785 (unsigned long long)bh->b_blocknr,
786 le32_to_cpu(eb->h_fs_generation));
793 int ocfs2_read_extent_block(struct inode *inode, u64 eb_blkno,
794 struct buffer_head **bh)
797 struct buffer_head *tmp = *bh;
799 rc = ocfs2_read_block(inode, eb_blkno, &tmp,
800 ocfs2_validate_extent_block);
802 /* If ocfs2_read_block() got us a new bh, pass it up. */
811 * How many free extents have we got before we need more meta data?
813 int ocfs2_num_free_extents(struct ocfs2_super *osb,
815 struct ocfs2_extent_tree *et)
818 struct ocfs2_extent_list *el = NULL;
819 struct ocfs2_extent_block *eb;
820 struct buffer_head *eb_bh = NULL;
826 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
829 retval = ocfs2_read_extent_block(inode, last_eb_blk, &eb_bh);
834 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
838 BUG_ON(el->l_tree_depth != 0);
840 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
848 /* expects array to already be allocated
850 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
853 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
857 struct ocfs2_alloc_context *meta_ac,
858 struct buffer_head *bhs[])
860 int count, status, i;
861 u16 suballoc_bit_start;
864 struct ocfs2_extent_block *eb;
869 while (count < wanted) {
870 status = ocfs2_claim_metadata(osb,
882 for(i = count; i < (num_got + count); i++) {
883 bhs[i] = sb_getblk(osb->sb, first_blkno);
884 if (bhs[i] == NULL) {
889 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
891 status = ocfs2_journal_access_eb(handle, inode, bhs[i],
892 OCFS2_JOURNAL_ACCESS_CREATE);
898 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
899 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
900 /* Ok, setup the minimal stuff here. */
901 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
902 eb->h_blkno = cpu_to_le64(first_blkno);
903 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
904 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
905 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
907 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
909 suballoc_bit_start++;
912 /* We'll also be dirtied by the caller, so
913 * this isn't absolutely necessary. */
914 status = ocfs2_journal_dirty(handle, bhs[i]);
927 for(i = 0; i < wanted; i++) {
937 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
939 * Returns the sum of the rightmost extent rec logical offset and
942 * ocfs2_add_branch() uses this to determine what logical cluster
943 * value should be populated into the leftmost new branch records.
945 * ocfs2_shift_tree_depth() uses this to determine the # clusters
946 * value for the new topmost tree record.
948 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
952 i = le16_to_cpu(el->l_next_free_rec) - 1;
954 return le32_to_cpu(el->l_recs[i].e_cpos) +
955 ocfs2_rec_clusters(el, &el->l_recs[i]);
959 * Add an entire tree branch to our inode. eb_bh is the extent block
960 * to start at, if we don't want to start the branch at the dinode
963 * last_eb_bh is required as we have to update it's next_leaf pointer
964 * for the new last extent block.
966 * the new branch will be 'empty' in the sense that every block will
967 * contain a single record with cluster count == 0.
969 static int ocfs2_add_branch(struct ocfs2_super *osb,
972 struct ocfs2_extent_tree *et,
973 struct buffer_head *eb_bh,
974 struct buffer_head **last_eb_bh,
975 struct ocfs2_alloc_context *meta_ac)
977 int status, new_blocks, i;
978 u64 next_blkno, new_last_eb_blk;
979 struct buffer_head *bh;
980 struct buffer_head **new_eb_bhs = NULL;
981 struct ocfs2_extent_block *eb;
982 struct ocfs2_extent_list *eb_el;
983 struct ocfs2_extent_list *el;
988 BUG_ON(!last_eb_bh || !*last_eb_bh);
991 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
996 /* we never add a branch to a leaf. */
997 BUG_ON(!el->l_tree_depth);
999 new_blocks = le16_to_cpu(el->l_tree_depth);
1001 /* allocate the number of new eb blocks we need */
1002 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1010 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
1011 meta_ac, new_eb_bhs);
1017 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1018 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1020 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1021 * linked with the rest of the tree.
1022 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1024 * when we leave the loop, new_last_eb_blk will point to the
1025 * newest leaf, and next_blkno will point to the topmost extent
1027 next_blkno = new_last_eb_blk = 0;
1028 for(i = 0; i < new_blocks; i++) {
1030 eb = (struct ocfs2_extent_block *) bh->b_data;
1031 /* ocfs2_create_new_meta_bhs() should create it right! */
1032 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1033 eb_el = &eb->h_list;
1035 status = ocfs2_journal_access_eb(handle, inode, bh,
1036 OCFS2_JOURNAL_ACCESS_CREATE);
1042 eb->h_next_leaf_blk = 0;
1043 eb_el->l_tree_depth = cpu_to_le16(i);
1044 eb_el->l_next_free_rec = cpu_to_le16(1);
1046 * This actually counts as an empty extent as
1049 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
1050 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
1052 * eb_el isn't always an interior node, but even leaf
1053 * nodes want a zero'd flags and reserved field so
1054 * this gets the whole 32 bits regardless of use.
1056 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
1057 if (!eb_el->l_tree_depth)
1058 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1060 status = ocfs2_journal_dirty(handle, bh);
1066 next_blkno = le64_to_cpu(eb->h_blkno);
1069 /* This is a bit hairy. We want to update up to three blocks
1070 * here without leaving any of them in an inconsistent state
1071 * in case of error. We don't have to worry about
1072 * journal_dirty erroring as it won't unless we've aborted the
1073 * handle (in which case we would never be here) so reserving
1074 * the write with journal_access is all we need to do. */
1075 status = ocfs2_journal_access_eb(handle, inode, *last_eb_bh,
1076 OCFS2_JOURNAL_ACCESS_WRITE);
1081 status = ocfs2_et_root_journal_access(handle, inode, et,
1082 OCFS2_JOURNAL_ACCESS_WRITE);
1088 status = ocfs2_journal_access_eb(handle, inode, eb_bh,
1089 OCFS2_JOURNAL_ACCESS_WRITE);
1096 /* Link the new branch into the rest of the tree (el will
1097 * either be on the root_bh, or the extent block passed in. */
1098 i = le16_to_cpu(el->l_next_free_rec);
1099 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1100 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1101 el->l_recs[i].e_int_clusters = 0;
1102 le16_add_cpu(&el->l_next_free_rec, 1);
1104 /* fe needs a new last extent block pointer, as does the
1105 * next_leaf on the previously last-extent-block. */
1106 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1108 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1109 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1111 status = ocfs2_journal_dirty(handle, *last_eb_bh);
1114 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1118 status = ocfs2_journal_dirty(handle, eb_bh);
1124 * Some callers want to track the rightmost leaf so pass it
1127 brelse(*last_eb_bh);
1128 get_bh(new_eb_bhs[0]);
1129 *last_eb_bh = new_eb_bhs[0];
1134 for (i = 0; i < new_blocks; i++)
1135 brelse(new_eb_bhs[i]);
1144 * adds another level to the allocation tree.
1145 * returns back the new extent block so you can add a branch to it
1148 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
1150 struct inode *inode,
1151 struct ocfs2_extent_tree *et,
1152 struct ocfs2_alloc_context *meta_ac,
1153 struct buffer_head **ret_new_eb_bh)
1157 struct buffer_head *new_eb_bh = NULL;
1158 struct ocfs2_extent_block *eb;
1159 struct ocfs2_extent_list *root_el;
1160 struct ocfs2_extent_list *eb_el;
1164 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1171 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1172 /* ocfs2_create_new_meta_bhs() should create it right! */
1173 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1175 eb_el = &eb->h_list;
1176 root_el = et->et_root_el;
1178 status = ocfs2_journal_access_eb(handle, inode, new_eb_bh,
1179 OCFS2_JOURNAL_ACCESS_CREATE);
1185 /* copy the root extent list data into the new extent block */
1186 eb_el->l_tree_depth = root_el->l_tree_depth;
1187 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1188 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1189 eb_el->l_recs[i] = root_el->l_recs[i];
1191 status = ocfs2_journal_dirty(handle, new_eb_bh);
1197 status = ocfs2_et_root_journal_access(handle, inode, et,
1198 OCFS2_JOURNAL_ACCESS_WRITE);
1204 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1206 /* update root_bh now */
1207 le16_add_cpu(&root_el->l_tree_depth, 1);
1208 root_el->l_recs[0].e_cpos = 0;
1209 root_el->l_recs[0].e_blkno = eb->h_blkno;
1210 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1211 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1212 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1213 root_el->l_next_free_rec = cpu_to_le16(1);
1215 /* If this is our 1st tree depth shift, then last_eb_blk
1216 * becomes the allocated extent block */
1217 if (root_el->l_tree_depth == cpu_to_le16(1))
1218 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1220 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1226 *ret_new_eb_bh = new_eb_bh;
1237 * Should only be called when there is no space left in any of the
1238 * leaf nodes. What we want to do is find the lowest tree depth
1239 * non-leaf extent block with room for new records. There are three
1240 * valid results of this search:
1242 * 1) a lowest extent block is found, then we pass it back in
1243 * *lowest_eb_bh and return '0'
1245 * 2) the search fails to find anything, but the root_el has room. We
1246 * pass NULL back in *lowest_eb_bh, but still return '0'
1248 * 3) the search fails to find anything AND the root_el is full, in
1249 * which case we return > 0
1251 * return status < 0 indicates an error.
1253 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1254 struct inode *inode,
1255 struct ocfs2_extent_tree *et,
1256 struct buffer_head **target_bh)
1260 struct ocfs2_extent_block *eb;
1261 struct ocfs2_extent_list *el;
1262 struct buffer_head *bh = NULL;
1263 struct buffer_head *lowest_bh = NULL;
1269 el = et->et_root_el;
1271 while(le16_to_cpu(el->l_tree_depth) > 1) {
1272 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1273 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
1274 "extent list (next_free_rec == 0)",
1275 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1279 i = le16_to_cpu(el->l_next_free_rec) - 1;
1280 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1282 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
1283 "list where extent # %d has no physical "
1285 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
1293 status = ocfs2_read_extent_block(inode, blkno, &bh);
1299 eb = (struct ocfs2_extent_block *) bh->b_data;
1302 if (le16_to_cpu(el->l_next_free_rec) <
1303 le16_to_cpu(el->l_count)) {
1310 /* If we didn't find one and the fe doesn't have any room,
1311 * then return '1' */
1312 el = et->et_root_el;
1313 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1316 *target_bh = lowest_bh;
1325 * Grow a b-tree so that it has more records.
1327 * We might shift the tree depth in which case existing paths should
1328 * be considered invalid.
1330 * Tree depth after the grow is returned via *final_depth.
1332 * *last_eb_bh will be updated by ocfs2_add_branch().
1334 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
1335 struct ocfs2_extent_tree *et, int *final_depth,
1336 struct buffer_head **last_eb_bh,
1337 struct ocfs2_alloc_context *meta_ac)
1340 struct ocfs2_extent_list *el = et->et_root_el;
1341 int depth = le16_to_cpu(el->l_tree_depth);
1342 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1343 struct buffer_head *bh = NULL;
1345 BUG_ON(meta_ac == NULL);
1347 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
1354 /* We traveled all the way to the bottom of the allocation tree
1355 * and didn't find room for any more extents - we need to add
1356 * another tree level */
1359 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1361 /* ocfs2_shift_tree_depth will return us a buffer with
1362 * the new extent block (so we can pass that to
1363 * ocfs2_add_branch). */
1364 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
1373 * Special case: we have room now if we shifted from
1374 * tree_depth 0, so no more work needs to be done.
1376 * We won't be calling add_branch, so pass
1377 * back *last_eb_bh as the new leaf. At depth
1378 * zero, it should always be null so there's
1379 * no reason to brelse.
1381 BUG_ON(*last_eb_bh);
1388 /* call ocfs2_add_branch to add the final part of the tree with
1390 mlog(0, "add branch. bh = %p\n", bh);
1391 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
1400 *final_depth = depth;
1406 * This function will discard the rightmost extent record.
1408 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1410 int next_free = le16_to_cpu(el->l_next_free_rec);
1411 int count = le16_to_cpu(el->l_count);
1412 unsigned int num_bytes;
1415 /* This will cause us to go off the end of our extent list. */
1416 BUG_ON(next_free >= count);
1418 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1420 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1423 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1424 struct ocfs2_extent_rec *insert_rec)
1426 int i, insert_index, next_free, has_empty, num_bytes;
1427 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1428 struct ocfs2_extent_rec *rec;
1430 next_free = le16_to_cpu(el->l_next_free_rec);
1431 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1435 /* The tree code before us didn't allow enough room in the leaf. */
1436 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1439 * The easiest way to approach this is to just remove the
1440 * empty extent and temporarily decrement next_free.
1444 * If next_free was 1 (only an empty extent), this
1445 * loop won't execute, which is fine. We still want
1446 * the decrement above to happen.
1448 for(i = 0; i < (next_free - 1); i++)
1449 el->l_recs[i] = el->l_recs[i+1];
1455 * Figure out what the new record index should be.
1457 for(i = 0; i < next_free; i++) {
1458 rec = &el->l_recs[i];
1460 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1465 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1466 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1468 BUG_ON(insert_index < 0);
1469 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1470 BUG_ON(insert_index > next_free);
1473 * No need to memmove if we're just adding to the tail.
1475 if (insert_index != next_free) {
1476 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1478 num_bytes = next_free - insert_index;
1479 num_bytes *= sizeof(struct ocfs2_extent_rec);
1480 memmove(&el->l_recs[insert_index + 1],
1481 &el->l_recs[insert_index],
1486 * Either we had an empty extent, and need to re-increment or
1487 * there was no empty extent on a non full rightmost leaf node,
1488 * in which case we still need to increment.
1491 el->l_next_free_rec = cpu_to_le16(next_free);
1493 * Make sure none of the math above just messed up our tree.
1495 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1497 el->l_recs[insert_index] = *insert_rec;
1501 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1503 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1505 BUG_ON(num_recs == 0);
1507 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1509 size = num_recs * sizeof(struct ocfs2_extent_rec);
1510 memmove(&el->l_recs[0], &el->l_recs[1], size);
1511 memset(&el->l_recs[num_recs], 0,
1512 sizeof(struct ocfs2_extent_rec));
1513 el->l_next_free_rec = cpu_to_le16(num_recs);
1518 * Create an empty extent record .
1520 * l_next_free_rec may be updated.
1522 * If an empty extent already exists do nothing.
1524 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1526 int next_free = le16_to_cpu(el->l_next_free_rec);
1528 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1533 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1536 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1537 "Asked to create an empty extent in a full list:\n"
1538 "count = %u, tree depth = %u",
1539 le16_to_cpu(el->l_count),
1540 le16_to_cpu(el->l_tree_depth));
1542 ocfs2_shift_records_right(el);
1545 le16_add_cpu(&el->l_next_free_rec, 1);
1546 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1550 * For a rotation which involves two leaf nodes, the "root node" is
1551 * the lowest level tree node which contains a path to both leafs. This
1552 * resulting set of information can be used to form a complete "subtree"
1554 * This function is passed two full paths from the dinode down to a
1555 * pair of adjacent leaves. It's task is to figure out which path
1556 * index contains the subtree root - this can be the root index itself
1557 * in a worst-case rotation.
1559 * The array index of the subtree root is passed back.
1561 static int ocfs2_find_subtree_root(struct inode *inode,
1562 struct ocfs2_path *left,
1563 struct ocfs2_path *right)
1568 * Check that the caller passed in two paths from the same tree.
1570 BUG_ON(path_root_bh(left) != path_root_bh(right));
1576 * The caller didn't pass two adjacent paths.
1578 mlog_bug_on_msg(i > left->p_tree_depth,
1579 "Inode %lu, left depth %u, right depth %u\n"
1580 "left leaf blk %llu, right leaf blk %llu\n",
1581 inode->i_ino, left->p_tree_depth,
1582 right->p_tree_depth,
1583 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1584 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1585 } while (left->p_node[i].bh->b_blocknr ==
1586 right->p_node[i].bh->b_blocknr);
1591 typedef void (path_insert_t)(void *, struct buffer_head *);
1594 * Traverse a btree path in search of cpos, starting at root_el.
1596 * This code can be called with a cpos larger than the tree, in which
1597 * case it will return the rightmost path.
1599 static int __ocfs2_find_path(struct inode *inode,
1600 struct ocfs2_extent_list *root_el, u32 cpos,
1601 path_insert_t *func, void *data)
1606 struct buffer_head *bh = NULL;
1607 struct ocfs2_extent_block *eb;
1608 struct ocfs2_extent_list *el;
1609 struct ocfs2_extent_rec *rec;
1610 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1613 while (el->l_tree_depth) {
1614 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1615 ocfs2_error(inode->i_sb,
1616 "Inode %llu has empty extent list at "
1618 (unsigned long long)oi->ip_blkno,
1619 le16_to_cpu(el->l_tree_depth));
1625 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1626 rec = &el->l_recs[i];
1629 * In the case that cpos is off the allocation
1630 * tree, this should just wind up returning the
1633 range = le32_to_cpu(rec->e_cpos) +
1634 ocfs2_rec_clusters(el, rec);
1635 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1639 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1641 ocfs2_error(inode->i_sb,
1642 "Inode %llu has bad blkno in extent list "
1643 "at depth %u (index %d)\n",
1644 (unsigned long long)oi->ip_blkno,
1645 le16_to_cpu(el->l_tree_depth), i);
1652 ret = ocfs2_read_extent_block(inode, blkno, &bh);
1658 eb = (struct ocfs2_extent_block *) bh->b_data;
1661 if (le16_to_cpu(el->l_next_free_rec) >
1662 le16_to_cpu(el->l_count)) {
1663 ocfs2_error(inode->i_sb,
1664 "Inode %llu has bad count in extent list "
1665 "at block %llu (next free=%u, count=%u)\n",
1666 (unsigned long long)oi->ip_blkno,
1667 (unsigned long long)bh->b_blocknr,
1668 le16_to_cpu(el->l_next_free_rec),
1669 le16_to_cpu(el->l_count));
1680 * Catch any trailing bh that the loop didn't handle.
1688 * Given an initialized path (that is, it has a valid root extent
1689 * list), this function will traverse the btree in search of the path
1690 * which would contain cpos.
1692 * The path traveled is recorded in the path structure.
1694 * Note that this will not do any comparisons on leaf node extent
1695 * records, so it will work fine in the case that we just added a tree
1698 struct find_path_data {
1700 struct ocfs2_path *path;
1702 static void find_path_ins(void *data, struct buffer_head *bh)
1704 struct find_path_data *fp = data;
1707 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1710 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1713 struct find_path_data data;
1717 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1718 find_path_ins, &data);
1721 static void find_leaf_ins(void *data, struct buffer_head *bh)
1723 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1724 struct ocfs2_extent_list *el = &eb->h_list;
1725 struct buffer_head **ret = data;
1727 /* We want to retain only the leaf block. */
1728 if (le16_to_cpu(el->l_tree_depth) == 0) {
1734 * Find the leaf block in the tree which would contain cpos. No
1735 * checking of the actual leaf is done.
1737 * Some paths want to call this instead of allocating a path structure
1738 * and calling ocfs2_find_path().
1740 * This function doesn't handle non btree extent lists.
1742 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1743 u32 cpos, struct buffer_head **leaf_bh)
1746 struct buffer_head *bh = NULL;
1748 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1760 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1762 * Basically, we've moved stuff around at the bottom of the tree and
1763 * we need to fix up the extent records above the changes to reflect
1766 * left_rec: the record on the left.
1767 * left_child_el: is the child list pointed to by left_rec
1768 * right_rec: the record to the right of left_rec
1769 * right_child_el: is the child list pointed to by right_rec
1771 * By definition, this only works on interior nodes.
1773 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1774 struct ocfs2_extent_list *left_child_el,
1775 struct ocfs2_extent_rec *right_rec,
1776 struct ocfs2_extent_list *right_child_el)
1778 u32 left_clusters, right_end;
1781 * Interior nodes never have holes. Their cpos is the cpos of
1782 * the leftmost record in their child list. Their cluster
1783 * count covers the full theoretical range of their child list
1784 * - the range between their cpos and the cpos of the record
1785 * immediately to their right.
1787 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1788 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1789 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1790 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1792 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1793 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1796 * Calculate the rightmost cluster count boundary before
1797 * moving cpos - we will need to adjust clusters after
1798 * updating e_cpos to keep the same highest cluster count.
1800 right_end = le32_to_cpu(right_rec->e_cpos);
1801 right_end += le32_to_cpu(right_rec->e_int_clusters);
1803 right_rec->e_cpos = left_rec->e_cpos;
1804 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1806 right_end -= le32_to_cpu(right_rec->e_cpos);
1807 right_rec->e_int_clusters = cpu_to_le32(right_end);
1811 * Adjust the adjacent root node records involved in a
1812 * rotation. left_el_blkno is passed in as a key so that we can easily
1813 * find it's index in the root list.
1815 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1816 struct ocfs2_extent_list *left_el,
1817 struct ocfs2_extent_list *right_el,
1822 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1823 le16_to_cpu(left_el->l_tree_depth));
1825 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1826 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1831 * The path walking code should have never returned a root and
1832 * two paths which are not adjacent.
1834 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1836 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1837 &root_el->l_recs[i + 1], right_el);
1841 * We've changed a leaf block (in right_path) and need to reflect that
1842 * change back up the subtree.
1844 * This happens in multiple places:
1845 * - When we've moved an extent record from the left path leaf to the right
1846 * path leaf to make room for an empty extent in the left path leaf.
1847 * - When our insert into the right path leaf is at the leftmost edge
1848 * and requires an update of the path immediately to it's left. This
1849 * can occur at the end of some types of rotation and appending inserts.
1850 * - When we've adjusted the last extent record in the left path leaf and the
1851 * 1st extent record in the right path leaf during cross extent block merge.
1853 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1854 struct ocfs2_path *left_path,
1855 struct ocfs2_path *right_path,
1859 struct ocfs2_extent_list *el, *left_el, *right_el;
1860 struct ocfs2_extent_rec *left_rec, *right_rec;
1861 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1864 * Update the counts and position values within all the
1865 * interior nodes to reflect the leaf rotation we just did.
1867 * The root node is handled below the loop.
1869 * We begin the loop with right_el and left_el pointing to the
1870 * leaf lists and work our way up.
1872 * NOTE: within this loop, left_el and right_el always refer
1873 * to the *child* lists.
1875 left_el = path_leaf_el(left_path);
1876 right_el = path_leaf_el(right_path);
1877 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1878 mlog(0, "Adjust records at index %u\n", i);
1881 * One nice property of knowing that all of these
1882 * nodes are below the root is that we only deal with
1883 * the leftmost right node record and the rightmost
1886 el = left_path->p_node[i].el;
1887 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1888 left_rec = &el->l_recs[idx];
1890 el = right_path->p_node[i].el;
1891 right_rec = &el->l_recs[0];
1893 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1896 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1900 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1905 * Setup our list pointers now so that the current
1906 * parents become children in the next iteration.
1908 left_el = left_path->p_node[i].el;
1909 right_el = right_path->p_node[i].el;
1913 * At the root node, adjust the two adjacent records which
1914 * begin our path to the leaves.
1917 el = left_path->p_node[subtree_index].el;
1918 left_el = left_path->p_node[subtree_index + 1].el;
1919 right_el = right_path->p_node[subtree_index + 1].el;
1921 ocfs2_adjust_root_records(el, left_el, right_el,
1922 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1924 root_bh = left_path->p_node[subtree_index].bh;
1926 ret = ocfs2_journal_dirty(handle, root_bh);
1931 static int ocfs2_rotate_subtree_right(struct inode *inode,
1933 struct ocfs2_path *left_path,
1934 struct ocfs2_path *right_path,
1938 struct buffer_head *right_leaf_bh;
1939 struct buffer_head *left_leaf_bh = NULL;
1940 struct buffer_head *root_bh;
1941 struct ocfs2_extent_list *right_el, *left_el;
1942 struct ocfs2_extent_rec move_rec;
1944 left_leaf_bh = path_leaf_bh(left_path);
1945 left_el = path_leaf_el(left_path);
1947 if (left_el->l_next_free_rec != left_el->l_count) {
1948 ocfs2_error(inode->i_sb,
1949 "Inode %llu has non-full interior leaf node %llu"
1951 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1952 (unsigned long long)left_leaf_bh->b_blocknr,
1953 le16_to_cpu(left_el->l_next_free_rec));
1958 * This extent block may already have an empty record, so we
1959 * return early if so.
1961 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1964 root_bh = left_path->p_node[subtree_index].bh;
1965 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1967 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
1974 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1975 ret = ocfs2_path_bh_journal_access(handle, inode,
1982 ret = ocfs2_path_bh_journal_access(handle, inode,
1990 right_leaf_bh = path_leaf_bh(right_path);
1991 right_el = path_leaf_el(right_path);
1993 /* This is a code error, not a disk corruption. */
1994 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1995 "because rightmost leaf block %llu is empty\n",
1996 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1997 (unsigned long long)right_leaf_bh->b_blocknr);
1999 ocfs2_create_empty_extent(right_el);
2001 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2007 /* Do the copy now. */
2008 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2009 move_rec = left_el->l_recs[i];
2010 right_el->l_recs[0] = move_rec;
2013 * Clear out the record we just copied and shift everything
2014 * over, leaving an empty extent in the left leaf.
2016 * We temporarily subtract from next_free_rec so that the
2017 * shift will lose the tail record (which is now defunct).
2019 le16_add_cpu(&left_el->l_next_free_rec, -1);
2020 ocfs2_shift_records_right(left_el);
2021 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2022 le16_add_cpu(&left_el->l_next_free_rec, 1);
2024 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2030 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2038 * Given a full path, determine what cpos value would return us a path
2039 * containing the leaf immediately to the left of the current one.
2041 * Will return zero if the path passed in is already the leftmost path.
2043 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2044 struct ocfs2_path *path, u32 *cpos)
2048 struct ocfs2_extent_list *el;
2050 BUG_ON(path->p_tree_depth == 0);
2054 blkno = path_leaf_bh(path)->b_blocknr;
2056 /* Start at the tree node just above the leaf and work our way up. */
2057 i = path->p_tree_depth - 1;
2059 el = path->p_node[i].el;
2062 * Find the extent record just before the one in our
2065 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2066 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2070 * We've determined that the
2071 * path specified is already
2072 * the leftmost one - return a
2078 * The leftmost record points to our
2079 * leaf - we need to travel up the
2085 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2086 *cpos = *cpos + ocfs2_rec_clusters(el,
2087 &el->l_recs[j - 1]);
2094 * If we got here, we never found a valid node where
2095 * the tree indicated one should be.
2098 "Invalid extent tree at extent block %llu\n",
2099 (unsigned long long)blkno);
2104 blkno = path->p_node[i].bh->b_blocknr;
2113 * Extend the transaction by enough credits to complete the rotation,
2114 * and still leave at least the original number of credits allocated
2115 * to this transaction.
2117 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2119 struct ocfs2_path *path)
2121 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2123 if (handle->h_buffer_credits < credits)
2124 return ocfs2_extend_trans(handle, credits);
2130 * Trap the case where we're inserting into the theoretical range past
2131 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2132 * whose cpos is less than ours into the right leaf.
2134 * It's only necessary to look at the rightmost record of the left
2135 * leaf because the logic that calls us should ensure that the
2136 * theoretical ranges in the path components above the leaves are
2139 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2142 struct ocfs2_extent_list *left_el;
2143 struct ocfs2_extent_rec *rec;
2146 left_el = path_leaf_el(left_path);
2147 next_free = le16_to_cpu(left_el->l_next_free_rec);
2148 rec = &left_el->l_recs[next_free - 1];
2150 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2155 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2157 int next_free = le16_to_cpu(el->l_next_free_rec);
2159 struct ocfs2_extent_rec *rec;
2164 rec = &el->l_recs[0];
2165 if (ocfs2_is_empty_extent(rec)) {
2169 rec = &el->l_recs[1];
2172 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2173 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2179 * Rotate all the records in a btree right one record, starting at insert_cpos.
2181 * The path to the rightmost leaf should be passed in.
2183 * The array is assumed to be large enough to hold an entire path (tree depth).
2185 * Upon succesful return from this function:
2187 * - The 'right_path' array will contain a path to the leaf block
2188 * whose range contains e_cpos.
2189 * - That leaf block will have a single empty extent in list index 0.
2190 * - In the case that the rotation requires a post-insert update,
2191 * *ret_left_path will contain a valid path which can be passed to
2192 * ocfs2_insert_path().
2194 static int ocfs2_rotate_tree_right(struct inode *inode,
2196 enum ocfs2_split_type split,
2198 struct ocfs2_path *right_path,
2199 struct ocfs2_path **ret_left_path)
2201 int ret, start, orig_credits = handle->h_buffer_credits;
2203 struct ocfs2_path *left_path = NULL;
2205 *ret_left_path = NULL;
2207 left_path = ocfs2_new_path_from_path(right_path);
2214 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2220 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2223 * What we want to do here is:
2225 * 1) Start with the rightmost path.
2227 * 2) Determine a path to the leaf block directly to the left
2230 * 3) Determine the 'subtree root' - the lowest level tree node
2231 * which contains a path to both leaves.
2233 * 4) Rotate the subtree.
2235 * 5) Find the next subtree by considering the left path to be
2236 * the new right path.
2238 * The check at the top of this while loop also accepts
2239 * insert_cpos == cpos because cpos is only a _theoretical_
2240 * value to get us the left path - insert_cpos might very well
2241 * be filling that hole.
2243 * Stop at a cpos of '0' because we either started at the
2244 * leftmost branch (i.e., a tree with one branch and a
2245 * rotation inside of it), or we've gone as far as we can in
2246 * rotating subtrees.
2248 while (cpos && insert_cpos <= cpos) {
2249 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2252 ret = ocfs2_find_path(inode, left_path, cpos);
2258 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2259 path_leaf_bh(right_path),
2260 "Inode %lu: error during insert of %u "
2261 "(left path cpos %u) results in two identical "
2262 "paths ending at %llu\n",
2263 inode->i_ino, insert_cpos, cpos,
2264 (unsigned long long)
2265 path_leaf_bh(left_path)->b_blocknr);
2267 if (split == SPLIT_NONE &&
2268 ocfs2_rotate_requires_path_adjustment(left_path,
2272 * We've rotated the tree as much as we
2273 * should. The rest is up to
2274 * ocfs2_insert_path() to complete, after the
2275 * record insertion. We indicate this
2276 * situation by returning the left path.
2278 * The reason we don't adjust the records here
2279 * before the record insert is that an error
2280 * later might break the rule where a parent
2281 * record e_cpos will reflect the actual
2282 * e_cpos of the 1st nonempty record of the
2285 *ret_left_path = left_path;
2289 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2291 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2293 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2294 right_path->p_tree_depth);
2296 ret = ocfs2_extend_rotate_transaction(handle, start,
2297 orig_credits, right_path);
2303 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2310 if (split != SPLIT_NONE &&
2311 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2314 * A rotate moves the rightmost left leaf
2315 * record over to the leftmost right leaf
2316 * slot. If we're doing an extent split
2317 * instead of a real insert, then we have to
2318 * check that the extent to be split wasn't
2319 * just moved over. If it was, then we can
2320 * exit here, passing left_path back -
2321 * ocfs2_split_extent() is smart enough to
2322 * search both leaves.
2324 *ret_left_path = left_path;
2329 * There is no need to re-read the next right path
2330 * as we know that it'll be our current left
2331 * path. Optimize by copying values instead.
2333 ocfs2_mv_path(right_path, left_path);
2335 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2344 ocfs2_free_path(left_path);
2350 static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2351 struct ocfs2_path *path)
2354 struct ocfs2_extent_rec *rec;
2355 struct ocfs2_extent_list *el;
2356 struct ocfs2_extent_block *eb;
2359 /* Path should always be rightmost. */
2360 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2361 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2364 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2365 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2366 rec = &el->l_recs[idx];
2367 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2369 for (i = 0; i < path->p_tree_depth; i++) {
2370 el = path->p_node[i].el;
2371 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2372 rec = &el->l_recs[idx];
2374 rec->e_int_clusters = cpu_to_le32(range);
2375 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2377 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2381 static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2382 struct ocfs2_cached_dealloc_ctxt *dealloc,
2383 struct ocfs2_path *path, int unlink_start)
2386 struct ocfs2_extent_block *eb;
2387 struct ocfs2_extent_list *el;
2388 struct buffer_head *bh;
2390 for(i = unlink_start; i < path_num_items(path); i++) {
2391 bh = path->p_node[i].bh;
2393 eb = (struct ocfs2_extent_block *)bh->b_data;
2395 * Not all nodes might have had their final count
2396 * decremented by the caller - handle this here.
2399 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2401 "Inode %llu, attempted to remove extent block "
2402 "%llu with %u records\n",
2403 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2404 (unsigned long long)le64_to_cpu(eb->h_blkno),
2405 le16_to_cpu(el->l_next_free_rec));
2407 ocfs2_journal_dirty(handle, bh);
2408 ocfs2_remove_from_cache(inode, bh);
2412 el->l_next_free_rec = 0;
2413 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2415 ocfs2_journal_dirty(handle, bh);
2417 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2421 ocfs2_remove_from_cache(inode, bh);
2425 static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2426 struct ocfs2_path *left_path,
2427 struct ocfs2_path *right_path,
2429 struct ocfs2_cached_dealloc_ctxt *dealloc)
2432 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2433 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2434 struct ocfs2_extent_list *el;
2435 struct ocfs2_extent_block *eb;
2437 el = path_leaf_el(left_path);
2439 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2441 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2442 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2445 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2447 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2448 le16_add_cpu(&root_el->l_next_free_rec, -1);
2450 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2451 eb->h_next_leaf_blk = 0;
2453 ocfs2_journal_dirty(handle, root_bh);
2454 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2456 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2460 static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2461 struct ocfs2_path *left_path,
2462 struct ocfs2_path *right_path,
2464 struct ocfs2_cached_dealloc_ctxt *dealloc,
2466 struct ocfs2_extent_tree *et)
2468 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2469 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2470 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2471 struct ocfs2_extent_block *eb;
2475 right_leaf_el = path_leaf_el(right_path);
2476 left_leaf_el = path_leaf_el(left_path);
2477 root_bh = left_path->p_node[subtree_index].bh;
2478 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2480 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2483 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2484 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2486 * It's legal for us to proceed if the right leaf is
2487 * the rightmost one and it has an empty extent. There
2488 * are two cases to handle - whether the leaf will be
2489 * empty after removal or not. If the leaf isn't empty
2490 * then just remove the empty extent up front. The
2491 * next block will handle empty leaves by flagging
2494 * Non rightmost leaves will throw -EAGAIN and the
2495 * caller can manually move the subtree and retry.
2498 if (eb->h_next_leaf_blk != 0ULL)
2501 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2502 ret = ocfs2_journal_access_eb(handle, inode,
2503 path_leaf_bh(right_path),
2504 OCFS2_JOURNAL_ACCESS_WRITE);
2510 ocfs2_remove_empty_extent(right_leaf_el);
2512 right_has_empty = 1;
2515 if (eb->h_next_leaf_blk == 0ULL &&
2516 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2518 * We have to update i_last_eb_blk during the meta
2521 ret = ocfs2_et_root_journal_access(handle, inode, et,
2522 OCFS2_JOURNAL_ACCESS_WRITE);
2528 del_right_subtree = 1;
2532 * Getting here with an empty extent in the right path implies
2533 * that it's the rightmost path and will be deleted.
2535 BUG_ON(right_has_empty && !del_right_subtree);
2537 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
2544 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2545 ret = ocfs2_path_bh_journal_access(handle, inode,
2552 ret = ocfs2_path_bh_journal_access(handle, inode,
2560 if (!right_has_empty) {
2562 * Only do this if we're moving a real
2563 * record. Otherwise, the action is delayed until
2564 * after removal of the right path in which case we
2565 * can do a simple shift to remove the empty extent.
2567 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2568 memset(&right_leaf_el->l_recs[0], 0,
2569 sizeof(struct ocfs2_extent_rec));
2571 if (eb->h_next_leaf_blk == 0ULL) {
2573 * Move recs over to get rid of empty extent, decrease
2574 * next_free. This is allowed to remove the last
2575 * extent in our leaf (setting l_next_free_rec to
2576 * zero) - the delete code below won't care.
2578 ocfs2_remove_empty_extent(right_leaf_el);
2581 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2584 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2588 if (del_right_subtree) {
2589 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2590 subtree_index, dealloc);
2591 ocfs2_update_edge_lengths(inode, handle, left_path);
2593 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2594 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2597 * Removal of the extent in the left leaf was skipped
2598 * above so we could delete the right path
2601 if (right_has_empty)
2602 ocfs2_remove_empty_extent(left_leaf_el);
2604 ret = ocfs2_journal_dirty(handle, et_root_bh);
2610 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2618 * Given a full path, determine what cpos value would return us a path
2619 * containing the leaf immediately to the right of the current one.
2621 * Will return zero if the path passed in is already the rightmost path.
2623 * This looks similar, but is subtly different to
2624 * ocfs2_find_cpos_for_left_leaf().
2626 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2627 struct ocfs2_path *path, u32 *cpos)
2631 struct ocfs2_extent_list *el;
2635 if (path->p_tree_depth == 0)
2638 blkno = path_leaf_bh(path)->b_blocknr;
2640 /* Start at the tree node just above the leaf and work our way up. */
2641 i = path->p_tree_depth - 1;
2645 el = path->p_node[i].el;
2648 * Find the extent record just after the one in our
2651 next_free = le16_to_cpu(el->l_next_free_rec);
2652 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2653 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2654 if (j == (next_free - 1)) {
2657 * We've determined that the
2658 * path specified is already
2659 * the rightmost one - return a
2665 * The rightmost record points to our
2666 * leaf - we need to travel up the
2672 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2678 * If we got here, we never found a valid node where
2679 * the tree indicated one should be.
2682 "Invalid extent tree at extent block %llu\n",
2683 (unsigned long long)blkno);
2688 blkno = path->p_node[i].bh->b_blocknr;
2696 static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2698 struct ocfs2_path *path)
2701 struct buffer_head *bh = path_leaf_bh(path);
2702 struct ocfs2_extent_list *el = path_leaf_el(path);
2704 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2707 ret = ocfs2_path_bh_journal_access(handle, inode, path,
2708 path_num_items(path) - 1);
2714 ocfs2_remove_empty_extent(el);
2716 ret = ocfs2_journal_dirty(handle, bh);
2724 static int __ocfs2_rotate_tree_left(struct inode *inode,
2725 handle_t *handle, int orig_credits,
2726 struct ocfs2_path *path,
2727 struct ocfs2_cached_dealloc_ctxt *dealloc,
2728 struct ocfs2_path **empty_extent_path,
2729 struct ocfs2_extent_tree *et)
2731 int ret, subtree_root, deleted;
2733 struct ocfs2_path *left_path = NULL;
2734 struct ocfs2_path *right_path = NULL;
2736 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2738 *empty_extent_path = NULL;
2740 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2747 left_path = ocfs2_new_path_from_path(path);
2754 ocfs2_cp_path(left_path, path);
2756 right_path = ocfs2_new_path_from_path(path);
2763 while (right_cpos) {
2764 ret = ocfs2_find_path(inode, right_path, right_cpos);
2770 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2773 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2775 (unsigned long long)
2776 right_path->p_node[subtree_root].bh->b_blocknr,
2777 right_path->p_tree_depth);
2779 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2780 orig_credits, left_path);
2787 * Caller might still want to make changes to the
2788 * tree root, so re-add it to the journal here.
2790 ret = ocfs2_path_bh_journal_access(handle, inode,
2797 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2798 right_path, subtree_root,
2799 dealloc, &deleted, et);
2800 if (ret == -EAGAIN) {
2802 * The rotation has to temporarily stop due to
2803 * the right subtree having an empty
2804 * extent. Pass it back to the caller for a
2807 *empty_extent_path = right_path;
2817 * The subtree rotate might have removed records on
2818 * the rightmost edge. If so, then rotation is
2824 ocfs2_mv_path(left_path, right_path);
2826 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2835 ocfs2_free_path(right_path);
2836 ocfs2_free_path(left_path);
2841 static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2842 struct ocfs2_path *path,
2843 struct ocfs2_cached_dealloc_ctxt *dealloc,
2844 struct ocfs2_extent_tree *et)
2846 int ret, subtree_index;
2848 struct ocfs2_path *left_path = NULL;
2849 struct ocfs2_extent_block *eb;
2850 struct ocfs2_extent_list *el;
2853 ret = ocfs2_et_sanity_check(inode, et);
2857 * There's two ways we handle this depending on
2858 * whether path is the only existing one.
2860 ret = ocfs2_extend_rotate_transaction(handle, 0,
2861 handle->h_buffer_credits,
2868 ret = ocfs2_journal_access_path(inode, handle, path);
2874 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2882 * We have a path to the left of this one - it needs
2885 left_path = ocfs2_new_path_from_path(path);
2892 ret = ocfs2_find_path(inode, left_path, cpos);
2898 ret = ocfs2_journal_access_path(inode, handle, left_path);
2904 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2906 ocfs2_unlink_subtree(inode, handle, left_path, path,
2907 subtree_index, dealloc);
2908 ocfs2_update_edge_lengths(inode, handle, left_path);
2910 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2911 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2914 * 'path' is also the leftmost path which
2915 * means it must be the only one. This gets
2916 * handled differently because we want to
2917 * revert the inode back to having extents
2920 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2922 el = et->et_root_el;
2923 el->l_tree_depth = 0;
2924 el->l_next_free_rec = 0;
2925 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2927 ocfs2_et_set_last_eb_blk(et, 0);
2930 ocfs2_journal_dirty(handle, path_root_bh(path));
2933 ocfs2_free_path(left_path);
2938 * Left rotation of btree records.
2940 * In many ways, this is (unsurprisingly) the opposite of right
2941 * rotation. We start at some non-rightmost path containing an empty
2942 * extent in the leaf block. The code works its way to the rightmost
2943 * path by rotating records to the left in every subtree.
2945 * This is used by any code which reduces the number of extent records
2946 * in a leaf. After removal, an empty record should be placed in the
2947 * leftmost list position.
2949 * This won't handle a length update of the rightmost path records if
2950 * the rightmost tree leaf record is removed so the caller is
2951 * responsible for detecting and correcting that.
2953 static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2954 struct ocfs2_path *path,
2955 struct ocfs2_cached_dealloc_ctxt *dealloc,
2956 struct ocfs2_extent_tree *et)
2958 int ret, orig_credits = handle->h_buffer_credits;
2959 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2960 struct ocfs2_extent_block *eb;
2961 struct ocfs2_extent_list *el;
2963 el = path_leaf_el(path);
2964 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2967 if (path->p_tree_depth == 0) {
2968 rightmost_no_delete:
2970 * Inline extents. This is trivially handled, so do
2973 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2981 * Handle rightmost branch now. There's several cases:
2982 * 1) simple rotation leaving records in there. That's trivial.
2983 * 2) rotation requiring a branch delete - there's no more
2984 * records left. Two cases of this:
2985 * a) There are branches to the left.
2986 * b) This is also the leftmost (the only) branch.
2988 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2989 * 2a) we need the left branch so that we can update it with the unlink
2990 * 2b) we need to bring the inode back to inline extents.
2993 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2995 if (eb->h_next_leaf_blk == 0) {
2997 * This gets a bit tricky if we're going to delete the
2998 * rightmost path. Get the other cases out of the way
3001 if (le16_to_cpu(el->l_next_free_rec) > 1)
3002 goto rightmost_no_delete;
3004 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3006 ocfs2_error(inode->i_sb,
3007 "Inode %llu has empty extent block at %llu",
3008 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3009 (unsigned long long)le64_to_cpu(eb->h_blkno));
3014 * XXX: The caller can not trust "path" any more after
3015 * this as it will have been deleted. What do we do?
3017 * In theory the rotate-for-merge code will never get
3018 * here because it'll always ask for a rotate in a
3022 ret = ocfs2_remove_rightmost_path(inode, handle, path,
3030 * Now we can loop, remembering the path we get from -EAGAIN
3031 * and restarting from there.
3034 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
3035 dealloc, &restart_path, et);
3036 if (ret && ret != -EAGAIN) {
3041 while (ret == -EAGAIN) {
3042 tmp_path = restart_path;
3043 restart_path = NULL;
3045 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
3048 if (ret && ret != -EAGAIN) {
3053 ocfs2_free_path(tmp_path);
3061 ocfs2_free_path(tmp_path);
3062 ocfs2_free_path(restart_path);
3066 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3069 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3072 if (rec->e_leaf_clusters == 0) {
3074 * We consumed all of the merged-from record. An empty
3075 * extent cannot exist anywhere but the 1st array
3076 * position, so move things over if the merged-from
3077 * record doesn't occupy that position.
3079 * This creates a new empty extent so the caller
3080 * should be smart enough to have removed any existing
3084 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3085 size = index * sizeof(struct ocfs2_extent_rec);
3086 memmove(&el->l_recs[1], &el->l_recs[0], size);
3090 * Always memset - the caller doesn't check whether it
3091 * created an empty extent, so there could be junk in
3094 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3098 static int ocfs2_get_right_path(struct inode *inode,
3099 struct ocfs2_path *left_path,
3100 struct ocfs2_path **ret_right_path)
3104 struct ocfs2_path *right_path = NULL;
3105 struct ocfs2_extent_list *left_el;
3107 *ret_right_path = NULL;
3109 /* This function shouldn't be called for non-trees. */
3110 BUG_ON(left_path->p_tree_depth == 0);
3112 left_el = path_leaf_el(left_path);
3113 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3115 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3122 /* This function shouldn't be called for the rightmost leaf. */
3123 BUG_ON(right_cpos == 0);
3125 right_path = ocfs2_new_path_from_path(left_path);
3132 ret = ocfs2_find_path(inode, right_path, right_cpos);
3138 *ret_right_path = right_path;
3141 ocfs2_free_path(right_path);
3146 * Remove split_rec clusters from the record at index and merge them
3147 * onto the beginning of the record "next" to it.
3148 * For index < l_count - 1, the next means the extent rec at index + 1.
3149 * For index == l_count - 1, the "next" means the 1st extent rec of the
3150 * next extent block.
3152 static int ocfs2_merge_rec_right(struct inode *inode,
3153 struct ocfs2_path *left_path,
3155 struct ocfs2_extent_rec *split_rec,
3158 int ret, next_free, i;
3159 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3160 struct ocfs2_extent_rec *left_rec;
3161 struct ocfs2_extent_rec *right_rec;
3162 struct ocfs2_extent_list *right_el;
3163 struct ocfs2_path *right_path = NULL;
3164 int subtree_index = 0;
3165 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3166 struct buffer_head *bh = path_leaf_bh(left_path);
3167 struct buffer_head *root_bh = NULL;
3169 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3170 left_rec = &el->l_recs[index];
3172 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3173 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3174 /* we meet with a cross extent block merge. */
3175 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3181 right_el = path_leaf_el(right_path);
3182 next_free = le16_to_cpu(right_el->l_next_free_rec);
3183 BUG_ON(next_free <= 0);
3184 right_rec = &right_el->l_recs[0];
3185 if (ocfs2_is_empty_extent(right_rec)) {
3186 BUG_ON(next_free <= 1);
3187 right_rec = &right_el->l_recs[1];
3190 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3191 le16_to_cpu(left_rec->e_leaf_clusters) !=
3192 le32_to_cpu(right_rec->e_cpos));
3194 subtree_index = ocfs2_find_subtree_root(inode,
3195 left_path, right_path);
3197 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3198 handle->h_buffer_credits,
3205 root_bh = left_path->p_node[subtree_index].bh;
3206 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3208 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
3215 for (i = subtree_index + 1;
3216 i < path_num_items(right_path); i++) {
3217 ret = ocfs2_path_bh_journal_access(handle, inode,
3224 ret = ocfs2_path_bh_journal_access(handle, inode,
3233 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3234 right_rec = &el->l_recs[index + 1];
3237 ret = ocfs2_path_bh_journal_access(handle, inode, left_path,
3238 path_num_items(left_path) - 1);
3244 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3246 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3247 le64_add_cpu(&right_rec->e_blkno,
3248 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3249 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3251 ocfs2_cleanup_merge(el, index);
3253 ret = ocfs2_journal_dirty(handle, bh);
3258 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3262 ocfs2_complete_edge_insert(inode, handle, left_path,
3263 right_path, subtree_index);
3267 ocfs2_free_path(right_path);
3271 static int ocfs2_get_left_path(struct inode *inode,
3272 struct ocfs2_path *right_path,
3273 struct ocfs2_path **ret_left_path)
3277 struct ocfs2_path *left_path = NULL;
3279 *ret_left_path = NULL;
3281 /* This function shouldn't be called for non-trees. */
3282 BUG_ON(right_path->p_tree_depth == 0);
3284 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3285 right_path, &left_cpos);
3291 /* This function shouldn't be called for the leftmost leaf. */
3292 BUG_ON(left_cpos == 0);
3294 left_path = ocfs2_new_path_from_path(right_path);
3301 ret = ocfs2_find_path(inode, left_path, left_cpos);
3307 *ret_left_path = left_path;
3310 ocfs2_free_path(left_path);
3315 * Remove split_rec clusters from the record at index and merge them
3316 * onto the tail of the record "before" it.
3317 * For index > 0, the "before" means the extent rec at index - 1.
3319 * For index == 0, the "before" means the last record of the previous
3320 * extent block. And there is also a situation that we may need to
3321 * remove the rightmost leaf extent block in the right_path and change
3322 * the right path to indicate the new rightmost path.
3324 static int ocfs2_merge_rec_left(struct inode *inode,
3325 struct ocfs2_path *right_path,
3327 struct ocfs2_extent_rec *split_rec,
3328 struct ocfs2_cached_dealloc_ctxt *dealloc,
3329 struct ocfs2_extent_tree *et,
3332 int ret, i, subtree_index = 0, has_empty_extent = 0;
3333 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3334 struct ocfs2_extent_rec *left_rec;
3335 struct ocfs2_extent_rec *right_rec;
3336 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3337 struct buffer_head *bh = path_leaf_bh(right_path);
3338 struct buffer_head *root_bh = NULL;
3339 struct ocfs2_path *left_path = NULL;
3340 struct ocfs2_extent_list *left_el;
3344 right_rec = &el->l_recs[index];
3346 /* we meet with a cross extent block merge. */
3347 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3353 left_el = path_leaf_el(left_path);
3354 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3355 le16_to_cpu(left_el->l_count));
3357 left_rec = &left_el->l_recs[
3358 le16_to_cpu(left_el->l_next_free_rec) - 1];
3359 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3360 le16_to_cpu(left_rec->e_leaf_clusters) !=
3361 le32_to_cpu(split_rec->e_cpos));
3363 subtree_index = ocfs2_find_subtree_root(inode,
3364 left_path, right_path);
3366 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3367 handle->h_buffer_credits,
3374 root_bh = left_path->p_node[subtree_index].bh;
3375 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3377 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
3384 for (i = subtree_index + 1;
3385 i < path_num_items(right_path); i++) {
3386 ret = ocfs2_path_bh_journal_access(handle, inode,
3393 ret = ocfs2_path_bh_journal_access(handle, inode,
3401 left_rec = &el->l_recs[index - 1];
3402 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3403 has_empty_extent = 1;
3406 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
3407 path_num_items(right_path) - 1);
3413 if (has_empty_extent && index == 1) {
3415 * The easy case - we can just plop the record right in.
3417 *left_rec = *split_rec;
3419 has_empty_extent = 0;
3421 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3423 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3424 le64_add_cpu(&right_rec->e_blkno,
3425 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3426 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3428 ocfs2_cleanup_merge(el, index);
3430 ret = ocfs2_journal_dirty(handle, bh);
3435 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3440 * In the situation that the right_rec is empty and the extent
3441 * block is empty also, ocfs2_complete_edge_insert can't handle
3442 * it and we need to delete the right extent block.
3444 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3445 le16_to_cpu(el->l_next_free_rec) == 1) {
3447 ret = ocfs2_remove_rightmost_path(inode, handle,
3455 /* Now the rightmost extent block has been deleted.
3456 * So we use the new rightmost path.
3458 ocfs2_mv_path(right_path, left_path);
3461 ocfs2_complete_edge_insert(inode, handle, left_path,
3462 right_path, subtree_index);
3466 ocfs2_free_path(left_path);
3470 static int ocfs2_try_to_merge_extent(struct inode *inode,
3472 struct ocfs2_path *path,
3474 struct ocfs2_extent_rec *split_rec,
3475 struct ocfs2_cached_dealloc_ctxt *dealloc,
3476 struct ocfs2_merge_ctxt *ctxt,
3477 struct ocfs2_extent_tree *et)
3481 struct ocfs2_extent_list *el = path_leaf_el(path);
3482 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3484 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3486 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3488 * The merge code will need to create an empty
3489 * extent to take the place of the newly
3490 * emptied slot. Remove any pre-existing empty
3491 * extents - having more than one in a leaf is
3494 ret = ocfs2_rotate_tree_left(inode, handle, path,
3501 rec = &el->l_recs[split_index];
3504 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3506 * Left-right contig implies this.
3508 BUG_ON(!ctxt->c_split_covers_rec);
3511 * Since the leftright insert always covers the entire
3512 * extent, this call will delete the insert record
3513 * entirely, resulting in an empty extent record added to
3516 * Since the adding of an empty extent shifts
3517 * everything back to the right, there's no need to
3518 * update split_index here.
3520 * When the split_index is zero, we need to merge it to the
3521 * prevoius extent block. It is more efficient and easier
3522 * if we do merge_right first and merge_left later.
3524 ret = ocfs2_merge_rec_right(inode, path,
3533 * We can only get this from logic error above.
3535 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3537 /* The merge left us with an empty extent, remove it. */
3538 ret = ocfs2_rotate_tree_left(inode, handle, path,
3545 rec = &el->l_recs[split_index];
3548 * Note that we don't pass split_rec here on purpose -
3549 * we've merged it into the rec already.
3551 ret = ocfs2_merge_rec_left(inode, path,
3561 ret = ocfs2_rotate_tree_left(inode, handle, path,
3564 * Error from this last rotate is not critical, so
3565 * print but don't bubble it up.
3572 * Merge a record to the left or right.
3574 * 'contig_type' is relative to the existing record,
3575 * so for example, if we're "right contig", it's to
3576 * the record on the left (hence the left merge).
3578 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3579 ret = ocfs2_merge_rec_left(inode,
3589 ret = ocfs2_merge_rec_right(inode,
3599 if (ctxt->c_split_covers_rec) {
3601 * The merge may have left an empty extent in
3602 * our leaf. Try to rotate it away.
3604 ret = ocfs2_rotate_tree_left(inode, handle, path,
3616 static void ocfs2_subtract_from_rec(struct super_block *sb,
3617 enum ocfs2_split_type split,
3618 struct ocfs2_extent_rec *rec,
3619 struct ocfs2_extent_rec *split_rec)
3623 len_blocks = ocfs2_clusters_to_blocks(sb,
3624 le16_to_cpu(split_rec->e_leaf_clusters));
3626 if (split == SPLIT_LEFT) {
3628 * Region is on the left edge of the existing
3631 le32_add_cpu(&rec->e_cpos,
3632 le16_to_cpu(split_rec->e_leaf_clusters));
3633 le64_add_cpu(&rec->e_blkno, len_blocks);
3634 le16_add_cpu(&rec->e_leaf_clusters,
3635 -le16_to_cpu(split_rec->e_leaf_clusters));
3638 * Region is on the right edge of the existing
3641 le16_add_cpu(&rec->e_leaf_clusters,
3642 -le16_to_cpu(split_rec->e_leaf_clusters));
3647 * Do the final bits of extent record insertion at the target leaf
3648 * list. If this leaf is part of an allocation tree, it is assumed
3649 * that the tree above has been prepared.
3651 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3652 struct ocfs2_extent_list *el,
3653 struct ocfs2_insert_type *insert,
3654 struct inode *inode)
3656 int i = insert->ins_contig_index;
3658 struct ocfs2_extent_rec *rec;
3660 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3662 if (insert->ins_split != SPLIT_NONE) {
3663 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3665 rec = &el->l_recs[i];
3666 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3672 * Contiguous insert - either left or right.
3674 if (insert->ins_contig != CONTIG_NONE) {
3675 rec = &el->l_recs[i];
3676 if (insert->ins_contig == CONTIG_LEFT) {
3677 rec->e_blkno = insert_rec->e_blkno;
3678 rec->e_cpos = insert_rec->e_cpos;
3680 le16_add_cpu(&rec->e_leaf_clusters,
3681 le16_to_cpu(insert_rec->e_leaf_clusters));
3686 * Handle insert into an empty leaf.
3688 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3689 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3690 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3691 el->l_recs[0] = *insert_rec;
3692 el->l_next_free_rec = cpu_to_le16(1);
3699 if (insert->ins_appending == APPEND_TAIL) {
3700 i = le16_to_cpu(el->l_next_free_rec) - 1;
3701 rec = &el->l_recs[i];
3702 range = le32_to_cpu(rec->e_cpos)
3703 + le16_to_cpu(rec->e_leaf_clusters);
3704 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3706 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3707 le16_to_cpu(el->l_count),
3708 "inode %lu, depth %u, count %u, next free %u, "
3709 "rec.cpos %u, rec.clusters %u, "
3710 "insert.cpos %u, insert.clusters %u\n",
3712 le16_to_cpu(el->l_tree_depth),
3713 le16_to_cpu(el->l_count),
3714 le16_to_cpu(el->l_next_free_rec),
3715 le32_to_cpu(el->l_recs[i].e_cpos),
3716 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3717 le32_to_cpu(insert_rec->e_cpos),
3718 le16_to_cpu(insert_rec->e_leaf_clusters));
3720 el->l_recs[i] = *insert_rec;
3721 le16_add_cpu(&el->l_next_free_rec, 1);
3727 * Ok, we have to rotate.
3729 * At this point, it is safe to assume that inserting into an
3730 * empty leaf and appending to a leaf have both been handled
3733 * This leaf needs to have space, either by the empty 1st
3734 * extent record, or by virtue of an l_next_rec < l_count.
3736 ocfs2_rotate_leaf(el, insert_rec);
3739 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3741 struct ocfs2_path *path,
3742 struct ocfs2_extent_rec *insert_rec)
3744 int ret, i, next_free;
3745 struct buffer_head *bh;
3746 struct ocfs2_extent_list *el;
3747 struct ocfs2_extent_rec *rec;
3750 * Update everything except the leaf block.
3752 for (i = 0; i < path->p_tree_depth; i++) {
3753 bh = path->p_node[i].bh;
3754 el = path->p_node[i].el;
3756 next_free = le16_to_cpu(el->l_next_free_rec);
3757 if (next_free == 0) {
3758 ocfs2_error(inode->i_sb,
3759 "Dinode %llu has a bad extent list",
3760 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3765 rec = &el->l_recs[next_free - 1];
3767 rec->e_int_clusters = insert_rec->e_cpos;
3768 le32_add_cpu(&rec->e_int_clusters,
3769 le16_to_cpu(insert_rec->e_leaf_clusters));
3770 le32_add_cpu(&rec->e_int_clusters,
3771 -le32_to_cpu(rec->e_cpos));
3773 ret = ocfs2_journal_dirty(handle, bh);
3780 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3781 struct ocfs2_extent_rec *insert_rec,
3782 struct ocfs2_path *right_path,
3783 struct ocfs2_path **ret_left_path)
3786 struct ocfs2_extent_list *el;
3787 struct ocfs2_path *left_path = NULL;
3789 *ret_left_path = NULL;
3792 * This shouldn't happen for non-trees. The extent rec cluster
3793 * count manipulation below only works for interior nodes.
3795 BUG_ON(right_path->p_tree_depth == 0);
3798 * If our appending insert is at the leftmost edge of a leaf,
3799 * then we might need to update the rightmost records of the
3802 el = path_leaf_el(right_path);
3803 next_free = le16_to_cpu(el->l_next_free_rec);
3804 if (next_free == 0 ||
3805 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3808 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3815 mlog(0, "Append may need a left path update. cpos: %u, "
3816 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3820 * No need to worry if the append is already in the
3824 left_path = ocfs2_new_path_from_path(right_path);
3831 ret = ocfs2_find_path(inode, left_path, left_cpos);
3838 * ocfs2_insert_path() will pass the left_path to the
3844 ret = ocfs2_journal_access_path(inode, handle, right_path);
3850 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
3852 *ret_left_path = left_path;
3856 ocfs2_free_path(left_path);
3861 static void ocfs2_split_record(struct inode *inode,
3862 struct ocfs2_path *left_path,
3863 struct ocfs2_path *right_path,
3864 struct ocfs2_extent_rec *split_rec,
3865 enum ocfs2_split_type split)
3868 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3869 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3870 struct ocfs2_extent_rec *rec, *tmprec;
3872 right_el = path_leaf_el(right_path);
3874 left_el = path_leaf_el(left_path);
3877 insert_el = right_el;
3878 index = ocfs2_search_extent_list(el, cpos);
3880 if (index == 0 && left_path) {
3881 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3884 * This typically means that the record
3885 * started in the left path but moved to the
3886 * right as a result of rotation. We either
3887 * move the existing record to the left, or we
3888 * do the later insert there.
3890 * In this case, the left path should always
3891 * exist as the rotate code will have passed
3892 * it back for a post-insert update.
3895 if (split == SPLIT_LEFT) {
3897 * It's a left split. Since we know
3898 * that the rotate code gave us an
3899 * empty extent in the left path, we
3900 * can just do the insert there.
3902 insert_el = left_el;
3905 * Right split - we have to move the
3906 * existing record over to the left
3907 * leaf. The insert will be into the
3908 * newly created empty extent in the
3911 tmprec = &right_el->l_recs[index];
3912 ocfs2_rotate_leaf(left_el, tmprec);
3915 memset(tmprec, 0, sizeof(*tmprec));
3916 index = ocfs2_search_extent_list(left_el, cpos);
3917 BUG_ON(index == -1);
3922 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3924 * Left path is easy - we can just allow the insert to
3928 insert_el = left_el;
3929 index = ocfs2_search_extent_list(el, cpos);
3930 BUG_ON(index == -1);
3933 rec = &el->l_recs[index];
3934 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3935 ocfs2_rotate_leaf(insert_el, split_rec);
3939 * This function only does inserts on an allocation b-tree. For tree
3940 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3942 * right_path is the path we want to do the actual insert
3943 * in. left_path should only be passed in if we need to update that
3944 * portion of the tree after an edge insert.
3946 static int ocfs2_insert_path(struct inode *inode,
3948 struct ocfs2_path *left_path,
3949 struct ocfs2_path *right_path,
3950 struct ocfs2_extent_rec *insert_rec,
3951 struct ocfs2_insert_type *insert)
3953 int ret, subtree_index;
3954 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
3957 int credits = handle->h_buffer_credits;
3960 * There's a chance that left_path got passed back to
3961 * us without being accounted for in the
3962 * journal. Extend our transaction here to be sure we
3963 * can change those blocks.
3965 credits += left_path->p_tree_depth;
3967 ret = ocfs2_extend_trans(handle, credits);
3973 ret = ocfs2_journal_access_path(inode, handle, left_path);
3981 * Pass both paths to the journal. The majority of inserts
3982 * will be touching all components anyway.
3984 ret = ocfs2_journal_access_path(inode, handle, right_path);
3990 if (insert->ins_split != SPLIT_NONE) {
3992 * We could call ocfs2_insert_at_leaf() for some types
3993 * of splits, but it's easier to just let one separate
3994 * function sort it all out.
3996 ocfs2_split_record(inode, left_path, right_path,
3997 insert_rec, insert->ins_split);
4000 * Split might have modified either leaf and we don't
4001 * have a guarantee that the later edge insert will
4002 * dirty this for us.
4005 ret = ocfs2_journal_dirty(handle,
4006 path_leaf_bh(left_path));
4010 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
4013 ret = ocfs2_journal_dirty(handle, leaf_bh);
4019 * The rotate code has indicated that we need to fix
4020 * up portions of the tree after the insert.
4022 * XXX: Should we extend the transaction here?
4024 subtree_index = ocfs2_find_subtree_root(inode, left_path,
4026 ocfs2_complete_edge_insert(inode, handle, left_path,
4027 right_path, subtree_index);
4035 static int ocfs2_do_insert_extent(struct inode *inode,
4037 struct ocfs2_extent_tree *et,
4038 struct ocfs2_extent_rec *insert_rec,
4039 struct ocfs2_insert_type *type)
4041 int ret, rotate = 0;
4043 struct ocfs2_path *right_path = NULL;
4044 struct ocfs2_path *left_path = NULL;
4045 struct ocfs2_extent_list *el;
4047 el = et->et_root_el;
4049 ret = ocfs2_et_root_journal_access(handle, inode, et,
4050 OCFS2_JOURNAL_ACCESS_WRITE);
4056 if (le16_to_cpu(el->l_tree_depth) == 0) {
4057 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4058 goto out_update_clusters;
4061 right_path = ocfs2_new_path_from_et(et);
4069 * Determine the path to start with. Rotations need the
4070 * rightmost path, everything else can go directly to the
4073 cpos = le32_to_cpu(insert_rec->e_cpos);
4074 if (type->ins_appending == APPEND_NONE &&
4075 type->ins_contig == CONTIG_NONE) {
4080 ret = ocfs2_find_path(inode, right_path, cpos);
4087 * Rotations and appends need special treatment - they modify
4088 * parts of the tree's above them.
4090 * Both might pass back a path immediate to the left of the
4091 * one being inserted to. This will be cause
4092 * ocfs2_insert_path() to modify the rightmost records of
4093 * left_path to account for an edge insert.
4095 * XXX: When modifying this code, keep in mind that an insert
4096 * can wind up skipping both of these two special cases...
4099 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
4100 le32_to_cpu(insert_rec->e_cpos),
4101 right_path, &left_path);
4108 * ocfs2_rotate_tree_right() might have extended the
4109 * transaction without re-journaling our tree root.
4111 ret = ocfs2_et_root_journal_access(handle, inode, et,
4112 OCFS2_JOURNAL_ACCESS_WRITE);
4117 } else if (type->ins_appending == APPEND_TAIL
4118 && type->ins_contig != CONTIG_LEFT) {
4119 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4120 right_path, &left_path);
4127 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4134 out_update_clusters:
4135 if (type->ins_split == SPLIT_NONE)
4136 ocfs2_et_update_clusters(inode, et,
4137 le16_to_cpu(insert_rec->e_leaf_clusters));
4139 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
4144 ocfs2_free_path(left_path);
4145 ocfs2_free_path(right_path);
4150 static enum ocfs2_contig_type
4151 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
4152 struct ocfs2_extent_list *el, int index,
4153 struct ocfs2_extent_rec *split_rec)
4156 enum ocfs2_contig_type ret = CONTIG_NONE;
4157 u32 left_cpos, right_cpos;
4158 struct ocfs2_extent_rec *rec = NULL;
4159 struct ocfs2_extent_list *new_el;
4160 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4161 struct buffer_head *bh;
4162 struct ocfs2_extent_block *eb;
4165 rec = &el->l_recs[index - 1];
4166 } else if (path->p_tree_depth > 0) {
4167 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4172 if (left_cpos != 0) {
4173 left_path = ocfs2_new_path_from_path(path);
4177 status = ocfs2_find_path(inode, left_path, left_cpos);
4181 new_el = path_leaf_el(left_path);
4183 if (le16_to_cpu(new_el->l_next_free_rec) !=
4184 le16_to_cpu(new_el->l_count)) {
4185 bh = path_leaf_bh(left_path);
4186 eb = (struct ocfs2_extent_block *)bh->b_data;
4187 ocfs2_error(inode->i_sb,
4188 "Extent block #%llu has an "
4189 "invalid l_next_free_rec of "
4190 "%d. It should have "
4191 "matched the l_count of %d",
4192 (unsigned long long)le64_to_cpu(eb->h_blkno),
4193 le16_to_cpu(new_el->l_next_free_rec),
4194 le16_to_cpu(new_el->l_count));
4198 rec = &new_el->l_recs[
4199 le16_to_cpu(new_el->l_next_free_rec) - 1];
4204 * We're careful to check for an empty extent record here -
4205 * the merge code will know what to do if it sees one.
4208 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4209 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4212 ret = ocfs2_extent_contig(inode, rec, split_rec);
4217 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4218 rec = &el->l_recs[index + 1];
4219 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4220 path->p_tree_depth > 0) {
4221 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4226 if (right_cpos == 0)
4229 right_path = ocfs2_new_path_from_path(path);
4233 status = ocfs2_find_path(inode, right_path, right_cpos);
4237 new_el = path_leaf_el(right_path);
4238 rec = &new_el->l_recs[0];
4239 if (ocfs2_is_empty_extent(rec)) {
4240 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4241 bh = path_leaf_bh(right_path);
4242 eb = (struct ocfs2_extent_block *)bh->b_data;
4243 ocfs2_error(inode->i_sb,
4244 "Extent block #%llu has an "
4245 "invalid l_next_free_rec of %d",
4246 (unsigned long long)le64_to_cpu(eb->h_blkno),
4247 le16_to_cpu(new_el->l_next_free_rec));
4251 rec = &new_el->l_recs[1];
4256 enum ocfs2_contig_type contig_type;
4258 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4260 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4261 ret = CONTIG_LEFTRIGHT;
4262 else if (ret == CONTIG_NONE)
4268 ocfs2_free_path(left_path);
4270 ocfs2_free_path(right_path);
4275 static void ocfs2_figure_contig_type(struct inode *inode,
4276 struct ocfs2_insert_type *insert,
4277 struct ocfs2_extent_list *el,
4278 struct ocfs2_extent_rec *insert_rec,
4279 struct ocfs2_extent_tree *et)
4282 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4284 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4286 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4287 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4289 if (contig_type != CONTIG_NONE) {
4290 insert->ins_contig_index = i;
4294 insert->ins_contig = contig_type;
4296 if (insert->ins_contig != CONTIG_NONE) {
4297 struct ocfs2_extent_rec *rec =
4298 &el->l_recs[insert->ins_contig_index];
4299 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4300 le16_to_cpu(insert_rec->e_leaf_clusters);
4303 * Caller might want us to limit the size of extents, don't
4304 * calculate contiguousness if we might exceed that limit.
4306 if (et->et_max_leaf_clusters &&
4307 (len > et->et_max_leaf_clusters))
4308 insert->ins_contig = CONTIG_NONE;
4313 * This should only be called against the righmost leaf extent list.
4315 * ocfs2_figure_appending_type() will figure out whether we'll have to
4316 * insert at the tail of the rightmost leaf.
4318 * This should also work against the root extent list for tree's with 0
4319 * depth. If we consider the root extent list to be the rightmost leaf node
4320 * then the logic here makes sense.
4322 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4323 struct ocfs2_extent_list *el,
4324 struct ocfs2_extent_rec *insert_rec)
4327 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4328 struct ocfs2_extent_rec *rec;
4330 insert->ins_appending = APPEND_NONE;
4332 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4334 if (!el->l_next_free_rec)
4335 goto set_tail_append;
4337 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4338 /* Were all records empty? */
4339 if (le16_to_cpu(el->l_next_free_rec) == 1)
4340 goto set_tail_append;
4343 i = le16_to_cpu(el->l_next_free_rec) - 1;
4344 rec = &el->l_recs[i];
4347 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4348 goto set_tail_append;
4353 insert->ins_appending = APPEND_TAIL;
4357 * Helper function called at the begining of an insert.
4359 * This computes a few things that are commonly used in the process of
4360 * inserting into the btree:
4361 * - Whether the new extent is contiguous with an existing one.
4362 * - The current tree depth.
4363 * - Whether the insert is an appending one.
4364 * - The total # of free records in the tree.
4366 * All of the information is stored on the ocfs2_insert_type
4369 static int ocfs2_figure_insert_type(struct inode *inode,
4370 struct ocfs2_extent_tree *et,
4371 struct buffer_head **last_eb_bh,
4372 struct ocfs2_extent_rec *insert_rec,
4374 struct ocfs2_insert_type *insert)
4377 struct ocfs2_extent_block *eb;
4378 struct ocfs2_extent_list *el;
4379 struct ocfs2_path *path = NULL;
4380 struct buffer_head *bh = NULL;
4382 insert->ins_split = SPLIT_NONE;
4384 el = et->et_root_el;
4385 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4387 if (el->l_tree_depth) {
4389 * If we have tree depth, we read in the
4390 * rightmost extent block ahead of time as
4391 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4392 * may want it later.
4394 ret = ocfs2_read_extent_block(inode,
4395 ocfs2_et_get_last_eb_blk(et),
4401 eb = (struct ocfs2_extent_block *) bh->b_data;
4406 * Unless we have a contiguous insert, we'll need to know if
4407 * there is room left in our allocation tree for another
4410 * XXX: This test is simplistic, we can search for empty
4411 * extent records too.
4413 *free_records = le16_to_cpu(el->l_count) -
4414 le16_to_cpu(el->l_next_free_rec);
4416 if (!insert->ins_tree_depth) {
4417 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4418 ocfs2_figure_appending_type(insert, el, insert_rec);
4422 path = ocfs2_new_path_from_et(et);
4430 * In the case that we're inserting past what the tree
4431 * currently accounts for, ocfs2_find_path() will return for
4432 * us the rightmost tree path. This is accounted for below in
4433 * the appending code.
4435 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4441 el = path_leaf_el(path);
4444 * Now that we have the path, there's two things we want to determine:
4445 * 1) Contiguousness (also set contig_index if this is so)
4447 * 2) Are we doing an append? We can trivially break this up
4448 * into two types of appends: simple record append, or a
4449 * rotate inside the tail leaf.
4451 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4454 * The insert code isn't quite ready to deal with all cases of
4455 * left contiguousness. Specifically, if it's an insert into
4456 * the 1st record in a leaf, it will require the adjustment of
4457 * cluster count on the last record of the path directly to it's
4458 * left. For now, just catch that case and fool the layers
4459 * above us. This works just fine for tree_depth == 0, which
4460 * is why we allow that above.
4462 if (insert->ins_contig == CONTIG_LEFT &&
4463 insert->ins_contig_index == 0)
4464 insert->ins_contig = CONTIG_NONE;
4467 * Ok, so we can simply compare against last_eb to figure out
4468 * whether the path doesn't exist. This will only happen in
4469 * the case that we're doing a tail append, so maybe we can
4470 * take advantage of that information somehow.
4472 if (ocfs2_et_get_last_eb_blk(et) ==
4473 path_leaf_bh(path)->b_blocknr) {
4475 * Ok, ocfs2_find_path() returned us the rightmost
4476 * tree path. This might be an appending insert. There are
4478 * 1) We're doing a true append at the tail:
4479 * -This might even be off the end of the leaf
4480 * 2) We're "appending" by rotating in the tail
4482 ocfs2_figure_appending_type(insert, el, insert_rec);
4486 ocfs2_free_path(path);
4496 * Insert an extent into an inode btree.
4498 * The caller needs to update fe->i_clusters
4500 int ocfs2_insert_extent(struct ocfs2_super *osb,
4502 struct inode *inode,
4503 struct ocfs2_extent_tree *et,
4508 struct ocfs2_alloc_context *meta_ac)
4511 int uninitialized_var(free_records);
4512 struct buffer_head *last_eb_bh = NULL;
4513 struct ocfs2_insert_type insert = {0, };
4514 struct ocfs2_extent_rec rec;
4516 mlog(0, "add %u clusters at position %u to inode %llu\n",
4517 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4519 memset(&rec, 0, sizeof(rec));
4520 rec.e_cpos = cpu_to_le32(cpos);
4521 rec.e_blkno = cpu_to_le64(start_blk);
4522 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4523 rec.e_flags = flags;
4524 status = ocfs2_et_insert_check(inode, et, &rec);
4530 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4531 &free_records, &insert);
4537 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4538 "Insert.contig_index: %d, Insert.free_records: %d, "
4539 "Insert.tree_depth: %d\n",
4540 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4541 free_records, insert.ins_tree_depth);
4543 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4544 status = ocfs2_grow_tree(inode, handle, et,
4545 &insert.ins_tree_depth, &last_eb_bh,
4553 /* Finally, we can add clusters. This might rotate the tree for us. */
4554 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4557 else if (et->et_ops == &ocfs2_dinode_et_ops)
4558 ocfs2_extent_map_insert_rec(inode, &rec);
4568 * Allcate and add clusters into the extent b-tree.
4569 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4570 * The extent b-tree's root is specified by et, and
4571 * it is not limited to the file storage. Any extent tree can use this
4572 * function if it implements the proper ocfs2_extent_tree.
4574 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4575 struct inode *inode,
4576 u32 *logical_offset,
4577 u32 clusters_to_add,
4579 struct ocfs2_extent_tree *et,
4581 struct ocfs2_alloc_context *data_ac,
4582 struct ocfs2_alloc_context *meta_ac,
4583 enum ocfs2_alloc_restarted *reason_ret)
4587 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4588 u32 bit_off, num_bits;
4592 BUG_ON(!clusters_to_add);
4595 flags = OCFS2_EXT_UNWRITTEN;
4597 free_extents = ocfs2_num_free_extents(osb, inode, et);
4598 if (free_extents < 0) {
4599 status = free_extents;
4604 /* there are two cases which could cause us to EAGAIN in the
4605 * we-need-more-metadata case:
4606 * 1) we haven't reserved *any*
4607 * 2) we are so fragmented, we've needed to add metadata too
4609 if (!free_extents && !meta_ac) {
4610 mlog(0, "we haven't reserved any metadata!\n");
4612 reason = RESTART_META;
4614 } else if ((!free_extents)
4615 && (ocfs2_alloc_context_bits_left(meta_ac)
4616 < ocfs2_extend_meta_needed(et->et_root_el))) {
4617 mlog(0, "filesystem is really fragmented...\n");
4619 reason = RESTART_META;
4623 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4624 clusters_to_add, &bit_off, &num_bits);
4626 if (status != -ENOSPC)
4631 BUG_ON(num_bits > clusters_to_add);
4633 /* reserve our write early -- insert_extent may update the tree root */
4634 status = ocfs2_et_root_journal_access(handle, inode, et,
4635 OCFS2_JOURNAL_ACCESS_WRITE);
4641 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4642 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4643 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4644 status = ocfs2_insert_extent(osb, handle, inode, et,
4645 *logical_offset, block,
4646 num_bits, flags, meta_ac);
4652 status = ocfs2_journal_dirty(handle, et->et_root_bh);
4658 clusters_to_add -= num_bits;
4659 *logical_offset += num_bits;
4661 if (clusters_to_add) {
4662 mlog(0, "need to alloc once more, wanted = %u\n",
4665 reason = RESTART_TRANS;
4671 *reason_ret = reason;
4675 static void ocfs2_make_right_split_rec(struct super_block *sb,
4676 struct ocfs2_extent_rec *split_rec,
4678 struct ocfs2_extent_rec *rec)
4680 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4681 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4683 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4685 split_rec->e_cpos = cpu_to_le32(cpos);
4686 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4688 split_rec->e_blkno = rec->e_blkno;
4689 le64_add_cpu(&split_rec->e_blkno,
4690 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4692 split_rec->e_flags = rec->e_flags;
4695 static int ocfs2_split_and_insert(struct inode *inode,
4697 struct ocfs2_path *path,
4698 struct ocfs2_extent_tree *et,
4699 struct buffer_head **last_eb_bh,
4701 struct ocfs2_extent_rec *orig_split_rec,
4702 struct ocfs2_alloc_context *meta_ac)
4705 unsigned int insert_range, rec_range, do_leftright = 0;
4706 struct ocfs2_extent_rec tmprec;
4707 struct ocfs2_extent_list *rightmost_el;
4708 struct ocfs2_extent_rec rec;
4709 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4710 struct ocfs2_insert_type insert;
4711 struct ocfs2_extent_block *eb;
4715 * Store a copy of the record on the stack - it might move
4716 * around as the tree is manipulated below.
4718 rec = path_leaf_el(path)->l_recs[split_index];
4720 rightmost_el = et->et_root_el;
4722 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4724 BUG_ON(!(*last_eb_bh));
4725 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4726 rightmost_el = &eb->h_list;
4729 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4730 le16_to_cpu(rightmost_el->l_count)) {
4731 ret = ocfs2_grow_tree(inode, handle, et,
4732 &depth, last_eb_bh, meta_ac);
4739 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4740 insert.ins_appending = APPEND_NONE;
4741 insert.ins_contig = CONTIG_NONE;
4742 insert.ins_tree_depth = depth;
4744 insert_range = le32_to_cpu(split_rec.e_cpos) +
4745 le16_to_cpu(split_rec.e_leaf_clusters);
4746 rec_range = le32_to_cpu(rec.e_cpos) +
4747 le16_to_cpu(rec.e_leaf_clusters);
4749 if (split_rec.e_cpos == rec.e_cpos) {
4750 insert.ins_split = SPLIT_LEFT;
4751 } else if (insert_range == rec_range) {
4752 insert.ins_split = SPLIT_RIGHT;
4755 * Left/right split. We fake this as a right split
4756 * first and then make a second pass as a left split.
4758 insert.ins_split = SPLIT_RIGHT;
4760 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4765 BUG_ON(do_leftright);
4769 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4775 if (do_leftright == 1) {
4777 struct ocfs2_extent_list *el;
4780 split_rec = *orig_split_rec;
4782 ocfs2_reinit_path(path, 1);
4784 cpos = le32_to_cpu(split_rec.e_cpos);
4785 ret = ocfs2_find_path(inode, path, cpos);
4791 el = path_leaf_el(path);
4792 split_index = ocfs2_search_extent_list(el, cpos);
4800 static int ocfs2_replace_extent_rec(struct inode *inode,
4802 struct ocfs2_path *path,
4803 struct ocfs2_extent_list *el,
4805 struct ocfs2_extent_rec *split_rec)
4809 ret = ocfs2_path_bh_journal_access(handle, inode, path,
4810 path_num_items(path) - 1);
4816 el->l_recs[split_index] = *split_rec;
4818 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4824 * Mark part or all of the extent record at split_index in the leaf
4825 * pointed to by path as written. This removes the unwritten
4828 * Care is taken to handle contiguousness so as to not grow the tree.
4830 * meta_ac is not strictly necessary - we only truly need it if growth
4831 * of the tree is required. All other cases will degrade into a less
4832 * optimal tree layout.
4834 * last_eb_bh should be the rightmost leaf block for any extent
4835 * btree. Since a split may grow the tree or a merge might shrink it,
4836 * the caller cannot trust the contents of that buffer after this call.
4838 * This code is optimized for readability - several passes might be
4839 * made over certain portions of the tree. All of those blocks will
4840 * have been brought into cache (and pinned via the journal), so the
4841 * extra overhead is not expressed in terms of disk reads.
4843 static int __ocfs2_mark_extent_written(struct inode *inode,
4844 struct ocfs2_extent_tree *et,
4846 struct ocfs2_path *path,
4848 struct ocfs2_extent_rec *split_rec,
4849 struct ocfs2_alloc_context *meta_ac,
4850 struct ocfs2_cached_dealloc_ctxt *dealloc)
4853 struct ocfs2_extent_list *el = path_leaf_el(path);
4854 struct buffer_head *last_eb_bh = NULL;
4855 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4856 struct ocfs2_merge_ctxt ctxt;
4857 struct ocfs2_extent_list *rightmost_el;
4859 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
4865 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4866 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4867 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4873 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
4878 * The core merge / split code wants to know how much room is
4879 * left in this inodes allocation tree, so we pass the
4880 * rightmost extent list.
4882 if (path->p_tree_depth) {
4883 struct ocfs2_extent_block *eb;
4885 ret = ocfs2_read_extent_block(inode,
4886 ocfs2_et_get_last_eb_blk(et),
4893 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4894 rightmost_el = &eb->h_list;
4896 rightmost_el = path_root_el(path);
4898 if (rec->e_cpos == split_rec->e_cpos &&
4899 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4900 ctxt.c_split_covers_rec = 1;
4902 ctxt.c_split_covers_rec = 0;
4904 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4906 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4907 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4908 ctxt.c_split_covers_rec);
4910 if (ctxt.c_contig_type == CONTIG_NONE) {
4911 if (ctxt.c_split_covers_rec)
4912 ret = ocfs2_replace_extent_rec(inode, handle,
4914 split_index, split_rec);
4916 ret = ocfs2_split_and_insert(inode, handle, path, et,
4917 &last_eb_bh, split_index,
4918 split_rec, meta_ac);
4922 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4923 split_index, split_rec,
4924 dealloc, &ctxt, et);
4935 * Mark the already-existing extent at cpos as written for len clusters.
4937 * If the existing extent is larger than the request, initiate a
4938 * split. An attempt will be made at merging with adjacent extents.
4940 * The caller is responsible for passing down meta_ac if we'll need it.
4942 int ocfs2_mark_extent_written(struct inode *inode,
4943 struct ocfs2_extent_tree *et,
4944 handle_t *handle, u32 cpos, u32 len, u32 phys,
4945 struct ocfs2_alloc_context *meta_ac,
4946 struct ocfs2_cached_dealloc_ctxt *dealloc)
4949 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4950 struct ocfs2_extent_rec split_rec;
4951 struct ocfs2_path *left_path = NULL;
4952 struct ocfs2_extent_list *el;
4954 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4955 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4957 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4958 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4959 "that are being written to, but the feature bit "
4960 "is not set in the super block.",
4961 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4967 * XXX: This should be fixed up so that we just re-insert the
4968 * next extent records.
4970 * XXX: This is a hack on the extent tree, maybe it should be
4973 if (et->et_ops == &ocfs2_dinode_et_ops)
4974 ocfs2_extent_map_trunc(inode, 0);
4976 left_path = ocfs2_new_path_from_et(et);
4983 ret = ocfs2_find_path(inode, left_path, cpos);
4988 el = path_leaf_el(left_path);
4990 index = ocfs2_search_extent_list(el, cpos);
4991 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4992 ocfs2_error(inode->i_sb,
4993 "Inode %llu has an extent at cpos %u which can no "
4994 "longer be found.\n",
4995 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5000 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5001 split_rec.e_cpos = cpu_to_le32(cpos);
5002 split_rec.e_leaf_clusters = cpu_to_le16(len);
5003 split_rec.e_blkno = cpu_to_le64(start_blkno);
5004 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5005 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5007 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
5008 index, &split_rec, meta_ac,
5014 ocfs2_free_path(left_path);
5018 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
5019 handle_t *handle, struct ocfs2_path *path,
5020 int index, u32 new_range,
5021 struct ocfs2_alloc_context *meta_ac)
5023 int ret, depth, credits = handle->h_buffer_credits;
5024 struct buffer_head *last_eb_bh = NULL;
5025 struct ocfs2_extent_block *eb;
5026 struct ocfs2_extent_list *rightmost_el, *el;
5027 struct ocfs2_extent_rec split_rec;
5028 struct ocfs2_extent_rec *rec;
5029 struct ocfs2_insert_type insert;
5032 * Setup the record to split before we grow the tree.
5034 el = path_leaf_el(path);
5035 rec = &el->l_recs[index];
5036 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5038 depth = path->p_tree_depth;
5040 ret = ocfs2_read_extent_block(inode,
5041 ocfs2_et_get_last_eb_blk(et),
5048 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5049 rightmost_el = &eb->h_list;
5051 rightmost_el = path_leaf_el(path);
5053 credits += path->p_tree_depth +
5054 ocfs2_extend_meta_needed(et->et_root_el);
5055 ret = ocfs2_extend_trans(handle, credits);
5061 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5062 le16_to_cpu(rightmost_el->l_count)) {
5063 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
5071 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5072 insert.ins_appending = APPEND_NONE;
5073 insert.ins_contig = CONTIG_NONE;
5074 insert.ins_split = SPLIT_RIGHT;
5075 insert.ins_tree_depth = depth;
5077 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
5086 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5087 struct ocfs2_path *path, int index,
5088 struct ocfs2_cached_dealloc_ctxt *dealloc,
5090 struct ocfs2_extent_tree *et)
5093 u32 left_cpos, rec_range, trunc_range;
5094 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5095 struct super_block *sb = inode->i_sb;
5096 struct ocfs2_path *left_path = NULL;
5097 struct ocfs2_extent_list *el = path_leaf_el(path);
5098 struct ocfs2_extent_rec *rec;
5099 struct ocfs2_extent_block *eb;
5101 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5102 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5111 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5112 path->p_tree_depth) {
5114 * Check whether this is the rightmost tree record. If
5115 * we remove all of this record or part of its right
5116 * edge then an update of the record lengths above it
5119 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5120 if (eb->h_next_leaf_blk == 0)
5121 is_rightmost_tree_rec = 1;
5124 rec = &el->l_recs[index];
5125 if (index == 0 && path->p_tree_depth &&
5126 le32_to_cpu(rec->e_cpos) == cpos) {
5128 * Changing the leftmost offset (via partial or whole
5129 * record truncate) of an interior (or rightmost) path
5130 * means we have to update the subtree that is formed
5131 * by this leaf and the one to it's left.
5133 * There are two cases we can skip:
5134 * 1) Path is the leftmost one in our inode tree.
5135 * 2) The leaf is rightmost and will be empty after
5136 * we remove the extent record - the rotate code
5137 * knows how to update the newly formed edge.
5140 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5147 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5148 left_path = ocfs2_new_path_from_path(path);
5155 ret = ocfs2_find_path(inode, left_path, left_cpos);
5163 ret = ocfs2_extend_rotate_transaction(handle, 0,
5164 handle->h_buffer_credits,
5171 ret = ocfs2_journal_access_path(inode, handle, path);
5177 ret = ocfs2_journal_access_path(inode, handle, left_path);
5183 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5184 trunc_range = cpos + len;
5186 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5189 memset(rec, 0, sizeof(*rec));
5190 ocfs2_cleanup_merge(el, index);
5193 next_free = le16_to_cpu(el->l_next_free_rec);
5194 if (is_rightmost_tree_rec && next_free > 1) {
5196 * We skip the edge update if this path will
5197 * be deleted by the rotate code.
5199 rec = &el->l_recs[next_free - 1];
5200 ocfs2_adjust_rightmost_records(inode, handle, path,
5203 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5204 /* Remove leftmost portion of the record. */
5205 le32_add_cpu(&rec->e_cpos, len);
5206 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5207 le16_add_cpu(&rec->e_leaf_clusters, -len);
5208 } else if (rec_range == trunc_range) {
5209 /* Remove rightmost portion of the record */
5210 le16_add_cpu(&rec->e_leaf_clusters, -len);
5211 if (is_rightmost_tree_rec)
5212 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5214 /* Caller should have trapped this. */
5215 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5216 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5217 le32_to_cpu(rec->e_cpos),
5218 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5225 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5226 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5230 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5232 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5239 ocfs2_free_path(left_path);
5243 int ocfs2_remove_extent(struct inode *inode,
5244 struct ocfs2_extent_tree *et,
5245 u32 cpos, u32 len, handle_t *handle,
5246 struct ocfs2_alloc_context *meta_ac,
5247 struct ocfs2_cached_dealloc_ctxt *dealloc)
5250 u32 rec_range, trunc_range;
5251 struct ocfs2_extent_rec *rec;
5252 struct ocfs2_extent_list *el;
5253 struct ocfs2_path *path = NULL;
5255 ocfs2_extent_map_trunc(inode, 0);
5257 path = ocfs2_new_path_from_et(et);
5264 ret = ocfs2_find_path(inode, path, cpos);
5270 el = path_leaf_el(path);
5271 index = ocfs2_search_extent_list(el, cpos);
5272 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5273 ocfs2_error(inode->i_sb,
5274 "Inode %llu has an extent at cpos %u which can no "
5275 "longer be found.\n",
5276 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5282 * We have 3 cases of extent removal:
5283 * 1) Range covers the entire extent rec
5284 * 2) Range begins or ends on one edge of the extent rec
5285 * 3) Range is in the middle of the extent rec (no shared edges)
5287 * For case 1 we remove the extent rec and left rotate to
5290 * For case 2 we just shrink the existing extent rec, with a
5291 * tree update if the shrinking edge is also the edge of an
5294 * For case 3 we do a right split to turn the extent rec into
5295 * something case 2 can handle.
5297 rec = &el->l_recs[index];
5298 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5299 trunc_range = cpos + len;
5301 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5303 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5304 "(cpos %u, len %u)\n",
5305 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5306 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5308 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5309 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5316 ret = ocfs2_split_tree(inode, et, handle, path, index,
5317 trunc_range, meta_ac);
5324 * The split could have manipulated the tree enough to
5325 * move the record location, so we have to look for it again.
5327 ocfs2_reinit_path(path, 1);
5329 ret = ocfs2_find_path(inode, path, cpos);
5335 el = path_leaf_el(path);
5336 index = ocfs2_search_extent_list(el, cpos);
5337 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5338 ocfs2_error(inode->i_sb,
5339 "Inode %llu: split at cpos %u lost record.",
5340 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5347 * Double check our values here. If anything is fishy,
5348 * it's easier to catch it at the top level.
5350 rec = &el->l_recs[index];
5351 rec_range = le32_to_cpu(rec->e_cpos) +
5352 ocfs2_rec_clusters(el, rec);
5353 if (rec_range != trunc_range) {
5354 ocfs2_error(inode->i_sb,
5355 "Inode %llu: error after split at cpos %u"
5356 "trunc len %u, existing record is (%u,%u)",
5357 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5358 cpos, len, le32_to_cpu(rec->e_cpos),
5359 ocfs2_rec_clusters(el, rec));
5364 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5373 ocfs2_free_path(path);
5377 int ocfs2_remove_btree_range(struct inode *inode,
5378 struct ocfs2_extent_tree *et,
5379 u32 cpos, u32 phys_cpos, u32 len,
5380 struct ocfs2_cached_dealloc_ctxt *dealloc)
5383 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5384 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5385 struct inode *tl_inode = osb->osb_tl_inode;
5387 struct ocfs2_alloc_context *meta_ac = NULL;
5389 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5395 mutex_lock(&tl_inode->i_mutex);
5397 if (ocfs2_truncate_log_needs_flush(osb)) {
5398 ret = __ocfs2_flush_truncate_log(osb);
5405 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5406 if (IS_ERR(handle)) {
5407 ret = PTR_ERR(handle);
5412 ret = ocfs2_et_root_journal_access(handle, inode, et,
5413 OCFS2_JOURNAL_ACCESS_WRITE);
5419 vfs_dq_free_space_nodirty(inode,
5420 ocfs2_clusters_to_bytes(inode->i_sb, len));
5422 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5429 ocfs2_et_update_clusters(inode, et, -len);
5431 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5437 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5442 ocfs2_commit_trans(osb, handle);
5444 mutex_unlock(&tl_inode->i_mutex);
5447 ocfs2_free_alloc_context(meta_ac);
5452 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5454 struct buffer_head *tl_bh = osb->osb_tl_bh;
5455 struct ocfs2_dinode *di;
5456 struct ocfs2_truncate_log *tl;
5458 di = (struct ocfs2_dinode *) tl_bh->b_data;
5459 tl = &di->id2.i_dealloc;
5461 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5462 "slot %d, invalid truncate log parameters: used = "
5463 "%u, count = %u\n", osb->slot_num,
5464 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5465 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5468 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5469 unsigned int new_start)
5471 unsigned int tail_index;
5472 unsigned int current_tail;
5474 /* No records, nothing to coalesce */
5475 if (!le16_to_cpu(tl->tl_used))
5478 tail_index = le16_to_cpu(tl->tl_used) - 1;
5479 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5480 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5482 return current_tail == new_start;
5485 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5488 unsigned int num_clusters)
5491 unsigned int start_cluster, tl_count;
5492 struct inode *tl_inode = osb->osb_tl_inode;
5493 struct buffer_head *tl_bh = osb->osb_tl_bh;
5494 struct ocfs2_dinode *di;
5495 struct ocfs2_truncate_log *tl;
5497 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5498 (unsigned long long)start_blk, num_clusters);
5500 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5502 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5504 di = (struct ocfs2_dinode *) tl_bh->b_data;
5506 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5507 * by the underlying call to ocfs2_read_inode_block(), so any
5508 * corruption is a code bug */
5509 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5511 tl = &di->id2.i_dealloc;
5512 tl_count = le16_to_cpu(tl->tl_count);
5513 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5515 "Truncate record count on #%llu invalid "
5516 "wanted %u, actual %u\n",
5517 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5518 ocfs2_truncate_recs_per_inode(osb->sb),
5519 le16_to_cpu(tl->tl_count));
5521 /* Caller should have known to flush before calling us. */
5522 index = le16_to_cpu(tl->tl_used);
5523 if (index >= tl_count) {
5529 status = ocfs2_journal_access_di(handle, tl_inode, tl_bh,
5530 OCFS2_JOURNAL_ACCESS_WRITE);
5536 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5537 "%llu (index = %d)\n", num_clusters, start_cluster,
5538 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5540 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5542 * Move index back to the record we are coalescing with.
5543 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5547 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5548 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5549 index, le32_to_cpu(tl->tl_recs[index].t_start),
5552 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5553 tl->tl_used = cpu_to_le16(index + 1);
5555 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5557 status = ocfs2_journal_dirty(handle, tl_bh);
5568 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5570 struct inode *data_alloc_inode,
5571 struct buffer_head *data_alloc_bh)
5575 unsigned int num_clusters;
5577 struct ocfs2_truncate_rec rec;
5578 struct ocfs2_dinode *di;
5579 struct ocfs2_truncate_log *tl;
5580 struct inode *tl_inode = osb->osb_tl_inode;
5581 struct buffer_head *tl_bh = osb->osb_tl_bh;
5585 di = (struct ocfs2_dinode *) tl_bh->b_data;
5586 tl = &di->id2.i_dealloc;
5587 i = le16_to_cpu(tl->tl_used) - 1;
5589 /* Caller has given us at least enough credits to
5590 * update the truncate log dinode */
5591 status = ocfs2_journal_access_di(handle, tl_inode, tl_bh,
5592 OCFS2_JOURNAL_ACCESS_WRITE);
5598 tl->tl_used = cpu_to_le16(i);
5600 status = ocfs2_journal_dirty(handle, tl_bh);
5606 /* TODO: Perhaps we can calculate the bulk of the
5607 * credits up front rather than extending like
5609 status = ocfs2_extend_trans(handle,
5610 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5616 rec = tl->tl_recs[i];
5617 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5618 le32_to_cpu(rec.t_start));
5619 num_clusters = le32_to_cpu(rec.t_clusters);
5621 /* if start_blk is not set, we ignore the record as
5624 mlog(0, "free record %d, start = %u, clusters = %u\n",
5625 i, le32_to_cpu(rec.t_start), num_clusters);
5627 status = ocfs2_free_clusters(handle, data_alloc_inode,
5628 data_alloc_bh, start_blk,
5643 /* Expects you to already be holding tl_inode->i_mutex */
5644 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5647 unsigned int num_to_flush;
5649 struct inode *tl_inode = osb->osb_tl_inode;
5650 struct inode *data_alloc_inode = NULL;
5651 struct buffer_head *tl_bh = osb->osb_tl_bh;
5652 struct buffer_head *data_alloc_bh = NULL;
5653 struct ocfs2_dinode *di;
5654 struct ocfs2_truncate_log *tl;
5658 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5660 di = (struct ocfs2_dinode *) tl_bh->b_data;
5662 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5663 * by the underlying call to ocfs2_read_inode_block(), so any
5664 * corruption is a code bug */
5665 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5667 tl = &di->id2.i_dealloc;
5668 num_to_flush = le16_to_cpu(tl->tl_used);
5669 mlog(0, "Flush %u records from truncate log #%llu\n",
5670 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5671 if (!num_to_flush) {
5676 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5677 GLOBAL_BITMAP_SYSTEM_INODE,
5678 OCFS2_INVALID_SLOT);
5679 if (!data_alloc_inode) {
5681 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5685 mutex_lock(&data_alloc_inode->i_mutex);
5687 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5693 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5694 if (IS_ERR(handle)) {
5695 status = PTR_ERR(handle);
5700 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5705 ocfs2_commit_trans(osb, handle);
5708 brelse(data_alloc_bh);
5709 ocfs2_inode_unlock(data_alloc_inode, 1);
5712 mutex_unlock(&data_alloc_inode->i_mutex);
5713 iput(data_alloc_inode);
5720 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5723 struct inode *tl_inode = osb->osb_tl_inode;
5725 mutex_lock(&tl_inode->i_mutex);
5726 status = __ocfs2_flush_truncate_log(osb);
5727 mutex_unlock(&tl_inode->i_mutex);
5732 static void ocfs2_truncate_log_worker(struct work_struct *work)
5735 struct ocfs2_super *osb =
5736 container_of(work, struct ocfs2_super,
5737 osb_truncate_log_wq.work);
5741 status = ocfs2_flush_truncate_log(osb);
5745 ocfs2_init_inode_steal_slot(osb);
5750 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5751 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5754 if (osb->osb_tl_inode) {
5755 /* We want to push off log flushes while truncates are
5758 cancel_delayed_work(&osb->osb_truncate_log_wq);
5760 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5761 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5765 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5767 struct inode **tl_inode,
5768 struct buffer_head **tl_bh)
5771 struct inode *inode = NULL;
5772 struct buffer_head *bh = NULL;
5774 inode = ocfs2_get_system_file_inode(osb,
5775 TRUNCATE_LOG_SYSTEM_INODE,
5779 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5783 status = ocfs2_read_inode_block(inode, &bh);
5797 /* called during the 1st stage of node recovery. we stamp a clean
5798 * truncate log and pass back a copy for processing later. if the
5799 * truncate log does not require processing, a *tl_copy is set to
5801 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5803 struct ocfs2_dinode **tl_copy)
5806 struct inode *tl_inode = NULL;
5807 struct buffer_head *tl_bh = NULL;
5808 struct ocfs2_dinode *di;
5809 struct ocfs2_truncate_log *tl;
5813 mlog(0, "recover truncate log from slot %d\n", slot_num);
5815 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5821 di = (struct ocfs2_dinode *) tl_bh->b_data;
5823 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5824 * validated by the underlying call to ocfs2_read_inode_block(),
5825 * so any corruption is a code bug */
5826 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5828 tl = &di->id2.i_dealloc;
5829 if (le16_to_cpu(tl->tl_used)) {
5830 mlog(0, "We'll have %u logs to recover\n",
5831 le16_to_cpu(tl->tl_used));
5833 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5840 /* Assuming the write-out below goes well, this copy
5841 * will be passed back to recovery for processing. */
5842 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5844 /* All we need to do to clear the truncate log is set
5848 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
5849 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5861 if (status < 0 && (*tl_copy)) {
5870 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5871 struct ocfs2_dinode *tl_copy)
5875 unsigned int clusters, num_recs, start_cluster;
5878 struct inode *tl_inode = osb->osb_tl_inode;
5879 struct ocfs2_truncate_log *tl;
5883 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5884 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5888 tl = &tl_copy->id2.i_dealloc;
5889 num_recs = le16_to_cpu(tl->tl_used);
5890 mlog(0, "cleanup %u records from %llu\n", num_recs,
5891 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
5893 mutex_lock(&tl_inode->i_mutex);
5894 for(i = 0; i < num_recs; i++) {
5895 if (ocfs2_truncate_log_needs_flush(osb)) {
5896 status = __ocfs2_flush_truncate_log(osb);
5903 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5904 if (IS_ERR(handle)) {
5905 status = PTR_ERR(handle);
5910 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5911 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5912 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5914 status = ocfs2_truncate_log_append(osb, handle,
5915 start_blk, clusters);
5916 ocfs2_commit_trans(osb, handle);
5924 mutex_unlock(&tl_inode->i_mutex);
5930 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5933 struct inode *tl_inode = osb->osb_tl_inode;
5938 cancel_delayed_work(&osb->osb_truncate_log_wq);
5939 flush_workqueue(ocfs2_wq);
5941 status = ocfs2_flush_truncate_log(osb);
5945 brelse(osb->osb_tl_bh);
5946 iput(osb->osb_tl_inode);
5952 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5955 struct inode *tl_inode = NULL;
5956 struct buffer_head *tl_bh = NULL;
5960 status = ocfs2_get_truncate_log_info(osb,
5967 /* ocfs2_truncate_log_shutdown keys on the existence of
5968 * osb->osb_tl_inode so we don't set any of the osb variables
5969 * until we're sure all is well. */
5970 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5971 ocfs2_truncate_log_worker);
5972 osb->osb_tl_bh = tl_bh;
5973 osb->osb_tl_inode = tl_inode;
5980 * Delayed de-allocation of suballocator blocks.
5982 * Some sets of block de-allocations might involve multiple suballocator inodes.
5984 * The locking for this can get extremely complicated, especially when
5985 * the suballocator inodes to delete from aren't known until deep
5986 * within an unrelated codepath.
5988 * ocfs2_extent_block structures are a good example of this - an inode
5989 * btree could have been grown by any number of nodes each allocating
5990 * out of their own suballoc inode.
5992 * These structures allow the delay of block de-allocation until a
5993 * later time, when locking of multiple cluster inodes won't cause
5998 * Describe a single bit freed from a suballocator. For the block
5999 * suballocators, it represents one block. For the global cluster
6000 * allocator, it represents some clusters and free_bit indicates
6003 struct ocfs2_cached_block_free {
6004 struct ocfs2_cached_block_free *free_next;
6006 unsigned int free_bit;
6009 struct ocfs2_per_slot_free_list {
6010 struct ocfs2_per_slot_free_list *f_next_suballocator;
6013 struct ocfs2_cached_block_free *f_first;
6016 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6019 struct ocfs2_cached_block_free *head)
6024 struct inode *inode;
6025 struct buffer_head *di_bh = NULL;
6026 struct ocfs2_cached_block_free *tmp;
6028 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6035 mutex_lock(&inode->i_mutex);
6037 ret = ocfs2_inode_lock(inode, &di_bh, 1);
6043 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6044 if (IS_ERR(handle)) {
6045 ret = PTR_ERR(handle);
6051 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6053 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6054 head->free_bit, (unsigned long long)head->free_blk);
6056 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6057 head->free_bit, bg_blkno, 1);
6063 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6070 head = head->free_next;
6075 ocfs2_commit_trans(osb, handle);
6078 ocfs2_inode_unlock(inode, 1);
6081 mutex_unlock(&inode->i_mutex);
6085 /* Premature exit may have left some dangling items. */
6087 head = head->free_next;
6094 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6095 u64 blkno, unsigned int bit)
6098 struct ocfs2_cached_block_free *item;
6100 item = kmalloc(sizeof(*item), GFP_NOFS);
6107 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6108 bit, (unsigned long long)blkno);
6110 item->free_blk = blkno;
6111 item->free_bit = bit;
6112 item->free_next = ctxt->c_global_allocator;
6114 ctxt->c_global_allocator = item;
6118 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6119 struct ocfs2_cached_block_free *head)
6121 struct ocfs2_cached_block_free *tmp;
6122 struct inode *tl_inode = osb->osb_tl_inode;
6126 mutex_lock(&tl_inode->i_mutex);
6129 if (ocfs2_truncate_log_needs_flush(osb)) {
6130 ret = __ocfs2_flush_truncate_log(osb);
6137 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6138 if (IS_ERR(handle)) {
6139 ret = PTR_ERR(handle);
6144 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6147 ocfs2_commit_trans(osb, handle);
6149 head = head->free_next;
6158 mutex_unlock(&tl_inode->i_mutex);
6161 /* Premature exit may have left some dangling items. */
6163 head = head->free_next;
6170 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6171 struct ocfs2_cached_dealloc_ctxt *ctxt)
6174 struct ocfs2_per_slot_free_list *fl;
6179 while (ctxt->c_first_suballocator) {
6180 fl = ctxt->c_first_suballocator;
6183 mlog(0, "Free items: (type %u, slot %d)\n",
6184 fl->f_inode_type, fl->f_slot);
6185 ret2 = ocfs2_free_cached_blocks(osb,
6195 ctxt->c_first_suballocator = fl->f_next_suballocator;
6199 if (ctxt->c_global_allocator) {
6200 ret2 = ocfs2_free_cached_clusters(osb,
6201 ctxt->c_global_allocator);
6207 ctxt->c_global_allocator = NULL;
6213 static struct ocfs2_per_slot_free_list *
6214 ocfs2_find_per_slot_free_list(int type,
6216 struct ocfs2_cached_dealloc_ctxt *ctxt)
6218 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6221 if (fl->f_inode_type == type && fl->f_slot == slot)
6224 fl = fl->f_next_suballocator;
6227 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6229 fl->f_inode_type = type;
6232 fl->f_next_suballocator = ctxt->c_first_suballocator;
6234 ctxt->c_first_suballocator = fl;
6239 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6240 int type, int slot, u64 blkno,
6244 struct ocfs2_per_slot_free_list *fl;
6245 struct ocfs2_cached_block_free *item;
6247 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6254 item = kmalloc(sizeof(*item), GFP_NOFS);
6261 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6262 type, slot, bit, (unsigned long long)blkno);
6264 item->free_blk = blkno;
6265 item->free_bit = bit;
6266 item->free_next = fl->f_first;
6275 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6276 struct ocfs2_extent_block *eb)
6278 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6279 le16_to_cpu(eb->h_suballoc_slot),
6280 le64_to_cpu(eb->h_blkno),
6281 le16_to_cpu(eb->h_suballoc_bit));
6284 /* This function will figure out whether the currently last extent
6285 * block will be deleted, and if it will, what the new last extent
6286 * block will be so we can update his h_next_leaf_blk field, as well
6287 * as the dinodes i_last_eb_blk */
6288 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6289 unsigned int clusters_to_del,
6290 struct ocfs2_path *path,
6291 struct buffer_head **new_last_eb)
6293 int next_free, ret = 0;
6295 struct ocfs2_extent_rec *rec;
6296 struct ocfs2_extent_block *eb;
6297 struct ocfs2_extent_list *el;
6298 struct buffer_head *bh = NULL;
6300 *new_last_eb = NULL;
6302 /* we have no tree, so of course, no last_eb. */
6303 if (!path->p_tree_depth)
6306 /* trunc to zero special case - this makes tree_depth = 0
6307 * regardless of what it is. */
6308 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6311 el = path_leaf_el(path);
6312 BUG_ON(!el->l_next_free_rec);
6315 * Make sure that this extent list will actually be empty
6316 * after we clear away the data. We can shortcut out if
6317 * there's more than one non-empty extent in the
6318 * list. Otherwise, a check of the remaining extent is
6321 next_free = le16_to_cpu(el->l_next_free_rec);
6323 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6327 /* We may have a valid extent in index 1, check it. */
6329 rec = &el->l_recs[1];
6332 * Fall through - no more nonempty extents, so we want
6333 * to delete this leaf.
6339 rec = &el->l_recs[0];
6344 * Check it we'll only be trimming off the end of this
6347 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6351 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6357 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6363 eb = (struct ocfs2_extent_block *) bh->b_data;
6366 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6367 * Any corruption is a code bug. */
6368 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
6371 get_bh(*new_last_eb);
6372 mlog(0, "returning block %llu, (cpos: %u)\n",
6373 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6381 * Trim some clusters off the rightmost edge of a tree. Only called
6384 * The caller needs to:
6385 * - start journaling of each path component.
6386 * - compute and fully set up any new last ext block
6388 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6389 handle_t *handle, struct ocfs2_truncate_context *tc,
6390 u32 clusters_to_del, u64 *delete_start)
6392 int ret, i, index = path->p_tree_depth;
6395 struct buffer_head *bh;
6396 struct ocfs2_extent_list *el;
6397 struct ocfs2_extent_rec *rec;
6401 while (index >= 0) {
6402 bh = path->p_node[index].bh;
6403 el = path->p_node[index].el;
6405 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6406 index, (unsigned long long)bh->b_blocknr);
6408 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6411 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6412 ocfs2_error(inode->i_sb,
6413 "Inode %lu has invalid ext. block %llu",
6415 (unsigned long long)bh->b_blocknr);
6421 i = le16_to_cpu(el->l_next_free_rec) - 1;
6422 rec = &el->l_recs[i];
6424 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6425 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6426 ocfs2_rec_clusters(el, rec),
6427 (unsigned long long)le64_to_cpu(rec->e_blkno),
6428 le16_to_cpu(el->l_next_free_rec));
6430 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6432 if (le16_to_cpu(el->l_tree_depth) == 0) {
6434 * If the leaf block contains a single empty
6435 * extent and no records, we can just remove
6438 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6440 sizeof(struct ocfs2_extent_rec));
6441 el->l_next_free_rec = cpu_to_le16(0);
6447 * Remove any empty extents by shifting things
6448 * left. That should make life much easier on
6449 * the code below. This condition is rare
6450 * enough that we shouldn't see a performance
6453 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6454 le16_add_cpu(&el->l_next_free_rec, -1);
6457 i < le16_to_cpu(el->l_next_free_rec); i++)
6458 el->l_recs[i] = el->l_recs[i + 1];
6460 memset(&el->l_recs[i], 0,
6461 sizeof(struct ocfs2_extent_rec));
6464 * We've modified our extent list. The
6465 * simplest way to handle this change
6466 * is to being the search from the
6469 goto find_tail_record;
6472 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6475 * We'll use "new_edge" on our way back up the
6476 * tree to know what our rightmost cpos is.
6478 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6479 new_edge += le32_to_cpu(rec->e_cpos);
6482 * The caller will use this to delete data blocks.
6484 *delete_start = le64_to_cpu(rec->e_blkno)
6485 + ocfs2_clusters_to_blocks(inode->i_sb,
6486 le16_to_cpu(rec->e_leaf_clusters));
6489 * If it's now empty, remove this record.
6491 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6493 sizeof(struct ocfs2_extent_rec));
6494 le16_add_cpu(&el->l_next_free_rec, -1);
6497 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6499 sizeof(struct ocfs2_extent_rec));
6500 le16_add_cpu(&el->l_next_free_rec, -1);
6505 /* Can this actually happen? */
6506 if (le16_to_cpu(el->l_next_free_rec) == 0)
6510 * We never actually deleted any clusters
6511 * because our leaf was empty. There's no
6512 * reason to adjust the rightmost edge then.
6517 rec->e_int_clusters = cpu_to_le32(new_edge);
6518 le32_add_cpu(&rec->e_int_clusters,
6519 -le32_to_cpu(rec->e_cpos));
6522 * A deleted child record should have been
6525 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6529 ret = ocfs2_journal_dirty(handle, bh);
6535 mlog(0, "extent list container %llu, after: record %d: "
6536 "(%u, %u, %llu), next = %u.\n",
6537 (unsigned long long)bh->b_blocknr, i,
6538 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6539 (unsigned long long)le64_to_cpu(rec->e_blkno),
6540 le16_to_cpu(el->l_next_free_rec));
6543 * We must be careful to only attempt delete of an
6544 * extent block (and not the root inode block).
6546 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6547 struct ocfs2_extent_block *eb =
6548 (struct ocfs2_extent_block *)bh->b_data;
6551 * Save this for use when processing the
6554 deleted_eb = le64_to_cpu(eb->h_blkno);
6556 mlog(0, "deleting this extent block.\n");
6558 ocfs2_remove_from_cache(inode, bh);
6560 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6561 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6562 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6564 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6565 /* An error here is not fatal. */
6580 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6581 unsigned int clusters_to_del,
6582 struct inode *inode,
6583 struct buffer_head *fe_bh,
6585 struct ocfs2_truncate_context *tc,
6586 struct ocfs2_path *path)
6589 struct ocfs2_dinode *fe;
6590 struct ocfs2_extent_block *last_eb = NULL;
6591 struct ocfs2_extent_list *el;
6592 struct buffer_head *last_eb_bh = NULL;
6595 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6597 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6605 * Each component will be touched, so we might as well journal
6606 * here to avoid having to handle errors later.
6608 status = ocfs2_journal_access_path(inode, handle, path);
6615 status = ocfs2_journal_access_eb(handle, inode, last_eb_bh,
6616 OCFS2_JOURNAL_ACCESS_WRITE);
6622 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6625 el = &(fe->id2.i_list);
6628 * Lower levels depend on this never happening, but it's best
6629 * to check it up here before changing the tree.
6631 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6632 ocfs2_error(inode->i_sb,
6633 "Inode %lu has an empty extent record, depth %u\n",
6634 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6639 vfs_dq_free_space_nodirty(inode,
6640 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
6641 spin_lock(&OCFS2_I(inode)->ip_lock);
6642 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6644 spin_unlock(&OCFS2_I(inode)->ip_lock);
6645 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6646 inode->i_blocks = ocfs2_inode_sector_count(inode);
6648 status = ocfs2_trim_tree(inode, path, handle, tc,
6649 clusters_to_del, &delete_blk);
6655 if (le32_to_cpu(fe->i_clusters) == 0) {
6656 /* trunc to zero is a special case. */
6657 el->l_tree_depth = 0;
6658 fe->i_last_eb_blk = 0;
6660 fe->i_last_eb_blk = last_eb->h_blkno;
6662 status = ocfs2_journal_dirty(handle, fe_bh);
6669 /* If there will be a new last extent block, then by
6670 * definition, there cannot be any leaves to the right of
6672 last_eb->h_next_leaf_blk = 0;
6673 status = ocfs2_journal_dirty(handle, last_eb_bh);
6681 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6695 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6697 set_buffer_uptodate(bh);
6698 mark_buffer_dirty(bh);
6702 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6703 unsigned int from, unsigned int to,
6704 struct page *page, int zero, u64 *phys)
6706 int ret, partial = 0;
6708 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6713 zero_user_segment(page, from, to);
6716 * Need to set the buffers we zero'd into uptodate
6717 * here if they aren't - ocfs2_map_page_blocks()
6718 * might've skipped some
6720 ret = walk_page_buffers(handle, page_buffers(page),
6725 else if (ocfs2_should_order_data(inode)) {
6726 ret = ocfs2_jbd2_file_inode(handle, inode);
6732 SetPageUptodate(page);
6734 flush_dcache_page(page);
6737 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6738 loff_t end, struct page **pages,
6739 int numpages, u64 phys, handle_t *handle)
6743 unsigned int from, to = PAGE_CACHE_SIZE;
6744 struct super_block *sb = inode->i_sb;
6746 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6751 to = PAGE_CACHE_SIZE;
6752 for(i = 0; i < numpages; i++) {
6755 from = start & (PAGE_CACHE_SIZE - 1);
6756 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6757 to = end & (PAGE_CACHE_SIZE - 1);
6759 BUG_ON(from > PAGE_CACHE_SIZE);
6760 BUG_ON(to > PAGE_CACHE_SIZE);
6762 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6765 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6769 ocfs2_unlock_and_free_pages(pages, numpages);
6772 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6773 struct page **pages, int *num)
6775 int numpages, ret = 0;
6776 struct super_block *sb = inode->i_sb;
6777 struct address_space *mapping = inode->i_mapping;
6778 unsigned long index;
6779 loff_t last_page_bytes;
6781 BUG_ON(start > end);
6783 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6784 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6787 last_page_bytes = PAGE_ALIGN(end);
6788 index = start >> PAGE_CACHE_SHIFT;
6790 pages[numpages] = grab_cache_page(mapping, index);
6791 if (!pages[numpages]) {
6799 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6804 ocfs2_unlock_and_free_pages(pages, numpages);
6814 * Zero the area past i_size but still within an allocated
6815 * cluster. This avoids exposing nonzero data on subsequent file
6818 * We need to call this before i_size is updated on the inode because
6819 * otherwise block_write_full_page() will skip writeout of pages past
6820 * i_size. The new_i_size parameter is passed for this reason.
6822 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6823 u64 range_start, u64 range_end)
6825 int ret = 0, numpages;
6826 struct page **pages = NULL;
6828 unsigned int ext_flags;
6829 struct super_block *sb = inode->i_sb;
6832 * File systems which don't support sparse files zero on every
6835 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6838 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6839 sizeof(struct page *), GFP_NOFS);
6840 if (pages == NULL) {
6846 if (range_start == range_end)
6849 ret = ocfs2_extent_map_get_blocks(inode,
6850 range_start >> sb->s_blocksize_bits,
6851 &phys, NULL, &ext_flags);
6858 * Tail is a hole, or is marked unwritten. In either case, we
6859 * can count on read and write to return/push zero's.
6861 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6864 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6871 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6872 numpages, phys, handle);
6875 * Initiate writeout of the pages we zero'd here. We don't
6876 * wait on them - the truncate_inode_pages() call later will
6879 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6880 range_end - 1, SYNC_FILE_RANGE_WRITE);
6891 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6892 struct ocfs2_dinode *di)
6894 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6895 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6897 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6898 memset(&di->id2, 0, blocksize -
6899 offsetof(struct ocfs2_dinode, id2) -
6902 memset(&di->id2, 0, blocksize -
6903 offsetof(struct ocfs2_dinode, id2));
6906 void ocfs2_dinode_new_extent_list(struct inode *inode,
6907 struct ocfs2_dinode *di)
6909 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6910 di->id2.i_list.l_tree_depth = 0;
6911 di->id2.i_list.l_next_free_rec = 0;
6912 di->id2.i_list.l_count = cpu_to_le16(
6913 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6916 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6918 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6919 struct ocfs2_inline_data *idata = &di->id2.i_data;
6921 spin_lock(&oi->ip_lock);
6922 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6923 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6924 spin_unlock(&oi->ip_lock);
6927 * We clear the entire i_data structure here so that all
6928 * fields can be properly initialized.
6930 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6932 idata->id_count = cpu_to_le16(
6933 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6936 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6937 struct buffer_head *di_bh)
6939 int ret, i, has_data, num_pages = 0;
6941 u64 uninitialized_var(block);
6942 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6943 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6944 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6945 struct ocfs2_alloc_context *data_ac = NULL;
6946 struct page **pages = NULL;
6947 loff_t end = osb->s_clustersize;
6948 struct ocfs2_extent_tree et;
6951 has_data = i_size_read(inode) ? 1 : 0;
6954 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6955 sizeof(struct page *), GFP_NOFS);
6956 if (pages == NULL) {
6962 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6969 handle = ocfs2_start_trans(osb,
6970 ocfs2_inline_to_extents_credits(osb->sb));
6971 if (IS_ERR(handle)) {
6972 ret = PTR_ERR(handle);
6977 ret = ocfs2_journal_access_di(handle, inode, di_bh,
6978 OCFS2_JOURNAL_ACCESS_WRITE);
6986 unsigned int page_end;
6989 if (vfs_dq_alloc_space_nodirty(inode,
6990 ocfs2_clusters_to_bytes(osb->sb, 1))) {
6996 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7004 * Save two copies, one for insert, and one that can
7005 * be changed by ocfs2_map_and_dirty_page() below.
7007 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7010 * Non sparse file systems zero on extend, so no need
7013 if (!ocfs2_sparse_alloc(osb) &&
7014 PAGE_CACHE_SIZE < osb->s_clustersize)
7015 end = PAGE_CACHE_SIZE;
7017 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7024 * This should populate the 1st page for us and mark
7027 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7033 page_end = PAGE_CACHE_SIZE;
7034 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7035 page_end = osb->s_clustersize;
7037 for (i = 0; i < num_pages; i++)
7038 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7039 pages[i], i > 0, &phys);
7042 spin_lock(&oi->ip_lock);
7043 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7044 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7045 spin_unlock(&oi->ip_lock);
7047 ocfs2_dinode_new_extent_list(inode, di);
7049 ocfs2_journal_dirty(handle, di_bh);
7053 * An error at this point should be extremely rare. If
7054 * this proves to be false, we could always re-build
7055 * the in-inode data from our pages.
7057 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
7058 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7059 0, block, 1, 0, NULL);
7065 inode->i_blocks = ocfs2_inode_sector_count(inode);
7069 if (ret < 0 && did_quota)
7070 vfs_dq_free_space_nodirty(inode,
7071 ocfs2_clusters_to_bytes(osb->sb, 1));
7073 ocfs2_commit_trans(osb, handle);
7077 ocfs2_free_alloc_context(data_ac);
7081 ocfs2_unlock_and_free_pages(pages, num_pages);
7089 * It is expected, that by the time you call this function,
7090 * inode->i_size and fe->i_size have been adjusted.
7092 * WARNING: This will kfree the truncate context
7094 int ocfs2_commit_truncate(struct ocfs2_super *osb,
7095 struct inode *inode,
7096 struct buffer_head *fe_bh,
7097 struct ocfs2_truncate_context *tc)
7099 int status, i, credits, tl_sem = 0;
7100 u32 clusters_to_del, new_highest_cpos, range;
7101 struct ocfs2_extent_list *el;
7102 handle_t *handle = NULL;
7103 struct inode *tl_inode = osb->osb_tl_inode;
7104 struct ocfs2_path *path = NULL;
7105 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
7109 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
7110 i_size_read(inode));
7112 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7113 ocfs2_journal_access_di);
7120 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7124 * Check that we still have allocation to delete.
7126 if (OCFS2_I(inode)->ip_clusters == 0) {
7132 * Truncate always works against the rightmost tree branch.
7134 status = ocfs2_find_path(inode, path, UINT_MAX);
7140 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7141 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7144 * By now, el will point to the extent list on the bottom most
7145 * portion of this tree. Only the tail record is considered in
7148 * We handle the following cases, in order:
7149 * - empty extent: delete the remaining branch
7150 * - remove the entire record
7151 * - remove a partial record
7152 * - no record needs to be removed (truncate has completed)
7154 el = path_leaf_el(path);
7155 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7156 ocfs2_error(inode->i_sb,
7157 "Inode %llu has empty extent block at %llu\n",
7158 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7159 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7164 i = le16_to_cpu(el->l_next_free_rec) - 1;
7165 range = le32_to_cpu(el->l_recs[i].e_cpos) +
7166 ocfs2_rec_clusters(el, &el->l_recs[i]);
7167 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7168 clusters_to_del = 0;
7169 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
7170 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
7171 } else if (range > new_highest_cpos) {
7172 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
7173 le32_to_cpu(el->l_recs[i].e_cpos)) -
7180 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7181 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7183 mutex_lock(&tl_inode->i_mutex);
7185 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7186 * record is free for use. If there isn't any, we flush to get
7187 * an empty truncate log. */
7188 if (ocfs2_truncate_log_needs_flush(osb)) {
7189 status = __ocfs2_flush_truncate_log(osb);
7196 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
7197 (struct ocfs2_dinode *)fe_bh->b_data,
7199 handle = ocfs2_start_trans(osb, credits);
7200 if (IS_ERR(handle)) {
7201 status = PTR_ERR(handle);
7207 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7214 mutex_unlock(&tl_inode->i_mutex);
7217 ocfs2_commit_trans(osb, handle);
7220 ocfs2_reinit_path(path, 1);
7223 * The check above will catch the case where we've truncated
7224 * away all allocation.
7230 ocfs2_schedule_truncate_log_flush(osb, 1);
7233 mutex_unlock(&tl_inode->i_mutex);
7236 ocfs2_commit_trans(osb, handle);
7238 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7240 ocfs2_free_path(path);
7242 /* This will drop the ext_alloc cluster lock for us */
7243 ocfs2_free_truncate_context(tc);
7250 * Expects the inode to already be locked.
7252 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7253 struct inode *inode,
7254 struct buffer_head *fe_bh,
7255 struct ocfs2_truncate_context **tc)
7258 unsigned int new_i_clusters;
7259 struct ocfs2_dinode *fe;
7260 struct ocfs2_extent_block *eb;
7261 struct buffer_head *last_eb_bh = NULL;
7267 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7268 i_size_read(inode));
7269 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7271 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7272 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7273 (unsigned long long)le64_to_cpu(fe->i_size));
7275 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7281 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7283 if (fe->id2.i_list.l_tree_depth) {
7284 status = ocfs2_read_extent_block(inode,
7285 le64_to_cpu(fe->i_last_eb_blk),
7291 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7294 (*tc)->tc_last_eb_bh = last_eb_bh;
7300 ocfs2_free_truncate_context(*tc);
7308 * 'start' is inclusive, 'end' is not.
7310 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7311 unsigned int start, unsigned int end, int trunc)
7314 unsigned int numbytes;
7316 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7317 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7318 struct ocfs2_inline_data *idata = &di->id2.i_data;
7320 if (end > i_size_read(inode))
7321 end = i_size_read(inode);
7323 BUG_ON(start >= end);
7325 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7326 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7327 !ocfs2_supports_inline_data(osb)) {
7328 ocfs2_error(inode->i_sb,
7329 "Inline data flags for inode %llu don't agree! "
7330 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7331 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7332 le16_to_cpu(di->i_dyn_features),
7333 OCFS2_I(inode)->ip_dyn_features,
7334 osb->s_feature_incompat);
7339 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7340 if (IS_ERR(handle)) {
7341 ret = PTR_ERR(handle);
7346 ret = ocfs2_journal_access_di(handle, inode, di_bh,
7347 OCFS2_JOURNAL_ACCESS_WRITE);
7353 numbytes = end - start;
7354 memset(idata->id_data + start, 0, numbytes);
7357 * No need to worry about the data page here - it's been
7358 * truncated already and inline data doesn't need it for
7359 * pushing zero's to disk, so we'll let readpage pick it up
7363 i_size_write(inode, start);
7364 di->i_size = cpu_to_le64(start);
7367 inode->i_blocks = ocfs2_inode_sector_count(inode);
7368 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7370 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7371 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7373 ocfs2_journal_dirty(handle, di_bh);
7376 ocfs2_commit_trans(osb, handle);
7382 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7385 * The caller is responsible for completing deallocation
7386 * before freeing the context.
7388 if (tc->tc_dealloc.c_first_suballocator != NULL)
7390 "Truncate completion has non-empty dealloc context\n");
7392 brelse(tc->tc_last_eb_bh);