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(struct inode *inode,
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 inode *inode,
1013 struct ocfs2_extent_tree *et)
1016 struct ocfs2_path *path = NULL;
1017 struct ocfs2_extent_list *el;
1018 struct ocfs2_extent_rec *rec;
1020 path = ocfs2_new_path_from_et(et);
1026 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
1032 status = ocfs2_extend_trans(handle, path_num_items(path) +
1033 handle->h_buffer_credits);
1039 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
1045 el = path_leaf_el(path);
1046 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1048 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
1051 ocfs2_free_path(path);
1056 * Add an entire tree branch to our inode. eb_bh is the extent block
1057 * to start at, if we don't want to start the branch at the dinode
1060 * last_eb_bh is required as we have to update it's next_leaf pointer
1061 * for the new last extent block.
1063 * the new branch will be 'empty' in the sense that every block will
1064 * contain a single record with cluster count == 0.
1066 static int ocfs2_add_branch(struct ocfs2_super *osb,
1068 struct inode *inode,
1069 struct ocfs2_extent_tree *et,
1070 struct buffer_head *eb_bh,
1071 struct buffer_head **last_eb_bh,
1072 struct ocfs2_alloc_context *meta_ac)
1074 int status, new_blocks, i;
1075 u64 next_blkno, new_last_eb_blk;
1076 struct buffer_head *bh;
1077 struct buffer_head **new_eb_bhs = NULL;
1078 struct ocfs2_extent_block *eb;
1079 struct ocfs2_extent_list *eb_el;
1080 struct ocfs2_extent_list *el;
1081 u32 new_cpos, root_end;
1085 BUG_ON(!last_eb_bh || !*last_eb_bh);
1088 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1091 el = et->et_root_el;
1093 /* we never add a branch to a leaf. */
1094 BUG_ON(!el->l_tree_depth);
1096 new_blocks = le16_to_cpu(el->l_tree_depth);
1098 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1099 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1100 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1103 * If there is a gap before the root end and the real end
1104 * of the righmost leaf block, we need to remove the gap
1105 * between new_cpos and root_end first so that the tree
1106 * is consistent after we add a new branch(it will start
1109 if (root_end > new_cpos) {
1110 mlog(0, "adjust the cluster end from %u to %u\n",
1111 root_end, new_cpos);
1112 status = ocfs2_adjust_rightmost_branch(handle, inode, et);
1119 /* allocate the number of new eb blocks we need */
1120 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1128 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
1129 meta_ac, new_eb_bhs);
1135 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1136 * linked with the rest of the tree.
1137 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1139 * when we leave the loop, new_last_eb_blk will point to the
1140 * newest leaf, and next_blkno will point to the topmost extent
1142 next_blkno = new_last_eb_blk = 0;
1143 for(i = 0; i < new_blocks; i++) {
1145 eb = (struct ocfs2_extent_block *) bh->b_data;
1146 /* ocfs2_create_new_meta_bhs() should create it right! */
1147 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1148 eb_el = &eb->h_list;
1150 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), bh,
1151 OCFS2_JOURNAL_ACCESS_CREATE);
1157 eb->h_next_leaf_blk = 0;
1158 eb_el->l_tree_depth = cpu_to_le16(i);
1159 eb_el->l_next_free_rec = cpu_to_le16(1);
1161 * This actually counts as an empty extent as
1164 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
1165 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
1167 * eb_el isn't always an interior node, but even leaf
1168 * nodes want a zero'd flags and reserved field so
1169 * this gets the whole 32 bits regardless of use.
1171 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
1172 if (!eb_el->l_tree_depth)
1173 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1175 status = ocfs2_journal_dirty(handle, bh);
1181 next_blkno = le64_to_cpu(eb->h_blkno);
1184 /* This is a bit hairy. We want to update up to three blocks
1185 * here without leaving any of them in an inconsistent state
1186 * in case of error. We don't have to worry about
1187 * journal_dirty erroring as it won't unless we've aborted the
1188 * handle (in which case we would never be here) so reserving
1189 * the write with journal_access is all we need to do. */
1190 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), *last_eb_bh,
1191 OCFS2_JOURNAL_ACCESS_WRITE);
1196 status = ocfs2_et_root_journal_access(handle, et,
1197 OCFS2_JOURNAL_ACCESS_WRITE);
1203 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), eb_bh,
1204 OCFS2_JOURNAL_ACCESS_WRITE);
1211 /* Link the new branch into the rest of the tree (el will
1212 * either be on the root_bh, or the extent block passed in. */
1213 i = le16_to_cpu(el->l_next_free_rec);
1214 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1215 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1216 el->l_recs[i].e_int_clusters = 0;
1217 le16_add_cpu(&el->l_next_free_rec, 1);
1219 /* fe needs a new last extent block pointer, as does the
1220 * next_leaf on the previously last-extent-block. */
1221 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1223 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1224 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1226 status = ocfs2_journal_dirty(handle, *last_eb_bh);
1229 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1233 status = ocfs2_journal_dirty(handle, eb_bh);
1239 * Some callers want to track the rightmost leaf so pass it
1242 brelse(*last_eb_bh);
1243 get_bh(new_eb_bhs[0]);
1244 *last_eb_bh = new_eb_bhs[0];
1249 for (i = 0; i < new_blocks; i++)
1250 brelse(new_eb_bhs[i]);
1259 * adds another level to the allocation tree.
1260 * returns back the new extent block so you can add a branch to it
1263 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
1265 struct inode *inode,
1266 struct ocfs2_extent_tree *et,
1267 struct ocfs2_alloc_context *meta_ac,
1268 struct buffer_head **ret_new_eb_bh)
1272 struct buffer_head *new_eb_bh = NULL;
1273 struct ocfs2_extent_block *eb;
1274 struct ocfs2_extent_list *root_el;
1275 struct ocfs2_extent_list *eb_el;
1279 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
1286 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1287 /* ocfs2_create_new_meta_bhs() should create it right! */
1288 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1290 eb_el = &eb->h_list;
1291 root_el = et->et_root_el;
1293 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), new_eb_bh,
1294 OCFS2_JOURNAL_ACCESS_CREATE);
1300 /* copy the root extent list data into the new extent block */
1301 eb_el->l_tree_depth = root_el->l_tree_depth;
1302 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1303 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1304 eb_el->l_recs[i] = root_el->l_recs[i];
1306 status = ocfs2_journal_dirty(handle, new_eb_bh);
1312 status = ocfs2_et_root_journal_access(handle, et,
1313 OCFS2_JOURNAL_ACCESS_WRITE);
1319 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1321 /* update root_bh now */
1322 le16_add_cpu(&root_el->l_tree_depth, 1);
1323 root_el->l_recs[0].e_cpos = 0;
1324 root_el->l_recs[0].e_blkno = eb->h_blkno;
1325 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1326 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1327 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1328 root_el->l_next_free_rec = cpu_to_le16(1);
1330 /* If this is our 1st tree depth shift, then last_eb_blk
1331 * becomes the allocated extent block */
1332 if (root_el->l_tree_depth == cpu_to_le16(1))
1333 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1335 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1341 *ret_new_eb_bh = new_eb_bh;
1352 * Should only be called when there is no space left in any of the
1353 * leaf nodes. What we want to do is find the lowest tree depth
1354 * non-leaf extent block with room for new records. There are three
1355 * valid results of this search:
1357 * 1) a lowest extent block is found, then we pass it back in
1358 * *lowest_eb_bh and return '0'
1360 * 2) the search fails to find anything, but the root_el has room. We
1361 * pass NULL back in *lowest_eb_bh, but still return '0'
1363 * 3) the search fails to find anything AND the root_el is full, in
1364 * which case we return > 0
1366 * return status < 0 indicates an error.
1368 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1369 struct ocfs2_extent_tree *et,
1370 struct buffer_head **target_bh)
1374 struct ocfs2_extent_block *eb;
1375 struct ocfs2_extent_list *el;
1376 struct buffer_head *bh = NULL;
1377 struct buffer_head *lowest_bh = NULL;
1383 el = et->et_root_el;
1385 while(le16_to_cpu(el->l_tree_depth) > 1) {
1386 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1387 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1388 "Owner %llu has empty "
1389 "extent list (next_free_rec == 0)",
1390 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
1394 i = le16_to_cpu(el->l_next_free_rec) - 1;
1395 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1397 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1398 "Owner %llu has extent "
1399 "list where extent # %d has no physical "
1401 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
1409 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
1415 eb = (struct ocfs2_extent_block *) bh->b_data;
1418 if (le16_to_cpu(el->l_next_free_rec) <
1419 le16_to_cpu(el->l_count)) {
1426 /* If we didn't find one and the fe doesn't have any room,
1427 * then return '1' */
1428 el = et->et_root_el;
1429 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1432 *target_bh = lowest_bh;
1441 * Grow a b-tree so that it has more records.
1443 * We might shift the tree depth in which case existing paths should
1444 * be considered invalid.
1446 * Tree depth after the grow is returned via *final_depth.
1448 * *last_eb_bh will be updated by ocfs2_add_branch().
1450 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
1451 struct ocfs2_extent_tree *et, int *final_depth,
1452 struct buffer_head **last_eb_bh,
1453 struct ocfs2_alloc_context *meta_ac)
1456 struct ocfs2_extent_list *el = et->et_root_el;
1457 int depth = le16_to_cpu(el->l_tree_depth);
1458 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1459 struct buffer_head *bh = NULL;
1461 BUG_ON(meta_ac == NULL);
1463 shift = ocfs2_find_branch_target(osb, et, &bh);
1470 /* We traveled all the way to the bottom of the allocation tree
1471 * and didn't find room for any more extents - we need to add
1472 * another tree level */
1475 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1477 /* ocfs2_shift_tree_depth will return us a buffer with
1478 * the new extent block (so we can pass that to
1479 * ocfs2_add_branch). */
1480 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
1489 * Special case: we have room now if we shifted from
1490 * tree_depth 0, so no more work needs to be done.
1492 * We won't be calling add_branch, so pass
1493 * back *last_eb_bh as the new leaf. At depth
1494 * zero, it should always be null so there's
1495 * no reason to brelse.
1497 BUG_ON(*last_eb_bh);
1504 /* call ocfs2_add_branch to add the final part of the tree with
1506 mlog(0, "add branch. bh = %p\n", bh);
1507 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
1516 *final_depth = depth;
1522 * This function will discard the rightmost extent record.
1524 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1526 int next_free = le16_to_cpu(el->l_next_free_rec);
1527 int count = le16_to_cpu(el->l_count);
1528 unsigned int num_bytes;
1531 /* This will cause us to go off the end of our extent list. */
1532 BUG_ON(next_free >= count);
1534 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1536 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1539 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1540 struct ocfs2_extent_rec *insert_rec)
1542 int i, insert_index, next_free, has_empty, num_bytes;
1543 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1544 struct ocfs2_extent_rec *rec;
1546 next_free = le16_to_cpu(el->l_next_free_rec);
1547 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1551 /* The tree code before us didn't allow enough room in the leaf. */
1552 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1555 * The easiest way to approach this is to just remove the
1556 * empty extent and temporarily decrement next_free.
1560 * If next_free was 1 (only an empty extent), this
1561 * loop won't execute, which is fine. We still want
1562 * the decrement above to happen.
1564 for(i = 0; i < (next_free - 1); i++)
1565 el->l_recs[i] = el->l_recs[i+1];
1571 * Figure out what the new record index should be.
1573 for(i = 0; i < next_free; i++) {
1574 rec = &el->l_recs[i];
1576 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1581 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1582 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1584 BUG_ON(insert_index < 0);
1585 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1586 BUG_ON(insert_index > next_free);
1589 * No need to memmove if we're just adding to the tail.
1591 if (insert_index != next_free) {
1592 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1594 num_bytes = next_free - insert_index;
1595 num_bytes *= sizeof(struct ocfs2_extent_rec);
1596 memmove(&el->l_recs[insert_index + 1],
1597 &el->l_recs[insert_index],
1602 * Either we had an empty extent, and need to re-increment or
1603 * there was no empty extent on a non full rightmost leaf node,
1604 * in which case we still need to increment.
1607 el->l_next_free_rec = cpu_to_le16(next_free);
1609 * Make sure none of the math above just messed up our tree.
1611 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1613 el->l_recs[insert_index] = *insert_rec;
1617 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1619 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1621 BUG_ON(num_recs == 0);
1623 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1625 size = num_recs * sizeof(struct ocfs2_extent_rec);
1626 memmove(&el->l_recs[0], &el->l_recs[1], size);
1627 memset(&el->l_recs[num_recs], 0,
1628 sizeof(struct ocfs2_extent_rec));
1629 el->l_next_free_rec = cpu_to_le16(num_recs);
1634 * Create an empty extent record .
1636 * l_next_free_rec may be updated.
1638 * If an empty extent already exists do nothing.
1640 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1642 int next_free = le16_to_cpu(el->l_next_free_rec);
1644 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1649 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1652 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1653 "Asked to create an empty extent in a full list:\n"
1654 "count = %u, tree depth = %u",
1655 le16_to_cpu(el->l_count),
1656 le16_to_cpu(el->l_tree_depth));
1658 ocfs2_shift_records_right(el);
1661 le16_add_cpu(&el->l_next_free_rec, 1);
1662 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1666 * For a rotation which involves two leaf nodes, the "root node" is
1667 * the lowest level tree node which contains a path to both leafs. This
1668 * resulting set of information can be used to form a complete "subtree"
1670 * This function is passed two full paths from the dinode down to a
1671 * pair of adjacent leaves. It's task is to figure out which path
1672 * index contains the subtree root - this can be the root index itself
1673 * in a worst-case rotation.
1675 * The array index of the subtree root is passed back.
1677 static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1678 struct ocfs2_path *left,
1679 struct ocfs2_path *right)
1684 * Check that the caller passed in two paths from the same tree.
1686 BUG_ON(path_root_bh(left) != path_root_bh(right));
1692 * The caller didn't pass two adjacent paths.
1694 mlog_bug_on_msg(i > left->p_tree_depth,
1695 "Owner %llu, left depth %u, right depth %u\n"
1696 "left leaf blk %llu, right leaf blk %llu\n",
1697 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1698 left->p_tree_depth, right->p_tree_depth,
1699 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1700 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1701 } while (left->p_node[i].bh->b_blocknr ==
1702 right->p_node[i].bh->b_blocknr);
1707 typedef void (path_insert_t)(void *, struct buffer_head *);
1710 * Traverse a btree path in search of cpos, starting at root_el.
1712 * This code can be called with a cpos larger than the tree, in which
1713 * case it will return the rightmost path.
1715 static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
1716 struct ocfs2_extent_list *root_el, u32 cpos,
1717 path_insert_t *func, void *data)
1722 struct buffer_head *bh = NULL;
1723 struct ocfs2_extent_block *eb;
1724 struct ocfs2_extent_list *el;
1725 struct ocfs2_extent_rec *rec;
1728 while (el->l_tree_depth) {
1729 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1730 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1731 "Owner %llu has empty extent list at "
1733 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1734 le16_to_cpu(el->l_tree_depth));
1740 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1741 rec = &el->l_recs[i];
1744 * In the case that cpos is off the allocation
1745 * tree, this should just wind up returning the
1748 range = le32_to_cpu(rec->e_cpos) +
1749 ocfs2_rec_clusters(el, rec);
1750 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1754 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1756 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1757 "Owner %llu has bad blkno in extent list "
1758 "at depth %u (index %d)\n",
1759 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1760 le16_to_cpu(el->l_tree_depth), i);
1767 ret = ocfs2_read_extent_block(ci, blkno, &bh);
1773 eb = (struct ocfs2_extent_block *) bh->b_data;
1776 if (le16_to_cpu(el->l_next_free_rec) >
1777 le16_to_cpu(el->l_count)) {
1778 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1779 "Owner %llu has bad count in extent list "
1780 "at block %llu (next free=%u, count=%u)\n",
1781 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1782 (unsigned long long)bh->b_blocknr,
1783 le16_to_cpu(el->l_next_free_rec),
1784 le16_to_cpu(el->l_count));
1795 * Catch any trailing bh that the loop didn't handle.
1803 * Given an initialized path (that is, it has a valid root extent
1804 * list), this function will traverse the btree in search of the path
1805 * which would contain cpos.
1807 * The path traveled is recorded in the path structure.
1809 * Note that this will not do any comparisons on leaf node extent
1810 * records, so it will work fine in the case that we just added a tree
1813 struct find_path_data {
1815 struct ocfs2_path *path;
1817 static void find_path_ins(void *data, struct buffer_head *bh)
1819 struct find_path_data *fp = data;
1822 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1825 static int ocfs2_find_path(struct ocfs2_caching_info *ci,
1826 struct ocfs2_path *path, u32 cpos)
1828 struct find_path_data data;
1832 return __ocfs2_find_path(ci, path_root_el(path), cpos,
1833 find_path_ins, &data);
1836 static void find_leaf_ins(void *data, struct buffer_head *bh)
1838 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1839 struct ocfs2_extent_list *el = &eb->h_list;
1840 struct buffer_head **ret = data;
1842 /* We want to retain only the leaf block. */
1843 if (le16_to_cpu(el->l_tree_depth) == 0) {
1849 * Find the leaf block in the tree which would contain cpos. No
1850 * checking of the actual leaf is done.
1852 * Some paths want to call this instead of allocating a path structure
1853 * and calling ocfs2_find_path().
1855 * This function doesn't handle non btree extent lists.
1857 int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1858 struct ocfs2_extent_list *root_el, u32 cpos,
1859 struct buffer_head **leaf_bh)
1862 struct buffer_head *bh = NULL;
1864 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
1876 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1878 * Basically, we've moved stuff around at the bottom of the tree and
1879 * we need to fix up the extent records above the changes to reflect
1882 * left_rec: the record on the left.
1883 * left_child_el: is the child list pointed to by left_rec
1884 * right_rec: the record to the right of left_rec
1885 * right_child_el: is the child list pointed to by right_rec
1887 * By definition, this only works on interior nodes.
1889 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1890 struct ocfs2_extent_list *left_child_el,
1891 struct ocfs2_extent_rec *right_rec,
1892 struct ocfs2_extent_list *right_child_el)
1894 u32 left_clusters, right_end;
1897 * Interior nodes never have holes. Their cpos is the cpos of
1898 * the leftmost record in their child list. Their cluster
1899 * count covers the full theoretical range of their child list
1900 * - the range between their cpos and the cpos of the record
1901 * immediately to their right.
1903 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1904 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1905 BUG_ON(right_child_el->l_tree_depth);
1906 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1907 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1909 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1910 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1913 * Calculate the rightmost cluster count boundary before
1914 * moving cpos - we will need to adjust clusters after
1915 * updating e_cpos to keep the same highest cluster count.
1917 right_end = le32_to_cpu(right_rec->e_cpos);
1918 right_end += le32_to_cpu(right_rec->e_int_clusters);
1920 right_rec->e_cpos = left_rec->e_cpos;
1921 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1923 right_end -= le32_to_cpu(right_rec->e_cpos);
1924 right_rec->e_int_clusters = cpu_to_le32(right_end);
1928 * Adjust the adjacent root node records involved in a
1929 * rotation. left_el_blkno is passed in as a key so that we can easily
1930 * find it's index in the root list.
1932 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1933 struct ocfs2_extent_list *left_el,
1934 struct ocfs2_extent_list *right_el,
1939 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1940 le16_to_cpu(left_el->l_tree_depth));
1942 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1943 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1948 * The path walking code should have never returned a root and
1949 * two paths which are not adjacent.
1951 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1953 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1954 &root_el->l_recs[i + 1], right_el);
1958 * We've changed a leaf block (in right_path) and need to reflect that
1959 * change back up the subtree.
1961 * This happens in multiple places:
1962 * - When we've moved an extent record from the left path leaf to the right
1963 * path leaf to make room for an empty extent in the left path leaf.
1964 * - When our insert into the right path leaf is at the leftmost edge
1965 * and requires an update of the path immediately to it's left. This
1966 * can occur at the end of some types of rotation and appending inserts.
1967 * - When we've adjusted the last extent record in the left path leaf and the
1968 * 1st extent record in the right path leaf during cross extent block merge.
1970 static void ocfs2_complete_edge_insert(handle_t *handle,
1971 struct ocfs2_path *left_path,
1972 struct ocfs2_path *right_path,
1976 struct ocfs2_extent_list *el, *left_el, *right_el;
1977 struct ocfs2_extent_rec *left_rec, *right_rec;
1978 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1981 * Update the counts and position values within all the
1982 * interior nodes to reflect the leaf rotation we just did.
1984 * The root node is handled below the loop.
1986 * We begin the loop with right_el and left_el pointing to the
1987 * leaf lists and work our way up.
1989 * NOTE: within this loop, left_el and right_el always refer
1990 * to the *child* lists.
1992 left_el = path_leaf_el(left_path);
1993 right_el = path_leaf_el(right_path);
1994 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1995 mlog(0, "Adjust records at index %u\n", i);
1998 * One nice property of knowing that all of these
1999 * nodes are below the root is that we only deal with
2000 * the leftmost right node record and the rightmost
2003 el = left_path->p_node[i].el;
2004 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2005 left_rec = &el->l_recs[idx];
2007 el = right_path->p_node[i].el;
2008 right_rec = &el->l_recs[0];
2010 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2013 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2017 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2022 * Setup our list pointers now so that the current
2023 * parents become children in the next iteration.
2025 left_el = left_path->p_node[i].el;
2026 right_el = right_path->p_node[i].el;
2030 * At the root node, adjust the two adjacent records which
2031 * begin our path to the leaves.
2034 el = left_path->p_node[subtree_index].el;
2035 left_el = left_path->p_node[subtree_index + 1].el;
2036 right_el = right_path->p_node[subtree_index + 1].el;
2038 ocfs2_adjust_root_records(el, left_el, right_el,
2039 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2041 root_bh = left_path->p_node[subtree_index].bh;
2043 ret = ocfs2_journal_dirty(handle, root_bh);
2048 static int ocfs2_rotate_subtree_right(handle_t *handle,
2049 struct ocfs2_extent_tree *et,
2050 struct ocfs2_path *left_path,
2051 struct ocfs2_path *right_path,
2055 struct buffer_head *right_leaf_bh;
2056 struct buffer_head *left_leaf_bh = NULL;
2057 struct buffer_head *root_bh;
2058 struct ocfs2_extent_list *right_el, *left_el;
2059 struct ocfs2_extent_rec move_rec;
2061 left_leaf_bh = path_leaf_bh(left_path);
2062 left_el = path_leaf_el(left_path);
2064 if (left_el->l_next_free_rec != left_el->l_count) {
2065 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
2066 "Inode %llu has non-full interior leaf node %llu"
2068 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2069 (unsigned long long)left_leaf_bh->b_blocknr,
2070 le16_to_cpu(left_el->l_next_free_rec));
2075 * This extent block may already have an empty record, so we
2076 * return early if so.
2078 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2081 root_bh = left_path->p_node[subtree_index].bh;
2082 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2084 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2091 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2092 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2099 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2107 right_leaf_bh = path_leaf_bh(right_path);
2108 right_el = path_leaf_el(right_path);
2110 /* This is a code error, not a disk corruption. */
2111 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2112 "because rightmost leaf block %llu is empty\n",
2113 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2114 (unsigned long long)right_leaf_bh->b_blocknr);
2116 ocfs2_create_empty_extent(right_el);
2118 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2124 /* Do the copy now. */
2125 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2126 move_rec = left_el->l_recs[i];
2127 right_el->l_recs[0] = move_rec;
2130 * Clear out the record we just copied and shift everything
2131 * over, leaving an empty extent in the left leaf.
2133 * We temporarily subtract from next_free_rec so that the
2134 * shift will lose the tail record (which is now defunct).
2136 le16_add_cpu(&left_el->l_next_free_rec, -1);
2137 ocfs2_shift_records_right(left_el);
2138 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2139 le16_add_cpu(&left_el->l_next_free_rec, 1);
2141 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2147 ocfs2_complete_edge_insert(handle, left_path, right_path,
2155 * Given a full path, determine what cpos value would return us a path
2156 * containing the leaf immediately to the left of the current one.
2158 * Will return zero if the path passed in is already the leftmost path.
2160 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2161 struct ocfs2_path *path, u32 *cpos)
2165 struct ocfs2_extent_list *el;
2167 BUG_ON(path->p_tree_depth == 0);
2171 blkno = path_leaf_bh(path)->b_blocknr;
2173 /* Start at the tree node just above the leaf and work our way up. */
2174 i = path->p_tree_depth - 1;
2176 el = path->p_node[i].el;
2179 * Find the extent record just before the one in our
2182 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2183 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2187 * We've determined that the
2188 * path specified is already
2189 * the leftmost one - return a
2195 * The leftmost record points to our
2196 * leaf - we need to travel up the
2202 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2203 *cpos = *cpos + ocfs2_rec_clusters(el,
2204 &el->l_recs[j - 1]);
2211 * If we got here, we never found a valid node where
2212 * the tree indicated one should be.
2215 "Invalid extent tree at extent block %llu\n",
2216 (unsigned long long)blkno);
2221 blkno = path->p_node[i].bh->b_blocknr;
2230 * Extend the transaction by enough credits to complete the rotation,
2231 * and still leave at least the original number of credits allocated
2232 * to this transaction.
2234 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2236 struct ocfs2_path *path)
2238 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2240 if (handle->h_buffer_credits < credits)
2241 return ocfs2_extend_trans(handle, credits);
2247 * Trap the case where we're inserting into the theoretical range past
2248 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2249 * whose cpos is less than ours into the right leaf.
2251 * It's only necessary to look at the rightmost record of the left
2252 * leaf because the logic that calls us should ensure that the
2253 * theoretical ranges in the path components above the leaves are
2256 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2259 struct ocfs2_extent_list *left_el;
2260 struct ocfs2_extent_rec *rec;
2263 left_el = path_leaf_el(left_path);
2264 next_free = le16_to_cpu(left_el->l_next_free_rec);
2265 rec = &left_el->l_recs[next_free - 1];
2267 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2272 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2274 int next_free = le16_to_cpu(el->l_next_free_rec);
2276 struct ocfs2_extent_rec *rec;
2281 rec = &el->l_recs[0];
2282 if (ocfs2_is_empty_extent(rec)) {
2286 rec = &el->l_recs[1];
2289 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2290 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2296 * Rotate all the records in a btree right one record, starting at insert_cpos.
2298 * The path to the rightmost leaf should be passed in.
2300 * The array is assumed to be large enough to hold an entire path (tree depth).
2302 * Upon succesful return from this function:
2304 * - The 'right_path' array will contain a path to the leaf block
2305 * whose range contains e_cpos.
2306 * - That leaf block will have a single empty extent in list index 0.
2307 * - In the case that the rotation requires a post-insert update,
2308 * *ret_left_path will contain a valid path which can be passed to
2309 * ocfs2_insert_path().
2311 static int ocfs2_rotate_tree_right(handle_t *handle,
2312 struct ocfs2_extent_tree *et,
2313 enum ocfs2_split_type split,
2315 struct ocfs2_path *right_path,
2316 struct ocfs2_path **ret_left_path)
2318 int ret, start, orig_credits = handle->h_buffer_credits;
2320 struct ocfs2_path *left_path = NULL;
2321 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2323 *ret_left_path = NULL;
2325 left_path = ocfs2_new_path_from_path(right_path);
2332 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2338 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2341 * What we want to do here is:
2343 * 1) Start with the rightmost path.
2345 * 2) Determine a path to the leaf block directly to the left
2348 * 3) Determine the 'subtree root' - the lowest level tree node
2349 * which contains a path to both leaves.
2351 * 4) Rotate the subtree.
2353 * 5) Find the next subtree by considering the left path to be
2354 * the new right path.
2356 * The check at the top of this while loop also accepts
2357 * insert_cpos == cpos because cpos is only a _theoretical_
2358 * value to get us the left path - insert_cpos might very well
2359 * be filling that hole.
2361 * Stop at a cpos of '0' because we either started at the
2362 * leftmost branch (i.e., a tree with one branch and a
2363 * rotation inside of it), or we've gone as far as we can in
2364 * rotating subtrees.
2366 while (cpos && insert_cpos <= cpos) {
2367 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2370 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
2376 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2377 path_leaf_bh(right_path),
2378 "Owner %llu: error during insert of %u "
2379 "(left path cpos %u) results in two identical "
2380 "paths ending at %llu\n",
2381 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2383 (unsigned long long)
2384 path_leaf_bh(left_path)->b_blocknr);
2386 if (split == SPLIT_NONE &&
2387 ocfs2_rotate_requires_path_adjustment(left_path,
2391 * We've rotated the tree as much as we
2392 * should. The rest is up to
2393 * ocfs2_insert_path() to complete, after the
2394 * record insertion. We indicate this
2395 * situation by returning the left path.
2397 * The reason we don't adjust the records here
2398 * before the record insert is that an error
2399 * later might break the rule where a parent
2400 * record e_cpos will reflect the actual
2401 * e_cpos of the 1st nonempty record of the
2404 *ret_left_path = left_path;
2408 start = ocfs2_find_subtree_root(et, left_path, right_path);
2410 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2412 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2413 right_path->p_tree_depth);
2415 ret = ocfs2_extend_rotate_transaction(handle, start,
2416 orig_credits, right_path);
2422 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
2429 if (split != SPLIT_NONE &&
2430 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2433 * A rotate moves the rightmost left leaf
2434 * record over to the leftmost right leaf
2435 * slot. If we're doing an extent split
2436 * instead of a real insert, then we have to
2437 * check that the extent to be split wasn't
2438 * just moved over. If it was, then we can
2439 * exit here, passing left_path back -
2440 * ocfs2_split_extent() is smart enough to
2441 * search both leaves.
2443 *ret_left_path = left_path;
2448 * There is no need to re-read the next right path
2449 * as we know that it'll be our current left
2450 * path. Optimize by copying values instead.
2452 ocfs2_mv_path(right_path, left_path);
2454 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2462 ocfs2_free_path(left_path);
2468 static int ocfs2_update_edge_lengths(handle_t *handle,
2469 struct ocfs2_extent_tree *et,
2470 int subtree_index, struct ocfs2_path *path)
2473 struct ocfs2_extent_rec *rec;
2474 struct ocfs2_extent_list *el;
2475 struct ocfs2_extent_block *eb;
2479 * In normal tree rotation process, we will never touch the
2480 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2481 * doesn't reserve the credits for them either.
2483 * But we do have a special case here which will update the rightmost
2484 * records for all the bh in the path.
2485 * So we have to allocate extra credits and access them.
2487 ret = ocfs2_extend_trans(handle,
2488 handle->h_buffer_credits + subtree_index);
2494 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
2500 /* Path should always be rightmost. */
2501 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2502 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2505 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2506 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2507 rec = &el->l_recs[idx];
2508 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2510 for (i = 0; i < path->p_tree_depth; i++) {
2511 el = path->p_node[i].el;
2512 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2513 rec = &el->l_recs[idx];
2515 rec->e_int_clusters = cpu_to_le32(range);
2516 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2518 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2524 static void ocfs2_unlink_path(handle_t *handle,
2525 struct ocfs2_extent_tree *et,
2526 struct ocfs2_cached_dealloc_ctxt *dealloc,
2527 struct ocfs2_path *path, int unlink_start)
2530 struct ocfs2_extent_block *eb;
2531 struct ocfs2_extent_list *el;
2532 struct buffer_head *bh;
2534 for(i = unlink_start; i < path_num_items(path); i++) {
2535 bh = path->p_node[i].bh;
2537 eb = (struct ocfs2_extent_block *)bh->b_data;
2539 * Not all nodes might have had their final count
2540 * decremented by the caller - handle this here.
2543 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2545 "Inode %llu, attempted to remove extent block "
2546 "%llu with %u records\n",
2547 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2548 (unsigned long long)le64_to_cpu(eb->h_blkno),
2549 le16_to_cpu(el->l_next_free_rec));
2551 ocfs2_journal_dirty(handle, bh);
2552 ocfs2_remove_from_cache(et->et_ci, bh);
2556 el->l_next_free_rec = 0;
2557 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2559 ocfs2_journal_dirty(handle, bh);
2561 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2565 ocfs2_remove_from_cache(et->et_ci, bh);
2569 static void ocfs2_unlink_subtree(handle_t *handle,
2570 struct ocfs2_extent_tree *et,
2571 struct ocfs2_path *left_path,
2572 struct ocfs2_path *right_path,
2574 struct ocfs2_cached_dealloc_ctxt *dealloc)
2577 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2578 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2579 struct ocfs2_extent_list *el;
2580 struct ocfs2_extent_block *eb;
2582 el = path_leaf_el(left_path);
2584 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2586 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2587 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2590 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2592 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2593 le16_add_cpu(&root_el->l_next_free_rec, -1);
2595 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2596 eb->h_next_leaf_blk = 0;
2598 ocfs2_journal_dirty(handle, root_bh);
2599 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2601 ocfs2_unlink_path(handle, et, dealloc, right_path,
2605 static int ocfs2_rotate_subtree_left(handle_t *handle,
2606 struct ocfs2_extent_tree *et,
2607 struct ocfs2_path *left_path,
2608 struct ocfs2_path *right_path,
2610 struct ocfs2_cached_dealloc_ctxt *dealloc,
2613 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2614 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2615 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2616 struct ocfs2_extent_block *eb;
2620 right_leaf_el = path_leaf_el(right_path);
2621 left_leaf_el = path_leaf_el(left_path);
2622 root_bh = left_path->p_node[subtree_index].bh;
2623 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2625 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2628 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2629 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2631 * It's legal for us to proceed if the right leaf is
2632 * the rightmost one and it has an empty extent. There
2633 * are two cases to handle - whether the leaf will be
2634 * empty after removal or not. If the leaf isn't empty
2635 * then just remove the empty extent up front. The
2636 * next block will handle empty leaves by flagging
2639 * Non rightmost leaves will throw -EAGAIN and the
2640 * caller can manually move the subtree and retry.
2643 if (eb->h_next_leaf_blk != 0ULL)
2646 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2647 ret = ocfs2_journal_access_eb(handle, et->et_ci,
2648 path_leaf_bh(right_path),
2649 OCFS2_JOURNAL_ACCESS_WRITE);
2655 ocfs2_remove_empty_extent(right_leaf_el);
2657 right_has_empty = 1;
2660 if (eb->h_next_leaf_blk == 0ULL &&
2661 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2663 * We have to update i_last_eb_blk during the meta
2666 ret = ocfs2_et_root_journal_access(handle, et,
2667 OCFS2_JOURNAL_ACCESS_WRITE);
2673 del_right_subtree = 1;
2677 * Getting here with an empty extent in the right path implies
2678 * that it's the rightmost path and will be deleted.
2680 BUG_ON(right_has_empty && !del_right_subtree);
2682 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2689 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2690 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2697 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2705 if (!right_has_empty) {
2707 * Only do this if we're moving a real
2708 * record. Otherwise, the action is delayed until
2709 * after removal of the right path in which case we
2710 * can do a simple shift to remove the empty extent.
2712 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2713 memset(&right_leaf_el->l_recs[0], 0,
2714 sizeof(struct ocfs2_extent_rec));
2716 if (eb->h_next_leaf_blk == 0ULL) {
2718 * Move recs over to get rid of empty extent, decrease
2719 * next_free. This is allowed to remove the last
2720 * extent in our leaf (setting l_next_free_rec to
2721 * zero) - the delete code below won't care.
2723 ocfs2_remove_empty_extent(right_leaf_el);
2726 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2729 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2733 if (del_right_subtree) {
2734 ocfs2_unlink_subtree(handle, et, left_path, right_path,
2735 subtree_index, dealloc);
2736 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
2743 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2744 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2747 * Removal of the extent in the left leaf was skipped
2748 * above so we could delete the right path
2751 if (right_has_empty)
2752 ocfs2_remove_empty_extent(left_leaf_el);
2754 ret = ocfs2_journal_dirty(handle, et_root_bh);
2760 ocfs2_complete_edge_insert(handle, left_path, right_path,
2768 * Given a full path, determine what cpos value would return us a path
2769 * containing the leaf immediately to the right of the current one.
2771 * Will return zero if the path passed in is already the rightmost path.
2773 * This looks similar, but is subtly different to
2774 * ocfs2_find_cpos_for_left_leaf().
2776 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2777 struct ocfs2_path *path, u32 *cpos)
2781 struct ocfs2_extent_list *el;
2785 if (path->p_tree_depth == 0)
2788 blkno = path_leaf_bh(path)->b_blocknr;
2790 /* Start at the tree node just above the leaf and work our way up. */
2791 i = path->p_tree_depth - 1;
2795 el = path->p_node[i].el;
2798 * Find the extent record just after the one in our
2801 next_free = le16_to_cpu(el->l_next_free_rec);
2802 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2803 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2804 if (j == (next_free - 1)) {
2807 * We've determined that the
2808 * path specified is already
2809 * the rightmost one - return a
2815 * The rightmost record points to our
2816 * leaf - we need to travel up the
2822 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2828 * If we got here, we never found a valid node where
2829 * the tree indicated one should be.
2832 "Invalid extent tree at extent block %llu\n",
2833 (unsigned long long)blkno);
2838 blkno = path->p_node[i].bh->b_blocknr;
2846 static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2847 struct ocfs2_extent_tree *et,
2848 struct ocfs2_path *path)
2851 struct buffer_head *bh = path_leaf_bh(path);
2852 struct ocfs2_extent_list *el = path_leaf_el(path);
2854 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2857 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
2858 path_num_items(path) - 1);
2864 ocfs2_remove_empty_extent(el);
2866 ret = ocfs2_journal_dirty(handle, bh);
2874 static int __ocfs2_rotate_tree_left(handle_t *handle,
2875 struct ocfs2_extent_tree *et,
2877 struct ocfs2_path *path,
2878 struct ocfs2_cached_dealloc_ctxt *dealloc,
2879 struct ocfs2_path **empty_extent_path)
2881 int ret, subtree_root, deleted;
2883 struct ocfs2_path *left_path = NULL;
2884 struct ocfs2_path *right_path = NULL;
2885 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2887 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2889 *empty_extent_path = NULL;
2891 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
2897 left_path = ocfs2_new_path_from_path(path);
2904 ocfs2_cp_path(left_path, path);
2906 right_path = ocfs2_new_path_from_path(path);
2913 while (right_cpos) {
2914 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
2920 subtree_root = ocfs2_find_subtree_root(et, left_path,
2923 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2925 (unsigned long long)
2926 right_path->p_node[subtree_root].bh->b_blocknr,
2927 right_path->p_tree_depth);
2929 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2930 orig_credits, left_path);
2937 * Caller might still want to make changes to the
2938 * tree root, so re-add it to the journal here.
2940 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2947 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
2948 right_path, subtree_root,
2950 if (ret == -EAGAIN) {
2952 * The rotation has to temporarily stop due to
2953 * the right subtree having an empty
2954 * extent. Pass it back to the caller for a
2957 *empty_extent_path = right_path;
2967 * The subtree rotate might have removed records on
2968 * the rightmost edge. If so, then rotation is
2974 ocfs2_mv_path(left_path, right_path);
2976 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
2985 ocfs2_free_path(right_path);
2986 ocfs2_free_path(left_path);
2991 static int ocfs2_remove_rightmost_path(handle_t *handle,
2992 struct ocfs2_extent_tree *et,
2993 struct ocfs2_path *path,
2994 struct ocfs2_cached_dealloc_ctxt *dealloc)
2996 int ret, subtree_index;
2998 struct ocfs2_path *left_path = NULL;
2999 struct ocfs2_extent_block *eb;
3000 struct ocfs2_extent_list *el;
3003 ret = ocfs2_et_sanity_check(et);
3007 * There's two ways we handle this depending on
3008 * whether path is the only existing one.
3010 ret = ocfs2_extend_rotate_transaction(handle, 0,
3011 handle->h_buffer_credits,
3018 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
3024 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3033 * We have a path to the left of this one - it needs
3036 left_path = ocfs2_new_path_from_path(path);
3043 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
3049 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
3055 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
3057 ocfs2_unlink_subtree(handle, et, left_path, path,
3058 subtree_index, dealloc);
3059 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
3066 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
3067 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
3070 * 'path' is also the leftmost path which
3071 * means it must be the only one. This gets
3072 * handled differently because we want to
3073 * revert the root back to having extents
3076 ocfs2_unlink_path(handle, et, dealloc, path, 1);
3078 el = et->et_root_el;
3079 el->l_tree_depth = 0;
3080 el->l_next_free_rec = 0;
3081 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3083 ocfs2_et_set_last_eb_blk(et, 0);
3086 ocfs2_journal_dirty(handle, path_root_bh(path));
3089 ocfs2_free_path(left_path);
3094 * Left rotation of btree records.
3096 * In many ways, this is (unsurprisingly) the opposite of right
3097 * rotation. We start at some non-rightmost path containing an empty
3098 * extent in the leaf block. The code works its way to the rightmost
3099 * path by rotating records to the left in every subtree.
3101 * This is used by any code which reduces the number of extent records
3102 * in a leaf. After removal, an empty record should be placed in the
3103 * leftmost list position.
3105 * This won't handle a length update of the rightmost path records if
3106 * the rightmost tree leaf record is removed so the caller is
3107 * responsible for detecting and correcting that.
3109 static int ocfs2_rotate_tree_left(handle_t *handle,
3110 struct ocfs2_extent_tree *et,
3111 struct ocfs2_path *path,
3112 struct ocfs2_cached_dealloc_ctxt *dealloc)
3114 int ret, orig_credits = handle->h_buffer_credits;
3115 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3116 struct ocfs2_extent_block *eb;
3117 struct ocfs2_extent_list *el;
3119 el = path_leaf_el(path);
3120 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3123 if (path->p_tree_depth == 0) {
3124 rightmost_no_delete:
3126 * Inline extents. This is trivially handled, so do
3129 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
3136 * Handle rightmost branch now. There's several cases:
3137 * 1) simple rotation leaving records in there. That's trivial.
3138 * 2) rotation requiring a branch delete - there's no more
3139 * records left. Two cases of this:
3140 * a) There are branches to the left.
3141 * b) This is also the leftmost (the only) branch.
3143 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3144 * 2a) we need the left branch so that we can update it with the unlink
3145 * 2b) we need to bring the root back to inline extents.
3148 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3150 if (eb->h_next_leaf_blk == 0) {
3152 * This gets a bit tricky if we're going to delete the
3153 * rightmost path. Get the other cases out of the way
3156 if (le16_to_cpu(el->l_next_free_rec) > 1)
3157 goto rightmost_no_delete;
3159 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3161 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3162 "Owner %llu has empty extent block at %llu",
3163 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
3164 (unsigned long long)le64_to_cpu(eb->h_blkno));
3169 * XXX: The caller can not trust "path" any more after
3170 * this as it will have been deleted. What do we do?
3172 * In theory the rotate-for-merge code will never get
3173 * here because it'll always ask for a rotate in a
3177 ret = ocfs2_remove_rightmost_path(handle, et, path,
3185 * Now we can loop, remembering the path we get from -EAGAIN
3186 * and restarting from there.
3189 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3190 dealloc, &restart_path);
3191 if (ret && ret != -EAGAIN) {
3196 while (ret == -EAGAIN) {
3197 tmp_path = restart_path;
3198 restart_path = NULL;
3200 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
3203 if (ret && ret != -EAGAIN) {
3208 ocfs2_free_path(tmp_path);
3216 ocfs2_free_path(tmp_path);
3217 ocfs2_free_path(restart_path);
3221 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3224 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3227 if (rec->e_leaf_clusters == 0) {
3229 * We consumed all of the merged-from record. An empty
3230 * extent cannot exist anywhere but the 1st array
3231 * position, so move things over if the merged-from
3232 * record doesn't occupy that position.
3234 * This creates a new empty extent so the caller
3235 * should be smart enough to have removed any existing
3239 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3240 size = index * sizeof(struct ocfs2_extent_rec);
3241 memmove(&el->l_recs[1], &el->l_recs[0], size);
3245 * Always memset - the caller doesn't check whether it
3246 * created an empty extent, so there could be junk in
3249 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3253 static int ocfs2_get_right_path(struct inode *inode,
3254 struct ocfs2_path *left_path,
3255 struct ocfs2_path **ret_right_path)
3259 struct ocfs2_path *right_path = NULL;
3260 struct ocfs2_extent_list *left_el;
3262 *ret_right_path = NULL;
3264 /* This function shouldn't be called for non-trees. */
3265 BUG_ON(left_path->p_tree_depth == 0);
3267 left_el = path_leaf_el(left_path);
3268 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3270 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3277 /* This function shouldn't be called for the rightmost leaf. */
3278 BUG_ON(right_cpos == 0);
3280 right_path = ocfs2_new_path_from_path(left_path);
3287 ret = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos);
3293 *ret_right_path = right_path;
3296 ocfs2_free_path(right_path);
3301 * Remove split_rec clusters from the record at index and merge them
3302 * onto the beginning of the record "next" to it.
3303 * For index < l_count - 1, the next means the extent rec at index + 1.
3304 * For index == l_count - 1, the "next" means the 1st extent rec of the
3305 * next extent block.
3307 static int ocfs2_merge_rec_right(struct inode *inode,
3308 struct ocfs2_path *left_path,
3310 struct ocfs2_extent_tree *et,
3311 struct ocfs2_extent_rec *split_rec,
3314 int ret, next_free, i;
3315 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3316 struct ocfs2_extent_rec *left_rec;
3317 struct ocfs2_extent_rec *right_rec;
3318 struct ocfs2_extent_list *right_el;
3319 struct ocfs2_path *right_path = NULL;
3320 int subtree_index = 0;
3321 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3322 struct buffer_head *bh = path_leaf_bh(left_path);
3323 struct buffer_head *root_bh = NULL;
3325 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3326 left_rec = &el->l_recs[index];
3328 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3329 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3330 /* we meet with a cross extent block merge. */
3331 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3337 right_el = path_leaf_el(right_path);
3338 next_free = le16_to_cpu(right_el->l_next_free_rec);
3339 BUG_ON(next_free <= 0);
3340 right_rec = &right_el->l_recs[0];
3341 if (ocfs2_is_empty_extent(right_rec)) {
3342 BUG_ON(next_free <= 1);
3343 right_rec = &right_el->l_recs[1];
3346 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3347 le16_to_cpu(left_rec->e_leaf_clusters) !=
3348 le32_to_cpu(right_rec->e_cpos));
3350 subtree_index = ocfs2_find_subtree_root(et, left_path,
3353 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3354 handle->h_buffer_credits,
3361 root_bh = left_path->p_node[subtree_index].bh;
3362 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3364 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3371 for (i = subtree_index + 1;
3372 i < path_num_items(right_path); i++) {
3373 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3380 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3389 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3390 right_rec = &el->l_recs[index + 1];
3393 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
3394 path_num_items(left_path) - 1);
3400 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3402 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3403 le64_add_cpu(&right_rec->e_blkno,
3404 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3406 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3408 ocfs2_cleanup_merge(el, index);
3410 ret = ocfs2_journal_dirty(handle, bh);
3415 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3419 ocfs2_complete_edge_insert(handle, left_path, right_path,
3424 ocfs2_free_path(right_path);
3428 static int ocfs2_get_left_path(struct inode *inode,
3429 struct ocfs2_path *right_path,
3430 struct ocfs2_path **ret_left_path)
3434 struct ocfs2_path *left_path = NULL;
3436 *ret_left_path = NULL;
3438 /* This function shouldn't be called for non-trees. */
3439 BUG_ON(right_path->p_tree_depth == 0);
3441 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3442 right_path, &left_cpos);
3448 /* This function shouldn't be called for the leftmost leaf. */
3449 BUG_ON(left_cpos == 0);
3451 left_path = ocfs2_new_path_from_path(right_path);
3458 ret = ocfs2_find_path(INODE_CACHE(inode), left_path, left_cpos);
3464 *ret_left_path = left_path;
3467 ocfs2_free_path(left_path);
3472 * Remove split_rec clusters from the record at index and merge them
3473 * onto the tail of the record "before" it.
3474 * For index > 0, the "before" means the extent rec at index - 1.
3476 * For index == 0, the "before" means the last record of the previous
3477 * extent block. And there is also a situation that we may need to
3478 * remove the rightmost leaf extent block in the right_path and change
3479 * the right path to indicate the new rightmost path.
3481 static int ocfs2_merge_rec_left(struct inode *inode,
3482 struct ocfs2_path *right_path,
3484 struct ocfs2_extent_rec *split_rec,
3485 struct ocfs2_cached_dealloc_ctxt *dealloc,
3486 struct ocfs2_extent_tree *et,
3489 int ret, i, subtree_index = 0, has_empty_extent = 0;
3490 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3491 struct ocfs2_extent_rec *left_rec;
3492 struct ocfs2_extent_rec *right_rec;
3493 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3494 struct buffer_head *bh = path_leaf_bh(right_path);
3495 struct buffer_head *root_bh = NULL;
3496 struct ocfs2_path *left_path = NULL;
3497 struct ocfs2_extent_list *left_el;
3501 right_rec = &el->l_recs[index];
3503 /* we meet with a cross extent block merge. */
3504 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3510 left_el = path_leaf_el(left_path);
3511 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3512 le16_to_cpu(left_el->l_count));
3514 left_rec = &left_el->l_recs[
3515 le16_to_cpu(left_el->l_next_free_rec) - 1];
3516 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3517 le16_to_cpu(left_rec->e_leaf_clusters) !=
3518 le32_to_cpu(split_rec->e_cpos));
3520 subtree_index = ocfs2_find_subtree_root(et, left_path,
3523 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3524 handle->h_buffer_credits,
3531 root_bh = left_path->p_node[subtree_index].bh;
3532 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3534 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
3541 for (i = subtree_index + 1;
3542 i < path_num_items(right_path); i++) {
3543 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
3550 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
3558 left_rec = &el->l_recs[index - 1];
3559 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3560 has_empty_extent = 1;
3563 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
3564 path_num_items(right_path) - 1);
3570 if (has_empty_extent && index == 1) {
3572 * The easy case - we can just plop the record right in.
3574 *left_rec = *split_rec;
3576 has_empty_extent = 0;
3578 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3580 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3581 le64_add_cpu(&right_rec->e_blkno,
3582 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3583 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3585 ocfs2_cleanup_merge(el, index);
3587 ret = ocfs2_journal_dirty(handle, bh);
3592 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3597 * In the situation that the right_rec is empty and the extent
3598 * block is empty also, ocfs2_complete_edge_insert can't handle
3599 * it and we need to delete the right extent block.
3601 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3602 le16_to_cpu(el->l_next_free_rec) == 1) {
3604 ret = ocfs2_remove_rightmost_path(handle, et,
3612 /* Now the rightmost extent block has been deleted.
3613 * So we use the new rightmost path.
3615 ocfs2_mv_path(right_path, left_path);
3618 ocfs2_complete_edge_insert(handle, left_path,
3619 right_path, subtree_index);
3623 ocfs2_free_path(left_path);
3627 static int ocfs2_try_to_merge_extent(struct inode *inode,
3629 struct ocfs2_path *path,
3631 struct ocfs2_extent_rec *split_rec,
3632 struct ocfs2_cached_dealloc_ctxt *dealloc,
3633 struct ocfs2_merge_ctxt *ctxt,
3634 struct ocfs2_extent_tree *et)
3638 struct ocfs2_extent_list *el = path_leaf_el(path);
3639 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3641 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3643 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3645 * The merge code will need to create an empty
3646 * extent to take the place of the newly
3647 * emptied slot. Remove any pre-existing empty
3648 * extents - having more than one in a leaf is
3651 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3657 rec = &el->l_recs[split_index];
3660 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3662 * Left-right contig implies this.
3664 BUG_ON(!ctxt->c_split_covers_rec);
3667 * Since the leftright insert always covers the entire
3668 * extent, this call will delete the insert record
3669 * entirely, resulting in an empty extent record added to
3672 * Since the adding of an empty extent shifts
3673 * everything back to the right, there's no need to
3674 * update split_index here.
3676 * When the split_index is zero, we need to merge it to the
3677 * prevoius extent block. It is more efficient and easier
3678 * if we do merge_right first and merge_left later.
3680 ret = ocfs2_merge_rec_right(inode, path,
3681 handle, et, split_rec,
3689 * We can only get this from logic error above.
3691 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3693 /* The merge left us with an empty extent, remove it. */
3694 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3700 rec = &el->l_recs[split_index];
3703 * Note that we don't pass split_rec here on purpose -
3704 * we've merged it into the rec already.
3706 ret = ocfs2_merge_rec_left(inode, path,
3716 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3718 * Error from this last rotate is not critical, so
3719 * print but don't bubble it up.
3726 * Merge a record to the left or right.
3728 * 'contig_type' is relative to the existing record,
3729 * so for example, if we're "right contig", it's to
3730 * the record on the left (hence the left merge).
3732 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3733 ret = ocfs2_merge_rec_left(inode,
3743 ret = ocfs2_merge_rec_right(inode, path, handle,
3752 if (ctxt->c_split_covers_rec) {
3754 * The merge may have left an empty extent in
3755 * our leaf. Try to rotate it away.
3757 ret = ocfs2_rotate_tree_left(handle, et, path,
3769 static void ocfs2_subtract_from_rec(struct super_block *sb,
3770 enum ocfs2_split_type split,
3771 struct ocfs2_extent_rec *rec,
3772 struct ocfs2_extent_rec *split_rec)
3776 len_blocks = ocfs2_clusters_to_blocks(sb,
3777 le16_to_cpu(split_rec->e_leaf_clusters));
3779 if (split == SPLIT_LEFT) {
3781 * Region is on the left edge of the existing
3784 le32_add_cpu(&rec->e_cpos,
3785 le16_to_cpu(split_rec->e_leaf_clusters));
3786 le64_add_cpu(&rec->e_blkno, len_blocks);
3787 le16_add_cpu(&rec->e_leaf_clusters,
3788 -le16_to_cpu(split_rec->e_leaf_clusters));
3791 * Region is on the right edge of the existing
3794 le16_add_cpu(&rec->e_leaf_clusters,
3795 -le16_to_cpu(split_rec->e_leaf_clusters));
3800 * Do the final bits of extent record insertion at the target leaf
3801 * list. If this leaf is part of an allocation tree, it is assumed
3802 * that the tree above has been prepared.
3804 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3805 struct ocfs2_extent_list *el,
3806 struct ocfs2_insert_type *insert,
3807 struct inode *inode)
3809 int i = insert->ins_contig_index;
3811 struct ocfs2_extent_rec *rec;
3813 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3815 if (insert->ins_split != SPLIT_NONE) {
3816 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3818 rec = &el->l_recs[i];
3819 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3825 * Contiguous insert - either left or right.
3827 if (insert->ins_contig != CONTIG_NONE) {
3828 rec = &el->l_recs[i];
3829 if (insert->ins_contig == CONTIG_LEFT) {
3830 rec->e_blkno = insert_rec->e_blkno;
3831 rec->e_cpos = insert_rec->e_cpos;
3833 le16_add_cpu(&rec->e_leaf_clusters,
3834 le16_to_cpu(insert_rec->e_leaf_clusters));
3839 * Handle insert into an empty leaf.
3841 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3842 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3843 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3844 el->l_recs[0] = *insert_rec;
3845 el->l_next_free_rec = cpu_to_le16(1);
3852 if (insert->ins_appending == APPEND_TAIL) {
3853 i = le16_to_cpu(el->l_next_free_rec) - 1;
3854 rec = &el->l_recs[i];
3855 range = le32_to_cpu(rec->e_cpos)
3856 + le16_to_cpu(rec->e_leaf_clusters);
3857 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3859 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3860 le16_to_cpu(el->l_count),
3861 "inode %lu, depth %u, count %u, next free %u, "
3862 "rec.cpos %u, rec.clusters %u, "
3863 "insert.cpos %u, insert.clusters %u\n",
3865 le16_to_cpu(el->l_tree_depth),
3866 le16_to_cpu(el->l_count),
3867 le16_to_cpu(el->l_next_free_rec),
3868 le32_to_cpu(el->l_recs[i].e_cpos),
3869 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3870 le32_to_cpu(insert_rec->e_cpos),
3871 le16_to_cpu(insert_rec->e_leaf_clusters));
3873 el->l_recs[i] = *insert_rec;
3874 le16_add_cpu(&el->l_next_free_rec, 1);
3880 * Ok, we have to rotate.
3882 * At this point, it is safe to assume that inserting into an
3883 * empty leaf and appending to a leaf have both been handled
3886 * This leaf needs to have space, either by the empty 1st
3887 * extent record, or by virtue of an l_next_rec < l_count.
3889 ocfs2_rotate_leaf(el, insert_rec);
3892 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3894 struct ocfs2_path *path,
3895 struct ocfs2_extent_rec *insert_rec)
3897 int ret, i, next_free;
3898 struct buffer_head *bh;
3899 struct ocfs2_extent_list *el;
3900 struct ocfs2_extent_rec *rec;
3903 * Update everything except the leaf block.
3905 for (i = 0; i < path->p_tree_depth; i++) {
3906 bh = path->p_node[i].bh;
3907 el = path->p_node[i].el;
3909 next_free = le16_to_cpu(el->l_next_free_rec);
3910 if (next_free == 0) {
3911 ocfs2_error(inode->i_sb,
3912 "Dinode %llu has a bad extent list",
3913 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3918 rec = &el->l_recs[next_free - 1];
3920 rec->e_int_clusters = insert_rec->e_cpos;
3921 le32_add_cpu(&rec->e_int_clusters,
3922 le16_to_cpu(insert_rec->e_leaf_clusters));
3923 le32_add_cpu(&rec->e_int_clusters,
3924 -le32_to_cpu(rec->e_cpos));
3926 ret = ocfs2_journal_dirty(handle, bh);
3933 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3934 struct ocfs2_extent_rec *insert_rec,
3935 struct ocfs2_path *right_path,
3936 struct ocfs2_path **ret_left_path)
3939 struct ocfs2_extent_list *el;
3940 struct ocfs2_path *left_path = NULL;
3942 *ret_left_path = NULL;
3945 * This shouldn't happen for non-trees. The extent rec cluster
3946 * count manipulation below only works for interior nodes.
3948 BUG_ON(right_path->p_tree_depth == 0);
3951 * If our appending insert is at the leftmost edge of a leaf,
3952 * then we might need to update the rightmost records of the
3955 el = path_leaf_el(right_path);
3956 next_free = le16_to_cpu(el->l_next_free_rec);
3957 if (next_free == 0 ||
3958 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3961 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3968 mlog(0, "Append may need a left path update. cpos: %u, "
3969 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3973 * No need to worry if the append is already in the
3977 left_path = ocfs2_new_path_from_path(right_path);
3984 ret = ocfs2_find_path(INODE_CACHE(inode), left_path,
3992 * ocfs2_insert_path() will pass the left_path to the
3998 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, right_path);
4004 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
4006 *ret_left_path = left_path;
4010 ocfs2_free_path(left_path);
4015 static void ocfs2_split_record(struct inode *inode,
4016 struct ocfs2_path *left_path,
4017 struct ocfs2_path *right_path,
4018 struct ocfs2_extent_rec *split_rec,
4019 enum ocfs2_split_type split)
4022 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4023 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4024 struct ocfs2_extent_rec *rec, *tmprec;
4026 right_el = path_leaf_el(right_path);
4028 left_el = path_leaf_el(left_path);
4031 insert_el = right_el;
4032 index = ocfs2_search_extent_list(el, cpos);
4034 if (index == 0 && left_path) {
4035 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4038 * This typically means that the record
4039 * started in the left path but moved to the
4040 * right as a result of rotation. We either
4041 * move the existing record to the left, or we
4042 * do the later insert there.
4044 * In this case, the left path should always
4045 * exist as the rotate code will have passed
4046 * it back for a post-insert update.
4049 if (split == SPLIT_LEFT) {
4051 * It's a left split. Since we know
4052 * that the rotate code gave us an
4053 * empty extent in the left path, we
4054 * can just do the insert there.
4056 insert_el = left_el;
4059 * Right split - we have to move the
4060 * existing record over to the left
4061 * leaf. The insert will be into the
4062 * newly created empty extent in the
4065 tmprec = &right_el->l_recs[index];
4066 ocfs2_rotate_leaf(left_el, tmprec);
4069 memset(tmprec, 0, sizeof(*tmprec));
4070 index = ocfs2_search_extent_list(left_el, cpos);
4071 BUG_ON(index == -1);
4076 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4078 * Left path is easy - we can just allow the insert to
4082 insert_el = left_el;
4083 index = ocfs2_search_extent_list(el, cpos);
4084 BUG_ON(index == -1);
4087 rec = &el->l_recs[index];
4088 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
4089 ocfs2_rotate_leaf(insert_el, split_rec);
4093 * This function only does inserts on an allocation b-tree. For tree
4094 * depth = 0, ocfs2_insert_at_leaf() is called directly.
4096 * right_path is the path we want to do the actual insert
4097 * in. left_path should only be passed in if we need to update that
4098 * portion of the tree after an edge insert.
4100 static int ocfs2_insert_path(struct inode *inode,
4102 struct ocfs2_extent_tree *et,
4103 struct ocfs2_path *left_path,
4104 struct ocfs2_path *right_path,
4105 struct ocfs2_extent_rec *insert_rec,
4106 struct ocfs2_insert_type *insert)
4108 int ret, subtree_index;
4109 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
4112 int credits = handle->h_buffer_credits;
4115 * There's a chance that left_path got passed back to
4116 * us without being accounted for in the
4117 * journal. Extend our transaction here to be sure we
4118 * can change those blocks.
4120 credits += left_path->p_tree_depth;
4122 ret = ocfs2_extend_trans(handle, credits);
4128 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
4136 * Pass both paths to the journal. The majority of inserts
4137 * will be touching all components anyway.
4139 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4145 if (insert->ins_split != SPLIT_NONE) {
4147 * We could call ocfs2_insert_at_leaf() for some types
4148 * of splits, but it's easier to just let one separate
4149 * function sort it all out.
4151 ocfs2_split_record(inode, left_path, right_path,
4152 insert_rec, insert->ins_split);
4155 * Split might have modified either leaf and we don't
4156 * have a guarantee that the later edge insert will
4157 * dirty this for us.
4160 ret = ocfs2_journal_dirty(handle,
4161 path_leaf_bh(left_path));
4165 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
4168 ret = ocfs2_journal_dirty(handle, leaf_bh);
4174 * The rotate code has indicated that we need to fix
4175 * up portions of the tree after the insert.
4177 * XXX: Should we extend the transaction here?
4179 subtree_index = ocfs2_find_subtree_root(et, left_path,
4181 ocfs2_complete_edge_insert(handle, left_path, right_path,
4190 static int ocfs2_do_insert_extent(struct inode *inode,
4192 struct ocfs2_extent_tree *et,
4193 struct ocfs2_extent_rec *insert_rec,
4194 struct ocfs2_insert_type *type)
4196 int ret, rotate = 0;
4198 struct ocfs2_path *right_path = NULL;
4199 struct ocfs2_path *left_path = NULL;
4200 struct ocfs2_extent_list *el;
4202 el = et->et_root_el;
4204 ret = ocfs2_et_root_journal_access(handle, et,
4205 OCFS2_JOURNAL_ACCESS_WRITE);
4211 if (le16_to_cpu(el->l_tree_depth) == 0) {
4212 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4213 goto out_update_clusters;
4216 right_path = ocfs2_new_path_from_et(et);
4224 * Determine the path to start with. Rotations need the
4225 * rightmost path, everything else can go directly to the
4228 cpos = le32_to_cpu(insert_rec->e_cpos);
4229 if (type->ins_appending == APPEND_NONE &&
4230 type->ins_contig == CONTIG_NONE) {
4235 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
4242 * Rotations and appends need special treatment - they modify
4243 * parts of the tree's above them.
4245 * Both might pass back a path immediate to the left of the
4246 * one being inserted to. This will be cause
4247 * ocfs2_insert_path() to modify the rightmost records of
4248 * left_path to account for an edge insert.
4250 * XXX: When modifying this code, keep in mind that an insert
4251 * can wind up skipping both of these two special cases...
4254 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
4255 le32_to_cpu(insert_rec->e_cpos),
4256 right_path, &left_path);
4263 * ocfs2_rotate_tree_right() might have extended the
4264 * transaction without re-journaling our tree root.
4266 ret = ocfs2_et_root_journal_access(handle, et,
4267 OCFS2_JOURNAL_ACCESS_WRITE);
4272 } else if (type->ins_appending == APPEND_TAIL
4273 && type->ins_contig != CONTIG_LEFT) {
4274 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4275 right_path, &left_path);
4282 ret = ocfs2_insert_path(inode, handle, et, left_path, right_path,
4289 out_update_clusters:
4290 if (type->ins_split == SPLIT_NONE)
4291 ocfs2_et_update_clusters(et,
4292 le16_to_cpu(insert_rec->e_leaf_clusters));
4294 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
4299 ocfs2_free_path(left_path);
4300 ocfs2_free_path(right_path);
4305 static enum ocfs2_contig_type
4306 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
4307 struct ocfs2_extent_list *el, int index,
4308 struct ocfs2_extent_rec *split_rec)
4311 enum ocfs2_contig_type ret = CONTIG_NONE;
4312 u32 left_cpos, right_cpos;
4313 struct ocfs2_extent_rec *rec = NULL;
4314 struct ocfs2_extent_list *new_el;
4315 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4316 struct buffer_head *bh;
4317 struct ocfs2_extent_block *eb;
4320 rec = &el->l_recs[index - 1];
4321 } else if (path->p_tree_depth > 0) {
4322 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4327 if (left_cpos != 0) {
4328 left_path = ocfs2_new_path_from_path(path);
4332 status = ocfs2_find_path(INODE_CACHE(inode),
4333 left_path, left_cpos);
4337 new_el = path_leaf_el(left_path);
4339 if (le16_to_cpu(new_el->l_next_free_rec) !=
4340 le16_to_cpu(new_el->l_count)) {
4341 bh = path_leaf_bh(left_path);
4342 eb = (struct ocfs2_extent_block *)bh->b_data;
4343 ocfs2_error(inode->i_sb,
4344 "Extent block #%llu has an "
4345 "invalid l_next_free_rec of "
4346 "%d. It should have "
4347 "matched the l_count of %d",
4348 (unsigned long long)le64_to_cpu(eb->h_blkno),
4349 le16_to_cpu(new_el->l_next_free_rec),
4350 le16_to_cpu(new_el->l_count));
4354 rec = &new_el->l_recs[
4355 le16_to_cpu(new_el->l_next_free_rec) - 1];
4360 * We're careful to check for an empty extent record here -
4361 * the merge code will know what to do if it sees one.
4364 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4365 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4368 ret = ocfs2_extent_contig(inode, rec, split_rec);
4373 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4374 rec = &el->l_recs[index + 1];
4375 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4376 path->p_tree_depth > 0) {
4377 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4382 if (right_cpos == 0)
4385 right_path = ocfs2_new_path_from_path(path);
4389 status = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos);
4393 new_el = path_leaf_el(right_path);
4394 rec = &new_el->l_recs[0];
4395 if (ocfs2_is_empty_extent(rec)) {
4396 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4397 bh = path_leaf_bh(right_path);
4398 eb = (struct ocfs2_extent_block *)bh->b_data;
4399 ocfs2_error(inode->i_sb,
4400 "Extent block #%llu has an "
4401 "invalid l_next_free_rec of %d",
4402 (unsigned long long)le64_to_cpu(eb->h_blkno),
4403 le16_to_cpu(new_el->l_next_free_rec));
4407 rec = &new_el->l_recs[1];
4412 enum ocfs2_contig_type contig_type;
4414 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4416 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4417 ret = CONTIG_LEFTRIGHT;
4418 else if (ret == CONTIG_NONE)
4424 ocfs2_free_path(left_path);
4426 ocfs2_free_path(right_path);
4431 static void ocfs2_figure_contig_type(struct inode *inode,
4432 struct ocfs2_insert_type *insert,
4433 struct ocfs2_extent_list *el,
4434 struct ocfs2_extent_rec *insert_rec,
4435 struct ocfs2_extent_tree *et)
4438 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4440 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4442 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4443 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4445 if (contig_type != CONTIG_NONE) {
4446 insert->ins_contig_index = i;
4450 insert->ins_contig = contig_type;
4452 if (insert->ins_contig != CONTIG_NONE) {
4453 struct ocfs2_extent_rec *rec =
4454 &el->l_recs[insert->ins_contig_index];
4455 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4456 le16_to_cpu(insert_rec->e_leaf_clusters);
4459 * Caller might want us to limit the size of extents, don't
4460 * calculate contiguousness if we might exceed that limit.
4462 if (et->et_max_leaf_clusters &&
4463 (len > et->et_max_leaf_clusters))
4464 insert->ins_contig = CONTIG_NONE;
4469 * This should only be called against the righmost leaf extent list.
4471 * ocfs2_figure_appending_type() will figure out whether we'll have to
4472 * insert at the tail of the rightmost leaf.
4474 * This should also work against the root extent list for tree's with 0
4475 * depth. If we consider the root extent list to be the rightmost leaf node
4476 * then the logic here makes sense.
4478 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4479 struct ocfs2_extent_list *el,
4480 struct ocfs2_extent_rec *insert_rec)
4483 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4484 struct ocfs2_extent_rec *rec;
4486 insert->ins_appending = APPEND_NONE;
4488 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4490 if (!el->l_next_free_rec)
4491 goto set_tail_append;
4493 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4494 /* Were all records empty? */
4495 if (le16_to_cpu(el->l_next_free_rec) == 1)
4496 goto set_tail_append;
4499 i = le16_to_cpu(el->l_next_free_rec) - 1;
4500 rec = &el->l_recs[i];
4503 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4504 goto set_tail_append;
4509 insert->ins_appending = APPEND_TAIL;
4513 * Helper function called at the begining of an insert.
4515 * This computes a few things that are commonly used in the process of
4516 * inserting into the btree:
4517 * - Whether the new extent is contiguous with an existing one.
4518 * - The current tree depth.
4519 * - Whether the insert is an appending one.
4520 * - The total # of free records in the tree.
4522 * All of the information is stored on the ocfs2_insert_type
4525 static int ocfs2_figure_insert_type(struct inode *inode,
4526 struct ocfs2_extent_tree *et,
4527 struct buffer_head **last_eb_bh,
4528 struct ocfs2_extent_rec *insert_rec,
4530 struct ocfs2_insert_type *insert)
4533 struct ocfs2_extent_block *eb;
4534 struct ocfs2_extent_list *el;
4535 struct ocfs2_path *path = NULL;
4536 struct buffer_head *bh = NULL;
4538 insert->ins_split = SPLIT_NONE;
4540 el = et->et_root_el;
4541 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4543 if (el->l_tree_depth) {
4545 * If we have tree depth, we read in the
4546 * rightmost extent block ahead of time as
4547 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4548 * may want it later.
4550 ret = ocfs2_read_extent_block(et->et_ci,
4551 ocfs2_et_get_last_eb_blk(et),
4557 eb = (struct ocfs2_extent_block *) bh->b_data;
4562 * Unless we have a contiguous insert, we'll need to know if
4563 * there is room left in our allocation tree for another
4566 * XXX: This test is simplistic, we can search for empty
4567 * extent records too.
4569 *free_records = le16_to_cpu(el->l_count) -
4570 le16_to_cpu(el->l_next_free_rec);
4572 if (!insert->ins_tree_depth) {
4573 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4574 ocfs2_figure_appending_type(insert, el, insert_rec);
4578 path = ocfs2_new_path_from_et(et);
4586 * In the case that we're inserting past what the tree
4587 * currently accounts for, ocfs2_find_path() will return for
4588 * us the rightmost tree path. This is accounted for below in
4589 * the appending code.
4591 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
4597 el = path_leaf_el(path);
4600 * Now that we have the path, there's two things we want to determine:
4601 * 1) Contiguousness (also set contig_index if this is so)
4603 * 2) Are we doing an append? We can trivially break this up
4604 * into two types of appends: simple record append, or a
4605 * rotate inside the tail leaf.
4607 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4610 * The insert code isn't quite ready to deal with all cases of
4611 * left contiguousness. Specifically, if it's an insert into
4612 * the 1st record in a leaf, it will require the adjustment of
4613 * cluster count on the last record of the path directly to it's
4614 * left. For now, just catch that case and fool the layers
4615 * above us. This works just fine for tree_depth == 0, which
4616 * is why we allow that above.
4618 if (insert->ins_contig == CONTIG_LEFT &&
4619 insert->ins_contig_index == 0)
4620 insert->ins_contig = CONTIG_NONE;
4623 * Ok, so we can simply compare against last_eb to figure out
4624 * whether the path doesn't exist. This will only happen in
4625 * the case that we're doing a tail append, so maybe we can
4626 * take advantage of that information somehow.
4628 if (ocfs2_et_get_last_eb_blk(et) ==
4629 path_leaf_bh(path)->b_blocknr) {
4631 * Ok, ocfs2_find_path() returned us the rightmost
4632 * tree path. This might be an appending insert. There are
4634 * 1) We're doing a true append at the tail:
4635 * -This might even be off the end of the leaf
4636 * 2) We're "appending" by rotating in the tail
4638 ocfs2_figure_appending_type(insert, el, insert_rec);
4642 ocfs2_free_path(path);
4652 * Insert an extent into an inode btree.
4654 * The caller needs to update fe->i_clusters
4656 int ocfs2_insert_extent(struct ocfs2_super *osb,
4658 struct inode *inode,
4659 struct ocfs2_extent_tree *et,
4664 struct ocfs2_alloc_context *meta_ac)
4667 int uninitialized_var(free_records);
4668 struct buffer_head *last_eb_bh = NULL;
4669 struct ocfs2_insert_type insert = {0, };
4670 struct ocfs2_extent_rec rec;
4672 mlog(0, "add %u clusters at position %u to inode %llu\n",
4673 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4675 memset(&rec, 0, sizeof(rec));
4676 rec.e_cpos = cpu_to_le32(cpos);
4677 rec.e_blkno = cpu_to_le64(start_blk);
4678 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4679 rec.e_flags = flags;
4680 status = ocfs2_et_insert_check(et, &rec);
4686 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4687 &free_records, &insert);
4693 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4694 "Insert.contig_index: %d, Insert.free_records: %d, "
4695 "Insert.tree_depth: %d\n",
4696 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4697 free_records, insert.ins_tree_depth);
4699 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4700 status = ocfs2_grow_tree(inode, handle, et,
4701 &insert.ins_tree_depth, &last_eb_bh,
4709 /* Finally, we can add clusters. This might rotate the tree for us. */
4710 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4713 else if (et->et_ops == &ocfs2_dinode_et_ops)
4714 ocfs2_extent_map_insert_rec(inode, &rec);
4724 * Allcate and add clusters into the extent b-tree.
4725 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4726 * The extent b-tree's root is specified by et, and
4727 * it is not limited to the file storage. Any extent tree can use this
4728 * function if it implements the proper ocfs2_extent_tree.
4730 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4731 struct inode *inode,
4732 u32 *logical_offset,
4733 u32 clusters_to_add,
4735 struct ocfs2_extent_tree *et,
4737 struct ocfs2_alloc_context *data_ac,
4738 struct ocfs2_alloc_context *meta_ac,
4739 enum ocfs2_alloc_restarted *reason_ret)
4743 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4744 u32 bit_off, num_bits;
4748 BUG_ON(!clusters_to_add);
4751 flags = OCFS2_EXT_UNWRITTEN;
4753 free_extents = ocfs2_num_free_extents(osb, et);
4754 if (free_extents < 0) {
4755 status = free_extents;
4760 /* there are two cases which could cause us to EAGAIN in the
4761 * we-need-more-metadata case:
4762 * 1) we haven't reserved *any*
4763 * 2) we are so fragmented, we've needed to add metadata too
4765 if (!free_extents && !meta_ac) {
4766 mlog(0, "we haven't reserved any metadata!\n");
4768 reason = RESTART_META;
4770 } else if ((!free_extents)
4771 && (ocfs2_alloc_context_bits_left(meta_ac)
4772 < ocfs2_extend_meta_needed(et->et_root_el))) {
4773 mlog(0, "filesystem is really fragmented...\n");
4775 reason = RESTART_META;
4779 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4780 clusters_to_add, &bit_off, &num_bits);
4782 if (status != -ENOSPC)
4787 BUG_ON(num_bits > clusters_to_add);
4789 /* reserve our write early -- insert_extent may update the tree root */
4790 status = ocfs2_et_root_journal_access(handle, et,
4791 OCFS2_JOURNAL_ACCESS_WRITE);
4797 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4798 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4799 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4800 status = ocfs2_insert_extent(osb, handle, inode, et,
4801 *logical_offset, block,
4802 num_bits, flags, meta_ac);
4808 status = ocfs2_journal_dirty(handle, et->et_root_bh);
4814 clusters_to_add -= num_bits;
4815 *logical_offset += num_bits;
4817 if (clusters_to_add) {
4818 mlog(0, "need to alloc once more, wanted = %u\n",
4821 reason = RESTART_TRANS;
4827 *reason_ret = reason;
4831 static void ocfs2_make_right_split_rec(struct super_block *sb,
4832 struct ocfs2_extent_rec *split_rec,
4834 struct ocfs2_extent_rec *rec)
4836 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4837 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4839 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4841 split_rec->e_cpos = cpu_to_le32(cpos);
4842 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4844 split_rec->e_blkno = rec->e_blkno;
4845 le64_add_cpu(&split_rec->e_blkno,
4846 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4848 split_rec->e_flags = rec->e_flags;
4851 static int ocfs2_split_and_insert(struct inode *inode,
4853 struct ocfs2_path *path,
4854 struct ocfs2_extent_tree *et,
4855 struct buffer_head **last_eb_bh,
4857 struct ocfs2_extent_rec *orig_split_rec,
4858 struct ocfs2_alloc_context *meta_ac)
4861 unsigned int insert_range, rec_range, do_leftright = 0;
4862 struct ocfs2_extent_rec tmprec;
4863 struct ocfs2_extent_list *rightmost_el;
4864 struct ocfs2_extent_rec rec;
4865 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4866 struct ocfs2_insert_type insert;
4867 struct ocfs2_extent_block *eb;
4871 * Store a copy of the record on the stack - it might move
4872 * around as the tree is manipulated below.
4874 rec = path_leaf_el(path)->l_recs[split_index];
4876 rightmost_el = et->et_root_el;
4878 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4880 BUG_ON(!(*last_eb_bh));
4881 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4882 rightmost_el = &eb->h_list;
4885 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4886 le16_to_cpu(rightmost_el->l_count)) {
4887 ret = ocfs2_grow_tree(inode, handle, et,
4888 &depth, last_eb_bh, meta_ac);
4895 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4896 insert.ins_appending = APPEND_NONE;
4897 insert.ins_contig = CONTIG_NONE;
4898 insert.ins_tree_depth = depth;
4900 insert_range = le32_to_cpu(split_rec.e_cpos) +
4901 le16_to_cpu(split_rec.e_leaf_clusters);
4902 rec_range = le32_to_cpu(rec.e_cpos) +
4903 le16_to_cpu(rec.e_leaf_clusters);
4905 if (split_rec.e_cpos == rec.e_cpos) {
4906 insert.ins_split = SPLIT_LEFT;
4907 } else if (insert_range == rec_range) {
4908 insert.ins_split = SPLIT_RIGHT;
4911 * Left/right split. We fake this as a right split
4912 * first and then make a second pass as a left split.
4914 insert.ins_split = SPLIT_RIGHT;
4916 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4921 BUG_ON(do_leftright);
4925 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4931 if (do_leftright == 1) {
4933 struct ocfs2_extent_list *el;
4936 split_rec = *orig_split_rec;
4938 ocfs2_reinit_path(path, 1);
4940 cpos = le32_to_cpu(split_rec.e_cpos);
4941 ret = ocfs2_find_path(et->et_ci, path, cpos);
4947 el = path_leaf_el(path);
4948 split_index = ocfs2_search_extent_list(el, cpos);
4956 static int ocfs2_replace_extent_rec(struct inode *inode,
4958 struct ocfs2_path *path,
4959 struct ocfs2_extent_list *el,
4961 struct ocfs2_extent_rec *split_rec)
4965 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
4966 path_num_items(path) - 1);
4972 el->l_recs[split_index] = *split_rec;
4974 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4980 * Mark part or all of the extent record at split_index in the leaf
4981 * pointed to by path as written. This removes the unwritten
4984 * Care is taken to handle contiguousness so as to not grow the tree.
4986 * meta_ac is not strictly necessary - we only truly need it if growth
4987 * of the tree is required. All other cases will degrade into a less
4988 * optimal tree layout.
4990 * last_eb_bh should be the rightmost leaf block for any extent
4991 * btree. Since a split may grow the tree or a merge might shrink it,
4992 * the caller cannot trust the contents of that buffer after this call.
4994 * This code is optimized for readability - several passes might be
4995 * made over certain portions of the tree. All of those blocks will
4996 * have been brought into cache (and pinned via the journal), so the
4997 * extra overhead is not expressed in terms of disk reads.
4999 static int __ocfs2_mark_extent_written(struct inode *inode,
5000 struct ocfs2_extent_tree *et,
5002 struct ocfs2_path *path,
5004 struct ocfs2_extent_rec *split_rec,
5005 struct ocfs2_alloc_context *meta_ac,
5006 struct ocfs2_cached_dealloc_ctxt *dealloc)
5009 struct ocfs2_extent_list *el = path_leaf_el(path);
5010 struct buffer_head *last_eb_bh = NULL;
5011 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5012 struct ocfs2_merge_ctxt ctxt;
5013 struct ocfs2_extent_list *rightmost_el;
5015 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
5021 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5022 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5023 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5029 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
5034 * The core merge / split code wants to know how much room is
5035 * left in this inodes allocation tree, so we pass the
5036 * rightmost extent list.
5038 if (path->p_tree_depth) {
5039 struct ocfs2_extent_block *eb;
5041 ret = ocfs2_read_extent_block(et->et_ci,
5042 ocfs2_et_get_last_eb_blk(et),
5049 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5050 rightmost_el = &eb->h_list;
5052 rightmost_el = path_root_el(path);
5054 if (rec->e_cpos == split_rec->e_cpos &&
5055 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5056 ctxt.c_split_covers_rec = 1;
5058 ctxt.c_split_covers_rec = 0;
5060 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5062 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5063 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5064 ctxt.c_split_covers_rec);
5066 if (ctxt.c_contig_type == CONTIG_NONE) {
5067 if (ctxt.c_split_covers_rec)
5068 ret = ocfs2_replace_extent_rec(inode, handle,
5070 split_index, split_rec);
5072 ret = ocfs2_split_and_insert(inode, handle, path, et,
5073 &last_eb_bh, split_index,
5074 split_rec, meta_ac);
5078 ret = ocfs2_try_to_merge_extent(inode, handle, path,
5079 split_index, split_rec,
5080 dealloc, &ctxt, et);
5091 * Mark the already-existing extent at cpos as written for len clusters.
5093 * If the existing extent is larger than the request, initiate a
5094 * split. An attempt will be made at merging with adjacent extents.
5096 * The caller is responsible for passing down meta_ac if we'll need it.
5098 int ocfs2_mark_extent_written(struct inode *inode,
5099 struct ocfs2_extent_tree *et,
5100 handle_t *handle, u32 cpos, u32 len, u32 phys,
5101 struct ocfs2_alloc_context *meta_ac,
5102 struct ocfs2_cached_dealloc_ctxt *dealloc)
5105 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5106 struct ocfs2_extent_rec split_rec;
5107 struct ocfs2_path *left_path = NULL;
5108 struct ocfs2_extent_list *el;
5110 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5111 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5113 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5114 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5115 "that are being written to, but the feature bit "
5116 "is not set in the super block.",
5117 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5123 * XXX: This should be fixed up so that we just re-insert the
5124 * next extent records.
5126 * XXX: This is a hack on the extent tree, maybe it should be
5129 if (et->et_ops == &ocfs2_dinode_et_ops)
5130 ocfs2_extent_map_trunc(inode, 0);
5132 left_path = ocfs2_new_path_from_et(et);
5139 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
5144 el = path_leaf_el(left_path);
5146 index = ocfs2_search_extent_list(el, cpos);
5147 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5148 ocfs2_error(inode->i_sb,
5149 "Inode %llu has an extent at cpos %u which can no "
5150 "longer be found.\n",
5151 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5156 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5157 split_rec.e_cpos = cpu_to_le32(cpos);
5158 split_rec.e_leaf_clusters = cpu_to_le16(len);
5159 split_rec.e_blkno = cpu_to_le64(start_blkno);
5160 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5161 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5163 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
5164 index, &split_rec, meta_ac,
5170 ocfs2_free_path(left_path);
5174 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
5175 handle_t *handle, struct ocfs2_path *path,
5176 int index, u32 new_range,
5177 struct ocfs2_alloc_context *meta_ac)
5179 int ret, depth, credits = handle->h_buffer_credits;
5180 struct buffer_head *last_eb_bh = NULL;
5181 struct ocfs2_extent_block *eb;
5182 struct ocfs2_extent_list *rightmost_el, *el;
5183 struct ocfs2_extent_rec split_rec;
5184 struct ocfs2_extent_rec *rec;
5185 struct ocfs2_insert_type insert;
5188 * Setup the record to split before we grow the tree.
5190 el = path_leaf_el(path);
5191 rec = &el->l_recs[index];
5192 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5194 depth = path->p_tree_depth;
5196 ret = ocfs2_read_extent_block(et->et_ci,
5197 ocfs2_et_get_last_eb_blk(et),
5204 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5205 rightmost_el = &eb->h_list;
5207 rightmost_el = path_leaf_el(path);
5209 credits += path->p_tree_depth +
5210 ocfs2_extend_meta_needed(et->et_root_el);
5211 ret = ocfs2_extend_trans(handle, credits);
5217 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5218 le16_to_cpu(rightmost_el->l_count)) {
5219 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
5227 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5228 insert.ins_appending = APPEND_NONE;
5229 insert.ins_contig = CONTIG_NONE;
5230 insert.ins_split = SPLIT_RIGHT;
5231 insert.ins_tree_depth = depth;
5233 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
5242 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5243 struct ocfs2_path *path, int index,
5244 struct ocfs2_cached_dealloc_ctxt *dealloc,
5246 struct ocfs2_extent_tree *et)
5249 u32 left_cpos, rec_range, trunc_range;
5250 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5251 struct super_block *sb = inode->i_sb;
5252 struct ocfs2_path *left_path = NULL;
5253 struct ocfs2_extent_list *el = path_leaf_el(path);
5254 struct ocfs2_extent_rec *rec;
5255 struct ocfs2_extent_block *eb;
5257 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5258 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5267 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5268 path->p_tree_depth) {
5270 * Check whether this is the rightmost tree record. If
5271 * we remove all of this record or part of its right
5272 * edge then an update of the record lengths above it
5275 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5276 if (eb->h_next_leaf_blk == 0)
5277 is_rightmost_tree_rec = 1;
5280 rec = &el->l_recs[index];
5281 if (index == 0 && path->p_tree_depth &&
5282 le32_to_cpu(rec->e_cpos) == cpos) {
5284 * Changing the leftmost offset (via partial or whole
5285 * record truncate) of an interior (or rightmost) path
5286 * means we have to update the subtree that is formed
5287 * by this leaf and the one to it's left.
5289 * There are two cases we can skip:
5290 * 1) Path is the leftmost one in our inode tree.
5291 * 2) The leaf is rightmost and will be empty after
5292 * we remove the extent record - the rotate code
5293 * knows how to update the newly formed edge.
5296 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5303 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5304 left_path = ocfs2_new_path_from_path(path);
5311 ret = ocfs2_find_path(et->et_ci, left_path,
5320 ret = ocfs2_extend_rotate_transaction(handle, 0,
5321 handle->h_buffer_credits,
5328 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
5334 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
5340 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5341 trunc_range = cpos + len;
5343 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5346 memset(rec, 0, sizeof(*rec));
5347 ocfs2_cleanup_merge(el, index);
5350 next_free = le16_to_cpu(el->l_next_free_rec);
5351 if (is_rightmost_tree_rec && next_free > 1) {
5353 * We skip the edge update if this path will
5354 * be deleted by the rotate code.
5356 rec = &el->l_recs[next_free - 1];
5357 ocfs2_adjust_rightmost_records(inode, handle, path,
5360 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5361 /* Remove leftmost portion of the record. */
5362 le32_add_cpu(&rec->e_cpos, len);
5363 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5364 le16_add_cpu(&rec->e_leaf_clusters, -len);
5365 } else if (rec_range == trunc_range) {
5366 /* Remove rightmost portion of the record */
5367 le16_add_cpu(&rec->e_leaf_clusters, -len);
5368 if (is_rightmost_tree_rec)
5369 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5371 /* Caller should have trapped this. */
5372 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5373 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5374 le32_to_cpu(rec->e_cpos),
5375 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5382 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
5383 ocfs2_complete_edge_insert(handle, left_path, path,
5387 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5389 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5396 ocfs2_free_path(left_path);
5400 int ocfs2_remove_extent(struct inode *inode,
5401 struct ocfs2_extent_tree *et,
5402 u32 cpos, u32 len, handle_t *handle,
5403 struct ocfs2_alloc_context *meta_ac,
5404 struct ocfs2_cached_dealloc_ctxt *dealloc)
5407 u32 rec_range, trunc_range;
5408 struct ocfs2_extent_rec *rec;
5409 struct ocfs2_extent_list *el;
5410 struct ocfs2_path *path = NULL;
5412 ocfs2_extent_map_trunc(inode, 0);
5414 path = ocfs2_new_path_from_et(et);
5421 ret = ocfs2_find_path(et->et_ci, path, cpos);
5427 el = path_leaf_el(path);
5428 index = ocfs2_search_extent_list(el, cpos);
5429 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5430 ocfs2_error(inode->i_sb,
5431 "Inode %llu has an extent at cpos %u which can no "
5432 "longer be found.\n",
5433 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5439 * We have 3 cases of extent removal:
5440 * 1) Range covers the entire extent rec
5441 * 2) Range begins or ends on one edge of the extent rec
5442 * 3) Range is in the middle of the extent rec (no shared edges)
5444 * For case 1 we remove the extent rec and left rotate to
5447 * For case 2 we just shrink the existing extent rec, with a
5448 * tree update if the shrinking edge is also the edge of an
5451 * For case 3 we do a right split to turn the extent rec into
5452 * something case 2 can handle.
5454 rec = &el->l_recs[index];
5455 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5456 trunc_range = cpos + len;
5458 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5460 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5461 "(cpos %u, len %u)\n",
5462 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5463 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5465 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5466 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5473 ret = ocfs2_split_tree(inode, et, handle, path, index,
5474 trunc_range, meta_ac);
5481 * The split could have manipulated the tree enough to
5482 * move the record location, so we have to look for it again.
5484 ocfs2_reinit_path(path, 1);
5486 ret = ocfs2_find_path(et->et_ci, path, cpos);
5492 el = path_leaf_el(path);
5493 index = ocfs2_search_extent_list(el, cpos);
5494 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5495 ocfs2_error(inode->i_sb,
5496 "Inode %llu: split at cpos %u lost record.",
5497 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5504 * Double check our values here. If anything is fishy,
5505 * it's easier to catch it at the top level.
5507 rec = &el->l_recs[index];
5508 rec_range = le32_to_cpu(rec->e_cpos) +
5509 ocfs2_rec_clusters(el, rec);
5510 if (rec_range != trunc_range) {
5511 ocfs2_error(inode->i_sb,
5512 "Inode %llu: error after split at cpos %u"
5513 "trunc len %u, existing record is (%u,%u)",
5514 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5515 cpos, len, le32_to_cpu(rec->e_cpos),
5516 ocfs2_rec_clusters(el, rec));
5521 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5530 ocfs2_free_path(path);
5534 int ocfs2_remove_btree_range(struct inode *inode,
5535 struct ocfs2_extent_tree *et,
5536 u32 cpos, u32 phys_cpos, u32 len,
5537 struct ocfs2_cached_dealloc_ctxt *dealloc)
5540 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5541 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5542 struct inode *tl_inode = osb->osb_tl_inode;
5544 struct ocfs2_alloc_context *meta_ac = NULL;
5546 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5552 mutex_lock(&tl_inode->i_mutex);
5554 if (ocfs2_truncate_log_needs_flush(osb)) {
5555 ret = __ocfs2_flush_truncate_log(osb);
5562 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5563 if (IS_ERR(handle)) {
5564 ret = PTR_ERR(handle);
5569 ret = ocfs2_et_root_journal_access(handle, et,
5570 OCFS2_JOURNAL_ACCESS_WRITE);
5576 vfs_dq_free_space_nodirty(inode,
5577 ocfs2_clusters_to_bytes(inode->i_sb, len));
5579 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5586 ocfs2_et_update_clusters(et, -len);
5588 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5594 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5599 ocfs2_commit_trans(osb, handle);
5601 mutex_unlock(&tl_inode->i_mutex);
5604 ocfs2_free_alloc_context(meta_ac);
5609 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5611 struct buffer_head *tl_bh = osb->osb_tl_bh;
5612 struct ocfs2_dinode *di;
5613 struct ocfs2_truncate_log *tl;
5615 di = (struct ocfs2_dinode *) tl_bh->b_data;
5616 tl = &di->id2.i_dealloc;
5618 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5619 "slot %d, invalid truncate log parameters: used = "
5620 "%u, count = %u\n", osb->slot_num,
5621 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5622 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5625 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5626 unsigned int new_start)
5628 unsigned int tail_index;
5629 unsigned int current_tail;
5631 /* No records, nothing to coalesce */
5632 if (!le16_to_cpu(tl->tl_used))
5635 tail_index = le16_to_cpu(tl->tl_used) - 1;
5636 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5637 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5639 return current_tail == new_start;
5642 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5645 unsigned int num_clusters)
5648 unsigned int start_cluster, tl_count;
5649 struct inode *tl_inode = osb->osb_tl_inode;
5650 struct buffer_head *tl_bh = osb->osb_tl_bh;
5651 struct ocfs2_dinode *di;
5652 struct ocfs2_truncate_log *tl;
5654 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5655 (unsigned long long)start_blk, num_clusters);
5657 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5659 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5661 di = (struct ocfs2_dinode *) tl_bh->b_data;
5663 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5664 * by the underlying call to ocfs2_read_inode_block(), so any
5665 * corruption is a code bug */
5666 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5668 tl = &di->id2.i_dealloc;
5669 tl_count = le16_to_cpu(tl->tl_count);
5670 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5672 "Truncate record count on #%llu invalid "
5673 "wanted %u, actual %u\n",
5674 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5675 ocfs2_truncate_recs_per_inode(osb->sb),
5676 le16_to_cpu(tl->tl_count));
5678 /* Caller should have known to flush before calling us. */
5679 index = le16_to_cpu(tl->tl_used);
5680 if (index >= tl_count) {
5686 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5687 OCFS2_JOURNAL_ACCESS_WRITE);
5693 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5694 "%llu (index = %d)\n", num_clusters, start_cluster,
5695 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5697 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5699 * Move index back to the record we are coalescing with.
5700 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5704 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5705 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5706 index, le32_to_cpu(tl->tl_recs[index].t_start),
5709 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5710 tl->tl_used = cpu_to_le16(index + 1);
5712 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5714 status = ocfs2_journal_dirty(handle, tl_bh);
5725 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5727 struct inode *data_alloc_inode,
5728 struct buffer_head *data_alloc_bh)
5732 unsigned int num_clusters;
5734 struct ocfs2_truncate_rec rec;
5735 struct ocfs2_dinode *di;
5736 struct ocfs2_truncate_log *tl;
5737 struct inode *tl_inode = osb->osb_tl_inode;
5738 struct buffer_head *tl_bh = osb->osb_tl_bh;
5742 di = (struct ocfs2_dinode *) tl_bh->b_data;
5743 tl = &di->id2.i_dealloc;
5744 i = le16_to_cpu(tl->tl_used) - 1;
5746 /* Caller has given us at least enough credits to
5747 * update the truncate log dinode */
5748 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5749 OCFS2_JOURNAL_ACCESS_WRITE);
5755 tl->tl_used = cpu_to_le16(i);
5757 status = ocfs2_journal_dirty(handle, tl_bh);
5763 /* TODO: Perhaps we can calculate the bulk of the
5764 * credits up front rather than extending like
5766 status = ocfs2_extend_trans(handle,
5767 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5773 rec = tl->tl_recs[i];
5774 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5775 le32_to_cpu(rec.t_start));
5776 num_clusters = le32_to_cpu(rec.t_clusters);
5778 /* if start_blk is not set, we ignore the record as
5781 mlog(0, "free record %d, start = %u, clusters = %u\n",
5782 i, le32_to_cpu(rec.t_start), num_clusters);
5784 status = ocfs2_free_clusters(handle, data_alloc_inode,
5785 data_alloc_bh, start_blk,
5800 /* Expects you to already be holding tl_inode->i_mutex */
5801 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5804 unsigned int num_to_flush;
5806 struct inode *tl_inode = osb->osb_tl_inode;
5807 struct inode *data_alloc_inode = NULL;
5808 struct buffer_head *tl_bh = osb->osb_tl_bh;
5809 struct buffer_head *data_alloc_bh = NULL;
5810 struct ocfs2_dinode *di;
5811 struct ocfs2_truncate_log *tl;
5815 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5817 di = (struct ocfs2_dinode *) tl_bh->b_data;
5819 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5820 * by the underlying call to ocfs2_read_inode_block(), so any
5821 * corruption is a code bug */
5822 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5824 tl = &di->id2.i_dealloc;
5825 num_to_flush = le16_to_cpu(tl->tl_used);
5826 mlog(0, "Flush %u records from truncate log #%llu\n",
5827 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5828 if (!num_to_flush) {
5833 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5834 GLOBAL_BITMAP_SYSTEM_INODE,
5835 OCFS2_INVALID_SLOT);
5836 if (!data_alloc_inode) {
5838 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5842 mutex_lock(&data_alloc_inode->i_mutex);
5844 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5850 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5851 if (IS_ERR(handle)) {
5852 status = PTR_ERR(handle);
5857 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5862 ocfs2_commit_trans(osb, handle);
5865 brelse(data_alloc_bh);
5866 ocfs2_inode_unlock(data_alloc_inode, 1);
5869 mutex_unlock(&data_alloc_inode->i_mutex);
5870 iput(data_alloc_inode);
5877 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5880 struct inode *tl_inode = osb->osb_tl_inode;
5882 mutex_lock(&tl_inode->i_mutex);
5883 status = __ocfs2_flush_truncate_log(osb);
5884 mutex_unlock(&tl_inode->i_mutex);
5889 static void ocfs2_truncate_log_worker(struct work_struct *work)
5892 struct ocfs2_super *osb =
5893 container_of(work, struct ocfs2_super,
5894 osb_truncate_log_wq.work);
5898 status = ocfs2_flush_truncate_log(osb);
5902 ocfs2_init_inode_steal_slot(osb);
5907 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5908 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5911 if (osb->osb_tl_inode) {
5912 /* We want to push off log flushes while truncates are
5915 cancel_delayed_work(&osb->osb_truncate_log_wq);
5917 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5918 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5922 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5924 struct inode **tl_inode,
5925 struct buffer_head **tl_bh)
5928 struct inode *inode = NULL;
5929 struct buffer_head *bh = NULL;
5931 inode = ocfs2_get_system_file_inode(osb,
5932 TRUNCATE_LOG_SYSTEM_INODE,
5936 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5940 status = ocfs2_read_inode_block(inode, &bh);
5954 /* called during the 1st stage of node recovery. we stamp a clean
5955 * truncate log and pass back a copy for processing later. if the
5956 * truncate log does not require processing, a *tl_copy is set to
5958 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5960 struct ocfs2_dinode **tl_copy)
5963 struct inode *tl_inode = NULL;
5964 struct buffer_head *tl_bh = NULL;
5965 struct ocfs2_dinode *di;
5966 struct ocfs2_truncate_log *tl;
5970 mlog(0, "recover truncate log from slot %d\n", slot_num);
5972 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5978 di = (struct ocfs2_dinode *) tl_bh->b_data;
5980 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5981 * validated by the underlying call to ocfs2_read_inode_block(),
5982 * so any corruption is a code bug */
5983 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5985 tl = &di->id2.i_dealloc;
5986 if (le16_to_cpu(tl->tl_used)) {
5987 mlog(0, "We'll have %u logs to recover\n",
5988 le16_to_cpu(tl->tl_used));
5990 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5997 /* Assuming the write-out below goes well, this copy
5998 * will be passed back to recovery for processing. */
5999 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6001 /* All we need to do to clear the truncate log is set
6005 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
6006 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
6018 if (status < 0 && (*tl_copy)) {
6027 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6028 struct ocfs2_dinode *tl_copy)
6032 unsigned int clusters, num_recs, start_cluster;
6035 struct inode *tl_inode = osb->osb_tl_inode;
6036 struct ocfs2_truncate_log *tl;
6040 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6041 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6045 tl = &tl_copy->id2.i_dealloc;
6046 num_recs = le16_to_cpu(tl->tl_used);
6047 mlog(0, "cleanup %u records from %llu\n", num_recs,
6048 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
6050 mutex_lock(&tl_inode->i_mutex);
6051 for(i = 0; i < num_recs; i++) {
6052 if (ocfs2_truncate_log_needs_flush(osb)) {
6053 status = __ocfs2_flush_truncate_log(osb);
6060 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6061 if (IS_ERR(handle)) {
6062 status = PTR_ERR(handle);
6067 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6068 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6069 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6071 status = ocfs2_truncate_log_append(osb, handle,
6072 start_blk, clusters);
6073 ocfs2_commit_trans(osb, handle);
6081 mutex_unlock(&tl_inode->i_mutex);
6087 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6090 struct inode *tl_inode = osb->osb_tl_inode;
6095 cancel_delayed_work(&osb->osb_truncate_log_wq);
6096 flush_workqueue(ocfs2_wq);
6098 status = ocfs2_flush_truncate_log(osb);
6102 brelse(osb->osb_tl_bh);
6103 iput(osb->osb_tl_inode);
6109 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6112 struct inode *tl_inode = NULL;
6113 struct buffer_head *tl_bh = NULL;
6117 status = ocfs2_get_truncate_log_info(osb,
6124 /* ocfs2_truncate_log_shutdown keys on the existence of
6125 * osb->osb_tl_inode so we don't set any of the osb variables
6126 * until we're sure all is well. */
6127 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6128 ocfs2_truncate_log_worker);
6129 osb->osb_tl_bh = tl_bh;
6130 osb->osb_tl_inode = tl_inode;
6137 * Delayed de-allocation of suballocator blocks.
6139 * Some sets of block de-allocations might involve multiple suballocator inodes.
6141 * The locking for this can get extremely complicated, especially when
6142 * the suballocator inodes to delete from aren't known until deep
6143 * within an unrelated codepath.
6145 * ocfs2_extent_block structures are a good example of this - an inode
6146 * btree could have been grown by any number of nodes each allocating
6147 * out of their own suballoc inode.
6149 * These structures allow the delay of block de-allocation until a
6150 * later time, when locking of multiple cluster inodes won't cause
6155 * Describe a single bit freed from a suballocator. For the block
6156 * suballocators, it represents one block. For the global cluster
6157 * allocator, it represents some clusters and free_bit indicates
6160 struct ocfs2_cached_block_free {
6161 struct ocfs2_cached_block_free *free_next;
6163 unsigned int free_bit;
6166 struct ocfs2_per_slot_free_list {
6167 struct ocfs2_per_slot_free_list *f_next_suballocator;
6170 struct ocfs2_cached_block_free *f_first;
6173 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6176 struct ocfs2_cached_block_free *head)
6181 struct inode *inode;
6182 struct buffer_head *di_bh = NULL;
6183 struct ocfs2_cached_block_free *tmp;
6185 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6192 mutex_lock(&inode->i_mutex);
6194 ret = ocfs2_inode_lock(inode, &di_bh, 1);
6200 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6201 if (IS_ERR(handle)) {
6202 ret = PTR_ERR(handle);
6208 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6210 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6211 head->free_bit, (unsigned long long)head->free_blk);
6213 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6214 head->free_bit, bg_blkno, 1);
6220 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6227 head = head->free_next;
6232 ocfs2_commit_trans(osb, handle);
6235 ocfs2_inode_unlock(inode, 1);
6238 mutex_unlock(&inode->i_mutex);
6242 /* Premature exit may have left some dangling items. */
6244 head = head->free_next;
6251 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6252 u64 blkno, unsigned int bit)
6255 struct ocfs2_cached_block_free *item;
6257 item = kmalloc(sizeof(*item), GFP_NOFS);
6264 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6265 bit, (unsigned long long)blkno);
6267 item->free_blk = blkno;
6268 item->free_bit = bit;
6269 item->free_next = ctxt->c_global_allocator;
6271 ctxt->c_global_allocator = item;
6275 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6276 struct ocfs2_cached_block_free *head)
6278 struct ocfs2_cached_block_free *tmp;
6279 struct inode *tl_inode = osb->osb_tl_inode;
6283 mutex_lock(&tl_inode->i_mutex);
6286 if (ocfs2_truncate_log_needs_flush(osb)) {
6287 ret = __ocfs2_flush_truncate_log(osb);
6294 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6295 if (IS_ERR(handle)) {
6296 ret = PTR_ERR(handle);
6301 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6304 ocfs2_commit_trans(osb, handle);
6306 head = head->free_next;
6315 mutex_unlock(&tl_inode->i_mutex);
6318 /* Premature exit may have left some dangling items. */
6320 head = head->free_next;
6327 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6328 struct ocfs2_cached_dealloc_ctxt *ctxt)
6331 struct ocfs2_per_slot_free_list *fl;
6336 while (ctxt->c_first_suballocator) {
6337 fl = ctxt->c_first_suballocator;
6340 mlog(0, "Free items: (type %u, slot %d)\n",
6341 fl->f_inode_type, fl->f_slot);
6342 ret2 = ocfs2_free_cached_blocks(osb,
6352 ctxt->c_first_suballocator = fl->f_next_suballocator;
6356 if (ctxt->c_global_allocator) {
6357 ret2 = ocfs2_free_cached_clusters(osb,
6358 ctxt->c_global_allocator);
6364 ctxt->c_global_allocator = NULL;
6370 static struct ocfs2_per_slot_free_list *
6371 ocfs2_find_per_slot_free_list(int type,
6373 struct ocfs2_cached_dealloc_ctxt *ctxt)
6375 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6378 if (fl->f_inode_type == type && fl->f_slot == slot)
6381 fl = fl->f_next_suballocator;
6384 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6386 fl->f_inode_type = type;
6389 fl->f_next_suballocator = ctxt->c_first_suballocator;
6391 ctxt->c_first_suballocator = fl;
6396 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6397 int type, int slot, u64 blkno,
6401 struct ocfs2_per_slot_free_list *fl;
6402 struct ocfs2_cached_block_free *item;
6404 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6411 item = kmalloc(sizeof(*item), GFP_NOFS);
6418 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6419 type, slot, bit, (unsigned long long)blkno);
6421 item->free_blk = blkno;
6422 item->free_bit = bit;
6423 item->free_next = fl->f_first;
6432 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6433 struct ocfs2_extent_block *eb)
6435 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6436 le16_to_cpu(eb->h_suballoc_slot),
6437 le64_to_cpu(eb->h_blkno),
6438 le16_to_cpu(eb->h_suballoc_bit));
6441 /* This function will figure out whether the currently last extent
6442 * block will be deleted, and if it will, what the new last extent
6443 * block will be so we can update his h_next_leaf_blk field, as well
6444 * as the dinodes i_last_eb_blk */
6445 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6446 unsigned int clusters_to_del,
6447 struct ocfs2_path *path,
6448 struct buffer_head **new_last_eb)
6450 int next_free, ret = 0;
6452 struct ocfs2_extent_rec *rec;
6453 struct ocfs2_extent_block *eb;
6454 struct ocfs2_extent_list *el;
6455 struct buffer_head *bh = NULL;
6457 *new_last_eb = NULL;
6459 /* we have no tree, so of course, no last_eb. */
6460 if (!path->p_tree_depth)
6463 /* trunc to zero special case - this makes tree_depth = 0
6464 * regardless of what it is. */
6465 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6468 el = path_leaf_el(path);
6469 BUG_ON(!el->l_next_free_rec);
6472 * Make sure that this extent list will actually be empty
6473 * after we clear away the data. We can shortcut out if
6474 * there's more than one non-empty extent in the
6475 * list. Otherwise, a check of the remaining extent is
6478 next_free = le16_to_cpu(el->l_next_free_rec);
6480 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6484 /* We may have a valid extent in index 1, check it. */
6486 rec = &el->l_recs[1];
6489 * Fall through - no more nonempty extents, so we want
6490 * to delete this leaf.
6496 rec = &el->l_recs[0];
6501 * Check it we'll only be trimming off the end of this
6504 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6508 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6514 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
6520 eb = (struct ocfs2_extent_block *) bh->b_data;
6523 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6524 * Any corruption is a code bug. */
6525 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
6528 get_bh(*new_last_eb);
6529 mlog(0, "returning block %llu, (cpos: %u)\n",
6530 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6538 * Trim some clusters off the rightmost edge of a tree. Only called
6541 * The caller needs to:
6542 * - start journaling of each path component.
6543 * - compute and fully set up any new last ext block
6545 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6546 handle_t *handle, struct ocfs2_truncate_context *tc,
6547 u32 clusters_to_del, u64 *delete_start)
6549 int ret, i, index = path->p_tree_depth;
6552 struct buffer_head *bh;
6553 struct ocfs2_extent_list *el;
6554 struct ocfs2_extent_rec *rec;
6558 while (index >= 0) {
6559 bh = path->p_node[index].bh;
6560 el = path->p_node[index].el;
6562 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6563 index, (unsigned long long)bh->b_blocknr);
6565 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6568 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6569 ocfs2_error(inode->i_sb,
6570 "Inode %lu has invalid ext. block %llu",
6572 (unsigned long long)bh->b_blocknr);
6578 i = le16_to_cpu(el->l_next_free_rec) - 1;
6579 rec = &el->l_recs[i];
6581 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6582 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6583 ocfs2_rec_clusters(el, rec),
6584 (unsigned long long)le64_to_cpu(rec->e_blkno),
6585 le16_to_cpu(el->l_next_free_rec));
6587 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6589 if (le16_to_cpu(el->l_tree_depth) == 0) {
6591 * If the leaf block contains a single empty
6592 * extent and no records, we can just remove
6595 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6597 sizeof(struct ocfs2_extent_rec));
6598 el->l_next_free_rec = cpu_to_le16(0);
6604 * Remove any empty extents by shifting things
6605 * left. That should make life much easier on
6606 * the code below. This condition is rare
6607 * enough that we shouldn't see a performance
6610 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6611 le16_add_cpu(&el->l_next_free_rec, -1);
6614 i < le16_to_cpu(el->l_next_free_rec); i++)
6615 el->l_recs[i] = el->l_recs[i + 1];
6617 memset(&el->l_recs[i], 0,
6618 sizeof(struct ocfs2_extent_rec));
6621 * We've modified our extent list. The
6622 * simplest way to handle this change
6623 * is to being the search from the
6626 goto find_tail_record;
6629 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6632 * We'll use "new_edge" on our way back up the
6633 * tree to know what our rightmost cpos is.
6635 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6636 new_edge += le32_to_cpu(rec->e_cpos);
6639 * The caller will use this to delete data blocks.
6641 *delete_start = le64_to_cpu(rec->e_blkno)
6642 + ocfs2_clusters_to_blocks(inode->i_sb,
6643 le16_to_cpu(rec->e_leaf_clusters));
6646 * If it's now empty, remove this record.
6648 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6650 sizeof(struct ocfs2_extent_rec));
6651 le16_add_cpu(&el->l_next_free_rec, -1);
6654 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6656 sizeof(struct ocfs2_extent_rec));
6657 le16_add_cpu(&el->l_next_free_rec, -1);
6662 /* Can this actually happen? */
6663 if (le16_to_cpu(el->l_next_free_rec) == 0)
6667 * We never actually deleted any clusters
6668 * because our leaf was empty. There's no
6669 * reason to adjust the rightmost edge then.
6674 rec->e_int_clusters = cpu_to_le32(new_edge);
6675 le32_add_cpu(&rec->e_int_clusters,
6676 -le32_to_cpu(rec->e_cpos));
6679 * A deleted child record should have been
6682 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6686 ret = ocfs2_journal_dirty(handle, bh);
6692 mlog(0, "extent list container %llu, after: record %d: "
6693 "(%u, %u, %llu), next = %u.\n",
6694 (unsigned long long)bh->b_blocknr, i,
6695 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6696 (unsigned long long)le64_to_cpu(rec->e_blkno),
6697 le16_to_cpu(el->l_next_free_rec));
6700 * We must be careful to only attempt delete of an
6701 * extent block (and not the root inode block).
6703 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6704 struct ocfs2_extent_block *eb =
6705 (struct ocfs2_extent_block *)bh->b_data;
6708 * Save this for use when processing the
6711 deleted_eb = le64_to_cpu(eb->h_blkno);
6713 mlog(0, "deleting this extent block.\n");
6715 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
6717 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6718 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6719 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6721 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6722 /* An error here is not fatal. */
6737 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6738 unsigned int clusters_to_del,
6739 struct inode *inode,
6740 struct buffer_head *fe_bh,
6742 struct ocfs2_truncate_context *tc,
6743 struct ocfs2_path *path)
6746 struct ocfs2_dinode *fe;
6747 struct ocfs2_extent_block *last_eb = NULL;
6748 struct ocfs2_extent_list *el;
6749 struct buffer_head *last_eb_bh = NULL;
6752 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6754 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6762 * Each component will be touched, so we might as well journal
6763 * here to avoid having to handle errors later.
6765 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
6772 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
6773 OCFS2_JOURNAL_ACCESS_WRITE);
6779 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6782 el = &(fe->id2.i_list);
6785 * Lower levels depend on this never happening, but it's best
6786 * to check it up here before changing the tree.
6788 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6789 ocfs2_error(inode->i_sb,
6790 "Inode %lu has an empty extent record, depth %u\n",
6791 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6796 vfs_dq_free_space_nodirty(inode,
6797 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
6798 spin_lock(&OCFS2_I(inode)->ip_lock);
6799 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6801 spin_unlock(&OCFS2_I(inode)->ip_lock);
6802 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6803 inode->i_blocks = ocfs2_inode_sector_count(inode);
6805 status = ocfs2_trim_tree(inode, path, handle, tc,
6806 clusters_to_del, &delete_blk);
6812 if (le32_to_cpu(fe->i_clusters) == 0) {
6813 /* trunc to zero is a special case. */
6814 el->l_tree_depth = 0;
6815 fe->i_last_eb_blk = 0;
6817 fe->i_last_eb_blk = last_eb->h_blkno;
6819 status = ocfs2_journal_dirty(handle, fe_bh);
6826 /* If there will be a new last extent block, then by
6827 * definition, there cannot be any leaves to the right of
6829 last_eb->h_next_leaf_blk = 0;
6830 status = ocfs2_journal_dirty(handle, last_eb_bh);
6838 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6852 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6854 set_buffer_uptodate(bh);
6855 mark_buffer_dirty(bh);
6859 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6860 unsigned int from, unsigned int to,
6861 struct page *page, int zero, u64 *phys)
6863 int ret, partial = 0;
6865 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6870 zero_user_segment(page, from, to);
6873 * Need to set the buffers we zero'd into uptodate
6874 * here if they aren't - ocfs2_map_page_blocks()
6875 * might've skipped some
6877 ret = walk_page_buffers(handle, page_buffers(page),
6882 else if (ocfs2_should_order_data(inode)) {
6883 ret = ocfs2_jbd2_file_inode(handle, inode);
6889 SetPageUptodate(page);
6891 flush_dcache_page(page);
6894 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6895 loff_t end, struct page **pages,
6896 int numpages, u64 phys, handle_t *handle)
6900 unsigned int from, to = PAGE_CACHE_SIZE;
6901 struct super_block *sb = inode->i_sb;
6903 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6908 to = PAGE_CACHE_SIZE;
6909 for(i = 0; i < numpages; i++) {
6912 from = start & (PAGE_CACHE_SIZE - 1);
6913 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6914 to = end & (PAGE_CACHE_SIZE - 1);
6916 BUG_ON(from > PAGE_CACHE_SIZE);
6917 BUG_ON(to > PAGE_CACHE_SIZE);
6919 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6922 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6926 ocfs2_unlock_and_free_pages(pages, numpages);
6929 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6930 struct page **pages, int *num)
6932 int numpages, ret = 0;
6933 struct super_block *sb = inode->i_sb;
6934 struct address_space *mapping = inode->i_mapping;
6935 unsigned long index;
6936 loff_t last_page_bytes;
6938 BUG_ON(start > end);
6940 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6941 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6944 last_page_bytes = PAGE_ALIGN(end);
6945 index = start >> PAGE_CACHE_SHIFT;
6947 pages[numpages] = grab_cache_page(mapping, index);
6948 if (!pages[numpages]) {
6956 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6961 ocfs2_unlock_and_free_pages(pages, numpages);
6971 * Zero the area past i_size but still within an allocated
6972 * cluster. This avoids exposing nonzero data on subsequent file
6975 * We need to call this before i_size is updated on the inode because
6976 * otherwise block_write_full_page() will skip writeout of pages past
6977 * i_size. The new_i_size parameter is passed for this reason.
6979 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6980 u64 range_start, u64 range_end)
6982 int ret = 0, numpages;
6983 struct page **pages = NULL;
6985 unsigned int ext_flags;
6986 struct super_block *sb = inode->i_sb;
6989 * File systems which don't support sparse files zero on every
6992 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6995 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6996 sizeof(struct page *), GFP_NOFS);
6997 if (pages == NULL) {
7003 if (range_start == range_end)
7006 ret = ocfs2_extent_map_get_blocks(inode,
7007 range_start >> sb->s_blocksize_bits,
7008 &phys, NULL, &ext_flags);
7015 * Tail is a hole, or is marked unwritten. In either case, we
7016 * can count on read and write to return/push zero's.
7018 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
7021 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7028 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7029 numpages, phys, handle);
7032 * Initiate writeout of the pages we zero'd here. We don't
7033 * wait on them - the truncate_inode_pages() call later will
7036 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7037 range_end - 1, SYNC_FILE_RANGE_WRITE);
7048 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7049 struct ocfs2_dinode *di)
7051 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
7052 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
7054 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7055 memset(&di->id2, 0, blocksize -
7056 offsetof(struct ocfs2_dinode, id2) -
7059 memset(&di->id2, 0, blocksize -
7060 offsetof(struct ocfs2_dinode, id2));
7063 void ocfs2_dinode_new_extent_list(struct inode *inode,
7064 struct ocfs2_dinode *di)
7066 ocfs2_zero_dinode_id2_with_xattr(inode, di);
7067 di->id2.i_list.l_tree_depth = 0;
7068 di->id2.i_list.l_next_free_rec = 0;
7069 di->id2.i_list.l_count = cpu_to_le16(
7070 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
7073 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7075 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7076 struct ocfs2_inline_data *idata = &di->id2.i_data;
7078 spin_lock(&oi->ip_lock);
7079 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7080 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7081 spin_unlock(&oi->ip_lock);
7084 * We clear the entire i_data structure here so that all
7085 * fields can be properly initialized.
7087 ocfs2_zero_dinode_id2_with_xattr(inode, di);
7089 idata->id_count = cpu_to_le16(
7090 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
7093 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7094 struct buffer_head *di_bh)
7096 int ret, i, has_data, num_pages = 0;
7098 u64 uninitialized_var(block);
7099 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7100 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7101 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7102 struct ocfs2_alloc_context *data_ac = NULL;
7103 struct page **pages = NULL;
7104 loff_t end = osb->s_clustersize;
7105 struct ocfs2_extent_tree et;
7108 has_data = i_size_read(inode) ? 1 : 0;
7111 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7112 sizeof(struct page *), GFP_NOFS);
7113 if (pages == NULL) {
7119 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7126 handle = ocfs2_start_trans(osb,
7127 ocfs2_inline_to_extents_credits(osb->sb));
7128 if (IS_ERR(handle)) {
7129 ret = PTR_ERR(handle);
7134 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7135 OCFS2_JOURNAL_ACCESS_WRITE);
7143 unsigned int page_end;
7146 if (vfs_dq_alloc_space_nodirty(inode,
7147 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7153 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7161 * Save two copies, one for insert, and one that can
7162 * be changed by ocfs2_map_and_dirty_page() below.
7164 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7167 * Non sparse file systems zero on extend, so no need
7170 if (!ocfs2_sparse_alloc(osb) &&
7171 PAGE_CACHE_SIZE < osb->s_clustersize)
7172 end = PAGE_CACHE_SIZE;
7174 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7181 * This should populate the 1st page for us and mark
7184 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7190 page_end = PAGE_CACHE_SIZE;
7191 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7192 page_end = osb->s_clustersize;
7194 for (i = 0; i < num_pages; i++)
7195 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7196 pages[i], i > 0, &phys);
7199 spin_lock(&oi->ip_lock);
7200 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7201 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7202 spin_unlock(&oi->ip_lock);
7204 ocfs2_dinode_new_extent_list(inode, di);
7206 ocfs2_journal_dirty(handle, di_bh);
7210 * An error at this point should be extremely rare. If
7211 * this proves to be false, we could always re-build
7212 * the in-inode data from our pages.
7214 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
7215 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7216 0, block, 1, 0, NULL);
7222 inode->i_blocks = ocfs2_inode_sector_count(inode);
7226 if (ret < 0 && did_quota)
7227 vfs_dq_free_space_nodirty(inode,
7228 ocfs2_clusters_to_bytes(osb->sb, 1));
7230 ocfs2_commit_trans(osb, handle);
7234 ocfs2_free_alloc_context(data_ac);
7238 ocfs2_unlock_and_free_pages(pages, num_pages);
7246 * It is expected, that by the time you call this function,
7247 * inode->i_size and fe->i_size have been adjusted.
7249 * WARNING: This will kfree the truncate context
7251 int ocfs2_commit_truncate(struct ocfs2_super *osb,
7252 struct inode *inode,
7253 struct buffer_head *fe_bh,
7254 struct ocfs2_truncate_context *tc)
7256 int status, i, credits, tl_sem = 0;
7257 u32 clusters_to_del, new_highest_cpos, range;
7258 struct ocfs2_extent_list *el;
7259 handle_t *handle = NULL;
7260 struct inode *tl_inode = osb->osb_tl_inode;
7261 struct ocfs2_path *path = NULL;
7262 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
7266 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
7267 i_size_read(inode));
7269 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7270 ocfs2_journal_access_di);
7277 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7281 * Check that we still have allocation to delete.
7283 if (OCFS2_I(inode)->ip_clusters == 0) {
7289 * Truncate always works against the rightmost tree branch.
7291 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
7297 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7298 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7301 * By now, el will point to the extent list on the bottom most
7302 * portion of this tree. Only the tail record is considered in
7305 * We handle the following cases, in order:
7306 * - empty extent: delete the remaining branch
7307 * - remove the entire record
7308 * - remove a partial record
7309 * - no record needs to be removed (truncate has completed)
7311 el = path_leaf_el(path);
7312 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7313 ocfs2_error(inode->i_sb,
7314 "Inode %llu has empty extent block at %llu\n",
7315 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7316 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7321 i = le16_to_cpu(el->l_next_free_rec) - 1;
7322 range = le32_to_cpu(el->l_recs[i].e_cpos) +
7323 ocfs2_rec_clusters(el, &el->l_recs[i]);
7324 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7325 clusters_to_del = 0;
7326 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
7327 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
7328 } else if (range > new_highest_cpos) {
7329 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
7330 le32_to_cpu(el->l_recs[i].e_cpos)) -
7337 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7338 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7340 mutex_lock(&tl_inode->i_mutex);
7342 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7343 * record is free for use. If there isn't any, we flush to get
7344 * an empty truncate log. */
7345 if (ocfs2_truncate_log_needs_flush(osb)) {
7346 status = __ocfs2_flush_truncate_log(osb);
7353 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
7354 (struct ocfs2_dinode *)fe_bh->b_data,
7356 handle = ocfs2_start_trans(osb, credits);
7357 if (IS_ERR(handle)) {
7358 status = PTR_ERR(handle);
7364 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7371 mutex_unlock(&tl_inode->i_mutex);
7374 ocfs2_commit_trans(osb, handle);
7377 ocfs2_reinit_path(path, 1);
7380 * The check above will catch the case where we've truncated
7381 * away all allocation.
7387 ocfs2_schedule_truncate_log_flush(osb, 1);
7390 mutex_unlock(&tl_inode->i_mutex);
7393 ocfs2_commit_trans(osb, handle);
7395 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7397 ocfs2_free_path(path);
7399 /* This will drop the ext_alloc cluster lock for us */
7400 ocfs2_free_truncate_context(tc);
7407 * Expects the inode to already be locked.
7409 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7410 struct inode *inode,
7411 struct buffer_head *fe_bh,
7412 struct ocfs2_truncate_context **tc)
7415 unsigned int new_i_clusters;
7416 struct ocfs2_dinode *fe;
7417 struct ocfs2_extent_block *eb;
7418 struct buffer_head *last_eb_bh = NULL;
7424 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7425 i_size_read(inode));
7426 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7428 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7429 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7430 (unsigned long long)le64_to_cpu(fe->i_size));
7432 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7438 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7440 if (fe->id2.i_list.l_tree_depth) {
7441 status = ocfs2_read_extent_block(INODE_CACHE(inode),
7442 le64_to_cpu(fe->i_last_eb_blk),
7448 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7451 (*tc)->tc_last_eb_bh = last_eb_bh;
7457 ocfs2_free_truncate_context(*tc);
7465 * 'start' is inclusive, 'end' is not.
7467 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7468 unsigned int start, unsigned int end, int trunc)
7471 unsigned int numbytes;
7473 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7474 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7475 struct ocfs2_inline_data *idata = &di->id2.i_data;
7477 if (end > i_size_read(inode))
7478 end = i_size_read(inode);
7480 BUG_ON(start >= end);
7482 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7483 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7484 !ocfs2_supports_inline_data(osb)) {
7485 ocfs2_error(inode->i_sb,
7486 "Inline data flags for inode %llu don't agree! "
7487 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7488 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7489 le16_to_cpu(di->i_dyn_features),
7490 OCFS2_I(inode)->ip_dyn_features,
7491 osb->s_feature_incompat);
7496 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7497 if (IS_ERR(handle)) {
7498 ret = PTR_ERR(handle);
7503 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7504 OCFS2_JOURNAL_ACCESS_WRITE);
7510 numbytes = end - start;
7511 memset(idata->id_data + start, 0, numbytes);
7514 * No need to worry about the data page here - it's been
7515 * truncated already and inline data doesn't need it for
7516 * pushing zero's to disk, so we'll let readpage pick it up
7520 i_size_write(inode, start);
7521 di->i_size = cpu_to_le64(start);
7524 inode->i_blocks = ocfs2_inode_sector_count(inode);
7525 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7527 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7528 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7530 ocfs2_journal_dirty(handle, di_bh);
7533 ocfs2_commit_trans(osb, handle);
7539 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7542 * The caller is responsible for completing deallocation
7543 * before freeing the context.
7545 if (tc->tc_dealloc.c_first_suballocator != NULL)
7547 "Truncate completion has non-empty dealloc context\n");
7549 brelse(tc->tc_last_eb_bh);