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"
52 #include "buffer_head_io.h"
56 * Operations for a specific extent tree type.
58 * To implement an on-disk btree (extent tree) type in ocfs2, add
59 * an ocfs2_extent_tree_operations structure and the matching
60 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
61 * for the allocation portion of the extent tree.
63 struct ocfs2_extent_tree_operations {
65 * last_eb_blk is the block number of the right most leaf extent
66 * block. Most on-disk structures containing an extent tree store
67 * this value for fast access. The ->eo_set_last_eb_blk() and
68 * ->eo_get_last_eb_blk() operations access this value. They are
71 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
73 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
76 * The on-disk structure usually keeps track of how many total
77 * clusters are stored in this extent tree. This function updates
78 * that value. new_clusters is the delta, and must be
79 * added to the total. Required.
81 void (*eo_update_clusters)(struct inode *inode,
82 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 inode *inode,
90 struct ocfs2_extent_tree *et,
91 struct ocfs2_extent_rec *rec);
92 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
95 * --------------------------------------------------------------
96 * The remaining are internal to ocfs2_extent_tree and don't have
101 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
104 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
107 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
108 * it exists. If it does not, et->et_max_leaf_clusters is set
109 * to 0 (unlimited). Optional.
111 void (*eo_fill_max_leaf_clusters)(struct inode *inode,
112 struct ocfs2_extent_tree *et);
117 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
120 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
121 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
123 static void ocfs2_dinode_update_clusters(struct inode *inode,
124 struct ocfs2_extent_tree *et,
126 static int ocfs2_dinode_insert_check(struct inode *inode,
127 struct ocfs2_extent_tree *et,
128 struct ocfs2_extent_rec *rec);
129 static int ocfs2_dinode_sanity_check(struct inode *inode,
130 struct ocfs2_extent_tree *et);
131 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
132 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
133 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
134 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
135 .eo_update_clusters = ocfs2_dinode_update_clusters,
136 .eo_insert_check = ocfs2_dinode_insert_check,
137 .eo_sanity_check = ocfs2_dinode_sanity_check,
138 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
141 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
144 struct ocfs2_dinode *di = et->et_object;
146 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
147 di->i_last_eb_blk = cpu_to_le64(blkno);
150 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
152 struct ocfs2_dinode *di = et->et_object;
154 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
155 return le64_to_cpu(di->i_last_eb_blk);
158 static void ocfs2_dinode_update_clusters(struct inode *inode,
159 struct ocfs2_extent_tree *et,
162 struct ocfs2_dinode *di = et->et_object;
164 le32_add_cpu(&di->i_clusters, clusters);
165 spin_lock(&OCFS2_I(inode)->ip_lock);
166 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
167 spin_unlock(&OCFS2_I(inode)->ip_lock);
170 static int ocfs2_dinode_insert_check(struct inode *inode,
171 struct ocfs2_extent_tree *et,
172 struct ocfs2_extent_rec *rec)
174 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
176 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
177 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
178 (OCFS2_I(inode)->ip_clusters != rec->e_cpos),
179 "Device %s, asking for sparse allocation: inode %llu, "
180 "cpos %u, clusters %u\n",
182 (unsigned long long)OCFS2_I(inode)->ip_blkno,
184 OCFS2_I(inode)->ip_clusters);
189 static int ocfs2_dinode_sanity_check(struct inode *inode,
190 struct ocfs2_extent_tree *et)
192 struct ocfs2_dinode *di = et->et_object;
194 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
195 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
200 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
202 struct ocfs2_dinode *di = et->et_object;
204 et->et_root_el = &di->id2.i_list;
208 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
210 struct ocfs2_xattr_value_root *xv = et->et_object;
212 et->et_root_el = &xv->xr_list;
215 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
218 struct ocfs2_xattr_value_root *xv =
219 (struct ocfs2_xattr_value_root *)et->et_object;
221 xv->xr_last_eb_blk = cpu_to_le64(blkno);
224 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
226 struct ocfs2_xattr_value_root *xv =
227 (struct ocfs2_xattr_value_root *) et->et_object;
229 return le64_to_cpu(xv->xr_last_eb_blk);
232 static void ocfs2_xattr_value_update_clusters(struct inode *inode,
233 struct ocfs2_extent_tree *et,
236 struct ocfs2_xattr_value_root *xv =
237 (struct ocfs2_xattr_value_root *)et->et_object;
239 le32_add_cpu(&xv->xr_clusters, clusters);
242 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
243 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
244 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
245 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
246 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
249 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
251 struct ocfs2_xattr_block *xb = et->et_object;
253 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
256 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
257 struct ocfs2_extent_tree *et)
259 et->et_max_leaf_clusters =
260 ocfs2_clusters_for_bytes(inode->i_sb,
261 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
264 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
267 struct ocfs2_xattr_block *xb = et->et_object;
268 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
270 xt->xt_last_eb_blk = cpu_to_le64(blkno);
273 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
275 struct ocfs2_xattr_block *xb = et->et_object;
276 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
278 return le64_to_cpu(xt->xt_last_eb_blk);
281 static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
282 struct ocfs2_extent_tree *et,
285 struct ocfs2_xattr_block *xb = et->et_object;
287 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
290 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
291 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
292 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
293 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
294 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
295 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
298 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
300 struct buffer_head *bh,
302 struct ocfs2_extent_tree_operations *ops)
307 obj = (void *)bh->b_data;
310 et->et_ops->eo_fill_root_el(et);
311 if (!et->et_ops->eo_fill_max_leaf_clusters)
312 et->et_max_leaf_clusters = 0;
314 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
317 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
319 struct buffer_head *bh)
321 __ocfs2_init_extent_tree(et, inode, bh, NULL, &ocfs2_dinode_et_ops);
324 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
326 struct buffer_head *bh)
328 __ocfs2_init_extent_tree(et, inode, bh, NULL,
329 &ocfs2_xattr_tree_et_ops);
332 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
334 struct buffer_head *bh,
335 struct ocfs2_xattr_value_root *xv)
337 __ocfs2_init_extent_tree(et, inode, bh, xv,
338 &ocfs2_xattr_value_et_ops);
341 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
344 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
347 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
349 return et->et_ops->eo_get_last_eb_blk(et);
352 static inline void ocfs2_et_update_clusters(struct inode *inode,
353 struct ocfs2_extent_tree *et,
356 et->et_ops->eo_update_clusters(inode, et, clusters);
359 static inline int ocfs2_et_insert_check(struct inode *inode,
360 struct ocfs2_extent_tree *et,
361 struct ocfs2_extent_rec *rec)
365 if (et->et_ops->eo_insert_check)
366 ret = et->et_ops->eo_insert_check(inode, et, rec);
370 static inline int ocfs2_et_sanity_check(struct inode *inode,
371 struct ocfs2_extent_tree *et)
375 if (et->et_ops->eo_sanity_check)
376 ret = et->et_ops->eo_sanity_check(inode, et);
380 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
381 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
382 struct ocfs2_extent_block *eb);
385 * Structures which describe a path through a btree, and functions to
388 * The idea here is to be as generic as possible with the tree
391 struct ocfs2_path_item {
392 struct buffer_head *bh;
393 struct ocfs2_extent_list *el;
396 #define OCFS2_MAX_PATH_DEPTH 5
400 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
403 #define path_root_bh(_path) ((_path)->p_node[0].bh)
404 #define path_root_el(_path) ((_path)->p_node[0].el)
405 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
406 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
407 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
410 * Reset the actual path elements so that we can re-use the structure
411 * to build another path. Generally, this involves freeing the buffer
414 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
416 int i, start = 0, depth = 0;
417 struct ocfs2_path_item *node;
422 for(i = start; i < path_num_items(path); i++) {
423 node = &path->p_node[i];
431 * Tree depth may change during truncate, or insert. If we're
432 * keeping the root extent list, then make sure that our path
433 * structure reflects the proper depth.
436 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
438 path->p_tree_depth = depth;
441 static void ocfs2_free_path(struct ocfs2_path *path)
444 ocfs2_reinit_path(path, 0);
450 * All the elements of src into dest. After this call, src could be freed
451 * without affecting dest.
453 * Both paths should have the same root. Any non-root elements of dest
456 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
460 BUG_ON(path_root_bh(dest) != path_root_bh(src));
461 BUG_ON(path_root_el(dest) != path_root_el(src));
463 ocfs2_reinit_path(dest, 1);
465 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
466 dest->p_node[i].bh = src->p_node[i].bh;
467 dest->p_node[i].el = src->p_node[i].el;
469 if (dest->p_node[i].bh)
470 get_bh(dest->p_node[i].bh);
475 * Make the *dest path the same as src and re-initialize src path to
478 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
482 BUG_ON(path_root_bh(dest) != path_root_bh(src));
484 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
485 brelse(dest->p_node[i].bh);
487 dest->p_node[i].bh = src->p_node[i].bh;
488 dest->p_node[i].el = src->p_node[i].el;
490 src->p_node[i].bh = NULL;
491 src->p_node[i].el = NULL;
496 * Insert an extent block at given index.
498 * This will not take an additional reference on eb_bh.
500 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
501 struct buffer_head *eb_bh)
503 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
506 * Right now, no root bh is an extent block, so this helps
507 * catch code errors with dinode trees. The assertion can be
508 * safely removed if we ever need to insert extent block
509 * structures at the root.
513 path->p_node[index].bh = eb_bh;
514 path->p_node[index].el = &eb->h_list;
517 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
518 struct ocfs2_extent_list *root_el)
520 struct ocfs2_path *path;
522 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
524 path = kzalloc(sizeof(*path), GFP_NOFS);
526 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
528 path_root_bh(path) = root_bh;
529 path_root_el(path) = root_el;
536 * Convenience function to journal all components in a path.
538 static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
539 struct ocfs2_path *path)
546 for(i = 0; i < path_num_items(path); i++) {
547 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
548 OCFS2_JOURNAL_ACCESS_WRITE);
560 * Return the index of the extent record which contains cluster #v_cluster.
561 * -1 is returned if it was not found.
563 * Should work fine on interior and exterior nodes.
565 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
569 struct ocfs2_extent_rec *rec;
570 u32 rec_end, rec_start, clusters;
572 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
573 rec = &el->l_recs[i];
575 rec_start = le32_to_cpu(rec->e_cpos);
576 clusters = ocfs2_rec_clusters(el, rec);
578 rec_end = rec_start + clusters;
580 if (v_cluster >= rec_start && v_cluster < rec_end) {
589 enum ocfs2_contig_type {
598 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
599 * ocfs2_extent_contig only work properly against leaf nodes!
601 static int ocfs2_block_extent_contig(struct super_block *sb,
602 struct ocfs2_extent_rec *ext,
605 u64 blk_end = le64_to_cpu(ext->e_blkno);
607 blk_end += ocfs2_clusters_to_blocks(sb,
608 le16_to_cpu(ext->e_leaf_clusters));
610 return blkno == blk_end;
613 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
614 struct ocfs2_extent_rec *right)
618 left_range = le32_to_cpu(left->e_cpos) +
619 le16_to_cpu(left->e_leaf_clusters);
621 return (left_range == le32_to_cpu(right->e_cpos));
624 static enum ocfs2_contig_type
625 ocfs2_extent_contig(struct inode *inode,
626 struct ocfs2_extent_rec *ext,
627 struct ocfs2_extent_rec *insert_rec)
629 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
632 * Refuse to coalesce extent records with different flag
633 * fields - we don't want to mix unwritten extents with user
636 if (ext->e_flags != insert_rec->e_flags)
639 if (ocfs2_extents_adjacent(ext, insert_rec) &&
640 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
643 blkno = le64_to_cpu(ext->e_blkno);
644 if (ocfs2_extents_adjacent(insert_rec, ext) &&
645 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
652 * NOTE: We can have pretty much any combination of contiguousness and
655 * The usefulness of APPEND_TAIL is more in that it lets us know that
656 * we'll have to update the path to that leaf.
658 enum ocfs2_append_type {
663 enum ocfs2_split_type {
669 struct ocfs2_insert_type {
670 enum ocfs2_split_type ins_split;
671 enum ocfs2_append_type ins_appending;
672 enum ocfs2_contig_type ins_contig;
673 int ins_contig_index;
677 struct ocfs2_merge_ctxt {
678 enum ocfs2_contig_type c_contig_type;
679 int c_has_empty_extent;
680 int c_split_covers_rec;
683 static int ocfs2_validate_extent_block(struct super_block *sb,
684 struct buffer_head *bh)
687 struct ocfs2_extent_block *eb =
688 (struct ocfs2_extent_block *)bh->b_data;
690 mlog(0, "Validating extent block %llu\n",
691 (unsigned long long)bh->b_blocknr);
693 BUG_ON(!buffer_uptodate(bh));
696 * If the ecc fails, we return the error but otherwise
697 * leave the filesystem running. We know any error is
698 * local to this block.
700 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
705 * Errors after here are fatal.
708 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
710 "Extent block #%llu has bad signature %.*s",
711 (unsigned long long)bh->b_blocknr, 7,
716 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
718 "Extent block #%llu has an invalid h_blkno "
720 (unsigned long long)bh->b_blocknr,
721 (unsigned long long)le64_to_cpu(eb->h_blkno));
725 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
727 "Extent block #%llu has an invalid "
728 "h_fs_generation of #%u",
729 (unsigned long long)bh->b_blocknr,
730 le32_to_cpu(eb->h_fs_generation));
737 int ocfs2_read_extent_block(struct inode *inode, u64 eb_blkno,
738 struct buffer_head **bh)
741 struct buffer_head *tmp = *bh;
743 rc = ocfs2_read_block(inode, eb_blkno, &tmp,
744 ocfs2_validate_extent_block);
746 /* If ocfs2_read_block() got us a new bh, pass it up. */
755 * How many free extents have we got before we need more meta data?
757 int ocfs2_num_free_extents(struct ocfs2_super *osb,
759 struct ocfs2_extent_tree *et)
762 struct ocfs2_extent_list *el = NULL;
763 struct ocfs2_extent_block *eb;
764 struct buffer_head *eb_bh = NULL;
770 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
773 retval = ocfs2_read_extent_block(inode, last_eb_blk, &eb_bh);
778 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
782 BUG_ON(el->l_tree_depth != 0);
784 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
792 /* expects array to already be allocated
794 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
797 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
801 struct ocfs2_alloc_context *meta_ac,
802 struct buffer_head *bhs[])
804 int count, status, i;
805 u16 suballoc_bit_start;
808 struct ocfs2_extent_block *eb;
813 while (count < wanted) {
814 status = ocfs2_claim_metadata(osb,
826 for(i = count; i < (num_got + count); i++) {
827 bhs[i] = sb_getblk(osb->sb, first_blkno);
828 if (bhs[i] == NULL) {
833 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
835 status = ocfs2_journal_access(handle, inode, bhs[i],
836 OCFS2_JOURNAL_ACCESS_CREATE);
842 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
843 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
844 /* Ok, setup the minimal stuff here. */
845 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
846 eb->h_blkno = cpu_to_le64(first_blkno);
847 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
848 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
849 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
851 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
853 suballoc_bit_start++;
856 /* We'll also be dirtied by the caller, so
857 * this isn't absolutely necessary. */
858 status = ocfs2_journal_dirty(handle, bhs[i]);
871 for(i = 0; i < wanted; i++) {
881 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
883 * Returns the sum of the rightmost extent rec logical offset and
886 * ocfs2_add_branch() uses this to determine what logical cluster
887 * value should be populated into the leftmost new branch records.
889 * ocfs2_shift_tree_depth() uses this to determine the # clusters
890 * value for the new topmost tree record.
892 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
896 i = le16_to_cpu(el->l_next_free_rec) - 1;
898 return le32_to_cpu(el->l_recs[i].e_cpos) +
899 ocfs2_rec_clusters(el, &el->l_recs[i]);
903 * Add an entire tree branch to our inode. eb_bh is the extent block
904 * to start at, if we don't want to start the branch at the dinode
907 * last_eb_bh is required as we have to update it's next_leaf pointer
908 * for the new last extent block.
910 * the new branch will be 'empty' in the sense that every block will
911 * contain a single record with cluster count == 0.
913 static int ocfs2_add_branch(struct ocfs2_super *osb,
916 struct ocfs2_extent_tree *et,
917 struct buffer_head *eb_bh,
918 struct buffer_head **last_eb_bh,
919 struct ocfs2_alloc_context *meta_ac)
921 int status, new_blocks, i;
922 u64 next_blkno, new_last_eb_blk;
923 struct buffer_head *bh;
924 struct buffer_head **new_eb_bhs = NULL;
925 struct ocfs2_extent_block *eb;
926 struct ocfs2_extent_list *eb_el;
927 struct ocfs2_extent_list *el;
932 BUG_ON(!last_eb_bh || !*last_eb_bh);
935 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
940 /* we never add a branch to a leaf. */
941 BUG_ON(!el->l_tree_depth);
943 new_blocks = le16_to_cpu(el->l_tree_depth);
945 /* allocate the number of new eb blocks we need */
946 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
954 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
955 meta_ac, new_eb_bhs);
961 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
962 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
964 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
965 * linked with the rest of the tree.
966 * conversly, new_eb_bhs[0] is the new bottommost leaf.
968 * when we leave the loop, new_last_eb_blk will point to the
969 * newest leaf, and next_blkno will point to the topmost extent
971 next_blkno = new_last_eb_blk = 0;
972 for(i = 0; i < new_blocks; i++) {
974 eb = (struct ocfs2_extent_block *) bh->b_data;
975 /* ocfs2_create_new_meta_bhs() should create it right! */
976 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
979 status = ocfs2_journal_access(handle, inode, bh,
980 OCFS2_JOURNAL_ACCESS_CREATE);
986 eb->h_next_leaf_blk = 0;
987 eb_el->l_tree_depth = cpu_to_le16(i);
988 eb_el->l_next_free_rec = cpu_to_le16(1);
990 * This actually counts as an empty extent as
993 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
994 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
996 * eb_el isn't always an interior node, but even leaf
997 * nodes want a zero'd flags and reserved field so
998 * this gets the whole 32 bits regardless of use.
1000 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
1001 if (!eb_el->l_tree_depth)
1002 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1004 status = ocfs2_journal_dirty(handle, bh);
1010 next_blkno = le64_to_cpu(eb->h_blkno);
1013 /* This is a bit hairy. We want to update up to three blocks
1014 * here without leaving any of them in an inconsistent state
1015 * in case of error. We don't have to worry about
1016 * journal_dirty erroring as it won't unless we've aborted the
1017 * handle (in which case we would never be here) so reserving
1018 * the write with journal_access is all we need to do. */
1019 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
1020 OCFS2_JOURNAL_ACCESS_WRITE);
1025 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
1026 OCFS2_JOURNAL_ACCESS_WRITE);
1032 status = ocfs2_journal_access(handle, inode, eb_bh,
1033 OCFS2_JOURNAL_ACCESS_WRITE);
1040 /* Link the new branch into the rest of the tree (el will
1041 * either be on the root_bh, or the extent block passed in. */
1042 i = le16_to_cpu(el->l_next_free_rec);
1043 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1044 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1045 el->l_recs[i].e_int_clusters = 0;
1046 le16_add_cpu(&el->l_next_free_rec, 1);
1048 /* fe needs a new last extent block pointer, as does the
1049 * next_leaf on the previously last-extent-block. */
1050 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1052 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1053 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1055 status = ocfs2_journal_dirty(handle, *last_eb_bh);
1058 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1062 status = ocfs2_journal_dirty(handle, eb_bh);
1068 * Some callers want to track the rightmost leaf so pass it
1071 brelse(*last_eb_bh);
1072 get_bh(new_eb_bhs[0]);
1073 *last_eb_bh = new_eb_bhs[0];
1078 for (i = 0; i < new_blocks; i++)
1079 brelse(new_eb_bhs[i]);
1088 * adds another level to the allocation tree.
1089 * returns back the new extent block so you can add a branch to it
1092 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
1094 struct inode *inode,
1095 struct ocfs2_extent_tree *et,
1096 struct ocfs2_alloc_context *meta_ac,
1097 struct buffer_head **ret_new_eb_bh)
1101 struct buffer_head *new_eb_bh = NULL;
1102 struct ocfs2_extent_block *eb;
1103 struct ocfs2_extent_list *root_el;
1104 struct ocfs2_extent_list *eb_el;
1108 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1115 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1116 /* ocfs2_create_new_meta_bhs() should create it right! */
1117 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1119 eb_el = &eb->h_list;
1120 root_el = et->et_root_el;
1122 status = ocfs2_journal_access(handle, inode, new_eb_bh,
1123 OCFS2_JOURNAL_ACCESS_CREATE);
1129 /* copy the root extent list data into the new extent block */
1130 eb_el->l_tree_depth = root_el->l_tree_depth;
1131 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1132 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1133 eb_el->l_recs[i] = root_el->l_recs[i];
1135 status = ocfs2_journal_dirty(handle, new_eb_bh);
1141 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
1142 OCFS2_JOURNAL_ACCESS_WRITE);
1148 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1150 /* update root_bh now */
1151 le16_add_cpu(&root_el->l_tree_depth, 1);
1152 root_el->l_recs[0].e_cpos = 0;
1153 root_el->l_recs[0].e_blkno = eb->h_blkno;
1154 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1155 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1156 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1157 root_el->l_next_free_rec = cpu_to_le16(1);
1159 /* If this is our 1st tree depth shift, then last_eb_blk
1160 * becomes the allocated extent block */
1161 if (root_el->l_tree_depth == cpu_to_le16(1))
1162 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1164 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1170 *ret_new_eb_bh = new_eb_bh;
1181 * Should only be called when there is no space left in any of the
1182 * leaf nodes. What we want to do is find the lowest tree depth
1183 * non-leaf extent block with room for new records. There are three
1184 * valid results of this search:
1186 * 1) a lowest extent block is found, then we pass it back in
1187 * *lowest_eb_bh and return '0'
1189 * 2) the search fails to find anything, but the root_el has room. We
1190 * pass NULL back in *lowest_eb_bh, but still return '0'
1192 * 3) the search fails to find anything AND the root_el is full, in
1193 * which case we return > 0
1195 * return status < 0 indicates an error.
1197 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1198 struct inode *inode,
1199 struct ocfs2_extent_tree *et,
1200 struct buffer_head **target_bh)
1204 struct ocfs2_extent_block *eb;
1205 struct ocfs2_extent_list *el;
1206 struct buffer_head *bh = NULL;
1207 struct buffer_head *lowest_bh = NULL;
1213 el = et->et_root_el;
1215 while(le16_to_cpu(el->l_tree_depth) > 1) {
1216 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1217 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
1218 "extent list (next_free_rec == 0)",
1219 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1223 i = le16_to_cpu(el->l_next_free_rec) - 1;
1224 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1226 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
1227 "list where extent # %d has no physical "
1229 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
1237 status = ocfs2_read_extent_block(inode, blkno, &bh);
1243 eb = (struct ocfs2_extent_block *) bh->b_data;
1246 if (le16_to_cpu(el->l_next_free_rec) <
1247 le16_to_cpu(el->l_count)) {
1254 /* If we didn't find one and the fe doesn't have any room,
1255 * then return '1' */
1256 el = et->et_root_el;
1257 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1260 *target_bh = lowest_bh;
1269 * Grow a b-tree so that it has more records.
1271 * We might shift the tree depth in which case existing paths should
1272 * be considered invalid.
1274 * Tree depth after the grow is returned via *final_depth.
1276 * *last_eb_bh will be updated by ocfs2_add_branch().
1278 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
1279 struct ocfs2_extent_tree *et, int *final_depth,
1280 struct buffer_head **last_eb_bh,
1281 struct ocfs2_alloc_context *meta_ac)
1284 struct ocfs2_extent_list *el = et->et_root_el;
1285 int depth = le16_to_cpu(el->l_tree_depth);
1286 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1287 struct buffer_head *bh = NULL;
1289 BUG_ON(meta_ac == NULL);
1291 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
1298 /* We traveled all the way to the bottom of the allocation tree
1299 * and didn't find room for any more extents - we need to add
1300 * another tree level */
1303 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1305 /* ocfs2_shift_tree_depth will return us a buffer with
1306 * the new extent block (so we can pass that to
1307 * ocfs2_add_branch). */
1308 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
1317 * Special case: we have room now if we shifted from
1318 * tree_depth 0, so no more work needs to be done.
1320 * We won't be calling add_branch, so pass
1321 * back *last_eb_bh as the new leaf. At depth
1322 * zero, it should always be null so there's
1323 * no reason to brelse.
1325 BUG_ON(*last_eb_bh);
1332 /* call ocfs2_add_branch to add the final part of the tree with
1334 mlog(0, "add branch. bh = %p\n", bh);
1335 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
1344 *final_depth = depth;
1350 * This function will discard the rightmost extent record.
1352 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1354 int next_free = le16_to_cpu(el->l_next_free_rec);
1355 int count = le16_to_cpu(el->l_count);
1356 unsigned int num_bytes;
1359 /* This will cause us to go off the end of our extent list. */
1360 BUG_ON(next_free >= count);
1362 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1364 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1367 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1368 struct ocfs2_extent_rec *insert_rec)
1370 int i, insert_index, next_free, has_empty, num_bytes;
1371 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1372 struct ocfs2_extent_rec *rec;
1374 next_free = le16_to_cpu(el->l_next_free_rec);
1375 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1379 /* The tree code before us didn't allow enough room in the leaf. */
1380 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1383 * The easiest way to approach this is to just remove the
1384 * empty extent and temporarily decrement next_free.
1388 * If next_free was 1 (only an empty extent), this
1389 * loop won't execute, which is fine. We still want
1390 * the decrement above to happen.
1392 for(i = 0; i < (next_free - 1); i++)
1393 el->l_recs[i] = el->l_recs[i+1];
1399 * Figure out what the new record index should be.
1401 for(i = 0; i < next_free; i++) {
1402 rec = &el->l_recs[i];
1404 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1409 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1410 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1412 BUG_ON(insert_index < 0);
1413 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1414 BUG_ON(insert_index > next_free);
1417 * No need to memmove if we're just adding to the tail.
1419 if (insert_index != next_free) {
1420 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1422 num_bytes = next_free - insert_index;
1423 num_bytes *= sizeof(struct ocfs2_extent_rec);
1424 memmove(&el->l_recs[insert_index + 1],
1425 &el->l_recs[insert_index],
1430 * Either we had an empty extent, and need to re-increment or
1431 * there was no empty extent on a non full rightmost leaf node,
1432 * in which case we still need to increment.
1435 el->l_next_free_rec = cpu_to_le16(next_free);
1437 * Make sure none of the math above just messed up our tree.
1439 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1441 el->l_recs[insert_index] = *insert_rec;
1445 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1447 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1449 BUG_ON(num_recs == 0);
1451 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1453 size = num_recs * sizeof(struct ocfs2_extent_rec);
1454 memmove(&el->l_recs[0], &el->l_recs[1], size);
1455 memset(&el->l_recs[num_recs], 0,
1456 sizeof(struct ocfs2_extent_rec));
1457 el->l_next_free_rec = cpu_to_le16(num_recs);
1462 * Create an empty extent record .
1464 * l_next_free_rec may be updated.
1466 * If an empty extent already exists do nothing.
1468 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1470 int next_free = le16_to_cpu(el->l_next_free_rec);
1472 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1477 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1480 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1481 "Asked to create an empty extent in a full list:\n"
1482 "count = %u, tree depth = %u",
1483 le16_to_cpu(el->l_count),
1484 le16_to_cpu(el->l_tree_depth));
1486 ocfs2_shift_records_right(el);
1489 le16_add_cpu(&el->l_next_free_rec, 1);
1490 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1494 * For a rotation which involves two leaf nodes, the "root node" is
1495 * the lowest level tree node which contains a path to both leafs. This
1496 * resulting set of information can be used to form a complete "subtree"
1498 * This function is passed two full paths from the dinode down to a
1499 * pair of adjacent leaves. It's task is to figure out which path
1500 * index contains the subtree root - this can be the root index itself
1501 * in a worst-case rotation.
1503 * The array index of the subtree root is passed back.
1505 static int ocfs2_find_subtree_root(struct inode *inode,
1506 struct ocfs2_path *left,
1507 struct ocfs2_path *right)
1512 * Check that the caller passed in two paths from the same tree.
1514 BUG_ON(path_root_bh(left) != path_root_bh(right));
1520 * The caller didn't pass two adjacent paths.
1522 mlog_bug_on_msg(i > left->p_tree_depth,
1523 "Inode %lu, left depth %u, right depth %u\n"
1524 "left leaf blk %llu, right leaf blk %llu\n",
1525 inode->i_ino, left->p_tree_depth,
1526 right->p_tree_depth,
1527 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1528 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1529 } while (left->p_node[i].bh->b_blocknr ==
1530 right->p_node[i].bh->b_blocknr);
1535 typedef void (path_insert_t)(void *, struct buffer_head *);
1538 * Traverse a btree path in search of cpos, starting at root_el.
1540 * This code can be called with a cpos larger than the tree, in which
1541 * case it will return the rightmost path.
1543 static int __ocfs2_find_path(struct inode *inode,
1544 struct ocfs2_extent_list *root_el, u32 cpos,
1545 path_insert_t *func, void *data)
1550 struct buffer_head *bh = NULL;
1551 struct ocfs2_extent_block *eb;
1552 struct ocfs2_extent_list *el;
1553 struct ocfs2_extent_rec *rec;
1554 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1557 while (el->l_tree_depth) {
1558 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1559 ocfs2_error(inode->i_sb,
1560 "Inode %llu has empty extent list at "
1562 (unsigned long long)oi->ip_blkno,
1563 le16_to_cpu(el->l_tree_depth));
1569 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1570 rec = &el->l_recs[i];
1573 * In the case that cpos is off the allocation
1574 * tree, this should just wind up returning the
1577 range = le32_to_cpu(rec->e_cpos) +
1578 ocfs2_rec_clusters(el, rec);
1579 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1583 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1585 ocfs2_error(inode->i_sb,
1586 "Inode %llu has bad blkno in extent list "
1587 "at depth %u (index %d)\n",
1588 (unsigned long long)oi->ip_blkno,
1589 le16_to_cpu(el->l_tree_depth), i);
1596 ret = ocfs2_read_extent_block(inode, blkno, &bh);
1602 eb = (struct ocfs2_extent_block *) bh->b_data;
1605 if (le16_to_cpu(el->l_next_free_rec) >
1606 le16_to_cpu(el->l_count)) {
1607 ocfs2_error(inode->i_sb,
1608 "Inode %llu has bad count in extent list "
1609 "at block %llu (next free=%u, count=%u)\n",
1610 (unsigned long long)oi->ip_blkno,
1611 (unsigned long long)bh->b_blocknr,
1612 le16_to_cpu(el->l_next_free_rec),
1613 le16_to_cpu(el->l_count));
1624 * Catch any trailing bh that the loop didn't handle.
1632 * Given an initialized path (that is, it has a valid root extent
1633 * list), this function will traverse the btree in search of the path
1634 * which would contain cpos.
1636 * The path traveled is recorded in the path structure.
1638 * Note that this will not do any comparisons on leaf node extent
1639 * records, so it will work fine in the case that we just added a tree
1642 struct find_path_data {
1644 struct ocfs2_path *path;
1646 static void find_path_ins(void *data, struct buffer_head *bh)
1648 struct find_path_data *fp = data;
1651 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1654 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1657 struct find_path_data data;
1661 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1662 find_path_ins, &data);
1665 static void find_leaf_ins(void *data, struct buffer_head *bh)
1667 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1668 struct ocfs2_extent_list *el = &eb->h_list;
1669 struct buffer_head **ret = data;
1671 /* We want to retain only the leaf block. */
1672 if (le16_to_cpu(el->l_tree_depth) == 0) {
1678 * Find the leaf block in the tree which would contain cpos. No
1679 * checking of the actual leaf is done.
1681 * Some paths want to call this instead of allocating a path structure
1682 * and calling ocfs2_find_path().
1684 * This function doesn't handle non btree extent lists.
1686 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1687 u32 cpos, struct buffer_head **leaf_bh)
1690 struct buffer_head *bh = NULL;
1692 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1704 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1706 * Basically, we've moved stuff around at the bottom of the tree and
1707 * we need to fix up the extent records above the changes to reflect
1710 * left_rec: the record on the left.
1711 * left_child_el: is the child list pointed to by left_rec
1712 * right_rec: the record to the right of left_rec
1713 * right_child_el: is the child list pointed to by right_rec
1715 * By definition, this only works on interior nodes.
1717 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1718 struct ocfs2_extent_list *left_child_el,
1719 struct ocfs2_extent_rec *right_rec,
1720 struct ocfs2_extent_list *right_child_el)
1722 u32 left_clusters, right_end;
1725 * Interior nodes never have holes. Their cpos is the cpos of
1726 * the leftmost record in their child list. Their cluster
1727 * count covers the full theoretical range of their child list
1728 * - the range between their cpos and the cpos of the record
1729 * immediately to their right.
1731 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1732 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1733 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1734 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1736 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1737 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1740 * Calculate the rightmost cluster count boundary before
1741 * moving cpos - we will need to adjust clusters after
1742 * updating e_cpos to keep the same highest cluster count.
1744 right_end = le32_to_cpu(right_rec->e_cpos);
1745 right_end += le32_to_cpu(right_rec->e_int_clusters);
1747 right_rec->e_cpos = left_rec->e_cpos;
1748 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1750 right_end -= le32_to_cpu(right_rec->e_cpos);
1751 right_rec->e_int_clusters = cpu_to_le32(right_end);
1755 * Adjust the adjacent root node records involved in a
1756 * rotation. left_el_blkno is passed in as a key so that we can easily
1757 * find it's index in the root list.
1759 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1760 struct ocfs2_extent_list *left_el,
1761 struct ocfs2_extent_list *right_el,
1766 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1767 le16_to_cpu(left_el->l_tree_depth));
1769 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1770 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1775 * The path walking code should have never returned a root and
1776 * two paths which are not adjacent.
1778 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1780 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1781 &root_el->l_recs[i + 1], right_el);
1785 * We've changed a leaf block (in right_path) and need to reflect that
1786 * change back up the subtree.
1788 * This happens in multiple places:
1789 * - When we've moved an extent record from the left path leaf to the right
1790 * path leaf to make room for an empty extent in the left path leaf.
1791 * - When our insert into the right path leaf is at the leftmost edge
1792 * and requires an update of the path immediately to it's left. This
1793 * can occur at the end of some types of rotation and appending inserts.
1794 * - When we've adjusted the last extent record in the left path leaf and the
1795 * 1st extent record in the right path leaf during cross extent block merge.
1797 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1798 struct ocfs2_path *left_path,
1799 struct ocfs2_path *right_path,
1803 struct ocfs2_extent_list *el, *left_el, *right_el;
1804 struct ocfs2_extent_rec *left_rec, *right_rec;
1805 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1808 * Update the counts and position values within all the
1809 * interior nodes to reflect the leaf rotation we just did.
1811 * The root node is handled below the loop.
1813 * We begin the loop with right_el and left_el pointing to the
1814 * leaf lists and work our way up.
1816 * NOTE: within this loop, left_el and right_el always refer
1817 * to the *child* lists.
1819 left_el = path_leaf_el(left_path);
1820 right_el = path_leaf_el(right_path);
1821 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1822 mlog(0, "Adjust records at index %u\n", i);
1825 * One nice property of knowing that all of these
1826 * nodes are below the root is that we only deal with
1827 * the leftmost right node record and the rightmost
1830 el = left_path->p_node[i].el;
1831 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1832 left_rec = &el->l_recs[idx];
1834 el = right_path->p_node[i].el;
1835 right_rec = &el->l_recs[0];
1837 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1840 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1844 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1849 * Setup our list pointers now so that the current
1850 * parents become children in the next iteration.
1852 left_el = left_path->p_node[i].el;
1853 right_el = right_path->p_node[i].el;
1857 * At the root node, adjust the two adjacent records which
1858 * begin our path to the leaves.
1861 el = left_path->p_node[subtree_index].el;
1862 left_el = left_path->p_node[subtree_index + 1].el;
1863 right_el = right_path->p_node[subtree_index + 1].el;
1865 ocfs2_adjust_root_records(el, left_el, right_el,
1866 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1868 root_bh = left_path->p_node[subtree_index].bh;
1870 ret = ocfs2_journal_dirty(handle, root_bh);
1875 static int ocfs2_rotate_subtree_right(struct inode *inode,
1877 struct ocfs2_path *left_path,
1878 struct ocfs2_path *right_path,
1882 struct buffer_head *right_leaf_bh;
1883 struct buffer_head *left_leaf_bh = NULL;
1884 struct buffer_head *root_bh;
1885 struct ocfs2_extent_list *right_el, *left_el;
1886 struct ocfs2_extent_rec move_rec;
1888 left_leaf_bh = path_leaf_bh(left_path);
1889 left_el = path_leaf_el(left_path);
1891 if (left_el->l_next_free_rec != left_el->l_count) {
1892 ocfs2_error(inode->i_sb,
1893 "Inode %llu has non-full interior leaf node %llu"
1895 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1896 (unsigned long long)left_leaf_bh->b_blocknr,
1897 le16_to_cpu(left_el->l_next_free_rec));
1902 * This extent block may already have an empty record, so we
1903 * return early if so.
1905 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1908 root_bh = left_path->p_node[subtree_index].bh;
1909 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1911 ret = ocfs2_journal_access(handle, inode, root_bh,
1912 OCFS2_JOURNAL_ACCESS_WRITE);
1918 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1919 ret = ocfs2_journal_access(handle, inode,
1920 right_path->p_node[i].bh,
1921 OCFS2_JOURNAL_ACCESS_WRITE);
1927 ret = ocfs2_journal_access(handle, inode,
1928 left_path->p_node[i].bh,
1929 OCFS2_JOURNAL_ACCESS_WRITE);
1936 right_leaf_bh = path_leaf_bh(right_path);
1937 right_el = path_leaf_el(right_path);
1939 /* This is a code error, not a disk corruption. */
1940 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1941 "because rightmost leaf block %llu is empty\n",
1942 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1943 (unsigned long long)right_leaf_bh->b_blocknr);
1945 ocfs2_create_empty_extent(right_el);
1947 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1953 /* Do the copy now. */
1954 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1955 move_rec = left_el->l_recs[i];
1956 right_el->l_recs[0] = move_rec;
1959 * Clear out the record we just copied and shift everything
1960 * over, leaving an empty extent in the left leaf.
1962 * We temporarily subtract from next_free_rec so that the
1963 * shift will lose the tail record (which is now defunct).
1965 le16_add_cpu(&left_el->l_next_free_rec, -1);
1966 ocfs2_shift_records_right(left_el);
1967 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1968 le16_add_cpu(&left_el->l_next_free_rec, 1);
1970 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1976 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1984 * Given a full path, determine what cpos value would return us a path
1985 * containing the leaf immediately to the left of the current one.
1987 * Will return zero if the path passed in is already the leftmost path.
1989 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1990 struct ocfs2_path *path, u32 *cpos)
1994 struct ocfs2_extent_list *el;
1996 BUG_ON(path->p_tree_depth == 0);
2000 blkno = path_leaf_bh(path)->b_blocknr;
2002 /* Start at the tree node just above the leaf and work our way up. */
2003 i = path->p_tree_depth - 1;
2005 el = path->p_node[i].el;
2008 * Find the extent record just before the one in our
2011 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2012 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2016 * We've determined that the
2017 * path specified is already
2018 * the leftmost one - return a
2024 * The leftmost record points to our
2025 * leaf - we need to travel up the
2031 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2032 *cpos = *cpos + ocfs2_rec_clusters(el,
2033 &el->l_recs[j - 1]);
2040 * If we got here, we never found a valid node where
2041 * the tree indicated one should be.
2044 "Invalid extent tree at extent block %llu\n",
2045 (unsigned long long)blkno);
2050 blkno = path->p_node[i].bh->b_blocknr;
2059 * Extend the transaction by enough credits to complete the rotation,
2060 * and still leave at least the original number of credits allocated
2061 * to this transaction.
2063 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2065 struct ocfs2_path *path)
2067 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2069 if (handle->h_buffer_credits < credits)
2070 return ocfs2_extend_trans(handle, credits);
2076 * Trap the case where we're inserting into the theoretical range past
2077 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2078 * whose cpos is less than ours into the right leaf.
2080 * It's only necessary to look at the rightmost record of the left
2081 * leaf because the logic that calls us should ensure that the
2082 * theoretical ranges in the path components above the leaves are
2085 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2088 struct ocfs2_extent_list *left_el;
2089 struct ocfs2_extent_rec *rec;
2092 left_el = path_leaf_el(left_path);
2093 next_free = le16_to_cpu(left_el->l_next_free_rec);
2094 rec = &left_el->l_recs[next_free - 1];
2096 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2101 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2103 int next_free = le16_to_cpu(el->l_next_free_rec);
2105 struct ocfs2_extent_rec *rec;
2110 rec = &el->l_recs[0];
2111 if (ocfs2_is_empty_extent(rec)) {
2115 rec = &el->l_recs[1];
2118 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2119 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2125 * Rotate all the records in a btree right one record, starting at insert_cpos.
2127 * The path to the rightmost leaf should be passed in.
2129 * The array is assumed to be large enough to hold an entire path (tree depth).
2131 * Upon succesful return from this function:
2133 * - The 'right_path' array will contain a path to the leaf block
2134 * whose range contains e_cpos.
2135 * - That leaf block will have a single empty extent in list index 0.
2136 * - In the case that the rotation requires a post-insert update,
2137 * *ret_left_path will contain a valid path which can be passed to
2138 * ocfs2_insert_path().
2140 static int ocfs2_rotate_tree_right(struct inode *inode,
2142 enum ocfs2_split_type split,
2144 struct ocfs2_path *right_path,
2145 struct ocfs2_path **ret_left_path)
2147 int ret, start, orig_credits = handle->h_buffer_credits;
2149 struct ocfs2_path *left_path = NULL;
2151 *ret_left_path = NULL;
2153 left_path = ocfs2_new_path(path_root_bh(right_path),
2154 path_root_el(right_path));
2161 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2167 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2170 * What we want to do here is:
2172 * 1) Start with the rightmost path.
2174 * 2) Determine a path to the leaf block directly to the left
2177 * 3) Determine the 'subtree root' - the lowest level tree node
2178 * which contains a path to both leaves.
2180 * 4) Rotate the subtree.
2182 * 5) Find the next subtree by considering the left path to be
2183 * the new right path.
2185 * The check at the top of this while loop also accepts
2186 * insert_cpos == cpos because cpos is only a _theoretical_
2187 * value to get us the left path - insert_cpos might very well
2188 * be filling that hole.
2190 * Stop at a cpos of '0' because we either started at the
2191 * leftmost branch (i.e., a tree with one branch and a
2192 * rotation inside of it), or we've gone as far as we can in
2193 * rotating subtrees.
2195 while (cpos && insert_cpos <= cpos) {
2196 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2199 ret = ocfs2_find_path(inode, left_path, cpos);
2205 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2206 path_leaf_bh(right_path),
2207 "Inode %lu: error during insert of %u "
2208 "(left path cpos %u) results in two identical "
2209 "paths ending at %llu\n",
2210 inode->i_ino, insert_cpos, cpos,
2211 (unsigned long long)
2212 path_leaf_bh(left_path)->b_blocknr);
2214 if (split == SPLIT_NONE &&
2215 ocfs2_rotate_requires_path_adjustment(left_path,
2219 * We've rotated the tree as much as we
2220 * should. The rest is up to
2221 * ocfs2_insert_path() to complete, after the
2222 * record insertion. We indicate this
2223 * situation by returning the left path.
2225 * The reason we don't adjust the records here
2226 * before the record insert is that an error
2227 * later might break the rule where a parent
2228 * record e_cpos will reflect the actual
2229 * e_cpos of the 1st nonempty record of the
2232 *ret_left_path = left_path;
2236 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2238 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2240 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2241 right_path->p_tree_depth);
2243 ret = ocfs2_extend_rotate_transaction(handle, start,
2244 orig_credits, right_path);
2250 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2257 if (split != SPLIT_NONE &&
2258 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2261 * A rotate moves the rightmost left leaf
2262 * record over to the leftmost right leaf
2263 * slot. If we're doing an extent split
2264 * instead of a real insert, then we have to
2265 * check that the extent to be split wasn't
2266 * just moved over. If it was, then we can
2267 * exit here, passing left_path back -
2268 * ocfs2_split_extent() is smart enough to
2269 * search both leaves.
2271 *ret_left_path = left_path;
2276 * There is no need to re-read the next right path
2277 * as we know that it'll be our current left
2278 * path. Optimize by copying values instead.
2280 ocfs2_mv_path(right_path, left_path);
2282 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2291 ocfs2_free_path(left_path);
2297 static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2298 struct ocfs2_path *path)
2301 struct ocfs2_extent_rec *rec;
2302 struct ocfs2_extent_list *el;
2303 struct ocfs2_extent_block *eb;
2306 /* Path should always be rightmost. */
2307 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2308 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2311 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2312 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2313 rec = &el->l_recs[idx];
2314 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2316 for (i = 0; i < path->p_tree_depth; i++) {
2317 el = path->p_node[i].el;
2318 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2319 rec = &el->l_recs[idx];
2321 rec->e_int_clusters = cpu_to_le32(range);
2322 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2324 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2328 static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2329 struct ocfs2_cached_dealloc_ctxt *dealloc,
2330 struct ocfs2_path *path, int unlink_start)
2333 struct ocfs2_extent_block *eb;
2334 struct ocfs2_extent_list *el;
2335 struct buffer_head *bh;
2337 for(i = unlink_start; i < path_num_items(path); i++) {
2338 bh = path->p_node[i].bh;
2340 eb = (struct ocfs2_extent_block *)bh->b_data;
2342 * Not all nodes might have had their final count
2343 * decremented by the caller - handle this here.
2346 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2348 "Inode %llu, attempted to remove extent block "
2349 "%llu with %u records\n",
2350 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2351 (unsigned long long)le64_to_cpu(eb->h_blkno),
2352 le16_to_cpu(el->l_next_free_rec));
2354 ocfs2_journal_dirty(handle, bh);
2355 ocfs2_remove_from_cache(inode, bh);
2359 el->l_next_free_rec = 0;
2360 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2362 ocfs2_journal_dirty(handle, bh);
2364 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2368 ocfs2_remove_from_cache(inode, bh);
2372 static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2373 struct ocfs2_path *left_path,
2374 struct ocfs2_path *right_path,
2376 struct ocfs2_cached_dealloc_ctxt *dealloc)
2379 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2380 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2381 struct ocfs2_extent_list *el;
2382 struct ocfs2_extent_block *eb;
2384 el = path_leaf_el(left_path);
2386 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2388 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2389 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2392 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2394 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2395 le16_add_cpu(&root_el->l_next_free_rec, -1);
2397 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2398 eb->h_next_leaf_blk = 0;
2400 ocfs2_journal_dirty(handle, root_bh);
2401 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2403 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2407 static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2408 struct ocfs2_path *left_path,
2409 struct ocfs2_path *right_path,
2411 struct ocfs2_cached_dealloc_ctxt *dealloc,
2413 struct ocfs2_extent_tree *et)
2415 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2416 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2417 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2418 struct ocfs2_extent_block *eb;
2422 right_leaf_el = path_leaf_el(right_path);
2423 left_leaf_el = path_leaf_el(left_path);
2424 root_bh = left_path->p_node[subtree_index].bh;
2425 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2427 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2430 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2431 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2433 * It's legal for us to proceed if the right leaf is
2434 * the rightmost one and it has an empty extent. There
2435 * are two cases to handle - whether the leaf will be
2436 * empty after removal or not. If the leaf isn't empty
2437 * then just remove the empty extent up front. The
2438 * next block will handle empty leaves by flagging
2441 * Non rightmost leaves will throw -EAGAIN and the
2442 * caller can manually move the subtree and retry.
2445 if (eb->h_next_leaf_blk != 0ULL)
2448 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2449 ret = ocfs2_journal_access(handle, inode,
2450 path_leaf_bh(right_path),
2451 OCFS2_JOURNAL_ACCESS_WRITE);
2457 ocfs2_remove_empty_extent(right_leaf_el);
2459 right_has_empty = 1;
2462 if (eb->h_next_leaf_blk == 0ULL &&
2463 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2465 * We have to update i_last_eb_blk during the meta
2468 ret = ocfs2_journal_access(handle, inode, et_root_bh,
2469 OCFS2_JOURNAL_ACCESS_WRITE);
2475 del_right_subtree = 1;
2479 * Getting here with an empty extent in the right path implies
2480 * that it's the rightmost path and will be deleted.
2482 BUG_ON(right_has_empty && !del_right_subtree);
2484 ret = ocfs2_journal_access(handle, inode, root_bh,
2485 OCFS2_JOURNAL_ACCESS_WRITE);
2491 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2492 ret = ocfs2_journal_access(handle, inode,
2493 right_path->p_node[i].bh,
2494 OCFS2_JOURNAL_ACCESS_WRITE);
2500 ret = ocfs2_journal_access(handle, inode,
2501 left_path->p_node[i].bh,
2502 OCFS2_JOURNAL_ACCESS_WRITE);
2509 if (!right_has_empty) {
2511 * Only do this if we're moving a real
2512 * record. Otherwise, the action is delayed until
2513 * after removal of the right path in which case we
2514 * can do a simple shift to remove the empty extent.
2516 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2517 memset(&right_leaf_el->l_recs[0], 0,
2518 sizeof(struct ocfs2_extent_rec));
2520 if (eb->h_next_leaf_blk == 0ULL) {
2522 * Move recs over to get rid of empty extent, decrease
2523 * next_free. This is allowed to remove the last
2524 * extent in our leaf (setting l_next_free_rec to
2525 * zero) - the delete code below won't care.
2527 ocfs2_remove_empty_extent(right_leaf_el);
2530 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2533 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2537 if (del_right_subtree) {
2538 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2539 subtree_index, dealloc);
2540 ocfs2_update_edge_lengths(inode, handle, left_path);
2542 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2543 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2546 * Removal of the extent in the left leaf was skipped
2547 * above so we could delete the right path
2550 if (right_has_empty)
2551 ocfs2_remove_empty_extent(left_leaf_el);
2553 ret = ocfs2_journal_dirty(handle, et_root_bh);
2559 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2567 * Given a full path, determine what cpos value would return us a path
2568 * containing the leaf immediately to the right of the current one.
2570 * Will return zero if the path passed in is already the rightmost path.
2572 * This looks similar, but is subtly different to
2573 * ocfs2_find_cpos_for_left_leaf().
2575 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2576 struct ocfs2_path *path, u32 *cpos)
2580 struct ocfs2_extent_list *el;
2584 if (path->p_tree_depth == 0)
2587 blkno = path_leaf_bh(path)->b_blocknr;
2589 /* Start at the tree node just above the leaf and work our way up. */
2590 i = path->p_tree_depth - 1;
2594 el = path->p_node[i].el;
2597 * Find the extent record just after the one in our
2600 next_free = le16_to_cpu(el->l_next_free_rec);
2601 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2602 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2603 if (j == (next_free - 1)) {
2606 * We've determined that the
2607 * path specified is already
2608 * the rightmost one - return a
2614 * The rightmost record points to our
2615 * leaf - we need to travel up the
2621 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2627 * If we got here, we never found a valid node where
2628 * the tree indicated one should be.
2631 "Invalid extent tree at extent block %llu\n",
2632 (unsigned long long)blkno);
2637 blkno = path->p_node[i].bh->b_blocknr;
2645 static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2647 struct buffer_head *bh,
2648 struct ocfs2_extent_list *el)
2652 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2655 ret = ocfs2_journal_access(handle, inode, bh,
2656 OCFS2_JOURNAL_ACCESS_WRITE);
2662 ocfs2_remove_empty_extent(el);
2664 ret = ocfs2_journal_dirty(handle, bh);
2672 static int __ocfs2_rotate_tree_left(struct inode *inode,
2673 handle_t *handle, int orig_credits,
2674 struct ocfs2_path *path,
2675 struct ocfs2_cached_dealloc_ctxt *dealloc,
2676 struct ocfs2_path **empty_extent_path,
2677 struct ocfs2_extent_tree *et)
2679 int ret, subtree_root, deleted;
2681 struct ocfs2_path *left_path = NULL;
2682 struct ocfs2_path *right_path = NULL;
2684 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2686 *empty_extent_path = NULL;
2688 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2695 left_path = ocfs2_new_path(path_root_bh(path),
2696 path_root_el(path));
2703 ocfs2_cp_path(left_path, path);
2705 right_path = ocfs2_new_path(path_root_bh(path),
2706 path_root_el(path));
2713 while (right_cpos) {
2714 ret = ocfs2_find_path(inode, right_path, right_cpos);
2720 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2723 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2725 (unsigned long long)
2726 right_path->p_node[subtree_root].bh->b_blocknr,
2727 right_path->p_tree_depth);
2729 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2730 orig_credits, left_path);
2737 * Caller might still want to make changes to the
2738 * tree root, so re-add it to the journal here.
2740 ret = ocfs2_journal_access(handle, inode,
2741 path_root_bh(left_path),
2742 OCFS2_JOURNAL_ACCESS_WRITE);
2748 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2749 right_path, subtree_root,
2750 dealloc, &deleted, et);
2751 if (ret == -EAGAIN) {
2753 * The rotation has to temporarily stop due to
2754 * the right subtree having an empty
2755 * extent. Pass it back to the caller for a
2758 *empty_extent_path = right_path;
2768 * The subtree rotate might have removed records on
2769 * the rightmost edge. If so, then rotation is
2775 ocfs2_mv_path(left_path, right_path);
2777 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2786 ocfs2_free_path(right_path);
2787 ocfs2_free_path(left_path);
2792 static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2793 struct ocfs2_path *path,
2794 struct ocfs2_cached_dealloc_ctxt *dealloc,
2795 struct ocfs2_extent_tree *et)
2797 int ret, subtree_index;
2799 struct ocfs2_path *left_path = NULL;
2800 struct ocfs2_extent_block *eb;
2801 struct ocfs2_extent_list *el;
2804 ret = ocfs2_et_sanity_check(inode, et);
2808 * There's two ways we handle this depending on
2809 * whether path is the only existing one.
2811 ret = ocfs2_extend_rotate_transaction(handle, 0,
2812 handle->h_buffer_credits,
2819 ret = ocfs2_journal_access_path(inode, handle, path);
2825 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2833 * We have a path to the left of this one - it needs
2836 left_path = ocfs2_new_path(path_root_bh(path),
2837 path_root_el(path));
2844 ret = ocfs2_find_path(inode, left_path, cpos);
2850 ret = ocfs2_journal_access_path(inode, handle, left_path);
2856 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2858 ocfs2_unlink_subtree(inode, handle, left_path, path,
2859 subtree_index, dealloc);
2860 ocfs2_update_edge_lengths(inode, handle, left_path);
2862 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2863 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2866 * 'path' is also the leftmost path which
2867 * means it must be the only one. This gets
2868 * handled differently because we want to
2869 * revert the inode back to having extents
2872 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2874 el = et->et_root_el;
2875 el->l_tree_depth = 0;
2876 el->l_next_free_rec = 0;
2877 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2879 ocfs2_et_set_last_eb_blk(et, 0);
2882 ocfs2_journal_dirty(handle, path_root_bh(path));
2885 ocfs2_free_path(left_path);
2890 * Left rotation of btree records.
2892 * In many ways, this is (unsurprisingly) the opposite of right
2893 * rotation. We start at some non-rightmost path containing an empty
2894 * extent in the leaf block. The code works its way to the rightmost
2895 * path by rotating records to the left in every subtree.
2897 * This is used by any code which reduces the number of extent records
2898 * in a leaf. After removal, an empty record should be placed in the
2899 * leftmost list position.
2901 * This won't handle a length update of the rightmost path records if
2902 * the rightmost tree leaf record is removed so the caller is
2903 * responsible for detecting and correcting that.
2905 static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2906 struct ocfs2_path *path,
2907 struct ocfs2_cached_dealloc_ctxt *dealloc,
2908 struct ocfs2_extent_tree *et)
2910 int ret, orig_credits = handle->h_buffer_credits;
2911 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2912 struct ocfs2_extent_block *eb;
2913 struct ocfs2_extent_list *el;
2915 el = path_leaf_el(path);
2916 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2919 if (path->p_tree_depth == 0) {
2920 rightmost_no_delete:
2922 * Inline extents. This is trivially handled, so do
2925 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2927 path_leaf_el(path));
2934 * Handle rightmost branch now. There's several cases:
2935 * 1) simple rotation leaving records in there. That's trivial.
2936 * 2) rotation requiring a branch delete - there's no more
2937 * records left. Two cases of this:
2938 * a) There are branches to the left.
2939 * b) This is also the leftmost (the only) branch.
2941 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2942 * 2a) we need the left branch so that we can update it with the unlink
2943 * 2b) we need to bring the inode back to inline extents.
2946 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2948 if (eb->h_next_leaf_blk == 0) {
2950 * This gets a bit tricky if we're going to delete the
2951 * rightmost path. Get the other cases out of the way
2954 if (le16_to_cpu(el->l_next_free_rec) > 1)
2955 goto rightmost_no_delete;
2957 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2959 ocfs2_error(inode->i_sb,
2960 "Inode %llu has empty extent block at %llu",
2961 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2962 (unsigned long long)le64_to_cpu(eb->h_blkno));
2967 * XXX: The caller can not trust "path" any more after
2968 * this as it will have been deleted. What do we do?
2970 * In theory the rotate-for-merge code will never get
2971 * here because it'll always ask for a rotate in a
2975 ret = ocfs2_remove_rightmost_path(inode, handle, path,
2983 * Now we can loop, remembering the path we get from -EAGAIN
2984 * and restarting from there.
2987 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
2988 dealloc, &restart_path, et);
2989 if (ret && ret != -EAGAIN) {
2994 while (ret == -EAGAIN) {
2995 tmp_path = restart_path;
2996 restart_path = NULL;
2998 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
3001 if (ret && ret != -EAGAIN) {
3006 ocfs2_free_path(tmp_path);
3014 ocfs2_free_path(tmp_path);
3015 ocfs2_free_path(restart_path);
3019 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3022 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3025 if (rec->e_leaf_clusters == 0) {
3027 * We consumed all of the merged-from record. An empty
3028 * extent cannot exist anywhere but the 1st array
3029 * position, so move things over if the merged-from
3030 * record doesn't occupy that position.
3032 * This creates a new empty extent so the caller
3033 * should be smart enough to have removed any existing
3037 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3038 size = index * sizeof(struct ocfs2_extent_rec);
3039 memmove(&el->l_recs[1], &el->l_recs[0], size);
3043 * Always memset - the caller doesn't check whether it
3044 * created an empty extent, so there could be junk in
3047 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3051 static int ocfs2_get_right_path(struct inode *inode,
3052 struct ocfs2_path *left_path,
3053 struct ocfs2_path **ret_right_path)
3057 struct ocfs2_path *right_path = NULL;
3058 struct ocfs2_extent_list *left_el;
3060 *ret_right_path = NULL;
3062 /* This function shouldn't be called for non-trees. */
3063 BUG_ON(left_path->p_tree_depth == 0);
3065 left_el = path_leaf_el(left_path);
3066 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3068 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3075 /* This function shouldn't be called for the rightmost leaf. */
3076 BUG_ON(right_cpos == 0);
3078 right_path = ocfs2_new_path(path_root_bh(left_path),
3079 path_root_el(left_path));
3086 ret = ocfs2_find_path(inode, right_path, right_cpos);
3092 *ret_right_path = right_path;
3095 ocfs2_free_path(right_path);
3100 * Remove split_rec clusters from the record at index and merge them
3101 * onto the beginning of the record "next" to it.
3102 * For index < l_count - 1, the next means the extent rec at index + 1.
3103 * For index == l_count - 1, the "next" means the 1st extent rec of the
3104 * next extent block.
3106 static int ocfs2_merge_rec_right(struct inode *inode,
3107 struct ocfs2_path *left_path,
3109 struct ocfs2_extent_rec *split_rec,
3112 int ret, next_free, i;
3113 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3114 struct ocfs2_extent_rec *left_rec;
3115 struct ocfs2_extent_rec *right_rec;
3116 struct ocfs2_extent_list *right_el;
3117 struct ocfs2_path *right_path = NULL;
3118 int subtree_index = 0;
3119 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3120 struct buffer_head *bh = path_leaf_bh(left_path);
3121 struct buffer_head *root_bh = NULL;
3123 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3124 left_rec = &el->l_recs[index];
3126 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3127 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3128 /* we meet with a cross extent block merge. */
3129 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3135 right_el = path_leaf_el(right_path);
3136 next_free = le16_to_cpu(right_el->l_next_free_rec);
3137 BUG_ON(next_free <= 0);
3138 right_rec = &right_el->l_recs[0];
3139 if (ocfs2_is_empty_extent(right_rec)) {
3140 BUG_ON(next_free <= 1);
3141 right_rec = &right_el->l_recs[1];
3144 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3145 le16_to_cpu(left_rec->e_leaf_clusters) !=
3146 le32_to_cpu(right_rec->e_cpos));
3148 subtree_index = ocfs2_find_subtree_root(inode,
3149 left_path, right_path);
3151 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3152 handle->h_buffer_credits,
3159 root_bh = left_path->p_node[subtree_index].bh;
3160 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3162 ret = ocfs2_journal_access(handle, inode, root_bh,
3163 OCFS2_JOURNAL_ACCESS_WRITE);
3169 for (i = subtree_index + 1;
3170 i < path_num_items(right_path); i++) {
3171 ret = ocfs2_journal_access(handle, inode,
3172 right_path->p_node[i].bh,
3173 OCFS2_JOURNAL_ACCESS_WRITE);
3179 ret = ocfs2_journal_access(handle, inode,
3180 left_path->p_node[i].bh,
3181 OCFS2_JOURNAL_ACCESS_WRITE);
3189 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3190 right_rec = &el->l_recs[index + 1];
3193 ret = ocfs2_journal_access(handle, inode, bh,
3194 OCFS2_JOURNAL_ACCESS_WRITE);
3200 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3202 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3203 le64_add_cpu(&right_rec->e_blkno,
3204 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3205 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3207 ocfs2_cleanup_merge(el, index);
3209 ret = ocfs2_journal_dirty(handle, bh);
3214 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3218 ocfs2_complete_edge_insert(inode, handle, left_path,
3219 right_path, subtree_index);
3223 ocfs2_free_path(right_path);
3227 static int ocfs2_get_left_path(struct inode *inode,
3228 struct ocfs2_path *right_path,
3229 struct ocfs2_path **ret_left_path)
3233 struct ocfs2_path *left_path = NULL;
3235 *ret_left_path = NULL;
3237 /* This function shouldn't be called for non-trees. */
3238 BUG_ON(right_path->p_tree_depth == 0);
3240 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3241 right_path, &left_cpos);
3247 /* This function shouldn't be called for the leftmost leaf. */
3248 BUG_ON(left_cpos == 0);
3250 left_path = ocfs2_new_path(path_root_bh(right_path),
3251 path_root_el(right_path));
3258 ret = ocfs2_find_path(inode, left_path, left_cpos);
3264 *ret_left_path = left_path;
3267 ocfs2_free_path(left_path);
3272 * Remove split_rec clusters from the record at index and merge them
3273 * onto the tail of the record "before" it.
3274 * For index > 0, the "before" means the extent rec at index - 1.
3276 * For index == 0, the "before" means the last record of the previous
3277 * extent block. And there is also a situation that we may need to
3278 * remove the rightmost leaf extent block in the right_path and change
3279 * the right path to indicate the new rightmost path.
3281 static int ocfs2_merge_rec_left(struct inode *inode,
3282 struct ocfs2_path *right_path,
3284 struct ocfs2_extent_rec *split_rec,
3285 struct ocfs2_cached_dealloc_ctxt *dealloc,
3286 struct ocfs2_extent_tree *et,
3289 int ret, i, subtree_index = 0, has_empty_extent = 0;
3290 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3291 struct ocfs2_extent_rec *left_rec;
3292 struct ocfs2_extent_rec *right_rec;
3293 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3294 struct buffer_head *bh = path_leaf_bh(right_path);
3295 struct buffer_head *root_bh = NULL;
3296 struct ocfs2_path *left_path = NULL;
3297 struct ocfs2_extent_list *left_el;
3301 right_rec = &el->l_recs[index];
3303 /* we meet with a cross extent block merge. */
3304 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3310 left_el = path_leaf_el(left_path);
3311 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3312 le16_to_cpu(left_el->l_count));
3314 left_rec = &left_el->l_recs[
3315 le16_to_cpu(left_el->l_next_free_rec) - 1];
3316 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3317 le16_to_cpu(left_rec->e_leaf_clusters) !=
3318 le32_to_cpu(split_rec->e_cpos));
3320 subtree_index = ocfs2_find_subtree_root(inode,
3321 left_path, right_path);
3323 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3324 handle->h_buffer_credits,
3331 root_bh = left_path->p_node[subtree_index].bh;
3332 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3334 ret = ocfs2_journal_access(handle, inode, root_bh,
3335 OCFS2_JOURNAL_ACCESS_WRITE);
3341 for (i = subtree_index + 1;
3342 i < path_num_items(right_path); i++) {
3343 ret = ocfs2_journal_access(handle, inode,
3344 right_path->p_node[i].bh,
3345 OCFS2_JOURNAL_ACCESS_WRITE);
3351 ret = ocfs2_journal_access(handle, inode,
3352 left_path->p_node[i].bh,
3353 OCFS2_JOURNAL_ACCESS_WRITE);
3360 left_rec = &el->l_recs[index - 1];
3361 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3362 has_empty_extent = 1;
3365 ret = ocfs2_journal_access(handle, inode, bh,
3366 OCFS2_JOURNAL_ACCESS_WRITE);
3372 if (has_empty_extent && index == 1) {
3374 * The easy case - we can just plop the record right in.
3376 *left_rec = *split_rec;
3378 has_empty_extent = 0;
3380 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3382 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3383 le64_add_cpu(&right_rec->e_blkno,
3384 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3385 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3387 ocfs2_cleanup_merge(el, index);
3389 ret = ocfs2_journal_dirty(handle, bh);
3394 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3399 * In the situation that the right_rec is empty and the extent
3400 * block is empty also, ocfs2_complete_edge_insert can't handle
3401 * it and we need to delete the right extent block.
3403 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3404 le16_to_cpu(el->l_next_free_rec) == 1) {
3406 ret = ocfs2_remove_rightmost_path(inode, handle,
3414 /* Now the rightmost extent block has been deleted.
3415 * So we use the new rightmost path.
3417 ocfs2_mv_path(right_path, left_path);
3420 ocfs2_complete_edge_insert(inode, handle, left_path,
3421 right_path, subtree_index);
3425 ocfs2_free_path(left_path);
3429 static int ocfs2_try_to_merge_extent(struct inode *inode,
3431 struct ocfs2_path *path,
3433 struct ocfs2_extent_rec *split_rec,
3434 struct ocfs2_cached_dealloc_ctxt *dealloc,
3435 struct ocfs2_merge_ctxt *ctxt,
3436 struct ocfs2_extent_tree *et)
3440 struct ocfs2_extent_list *el = path_leaf_el(path);
3441 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3443 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3445 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3447 * The merge code will need to create an empty
3448 * extent to take the place of the newly
3449 * emptied slot. Remove any pre-existing empty
3450 * extents - having more than one in a leaf is
3453 ret = ocfs2_rotate_tree_left(inode, handle, path,
3460 rec = &el->l_recs[split_index];
3463 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3465 * Left-right contig implies this.
3467 BUG_ON(!ctxt->c_split_covers_rec);
3470 * Since the leftright insert always covers the entire
3471 * extent, this call will delete the insert record
3472 * entirely, resulting in an empty extent record added to
3475 * Since the adding of an empty extent shifts
3476 * everything back to the right, there's no need to
3477 * update split_index here.
3479 * When the split_index is zero, we need to merge it to the
3480 * prevoius extent block. It is more efficient and easier
3481 * if we do merge_right first and merge_left later.
3483 ret = ocfs2_merge_rec_right(inode, path,
3492 * We can only get this from logic error above.
3494 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3496 /* The merge left us with an empty extent, remove it. */
3497 ret = ocfs2_rotate_tree_left(inode, handle, path,
3504 rec = &el->l_recs[split_index];
3507 * Note that we don't pass split_rec here on purpose -
3508 * we've merged it into the rec already.
3510 ret = ocfs2_merge_rec_left(inode, path,
3520 ret = ocfs2_rotate_tree_left(inode, handle, path,
3523 * Error from this last rotate is not critical, so
3524 * print but don't bubble it up.
3531 * Merge a record to the left or right.
3533 * 'contig_type' is relative to the existing record,
3534 * so for example, if we're "right contig", it's to
3535 * the record on the left (hence the left merge).
3537 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3538 ret = ocfs2_merge_rec_left(inode,
3548 ret = ocfs2_merge_rec_right(inode,
3558 if (ctxt->c_split_covers_rec) {
3560 * The merge may have left an empty extent in
3561 * our leaf. Try to rotate it away.
3563 ret = ocfs2_rotate_tree_left(inode, handle, path,
3575 static void ocfs2_subtract_from_rec(struct super_block *sb,
3576 enum ocfs2_split_type split,
3577 struct ocfs2_extent_rec *rec,
3578 struct ocfs2_extent_rec *split_rec)
3582 len_blocks = ocfs2_clusters_to_blocks(sb,
3583 le16_to_cpu(split_rec->e_leaf_clusters));
3585 if (split == SPLIT_LEFT) {
3587 * Region is on the left edge of the existing
3590 le32_add_cpu(&rec->e_cpos,
3591 le16_to_cpu(split_rec->e_leaf_clusters));
3592 le64_add_cpu(&rec->e_blkno, len_blocks);
3593 le16_add_cpu(&rec->e_leaf_clusters,
3594 -le16_to_cpu(split_rec->e_leaf_clusters));
3597 * Region is on the right edge of the existing
3600 le16_add_cpu(&rec->e_leaf_clusters,
3601 -le16_to_cpu(split_rec->e_leaf_clusters));
3606 * Do the final bits of extent record insertion at the target leaf
3607 * list. If this leaf is part of an allocation tree, it is assumed
3608 * that the tree above has been prepared.
3610 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3611 struct ocfs2_extent_list *el,
3612 struct ocfs2_insert_type *insert,
3613 struct inode *inode)
3615 int i = insert->ins_contig_index;
3617 struct ocfs2_extent_rec *rec;
3619 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3621 if (insert->ins_split != SPLIT_NONE) {
3622 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3624 rec = &el->l_recs[i];
3625 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3631 * Contiguous insert - either left or right.
3633 if (insert->ins_contig != CONTIG_NONE) {
3634 rec = &el->l_recs[i];
3635 if (insert->ins_contig == CONTIG_LEFT) {
3636 rec->e_blkno = insert_rec->e_blkno;
3637 rec->e_cpos = insert_rec->e_cpos;
3639 le16_add_cpu(&rec->e_leaf_clusters,
3640 le16_to_cpu(insert_rec->e_leaf_clusters));
3645 * Handle insert into an empty leaf.
3647 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3648 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3649 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3650 el->l_recs[0] = *insert_rec;
3651 el->l_next_free_rec = cpu_to_le16(1);
3658 if (insert->ins_appending == APPEND_TAIL) {
3659 i = le16_to_cpu(el->l_next_free_rec) - 1;
3660 rec = &el->l_recs[i];
3661 range = le32_to_cpu(rec->e_cpos)
3662 + le16_to_cpu(rec->e_leaf_clusters);
3663 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3665 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3666 le16_to_cpu(el->l_count),
3667 "inode %lu, depth %u, count %u, next free %u, "
3668 "rec.cpos %u, rec.clusters %u, "
3669 "insert.cpos %u, insert.clusters %u\n",
3671 le16_to_cpu(el->l_tree_depth),
3672 le16_to_cpu(el->l_count),
3673 le16_to_cpu(el->l_next_free_rec),
3674 le32_to_cpu(el->l_recs[i].e_cpos),
3675 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3676 le32_to_cpu(insert_rec->e_cpos),
3677 le16_to_cpu(insert_rec->e_leaf_clusters));
3679 el->l_recs[i] = *insert_rec;
3680 le16_add_cpu(&el->l_next_free_rec, 1);
3686 * Ok, we have to rotate.
3688 * At this point, it is safe to assume that inserting into an
3689 * empty leaf and appending to a leaf have both been handled
3692 * This leaf needs to have space, either by the empty 1st
3693 * extent record, or by virtue of an l_next_rec < l_count.
3695 ocfs2_rotate_leaf(el, insert_rec);
3698 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3700 struct ocfs2_path *path,
3701 struct ocfs2_extent_rec *insert_rec)
3703 int ret, i, next_free;
3704 struct buffer_head *bh;
3705 struct ocfs2_extent_list *el;
3706 struct ocfs2_extent_rec *rec;
3709 * Update everything except the leaf block.
3711 for (i = 0; i < path->p_tree_depth; i++) {
3712 bh = path->p_node[i].bh;
3713 el = path->p_node[i].el;
3715 next_free = le16_to_cpu(el->l_next_free_rec);
3716 if (next_free == 0) {
3717 ocfs2_error(inode->i_sb,
3718 "Dinode %llu has a bad extent list",
3719 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3724 rec = &el->l_recs[next_free - 1];
3726 rec->e_int_clusters = insert_rec->e_cpos;
3727 le32_add_cpu(&rec->e_int_clusters,
3728 le16_to_cpu(insert_rec->e_leaf_clusters));
3729 le32_add_cpu(&rec->e_int_clusters,
3730 -le32_to_cpu(rec->e_cpos));
3732 ret = ocfs2_journal_dirty(handle, bh);
3739 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3740 struct ocfs2_extent_rec *insert_rec,
3741 struct ocfs2_path *right_path,
3742 struct ocfs2_path **ret_left_path)
3745 struct ocfs2_extent_list *el;
3746 struct ocfs2_path *left_path = NULL;
3748 *ret_left_path = NULL;
3751 * This shouldn't happen for non-trees. The extent rec cluster
3752 * count manipulation below only works for interior nodes.
3754 BUG_ON(right_path->p_tree_depth == 0);
3757 * If our appending insert is at the leftmost edge of a leaf,
3758 * then we might need to update the rightmost records of the
3761 el = path_leaf_el(right_path);
3762 next_free = le16_to_cpu(el->l_next_free_rec);
3763 if (next_free == 0 ||
3764 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3767 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3774 mlog(0, "Append may need a left path update. cpos: %u, "
3775 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3779 * No need to worry if the append is already in the
3783 left_path = ocfs2_new_path(path_root_bh(right_path),
3784 path_root_el(right_path));
3791 ret = ocfs2_find_path(inode, left_path, left_cpos);
3798 * ocfs2_insert_path() will pass the left_path to the
3804 ret = ocfs2_journal_access_path(inode, handle, right_path);
3810 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
3812 *ret_left_path = left_path;
3816 ocfs2_free_path(left_path);
3821 static void ocfs2_split_record(struct inode *inode,
3822 struct ocfs2_path *left_path,
3823 struct ocfs2_path *right_path,
3824 struct ocfs2_extent_rec *split_rec,
3825 enum ocfs2_split_type split)
3828 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3829 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3830 struct ocfs2_extent_rec *rec, *tmprec;
3832 right_el = path_leaf_el(right_path);;
3834 left_el = path_leaf_el(left_path);
3837 insert_el = right_el;
3838 index = ocfs2_search_extent_list(el, cpos);
3840 if (index == 0 && left_path) {
3841 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3844 * This typically means that the record
3845 * started in the left path but moved to the
3846 * right as a result of rotation. We either
3847 * move the existing record to the left, or we
3848 * do the later insert there.
3850 * In this case, the left path should always
3851 * exist as the rotate code will have passed
3852 * it back for a post-insert update.
3855 if (split == SPLIT_LEFT) {
3857 * It's a left split. Since we know
3858 * that the rotate code gave us an
3859 * empty extent in the left path, we
3860 * can just do the insert there.
3862 insert_el = left_el;
3865 * Right split - we have to move the
3866 * existing record over to the left
3867 * leaf. The insert will be into the
3868 * newly created empty extent in the
3871 tmprec = &right_el->l_recs[index];
3872 ocfs2_rotate_leaf(left_el, tmprec);
3875 memset(tmprec, 0, sizeof(*tmprec));
3876 index = ocfs2_search_extent_list(left_el, cpos);
3877 BUG_ON(index == -1);
3882 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3884 * Left path is easy - we can just allow the insert to
3888 insert_el = left_el;
3889 index = ocfs2_search_extent_list(el, cpos);
3890 BUG_ON(index == -1);
3893 rec = &el->l_recs[index];
3894 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3895 ocfs2_rotate_leaf(insert_el, split_rec);
3899 * This function only does inserts on an allocation b-tree. For tree
3900 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3902 * right_path is the path we want to do the actual insert
3903 * in. left_path should only be passed in if we need to update that
3904 * portion of the tree after an edge insert.
3906 static int ocfs2_insert_path(struct inode *inode,
3908 struct ocfs2_path *left_path,
3909 struct ocfs2_path *right_path,
3910 struct ocfs2_extent_rec *insert_rec,
3911 struct ocfs2_insert_type *insert)
3913 int ret, subtree_index;
3914 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
3917 int credits = handle->h_buffer_credits;
3920 * There's a chance that left_path got passed back to
3921 * us without being accounted for in the
3922 * journal. Extend our transaction here to be sure we
3923 * can change those blocks.
3925 credits += left_path->p_tree_depth;
3927 ret = ocfs2_extend_trans(handle, credits);
3933 ret = ocfs2_journal_access_path(inode, handle, left_path);
3941 * Pass both paths to the journal. The majority of inserts
3942 * will be touching all components anyway.
3944 ret = ocfs2_journal_access_path(inode, handle, right_path);
3950 if (insert->ins_split != SPLIT_NONE) {
3952 * We could call ocfs2_insert_at_leaf() for some types
3953 * of splits, but it's easier to just let one separate
3954 * function sort it all out.
3956 ocfs2_split_record(inode, left_path, right_path,
3957 insert_rec, insert->ins_split);
3960 * Split might have modified either leaf and we don't
3961 * have a guarantee that the later edge insert will
3962 * dirty this for us.
3965 ret = ocfs2_journal_dirty(handle,
3966 path_leaf_bh(left_path));
3970 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3973 ret = ocfs2_journal_dirty(handle, leaf_bh);
3979 * The rotate code has indicated that we need to fix
3980 * up portions of the tree after the insert.
3982 * XXX: Should we extend the transaction here?
3984 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3986 ocfs2_complete_edge_insert(inode, handle, left_path,
3987 right_path, subtree_index);
3995 static int ocfs2_do_insert_extent(struct inode *inode,
3997 struct ocfs2_extent_tree *et,
3998 struct ocfs2_extent_rec *insert_rec,
3999 struct ocfs2_insert_type *type)
4001 int ret, rotate = 0;
4003 struct ocfs2_path *right_path = NULL;
4004 struct ocfs2_path *left_path = NULL;
4005 struct ocfs2_extent_list *el;
4007 el = et->et_root_el;
4009 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
4010 OCFS2_JOURNAL_ACCESS_WRITE);
4016 if (le16_to_cpu(el->l_tree_depth) == 0) {
4017 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4018 goto out_update_clusters;
4021 right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4029 * Determine the path to start with. Rotations need the
4030 * rightmost path, everything else can go directly to the
4033 cpos = le32_to_cpu(insert_rec->e_cpos);
4034 if (type->ins_appending == APPEND_NONE &&
4035 type->ins_contig == CONTIG_NONE) {
4040 ret = ocfs2_find_path(inode, right_path, cpos);
4047 * Rotations and appends need special treatment - they modify
4048 * parts of the tree's above them.
4050 * Both might pass back a path immediate to the left of the
4051 * one being inserted to. This will be cause
4052 * ocfs2_insert_path() to modify the rightmost records of
4053 * left_path to account for an edge insert.
4055 * XXX: When modifying this code, keep in mind that an insert
4056 * can wind up skipping both of these two special cases...
4059 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
4060 le32_to_cpu(insert_rec->e_cpos),
4061 right_path, &left_path);
4068 * ocfs2_rotate_tree_right() might have extended the
4069 * transaction without re-journaling our tree root.
4071 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
4072 OCFS2_JOURNAL_ACCESS_WRITE);
4077 } else if (type->ins_appending == APPEND_TAIL
4078 && type->ins_contig != CONTIG_LEFT) {
4079 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4080 right_path, &left_path);
4087 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4094 out_update_clusters:
4095 if (type->ins_split == SPLIT_NONE)
4096 ocfs2_et_update_clusters(inode, et,
4097 le16_to_cpu(insert_rec->e_leaf_clusters));
4099 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
4104 ocfs2_free_path(left_path);
4105 ocfs2_free_path(right_path);
4110 static enum ocfs2_contig_type
4111 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
4112 struct ocfs2_extent_list *el, int index,
4113 struct ocfs2_extent_rec *split_rec)
4116 enum ocfs2_contig_type ret = CONTIG_NONE;
4117 u32 left_cpos, right_cpos;
4118 struct ocfs2_extent_rec *rec = NULL;
4119 struct ocfs2_extent_list *new_el;
4120 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4121 struct buffer_head *bh;
4122 struct ocfs2_extent_block *eb;
4125 rec = &el->l_recs[index - 1];
4126 } else if (path->p_tree_depth > 0) {
4127 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4132 if (left_cpos != 0) {
4133 left_path = ocfs2_new_path(path_root_bh(path),
4134 path_root_el(path));
4138 status = ocfs2_find_path(inode, left_path, left_cpos);
4142 new_el = path_leaf_el(left_path);
4144 if (le16_to_cpu(new_el->l_next_free_rec) !=
4145 le16_to_cpu(new_el->l_count)) {
4146 bh = path_leaf_bh(left_path);
4147 eb = (struct ocfs2_extent_block *)bh->b_data;
4148 ocfs2_error(inode->i_sb,
4149 "Extent block #%llu has an "
4150 "invalid l_next_free_rec of "
4151 "%d. It should have "
4152 "matched the l_count of %d",
4153 (unsigned long long)le64_to_cpu(eb->h_blkno),
4154 le16_to_cpu(new_el->l_next_free_rec),
4155 le16_to_cpu(new_el->l_count));
4159 rec = &new_el->l_recs[
4160 le16_to_cpu(new_el->l_next_free_rec) - 1];
4165 * We're careful to check for an empty extent record here -
4166 * the merge code will know what to do if it sees one.
4169 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4170 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4173 ret = ocfs2_extent_contig(inode, rec, split_rec);
4178 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4179 rec = &el->l_recs[index + 1];
4180 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4181 path->p_tree_depth > 0) {
4182 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4187 if (right_cpos == 0)
4190 right_path = ocfs2_new_path(path_root_bh(path),
4191 path_root_el(path));
4195 status = ocfs2_find_path(inode, right_path, right_cpos);
4199 new_el = path_leaf_el(right_path);
4200 rec = &new_el->l_recs[0];
4201 if (ocfs2_is_empty_extent(rec)) {
4202 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4203 bh = path_leaf_bh(right_path);
4204 eb = (struct ocfs2_extent_block *)bh->b_data;
4205 ocfs2_error(inode->i_sb,
4206 "Extent block #%llu has an "
4207 "invalid l_next_free_rec of %d",
4208 (unsigned long long)le64_to_cpu(eb->h_blkno),
4209 le16_to_cpu(new_el->l_next_free_rec));
4213 rec = &new_el->l_recs[1];
4218 enum ocfs2_contig_type contig_type;
4220 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4222 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4223 ret = CONTIG_LEFTRIGHT;
4224 else if (ret == CONTIG_NONE)
4230 ocfs2_free_path(left_path);
4232 ocfs2_free_path(right_path);
4237 static void ocfs2_figure_contig_type(struct inode *inode,
4238 struct ocfs2_insert_type *insert,
4239 struct ocfs2_extent_list *el,
4240 struct ocfs2_extent_rec *insert_rec,
4241 struct ocfs2_extent_tree *et)
4244 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4246 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4248 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4249 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4251 if (contig_type != CONTIG_NONE) {
4252 insert->ins_contig_index = i;
4256 insert->ins_contig = contig_type;
4258 if (insert->ins_contig != CONTIG_NONE) {
4259 struct ocfs2_extent_rec *rec =
4260 &el->l_recs[insert->ins_contig_index];
4261 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4262 le16_to_cpu(insert_rec->e_leaf_clusters);
4265 * Caller might want us to limit the size of extents, don't
4266 * calculate contiguousness if we might exceed that limit.
4268 if (et->et_max_leaf_clusters &&
4269 (len > et->et_max_leaf_clusters))
4270 insert->ins_contig = CONTIG_NONE;
4275 * This should only be called against the righmost leaf extent list.
4277 * ocfs2_figure_appending_type() will figure out whether we'll have to
4278 * insert at the tail of the rightmost leaf.
4280 * This should also work against the root extent list for tree's with 0
4281 * depth. If we consider the root extent list to be the rightmost leaf node
4282 * then the logic here makes sense.
4284 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4285 struct ocfs2_extent_list *el,
4286 struct ocfs2_extent_rec *insert_rec)
4289 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4290 struct ocfs2_extent_rec *rec;
4292 insert->ins_appending = APPEND_NONE;
4294 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4296 if (!el->l_next_free_rec)
4297 goto set_tail_append;
4299 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4300 /* Were all records empty? */
4301 if (le16_to_cpu(el->l_next_free_rec) == 1)
4302 goto set_tail_append;
4305 i = le16_to_cpu(el->l_next_free_rec) - 1;
4306 rec = &el->l_recs[i];
4309 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4310 goto set_tail_append;
4315 insert->ins_appending = APPEND_TAIL;
4319 * Helper function called at the begining of an insert.
4321 * This computes a few things that are commonly used in the process of
4322 * inserting into the btree:
4323 * - Whether the new extent is contiguous with an existing one.
4324 * - The current tree depth.
4325 * - Whether the insert is an appending one.
4326 * - The total # of free records in the tree.
4328 * All of the information is stored on the ocfs2_insert_type
4331 static int ocfs2_figure_insert_type(struct inode *inode,
4332 struct ocfs2_extent_tree *et,
4333 struct buffer_head **last_eb_bh,
4334 struct ocfs2_extent_rec *insert_rec,
4336 struct ocfs2_insert_type *insert)
4339 struct ocfs2_extent_block *eb;
4340 struct ocfs2_extent_list *el;
4341 struct ocfs2_path *path = NULL;
4342 struct buffer_head *bh = NULL;
4344 insert->ins_split = SPLIT_NONE;
4346 el = et->et_root_el;
4347 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4349 if (el->l_tree_depth) {
4351 * If we have tree depth, we read in the
4352 * rightmost extent block ahead of time as
4353 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4354 * may want it later.
4356 ret = ocfs2_read_extent_block(inode,
4357 ocfs2_et_get_last_eb_blk(et),
4363 eb = (struct ocfs2_extent_block *) bh->b_data;
4368 * Unless we have a contiguous insert, we'll need to know if
4369 * there is room left in our allocation tree for another
4372 * XXX: This test is simplistic, we can search for empty
4373 * extent records too.
4375 *free_records = le16_to_cpu(el->l_count) -
4376 le16_to_cpu(el->l_next_free_rec);
4378 if (!insert->ins_tree_depth) {
4379 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4380 ocfs2_figure_appending_type(insert, el, insert_rec);
4384 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4392 * In the case that we're inserting past what the tree
4393 * currently accounts for, ocfs2_find_path() will return for
4394 * us the rightmost tree path. This is accounted for below in
4395 * the appending code.
4397 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4403 el = path_leaf_el(path);
4406 * Now that we have the path, there's two things we want to determine:
4407 * 1) Contiguousness (also set contig_index if this is so)
4409 * 2) Are we doing an append? We can trivially break this up
4410 * into two types of appends: simple record append, or a
4411 * rotate inside the tail leaf.
4413 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4416 * The insert code isn't quite ready to deal with all cases of
4417 * left contiguousness. Specifically, if it's an insert into
4418 * the 1st record in a leaf, it will require the adjustment of
4419 * cluster count on the last record of the path directly to it's
4420 * left. For now, just catch that case and fool the layers
4421 * above us. This works just fine for tree_depth == 0, which
4422 * is why we allow that above.
4424 if (insert->ins_contig == CONTIG_LEFT &&
4425 insert->ins_contig_index == 0)
4426 insert->ins_contig = CONTIG_NONE;
4429 * Ok, so we can simply compare against last_eb to figure out
4430 * whether the path doesn't exist. This will only happen in
4431 * the case that we're doing a tail append, so maybe we can
4432 * take advantage of that information somehow.
4434 if (ocfs2_et_get_last_eb_blk(et) ==
4435 path_leaf_bh(path)->b_blocknr) {
4437 * Ok, ocfs2_find_path() returned us the rightmost
4438 * tree path. This might be an appending insert. There are
4440 * 1) We're doing a true append at the tail:
4441 * -This might even be off the end of the leaf
4442 * 2) We're "appending" by rotating in the tail
4444 ocfs2_figure_appending_type(insert, el, insert_rec);
4448 ocfs2_free_path(path);
4458 * Insert an extent into an inode btree.
4460 * The caller needs to update fe->i_clusters
4462 int ocfs2_insert_extent(struct ocfs2_super *osb,
4464 struct inode *inode,
4465 struct ocfs2_extent_tree *et,
4470 struct ocfs2_alloc_context *meta_ac)
4473 int uninitialized_var(free_records);
4474 struct buffer_head *last_eb_bh = NULL;
4475 struct ocfs2_insert_type insert = {0, };
4476 struct ocfs2_extent_rec rec;
4478 mlog(0, "add %u clusters at position %u to inode %llu\n",
4479 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4481 memset(&rec, 0, sizeof(rec));
4482 rec.e_cpos = cpu_to_le32(cpos);
4483 rec.e_blkno = cpu_to_le64(start_blk);
4484 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4485 rec.e_flags = flags;
4486 status = ocfs2_et_insert_check(inode, et, &rec);
4492 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4493 &free_records, &insert);
4499 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4500 "Insert.contig_index: %d, Insert.free_records: %d, "
4501 "Insert.tree_depth: %d\n",
4502 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4503 free_records, insert.ins_tree_depth);
4505 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4506 status = ocfs2_grow_tree(inode, handle, et,
4507 &insert.ins_tree_depth, &last_eb_bh,
4515 /* Finally, we can add clusters. This might rotate the tree for us. */
4516 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4519 else if (et->et_ops == &ocfs2_dinode_et_ops)
4520 ocfs2_extent_map_insert_rec(inode, &rec);
4530 * Allcate and add clusters into the extent b-tree.
4531 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4532 * The extent b-tree's root is specified by et, and
4533 * it is not limited to the file storage. Any extent tree can use this
4534 * function if it implements the proper ocfs2_extent_tree.
4536 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4537 struct inode *inode,
4538 u32 *logical_offset,
4539 u32 clusters_to_add,
4541 struct ocfs2_extent_tree *et,
4543 struct ocfs2_alloc_context *data_ac,
4544 struct ocfs2_alloc_context *meta_ac,
4545 enum ocfs2_alloc_restarted *reason_ret)
4549 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4550 u32 bit_off, num_bits;
4554 BUG_ON(!clusters_to_add);
4557 flags = OCFS2_EXT_UNWRITTEN;
4559 free_extents = ocfs2_num_free_extents(osb, inode, et);
4560 if (free_extents < 0) {
4561 status = free_extents;
4566 /* there are two cases which could cause us to EAGAIN in the
4567 * we-need-more-metadata case:
4568 * 1) we haven't reserved *any*
4569 * 2) we are so fragmented, we've needed to add metadata too
4571 if (!free_extents && !meta_ac) {
4572 mlog(0, "we haven't reserved any metadata!\n");
4574 reason = RESTART_META;
4576 } else if ((!free_extents)
4577 && (ocfs2_alloc_context_bits_left(meta_ac)
4578 < ocfs2_extend_meta_needed(et->et_root_el))) {
4579 mlog(0, "filesystem is really fragmented...\n");
4581 reason = RESTART_META;
4585 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4586 clusters_to_add, &bit_off, &num_bits);
4588 if (status != -ENOSPC)
4593 BUG_ON(num_bits > clusters_to_add);
4595 /* reserve our write early -- insert_extent may update the inode */
4596 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
4597 OCFS2_JOURNAL_ACCESS_WRITE);
4603 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4604 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4605 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4606 status = ocfs2_insert_extent(osb, handle, inode, et,
4607 *logical_offset, block,
4608 num_bits, flags, meta_ac);
4614 status = ocfs2_journal_dirty(handle, et->et_root_bh);
4620 clusters_to_add -= num_bits;
4621 *logical_offset += num_bits;
4623 if (clusters_to_add) {
4624 mlog(0, "need to alloc once more, wanted = %u\n",
4627 reason = RESTART_TRANS;
4633 *reason_ret = reason;
4637 static void ocfs2_make_right_split_rec(struct super_block *sb,
4638 struct ocfs2_extent_rec *split_rec,
4640 struct ocfs2_extent_rec *rec)
4642 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4643 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4645 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4647 split_rec->e_cpos = cpu_to_le32(cpos);
4648 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4650 split_rec->e_blkno = rec->e_blkno;
4651 le64_add_cpu(&split_rec->e_blkno,
4652 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4654 split_rec->e_flags = rec->e_flags;
4657 static int ocfs2_split_and_insert(struct inode *inode,
4659 struct ocfs2_path *path,
4660 struct ocfs2_extent_tree *et,
4661 struct buffer_head **last_eb_bh,
4663 struct ocfs2_extent_rec *orig_split_rec,
4664 struct ocfs2_alloc_context *meta_ac)
4667 unsigned int insert_range, rec_range, do_leftright = 0;
4668 struct ocfs2_extent_rec tmprec;
4669 struct ocfs2_extent_list *rightmost_el;
4670 struct ocfs2_extent_rec rec;
4671 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4672 struct ocfs2_insert_type insert;
4673 struct ocfs2_extent_block *eb;
4677 * Store a copy of the record on the stack - it might move
4678 * around as the tree is manipulated below.
4680 rec = path_leaf_el(path)->l_recs[split_index];
4682 rightmost_el = et->et_root_el;
4684 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4686 BUG_ON(!(*last_eb_bh));
4687 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4688 rightmost_el = &eb->h_list;
4691 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4692 le16_to_cpu(rightmost_el->l_count)) {
4693 ret = ocfs2_grow_tree(inode, handle, et,
4694 &depth, last_eb_bh, meta_ac);
4701 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4702 insert.ins_appending = APPEND_NONE;
4703 insert.ins_contig = CONTIG_NONE;
4704 insert.ins_tree_depth = depth;
4706 insert_range = le32_to_cpu(split_rec.e_cpos) +
4707 le16_to_cpu(split_rec.e_leaf_clusters);
4708 rec_range = le32_to_cpu(rec.e_cpos) +
4709 le16_to_cpu(rec.e_leaf_clusters);
4711 if (split_rec.e_cpos == rec.e_cpos) {
4712 insert.ins_split = SPLIT_LEFT;
4713 } else if (insert_range == rec_range) {
4714 insert.ins_split = SPLIT_RIGHT;
4717 * Left/right split. We fake this as a right split
4718 * first and then make a second pass as a left split.
4720 insert.ins_split = SPLIT_RIGHT;
4722 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4727 BUG_ON(do_leftright);
4731 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4737 if (do_leftright == 1) {
4739 struct ocfs2_extent_list *el;
4742 split_rec = *orig_split_rec;
4744 ocfs2_reinit_path(path, 1);
4746 cpos = le32_to_cpu(split_rec.e_cpos);
4747 ret = ocfs2_find_path(inode, path, cpos);
4753 el = path_leaf_el(path);
4754 split_index = ocfs2_search_extent_list(el, cpos);
4763 * Mark part or all of the extent record at split_index in the leaf
4764 * pointed to by path as written. This removes the unwritten
4767 * Care is taken to handle contiguousness so as to not grow the tree.
4769 * meta_ac is not strictly necessary - we only truly need it if growth
4770 * of the tree is required. All other cases will degrade into a less
4771 * optimal tree layout.
4773 * last_eb_bh should be the rightmost leaf block for any extent
4774 * btree. Since a split may grow the tree or a merge might shrink it,
4775 * the caller cannot trust the contents of that buffer after this call.
4777 * This code is optimized for readability - several passes might be
4778 * made over certain portions of the tree. All of those blocks will
4779 * have been brought into cache (and pinned via the journal), so the
4780 * extra overhead is not expressed in terms of disk reads.
4782 static int __ocfs2_mark_extent_written(struct inode *inode,
4783 struct ocfs2_extent_tree *et,
4785 struct ocfs2_path *path,
4787 struct ocfs2_extent_rec *split_rec,
4788 struct ocfs2_alloc_context *meta_ac,
4789 struct ocfs2_cached_dealloc_ctxt *dealloc)
4792 struct ocfs2_extent_list *el = path_leaf_el(path);
4793 struct buffer_head *last_eb_bh = NULL;
4794 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4795 struct ocfs2_merge_ctxt ctxt;
4796 struct ocfs2_extent_list *rightmost_el;
4798 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
4804 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4805 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4806 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4812 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
4817 * The core merge / split code wants to know how much room is
4818 * left in this inodes allocation tree, so we pass the
4819 * rightmost extent list.
4821 if (path->p_tree_depth) {
4822 struct ocfs2_extent_block *eb;
4824 ret = ocfs2_read_extent_block(inode,
4825 ocfs2_et_get_last_eb_blk(et),
4832 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4833 rightmost_el = &eb->h_list;
4835 rightmost_el = path_root_el(path);
4837 if (rec->e_cpos == split_rec->e_cpos &&
4838 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4839 ctxt.c_split_covers_rec = 1;
4841 ctxt.c_split_covers_rec = 0;
4843 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4845 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4846 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4847 ctxt.c_split_covers_rec);
4849 if (ctxt.c_contig_type == CONTIG_NONE) {
4850 if (ctxt.c_split_covers_rec)
4851 el->l_recs[split_index] = *split_rec;
4853 ret = ocfs2_split_and_insert(inode, handle, path, et,
4854 &last_eb_bh, split_index,
4855 split_rec, meta_ac);
4859 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4860 split_index, split_rec,
4861 dealloc, &ctxt, et);
4872 * Mark the already-existing extent at cpos as written for len clusters.
4874 * If the existing extent is larger than the request, initiate a
4875 * split. An attempt will be made at merging with adjacent extents.
4877 * The caller is responsible for passing down meta_ac if we'll need it.
4879 int ocfs2_mark_extent_written(struct inode *inode,
4880 struct ocfs2_extent_tree *et,
4881 handle_t *handle, u32 cpos, u32 len, u32 phys,
4882 struct ocfs2_alloc_context *meta_ac,
4883 struct ocfs2_cached_dealloc_ctxt *dealloc)
4886 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4887 struct ocfs2_extent_rec split_rec;
4888 struct ocfs2_path *left_path = NULL;
4889 struct ocfs2_extent_list *el;
4891 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4892 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4894 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4895 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4896 "that are being written to, but the feature bit "
4897 "is not set in the super block.",
4898 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4904 * XXX: This should be fixed up so that we just re-insert the
4905 * next extent records.
4907 * XXX: This is a hack on the extent tree, maybe it should be
4910 if (et->et_ops == &ocfs2_dinode_et_ops)
4911 ocfs2_extent_map_trunc(inode, 0);
4913 left_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4920 ret = ocfs2_find_path(inode, left_path, cpos);
4925 el = path_leaf_el(left_path);
4927 index = ocfs2_search_extent_list(el, cpos);
4928 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4929 ocfs2_error(inode->i_sb,
4930 "Inode %llu has an extent at cpos %u which can no "
4931 "longer be found.\n",
4932 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4937 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4938 split_rec.e_cpos = cpu_to_le32(cpos);
4939 split_rec.e_leaf_clusters = cpu_to_le16(len);
4940 split_rec.e_blkno = cpu_to_le64(start_blkno);
4941 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4942 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4944 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
4945 index, &split_rec, meta_ac,
4951 ocfs2_free_path(left_path);
4955 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
4956 handle_t *handle, struct ocfs2_path *path,
4957 int index, u32 new_range,
4958 struct ocfs2_alloc_context *meta_ac)
4960 int ret, depth, credits = handle->h_buffer_credits;
4961 struct buffer_head *last_eb_bh = NULL;
4962 struct ocfs2_extent_block *eb;
4963 struct ocfs2_extent_list *rightmost_el, *el;
4964 struct ocfs2_extent_rec split_rec;
4965 struct ocfs2_extent_rec *rec;
4966 struct ocfs2_insert_type insert;
4969 * Setup the record to split before we grow the tree.
4971 el = path_leaf_el(path);
4972 rec = &el->l_recs[index];
4973 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4975 depth = path->p_tree_depth;
4977 ret = ocfs2_read_extent_block(inode,
4978 ocfs2_et_get_last_eb_blk(et),
4985 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4986 rightmost_el = &eb->h_list;
4988 rightmost_el = path_leaf_el(path);
4990 credits += path->p_tree_depth +
4991 ocfs2_extend_meta_needed(et->et_root_el);
4992 ret = ocfs2_extend_trans(handle, credits);
4998 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4999 le16_to_cpu(rightmost_el->l_count)) {
5000 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
5008 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5009 insert.ins_appending = APPEND_NONE;
5010 insert.ins_contig = CONTIG_NONE;
5011 insert.ins_split = SPLIT_RIGHT;
5012 insert.ins_tree_depth = depth;
5014 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
5023 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5024 struct ocfs2_path *path, int index,
5025 struct ocfs2_cached_dealloc_ctxt *dealloc,
5027 struct ocfs2_extent_tree *et)
5030 u32 left_cpos, rec_range, trunc_range;
5031 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5032 struct super_block *sb = inode->i_sb;
5033 struct ocfs2_path *left_path = NULL;
5034 struct ocfs2_extent_list *el = path_leaf_el(path);
5035 struct ocfs2_extent_rec *rec;
5036 struct ocfs2_extent_block *eb;
5038 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5039 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5048 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5049 path->p_tree_depth) {
5051 * Check whether this is the rightmost tree record. If
5052 * we remove all of this record or part of its right
5053 * edge then an update of the record lengths above it
5056 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5057 if (eb->h_next_leaf_blk == 0)
5058 is_rightmost_tree_rec = 1;
5061 rec = &el->l_recs[index];
5062 if (index == 0 && path->p_tree_depth &&
5063 le32_to_cpu(rec->e_cpos) == cpos) {
5065 * Changing the leftmost offset (via partial or whole
5066 * record truncate) of an interior (or rightmost) path
5067 * means we have to update the subtree that is formed
5068 * by this leaf and the one to it's left.
5070 * There are two cases we can skip:
5071 * 1) Path is the leftmost one in our inode tree.
5072 * 2) The leaf is rightmost and will be empty after
5073 * we remove the extent record - the rotate code
5074 * knows how to update the newly formed edge.
5077 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5084 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5085 left_path = ocfs2_new_path(path_root_bh(path),
5086 path_root_el(path));
5093 ret = ocfs2_find_path(inode, left_path, left_cpos);
5101 ret = ocfs2_extend_rotate_transaction(handle, 0,
5102 handle->h_buffer_credits,
5109 ret = ocfs2_journal_access_path(inode, handle, path);
5115 ret = ocfs2_journal_access_path(inode, handle, left_path);
5121 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5122 trunc_range = cpos + len;
5124 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5127 memset(rec, 0, sizeof(*rec));
5128 ocfs2_cleanup_merge(el, index);
5131 next_free = le16_to_cpu(el->l_next_free_rec);
5132 if (is_rightmost_tree_rec && next_free > 1) {
5134 * We skip the edge update if this path will
5135 * be deleted by the rotate code.
5137 rec = &el->l_recs[next_free - 1];
5138 ocfs2_adjust_rightmost_records(inode, handle, path,
5141 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5142 /* Remove leftmost portion of the record. */
5143 le32_add_cpu(&rec->e_cpos, len);
5144 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5145 le16_add_cpu(&rec->e_leaf_clusters, -len);
5146 } else if (rec_range == trunc_range) {
5147 /* Remove rightmost portion of the record */
5148 le16_add_cpu(&rec->e_leaf_clusters, -len);
5149 if (is_rightmost_tree_rec)
5150 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5152 /* Caller should have trapped this. */
5153 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5154 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5155 le32_to_cpu(rec->e_cpos),
5156 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5163 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5164 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5168 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5170 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5177 ocfs2_free_path(left_path);
5181 int ocfs2_remove_extent(struct inode *inode,
5182 struct ocfs2_extent_tree *et,
5183 u32 cpos, u32 len, handle_t *handle,
5184 struct ocfs2_alloc_context *meta_ac,
5185 struct ocfs2_cached_dealloc_ctxt *dealloc)
5188 u32 rec_range, trunc_range;
5189 struct ocfs2_extent_rec *rec;
5190 struct ocfs2_extent_list *el;
5191 struct ocfs2_path *path = NULL;
5193 ocfs2_extent_map_trunc(inode, 0);
5195 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
5202 ret = ocfs2_find_path(inode, path, cpos);
5208 el = path_leaf_el(path);
5209 index = ocfs2_search_extent_list(el, cpos);
5210 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5211 ocfs2_error(inode->i_sb,
5212 "Inode %llu has an extent at cpos %u which can no "
5213 "longer be found.\n",
5214 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5220 * We have 3 cases of extent removal:
5221 * 1) Range covers the entire extent rec
5222 * 2) Range begins or ends on one edge of the extent rec
5223 * 3) Range is in the middle of the extent rec (no shared edges)
5225 * For case 1 we remove the extent rec and left rotate to
5228 * For case 2 we just shrink the existing extent rec, with a
5229 * tree update if the shrinking edge is also the edge of an
5232 * For case 3 we do a right split to turn the extent rec into
5233 * something case 2 can handle.
5235 rec = &el->l_recs[index];
5236 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5237 trunc_range = cpos + len;
5239 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5241 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5242 "(cpos %u, len %u)\n",
5243 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5244 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5246 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5247 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5254 ret = ocfs2_split_tree(inode, et, handle, path, index,
5255 trunc_range, meta_ac);
5262 * The split could have manipulated the tree enough to
5263 * move the record location, so we have to look for it again.
5265 ocfs2_reinit_path(path, 1);
5267 ret = ocfs2_find_path(inode, path, cpos);
5273 el = path_leaf_el(path);
5274 index = ocfs2_search_extent_list(el, cpos);
5275 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5276 ocfs2_error(inode->i_sb,
5277 "Inode %llu: split at cpos %u lost record.",
5278 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5285 * Double check our values here. If anything is fishy,
5286 * it's easier to catch it at the top level.
5288 rec = &el->l_recs[index];
5289 rec_range = le32_to_cpu(rec->e_cpos) +
5290 ocfs2_rec_clusters(el, rec);
5291 if (rec_range != trunc_range) {
5292 ocfs2_error(inode->i_sb,
5293 "Inode %llu: error after split at cpos %u"
5294 "trunc len %u, existing record is (%u,%u)",
5295 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5296 cpos, len, le32_to_cpu(rec->e_cpos),
5297 ocfs2_rec_clusters(el, rec));
5302 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5311 ocfs2_free_path(path);
5315 int ocfs2_remove_btree_range(struct inode *inode,
5316 struct ocfs2_extent_tree *et,
5317 u32 cpos, u32 phys_cpos, u32 len,
5318 struct ocfs2_cached_dealloc_ctxt *dealloc)
5321 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5322 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5323 struct inode *tl_inode = osb->osb_tl_inode;
5325 struct ocfs2_alloc_context *meta_ac = NULL;
5327 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5333 mutex_lock(&tl_inode->i_mutex);
5335 if (ocfs2_truncate_log_needs_flush(osb)) {
5336 ret = __ocfs2_flush_truncate_log(osb);
5343 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5344 if (IS_ERR(handle)) {
5345 ret = PTR_ERR(handle);
5350 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
5351 OCFS2_JOURNAL_ACCESS_WRITE);
5357 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5364 ocfs2_et_update_clusters(inode, et, -len);
5366 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5372 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5377 ocfs2_commit_trans(osb, handle);
5379 mutex_unlock(&tl_inode->i_mutex);
5382 ocfs2_free_alloc_context(meta_ac);
5387 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5389 struct buffer_head *tl_bh = osb->osb_tl_bh;
5390 struct ocfs2_dinode *di;
5391 struct ocfs2_truncate_log *tl;
5393 di = (struct ocfs2_dinode *) tl_bh->b_data;
5394 tl = &di->id2.i_dealloc;
5396 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5397 "slot %d, invalid truncate log parameters: used = "
5398 "%u, count = %u\n", osb->slot_num,
5399 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5400 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5403 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5404 unsigned int new_start)
5406 unsigned int tail_index;
5407 unsigned int current_tail;
5409 /* No records, nothing to coalesce */
5410 if (!le16_to_cpu(tl->tl_used))
5413 tail_index = le16_to_cpu(tl->tl_used) - 1;
5414 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5415 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5417 return current_tail == new_start;
5420 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5423 unsigned int num_clusters)
5426 unsigned int start_cluster, tl_count;
5427 struct inode *tl_inode = osb->osb_tl_inode;
5428 struct buffer_head *tl_bh = osb->osb_tl_bh;
5429 struct ocfs2_dinode *di;
5430 struct ocfs2_truncate_log *tl;
5432 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5433 (unsigned long long)start_blk, num_clusters);
5435 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5437 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5439 di = (struct ocfs2_dinode *) tl_bh->b_data;
5441 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5442 * by the underlying call to ocfs2_read_inode_block(), so any
5443 * corruption is a code bug */
5444 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5446 tl = &di->id2.i_dealloc;
5447 tl_count = le16_to_cpu(tl->tl_count);
5448 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5450 "Truncate record count on #%llu invalid "
5451 "wanted %u, actual %u\n",
5452 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5453 ocfs2_truncate_recs_per_inode(osb->sb),
5454 le16_to_cpu(tl->tl_count));
5456 /* Caller should have known to flush before calling us. */
5457 index = le16_to_cpu(tl->tl_used);
5458 if (index >= tl_count) {
5464 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5465 OCFS2_JOURNAL_ACCESS_WRITE);
5471 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5472 "%llu (index = %d)\n", num_clusters, start_cluster,
5473 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5475 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5477 * Move index back to the record we are coalescing with.
5478 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5482 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5483 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5484 index, le32_to_cpu(tl->tl_recs[index].t_start),
5487 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5488 tl->tl_used = cpu_to_le16(index + 1);
5490 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5492 status = ocfs2_journal_dirty(handle, tl_bh);
5503 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5505 struct inode *data_alloc_inode,
5506 struct buffer_head *data_alloc_bh)
5510 unsigned int num_clusters;
5512 struct ocfs2_truncate_rec rec;
5513 struct ocfs2_dinode *di;
5514 struct ocfs2_truncate_log *tl;
5515 struct inode *tl_inode = osb->osb_tl_inode;
5516 struct buffer_head *tl_bh = osb->osb_tl_bh;
5520 di = (struct ocfs2_dinode *) tl_bh->b_data;
5521 tl = &di->id2.i_dealloc;
5522 i = le16_to_cpu(tl->tl_used) - 1;
5524 /* Caller has given us at least enough credits to
5525 * update the truncate log dinode */
5526 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5527 OCFS2_JOURNAL_ACCESS_WRITE);
5533 tl->tl_used = cpu_to_le16(i);
5535 status = ocfs2_journal_dirty(handle, tl_bh);
5541 /* TODO: Perhaps we can calculate the bulk of the
5542 * credits up front rather than extending like
5544 status = ocfs2_extend_trans(handle,
5545 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5551 rec = tl->tl_recs[i];
5552 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5553 le32_to_cpu(rec.t_start));
5554 num_clusters = le32_to_cpu(rec.t_clusters);
5556 /* if start_blk is not set, we ignore the record as
5559 mlog(0, "free record %d, start = %u, clusters = %u\n",
5560 i, le32_to_cpu(rec.t_start), num_clusters);
5562 status = ocfs2_free_clusters(handle, data_alloc_inode,
5563 data_alloc_bh, start_blk,
5578 /* Expects you to already be holding tl_inode->i_mutex */
5579 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5582 unsigned int num_to_flush;
5584 struct inode *tl_inode = osb->osb_tl_inode;
5585 struct inode *data_alloc_inode = NULL;
5586 struct buffer_head *tl_bh = osb->osb_tl_bh;
5587 struct buffer_head *data_alloc_bh = NULL;
5588 struct ocfs2_dinode *di;
5589 struct ocfs2_truncate_log *tl;
5593 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5595 di = (struct ocfs2_dinode *) tl_bh->b_data;
5597 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5598 * by the underlying call to ocfs2_read_inode_block(), so any
5599 * corruption is a code bug */
5600 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5602 tl = &di->id2.i_dealloc;
5603 num_to_flush = le16_to_cpu(tl->tl_used);
5604 mlog(0, "Flush %u records from truncate log #%llu\n",
5605 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5606 if (!num_to_flush) {
5611 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5612 GLOBAL_BITMAP_SYSTEM_INODE,
5613 OCFS2_INVALID_SLOT);
5614 if (!data_alloc_inode) {
5616 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5620 mutex_lock(&data_alloc_inode->i_mutex);
5622 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5628 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5629 if (IS_ERR(handle)) {
5630 status = PTR_ERR(handle);
5635 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5640 ocfs2_commit_trans(osb, handle);
5643 brelse(data_alloc_bh);
5644 ocfs2_inode_unlock(data_alloc_inode, 1);
5647 mutex_unlock(&data_alloc_inode->i_mutex);
5648 iput(data_alloc_inode);
5655 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5658 struct inode *tl_inode = osb->osb_tl_inode;
5660 mutex_lock(&tl_inode->i_mutex);
5661 status = __ocfs2_flush_truncate_log(osb);
5662 mutex_unlock(&tl_inode->i_mutex);
5667 static void ocfs2_truncate_log_worker(struct work_struct *work)
5670 struct ocfs2_super *osb =
5671 container_of(work, struct ocfs2_super,
5672 osb_truncate_log_wq.work);
5676 status = ocfs2_flush_truncate_log(osb);
5680 ocfs2_init_inode_steal_slot(osb);
5685 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5686 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5689 if (osb->osb_tl_inode) {
5690 /* We want to push off log flushes while truncates are
5693 cancel_delayed_work(&osb->osb_truncate_log_wq);
5695 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5696 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5700 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5702 struct inode **tl_inode,
5703 struct buffer_head **tl_bh)
5706 struct inode *inode = NULL;
5707 struct buffer_head *bh = NULL;
5709 inode = ocfs2_get_system_file_inode(osb,
5710 TRUNCATE_LOG_SYSTEM_INODE,
5714 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5718 status = ocfs2_read_inode_block(inode, &bh);
5732 /* called during the 1st stage of node recovery. we stamp a clean
5733 * truncate log and pass back a copy for processing later. if the
5734 * truncate log does not require processing, a *tl_copy is set to
5736 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5738 struct ocfs2_dinode **tl_copy)
5741 struct inode *tl_inode = NULL;
5742 struct buffer_head *tl_bh = NULL;
5743 struct ocfs2_dinode *di;
5744 struct ocfs2_truncate_log *tl;
5748 mlog(0, "recover truncate log from slot %d\n", slot_num);
5750 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5756 di = (struct ocfs2_dinode *) tl_bh->b_data;
5758 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5759 * validated by the underlying call to ocfs2_read_inode_block(),
5760 * so any corruption is a code bug */
5761 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5763 tl = &di->id2.i_dealloc;
5764 if (le16_to_cpu(tl->tl_used)) {
5765 mlog(0, "We'll have %u logs to recover\n",
5766 le16_to_cpu(tl->tl_used));
5768 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5775 /* Assuming the write-out below goes well, this copy
5776 * will be passed back to recovery for processing. */
5777 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5779 /* All we need to do to clear the truncate log is set
5783 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5795 if (status < 0 && (*tl_copy)) {
5804 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5805 struct ocfs2_dinode *tl_copy)
5809 unsigned int clusters, num_recs, start_cluster;
5812 struct inode *tl_inode = osb->osb_tl_inode;
5813 struct ocfs2_truncate_log *tl;
5817 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5818 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5822 tl = &tl_copy->id2.i_dealloc;
5823 num_recs = le16_to_cpu(tl->tl_used);
5824 mlog(0, "cleanup %u records from %llu\n", num_recs,
5825 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
5827 mutex_lock(&tl_inode->i_mutex);
5828 for(i = 0; i < num_recs; i++) {
5829 if (ocfs2_truncate_log_needs_flush(osb)) {
5830 status = __ocfs2_flush_truncate_log(osb);
5837 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5838 if (IS_ERR(handle)) {
5839 status = PTR_ERR(handle);
5844 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5845 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5846 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5848 status = ocfs2_truncate_log_append(osb, handle,
5849 start_blk, clusters);
5850 ocfs2_commit_trans(osb, handle);
5858 mutex_unlock(&tl_inode->i_mutex);
5864 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5867 struct inode *tl_inode = osb->osb_tl_inode;
5872 cancel_delayed_work(&osb->osb_truncate_log_wq);
5873 flush_workqueue(ocfs2_wq);
5875 status = ocfs2_flush_truncate_log(osb);
5879 brelse(osb->osb_tl_bh);
5880 iput(osb->osb_tl_inode);
5886 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5889 struct inode *tl_inode = NULL;
5890 struct buffer_head *tl_bh = NULL;
5894 status = ocfs2_get_truncate_log_info(osb,
5901 /* ocfs2_truncate_log_shutdown keys on the existence of
5902 * osb->osb_tl_inode so we don't set any of the osb variables
5903 * until we're sure all is well. */
5904 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5905 ocfs2_truncate_log_worker);
5906 osb->osb_tl_bh = tl_bh;
5907 osb->osb_tl_inode = tl_inode;
5914 * Delayed de-allocation of suballocator blocks.
5916 * Some sets of block de-allocations might involve multiple suballocator inodes.
5918 * The locking for this can get extremely complicated, especially when
5919 * the suballocator inodes to delete from aren't known until deep
5920 * within an unrelated codepath.
5922 * ocfs2_extent_block structures are a good example of this - an inode
5923 * btree could have been grown by any number of nodes each allocating
5924 * out of their own suballoc inode.
5926 * These structures allow the delay of block de-allocation until a
5927 * later time, when locking of multiple cluster inodes won't cause
5932 * Describe a single bit freed from a suballocator. For the block
5933 * suballocators, it represents one block. For the global cluster
5934 * allocator, it represents some clusters and free_bit indicates
5937 struct ocfs2_cached_block_free {
5938 struct ocfs2_cached_block_free *free_next;
5940 unsigned int free_bit;
5943 struct ocfs2_per_slot_free_list {
5944 struct ocfs2_per_slot_free_list *f_next_suballocator;
5947 struct ocfs2_cached_block_free *f_first;
5950 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
5953 struct ocfs2_cached_block_free *head)
5958 struct inode *inode;
5959 struct buffer_head *di_bh = NULL;
5960 struct ocfs2_cached_block_free *tmp;
5962 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5969 mutex_lock(&inode->i_mutex);
5971 ret = ocfs2_inode_lock(inode, &di_bh, 1);
5977 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5978 if (IS_ERR(handle)) {
5979 ret = PTR_ERR(handle);
5985 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5987 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5988 head->free_bit, (unsigned long long)head->free_blk);
5990 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5991 head->free_bit, bg_blkno, 1);
5997 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6004 head = head->free_next;
6009 ocfs2_commit_trans(osb, handle);
6012 ocfs2_inode_unlock(inode, 1);
6015 mutex_unlock(&inode->i_mutex);
6019 /* Premature exit may have left some dangling items. */
6021 head = head->free_next;
6028 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6029 u64 blkno, unsigned int bit)
6032 struct ocfs2_cached_block_free *item;
6034 item = kmalloc(sizeof(*item), GFP_NOFS);
6041 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6042 bit, (unsigned long long)blkno);
6044 item->free_blk = blkno;
6045 item->free_bit = bit;
6046 item->free_next = ctxt->c_global_allocator;
6048 ctxt->c_global_allocator = item;
6052 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6053 struct ocfs2_cached_block_free *head)
6055 struct ocfs2_cached_block_free *tmp;
6056 struct inode *tl_inode = osb->osb_tl_inode;
6060 mutex_lock(&tl_inode->i_mutex);
6063 if (ocfs2_truncate_log_needs_flush(osb)) {
6064 ret = __ocfs2_flush_truncate_log(osb);
6071 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6072 if (IS_ERR(handle)) {
6073 ret = PTR_ERR(handle);
6078 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6081 ocfs2_commit_trans(osb, handle);
6083 head = head->free_next;
6092 mutex_unlock(&tl_inode->i_mutex);
6095 /* Premature exit may have left some dangling items. */
6097 head = head->free_next;
6104 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6105 struct ocfs2_cached_dealloc_ctxt *ctxt)
6108 struct ocfs2_per_slot_free_list *fl;
6113 while (ctxt->c_first_suballocator) {
6114 fl = ctxt->c_first_suballocator;
6117 mlog(0, "Free items: (type %u, slot %d)\n",
6118 fl->f_inode_type, fl->f_slot);
6119 ret2 = ocfs2_free_cached_blocks(osb,
6129 ctxt->c_first_suballocator = fl->f_next_suballocator;
6133 if (ctxt->c_global_allocator) {
6134 ret2 = ocfs2_free_cached_clusters(osb,
6135 ctxt->c_global_allocator);
6141 ctxt->c_global_allocator = NULL;
6147 static struct ocfs2_per_slot_free_list *
6148 ocfs2_find_per_slot_free_list(int type,
6150 struct ocfs2_cached_dealloc_ctxt *ctxt)
6152 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6155 if (fl->f_inode_type == type && fl->f_slot == slot)
6158 fl = fl->f_next_suballocator;
6161 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6163 fl->f_inode_type = type;
6166 fl->f_next_suballocator = ctxt->c_first_suballocator;
6168 ctxt->c_first_suballocator = fl;
6173 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6174 int type, int slot, u64 blkno,
6178 struct ocfs2_per_slot_free_list *fl;
6179 struct ocfs2_cached_block_free *item;
6181 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6188 item = kmalloc(sizeof(*item), GFP_NOFS);
6195 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6196 type, slot, bit, (unsigned long long)blkno);
6198 item->free_blk = blkno;
6199 item->free_bit = bit;
6200 item->free_next = fl->f_first;
6209 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6210 struct ocfs2_extent_block *eb)
6212 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6213 le16_to_cpu(eb->h_suballoc_slot),
6214 le64_to_cpu(eb->h_blkno),
6215 le16_to_cpu(eb->h_suballoc_bit));
6218 /* This function will figure out whether the currently last extent
6219 * block will be deleted, and if it will, what the new last extent
6220 * block will be so we can update his h_next_leaf_blk field, as well
6221 * as the dinodes i_last_eb_blk */
6222 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6223 unsigned int clusters_to_del,
6224 struct ocfs2_path *path,
6225 struct buffer_head **new_last_eb)
6227 int next_free, ret = 0;
6229 struct ocfs2_extent_rec *rec;
6230 struct ocfs2_extent_block *eb;
6231 struct ocfs2_extent_list *el;
6232 struct buffer_head *bh = NULL;
6234 *new_last_eb = NULL;
6236 /* we have no tree, so of course, no last_eb. */
6237 if (!path->p_tree_depth)
6240 /* trunc to zero special case - this makes tree_depth = 0
6241 * regardless of what it is. */
6242 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6245 el = path_leaf_el(path);
6246 BUG_ON(!el->l_next_free_rec);
6249 * Make sure that this extent list will actually be empty
6250 * after we clear away the data. We can shortcut out if
6251 * there's more than one non-empty extent in the
6252 * list. Otherwise, a check of the remaining extent is
6255 next_free = le16_to_cpu(el->l_next_free_rec);
6257 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6261 /* We may have a valid extent in index 1, check it. */
6263 rec = &el->l_recs[1];
6266 * Fall through - no more nonempty extents, so we want
6267 * to delete this leaf.
6273 rec = &el->l_recs[0];
6278 * Check it we'll only be trimming off the end of this
6281 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6285 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6291 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6297 eb = (struct ocfs2_extent_block *) bh->b_data;
6300 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6301 * Any corruption is a code bug. */
6302 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
6305 get_bh(*new_last_eb);
6306 mlog(0, "returning block %llu, (cpos: %u)\n",
6307 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6315 * Trim some clusters off the rightmost edge of a tree. Only called
6318 * The caller needs to:
6319 * - start journaling of each path component.
6320 * - compute and fully set up any new last ext block
6322 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6323 handle_t *handle, struct ocfs2_truncate_context *tc,
6324 u32 clusters_to_del, u64 *delete_start)
6326 int ret, i, index = path->p_tree_depth;
6329 struct buffer_head *bh;
6330 struct ocfs2_extent_list *el;
6331 struct ocfs2_extent_rec *rec;
6335 while (index >= 0) {
6336 bh = path->p_node[index].bh;
6337 el = path->p_node[index].el;
6339 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6340 index, (unsigned long long)bh->b_blocknr);
6342 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6345 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6346 ocfs2_error(inode->i_sb,
6347 "Inode %lu has invalid ext. block %llu",
6349 (unsigned long long)bh->b_blocknr);
6355 i = le16_to_cpu(el->l_next_free_rec) - 1;
6356 rec = &el->l_recs[i];
6358 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6359 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6360 ocfs2_rec_clusters(el, rec),
6361 (unsigned long long)le64_to_cpu(rec->e_blkno),
6362 le16_to_cpu(el->l_next_free_rec));
6364 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6366 if (le16_to_cpu(el->l_tree_depth) == 0) {
6368 * If the leaf block contains a single empty
6369 * extent and no records, we can just remove
6372 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6374 sizeof(struct ocfs2_extent_rec));
6375 el->l_next_free_rec = cpu_to_le16(0);
6381 * Remove any empty extents by shifting things
6382 * left. That should make life much easier on
6383 * the code below. This condition is rare
6384 * enough that we shouldn't see a performance
6387 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6388 le16_add_cpu(&el->l_next_free_rec, -1);
6391 i < le16_to_cpu(el->l_next_free_rec); i++)
6392 el->l_recs[i] = el->l_recs[i + 1];
6394 memset(&el->l_recs[i], 0,
6395 sizeof(struct ocfs2_extent_rec));
6398 * We've modified our extent list. The
6399 * simplest way to handle this change
6400 * is to being the search from the
6403 goto find_tail_record;
6406 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6409 * We'll use "new_edge" on our way back up the
6410 * tree to know what our rightmost cpos is.
6412 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6413 new_edge += le32_to_cpu(rec->e_cpos);
6416 * The caller will use this to delete data blocks.
6418 *delete_start = le64_to_cpu(rec->e_blkno)
6419 + ocfs2_clusters_to_blocks(inode->i_sb,
6420 le16_to_cpu(rec->e_leaf_clusters));
6423 * If it's now empty, remove this record.
6425 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6427 sizeof(struct ocfs2_extent_rec));
6428 le16_add_cpu(&el->l_next_free_rec, -1);
6431 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6433 sizeof(struct ocfs2_extent_rec));
6434 le16_add_cpu(&el->l_next_free_rec, -1);
6439 /* Can this actually happen? */
6440 if (le16_to_cpu(el->l_next_free_rec) == 0)
6444 * We never actually deleted any clusters
6445 * because our leaf was empty. There's no
6446 * reason to adjust the rightmost edge then.
6451 rec->e_int_clusters = cpu_to_le32(new_edge);
6452 le32_add_cpu(&rec->e_int_clusters,
6453 -le32_to_cpu(rec->e_cpos));
6456 * A deleted child record should have been
6459 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6463 ret = ocfs2_journal_dirty(handle, bh);
6469 mlog(0, "extent list container %llu, after: record %d: "
6470 "(%u, %u, %llu), next = %u.\n",
6471 (unsigned long long)bh->b_blocknr, i,
6472 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6473 (unsigned long long)le64_to_cpu(rec->e_blkno),
6474 le16_to_cpu(el->l_next_free_rec));
6477 * We must be careful to only attempt delete of an
6478 * extent block (and not the root inode block).
6480 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6481 struct ocfs2_extent_block *eb =
6482 (struct ocfs2_extent_block *)bh->b_data;
6485 * Save this for use when processing the
6488 deleted_eb = le64_to_cpu(eb->h_blkno);
6490 mlog(0, "deleting this extent block.\n");
6492 ocfs2_remove_from_cache(inode, bh);
6494 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6495 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6496 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6498 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6499 /* An error here is not fatal. */
6514 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6515 unsigned int clusters_to_del,
6516 struct inode *inode,
6517 struct buffer_head *fe_bh,
6519 struct ocfs2_truncate_context *tc,
6520 struct ocfs2_path *path)
6523 struct ocfs2_dinode *fe;
6524 struct ocfs2_extent_block *last_eb = NULL;
6525 struct ocfs2_extent_list *el;
6526 struct buffer_head *last_eb_bh = NULL;
6529 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6531 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6539 * Each component will be touched, so we might as well journal
6540 * here to avoid having to handle errors later.
6542 status = ocfs2_journal_access_path(inode, handle, path);
6549 status = ocfs2_journal_access(handle, inode, last_eb_bh,
6550 OCFS2_JOURNAL_ACCESS_WRITE);
6556 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6559 el = &(fe->id2.i_list);
6562 * Lower levels depend on this never happening, but it's best
6563 * to check it up here before changing the tree.
6565 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6566 ocfs2_error(inode->i_sb,
6567 "Inode %lu has an empty extent record, depth %u\n",
6568 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6573 vfs_dq_free_space_nodirty(inode,
6574 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
6575 spin_lock(&OCFS2_I(inode)->ip_lock);
6576 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6578 spin_unlock(&OCFS2_I(inode)->ip_lock);
6579 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6580 inode->i_blocks = ocfs2_inode_sector_count(inode);
6582 status = ocfs2_trim_tree(inode, path, handle, tc,
6583 clusters_to_del, &delete_blk);
6589 if (le32_to_cpu(fe->i_clusters) == 0) {
6590 /* trunc to zero is a special case. */
6591 el->l_tree_depth = 0;
6592 fe->i_last_eb_blk = 0;
6594 fe->i_last_eb_blk = last_eb->h_blkno;
6596 status = ocfs2_journal_dirty(handle, fe_bh);
6603 /* If there will be a new last extent block, then by
6604 * definition, there cannot be any leaves to the right of
6606 last_eb->h_next_leaf_blk = 0;
6607 status = ocfs2_journal_dirty(handle, last_eb_bh);
6615 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6629 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6631 set_buffer_uptodate(bh);
6632 mark_buffer_dirty(bh);
6636 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6637 unsigned int from, unsigned int to,
6638 struct page *page, int zero, u64 *phys)
6640 int ret, partial = 0;
6642 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6647 zero_user_segment(page, from, to);
6650 * Need to set the buffers we zero'd into uptodate
6651 * here if they aren't - ocfs2_map_page_blocks()
6652 * might've skipped some
6654 ret = walk_page_buffers(handle, page_buffers(page),
6659 else if (ocfs2_should_order_data(inode)) {
6660 ret = ocfs2_jbd2_file_inode(handle, inode);
6666 SetPageUptodate(page);
6668 flush_dcache_page(page);
6671 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6672 loff_t end, struct page **pages,
6673 int numpages, u64 phys, handle_t *handle)
6677 unsigned int from, to = PAGE_CACHE_SIZE;
6678 struct super_block *sb = inode->i_sb;
6680 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6685 to = PAGE_CACHE_SIZE;
6686 for(i = 0; i < numpages; i++) {
6689 from = start & (PAGE_CACHE_SIZE - 1);
6690 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6691 to = end & (PAGE_CACHE_SIZE - 1);
6693 BUG_ON(from > PAGE_CACHE_SIZE);
6694 BUG_ON(to > PAGE_CACHE_SIZE);
6696 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6699 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6703 ocfs2_unlock_and_free_pages(pages, numpages);
6706 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6707 struct page **pages, int *num)
6709 int numpages, ret = 0;
6710 struct super_block *sb = inode->i_sb;
6711 struct address_space *mapping = inode->i_mapping;
6712 unsigned long index;
6713 loff_t last_page_bytes;
6715 BUG_ON(start > end);
6717 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6718 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6721 last_page_bytes = PAGE_ALIGN(end);
6722 index = start >> PAGE_CACHE_SHIFT;
6724 pages[numpages] = grab_cache_page(mapping, index);
6725 if (!pages[numpages]) {
6733 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6738 ocfs2_unlock_and_free_pages(pages, numpages);
6748 * Zero the area past i_size but still within an allocated
6749 * cluster. This avoids exposing nonzero data on subsequent file
6752 * We need to call this before i_size is updated on the inode because
6753 * otherwise block_write_full_page() will skip writeout of pages past
6754 * i_size. The new_i_size parameter is passed for this reason.
6756 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6757 u64 range_start, u64 range_end)
6759 int ret = 0, numpages;
6760 struct page **pages = NULL;
6762 unsigned int ext_flags;
6763 struct super_block *sb = inode->i_sb;
6766 * File systems which don't support sparse files zero on every
6769 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6772 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6773 sizeof(struct page *), GFP_NOFS);
6774 if (pages == NULL) {
6780 if (range_start == range_end)
6783 ret = ocfs2_extent_map_get_blocks(inode,
6784 range_start >> sb->s_blocksize_bits,
6785 &phys, NULL, &ext_flags);
6792 * Tail is a hole, or is marked unwritten. In either case, we
6793 * can count on read and write to return/push zero's.
6795 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6798 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6805 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6806 numpages, phys, handle);
6809 * Initiate writeout of the pages we zero'd here. We don't
6810 * wait on them - the truncate_inode_pages() call later will
6813 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6814 range_end - 1, SYNC_FILE_RANGE_WRITE);
6825 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6826 struct ocfs2_dinode *di)
6828 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6829 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6831 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6832 memset(&di->id2, 0, blocksize -
6833 offsetof(struct ocfs2_dinode, id2) -
6836 memset(&di->id2, 0, blocksize -
6837 offsetof(struct ocfs2_dinode, id2));
6840 void ocfs2_dinode_new_extent_list(struct inode *inode,
6841 struct ocfs2_dinode *di)
6843 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6844 di->id2.i_list.l_tree_depth = 0;
6845 di->id2.i_list.l_next_free_rec = 0;
6846 di->id2.i_list.l_count = cpu_to_le16(
6847 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6850 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6852 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6853 struct ocfs2_inline_data *idata = &di->id2.i_data;
6855 spin_lock(&oi->ip_lock);
6856 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6857 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6858 spin_unlock(&oi->ip_lock);
6861 * We clear the entire i_data structure here so that all
6862 * fields can be properly initialized.
6864 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6866 idata->id_count = cpu_to_le16(
6867 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6870 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6871 struct buffer_head *di_bh)
6873 int ret, i, has_data, num_pages = 0;
6875 u64 uninitialized_var(block);
6876 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6877 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6878 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6879 struct ocfs2_alloc_context *data_ac = NULL;
6880 struct page **pages = NULL;
6881 loff_t end = osb->s_clustersize;
6882 struct ocfs2_extent_tree et;
6885 has_data = i_size_read(inode) ? 1 : 0;
6888 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6889 sizeof(struct page *), GFP_NOFS);
6890 if (pages == NULL) {
6896 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6903 handle = ocfs2_start_trans(osb,
6904 ocfs2_inline_to_extents_credits(osb->sb));
6905 if (IS_ERR(handle)) {
6906 ret = PTR_ERR(handle);
6911 ret = ocfs2_journal_access(handle, inode, di_bh,
6912 OCFS2_JOURNAL_ACCESS_WRITE);
6920 unsigned int page_end;
6923 if (vfs_dq_alloc_space_nodirty(inode,
6924 ocfs2_clusters_to_bytes(osb->sb, 1))) {
6930 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6938 * Save two copies, one for insert, and one that can
6939 * be changed by ocfs2_map_and_dirty_page() below.
6941 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6944 * Non sparse file systems zero on extend, so no need
6947 if (!ocfs2_sparse_alloc(osb) &&
6948 PAGE_CACHE_SIZE < osb->s_clustersize)
6949 end = PAGE_CACHE_SIZE;
6951 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6958 * This should populate the 1st page for us and mark
6961 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6967 page_end = PAGE_CACHE_SIZE;
6968 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6969 page_end = osb->s_clustersize;
6971 for (i = 0; i < num_pages; i++)
6972 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6973 pages[i], i > 0, &phys);
6976 spin_lock(&oi->ip_lock);
6977 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6978 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6979 spin_unlock(&oi->ip_lock);
6981 ocfs2_dinode_new_extent_list(inode, di);
6983 ocfs2_journal_dirty(handle, di_bh);
6987 * An error at this point should be extremely rare. If
6988 * this proves to be false, we could always re-build
6989 * the in-inode data from our pages.
6991 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
6992 ret = ocfs2_insert_extent(osb, handle, inode, &et,
6993 0, block, 1, 0, NULL);
6999 inode->i_blocks = ocfs2_inode_sector_count(inode);
7003 if (ret < 0 && did_quota)
7004 vfs_dq_free_space_nodirty(inode,
7005 ocfs2_clusters_to_bytes(osb->sb, 1));
7007 ocfs2_commit_trans(osb, handle);
7011 ocfs2_free_alloc_context(data_ac);
7015 ocfs2_unlock_and_free_pages(pages, num_pages);
7023 * It is expected, that by the time you call this function,
7024 * inode->i_size and fe->i_size have been adjusted.
7026 * WARNING: This will kfree the truncate context
7028 int ocfs2_commit_truncate(struct ocfs2_super *osb,
7029 struct inode *inode,
7030 struct buffer_head *fe_bh,
7031 struct ocfs2_truncate_context *tc)
7033 int status, i, credits, tl_sem = 0;
7034 u32 clusters_to_del, new_highest_cpos, range;
7035 struct ocfs2_extent_list *el;
7036 handle_t *handle = NULL;
7037 struct inode *tl_inode = osb->osb_tl_inode;
7038 struct ocfs2_path *path = NULL;
7039 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
7043 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
7044 i_size_read(inode));
7046 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
7053 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7057 * Check that we still have allocation to delete.
7059 if (OCFS2_I(inode)->ip_clusters == 0) {
7065 * Truncate always works against the rightmost tree branch.
7067 status = ocfs2_find_path(inode, path, UINT_MAX);
7073 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7074 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7077 * By now, el will point to the extent list on the bottom most
7078 * portion of this tree. Only the tail record is considered in
7081 * We handle the following cases, in order:
7082 * - empty extent: delete the remaining branch
7083 * - remove the entire record
7084 * - remove a partial record
7085 * - no record needs to be removed (truncate has completed)
7087 el = path_leaf_el(path);
7088 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7089 ocfs2_error(inode->i_sb,
7090 "Inode %llu has empty extent block at %llu\n",
7091 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7092 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7097 i = le16_to_cpu(el->l_next_free_rec) - 1;
7098 range = le32_to_cpu(el->l_recs[i].e_cpos) +
7099 ocfs2_rec_clusters(el, &el->l_recs[i]);
7100 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7101 clusters_to_del = 0;
7102 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
7103 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
7104 } else if (range > new_highest_cpos) {
7105 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
7106 le32_to_cpu(el->l_recs[i].e_cpos)) -
7113 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7114 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7116 mutex_lock(&tl_inode->i_mutex);
7118 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7119 * record is free for use. If there isn't any, we flush to get
7120 * an empty truncate log. */
7121 if (ocfs2_truncate_log_needs_flush(osb)) {
7122 status = __ocfs2_flush_truncate_log(osb);
7129 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
7130 (struct ocfs2_dinode *)fe_bh->b_data,
7132 handle = ocfs2_start_trans(osb, credits);
7133 if (IS_ERR(handle)) {
7134 status = PTR_ERR(handle);
7140 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7147 mutex_unlock(&tl_inode->i_mutex);
7150 ocfs2_commit_trans(osb, handle);
7153 ocfs2_reinit_path(path, 1);
7156 * The check above will catch the case where we've truncated
7157 * away all allocation.
7163 ocfs2_schedule_truncate_log_flush(osb, 1);
7166 mutex_unlock(&tl_inode->i_mutex);
7169 ocfs2_commit_trans(osb, handle);
7171 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7173 ocfs2_free_path(path);
7175 /* This will drop the ext_alloc cluster lock for us */
7176 ocfs2_free_truncate_context(tc);
7183 * Expects the inode to already be locked.
7185 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7186 struct inode *inode,
7187 struct buffer_head *fe_bh,
7188 struct ocfs2_truncate_context **tc)
7191 unsigned int new_i_clusters;
7192 struct ocfs2_dinode *fe;
7193 struct ocfs2_extent_block *eb;
7194 struct buffer_head *last_eb_bh = NULL;
7200 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7201 i_size_read(inode));
7202 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7204 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7205 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7206 (unsigned long long)le64_to_cpu(fe->i_size));
7208 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7214 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7216 if (fe->id2.i_list.l_tree_depth) {
7217 status = ocfs2_read_extent_block(inode,
7218 le64_to_cpu(fe->i_last_eb_blk),
7224 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7227 (*tc)->tc_last_eb_bh = last_eb_bh;
7233 ocfs2_free_truncate_context(*tc);
7241 * 'start' is inclusive, 'end' is not.
7243 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7244 unsigned int start, unsigned int end, int trunc)
7247 unsigned int numbytes;
7249 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7250 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7251 struct ocfs2_inline_data *idata = &di->id2.i_data;
7253 if (end > i_size_read(inode))
7254 end = i_size_read(inode);
7256 BUG_ON(start >= end);
7258 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7259 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7260 !ocfs2_supports_inline_data(osb)) {
7261 ocfs2_error(inode->i_sb,
7262 "Inline data flags for inode %llu don't agree! "
7263 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7264 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7265 le16_to_cpu(di->i_dyn_features),
7266 OCFS2_I(inode)->ip_dyn_features,
7267 osb->s_feature_incompat);
7272 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7273 if (IS_ERR(handle)) {
7274 ret = PTR_ERR(handle);
7279 ret = ocfs2_journal_access(handle, inode, di_bh,
7280 OCFS2_JOURNAL_ACCESS_WRITE);
7286 numbytes = end - start;
7287 memset(idata->id_data + start, 0, numbytes);
7290 * No need to worry about the data page here - it's been
7291 * truncated already and inline data doesn't need it for
7292 * pushing zero's to disk, so we'll let readpage pick it up
7296 i_size_write(inode, start);
7297 di->i_size = cpu_to_le64(start);
7300 inode->i_blocks = ocfs2_inode_sector_count(inode);
7301 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7303 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7304 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7306 ocfs2_journal_dirty(handle, di_bh);
7309 ocfs2_commit_trans(osb, handle);
7315 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7318 * The caller is responsible for completing deallocation
7319 * before freeing the context.
7321 if (tc->tc_dealloc.c_first_suballocator != NULL)
7323 "Truncate completion has non-empty dealloc context\n");
7325 brelse(tc->tc_last_eb_bh);