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>
32 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33 #include <cluster/masklog.h>
40 #include "extent_map.h"
43 #include "localalloc.h"
50 #include "buffer_head_io.h"
53 * ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract
54 * the b-tree operations in ocfs2. Now all the b-tree operations are not
55 * limited to ocfs2_dinode only. Any data which need to allocate clusters
56 * to store can use b-tree. And it only needs to implement its ocfs2_extent_tree
59 * ocfs2_extent_tree contains info for the root of the b-tree, it must have a
60 * root ocfs2_extent_list and a root_bh so that they can be used in the b-tree
62 * ocfs2_extent_tree_operations abstract the normal operations we do for
63 * the root of extent b-tree.
65 struct ocfs2_extent_tree;
67 struct ocfs2_extent_tree_operations {
68 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
70 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
71 void (*eo_update_clusters)(struct inode *inode,
72 struct ocfs2_extent_tree *et,
74 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
76 /* These are internal to ocfs2_extent_tree and don't have
77 * accessor functions */
78 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
81 struct ocfs2_extent_tree {
82 enum ocfs2_extent_tree_type et_type;
83 struct ocfs2_extent_tree_operations *et_ops;
84 struct buffer_head *et_root_bh;
85 struct ocfs2_extent_list *et_root_el;
87 unsigned int et_max_leaf_clusters;
90 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
92 struct ocfs2_dinode *di = et->et_object;
94 et->et_root_el = &di->id2.i_list;
97 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
100 struct ocfs2_dinode *di = et->et_object;
102 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
103 di->i_last_eb_blk = cpu_to_le64(blkno);
106 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
108 struct ocfs2_dinode *di = et->et_object;
110 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
111 return le64_to_cpu(di->i_last_eb_blk);
114 static void ocfs2_dinode_update_clusters(struct inode *inode,
115 struct ocfs2_extent_tree *et,
118 struct ocfs2_dinode *di = et->et_object;
120 le32_add_cpu(&di->i_clusters, clusters);
121 spin_lock(&OCFS2_I(inode)->ip_lock);
122 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
123 spin_unlock(&OCFS2_I(inode)->ip_lock);
126 static int ocfs2_dinode_sanity_check(struct inode *inode,
127 struct ocfs2_extent_tree *et)
130 struct ocfs2_dinode *di;
132 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
135 if (!OCFS2_IS_VALID_DINODE(di)) {
137 ocfs2_error(inode->i_sb,
138 "Inode %llu has invalid path root",
139 (unsigned long long)OCFS2_I(inode)->ip_blkno);
145 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
146 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
147 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
148 .eo_update_clusters = ocfs2_dinode_update_clusters,
149 .eo_sanity_check = ocfs2_dinode_sanity_check,
150 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
153 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
155 struct ocfs2_xattr_value_root *xv = et->et_object;
157 et->et_root_el = &xv->xr_list;
160 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
163 struct ocfs2_xattr_value_root *xv =
164 (struct ocfs2_xattr_value_root *)et->et_object;
166 xv->xr_last_eb_blk = cpu_to_le64(blkno);
169 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
171 struct ocfs2_xattr_value_root *xv =
172 (struct ocfs2_xattr_value_root *) et->et_object;
174 return le64_to_cpu(xv->xr_last_eb_blk);
177 static void ocfs2_xattr_value_update_clusters(struct inode *inode,
178 struct ocfs2_extent_tree *et,
181 struct ocfs2_xattr_value_root *xv =
182 (struct ocfs2_xattr_value_root *)et->et_object;
184 le32_add_cpu(&xv->xr_clusters, clusters);
187 static int ocfs2_xattr_value_sanity_check(struct inode *inode,
188 struct ocfs2_extent_tree *et)
193 static struct ocfs2_extent_tree_operations ocfs2_xattr_et_ops = {
194 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
195 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
196 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
197 .eo_sanity_check = ocfs2_xattr_value_sanity_check,
198 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
201 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
203 struct ocfs2_xattr_block *xb = et->et_object;
205 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
208 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
211 struct ocfs2_xattr_block *xb = et->et_object;
212 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
214 xt->xt_last_eb_blk = cpu_to_le64(blkno);
217 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
219 struct ocfs2_xattr_block *xb = et->et_object;
220 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
222 return le64_to_cpu(xt->xt_last_eb_blk);
225 static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
226 struct ocfs2_extent_tree *et,
229 struct ocfs2_xattr_block *xb = et->et_object;
231 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
234 static int ocfs2_xattr_tree_sanity_check(struct inode *inode,
235 struct ocfs2_extent_tree *et)
240 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
241 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
242 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
243 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
244 .eo_sanity_check = ocfs2_xattr_tree_sanity_check,
245 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
248 static void ocfs2_get_extent_tree(struct ocfs2_extent_tree *et,
250 struct buffer_head *bh,
251 enum ocfs2_extent_tree_type et_type,
254 et->et_type = et_type;
257 et->et_max_leaf_clusters = 0;
259 obj = (void *)bh->b_data;
262 if (et_type == OCFS2_DINODE_EXTENT) {
263 et->et_ops = &ocfs2_dinode_et_ops;
264 } else if (et_type == OCFS2_XATTR_VALUE_EXTENT) {
265 et->et_ops = &ocfs2_xattr_et_ops;
266 } else if (et_type == OCFS2_XATTR_TREE_EXTENT) {
267 et->et_ops = &ocfs2_xattr_tree_et_ops;
268 et->et_max_leaf_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
269 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
272 et->et_ops->eo_fill_root_el(et);
275 static void ocfs2_put_extent_tree(struct ocfs2_extent_tree *et)
277 brelse(et->et_root_bh);
280 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
283 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
286 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
288 return et->et_ops->eo_get_last_eb_blk(et);
291 static inline void ocfs2_et_update_clusters(struct inode *inode,
292 struct ocfs2_extent_tree *et,
295 et->et_ops->eo_update_clusters(inode, et, clusters);
298 static inline int ocfs2_et_sanity_check(struct inode *inode,
299 struct ocfs2_extent_tree *et)
301 return et->et_ops->eo_sanity_check(inode, et);
304 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
305 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
306 struct ocfs2_extent_block *eb);
309 * Structures which describe a path through a btree, and functions to
312 * The idea here is to be as generic as possible with the tree
315 struct ocfs2_path_item {
316 struct buffer_head *bh;
317 struct ocfs2_extent_list *el;
320 #define OCFS2_MAX_PATH_DEPTH 5
324 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
327 #define path_root_bh(_path) ((_path)->p_node[0].bh)
328 #define path_root_el(_path) ((_path)->p_node[0].el)
329 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
330 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
331 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
334 * Reset the actual path elements so that we can re-use the structure
335 * to build another path. Generally, this involves freeing the buffer
338 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
340 int i, start = 0, depth = 0;
341 struct ocfs2_path_item *node;
346 for(i = start; i < path_num_items(path); i++) {
347 node = &path->p_node[i];
355 * Tree depth may change during truncate, or insert. If we're
356 * keeping the root extent list, then make sure that our path
357 * structure reflects the proper depth.
360 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
362 path->p_tree_depth = depth;
365 static void ocfs2_free_path(struct ocfs2_path *path)
368 ocfs2_reinit_path(path, 0);
374 * All the elements of src into dest. After this call, src could be freed
375 * without affecting dest.
377 * Both paths should have the same root. Any non-root elements of dest
380 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
384 BUG_ON(path_root_bh(dest) != path_root_bh(src));
385 BUG_ON(path_root_el(dest) != path_root_el(src));
387 ocfs2_reinit_path(dest, 1);
389 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
390 dest->p_node[i].bh = src->p_node[i].bh;
391 dest->p_node[i].el = src->p_node[i].el;
393 if (dest->p_node[i].bh)
394 get_bh(dest->p_node[i].bh);
399 * Make the *dest path the same as src and re-initialize src path to
402 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
406 BUG_ON(path_root_bh(dest) != path_root_bh(src));
408 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
409 brelse(dest->p_node[i].bh);
411 dest->p_node[i].bh = src->p_node[i].bh;
412 dest->p_node[i].el = src->p_node[i].el;
414 src->p_node[i].bh = NULL;
415 src->p_node[i].el = NULL;
420 * Insert an extent block at given index.
422 * This will not take an additional reference on eb_bh.
424 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
425 struct buffer_head *eb_bh)
427 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
430 * Right now, no root bh is an extent block, so this helps
431 * catch code errors with dinode trees. The assertion can be
432 * safely removed if we ever need to insert extent block
433 * structures at the root.
437 path->p_node[index].bh = eb_bh;
438 path->p_node[index].el = &eb->h_list;
441 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
442 struct ocfs2_extent_list *root_el)
444 struct ocfs2_path *path;
446 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
448 path = kzalloc(sizeof(*path), GFP_NOFS);
450 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
452 path_root_bh(path) = root_bh;
453 path_root_el(path) = root_el;
460 * Convenience function to journal all components in a path.
462 static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
463 struct ocfs2_path *path)
470 for(i = 0; i < path_num_items(path); i++) {
471 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
472 OCFS2_JOURNAL_ACCESS_WRITE);
484 * Return the index of the extent record which contains cluster #v_cluster.
485 * -1 is returned if it was not found.
487 * Should work fine on interior and exterior nodes.
489 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
493 struct ocfs2_extent_rec *rec;
494 u32 rec_end, rec_start, clusters;
496 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
497 rec = &el->l_recs[i];
499 rec_start = le32_to_cpu(rec->e_cpos);
500 clusters = ocfs2_rec_clusters(el, rec);
502 rec_end = rec_start + clusters;
504 if (v_cluster >= rec_start && v_cluster < rec_end) {
513 enum ocfs2_contig_type {
522 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
523 * ocfs2_extent_contig only work properly against leaf nodes!
525 static int ocfs2_block_extent_contig(struct super_block *sb,
526 struct ocfs2_extent_rec *ext,
529 u64 blk_end = le64_to_cpu(ext->e_blkno);
531 blk_end += ocfs2_clusters_to_blocks(sb,
532 le16_to_cpu(ext->e_leaf_clusters));
534 return blkno == blk_end;
537 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
538 struct ocfs2_extent_rec *right)
542 left_range = le32_to_cpu(left->e_cpos) +
543 le16_to_cpu(left->e_leaf_clusters);
545 return (left_range == le32_to_cpu(right->e_cpos));
548 static enum ocfs2_contig_type
549 ocfs2_extent_contig(struct inode *inode,
550 struct ocfs2_extent_rec *ext,
551 struct ocfs2_extent_rec *insert_rec)
553 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
556 * Refuse to coalesce extent records with different flag
557 * fields - we don't want to mix unwritten extents with user
560 if (ext->e_flags != insert_rec->e_flags)
563 if (ocfs2_extents_adjacent(ext, insert_rec) &&
564 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
567 blkno = le64_to_cpu(ext->e_blkno);
568 if (ocfs2_extents_adjacent(insert_rec, ext) &&
569 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
576 * NOTE: We can have pretty much any combination of contiguousness and
579 * The usefulness of APPEND_TAIL is more in that it lets us know that
580 * we'll have to update the path to that leaf.
582 enum ocfs2_append_type {
587 enum ocfs2_split_type {
593 struct ocfs2_insert_type {
594 enum ocfs2_split_type ins_split;
595 enum ocfs2_append_type ins_appending;
596 enum ocfs2_contig_type ins_contig;
597 int ins_contig_index;
601 struct ocfs2_merge_ctxt {
602 enum ocfs2_contig_type c_contig_type;
603 int c_has_empty_extent;
604 int c_split_covers_rec;
608 * How many free extents have we got before we need more meta data?
610 int ocfs2_num_free_extents(struct ocfs2_super *osb,
612 struct buffer_head *root_bh,
613 enum ocfs2_extent_tree_type type,
617 struct ocfs2_extent_list *el = NULL;
618 struct ocfs2_extent_block *eb;
619 struct buffer_head *eb_bh = NULL;
621 struct ocfs2_extent_tree et;
625 ocfs2_get_extent_tree(&et, inode, root_bh, type, obj);
627 last_eb_blk = ocfs2_et_get_last_eb_blk(&et);
630 retval = ocfs2_read_block(osb, last_eb_blk,
631 &eb_bh, OCFS2_BH_CACHED, inode);
636 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
640 BUG_ON(el->l_tree_depth != 0);
642 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
647 ocfs2_put_extent_tree(&et);
652 /* expects array to already be allocated
654 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
657 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
661 struct ocfs2_alloc_context *meta_ac,
662 struct buffer_head *bhs[])
664 int count, status, i;
665 u16 suballoc_bit_start;
668 struct ocfs2_extent_block *eb;
673 while (count < wanted) {
674 status = ocfs2_claim_metadata(osb,
686 for(i = count; i < (num_got + count); i++) {
687 bhs[i] = sb_getblk(osb->sb, first_blkno);
688 if (bhs[i] == NULL) {
693 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
695 status = ocfs2_journal_access(handle, inode, bhs[i],
696 OCFS2_JOURNAL_ACCESS_CREATE);
702 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
703 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
704 /* Ok, setup the minimal stuff here. */
705 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
706 eb->h_blkno = cpu_to_le64(first_blkno);
707 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
708 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
709 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
711 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
713 suballoc_bit_start++;
716 /* We'll also be dirtied by the caller, so
717 * this isn't absolutely necessary. */
718 status = ocfs2_journal_dirty(handle, bhs[i]);
731 for(i = 0; i < wanted; i++) {
742 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
744 * Returns the sum of the rightmost extent rec logical offset and
747 * ocfs2_add_branch() uses this to determine what logical cluster
748 * value should be populated into the leftmost new branch records.
750 * ocfs2_shift_tree_depth() uses this to determine the # clusters
751 * value for the new topmost tree record.
753 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
757 i = le16_to_cpu(el->l_next_free_rec) - 1;
759 return le32_to_cpu(el->l_recs[i].e_cpos) +
760 ocfs2_rec_clusters(el, &el->l_recs[i]);
764 * Add an entire tree branch to our inode. eb_bh is the extent block
765 * to start at, if we don't want to start the branch at the dinode
768 * last_eb_bh is required as we have to update it's next_leaf pointer
769 * for the new last extent block.
771 * the new branch will be 'empty' in the sense that every block will
772 * contain a single record with cluster count == 0.
774 static int ocfs2_add_branch(struct ocfs2_super *osb,
777 struct ocfs2_extent_tree *et,
778 struct buffer_head *eb_bh,
779 struct buffer_head **last_eb_bh,
780 struct ocfs2_alloc_context *meta_ac)
782 int status, new_blocks, i;
783 u64 next_blkno, new_last_eb_blk;
784 struct buffer_head *bh;
785 struct buffer_head **new_eb_bhs = NULL;
786 struct ocfs2_extent_block *eb;
787 struct ocfs2_extent_list *eb_el;
788 struct ocfs2_extent_list *el;
793 BUG_ON(!last_eb_bh || !*last_eb_bh);
796 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
801 /* we never add a branch to a leaf. */
802 BUG_ON(!el->l_tree_depth);
804 new_blocks = le16_to_cpu(el->l_tree_depth);
806 /* allocate the number of new eb blocks we need */
807 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
815 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
816 meta_ac, new_eb_bhs);
822 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
823 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
825 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
826 * linked with the rest of the tree.
827 * conversly, new_eb_bhs[0] is the new bottommost leaf.
829 * when we leave the loop, new_last_eb_blk will point to the
830 * newest leaf, and next_blkno will point to the topmost extent
832 next_blkno = new_last_eb_blk = 0;
833 for(i = 0; i < new_blocks; i++) {
835 eb = (struct ocfs2_extent_block *) bh->b_data;
836 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
837 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
843 status = ocfs2_journal_access(handle, inode, bh,
844 OCFS2_JOURNAL_ACCESS_CREATE);
850 eb->h_next_leaf_blk = 0;
851 eb_el->l_tree_depth = cpu_to_le16(i);
852 eb_el->l_next_free_rec = cpu_to_le16(1);
854 * This actually counts as an empty extent as
857 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
858 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
860 * eb_el isn't always an interior node, but even leaf
861 * nodes want a zero'd flags and reserved field so
862 * this gets the whole 32 bits regardless of use.
864 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
865 if (!eb_el->l_tree_depth)
866 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
868 status = ocfs2_journal_dirty(handle, bh);
874 next_blkno = le64_to_cpu(eb->h_blkno);
877 /* This is a bit hairy. We want to update up to three blocks
878 * here without leaving any of them in an inconsistent state
879 * in case of error. We don't have to worry about
880 * journal_dirty erroring as it won't unless we've aborted the
881 * handle (in which case we would never be here) so reserving
882 * the write with journal_access is all we need to do. */
883 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
884 OCFS2_JOURNAL_ACCESS_WRITE);
889 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
890 OCFS2_JOURNAL_ACCESS_WRITE);
896 status = ocfs2_journal_access(handle, inode, eb_bh,
897 OCFS2_JOURNAL_ACCESS_WRITE);
904 /* Link the new branch into the rest of the tree (el will
905 * either be on the root_bh, or the extent block passed in. */
906 i = le16_to_cpu(el->l_next_free_rec);
907 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
908 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
909 el->l_recs[i].e_int_clusters = 0;
910 le16_add_cpu(&el->l_next_free_rec, 1);
912 /* fe needs a new last extent block pointer, as does the
913 * next_leaf on the previously last-extent-block. */
914 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
916 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
917 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
919 status = ocfs2_journal_dirty(handle, *last_eb_bh);
922 status = ocfs2_journal_dirty(handle, et->et_root_bh);
926 status = ocfs2_journal_dirty(handle, eb_bh);
932 * Some callers want to track the rightmost leaf so pass it
936 get_bh(new_eb_bhs[0]);
937 *last_eb_bh = new_eb_bhs[0];
942 for (i = 0; i < new_blocks; i++)
944 brelse(new_eb_bhs[i]);
953 * adds another level to the allocation tree.
954 * returns back the new extent block so you can add a branch to it
957 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
960 struct ocfs2_extent_tree *et,
961 struct ocfs2_alloc_context *meta_ac,
962 struct buffer_head **ret_new_eb_bh)
966 struct buffer_head *new_eb_bh = NULL;
967 struct ocfs2_extent_block *eb;
968 struct ocfs2_extent_list *root_el;
969 struct ocfs2_extent_list *eb_el;
973 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
980 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
981 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
982 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
988 root_el = et->et_root_el;
990 status = ocfs2_journal_access(handle, inode, new_eb_bh,
991 OCFS2_JOURNAL_ACCESS_CREATE);
997 /* copy the root extent list data into the new extent block */
998 eb_el->l_tree_depth = root_el->l_tree_depth;
999 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1000 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1001 eb_el->l_recs[i] = root_el->l_recs[i];
1003 status = ocfs2_journal_dirty(handle, new_eb_bh);
1009 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
1010 OCFS2_JOURNAL_ACCESS_WRITE);
1016 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1018 /* update root_bh now */
1019 le16_add_cpu(&root_el->l_tree_depth, 1);
1020 root_el->l_recs[0].e_cpos = 0;
1021 root_el->l_recs[0].e_blkno = eb->h_blkno;
1022 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1023 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1024 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1025 root_el->l_next_free_rec = cpu_to_le16(1);
1027 /* If this is our 1st tree depth shift, then last_eb_blk
1028 * becomes the allocated extent block */
1029 if (root_el->l_tree_depth == cpu_to_le16(1))
1030 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1032 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1038 *ret_new_eb_bh = new_eb_bh;
1050 * Should only be called when there is no space left in any of the
1051 * leaf nodes. What we want to do is find the lowest tree depth
1052 * non-leaf extent block with room for new records. There are three
1053 * valid results of this search:
1055 * 1) a lowest extent block is found, then we pass it back in
1056 * *lowest_eb_bh and return '0'
1058 * 2) the search fails to find anything, but the root_el has room. We
1059 * pass NULL back in *lowest_eb_bh, but still return '0'
1061 * 3) the search fails to find anything AND the root_el is full, in
1062 * which case we return > 0
1064 * return status < 0 indicates an error.
1066 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1067 struct inode *inode,
1068 struct ocfs2_extent_tree *et,
1069 struct buffer_head **target_bh)
1073 struct ocfs2_extent_block *eb;
1074 struct ocfs2_extent_list *el;
1075 struct buffer_head *bh = NULL;
1076 struct buffer_head *lowest_bh = NULL;
1082 el = et->et_root_el;
1084 while(le16_to_cpu(el->l_tree_depth) > 1) {
1085 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1086 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
1087 "extent list (next_free_rec == 0)",
1088 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1092 i = le16_to_cpu(el->l_next_free_rec) - 1;
1093 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1095 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
1096 "list where extent # %d has no physical "
1098 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
1108 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
1115 eb = (struct ocfs2_extent_block *) bh->b_data;
1116 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1117 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1123 if (le16_to_cpu(el->l_next_free_rec) <
1124 le16_to_cpu(el->l_count)) {
1132 /* If we didn't find one and the fe doesn't have any room,
1133 * then return '1' */
1134 el = et->et_root_el;
1135 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1138 *target_bh = lowest_bh;
1148 * Grow a b-tree so that it has more records.
1150 * We might shift the tree depth in which case existing paths should
1151 * be considered invalid.
1153 * Tree depth after the grow is returned via *final_depth.
1155 * *last_eb_bh will be updated by ocfs2_add_branch().
1157 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
1158 struct ocfs2_extent_tree *et, int *final_depth,
1159 struct buffer_head **last_eb_bh,
1160 struct ocfs2_alloc_context *meta_ac)
1163 struct ocfs2_extent_list *el = et->et_root_el;
1164 int depth = le16_to_cpu(el->l_tree_depth);
1165 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1166 struct buffer_head *bh = NULL;
1168 BUG_ON(meta_ac == NULL);
1170 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
1177 /* We traveled all the way to the bottom of the allocation tree
1178 * and didn't find room for any more extents - we need to add
1179 * another tree level */
1182 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1184 /* ocfs2_shift_tree_depth will return us a buffer with
1185 * the new extent block (so we can pass that to
1186 * ocfs2_add_branch). */
1187 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
1196 * Special case: we have room now if we shifted from
1197 * tree_depth 0, so no more work needs to be done.
1199 * We won't be calling add_branch, so pass
1200 * back *last_eb_bh as the new leaf. At depth
1201 * zero, it should always be null so there's
1202 * no reason to brelse.
1204 BUG_ON(*last_eb_bh);
1211 /* call ocfs2_add_branch to add the final part of the tree with
1213 mlog(0, "add branch. bh = %p\n", bh);
1214 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
1223 *final_depth = depth;
1229 * This function will discard the rightmost extent record.
1231 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1233 int next_free = le16_to_cpu(el->l_next_free_rec);
1234 int count = le16_to_cpu(el->l_count);
1235 unsigned int num_bytes;
1238 /* This will cause us to go off the end of our extent list. */
1239 BUG_ON(next_free >= count);
1241 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1243 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1246 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1247 struct ocfs2_extent_rec *insert_rec)
1249 int i, insert_index, next_free, has_empty, num_bytes;
1250 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1251 struct ocfs2_extent_rec *rec;
1253 next_free = le16_to_cpu(el->l_next_free_rec);
1254 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1258 /* The tree code before us didn't allow enough room in the leaf. */
1259 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1262 * The easiest way to approach this is to just remove the
1263 * empty extent and temporarily decrement next_free.
1267 * If next_free was 1 (only an empty extent), this
1268 * loop won't execute, which is fine. We still want
1269 * the decrement above to happen.
1271 for(i = 0; i < (next_free - 1); i++)
1272 el->l_recs[i] = el->l_recs[i+1];
1278 * Figure out what the new record index should be.
1280 for(i = 0; i < next_free; i++) {
1281 rec = &el->l_recs[i];
1283 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1288 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1289 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1291 BUG_ON(insert_index < 0);
1292 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1293 BUG_ON(insert_index > next_free);
1296 * No need to memmove if we're just adding to the tail.
1298 if (insert_index != next_free) {
1299 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1301 num_bytes = next_free - insert_index;
1302 num_bytes *= sizeof(struct ocfs2_extent_rec);
1303 memmove(&el->l_recs[insert_index + 1],
1304 &el->l_recs[insert_index],
1309 * Either we had an empty extent, and need to re-increment or
1310 * there was no empty extent on a non full rightmost leaf node,
1311 * in which case we still need to increment.
1314 el->l_next_free_rec = cpu_to_le16(next_free);
1316 * Make sure none of the math above just messed up our tree.
1318 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1320 el->l_recs[insert_index] = *insert_rec;
1324 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1326 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1328 BUG_ON(num_recs == 0);
1330 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1332 size = num_recs * sizeof(struct ocfs2_extent_rec);
1333 memmove(&el->l_recs[0], &el->l_recs[1], size);
1334 memset(&el->l_recs[num_recs], 0,
1335 sizeof(struct ocfs2_extent_rec));
1336 el->l_next_free_rec = cpu_to_le16(num_recs);
1341 * Create an empty extent record .
1343 * l_next_free_rec may be updated.
1345 * If an empty extent already exists do nothing.
1347 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1349 int next_free = le16_to_cpu(el->l_next_free_rec);
1351 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1356 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1359 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1360 "Asked to create an empty extent in a full list:\n"
1361 "count = %u, tree depth = %u",
1362 le16_to_cpu(el->l_count),
1363 le16_to_cpu(el->l_tree_depth));
1365 ocfs2_shift_records_right(el);
1368 le16_add_cpu(&el->l_next_free_rec, 1);
1369 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1373 * For a rotation which involves two leaf nodes, the "root node" is
1374 * the lowest level tree node which contains a path to both leafs. This
1375 * resulting set of information can be used to form a complete "subtree"
1377 * This function is passed two full paths from the dinode down to a
1378 * pair of adjacent leaves. It's task is to figure out which path
1379 * index contains the subtree root - this can be the root index itself
1380 * in a worst-case rotation.
1382 * The array index of the subtree root is passed back.
1384 static int ocfs2_find_subtree_root(struct inode *inode,
1385 struct ocfs2_path *left,
1386 struct ocfs2_path *right)
1391 * Check that the caller passed in two paths from the same tree.
1393 BUG_ON(path_root_bh(left) != path_root_bh(right));
1399 * The caller didn't pass two adjacent paths.
1401 mlog_bug_on_msg(i > left->p_tree_depth,
1402 "Inode %lu, left depth %u, right depth %u\n"
1403 "left leaf blk %llu, right leaf blk %llu\n",
1404 inode->i_ino, left->p_tree_depth,
1405 right->p_tree_depth,
1406 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1407 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1408 } while (left->p_node[i].bh->b_blocknr ==
1409 right->p_node[i].bh->b_blocknr);
1414 typedef void (path_insert_t)(void *, struct buffer_head *);
1417 * Traverse a btree path in search of cpos, starting at root_el.
1419 * This code can be called with a cpos larger than the tree, in which
1420 * case it will return the rightmost path.
1422 static int __ocfs2_find_path(struct inode *inode,
1423 struct ocfs2_extent_list *root_el, u32 cpos,
1424 path_insert_t *func, void *data)
1429 struct buffer_head *bh = NULL;
1430 struct ocfs2_extent_block *eb;
1431 struct ocfs2_extent_list *el;
1432 struct ocfs2_extent_rec *rec;
1433 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1436 while (el->l_tree_depth) {
1437 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1438 ocfs2_error(inode->i_sb,
1439 "Inode %llu has empty extent list at "
1441 (unsigned long long)oi->ip_blkno,
1442 le16_to_cpu(el->l_tree_depth));
1448 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1449 rec = &el->l_recs[i];
1452 * In the case that cpos is off the allocation
1453 * tree, this should just wind up returning the
1456 range = le32_to_cpu(rec->e_cpos) +
1457 ocfs2_rec_clusters(el, rec);
1458 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1462 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1464 ocfs2_error(inode->i_sb,
1465 "Inode %llu has bad blkno in extent list "
1466 "at depth %u (index %d)\n",
1467 (unsigned long long)oi->ip_blkno,
1468 le16_to_cpu(el->l_tree_depth), i);
1475 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1476 &bh, OCFS2_BH_CACHED, inode);
1482 eb = (struct ocfs2_extent_block *) bh->b_data;
1484 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1485 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1490 if (le16_to_cpu(el->l_next_free_rec) >
1491 le16_to_cpu(el->l_count)) {
1492 ocfs2_error(inode->i_sb,
1493 "Inode %llu has bad count in extent list "
1494 "at block %llu (next free=%u, count=%u)\n",
1495 (unsigned long long)oi->ip_blkno,
1496 (unsigned long long)bh->b_blocknr,
1497 le16_to_cpu(el->l_next_free_rec),
1498 le16_to_cpu(el->l_count));
1509 * Catch any trailing bh that the loop didn't handle.
1517 * Given an initialized path (that is, it has a valid root extent
1518 * list), this function will traverse the btree in search of the path
1519 * which would contain cpos.
1521 * The path traveled is recorded in the path structure.
1523 * Note that this will not do any comparisons on leaf node extent
1524 * records, so it will work fine in the case that we just added a tree
1527 struct find_path_data {
1529 struct ocfs2_path *path;
1531 static void find_path_ins(void *data, struct buffer_head *bh)
1533 struct find_path_data *fp = data;
1536 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1539 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1542 struct find_path_data data;
1546 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1547 find_path_ins, &data);
1550 static void find_leaf_ins(void *data, struct buffer_head *bh)
1552 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1553 struct ocfs2_extent_list *el = &eb->h_list;
1554 struct buffer_head **ret = data;
1556 /* We want to retain only the leaf block. */
1557 if (le16_to_cpu(el->l_tree_depth) == 0) {
1563 * Find the leaf block in the tree which would contain cpos. No
1564 * checking of the actual leaf is done.
1566 * Some paths want to call this instead of allocating a path structure
1567 * and calling ocfs2_find_path().
1569 * This function doesn't handle non btree extent lists.
1571 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1572 u32 cpos, struct buffer_head **leaf_bh)
1575 struct buffer_head *bh = NULL;
1577 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1589 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1591 * Basically, we've moved stuff around at the bottom of the tree and
1592 * we need to fix up the extent records above the changes to reflect
1595 * left_rec: the record on the left.
1596 * left_child_el: is the child list pointed to by left_rec
1597 * right_rec: the record to the right of left_rec
1598 * right_child_el: is the child list pointed to by right_rec
1600 * By definition, this only works on interior nodes.
1602 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1603 struct ocfs2_extent_list *left_child_el,
1604 struct ocfs2_extent_rec *right_rec,
1605 struct ocfs2_extent_list *right_child_el)
1607 u32 left_clusters, right_end;
1610 * Interior nodes never have holes. Their cpos is the cpos of
1611 * the leftmost record in their child list. Their cluster
1612 * count covers the full theoretical range of their child list
1613 * - the range between their cpos and the cpos of the record
1614 * immediately to their right.
1616 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1617 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1618 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1619 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1621 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1622 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1625 * Calculate the rightmost cluster count boundary before
1626 * moving cpos - we will need to adjust clusters after
1627 * updating e_cpos to keep the same highest cluster count.
1629 right_end = le32_to_cpu(right_rec->e_cpos);
1630 right_end += le32_to_cpu(right_rec->e_int_clusters);
1632 right_rec->e_cpos = left_rec->e_cpos;
1633 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1635 right_end -= le32_to_cpu(right_rec->e_cpos);
1636 right_rec->e_int_clusters = cpu_to_le32(right_end);
1640 * Adjust the adjacent root node records involved in a
1641 * rotation. left_el_blkno is passed in as a key so that we can easily
1642 * find it's index in the root list.
1644 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1645 struct ocfs2_extent_list *left_el,
1646 struct ocfs2_extent_list *right_el,
1651 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1652 le16_to_cpu(left_el->l_tree_depth));
1654 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1655 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1660 * The path walking code should have never returned a root and
1661 * two paths which are not adjacent.
1663 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1665 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1666 &root_el->l_recs[i + 1], right_el);
1670 * We've changed a leaf block (in right_path) and need to reflect that
1671 * change back up the subtree.
1673 * This happens in multiple places:
1674 * - When we've moved an extent record from the left path leaf to the right
1675 * path leaf to make room for an empty extent in the left path leaf.
1676 * - When our insert into the right path leaf is at the leftmost edge
1677 * and requires an update of the path immediately to it's left. This
1678 * can occur at the end of some types of rotation and appending inserts.
1679 * - When we've adjusted the last extent record in the left path leaf and the
1680 * 1st extent record in the right path leaf during cross extent block merge.
1682 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1683 struct ocfs2_path *left_path,
1684 struct ocfs2_path *right_path,
1688 struct ocfs2_extent_list *el, *left_el, *right_el;
1689 struct ocfs2_extent_rec *left_rec, *right_rec;
1690 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1693 * Update the counts and position values within all the
1694 * interior nodes to reflect the leaf rotation we just did.
1696 * The root node is handled below the loop.
1698 * We begin the loop with right_el and left_el pointing to the
1699 * leaf lists and work our way up.
1701 * NOTE: within this loop, left_el and right_el always refer
1702 * to the *child* lists.
1704 left_el = path_leaf_el(left_path);
1705 right_el = path_leaf_el(right_path);
1706 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1707 mlog(0, "Adjust records at index %u\n", i);
1710 * One nice property of knowing that all of these
1711 * nodes are below the root is that we only deal with
1712 * the leftmost right node record and the rightmost
1715 el = left_path->p_node[i].el;
1716 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1717 left_rec = &el->l_recs[idx];
1719 el = right_path->p_node[i].el;
1720 right_rec = &el->l_recs[0];
1722 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1725 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1729 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1734 * Setup our list pointers now so that the current
1735 * parents become children in the next iteration.
1737 left_el = left_path->p_node[i].el;
1738 right_el = right_path->p_node[i].el;
1742 * At the root node, adjust the two adjacent records which
1743 * begin our path to the leaves.
1746 el = left_path->p_node[subtree_index].el;
1747 left_el = left_path->p_node[subtree_index + 1].el;
1748 right_el = right_path->p_node[subtree_index + 1].el;
1750 ocfs2_adjust_root_records(el, left_el, right_el,
1751 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1753 root_bh = left_path->p_node[subtree_index].bh;
1755 ret = ocfs2_journal_dirty(handle, root_bh);
1760 static int ocfs2_rotate_subtree_right(struct inode *inode,
1762 struct ocfs2_path *left_path,
1763 struct ocfs2_path *right_path,
1767 struct buffer_head *right_leaf_bh;
1768 struct buffer_head *left_leaf_bh = NULL;
1769 struct buffer_head *root_bh;
1770 struct ocfs2_extent_list *right_el, *left_el;
1771 struct ocfs2_extent_rec move_rec;
1773 left_leaf_bh = path_leaf_bh(left_path);
1774 left_el = path_leaf_el(left_path);
1776 if (left_el->l_next_free_rec != left_el->l_count) {
1777 ocfs2_error(inode->i_sb,
1778 "Inode %llu has non-full interior leaf node %llu"
1780 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1781 (unsigned long long)left_leaf_bh->b_blocknr,
1782 le16_to_cpu(left_el->l_next_free_rec));
1787 * This extent block may already have an empty record, so we
1788 * return early if so.
1790 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1793 root_bh = left_path->p_node[subtree_index].bh;
1794 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1796 ret = ocfs2_journal_access(handle, inode, root_bh,
1797 OCFS2_JOURNAL_ACCESS_WRITE);
1803 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1804 ret = ocfs2_journal_access(handle, inode,
1805 right_path->p_node[i].bh,
1806 OCFS2_JOURNAL_ACCESS_WRITE);
1812 ret = ocfs2_journal_access(handle, inode,
1813 left_path->p_node[i].bh,
1814 OCFS2_JOURNAL_ACCESS_WRITE);
1821 right_leaf_bh = path_leaf_bh(right_path);
1822 right_el = path_leaf_el(right_path);
1824 /* This is a code error, not a disk corruption. */
1825 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1826 "because rightmost leaf block %llu is empty\n",
1827 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1828 (unsigned long long)right_leaf_bh->b_blocknr);
1830 ocfs2_create_empty_extent(right_el);
1832 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1838 /* Do the copy now. */
1839 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1840 move_rec = left_el->l_recs[i];
1841 right_el->l_recs[0] = move_rec;
1844 * Clear out the record we just copied and shift everything
1845 * over, leaving an empty extent in the left leaf.
1847 * We temporarily subtract from next_free_rec so that the
1848 * shift will lose the tail record (which is now defunct).
1850 le16_add_cpu(&left_el->l_next_free_rec, -1);
1851 ocfs2_shift_records_right(left_el);
1852 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1853 le16_add_cpu(&left_el->l_next_free_rec, 1);
1855 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1861 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1869 * Given a full path, determine what cpos value would return us a path
1870 * containing the leaf immediately to the left of the current one.
1872 * Will return zero if the path passed in is already the leftmost path.
1874 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1875 struct ocfs2_path *path, u32 *cpos)
1879 struct ocfs2_extent_list *el;
1881 BUG_ON(path->p_tree_depth == 0);
1885 blkno = path_leaf_bh(path)->b_blocknr;
1887 /* Start at the tree node just above the leaf and work our way up. */
1888 i = path->p_tree_depth - 1;
1890 el = path->p_node[i].el;
1893 * Find the extent record just before the one in our
1896 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1897 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1901 * We've determined that the
1902 * path specified is already
1903 * the leftmost one - return a
1909 * The leftmost record points to our
1910 * leaf - we need to travel up the
1916 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
1917 *cpos = *cpos + ocfs2_rec_clusters(el,
1918 &el->l_recs[j - 1]);
1925 * If we got here, we never found a valid node where
1926 * the tree indicated one should be.
1929 "Invalid extent tree at extent block %llu\n",
1930 (unsigned long long)blkno);
1935 blkno = path->p_node[i].bh->b_blocknr;
1944 * Extend the transaction by enough credits to complete the rotation,
1945 * and still leave at least the original number of credits allocated
1946 * to this transaction.
1948 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
1950 struct ocfs2_path *path)
1952 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
1954 if (handle->h_buffer_credits < credits)
1955 return ocfs2_extend_trans(handle, credits);
1961 * Trap the case where we're inserting into the theoretical range past
1962 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1963 * whose cpos is less than ours into the right leaf.
1965 * It's only necessary to look at the rightmost record of the left
1966 * leaf because the logic that calls us should ensure that the
1967 * theoretical ranges in the path components above the leaves are
1970 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1973 struct ocfs2_extent_list *left_el;
1974 struct ocfs2_extent_rec *rec;
1977 left_el = path_leaf_el(left_path);
1978 next_free = le16_to_cpu(left_el->l_next_free_rec);
1979 rec = &left_el->l_recs[next_free - 1];
1981 if (insert_cpos > le32_to_cpu(rec->e_cpos))
1986 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
1988 int next_free = le16_to_cpu(el->l_next_free_rec);
1990 struct ocfs2_extent_rec *rec;
1995 rec = &el->l_recs[0];
1996 if (ocfs2_is_empty_extent(rec)) {
2000 rec = &el->l_recs[1];
2003 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2004 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2010 * Rotate all the records in a btree right one record, starting at insert_cpos.
2012 * The path to the rightmost leaf should be passed in.
2014 * The array is assumed to be large enough to hold an entire path (tree depth).
2016 * Upon succesful return from this function:
2018 * - The 'right_path' array will contain a path to the leaf block
2019 * whose range contains e_cpos.
2020 * - That leaf block will have a single empty extent in list index 0.
2021 * - In the case that the rotation requires a post-insert update,
2022 * *ret_left_path will contain a valid path which can be passed to
2023 * ocfs2_insert_path().
2025 static int ocfs2_rotate_tree_right(struct inode *inode,
2027 enum ocfs2_split_type split,
2029 struct ocfs2_path *right_path,
2030 struct ocfs2_path **ret_left_path)
2032 int ret, start, orig_credits = handle->h_buffer_credits;
2034 struct ocfs2_path *left_path = NULL;
2036 *ret_left_path = NULL;
2038 left_path = ocfs2_new_path(path_root_bh(right_path),
2039 path_root_el(right_path));
2046 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2052 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2055 * What we want to do here is:
2057 * 1) Start with the rightmost path.
2059 * 2) Determine a path to the leaf block directly to the left
2062 * 3) Determine the 'subtree root' - the lowest level tree node
2063 * which contains a path to both leaves.
2065 * 4) Rotate the subtree.
2067 * 5) Find the next subtree by considering the left path to be
2068 * the new right path.
2070 * The check at the top of this while loop also accepts
2071 * insert_cpos == cpos because cpos is only a _theoretical_
2072 * value to get us the left path - insert_cpos might very well
2073 * be filling that hole.
2075 * Stop at a cpos of '0' because we either started at the
2076 * leftmost branch (i.e., a tree with one branch and a
2077 * rotation inside of it), or we've gone as far as we can in
2078 * rotating subtrees.
2080 while (cpos && insert_cpos <= cpos) {
2081 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2084 ret = ocfs2_find_path(inode, left_path, cpos);
2090 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2091 path_leaf_bh(right_path),
2092 "Inode %lu: error during insert of %u "
2093 "(left path cpos %u) results in two identical "
2094 "paths ending at %llu\n",
2095 inode->i_ino, insert_cpos, cpos,
2096 (unsigned long long)
2097 path_leaf_bh(left_path)->b_blocknr);
2099 if (split == SPLIT_NONE &&
2100 ocfs2_rotate_requires_path_adjustment(left_path,
2104 * We've rotated the tree as much as we
2105 * should. The rest is up to
2106 * ocfs2_insert_path() to complete, after the
2107 * record insertion. We indicate this
2108 * situation by returning the left path.
2110 * The reason we don't adjust the records here
2111 * before the record insert is that an error
2112 * later might break the rule where a parent
2113 * record e_cpos will reflect the actual
2114 * e_cpos of the 1st nonempty record of the
2117 *ret_left_path = left_path;
2121 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2123 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2125 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2126 right_path->p_tree_depth);
2128 ret = ocfs2_extend_rotate_transaction(handle, start,
2129 orig_credits, right_path);
2135 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2142 if (split != SPLIT_NONE &&
2143 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2146 * A rotate moves the rightmost left leaf
2147 * record over to the leftmost right leaf
2148 * slot. If we're doing an extent split
2149 * instead of a real insert, then we have to
2150 * check that the extent to be split wasn't
2151 * just moved over. If it was, then we can
2152 * exit here, passing left_path back -
2153 * ocfs2_split_extent() is smart enough to
2154 * search both leaves.
2156 *ret_left_path = left_path;
2161 * There is no need to re-read the next right path
2162 * as we know that it'll be our current left
2163 * path. Optimize by copying values instead.
2165 ocfs2_mv_path(right_path, left_path);
2167 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2176 ocfs2_free_path(left_path);
2182 static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2183 struct ocfs2_path *path)
2186 struct ocfs2_extent_rec *rec;
2187 struct ocfs2_extent_list *el;
2188 struct ocfs2_extent_block *eb;
2191 /* Path should always be rightmost. */
2192 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2193 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2196 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2197 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2198 rec = &el->l_recs[idx];
2199 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2201 for (i = 0; i < path->p_tree_depth; i++) {
2202 el = path->p_node[i].el;
2203 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2204 rec = &el->l_recs[idx];
2206 rec->e_int_clusters = cpu_to_le32(range);
2207 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2209 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2213 static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2214 struct ocfs2_cached_dealloc_ctxt *dealloc,
2215 struct ocfs2_path *path, int unlink_start)
2218 struct ocfs2_extent_block *eb;
2219 struct ocfs2_extent_list *el;
2220 struct buffer_head *bh;
2222 for(i = unlink_start; i < path_num_items(path); i++) {
2223 bh = path->p_node[i].bh;
2225 eb = (struct ocfs2_extent_block *)bh->b_data;
2227 * Not all nodes might have had their final count
2228 * decremented by the caller - handle this here.
2231 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2233 "Inode %llu, attempted to remove extent block "
2234 "%llu with %u records\n",
2235 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2236 (unsigned long long)le64_to_cpu(eb->h_blkno),
2237 le16_to_cpu(el->l_next_free_rec));
2239 ocfs2_journal_dirty(handle, bh);
2240 ocfs2_remove_from_cache(inode, bh);
2244 el->l_next_free_rec = 0;
2245 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2247 ocfs2_journal_dirty(handle, bh);
2249 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2253 ocfs2_remove_from_cache(inode, bh);
2257 static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2258 struct ocfs2_path *left_path,
2259 struct ocfs2_path *right_path,
2261 struct ocfs2_cached_dealloc_ctxt *dealloc)
2264 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2265 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2266 struct ocfs2_extent_list *el;
2267 struct ocfs2_extent_block *eb;
2269 el = path_leaf_el(left_path);
2271 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2273 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2274 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2277 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2279 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2280 le16_add_cpu(&root_el->l_next_free_rec, -1);
2282 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2283 eb->h_next_leaf_blk = 0;
2285 ocfs2_journal_dirty(handle, root_bh);
2286 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2288 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2292 static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2293 struct ocfs2_path *left_path,
2294 struct ocfs2_path *right_path,
2296 struct ocfs2_cached_dealloc_ctxt *dealloc,
2298 struct ocfs2_extent_tree *et)
2300 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2301 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2302 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2303 struct ocfs2_extent_block *eb;
2307 right_leaf_el = path_leaf_el(right_path);
2308 left_leaf_el = path_leaf_el(left_path);
2309 root_bh = left_path->p_node[subtree_index].bh;
2310 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2312 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2315 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2316 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2318 * It's legal for us to proceed if the right leaf is
2319 * the rightmost one and it has an empty extent. There
2320 * are two cases to handle - whether the leaf will be
2321 * empty after removal or not. If the leaf isn't empty
2322 * then just remove the empty extent up front. The
2323 * next block will handle empty leaves by flagging
2326 * Non rightmost leaves will throw -EAGAIN and the
2327 * caller can manually move the subtree and retry.
2330 if (eb->h_next_leaf_blk != 0ULL)
2333 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2334 ret = ocfs2_journal_access(handle, inode,
2335 path_leaf_bh(right_path),
2336 OCFS2_JOURNAL_ACCESS_WRITE);
2342 ocfs2_remove_empty_extent(right_leaf_el);
2344 right_has_empty = 1;
2347 if (eb->h_next_leaf_blk == 0ULL &&
2348 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2350 * We have to update i_last_eb_blk during the meta
2353 ret = ocfs2_journal_access(handle, inode, et_root_bh,
2354 OCFS2_JOURNAL_ACCESS_WRITE);
2360 del_right_subtree = 1;
2364 * Getting here with an empty extent in the right path implies
2365 * that it's the rightmost path and will be deleted.
2367 BUG_ON(right_has_empty && !del_right_subtree);
2369 ret = ocfs2_journal_access(handle, inode, root_bh,
2370 OCFS2_JOURNAL_ACCESS_WRITE);
2376 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2377 ret = ocfs2_journal_access(handle, inode,
2378 right_path->p_node[i].bh,
2379 OCFS2_JOURNAL_ACCESS_WRITE);
2385 ret = ocfs2_journal_access(handle, inode,
2386 left_path->p_node[i].bh,
2387 OCFS2_JOURNAL_ACCESS_WRITE);
2394 if (!right_has_empty) {
2396 * Only do this if we're moving a real
2397 * record. Otherwise, the action is delayed until
2398 * after removal of the right path in which case we
2399 * can do a simple shift to remove the empty extent.
2401 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2402 memset(&right_leaf_el->l_recs[0], 0,
2403 sizeof(struct ocfs2_extent_rec));
2405 if (eb->h_next_leaf_blk == 0ULL) {
2407 * Move recs over to get rid of empty extent, decrease
2408 * next_free. This is allowed to remove the last
2409 * extent in our leaf (setting l_next_free_rec to
2410 * zero) - the delete code below won't care.
2412 ocfs2_remove_empty_extent(right_leaf_el);
2415 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2418 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2422 if (del_right_subtree) {
2423 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2424 subtree_index, dealloc);
2425 ocfs2_update_edge_lengths(inode, handle, left_path);
2427 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2428 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2431 * Removal of the extent in the left leaf was skipped
2432 * above so we could delete the right path
2435 if (right_has_empty)
2436 ocfs2_remove_empty_extent(left_leaf_el);
2438 ret = ocfs2_journal_dirty(handle, et_root_bh);
2444 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2452 * Given a full path, determine what cpos value would return us a path
2453 * containing the leaf immediately to the right of the current one.
2455 * Will return zero if the path passed in is already the rightmost path.
2457 * This looks similar, but is subtly different to
2458 * ocfs2_find_cpos_for_left_leaf().
2460 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2461 struct ocfs2_path *path, u32 *cpos)
2465 struct ocfs2_extent_list *el;
2469 if (path->p_tree_depth == 0)
2472 blkno = path_leaf_bh(path)->b_blocknr;
2474 /* Start at the tree node just above the leaf and work our way up. */
2475 i = path->p_tree_depth - 1;
2479 el = path->p_node[i].el;
2482 * Find the extent record just after the one in our
2485 next_free = le16_to_cpu(el->l_next_free_rec);
2486 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2487 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2488 if (j == (next_free - 1)) {
2491 * We've determined that the
2492 * path specified is already
2493 * the rightmost one - return a
2499 * The rightmost record points to our
2500 * leaf - we need to travel up the
2506 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2512 * If we got here, we never found a valid node where
2513 * the tree indicated one should be.
2516 "Invalid extent tree at extent block %llu\n",
2517 (unsigned long long)blkno);
2522 blkno = path->p_node[i].bh->b_blocknr;
2530 static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2532 struct buffer_head *bh,
2533 struct ocfs2_extent_list *el)
2537 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2540 ret = ocfs2_journal_access(handle, inode, bh,
2541 OCFS2_JOURNAL_ACCESS_WRITE);
2547 ocfs2_remove_empty_extent(el);
2549 ret = ocfs2_journal_dirty(handle, bh);
2557 static int __ocfs2_rotate_tree_left(struct inode *inode,
2558 handle_t *handle, int orig_credits,
2559 struct ocfs2_path *path,
2560 struct ocfs2_cached_dealloc_ctxt *dealloc,
2561 struct ocfs2_path **empty_extent_path,
2562 struct ocfs2_extent_tree *et)
2564 int ret, subtree_root, deleted;
2566 struct ocfs2_path *left_path = NULL;
2567 struct ocfs2_path *right_path = NULL;
2569 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2571 *empty_extent_path = NULL;
2573 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2580 left_path = ocfs2_new_path(path_root_bh(path),
2581 path_root_el(path));
2588 ocfs2_cp_path(left_path, path);
2590 right_path = ocfs2_new_path(path_root_bh(path),
2591 path_root_el(path));
2598 while (right_cpos) {
2599 ret = ocfs2_find_path(inode, right_path, right_cpos);
2605 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2608 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2610 (unsigned long long)
2611 right_path->p_node[subtree_root].bh->b_blocknr,
2612 right_path->p_tree_depth);
2614 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2615 orig_credits, left_path);
2622 * Caller might still want to make changes to the
2623 * tree root, so re-add it to the journal here.
2625 ret = ocfs2_journal_access(handle, inode,
2626 path_root_bh(left_path),
2627 OCFS2_JOURNAL_ACCESS_WRITE);
2633 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2634 right_path, subtree_root,
2635 dealloc, &deleted, et);
2636 if (ret == -EAGAIN) {
2638 * The rotation has to temporarily stop due to
2639 * the right subtree having an empty
2640 * extent. Pass it back to the caller for a
2643 *empty_extent_path = right_path;
2653 * The subtree rotate might have removed records on
2654 * the rightmost edge. If so, then rotation is
2660 ocfs2_mv_path(left_path, right_path);
2662 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2671 ocfs2_free_path(right_path);
2672 ocfs2_free_path(left_path);
2677 static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2678 struct ocfs2_path *path,
2679 struct ocfs2_cached_dealloc_ctxt *dealloc,
2680 struct ocfs2_extent_tree *et)
2682 int ret, subtree_index;
2684 struct ocfs2_path *left_path = NULL;
2685 struct ocfs2_extent_block *eb;
2686 struct ocfs2_extent_list *el;
2689 ret = ocfs2_et_sanity_check(inode, et);
2693 * There's two ways we handle this depending on
2694 * whether path is the only existing one.
2696 ret = ocfs2_extend_rotate_transaction(handle, 0,
2697 handle->h_buffer_credits,
2704 ret = ocfs2_journal_access_path(inode, handle, path);
2710 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2718 * We have a path to the left of this one - it needs
2721 left_path = ocfs2_new_path(path_root_bh(path),
2722 path_root_el(path));
2729 ret = ocfs2_find_path(inode, left_path, cpos);
2735 ret = ocfs2_journal_access_path(inode, handle, left_path);
2741 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2743 ocfs2_unlink_subtree(inode, handle, left_path, path,
2744 subtree_index, dealloc);
2745 ocfs2_update_edge_lengths(inode, handle, left_path);
2747 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2748 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2751 * 'path' is also the leftmost path which
2752 * means it must be the only one. This gets
2753 * handled differently because we want to
2754 * revert the inode back to having extents
2757 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2759 el = et->et_root_el;
2760 el->l_tree_depth = 0;
2761 el->l_next_free_rec = 0;
2762 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2764 ocfs2_et_set_last_eb_blk(et, 0);
2767 ocfs2_journal_dirty(handle, path_root_bh(path));
2770 ocfs2_free_path(left_path);
2775 * Left rotation of btree records.
2777 * In many ways, this is (unsurprisingly) the opposite of right
2778 * rotation. We start at some non-rightmost path containing an empty
2779 * extent in the leaf block. The code works its way to the rightmost
2780 * path by rotating records to the left in every subtree.
2782 * This is used by any code which reduces the number of extent records
2783 * in a leaf. After removal, an empty record should be placed in the
2784 * leftmost list position.
2786 * This won't handle a length update of the rightmost path records if
2787 * the rightmost tree leaf record is removed so the caller is
2788 * responsible for detecting and correcting that.
2790 static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2791 struct ocfs2_path *path,
2792 struct ocfs2_cached_dealloc_ctxt *dealloc,
2793 struct ocfs2_extent_tree *et)
2795 int ret, orig_credits = handle->h_buffer_credits;
2796 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2797 struct ocfs2_extent_block *eb;
2798 struct ocfs2_extent_list *el;
2800 el = path_leaf_el(path);
2801 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2804 if (path->p_tree_depth == 0) {
2805 rightmost_no_delete:
2807 * Inline extents. This is trivially handled, so do
2810 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2812 path_leaf_el(path));
2819 * Handle rightmost branch now. There's several cases:
2820 * 1) simple rotation leaving records in there. That's trivial.
2821 * 2) rotation requiring a branch delete - there's no more
2822 * records left. Two cases of this:
2823 * a) There are branches to the left.
2824 * b) This is also the leftmost (the only) branch.
2826 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2827 * 2a) we need the left branch so that we can update it with the unlink
2828 * 2b) we need to bring the inode back to inline extents.
2831 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2833 if (eb->h_next_leaf_blk == 0) {
2835 * This gets a bit tricky if we're going to delete the
2836 * rightmost path. Get the other cases out of the way
2839 if (le16_to_cpu(el->l_next_free_rec) > 1)
2840 goto rightmost_no_delete;
2842 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2844 ocfs2_error(inode->i_sb,
2845 "Inode %llu has empty extent block at %llu",
2846 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2847 (unsigned long long)le64_to_cpu(eb->h_blkno));
2852 * XXX: The caller can not trust "path" any more after
2853 * this as it will have been deleted. What do we do?
2855 * In theory the rotate-for-merge code will never get
2856 * here because it'll always ask for a rotate in a
2860 ret = ocfs2_remove_rightmost_path(inode, handle, path,
2868 * Now we can loop, remembering the path we get from -EAGAIN
2869 * and restarting from there.
2872 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
2873 dealloc, &restart_path, et);
2874 if (ret && ret != -EAGAIN) {
2879 while (ret == -EAGAIN) {
2880 tmp_path = restart_path;
2881 restart_path = NULL;
2883 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2886 if (ret && ret != -EAGAIN) {
2891 ocfs2_free_path(tmp_path);
2899 ocfs2_free_path(tmp_path);
2900 ocfs2_free_path(restart_path);
2904 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2907 struct ocfs2_extent_rec *rec = &el->l_recs[index];
2910 if (rec->e_leaf_clusters == 0) {
2912 * We consumed all of the merged-from record. An empty
2913 * extent cannot exist anywhere but the 1st array
2914 * position, so move things over if the merged-from
2915 * record doesn't occupy that position.
2917 * This creates a new empty extent so the caller
2918 * should be smart enough to have removed any existing
2922 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
2923 size = index * sizeof(struct ocfs2_extent_rec);
2924 memmove(&el->l_recs[1], &el->l_recs[0], size);
2928 * Always memset - the caller doesn't check whether it
2929 * created an empty extent, so there could be junk in
2932 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2936 static int ocfs2_get_right_path(struct inode *inode,
2937 struct ocfs2_path *left_path,
2938 struct ocfs2_path **ret_right_path)
2942 struct ocfs2_path *right_path = NULL;
2943 struct ocfs2_extent_list *left_el;
2945 *ret_right_path = NULL;
2947 /* This function shouldn't be called for non-trees. */
2948 BUG_ON(left_path->p_tree_depth == 0);
2950 left_el = path_leaf_el(left_path);
2951 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
2953 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2960 /* This function shouldn't be called for the rightmost leaf. */
2961 BUG_ON(right_cpos == 0);
2963 right_path = ocfs2_new_path(path_root_bh(left_path),
2964 path_root_el(left_path));
2971 ret = ocfs2_find_path(inode, right_path, right_cpos);
2977 *ret_right_path = right_path;
2980 ocfs2_free_path(right_path);
2985 * Remove split_rec clusters from the record at index and merge them
2986 * onto the beginning of the record "next" to it.
2987 * For index < l_count - 1, the next means the extent rec at index + 1.
2988 * For index == l_count - 1, the "next" means the 1st extent rec of the
2989 * next extent block.
2991 static int ocfs2_merge_rec_right(struct inode *inode,
2992 struct ocfs2_path *left_path,
2994 struct ocfs2_extent_rec *split_rec,
2997 int ret, next_free, i;
2998 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
2999 struct ocfs2_extent_rec *left_rec;
3000 struct ocfs2_extent_rec *right_rec;
3001 struct ocfs2_extent_list *right_el;
3002 struct ocfs2_path *right_path = NULL;
3003 int subtree_index = 0;
3004 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3005 struct buffer_head *bh = path_leaf_bh(left_path);
3006 struct buffer_head *root_bh = NULL;
3008 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3009 left_rec = &el->l_recs[index];
3011 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3012 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3013 /* we meet with a cross extent block merge. */
3014 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3020 right_el = path_leaf_el(right_path);
3021 next_free = le16_to_cpu(right_el->l_next_free_rec);
3022 BUG_ON(next_free <= 0);
3023 right_rec = &right_el->l_recs[0];
3024 if (ocfs2_is_empty_extent(right_rec)) {
3025 BUG_ON(next_free <= 1);
3026 right_rec = &right_el->l_recs[1];
3029 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3030 le16_to_cpu(left_rec->e_leaf_clusters) !=
3031 le32_to_cpu(right_rec->e_cpos));
3033 subtree_index = ocfs2_find_subtree_root(inode,
3034 left_path, right_path);
3036 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3037 handle->h_buffer_credits,
3044 root_bh = left_path->p_node[subtree_index].bh;
3045 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3047 ret = ocfs2_journal_access(handle, inode, root_bh,
3048 OCFS2_JOURNAL_ACCESS_WRITE);
3054 for (i = subtree_index + 1;
3055 i < path_num_items(right_path); i++) {
3056 ret = ocfs2_journal_access(handle, inode,
3057 right_path->p_node[i].bh,
3058 OCFS2_JOURNAL_ACCESS_WRITE);
3064 ret = ocfs2_journal_access(handle, inode,
3065 left_path->p_node[i].bh,
3066 OCFS2_JOURNAL_ACCESS_WRITE);
3074 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3075 right_rec = &el->l_recs[index + 1];
3078 ret = ocfs2_journal_access(handle, inode, bh,
3079 OCFS2_JOURNAL_ACCESS_WRITE);
3085 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3087 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3088 le64_add_cpu(&right_rec->e_blkno,
3089 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3090 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3092 ocfs2_cleanup_merge(el, index);
3094 ret = ocfs2_journal_dirty(handle, bh);
3099 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3103 ocfs2_complete_edge_insert(inode, handle, left_path,
3104 right_path, subtree_index);
3108 ocfs2_free_path(right_path);
3112 static int ocfs2_get_left_path(struct inode *inode,
3113 struct ocfs2_path *right_path,
3114 struct ocfs2_path **ret_left_path)
3118 struct ocfs2_path *left_path = NULL;
3120 *ret_left_path = NULL;
3122 /* This function shouldn't be called for non-trees. */
3123 BUG_ON(right_path->p_tree_depth == 0);
3125 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3126 right_path, &left_cpos);
3132 /* This function shouldn't be called for the leftmost leaf. */
3133 BUG_ON(left_cpos == 0);
3135 left_path = ocfs2_new_path(path_root_bh(right_path),
3136 path_root_el(right_path));
3143 ret = ocfs2_find_path(inode, left_path, left_cpos);
3149 *ret_left_path = left_path;
3152 ocfs2_free_path(left_path);
3157 * Remove split_rec clusters from the record at index and merge them
3158 * onto the tail of the record "before" it.
3159 * For index > 0, the "before" means the extent rec at index - 1.
3161 * For index == 0, the "before" means the last record of the previous
3162 * extent block. And there is also a situation that we may need to
3163 * remove the rightmost leaf extent block in the right_path and change
3164 * the right path to indicate the new rightmost path.
3166 static int ocfs2_merge_rec_left(struct inode *inode,
3167 struct ocfs2_path *right_path,
3169 struct ocfs2_extent_rec *split_rec,
3170 struct ocfs2_cached_dealloc_ctxt *dealloc,
3171 struct ocfs2_extent_tree *et,
3174 int ret, i, subtree_index = 0, has_empty_extent = 0;
3175 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3176 struct ocfs2_extent_rec *left_rec;
3177 struct ocfs2_extent_rec *right_rec;
3178 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3179 struct buffer_head *bh = path_leaf_bh(right_path);
3180 struct buffer_head *root_bh = NULL;
3181 struct ocfs2_path *left_path = NULL;
3182 struct ocfs2_extent_list *left_el;
3186 right_rec = &el->l_recs[index];
3188 /* we meet with a cross extent block merge. */
3189 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3195 left_el = path_leaf_el(left_path);
3196 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3197 le16_to_cpu(left_el->l_count));
3199 left_rec = &left_el->l_recs[
3200 le16_to_cpu(left_el->l_next_free_rec) - 1];
3201 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3202 le16_to_cpu(left_rec->e_leaf_clusters) !=
3203 le32_to_cpu(split_rec->e_cpos));
3205 subtree_index = ocfs2_find_subtree_root(inode,
3206 left_path, right_path);
3208 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3209 handle->h_buffer_credits,
3216 root_bh = left_path->p_node[subtree_index].bh;
3217 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3219 ret = ocfs2_journal_access(handle, inode, root_bh,
3220 OCFS2_JOURNAL_ACCESS_WRITE);
3226 for (i = subtree_index + 1;
3227 i < path_num_items(right_path); i++) {
3228 ret = ocfs2_journal_access(handle, inode,
3229 right_path->p_node[i].bh,
3230 OCFS2_JOURNAL_ACCESS_WRITE);
3236 ret = ocfs2_journal_access(handle, inode,
3237 left_path->p_node[i].bh,
3238 OCFS2_JOURNAL_ACCESS_WRITE);
3245 left_rec = &el->l_recs[index - 1];
3246 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3247 has_empty_extent = 1;
3250 ret = ocfs2_journal_access(handle, inode, bh,
3251 OCFS2_JOURNAL_ACCESS_WRITE);
3257 if (has_empty_extent && index == 1) {
3259 * The easy case - we can just plop the record right in.
3261 *left_rec = *split_rec;
3263 has_empty_extent = 0;
3265 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3267 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3268 le64_add_cpu(&right_rec->e_blkno,
3269 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3270 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3272 ocfs2_cleanup_merge(el, index);
3274 ret = ocfs2_journal_dirty(handle, bh);
3279 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3284 * In the situation that the right_rec is empty and the extent
3285 * block is empty also, ocfs2_complete_edge_insert can't handle
3286 * it and we need to delete the right extent block.
3288 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3289 le16_to_cpu(el->l_next_free_rec) == 1) {
3291 ret = ocfs2_remove_rightmost_path(inode, handle,
3299 /* Now the rightmost extent block has been deleted.
3300 * So we use the new rightmost path.
3302 ocfs2_mv_path(right_path, left_path);
3305 ocfs2_complete_edge_insert(inode, handle, left_path,
3306 right_path, subtree_index);
3310 ocfs2_free_path(left_path);
3314 static int ocfs2_try_to_merge_extent(struct inode *inode,
3316 struct ocfs2_path *path,
3318 struct ocfs2_extent_rec *split_rec,
3319 struct ocfs2_cached_dealloc_ctxt *dealloc,
3320 struct ocfs2_merge_ctxt *ctxt,
3321 struct ocfs2_extent_tree *et)
3325 struct ocfs2_extent_list *el = path_leaf_el(path);
3326 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3328 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3330 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3332 * The merge code will need to create an empty
3333 * extent to take the place of the newly
3334 * emptied slot. Remove any pre-existing empty
3335 * extents - having more than one in a leaf is
3338 ret = ocfs2_rotate_tree_left(inode, handle, path,
3345 rec = &el->l_recs[split_index];
3348 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3350 * Left-right contig implies this.
3352 BUG_ON(!ctxt->c_split_covers_rec);
3355 * Since the leftright insert always covers the entire
3356 * extent, this call will delete the insert record
3357 * entirely, resulting in an empty extent record added to
3360 * Since the adding of an empty extent shifts
3361 * everything back to the right, there's no need to
3362 * update split_index here.
3364 * When the split_index is zero, we need to merge it to the
3365 * prevoius extent block. It is more efficient and easier
3366 * if we do merge_right first and merge_left later.
3368 ret = ocfs2_merge_rec_right(inode, path,
3377 * We can only get this from logic error above.
3379 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3381 /* The merge left us with an empty extent, remove it. */
3382 ret = ocfs2_rotate_tree_left(inode, handle, path,
3389 rec = &el->l_recs[split_index];
3392 * Note that we don't pass split_rec here on purpose -
3393 * we've merged it into the rec already.
3395 ret = ocfs2_merge_rec_left(inode, path,
3405 ret = ocfs2_rotate_tree_left(inode, handle, path,
3408 * Error from this last rotate is not critical, so
3409 * print but don't bubble it up.
3416 * Merge a record to the left or right.
3418 * 'contig_type' is relative to the existing record,
3419 * so for example, if we're "right contig", it's to
3420 * the record on the left (hence the left merge).
3422 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3423 ret = ocfs2_merge_rec_left(inode,
3433 ret = ocfs2_merge_rec_right(inode,
3443 if (ctxt->c_split_covers_rec) {
3445 * The merge may have left an empty extent in
3446 * our leaf. Try to rotate it away.
3448 ret = ocfs2_rotate_tree_left(inode, handle, path,
3460 static void ocfs2_subtract_from_rec(struct super_block *sb,
3461 enum ocfs2_split_type split,
3462 struct ocfs2_extent_rec *rec,
3463 struct ocfs2_extent_rec *split_rec)
3467 len_blocks = ocfs2_clusters_to_blocks(sb,
3468 le16_to_cpu(split_rec->e_leaf_clusters));
3470 if (split == SPLIT_LEFT) {
3472 * Region is on the left edge of the existing
3475 le32_add_cpu(&rec->e_cpos,
3476 le16_to_cpu(split_rec->e_leaf_clusters));
3477 le64_add_cpu(&rec->e_blkno, len_blocks);
3478 le16_add_cpu(&rec->e_leaf_clusters,
3479 -le16_to_cpu(split_rec->e_leaf_clusters));
3482 * Region is on the right edge of the existing
3485 le16_add_cpu(&rec->e_leaf_clusters,
3486 -le16_to_cpu(split_rec->e_leaf_clusters));
3491 * Do the final bits of extent record insertion at the target leaf
3492 * list. If this leaf is part of an allocation tree, it is assumed
3493 * that the tree above has been prepared.
3495 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3496 struct ocfs2_extent_list *el,
3497 struct ocfs2_insert_type *insert,
3498 struct inode *inode)
3500 int i = insert->ins_contig_index;
3502 struct ocfs2_extent_rec *rec;
3504 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3506 if (insert->ins_split != SPLIT_NONE) {
3507 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3509 rec = &el->l_recs[i];
3510 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3516 * Contiguous insert - either left or right.
3518 if (insert->ins_contig != CONTIG_NONE) {
3519 rec = &el->l_recs[i];
3520 if (insert->ins_contig == CONTIG_LEFT) {
3521 rec->e_blkno = insert_rec->e_blkno;
3522 rec->e_cpos = insert_rec->e_cpos;
3524 le16_add_cpu(&rec->e_leaf_clusters,
3525 le16_to_cpu(insert_rec->e_leaf_clusters));
3530 * Handle insert into an empty leaf.
3532 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3533 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3534 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3535 el->l_recs[0] = *insert_rec;
3536 el->l_next_free_rec = cpu_to_le16(1);
3543 if (insert->ins_appending == APPEND_TAIL) {
3544 i = le16_to_cpu(el->l_next_free_rec) - 1;
3545 rec = &el->l_recs[i];
3546 range = le32_to_cpu(rec->e_cpos)
3547 + le16_to_cpu(rec->e_leaf_clusters);
3548 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3550 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3551 le16_to_cpu(el->l_count),
3552 "inode %lu, depth %u, count %u, next free %u, "
3553 "rec.cpos %u, rec.clusters %u, "
3554 "insert.cpos %u, insert.clusters %u\n",
3556 le16_to_cpu(el->l_tree_depth),
3557 le16_to_cpu(el->l_count),
3558 le16_to_cpu(el->l_next_free_rec),
3559 le32_to_cpu(el->l_recs[i].e_cpos),
3560 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3561 le32_to_cpu(insert_rec->e_cpos),
3562 le16_to_cpu(insert_rec->e_leaf_clusters));
3564 el->l_recs[i] = *insert_rec;
3565 le16_add_cpu(&el->l_next_free_rec, 1);
3571 * Ok, we have to rotate.
3573 * At this point, it is safe to assume that inserting into an
3574 * empty leaf and appending to a leaf have both been handled
3577 * This leaf needs to have space, either by the empty 1st
3578 * extent record, or by virtue of an l_next_rec < l_count.
3580 ocfs2_rotate_leaf(el, insert_rec);
3583 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3585 struct ocfs2_path *path,
3586 struct ocfs2_extent_rec *insert_rec)
3588 int ret, i, next_free;
3589 struct buffer_head *bh;
3590 struct ocfs2_extent_list *el;
3591 struct ocfs2_extent_rec *rec;
3594 * Update everything except the leaf block.
3596 for (i = 0; i < path->p_tree_depth; i++) {
3597 bh = path->p_node[i].bh;
3598 el = path->p_node[i].el;
3600 next_free = le16_to_cpu(el->l_next_free_rec);
3601 if (next_free == 0) {
3602 ocfs2_error(inode->i_sb,
3603 "Dinode %llu has a bad extent list",
3604 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3609 rec = &el->l_recs[next_free - 1];
3611 rec->e_int_clusters = insert_rec->e_cpos;
3612 le32_add_cpu(&rec->e_int_clusters,
3613 le16_to_cpu(insert_rec->e_leaf_clusters));
3614 le32_add_cpu(&rec->e_int_clusters,
3615 -le32_to_cpu(rec->e_cpos));
3617 ret = ocfs2_journal_dirty(handle, bh);
3624 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3625 struct ocfs2_extent_rec *insert_rec,
3626 struct ocfs2_path *right_path,
3627 struct ocfs2_path **ret_left_path)
3630 struct ocfs2_extent_list *el;
3631 struct ocfs2_path *left_path = NULL;
3633 *ret_left_path = NULL;
3636 * This shouldn't happen for non-trees. The extent rec cluster
3637 * count manipulation below only works for interior nodes.
3639 BUG_ON(right_path->p_tree_depth == 0);
3642 * If our appending insert is at the leftmost edge of a leaf,
3643 * then we might need to update the rightmost records of the
3646 el = path_leaf_el(right_path);
3647 next_free = le16_to_cpu(el->l_next_free_rec);
3648 if (next_free == 0 ||
3649 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3652 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3659 mlog(0, "Append may need a left path update. cpos: %u, "
3660 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3664 * No need to worry if the append is already in the
3668 left_path = ocfs2_new_path(path_root_bh(right_path),
3669 path_root_el(right_path));
3676 ret = ocfs2_find_path(inode, left_path, left_cpos);
3683 * ocfs2_insert_path() will pass the left_path to the
3689 ret = ocfs2_journal_access_path(inode, handle, right_path);
3695 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
3697 *ret_left_path = left_path;
3701 ocfs2_free_path(left_path);
3706 static void ocfs2_split_record(struct inode *inode,
3707 struct ocfs2_path *left_path,
3708 struct ocfs2_path *right_path,
3709 struct ocfs2_extent_rec *split_rec,
3710 enum ocfs2_split_type split)
3713 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3714 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3715 struct ocfs2_extent_rec *rec, *tmprec;
3717 right_el = path_leaf_el(right_path);;
3719 left_el = path_leaf_el(left_path);
3722 insert_el = right_el;
3723 index = ocfs2_search_extent_list(el, cpos);
3725 if (index == 0 && left_path) {
3726 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3729 * This typically means that the record
3730 * started in the left path but moved to the
3731 * right as a result of rotation. We either
3732 * move the existing record to the left, or we
3733 * do the later insert there.
3735 * In this case, the left path should always
3736 * exist as the rotate code will have passed
3737 * it back for a post-insert update.
3740 if (split == SPLIT_LEFT) {
3742 * It's a left split. Since we know
3743 * that the rotate code gave us an
3744 * empty extent in the left path, we
3745 * can just do the insert there.
3747 insert_el = left_el;
3750 * Right split - we have to move the
3751 * existing record over to the left
3752 * leaf. The insert will be into the
3753 * newly created empty extent in the
3756 tmprec = &right_el->l_recs[index];
3757 ocfs2_rotate_leaf(left_el, tmprec);
3760 memset(tmprec, 0, sizeof(*tmprec));
3761 index = ocfs2_search_extent_list(left_el, cpos);
3762 BUG_ON(index == -1);
3767 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3769 * Left path is easy - we can just allow the insert to
3773 insert_el = left_el;
3774 index = ocfs2_search_extent_list(el, cpos);
3775 BUG_ON(index == -1);
3778 rec = &el->l_recs[index];
3779 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3780 ocfs2_rotate_leaf(insert_el, split_rec);
3784 * This function only does inserts on an allocation b-tree. For tree
3785 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3787 * right_path is the path we want to do the actual insert
3788 * in. left_path should only be passed in if we need to update that
3789 * portion of the tree after an edge insert.
3791 static int ocfs2_insert_path(struct inode *inode,
3793 struct ocfs2_path *left_path,
3794 struct ocfs2_path *right_path,
3795 struct ocfs2_extent_rec *insert_rec,
3796 struct ocfs2_insert_type *insert)
3798 int ret, subtree_index;
3799 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
3802 int credits = handle->h_buffer_credits;
3805 * There's a chance that left_path got passed back to
3806 * us without being accounted for in the
3807 * journal. Extend our transaction here to be sure we
3808 * can change those blocks.
3810 credits += left_path->p_tree_depth;
3812 ret = ocfs2_extend_trans(handle, credits);
3818 ret = ocfs2_journal_access_path(inode, handle, left_path);
3826 * Pass both paths to the journal. The majority of inserts
3827 * will be touching all components anyway.
3829 ret = ocfs2_journal_access_path(inode, handle, right_path);
3835 if (insert->ins_split != SPLIT_NONE) {
3837 * We could call ocfs2_insert_at_leaf() for some types
3838 * of splits, but it's easier to just let one separate
3839 * function sort it all out.
3841 ocfs2_split_record(inode, left_path, right_path,
3842 insert_rec, insert->ins_split);
3845 * Split might have modified either leaf and we don't
3846 * have a guarantee that the later edge insert will
3847 * dirty this for us.
3850 ret = ocfs2_journal_dirty(handle,
3851 path_leaf_bh(left_path));
3855 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3858 ret = ocfs2_journal_dirty(handle, leaf_bh);
3864 * The rotate code has indicated that we need to fix
3865 * up portions of the tree after the insert.
3867 * XXX: Should we extend the transaction here?
3869 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3871 ocfs2_complete_edge_insert(inode, handle, left_path,
3872 right_path, subtree_index);
3880 static int ocfs2_do_insert_extent(struct inode *inode,
3882 struct ocfs2_extent_tree *et,
3883 struct ocfs2_extent_rec *insert_rec,
3884 struct ocfs2_insert_type *type)
3886 int ret, rotate = 0;
3888 struct ocfs2_path *right_path = NULL;
3889 struct ocfs2_path *left_path = NULL;
3890 struct ocfs2_extent_list *el;
3892 el = et->et_root_el;
3894 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
3895 OCFS2_JOURNAL_ACCESS_WRITE);
3901 if (le16_to_cpu(el->l_tree_depth) == 0) {
3902 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3903 goto out_update_clusters;
3906 right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
3914 * Determine the path to start with. Rotations need the
3915 * rightmost path, everything else can go directly to the
3918 cpos = le32_to_cpu(insert_rec->e_cpos);
3919 if (type->ins_appending == APPEND_NONE &&
3920 type->ins_contig == CONTIG_NONE) {
3925 ret = ocfs2_find_path(inode, right_path, cpos);
3932 * Rotations and appends need special treatment - they modify
3933 * parts of the tree's above them.
3935 * Both might pass back a path immediate to the left of the
3936 * one being inserted to. This will be cause
3937 * ocfs2_insert_path() to modify the rightmost records of
3938 * left_path to account for an edge insert.
3940 * XXX: When modifying this code, keep in mind that an insert
3941 * can wind up skipping both of these two special cases...
3944 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
3945 le32_to_cpu(insert_rec->e_cpos),
3946 right_path, &left_path);
3953 * ocfs2_rotate_tree_right() might have extended the
3954 * transaction without re-journaling our tree root.
3956 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
3957 OCFS2_JOURNAL_ACCESS_WRITE);
3962 } else if (type->ins_appending == APPEND_TAIL
3963 && type->ins_contig != CONTIG_LEFT) {
3964 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
3965 right_path, &left_path);
3972 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
3979 out_update_clusters:
3980 if (type->ins_split == SPLIT_NONE)
3981 ocfs2_et_update_clusters(inode, et,
3982 le16_to_cpu(insert_rec->e_leaf_clusters));
3984 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
3989 ocfs2_free_path(left_path);
3990 ocfs2_free_path(right_path);
3995 static enum ocfs2_contig_type
3996 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
3997 struct ocfs2_extent_list *el, int index,
3998 struct ocfs2_extent_rec *split_rec)
4001 enum ocfs2_contig_type ret = CONTIG_NONE;
4002 u32 left_cpos, right_cpos;
4003 struct ocfs2_extent_rec *rec = NULL;
4004 struct ocfs2_extent_list *new_el;
4005 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4006 struct buffer_head *bh;
4007 struct ocfs2_extent_block *eb;
4010 rec = &el->l_recs[index - 1];
4011 } else if (path->p_tree_depth > 0) {
4012 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4017 if (left_cpos != 0) {
4018 left_path = ocfs2_new_path(path_root_bh(path),
4019 path_root_el(path));
4023 status = ocfs2_find_path(inode, left_path, left_cpos);
4027 new_el = path_leaf_el(left_path);
4029 if (le16_to_cpu(new_el->l_next_free_rec) !=
4030 le16_to_cpu(new_el->l_count)) {
4031 bh = path_leaf_bh(left_path);
4032 eb = (struct ocfs2_extent_block *)bh->b_data;
4033 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4037 rec = &new_el->l_recs[
4038 le16_to_cpu(new_el->l_next_free_rec) - 1];
4043 * We're careful to check for an empty extent record here -
4044 * the merge code will know what to do if it sees one.
4047 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4048 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4051 ret = ocfs2_extent_contig(inode, rec, split_rec);
4056 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4057 rec = &el->l_recs[index + 1];
4058 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4059 path->p_tree_depth > 0) {
4060 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4065 if (right_cpos == 0)
4068 right_path = ocfs2_new_path(path_root_bh(path),
4069 path_root_el(path));
4073 status = ocfs2_find_path(inode, right_path, right_cpos);
4077 new_el = path_leaf_el(right_path);
4078 rec = &new_el->l_recs[0];
4079 if (ocfs2_is_empty_extent(rec)) {
4080 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4081 bh = path_leaf_bh(right_path);
4082 eb = (struct ocfs2_extent_block *)bh->b_data;
4083 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4087 rec = &new_el->l_recs[1];
4092 enum ocfs2_contig_type contig_type;
4094 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4096 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4097 ret = CONTIG_LEFTRIGHT;
4098 else if (ret == CONTIG_NONE)
4104 ocfs2_free_path(left_path);
4106 ocfs2_free_path(right_path);
4111 static void ocfs2_figure_contig_type(struct inode *inode,
4112 struct ocfs2_insert_type *insert,
4113 struct ocfs2_extent_list *el,
4114 struct ocfs2_extent_rec *insert_rec,
4115 struct ocfs2_extent_tree *et)
4118 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4120 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4122 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4123 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4125 if (contig_type != CONTIG_NONE) {
4126 insert->ins_contig_index = i;
4130 insert->ins_contig = contig_type;
4132 if (insert->ins_contig != CONTIG_NONE) {
4133 struct ocfs2_extent_rec *rec =
4134 &el->l_recs[insert->ins_contig_index];
4135 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4136 le16_to_cpu(insert_rec->e_leaf_clusters);
4139 * Caller might want us to limit the size of extents, don't
4140 * calculate contiguousness if we might exceed that limit.
4142 if (et->et_max_leaf_clusters &&
4143 (len > et->et_max_leaf_clusters))
4144 insert->ins_contig = CONTIG_NONE;
4149 * This should only be called against the righmost leaf extent list.
4151 * ocfs2_figure_appending_type() will figure out whether we'll have to
4152 * insert at the tail of the rightmost leaf.
4154 * This should also work against the root extent list for tree's with 0
4155 * depth. If we consider the root extent list to be the rightmost leaf node
4156 * then the logic here makes sense.
4158 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4159 struct ocfs2_extent_list *el,
4160 struct ocfs2_extent_rec *insert_rec)
4163 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4164 struct ocfs2_extent_rec *rec;
4166 insert->ins_appending = APPEND_NONE;
4168 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4170 if (!el->l_next_free_rec)
4171 goto set_tail_append;
4173 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4174 /* Were all records empty? */
4175 if (le16_to_cpu(el->l_next_free_rec) == 1)
4176 goto set_tail_append;
4179 i = le16_to_cpu(el->l_next_free_rec) - 1;
4180 rec = &el->l_recs[i];
4183 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4184 goto set_tail_append;
4189 insert->ins_appending = APPEND_TAIL;
4193 * Helper function called at the begining of an insert.
4195 * This computes a few things that are commonly used in the process of
4196 * inserting into the btree:
4197 * - Whether the new extent is contiguous with an existing one.
4198 * - The current tree depth.
4199 * - Whether the insert is an appending one.
4200 * - The total # of free records in the tree.
4202 * All of the information is stored on the ocfs2_insert_type
4205 static int ocfs2_figure_insert_type(struct inode *inode,
4206 struct ocfs2_extent_tree *et,
4207 struct buffer_head **last_eb_bh,
4208 struct ocfs2_extent_rec *insert_rec,
4210 struct ocfs2_insert_type *insert)
4213 struct ocfs2_extent_block *eb;
4214 struct ocfs2_extent_list *el;
4215 struct ocfs2_path *path = NULL;
4216 struct buffer_head *bh = NULL;
4218 insert->ins_split = SPLIT_NONE;
4220 el = et->et_root_el;
4221 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4223 if (el->l_tree_depth) {
4225 * If we have tree depth, we read in the
4226 * rightmost extent block ahead of time as
4227 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4228 * may want it later.
4230 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4231 ocfs2_et_get_last_eb_blk(et), &bh,
4232 OCFS2_BH_CACHED, inode);
4237 eb = (struct ocfs2_extent_block *) bh->b_data;
4242 * Unless we have a contiguous insert, we'll need to know if
4243 * there is room left in our allocation tree for another
4246 * XXX: This test is simplistic, we can search for empty
4247 * extent records too.
4249 *free_records = le16_to_cpu(el->l_count) -
4250 le16_to_cpu(el->l_next_free_rec);
4252 if (!insert->ins_tree_depth) {
4253 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4254 ocfs2_figure_appending_type(insert, el, insert_rec);
4258 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4266 * In the case that we're inserting past what the tree
4267 * currently accounts for, ocfs2_find_path() will return for
4268 * us the rightmost tree path. This is accounted for below in
4269 * the appending code.
4271 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4277 el = path_leaf_el(path);
4280 * Now that we have the path, there's two things we want to determine:
4281 * 1) Contiguousness (also set contig_index if this is so)
4283 * 2) Are we doing an append? We can trivially break this up
4284 * into two types of appends: simple record append, or a
4285 * rotate inside the tail leaf.
4287 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4290 * The insert code isn't quite ready to deal with all cases of
4291 * left contiguousness. Specifically, if it's an insert into
4292 * the 1st record in a leaf, it will require the adjustment of
4293 * cluster count on the last record of the path directly to it's
4294 * left. For now, just catch that case and fool the layers
4295 * above us. This works just fine for tree_depth == 0, which
4296 * is why we allow that above.
4298 if (insert->ins_contig == CONTIG_LEFT &&
4299 insert->ins_contig_index == 0)
4300 insert->ins_contig = CONTIG_NONE;
4303 * Ok, so we can simply compare against last_eb to figure out
4304 * whether the path doesn't exist. This will only happen in
4305 * the case that we're doing a tail append, so maybe we can
4306 * take advantage of that information somehow.
4308 if (ocfs2_et_get_last_eb_blk(et) ==
4309 path_leaf_bh(path)->b_blocknr) {
4311 * Ok, ocfs2_find_path() returned us the rightmost
4312 * tree path. This might be an appending insert. There are
4314 * 1) We're doing a true append at the tail:
4315 * -This might even be off the end of the leaf
4316 * 2) We're "appending" by rotating in the tail
4318 ocfs2_figure_appending_type(insert, el, insert_rec);
4322 ocfs2_free_path(path);
4332 * Insert an extent into an inode btree.
4334 * The caller needs to update fe->i_clusters
4336 static int ocfs2_insert_extent(struct ocfs2_super *osb,
4338 struct inode *inode,
4339 struct buffer_head *root_bh,
4344 struct ocfs2_alloc_context *meta_ac,
4345 struct ocfs2_extent_tree *et)
4348 int uninitialized_var(free_records);
4349 struct buffer_head *last_eb_bh = NULL;
4350 struct ocfs2_insert_type insert = {0, };
4351 struct ocfs2_extent_rec rec;
4353 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
4355 mlog(0, "add %u clusters at position %u to inode %llu\n",
4356 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4358 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
4359 (OCFS2_I(inode)->ip_clusters != cpos),
4360 "Device %s, asking for sparse allocation: inode %llu, "
4361 "cpos %u, clusters %u\n",
4363 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
4364 OCFS2_I(inode)->ip_clusters);
4366 memset(&rec, 0, sizeof(rec));
4367 rec.e_cpos = cpu_to_le32(cpos);
4368 rec.e_blkno = cpu_to_le64(start_blk);
4369 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4370 rec.e_flags = flags;
4372 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4373 &free_records, &insert);
4379 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4380 "Insert.contig_index: %d, Insert.free_records: %d, "
4381 "Insert.tree_depth: %d\n",
4382 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4383 free_records, insert.ins_tree_depth);
4385 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4386 status = ocfs2_grow_tree(inode, handle, et,
4387 &insert.ins_tree_depth, &last_eb_bh,
4395 /* Finally, we can add clusters. This might rotate the tree for us. */
4396 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4399 else if (et->et_type == OCFS2_DINODE_EXTENT)
4400 ocfs2_extent_map_insert_rec(inode, &rec);
4410 int ocfs2_dinode_insert_extent(struct ocfs2_super *osb,
4412 struct inode *inode,
4413 struct buffer_head *root_bh,
4418 struct ocfs2_alloc_context *meta_ac)
4421 struct ocfs2_extent_tree et;
4423 ocfs2_get_extent_tree(&et, inode, root_bh, OCFS2_DINODE_EXTENT,
4425 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4426 cpos, start_blk, new_clusters,
4427 flags, meta_ac, &et);
4428 ocfs2_put_extent_tree(&et);
4433 int ocfs2_xattr_value_insert_extent(struct ocfs2_super *osb,
4435 struct inode *inode,
4436 struct buffer_head *root_bh,
4441 struct ocfs2_alloc_context *meta_ac,
4445 struct ocfs2_extent_tree et;
4447 ocfs2_get_extent_tree(&et, inode, root_bh,
4448 OCFS2_XATTR_VALUE_EXTENT, obj);
4449 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4450 cpos, start_blk, new_clusters,
4451 flags, meta_ac, &et);
4452 ocfs2_put_extent_tree(&et);
4457 int ocfs2_xattr_tree_insert_extent(struct ocfs2_super *osb,
4459 struct inode *inode,
4460 struct buffer_head *root_bh,
4465 struct ocfs2_alloc_context *meta_ac)
4468 struct ocfs2_extent_tree et;
4470 ocfs2_get_extent_tree(&et, inode, root_bh, OCFS2_XATTR_TREE_EXTENT,
4472 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4473 cpos, start_blk, new_clusters,
4474 flags, meta_ac, &et);
4475 ocfs2_put_extent_tree(&et);
4481 * Allcate and add clusters into the extent b-tree.
4482 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4483 * The extent b-tree's root is root_el and it should be in root_bh, and
4484 * it is not limited to the file storage. Any extent tree can use this
4485 * function if it implements the proper ocfs2_extent_tree.
4487 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4488 struct inode *inode,
4489 u32 *logical_offset,
4490 u32 clusters_to_add,
4492 struct buffer_head *root_bh,
4493 struct ocfs2_extent_list *root_el,
4495 struct ocfs2_alloc_context *data_ac,
4496 struct ocfs2_alloc_context *meta_ac,
4497 enum ocfs2_alloc_restarted *reason_ret,
4498 enum ocfs2_extent_tree_type type,
4503 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4504 u32 bit_off, num_bits;
4508 BUG_ON(!clusters_to_add);
4511 flags = OCFS2_EXT_UNWRITTEN;
4513 free_extents = ocfs2_num_free_extents(osb, inode, root_bh, type,
4515 if (free_extents < 0) {
4516 status = free_extents;
4521 /* there are two cases which could cause us to EAGAIN in the
4522 * we-need-more-metadata case:
4523 * 1) we haven't reserved *any*
4524 * 2) we are so fragmented, we've needed to add metadata too
4526 if (!free_extents && !meta_ac) {
4527 mlog(0, "we haven't reserved any metadata!\n");
4529 reason = RESTART_META;
4531 } else if ((!free_extents)
4532 && (ocfs2_alloc_context_bits_left(meta_ac)
4533 < ocfs2_extend_meta_needed(root_el))) {
4534 mlog(0, "filesystem is really fragmented...\n");
4536 reason = RESTART_META;
4540 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4541 clusters_to_add, &bit_off, &num_bits);
4543 if (status != -ENOSPC)
4548 BUG_ON(num_bits > clusters_to_add);
4550 /* reserve our write early -- insert_extent may update the inode */
4551 status = ocfs2_journal_access(handle, inode, root_bh,
4552 OCFS2_JOURNAL_ACCESS_WRITE);
4558 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4559 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4560 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4561 if (type == OCFS2_DINODE_EXTENT)
4562 status = ocfs2_dinode_insert_extent(osb, handle, inode, root_bh,
4563 *logical_offset, block,
4564 num_bits, flags, meta_ac);
4565 else if (type == OCFS2_XATTR_TREE_EXTENT)
4566 status = ocfs2_xattr_tree_insert_extent(osb, handle,
4569 block, num_bits, flags,
4572 status = ocfs2_xattr_value_insert_extent(osb, handle,
4575 block, num_bits, flags,
4582 status = ocfs2_journal_dirty(handle, root_bh);
4588 clusters_to_add -= num_bits;
4589 *logical_offset += num_bits;
4591 if (clusters_to_add) {
4592 mlog(0, "need to alloc once more, wanted = %u\n",
4595 reason = RESTART_TRANS;
4601 *reason_ret = reason;
4605 static void ocfs2_make_right_split_rec(struct super_block *sb,
4606 struct ocfs2_extent_rec *split_rec,
4608 struct ocfs2_extent_rec *rec)
4610 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4611 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4613 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4615 split_rec->e_cpos = cpu_to_le32(cpos);
4616 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4618 split_rec->e_blkno = rec->e_blkno;
4619 le64_add_cpu(&split_rec->e_blkno,
4620 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4622 split_rec->e_flags = rec->e_flags;
4625 static int ocfs2_split_and_insert(struct inode *inode,
4627 struct ocfs2_path *path,
4628 struct ocfs2_extent_tree *et,
4629 struct buffer_head **last_eb_bh,
4631 struct ocfs2_extent_rec *orig_split_rec,
4632 struct ocfs2_alloc_context *meta_ac)
4635 unsigned int insert_range, rec_range, do_leftright = 0;
4636 struct ocfs2_extent_rec tmprec;
4637 struct ocfs2_extent_list *rightmost_el;
4638 struct ocfs2_extent_rec rec;
4639 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4640 struct ocfs2_insert_type insert;
4641 struct ocfs2_extent_block *eb;
4645 * Store a copy of the record on the stack - it might move
4646 * around as the tree is manipulated below.
4648 rec = path_leaf_el(path)->l_recs[split_index];
4650 rightmost_el = et->et_root_el;
4652 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4654 BUG_ON(!(*last_eb_bh));
4655 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4656 rightmost_el = &eb->h_list;
4659 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4660 le16_to_cpu(rightmost_el->l_count)) {
4661 ret = ocfs2_grow_tree(inode, handle, et,
4662 &depth, last_eb_bh, meta_ac);
4669 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4670 insert.ins_appending = APPEND_NONE;
4671 insert.ins_contig = CONTIG_NONE;
4672 insert.ins_tree_depth = depth;
4674 insert_range = le32_to_cpu(split_rec.e_cpos) +
4675 le16_to_cpu(split_rec.e_leaf_clusters);
4676 rec_range = le32_to_cpu(rec.e_cpos) +
4677 le16_to_cpu(rec.e_leaf_clusters);
4679 if (split_rec.e_cpos == rec.e_cpos) {
4680 insert.ins_split = SPLIT_LEFT;
4681 } else if (insert_range == rec_range) {
4682 insert.ins_split = SPLIT_RIGHT;
4685 * Left/right split. We fake this as a right split
4686 * first and then make a second pass as a left split.
4688 insert.ins_split = SPLIT_RIGHT;
4690 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4695 BUG_ON(do_leftright);
4699 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4705 if (do_leftright == 1) {
4707 struct ocfs2_extent_list *el;
4710 split_rec = *orig_split_rec;
4712 ocfs2_reinit_path(path, 1);
4714 cpos = le32_to_cpu(split_rec.e_cpos);
4715 ret = ocfs2_find_path(inode, path, cpos);
4721 el = path_leaf_el(path);
4722 split_index = ocfs2_search_extent_list(el, cpos);
4731 * Mark part or all of the extent record at split_index in the leaf
4732 * pointed to by path as written. This removes the unwritten
4735 * Care is taken to handle contiguousness so as to not grow the tree.
4737 * meta_ac is not strictly necessary - we only truly need it if growth
4738 * of the tree is required. All other cases will degrade into a less
4739 * optimal tree layout.
4741 * last_eb_bh should be the rightmost leaf block for any extent
4742 * btree. Since a split may grow the tree or a merge might shrink it,
4743 * the caller cannot trust the contents of that buffer after this call.
4745 * This code is optimized for readability - several passes might be
4746 * made over certain portions of the tree. All of those blocks will
4747 * have been brought into cache (and pinned via the journal), so the
4748 * extra overhead is not expressed in terms of disk reads.
4750 static int __ocfs2_mark_extent_written(struct inode *inode,
4751 struct ocfs2_extent_tree *et,
4753 struct ocfs2_path *path,
4755 struct ocfs2_extent_rec *split_rec,
4756 struct ocfs2_alloc_context *meta_ac,
4757 struct ocfs2_cached_dealloc_ctxt *dealloc)
4760 struct ocfs2_extent_list *el = path_leaf_el(path);
4761 struct buffer_head *last_eb_bh = NULL;
4762 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4763 struct ocfs2_merge_ctxt ctxt;
4764 struct ocfs2_extent_list *rightmost_el;
4766 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
4772 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4773 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4774 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4780 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
4785 * The core merge / split code wants to know how much room is
4786 * left in this inodes allocation tree, so we pass the
4787 * rightmost extent list.
4789 if (path->p_tree_depth) {
4790 struct ocfs2_extent_block *eb;
4792 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4793 ocfs2_et_get_last_eb_blk(et),
4794 &last_eb_bh, OCFS2_BH_CACHED, inode);
4800 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4801 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4802 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4807 rightmost_el = &eb->h_list;
4809 rightmost_el = path_root_el(path);
4811 if (rec->e_cpos == split_rec->e_cpos &&
4812 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4813 ctxt.c_split_covers_rec = 1;
4815 ctxt.c_split_covers_rec = 0;
4817 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4819 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4820 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4821 ctxt.c_split_covers_rec);
4823 if (ctxt.c_contig_type == CONTIG_NONE) {
4824 if (ctxt.c_split_covers_rec)
4825 el->l_recs[split_index] = *split_rec;
4827 ret = ocfs2_split_and_insert(inode, handle, path, et,
4828 &last_eb_bh, split_index,
4829 split_rec, meta_ac);
4833 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4834 split_index, split_rec,
4835 dealloc, &ctxt, et);
4846 * Mark the already-existing extent at cpos as written for len clusters.
4848 * If the existing extent is larger than the request, initiate a
4849 * split. An attempt will be made at merging with adjacent extents.
4851 * The caller is responsible for passing down meta_ac if we'll need it.
4853 int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh,
4854 handle_t *handle, u32 cpos, u32 len, u32 phys,
4855 struct ocfs2_alloc_context *meta_ac,
4856 struct ocfs2_cached_dealloc_ctxt *dealloc,
4857 enum ocfs2_extent_tree_type et_type,
4861 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4862 struct ocfs2_extent_rec split_rec;
4863 struct ocfs2_path *left_path = NULL;
4864 struct ocfs2_extent_list *el;
4865 struct ocfs2_extent_tree et;
4867 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4868 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4870 ocfs2_get_extent_tree(&et, inode, root_bh, et_type, obj);
4872 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4873 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4874 "that are being written to, but the feature bit "
4875 "is not set in the super block.",
4876 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4882 * XXX: This should be fixed up so that we just re-insert the
4883 * next extent records.
4885 if (et_type == OCFS2_DINODE_EXTENT)
4886 ocfs2_extent_map_trunc(inode, 0);
4888 left_path = ocfs2_new_path(et.et_root_bh, et.et_root_el);
4895 ret = ocfs2_find_path(inode, left_path, cpos);
4900 el = path_leaf_el(left_path);
4902 index = ocfs2_search_extent_list(el, cpos);
4903 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4904 ocfs2_error(inode->i_sb,
4905 "Inode %llu has an extent at cpos %u which can no "
4906 "longer be found.\n",
4907 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4912 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4913 split_rec.e_cpos = cpu_to_le32(cpos);
4914 split_rec.e_leaf_clusters = cpu_to_le16(len);
4915 split_rec.e_blkno = cpu_to_le64(start_blkno);
4916 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4917 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4919 ret = __ocfs2_mark_extent_written(inode, &et, handle, left_path,
4920 index, &split_rec, meta_ac,
4926 ocfs2_free_path(left_path);
4927 ocfs2_put_extent_tree(&et);
4931 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
4932 handle_t *handle, struct ocfs2_path *path,
4933 int index, u32 new_range,
4934 struct ocfs2_alloc_context *meta_ac)
4936 int ret, depth, credits = handle->h_buffer_credits;
4937 struct buffer_head *last_eb_bh = NULL;
4938 struct ocfs2_extent_block *eb;
4939 struct ocfs2_extent_list *rightmost_el, *el;
4940 struct ocfs2_extent_rec split_rec;
4941 struct ocfs2_extent_rec *rec;
4942 struct ocfs2_insert_type insert;
4945 * Setup the record to split before we grow the tree.
4947 el = path_leaf_el(path);
4948 rec = &el->l_recs[index];
4949 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4951 depth = path->p_tree_depth;
4953 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4954 ocfs2_et_get_last_eb_blk(et),
4955 &last_eb_bh, OCFS2_BH_CACHED, inode);
4961 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4962 rightmost_el = &eb->h_list;
4964 rightmost_el = path_leaf_el(path);
4966 credits += path->p_tree_depth +
4967 ocfs2_extend_meta_needed(et->et_root_el);
4968 ret = ocfs2_extend_trans(handle, credits);
4974 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4975 le16_to_cpu(rightmost_el->l_count)) {
4976 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
4984 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4985 insert.ins_appending = APPEND_NONE;
4986 insert.ins_contig = CONTIG_NONE;
4987 insert.ins_split = SPLIT_RIGHT;
4988 insert.ins_tree_depth = depth;
4990 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4999 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5000 struct ocfs2_path *path, int index,
5001 struct ocfs2_cached_dealloc_ctxt *dealloc,
5003 struct ocfs2_extent_tree *et)
5006 u32 left_cpos, rec_range, trunc_range;
5007 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5008 struct super_block *sb = inode->i_sb;
5009 struct ocfs2_path *left_path = NULL;
5010 struct ocfs2_extent_list *el = path_leaf_el(path);
5011 struct ocfs2_extent_rec *rec;
5012 struct ocfs2_extent_block *eb;
5014 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5015 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5024 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5025 path->p_tree_depth) {
5027 * Check whether this is the rightmost tree record. If
5028 * we remove all of this record or part of its right
5029 * edge then an update of the record lengths above it
5032 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5033 if (eb->h_next_leaf_blk == 0)
5034 is_rightmost_tree_rec = 1;
5037 rec = &el->l_recs[index];
5038 if (index == 0 && path->p_tree_depth &&
5039 le32_to_cpu(rec->e_cpos) == cpos) {
5041 * Changing the leftmost offset (via partial or whole
5042 * record truncate) of an interior (or rightmost) path
5043 * means we have to update the subtree that is formed
5044 * by this leaf and the one to it's left.
5046 * There are two cases we can skip:
5047 * 1) Path is the leftmost one in our inode tree.
5048 * 2) The leaf is rightmost and will be empty after
5049 * we remove the extent record - the rotate code
5050 * knows how to update the newly formed edge.
5053 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5060 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5061 left_path = ocfs2_new_path(path_root_bh(path),
5062 path_root_el(path));
5069 ret = ocfs2_find_path(inode, left_path, left_cpos);
5077 ret = ocfs2_extend_rotate_transaction(handle, 0,
5078 handle->h_buffer_credits,
5085 ret = ocfs2_journal_access_path(inode, handle, path);
5091 ret = ocfs2_journal_access_path(inode, handle, left_path);
5097 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5098 trunc_range = cpos + len;
5100 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5103 memset(rec, 0, sizeof(*rec));
5104 ocfs2_cleanup_merge(el, index);
5107 next_free = le16_to_cpu(el->l_next_free_rec);
5108 if (is_rightmost_tree_rec && next_free > 1) {
5110 * We skip the edge update if this path will
5111 * be deleted by the rotate code.
5113 rec = &el->l_recs[next_free - 1];
5114 ocfs2_adjust_rightmost_records(inode, handle, path,
5117 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5118 /* Remove leftmost portion of the record. */
5119 le32_add_cpu(&rec->e_cpos, len);
5120 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5121 le16_add_cpu(&rec->e_leaf_clusters, -len);
5122 } else if (rec_range == trunc_range) {
5123 /* Remove rightmost portion of the record */
5124 le16_add_cpu(&rec->e_leaf_clusters, -len);
5125 if (is_rightmost_tree_rec)
5126 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5128 /* Caller should have trapped this. */
5129 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5130 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5131 le32_to_cpu(rec->e_cpos),
5132 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5139 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5140 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5144 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5146 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5153 ocfs2_free_path(left_path);
5157 int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
5158 u32 cpos, u32 len, handle_t *handle,
5159 struct ocfs2_alloc_context *meta_ac,
5160 struct ocfs2_cached_dealloc_ctxt *dealloc,
5161 enum ocfs2_extent_tree_type et_type,
5165 u32 rec_range, trunc_range;
5166 struct ocfs2_extent_rec *rec;
5167 struct ocfs2_extent_list *el;
5168 struct ocfs2_path *path = NULL;
5169 struct ocfs2_extent_tree et;
5171 ocfs2_get_extent_tree(&et, inode, root_bh, et_type, obj);
5173 ocfs2_extent_map_trunc(inode, 0);
5175 path = ocfs2_new_path(et.et_root_bh, et.et_root_el);
5182 ret = ocfs2_find_path(inode, path, cpos);
5188 el = path_leaf_el(path);
5189 index = ocfs2_search_extent_list(el, cpos);
5190 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5191 ocfs2_error(inode->i_sb,
5192 "Inode %llu has an extent at cpos %u which can no "
5193 "longer be found.\n",
5194 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5200 * We have 3 cases of extent removal:
5201 * 1) Range covers the entire extent rec
5202 * 2) Range begins or ends on one edge of the extent rec
5203 * 3) Range is in the middle of the extent rec (no shared edges)
5205 * For case 1 we remove the extent rec and left rotate to
5208 * For case 2 we just shrink the existing extent rec, with a
5209 * tree update if the shrinking edge is also the edge of an
5212 * For case 3 we do a right split to turn the extent rec into
5213 * something case 2 can handle.
5215 rec = &el->l_recs[index];
5216 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5217 trunc_range = cpos + len;
5219 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5221 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5222 "(cpos %u, len %u)\n",
5223 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5224 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5226 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5227 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5234 ret = ocfs2_split_tree(inode, &et, handle, path, index,
5235 trunc_range, meta_ac);
5242 * The split could have manipulated the tree enough to
5243 * move the record location, so we have to look for it again.
5245 ocfs2_reinit_path(path, 1);
5247 ret = ocfs2_find_path(inode, path, cpos);
5253 el = path_leaf_el(path);
5254 index = ocfs2_search_extent_list(el, cpos);
5255 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5256 ocfs2_error(inode->i_sb,
5257 "Inode %llu: split at cpos %u lost record.",
5258 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5265 * Double check our values here. If anything is fishy,
5266 * it's easier to catch it at the top level.
5268 rec = &el->l_recs[index];
5269 rec_range = le32_to_cpu(rec->e_cpos) +
5270 ocfs2_rec_clusters(el, rec);
5271 if (rec_range != trunc_range) {
5272 ocfs2_error(inode->i_sb,
5273 "Inode %llu: error after split at cpos %u"
5274 "trunc len %u, existing record is (%u,%u)",
5275 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5276 cpos, len, le32_to_cpu(rec->e_cpos),
5277 ocfs2_rec_clusters(el, rec));
5282 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5291 ocfs2_free_path(path);
5292 ocfs2_put_extent_tree(&et);
5296 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5298 struct buffer_head *tl_bh = osb->osb_tl_bh;
5299 struct ocfs2_dinode *di;
5300 struct ocfs2_truncate_log *tl;
5302 di = (struct ocfs2_dinode *) tl_bh->b_data;
5303 tl = &di->id2.i_dealloc;
5305 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5306 "slot %d, invalid truncate log parameters: used = "
5307 "%u, count = %u\n", osb->slot_num,
5308 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5309 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5312 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5313 unsigned int new_start)
5315 unsigned int tail_index;
5316 unsigned int current_tail;
5318 /* No records, nothing to coalesce */
5319 if (!le16_to_cpu(tl->tl_used))
5322 tail_index = le16_to_cpu(tl->tl_used) - 1;
5323 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5324 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5326 return current_tail == new_start;
5329 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5332 unsigned int num_clusters)
5335 unsigned int start_cluster, tl_count;
5336 struct inode *tl_inode = osb->osb_tl_inode;
5337 struct buffer_head *tl_bh = osb->osb_tl_bh;
5338 struct ocfs2_dinode *di;
5339 struct ocfs2_truncate_log *tl;
5341 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5342 (unsigned long long)start_blk, num_clusters);
5344 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5346 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5348 di = (struct ocfs2_dinode *) tl_bh->b_data;
5349 tl = &di->id2.i_dealloc;
5350 if (!OCFS2_IS_VALID_DINODE(di)) {
5351 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5356 tl_count = le16_to_cpu(tl->tl_count);
5357 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5359 "Truncate record count on #%llu invalid "
5360 "wanted %u, actual %u\n",
5361 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5362 ocfs2_truncate_recs_per_inode(osb->sb),
5363 le16_to_cpu(tl->tl_count));
5365 /* Caller should have known to flush before calling us. */
5366 index = le16_to_cpu(tl->tl_used);
5367 if (index >= tl_count) {
5373 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5374 OCFS2_JOURNAL_ACCESS_WRITE);
5380 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5381 "%llu (index = %d)\n", num_clusters, start_cluster,
5382 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5384 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5386 * Move index back to the record we are coalescing with.
5387 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5391 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5392 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5393 index, le32_to_cpu(tl->tl_recs[index].t_start),
5396 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5397 tl->tl_used = cpu_to_le16(index + 1);
5399 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5401 status = ocfs2_journal_dirty(handle, tl_bh);
5412 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5414 struct inode *data_alloc_inode,
5415 struct buffer_head *data_alloc_bh)
5419 unsigned int num_clusters;
5421 struct ocfs2_truncate_rec rec;
5422 struct ocfs2_dinode *di;
5423 struct ocfs2_truncate_log *tl;
5424 struct inode *tl_inode = osb->osb_tl_inode;
5425 struct buffer_head *tl_bh = osb->osb_tl_bh;
5429 di = (struct ocfs2_dinode *) tl_bh->b_data;
5430 tl = &di->id2.i_dealloc;
5431 i = le16_to_cpu(tl->tl_used) - 1;
5433 /* Caller has given us at least enough credits to
5434 * update the truncate log dinode */
5435 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5436 OCFS2_JOURNAL_ACCESS_WRITE);
5442 tl->tl_used = cpu_to_le16(i);
5444 status = ocfs2_journal_dirty(handle, tl_bh);
5450 /* TODO: Perhaps we can calculate the bulk of the
5451 * credits up front rather than extending like
5453 status = ocfs2_extend_trans(handle,
5454 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5460 rec = tl->tl_recs[i];
5461 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5462 le32_to_cpu(rec.t_start));
5463 num_clusters = le32_to_cpu(rec.t_clusters);
5465 /* if start_blk is not set, we ignore the record as
5468 mlog(0, "free record %d, start = %u, clusters = %u\n",
5469 i, le32_to_cpu(rec.t_start), num_clusters);
5471 status = ocfs2_free_clusters(handle, data_alloc_inode,
5472 data_alloc_bh, start_blk,
5487 /* Expects you to already be holding tl_inode->i_mutex */
5488 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5491 unsigned int num_to_flush;
5493 struct inode *tl_inode = osb->osb_tl_inode;
5494 struct inode *data_alloc_inode = NULL;
5495 struct buffer_head *tl_bh = osb->osb_tl_bh;
5496 struct buffer_head *data_alloc_bh = NULL;
5497 struct ocfs2_dinode *di;
5498 struct ocfs2_truncate_log *tl;
5502 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5504 di = (struct ocfs2_dinode *) tl_bh->b_data;
5505 tl = &di->id2.i_dealloc;
5506 if (!OCFS2_IS_VALID_DINODE(di)) {
5507 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5512 num_to_flush = le16_to_cpu(tl->tl_used);
5513 mlog(0, "Flush %u records from truncate log #%llu\n",
5514 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5515 if (!num_to_flush) {
5520 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5521 GLOBAL_BITMAP_SYSTEM_INODE,
5522 OCFS2_INVALID_SLOT);
5523 if (!data_alloc_inode) {
5525 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5529 mutex_lock(&data_alloc_inode->i_mutex);
5531 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5537 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5538 if (IS_ERR(handle)) {
5539 status = PTR_ERR(handle);
5544 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5549 ocfs2_commit_trans(osb, handle);
5552 brelse(data_alloc_bh);
5553 ocfs2_inode_unlock(data_alloc_inode, 1);
5556 mutex_unlock(&data_alloc_inode->i_mutex);
5557 iput(data_alloc_inode);
5564 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5567 struct inode *tl_inode = osb->osb_tl_inode;
5569 mutex_lock(&tl_inode->i_mutex);
5570 status = __ocfs2_flush_truncate_log(osb);
5571 mutex_unlock(&tl_inode->i_mutex);
5576 static void ocfs2_truncate_log_worker(struct work_struct *work)
5579 struct ocfs2_super *osb =
5580 container_of(work, struct ocfs2_super,
5581 osb_truncate_log_wq.work);
5585 status = ocfs2_flush_truncate_log(osb);
5589 ocfs2_init_inode_steal_slot(osb);
5594 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5595 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5598 if (osb->osb_tl_inode) {
5599 /* We want to push off log flushes while truncates are
5602 cancel_delayed_work(&osb->osb_truncate_log_wq);
5604 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5605 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5609 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5611 struct inode **tl_inode,
5612 struct buffer_head **tl_bh)
5615 struct inode *inode = NULL;
5616 struct buffer_head *bh = NULL;
5618 inode = ocfs2_get_system_file_inode(osb,
5619 TRUNCATE_LOG_SYSTEM_INODE,
5623 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5627 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
5628 OCFS2_BH_CACHED, inode);
5642 /* called during the 1st stage of node recovery. we stamp a clean
5643 * truncate log and pass back a copy for processing later. if the
5644 * truncate log does not require processing, a *tl_copy is set to
5646 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5648 struct ocfs2_dinode **tl_copy)
5651 struct inode *tl_inode = NULL;
5652 struct buffer_head *tl_bh = NULL;
5653 struct ocfs2_dinode *di;
5654 struct ocfs2_truncate_log *tl;
5658 mlog(0, "recover truncate log from slot %d\n", slot_num);
5660 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5666 di = (struct ocfs2_dinode *) tl_bh->b_data;
5667 tl = &di->id2.i_dealloc;
5668 if (!OCFS2_IS_VALID_DINODE(di)) {
5669 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
5674 if (le16_to_cpu(tl->tl_used)) {
5675 mlog(0, "We'll have %u logs to recover\n",
5676 le16_to_cpu(tl->tl_used));
5678 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5685 /* Assuming the write-out below goes well, this copy
5686 * will be passed back to recovery for processing. */
5687 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5689 /* All we need to do to clear the truncate log is set
5693 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5706 if (status < 0 && (*tl_copy)) {
5715 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5716 struct ocfs2_dinode *tl_copy)
5720 unsigned int clusters, num_recs, start_cluster;
5723 struct inode *tl_inode = osb->osb_tl_inode;
5724 struct ocfs2_truncate_log *tl;
5728 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5729 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5733 tl = &tl_copy->id2.i_dealloc;
5734 num_recs = le16_to_cpu(tl->tl_used);
5735 mlog(0, "cleanup %u records from %llu\n", num_recs,
5736 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
5738 mutex_lock(&tl_inode->i_mutex);
5739 for(i = 0; i < num_recs; i++) {
5740 if (ocfs2_truncate_log_needs_flush(osb)) {
5741 status = __ocfs2_flush_truncate_log(osb);
5748 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5749 if (IS_ERR(handle)) {
5750 status = PTR_ERR(handle);
5755 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5756 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5757 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5759 status = ocfs2_truncate_log_append(osb, handle,
5760 start_blk, clusters);
5761 ocfs2_commit_trans(osb, handle);
5769 mutex_unlock(&tl_inode->i_mutex);
5775 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5778 struct inode *tl_inode = osb->osb_tl_inode;
5783 cancel_delayed_work(&osb->osb_truncate_log_wq);
5784 flush_workqueue(ocfs2_wq);
5786 status = ocfs2_flush_truncate_log(osb);
5790 brelse(osb->osb_tl_bh);
5791 iput(osb->osb_tl_inode);
5797 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5800 struct inode *tl_inode = NULL;
5801 struct buffer_head *tl_bh = NULL;
5805 status = ocfs2_get_truncate_log_info(osb,
5812 /* ocfs2_truncate_log_shutdown keys on the existence of
5813 * osb->osb_tl_inode so we don't set any of the osb variables
5814 * until we're sure all is well. */
5815 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5816 ocfs2_truncate_log_worker);
5817 osb->osb_tl_bh = tl_bh;
5818 osb->osb_tl_inode = tl_inode;
5825 * Delayed de-allocation of suballocator blocks.
5827 * Some sets of block de-allocations might involve multiple suballocator inodes.
5829 * The locking for this can get extremely complicated, especially when
5830 * the suballocator inodes to delete from aren't known until deep
5831 * within an unrelated codepath.
5833 * ocfs2_extent_block structures are a good example of this - an inode
5834 * btree could have been grown by any number of nodes each allocating
5835 * out of their own suballoc inode.
5837 * These structures allow the delay of block de-allocation until a
5838 * later time, when locking of multiple cluster inodes won't cause
5843 * Describes a single block free from a suballocator
5845 struct ocfs2_cached_block_free {
5846 struct ocfs2_cached_block_free *free_next;
5848 unsigned int free_bit;
5851 struct ocfs2_per_slot_free_list {
5852 struct ocfs2_per_slot_free_list *f_next_suballocator;
5855 struct ocfs2_cached_block_free *f_first;
5858 static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5861 struct ocfs2_cached_block_free *head)
5866 struct inode *inode;
5867 struct buffer_head *di_bh = NULL;
5868 struct ocfs2_cached_block_free *tmp;
5870 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5877 mutex_lock(&inode->i_mutex);
5879 ret = ocfs2_inode_lock(inode, &di_bh, 1);
5885 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5886 if (IS_ERR(handle)) {
5887 ret = PTR_ERR(handle);
5893 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5895 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5896 head->free_bit, (unsigned long long)head->free_blk);
5898 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5899 head->free_bit, bg_blkno, 1);
5905 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5912 head = head->free_next;
5917 ocfs2_commit_trans(osb, handle);
5920 ocfs2_inode_unlock(inode, 1);
5923 mutex_unlock(&inode->i_mutex);
5927 /* Premature exit may have left some dangling items. */
5929 head = head->free_next;
5936 int ocfs2_run_deallocs(struct ocfs2_super *osb,
5937 struct ocfs2_cached_dealloc_ctxt *ctxt)
5940 struct ocfs2_per_slot_free_list *fl;
5945 while (ctxt->c_first_suballocator) {
5946 fl = ctxt->c_first_suballocator;
5949 mlog(0, "Free items: (type %u, slot %d)\n",
5950 fl->f_inode_type, fl->f_slot);
5951 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
5952 fl->f_slot, fl->f_first);
5959 ctxt->c_first_suballocator = fl->f_next_suballocator;
5966 static struct ocfs2_per_slot_free_list *
5967 ocfs2_find_per_slot_free_list(int type,
5969 struct ocfs2_cached_dealloc_ctxt *ctxt)
5971 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
5974 if (fl->f_inode_type == type && fl->f_slot == slot)
5977 fl = fl->f_next_suballocator;
5980 fl = kmalloc(sizeof(*fl), GFP_NOFS);
5982 fl->f_inode_type = type;
5985 fl->f_next_suballocator = ctxt->c_first_suballocator;
5987 ctxt->c_first_suballocator = fl;
5992 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
5993 int type, int slot, u64 blkno,
5997 struct ocfs2_per_slot_free_list *fl;
5998 struct ocfs2_cached_block_free *item;
6000 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6007 item = kmalloc(sizeof(*item), GFP_NOFS);
6014 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6015 type, slot, bit, (unsigned long long)blkno);
6017 item->free_blk = blkno;
6018 item->free_bit = bit;
6019 item->free_next = fl->f_first;
6028 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6029 struct ocfs2_extent_block *eb)
6031 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6032 le16_to_cpu(eb->h_suballoc_slot),
6033 le64_to_cpu(eb->h_blkno),
6034 le16_to_cpu(eb->h_suballoc_bit));
6037 /* This function will figure out whether the currently last extent
6038 * block will be deleted, and if it will, what the new last extent
6039 * block will be so we can update his h_next_leaf_blk field, as well
6040 * as the dinodes i_last_eb_blk */
6041 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6042 unsigned int clusters_to_del,
6043 struct ocfs2_path *path,
6044 struct buffer_head **new_last_eb)
6046 int next_free, ret = 0;
6048 struct ocfs2_extent_rec *rec;
6049 struct ocfs2_extent_block *eb;
6050 struct ocfs2_extent_list *el;
6051 struct buffer_head *bh = NULL;
6053 *new_last_eb = NULL;
6055 /* we have no tree, so of course, no last_eb. */
6056 if (!path->p_tree_depth)
6059 /* trunc to zero special case - this makes tree_depth = 0
6060 * regardless of what it is. */
6061 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6064 el = path_leaf_el(path);
6065 BUG_ON(!el->l_next_free_rec);
6068 * Make sure that this extent list will actually be empty
6069 * after we clear away the data. We can shortcut out if
6070 * there's more than one non-empty extent in the
6071 * list. Otherwise, a check of the remaining extent is
6074 next_free = le16_to_cpu(el->l_next_free_rec);
6076 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6080 /* We may have a valid extent in index 1, check it. */
6082 rec = &el->l_recs[1];
6085 * Fall through - no more nonempty extents, so we want
6086 * to delete this leaf.
6092 rec = &el->l_recs[0];
6097 * Check it we'll only be trimming off the end of this
6100 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6104 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6110 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6116 eb = (struct ocfs2_extent_block *) bh->b_data;
6118 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6119 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6125 get_bh(*new_last_eb);
6126 mlog(0, "returning block %llu, (cpos: %u)\n",
6127 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6135 * Trim some clusters off the rightmost edge of a tree. Only called
6138 * The caller needs to:
6139 * - start journaling of each path component.
6140 * - compute and fully set up any new last ext block
6142 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6143 handle_t *handle, struct ocfs2_truncate_context *tc,
6144 u32 clusters_to_del, u64 *delete_start)
6146 int ret, i, index = path->p_tree_depth;
6149 struct buffer_head *bh;
6150 struct ocfs2_extent_list *el;
6151 struct ocfs2_extent_rec *rec;
6155 while (index >= 0) {
6156 bh = path->p_node[index].bh;
6157 el = path->p_node[index].el;
6159 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6160 index, (unsigned long long)bh->b_blocknr);
6162 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6165 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6166 ocfs2_error(inode->i_sb,
6167 "Inode %lu has invalid ext. block %llu",
6169 (unsigned long long)bh->b_blocknr);
6175 i = le16_to_cpu(el->l_next_free_rec) - 1;
6176 rec = &el->l_recs[i];
6178 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6179 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6180 ocfs2_rec_clusters(el, rec),
6181 (unsigned long long)le64_to_cpu(rec->e_blkno),
6182 le16_to_cpu(el->l_next_free_rec));
6184 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6186 if (le16_to_cpu(el->l_tree_depth) == 0) {
6188 * If the leaf block contains a single empty
6189 * extent and no records, we can just remove
6192 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6194 sizeof(struct ocfs2_extent_rec));
6195 el->l_next_free_rec = cpu_to_le16(0);
6201 * Remove any empty extents by shifting things
6202 * left. That should make life much easier on
6203 * the code below. This condition is rare
6204 * enough that we shouldn't see a performance
6207 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6208 le16_add_cpu(&el->l_next_free_rec, -1);
6211 i < le16_to_cpu(el->l_next_free_rec); i++)
6212 el->l_recs[i] = el->l_recs[i + 1];
6214 memset(&el->l_recs[i], 0,
6215 sizeof(struct ocfs2_extent_rec));
6218 * We've modified our extent list. The
6219 * simplest way to handle this change
6220 * is to being the search from the
6223 goto find_tail_record;
6226 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6229 * We'll use "new_edge" on our way back up the
6230 * tree to know what our rightmost cpos is.
6232 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6233 new_edge += le32_to_cpu(rec->e_cpos);
6236 * The caller will use this to delete data blocks.
6238 *delete_start = le64_to_cpu(rec->e_blkno)
6239 + ocfs2_clusters_to_blocks(inode->i_sb,
6240 le16_to_cpu(rec->e_leaf_clusters));
6243 * If it's now empty, remove this record.
6245 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6247 sizeof(struct ocfs2_extent_rec));
6248 le16_add_cpu(&el->l_next_free_rec, -1);
6251 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6253 sizeof(struct ocfs2_extent_rec));
6254 le16_add_cpu(&el->l_next_free_rec, -1);
6259 /* Can this actually happen? */
6260 if (le16_to_cpu(el->l_next_free_rec) == 0)
6264 * We never actually deleted any clusters
6265 * because our leaf was empty. There's no
6266 * reason to adjust the rightmost edge then.
6271 rec->e_int_clusters = cpu_to_le32(new_edge);
6272 le32_add_cpu(&rec->e_int_clusters,
6273 -le32_to_cpu(rec->e_cpos));
6276 * A deleted child record should have been
6279 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6283 ret = ocfs2_journal_dirty(handle, bh);
6289 mlog(0, "extent list container %llu, after: record %d: "
6290 "(%u, %u, %llu), next = %u.\n",
6291 (unsigned long long)bh->b_blocknr, i,
6292 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6293 (unsigned long long)le64_to_cpu(rec->e_blkno),
6294 le16_to_cpu(el->l_next_free_rec));
6297 * We must be careful to only attempt delete of an
6298 * extent block (and not the root inode block).
6300 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6301 struct ocfs2_extent_block *eb =
6302 (struct ocfs2_extent_block *)bh->b_data;
6305 * Save this for use when processing the
6308 deleted_eb = le64_to_cpu(eb->h_blkno);
6310 mlog(0, "deleting this extent block.\n");
6312 ocfs2_remove_from_cache(inode, bh);
6314 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6315 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6316 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6318 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6319 /* An error here is not fatal. */
6334 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6335 unsigned int clusters_to_del,
6336 struct inode *inode,
6337 struct buffer_head *fe_bh,
6339 struct ocfs2_truncate_context *tc,
6340 struct ocfs2_path *path)
6343 struct ocfs2_dinode *fe;
6344 struct ocfs2_extent_block *last_eb = NULL;
6345 struct ocfs2_extent_list *el;
6346 struct buffer_head *last_eb_bh = NULL;
6349 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6351 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6359 * Each component will be touched, so we might as well journal
6360 * here to avoid having to handle errors later.
6362 status = ocfs2_journal_access_path(inode, handle, path);
6369 status = ocfs2_journal_access(handle, inode, last_eb_bh,
6370 OCFS2_JOURNAL_ACCESS_WRITE);
6376 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6379 el = &(fe->id2.i_list);
6382 * Lower levels depend on this never happening, but it's best
6383 * to check it up here before changing the tree.
6385 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6386 ocfs2_error(inode->i_sb,
6387 "Inode %lu has an empty extent record, depth %u\n",
6388 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6393 spin_lock(&OCFS2_I(inode)->ip_lock);
6394 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6396 spin_unlock(&OCFS2_I(inode)->ip_lock);
6397 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6398 inode->i_blocks = ocfs2_inode_sector_count(inode);
6400 status = ocfs2_trim_tree(inode, path, handle, tc,
6401 clusters_to_del, &delete_blk);
6407 if (le32_to_cpu(fe->i_clusters) == 0) {
6408 /* trunc to zero is a special case. */
6409 el->l_tree_depth = 0;
6410 fe->i_last_eb_blk = 0;
6412 fe->i_last_eb_blk = last_eb->h_blkno;
6414 status = ocfs2_journal_dirty(handle, fe_bh);
6421 /* If there will be a new last extent block, then by
6422 * definition, there cannot be any leaves to the right of
6424 last_eb->h_next_leaf_blk = 0;
6425 status = ocfs2_journal_dirty(handle, last_eb_bh);
6433 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6447 static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
6449 set_buffer_uptodate(bh);
6450 mark_buffer_dirty(bh);
6454 static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
6456 set_buffer_uptodate(bh);
6457 mark_buffer_dirty(bh);
6458 return ocfs2_journal_dirty_data(handle, bh);
6461 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6462 unsigned int from, unsigned int to,
6463 struct page *page, int zero, u64 *phys)
6465 int ret, partial = 0;
6467 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6472 zero_user_segment(page, from, to);
6475 * Need to set the buffers we zero'd into uptodate
6476 * here if they aren't - ocfs2_map_page_blocks()
6477 * might've skipped some
6479 if (ocfs2_should_order_data(inode)) {
6480 ret = walk_page_buffers(handle,
6483 ocfs2_ordered_zero_func);
6487 ret = walk_page_buffers(handle, page_buffers(page),
6489 ocfs2_writeback_zero_func);
6495 SetPageUptodate(page);
6497 flush_dcache_page(page);
6500 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6501 loff_t end, struct page **pages,
6502 int numpages, u64 phys, handle_t *handle)
6506 unsigned int from, to = PAGE_CACHE_SIZE;
6507 struct super_block *sb = inode->i_sb;
6509 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6514 to = PAGE_CACHE_SIZE;
6515 for(i = 0; i < numpages; i++) {
6518 from = start & (PAGE_CACHE_SIZE - 1);
6519 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6520 to = end & (PAGE_CACHE_SIZE - 1);
6522 BUG_ON(from > PAGE_CACHE_SIZE);
6523 BUG_ON(to > PAGE_CACHE_SIZE);
6525 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6528 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6532 ocfs2_unlock_and_free_pages(pages, numpages);
6535 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6536 struct page **pages, int *num)
6538 int numpages, ret = 0;
6539 struct super_block *sb = inode->i_sb;
6540 struct address_space *mapping = inode->i_mapping;
6541 unsigned long index;
6542 loff_t last_page_bytes;
6544 BUG_ON(start > end);
6546 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6547 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6550 last_page_bytes = PAGE_ALIGN(end);
6551 index = start >> PAGE_CACHE_SHIFT;
6553 pages[numpages] = grab_cache_page(mapping, index);
6554 if (!pages[numpages]) {
6562 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6567 ocfs2_unlock_and_free_pages(pages, numpages);
6577 * Zero the area past i_size but still within an allocated
6578 * cluster. This avoids exposing nonzero data on subsequent file
6581 * We need to call this before i_size is updated on the inode because
6582 * otherwise block_write_full_page() will skip writeout of pages past
6583 * i_size. The new_i_size parameter is passed for this reason.
6585 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6586 u64 range_start, u64 range_end)
6588 int ret = 0, numpages;
6589 struct page **pages = NULL;
6591 unsigned int ext_flags;
6592 struct super_block *sb = inode->i_sb;
6595 * File systems which don't support sparse files zero on every
6598 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6601 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6602 sizeof(struct page *), GFP_NOFS);
6603 if (pages == NULL) {
6609 if (range_start == range_end)
6612 ret = ocfs2_extent_map_get_blocks(inode,
6613 range_start >> sb->s_blocksize_bits,
6614 &phys, NULL, &ext_flags);
6621 * Tail is a hole, or is marked unwritten. In either case, we
6622 * can count on read and write to return/push zero's.
6624 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6627 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6634 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6635 numpages, phys, handle);
6638 * Initiate writeout of the pages we zero'd here. We don't
6639 * wait on them - the truncate_inode_pages() call later will
6642 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6643 range_end - 1, SYNC_FILE_RANGE_WRITE);
6654 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6655 struct ocfs2_dinode *di)
6657 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6658 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6660 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6661 memset(&di->id2, 0, blocksize -
6662 offsetof(struct ocfs2_dinode, id2) -
6665 memset(&di->id2, 0, blocksize -
6666 offsetof(struct ocfs2_dinode, id2));
6669 void ocfs2_dinode_new_extent_list(struct inode *inode,
6670 struct ocfs2_dinode *di)
6672 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6673 di->id2.i_list.l_tree_depth = 0;
6674 di->id2.i_list.l_next_free_rec = 0;
6675 di->id2.i_list.l_count = cpu_to_le16(
6676 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6679 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6681 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6682 struct ocfs2_inline_data *idata = &di->id2.i_data;
6684 spin_lock(&oi->ip_lock);
6685 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6686 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6687 spin_unlock(&oi->ip_lock);
6690 * We clear the entire i_data structure here so that all
6691 * fields can be properly initialized.
6693 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6695 idata->id_count = cpu_to_le16(
6696 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6699 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6700 struct buffer_head *di_bh)
6702 int ret, i, has_data, num_pages = 0;
6704 u64 uninitialized_var(block);
6705 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6706 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6707 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6708 struct ocfs2_alloc_context *data_ac = NULL;
6709 struct page **pages = NULL;
6710 loff_t end = osb->s_clustersize;
6712 has_data = i_size_read(inode) ? 1 : 0;
6715 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6716 sizeof(struct page *), GFP_NOFS);
6717 if (pages == NULL) {
6723 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6730 handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
6731 if (IS_ERR(handle)) {
6732 ret = PTR_ERR(handle);
6737 ret = ocfs2_journal_access(handle, inode, di_bh,
6738 OCFS2_JOURNAL_ACCESS_WRITE);
6746 unsigned int page_end;
6749 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6757 * Save two copies, one for insert, and one that can
6758 * be changed by ocfs2_map_and_dirty_page() below.
6760 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6763 * Non sparse file systems zero on extend, so no need
6766 if (!ocfs2_sparse_alloc(osb) &&
6767 PAGE_CACHE_SIZE < osb->s_clustersize)
6768 end = PAGE_CACHE_SIZE;
6770 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6777 * This should populate the 1st page for us and mark
6780 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6786 page_end = PAGE_CACHE_SIZE;
6787 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6788 page_end = osb->s_clustersize;
6790 for (i = 0; i < num_pages; i++)
6791 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6792 pages[i], i > 0, &phys);
6795 spin_lock(&oi->ip_lock);
6796 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6797 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6798 spin_unlock(&oi->ip_lock);
6800 ocfs2_dinode_new_extent_list(inode, di);
6802 ocfs2_journal_dirty(handle, di_bh);
6806 * An error at this point should be extremely rare. If
6807 * this proves to be false, we could always re-build
6808 * the in-inode data from our pages.
6810 ret = ocfs2_dinode_insert_extent(osb, handle, inode, di_bh,
6811 0, block, 1, 0, NULL);
6817 inode->i_blocks = ocfs2_inode_sector_count(inode);
6821 ocfs2_commit_trans(osb, handle);
6825 ocfs2_free_alloc_context(data_ac);
6829 ocfs2_unlock_and_free_pages(pages, num_pages);
6837 * It is expected, that by the time you call this function,
6838 * inode->i_size and fe->i_size have been adjusted.
6840 * WARNING: This will kfree the truncate context
6842 int ocfs2_commit_truncate(struct ocfs2_super *osb,
6843 struct inode *inode,
6844 struct buffer_head *fe_bh,
6845 struct ocfs2_truncate_context *tc)
6847 int status, i, credits, tl_sem = 0;
6848 u32 clusters_to_del, new_highest_cpos, range;
6849 struct ocfs2_extent_list *el;
6850 handle_t *handle = NULL;
6851 struct inode *tl_inode = osb->osb_tl_inode;
6852 struct ocfs2_path *path = NULL;
6853 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6857 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
6858 i_size_read(inode));
6860 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
6867 ocfs2_extent_map_trunc(inode, new_highest_cpos);
6871 * Check that we still have allocation to delete.
6873 if (OCFS2_I(inode)->ip_clusters == 0) {
6879 * Truncate always works against the rightmost tree branch.
6881 status = ocfs2_find_path(inode, path, UINT_MAX);
6887 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6888 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
6891 * By now, el will point to the extent list on the bottom most
6892 * portion of this tree. Only the tail record is considered in
6895 * We handle the following cases, in order:
6896 * - empty extent: delete the remaining branch
6897 * - remove the entire record
6898 * - remove a partial record
6899 * - no record needs to be removed (truncate has completed)
6901 el = path_leaf_el(path);
6902 if (le16_to_cpu(el->l_next_free_rec) == 0) {
6903 ocfs2_error(inode->i_sb,
6904 "Inode %llu has empty extent block at %llu\n",
6905 (unsigned long long)OCFS2_I(inode)->ip_blkno,
6906 (unsigned long long)path_leaf_bh(path)->b_blocknr);
6911 i = le16_to_cpu(el->l_next_free_rec) - 1;
6912 range = le32_to_cpu(el->l_recs[i].e_cpos) +
6913 ocfs2_rec_clusters(el, &el->l_recs[i]);
6914 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
6915 clusters_to_del = 0;
6916 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
6917 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
6918 } else if (range > new_highest_cpos) {
6919 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
6920 le32_to_cpu(el->l_recs[i].e_cpos)) -
6927 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6928 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
6930 mutex_lock(&tl_inode->i_mutex);
6932 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6933 * record is free for use. If there isn't any, we flush to get
6934 * an empty truncate log. */
6935 if (ocfs2_truncate_log_needs_flush(osb)) {
6936 status = __ocfs2_flush_truncate_log(osb);
6943 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
6944 (struct ocfs2_dinode *)fe_bh->b_data,
6946 handle = ocfs2_start_trans(osb, credits);
6947 if (IS_ERR(handle)) {
6948 status = PTR_ERR(handle);
6954 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
6961 mutex_unlock(&tl_inode->i_mutex);
6964 ocfs2_commit_trans(osb, handle);
6967 ocfs2_reinit_path(path, 1);
6970 * The check above will catch the case where we've truncated
6971 * away all allocation.
6977 ocfs2_schedule_truncate_log_flush(osb, 1);
6980 mutex_unlock(&tl_inode->i_mutex);
6983 ocfs2_commit_trans(osb, handle);
6985 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
6987 ocfs2_free_path(path);
6989 /* This will drop the ext_alloc cluster lock for us */
6990 ocfs2_free_truncate_context(tc);
6997 * Expects the inode to already be locked.
6999 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7000 struct inode *inode,
7001 struct buffer_head *fe_bh,
7002 struct ocfs2_truncate_context **tc)
7005 unsigned int new_i_clusters;
7006 struct ocfs2_dinode *fe;
7007 struct ocfs2_extent_block *eb;
7008 struct buffer_head *last_eb_bh = NULL;
7014 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7015 i_size_read(inode));
7016 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7018 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7019 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7020 (unsigned long long)le64_to_cpu(fe->i_size));
7022 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7028 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7030 if (fe->id2.i_list.l_tree_depth) {
7031 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
7032 &last_eb_bh, OCFS2_BH_CACHED, inode);
7037 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7038 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
7039 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
7047 (*tc)->tc_last_eb_bh = last_eb_bh;
7053 ocfs2_free_truncate_context(*tc);
7061 * 'start' is inclusive, 'end' is not.
7063 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7064 unsigned int start, unsigned int end, int trunc)
7067 unsigned int numbytes;
7069 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7070 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7071 struct ocfs2_inline_data *idata = &di->id2.i_data;
7073 if (end > i_size_read(inode))
7074 end = i_size_read(inode);
7076 BUG_ON(start >= end);
7078 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7079 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7080 !ocfs2_supports_inline_data(osb)) {
7081 ocfs2_error(inode->i_sb,
7082 "Inline data flags for inode %llu don't agree! "
7083 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7084 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7085 le16_to_cpu(di->i_dyn_features),
7086 OCFS2_I(inode)->ip_dyn_features,
7087 osb->s_feature_incompat);
7092 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7093 if (IS_ERR(handle)) {
7094 ret = PTR_ERR(handle);
7099 ret = ocfs2_journal_access(handle, inode, di_bh,
7100 OCFS2_JOURNAL_ACCESS_WRITE);
7106 numbytes = end - start;
7107 memset(idata->id_data + start, 0, numbytes);
7110 * No need to worry about the data page here - it's been
7111 * truncated already and inline data doesn't need it for
7112 * pushing zero's to disk, so we'll let readpage pick it up
7116 i_size_write(inode, start);
7117 di->i_size = cpu_to_le64(start);
7120 inode->i_blocks = ocfs2_inode_sector_count(inode);
7121 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7123 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7124 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7126 ocfs2_journal_dirty(handle, di_bh);
7129 ocfs2_commit_trans(osb, handle);
7135 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7138 * The caller is responsible for completing deallocation
7139 * before freeing the context.
7141 if (tc->tc_dealloc.c_first_suballocator != NULL)
7143 "Truncate completion has non-empty dealloc context\n");
7145 if (tc->tc_last_eb_bh)
7146 brelse(tc->tc_last_eb_bh);