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_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
300 struct ocfs2_dx_root_block *dx_root = et->et_object;
302 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
305 static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
307 struct ocfs2_dx_root_block *dx_root = et->et_object;
309 return le64_to_cpu(dx_root->dr_last_eb_blk);
312 static void ocfs2_dx_root_update_clusters(struct inode *inode,
313 struct ocfs2_extent_tree *et,
316 struct ocfs2_dx_root_block *dx_root = et->et_object;
318 le32_add_cpu(&dx_root->dr_clusters, clusters);
321 static int ocfs2_dx_root_sanity_check(struct inode *inode,
322 struct ocfs2_extent_tree *et)
324 struct ocfs2_dx_root_block *dx_root = et->et_object;
326 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
331 static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
333 struct ocfs2_dx_root_block *dx_root = et->et_object;
335 et->et_root_el = &dx_root->dr_list;
338 static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
339 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
340 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
341 .eo_update_clusters = ocfs2_dx_root_update_clusters,
342 .eo_sanity_check = ocfs2_dx_root_sanity_check,
343 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
346 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
348 struct buffer_head *bh,
349 ocfs2_journal_access_func access,
351 struct ocfs2_extent_tree_operations *ops)
355 et->et_ci = INODE_CACHE(inode);
356 et->et_root_journal_access = access;
358 obj = (void *)bh->b_data;
361 et->et_ops->eo_fill_root_el(et);
362 if (!et->et_ops->eo_fill_max_leaf_clusters)
363 et->et_max_leaf_clusters = 0;
365 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
368 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
370 struct buffer_head *bh)
372 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di,
373 NULL, &ocfs2_dinode_et_ops);
376 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
378 struct buffer_head *bh)
380 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb,
381 NULL, &ocfs2_xattr_tree_et_ops);
384 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
386 struct ocfs2_xattr_value_buf *vb)
388 __ocfs2_init_extent_tree(et, inode, vb->vb_bh, vb->vb_access, vb,
389 &ocfs2_xattr_value_et_ops);
392 void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
394 struct buffer_head *bh)
396 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_dr,
397 NULL, &ocfs2_dx_root_et_ops);
400 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
403 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
406 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
408 return et->et_ops->eo_get_last_eb_blk(et);
411 static inline void ocfs2_et_update_clusters(struct inode *inode,
412 struct ocfs2_extent_tree *et,
415 et->et_ops->eo_update_clusters(inode, et, clusters);
418 static inline int ocfs2_et_root_journal_access(handle_t *handle,
419 struct ocfs2_extent_tree *et,
422 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
426 static inline int ocfs2_et_insert_check(struct inode *inode,
427 struct ocfs2_extent_tree *et,
428 struct ocfs2_extent_rec *rec)
432 if (et->et_ops->eo_insert_check)
433 ret = et->et_ops->eo_insert_check(inode, et, rec);
437 static inline int ocfs2_et_sanity_check(struct inode *inode,
438 struct ocfs2_extent_tree *et)
442 if (et->et_ops->eo_sanity_check)
443 ret = et->et_ops->eo_sanity_check(inode, et);
447 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
448 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
449 struct ocfs2_extent_block *eb);
452 * Structures which describe a path through a btree, and functions to
455 * The idea here is to be as generic as possible with the tree
458 struct ocfs2_path_item {
459 struct buffer_head *bh;
460 struct ocfs2_extent_list *el;
463 #define OCFS2_MAX_PATH_DEPTH 5
467 ocfs2_journal_access_func p_root_access;
468 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
471 #define path_root_bh(_path) ((_path)->p_node[0].bh)
472 #define path_root_el(_path) ((_path)->p_node[0].el)
473 #define path_root_access(_path)((_path)->p_root_access)
474 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
475 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
476 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
478 static int ocfs2_find_path(struct ocfs2_caching_info *ci,
479 struct ocfs2_path *path, u32 cpos);
480 static void ocfs2_adjust_rightmost_records(struct inode *inode,
482 struct ocfs2_path *path,
483 struct ocfs2_extent_rec *insert_rec);
485 * Reset the actual path elements so that we can re-use the structure
486 * to build another path. Generally, this involves freeing the buffer
489 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
491 int i, start = 0, depth = 0;
492 struct ocfs2_path_item *node;
497 for(i = start; i < path_num_items(path); i++) {
498 node = &path->p_node[i];
506 * Tree depth may change during truncate, or insert. If we're
507 * keeping the root extent list, then make sure that our path
508 * structure reflects the proper depth.
511 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
513 path_root_access(path) = NULL;
515 path->p_tree_depth = depth;
518 static void ocfs2_free_path(struct ocfs2_path *path)
521 ocfs2_reinit_path(path, 0);
527 * All the elements of src into dest. After this call, src could be freed
528 * without affecting dest.
530 * Both paths should have the same root. Any non-root elements of dest
533 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
537 BUG_ON(path_root_bh(dest) != path_root_bh(src));
538 BUG_ON(path_root_el(dest) != path_root_el(src));
539 BUG_ON(path_root_access(dest) != path_root_access(src));
541 ocfs2_reinit_path(dest, 1);
543 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
544 dest->p_node[i].bh = src->p_node[i].bh;
545 dest->p_node[i].el = src->p_node[i].el;
547 if (dest->p_node[i].bh)
548 get_bh(dest->p_node[i].bh);
553 * Make the *dest path the same as src and re-initialize src path to
556 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
560 BUG_ON(path_root_bh(dest) != path_root_bh(src));
561 BUG_ON(path_root_access(dest) != path_root_access(src));
563 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
564 brelse(dest->p_node[i].bh);
566 dest->p_node[i].bh = src->p_node[i].bh;
567 dest->p_node[i].el = src->p_node[i].el;
569 src->p_node[i].bh = NULL;
570 src->p_node[i].el = NULL;
575 * Insert an extent block at given index.
577 * This will not take an additional reference on eb_bh.
579 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
580 struct buffer_head *eb_bh)
582 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
585 * Right now, no root bh is an extent block, so this helps
586 * catch code errors with dinode trees. The assertion can be
587 * safely removed if we ever need to insert extent block
588 * structures at the root.
592 path->p_node[index].bh = eb_bh;
593 path->p_node[index].el = &eb->h_list;
596 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
597 struct ocfs2_extent_list *root_el,
598 ocfs2_journal_access_func access)
600 struct ocfs2_path *path;
602 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
604 path = kzalloc(sizeof(*path), GFP_NOFS);
606 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
608 path_root_bh(path) = root_bh;
609 path_root_el(path) = root_el;
610 path_root_access(path) = access;
616 static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
618 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
619 path_root_access(path));
622 static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
624 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
625 et->et_root_journal_access);
629 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
630 * otherwise it's the root_access function.
632 * I don't like the way this function's name looks next to
633 * ocfs2_journal_access_path(), but I don't have a better one.
635 static int ocfs2_path_bh_journal_access(handle_t *handle,
636 struct ocfs2_caching_info *ci,
637 struct ocfs2_path *path,
640 ocfs2_journal_access_func access = path_root_access(path);
643 access = ocfs2_journal_access;
646 access = ocfs2_journal_access_eb;
648 return access(handle, ci, path->p_node[idx].bh,
649 OCFS2_JOURNAL_ACCESS_WRITE);
653 * Convenience function to journal all components in a path.
655 static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
657 struct ocfs2_path *path)
664 for(i = 0; i < path_num_items(path); i++) {
665 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
677 * Return the index of the extent record which contains cluster #v_cluster.
678 * -1 is returned if it was not found.
680 * Should work fine on interior and exterior nodes.
682 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
686 struct ocfs2_extent_rec *rec;
687 u32 rec_end, rec_start, clusters;
689 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
690 rec = &el->l_recs[i];
692 rec_start = le32_to_cpu(rec->e_cpos);
693 clusters = ocfs2_rec_clusters(el, rec);
695 rec_end = rec_start + clusters;
697 if (v_cluster >= rec_start && v_cluster < rec_end) {
706 enum ocfs2_contig_type {
715 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
716 * ocfs2_extent_contig only work properly against leaf nodes!
718 static int ocfs2_block_extent_contig(struct super_block *sb,
719 struct ocfs2_extent_rec *ext,
722 u64 blk_end = le64_to_cpu(ext->e_blkno);
724 blk_end += ocfs2_clusters_to_blocks(sb,
725 le16_to_cpu(ext->e_leaf_clusters));
727 return blkno == blk_end;
730 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
731 struct ocfs2_extent_rec *right)
735 left_range = le32_to_cpu(left->e_cpos) +
736 le16_to_cpu(left->e_leaf_clusters);
738 return (left_range == le32_to_cpu(right->e_cpos));
741 static enum ocfs2_contig_type
742 ocfs2_extent_contig(struct inode *inode,
743 struct ocfs2_extent_rec *ext,
744 struct ocfs2_extent_rec *insert_rec)
746 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
749 * Refuse to coalesce extent records with different flag
750 * fields - we don't want to mix unwritten extents with user
753 if (ext->e_flags != insert_rec->e_flags)
756 if (ocfs2_extents_adjacent(ext, insert_rec) &&
757 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
760 blkno = le64_to_cpu(ext->e_blkno);
761 if (ocfs2_extents_adjacent(insert_rec, ext) &&
762 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
769 * NOTE: We can have pretty much any combination of contiguousness and
772 * The usefulness of APPEND_TAIL is more in that it lets us know that
773 * we'll have to update the path to that leaf.
775 enum ocfs2_append_type {
780 enum ocfs2_split_type {
786 struct ocfs2_insert_type {
787 enum ocfs2_split_type ins_split;
788 enum ocfs2_append_type ins_appending;
789 enum ocfs2_contig_type ins_contig;
790 int ins_contig_index;
794 struct ocfs2_merge_ctxt {
795 enum ocfs2_contig_type c_contig_type;
796 int c_has_empty_extent;
797 int c_split_covers_rec;
800 static int ocfs2_validate_extent_block(struct super_block *sb,
801 struct buffer_head *bh)
804 struct ocfs2_extent_block *eb =
805 (struct ocfs2_extent_block *)bh->b_data;
807 mlog(0, "Validating extent block %llu\n",
808 (unsigned long long)bh->b_blocknr);
810 BUG_ON(!buffer_uptodate(bh));
813 * If the ecc fails, we return the error but otherwise
814 * leave the filesystem running. We know any error is
815 * local to this block.
817 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
819 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
820 (unsigned long long)bh->b_blocknr);
825 * Errors after here are fatal.
828 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
830 "Extent block #%llu has bad signature %.*s",
831 (unsigned long long)bh->b_blocknr, 7,
836 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
838 "Extent block #%llu has an invalid h_blkno "
840 (unsigned long long)bh->b_blocknr,
841 (unsigned long long)le64_to_cpu(eb->h_blkno));
845 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
847 "Extent block #%llu has an invalid "
848 "h_fs_generation of #%u",
849 (unsigned long long)bh->b_blocknr,
850 le32_to_cpu(eb->h_fs_generation));
857 int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
858 struct buffer_head **bh)
861 struct buffer_head *tmp = *bh;
863 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
864 ocfs2_validate_extent_block);
866 /* If ocfs2_read_block() got us a new bh, pass it up. */
875 * How many free extents have we got before we need more meta data?
877 int ocfs2_num_free_extents(struct ocfs2_super *osb,
878 struct ocfs2_extent_tree *et)
881 struct ocfs2_extent_list *el = NULL;
882 struct ocfs2_extent_block *eb;
883 struct buffer_head *eb_bh = NULL;
889 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
892 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
898 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
902 BUG_ON(el->l_tree_depth != 0);
904 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
912 /* expects array to already be allocated
914 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
917 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
921 struct ocfs2_alloc_context *meta_ac,
922 struct buffer_head *bhs[])
924 int count, status, i;
925 u16 suballoc_bit_start;
928 struct ocfs2_extent_block *eb;
933 while (count < wanted) {
934 status = ocfs2_claim_metadata(osb,
946 for(i = count; i < (num_got + count); i++) {
947 bhs[i] = sb_getblk(osb->sb, first_blkno);
948 if (bhs[i] == NULL) {
953 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
956 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), bhs[i],
957 OCFS2_JOURNAL_ACCESS_CREATE);
963 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
964 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
965 /* Ok, setup the minimal stuff here. */
966 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
967 eb->h_blkno = cpu_to_le64(first_blkno);
968 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
969 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
970 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
972 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
974 suballoc_bit_start++;
977 /* We'll also be dirtied by the caller, so
978 * this isn't absolutely necessary. */
979 status = ocfs2_journal_dirty(handle, bhs[i]);
992 for(i = 0; i < wanted; i++) {
1002 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1004 * Returns the sum of the rightmost extent rec logical offset and
1007 * ocfs2_add_branch() uses this to determine what logical cluster
1008 * value should be populated into the leftmost new branch records.
1010 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1011 * value for the new topmost tree record.
1013 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1017 i = le16_to_cpu(el->l_next_free_rec) - 1;
1019 return le32_to_cpu(el->l_recs[i].e_cpos) +
1020 ocfs2_rec_clusters(el, &el->l_recs[i]);
1024 * Change range of the branches in the right most path according to the leaf
1025 * extent block's rightmost record.
1027 static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1028 struct inode *inode,
1029 struct ocfs2_extent_tree *et)
1032 struct ocfs2_path *path = NULL;
1033 struct ocfs2_extent_list *el;
1034 struct ocfs2_extent_rec *rec;
1036 path = ocfs2_new_path_from_et(et);
1042 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
1048 status = ocfs2_extend_trans(handle, path_num_items(path) +
1049 handle->h_buffer_credits);
1055 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
1061 el = path_leaf_el(path);
1062 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1064 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
1067 ocfs2_free_path(path);
1072 * Add an entire tree branch to our inode. eb_bh is the extent block
1073 * to start at, if we don't want to start the branch at the dinode
1076 * last_eb_bh is required as we have to update it's next_leaf pointer
1077 * for the new last extent block.
1079 * the new branch will be 'empty' in the sense that every block will
1080 * contain a single record with cluster count == 0.
1082 static int ocfs2_add_branch(struct ocfs2_super *osb,
1084 struct inode *inode,
1085 struct ocfs2_extent_tree *et,
1086 struct buffer_head *eb_bh,
1087 struct buffer_head **last_eb_bh,
1088 struct ocfs2_alloc_context *meta_ac)
1090 int status, new_blocks, i;
1091 u64 next_blkno, new_last_eb_blk;
1092 struct buffer_head *bh;
1093 struct buffer_head **new_eb_bhs = NULL;
1094 struct ocfs2_extent_block *eb;
1095 struct ocfs2_extent_list *eb_el;
1096 struct ocfs2_extent_list *el;
1097 u32 new_cpos, root_end;
1101 BUG_ON(!last_eb_bh || !*last_eb_bh);
1104 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1107 el = et->et_root_el;
1109 /* we never add a branch to a leaf. */
1110 BUG_ON(!el->l_tree_depth);
1112 new_blocks = le16_to_cpu(el->l_tree_depth);
1114 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1115 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1116 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1119 * If there is a gap before the root end and the real end
1120 * of the righmost leaf block, we need to remove the gap
1121 * between new_cpos and root_end first so that the tree
1122 * is consistent after we add a new branch(it will start
1125 if (root_end > new_cpos) {
1126 mlog(0, "adjust the cluster end from %u to %u\n",
1127 root_end, new_cpos);
1128 status = ocfs2_adjust_rightmost_branch(handle, inode, et);
1135 /* allocate the number of new eb blocks we need */
1136 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1144 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
1145 meta_ac, new_eb_bhs);
1151 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1152 * linked with the rest of the tree.
1153 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1155 * when we leave the loop, new_last_eb_blk will point to the
1156 * newest leaf, and next_blkno will point to the topmost extent
1158 next_blkno = new_last_eb_blk = 0;
1159 for(i = 0; i < new_blocks; i++) {
1161 eb = (struct ocfs2_extent_block *) bh->b_data;
1162 /* ocfs2_create_new_meta_bhs() should create it right! */
1163 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1164 eb_el = &eb->h_list;
1166 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), bh,
1167 OCFS2_JOURNAL_ACCESS_CREATE);
1173 eb->h_next_leaf_blk = 0;
1174 eb_el->l_tree_depth = cpu_to_le16(i);
1175 eb_el->l_next_free_rec = cpu_to_le16(1);
1177 * This actually counts as an empty extent as
1180 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
1181 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
1183 * eb_el isn't always an interior node, but even leaf
1184 * nodes want a zero'd flags and reserved field so
1185 * this gets the whole 32 bits regardless of use.
1187 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
1188 if (!eb_el->l_tree_depth)
1189 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1191 status = ocfs2_journal_dirty(handle, bh);
1197 next_blkno = le64_to_cpu(eb->h_blkno);
1200 /* This is a bit hairy. We want to update up to three blocks
1201 * here without leaving any of them in an inconsistent state
1202 * in case of error. We don't have to worry about
1203 * journal_dirty erroring as it won't unless we've aborted the
1204 * handle (in which case we would never be here) so reserving
1205 * the write with journal_access is all we need to do. */
1206 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), *last_eb_bh,
1207 OCFS2_JOURNAL_ACCESS_WRITE);
1212 status = ocfs2_et_root_journal_access(handle, et,
1213 OCFS2_JOURNAL_ACCESS_WRITE);
1219 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), eb_bh,
1220 OCFS2_JOURNAL_ACCESS_WRITE);
1227 /* Link the new branch into the rest of the tree (el will
1228 * either be on the root_bh, or the extent block passed in. */
1229 i = le16_to_cpu(el->l_next_free_rec);
1230 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1231 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1232 el->l_recs[i].e_int_clusters = 0;
1233 le16_add_cpu(&el->l_next_free_rec, 1);
1235 /* fe needs a new last extent block pointer, as does the
1236 * next_leaf on the previously last-extent-block. */
1237 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1239 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1240 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1242 status = ocfs2_journal_dirty(handle, *last_eb_bh);
1245 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1249 status = ocfs2_journal_dirty(handle, eb_bh);
1255 * Some callers want to track the rightmost leaf so pass it
1258 brelse(*last_eb_bh);
1259 get_bh(new_eb_bhs[0]);
1260 *last_eb_bh = new_eb_bhs[0];
1265 for (i = 0; i < new_blocks; i++)
1266 brelse(new_eb_bhs[i]);
1275 * adds another level to the allocation tree.
1276 * returns back the new extent block so you can add a branch to it
1279 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
1281 struct inode *inode,
1282 struct ocfs2_extent_tree *et,
1283 struct ocfs2_alloc_context *meta_ac,
1284 struct buffer_head **ret_new_eb_bh)
1288 struct buffer_head *new_eb_bh = NULL;
1289 struct ocfs2_extent_block *eb;
1290 struct ocfs2_extent_list *root_el;
1291 struct ocfs2_extent_list *eb_el;
1295 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1302 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1303 /* ocfs2_create_new_meta_bhs() should create it right! */
1304 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1306 eb_el = &eb->h_list;
1307 root_el = et->et_root_el;
1309 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), new_eb_bh,
1310 OCFS2_JOURNAL_ACCESS_CREATE);
1316 /* copy the root extent list data into the new extent block */
1317 eb_el->l_tree_depth = root_el->l_tree_depth;
1318 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1319 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1320 eb_el->l_recs[i] = root_el->l_recs[i];
1322 status = ocfs2_journal_dirty(handle, new_eb_bh);
1328 status = ocfs2_et_root_journal_access(handle, et,
1329 OCFS2_JOURNAL_ACCESS_WRITE);
1335 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1337 /* update root_bh now */
1338 le16_add_cpu(&root_el->l_tree_depth, 1);
1339 root_el->l_recs[0].e_cpos = 0;
1340 root_el->l_recs[0].e_blkno = eb->h_blkno;
1341 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1342 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1343 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1344 root_el->l_next_free_rec = cpu_to_le16(1);
1346 /* If this is our 1st tree depth shift, then last_eb_blk
1347 * becomes the allocated extent block */
1348 if (root_el->l_tree_depth == cpu_to_le16(1))
1349 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1351 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1357 *ret_new_eb_bh = new_eb_bh;
1368 * Should only be called when there is no space left in any of the
1369 * leaf nodes. What we want to do is find the lowest tree depth
1370 * non-leaf extent block with room for new records. There are three
1371 * valid results of this search:
1373 * 1) a lowest extent block is found, then we pass it back in
1374 * *lowest_eb_bh and return '0'
1376 * 2) the search fails to find anything, but the root_el has room. We
1377 * pass NULL back in *lowest_eb_bh, but still return '0'
1379 * 3) the search fails to find anything AND the root_el is full, in
1380 * which case we return > 0
1382 * return status < 0 indicates an error.
1384 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1385 struct ocfs2_extent_tree *et,
1386 struct buffer_head **target_bh)
1390 struct ocfs2_extent_block *eb;
1391 struct ocfs2_extent_list *el;
1392 struct buffer_head *bh = NULL;
1393 struct buffer_head *lowest_bh = NULL;
1399 el = et->et_root_el;
1401 while(le16_to_cpu(el->l_tree_depth) > 1) {
1402 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1403 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1404 "Owner %llu has empty "
1405 "extent list (next_free_rec == 0)",
1406 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
1410 i = le16_to_cpu(el->l_next_free_rec) - 1;
1411 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1413 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1414 "Owner %llu has extent "
1415 "list where extent # %d has no physical "
1417 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
1425 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
1431 eb = (struct ocfs2_extent_block *) bh->b_data;
1434 if (le16_to_cpu(el->l_next_free_rec) <
1435 le16_to_cpu(el->l_count)) {
1442 /* If we didn't find one and the fe doesn't have any room,
1443 * then return '1' */
1444 el = et->et_root_el;
1445 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1448 *target_bh = lowest_bh;
1457 * Grow a b-tree so that it has more records.
1459 * We might shift the tree depth in which case existing paths should
1460 * be considered invalid.
1462 * Tree depth after the grow is returned via *final_depth.
1464 * *last_eb_bh will be updated by ocfs2_add_branch().
1466 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
1467 struct ocfs2_extent_tree *et, int *final_depth,
1468 struct buffer_head **last_eb_bh,
1469 struct ocfs2_alloc_context *meta_ac)
1472 struct ocfs2_extent_list *el = et->et_root_el;
1473 int depth = le16_to_cpu(el->l_tree_depth);
1474 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1475 struct buffer_head *bh = NULL;
1477 BUG_ON(meta_ac == NULL);
1479 shift = ocfs2_find_branch_target(osb, et, &bh);
1486 /* We traveled all the way to the bottom of the allocation tree
1487 * and didn't find room for any more extents - we need to add
1488 * another tree level */
1491 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1493 /* ocfs2_shift_tree_depth will return us a buffer with
1494 * the new extent block (so we can pass that to
1495 * ocfs2_add_branch). */
1496 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
1505 * Special case: we have room now if we shifted from
1506 * tree_depth 0, so no more work needs to be done.
1508 * We won't be calling add_branch, so pass
1509 * back *last_eb_bh as the new leaf. At depth
1510 * zero, it should always be null so there's
1511 * no reason to brelse.
1513 BUG_ON(*last_eb_bh);
1520 /* call ocfs2_add_branch to add the final part of the tree with
1522 mlog(0, "add branch. bh = %p\n", bh);
1523 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
1532 *final_depth = depth;
1538 * This function will discard the rightmost extent record.
1540 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1542 int next_free = le16_to_cpu(el->l_next_free_rec);
1543 int count = le16_to_cpu(el->l_count);
1544 unsigned int num_bytes;
1547 /* This will cause us to go off the end of our extent list. */
1548 BUG_ON(next_free >= count);
1550 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1552 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1555 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1556 struct ocfs2_extent_rec *insert_rec)
1558 int i, insert_index, next_free, has_empty, num_bytes;
1559 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1560 struct ocfs2_extent_rec *rec;
1562 next_free = le16_to_cpu(el->l_next_free_rec);
1563 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1567 /* The tree code before us didn't allow enough room in the leaf. */
1568 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1571 * The easiest way to approach this is to just remove the
1572 * empty extent and temporarily decrement next_free.
1576 * If next_free was 1 (only an empty extent), this
1577 * loop won't execute, which is fine. We still want
1578 * the decrement above to happen.
1580 for(i = 0; i < (next_free - 1); i++)
1581 el->l_recs[i] = el->l_recs[i+1];
1587 * Figure out what the new record index should be.
1589 for(i = 0; i < next_free; i++) {
1590 rec = &el->l_recs[i];
1592 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1597 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1598 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1600 BUG_ON(insert_index < 0);
1601 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1602 BUG_ON(insert_index > next_free);
1605 * No need to memmove if we're just adding to the tail.
1607 if (insert_index != next_free) {
1608 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1610 num_bytes = next_free - insert_index;
1611 num_bytes *= sizeof(struct ocfs2_extent_rec);
1612 memmove(&el->l_recs[insert_index + 1],
1613 &el->l_recs[insert_index],
1618 * Either we had an empty extent, and need to re-increment or
1619 * there was no empty extent on a non full rightmost leaf node,
1620 * in which case we still need to increment.
1623 el->l_next_free_rec = cpu_to_le16(next_free);
1625 * Make sure none of the math above just messed up our tree.
1627 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1629 el->l_recs[insert_index] = *insert_rec;
1633 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1635 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1637 BUG_ON(num_recs == 0);
1639 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1641 size = num_recs * sizeof(struct ocfs2_extent_rec);
1642 memmove(&el->l_recs[0], &el->l_recs[1], size);
1643 memset(&el->l_recs[num_recs], 0,
1644 sizeof(struct ocfs2_extent_rec));
1645 el->l_next_free_rec = cpu_to_le16(num_recs);
1650 * Create an empty extent record .
1652 * l_next_free_rec may be updated.
1654 * If an empty extent already exists do nothing.
1656 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1658 int next_free = le16_to_cpu(el->l_next_free_rec);
1660 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1665 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1668 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1669 "Asked to create an empty extent in a full list:\n"
1670 "count = %u, tree depth = %u",
1671 le16_to_cpu(el->l_count),
1672 le16_to_cpu(el->l_tree_depth));
1674 ocfs2_shift_records_right(el);
1677 le16_add_cpu(&el->l_next_free_rec, 1);
1678 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1682 * For a rotation which involves two leaf nodes, the "root node" is
1683 * the lowest level tree node which contains a path to both leafs. This
1684 * resulting set of information can be used to form a complete "subtree"
1686 * This function is passed two full paths from the dinode down to a
1687 * pair of adjacent leaves. It's task is to figure out which path
1688 * index contains the subtree root - this can be the root index itself
1689 * in a worst-case rotation.
1691 * The array index of the subtree root is passed back.
1693 static int ocfs2_find_subtree_root(struct inode *inode,
1694 struct ocfs2_path *left,
1695 struct ocfs2_path *right)
1700 * Check that the caller passed in two paths from the same tree.
1702 BUG_ON(path_root_bh(left) != path_root_bh(right));
1708 * The caller didn't pass two adjacent paths.
1710 mlog_bug_on_msg(i > left->p_tree_depth,
1711 "Inode %lu, left depth %u, right depth %u\n"
1712 "left leaf blk %llu, right leaf blk %llu\n",
1713 inode->i_ino, left->p_tree_depth,
1714 right->p_tree_depth,
1715 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1716 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1717 } while (left->p_node[i].bh->b_blocknr ==
1718 right->p_node[i].bh->b_blocknr);
1723 typedef void (path_insert_t)(void *, struct buffer_head *);
1726 * Traverse a btree path in search of cpos, starting at root_el.
1728 * This code can be called with a cpos larger than the tree, in which
1729 * case it will return the rightmost path.
1731 static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
1732 struct ocfs2_extent_list *root_el, u32 cpos,
1733 path_insert_t *func, void *data)
1738 struct buffer_head *bh = NULL;
1739 struct ocfs2_extent_block *eb;
1740 struct ocfs2_extent_list *el;
1741 struct ocfs2_extent_rec *rec;
1744 while (el->l_tree_depth) {
1745 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1746 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1747 "Owner %llu has empty extent list at "
1749 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1750 le16_to_cpu(el->l_tree_depth));
1756 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1757 rec = &el->l_recs[i];
1760 * In the case that cpos is off the allocation
1761 * tree, this should just wind up returning the
1764 range = le32_to_cpu(rec->e_cpos) +
1765 ocfs2_rec_clusters(el, rec);
1766 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1770 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1772 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1773 "Owner %llu has bad blkno in extent list "
1774 "at depth %u (index %d)\n",
1775 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1776 le16_to_cpu(el->l_tree_depth), i);
1783 ret = ocfs2_read_extent_block(ci, blkno, &bh);
1789 eb = (struct ocfs2_extent_block *) bh->b_data;
1792 if (le16_to_cpu(el->l_next_free_rec) >
1793 le16_to_cpu(el->l_count)) {
1794 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1795 "Owner %llu has bad count in extent list "
1796 "at block %llu (next free=%u, count=%u)\n",
1797 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1798 (unsigned long long)bh->b_blocknr,
1799 le16_to_cpu(el->l_next_free_rec),
1800 le16_to_cpu(el->l_count));
1811 * Catch any trailing bh that the loop didn't handle.
1819 * Given an initialized path (that is, it has a valid root extent
1820 * list), this function will traverse the btree in search of the path
1821 * which would contain cpos.
1823 * The path traveled is recorded in the path structure.
1825 * Note that this will not do any comparisons on leaf node extent
1826 * records, so it will work fine in the case that we just added a tree
1829 struct find_path_data {
1831 struct ocfs2_path *path;
1833 static void find_path_ins(void *data, struct buffer_head *bh)
1835 struct find_path_data *fp = data;
1838 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1841 static int ocfs2_find_path(struct ocfs2_caching_info *ci,
1842 struct ocfs2_path *path, u32 cpos)
1844 struct find_path_data data;
1848 return __ocfs2_find_path(ci, path_root_el(path), cpos,
1849 find_path_ins, &data);
1852 static void find_leaf_ins(void *data, struct buffer_head *bh)
1854 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1855 struct ocfs2_extent_list *el = &eb->h_list;
1856 struct buffer_head **ret = data;
1858 /* We want to retain only the leaf block. */
1859 if (le16_to_cpu(el->l_tree_depth) == 0) {
1865 * Find the leaf block in the tree which would contain cpos. No
1866 * checking of the actual leaf is done.
1868 * Some paths want to call this instead of allocating a path structure
1869 * and calling ocfs2_find_path().
1871 * This function doesn't handle non btree extent lists.
1873 int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1874 struct ocfs2_extent_list *root_el, u32 cpos,
1875 struct buffer_head **leaf_bh)
1878 struct buffer_head *bh = NULL;
1880 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
1892 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1894 * Basically, we've moved stuff around at the bottom of the tree and
1895 * we need to fix up the extent records above the changes to reflect
1898 * left_rec: the record on the left.
1899 * left_child_el: is the child list pointed to by left_rec
1900 * right_rec: the record to the right of left_rec
1901 * right_child_el: is the child list pointed to by right_rec
1903 * By definition, this only works on interior nodes.
1905 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1906 struct ocfs2_extent_list *left_child_el,
1907 struct ocfs2_extent_rec *right_rec,
1908 struct ocfs2_extent_list *right_child_el)
1910 u32 left_clusters, right_end;
1913 * Interior nodes never have holes. Their cpos is the cpos of
1914 * the leftmost record in their child list. Their cluster
1915 * count covers the full theoretical range of their child list
1916 * - the range between their cpos and the cpos of the record
1917 * immediately to their right.
1919 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1920 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1921 BUG_ON(right_child_el->l_tree_depth);
1922 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1923 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1925 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1926 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1929 * Calculate the rightmost cluster count boundary before
1930 * moving cpos - we will need to adjust clusters after
1931 * updating e_cpos to keep the same highest cluster count.
1933 right_end = le32_to_cpu(right_rec->e_cpos);
1934 right_end += le32_to_cpu(right_rec->e_int_clusters);
1936 right_rec->e_cpos = left_rec->e_cpos;
1937 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1939 right_end -= le32_to_cpu(right_rec->e_cpos);
1940 right_rec->e_int_clusters = cpu_to_le32(right_end);
1944 * Adjust the adjacent root node records involved in a
1945 * rotation. left_el_blkno is passed in as a key so that we can easily
1946 * find it's index in the root list.
1948 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1949 struct ocfs2_extent_list *left_el,
1950 struct ocfs2_extent_list *right_el,
1955 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1956 le16_to_cpu(left_el->l_tree_depth));
1958 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1959 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1964 * The path walking code should have never returned a root and
1965 * two paths which are not adjacent.
1967 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1969 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1970 &root_el->l_recs[i + 1], right_el);
1974 * We've changed a leaf block (in right_path) and need to reflect that
1975 * change back up the subtree.
1977 * This happens in multiple places:
1978 * - When we've moved an extent record from the left path leaf to the right
1979 * path leaf to make room for an empty extent in the left path leaf.
1980 * - When our insert into the right path leaf is at the leftmost edge
1981 * and requires an update of the path immediately to it's left. This
1982 * can occur at the end of some types of rotation and appending inserts.
1983 * - When we've adjusted the last extent record in the left path leaf and the
1984 * 1st extent record in the right path leaf during cross extent block merge.
1986 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1987 struct ocfs2_path *left_path,
1988 struct ocfs2_path *right_path,
1992 struct ocfs2_extent_list *el, *left_el, *right_el;
1993 struct ocfs2_extent_rec *left_rec, *right_rec;
1994 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1997 * Update the counts and position values within all the
1998 * interior nodes to reflect the leaf rotation we just did.
2000 * The root node is handled below the loop.
2002 * We begin the loop with right_el and left_el pointing to the
2003 * leaf lists and work our way up.
2005 * NOTE: within this loop, left_el and right_el always refer
2006 * to the *child* lists.
2008 left_el = path_leaf_el(left_path);
2009 right_el = path_leaf_el(right_path);
2010 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2011 mlog(0, "Adjust records at index %u\n", i);
2014 * One nice property of knowing that all of these
2015 * nodes are below the root is that we only deal with
2016 * the leftmost right node record and the rightmost
2019 el = left_path->p_node[i].el;
2020 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2021 left_rec = &el->l_recs[idx];
2023 el = right_path->p_node[i].el;
2024 right_rec = &el->l_recs[0];
2026 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2029 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2033 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2038 * Setup our list pointers now so that the current
2039 * parents become children in the next iteration.
2041 left_el = left_path->p_node[i].el;
2042 right_el = right_path->p_node[i].el;
2046 * At the root node, adjust the two adjacent records which
2047 * begin our path to the leaves.
2050 el = left_path->p_node[subtree_index].el;
2051 left_el = left_path->p_node[subtree_index + 1].el;
2052 right_el = right_path->p_node[subtree_index + 1].el;
2054 ocfs2_adjust_root_records(el, left_el, right_el,
2055 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2057 root_bh = left_path->p_node[subtree_index].bh;
2059 ret = ocfs2_journal_dirty(handle, root_bh);
2064 static int ocfs2_rotate_subtree_right(struct inode *inode,
2066 struct ocfs2_path *left_path,
2067 struct ocfs2_path *right_path,
2071 struct buffer_head *right_leaf_bh;
2072 struct buffer_head *left_leaf_bh = NULL;
2073 struct buffer_head *root_bh;
2074 struct ocfs2_extent_list *right_el, *left_el;
2075 struct ocfs2_extent_rec move_rec;
2077 left_leaf_bh = path_leaf_bh(left_path);
2078 left_el = path_leaf_el(left_path);
2080 if (left_el->l_next_free_rec != left_el->l_count) {
2081 ocfs2_error(inode->i_sb,
2082 "Inode %llu has non-full interior leaf node %llu"
2084 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2085 (unsigned long long)left_leaf_bh->b_blocknr,
2086 le16_to_cpu(left_el->l_next_free_rec));
2091 * This extent block may already have an empty record, so we
2092 * return early if so.
2094 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2097 root_bh = left_path->p_node[subtree_index].bh;
2098 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2100 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
2107 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2108 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
2115 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
2123 right_leaf_bh = path_leaf_bh(right_path);
2124 right_el = path_leaf_el(right_path);
2126 /* This is a code error, not a disk corruption. */
2127 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2128 "because rightmost leaf block %llu is empty\n",
2129 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2130 (unsigned long long)right_leaf_bh->b_blocknr);
2132 ocfs2_create_empty_extent(right_el);
2134 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2140 /* Do the copy now. */
2141 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2142 move_rec = left_el->l_recs[i];
2143 right_el->l_recs[0] = move_rec;
2146 * Clear out the record we just copied and shift everything
2147 * over, leaving an empty extent in the left leaf.
2149 * We temporarily subtract from next_free_rec so that the
2150 * shift will lose the tail record (which is now defunct).
2152 le16_add_cpu(&left_el->l_next_free_rec, -1);
2153 ocfs2_shift_records_right(left_el);
2154 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2155 le16_add_cpu(&left_el->l_next_free_rec, 1);
2157 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2163 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2171 * Given a full path, determine what cpos value would return us a path
2172 * containing the leaf immediately to the left of the current one.
2174 * Will return zero if the path passed in is already the leftmost path.
2176 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2177 struct ocfs2_path *path, u32 *cpos)
2181 struct ocfs2_extent_list *el;
2183 BUG_ON(path->p_tree_depth == 0);
2187 blkno = path_leaf_bh(path)->b_blocknr;
2189 /* Start at the tree node just above the leaf and work our way up. */
2190 i = path->p_tree_depth - 1;
2192 el = path->p_node[i].el;
2195 * Find the extent record just before the one in our
2198 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2199 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2203 * We've determined that the
2204 * path specified is already
2205 * the leftmost one - return a
2211 * The leftmost record points to our
2212 * leaf - we need to travel up the
2218 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2219 *cpos = *cpos + ocfs2_rec_clusters(el,
2220 &el->l_recs[j - 1]);
2227 * If we got here, we never found a valid node where
2228 * the tree indicated one should be.
2231 "Invalid extent tree at extent block %llu\n",
2232 (unsigned long long)blkno);
2237 blkno = path->p_node[i].bh->b_blocknr;
2246 * Extend the transaction by enough credits to complete the rotation,
2247 * and still leave at least the original number of credits allocated
2248 * to this transaction.
2250 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2252 struct ocfs2_path *path)
2254 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2256 if (handle->h_buffer_credits < credits)
2257 return ocfs2_extend_trans(handle, credits);
2263 * Trap the case where we're inserting into the theoretical range past
2264 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2265 * whose cpos is less than ours into the right leaf.
2267 * It's only necessary to look at the rightmost record of the left
2268 * leaf because the logic that calls us should ensure that the
2269 * theoretical ranges in the path components above the leaves are
2272 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2275 struct ocfs2_extent_list *left_el;
2276 struct ocfs2_extent_rec *rec;
2279 left_el = path_leaf_el(left_path);
2280 next_free = le16_to_cpu(left_el->l_next_free_rec);
2281 rec = &left_el->l_recs[next_free - 1];
2283 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2288 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2290 int next_free = le16_to_cpu(el->l_next_free_rec);
2292 struct ocfs2_extent_rec *rec;
2297 rec = &el->l_recs[0];
2298 if (ocfs2_is_empty_extent(rec)) {
2302 rec = &el->l_recs[1];
2305 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2306 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2312 * Rotate all the records in a btree right one record, starting at insert_cpos.
2314 * The path to the rightmost leaf should be passed in.
2316 * The array is assumed to be large enough to hold an entire path (tree depth).
2318 * Upon succesful return from this function:
2320 * - The 'right_path' array will contain a path to the leaf block
2321 * whose range contains e_cpos.
2322 * - That leaf block will have a single empty extent in list index 0.
2323 * - In the case that the rotation requires a post-insert update,
2324 * *ret_left_path will contain a valid path which can be passed to
2325 * ocfs2_insert_path().
2327 static int ocfs2_rotate_tree_right(struct inode *inode,
2329 enum ocfs2_split_type split,
2331 struct ocfs2_path *right_path,
2332 struct ocfs2_path **ret_left_path)
2334 int ret, start, orig_credits = handle->h_buffer_credits;
2336 struct ocfs2_path *left_path = NULL;
2338 *ret_left_path = NULL;
2340 left_path = ocfs2_new_path_from_path(right_path);
2347 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2353 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2356 * What we want to do here is:
2358 * 1) Start with the rightmost path.
2360 * 2) Determine a path to the leaf block directly to the left
2363 * 3) Determine the 'subtree root' - the lowest level tree node
2364 * which contains a path to both leaves.
2366 * 4) Rotate the subtree.
2368 * 5) Find the next subtree by considering the left path to be
2369 * the new right path.
2371 * The check at the top of this while loop also accepts
2372 * insert_cpos == cpos because cpos is only a _theoretical_
2373 * value to get us the left path - insert_cpos might very well
2374 * be filling that hole.
2376 * Stop at a cpos of '0' because we either started at the
2377 * leftmost branch (i.e., a tree with one branch and a
2378 * rotation inside of it), or we've gone as far as we can in
2379 * rotating subtrees.
2381 while (cpos && insert_cpos <= cpos) {
2382 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2385 ret = ocfs2_find_path(INODE_CACHE(inode), left_path, cpos);
2391 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2392 path_leaf_bh(right_path),
2393 "Inode %lu: error during insert of %u "
2394 "(left path cpos %u) results in two identical "
2395 "paths ending at %llu\n",
2396 inode->i_ino, insert_cpos, cpos,
2397 (unsigned long long)
2398 path_leaf_bh(left_path)->b_blocknr);
2400 if (split == SPLIT_NONE &&
2401 ocfs2_rotate_requires_path_adjustment(left_path,
2405 * We've rotated the tree as much as we
2406 * should. The rest is up to
2407 * ocfs2_insert_path() to complete, after the
2408 * record insertion. We indicate this
2409 * situation by returning the left path.
2411 * The reason we don't adjust the records here
2412 * before the record insert is that an error
2413 * later might break the rule where a parent
2414 * record e_cpos will reflect the actual
2415 * e_cpos of the 1st nonempty record of the
2418 *ret_left_path = left_path;
2422 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2424 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2426 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2427 right_path->p_tree_depth);
2429 ret = ocfs2_extend_rotate_transaction(handle, start,
2430 orig_credits, right_path);
2436 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2443 if (split != SPLIT_NONE &&
2444 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2447 * A rotate moves the rightmost left leaf
2448 * record over to the leftmost right leaf
2449 * slot. If we're doing an extent split
2450 * instead of a real insert, then we have to
2451 * check that the extent to be split wasn't
2452 * just moved over. If it was, then we can
2453 * exit here, passing left_path back -
2454 * ocfs2_split_extent() is smart enough to
2455 * search both leaves.
2457 *ret_left_path = left_path;
2462 * There is no need to re-read the next right path
2463 * as we know that it'll be our current left
2464 * path. Optimize by copying values instead.
2466 ocfs2_mv_path(right_path, left_path);
2468 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2477 ocfs2_free_path(left_path);
2483 static int ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2484 int subtree_index, struct ocfs2_path *path)
2487 struct ocfs2_extent_rec *rec;
2488 struct ocfs2_extent_list *el;
2489 struct ocfs2_extent_block *eb;
2493 * In normal tree rotation process, we will never touch the
2494 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2495 * doesn't reserve the credits for them either.
2497 * But we do have a special case here which will update the rightmost
2498 * records for all the bh in the path.
2499 * So we have to allocate extra credits and access them.
2501 ret = ocfs2_extend_trans(handle,
2502 handle->h_buffer_credits + subtree_index);
2508 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
2514 /* Path should always be rightmost. */
2515 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2516 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2519 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2520 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2521 rec = &el->l_recs[idx];
2522 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2524 for (i = 0; i < path->p_tree_depth; i++) {
2525 el = path->p_node[i].el;
2526 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2527 rec = &el->l_recs[idx];
2529 rec->e_int_clusters = cpu_to_le32(range);
2530 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2532 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2538 static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2539 struct ocfs2_cached_dealloc_ctxt *dealloc,
2540 struct ocfs2_path *path, int unlink_start)
2543 struct ocfs2_extent_block *eb;
2544 struct ocfs2_extent_list *el;
2545 struct buffer_head *bh;
2547 for(i = unlink_start; i < path_num_items(path); i++) {
2548 bh = path->p_node[i].bh;
2550 eb = (struct ocfs2_extent_block *)bh->b_data;
2552 * Not all nodes might have had their final count
2553 * decremented by the caller - handle this here.
2556 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2558 "Inode %llu, attempted to remove extent block "
2559 "%llu with %u records\n",
2560 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2561 (unsigned long long)le64_to_cpu(eb->h_blkno),
2562 le16_to_cpu(el->l_next_free_rec));
2564 ocfs2_journal_dirty(handle, bh);
2565 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
2569 el->l_next_free_rec = 0;
2570 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2572 ocfs2_journal_dirty(handle, bh);
2574 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2578 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
2582 static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2583 struct ocfs2_path *left_path,
2584 struct ocfs2_path *right_path,
2586 struct ocfs2_cached_dealloc_ctxt *dealloc)
2589 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2590 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2591 struct ocfs2_extent_list *el;
2592 struct ocfs2_extent_block *eb;
2594 el = path_leaf_el(left_path);
2596 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2598 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2599 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2602 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2604 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2605 le16_add_cpu(&root_el->l_next_free_rec, -1);
2607 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2608 eb->h_next_leaf_blk = 0;
2610 ocfs2_journal_dirty(handle, root_bh);
2611 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2613 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2617 static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2618 struct ocfs2_path *left_path,
2619 struct ocfs2_path *right_path,
2621 struct ocfs2_cached_dealloc_ctxt *dealloc,
2623 struct ocfs2_extent_tree *et)
2625 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2626 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2627 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2628 struct ocfs2_extent_block *eb;
2632 right_leaf_el = path_leaf_el(right_path);
2633 left_leaf_el = path_leaf_el(left_path);
2634 root_bh = left_path->p_node[subtree_index].bh;
2635 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2637 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2640 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2641 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2643 * It's legal for us to proceed if the right leaf is
2644 * the rightmost one and it has an empty extent. There
2645 * are two cases to handle - whether the leaf will be
2646 * empty after removal or not. If the leaf isn't empty
2647 * then just remove the empty extent up front. The
2648 * next block will handle empty leaves by flagging
2651 * Non rightmost leaves will throw -EAGAIN and the
2652 * caller can manually move the subtree and retry.
2655 if (eb->h_next_leaf_blk != 0ULL)
2658 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2659 ret = ocfs2_journal_access_eb(handle, INODE_CACHE(inode),
2660 path_leaf_bh(right_path),
2661 OCFS2_JOURNAL_ACCESS_WRITE);
2667 ocfs2_remove_empty_extent(right_leaf_el);
2669 right_has_empty = 1;
2672 if (eb->h_next_leaf_blk == 0ULL &&
2673 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2675 * We have to update i_last_eb_blk during the meta
2678 ret = ocfs2_et_root_journal_access(handle, et,
2679 OCFS2_JOURNAL_ACCESS_WRITE);
2685 del_right_subtree = 1;
2689 * Getting here with an empty extent in the right path implies
2690 * that it's the rightmost path and will be deleted.
2692 BUG_ON(right_has_empty && !del_right_subtree);
2694 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
2701 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2702 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
2709 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
2717 if (!right_has_empty) {
2719 * Only do this if we're moving a real
2720 * record. Otherwise, the action is delayed until
2721 * after removal of the right path in which case we
2722 * can do a simple shift to remove the empty extent.
2724 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2725 memset(&right_leaf_el->l_recs[0], 0,
2726 sizeof(struct ocfs2_extent_rec));
2728 if (eb->h_next_leaf_blk == 0ULL) {
2730 * Move recs over to get rid of empty extent, decrease
2731 * next_free. This is allowed to remove the last
2732 * extent in our leaf (setting l_next_free_rec to
2733 * zero) - the delete code below won't care.
2735 ocfs2_remove_empty_extent(right_leaf_el);
2738 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2741 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2745 if (del_right_subtree) {
2746 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2747 subtree_index, dealloc);
2748 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
2755 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2756 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2759 * Removal of the extent in the left leaf was skipped
2760 * above so we could delete the right path
2763 if (right_has_empty)
2764 ocfs2_remove_empty_extent(left_leaf_el);
2766 ret = ocfs2_journal_dirty(handle, et_root_bh);
2772 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2780 * Given a full path, determine what cpos value would return us a path
2781 * containing the leaf immediately to the right of the current one.
2783 * Will return zero if the path passed in is already the rightmost path.
2785 * This looks similar, but is subtly different to
2786 * ocfs2_find_cpos_for_left_leaf().
2788 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2789 struct ocfs2_path *path, u32 *cpos)
2793 struct ocfs2_extent_list *el;
2797 if (path->p_tree_depth == 0)
2800 blkno = path_leaf_bh(path)->b_blocknr;
2802 /* Start at the tree node just above the leaf and work our way up. */
2803 i = path->p_tree_depth - 1;
2807 el = path->p_node[i].el;
2810 * Find the extent record just after the one in our
2813 next_free = le16_to_cpu(el->l_next_free_rec);
2814 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2815 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2816 if (j == (next_free - 1)) {
2819 * We've determined that the
2820 * path specified is already
2821 * the rightmost one - return a
2827 * The rightmost record points to our
2828 * leaf - we need to travel up the
2834 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2840 * If we got here, we never found a valid node where
2841 * the tree indicated one should be.
2844 "Invalid extent tree at extent block %llu\n",
2845 (unsigned long long)blkno);
2850 blkno = path->p_node[i].bh->b_blocknr;
2858 static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2860 struct ocfs2_path *path)
2863 struct buffer_head *bh = path_leaf_bh(path);
2864 struct ocfs2_extent_list *el = path_leaf_el(path);
2866 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2869 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
2870 path_num_items(path) - 1);
2876 ocfs2_remove_empty_extent(el);
2878 ret = ocfs2_journal_dirty(handle, bh);
2886 static int __ocfs2_rotate_tree_left(struct inode *inode,
2887 handle_t *handle, int orig_credits,
2888 struct ocfs2_path *path,
2889 struct ocfs2_cached_dealloc_ctxt *dealloc,
2890 struct ocfs2_path **empty_extent_path,
2891 struct ocfs2_extent_tree *et)
2893 int ret, subtree_root, deleted;
2895 struct ocfs2_path *left_path = NULL;
2896 struct ocfs2_path *right_path = NULL;
2898 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2900 *empty_extent_path = NULL;
2902 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2909 left_path = ocfs2_new_path_from_path(path);
2916 ocfs2_cp_path(left_path, path);
2918 right_path = ocfs2_new_path_from_path(path);
2925 while (right_cpos) {
2926 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
2932 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2935 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2937 (unsigned long long)
2938 right_path->p_node[subtree_root].bh->b_blocknr,
2939 right_path->p_tree_depth);
2941 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2942 orig_credits, left_path);
2949 * Caller might still want to make changes to the
2950 * tree root, so re-add it to the journal here.
2952 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
2959 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2960 right_path, subtree_root,
2961 dealloc, &deleted, et);
2962 if (ret == -EAGAIN) {
2964 * The rotation has to temporarily stop due to
2965 * the right subtree having an empty
2966 * extent. Pass it back to the caller for a
2969 *empty_extent_path = right_path;
2979 * The subtree rotate might have removed records on
2980 * the rightmost edge. If so, then rotation is
2986 ocfs2_mv_path(left_path, right_path);
2988 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2997 ocfs2_free_path(right_path);
2998 ocfs2_free_path(left_path);
3003 static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
3004 struct ocfs2_path *path,
3005 struct ocfs2_cached_dealloc_ctxt *dealloc,
3006 struct ocfs2_extent_tree *et)
3008 int ret, subtree_index;
3010 struct ocfs2_path *left_path = NULL;
3011 struct ocfs2_extent_block *eb;
3012 struct ocfs2_extent_list *el;
3015 ret = ocfs2_et_sanity_check(inode, et);
3019 * There's two ways we handle this depending on
3020 * whether path is the only existing one.
3022 ret = ocfs2_extend_rotate_transaction(handle, 0,
3023 handle->h_buffer_credits,
3030 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
3036 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3045 * We have a path to the left of this one - it needs
3048 left_path = ocfs2_new_path_from_path(path);
3055 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
3061 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
3067 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
3069 ocfs2_unlink_subtree(inode, handle, left_path, path,
3070 subtree_index, dealloc);
3071 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
3078 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
3079 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
3082 * 'path' is also the leftmost path which
3083 * means it must be the only one. This gets
3084 * handled differently because we want to
3085 * revert the inode back to having extents
3088 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
3090 el = et->et_root_el;
3091 el->l_tree_depth = 0;
3092 el->l_next_free_rec = 0;
3093 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3095 ocfs2_et_set_last_eb_blk(et, 0);
3098 ocfs2_journal_dirty(handle, path_root_bh(path));
3101 ocfs2_free_path(left_path);
3106 * Left rotation of btree records.
3108 * In many ways, this is (unsurprisingly) the opposite of right
3109 * rotation. We start at some non-rightmost path containing an empty
3110 * extent in the leaf block. The code works its way to the rightmost
3111 * path by rotating records to the left in every subtree.
3113 * This is used by any code which reduces the number of extent records
3114 * in a leaf. After removal, an empty record should be placed in the
3115 * leftmost list position.
3117 * This won't handle a length update of the rightmost path records if
3118 * the rightmost tree leaf record is removed so the caller is
3119 * responsible for detecting and correcting that.
3121 static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
3122 struct ocfs2_path *path,
3123 struct ocfs2_cached_dealloc_ctxt *dealloc,
3124 struct ocfs2_extent_tree *et)
3126 int ret, orig_credits = handle->h_buffer_credits;
3127 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3128 struct ocfs2_extent_block *eb;
3129 struct ocfs2_extent_list *el;
3131 el = path_leaf_el(path);
3132 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3135 if (path->p_tree_depth == 0) {
3136 rightmost_no_delete:
3138 * Inline extents. This is trivially handled, so do
3141 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
3149 * Handle rightmost branch now. There's several cases:
3150 * 1) simple rotation leaving records in there. That's trivial.
3151 * 2) rotation requiring a branch delete - there's no more
3152 * records left. Two cases of this:
3153 * a) There are branches to the left.
3154 * b) This is also the leftmost (the only) branch.
3156 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3157 * 2a) we need the left branch so that we can update it with the unlink
3158 * 2b) we need to bring the inode back to inline extents.
3161 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3163 if (eb->h_next_leaf_blk == 0) {
3165 * This gets a bit tricky if we're going to delete the
3166 * rightmost path. Get the other cases out of the way
3169 if (le16_to_cpu(el->l_next_free_rec) > 1)
3170 goto rightmost_no_delete;
3172 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3174 ocfs2_error(inode->i_sb,
3175 "Inode %llu has empty extent block at %llu",
3176 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3177 (unsigned long long)le64_to_cpu(eb->h_blkno));
3182 * XXX: The caller can not trust "path" any more after
3183 * this as it will have been deleted. What do we do?
3185 * In theory the rotate-for-merge code will never get
3186 * here because it'll always ask for a rotate in a
3190 ret = ocfs2_remove_rightmost_path(inode, handle, path,
3198 * Now we can loop, remembering the path we get from -EAGAIN
3199 * and restarting from there.
3202 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
3203 dealloc, &restart_path, et);
3204 if (ret && ret != -EAGAIN) {
3209 while (ret == -EAGAIN) {
3210 tmp_path = restart_path;
3211 restart_path = NULL;
3213 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
3216 if (ret && ret != -EAGAIN) {
3221 ocfs2_free_path(tmp_path);
3229 ocfs2_free_path(tmp_path);
3230 ocfs2_free_path(restart_path);
3234 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3237 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3240 if (rec->e_leaf_clusters == 0) {
3242 * We consumed all of the merged-from record. An empty
3243 * extent cannot exist anywhere but the 1st array
3244 * position, so move things over if the merged-from
3245 * record doesn't occupy that position.
3247 * This creates a new empty extent so the caller
3248 * should be smart enough to have removed any existing
3252 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3253 size = index * sizeof(struct ocfs2_extent_rec);
3254 memmove(&el->l_recs[1], &el->l_recs[0], size);
3258 * Always memset - the caller doesn't check whether it
3259 * created an empty extent, so there could be junk in
3262 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3266 static int ocfs2_get_right_path(struct inode *inode,
3267 struct ocfs2_path *left_path,
3268 struct ocfs2_path **ret_right_path)
3272 struct ocfs2_path *right_path = NULL;
3273 struct ocfs2_extent_list *left_el;
3275 *ret_right_path = NULL;
3277 /* This function shouldn't be called for non-trees. */
3278 BUG_ON(left_path->p_tree_depth == 0);
3280 left_el = path_leaf_el(left_path);
3281 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3283 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3290 /* This function shouldn't be called for the rightmost leaf. */
3291 BUG_ON(right_cpos == 0);
3293 right_path = ocfs2_new_path_from_path(left_path);
3300 ret = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos);
3306 *ret_right_path = right_path;
3309 ocfs2_free_path(right_path);
3314 * Remove split_rec clusters from the record at index and merge them
3315 * onto the beginning of the record "next" to it.
3316 * For index < l_count - 1, the next means the extent rec at index + 1.
3317 * For index == l_count - 1, the "next" means the 1st extent rec of the
3318 * next extent block.
3320 static int ocfs2_merge_rec_right(struct inode *inode,
3321 struct ocfs2_path *left_path,
3323 struct ocfs2_extent_rec *split_rec,
3326 int ret, next_free, i;
3327 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3328 struct ocfs2_extent_rec *left_rec;
3329 struct ocfs2_extent_rec *right_rec;
3330 struct ocfs2_extent_list *right_el;
3331 struct ocfs2_path *right_path = NULL;
3332 int subtree_index = 0;
3333 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3334 struct buffer_head *bh = path_leaf_bh(left_path);
3335 struct buffer_head *root_bh = NULL;
3337 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3338 left_rec = &el->l_recs[index];
3340 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3341 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3342 /* we meet with a cross extent block merge. */
3343 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3349 right_el = path_leaf_el(right_path);
3350 next_free = le16_to_cpu(right_el->l_next_free_rec);
3351 BUG_ON(next_free <= 0);
3352 right_rec = &right_el->l_recs[0];
3353 if (ocfs2_is_empty_extent(right_rec)) {
3354 BUG_ON(next_free <= 1);
3355 right_rec = &right_el->l_recs[1];
3358 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3359 le16_to_cpu(left_rec->e_leaf_clusters) !=
3360 le32_to_cpu(right_rec->e_cpos));
3362 subtree_index = ocfs2_find_subtree_root(inode,
3363 left_path, right_path);
3365 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3366 handle->h_buffer_credits,
3373 root_bh = left_path->p_node[subtree_index].bh;
3374 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3376 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
3383 for (i = subtree_index + 1;
3384 i < path_num_items(right_path); i++) {
3385 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
3392 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
3401 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3402 right_rec = &el->l_recs[index + 1];
3405 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), left_path,
3406 path_num_items(left_path) - 1);
3412 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3414 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3415 le64_add_cpu(&right_rec->e_blkno,
3416 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3417 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3419 ocfs2_cleanup_merge(el, index);
3421 ret = ocfs2_journal_dirty(handle, bh);
3426 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3430 ocfs2_complete_edge_insert(inode, handle, left_path,
3431 right_path, subtree_index);
3435 ocfs2_free_path(right_path);
3439 static int ocfs2_get_left_path(struct inode *inode,
3440 struct ocfs2_path *right_path,
3441 struct ocfs2_path **ret_left_path)
3445 struct ocfs2_path *left_path = NULL;
3447 *ret_left_path = NULL;
3449 /* This function shouldn't be called for non-trees. */
3450 BUG_ON(right_path->p_tree_depth == 0);
3452 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3453 right_path, &left_cpos);
3459 /* This function shouldn't be called for the leftmost leaf. */
3460 BUG_ON(left_cpos == 0);
3462 left_path = ocfs2_new_path_from_path(right_path);
3469 ret = ocfs2_find_path(INODE_CACHE(inode), left_path, left_cpos);
3475 *ret_left_path = left_path;
3478 ocfs2_free_path(left_path);
3483 * Remove split_rec clusters from the record at index and merge them
3484 * onto the tail of the record "before" it.
3485 * For index > 0, the "before" means the extent rec at index - 1.
3487 * For index == 0, the "before" means the last record of the previous
3488 * extent block. And there is also a situation that we may need to
3489 * remove the rightmost leaf extent block in the right_path and change
3490 * the right path to indicate the new rightmost path.
3492 static int ocfs2_merge_rec_left(struct inode *inode,
3493 struct ocfs2_path *right_path,
3495 struct ocfs2_extent_rec *split_rec,
3496 struct ocfs2_cached_dealloc_ctxt *dealloc,
3497 struct ocfs2_extent_tree *et,
3500 int ret, i, subtree_index = 0, has_empty_extent = 0;
3501 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3502 struct ocfs2_extent_rec *left_rec;
3503 struct ocfs2_extent_rec *right_rec;
3504 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3505 struct buffer_head *bh = path_leaf_bh(right_path);
3506 struct buffer_head *root_bh = NULL;
3507 struct ocfs2_path *left_path = NULL;
3508 struct ocfs2_extent_list *left_el;
3512 right_rec = &el->l_recs[index];
3514 /* we meet with a cross extent block merge. */
3515 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3521 left_el = path_leaf_el(left_path);
3522 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3523 le16_to_cpu(left_el->l_count));
3525 left_rec = &left_el->l_recs[
3526 le16_to_cpu(left_el->l_next_free_rec) - 1];
3527 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3528 le16_to_cpu(left_rec->e_leaf_clusters) !=
3529 le32_to_cpu(split_rec->e_cpos));
3531 subtree_index = ocfs2_find_subtree_root(inode,
3532 left_path, right_path);
3534 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3535 handle->h_buffer_credits,
3542 root_bh = left_path->p_node[subtree_index].bh;
3543 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3545 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
3552 for (i = subtree_index + 1;
3553 i < path_num_items(right_path); i++) {
3554 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
3561 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
3569 left_rec = &el->l_recs[index - 1];
3570 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3571 has_empty_extent = 1;
3574 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
3575 path_num_items(right_path) - 1);
3581 if (has_empty_extent && index == 1) {
3583 * The easy case - we can just plop the record right in.
3585 *left_rec = *split_rec;
3587 has_empty_extent = 0;
3589 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3591 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3592 le64_add_cpu(&right_rec->e_blkno,
3593 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3594 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3596 ocfs2_cleanup_merge(el, index);
3598 ret = ocfs2_journal_dirty(handle, bh);
3603 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3608 * In the situation that the right_rec is empty and the extent
3609 * block is empty also, ocfs2_complete_edge_insert can't handle
3610 * it and we need to delete the right extent block.
3612 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3613 le16_to_cpu(el->l_next_free_rec) == 1) {
3615 ret = ocfs2_remove_rightmost_path(inode, handle,
3623 /* Now the rightmost extent block has been deleted.
3624 * So we use the new rightmost path.
3626 ocfs2_mv_path(right_path, left_path);
3629 ocfs2_complete_edge_insert(inode, handle, left_path,
3630 right_path, subtree_index);
3634 ocfs2_free_path(left_path);
3638 static int ocfs2_try_to_merge_extent(struct inode *inode,
3640 struct ocfs2_path *path,
3642 struct ocfs2_extent_rec *split_rec,
3643 struct ocfs2_cached_dealloc_ctxt *dealloc,
3644 struct ocfs2_merge_ctxt *ctxt,
3645 struct ocfs2_extent_tree *et)
3649 struct ocfs2_extent_list *el = path_leaf_el(path);
3650 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3652 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3654 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3656 * The merge code will need to create an empty
3657 * extent to take the place of the newly
3658 * emptied slot. Remove any pre-existing empty
3659 * extents - having more than one in a leaf is
3662 ret = ocfs2_rotate_tree_left(inode, handle, path,
3669 rec = &el->l_recs[split_index];
3672 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3674 * Left-right contig implies this.
3676 BUG_ON(!ctxt->c_split_covers_rec);
3679 * Since the leftright insert always covers the entire
3680 * extent, this call will delete the insert record
3681 * entirely, resulting in an empty extent record added to
3684 * Since the adding of an empty extent shifts
3685 * everything back to the right, there's no need to
3686 * update split_index here.
3688 * When the split_index is zero, we need to merge it to the
3689 * prevoius extent block. It is more efficient and easier
3690 * if we do merge_right first and merge_left later.
3692 ret = ocfs2_merge_rec_right(inode, path,
3701 * We can only get this from logic error above.
3703 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3705 /* The merge left us with an empty extent, remove it. */
3706 ret = ocfs2_rotate_tree_left(inode, handle, path,
3713 rec = &el->l_recs[split_index];
3716 * Note that we don't pass split_rec here on purpose -
3717 * we've merged it into the rec already.
3719 ret = ocfs2_merge_rec_left(inode, path,
3729 ret = ocfs2_rotate_tree_left(inode, handle, path,
3732 * Error from this last rotate is not critical, so
3733 * print but don't bubble it up.
3740 * Merge a record to the left or right.
3742 * 'contig_type' is relative to the existing record,
3743 * so for example, if we're "right contig", it's to
3744 * the record on the left (hence the left merge).
3746 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3747 ret = ocfs2_merge_rec_left(inode,
3757 ret = ocfs2_merge_rec_right(inode,
3767 if (ctxt->c_split_covers_rec) {
3769 * The merge may have left an empty extent in
3770 * our leaf. Try to rotate it away.
3772 ret = ocfs2_rotate_tree_left(inode, handle, path,
3784 static void ocfs2_subtract_from_rec(struct super_block *sb,
3785 enum ocfs2_split_type split,
3786 struct ocfs2_extent_rec *rec,
3787 struct ocfs2_extent_rec *split_rec)
3791 len_blocks = ocfs2_clusters_to_blocks(sb,
3792 le16_to_cpu(split_rec->e_leaf_clusters));
3794 if (split == SPLIT_LEFT) {
3796 * Region is on the left edge of the existing
3799 le32_add_cpu(&rec->e_cpos,
3800 le16_to_cpu(split_rec->e_leaf_clusters));
3801 le64_add_cpu(&rec->e_blkno, len_blocks);
3802 le16_add_cpu(&rec->e_leaf_clusters,
3803 -le16_to_cpu(split_rec->e_leaf_clusters));
3806 * Region is on the right edge of the existing
3809 le16_add_cpu(&rec->e_leaf_clusters,
3810 -le16_to_cpu(split_rec->e_leaf_clusters));
3815 * Do the final bits of extent record insertion at the target leaf
3816 * list. If this leaf is part of an allocation tree, it is assumed
3817 * that the tree above has been prepared.
3819 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3820 struct ocfs2_extent_list *el,
3821 struct ocfs2_insert_type *insert,
3822 struct inode *inode)
3824 int i = insert->ins_contig_index;
3826 struct ocfs2_extent_rec *rec;
3828 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3830 if (insert->ins_split != SPLIT_NONE) {
3831 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3833 rec = &el->l_recs[i];
3834 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3840 * Contiguous insert - either left or right.
3842 if (insert->ins_contig != CONTIG_NONE) {
3843 rec = &el->l_recs[i];
3844 if (insert->ins_contig == CONTIG_LEFT) {
3845 rec->e_blkno = insert_rec->e_blkno;
3846 rec->e_cpos = insert_rec->e_cpos;
3848 le16_add_cpu(&rec->e_leaf_clusters,
3849 le16_to_cpu(insert_rec->e_leaf_clusters));
3854 * Handle insert into an empty leaf.
3856 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3857 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3858 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3859 el->l_recs[0] = *insert_rec;
3860 el->l_next_free_rec = cpu_to_le16(1);
3867 if (insert->ins_appending == APPEND_TAIL) {
3868 i = le16_to_cpu(el->l_next_free_rec) - 1;
3869 rec = &el->l_recs[i];
3870 range = le32_to_cpu(rec->e_cpos)
3871 + le16_to_cpu(rec->e_leaf_clusters);
3872 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3874 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3875 le16_to_cpu(el->l_count),
3876 "inode %lu, depth %u, count %u, next free %u, "
3877 "rec.cpos %u, rec.clusters %u, "
3878 "insert.cpos %u, insert.clusters %u\n",
3880 le16_to_cpu(el->l_tree_depth),
3881 le16_to_cpu(el->l_count),
3882 le16_to_cpu(el->l_next_free_rec),
3883 le32_to_cpu(el->l_recs[i].e_cpos),
3884 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3885 le32_to_cpu(insert_rec->e_cpos),
3886 le16_to_cpu(insert_rec->e_leaf_clusters));
3888 el->l_recs[i] = *insert_rec;
3889 le16_add_cpu(&el->l_next_free_rec, 1);
3895 * Ok, we have to rotate.
3897 * At this point, it is safe to assume that inserting into an
3898 * empty leaf and appending to a leaf have both been handled
3901 * This leaf needs to have space, either by the empty 1st
3902 * extent record, or by virtue of an l_next_rec < l_count.
3904 ocfs2_rotate_leaf(el, insert_rec);
3907 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3909 struct ocfs2_path *path,
3910 struct ocfs2_extent_rec *insert_rec)
3912 int ret, i, next_free;
3913 struct buffer_head *bh;
3914 struct ocfs2_extent_list *el;
3915 struct ocfs2_extent_rec *rec;
3918 * Update everything except the leaf block.
3920 for (i = 0; i < path->p_tree_depth; i++) {
3921 bh = path->p_node[i].bh;
3922 el = path->p_node[i].el;
3924 next_free = le16_to_cpu(el->l_next_free_rec);
3925 if (next_free == 0) {
3926 ocfs2_error(inode->i_sb,
3927 "Dinode %llu has a bad extent list",
3928 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3933 rec = &el->l_recs[next_free - 1];
3935 rec->e_int_clusters = insert_rec->e_cpos;
3936 le32_add_cpu(&rec->e_int_clusters,
3937 le16_to_cpu(insert_rec->e_leaf_clusters));
3938 le32_add_cpu(&rec->e_int_clusters,
3939 -le32_to_cpu(rec->e_cpos));
3941 ret = ocfs2_journal_dirty(handle, bh);
3948 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3949 struct ocfs2_extent_rec *insert_rec,
3950 struct ocfs2_path *right_path,
3951 struct ocfs2_path **ret_left_path)
3954 struct ocfs2_extent_list *el;
3955 struct ocfs2_path *left_path = NULL;
3957 *ret_left_path = NULL;
3960 * This shouldn't happen for non-trees. The extent rec cluster
3961 * count manipulation below only works for interior nodes.
3963 BUG_ON(right_path->p_tree_depth == 0);
3966 * If our appending insert is at the leftmost edge of a leaf,
3967 * then we might need to update the rightmost records of the
3970 el = path_leaf_el(right_path);
3971 next_free = le16_to_cpu(el->l_next_free_rec);
3972 if (next_free == 0 ||
3973 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3976 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3983 mlog(0, "Append may need a left path update. cpos: %u, "
3984 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3988 * No need to worry if the append is already in the
3992 left_path = ocfs2_new_path_from_path(right_path);
3999 ret = ocfs2_find_path(INODE_CACHE(inode), left_path,
4007 * ocfs2_insert_path() will pass the left_path to the
4013 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, right_path);
4019 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
4021 *ret_left_path = left_path;
4025 ocfs2_free_path(left_path);
4030 static void ocfs2_split_record(struct inode *inode,
4031 struct ocfs2_path *left_path,
4032 struct ocfs2_path *right_path,
4033 struct ocfs2_extent_rec *split_rec,
4034 enum ocfs2_split_type split)
4037 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4038 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4039 struct ocfs2_extent_rec *rec, *tmprec;
4041 right_el = path_leaf_el(right_path);
4043 left_el = path_leaf_el(left_path);
4046 insert_el = right_el;
4047 index = ocfs2_search_extent_list(el, cpos);
4049 if (index == 0 && left_path) {
4050 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4053 * This typically means that the record
4054 * started in the left path but moved to the
4055 * right as a result of rotation. We either
4056 * move the existing record to the left, or we
4057 * do the later insert there.
4059 * In this case, the left path should always
4060 * exist as the rotate code will have passed
4061 * it back for a post-insert update.
4064 if (split == SPLIT_LEFT) {
4066 * It's a left split. Since we know
4067 * that the rotate code gave us an
4068 * empty extent in the left path, we
4069 * can just do the insert there.
4071 insert_el = left_el;
4074 * Right split - we have to move the
4075 * existing record over to the left
4076 * leaf. The insert will be into the
4077 * newly created empty extent in the
4080 tmprec = &right_el->l_recs[index];
4081 ocfs2_rotate_leaf(left_el, tmprec);
4084 memset(tmprec, 0, sizeof(*tmprec));
4085 index = ocfs2_search_extent_list(left_el, cpos);
4086 BUG_ON(index == -1);
4091 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4093 * Left path is easy - we can just allow the insert to
4097 insert_el = left_el;
4098 index = ocfs2_search_extent_list(el, cpos);
4099 BUG_ON(index == -1);
4102 rec = &el->l_recs[index];
4103 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
4104 ocfs2_rotate_leaf(insert_el, split_rec);
4108 * This function only does inserts on an allocation b-tree. For tree
4109 * depth = 0, ocfs2_insert_at_leaf() is called directly.
4111 * right_path is the path we want to do the actual insert
4112 * in. left_path should only be passed in if we need to update that
4113 * portion of the tree after an edge insert.
4115 static int ocfs2_insert_path(struct inode *inode,
4117 struct ocfs2_path *left_path,
4118 struct ocfs2_path *right_path,
4119 struct ocfs2_extent_rec *insert_rec,
4120 struct ocfs2_insert_type *insert)
4122 int ret, subtree_index;
4123 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
4126 int credits = handle->h_buffer_credits;
4129 * There's a chance that left_path got passed back to
4130 * us without being accounted for in the
4131 * journal. Extend our transaction here to be sure we
4132 * can change those blocks.
4134 credits += left_path->p_tree_depth;
4136 ret = ocfs2_extend_trans(handle, credits);
4142 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, left_path);
4150 * Pass both paths to the journal. The majority of inserts
4151 * will be touching all components anyway.
4153 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, right_path);
4159 if (insert->ins_split != SPLIT_NONE) {
4161 * We could call ocfs2_insert_at_leaf() for some types
4162 * of splits, but it's easier to just let one separate
4163 * function sort it all out.
4165 ocfs2_split_record(inode, left_path, right_path,
4166 insert_rec, insert->ins_split);
4169 * Split might have modified either leaf and we don't
4170 * have a guarantee that the later edge insert will
4171 * dirty this for us.
4174 ret = ocfs2_journal_dirty(handle,
4175 path_leaf_bh(left_path));
4179 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
4182 ret = ocfs2_journal_dirty(handle, leaf_bh);
4188 * The rotate code has indicated that we need to fix
4189 * up portions of the tree after the insert.
4191 * XXX: Should we extend the transaction here?
4193 subtree_index = ocfs2_find_subtree_root(inode, left_path,
4195 ocfs2_complete_edge_insert(inode, handle, left_path,
4196 right_path, subtree_index);
4204 static int ocfs2_do_insert_extent(struct inode *inode,
4206 struct ocfs2_extent_tree *et,
4207 struct ocfs2_extent_rec *insert_rec,
4208 struct ocfs2_insert_type *type)
4210 int ret, rotate = 0;
4212 struct ocfs2_path *right_path = NULL;
4213 struct ocfs2_path *left_path = NULL;
4214 struct ocfs2_extent_list *el;
4216 el = et->et_root_el;
4218 ret = ocfs2_et_root_journal_access(handle, et,
4219 OCFS2_JOURNAL_ACCESS_WRITE);
4225 if (le16_to_cpu(el->l_tree_depth) == 0) {
4226 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4227 goto out_update_clusters;
4230 right_path = ocfs2_new_path_from_et(et);
4238 * Determine the path to start with. Rotations need the
4239 * rightmost path, everything else can go directly to the
4242 cpos = le32_to_cpu(insert_rec->e_cpos);
4243 if (type->ins_appending == APPEND_NONE &&
4244 type->ins_contig == CONTIG_NONE) {
4249 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
4256 * Rotations and appends need special treatment - they modify
4257 * parts of the tree's above them.
4259 * Both might pass back a path immediate to the left of the
4260 * one being inserted to. This will be cause
4261 * ocfs2_insert_path() to modify the rightmost records of
4262 * left_path to account for an edge insert.
4264 * XXX: When modifying this code, keep in mind that an insert
4265 * can wind up skipping both of these two special cases...
4268 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
4269 le32_to_cpu(insert_rec->e_cpos),
4270 right_path, &left_path);
4277 * ocfs2_rotate_tree_right() might have extended the
4278 * transaction without re-journaling our tree root.
4280 ret = ocfs2_et_root_journal_access(handle, et,
4281 OCFS2_JOURNAL_ACCESS_WRITE);
4286 } else if (type->ins_appending == APPEND_TAIL
4287 && type->ins_contig != CONTIG_LEFT) {
4288 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4289 right_path, &left_path);
4296 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4303 out_update_clusters:
4304 if (type->ins_split == SPLIT_NONE)
4305 ocfs2_et_update_clusters(inode, et,
4306 le16_to_cpu(insert_rec->e_leaf_clusters));
4308 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
4313 ocfs2_free_path(left_path);
4314 ocfs2_free_path(right_path);
4319 static enum ocfs2_contig_type
4320 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
4321 struct ocfs2_extent_list *el, int index,
4322 struct ocfs2_extent_rec *split_rec)
4325 enum ocfs2_contig_type ret = CONTIG_NONE;
4326 u32 left_cpos, right_cpos;
4327 struct ocfs2_extent_rec *rec = NULL;
4328 struct ocfs2_extent_list *new_el;
4329 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4330 struct buffer_head *bh;
4331 struct ocfs2_extent_block *eb;
4334 rec = &el->l_recs[index - 1];
4335 } else if (path->p_tree_depth > 0) {
4336 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4341 if (left_cpos != 0) {
4342 left_path = ocfs2_new_path_from_path(path);
4346 status = ocfs2_find_path(INODE_CACHE(inode),
4347 left_path, left_cpos);
4351 new_el = path_leaf_el(left_path);
4353 if (le16_to_cpu(new_el->l_next_free_rec) !=
4354 le16_to_cpu(new_el->l_count)) {
4355 bh = path_leaf_bh(left_path);
4356 eb = (struct ocfs2_extent_block *)bh->b_data;
4357 ocfs2_error(inode->i_sb,
4358 "Extent block #%llu has an "
4359 "invalid l_next_free_rec of "
4360 "%d. It should have "
4361 "matched the l_count of %d",
4362 (unsigned long long)le64_to_cpu(eb->h_blkno),
4363 le16_to_cpu(new_el->l_next_free_rec),
4364 le16_to_cpu(new_el->l_count));
4368 rec = &new_el->l_recs[
4369 le16_to_cpu(new_el->l_next_free_rec) - 1];
4374 * We're careful to check for an empty extent record here -
4375 * the merge code will know what to do if it sees one.
4378 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4379 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4382 ret = ocfs2_extent_contig(inode, rec, split_rec);
4387 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4388 rec = &el->l_recs[index + 1];
4389 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4390 path->p_tree_depth > 0) {
4391 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4396 if (right_cpos == 0)
4399 right_path = ocfs2_new_path_from_path(path);
4403 status = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos);
4407 new_el = path_leaf_el(right_path);
4408 rec = &new_el->l_recs[0];
4409 if (ocfs2_is_empty_extent(rec)) {
4410 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4411 bh = path_leaf_bh(right_path);
4412 eb = (struct ocfs2_extent_block *)bh->b_data;
4413 ocfs2_error(inode->i_sb,
4414 "Extent block #%llu has an "
4415 "invalid l_next_free_rec of %d",
4416 (unsigned long long)le64_to_cpu(eb->h_blkno),
4417 le16_to_cpu(new_el->l_next_free_rec));
4421 rec = &new_el->l_recs[1];
4426 enum ocfs2_contig_type contig_type;
4428 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4430 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4431 ret = CONTIG_LEFTRIGHT;
4432 else if (ret == CONTIG_NONE)
4438 ocfs2_free_path(left_path);
4440 ocfs2_free_path(right_path);
4445 static void ocfs2_figure_contig_type(struct inode *inode,
4446 struct ocfs2_insert_type *insert,
4447 struct ocfs2_extent_list *el,
4448 struct ocfs2_extent_rec *insert_rec,
4449 struct ocfs2_extent_tree *et)
4452 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4454 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4456 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4457 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4459 if (contig_type != CONTIG_NONE) {
4460 insert->ins_contig_index = i;
4464 insert->ins_contig = contig_type;
4466 if (insert->ins_contig != CONTIG_NONE) {
4467 struct ocfs2_extent_rec *rec =
4468 &el->l_recs[insert->ins_contig_index];
4469 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4470 le16_to_cpu(insert_rec->e_leaf_clusters);
4473 * Caller might want us to limit the size of extents, don't
4474 * calculate contiguousness if we might exceed that limit.
4476 if (et->et_max_leaf_clusters &&
4477 (len > et->et_max_leaf_clusters))
4478 insert->ins_contig = CONTIG_NONE;
4483 * This should only be called against the righmost leaf extent list.
4485 * ocfs2_figure_appending_type() will figure out whether we'll have to
4486 * insert at the tail of the rightmost leaf.
4488 * This should also work against the root extent list for tree's with 0
4489 * depth. If we consider the root extent list to be the rightmost leaf node
4490 * then the logic here makes sense.
4492 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4493 struct ocfs2_extent_list *el,
4494 struct ocfs2_extent_rec *insert_rec)
4497 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4498 struct ocfs2_extent_rec *rec;
4500 insert->ins_appending = APPEND_NONE;
4502 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4504 if (!el->l_next_free_rec)
4505 goto set_tail_append;
4507 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4508 /* Were all records empty? */
4509 if (le16_to_cpu(el->l_next_free_rec) == 1)
4510 goto set_tail_append;
4513 i = le16_to_cpu(el->l_next_free_rec) - 1;
4514 rec = &el->l_recs[i];
4517 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4518 goto set_tail_append;
4523 insert->ins_appending = APPEND_TAIL;
4527 * Helper function called at the begining of an insert.
4529 * This computes a few things that are commonly used in the process of
4530 * inserting into the btree:
4531 * - Whether the new extent is contiguous with an existing one.
4532 * - The current tree depth.
4533 * - Whether the insert is an appending one.
4534 * - The total # of free records in the tree.
4536 * All of the information is stored on the ocfs2_insert_type
4539 static int ocfs2_figure_insert_type(struct inode *inode,
4540 struct ocfs2_extent_tree *et,
4541 struct buffer_head **last_eb_bh,
4542 struct ocfs2_extent_rec *insert_rec,
4544 struct ocfs2_insert_type *insert)
4547 struct ocfs2_extent_block *eb;
4548 struct ocfs2_extent_list *el;
4549 struct ocfs2_path *path = NULL;
4550 struct buffer_head *bh = NULL;
4552 insert->ins_split = SPLIT_NONE;
4554 el = et->et_root_el;
4555 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4557 if (el->l_tree_depth) {
4559 * If we have tree depth, we read in the
4560 * rightmost extent block ahead of time as
4561 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4562 * may want it later.
4564 ret = ocfs2_read_extent_block(et->et_ci,
4565 ocfs2_et_get_last_eb_blk(et),
4571 eb = (struct ocfs2_extent_block *) bh->b_data;
4576 * Unless we have a contiguous insert, we'll need to know if
4577 * there is room left in our allocation tree for another
4580 * XXX: This test is simplistic, we can search for empty
4581 * extent records too.
4583 *free_records = le16_to_cpu(el->l_count) -
4584 le16_to_cpu(el->l_next_free_rec);
4586 if (!insert->ins_tree_depth) {
4587 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4588 ocfs2_figure_appending_type(insert, el, insert_rec);
4592 path = ocfs2_new_path_from_et(et);
4600 * In the case that we're inserting past what the tree
4601 * currently accounts for, ocfs2_find_path() will return for
4602 * us the rightmost tree path. This is accounted for below in
4603 * the appending code.
4605 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
4611 el = path_leaf_el(path);
4614 * Now that we have the path, there's two things we want to determine:
4615 * 1) Contiguousness (also set contig_index if this is so)
4617 * 2) Are we doing an append? We can trivially break this up
4618 * into two types of appends: simple record append, or a
4619 * rotate inside the tail leaf.
4621 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4624 * The insert code isn't quite ready to deal with all cases of
4625 * left contiguousness. Specifically, if it's an insert into
4626 * the 1st record in a leaf, it will require the adjustment of
4627 * cluster count on the last record of the path directly to it's
4628 * left. For now, just catch that case and fool the layers
4629 * above us. This works just fine for tree_depth == 0, which
4630 * is why we allow that above.
4632 if (insert->ins_contig == CONTIG_LEFT &&
4633 insert->ins_contig_index == 0)
4634 insert->ins_contig = CONTIG_NONE;
4637 * Ok, so we can simply compare against last_eb to figure out
4638 * whether the path doesn't exist. This will only happen in
4639 * the case that we're doing a tail append, so maybe we can
4640 * take advantage of that information somehow.
4642 if (ocfs2_et_get_last_eb_blk(et) ==
4643 path_leaf_bh(path)->b_blocknr) {
4645 * Ok, ocfs2_find_path() returned us the rightmost
4646 * tree path. This might be an appending insert. There are
4648 * 1) We're doing a true append at the tail:
4649 * -This might even be off the end of the leaf
4650 * 2) We're "appending" by rotating in the tail
4652 ocfs2_figure_appending_type(insert, el, insert_rec);
4656 ocfs2_free_path(path);
4666 * Insert an extent into an inode btree.
4668 * The caller needs to update fe->i_clusters
4670 int ocfs2_insert_extent(struct ocfs2_super *osb,
4672 struct inode *inode,
4673 struct ocfs2_extent_tree *et,
4678 struct ocfs2_alloc_context *meta_ac)
4681 int uninitialized_var(free_records);
4682 struct buffer_head *last_eb_bh = NULL;
4683 struct ocfs2_insert_type insert = {0, };
4684 struct ocfs2_extent_rec rec;
4686 mlog(0, "add %u clusters at position %u to inode %llu\n",
4687 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4689 memset(&rec, 0, sizeof(rec));
4690 rec.e_cpos = cpu_to_le32(cpos);
4691 rec.e_blkno = cpu_to_le64(start_blk);
4692 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4693 rec.e_flags = flags;
4694 status = ocfs2_et_insert_check(inode, et, &rec);
4700 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4701 &free_records, &insert);
4707 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4708 "Insert.contig_index: %d, Insert.free_records: %d, "
4709 "Insert.tree_depth: %d\n",
4710 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4711 free_records, insert.ins_tree_depth);
4713 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4714 status = ocfs2_grow_tree(inode, handle, et,
4715 &insert.ins_tree_depth, &last_eb_bh,
4723 /* Finally, we can add clusters. This might rotate the tree for us. */
4724 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4727 else if (et->et_ops == &ocfs2_dinode_et_ops)
4728 ocfs2_extent_map_insert_rec(inode, &rec);
4738 * Allcate and add clusters into the extent b-tree.
4739 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4740 * The extent b-tree's root is specified by et, and
4741 * it is not limited to the file storage. Any extent tree can use this
4742 * function if it implements the proper ocfs2_extent_tree.
4744 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4745 struct inode *inode,
4746 u32 *logical_offset,
4747 u32 clusters_to_add,
4749 struct ocfs2_extent_tree *et,
4751 struct ocfs2_alloc_context *data_ac,
4752 struct ocfs2_alloc_context *meta_ac,
4753 enum ocfs2_alloc_restarted *reason_ret)
4757 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4758 u32 bit_off, num_bits;
4762 BUG_ON(!clusters_to_add);
4765 flags = OCFS2_EXT_UNWRITTEN;
4767 free_extents = ocfs2_num_free_extents(osb, et);
4768 if (free_extents < 0) {
4769 status = free_extents;
4774 /* there are two cases which could cause us to EAGAIN in the
4775 * we-need-more-metadata case:
4776 * 1) we haven't reserved *any*
4777 * 2) we are so fragmented, we've needed to add metadata too
4779 if (!free_extents && !meta_ac) {
4780 mlog(0, "we haven't reserved any metadata!\n");
4782 reason = RESTART_META;
4784 } else if ((!free_extents)
4785 && (ocfs2_alloc_context_bits_left(meta_ac)
4786 < ocfs2_extend_meta_needed(et->et_root_el))) {
4787 mlog(0, "filesystem is really fragmented...\n");
4789 reason = RESTART_META;
4793 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4794 clusters_to_add, &bit_off, &num_bits);
4796 if (status != -ENOSPC)
4801 BUG_ON(num_bits > clusters_to_add);
4803 /* reserve our write early -- insert_extent may update the tree root */
4804 status = ocfs2_et_root_journal_access(handle, et,
4805 OCFS2_JOURNAL_ACCESS_WRITE);
4811 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4812 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4813 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4814 status = ocfs2_insert_extent(osb, handle, inode, et,
4815 *logical_offset, block,
4816 num_bits, flags, meta_ac);
4822 status = ocfs2_journal_dirty(handle, et->et_root_bh);
4828 clusters_to_add -= num_bits;
4829 *logical_offset += num_bits;
4831 if (clusters_to_add) {
4832 mlog(0, "need to alloc once more, wanted = %u\n",
4835 reason = RESTART_TRANS;
4841 *reason_ret = reason;
4845 static void ocfs2_make_right_split_rec(struct super_block *sb,
4846 struct ocfs2_extent_rec *split_rec,
4848 struct ocfs2_extent_rec *rec)
4850 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4851 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4853 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4855 split_rec->e_cpos = cpu_to_le32(cpos);
4856 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4858 split_rec->e_blkno = rec->e_blkno;
4859 le64_add_cpu(&split_rec->e_blkno,
4860 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4862 split_rec->e_flags = rec->e_flags;
4865 static int ocfs2_split_and_insert(struct inode *inode,
4867 struct ocfs2_path *path,
4868 struct ocfs2_extent_tree *et,
4869 struct buffer_head **last_eb_bh,
4871 struct ocfs2_extent_rec *orig_split_rec,
4872 struct ocfs2_alloc_context *meta_ac)
4875 unsigned int insert_range, rec_range, do_leftright = 0;
4876 struct ocfs2_extent_rec tmprec;
4877 struct ocfs2_extent_list *rightmost_el;
4878 struct ocfs2_extent_rec rec;
4879 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4880 struct ocfs2_insert_type insert;
4881 struct ocfs2_extent_block *eb;
4885 * Store a copy of the record on the stack - it might move
4886 * around as the tree is manipulated below.
4888 rec = path_leaf_el(path)->l_recs[split_index];
4890 rightmost_el = et->et_root_el;
4892 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4894 BUG_ON(!(*last_eb_bh));
4895 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4896 rightmost_el = &eb->h_list;
4899 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4900 le16_to_cpu(rightmost_el->l_count)) {
4901 ret = ocfs2_grow_tree(inode, handle, et,
4902 &depth, last_eb_bh, meta_ac);
4909 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4910 insert.ins_appending = APPEND_NONE;
4911 insert.ins_contig = CONTIG_NONE;
4912 insert.ins_tree_depth = depth;
4914 insert_range = le32_to_cpu(split_rec.e_cpos) +
4915 le16_to_cpu(split_rec.e_leaf_clusters);
4916 rec_range = le32_to_cpu(rec.e_cpos) +
4917 le16_to_cpu(rec.e_leaf_clusters);
4919 if (split_rec.e_cpos == rec.e_cpos) {
4920 insert.ins_split = SPLIT_LEFT;
4921 } else if (insert_range == rec_range) {
4922 insert.ins_split = SPLIT_RIGHT;
4925 * Left/right split. We fake this as a right split
4926 * first and then make a second pass as a left split.
4928 insert.ins_split = SPLIT_RIGHT;
4930 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4935 BUG_ON(do_leftright);
4939 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4945 if (do_leftright == 1) {
4947 struct ocfs2_extent_list *el;
4950 split_rec = *orig_split_rec;
4952 ocfs2_reinit_path(path, 1);
4954 cpos = le32_to_cpu(split_rec.e_cpos);
4955 ret = ocfs2_find_path(et->et_ci, path, cpos);
4961 el = path_leaf_el(path);
4962 split_index = ocfs2_search_extent_list(el, cpos);
4970 static int ocfs2_replace_extent_rec(struct inode *inode,
4972 struct ocfs2_path *path,
4973 struct ocfs2_extent_list *el,
4975 struct ocfs2_extent_rec *split_rec)
4979 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
4980 path_num_items(path) - 1);
4986 el->l_recs[split_index] = *split_rec;
4988 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4994 * Mark part or all of the extent record at split_index in the leaf
4995 * pointed to by path as written. This removes the unwritten
4998 * Care is taken to handle contiguousness so as to not grow the tree.
5000 * meta_ac is not strictly necessary - we only truly need it if growth
5001 * of the tree is required. All other cases will degrade into a less
5002 * optimal tree layout.
5004 * last_eb_bh should be the rightmost leaf block for any extent
5005 * btree. Since a split may grow the tree or a merge might shrink it,
5006 * the caller cannot trust the contents of that buffer after this call.
5008 * This code is optimized for readability - several passes might be
5009 * made over certain portions of the tree. All of those blocks will
5010 * have been brought into cache (and pinned via the journal), so the
5011 * extra overhead is not expressed in terms of disk reads.
5013 static int __ocfs2_mark_extent_written(struct inode *inode,
5014 struct ocfs2_extent_tree *et,
5016 struct ocfs2_path *path,
5018 struct ocfs2_extent_rec *split_rec,
5019 struct ocfs2_alloc_context *meta_ac,
5020 struct ocfs2_cached_dealloc_ctxt *dealloc)
5023 struct ocfs2_extent_list *el = path_leaf_el(path);
5024 struct buffer_head *last_eb_bh = NULL;
5025 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5026 struct ocfs2_merge_ctxt ctxt;
5027 struct ocfs2_extent_list *rightmost_el;
5029 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
5035 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5036 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5037 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5043 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
5048 * The core merge / split code wants to know how much room is
5049 * left in this inodes allocation tree, so we pass the
5050 * rightmost extent list.
5052 if (path->p_tree_depth) {
5053 struct ocfs2_extent_block *eb;
5055 ret = ocfs2_read_extent_block(et->et_ci,
5056 ocfs2_et_get_last_eb_blk(et),
5063 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5064 rightmost_el = &eb->h_list;
5066 rightmost_el = path_root_el(path);
5068 if (rec->e_cpos == split_rec->e_cpos &&
5069 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5070 ctxt.c_split_covers_rec = 1;
5072 ctxt.c_split_covers_rec = 0;
5074 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5076 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5077 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5078 ctxt.c_split_covers_rec);
5080 if (ctxt.c_contig_type == CONTIG_NONE) {
5081 if (ctxt.c_split_covers_rec)
5082 ret = ocfs2_replace_extent_rec(inode, handle,
5084 split_index, split_rec);
5086 ret = ocfs2_split_and_insert(inode, handle, path, et,
5087 &last_eb_bh, split_index,
5088 split_rec, meta_ac);
5092 ret = ocfs2_try_to_merge_extent(inode, handle, path,
5093 split_index, split_rec,
5094 dealloc, &ctxt, et);
5105 * Mark the already-existing extent at cpos as written for len clusters.
5107 * If the existing extent is larger than the request, initiate a
5108 * split. An attempt will be made at merging with adjacent extents.
5110 * The caller is responsible for passing down meta_ac if we'll need it.
5112 int ocfs2_mark_extent_written(struct inode *inode,
5113 struct ocfs2_extent_tree *et,
5114 handle_t *handle, u32 cpos, u32 len, u32 phys,
5115 struct ocfs2_alloc_context *meta_ac,
5116 struct ocfs2_cached_dealloc_ctxt *dealloc)
5119 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5120 struct ocfs2_extent_rec split_rec;
5121 struct ocfs2_path *left_path = NULL;
5122 struct ocfs2_extent_list *el;
5124 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5125 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5127 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5128 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5129 "that are being written to, but the feature bit "
5130 "is not set in the super block.",
5131 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5137 * XXX: This should be fixed up so that we just re-insert the
5138 * next extent records.
5140 * XXX: This is a hack on the extent tree, maybe it should be
5143 if (et->et_ops == &ocfs2_dinode_et_ops)
5144 ocfs2_extent_map_trunc(inode, 0);
5146 left_path = ocfs2_new_path_from_et(et);
5153 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
5158 el = path_leaf_el(left_path);
5160 index = ocfs2_search_extent_list(el, cpos);
5161 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5162 ocfs2_error(inode->i_sb,
5163 "Inode %llu has an extent at cpos %u which can no "
5164 "longer be found.\n",
5165 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5170 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5171 split_rec.e_cpos = cpu_to_le32(cpos);
5172 split_rec.e_leaf_clusters = cpu_to_le16(len);
5173 split_rec.e_blkno = cpu_to_le64(start_blkno);
5174 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5175 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5177 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
5178 index, &split_rec, meta_ac,
5184 ocfs2_free_path(left_path);
5188 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
5189 handle_t *handle, struct ocfs2_path *path,
5190 int index, u32 new_range,
5191 struct ocfs2_alloc_context *meta_ac)
5193 int ret, depth, credits = handle->h_buffer_credits;
5194 struct buffer_head *last_eb_bh = NULL;
5195 struct ocfs2_extent_block *eb;
5196 struct ocfs2_extent_list *rightmost_el, *el;
5197 struct ocfs2_extent_rec split_rec;
5198 struct ocfs2_extent_rec *rec;
5199 struct ocfs2_insert_type insert;
5202 * Setup the record to split before we grow the tree.
5204 el = path_leaf_el(path);
5205 rec = &el->l_recs[index];
5206 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5208 depth = path->p_tree_depth;
5210 ret = ocfs2_read_extent_block(et->et_ci,
5211 ocfs2_et_get_last_eb_blk(et),
5218 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5219 rightmost_el = &eb->h_list;
5221 rightmost_el = path_leaf_el(path);
5223 credits += path->p_tree_depth +
5224 ocfs2_extend_meta_needed(et->et_root_el);
5225 ret = ocfs2_extend_trans(handle, credits);
5231 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5232 le16_to_cpu(rightmost_el->l_count)) {
5233 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
5241 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5242 insert.ins_appending = APPEND_NONE;
5243 insert.ins_contig = CONTIG_NONE;
5244 insert.ins_split = SPLIT_RIGHT;
5245 insert.ins_tree_depth = depth;
5247 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
5256 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5257 struct ocfs2_path *path, int index,
5258 struct ocfs2_cached_dealloc_ctxt *dealloc,
5260 struct ocfs2_extent_tree *et)
5263 u32 left_cpos, rec_range, trunc_range;
5264 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5265 struct super_block *sb = inode->i_sb;
5266 struct ocfs2_path *left_path = NULL;
5267 struct ocfs2_extent_list *el = path_leaf_el(path);
5268 struct ocfs2_extent_rec *rec;
5269 struct ocfs2_extent_block *eb;
5271 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5272 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5281 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5282 path->p_tree_depth) {
5284 * Check whether this is the rightmost tree record. If
5285 * we remove all of this record or part of its right
5286 * edge then an update of the record lengths above it
5289 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5290 if (eb->h_next_leaf_blk == 0)
5291 is_rightmost_tree_rec = 1;
5294 rec = &el->l_recs[index];
5295 if (index == 0 && path->p_tree_depth &&
5296 le32_to_cpu(rec->e_cpos) == cpos) {
5298 * Changing the leftmost offset (via partial or whole
5299 * record truncate) of an interior (or rightmost) path
5300 * means we have to update the subtree that is formed
5301 * by this leaf and the one to it's left.
5303 * There are two cases we can skip:
5304 * 1) Path is the leftmost one in our inode tree.
5305 * 2) The leaf is rightmost and will be empty after
5306 * we remove the extent record - the rotate code
5307 * knows how to update the newly formed edge.
5310 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5317 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5318 left_path = ocfs2_new_path_from_path(path);
5325 ret = ocfs2_find_path(et->et_ci, left_path,
5334 ret = ocfs2_extend_rotate_transaction(handle, 0,
5335 handle->h_buffer_credits,
5342 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
5348 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
5354 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5355 trunc_range = cpos + len;
5357 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5360 memset(rec, 0, sizeof(*rec));
5361 ocfs2_cleanup_merge(el, index);
5364 next_free = le16_to_cpu(el->l_next_free_rec);
5365 if (is_rightmost_tree_rec && next_free > 1) {
5367 * We skip the edge update if this path will
5368 * be deleted by the rotate code.
5370 rec = &el->l_recs[next_free - 1];
5371 ocfs2_adjust_rightmost_records(inode, handle, path,
5374 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5375 /* Remove leftmost portion of the record. */
5376 le32_add_cpu(&rec->e_cpos, len);
5377 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5378 le16_add_cpu(&rec->e_leaf_clusters, -len);
5379 } else if (rec_range == trunc_range) {
5380 /* Remove rightmost portion of the record */
5381 le16_add_cpu(&rec->e_leaf_clusters, -len);
5382 if (is_rightmost_tree_rec)
5383 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5385 /* Caller should have trapped this. */
5386 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5387 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5388 le32_to_cpu(rec->e_cpos),
5389 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5396 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5397 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5401 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5403 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5410 ocfs2_free_path(left_path);
5414 int ocfs2_remove_extent(struct inode *inode,
5415 struct ocfs2_extent_tree *et,
5416 u32 cpos, u32 len, handle_t *handle,
5417 struct ocfs2_alloc_context *meta_ac,
5418 struct ocfs2_cached_dealloc_ctxt *dealloc)
5421 u32 rec_range, trunc_range;
5422 struct ocfs2_extent_rec *rec;
5423 struct ocfs2_extent_list *el;
5424 struct ocfs2_path *path = NULL;
5426 ocfs2_extent_map_trunc(inode, 0);
5428 path = ocfs2_new_path_from_et(et);
5435 ret = ocfs2_find_path(et->et_ci, path, cpos);
5441 el = path_leaf_el(path);
5442 index = ocfs2_search_extent_list(el, cpos);
5443 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5444 ocfs2_error(inode->i_sb,
5445 "Inode %llu has an extent at cpos %u which can no "
5446 "longer be found.\n",
5447 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5453 * We have 3 cases of extent removal:
5454 * 1) Range covers the entire extent rec
5455 * 2) Range begins or ends on one edge of the extent rec
5456 * 3) Range is in the middle of the extent rec (no shared edges)
5458 * For case 1 we remove the extent rec and left rotate to
5461 * For case 2 we just shrink the existing extent rec, with a
5462 * tree update if the shrinking edge is also the edge of an
5465 * For case 3 we do a right split to turn the extent rec into
5466 * something case 2 can handle.
5468 rec = &el->l_recs[index];
5469 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5470 trunc_range = cpos + len;
5472 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5474 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5475 "(cpos %u, len %u)\n",
5476 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5477 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5479 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5480 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5487 ret = ocfs2_split_tree(inode, et, handle, path, index,
5488 trunc_range, meta_ac);
5495 * The split could have manipulated the tree enough to
5496 * move the record location, so we have to look for it again.
5498 ocfs2_reinit_path(path, 1);
5500 ret = ocfs2_find_path(et->et_ci, path, cpos);
5506 el = path_leaf_el(path);
5507 index = ocfs2_search_extent_list(el, cpos);
5508 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5509 ocfs2_error(inode->i_sb,
5510 "Inode %llu: split at cpos %u lost record.",
5511 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5518 * Double check our values here. If anything is fishy,
5519 * it's easier to catch it at the top level.
5521 rec = &el->l_recs[index];
5522 rec_range = le32_to_cpu(rec->e_cpos) +
5523 ocfs2_rec_clusters(el, rec);
5524 if (rec_range != trunc_range) {
5525 ocfs2_error(inode->i_sb,
5526 "Inode %llu: error after split at cpos %u"
5527 "trunc len %u, existing record is (%u,%u)",
5528 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5529 cpos, len, le32_to_cpu(rec->e_cpos),
5530 ocfs2_rec_clusters(el, rec));
5535 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5544 ocfs2_free_path(path);
5548 int ocfs2_remove_btree_range(struct inode *inode,
5549 struct ocfs2_extent_tree *et,
5550 u32 cpos, u32 phys_cpos, u32 len,
5551 struct ocfs2_cached_dealloc_ctxt *dealloc)
5554 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5555 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5556 struct inode *tl_inode = osb->osb_tl_inode;
5558 struct ocfs2_alloc_context *meta_ac = NULL;
5560 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5566 mutex_lock(&tl_inode->i_mutex);
5568 if (ocfs2_truncate_log_needs_flush(osb)) {
5569 ret = __ocfs2_flush_truncate_log(osb);
5576 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5577 if (IS_ERR(handle)) {
5578 ret = PTR_ERR(handle);
5583 ret = ocfs2_et_root_journal_access(handle, et,
5584 OCFS2_JOURNAL_ACCESS_WRITE);
5590 vfs_dq_free_space_nodirty(inode,
5591 ocfs2_clusters_to_bytes(inode->i_sb, len));
5593 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5600 ocfs2_et_update_clusters(inode, et, -len);
5602 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5608 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5613 ocfs2_commit_trans(osb, handle);
5615 mutex_unlock(&tl_inode->i_mutex);
5618 ocfs2_free_alloc_context(meta_ac);
5623 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5625 struct buffer_head *tl_bh = osb->osb_tl_bh;
5626 struct ocfs2_dinode *di;
5627 struct ocfs2_truncate_log *tl;
5629 di = (struct ocfs2_dinode *) tl_bh->b_data;
5630 tl = &di->id2.i_dealloc;
5632 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5633 "slot %d, invalid truncate log parameters: used = "
5634 "%u, count = %u\n", osb->slot_num,
5635 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5636 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5639 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5640 unsigned int new_start)
5642 unsigned int tail_index;
5643 unsigned int current_tail;
5645 /* No records, nothing to coalesce */
5646 if (!le16_to_cpu(tl->tl_used))
5649 tail_index = le16_to_cpu(tl->tl_used) - 1;
5650 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5651 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5653 return current_tail == new_start;
5656 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5659 unsigned int num_clusters)
5662 unsigned int start_cluster, tl_count;
5663 struct inode *tl_inode = osb->osb_tl_inode;
5664 struct buffer_head *tl_bh = osb->osb_tl_bh;
5665 struct ocfs2_dinode *di;
5666 struct ocfs2_truncate_log *tl;
5668 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5669 (unsigned long long)start_blk, num_clusters);
5671 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5673 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5675 di = (struct ocfs2_dinode *) tl_bh->b_data;
5677 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5678 * by the underlying call to ocfs2_read_inode_block(), so any
5679 * corruption is a code bug */
5680 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5682 tl = &di->id2.i_dealloc;
5683 tl_count = le16_to_cpu(tl->tl_count);
5684 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5686 "Truncate record count on #%llu invalid "
5687 "wanted %u, actual %u\n",
5688 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5689 ocfs2_truncate_recs_per_inode(osb->sb),
5690 le16_to_cpu(tl->tl_count));
5692 /* Caller should have known to flush before calling us. */
5693 index = le16_to_cpu(tl->tl_used);
5694 if (index >= tl_count) {
5700 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5701 OCFS2_JOURNAL_ACCESS_WRITE);
5707 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5708 "%llu (index = %d)\n", num_clusters, start_cluster,
5709 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5711 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5713 * Move index back to the record we are coalescing with.
5714 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5718 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5719 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5720 index, le32_to_cpu(tl->tl_recs[index].t_start),
5723 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5724 tl->tl_used = cpu_to_le16(index + 1);
5726 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5728 status = ocfs2_journal_dirty(handle, tl_bh);
5739 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5741 struct inode *data_alloc_inode,
5742 struct buffer_head *data_alloc_bh)
5746 unsigned int num_clusters;
5748 struct ocfs2_truncate_rec rec;
5749 struct ocfs2_dinode *di;
5750 struct ocfs2_truncate_log *tl;
5751 struct inode *tl_inode = osb->osb_tl_inode;
5752 struct buffer_head *tl_bh = osb->osb_tl_bh;
5756 di = (struct ocfs2_dinode *) tl_bh->b_data;
5757 tl = &di->id2.i_dealloc;
5758 i = le16_to_cpu(tl->tl_used) - 1;
5760 /* Caller has given us at least enough credits to
5761 * update the truncate log dinode */
5762 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5763 OCFS2_JOURNAL_ACCESS_WRITE);
5769 tl->tl_used = cpu_to_le16(i);
5771 status = ocfs2_journal_dirty(handle, tl_bh);
5777 /* TODO: Perhaps we can calculate the bulk of the
5778 * credits up front rather than extending like
5780 status = ocfs2_extend_trans(handle,
5781 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5787 rec = tl->tl_recs[i];
5788 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5789 le32_to_cpu(rec.t_start));
5790 num_clusters = le32_to_cpu(rec.t_clusters);
5792 /* if start_blk is not set, we ignore the record as
5795 mlog(0, "free record %d, start = %u, clusters = %u\n",
5796 i, le32_to_cpu(rec.t_start), num_clusters);
5798 status = ocfs2_free_clusters(handle, data_alloc_inode,
5799 data_alloc_bh, start_blk,
5814 /* Expects you to already be holding tl_inode->i_mutex */
5815 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5818 unsigned int num_to_flush;
5820 struct inode *tl_inode = osb->osb_tl_inode;
5821 struct inode *data_alloc_inode = NULL;
5822 struct buffer_head *tl_bh = osb->osb_tl_bh;
5823 struct buffer_head *data_alloc_bh = NULL;
5824 struct ocfs2_dinode *di;
5825 struct ocfs2_truncate_log *tl;
5829 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5831 di = (struct ocfs2_dinode *) tl_bh->b_data;
5833 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5834 * by the underlying call to ocfs2_read_inode_block(), so any
5835 * corruption is a code bug */
5836 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5838 tl = &di->id2.i_dealloc;
5839 num_to_flush = le16_to_cpu(tl->tl_used);
5840 mlog(0, "Flush %u records from truncate log #%llu\n",
5841 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5842 if (!num_to_flush) {
5847 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5848 GLOBAL_BITMAP_SYSTEM_INODE,
5849 OCFS2_INVALID_SLOT);
5850 if (!data_alloc_inode) {
5852 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5856 mutex_lock(&data_alloc_inode->i_mutex);
5858 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5864 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5865 if (IS_ERR(handle)) {
5866 status = PTR_ERR(handle);
5871 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5876 ocfs2_commit_trans(osb, handle);
5879 brelse(data_alloc_bh);
5880 ocfs2_inode_unlock(data_alloc_inode, 1);
5883 mutex_unlock(&data_alloc_inode->i_mutex);
5884 iput(data_alloc_inode);
5891 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5894 struct inode *tl_inode = osb->osb_tl_inode;
5896 mutex_lock(&tl_inode->i_mutex);
5897 status = __ocfs2_flush_truncate_log(osb);
5898 mutex_unlock(&tl_inode->i_mutex);
5903 static void ocfs2_truncate_log_worker(struct work_struct *work)
5906 struct ocfs2_super *osb =
5907 container_of(work, struct ocfs2_super,
5908 osb_truncate_log_wq.work);
5912 status = ocfs2_flush_truncate_log(osb);
5916 ocfs2_init_inode_steal_slot(osb);
5921 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5922 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5925 if (osb->osb_tl_inode) {
5926 /* We want to push off log flushes while truncates are
5929 cancel_delayed_work(&osb->osb_truncate_log_wq);
5931 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5932 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5936 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5938 struct inode **tl_inode,
5939 struct buffer_head **tl_bh)
5942 struct inode *inode = NULL;
5943 struct buffer_head *bh = NULL;
5945 inode = ocfs2_get_system_file_inode(osb,
5946 TRUNCATE_LOG_SYSTEM_INODE,
5950 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5954 status = ocfs2_read_inode_block(inode, &bh);
5968 /* called during the 1st stage of node recovery. we stamp a clean
5969 * truncate log and pass back a copy for processing later. if the
5970 * truncate log does not require processing, a *tl_copy is set to
5972 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5974 struct ocfs2_dinode **tl_copy)
5977 struct inode *tl_inode = NULL;
5978 struct buffer_head *tl_bh = NULL;
5979 struct ocfs2_dinode *di;
5980 struct ocfs2_truncate_log *tl;
5984 mlog(0, "recover truncate log from slot %d\n", slot_num);
5986 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5992 di = (struct ocfs2_dinode *) tl_bh->b_data;
5994 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5995 * validated by the underlying call to ocfs2_read_inode_block(),
5996 * so any corruption is a code bug */
5997 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5999 tl = &di->id2.i_dealloc;
6000 if (le16_to_cpu(tl->tl_used)) {
6001 mlog(0, "We'll have %u logs to recover\n",
6002 le16_to_cpu(tl->tl_used));
6004 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6011 /* Assuming the write-out below goes well, this copy
6012 * will be passed back to recovery for processing. */
6013 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6015 /* All we need to do to clear the truncate log is set
6019 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
6020 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
6032 if (status < 0 && (*tl_copy)) {
6041 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6042 struct ocfs2_dinode *tl_copy)
6046 unsigned int clusters, num_recs, start_cluster;
6049 struct inode *tl_inode = osb->osb_tl_inode;
6050 struct ocfs2_truncate_log *tl;
6054 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6055 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6059 tl = &tl_copy->id2.i_dealloc;
6060 num_recs = le16_to_cpu(tl->tl_used);
6061 mlog(0, "cleanup %u records from %llu\n", num_recs,
6062 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
6064 mutex_lock(&tl_inode->i_mutex);
6065 for(i = 0; i < num_recs; i++) {
6066 if (ocfs2_truncate_log_needs_flush(osb)) {
6067 status = __ocfs2_flush_truncate_log(osb);
6074 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6075 if (IS_ERR(handle)) {
6076 status = PTR_ERR(handle);
6081 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6082 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6083 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6085 status = ocfs2_truncate_log_append(osb, handle,
6086 start_blk, clusters);
6087 ocfs2_commit_trans(osb, handle);
6095 mutex_unlock(&tl_inode->i_mutex);
6101 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6104 struct inode *tl_inode = osb->osb_tl_inode;
6109 cancel_delayed_work(&osb->osb_truncate_log_wq);
6110 flush_workqueue(ocfs2_wq);
6112 status = ocfs2_flush_truncate_log(osb);
6116 brelse(osb->osb_tl_bh);
6117 iput(osb->osb_tl_inode);
6123 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6126 struct inode *tl_inode = NULL;
6127 struct buffer_head *tl_bh = NULL;
6131 status = ocfs2_get_truncate_log_info(osb,
6138 /* ocfs2_truncate_log_shutdown keys on the existence of
6139 * osb->osb_tl_inode so we don't set any of the osb variables
6140 * until we're sure all is well. */
6141 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6142 ocfs2_truncate_log_worker);
6143 osb->osb_tl_bh = tl_bh;
6144 osb->osb_tl_inode = tl_inode;
6151 * Delayed de-allocation of suballocator blocks.
6153 * Some sets of block de-allocations might involve multiple suballocator inodes.
6155 * The locking for this can get extremely complicated, especially when
6156 * the suballocator inodes to delete from aren't known until deep
6157 * within an unrelated codepath.
6159 * ocfs2_extent_block structures are a good example of this - an inode
6160 * btree could have been grown by any number of nodes each allocating
6161 * out of their own suballoc inode.
6163 * These structures allow the delay of block de-allocation until a
6164 * later time, when locking of multiple cluster inodes won't cause
6169 * Describe a single bit freed from a suballocator. For the block
6170 * suballocators, it represents one block. For the global cluster
6171 * allocator, it represents some clusters and free_bit indicates
6174 struct ocfs2_cached_block_free {
6175 struct ocfs2_cached_block_free *free_next;
6177 unsigned int free_bit;
6180 struct ocfs2_per_slot_free_list {
6181 struct ocfs2_per_slot_free_list *f_next_suballocator;
6184 struct ocfs2_cached_block_free *f_first;
6187 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6190 struct ocfs2_cached_block_free *head)
6195 struct inode *inode;
6196 struct buffer_head *di_bh = NULL;
6197 struct ocfs2_cached_block_free *tmp;
6199 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6206 mutex_lock(&inode->i_mutex);
6208 ret = ocfs2_inode_lock(inode, &di_bh, 1);
6214 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6215 if (IS_ERR(handle)) {
6216 ret = PTR_ERR(handle);
6222 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6224 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6225 head->free_bit, (unsigned long long)head->free_blk);
6227 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6228 head->free_bit, bg_blkno, 1);
6234 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6241 head = head->free_next;
6246 ocfs2_commit_trans(osb, handle);
6249 ocfs2_inode_unlock(inode, 1);
6252 mutex_unlock(&inode->i_mutex);
6256 /* Premature exit may have left some dangling items. */
6258 head = head->free_next;
6265 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6266 u64 blkno, unsigned int bit)
6269 struct ocfs2_cached_block_free *item;
6271 item = kmalloc(sizeof(*item), GFP_NOFS);
6278 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6279 bit, (unsigned long long)blkno);
6281 item->free_blk = blkno;
6282 item->free_bit = bit;
6283 item->free_next = ctxt->c_global_allocator;
6285 ctxt->c_global_allocator = item;
6289 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6290 struct ocfs2_cached_block_free *head)
6292 struct ocfs2_cached_block_free *tmp;
6293 struct inode *tl_inode = osb->osb_tl_inode;
6297 mutex_lock(&tl_inode->i_mutex);
6300 if (ocfs2_truncate_log_needs_flush(osb)) {
6301 ret = __ocfs2_flush_truncate_log(osb);
6308 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6309 if (IS_ERR(handle)) {
6310 ret = PTR_ERR(handle);
6315 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6318 ocfs2_commit_trans(osb, handle);
6320 head = head->free_next;
6329 mutex_unlock(&tl_inode->i_mutex);
6332 /* Premature exit may have left some dangling items. */
6334 head = head->free_next;
6341 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6342 struct ocfs2_cached_dealloc_ctxt *ctxt)
6345 struct ocfs2_per_slot_free_list *fl;
6350 while (ctxt->c_first_suballocator) {
6351 fl = ctxt->c_first_suballocator;
6354 mlog(0, "Free items: (type %u, slot %d)\n",
6355 fl->f_inode_type, fl->f_slot);
6356 ret2 = ocfs2_free_cached_blocks(osb,
6366 ctxt->c_first_suballocator = fl->f_next_suballocator;
6370 if (ctxt->c_global_allocator) {
6371 ret2 = ocfs2_free_cached_clusters(osb,
6372 ctxt->c_global_allocator);
6378 ctxt->c_global_allocator = NULL;
6384 static struct ocfs2_per_slot_free_list *
6385 ocfs2_find_per_slot_free_list(int type,
6387 struct ocfs2_cached_dealloc_ctxt *ctxt)
6389 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6392 if (fl->f_inode_type == type && fl->f_slot == slot)
6395 fl = fl->f_next_suballocator;
6398 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6400 fl->f_inode_type = type;
6403 fl->f_next_suballocator = ctxt->c_first_suballocator;
6405 ctxt->c_first_suballocator = fl;
6410 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6411 int type, int slot, u64 blkno,
6415 struct ocfs2_per_slot_free_list *fl;
6416 struct ocfs2_cached_block_free *item;
6418 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6425 item = kmalloc(sizeof(*item), GFP_NOFS);
6432 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6433 type, slot, bit, (unsigned long long)blkno);
6435 item->free_blk = blkno;
6436 item->free_bit = bit;
6437 item->free_next = fl->f_first;
6446 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6447 struct ocfs2_extent_block *eb)
6449 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6450 le16_to_cpu(eb->h_suballoc_slot),
6451 le64_to_cpu(eb->h_blkno),
6452 le16_to_cpu(eb->h_suballoc_bit));
6455 /* This function will figure out whether the currently last extent
6456 * block will be deleted, and if it will, what the new last extent
6457 * block will be so we can update his h_next_leaf_blk field, as well
6458 * as the dinodes i_last_eb_blk */
6459 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6460 unsigned int clusters_to_del,
6461 struct ocfs2_path *path,
6462 struct buffer_head **new_last_eb)
6464 int next_free, ret = 0;
6466 struct ocfs2_extent_rec *rec;
6467 struct ocfs2_extent_block *eb;
6468 struct ocfs2_extent_list *el;
6469 struct buffer_head *bh = NULL;
6471 *new_last_eb = NULL;
6473 /* we have no tree, so of course, no last_eb. */
6474 if (!path->p_tree_depth)
6477 /* trunc to zero special case - this makes tree_depth = 0
6478 * regardless of what it is. */
6479 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6482 el = path_leaf_el(path);
6483 BUG_ON(!el->l_next_free_rec);
6486 * Make sure that this extent list will actually be empty
6487 * after we clear away the data. We can shortcut out if
6488 * there's more than one non-empty extent in the
6489 * list. Otherwise, a check of the remaining extent is
6492 next_free = le16_to_cpu(el->l_next_free_rec);
6494 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6498 /* We may have a valid extent in index 1, check it. */
6500 rec = &el->l_recs[1];
6503 * Fall through - no more nonempty extents, so we want
6504 * to delete this leaf.
6510 rec = &el->l_recs[0];
6515 * Check it we'll only be trimming off the end of this
6518 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6522 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6528 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
6534 eb = (struct ocfs2_extent_block *) bh->b_data;
6537 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6538 * Any corruption is a code bug. */
6539 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
6542 get_bh(*new_last_eb);
6543 mlog(0, "returning block %llu, (cpos: %u)\n",
6544 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6552 * Trim some clusters off the rightmost edge of a tree. Only called
6555 * The caller needs to:
6556 * - start journaling of each path component.
6557 * - compute and fully set up any new last ext block
6559 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6560 handle_t *handle, struct ocfs2_truncate_context *tc,
6561 u32 clusters_to_del, u64 *delete_start)
6563 int ret, i, index = path->p_tree_depth;
6566 struct buffer_head *bh;
6567 struct ocfs2_extent_list *el;
6568 struct ocfs2_extent_rec *rec;
6572 while (index >= 0) {
6573 bh = path->p_node[index].bh;
6574 el = path->p_node[index].el;
6576 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6577 index, (unsigned long long)bh->b_blocknr);
6579 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6582 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6583 ocfs2_error(inode->i_sb,
6584 "Inode %lu has invalid ext. block %llu",
6586 (unsigned long long)bh->b_blocknr);
6592 i = le16_to_cpu(el->l_next_free_rec) - 1;
6593 rec = &el->l_recs[i];
6595 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6596 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6597 ocfs2_rec_clusters(el, rec),
6598 (unsigned long long)le64_to_cpu(rec->e_blkno),
6599 le16_to_cpu(el->l_next_free_rec));
6601 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6603 if (le16_to_cpu(el->l_tree_depth) == 0) {
6605 * If the leaf block contains a single empty
6606 * extent and no records, we can just remove
6609 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6611 sizeof(struct ocfs2_extent_rec));
6612 el->l_next_free_rec = cpu_to_le16(0);
6618 * Remove any empty extents by shifting things
6619 * left. That should make life much easier on
6620 * the code below. This condition is rare
6621 * enough that we shouldn't see a performance
6624 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6625 le16_add_cpu(&el->l_next_free_rec, -1);
6628 i < le16_to_cpu(el->l_next_free_rec); i++)
6629 el->l_recs[i] = el->l_recs[i + 1];
6631 memset(&el->l_recs[i], 0,
6632 sizeof(struct ocfs2_extent_rec));
6635 * We've modified our extent list. The
6636 * simplest way to handle this change
6637 * is to being the search from the
6640 goto find_tail_record;
6643 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6646 * We'll use "new_edge" on our way back up the
6647 * tree to know what our rightmost cpos is.
6649 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6650 new_edge += le32_to_cpu(rec->e_cpos);
6653 * The caller will use this to delete data blocks.
6655 *delete_start = le64_to_cpu(rec->e_blkno)
6656 + ocfs2_clusters_to_blocks(inode->i_sb,
6657 le16_to_cpu(rec->e_leaf_clusters));
6660 * If it's now empty, remove this record.
6662 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6664 sizeof(struct ocfs2_extent_rec));
6665 le16_add_cpu(&el->l_next_free_rec, -1);
6668 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6670 sizeof(struct ocfs2_extent_rec));
6671 le16_add_cpu(&el->l_next_free_rec, -1);
6676 /* Can this actually happen? */
6677 if (le16_to_cpu(el->l_next_free_rec) == 0)
6681 * We never actually deleted any clusters
6682 * because our leaf was empty. There's no
6683 * reason to adjust the rightmost edge then.
6688 rec->e_int_clusters = cpu_to_le32(new_edge);
6689 le32_add_cpu(&rec->e_int_clusters,
6690 -le32_to_cpu(rec->e_cpos));
6693 * A deleted child record should have been
6696 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6700 ret = ocfs2_journal_dirty(handle, bh);
6706 mlog(0, "extent list container %llu, after: record %d: "
6707 "(%u, %u, %llu), next = %u.\n",
6708 (unsigned long long)bh->b_blocknr, i,
6709 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6710 (unsigned long long)le64_to_cpu(rec->e_blkno),
6711 le16_to_cpu(el->l_next_free_rec));
6714 * We must be careful to only attempt delete of an
6715 * extent block (and not the root inode block).
6717 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6718 struct ocfs2_extent_block *eb =
6719 (struct ocfs2_extent_block *)bh->b_data;
6722 * Save this for use when processing the
6725 deleted_eb = le64_to_cpu(eb->h_blkno);
6727 mlog(0, "deleting this extent block.\n");
6729 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
6731 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6732 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6733 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6735 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6736 /* An error here is not fatal. */
6751 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6752 unsigned int clusters_to_del,
6753 struct inode *inode,
6754 struct buffer_head *fe_bh,
6756 struct ocfs2_truncate_context *tc,
6757 struct ocfs2_path *path)
6760 struct ocfs2_dinode *fe;
6761 struct ocfs2_extent_block *last_eb = NULL;
6762 struct ocfs2_extent_list *el;
6763 struct buffer_head *last_eb_bh = NULL;
6766 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6768 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6776 * Each component will be touched, so we might as well journal
6777 * here to avoid having to handle errors later.
6779 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
6786 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
6787 OCFS2_JOURNAL_ACCESS_WRITE);
6793 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6796 el = &(fe->id2.i_list);
6799 * Lower levels depend on this never happening, but it's best
6800 * to check it up here before changing the tree.
6802 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6803 ocfs2_error(inode->i_sb,
6804 "Inode %lu has an empty extent record, depth %u\n",
6805 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6810 vfs_dq_free_space_nodirty(inode,
6811 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
6812 spin_lock(&OCFS2_I(inode)->ip_lock);
6813 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6815 spin_unlock(&OCFS2_I(inode)->ip_lock);
6816 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6817 inode->i_blocks = ocfs2_inode_sector_count(inode);
6819 status = ocfs2_trim_tree(inode, path, handle, tc,
6820 clusters_to_del, &delete_blk);
6826 if (le32_to_cpu(fe->i_clusters) == 0) {
6827 /* trunc to zero is a special case. */
6828 el->l_tree_depth = 0;
6829 fe->i_last_eb_blk = 0;
6831 fe->i_last_eb_blk = last_eb->h_blkno;
6833 status = ocfs2_journal_dirty(handle, fe_bh);
6840 /* If there will be a new last extent block, then by
6841 * definition, there cannot be any leaves to the right of
6843 last_eb->h_next_leaf_blk = 0;
6844 status = ocfs2_journal_dirty(handle, last_eb_bh);
6852 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6866 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6868 set_buffer_uptodate(bh);
6869 mark_buffer_dirty(bh);
6873 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6874 unsigned int from, unsigned int to,
6875 struct page *page, int zero, u64 *phys)
6877 int ret, partial = 0;
6879 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6884 zero_user_segment(page, from, to);
6887 * Need to set the buffers we zero'd into uptodate
6888 * here if they aren't - ocfs2_map_page_blocks()
6889 * might've skipped some
6891 ret = walk_page_buffers(handle, page_buffers(page),
6896 else if (ocfs2_should_order_data(inode)) {
6897 ret = ocfs2_jbd2_file_inode(handle, inode);
6903 SetPageUptodate(page);
6905 flush_dcache_page(page);
6908 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6909 loff_t end, struct page **pages,
6910 int numpages, u64 phys, handle_t *handle)
6914 unsigned int from, to = PAGE_CACHE_SIZE;
6915 struct super_block *sb = inode->i_sb;
6917 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6922 to = PAGE_CACHE_SIZE;
6923 for(i = 0; i < numpages; i++) {
6926 from = start & (PAGE_CACHE_SIZE - 1);
6927 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6928 to = end & (PAGE_CACHE_SIZE - 1);
6930 BUG_ON(from > PAGE_CACHE_SIZE);
6931 BUG_ON(to > PAGE_CACHE_SIZE);
6933 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6936 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6940 ocfs2_unlock_and_free_pages(pages, numpages);
6943 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6944 struct page **pages, int *num)
6946 int numpages, ret = 0;
6947 struct super_block *sb = inode->i_sb;
6948 struct address_space *mapping = inode->i_mapping;
6949 unsigned long index;
6950 loff_t last_page_bytes;
6952 BUG_ON(start > end);
6954 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6955 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6958 last_page_bytes = PAGE_ALIGN(end);
6959 index = start >> PAGE_CACHE_SHIFT;
6961 pages[numpages] = grab_cache_page(mapping, index);
6962 if (!pages[numpages]) {
6970 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6975 ocfs2_unlock_and_free_pages(pages, numpages);
6985 * Zero the area past i_size but still within an allocated
6986 * cluster. This avoids exposing nonzero data on subsequent file
6989 * We need to call this before i_size is updated on the inode because
6990 * otherwise block_write_full_page() will skip writeout of pages past
6991 * i_size. The new_i_size parameter is passed for this reason.
6993 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6994 u64 range_start, u64 range_end)
6996 int ret = 0, numpages;
6997 struct page **pages = NULL;
6999 unsigned int ext_flags;
7000 struct super_block *sb = inode->i_sb;
7003 * File systems which don't support sparse files zero on every
7006 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
7009 pages = kcalloc(ocfs2_pages_per_cluster(sb),
7010 sizeof(struct page *), GFP_NOFS);
7011 if (pages == NULL) {
7017 if (range_start == range_end)
7020 ret = ocfs2_extent_map_get_blocks(inode,
7021 range_start >> sb->s_blocksize_bits,
7022 &phys, NULL, &ext_flags);
7029 * Tail is a hole, or is marked unwritten. In either case, we
7030 * can count on read and write to return/push zero's.
7032 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
7035 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7042 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7043 numpages, phys, handle);
7046 * Initiate writeout of the pages we zero'd here. We don't
7047 * wait on them - the truncate_inode_pages() call later will
7050 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7051 range_end - 1, SYNC_FILE_RANGE_WRITE);
7062 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7063 struct ocfs2_dinode *di)
7065 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
7066 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
7068 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7069 memset(&di->id2, 0, blocksize -
7070 offsetof(struct ocfs2_dinode, id2) -
7073 memset(&di->id2, 0, blocksize -
7074 offsetof(struct ocfs2_dinode, id2));
7077 void ocfs2_dinode_new_extent_list(struct inode *inode,
7078 struct ocfs2_dinode *di)
7080 ocfs2_zero_dinode_id2_with_xattr(inode, di);
7081 di->id2.i_list.l_tree_depth = 0;
7082 di->id2.i_list.l_next_free_rec = 0;
7083 di->id2.i_list.l_count = cpu_to_le16(
7084 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
7087 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7089 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7090 struct ocfs2_inline_data *idata = &di->id2.i_data;
7092 spin_lock(&oi->ip_lock);
7093 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7094 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7095 spin_unlock(&oi->ip_lock);
7098 * We clear the entire i_data structure here so that all
7099 * fields can be properly initialized.
7101 ocfs2_zero_dinode_id2_with_xattr(inode, di);
7103 idata->id_count = cpu_to_le16(
7104 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
7107 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7108 struct buffer_head *di_bh)
7110 int ret, i, has_data, num_pages = 0;
7112 u64 uninitialized_var(block);
7113 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7114 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7115 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7116 struct ocfs2_alloc_context *data_ac = NULL;
7117 struct page **pages = NULL;
7118 loff_t end = osb->s_clustersize;
7119 struct ocfs2_extent_tree et;
7122 has_data = i_size_read(inode) ? 1 : 0;
7125 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7126 sizeof(struct page *), GFP_NOFS);
7127 if (pages == NULL) {
7133 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7140 handle = ocfs2_start_trans(osb,
7141 ocfs2_inline_to_extents_credits(osb->sb));
7142 if (IS_ERR(handle)) {
7143 ret = PTR_ERR(handle);
7148 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7149 OCFS2_JOURNAL_ACCESS_WRITE);
7157 unsigned int page_end;
7160 if (vfs_dq_alloc_space_nodirty(inode,
7161 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7167 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7175 * Save two copies, one for insert, and one that can
7176 * be changed by ocfs2_map_and_dirty_page() below.
7178 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7181 * Non sparse file systems zero on extend, so no need
7184 if (!ocfs2_sparse_alloc(osb) &&
7185 PAGE_CACHE_SIZE < osb->s_clustersize)
7186 end = PAGE_CACHE_SIZE;
7188 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7195 * This should populate the 1st page for us and mark
7198 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7204 page_end = PAGE_CACHE_SIZE;
7205 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7206 page_end = osb->s_clustersize;
7208 for (i = 0; i < num_pages; i++)
7209 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7210 pages[i], i > 0, &phys);
7213 spin_lock(&oi->ip_lock);
7214 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7215 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7216 spin_unlock(&oi->ip_lock);
7218 ocfs2_dinode_new_extent_list(inode, di);
7220 ocfs2_journal_dirty(handle, di_bh);
7224 * An error at this point should be extremely rare. If
7225 * this proves to be false, we could always re-build
7226 * the in-inode data from our pages.
7228 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
7229 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7230 0, block, 1, 0, NULL);
7236 inode->i_blocks = ocfs2_inode_sector_count(inode);
7240 if (ret < 0 && did_quota)
7241 vfs_dq_free_space_nodirty(inode,
7242 ocfs2_clusters_to_bytes(osb->sb, 1));
7244 ocfs2_commit_trans(osb, handle);
7248 ocfs2_free_alloc_context(data_ac);
7252 ocfs2_unlock_and_free_pages(pages, num_pages);
7260 * It is expected, that by the time you call this function,
7261 * inode->i_size and fe->i_size have been adjusted.
7263 * WARNING: This will kfree the truncate context
7265 int ocfs2_commit_truncate(struct ocfs2_super *osb,
7266 struct inode *inode,
7267 struct buffer_head *fe_bh,
7268 struct ocfs2_truncate_context *tc)
7270 int status, i, credits, tl_sem = 0;
7271 u32 clusters_to_del, new_highest_cpos, range;
7272 struct ocfs2_extent_list *el;
7273 handle_t *handle = NULL;
7274 struct inode *tl_inode = osb->osb_tl_inode;
7275 struct ocfs2_path *path = NULL;
7276 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
7280 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
7281 i_size_read(inode));
7283 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7284 ocfs2_journal_access_di);
7291 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7295 * Check that we still have allocation to delete.
7297 if (OCFS2_I(inode)->ip_clusters == 0) {
7303 * Truncate always works against the rightmost tree branch.
7305 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
7311 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7312 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7315 * By now, el will point to the extent list on the bottom most
7316 * portion of this tree. Only the tail record is considered in
7319 * We handle the following cases, in order:
7320 * - empty extent: delete the remaining branch
7321 * - remove the entire record
7322 * - remove a partial record
7323 * - no record needs to be removed (truncate has completed)
7325 el = path_leaf_el(path);
7326 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7327 ocfs2_error(inode->i_sb,
7328 "Inode %llu has empty extent block at %llu\n",
7329 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7330 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7335 i = le16_to_cpu(el->l_next_free_rec) - 1;
7336 range = le32_to_cpu(el->l_recs[i].e_cpos) +
7337 ocfs2_rec_clusters(el, &el->l_recs[i]);
7338 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7339 clusters_to_del = 0;
7340 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
7341 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
7342 } else if (range > new_highest_cpos) {
7343 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
7344 le32_to_cpu(el->l_recs[i].e_cpos)) -
7351 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7352 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7354 mutex_lock(&tl_inode->i_mutex);
7356 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7357 * record is free for use. If there isn't any, we flush to get
7358 * an empty truncate log. */
7359 if (ocfs2_truncate_log_needs_flush(osb)) {
7360 status = __ocfs2_flush_truncate_log(osb);
7367 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
7368 (struct ocfs2_dinode *)fe_bh->b_data,
7370 handle = ocfs2_start_trans(osb, credits);
7371 if (IS_ERR(handle)) {
7372 status = PTR_ERR(handle);
7378 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7385 mutex_unlock(&tl_inode->i_mutex);
7388 ocfs2_commit_trans(osb, handle);
7391 ocfs2_reinit_path(path, 1);
7394 * The check above will catch the case where we've truncated
7395 * away all allocation.
7401 ocfs2_schedule_truncate_log_flush(osb, 1);
7404 mutex_unlock(&tl_inode->i_mutex);
7407 ocfs2_commit_trans(osb, handle);
7409 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7411 ocfs2_free_path(path);
7413 /* This will drop the ext_alloc cluster lock for us */
7414 ocfs2_free_truncate_context(tc);
7421 * Expects the inode to already be locked.
7423 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7424 struct inode *inode,
7425 struct buffer_head *fe_bh,
7426 struct ocfs2_truncate_context **tc)
7429 unsigned int new_i_clusters;
7430 struct ocfs2_dinode *fe;
7431 struct ocfs2_extent_block *eb;
7432 struct buffer_head *last_eb_bh = NULL;
7438 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7439 i_size_read(inode));
7440 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7442 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7443 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7444 (unsigned long long)le64_to_cpu(fe->i_size));
7446 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7452 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7454 if (fe->id2.i_list.l_tree_depth) {
7455 status = ocfs2_read_extent_block(INODE_CACHE(inode),
7456 le64_to_cpu(fe->i_last_eb_blk),
7462 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7465 (*tc)->tc_last_eb_bh = last_eb_bh;
7471 ocfs2_free_truncate_context(*tc);
7479 * 'start' is inclusive, 'end' is not.
7481 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7482 unsigned int start, unsigned int end, int trunc)
7485 unsigned int numbytes;
7487 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7488 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7489 struct ocfs2_inline_data *idata = &di->id2.i_data;
7491 if (end > i_size_read(inode))
7492 end = i_size_read(inode);
7494 BUG_ON(start >= end);
7496 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7497 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7498 !ocfs2_supports_inline_data(osb)) {
7499 ocfs2_error(inode->i_sb,
7500 "Inline data flags for inode %llu don't agree! "
7501 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7502 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7503 le16_to_cpu(di->i_dyn_features),
7504 OCFS2_I(inode)->ip_dyn_features,
7505 osb->s_feature_incompat);
7510 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7511 if (IS_ERR(handle)) {
7512 ret = PTR_ERR(handle);
7517 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7518 OCFS2_JOURNAL_ACCESS_WRITE);
7524 numbytes = end - start;
7525 memset(idata->id_data + start, 0, numbytes);
7528 * No need to worry about the data page here - it's been
7529 * truncated already and inline data doesn't need it for
7530 * pushing zero's to disk, so we'll let readpage pick it up
7534 i_size_write(inode, start);
7535 di->i_size = cpu_to_le64(start);
7538 inode->i_blocks = ocfs2_inode_sector_count(inode);
7539 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7541 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7542 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7544 ocfs2_journal_dirty(handle, di_bh);
7547 ocfs2_commit_trans(osb, handle);
7553 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7556 * The caller is responsible for completing deallocation
7557 * before freeing the context.
7559 if (tc->tc_dealloc.c_first_suballocator != NULL)
7561 "Truncate completion has non-empty dealloc context\n");
7563 brelse(tc->tc_last_eb_bh);