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 ocfs2_extent_tree *et,
86 * If ->eo_insert_check() exists, it is called before rec is
87 * inserted into the extent tree. It is optional.
89 int (*eo_insert_check)(struct ocfs2_extent_tree *et,
90 struct ocfs2_extent_rec *rec);
91 int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
94 * --------------------------------------------------------------
95 * The remaining are internal to ocfs2_extent_tree and don't have
100 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
103 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
106 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
107 * it exists. If it does not, et->et_max_leaf_clusters is set
108 * to 0 (unlimited). Optional.
110 void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
115 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
118 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
119 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
121 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
123 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
124 struct ocfs2_extent_rec *rec);
125 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
126 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
127 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
128 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
129 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
130 .eo_update_clusters = ocfs2_dinode_update_clusters,
131 .eo_insert_check = ocfs2_dinode_insert_check,
132 .eo_sanity_check = ocfs2_dinode_sanity_check,
133 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
136 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
139 struct ocfs2_dinode *di = et->et_object;
141 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
142 di->i_last_eb_blk = cpu_to_le64(blkno);
145 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
147 struct ocfs2_dinode *di = et->et_object;
149 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
150 return le64_to_cpu(di->i_last_eb_blk);
153 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
156 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
157 struct ocfs2_dinode *di = et->et_object;
159 le32_add_cpu(&di->i_clusters, clusters);
160 spin_lock(&oi->ip_lock);
161 oi->ip_clusters = le32_to_cpu(di->i_clusters);
162 spin_unlock(&oi->ip_lock);
165 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
166 struct ocfs2_extent_rec *rec)
168 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
169 struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
171 BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
172 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
173 (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
174 "Device %s, asking for sparse allocation: inode %llu, "
175 "cpos %u, clusters %u\n",
177 (unsigned long long)oi->ip_blkno,
178 rec->e_cpos, oi->ip_clusters);
183 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
185 struct ocfs2_dinode *di = et->et_object;
187 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
188 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
193 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
195 struct ocfs2_dinode *di = et->et_object;
197 et->et_root_el = &di->id2.i_list;
201 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
203 struct ocfs2_xattr_value_buf *vb = et->et_object;
205 et->et_root_el = &vb->vb_xv->xr_list;
208 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
211 struct ocfs2_xattr_value_buf *vb = et->et_object;
213 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
216 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
218 struct ocfs2_xattr_value_buf *vb = et->et_object;
220 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
223 static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
226 struct ocfs2_xattr_value_buf *vb = et->et_object;
228 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
231 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
232 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
233 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
234 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
235 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
238 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
240 struct ocfs2_xattr_block *xb = et->et_object;
242 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
245 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
247 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
248 et->et_max_leaf_clusters =
249 ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
252 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
255 struct ocfs2_xattr_block *xb = et->et_object;
256 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
258 xt->xt_last_eb_blk = cpu_to_le64(blkno);
261 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
263 struct ocfs2_xattr_block *xb = et->et_object;
264 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
266 return le64_to_cpu(xt->xt_last_eb_blk);
269 static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
272 struct ocfs2_xattr_block *xb = et->et_object;
274 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
277 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
278 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
279 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
280 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
281 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
282 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
285 static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
288 struct ocfs2_dx_root_block *dx_root = et->et_object;
290 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
293 static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
295 struct ocfs2_dx_root_block *dx_root = et->et_object;
297 return le64_to_cpu(dx_root->dr_last_eb_blk);
300 static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
303 struct ocfs2_dx_root_block *dx_root = et->et_object;
305 le32_add_cpu(&dx_root->dr_clusters, clusters);
308 static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
310 struct ocfs2_dx_root_block *dx_root = et->et_object;
312 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
317 static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
319 struct ocfs2_dx_root_block *dx_root = et->et_object;
321 et->et_root_el = &dx_root->dr_list;
324 static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
325 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
326 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
327 .eo_update_clusters = ocfs2_dx_root_update_clusters,
328 .eo_sanity_check = ocfs2_dx_root_sanity_check,
329 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
332 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
334 struct buffer_head *bh,
335 ocfs2_journal_access_func access,
337 struct ocfs2_extent_tree_operations *ops)
341 et->et_ci = INODE_CACHE(inode);
342 et->et_root_journal_access = access;
344 obj = (void *)bh->b_data;
347 et->et_ops->eo_fill_root_el(et);
348 if (!et->et_ops->eo_fill_max_leaf_clusters)
349 et->et_max_leaf_clusters = 0;
351 et->et_ops->eo_fill_max_leaf_clusters(et);
354 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
356 struct buffer_head *bh)
358 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di,
359 NULL, &ocfs2_dinode_et_ops);
362 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
364 struct buffer_head *bh)
366 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb,
367 NULL, &ocfs2_xattr_tree_et_ops);
370 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
372 struct ocfs2_xattr_value_buf *vb)
374 __ocfs2_init_extent_tree(et, inode, vb->vb_bh, vb->vb_access, vb,
375 &ocfs2_xattr_value_et_ops);
378 void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
380 struct buffer_head *bh)
382 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_dr,
383 NULL, &ocfs2_dx_root_et_ops);
386 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
389 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
392 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
394 return et->et_ops->eo_get_last_eb_blk(et);
397 static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
400 et->et_ops->eo_update_clusters(et, clusters);
403 static inline int ocfs2_et_root_journal_access(handle_t *handle,
404 struct ocfs2_extent_tree *et,
407 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
411 static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
412 struct ocfs2_extent_rec *rec)
416 if (et->et_ops->eo_insert_check)
417 ret = et->et_ops->eo_insert_check(et, rec);
421 static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
425 if (et->et_ops->eo_sanity_check)
426 ret = et->et_ops->eo_sanity_check(et);
430 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
431 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
432 struct ocfs2_extent_block *eb);
435 * Structures which describe a path through a btree, and functions to
438 * The idea here is to be as generic as possible with the tree
441 struct ocfs2_path_item {
442 struct buffer_head *bh;
443 struct ocfs2_extent_list *el;
446 #define OCFS2_MAX_PATH_DEPTH 5
450 ocfs2_journal_access_func p_root_access;
451 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
454 #define path_root_bh(_path) ((_path)->p_node[0].bh)
455 #define path_root_el(_path) ((_path)->p_node[0].el)
456 #define path_root_access(_path)((_path)->p_root_access)
457 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
458 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
459 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
461 static int ocfs2_find_path(struct ocfs2_caching_info *ci,
462 struct ocfs2_path *path, u32 cpos);
463 static void ocfs2_adjust_rightmost_records(handle_t *handle,
464 struct ocfs2_extent_tree *et,
465 struct ocfs2_path *path,
466 struct ocfs2_extent_rec *insert_rec);
468 * Reset the actual path elements so that we can re-use the structure
469 * to build another path. Generally, this involves freeing the buffer
472 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
474 int i, start = 0, depth = 0;
475 struct ocfs2_path_item *node;
480 for(i = start; i < path_num_items(path); i++) {
481 node = &path->p_node[i];
489 * Tree depth may change during truncate, or insert. If we're
490 * keeping the root extent list, then make sure that our path
491 * structure reflects the proper depth.
494 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
496 path_root_access(path) = NULL;
498 path->p_tree_depth = depth;
501 static void ocfs2_free_path(struct ocfs2_path *path)
504 ocfs2_reinit_path(path, 0);
510 * All the elements of src into dest. After this call, src could be freed
511 * without affecting dest.
513 * Both paths should have the same root. Any non-root elements of dest
516 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
520 BUG_ON(path_root_bh(dest) != path_root_bh(src));
521 BUG_ON(path_root_el(dest) != path_root_el(src));
522 BUG_ON(path_root_access(dest) != path_root_access(src));
524 ocfs2_reinit_path(dest, 1);
526 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
527 dest->p_node[i].bh = src->p_node[i].bh;
528 dest->p_node[i].el = src->p_node[i].el;
530 if (dest->p_node[i].bh)
531 get_bh(dest->p_node[i].bh);
536 * Make the *dest path the same as src and re-initialize src path to
539 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
543 BUG_ON(path_root_bh(dest) != path_root_bh(src));
544 BUG_ON(path_root_access(dest) != path_root_access(src));
546 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
547 brelse(dest->p_node[i].bh);
549 dest->p_node[i].bh = src->p_node[i].bh;
550 dest->p_node[i].el = src->p_node[i].el;
552 src->p_node[i].bh = NULL;
553 src->p_node[i].el = NULL;
558 * Insert an extent block at given index.
560 * This will not take an additional reference on eb_bh.
562 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
563 struct buffer_head *eb_bh)
565 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
568 * Right now, no root bh is an extent block, so this helps
569 * catch code errors with dinode trees. The assertion can be
570 * safely removed if we ever need to insert extent block
571 * structures at the root.
575 path->p_node[index].bh = eb_bh;
576 path->p_node[index].el = &eb->h_list;
579 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
580 struct ocfs2_extent_list *root_el,
581 ocfs2_journal_access_func access)
583 struct ocfs2_path *path;
585 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
587 path = kzalloc(sizeof(*path), GFP_NOFS);
589 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
591 path_root_bh(path) = root_bh;
592 path_root_el(path) = root_el;
593 path_root_access(path) = access;
599 static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
601 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
602 path_root_access(path));
605 static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
607 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
608 et->et_root_journal_access);
612 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
613 * otherwise it's the root_access function.
615 * I don't like the way this function's name looks next to
616 * ocfs2_journal_access_path(), but I don't have a better one.
618 static int ocfs2_path_bh_journal_access(handle_t *handle,
619 struct ocfs2_caching_info *ci,
620 struct ocfs2_path *path,
623 ocfs2_journal_access_func access = path_root_access(path);
626 access = ocfs2_journal_access;
629 access = ocfs2_journal_access_eb;
631 return access(handle, ci, path->p_node[idx].bh,
632 OCFS2_JOURNAL_ACCESS_WRITE);
636 * Convenience function to journal all components in a path.
638 static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
640 struct ocfs2_path *path)
647 for(i = 0; i < path_num_items(path); i++) {
648 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
660 * Return the index of the extent record which contains cluster #v_cluster.
661 * -1 is returned if it was not found.
663 * Should work fine on interior and exterior nodes.
665 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
669 struct ocfs2_extent_rec *rec;
670 u32 rec_end, rec_start, clusters;
672 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
673 rec = &el->l_recs[i];
675 rec_start = le32_to_cpu(rec->e_cpos);
676 clusters = ocfs2_rec_clusters(el, rec);
678 rec_end = rec_start + clusters;
680 if (v_cluster >= rec_start && v_cluster < rec_end) {
689 enum ocfs2_contig_type {
698 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
699 * ocfs2_extent_contig only work properly against leaf nodes!
701 static int ocfs2_block_extent_contig(struct super_block *sb,
702 struct ocfs2_extent_rec *ext,
705 u64 blk_end = le64_to_cpu(ext->e_blkno);
707 blk_end += ocfs2_clusters_to_blocks(sb,
708 le16_to_cpu(ext->e_leaf_clusters));
710 return blkno == blk_end;
713 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
714 struct ocfs2_extent_rec *right)
718 left_range = le32_to_cpu(left->e_cpos) +
719 le16_to_cpu(left->e_leaf_clusters);
721 return (left_range == le32_to_cpu(right->e_cpos));
724 static enum ocfs2_contig_type
725 ocfs2_extent_contig(struct inode *inode,
726 struct ocfs2_extent_rec *ext,
727 struct ocfs2_extent_rec *insert_rec)
729 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
732 * Refuse to coalesce extent records with different flag
733 * fields - we don't want to mix unwritten extents with user
736 if (ext->e_flags != insert_rec->e_flags)
739 if (ocfs2_extents_adjacent(ext, insert_rec) &&
740 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
743 blkno = le64_to_cpu(ext->e_blkno);
744 if (ocfs2_extents_adjacent(insert_rec, ext) &&
745 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
752 * NOTE: We can have pretty much any combination of contiguousness and
755 * The usefulness of APPEND_TAIL is more in that it lets us know that
756 * we'll have to update the path to that leaf.
758 enum ocfs2_append_type {
763 enum ocfs2_split_type {
769 struct ocfs2_insert_type {
770 enum ocfs2_split_type ins_split;
771 enum ocfs2_append_type ins_appending;
772 enum ocfs2_contig_type ins_contig;
773 int ins_contig_index;
777 struct ocfs2_merge_ctxt {
778 enum ocfs2_contig_type c_contig_type;
779 int c_has_empty_extent;
780 int c_split_covers_rec;
783 static int ocfs2_validate_extent_block(struct super_block *sb,
784 struct buffer_head *bh)
787 struct ocfs2_extent_block *eb =
788 (struct ocfs2_extent_block *)bh->b_data;
790 mlog(0, "Validating extent block %llu\n",
791 (unsigned long long)bh->b_blocknr);
793 BUG_ON(!buffer_uptodate(bh));
796 * If the ecc fails, we return the error but otherwise
797 * leave the filesystem running. We know any error is
798 * local to this block.
800 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
802 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
803 (unsigned long long)bh->b_blocknr);
808 * Errors after here are fatal.
811 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
813 "Extent block #%llu has bad signature %.*s",
814 (unsigned long long)bh->b_blocknr, 7,
819 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
821 "Extent block #%llu has an invalid h_blkno "
823 (unsigned long long)bh->b_blocknr,
824 (unsigned long long)le64_to_cpu(eb->h_blkno));
828 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
830 "Extent block #%llu has an invalid "
831 "h_fs_generation of #%u",
832 (unsigned long long)bh->b_blocknr,
833 le32_to_cpu(eb->h_fs_generation));
840 int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
841 struct buffer_head **bh)
844 struct buffer_head *tmp = *bh;
846 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
847 ocfs2_validate_extent_block);
849 /* If ocfs2_read_block() got us a new bh, pass it up. */
858 * How many free extents have we got before we need more meta data?
860 int ocfs2_num_free_extents(struct ocfs2_super *osb,
861 struct ocfs2_extent_tree *et)
864 struct ocfs2_extent_list *el = NULL;
865 struct ocfs2_extent_block *eb;
866 struct buffer_head *eb_bh = NULL;
872 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
875 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
881 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
885 BUG_ON(el->l_tree_depth != 0);
887 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
895 /* expects array to already be allocated
897 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
900 static int ocfs2_create_new_meta_bhs(handle_t *handle,
901 struct ocfs2_extent_tree *et,
903 struct ocfs2_alloc_context *meta_ac,
904 struct buffer_head *bhs[])
906 int count, status, i;
907 u16 suballoc_bit_start;
910 struct ocfs2_super *osb =
911 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
912 struct ocfs2_extent_block *eb;
917 while (count < wanted) {
918 status = ocfs2_claim_metadata(osb,
930 for(i = count; i < (num_got + count); i++) {
931 bhs[i] = sb_getblk(osb->sb, first_blkno);
932 if (bhs[i] == NULL) {
937 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
939 status = ocfs2_journal_access_eb(handle, et->et_ci,
941 OCFS2_JOURNAL_ACCESS_CREATE);
947 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
948 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
949 /* Ok, setup the minimal stuff here. */
950 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
951 eb->h_blkno = cpu_to_le64(first_blkno);
952 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
953 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
954 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
956 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
958 suballoc_bit_start++;
961 /* We'll also be dirtied by the caller, so
962 * this isn't absolutely necessary. */
963 status = ocfs2_journal_dirty(handle, bhs[i]);
976 for(i = 0; i < wanted; i++) {
986 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
988 * Returns the sum of the rightmost extent rec logical offset and
991 * ocfs2_add_branch() uses this to determine what logical cluster
992 * value should be populated into the leftmost new branch records.
994 * ocfs2_shift_tree_depth() uses this to determine the # clusters
995 * value for the new topmost tree record.
997 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1001 i = le16_to_cpu(el->l_next_free_rec) - 1;
1003 return le32_to_cpu(el->l_recs[i].e_cpos) +
1004 ocfs2_rec_clusters(el, &el->l_recs[i]);
1008 * Change range of the branches in the right most path according to the leaf
1009 * extent block's rightmost record.
1011 static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1012 struct ocfs2_extent_tree *et)
1015 struct ocfs2_path *path = NULL;
1016 struct ocfs2_extent_list *el;
1017 struct ocfs2_extent_rec *rec;
1019 path = ocfs2_new_path_from_et(et);
1025 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
1031 status = ocfs2_extend_trans(handle, path_num_items(path) +
1032 handle->h_buffer_credits);
1038 status = ocfs2_journal_access_path(et->et_ci, handle, path);
1044 el = path_leaf_el(path);
1045 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1047 ocfs2_adjust_rightmost_records(handle, et, path, rec);
1050 ocfs2_free_path(path);
1055 * Add an entire tree branch to our inode. eb_bh is the extent block
1056 * to start at, if we don't want to start the branch at the root
1059 * last_eb_bh is required as we have to update it's next_leaf pointer
1060 * for the new last extent block.
1062 * the new branch will be 'empty' in the sense that every block will
1063 * contain a single record with cluster count == 0.
1065 static int ocfs2_add_branch(handle_t *handle,
1066 struct ocfs2_extent_tree *et,
1067 struct buffer_head *eb_bh,
1068 struct buffer_head **last_eb_bh,
1069 struct ocfs2_alloc_context *meta_ac)
1071 int status, new_blocks, i;
1072 u64 next_blkno, new_last_eb_blk;
1073 struct buffer_head *bh;
1074 struct buffer_head **new_eb_bhs = NULL;
1075 struct ocfs2_extent_block *eb;
1076 struct ocfs2_extent_list *eb_el;
1077 struct ocfs2_extent_list *el;
1078 u32 new_cpos, root_end;
1082 BUG_ON(!last_eb_bh || !*last_eb_bh);
1085 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1088 el = et->et_root_el;
1090 /* we never add a branch to a leaf. */
1091 BUG_ON(!el->l_tree_depth);
1093 new_blocks = le16_to_cpu(el->l_tree_depth);
1095 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1096 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1097 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1100 * If there is a gap before the root end and the real end
1101 * of the righmost leaf block, we need to remove the gap
1102 * between new_cpos and root_end first so that the tree
1103 * is consistent after we add a new branch(it will start
1106 if (root_end > new_cpos) {
1107 mlog(0, "adjust the cluster end from %u to %u\n",
1108 root_end, new_cpos);
1109 status = ocfs2_adjust_rightmost_branch(handle, et);
1116 /* allocate the number of new eb blocks we need */
1117 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1125 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
1126 meta_ac, new_eb_bhs);
1132 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1133 * linked with the rest of the tree.
1134 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1136 * when we leave the loop, new_last_eb_blk will point to the
1137 * newest leaf, and next_blkno will point to the topmost extent
1139 next_blkno = new_last_eb_blk = 0;
1140 for(i = 0; i < new_blocks; i++) {
1142 eb = (struct ocfs2_extent_block *) bh->b_data;
1143 /* ocfs2_create_new_meta_bhs() should create it right! */
1144 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1145 eb_el = &eb->h_list;
1147 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
1148 OCFS2_JOURNAL_ACCESS_CREATE);
1154 eb->h_next_leaf_blk = 0;
1155 eb_el->l_tree_depth = cpu_to_le16(i);
1156 eb_el->l_next_free_rec = cpu_to_le16(1);
1158 * This actually counts as an empty extent as
1161 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
1162 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
1164 * eb_el isn't always an interior node, but even leaf
1165 * nodes want a zero'd flags and reserved field so
1166 * this gets the whole 32 bits regardless of use.
1168 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
1169 if (!eb_el->l_tree_depth)
1170 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1172 status = ocfs2_journal_dirty(handle, bh);
1178 next_blkno = le64_to_cpu(eb->h_blkno);
1181 /* This is a bit hairy. We want to update up to three blocks
1182 * here without leaving any of them in an inconsistent state
1183 * in case of error. We don't have to worry about
1184 * journal_dirty erroring as it won't unless we've aborted the
1185 * handle (in which case we would never be here) so reserving
1186 * the write with journal_access is all we need to do. */
1187 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
1188 OCFS2_JOURNAL_ACCESS_WRITE);
1193 status = ocfs2_et_root_journal_access(handle, et,
1194 OCFS2_JOURNAL_ACCESS_WRITE);
1200 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
1201 OCFS2_JOURNAL_ACCESS_WRITE);
1208 /* Link the new branch into the rest of the tree (el will
1209 * either be on the root_bh, or the extent block passed in. */
1210 i = le16_to_cpu(el->l_next_free_rec);
1211 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1212 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1213 el->l_recs[i].e_int_clusters = 0;
1214 le16_add_cpu(&el->l_next_free_rec, 1);
1216 /* fe needs a new last extent block pointer, as does the
1217 * next_leaf on the previously last-extent-block. */
1218 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1220 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1221 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1223 status = ocfs2_journal_dirty(handle, *last_eb_bh);
1226 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1230 status = ocfs2_journal_dirty(handle, eb_bh);
1236 * Some callers want to track the rightmost leaf so pass it
1239 brelse(*last_eb_bh);
1240 get_bh(new_eb_bhs[0]);
1241 *last_eb_bh = new_eb_bhs[0];
1246 for (i = 0; i < new_blocks; i++)
1247 brelse(new_eb_bhs[i]);
1256 * adds another level to the allocation tree.
1257 * returns back the new extent block so you can add a branch to it
1260 static int ocfs2_shift_tree_depth(handle_t *handle,
1261 struct ocfs2_extent_tree *et,
1262 struct ocfs2_alloc_context *meta_ac,
1263 struct buffer_head **ret_new_eb_bh)
1267 struct buffer_head *new_eb_bh = NULL;
1268 struct ocfs2_extent_block *eb;
1269 struct ocfs2_extent_list *root_el;
1270 struct ocfs2_extent_list *eb_el;
1274 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
1281 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1282 /* ocfs2_create_new_meta_bhs() should create it right! */
1283 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1285 eb_el = &eb->h_list;
1286 root_el = et->et_root_el;
1288 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
1289 OCFS2_JOURNAL_ACCESS_CREATE);
1295 /* copy the root extent list data into the new extent block */
1296 eb_el->l_tree_depth = root_el->l_tree_depth;
1297 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1298 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1299 eb_el->l_recs[i] = root_el->l_recs[i];
1301 status = ocfs2_journal_dirty(handle, new_eb_bh);
1307 status = ocfs2_et_root_journal_access(handle, et,
1308 OCFS2_JOURNAL_ACCESS_WRITE);
1314 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1316 /* update root_bh now */
1317 le16_add_cpu(&root_el->l_tree_depth, 1);
1318 root_el->l_recs[0].e_cpos = 0;
1319 root_el->l_recs[0].e_blkno = eb->h_blkno;
1320 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1321 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1322 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1323 root_el->l_next_free_rec = cpu_to_le16(1);
1325 /* If this is our 1st tree depth shift, then last_eb_blk
1326 * becomes the allocated extent block */
1327 if (root_el->l_tree_depth == cpu_to_le16(1))
1328 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1330 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1336 *ret_new_eb_bh = new_eb_bh;
1347 * Should only be called when there is no space left in any of the
1348 * leaf nodes. What we want to do is find the lowest tree depth
1349 * non-leaf extent block with room for new records. There are three
1350 * valid results of this search:
1352 * 1) a lowest extent block is found, then we pass it back in
1353 * *lowest_eb_bh and return '0'
1355 * 2) the search fails to find anything, but the root_el has room. We
1356 * pass NULL back in *lowest_eb_bh, but still return '0'
1358 * 3) the search fails to find anything AND the root_el is full, in
1359 * which case we return > 0
1361 * return status < 0 indicates an error.
1363 static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
1364 struct buffer_head **target_bh)
1368 struct ocfs2_extent_block *eb;
1369 struct ocfs2_extent_list *el;
1370 struct buffer_head *bh = NULL;
1371 struct buffer_head *lowest_bh = NULL;
1377 el = et->et_root_el;
1379 while(le16_to_cpu(el->l_tree_depth) > 1) {
1380 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1381 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1382 "Owner %llu has empty "
1383 "extent list (next_free_rec == 0)",
1384 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
1388 i = le16_to_cpu(el->l_next_free_rec) - 1;
1389 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1391 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1392 "Owner %llu has extent "
1393 "list where extent # %d has no physical "
1395 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
1403 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
1409 eb = (struct ocfs2_extent_block *) bh->b_data;
1412 if (le16_to_cpu(el->l_next_free_rec) <
1413 le16_to_cpu(el->l_count)) {
1420 /* If we didn't find one and the fe doesn't have any room,
1421 * then return '1' */
1422 el = et->et_root_el;
1423 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1426 *target_bh = lowest_bh;
1435 * Grow a b-tree so that it has more records.
1437 * We might shift the tree depth in which case existing paths should
1438 * be considered invalid.
1440 * Tree depth after the grow is returned via *final_depth.
1442 * *last_eb_bh will be updated by ocfs2_add_branch().
1444 static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1445 int *final_depth, struct buffer_head **last_eb_bh,
1446 struct ocfs2_alloc_context *meta_ac)
1449 struct ocfs2_extent_list *el = et->et_root_el;
1450 int depth = le16_to_cpu(el->l_tree_depth);
1451 struct buffer_head *bh = NULL;
1453 BUG_ON(meta_ac == NULL);
1455 shift = ocfs2_find_branch_target(et, &bh);
1462 /* We traveled all the way to the bottom of the allocation tree
1463 * and didn't find room for any more extents - we need to add
1464 * another tree level */
1467 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1469 /* ocfs2_shift_tree_depth will return us a buffer with
1470 * the new extent block (so we can pass that to
1471 * ocfs2_add_branch). */
1472 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
1480 * Special case: we have room now if we shifted from
1481 * tree_depth 0, so no more work needs to be done.
1483 * We won't be calling add_branch, so pass
1484 * back *last_eb_bh as the new leaf. At depth
1485 * zero, it should always be null so there's
1486 * no reason to brelse.
1488 BUG_ON(*last_eb_bh);
1495 /* call ocfs2_add_branch to add the final part of the tree with
1497 mlog(0, "add branch. bh = %p\n", bh);
1498 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
1507 *final_depth = depth;
1513 * This function will discard the rightmost extent record.
1515 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1517 int next_free = le16_to_cpu(el->l_next_free_rec);
1518 int count = le16_to_cpu(el->l_count);
1519 unsigned int num_bytes;
1522 /* This will cause us to go off the end of our extent list. */
1523 BUG_ON(next_free >= count);
1525 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1527 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1530 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1531 struct ocfs2_extent_rec *insert_rec)
1533 int i, insert_index, next_free, has_empty, num_bytes;
1534 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1535 struct ocfs2_extent_rec *rec;
1537 next_free = le16_to_cpu(el->l_next_free_rec);
1538 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1542 /* The tree code before us didn't allow enough room in the leaf. */
1543 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1546 * The easiest way to approach this is to just remove the
1547 * empty extent and temporarily decrement next_free.
1551 * If next_free was 1 (only an empty extent), this
1552 * loop won't execute, which is fine. We still want
1553 * the decrement above to happen.
1555 for(i = 0; i < (next_free - 1); i++)
1556 el->l_recs[i] = el->l_recs[i+1];
1562 * Figure out what the new record index should be.
1564 for(i = 0; i < next_free; i++) {
1565 rec = &el->l_recs[i];
1567 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1572 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1573 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1575 BUG_ON(insert_index < 0);
1576 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1577 BUG_ON(insert_index > next_free);
1580 * No need to memmove if we're just adding to the tail.
1582 if (insert_index != next_free) {
1583 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1585 num_bytes = next_free - insert_index;
1586 num_bytes *= sizeof(struct ocfs2_extent_rec);
1587 memmove(&el->l_recs[insert_index + 1],
1588 &el->l_recs[insert_index],
1593 * Either we had an empty extent, and need to re-increment or
1594 * there was no empty extent on a non full rightmost leaf node,
1595 * in which case we still need to increment.
1598 el->l_next_free_rec = cpu_to_le16(next_free);
1600 * Make sure none of the math above just messed up our tree.
1602 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1604 el->l_recs[insert_index] = *insert_rec;
1608 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1610 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1612 BUG_ON(num_recs == 0);
1614 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1616 size = num_recs * sizeof(struct ocfs2_extent_rec);
1617 memmove(&el->l_recs[0], &el->l_recs[1], size);
1618 memset(&el->l_recs[num_recs], 0,
1619 sizeof(struct ocfs2_extent_rec));
1620 el->l_next_free_rec = cpu_to_le16(num_recs);
1625 * Create an empty extent record .
1627 * l_next_free_rec may be updated.
1629 * If an empty extent already exists do nothing.
1631 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1633 int next_free = le16_to_cpu(el->l_next_free_rec);
1635 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1640 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1643 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1644 "Asked to create an empty extent in a full list:\n"
1645 "count = %u, tree depth = %u",
1646 le16_to_cpu(el->l_count),
1647 le16_to_cpu(el->l_tree_depth));
1649 ocfs2_shift_records_right(el);
1652 le16_add_cpu(&el->l_next_free_rec, 1);
1653 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1657 * For a rotation which involves two leaf nodes, the "root node" is
1658 * the lowest level tree node which contains a path to both leafs. This
1659 * resulting set of information can be used to form a complete "subtree"
1661 * This function is passed two full paths from the dinode down to a
1662 * pair of adjacent leaves. It's task is to figure out which path
1663 * index contains the subtree root - this can be the root index itself
1664 * in a worst-case rotation.
1666 * The array index of the subtree root is passed back.
1668 static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1669 struct ocfs2_path *left,
1670 struct ocfs2_path *right)
1675 * Check that the caller passed in two paths from the same tree.
1677 BUG_ON(path_root_bh(left) != path_root_bh(right));
1683 * The caller didn't pass two adjacent paths.
1685 mlog_bug_on_msg(i > left->p_tree_depth,
1686 "Owner %llu, left depth %u, right depth %u\n"
1687 "left leaf blk %llu, right leaf blk %llu\n",
1688 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1689 left->p_tree_depth, right->p_tree_depth,
1690 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1691 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1692 } while (left->p_node[i].bh->b_blocknr ==
1693 right->p_node[i].bh->b_blocknr);
1698 typedef void (path_insert_t)(void *, struct buffer_head *);
1701 * Traverse a btree path in search of cpos, starting at root_el.
1703 * This code can be called with a cpos larger than the tree, in which
1704 * case it will return the rightmost path.
1706 static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
1707 struct ocfs2_extent_list *root_el, u32 cpos,
1708 path_insert_t *func, void *data)
1713 struct buffer_head *bh = NULL;
1714 struct ocfs2_extent_block *eb;
1715 struct ocfs2_extent_list *el;
1716 struct ocfs2_extent_rec *rec;
1719 while (el->l_tree_depth) {
1720 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1721 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1722 "Owner %llu has empty extent list at "
1724 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1725 le16_to_cpu(el->l_tree_depth));
1731 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1732 rec = &el->l_recs[i];
1735 * In the case that cpos is off the allocation
1736 * tree, this should just wind up returning the
1739 range = le32_to_cpu(rec->e_cpos) +
1740 ocfs2_rec_clusters(el, rec);
1741 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1745 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1747 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1748 "Owner %llu has bad blkno in extent list "
1749 "at depth %u (index %d)\n",
1750 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1751 le16_to_cpu(el->l_tree_depth), i);
1758 ret = ocfs2_read_extent_block(ci, blkno, &bh);
1764 eb = (struct ocfs2_extent_block *) bh->b_data;
1767 if (le16_to_cpu(el->l_next_free_rec) >
1768 le16_to_cpu(el->l_count)) {
1769 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1770 "Owner %llu has bad count in extent list "
1771 "at block %llu (next free=%u, count=%u)\n",
1772 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1773 (unsigned long long)bh->b_blocknr,
1774 le16_to_cpu(el->l_next_free_rec),
1775 le16_to_cpu(el->l_count));
1786 * Catch any trailing bh that the loop didn't handle.
1794 * Given an initialized path (that is, it has a valid root extent
1795 * list), this function will traverse the btree in search of the path
1796 * which would contain cpos.
1798 * The path traveled is recorded in the path structure.
1800 * Note that this will not do any comparisons on leaf node extent
1801 * records, so it will work fine in the case that we just added a tree
1804 struct find_path_data {
1806 struct ocfs2_path *path;
1808 static void find_path_ins(void *data, struct buffer_head *bh)
1810 struct find_path_data *fp = data;
1813 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1816 static int ocfs2_find_path(struct ocfs2_caching_info *ci,
1817 struct ocfs2_path *path, u32 cpos)
1819 struct find_path_data data;
1823 return __ocfs2_find_path(ci, path_root_el(path), cpos,
1824 find_path_ins, &data);
1827 static void find_leaf_ins(void *data, struct buffer_head *bh)
1829 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1830 struct ocfs2_extent_list *el = &eb->h_list;
1831 struct buffer_head **ret = data;
1833 /* We want to retain only the leaf block. */
1834 if (le16_to_cpu(el->l_tree_depth) == 0) {
1840 * Find the leaf block in the tree which would contain cpos. No
1841 * checking of the actual leaf is done.
1843 * Some paths want to call this instead of allocating a path structure
1844 * and calling ocfs2_find_path().
1846 * This function doesn't handle non btree extent lists.
1848 int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1849 struct ocfs2_extent_list *root_el, u32 cpos,
1850 struct buffer_head **leaf_bh)
1853 struct buffer_head *bh = NULL;
1855 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
1867 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1869 * Basically, we've moved stuff around at the bottom of the tree and
1870 * we need to fix up the extent records above the changes to reflect
1873 * left_rec: the record on the left.
1874 * left_child_el: is the child list pointed to by left_rec
1875 * right_rec: the record to the right of left_rec
1876 * right_child_el: is the child list pointed to by right_rec
1878 * By definition, this only works on interior nodes.
1880 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1881 struct ocfs2_extent_list *left_child_el,
1882 struct ocfs2_extent_rec *right_rec,
1883 struct ocfs2_extent_list *right_child_el)
1885 u32 left_clusters, right_end;
1888 * Interior nodes never have holes. Their cpos is the cpos of
1889 * the leftmost record in their child list. Their cluster
1890 * count covers the full theoretical range of their child list
1891 * - the range between their cpos and the cpos of the record
1892 * immediately to their right.
1894 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1895 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1896 BUG_ON(right_child_el->l_tree_depth);
1897 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1898 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1900 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1901 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1904 * Calculate the rightmost cluster count boundary before
1905 * moving cpos - we will need to adjust clusters after
1906 * updating e_cpos to keep the same highest cluster count.
1908 right_end = le32_to_cpu(right_rec->e_cpos);
1909 right_end += le32_to_cpu(right_rec->e_int_clusters);
1911 right_rec->e_cpos = left_rec->e_cpos;
1912 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1914 right_end -= le32_to_cpu(right_rec->e_cpos);
1915 right_rec->e_int_clusters = cpu_to_le32(right_end);
1919 * Adjust the adjacent root node records involved in a
1920 * rotation. left_el_blkno is passed in as a key so that we can easily
1921 * find it's index in the root list.
1923 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1924 struct ocfs2_extent_list *left_el,
1925 struct ocfs2_extent_list *right_el,
1930 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1931 le16_to_cpu(left_el->l_tree_depth));
1933 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1934 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1939 * The path walking code should have never returned a root and
1940 * two paths which are not adjacent.
1942 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1944 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1945 &root_el->l_recs[i + 1], right_el);
1949 * We've changed a leaf block (in right_path) and need to reflect that
1950 * change back up the subtree.
1952 * This happens in multiple places:
1953 * - When we've moved an extent record from the left path leaf to the right
1954 * path leaf to make room for an empty extent in the left path leaf.
1955 * - When our insert into the right path leaf is at the leftmost edge
1956 * and requires an update of the path immediately to it's left. This
1957 * can occur at the end of some types of rotation and appending inserts.
1958 * - When we've adjusted the last extent record in the left path leaf and the
1959 * 1st extent record in the right path leaf during cross extent block merge.
1961 static void ocfs2_complete_edge_insert(handle_t *handle,
1962 struct ocfs2_path *left_path,
1963 struct ocfs2_path *right_path,
1967 struct ocfs2_extent_list *el, *left_el, *right_el;
1968 struct ocfs2_extent_rec *left_rec, *right_rec;
1969 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1972 * Update the counts and position values within all the
1973 * interior nodes to reflect the leaf rotation we just did.
1975 * The root node is handled below the loop.
1977 * We begin the loop with right_el and left_el pointing to the
1978 * leaf lists and work our way up.
1980 * NOTE: within this loop, left_el and right_el always refer
1981 * to the *child* lists.
1983 left_el = path_leaf_el(left_path);
1984 right_el = path_leaf_el(right_path);
1985 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1986 mlog(0, "Adjust records at index %u\n", i);
1989 * One nice property of knowing that all of these
1990 * nodes are below the root is that we only deal with
1991 * the leftmost right node record and the rightmost
1994 el = left_path->p_node[i].el;
1995 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1996 left_rec = &el->l_recs[idx];
1998 el = right_path->p_node[i].el;
1999 right_rec = &el->l_recs[0];
2001 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2004 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2008 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2013 * Setup our list pointers now so that the current
2014 * parents become children in the next iteration.
2016 left_el = left_path->p_node[i].el;
2017 right_el = right_path->p_node[i].el;
2021 * At the root node, adjust the two adjacent records which
2022 * begin our path to the leaves.
2025 el = left_path->p_node[subtree_index].el;
2026 left_el = left_path->p_node[subtree_index + 1].el;
2027 right_el = right_path->p_node[subtree_index + 1].el;
2029 ocfs2_adjust_root_records(el, left_el, right_el,
2030 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2032 root_bh = left_path->p_node[subtree_index].bh;
2034 ret = ocfs2_journal_dirty(handle, root_bh);
2039 static int ocfs2_rotate_subtree_right(handle_t *handle,
2040 struct ocfs2_extent_tree *et,
2041 struct ocfs2_path *left_path,
2042 struct ocfs2_path *right_path,
2046 struct buffer_head *right_leaf_bh;
2047 struct buffer_head *left_leaf_bh = NULL;
2048 struct buffer_head *root_bh;
2049 struct ocfs2_extent_list *right_el, *left_el;
2050 struct ocfs2_extent_rec move_rec;
2052 left_leaf_bh = path_leaf_bh(left_path);
2053 left_el = path_leaf_el(left_path);
2055 if (left_el->l_next_free_rec != left_el->l_count) {
2056 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
2057 "Inode %llu has non-full interior leaf node %llu"
2059 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2060 (unsigned long long)left_leaf_bh->b_blocknr,
2061 le16_to_cpu(left_el->l_next_free_rec));
2066 * This extent block may already have an empty record, so we
2067 * return early if so.
2069 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2072 root_bh = left_path->p_node[subtree_index].bh;
2073 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2075 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2082 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2083 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2090 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2098 right_leaf_bh = path_leaf_bh(right_path);
2099 right_el = path_leaf_el(right_path);
2101 /* This is a code error, not a disk corruption. */
2102 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2103 "because rightmost leaf block %llu is empty\n",
2104 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2105 (unsigned long long)right_leaf_bh->b_blocknr);
2107 ocfs2_create_empty_extent(right_el);
2109 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2115 /* Do the copy now. */
2116 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2117 move_rec = left_el->l_recs[i];
2118 right_el->l_recs[0] = move_rec;
2121 * Clear out the record we just copied and shift everything
2122 * over, leaving an empty extent in the left leaf.
2124 * We temporarily subtract from next_free_rec so that the
2125 * shift will lose the tail record (which is now defunct).
2127 le16_add_cpu(&left_el->l_next_free_rec, -1);
2128 ocfs2_shift_records_right(left_el);
2129 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2130 le16_add_cpu(&left_el->l_next_free_rec, 1);
2132 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2138 ocfs2_complete_edge_insert(handle, left_path, right_path,
2146 * Given a full path, determine what cpos value would return us a path
2147 * containing the leaf immediately to the left of the current one.
2149 * Will return zero if the path passed in is already the leftmost path.
2151 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2152 struct ocfs2_path *path, u32 *cpos)
2156 struct ocfs2_extent_list *el;
2158 BUG_ON(path->p_tree_depth == 0);
2162 blkno = path_leaf_bh(path)->b_blocknr;
2164 /* Start at the tree node just above the leaf and work our way up. */
2165 i = path->p_tree_depth - 1;
2167 el = path->p_node[i].el;
2170 * Find the extent record just before the one in our
2173 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2174 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2178 * We've determined that the
2179 * path specified is already
2180 * the leftmost one - return a
2186 * The leftmost record points to our
2187 * leaf - we need to travel up the
2193 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2194 *cpos = *cpos + ocfs2_rec_clusters(el,
2195 &el->l_recs[j - 1]);
2202 * If we got here, we never found a valid node where
2203 * the tree indicated one should be.
2206 "Invalid extent tree at extent block %llu\n",
2207 (unsigned long long)blkno);
2212 blkno = path->p_node[i].bh->b_blocknr;
2221 * Extend the transaction by enough credits to complete the rotation,
2222 * and still leave at least the original number of credits allocated
2223 * to this transaction.
2225 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2227 struct ocfs2_path *path)
2229 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2231 if (handle->h_buffer_credits < credits)
2232 return ocfs2_extend_trans(handle, credits);
2238 * Trap the case where we're inserting into the theoretical range past
2239 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2240 * whose cpos is less than ours into the right leaf.
2242 * It's only necessary to look at the rightmost record of the left
2243 * leaf because the logic that calls us should ensure that the
2244 * theoretical ranges in the path components above the leaves are
2247 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2250 struct ocfs2_extent_list *left_el;
2251 struct ocfs2_extent_rec *rec;
2254 left_el = path_leaf_el(left_path);
2255 next_free = le16_to_cpu(left_el->l_next_free_rec);
2256 rec = &left_el->l_recs[next_free - 1];
2258 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2263 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2265 int next_free = le16_to_cpu(el->l_next_free_rec);
2267 struct ocfs2_extent_rec *rec;
2272 rec = &el->l_recs[0];
2273 if (ocfs2_is_empty_extent(rec)) {
2277 rec = &el->l_recs[1];
2280 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2281 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2287 * Rotate all the records in a btree right one record, starting at insert_cpos.
2289 * The path to the rightmost leaf should be passed in.
2291 * The array is assumed to be large enough to hold an entire path (tree depth).
2293 * Upon succesful return from this function:
2295 * - The 'right_path' array will contain a path to the leaf block
2296 * whose range contains e_cpos.
2297 * - That leaf block will have a single empty extent in list index 0.
2298 * - In the case that the rotation requires a post-insert update,
2299 * *ret_left_path will contain a valid path which can be passed to
2300 * ocfs2_insert_path().
2302 static int ocfs2_rotate_tree_right(handle_t *handle,
2303 struct ocfs2_extent_tree *et,
2304 enum ocfs2_split_type split,
2306 struct ocfs2_path *right_path,
2307 struct ocfs2_path **ret_left_path)
2309 int ret, start, orig_credits = handle->h_buffer_credits;
2311 struct ocfs2_path *left_path = NULL;
2312 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2314 *ret_left_path = NULL;
2316 left_path = ocfs2_new_path_from_path(right_path);
2323 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2329 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2332 * What we want to do here is:
2334 * 1) Start with the rightmost path.
2336 * 2) Determine a path to the leaf block directly to the left
2339 * 3) Determine the 'subtree root' - the lowest level tree node
2340 * which contains a path to both leaves.
2342 * 4) Rotate the subtree.
2344 * 5) Find the next subtree by considering the left path to be
2345 * the new right path.
2347 * The check at the top of this while loop also accepts
2348 * insert_cpos == cpos because cpos is only a _theoretical_
2349 * value to get us the left path - insert_cpos might very well
2350 * be filling that hole.
2352 * Stop at a cpos of '0' because we either started at the
2353 * leftmost branch (i.e., a tree with one branch and a
2354 * rotation inside of it), or we've gone as far as we can in
2355 * rotating subtrees.
2357 while (cpos && insert_cpos <= cpos) {
2358 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2361 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
2367 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2368 path_leaf_bh(right_path),
2369 "Owner %llu: error during insert of %u "
2370 "(left path cpos %u) results in two identical "
2371 "paths ending at %llu\n",
2372 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2374 (unsigned long long)
2375 path_leaf_bh(left_path)->b_blocknr);
2377 if (split == SPLIT_NONE &&
2378 ocfs2_rotate_requires_path_adjustment(left_path,
2382 * We've rotated the tree as much as we
2383 * should. The rest is up to
2384 * ocfs2_insert_path() to complete, after the
2385 * record insertion. We indicate this
2386 * situation by returning the left path.
2388 * The reason we don't adjust the records here
2389 * before the record insert is that an error
2390 * later might break the rule where a parent
2391 * record e_cpos will reflect the actual
2392 * e_cpos of the 1st nonempty record of the
2395 *ret_left_path = left_path;
2399 start = ocfs2_find_subtree_root(et, left_path, right_path);
2401 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2403 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2404 right_path->p_tree_depth);
2406 ret = ocfs2_extend_rotate_transaction(handle, start,
2407 orig_credits, right_path);
2413 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
2420 if (split != SPLIT_NONE &&
2421 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2424 * A rotate moves the rightmost left leaf
2425 * record over to the leftmost right leaf
2426 * slot. If we're doing an extent split
2427 * instead of a real insert, then we have to
2428 * check that the extent to be split wasn't
2429 * just moved over. If it was, then we can
2430 * exit here, passing left_path back -
2431 * ocfs2_split_extent() is smart enough to
2432 * search both leaves.
2434 *ret_left_path = left_path;
2439 * There is no need to re-read the next right path
2440 * as we know that it'll be our current left
2441 * path. Optimize by copying values instead.
2443 ocfs2_mv_path(right_path, left_path);
2445 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2453 ocfs2_free_path(left_path);
2459 static int ocfs2_update_edge_lengths(handle_t *handle,
2460 struct ocfs2_extent_tree *et,
2461 int subtree_index, struct ocfs2_path *path)
2464 struct ocfs2_extent_rec *rec;
2465 struct ocfs2_extent_list *el;
2466 struct ocfs2_extent_block *eb;
2470 * In normal tree rotation process, we will never touch the
2471 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2472 * doesn't reserve the credits for them either.
2474 * But we do have a special case here which will update the rightmost
2475 * records for all the bh in the path.
2476 * So we have to allocate extra credits and access them.
2478 ret = ocfs2_extend_trans(handle,
2479 handle->h_buffer_credits + subtree_index);
2485 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
2491 /* Path should always be rightmost. */
2492 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2493 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2496 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2497 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2498 rec = &el->l_recs[idx];
2499 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2501 for (i = 0; i < path->p_tree_depth; i++) {
2502 el = path->p_node[i].el;
2503 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2504 rec = &el->l_recs[idx];
2506 rec->e_int_clusters = cpu_to_le32(range);
2507 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2509 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2515 static void ocfs2_unlink_path(handle_t *handle,
2516 struct ocfs2_extent_tree *et,
2517 struct ocfs2_cached_dealloc_ctxt *dealloc,
2518 struct ocfs2_path *path, int unlink_start)
2521 struct ocfs2_extent_block *eb;
2522 struct ocfs2_extent_list *el;
2523 struct buffer_head *bh;
2525 for(i = unlink_start; i < path_num_items(path); i++) {
2526 bh = path->p_node[i].bh;
2528 eb = (struct ocfs2_extent_block *)bh->b_data;
2530 * Not all nodes might have had their final count
2531 * decremented by the caller - handle this here.
2534 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2536 "Inode %llu, attempted to remove extent block "
2537 "%llu with %u records\n",
2538 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2539 (unsigned long long)le64_to_cpu(eb->h_blkno),
2540 le16_to_cpu(el->l_next_free_rec));
2542 ocfs2_journal_dirty(handle, bh);
2543 ocfs2_remove_from_cache(et->et_ci, bh);
2547 el->l_next_free_rec = 0;
2548 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2550 ocfs2_journal_dirty(handle, bh);
2552 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2556 ocfs2_remove_from_cache(et->et_ci, bh);
2560 static void ocfs2_unlink_subtree(handle_t *handle,
2561 struct ocfs2_extent_tree *et,
2562 struct ocfs2_path *left_path,
2563 struct ocfs2_path *right_path,
2565 struct ocfs2_cached_dealloc_ctxt *dealloc)
2568 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2569 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2570 struct ocfs2_extent_list *el;
2571 struct ocfs2_extent_block *eb;
2573 el = path_leaf_el(left_path);
2575 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2577 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2578 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2581 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2583 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2584 le16_add_cpu(&root_el->l_next_free_rec, -1);
2586 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2587 eb->h_next_leaf_blk = 0;
2589 ocfs2_journal_dirty(handle, root_bh);
2590 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2592 ocfs2_unlink_path(handle, et, dealloc, right_path,
2596 static int ocfs2_rotate_subtree_left(handle_t *handle,
2597 struct ocfs2_extent_tree *et,
2598 struct ocfs2_path *left_path,
2599 struct ocfs2_path *right_path,
2601 struct ocfs2_cached_dealloc_ctxt *dealloc,
2604 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2605 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2606 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2607 struct ocfs2_extent_block *eb;
2611 right_leaf_el = path_leaf_el(right_path);
2612 left_leaf_el = path_leaf_el(left_path);
2613 root_bh = left_path->p_node[subtree_index].bh;
2614 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2616 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2619 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2620 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2622 * It's legal for us to proceed if the right leaf is
2623 * the rightmost one and it has an empty extent. There
2624 * are two cases to handle - whether the leaf will be
2625 * empty after removal or not. If the leaf isn't empty
2626 * then just remove the empty extent up front. The
2627 * next block will handle empty leaves by flagging
2630 * Non rightmost leaves will throw -EAGAIN and the
2631 * caller can manually move the subtree and retry.
2634 if (eb->h_next_leaf_blk != 0ULL)
2637 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2638 ret = ocfs2_journal_access_eb(handle, et->et_ci,
2639 path_leaf_bh(right_path),
2640 OCFS2_JOURNAL_ACCESS_WRITE);
2646 ocfs2_remove_empty_extent(right_leaf_el);
2648 right_has_empty = 1;
2651 if (eb->h_next_leaf_blk == 0ULL &&
2652 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2654 * We have to update i_last_eb_blk during the meta
2657 ret = ocfs2_et_root_journal_access(handle, et,
2658 OCFS2_JOURNAL_ACCESS_WRITE);
2664 del_right_subtree = 1;
2668 * Getting here with an empty extent in the right path implies
2669 * that it's the rightmost path and will be deleted.
2671 BUG_ON(right_has_empty && !del_right_subtree);
2673 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2680 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2681 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2688 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2696 if (!right_has_empty) {
2698 * Only do this if we're moving a real
2699 * record. Otherwise, the action is delayed until
2700 * after removal of the right path in which case we
2701 * can do a simple shift to remove the empty extent.
2703 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2704 memset(&right_leaf_el->l_recs[0], 0,
2705 sizeof(struct ocfs2_extent_rec));
2707 if (eb->h_next_leaf_blk == 0ULL) {
2709 * Move recs over to get rid of empty extent, decrease
2710 * next_free. This is allowed to remove the last
2711 * extent in our leaf (setting l_next_free_rec to
2712 * zero) - the delete code below won't care.
2714 ocfs2_remove_empty_extent(right_leaf_el);
2717 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2720 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2724 if (del_right_subtree) {
2725 ocfs2_unlink_subtree(handle, et, left_path, right_path,
2726 subtree_index, dealloc);
2727 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
2734 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2735 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2738 * Removal of the extent in the left leaf was skipped
2739 * above so we could delete the right path
2742 if (right_has_empty)
2743 ocfs2_remove_empty_extent(left_leaf_el);
2745 ret = ocfs2_journal_dirty(handle, et_root_bh);
2751 ocfs2_complete_edge_insert(handle, left_path, right_path,
2759 * Given a full path, determine what cpos value would return us a path
2760 * containing the leaf immediately to the right of the current one.
2762 * Will return zero if the path passed in is already the rightmost path.
2764 * This looks similar, but is subtly different to
2765 * ocfs2_find_cpos_for_left_leaf().
2767 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2768 struct ocfs2_path *path, u32 *cpos)
2772 struct ocfs2_extent_list *el;
2776 if (path->p_tree_depth == 0)
2779 blkno = path_leaf_bh(path)->b_blocknr;
2781 /* Start at the tree node just above the leaf and work our way up. */
2782 i = path->p_tree_depth - 1;
2786 el = path->p_node[i].el;
2789 * Find the extent record just after the one in our
2792 next_free = le16_to_cpu(el->l_next_free_rec);
2793 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2794 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2795 if (j == (next_free - 1)) {
2798 * We've determined that the
2799 * path specified is already
2800 * the rightmost one - return a
2806 * The rightmost record points to our
2807 * leaf - we need to travel up the
2813 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2819 * If we got here, we never found a valid node where
2820 * the tree indicated one should be.
2823 "Invalid extent tree at extent block %llu\n",
2824 (unsigned long long)blkno);
2829 blkno = path->p_node[i].bh->b_blocknr;
2837 static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2838 struct ocfs2_extent_tree *et,
2839 struct ocfs2_path *path)
2842 struct buffer_head *bh = path_leaf_bh(path);
2843 struct ocfs2_extent_list *el = path_leaf_el(path);
2845 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2848 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
2849 path_num_items(path) - 1);
2855 ocfs2_remove_empty_extent(el);
2857 ret = ocfs2_journal_dirty(handle, bh);
2865 static int __ocfs2_rotate_tree_left(handle_t *handle,
2866 struct ocfs2_extent_tree *et,
2868 struct ocfs2_path *path,
2869 struct ocfs2_cached_dealloc_ctxt *dealloc,
2870 struct ocfs2_path **empty_extent_path)
2872 int ret, subtree_root, deleted;
2874 struct ocfs2_path *left_path = NULL;
2875 struct ocfs2_path *right_path = NULL;
2876 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2878 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2880 *empty_extent_path = NULL;
2882 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
2888 left_path = ocfs2_new_path_from_path(path);
2895 ocfs2_cp_path(left_path, path);
2897 right_path = ocfs2_new_path_from_path(path);
2904 while (right_cpos) {
2905 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
2911 subtree_root = ocfs2_find_subtree_root(et, left_path,
2914 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2916 (unsigned long long)
2917 right_path->p_node[subtree_root].bh->b_blocknr,
2918 right_path->p_tree_depth);
2920 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2921 orig_credits, left_path);
2928 * Caller might still want to make changes to the
2929 * tree root, so re-add it to the journal here.
2931 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2938 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
2939 right_path, subtree_root,
2941 if (ret == -EAGAIN) {
2943 * The rotation has to temporarily stop due to
2944 * the right subtree having an empty
2945 * extent. Pass it back to the caller for a
2948 *empty_extent_path = right_path;
2958 * The subtree rotate might have removed records on
2959 * the rightmost edge. If so, then rotation is
2965 ocfs2_mv_path(left_path, right_path);
2967 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
2976 ocfs2_free_path(right_path);
2977 ocfs2_free_path(left_path);
2982 static int ocfs2_remove_rightmost_path(handle_t *handle,
2983 struct ocfs2_extent_tree *et,
2984 struct ocfs2_path *path,
2985 struct ocfs2_cached_dealloc_ctxt *dealloc)
2987 int ret, subtree_index;
2989 struct ocfs2_path *left_path = NULL;
2990 struct ocfs2_extent_block *eb;
2991 struct ocfs2_extent_list *el;
2994 ret = ocfs2_et_sanity_check(et);
2998 * There's two ways we handle this depending on
2999 * whether path is the only existing one.
3001 ret = ocfs2_extend_rotate_transaction(handle, 0,
3002 handle->h_buffer_credits,
3009 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
3015 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3024 * We have a path to the left of this one - it needs
3027 left_path = ocfs2_new_path_from_path(path);
3034 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
3040 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
3046 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
3048 ocfs2_unlink_subtree(handle, et, left_path, path,
3049 subtree_index, dealloc);
3050 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
3057 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
3058 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
3061 * 'path' is also the leftmost path which
3062 * means it must be the only one. This gets
3063 * handled differently because we want to
3064 * revert the root back to having extents
3067 ocfs2_unlink_path(handle, et, dealloc, path, 1);
3069 el = et->et_root_el;
3070 el->l_tree_depth = 0;
3071 el->l_next_free_rec = 0;
3072 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3074 ocfs2_et_set_last_eb_blk(et, 0);
3077 ocfs2_journal_dirty(handle, path_root_bh(path));
3080 ocfs2_free_path(left_path);
3085 * Left rotation of btree records.
3087 * In many ways, this is (unsurprisingly) the opposite of right
3088 * rotation. We start at some non-rightmost path containing an empty
3089 * extent in the leaf block. The code works its way to the rightmost
3090 * path by rotating records to the left in every subtree.
3092 * This is used by any code which reduces the number of extent records
3093 * in a leaf. After removal, an empty record should be placed in the
3094 * leftmost list position.
3096 * This won't handle a length update of the rightmost path records if
3097 * the rightmost tree leaf record is removed so the caller is
3098 * responsible for detecting and correcting that.
3100 static int ocfs2_rotate_tree_left(handle_t *handle,
3101 struct ocfs2_extent_tree *et,
3102 struct ocfs2_path *path,
3103 struct ocfs2_cached_dealloc_ctxt *dealloc)
3105 int ret, orig_credits = handle->h_buffer_credits;
3106 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3107 struct ocfs2_extent_block *eb;
3108 struct ocfs2_extent_list *el;
3110 el = path_leaf_el(path);
3111 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3114 if (path->p_tree_depth == 0) {
3115 rightmost_no_delete:
3117 * Inline extents. This is trivially handled, so do
3120 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
3127 * Handle rightmost branch now. There's several cases:
3128 * 1) simple rotation leaving records in there. That's trivial.
3129 * 2) rotation requiring a branch delete - there's no more
3130 * records left. Two cases of this:
3131 * a) There are branches to the left.
3132 * b) This is also the leftmost (the only) branch.
3134 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3135 * 2a) we need the left branch so that we can update it with the unlink
3136 * 2b) we need to bring the root back to inline extents.
3139 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3141 if (eb->h_next_leaf_blk == 0) {
3143 * This gets a bit tricky if we're going to delete the
3144 * rightmost path. Get the other cases out of the way
3147 if (le16_to_cpu(el->l_next_free_rec) > 1)
3148 goto rightmost_no_delete;
3150 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3152 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3153 "Owner %llu has empty extent block at %llu",
3154 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
3155 (unsigned long long)le64_to_cpu(eb->h_blkno));
3160 * XXX: The caller can not trust "path" any more after
3161 * this as it will have been deleted. What do we do?
3163 * In theory the rotate-for-merge code will never get
3164 * here because it'll always ask for a rotate in a
3168 ret = ocfs2_remove_rightmost_path(handle, et, path,
3176 * Now we can loop, remembering the path we get from -EAGAIN
3177 * and restarting from there.
3180 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3181 dealloc, &restart_path);
3182 if (ret && ret != -EAGAIN) {
3187 while (ret == -EAGAIN) {
3188 tmp_path = restart_path;
3189 restart_path = NULL;
3191 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
3194 if (ret && ret != -EAGAIN) {
3199 ocfs2_free_path(tmp_path);
3207 ocfs2_free_path(tmp_path);
3208 ocfs2_free_path(restart_path);
3212 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3215 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3218 if (rec->e_leaf_clusters == 0) {
3220 * We consumed all of the merged-from record. An empty
3221 * extent cannot exist anywhere but the 1st array
3222 * position, so move things over if the merged-from
3223 * record doesn't occupy that position.
3225 * This creates a new empty extent so the caller
3226 * should be smart enough to have removed any existing
3230 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3231 size = index * sizeof(struct ocfs2_extent_rec);
3232 memmove(&el->l_recs[1], &el->l_recs[0], size);
3236 * Always memset - the caller doesn't check whether it
3237 * created an empty extent, so there could be junk in
3240 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3244 static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
3245 struct ocfs2_path *left_path,
3246 struct ocfs2_path **ret_right_path)
3250 struct ocfs2_path *right_path = NULL;
3251 struct ocfs2_extent_list *left_el;
3253 *ret_right_path = NULL;
3255 /* This function shouldn't be called for non-trees. */
3256 BUG_ON(left_path->p_tree_depth == 0);
3258 left_el = path_leaf_el(left_path);
3259 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3261 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3262 left_path, &right_cpos);
3268 /* This function shouldn't be called for the rightmost leaf. */
3269 BUG_ON(right_cpos == 0);
3271 right_path = ocfs2_new_path_from_path(left_path);
3278 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
3284 *ret_right_path = right_path;
3287 ocfs2_free_path(right_path);
3292 * Remove split_rec clusters from the record at index and merge them
3293 * onto the beginning of the record "next" to it.
3294 * For index < l_count - 1, the next means the extent rec at index + 1.
3295 * For index == l_count - 1, the "next" means the 1st extent rec of the
3296 * next extent block.
3298 static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
3300 struct ocfs2_extent_tree *et,
3301 struct ocfs2_extent_rec *split_rec,
3304 int ret, next_free, i;
3305 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3306 struct ocfs2_extent_rec *left_rec;
3307 struct ocfs2_extent_rec *right_rec;
3308 struct ocfs2_extent_list *right_el;
3309 struct ocfs2_path *right_path = NULL;
3310 int subtree_index = 0;
3311 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3312 struct buffer_head *bh = path_leaf_bh(left_path);
3313 struct buffer_head *root_bh = NULL;
3315 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3316 left_rec = &el->l_recs[index];
3318 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3319 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3320 /* we meet with a cross extent block merge. */
3321 ret = ocfs2_get_right_path(et, left_path, &right_path);
3327 right_el = path_leaf_el(right_path);
3328 next_free = le16_to_cpu(right_el->l_next_free_rec);
3329 BUG_ON(next_free <= 0);
3330 right_rec = &right_el->l_recs[0];
3331 if (ocfs2_is_empty_extent(right_rec)) {
3332 BUG_ON(next_free <= 1);
3333 right_rec = &right_el->l_recs[1];
3336 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3337 le16_to_cpu(left_rec->e_leaf_clusters) !=
3338 le32_to_cpu(right_rec->e_cpos));
3340 subtree_index = ocfs2_find_subtree_root(et, left_path,
3343 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3344 handle->h_buffer_credits,
3351 root_bh = left_path->p_node[subtree_index].bh;
3352 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3354 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3361 for (i = subtree_index + 1;
3362 i < path_num_items(right_path); i++) {
3363 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3370 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3379 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3380 right_rec = &el->l_recs[index + 1];
3383 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
3384 path_num_items(left_path) - 1);
3390 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3392 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3393 le64_add_cpu(&right_rec->e_blkno,
3394 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3396 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3398 ocfs2_cleanup_merge(el, index);
3400 ret = ocfs2_journal_dirty(handle, bh);
3405 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3409 ocfs2_complete_edge_insert(handle, left_path, right_path,
3414 ocfs2_free_path(right_path);
3418 static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
3419 struct ocfs2_path *right_path,
3420 struct ocfs2_path **ret_left_path)
3424 struct ocfs2_path *left_path = NULL;
3426 *ret_left_path = NULL;
3428 /* This function shouldn't be called for non-trees. */
3429 BUG_ON(right_path->p_tree_depth == 0);
3431 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3432 right_path, &left_cpos);
3438 /* This function shouldn't be called for the leftmost leaf. */
3439 BUG_ON(left_cpos == 0);
3441 left_path = ocfs2_new_path_from_path(right_path);
3448 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
3454 *ret_left_path = left_path;
3457 ocfs2_free_path(left_path);
3462 * Remove split_rec clusters from the record at index and merge them
3463 * onto the tail of the record "before" it.
3464 * For index > 0, the "before" means the extent rec at index - 1.
3466 * For index == 0, the "before" means the last record of the previous
3467 * extent block. And there is also a situation that we may need to
3468 * remove the rightmost leaf extent block in the right_path and change
3469 * the right path to indicate the new rightmost path.
3471 static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
3473 struct ocfs2_extent_tree *et,
3474 struct ocfs2_extent_rec *split_rec,
3475 struct ocfs2_cached_dealloc_ctxt *dealloc,
3478 int ret, i, subtree_index = 0, has_empty_extent = 0;
3479 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3480 struct ocfs2_extent_rec *left_rec;
3481 struct ocfs2_extent_rec *right_rec;
3482 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3483 struct buffer_head *bh = path_leaf_bh(right_path);
3484 struct buffer_head *root_bh = NULL;
3485 struct ocfs2_path *left_path = NULL;
3486 struct ocfs2_extent_list *left_el;
3490 right_rec = &el->l_recs[index];
3492 /* we meet with a cross extent block merge. */
3493 ret = ocfs2_get_left_path(et, right_path, &left_path);
3499 left_el = path_leaf_el(left_path);
3500 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3501 le16_to_cpu(left_el->l_count));
3503 left_rec = &left_el->l_recs[
3504 le16_to_cpu(left_el->l_next_free_rec) - 1];
3505 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3506 le16_to_cpu(left_rec->e_leaf_clusters) !=
3507 le32_to_cpu(split_rec->e_cpos));
3509 subtree_index = ocfs2_find_subtree_root(et, left_path,
3512 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3513 handle->h_buffer_credits,
3520 root_bh = left_path->p_node[subtree_index].bh;
3521 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3523 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3530 for (i = subtree_index + 1;
3531 i < path_num_items(right_path); i++) {
3532 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3539 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3547 left_rec = &el->l_recs[index - 1];
3548 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3549 has_empty_extent = 1;
3552 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3553 path_num_items(right_path) - 1);
3559 if (has_empty_extent && index == 1) {
3561 * The easy case - we can just plop the record right in.
3563 *left_rec = *split_rec;
3565 has_empty_extent = 0;
3567 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3569 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3570 le64_add_cpu(&right_rec->e_blkno,
3571 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3573 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3575 ocfs2_cleanup_merge(el, index);
3577 ret = ocfs2_journal_dirty(handle, bh);
3582 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3587 * In the situation that the right_rec is empty and the extent
3588 * block is empty also, ocfs2_complete_edge_insert can't handle
3589 * it and we need to delete the right extent block.
3591 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3592 le16_to_cpu(el->l_next_free_rec) == 1) {
3594 ret = ocfs2_remove_rightmost_path(handle, et,
3602 /* Now the rightmost extent block has been deleted.
3603 * So we use the new rightmost path.
3605 ocfs2_mv_path(right_path, left_path);
3608 ocfs2_complete_edge_insert(handle, left_path,
3609 right_path, subtree_index);
3613 ocfs2_free_path(left_path);
3617 static int ocfs2_try_to_merge_extent(handle_t *handle,
3618 struct ocfs2_extent_tree *et,
3619 struct ocfs2_path *path,
3621 struct ocfs2_extent_rec *split_rec,
3622 struct ocfs2_cached_dealloc_ctxt *dealloc,
3623 struct ocfs2_merge_ctxt *ctxt)
3626 struct ocfs2_extent_list *el = path_leaf_el(path);
3627 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3629 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3631 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3633 * The merge code will need to create an empty
3634 * extent to take the place of the newly
3635 * emptied slot. Remove any pre-existing empty
3636 * extents - having more than one in a leaf is
3639 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3645 rec = &el->l_recs[split_index];
3648 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3650 * Left-right contig implies this.
3652 BUG_ON(!ctxt->c_split_covers_rec);
3655 * Since the leftright insert always covers the entire
3656 * extent, this call will delete the insert record
3657 * entirely, resulting in an empty extent record added to
3660 * Since the adding of an empty extent shifts
3661 * everything back to the right, there's no need to
3662 * update split_index here.
3664 * When the split_index is zero, we need to merge it to the
3665 * prevoius extent block. It is more efficient and easier
3666 * if we do merge_right first and merge_left later.
3668 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
3676 * We can only get this from logic error above.
3678 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3680 /* The merge left us with an empty extent, remove it. */
3681 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3687 rec = &el->l_recs[split_index];
3690 * Note that we don't pass split_rec here on purpose -
3691 * we've merged it into the rec already.
3693 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3694 dealloc, split_index);
3701 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3703 * Error from this last rotate is not critical, so
3704 * print but don't bubble it up.
3711 * Merge a record to the left or right.
3713 * 'contig_type' is relative to the existing record,
3714 * so for example, if we're "right contig", it's to
3715 * the record on the left (hence the left merge).
3717 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3718 ret = ocfs2_merge_rec_left(path, handle, et,
3726 ret = ocfs2_merge_rec_right(path, handle,
3735 if (ctxt->c_split_covers_rec) {
3737 * The merge may have left an empty extent in
3738 * our leaf. Try to rotate it away.
3740 ret = ocfs2_rotate_tree_left(handle, et, path,
3752 static void ocfs2_subtract_from_rec(struct super_block *sb,
3753 enum ocfs2_split_type split,
3754 struct ocfs2_extent_rec *rec,
3755 struct ocfs2_extent_rec *split_rec)
3759 len_blocks = ocfs2_clusters_to_blocks(sb,
3760 le16_to_cpu(split_rec->e_leaf_clusters));
3762 if (split == SPLIT_LEFT) {
3764 * Region is on the left edge of the existing
3767 le32_add_cpu(&rec->e_cpos,
3768 le16_to_cpu(split_rec->e_leaf_clusters));
3769 le64_add_cpu(&rec->e_blkno, len_blocks);
3770 le16_add_cpu(&rec->e_leaf_clusters,
3771 -le16_to_cpu(split_rec->e_leaf_clusters));
3774 * Region is on the right edge of the existing
3777 le16_add_cpu(&rec->e_leaf_clusters,
3778 -le16_to_cpu(split_rec->e_leaf_clusters));
3783 * Do the final bits of extent record insertion at the target leaf
3784 * list. If this leaf is part of an allocation tree, it is assumed
3785 * that the tree above has been prepared.
3787 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3788 struct ocfs2_extent_list *el,
3789 struct ocfs2_insert_type *insert,
3790 struct inode *inode)
3792 int i = insert->ins_contig_index;
3794 struct ocfs2_extent_rec *rec;
3796 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3798 if (insert->ins_split != SPLIT_NONE) {
3799 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3801 rec = &el->l_recs[i];
3802 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3808 * Contiguous insert - either left or right.
3810 if (insert->ins_contig != CONTIG_NONE) {
3811 rec = &el->l_recs[i];
3812 if (insert->ins_contig == CONTIG_LEFT) {
3813 rec->e_blkno = insert_rec->e_blkno;
3814 rec->e_cpos = insert_rec->e_cpos;
3816 le16_add_cpu(&rec->e_leaf_clusters,
3817 le16_to_cpu(insert_rec->e_leaf_clusters));
3822 * Handle insert into an empty leaf.
3824 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3825 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3826 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3827 el->l_recs[0] = *insert_rec;
3828 el->l_next_free_rec = cpu_to_le16(1);
3835 if (insert->ins_appending == APPEND_TAIL) {
3836 i = le16_to_cpu(el->l_next_free_rec) - 1;
3837 rec = &el->l_recs[i];
3838 range = le32_to_cpu(rec->e_cpos)
3839 + le16_to_cpu(rec->e_leaf_clusters);
3840 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3842 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3843 le16_to_cpu(el->l_count),
3844 "inode %lu, depth %u, count %u, next free %u, "
3845 "rec.cpos %u, rec.clusters %u, "
3846 "insert.cpos %u, insert.clusters %u\n",
3848 le16_to_cpu(el->l_tree_depth),
3849 le16_to_cpu(el->l_count),
3850 le16_to_cpu(el->l_next_free_rec),
3851 le32_to_cpu(el->l_recs[i].e_cpos),
3852 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3853 le32_to_cpu(insert_rec->e_cpos),
3854 le16_to_cpu(insert_rec->e_leaf_clusters));
3856 el->l_recs[i] = *insert_rec;
3857 le16_add_cpu(&el->l_next_free_rec, 1);
3863 * Ok, we have to rotate.
3865 * At this point, it is safe to assume that inserting into an
3866 * empty leaf and appending to a leaf have both been handled
3869 * This leaf needs to have space, either by the empty 1st
3870 * extent record, or by virtue of an l_next_rec < l_count.
3872 ocfs2_rotate_leaf(el, insert_rec);
3875 static void ocfs2_adjust_rightmost_records(handle_t *handle,
3876 struct ocfs2_extent_tree *et,
3877 struct ocfs2_path *path,
3878 struct ocfs2_extent_rec *insert_rec)
3880 int ret, i, next_free;
3881 struct buffer_head *bh;
3882 struct ocfs2_extent_list *el;
3883 struct ocfs2_extent_rec *rec;
3886 * Update everything except the leaf block.
3888 for (i = 0; i < path->p_tree_depth; i++) {
3889 bh = path->p_node[i].bh;
3890 el = path->p_node[i].el;
3892 next_free = le16_to_cpu(el->l_next_free_rec);
3893 if (next_free == 0) {
3894 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3895 "Owner %llu has a bad extent list",
3896 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
3901 rec = &el->l_recs[next_free - 1];
3903 rec->e_int_clusters = insert_rec->e_cpos;
3904 le32_add_cpu(&rec->e_int_clusters,
3905 le16_to_cpu(insert_rec->e_leaf_clusters));
3906 le32_add_cpu(&rec->e_int_clusters,
3907 -le32_to_cpu(rec->e_cpos));
3909 ret = ocfs2_journal_dirty(handle, bh);
3916 static int ocfs2_append_rec_to_path(handle_t *handle,
3917 struct ocfs2_extent_tree *et,
3918 struct ocfs2_extent_rec *insert_rec,
3919 struct ocfs2_path *right_path,
3920 struct ocfs2_path **ret_left_path)
3923 struct ocfs2_extent_list *el;
3924 struct ocfs2_path *left_path = NULL;
3926 *ret_left_path = NULL;
3929 * This shouldn't happen for non-trees. The extent rec cluster
3930 * count manipulation below only works for interior nodes.
3932 BUG_ON(right_path->p_tree_depth == 0);
3935 * If our appending insert is at the leftmost edge of a leaf,
3936 * then we might need to update the rightmost records of the
3939 el = path_leaf_el(right_path);
3940 next_free = le16_to_cpu(el->l_next_free_rec);
3941 if (next_free == 0 ||
3942 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3945 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3946 right_path, &left_cpos);
3952 mlog(0, "Append may need a left path update. cpos: %u, "
3953 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3957 * No need to worry if the append is already in the
3961 left_path = ocfs2_new_path_from_path(right_path);
3968 ret = ocfs2_find_path(et->et_ci, left_path,
3976 * ocfs2_insert_path() will pass the left_path to the
3982 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
3988 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
3990 *ret_left_path = left_path;
3994 ocfs2_free_path(left_path);
3999 static void ocfs2_split_record(struct inode *inode,
4000 struct ocfs2_path *left_path,
4001 struct ocfs2_path *right_path,
4002 struct ocfs2_extent_rec *split_rec,
4003 enum ocfs2_split_type split)
4006 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4007 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4008 struct ocfs2_extent_rec *rec, *tmprec;
4010 right_el = path_leaf_el(right_path);
4012 left_el = path_leaf_el(left_path);
4015 insert_el = right_el;
4016 index = ocfs2_search_extent_list(el, cpos);
4018 if (index == 0 && left_path) {
4019 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4022 * This typically means that the record
4023 * started in the left path but moved to the
4024 * right as a result of rotation. We either
4025 * move the existing record to the left, or we
4026 * do the later insert there.
4028 * In this case, the left path should always
4029 * exist as the rotate code will have passed
4030 * it back for a post-insert update.
4033 if (split == SPLIT_LEFT) {
4035 * It's a left split. Since we know
4036 * that the rotate code gave us an
4037 * empty extent in the left path, we
4038 * can just do the insert there.
4040 insert_el = left_el;
4043 * Right split - we have to move the
4044 * existing record over to the left
4045 * leaf. The insert will be into the
4046 * newly created empty extent in the
4049 tmprec = &right_el->l_recs[index];
4050 ocfs2_rotate_leaf(left_el, tmprec);
4053 memset(tmprec, 0, sizeof(*tmprec));
4054 index = ocfs2_search_extent_list(left_el, cpos);
4055 BUG_ON(index == -1);
4060 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4062 * Left path is easy - we can just allow the insert to
4066 insert_el = left_el;
4067 index = ocfs2_search_extent_list(el, cpos);
4068 BUG_ON(index == -1);
4071 rec = &el->l_recs[index];
4072 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
4073 ocfs2_rotate_leaf(insert_el, split_rec);
4077 * This function only does inserts on an allocation b-tree. For tree
4078 * depth = 0, ocfs2_insert_at_leaf() is called directly.
4080 * right_path is the path we want to do the actual insert
4081 * in. left_path should only be passed in if we need to update that
4082 * portion of the tree after an edge insert.
4084 static int ocfs2_insert_path(struct inode *inode,
4086 struct ocfs2_extent_tree *et,
4087 struct ocfs2_path *left_path,
4088 struct ocfs2_path *right_path,
4089 struct ocfs2_extent_rec *insert_rec,
4090 struct ocfs2_insert_type *insert)
4092 int ret, subtree_index;
4093 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
4096 int credits = handle->h_buffer_credits;
4099 * There's a chance that left_path got passed back to
4100 * us without being accounted for in the
4101 * journal. Extend our transaction here to be sure we
4102 * can change those blocks.
4104 credits += left_path->p_tree_depth;
4106 ret = ocfs2_extend_trans(handle, credits);
4112 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
4120 * Pass both paths to the journal. The majority of inserts
4121 * will be touching all components anyway.
4123 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4129 if (insert->ins_split != SPLIT_NONE) {
4131 * We could call ocfs2_insert_at_leaf() for some types
4132 * of splits, but it's easier to just let one separate
4133 * function sort it all out.
4135 ocfs2_split_record(inode, left_path, right_path,
4136 insert_rec, insert->ins_split);
4139 * Split might have modified either leaf and we don't
4140 * have a guarantee that the later edge insert will
4141 * dirty this for us.
4144 ret = ocfs2_journal_dirty(handle,
4145 path_leaf_bh(left_path));
4149 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
4152 ret = ocfs2_journal_dirty(handle, leaf_bh);
4158 * The rotate code has indicated that we need to fix
4159 * up portions of the tree after the insert.
4161 * XXX: Should we extend the transaction here?
4163 subtree_index = ocfs2_find_subtree_root(et, left_path,
4165 ocfs2_complete_edge_insert(handle, left_path, right_path,
4174 static int ocfs2_do_insert_extent(struct inode *inode,
4176 struct ocfs2_extent_tree *et,
4177 struct ocfs2_extent_rec *insert_rec,
4178 struct ocfs2_insert_type *type)
4180 int ret, rotate = 0;
4182 struct ocfs2_path *right_path = NULL;
4183 struct ocfs2_path *left_path = NULL;
4184 struct ocfs2_extent_list *el;
4186 el = et->et_root_el;
4188 ret = ocfs2_et_root_journal_access(handle, et,
4189 OCFS2_JOURNAL_ACCESS_WRITE);
4195 if (le16_to_cpu(el->l_tree_depth) == 0) {
4196 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4197 goto out_update_clusters;
4200 right_path = ocfs2_new_path_from_et(et);
4208 * Determine the path to start with. Rotations need the
4209 * rightmost path, everything else can go directly to the
4212 cpos = le32_to_cpu(insert_rec->e_cpos);
4213 if (type->ins_appending == APPEND_NONE &&
4214 type->ins_contig == CONTIG_NONE) {
4219 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
4226 * Rotations and appends need special treatment - they modify
4227 * parts of the tree's above them.
4229 * Both might pass back a path immediate to the left of the
4230 * one being inserted to. This will be cause
4231 * ocfs2_insert_path() to modify the rightmost records of
4232 * left_path to account for an edge insert.
4234 * XXX: When modifying this code, keep in mind that an insert
4235 * can wind up skipping both of these two special cases...
4238 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
4239 le32_to_cpu(insert_rec->e_cpos),
4240 right_path, &left_path);
4247 * ocfs2_rotate_tree_right() might have extended the
4248 * transaction without re-journaling our tree root.
4250 ret = ocfs2_et_root_journal_access(handle, et,
4251 OCFS2_JOURNAL_ACCESS_WRITE);
4256 } else if (type->ins_appending == APPEND_TAIL
4257 && type->ins_contig != CONTIG_LEFT) {
4258 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
4259 right_path, &left_path);
4266 ret = ocfs2_insert_path(inode, handle, et, left_path, right_path,
4273 out_update_clusters:
4274 if (type->ins_split == SPLIT_NONE)
4275 ocfs2_et_update_clusters(et,
4276 le16_to_cpu(insert_rec->e_leaf_clusters));
4278 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
4283 ocfs2_free_path(left_path);
4284 ocfs2_free_path(right_path);
4289 static enum ocfs2_contig_type
4290 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
4291 struct ocfs2_extent_list *el, int index,
4292 struct ocfs2_extent_rec *split_rec)
4295 enum ocfs2_contig_type ret = CONTIG_NONE;
4296 u32 left_cpos, right_cpos;
4297 struct ocfs2_extent_rec *rec = NULL;
4298 struct ocfs2_extent_list *new_el;
4299 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4300 struct buffer_head *bh;
4301 struct ocfs2_extent_block *eb;
4304 rec = &el->l_recs[index - 1];
4305 } else if (path->p_tree_depth > 0) {
4306 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4311 if (left_cpos != 0) {
4312 left_path = ocfs2_new_path_from_path(path);
4316 status = ocfs2_find_path(INODE_CACHE(inode),
4317 left_path, left_cpos);
4321 new_el = path_leaf_el(left_path);
4323 if (le16_to_cpu(new_el->l_next_free_rec) !=
4324 le16_to_cpu(new_el->l_count)) {
4325 bh = path_leaf_bh(left_path);
4326 eb = (struct ocfs2_extent_block *)bh->b_data;
4327 ocfs2_error(inode->i_sb,
4328 "Extent block #%llu has an "
4329 "invalid l_next_free_rec of "
4330 "%d. It should have "
4331 "matched the l_count of %d",
4332 (unsigned long long)le64_to_cpu(eb->h_blkno),
4333 le16_to_cpu(new_el->l_next_free_rec),
4334 le16_to_cpu(new_el->l_count));
4338 rec = &new_el->l_recs[
4339 le16_to_cpu(new_el->l_next_free_rec) - 1];
4344 * We're careful to check for an empty extent record here -
4345 * the merge code will know what to do if it sees one.
4348 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4349 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4352 ret = ocfs2_extent_contig(inode, rec, split_rec);
4357 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4358 rec = &el->l_recs[index + 1];
4359 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4360 path->p_tree_depth > 0) {
4361 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4366 if (right_cpos == 0)
4369 right_path = ocfs2_new_path_from_path(path);
4373 status = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos);
4377 new_el = path_leaf_el(right_path);
4378 rec = &new_el->l_recs[0];
4379 if (ocfs2_is_empty_extent(rec)) {
4380 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4381 bh = path_leaf_bh(right_path);
4382 eb = (struct ocfs2_extent_block *)bh->b_data;
4383 ocfs2_error(inode->i_sb,
4384 "Extent block #%llu has an "
4385 "invalid l_next_free_rec of %d",
4386 (unsigned long long)le64_to_cpu(eb->h_blkno),
4387 le16_to_cpu(new_el->l_next_free_rec));
4391 rec = &new_el->l_recs[1];
4396 enum ocfs2_contig_type contig_type;
4398 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4400 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4401 ret = CONTIG_LEFTRIGHT;
4402 else if (ret == CONTIG_NONE)
4408 ocfs2_free_path(left_path);
4410 ocfs2_free_path(right_path);
4415 static void ocfs2_figure_contig_type(struct inode *inode,
4416 struct ocfs2_insert_type *insert,
4417 struct ocfs2_extent_list *el,
4418 struct ocfs2_extent_rec *insert_rec,
4419 struct ocfs2_extent_tree *et)
4422 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4424 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4426 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4427 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4429 if (contig_type != CONTIG_NONE) {
4430 insert->ins_contig_index = i;
4434 insert->ins_contig = contig_type;
4436 if (insert->ins_contig != CONTIG_NONE) {
4437 struct ocfs2_extent_rec *rec =
4438 &el->l_recs[insert->ins_contig_index];
4439 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4440 le16_to_cpu(insert_rec->e_leaf_clusters);
4443 * Caller might want us to limit the size of extents, don't
4444 * calculate contiguousness if we might exceed that limit.
4446 if (et->et_max_leaf_clusters &&
4447 (len > et->et_max_leaf_clusters))
4448 insert->ins_contig = CONTIG_NONE;
4453 * This should only be called against the righmost leaf extent list.
4455 * ocfs2_figure_appending_type() will figure out whether we'll have to
4456 * insert at the tail of the rightmost leaf.
4458 * This should also work against the root extent list for tree's with 0
4459 * depth. If we consider the root extent list to be the rightmost leaf node
4460 * then the logic here makes sense.
4462 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4463 struct ocfs2_extent_list *el,
4464 struct ocfs2_extent_rec *insert_rec)
4467 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4468 struct ocfs2_extent_rec *rec;
4470 insert->ins_appending = APPEND_NONE;
4472 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4474 if (!el->l_next_free_rec)
4475 goto set_tail_append;
4477 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4478 /* Were all records empty? */
4479 if (le16_to_cpu(el->l_next_free_rec) == 1)
4480 goto set_tail_append;
4483 i = le16_to_cpu(el->l_next_free_rec) - 1;
4484 rec = &el->l_recs[i];
4487 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4488 goto set_tail_append;
4493 insert->ins_appending = APPEND_TAIL;
4497 * Helper function called at the begining of an insert.
4499 * This computes a few things that are commonly used in the process of
4500 * inserting into the btree:
4501 * - Whether the new extent is contiguous with an existing one.
4502 * - The current tree depth.
4503 * - Whether the insert is an appending one.
4504 * - The total # of free records in the tree.
4506 * All of the information is stored on the ocfs2_insert_type
4509 static int ocfs2_figure_insert_type(struct inode *inode,
4510 struct ocfs2_extent_tree *et,
4511 struct buffer_head **last_eb_bh,
4512 struct ocfs2_extent_rec *insert_rec,
4514 struct ocfs2_insert_type *insert)
4517 struct ocfs2_extent_block *eb;
4518 struct ocfs2_extent_list *el;
4519 struct ocfs2_path *path = NULL;
4520 struct buffer_head *bh = NULL;
4522 insert->ins_split = SPLIT_NONE;
4524 el = et->et_root_el;
4525 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4527 if (el->l_tree_depth) {
4529 * If we have tree depth, we read in the
4530 * rightmost extent block ahead of time as
4531 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4532 * may want it later.
4534 ret = ocfs2_read_extent_block(et->et_ci,
4535 ocfs2_et_get_last_eb_blk(et),
4541 eb = (struct ocfs2_extent_block *) bh->b_data;
4546 * Unless we have a contiguous insert, we'll need to know if
4547 * there is room left in our allocation tree for another
4550 * XXX: This test is simplistic, we can search for empty
4551 * extent records too.
4553 *free_records = le16_to_cpu(el->l_count) -
4554 le16_to_cpu(el->l_next_free_rec);
4556 if (!insert->ins_tree_depth) {
4557 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4558 ocfs2_figure_appending_type(insert, el, insert_rec);
4562 path = ocfs2_new_path_from_et(et);
4570 * In the case that we're inserting past what the tree
4571 * currently accounts for, ocfs2_find_path() will return for
4572 * us the rightmost tree path. This is accounted for below in
4573 * the appending code.
4575 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
4581 el = path_leaf_el(path);
4584 * Now that we have the path, there's two things we want to determine:
4585 * 1) Contiguousness (also set contig_index if this is so)
4587 * 2) Are we doing an append? We can trivially break this up
4588 * into two types of appends: simple record append, or a
4589 * rotate inside the tail leaf.
4591 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4594 * The insert code isn't quite ready to deal with all cases of
4595 * left contiguousness. Specifically, if it's an insert into
4596 * the 1st record in a leaf, it will require the adjustment of
4597 * cluster count on the last record of the path directly to it's
4598 * left. For now, just catch that case and fool the layers
4599 * above us. This works just fine for tree_depth == 0, which
4600 * is why we allow that above.
4602 if (insert->ins_contig == CONTIG_LEFT &&
4603 insert->ins_contig_index == 0)
4604 insert->ins_contig = CONTIG_NONE;
4607 * Ok, so we can simply compare against last_eb to figure out
4608 * whether the path doesn't exist. This will only happen in
4609 * the case that we're doing a tail append, so maybe we can
4610 * take advantage of that information somehow.
4612 if (ocfs2_et_get_last_eb_blk(et) ==
4613 path_leaf_bh(path)->b_blocknr) {
4615 * Ok, ocfs2_find_path() returned us the rightmost
4616 * tree path. This might be an appending insert. There are
4618 * 1) We're doing a true append at the tail:
4619 * -This might even be off the end of the leaf
4620 * 2) We're "appending" by rotating in the tail
4622 ocfs2_figure_appending_type(insert, el, insert_rec);
4626 ocfs2_free_path(path);
4636 * Insert an extent into an inode btree.
4638 * The caller needs to update fe->i_clusters
4640 int ocfs2_insert_extent(struct ocfs2_super *osb,
4642 struct inode *inode,
4643 struct ocfs2_extent_tree *et,
4648 struct ocfs2_alloc_context *meta_ac)
4651 int uninitialized_var(free_records);
4652 struct buffer_head *last_eb_bh = NULL;
4653 struct ocfs2_insert_type insert = {0, };
4654 struct ocfs2_extent_rec rec;
4656 mlog(0, "add %u clusters at position %u to inode %llu\n",
4657 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4659 memset(&rec, 0, sizeof(rec));
4660 rec.e_cpos = cpu_to_le32(cpos);
4661 rec.e_blkno = cpu_to_le64(start_blk);
4662 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4663 rec.e_flags = flags;
4664 status = ocfs2_et_insert_check(et, &rec);
4670 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4671 &free_records, &insert);
4677 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4678 "Insert.contig_index: %d, Insert.free_records: %d, "
4679 "Insert.tree_depth: %d\n",
4680 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4681 free_records, insert.ins_tree_depth);
4683 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4684 status = ocfs2_grow_tree(handle, et,
4685 &insert.ins_tree_depth, &last_eb_bh,
4693 /* Finally, we can add clusters. This might rotate the tree for us. */
4694 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4697 else if (et->et_ops == &ocfs2_dinode_et_ops)
4698 ocfs2_extent_map_insert_rec(inode, &rec);
4708 * Allcate and add clusters into the extent b-tree.
4709 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4710 * The extent b-tree's root is specified by et, and
4711 * it is not limited to the file storage. Any extent tree can use this
4712 * function if it implements the proper ocfs2_extent_tree.
4714 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4715 struct inode *inode,
4716 u32 *logical_offset,
4717 u32 clusters_to_add,
4719 struct ocfs2_extent_tree *et,
4721 struct ocfs2_alloc_context *data_ac,
4722 struct ocfs2_alloc_context *meta_ac,
4723 enum ocfs2_alloc_restarted *reason_ret)
4727 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4728 u32 bit_off, num_bits;
4732 BUG_ON(!clusters_to_add);
4735 flags = OCFS2_EXT_UNWRITTEN;
4737 free_extents = ocfs2_num_free_extents(osb, et);
4738 if (free_extents < 0) {
4739 status = free_extents;
4744 /* there are two cases which could cause us to EAGAIN in the
4745 * we-need-more-metadata case:
4746 * 1) we haven't reserved *any*
4747 * 2) we are so fragmented, we've needed to add metadata too
4749 if (!free_extents && !meta_ac) {
4750 mlog(0, "we haven't reserved any metadata!\n");
4752 reason = RESTART_META;
4754 } else if ((!free_extents)
4755 && (ocfs2_alloc_context_bits_left(meta_ac)
4756 < ocfs2_extend_meta_needed(et->et_root_el))) {
4757 mlog(0, "filesystem is really fragmented...\n");
4759 reason = RESTART_META;
4763 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4764 clusters_to_add, &bit_off, &num_bits);
4766 if (status != -ENOSPC)
4771 BUG_ON(num_bits > clusters_to_add);
4773 /* reserve our write early -- insert_extent may update the tree root */
4774 status = ocfs2_et_root_journal_access(handle, et,
4775 OCFS2_JOURNAL_ACCESS_WRITE);
4781 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4782 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4783 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4784 status = ocfs2_insert_extent(osb, handle, inode, et,
4785 *logical_offset, block,
4786 num_bits, flags, meta_ac);
4792 status = ocfs2_journal_dirty(handle, et->et_root_bh);
4798 clusters_to_add -= num_bits;
4799 *logical_offset += num_bits;
4801 if (clusters_to_add) {
4802 mlog(0, "need to alloc once more, wanted = %u\n",
4805 reason = RESTART_TRANS;
4811 *reason_ret = reason;
4815 static void ocfs2_make_right_split_rec(struct super_block *sb,
4816 struct ocfs2_extent_rec *split_rec,
4818 struct ocfs2_extent_rec *rec)
4820 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4821 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4823 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4825 split_rec->e_cpos = cpu_to_le32(cpos);
4826 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4828 split_rec->e_blkno = rec->e_blkno;
4829 le64_add_cpu(&split_rec->e_blkno,
4830 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4832 split_rec->e_flags = rec->e_flags;
4835 static int ocfs2_split_and_insert(struct inode *inode,
4837 struct ocfs2_path *path,
4838 struct ocfs2_extent_tree *et,
4839 struct buffer_head **last_eb_bh,
4841 struct ocfs2_extent_rec *orig_split_rec,
4842 struct ocfs2_alloc_context *meta_ac)
4845 unsigned int insert_range, rec_range, do_leftright = 0;
4846 struct ocfs2_extent_rec tmprec;
4847 struct ocfs2_extent_list *rightmost_el;
4848 struct ocfs2_extent_rec rec;
4849 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4850 struct ocfs2_insert_type insert;
4851 struct ocfs2_extent_block *eb;
4855 * Store a copy of the record on the stack - it might move
4856 * around as the tree is manipulated below.
4858 rec = path_leaf_el(path)->l_recs[split_index];
4860 rightmost_el = et->et_root_el;
4862 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4864 BUG_ON(!(*last_eb_bh));
4865 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4866 rightmost_el = &eb->h_list;
4869 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4870 le16_to_cpu(rightmost_el->l_count)) {
4871 ret = ocfs2_grow_tree(handle, et,
4872 &depth, last_eb_bh, meta_ac);
4879 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4880 insert.ins_appending = APPEND_NONE;
4881 insert.ins_contig = CONTIG_NONE;
4882 insert.ins_tree_depth = depth;
4884 insert_range = le32_to_cpu(split_rec.e_cpos) +
4885 le16_to_cpu(split_rec.e_leaf_clusters);
4886 rec_range = le32_to_cpu(rec.e_cpos) +
4887 le16_to_cpu(rec.e_leaf_clusters);
4889 if (split_rec.e_cpos == rec.e_cpos) {
4890 insert.ins_split = SPLIT_LEFT;
4891 } else if (insert_range == rec_range) {
4892 insert.ins_split = SPLIT_RIGHT;
4895 * Left/right split. We fake this as a right split
4896 * first and then make a second pass as a left split.
4898 insert.ins_split = SPLIT_RIGHT;
4900 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4905 BUG_ON(do_leftright);
4909 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4915 if (do_leftright == 1) {
4917 struct ocfs2_extent_list *el;
4920 split_rec = *orig_split_rec;
4922 ocfs2_reinit_path(path, 1);
4924 cpos = le32_to_cpu(split_rec.e_cpos);
4925 ret = ocfs2_find_path(et->et_ci, path, cpos);
4931 el = path_leaf_el(path);
4932 split_index = ocfs2_search_extent_list(el, cpos);
4940 static int ocfs2_replace_extent_rec(struct inode *inode,
4942 struct ocfs2_path *path,
4943 struct ocfs2_extent_list *el,
4945 struct ocfs2_extent_rec *split_rec)
4949 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
4950 path_num_items(path) - 1);
4956 el->l_recs[split_index] = *split_rec;
4958 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4964 * Mark part or all of the extent record at split_index in the leaf
4965 * pointed to by path as written. This removes the unwritten
4968 * Care is taken to handle contiguousness so as to not grow the tree.
4970 * meta_ac is not strictly necessary - we only truly need it if growth
4971 * of the tree is required. All other cases will degrade into a less
4972 * optimal tree layout.
4974 * last_eb_bh should be the rightmost leaf block for any extent
4975 * btree. Since a split may grow the tree or a merge might shrink it,
4976 * the caller cannot trust the contents of that buffer after this call.
4978 * This code is optimized for readability - several passes might be
4979 * made over certain portions of the tree. All of those blocks will
4980 * have been brought into cache (and pinned via the journal), so the
4981 * extra overhead is not expressed in terms of disk reads.
4983 static int __ocfs2_mark_extent_written(struct inode *inode,
4984 struct ocfs2_extent_tree *et,
4986 struct ocfs2_path *path,
4988 struct ocfs2_extent_rec *split_rec,
4989 struct ocfs2_alloc_context *meta_ac,
4990 struct ocfs2_cached_dealloc_ctxt *dealloc)
4993 struct ocfs2_extent_list *el = path_leaf_el(path);
4994 struct buffer_head *last_eb_bh = NULL;
4995 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4996 struct ocfs2_merge_ctxt ctxt;
4997 struct ocfs2_extent_list *rightmost_el;
4999 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
5005 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5006 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5007 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5013 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
5018 * The core merge / split code wants to know how much room is
5019 * left in this inodes allocation tree, so we pass the
5020 * rightmost extent list.
5022 if (path->p_tree_depth) {
5023 struct ocfs2_extent_block *eb;
5025 ret = ocfs2_read_extent_block(et->et_ci,
5026 ocfs2_et_get_last_eb_blk(et),
5033 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5034 rightmost_el = &eb->h_list;
5036 rightmost_el = path_root_el(path);
5038 if (rec->e_cpos == split_rec->e_cpos &&
5039 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5040 ctxt.c_split_covers_rec = 1;
5042 ctxt.c_split_covers_rec = 0;
5044 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5046 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5047 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5048 ctxt.c_split_covers_rec);
5050 if (ctxt.c_contig_type == CONTIG_NONE) {
5051 if (ctxt.c_split_covers_rec)
5052 ret = ocfs2_replace_extent_rec(inode, handle,
5054 split_index, split_rec);
5056 ret = ocfs2_split_and_insert(inode, handle, path, et,
5057 &last_eb_bh, split_index,
5058 split_rec, meta_ac);
5062 ret = ocfs2_try_to_merge_extent(handle, et, path,
5063 split_index, split_rec,
5075 * Mark the already-existing extent at cpos as written for len clusters.
5077 * If the existing extent is larger than the request, initiate a
5078 * split. An attempt will be made at merging with adjacent extents.
5080 * The caller is responsible for passing down meta_ac if we'll need it.
5082 int ocfs2_mark_extent_written(struct inode *inode,
5083 struct ocfs2_extent_tree *et,
5084 handle_t *handle, u32 cpos, u32 len, u32 phys,
5085 struct ocfs2_alloc_context *meta_ac,
5086 struct ocfs2_cached_dealloc_ctxt *dealloc)
5089 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5090 struct ocfs2_extent_rec split_rec;
5091 struct ocfs2_path *left_path = NULL;
5092 struct ocfs2_extent_list *el;
5094 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5095 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5097 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5098 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5099 "that are being written to, but the feature bit "
5100 "is not set in the super block.",
5101 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5107 * XXX: This should be fixed up so that we just re-insert the
5108 * next extent records.
5110 * XXX: This is a hack on the extent tree, maybe it should be
5113 if (et->et_ops == &ocfs2_dinode_et_ops)
5114 ocfs2_extent_map_trunc(inode, 0);
5116 left_path = ocfs2_new_path_from_et(et);
5123 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
5128 el = path_leaf_el(left_path);
5130 index = ocfs2_search_extent_list(el, cpos);
5131 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5132 ocfs2_error(inode->i_sb,
5133 "Inode %llu has an extent at cpos %u which can no "
5134 "longer be found.\n",
5135 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5140 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5141 split_rec.e_cpos = cpu_to_le32(cpos);
5142 split_rec.e_leaf_clusters = cpu_to_le16(len);
5143 split_rec.e_blkno = cpu_to_le64(start_blkno);
5144 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5145 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5147 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
5148 index, &split_rec, meta_ac,
5154 ocfs2_free_path(left_path);
5158 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
5159 handle_t *handle, struct ocfs2_path *path,
5160 int index, u32 new_range,
5161 struct ocfs2_alloc_context *meta_ac)
5163 int ret, depth, credits = handle->h_buffer_credits;
5164 struct buffer_head *last_eb_bh = NULL;
5165 struct ocfs2_extent_block *eb;
5166 struct ocfs2_extent_list *rightmost_el, *el;
5167 struct ocfs2_extent_rec split_rec;
5168 struct ocfs2_extent_rec *rec;
5169 struct ocfs2_insert_type insert;
5172 * Setup the record to split before we grow the tree.
5174 el = path_leaf_el(path);
5175 rec = &el->l_recs[index];
5176 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5178 depth = path->p_tree_depth;
5180 ret = ocfs2_read_extent_block(et->et_ci,
5181 ocfs2_et_get_last_eb_blk(et),
5188 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5189 rightmost_el = &eb->h_list;
5191 rightmost_el = path_leaf_el(path);
5193 credits += path->p_tree_depth +
5194 ocfs2_extend_meta_needed(et->et_root_el);
5195 ret = ocfs2_extend_trans(handle, credits);
5201 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5202 le16_to_cpu(rightmost_el->l_count)) {
5203 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
5211 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5212 insert.ins_appending = APPEND_NONE;
5213 insert.ins_contig = CONTIG_NONE;
5214 insert.ins_split = SPLIT_RIGHT;
5215 insert.ins_tree_depth = depth;
5217 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
5226 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5227 struct ocfs2_path *path, int index,
5228 struct ocfs2_cached_dealloc_ctxt *dealloc,
5230 struct ocfs2_extent_tree *et)
5233 u32 left_cpos, rec_range, trunc_range;
5234 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5235 struct super_block *sb = inode->i_sb;
5236 struct ocfs2_path *left_path = NULL;
5237 struct ocfs2_extent_list *el = path_leaf_el(path);
5238 struct ocfs2_extent_rec *rec;
5239 struct ocfs2_extent_block *eb;
5241 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5242 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5251 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5252 path->p_tree_depth) {
5254 * Check whether this is the rightmost tree record. If
5255 * we remove all of this record or part of its right
5256 * edge then an update of the record lengths above it
5259 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5260 if (eb->h_next_leaf_blk == 0)
5261 is_rightmost_tree_rec = 1;
5264 rec = &el->l_recs[index];
5265 if (index == 0 && path->p_tree_depth &&
5266 le32_to_cpu(rec->e_cpos) == cpos) {
5268 * Changing the leftmost offset (via partial or whole
5269 * record truncate) of an interior (or rightmost) path
5270 * means we have to update the subtree that is formed
5271 * by this leaf and the one to it's left.
5273 * There are two cases we can skip:
5274 * 1) Path is the leftmost one in our inode tree.
5275 * 2) The leaf is rightmost and will be empty after
5276 * we remove the extent record - the rotate code
5277 * knows how to update the newly formed edge.
5280 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5287 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5288 left_path = ocfs2_new_path_from_path(path);
5295 ret = ocfs2_find_path(et->et_ci, left_path,
5304 ret = ocfs2_extend_rotate_transaction(handle, 0,
5305 handle->h_buffer_credits,
5312 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
5318 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
5324 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5325 trunc_range = cpos + len;
5327 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5330 memset(rec, 0, sizeof(*rec));
5331 ocfs2_cleanup_merge(el, index);
5334 next_free = le16_to_cpu(el->l_next_free_rec);
5335 if (is_rightmost_tree_rec && next_free > 1) {
5337 * We skip the edge update if this path will
5338 * be deleted by the rotate code.
5340 rec = &el->l_recs[next_free - 1];
5341 ocfs2_adjust_rightmost_records(handle, et, path,
5344 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5345 /* Remove leftmost portion of the record. */
5346 le32_add_cpu(&rec->e_cpos, len);
5347 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5348 le16_add_cpu(&rec->e_leaf_clusters, -len);
5349 } else if (rec_range == trunc_range) {
5350 /* Remove rightmost portion of the record */
5351 le16_add_cpu(&rec->e_leaf_clusters, -len);
5352 if (is_rightmost_tree_rec)
5353 ocfs2_adjust_rightmost_records(handle, et, path, rec);
5355 /* Caller should have trapped this. */
5356 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5357 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5358 le32_to_cpu(rec->e_cpos),
5359 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5366 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
5367 ocfs2_complete_edge_insert(handle, left_path, path,
5371 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5373 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5380 ocfs2_free_path(left_path);
5384 int ocfs2_remove_extent(struct inode *inode,
5385 struct ocfs2_extent_tree *et,
5386 u32 cpos, u32 len, handle_t *handle,
5387 struct ocfs2_alloc_context *meta_ac,
5388 struct ocfs2_cached_dealloc_ctxt *dealloc)
5391 u32 rec_range, trunc_range;
5392 struct ocfs2_extent_rec *rec;
5393 struct ocfs2_extent_list *el;
5394 struct ocfs2_path *path = NULL;
5396 ocfs2_extent_map_trunc(inode, 0);
5398 path = ocfs2_new_path_from_et(et);
5405 ret = ocfs2_find_path(et->et_ci, path, cpos);
5411 el = path_leaf_el(path);
5412 index = ocfs2_search_extent_list(el, cpos);
5413 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5414 ocfs2_error(inode->i_sb,
5415 "Inode %llu has an extent at cpos %u which can no "
5416 "longer be found.\n",
5417 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5423 * We have 3 cases of extent removal:
5424 * 1) Range covers the entire extent rec
5425 * 2) Range begins or ends on one edge of the extent rec
5426 * 3) Range is in the middle of the extent rec (no shared edges)
5428 * For case 1 we remove the extent rec and left rotate to
5431 * For case 2 we just shrink the existing extent rec, with a
5432 * tree update if the shrinking edge is also the edge of an
5435 * For case 3 we do a right split to turn the extent rec into
5436 * something case 2 can handle.
5438 rec = &el->l_recs[index];
5439 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5440 trunc_range = cpos + len;
5442 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5444 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5445 "(cpos %u, len %u)\n",
5446 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5447 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5449 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5450 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5457 ret = ocfs2_split_tree(inode, et, handle, path, index,
5458 trunc_range, meta_ac);
5465 * The split could have manipulated the tree enough to
5466 * move the record location, so we have to look for it again.
5468 ocfs2_reinit_path(path, 1);
5470 ret = ocfs2_find_path(et->et_ci, path, cpos);
5476 el = path_leaf_el(path);
5477 index = ocfs2_search_extent_list(el, cpos);
5478 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5479 ocfs2_error(inode->i_sb,
5480 "Inode %llu: split at cpos %u lost record.",
5481 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5488 * Double check our values here. If anything is fishy,
5489 * it's easier to catch it at the top level.
5491 rec = &el->l_recs[index];
5492 rec_range = le32_to_cpu(rec->e_cpos) +
5493 ocfs2_rec_clusters(el, rec);
5494 if (rec_range != trunc_range) {
5495 ocfs2_error(inode->i_sb,
5496 "Inode %llu: error after split at cpos %u"
5497 "trunc len %u, existing record is (%u,%u)",
5498 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5499 cpos, len, le32_to_cpu(rec->e_cpos),
5500 ocfs2_rec_clusters(el, rec));
5505 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5514 ocfs2_free_path(path);
5518 int ocfs2_remove_btree_range(struct inode *inode,
5519 struct ocfs2_extent_tree *et,
5520 u32 cpos, u32 phys_cpos, u32 len,
5521 struct ocfs2_cached_dealloc_ctxt *dealloc)
5524 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5525 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5526 struct inode *tl_inode = osb->osb_tl_inode;
5528 struct ocfs2_alloc_context *meta_ac = NULL;
5530 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5536 mutex_lock(&tl_inode->i_mutex);
5538 if (ocfs2_truncate_log_needs_flush(osb)) {
5539 ret = __ocfs2_flush_truncate_log(osb);
5546 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5547 if (IS_ERR(handle)) {
5548 ret = PTR_ERR(handle);
5553 ret = ocfs2_et_root_journal_access(handle, et,
5554 OCFS2_JOURNAL_ACCESS_WRITE);
5560 vfs_dq_free_space_nodirty(inode,
5561 ocfs2_clusters_to_bytes(inode->i_sb, len));
5563 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5570 ocfs2_et_update_clusters(et, -len);
5572 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5578 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5583 ocfs2_commit_trans(osb, handle);
5585 mutex_unlock(&tl_inode->i_mutex);
5588 ocfs2_free_alloc_context(meta_ac);
5593 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5595 struct buffer_head *tl_bh = osb->osb_tl_bh;
5596 struct ocfs2_dinode *di;
5597 struct ocfs2_truncate_log *tl;
5599 di = (struct ocfs2_dinode *) tl_bh->b_data;
5600 tl = &di->id2.i_dealloc;
5602 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5603 "slot %d, invalid truncate log parameters: used = "
5604 "%u, count = %u\n", osb->slot_num,
5605 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5606 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5609 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5610 unsigned int new_start)
5612 unsigned int tail_index;
5613 unsigned int current_tail;
5615 /* No records, nothing to coalesce */
5616 if (!le16_to_cpu(tl->tl_used))
5619 tail_index = le16_to_cpu(tl->tl_used) - 1;
5620 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5621 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5623 return current_tail == new_start;
5626 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5629 unsigned int num_clusters)
5632 unsigned int start_cluster, tl_count;
5633 struct inode *tl_inode = osb->osb_tl_inode;
5634 struct buffer_head *tl_bh = osb->osb_tl_bh;
5635 struct ocfs2_dinode *di;
5636 struct ocfs2_truncate_log *tl;
5638 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5639 (unsigned long long)start_blk, num_clusters);
5641 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5643 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5645 di = (struct ocfs2_dinode *) tl_bh->b_data;
5647 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5648 * by the underlying call to ocfs2_read_inode_block(), so any
5649 * corruption is a code bug */
5650 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5652 tl = &di->id2.i_dealloc;
5653 tl_count = le16_to_cpu(tl->tl_count);
5654 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5656 "Truncate record count on #%llu invalid "
5657 "wanted %u, actual %u\n",
5658 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5659 ocfs2_truncate_recs_per_inode(osb->sb),
5660 le16_to_cpu(tl->tl_count));
5662 /* Caller should have known to flush before calling us. */
5663 index = le16_to_cpu(tl->tl_used);
5664 if (index >= tl_count) {
5670 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5671 OCFS2_JOURNAL_ACCESS_WRITE);
5677 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5678 "%llu (index = %d)\n", num_clusters, start_cluster,
5679 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5681 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5683 * Move index back to the record we are coalescing with.
5684 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5688 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5689 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5690 index, le32_to_cpu(tl->tl_recs[index].t_start),
5693 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5694 tl->tl_used = cpu_to_le16(index + 1);
5696 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5698 status = ocfs2_journal_dirty(handle, tl_bh);
5709 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5711 struct inode *data_alloc_inode,
5712 struct buffer_head *data_alloc_bh)
5716 unsigned int num_clusters;
5718 struct ocfs2_truncate_rec rec;
5719 struct ocfs2_dinode *di;
5720 struct ocfs2_truncate_log *tl;
5721 struct inode *tl_inode = osb->osb_tl_inode;
5722 struct buffer_head *tl_bh = osb->osb_tl_bh;
5726 di = (struct ocfs2_dinode *) tl_bh->b_data;
5727 tl = &di->id2.i_dealloc;
5728 i = le16_to_cpu(tl->tl_used) - 1;
5730 /* Caller has given us at least enough credits to
5731 * update the truncate log dinode */
5732 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5733 OCFS2_JOURNAL_ACCESS_WRITE);
5739 tl->tl_used = cpu_to_le16(i);
5741 status = ocfs2_journal_dirty(handle, tl_bh);
5747 /* TODO: Perhaps we can calculate the bulk of the
5748 * credits up front rather than extending like
5750 status = ocfs2_extend_trans(handle,
5751 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5757 rec = tl->tl_recs[i];
5758 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5759 le32_to_cpu(rec.t_start));
5760 num_clusters = le32_to_cpu(rec.t_clusters);
5762 /* if start_blk is not set, we ignore the record as
5765 mlog(0, "free record %d, start = %u, clusters = %u\n",
5766 i, le32_to_cpu(rec.t_start), num_clusters);
5768 status = ocfs2_free_clusters(handle, data_alloc_inode,
5769 data_alloc_bh, start_blk,
5784 /* Expects you to already be holding tl_inode->i_mutex */
5785 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5788 unsigned int num_to_flush;
5790 struct inode *tl_inode = osb->osb_tl_inode;
5791 struct inode *data_alloc_inode = NULL;
5792 struct buffer_head *tl_bh = osb->osb_tl_bh;
5793 struct buffer_head *data_alloc_bh = NULL;
5794 struct ocfs2_dinode *di;
5795 struct ocfs2_truncate_log *tl;
5799 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5801 di = (struct ocfs2_dinode *) tl_bh->b_data;
5803 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5804 * by the underlying call to ocfs2_read_inode_block(), so any
5805 * corruption is a code bug */
5806 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5808 tl = &di->id2.i_dealloc;
5809 num_to_flush = le16_to_cpu(tl->tl_used);
5810 mlog(0, "Flush %u records from truncate log #%llu\n",
5811 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5812 if (!num_to_flush) {
5817 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5818 GLOBAL_BITMAP_SYSTEM_INODE,
5819 OCFS2_INVALID_SLOT);
5820 if (!data_alloc_inode) {
5822 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5826 mutex_lock(&data_alloc_inode->i_mutex);
5828 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5834 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5835 if (IS_ERR(handle)) {
5836 status = PTR_ERR(handle);
5841 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5846 ocfs2_commit_trans(osb, handle);
5849 brelse(data_alloc_bh);
5850 ocfs2_inode_unlock(data_alloc_inode, 1);
5853 mutex_unlock(&data_alloc_inode->i_mutex);
5854 iput(data_alloc_inode);
5861 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5864 struct inode *tl_inode = osb->osb_tl_inode;
5866 mutex_lock(&tl_inode->i_mutex);
5867 status = __ocfs2_flush_truncate_log(osb);
5868 mutex_unlock(&tl_inode->i_mutex);
5873 static void ocfs2_truncate_log_worker(struct work_struct *work)
5876 struct ocfs2_super *osb =
5877 container_of(work, struct ocfs2_super,
5878 osb_truncate_log_wq.work);
5882 status = ocfs2_flush_truncate_log(osb);
5886 ocfs2_init_inode_steal_slot(osb);
5891 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5892 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5895 if (osb->osb_tl_inode) {
5896 /* We want to push off log flushes while truncates are
5899 cancel_delayed_work(&osb->osb_truncate_log_wq);
5901 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5902 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5906 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5908 struct inode **tl_inode,
5909 struct buffer_head **tl_bh)
5912 struct inode *inode = NULL;
5913 struct buffer_head *bh = NULL;
5915 inode = ocfs2_get_system_file_inode(osb,
5916 TRUNCATE_LOG_SYSTEM_INODE,
5920 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5924 status = ocfs2_read_inode_block(inode, &bh);
5938 /* called during the 1st stage of node recovery. we stamp a clean
5939 * truncate log and pass back a copy for processing later. if the
5940 * truncate log does not require processing, a *tl_copy is set to
5942 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5944 struct ocfs2_dinode **tl_copy)
5947 struct inode *tl_inode = NULL;
5948 struct buffer_head *tl_bh = NULL;
5949 struct ocfs2_dinode *di;
5950 struct ocfs2_truncate_log *tl;
5954 mlog(0, "recover truncate log from slot %d\n", slot_num);
5956 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5962 di = (struct ocfs2_dinode *) tl_bh->b_data;
5964 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5965 * validated by the underlying call to ocfs2_read_inode_block(),
5966 * so any corruption is a code bug */
5967 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5969 tl = &di->id2.i_dealloc;
5970 if (le16_to_cpu(tl->tl_used)) {
5971 mlog(0, "We'll have %u logs to recover\n",
5972 le16_to_cpu(tl->tl_used));
5974 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5981 /* Assuming the write-out below goes well, this copy
5982 * will be passed back to recovery for processing. */
5983 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5985 /* All we need to do to clear the truncate log is set
5989 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
5990 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
6002 if (status < 0 && (*tl_copy)) {
6011 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6012 struct ocfs2_dinode *tl_copy)
6016 unsigned int clusters, num_recs, start_cluster;
6019 struct inode *tl_inode = osb->osb_tl_inode;
6020 struct ocfs2_truncate_log *tl;
6024 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6025 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6029 tl = &tl_copy->id2.i_dealloc;
6030 num_recs = le16_to_cpu(tl->tl_used);
6031 mlog(0, "cleanup %u records from %llu\n", num_recs,
6032 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
6034 mutex_lock(&tl_inode->i_mutex);
6035 for(i = 0; i < num_recs; i++) {
6036 if (ocfs2_truncate_log_needs_flush(osb)) {
6037 status = __ocfs2_flush_truncate_log(osb);
6044 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6045 if (IS_ERR(handle)) {
6046 status = PTR_ERR(handle);
6051 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6052 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6053 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6055 status = ocfs2_truncate_log_append(osb, handle,
6056 start_blk, clusters);
6057 ocfs2_commit_trans(osb, handle);
6065 mutex_unlock(&tl_inode->i_mutex);
6071 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6074 struct inode *tl_inode = osb->osb_tl_inode;
6079 cancel_delayed_work(&osb->osb_truncate_log_wq);
6080 flush_workqueue(ocfs2_wq);
6082 status = ocfs2_flush_truncate_log(osb);
6086 brelse(osb->osb_tl_bh);
6087 iput(osb->osb_tl_inode);
6093 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6096 struct inode *tl_inode = NULL;
6097 struct buffer_head *tl_bh = NULL;
6101 status = ocfs2_get_truncate_log_info(osb,
6108 /* ocfs2_truncate_log_shutdown keys on the existence of
6109 * osb->osb_tl_inode so we don't set any of the osb variables
6110 * until we're sure all is well. */
6111 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6112 ocfs2_truncate_log_worker);
6113 osb->osb_tl_bh = tl_bh;
6114 osb->osb_tl_inode = tl_inode;
6121 * Delayed de-allocation of suballocator blocks.
6123 * Some sets of block de-allocations might involve multiple suballocator inodes.
6125 * The locking for this can get extremely complicated, especially when
6126 * the suballocator inodes to delete from aren't known until deep
6127 * within an unrelated codepath.
6129 * ocfs2_extent_block structures are a good example of this - an inode
6130 * btree could have been grown by any number of nodes each allocating
6131 * out of their own suballoc inode.
6133 * These structures allow the delay of block de-allocation until a
6134 * later time, when locking of multiple cluster inodes won't cause
6139 * Describe a single bit freed from a suballocator. For the block
6140 * suballocators, it represents one block. For the global cluster
6141 * allocator, it represents some clusters and free_bit indicates
6144 struct ocfs2_cached_block_free {
6145 struct ocfs2_cached_block_free *free_next;
6147 unsigned int free_bit;
6150 struct ocfs2_per_slot_free_list {
6151 struct ocfs2_per_slot_free_list *f_next_suballocator;
6154 struct ocfs2_cached_block_free *f_first;
6157 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6160 struct ocfs2_cached_block_free *head)
6165 struct inode *inode;
6166 struct buffer_head *di_bh = NULL;
6167 struct ocfs2_cached_block_free *tmp;
6169 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6176 mutex_lock(&inode->i_mutex);
6178 ret = ocfs2_inode_lock(inode, &di_bh, 1);
6184 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6185 if (IS_ERR(handle)) {
6186 ret = PTR_ERR(handle);
6192 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6194 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6195 head->free_bit, (unsigned long long)head->free_blk);
6197 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6198 head->free_bit, bg_blkno, 1);
6204 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6211 head = head->free_next;
6216 ocfs2_commit_trans(osb, handle);
6219 ocfs2_inode_unlock(inode, 1);
6222 mutex_unlock(&inode->i_mutex);
6226 /* Premature exit may have left some dangling items. */
6228 head = head->free_next;
6235 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6236 u64 blkno, unsigned int bit)
6239 struct ocfs2_cached_block_free *item;
6241 item = kmalloc(sizeof(*item), GFP_NOFS);
6248 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6249 bit, (unsigned long long)blkno);
6251 item->free_blk = blkno;
6252 item->free_bit = bit;
6253 item->free_next = ctxt->c_global_allocator;
6255 ctxt->c_global_allocator = item;
6259 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6260 struct ocfs2_cached_block_free *head)
6262 struct ocfs2_cached_block_free *tmp;
6263 struct inode *tl_inode = osb->osb_tl_inode;
6267 mutex_lock(&tl_inode->i_mutex);
6270 if (ocfs2_truncate_log_needs_flush(osb)) {
6271 ret = __ocfs2_flush_truncate_log(osb);
6278 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6279 if (IS_ERR(handle)) {
6280 ret = PTR_ERR(handle);
6285 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6288 ocfs2_commit_trans(osb, handle);
6290 head = head->free_next;
6299 mutex_unlock(&tl_inode->i_mutex);
6302 /* Premature exit may have left some dangling items. */
6304 head = head->free_next;
6311 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6312 struct ocfs2_cached_dealloc_ctxt *ctxt)
6315 struct ocfs2_per_slot_free_list *fl;
6320 while (ctxt->c_first_suballocator) {
6321 fl = ctxt->c_first_suballocator;
6324 mlog(0, "Free items: (type %u, slot %d)\n",
6325 fl->f_inode_type, fl->f_slot);
6326 ret2 = ocfs2_free_cached_blocks(osb,
6336 ctxt->c_first_suballocator = fl->f_next_suballocator;
6340 if (ctxt->c_global_allocator) {
6341 ret2 = ocfs2_free_cached_clusters(osb,
6342 ctxt->c_global_allocator);
6348 ctxt->c_global_allocator = NULL;
6354 static struct ocfs2_per_slot_free_list *
6355 ocfs2_find_per_slot_free_list(int type,
6357 struct ocfs2_cached_dealloc_ctxt *ctxt)
6359 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6362 if (fl->f_inode_type == type && fl->f_slot == slot)
6365 fl = fl->f_next_suballocator;
6368 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6370 fl->f_inode_type = type;
6373 fl->f_next_suballocator = ctxt->c_first_suballocator;
6375 ctxt->c_first_suballocator = fl;
6380 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6381 int type, int slot, u64 blkno,
6385 struct ocfs2_per_slot_free_list *fl;
6386 struct ocfs2_cached_block_free *item;
6388 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6395 item = kmalloc(sizeof(*item), GFP_NOFS);
6402 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6403 type, slot, bit, (unsigned long long)blkno);
6405 item->free_blk = blkno;
6406 item->free_bit = bit;
6407 item->free_next = fl->f_first;
6416 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6417 struct ocfs2_extent_block *eb)
6419 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6420 le16_to_cpu(eb->h_suballoc_slot),
6421 le64_to_cpu(eb->h_blkno),
6422 le16_to_cpu(eb->h_suballoc_bit));
6425 /* This function will figure out whether the currently last extent
6426 * block will be deleted, and if it will, what the new last extent
6427 * block will be so we can update his h_next_leaf_blk field, as well
6428 * as the dinodes i_last_eb_blk */
6429 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6430 unsigned int clusters_to_del,
6431 struct ocfs2_path *path,
6432 struct buffer_head **new_last_eb)
6434 int next_free, ret = 0;
6436 struct ocfs2_extent_rec *rec;
6437 struct ocfs2_extent_block *eb;
6438 struct ocfs2_extent_list *el;
6439 struct buffer_head *bh = NULL;
6441 *new_last_eb = NULL;
6443 /* we have no tree, so of course, no last_eb. */
6444 if (!path->p_tree_depth)
6447 /* trunc to zero special case - this makes tree_depth = 0
6448 * regardless of what it is. */
6449 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6452 el = path_leaf_el(path);
6453 BUG_ON(!el->l_next_free_rec);
6456 * Make sure that this extent list will actually be empty
6457 * after we clear away the data. We can shortcut out if
6458 * there's more than one non-empty extent in the
6459 * list. Otherwise, a check of the remaining extent is
6462 next_free = le16_to_cpu(el->l_next_free_rec);
6464 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6468 /* We may have a valid extent in index 1, check it. */
6470 rec = &el->l_recs[1];
6473 * Fall through - no more nonempty extents, so we want
6474 * to delete this leaf.
6480 rec = &el->l_recs[0];
6485 * Check it we'll only be trimming off the end of this
6488 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6492 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6498 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
6504 eb = (struct ocfs2_extent_block *) bh->b_data;
6507 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6508 * Any corruption is a code bug. */
6509 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
6512 get_bh(*new_last_eb);
6513 mlog(0, "returning block %llu, (cpos: %u)\n",
6514 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6522 * Trim some clusters off the rightmost edge of a tree. Only called
6525 * The caller needs to:
6526 * - start journaling of each path component.
6527 * - compute and fully set up any new last ext block
6529 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6530 handle_t *handle, struct ocfs2_truncate_context *tc,
6531 u32 clusters_to_del, u64 *delete_start)
6533 int ret, i, index = path->p_tree_depth;
6536 struct buffer_head *bh;
6537 struct ocfs2_extent_list *el;
6538 struct ocfs2_extent_rec *rec;
6542 while (index >= 0) {
6543 bh = path->p_node[index].bh;
6544 el = path->p_node[index].el;
6546 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6547 index, (unsigned long long)bh->b_blocknr);
6549 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6552 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6553 ocfs2_error(inode->i_sb,
6554 "Inode %lu has invalid ext. block %llu",
6556 (unsigned long long)bh->b_blocknr);
6562 i = le16_to_cpu(el->l_next_free_rec) - 1;
6563 rec = &el->l_recs[i];
6565 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6566 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6567 ocfs2_rec_clusters(el, rec),
6568 (unsigned long long)le64_to_cpu(rec->e_blkno),
6569 le16_to_cpu(el->l_next_free_rec));
6571 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6573 if (le16_to_cpu(el->l_tree_depth) == 0) {
6575 * If the leaf block contains a single empty
6576 * extent and no records, we can just remove
6579 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6581 sizeof(struct ocfs2_extent_rec));
6582 el->l_next_free_rec = cpu_to_le16(0);
6588 * Remove any empty extents by shifting things
6589 * left. That should make life much easier on
6590 * the code below. This condition is rare
6591 * enough that we shouldn't see a performance
6594 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6595 le16_add_cpu(&el->l_next_free_rec, -1);
6598 i < le16_to_cpu(el->l_next_free_rec); i++)
6599 el->l_recs[i] = el->l_recs[i + 1];
6601 memset(&el->l_recs[i], 0,
6602 sizeof(struct ocfs2_extent_rec));
6605 * We've modified our extent list. The
6606 * simplest way to handle this change
6607 * is to being the search from the
6610 goto find_tail_record;
6613 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6616 * We'll use "new_edge" on our way back up the
6617 * tree to know what our rightmost cpos is.
6619 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6620 new_edge += le32_to_cpu(rec->e_cpos);
6623 * The caller will use this to delete data blocks.
6625 *delete_start = le64_to_cpu(rec->e_blkno)
6626 + ocfs2_clusters_to_blocks(inode->i_sb,
6627 le16_to_cpu(rec->e_leaf_clusters));
6630 * If it's now empty, remove this record.
6632 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6634 sizeof(struct ocfs2_extent_rec));
6635 le16_add_cpu(&el->l_next_free_rec, -1);
6638 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6640 sizeof(struct ocfs2_extent_rec));
6641 le16_add_cpu(&el->l_next_free_rec, -1);
6646 /* Can this actually happen? */
6647 if (le16_to_cpu(el->l_next_free_rec) == 0)
6651 * We never actually deleted any clusters
6652 * because our leaf was empty. There's no
6653 * reason to adjust the rightmost edge then.
6658 rec->e_int_clusters = cpu_to_le32(new_edge);
6659 le32_add_cpu(&rec->e_int_clusters,
6660 -le32_to_cpu(rec->e_cpos));
6663 * A deleted child record should have been
6666 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6670 ret = ocfs2_journal_dirty(handle, bh);
6676 mlog(0, "extent list container %llu, after: record %d: "
6677 "(%u, %u, %llu), next = %u.\n",
6678 (unsigned long long)bh->b_blocknr, i,
6679 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6680 (unsigned long long)le64_to_cpu(rec->e_blkno),
6681 le16_to_cpu(el->l_next_free_rec));
6684 * We must be careful to only attempt delete of an
6685 * extent block (and not the root inode block).
6687 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6688 struct ocfs2_extent_block *eb =
6689 (struct ocfs2_extent_block *)bh->b_data;
6692 * Save this for use when processing the
6695 deleted_eb = le64_to_cpu(eb->h_blkno);
6697 mlog(0, "deleting this extent block.\n");
6699 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
6701 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6702 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6703 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6705 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6706 /* An error here is not fatal. */
6721 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6722 unsigned int clusters_to_del,
6723 struct inode *inode,
6724 struct buffer_head *fe_bh,
6726 struct ocfs2_truncate_context *tc,
6727 struct ocfs2_path *path)
6730 struct ocfs2_dinode *fe;
6731 struct ocfs2_extent_block *last_eb = NULL;
6732 struct ocfs2_extent_list *el;
6733 struct buffer_head *last_eb_bh = NULL;
6736 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6738 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6746 * Each component will be touched, so we might as well journal
6747 * here to avoid having to handle errors later.
6749 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
6756 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
6757 OCFS2_JOURNAL_ACCESS_WRITE);
6763 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6766 el = &(fe->id2.i_list);
6769 * Lower levels depend on this never happening, but it's best
6770 * to check it up here before changing the tree.
6772 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6773 ocfs2_error(inode->i_sb,
6774 "Inode %lu has an empty extent record, depth %u\n",
6775 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6780 vfs_dq_free_space_nodirty(inode,
6781 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
6782 spin_lock(&OCFS2_I(inode)->ip_lock);
6783 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6785 spin_unlock(&OCFS2_I(inode)->ip_lock);
6786 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6787 inode->i_blocks = ocfs2_inode_sector_count(inode);
6789 status = ocfs2_trim_tree(inode, path, handle, tc,
6790 clusters_to_del, &delete_blk);
6796 if (le32_to_cpu(fe->i_clusters) == 0) {
6797 /* trunc to zero is a special case. */
6798 el->l_tree_depth = 0;
6799 fe->i_last_eb_blk = 0;
6801 fe->i_last_eb_blk = last_eb->h_blkno;
6803 status = ocfs2_journal_dirty(handle, fe_bh);
6810 /* If there will be a new last extent block, then by
6811 * definition, there cannot be any leaves to the right of
6813 last_eb->h_next_leaf_blk = 0;
6814 status = ocfs2_journal_dirty(handle, last_eb_bh);
6822 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6836 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6838 set_buffer_uptodate(bh);
6839 mark_buffer_dirty(bh);
6843 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6844 unsigned int from, unsigned int to,
6845 struct page *page, int zero, u64 *phys)
6847 int ret, partial = 0;
6849 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6854 zero_user_segment(page, from, to);
6857 * Need to set the buffers we zero'd into uptodate
6858 * here if they aren't - ocfs2_map_page_blocks()
6859 * might've skipped some
6861 ret = walk_page_buffers(handle, page_buffers(page),
6866 else if (ocfs2_should_order_data(inode)) {
6867 ret = ocfs2_jbd2_file_inode(handle, inode);
6873 SetPageUptodate(page);
6875 flush_dcache_page(page);
6878 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6879 loff_t end, struct page **pages,
6880 int numpages, u64 phys, handle_t *handle)
6884 unsigned int from, to = PAGE_CACHE_SIZE;
6885 struct super_block *sb = inode->i_sb;
6887 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6892 to = PAGE_CACHE_SIZE;
6893 for(i = 0; i < numpages; i++) {
6896 from = start & (PAGE_CACHE_SIZE - 1);
6897 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6898 to = end & (PAGE_CACHE_SIZE - 1);
6900 BUG_ON(from > PAGE_CACHE_SIZE);
6901 BUG_ON(to > PAGE_CACHE_SIZE);
6903 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6906 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6910 ocfs2_unlock_and_free_pages(pages, numpages);
6913 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6914 struct page **pages, int *num)
6916 int numpages, ret = 0;
6917 struct super_block *sb = inode->i_sb;
6918 struct address_space *mapping = inode->i_mapping;
6919 unsigned long index;
6920 loff_t last_page_bytes;
6922 BUG_ON(start > end);
6924 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6925 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6928 last_page_bytes = PAGE_ALIGN(end);
6929 index = start >> PAGE_CACHE_SHIFT;
6931 pages[numpages] = grab_cache_page(mapping, index);
6932 if (!pages[numpages]) {
6940 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6945 ocfs2_unlock_and_free_pages(pages, numpages);
6955 * Zero the area past i_size but still within an allocated
6956 * cluster. This avoids exposing nonzero data on subsequent file
6959 * We need to call this before i_size is updated on the inode because
6960 * otherwise block_write_full_page() will skip writeout of pages past
6961 * i_size. The new_i_size parameter is passed for this reason.
6963 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6964 u64 range_start, u64 range_end)
6966 int ret = 0, numpages;
6967 struct page **pages = NULL;
6969 unsigned int ext_flags;
6970 struct super_block *sb = inode->i_sb;
6973 * File systems which don't support sparse files zero on every
6976 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6979 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6980 sizeof(struct page *), GFP_NOFS);
6981 if (pages == NULL) {
6987 if (range_start == range_end)
6990 ret = ocfs2_extent_map_get_blocks(inode,
6991 range_start >> sb->s_blocksize_bits,
6992 &phys, NULL, &ext_flags);
6999 * Tail is a hole, or is marked unwritten. In either case, we
7000 * can count on read and write to return/push zero's.
7002 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
7005 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7012 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7013 numpages, phys, handle);
7016 * Initiate writeout of the pages we zero'd here. We don't
7017 * wait on them - the truncate_inode_pages() call later will
7020 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7021 range_end - 1, SYNC_FILE_RANGE_WRITE);
7032 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7033 struct ocfs2_dinode *di)
7035 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
7036 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
7038 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7039 memset(&di->id2, 0, blocksize -
7040 offsetof(struct ocfs2_dinode, id2) -
7043 memset(&di->id2, 0, blocksize -
7044 offsetof(struct ocfs2_dinode, id2));
7047 void ocfs2_dinode_new_extent_list(struct inode *inode,
7048 struct ocfs2_dinode *di)
7050 ocfs2_zero_dinode_id2_with_xattr(inode, di);
7051 di->id2.i_list.l_tree_depth = 0;
7052 di->id2.i_list.l_next_free_rec = 0;
7053 di->id2.i_list.l_count = cpu_to_le16(
7054 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
7057 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7059 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7060 struct ocfs2_inline_data *idata = &di->id2.i_data;
7062 spin_lock(&oi->ip_lock);
7063 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7064 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7065 spin_unlock(&oi->ip_lock);
7068 * We clear the entire i_data structure here so that all
7069 * fields can be properly initialized.
7071 ocfs2_zero_dinode_id2_with_xattr(inode, di);
7073 idata->id_count = cpu_to_le16(
7074 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
7077 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7078 struct buffer_head *di_bh)
7080 int ret, i, has_data, num_pages = 0;
7082 u64 uninitialized_var(block);
7083 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7084 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7085 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7086 struct ocfs2_alloc_context *data_ac = NULL;
7087 struct page **pages = NULL;
7088 loff_t end = osb->s_clustersize;
7089 struct ocfs2_extent_tree et;
7092 has_data = i_size_read(inode) ? 1 : 0;
7095 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7096 sizeof(struct page *), GFP_NOFS);
7097 if (pages == NULL) {
7103 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7110 handle = ocfs2_start_trans(osb,
7111 ocfs2_inline_to_extents_credits(osb->sb));
7112 if (IS_ERR(handle)) {
7113 ret = PTR_ERR(handle);
7118 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7119 OCFS2_JOURNAL_ACCESS_WRITE);
7127 unsigned int page_end;
7130 if (vfs_dq_alloc_space_nodirty(inode,
7131 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7137 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7145 * Save two copies, one for insert, and one that can
7146 * be changed by ocfs2_map_and_dirty_page() below.
7148 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7151 * Non sparse file systems zero on extend, so no need
7154 if (!ocfs2_sparse_alloc(osb) &&
7155 PAGE_CACHE_SIZE < osb->s_clustersize)
7156 end = PAGE_CACHE_SIZE;
7158 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7165 * This should populate the 1st page for us and mark
7168 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7174 page_end = PAGE_CACHE_SIZE;
7175 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7176 page_end = osb->s_clustersize;
7178 for (i = 0; i < num_pages; i++)
7179 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7180 pages[i], i > 0, &phys);
7183 spin_lock(&oi->ip_lock);
7184 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7185 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7186 spin_unlock(&oi->ip_lock);
7188 ocfs2_dinode_new_extent_list(inode, di);
7190 ocfs2_journal_dirty(handle, di_bh);
7194 * An error at this point should be extremely rare. If
7195 * this proves to be false, we could always re-build
7196 * the in-inode data from our pages.
7198 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
7199 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7200 0, block, 1, 0, NULL);
7206 inode->i_blocks = ocfs2_inode_sector_count(inode);
7210 if (ret < 0 && did_quota)
7211 vfs_dq_free_space_nodirty(inode,
7212 ocfs2_clusters_to_bytes(osb->sb, 1));
7214 ocfs2_commit_trans(osb, handle);
7218 ocfs2_free_alloc_context(data_ac);
7222 ocfs2_unlock_and_free_pages(pages, num_pages);
7230 * It is expected, that by the time you call this function,
7231 * inode->i_size and fe->i_size have been adjusted.
7233 * WARNING: This will kfree the truncate context
7235 int ocfs2_commit_truncate(struct ocfs2_super *osb,
7236 struct inode *inode,
7237 struct buffer_head *fe_bh,
7238 struct ocfs2_truncate_context *tc)
7240 int status, i, credits, tl_sem = 0;
7241 u32 clusters_to_del, new_highest_cpos, range;
7242 struct ocfs2_extent_list *el;
7243 handle_t *handle = NULL;
7244 struct inode *tl_inode = osb->osb_tl_inode;
7245 struct ocfs2_path *path = NULL;
7246 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
7250 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
7251 i_size_read(inode));
7253 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7254 ocfs2_journal_access_di);
7261 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7265 * Check that we still have allocation to delete.
7267 if (OCFS2_I(inode)->ip_clusters == 0) {
7273 * Truncate always works against the rightmost tree branch.
7275 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
7281 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7282 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7285 * By now, el will point to the extent list on the bottom most
7286 * portion of this tree. Only the tail record is considered in
7289 * We handle the following cases, in order:
7290 * - empty extent: delete the remaining branch
7291 * - remove the entire record
7292 * - remove a partial record
7293 * - no record needs to be removed (truncate has completed)
7295 el = path_leaf_el(path);
7296 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7297 ocfs2_error(inode->i_sb,
7298 "Inode %llu has empty extent block at %llu\n",
7299 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7300 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7305 i = le16_to_cpu(el->l_next_free_rec) - 1;
7306 range = le32_to_cpu(el->l_recs[i].e_cpos) +
7307 ocfs2_rec_clusters(el, &el->l_recs[i]);
7308 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7309 clusters_to_del = 0;
7310 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
7311 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
7312 } else if (range > new_highest_cpos) {
7313 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
7314 le32_to_cpu(el->l_recs[i].e_cpos)) -
7321 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7322 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7324 mutex_lock(&tl_inode->i_mutex);
7326 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7327 * record is free for use. If there isn't any, we flush to get
7328 * an empty truncate log. */
7329 if (ocfs2_truncate_log_needs_flush(osb)) {
7330 status = __ocfs2_flush_truncate_log(osb);
7337 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
7338 (struct ocfs2_dinode *)fe_bh->b_data,
7340 handle = ocfs2_start_trans(osb, credits);
7341 if (IS_ERR(handle)) {
7342 status = PTR_ERR(handle);
7348 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7355 mutex_unlock(&tl_inode->i_mutex);
7358 ocfs2_commit_trans(osb, handle);
7361 ocfs2_reinit_path(path, 1);
7364 * The check above will catch the case where we've truncated
7365 * away all allocation.
7371 ocfs2_schedule_truncate_log_flush(osb, 1);
7374 mutex_unlock(&tl_inode->i_mutex);
7377 ocfs2_commit_trans(osb, handle);
7379 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7381 ocfs2_free_path(path);
7383 /* This will drop the ext_alloc cluster lock for us */
7384 ocfs2_free_truncate_context(tc);
7391 * Expects the inode to already be locked.
7393 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7394 struct inode *inode,
7395 struct buffer_head *fe_bh,
7396 struct ocfs2_truncate_context **tc)
7399 unsigned int new_i_clusters;
7400 struct ocfs2_dinode *fe;
7401 struct ocfs2_extent_block *eb;
7402 struct buffer_head *last_eb_bh = NULL;
7408 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7409 i_size_read(inode));
7410 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7412 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7413 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7414 (unsigned long long)le64_to_cpu(fe->i_size));
7416 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7422 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7424 if (fe->id2.i_list.l_tree_depth) {
7425 status = ocfs2_read_extent_block(INODE_CACHE(inode),
7426 le64_to_cpu(fe->i_last_eb_blk),
7432 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7435 (*tc)->tc_last_eb_bh = last_eb_bh;
7441 ocfs2_free_truncate_context(*tc);
7449 * 'start' is inclusive, 'end' is not.
7451 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7452 unsigned int start, unsigned int end, int trunc)
7455 unsigned int numbytes;
7457 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7458 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7459 struct ocfs2_inline_data *idata = &di->id2.i_data;
7461 if (end > i_size_read(inode))
7462 end = i_size_read(inode);
7464 BUG_ON(start >= end);
7466 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7467 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7468 !ocfs2_supports_inline_data(osb)) {
7469 ocfs2_error(inode->i_sb,
7470 "Inline data flags for inode %llu don't agree! "
7471 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7472 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7473 le16_to_cpu(di->i_dyn_features),
7474 OCFS2_I(inode)->ip_dyn_features,
7475 osb->s_feature_incompat);
7480 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7481 if (IS_ERR(handle)) {
7482 ret = PTR_ERR(handle);
7487 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7488 OCFS2_JOURNAL_ACCESS_WRITE);
7494 numbytes = end - start;
7495 memset(idata->id_data + start, 0, numbytes);
7498 * No need to worry about the data page here - it's been
7499 * truncated already and inline data doesn't need it for
7500 * pushing zero's to disk, so we'll let readpage pick it up
7504 i_size_write(inode, start);
7505 di->i_size = cpu_to_le64(start);
7508 inode->i_blocks = ocfs2_inode_sector_count(inode);
7509 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7511 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7512 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7514 ocfs2_journal_dirty(handle, di_bh);
7517 ocfs2_commit_trans(osb, handle);
7523 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7526 * The caller is responsible for completing deallocation
7527 * before freeing the context.
7529 if (tc->tc_dealloc.c_first_suballocator != NULL)
7531 "Truncate completion has non-empty dealloc context\n");
7533 brelse(tc->tc_last_eb_bh);