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);
77 struct ocfs2_extent_tree {
78 enum ocfs2_extent_tree_type et_type;
79 struct ocfs2_extent_tree_operations *et_ops;
80 struct buffer_head *et_root_bh;
81 struct ocfs2_extent_list *et_root_el;
83 unsigned int et_max_leaf_clusters;
86 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
89 struct ocfs2_dinode *di =
90 (struct ocfs2_dinode *)et->et_root_bh->b_data;
92 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
93 di->i_last_eb_blk = cpu_to_le64(blkno);
96 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
98 struct ocfs2_dinode *di =
99 (struct ocfs2_dinode *)et->et_root_bh->b_data;
101 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
102 return le64_to_cpu(di->i_last_eb_blk);
105 static void ocfs2_dinode_update_clusters(struct inode *inode,
106 struct ocfs2_extent_tree *et,
109 struct ocfs2_dinode *di =
110 (struct ocfs2_dinode *)et->et_root_bh->b_data;
112 le32_add_cpu(&di->i_clusters, clusters);
113 spin_lock(&OCFS2_I(inode)->ip_lock);
114 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
115 spin_unlock(&OCFS2_I(inode)->ip_lock);
118 static int ocfs2_dinode_sanity_check(struct inode *inode,
119 struct ocfs2_extent_tree *et)
122 struct ocfs2_dinode *di;
124 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
126 di = (struct ocfs2_dinode *)et->et_root_bh->b_data;
127 if (!OCFS2_IS_VALID_DINODE(di)) {
129 ocfs2_error(inode->i_sb,
130 "Inode %llu has invalid path root",
131 (unsigned long long)OCFS2_I(inode)->ip_blkno);
137 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
138 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
139 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
140 .eo_update_clusters = ocfs2_dinode_update_clusters,
141 .eo_sanity_check = ocfs2_dinode_sanity_check,
144 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
147 struct ocfs2_xattr_value_root *xv =
148 (struct ocfs2_xattr_value_root *)et->et_private;
150 xv->xr_last_eb_blk = cpu_to_le64(blkno);
153 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
155 struct ocfs2_xattr_value_root *xv =
156 (struct ocfs2_xattr_value_root *) et->et_private;
158 return le64_to_cpu(xv->xr_last_eb_blk);
161 static void ocfs2_xattr_value_update_clusters(struct inode *inode,
162 struct ocfs2_extent_tree *et,
165 struct ocfs2_xattr_value_root *xv =
166 (struct ocfs2_xattr_value_root *)et->et_private;
168 le32_add_cpu(&xv->xr_clusters, clusters);
171 static int ocfs2_xattr_value_sanity_check(struct inode *inode,
172 struct ocfs2_extent_tree *et)
177 static struct ocfs2_extent_tree_operations ocfs2_xattr_et_ops = {
178 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
179 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
180 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
181 .eo_sanity_check = ocfs2_xattr_value_sanity_check,
184 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
187 struct ocfs2_xattr_block *xb =
188 (struct ocfs2_xattr_block *) et->et_root_bh->b_data;
189 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
191 xt->xt_last_eb_blk = cpu_to_le64(blkno);
194 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
196 struct ocfs2_xattr_block *xb =
197 (struct ocfs2_xattr_block *) et->et_root_bh->b_data;
198 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
200 return le64_to_cpu(xt->xt_last_eb_blk);
203 static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
204 struct ocfs2_extent_tree *et,
207 struct ocfs2_xattr_block *xb =
208 (struct ocfs2_xattr_block *)et->et_root_bh->b_data;
210 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
213 static int ocfs2_xattr_tree_sanity_check(struct inode *inode,
214 struct ocfs2_extent_tree *et)
219 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
220 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
221 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
222 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
223 .eo_sanity_check = ocfs2_xattr_tree_sanity_check,
226 static void ocfs2_get_extent_tree(struct ocfs2_extent_tree *et,
228 struct buffer_head *bh,
229 enum ocfs2_extent_tree_type et_type,
232 et->et_type = et_type;
235 et->et_private = private;
236 et->et_max_leaf_clusters = 0;
238 if (et_type == OCFS2_DINODE_EXTENT) {
240 &((struct ocfs2_dinode *)bh->b_data)->id2.i_list;
241 et->et_ops = &ocfs2_dinode_et_ops;
242 } else if (et_type == OCFS2_XATTR_VALUE_EXTENT) {
243 struct ocfs2_xattr_value_root *xv =
244 (struct ocfs2_xattr_value_root *) private;
245 et->et_root_el = &xv->xr_list;
246 et->et_ops = &ocfs2_xattr_et_ops;
247 } else if (et_type == OCFS2_XATTR_TREE_EXTENT) {
248 struct ocfs2_xattr_block *xb =
249 (struct ocfs2_xattr_block *)bh->b_data;
250 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
251 et->et_ops = &ocfs2_xattr_tree_et_ops;
252 et->et_max_leaf_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
253 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
257 static void ocfs2_put_extent_tree(struct ocfs2_extent_tree *et)
259 brelse(et->et_root_bh);
262 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
265 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
268 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
270 return et->et_ops->eo_get_last_eb_blk(et);
273 static inline void ocfs2_et_update_clusters(struct inode *inode,
274 struct ocfs2_extent_tree *et,
277 et->et_ops->eo_update_clusters(inode, et, clusters);
280 static inline int ocfs2_et_sanity_check(struct inode *inode,
281 struct ocfs2_extent_tree *et)
283 return et->et_ops->eo_sanity_check(inode, et);
286 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
287 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
288 struct ocfs2_extent_block *eb);
291 * Structures which describe a path through a btree, and functions to
294 * The idea here is to be as generic as possible with the tree
297 struct ocfs2_path_item {
298 struct buffer_head *bh;
299 struct ocfs2_extent_list *el;
302 #define OCFS2_MAX_PATH_DEPTH 5
306 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
309 #define path_root_bh(_path) ((_path)->p_node[0].bh)
310 #define path_root_el(_path) ((_path)->p_node[0].el)
311 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
312 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
313 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
316 * Reset the actual path elements so that we can re-use the structure
317 * to build another path. Generally, this involves freeing the buffer
320 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
322 int i, start = 0, depth = 0;
323 struct ocfs2_path_item *node;
328 for(i = start; i < path_num_items(path); i++) {
329 node = &path->p_node[i];
337 * Tree depth may change during truncate, or insert. If we're
338 * keeping the root extent list, then make sure that our path
339 * structure reflects the proper depth.
342 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
344 path->p_tree_depth = depth;
347 static void ocfs2_free_path(struct ocfs2_path *path)
350 ocfs2_reinit_path(path, 0);
356 * All the elements of src into dest. After this call, src could be freed
357 * without affecting dest.
359 * Both paths should have the same root. Any non-root elements of dest
362 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
366 BUG_ON(path_root_bh(dest) != path_root_bh(src));
367 BUG_ON(path_root_el(dest) != path_root_el(src));
369 ocfs2_reinit_path(dest, 1);
371 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
372 dest->p_node[i].bh = src->p_node[i].bh;
373 dest->p_node[i].el = src->p_node[i].el;
375 if (dest->p_node[i].bh)
376 get_bh(dest->p_node[i].bh);
381 * Make the *dest path the same as src and re-initialize src path to
384 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
388 BUG_ON(path_root_bh(dest) != path_root_bh(src));
390 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
391 brelse(dest->p_node[i].bh);
393 dest->p_node[i].bh = src->p_node[i].bh;
394 dest->p_node[i].el = src->p_node[i].el;
396 src->p_node[i].bh = NULL;
397 src->p_node[i].el = NULL;
402 * Insert an extent block at given index.
404 * This will not take an additional reference on eb_bh.
406 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
407 struct buffer_head *eb_bh)
409 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
412 * Right now, no root bh is an extent block, so this helps
413 * catch code errors with dinode trees. The assertion can be
414 * safely removed if we ever need to insert extent block
415 * structures at the root.
419 path->p_node[index].bh = eb_bh;
420 path->p_node[index].el = &eb->h_list;
423 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
424 struct ocfs2_extent_list *root_el)
426 struct ocfs2_path *path;
428 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
430 path = kzalloc(sizeof(*path), GFP_NOFS);
432 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
434 path_root_bh(path) = root_bh;
435 path_root_el(path) = root_el;
442 * Convenience function to journal all components in a path.
444 static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
445 struct ocfs2_path *path)
452 for(i = 0; i < path_num_items(path); i++) {
453 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
454 OCFS2_JOURNAL_ACCESS_WRITE);
466 * Return the index of the extent record which contains cluster #v_cluster.
467 * -1 is returned if it was not found.
469 * Should work fine on interior and exterior nodes.
471 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
475 struct ocfs2_extent_rec *rec;
476 u32 rec_end, rec_start, clusters;
478 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
479 rec = &el->l_recs[i];
481 rec_start = le32_to_cpu(rec->e_cpos);
482 clusters = ocfs2_rec_clusters(el, rec);
484 rec_end = rec_start + clusters;
486 if (v_cluster >= rec_start && v_cluster < rec_end) {
495 enum ocfs2_contig_type {
504 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
505 * ocfs2_extent_contig only work properly against leaf nodes!
507 static int ocfs2_block_extent_contig(struct super_block *sb,
508 struct ocfs2_extent_rec *ext,
511 u64 blk_end = le64_to_cpu(ext->e_blkno);
513 blk_end += ocfs2_clusters_to_blocks(sb,
514 le16_to_cpu(ext->e_leaf_clusters));
516 return blkno == blk_end;
519 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
520 struct ocfs2_extent_rec *right)
524 left_range = le32_to_cpu(left->e_cpos) +
525 le16_to_cpu(left->e_leaf_clusters);
527 return (left_range == le32_to_cpu(right->e_cpos));
530 static enum ocfs2_contig_type
531 ocfs2_extent_contig(struct inode *inode,
532 struct ocfs2_extent_rec *ext,
533 struct ocfs2_extent_rec *insert_rec)
535 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
538 * Refuse to coalesce extent records with different flag
539 * fields - we don't want to mix unwritten extents with user
542 if (ext->e_flags != insert_rec->e_flags)
545 if (ocfs2_extents_adjacent(ext, insert_rec) &&
546 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
549 blkno = le64_to_cpu(ext->e_blkno);
550 if (ocfs2_extents_adjacent(insert_rec, ext) &&
551 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
558 * NOTE: We can have pretty much any combination of contiguousness and
561 * The usefulness of APPEND_TAIL is more in that it lets us know that
562 * we'll have to update the path to that leaf.
564 enum ocfs2_append_type {
569 enum ocfs2_split_type {
575 struct ocfs2_insert_type {
576 enum ocfs2_split_type ins_split;
577 enum ocfs2_append_type ins_appending;
578 enum ocfs2_contig_type ins_contig;
579 int ins_contig_index;
583 struct ocfs2_merge_ctxt {
584 enum ocfs2_contig_type c_contig_type;
585 int c_has_empty_extent;
586 int c_split_covers_rec;
590 * How many free extents have we got before we need more meta data?
592 int ocfs2_num_free_extents(struct ocfs2_super *osb,
594 struct buffer_head *root_bh,
595 enum ocfs2_extent_tree_type type,
599 struct ocfs2_extent_list *el = NULL;
600 struct ocfs2_extent_block *eb;
601 struct buffer_head *eb_bh = NULL;
606 if (type == OCFS2_DINODE_EXTENT) {
607 struct ocfs2_dinode *fe =
608 (struct ocfs2_dinode *)root_bh->b_data;
609 if (!OCFS2_IS_VALID_DINODE(fe)) {
610 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
615 if (fe->i_last_eb_blk)
616 last_eb_blk = le64_to_cpu(fe->i_last_eb_blk);
617 el = &fe->id2.i_list;
618 } else if (type == OCFS2_XATTR_VALUE_EXTENT) {
619 struct ocfs2_xattr_value_root *xv =
620 (struct ocfs2_xattr_value_root *) private;
622 last_eb_blk = le64_to_cpu(xv->xr_last_eb_blk);
624 } else if (type == OCFS2_XATTR_TREE_EXTENT) {
625 struct ocfs2_xattr_block *xb =
626 (struct ocfs2_xattr_block *)root_bh->b_data;
628 last_eb_blk = le64_to_cpu(xb->xb_attrs.xb_root.xt_last_eb_blk);
629 el = &xb->xb_attrs.xb_root.xt_list;
633 retval = ocfs2_read_block(osb, last_eb_blk,
634 &eb_bh, OCFS2_BH_CACHED, inode);
639 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
643 BUG_ON(el->l_tree_depth != 0);
645 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
654 /* expects array to already be allocated
656 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
659 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
663 struct ocfs2_alloc_context *meta_ac,
664 struct buffer_head *bhs[])
666 int count, status, i;
667 u16 suballoc_bit_start;
670 struct ocfs2_extent_block *eb;
675 while (count < wanted) {
676 status = ocfs2_claim_metadata(osb,
688 for(i = count; i < (num_got + count); i++) {
689 bhs[i] = sb_getblk(osb->sb, first_blkno);
690 if (bhs[i] == NULL) {
695 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
697 status = ocfs2_journal_access(handle, inode, bhs[i],
698 OCFS2_JOURNAL_ACCESS_CREATE);
704 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
705 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
706 /* Ok, setup the minimal stuff here. */
707 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
708 eb->h_blkno = cpu_to_le64(first_blkno);
709 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
710 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
711 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
713 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
715 suballoc_bit_start++;
718 /* We'll also be dirtied by the caller, so
719 * this isn't absolutely necessary. */
720 status = ocfs2_journal_dirty(handle, bhs[i]);
733 for(i = 0; i < wanted; i++) {
744 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
746 * Returns the sum of the rightmost extent rec logical offset and
749 * ocfs2_add_branch() uses this to determine what logical cluster
750 * value should be populated into the leftmost new branch records.
752 * ocfs2_shift_tree_depth() uses this to determine the # clusters
753 * value for the new topmost tree record.
755 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
759 i = le16_to_cpu(el->l_next_free_rec) - 1;
761 return le32_to_cpu(el->l_recs[i].e_cpos) +
762 ocfs2_rec_clusters(el, &el->l_recs[i]);
766 * Add an entire tree branch to our inode. eb_bh is the extent block
767 * to start at, if we don't want to start the branch at the dinode
770 * last_eb_bh is required as we have to update it's next_leaf pointer
771 * for the new last extent block.
773 * the new branch will be 'empty' in the sense that every block will
774 * contain a single record with cluster count == 0.
776 static int ocfs2_add_branch(struct ocfs2_super *osb,
779 struct ocfs2_extent_tree *et,
780 struct buffer_head *eb_bh,
781 struct buffer_head **last_eb_bh,
782 struct ocfs2_alloc_context *meta_ac)
784 int status, new_blocks, i;
785 u64 next_blkno, new_last_eb_blk;
786 struct buffer_head *bh;
787 struct buffer_head **new_eb_bhs = NULL;
788 struct ocfs2_extent_block *eb;
789 struct ocfs2_extent_list *eb_el;
790 struct ocfs2_extent_list *el;
795 BUG_ON(!last_eb_bh || !*last_eb_bh);
798 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
803 /* we never add a branch to a leaf. */
804 BUG_ON(!el->l_tree_depth);
806 new_blocks = le16_to_cpu(el->l_tree_depth);
808 /* allocate the number of new eb blocks we need */
809 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
817 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
818 meta_ac, new_eb_bhs);
824 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
825 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
827 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
828 * linked with the rest of the tree.
829 * conversly, new_eb_bhs[0] is the new bottommost leaf.
831 * when we leave the loop, new_last_eb_blk will point to the
832 * newest leaf, and next_blkno will point to the topmost extent
834 next_blkno = new_last_eb_blk = 0;
835 for(i = 0; i < new_blocks; i++) {
837 eb = (struct ocfs2_extent_block *) bh->b_data;
838 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
839 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
845 status = ocfs2_journal_access(handle, inode, bh,
846 OCFS2_JOURNAL_ACCESS_CREATE);
852 eb->h_next_leaf_blk = 0;
853 eb_el->l_tree_depth = cpu_to_le16(i);
854 eb_el->l_next_free_rec = cpu_to_le16(1);
856 * This actually counts as an empty extent as
859 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
860 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
862 * eb_el isn't always an interior node, but even leaf
863 * nodes want a zero'd flags and reserved field so
864 * this gets the whole 32 bits regardless of use.
866 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
867 if (!eb_el->l_tree_depth)
868 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
870 status = ocfs2_journal_dirty(handle, bh);
876 next_blkno = le64_to_cpu(eb->h_blkno);
879 /* This is a bit hairy. We want to update up to three blocks
880 * here without leaving any of them in an inconsistent state
881 * in case of error. We don't have to worry about
882 * journal_dirty erroring as it won't unless we've aborted the
883 * handle (in which case we would never be here) so reserving
884 * the write with journal_access is all we need to do. */
885 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
886 OCFS2_JOURNAL_ACCESS_WRITE);
891 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
892 OCFS2_JOURNAL_ACCESS_WRITE);
898 status = ocfs2_journal_access(handle, inode, eb_bh,
899 OCFS2_JOURNAL_ACCESS_WRITE);
906 /* Link the new branch into the rest of the tree (el will
907 * either be on the root_bh, or the extent block passed in. */
908 i = le16_to_cpu(el->l_next_free_rec);
909 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
910 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
911 el->l_recs[i].e_int_clusters = 0;
912 le16_add_cpu(&el->l_next_free_rec, 1);
914 /* fe needs a new last extent block pointer, as does the
915 * next_leaf on the previously last-extent-block. */
916 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
918 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
919 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
921 status = ocfs2_journal_dirty(handle, *last_eb_bh);
924 status = ocfs2_journal_dirty(handle, et->et_root_bh);
928 status = ocfs2_journal_dirty(handle, eb_bh);
934 * Some callers want to track the rightmost leaf so pass it
938 get_bh(new_eb_bhs[0]);
939 *last_eb_bh = new_eb_bhs[0];
944 for (i = 0; i < new_blocks; i++)
946 brelse(new_eb_bhs[i]);
955 * adds another level to the allocation tree.
956 * returns back the new extent block so you can add a branch to it
959 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
962 struct ocfs2_extent_tree *et,
963 struct ocfs2_alloc_context *meta_ac,
964 struct buffer_head **ret_new_eb_bh)
968 struct buffer_head *new_eb_bh = NULL;
969 struct ocfs2_extent_block *eb;
970 struct ocfs2_extent_list *root_el;
971 struct ocfs2_extent_list *eb_el;
975 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
982 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
983 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
984 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
990 root_el = et->et_root_el;
992 status = ocfs2_journal_access(handle, inode, new_eb_bh,
993 OCFS2_JOURNAL_ACCESS_CREATE);
999 /* copy the root extent list data into the new extent block */
1000 eb_el->l_tree_depth = root_el->l_tree_depth;
1001 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1002 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1003 eb_el->l_recs[i] = root_el->l_recs[i];
1005 status = ocfs2_journal_dirty(handle, new_eb_bh);
1011 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
1012 OCFS2_JOURNAL_ACCESS_WRITE);
1018 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1020 /* update root_bh now */
1021 le16_add_cpu(&root_el->l_tree_depth, 1);
1022 root_el->l_recs[0].e_cpos = 0;
1023 root_el->l_recs[0].e_blkno = eb->h_blkno;
1024 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1025 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1026 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1027 root_el->l_next_free_rec = cpu_to_le16(1);
1029 /* If this is our 1st tree depth shift, then last_eb_blk
1030 * becomes the allocated extent block */
1031 if (root_el->l_tree_depth == cpu_to_le16(1))
1032 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1034 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1040 *ret_new_eb_bh = new_eb_bh;
1052 * Should only be called when there is no space left in any of the
1053 * leaf nodes. What we want to do is find the lowest tree depth
1054 * non-leaf extent block with room for new records. There are three
1055 * valid results of this search:
1057 * 1) a lowest extent block is found, then we pass it back in
1058 * *lowest_eb_bh and return '0'
1060 * 2) the search fails to find anything, but the root_el has room. We
1061 * pass NULL back in *lowest_eb_bh, but still return '0'
1063 * 3) the search fails to find anything AND the root_el is full, in
1064 * which case we return > 0
1066 * return status < 0 indicates an error.
1068 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1069 struct inode *inode,
1070 struct ocfs2_extent_tree *et,
1071 struct buffer_head **target_bh)
1075 struct ocfs2_extent_block *eb;
1076 struct ocfs2_extent_list *el;
1077 struct buffer_head *bh = NULL;
1078 struct buffer_head *lowest_bh = NULL;
1084 el = et->et_root_el;
1086 while(le16_to_cpu(el->l_tree_depth) > 1) {
1087 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1088 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
1089 "extent list (next_free_rec == 0)",
1090 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1094 i = le16_to_cpu(el->l_next_free_rec) - 1;
1095 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1097 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
1098 "list where extent # %d has no physical "
1100 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
1110 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
1117 eb = (struct ocfs2_extent_block *) bh->b_data;
1118 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1119 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1125 if (le16_to_cpu(el->l_next_free_rec) <
1126 le16_to_cpu(el->l_count)) {
1134 /* If we didn't find one and the fe doesn't have any room,
1135 * then return '1' */
1136 el = et->et_root_el;
1137 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1140 *target_bh = lowest_bh;
1150 * Grow a b-tree so that it has more records.
1152 * We might shift the tree depth in which case existing paths should
1153 * be considered invalid.
1155 * Tree depth after the grow is returned via *final_depth.
1157 * *last_eb_bh will be updated by ocfs2_add_branch().
1159 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
1160 struct ocfs2_extent_tree *et, int *final_depth,
1161 struct buffer_head **last_eb_bh,
1162 struct ocfs2_alloc_context *meta_ac)
1165 struct ocfs2_extent_list *el = et->et_root_el;
1166 int depth = le16_to_cpu(el->l_tree_depth);
1167 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1168 struct buffer_head *bh = NULL;
1170 BUG_ON(meta_ac == NULL);
1172 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
1179 /* We traveled all the way to the bottom of the allocation tree
1180 * and didn't find room for any more extents - we need to add
1181 * another tree level */
1184 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1186 /* ocfs2_shift_tree_depth will return us a buffer with
1187 * the new extent block (so we can pass that to
1188 * ocfs2_add_branch). */
1189 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
1198 * Special case: we have room now if we shifted from
1199 * tree_depth 0, so no more work needs to be done.
1201 * We won't be calling add_branch, so pass
1202 * back *last_eb_bh as the new leaf. At depth
1203 * zero, it should always be null so there's
1204 * no reason to brelse.
1206 BUG_ON(*last_eb_bh);
1213 /* call ocfs2_add_branch to add the final part of the tree with
1215 mlog(0, "add branch. bh = %p\n", bh);
1216 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
1225 *final_depth = depth;
1231 * This function will discard the rightmost extent record.
1233 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1235 int next_free = le16_to_cpu(el->l_next_free_rec);
1236 int count = le16_to_cpu(el->l_count);
1237 unsigned int num_bytes;
1240 /* This will cause us to go off the end of our extent list. */
1241 BUG_ON(next_free >= count);
1243 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1245 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1248 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1249 struct ocfs2_extent_rec *insert_rec)
1251 int i, insert_index, next_free, has_empty, num_bytes;
1252 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1253 struct ocfs2_extent_rec *rec;
1255 next_free = le16_to_cpu(el->l_next_free_rec);
1256 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1260 /* The tree code before us didn't allow enough room in the leaf. */
1261 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1264 * The easiest way to approach this is to just remove the
1265 * empty extent and temporarily decrement next_free.
1269 * If next_free was 1 (only an empty extent), this
1270 * loop won't execute, which is fine. We still want
1271 * the decrement above to happen.
1273 for(i = 0; i < (next_free - 1); i++)
1274 el->l_recs[i] = el->l_recs[i+1];
1280 * Figure out what the new record index should be.
1282 for(i = 0; i < next_free; i++) {
1283 rec = &el->l_recs[i];
1285 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1290 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1291 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1293 BUG_ON(insert_index < 0);
1294 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1295 BUG_ON(insert_index > next_free);
1298 * No need to memmove if we're just adding to the tail.
1300 if (insert_index != next_free) {
1301 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1303 num_bytes = next_free - insert_index;
1304 num_bytes *= sizeof(struct ocfs2_extent_rec);
1305 memmove(&el->l_recs[insert_index + 1],
1306 &el->l_recs[insert_index],
1311 * Either we had an empty extent, and need to re-increment or
1312 * there was no empty extent on a non full rightmost leaf node,
1313 * in which case we still need to increment.
1316 el->l_next_free_rec = cpu_to_le16(next_free);
1318 * Make sure none of the math above just messed up our tree.
1320 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1322 el->l_recs[insert_index] = *insert_rec;
1326 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1328 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1330 BUG_ON(num_recs == 0);
1332 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1334 size = num_recs * sizeof(struct ocfs2_extent_rec);
1335 memmove(&el->l_recs[0], &el->l_recs[1], size);
1336 memset(&el->l_recs[num_recs], 0,
1337 sizeof(struct ocfs2_extent_rec));
1338 el->l_next_free_rec = cpu_to_le16(num_recs);
1343 * Create an empty extent record .
1345 * l_next_free_rec may be updated.
1347 * If an empty extent already exists do nothing.
1349 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1351 int next_free = le16_to_cpu(el->l_next_free_rec);
1353 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1358 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1361 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1362 "Asked to create an empty extent in a full list:\n"
1363 "count = %u, tree depth = %u",
1364 le16_to_cpu(el->l_count),
1365 le16_to_cpu(el->l_tree_depth));
1367 ocfs2_shift_records_right(el);
1370 le16_add_cpu(&el->l_next_free_rec, 1);
1371 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1375 * For a rotation which involves two leaf nodes, the "root node" is
1376 * the lowest level tree node which contains a path to both leafs. This
1377 * resulting set of information can be used to form a complete "subtree"
1379 * This function is passed two full paths from the dinode down to a
1380 * pair of adjacent leaves. It's task is to figure out which path
1381 * index contains the subtree root - this can be the root index itself
1382 * in a worst-case rotation.
1384 * The array index of the subtree root is passed back.
1386 static int ocfs2_find_subtree_root(struct inode *inode,
1387 struct ocfs2_path *left,
1388 struct ocfs2_path *right)
1393 * Check that the caller passed in two paths from the same tree.
1395 BUG_ON(path_root_bh(left) != path_root_bh(right));
1401 * The caller didn't pass two adjacent paths.
1403 mlog_bug_on_msg(i > left->p_tree_depth,
1404 "Inode %lu, left depth %u, right depth %u\n"
1405 "left leaf blk %llu, right leaf blk %llu\n",
1406 inode->i_ino, left->p_tree_depth,
1407 right->p_tree_depth,
1408 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1409 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1410 } while (left->p_node[i].bh->b_blocknr ==
1411 right->p_node[i].bh->b_blocknr);
1416 typedef void (path_insert_t)(void *, struct buffer_head *);
1419 * Traverse a btree path in search of cpos, starting at root_el.
1421 * This code can be called with a cpos larger than the tree, in which
1422 * case it will return the rightmost path.
1424 static int __ocfs2_find_path(struct inode *inode,
1425 struct ocfs2_extent_list *root_el, u32 cpos,
1426 path_insert_t *func, void *data)
1431 struct buffer_head *bh = NULL;
1432 struct ocfs2_extent_block *eb;
1433 struct ocfs2_extent_list *el;
1434 struct ocfs2_extent_rec *rec;
1435 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1438 while (el->l_tree_depth) {
1439 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1440 ocfs2_error(inode->i_sb,
1441 "Inode %llu has empty extent list at "
1443 (unsigned long long)oi->ip_blkno,
1444 le16_to_cpu(el->l_tree_depth));
1450 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1451 rec = &el->l_recs[i];
1454 * In the case that cpos is off the allocation
1455 * tree, this should just wind up returning the
1458 range = le32_to_cpu(rec->e_cpos) +
1459 ocfs2_rec_clusters(el, rec);
1460 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1464 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1466 ocfs2_error(inode->i_sb,
1467 "Inode %llu has bad blkno in extent list "
1468 "at depth %u (index %d)\n",
1469 (unsigned long long)oi->ip_blkno,
1470 le16_to_cpu(el->l_tree_depth), i);
1477 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1478 &bh, OCFS2_BH_CACHED, inode);
1484 eb = (struct ocfs2_extent_block *) bh->b_data;
1486 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1487 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1492 if (le16_to_cpu(el->l_next_free_rec) >
1493 le16_to_cpu(el->l_count)) {
1494 ocfs2_error(inode->i_sb,
1495 "Inode %llu has bad count in extent list "
1496 "at block %llu (next free=%u, count=%u)\n",
1497 (unsigned long long)oi->ip_blkno,
1498 (unsigned long long)bh->b_blocknr,
1499 le16_to_cpu(el->l_next_free_rec),
1500 le16_to_cpu(el->l_count));
1511 * Catch any trailing bh that the loop didn't handle.
1519 * Given an initialized path (that is, it has a valid root extent
1520 * list), this function will traverse the btree in search of the path
1521 * which would contain cpos.
1523 * The path traveled is recorded in the path structure.
1525 * Note that this will not do any comparisons on leaf node extent
1526 * records, so it will work fine in the case that we just added a tree
1529 struct find_path_data {
1531 struct ocfs2_path *path;
1533 static void find_path_ins(void *data, struct buffer_head *bh)
1535 struct find_path_data *fp = data;
1538 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1541 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1544 struct find_path_data data;
1548 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1549 find_path_ins, &data);
1552 static void find_leaf_ins(void *data, struct buffer_head *bh)
1554 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1555 struct ocfs2_extent_list *el = &eb->h_list;
1556 struct buffer_head **ret = data;
1558 /* We want to retain only the leaf block. */
1559 if (le16_to_cpu(el->l_tree_depth) == 0) {
1565 * Find the leaf block in the tree which would contain cpos. No
1566 * checking of the actual leaf is done.
1568 * Some paths want to call this instead of allocating a path structure
1569 * and calling ocfs2_find_path().
1571 * This function doesn't handle non btree extent lists.
1573 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1574 u32 cpos, struct buffer_head **leaf_bh)
1577 struct buffer_head *bh = NULL;
1579 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1591 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1593 * Basically, we've moved stuff around at the bottom of the tree and
1594 * we need to fix up the extent records above the changes to reflect
1597 * left_rec: the record on the left.
1598 * left_child_el: is the child list pointed to by left_rec
1599 * right_rec: the record to the right of left_rec
1600 * right_child_el: is the child list pointed to by right_rec
1602 * By definition, this only works on interior nodes.
1604 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1605 struct ocfs2_extent_list *left_child_el,
1606 struct ocfs2_extent_rec *right_rec,
1607 struct ocfs2_extent_list *right_child_el)
1609 u32 left_clusters, right_end;
1612 * Interior nodes never have holes. Their cpos is the cpos of
1613 * the leftmost record in their child list. Their cluster
1614 * count covers the full theoretical range of their child list
1615 * - the range between their cpos and the cpos of the record
1616 * immediately to their right.
1618 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1619 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1620 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1621 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1623 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1624 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1627 * Calculate the rightmost cluster count boundary before
1628 * moving cpos - we will need to adjust clusters after
1629 * updating e_cpos to keep the same highest cluster count.
1631 right_end = le32_to_cpu(right_rec->e_cpos);
1632 right_end += le32_to_cpu(right_rec->e_int_clusters);
1634 right_rec->e_cpos = left_rec->e_cpos;
1635 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1637 right_end -= le32_to_cpu(right_rec->e_cpos);
1638 right_rec->e_int_clusters = cpu_to_le32(right_end);
1642 * Adjust the adjacent root node records involved in a
1643 * rotation. left_el_blkno is passed in as a key so that we can easily
1644 * find it's index in the root list.
1646 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1647 struct ocfs2_extent_list *left_el,
1648 struct ocfs2_extent_list *right_el,
1653 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1654 le16_to_cpu(left_el->l_tree_depth));
1656 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1657 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1662 * The path walking code should have never returned a root and
1663 * two paths which are not adjacent.
1665 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1667 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1668 &root_el->l_recs[i + 1], right_el);
1672 * We've changed a leaf block (in right_path) and need to reflect that
1673 * change back up the subtree.
1675 * This happens in multiple places:
1676 * - When we've moved an extent record from the left path leaf to the right
1677 * path leaf to make room for an empty extent in the left path leaf.
1678 * - When our insert into the right path leaf is at the leftmost edge
1679 * and requires an update of the path immediately to it's left. This
1680 * can occur at the end of some types of rotation and appending inserts.
1681 * - When we've adjusted the last extent record in the left path leaf and the
1682 * 1st extent record in the right path leaf during cross extent block merge.
1684 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1685 struct ocfs2_path *left_path,
1686 struct ocfs2_path *right_path,
1690 struct ocfs2_extent_list *el, *left_el, *right_el;
1691 struct ocfs2_extent_rec *left_rec, *right_rec;
1692 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1695 * Update the counts and position values within all the
1696 * interior nodes to reflect the leaf rotation we just did.
1698 * The root node is handled below the loop.
1700 * We begin the loop with right_el and left_el pointing to the
1701 * leaf lists and work our way up.
1703 * NOTE: within this loop, left_el and right_el always refer
1704 * to the *child* lists.
1706 left_el = path_leaf_el(left_path);
1707 right_el = path_leaf_el(right_path);
1708 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1709 mlog(0, "Adjust records at index %u\n", i);
1712 * One nice property of knowing that all of these
1713 * nodes are below the root is that we only deal with
1714 * the leftmost right node record and the rightmost
1717 el = left_path->p_node[i].el;
1718 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1719 left_rec = &el->l_recs[idx];
1721 el = right_path->p_node[i].el;
1722 right_rec = &el->l_recs[0];
1724 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1727 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1731 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1736 * Setup our list pointers now so that the current
1737 * parents become children in the next iteration.
1739 left_el = left_path->p_node[i].el;
1740 right_el = right_path->p_node[i].el;
1744 * At the root node, adjust the two adjacent records which
1745 * begin our path to the leaves.
1748 el = left_path->p_node[subtree_index].el;
1749 left_el = left_path->p_node[subtree_index + 1].el;
1750 right_el = right_path->p_node[subtree_index + 1].el;
1752 ocfs2_adjust_root_records(el, left_el, right_el,
1753 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1755 root_bh = left_path->p_node[subtree_index].bh;
1757 ret = ocfs2_journal_dirty(handle, root_bh);
1762 static int ocfs2_rotate_subtree_right(struct inode *inode,
1764 struct ocfs2_path *left_path,
1765 struct ocfs2_path *right_path,
1769 struct buffer_head *right_leaf_bh;
1770 struct buffer_head *left_leaf_bh = NULL;
1771 struct buffer_head *root_bh;
1772 struct ocfs2_extent_list *right_el, *left_el;
1773 struct ocfs2_extent_rec move_rec;
1775 left_leaf_bh = path_leaf_bh(left_path);
1776 left_el = path_leaf_el(left_path);
1778 if (left_el->l_next_free_rec != left_el->l_count) {
1779 ocfs2_error(inode->i_sb,
1780 "Inode %llu has non-full interior leaf node %llu"
1782 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1783 (unsigned long long)left_leaf_bh->b_blocknr,
1784 le16_to_cpu(left_el->l_next_free_rec));
1789 * This extent block may already have an empty record, so we
1790 * return early if so.
1792 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1795 root_bh = left_path->p_node[subtree_index].bh;
1796 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1798 ret = ocfs2_journal_access(handle, inode, root_bh,
1799 OCFS2_JOURNAL_ACCESS_WRITE);
1805 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1806 ret = ocfs2_journal_access(handle, inode,
1807 right_path->p_node[i].bh,
1808 OCFS2_JOURNAL_ACCESS_WRITE);
1814 ret = ocfs2_journal_access(handle, inode,
1815 left_path->p_node[i].bh,
1816 OCFS2_JOURNAL_ACCESS_WRITE);
1823 right_leaf_bh = path_leaf_bh(right_path);
1824 right_el = path_leaf_el(right_path);
1826 /* This is a code error, not a disk corruption. */
1827 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1828 "because rightmost leaf block %llu is empty\n",
1829 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1830 (unsigned long long)right_leaf_bh->b_blocknr);
1832 ocfs2_create_empty_extent(right_el);
1834 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1840 /* Do the copy now. */
1841 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1842 move_rec = left_el->l_recs[i];
1843 right_el->l_recs[0] = move_rec;
1846 * Clear out the record we just copied and shift everything
1847 * over, leaving an empty extent in the left leaf.
1849 * We temporarily subtract from next_free_rec so that the
1850 * shift will lose the tail record (which is now defunct).
1852 le16_add_cpu(&left_el->l_next_free_rec, -1);
1853 ocfs2_shift_records_right(left_el);
1854 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1855 le16_add_cpu(&left_el->l_next_free_rec, 1);
1857 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1863 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1871 * Given a full path, determine what cpos value would return us a path
1872 * containing the leaf immediately to the left of the current one.
1874 * Will return zero if the path passed in is already the leftmost path.
1876 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1877 struct ocfs2_path *path, u32 *cpos)
1881 struct ocfs2_extent_list *el;
1883 BUG_ON(path->p_tree_depth == 0);
1887 blkno = path_leaf_bh(path)->b_blocknr;
1889 /* Start at the tree node just above the leaf and work our way up. */
1890 i = path->p_tree_depth - 1;
1892 el = path->p_node[i].el;
1895 * Find the extent record just before the one in our
1898 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1899 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1903 * We've determined that the
1904 * path specified is already
1905 * the leftmost one - return a
1911 * The leftmost record points to our
1912 * leaf - we need to travel up the
1918 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
1919 *cpos = *cpos + ocfs2_rec_clusters(el,
1920 &el->l_recs[j - 1]);
1927 * If we got here, we never found a valid node where
1928 * the tree indicated one should be.
1931 "Invalid extent tree at extent block %llu\n",
1932 (unsigned long long)blkno);
1937 blkno = path->p_node[i].bh->b_blocknr;
1946 * Extend the transaction by enough credits to complete the rotation,
1947 * and still leave at least the original number of credits allocated
1948 * to this transaction.
1950 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
1952 struct ocfs2_path *path)
1954 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
1956 if (handle->h_buffer_credits < credits)
1957 return ocfs2_extend_trans(handle, credits);
1963 * Trap the case where we're inserting into the theoretical range past
1964 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1965 * whose cpos is less than ours into the right leaf.
1967 * It's only necessary to look at the rightmost record of the left
1968 * leaf because the logic that calls us should ensure that the
1969 * theoretical ranges in the path components above the leaves are
1972 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1975 struct ocfs2_extent_list *left_el;
1976 struct ocfs2_extent_rec *rec;
1979 left_el = path_leaf_el(left_path);
1980 next_free = le16_to_cpu(left_el->l_next_free_rec);
1981 rec = &left_el->l_recs[next_free - 1];
1983 if (insert_cpos > le32_to_cpu(rec->e_cpos))
1988 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
1990 int next_free = le16_to_cpu(el->l_next_free_rec);
1992 struct ocfs2_extent_rec *rec;
1997 rec = &el->l_recs[0];
1998 if (ocfs2_is_empty_extent(rec)) {
2002 rec = &el->l_recs[1];
2005 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2006 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2012 * Rotate all the records in a btree right one record, starting at insert_cpos.
2014 * The path to the rightmost leaf should be passed in.
2016 * The array is assumed to be large enough to hold an entire path (tree depth).
2018 * Upon succesful return from this function:
2020 * - The 'right_path' array will contain a path to the leaf block
2021 * whose range contains e_cpos.
2022 * - That leaf block will have a single empty extent in list index 0.
2023 * - In the case that the rotation requires a post-insert update,
2024 * *ret_left_path will contain a valid path which can be passed to
2025 * ocfs2_insert_path().
2027 static int ocfs2_rotate_tree_right(struct inode *inode,
2029 enum ocfs2_split_type split,
2031 struct ocfs2_path *right_path,
2032 struct ocfs2_path **ret_left_path)
2034 int ret, start, orig_credits = handle->h_buffer_credits;
2036 struct ocfs2_path *left_path = NULL;
2038 *ret_left_path = NULL;
2040 left_path = ocfs2_new_path(path_root_bh(right_path),
2041 path_root_el(right_path));
2048 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2054 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2057 * What we want to do here is:
2059 * 1) Start with the rightmost path.
2061 * 2) Determine a path to the leaf block directly to the left
2064 * 3) Determine the 'subtree root' - the lowest level tree node
2065 * which contains a path to both leaves.
2067 * 4) Rotate the subtree.
2069 * 5) Find the next subtree by considering the left path to be
2070 * the new right path.
2072 * The check at the top of this while loop also accepts
2073 * insert_cpos == cpos because cpos is only a _theoretical_
2074 * value to get us the left path - insert_cpos might very well
2075 * be filling that hole.
2077 * Stop at a cpos of '0' because we either started at the
2078 * leftmost branch (i.e., a tree with one branch and a
2079 * rotation inside of it), or we've gone as far as we can in
2080 * rotating subtrees.
2082 while (cpos && insert_cpos <= cpos) {
2083 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2086 ret = ocfs2_find_path(inode, left_path, cpos);
2092 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2093 path_leaf_bh(right_path),
2094 "Inode %lu: error during insert of %u "
2095 "(left path cpos %u) results in two identical "
2096 "paths ending at %llu\n",
2097 inode->i_ino, insert_cpos, cpos,
2098 (unsigned long long)
2099 path_leaf_bh(left_path)->b_blocknr);
2101 if (split == SPLIT_NONE &&
2102 ocfs2_rotate_requires_path_adjustment(left_path,
2106 * We've rotated the tree as much as we
2107 * should. The rest is up to
2108 * ocfs2_insert_path() to complete, after the
2109 * record insertion. We indicate this
2110 * situation by returning the left path.
2112 * The reason we don't adjust the records here
2113 * before the record insert is that an error
2114 * later might break the rule where a parent
2115 * record e_cpos will reflect the actual
2116 * e_cpos of the 1st nonempty record of the
2119 *ret_left_path = left_path;
2123 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2125 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2127 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2128 right_path->p_tree_depth);
2130 ret = ocfs2_extend_rotate_transaction(handle, start,
2131 orig_credits, right_path);
2137 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2144 if (split != SPLIT_NONE &&
2145 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2148 * A rotate moves the rightmost left leaf
2149 * record over to the leftmost right leaf
2150 * slot. If we're doing an extent split
2151 * instead of a real insert, then we have to
2152 * check that the extent to be split wasn't
2153 * just moved over. If it was, then we can
2154 * exit here, passing left_path back -
2155 * ocfs2_split_extent() is smart enough to
2156 * search both leaves.
2158 *ret_left_path = left_path;
2163 * There is no need to re-read the next right path
2164 * as we know that it'll be our current left
2165 * path. Optimize by copying values instead.
2167 ocfs2_mv_path(right_path, left_path);
2169 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2178 ocfs2_free_path(left_path);
2184 static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2185 struct ocfs2_path *path)
2188 struct ocfs2_extent_rec *rec;
2189 struct ocfs2_extent_list *el;
2190 struct ocfs2_extent_block *eb;
2193 /* Path should always be rightmost. */
2194 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2195 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2198 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2199 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2200 rec = &el->l_recs[idx];
2201 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2203 for (i = 0; i < path->p_tree_depth; i++) {
2204 el = path->p_node[i].el;
2205 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2206 rec = &el->l_recs[idx];
2208 rec->e_int_clusters = cpu_to_le32(range);
2209 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2211 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2215 static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2216 struct ocfs2_cached_dealloc_ctxt *dealloc,
2217 struct ocfs2_path *path, int unlink_start)
2220 struct ocfs2_extent_block *eb;
2221 struct ocfs2_extent_list *el;
2222 struct buffer_head *bh;
2224 for(i = unlink_start; i < path_num_items(path); i++) {
2225 bh = path->p_node[i].bh;
2227 eb = (struct ocfs2_extent_block *)bh->b_data;
2229 * Not all nodes might have had their final count
2230 * decremented by the caller - handle this here.
2233 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2235 "Inode %llu, attempted to remove extent block "
2236 "%llu with %u records\n",
2237 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2238 (unsigned long long)le64_to_cpu(eb->h_blkno),
2239 le16_to_cpu(el->l_next_free_rec));
2241 ocfs2_journal_dirty(handle, bh);
2242 ocfs2_remove_from_cache(inode, bh);
2246 el->l_next_free_rec = 0;
2247 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2249 ocfs2_journal_dirty(handle, bh);
2251 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2255 ocfs2_remove_from_cache(inode, bh);
2259 static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2260 struct ocfs2_path *left_path,
2261 struct ocfs2_path *right_path,
2263 struct ocfs2_cached_dealloc_ctxt *dealloc)
2266 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2267 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2268 struct ocfs2_extent_list *el;
2269 struct ocfs2_extent_block *eb;
2271 el = path_leaf_el(left_path);
2273 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2275 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2276 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2279 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2281 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2282 le16_add_cpu(&root_el->l_next_free_rec, -1);
2284 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2285 eb->h_next_leaf_blk = 0;
2287 ocfs2_journal_dirty(handle, root_bh);
2288 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2290 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2294 static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2295 struct ocfs2_path *left_path,
2296 struct ocfs2_path *right_path,
2298 struct ocfs2_cached_dealloc_ctxt *dealloc,
2300 struct ocfs2_extent_tree *et)
2302 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2303 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2304 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2305 struct ocfs2_extent_block *eb;
2309 right_leaf_el = path_leaf_el(right_path);
2310 left_leaf_el = path_leaf_el(left_path);
2311 root_bh = left_path->p_node[subtree_index].bh;
2312 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2314 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2317 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2318 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2320 * It's legal for us to proceed if the right leaf is
2321 * the rightmost one and it has an empty extent. There
2322 * are two cases to handle - whether the leaf will be
2323 * empty after removal or not. If the leaf isn't empty
2324 * then just remove the empty extent up front. The
2325 * next block will handle empty leaves by flagging
2328 * Non rightmost leaves will throw -EAGAIN and the
2329 * caller can manually move the subtree and retry.
2332 if (eb->h_next_leaf_blk != 0ULL)
2335 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2336 ret = ocfs2_journal_access(handle, inode,
2337 path_leaf_bh(right_path),
2338 OCFS2_JOURNAL_ACCESS_WRITE);
2344 ocfs2_remove_empty_extent(right_leaf_el);
2346 right_has_empty = 1;
2349 if (eb->h_next_leaf_blk == 0ULL &&
2350 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2352 * We have to update i_last_eb_blk during the meta
2355 ret = ocfs2_journal_access(handle, inode, et_root_bh,
2356 OCFS2_JOURNAL_ACCESS_WRITE);
2362 del_right_subtree = 1;
2366 * Getting here with an empty extent in the right path implies
2367 * that it's the rightmost path and will be deleted.
2369 BUG_ON(right_has_empty && !del_right_subtree);
2371 ret = ocfs2_journal_access(handle, inode, root_bh,
2372 OCFS2_JOURNAL_ACCESS_WRITE);
2378 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2379 ret = ocfs2_journal_access(handle, inode,
2380 right_path->p_node[i].bh,
2381 OCFS2_JOURNAL_ACCESS_WRITE);
2387 ret = ocfs2_journal_access(handle, inode,
2388 left_path->p_node[i].bh,
2389 OCFS2_JOURNAL_ACCESS_WRITE);
2396 if (!right_has_empty) {
2398 * Only do this if we're moving a real
2399 * record. Otherwise, the action is delayed until
2400 * after removal of the right path in which case we
2401 * can do a simple shift to remove the empty extent.
2403 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2404 memset(&right_leaf_el->l_recs[0], 0,
2405 sizeof(struct ocfs2_extent_rec));
2407 if (eb->h_next_leaf_blk == 0ULL) {
2409 * Move recs over to get rid of empty extent, decrease
2410 * next_free. This is allowed to remove the last
2411 * extent in our leaf (setting l_next_free_rec to
2412 * zero) - the delete code below won't care.
2414 ocfs2_remove_empty_extent(right_leaf_el);
2417 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2420 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2424 if (del_right_subtree) {
2425 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2426 subtree_index, dealloc);
2427 ocfs2_update_edge_lengths(inode, handle, left_path);
2429 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2430 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2433 * Removal of the extent in the left leaf was skipped
2434 * above so we could delete the right path
2437 if (right_has_empty)
2438 ocfs2_remove_empty_extent(left_leaf_el);
2440 ret = ocfs2_journal_dirty(handle, et_root_bh);
2446 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2454 * Given a full path, determine what cpos value would return us a path
2455 * containing the leaf immediately to the right of the current one.
2457 * Will return zero if the path passed in is already the rightmost path.
2459 * This looks similar, but is subtly different to
2460 * ocfs2_find_cpos_for_left_leaf().
2462 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2463 struct ocfs2_path *path, u32 *cpos)
2467 struct ocfs2_extent_list *el;
2471 if (path->p_tree_depth == 0)
2474 blkno = path_leaf_bh(path)->b_blocknr;
2476 /* Start at the tree node just above the leaf and work our way up. */
2477 i = path->p_tree_depth - 1;
2481 el = path->p_node[i].el;
2484 * Find the extent record just after the one in our
2487 next_free = le16_to_cpu(el->l_next_free_rec);
2488 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2489 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2490 if (j == (next_free - 1)) {
2493 * We've determined that the
2494 * path specified is already
2495 * the rightmost one - return a
2501 * The rightmost record points to our
2502 * leaf - we need to travel up the
2508 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2514 * If we got here, we never found a valid node where
2515 * the tree indicated one should be.
2518 "Invalid extent tree at extent block %llu\n",
2519 (unsigned long long)blkno);
2524 blkno = path->p_node[i].bh->b_blocknr;
2532 static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2534 struct buffer_head *bh,
2535 struct ocfs2_extent_list *el)
2539 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2542 ret = ocfs2_journal_access(handle, inode, bh,
2543 OCFS2_JOURNAL_ACCESS_WRITE);
2549 ocfs2_remove_empty_extent(el);
2551 ret = ocfs2_journal_dirty(handle, bh);
2559 static int __ocfs2_rotate_tree_left(struct inode *inode,
2560 handle_t *handle, int orig_credits,
2561 struct ocfs2_path *path,
2562 struct ocfs2_cached_dealloc_ctxt *dealloc,
2563 struct ocfs2_path **empty_extent_path,
2564 struct ocfs2_extent_tree *et)
2566 int ret, subtree_root, deleted;
2568 struct ocfs2_path *left_path = NULL;
2569 struct ocfs2_path *right_path = NULL;
2571 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2573 *empty_extent_path = NULL;
2575 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2582 left_path = ocfs2_new_path(path_root_bh(path),
2583 path_root_el(path));
2590 ocfs2_cp_path(left_path, path);
2592 right_path = ocfs2_new_path(path_root_bh(path),
2593 path_root_el(path));
2600 while (right_cpos) {
2601 ret = ocfs2_find_path(inode, right_path, right_cpos);
2607 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2610 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2612 (unsigned long long)
2613 right_path->p_node[subtree_root].bh->b_blocknr,
2614 right_path->p_tree_depth);
2616 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2617 orig_credits, left_path);
2624 * Caller might still want to make changes to the
2625 * tree root, so re-add it to the journal here.
2627 ret = ocfs2_journal_access(handle, inode,
2628 path_root_bh(left_path),
2629 OCFS2_JOURNAL_ACCESS_WRITE);
2635 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2636 right_path, subtree_root,
2637 dealloc, &deleted, et);
2638 if (ret == -EAGAIN) {
2640 * The rotation has to temporarily stop due to
2641 * the right subtree having an empty
2642 * extent. Pass it back to the caller for a
2645 *empty_extent_path = right_path;
2655 * The subtree rotate might have removed records on
2656 * the rightmost edge. If so, then rotation is
2662 ocfs2_mv_path(left_path, right_path);
2664 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2673 ocfs2_free_path(right_path);
2674 ocfs2_free_path(left_path);
2679 static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2680 struct ocfs2_path *path,
2681 struct ocfs2_cached_dealloc_ctxt *dealloc,
2682 struct ocfs2_extent_tree *et)
2684 int ret, subtree_index;
2686 struct ocfs2_path *left_path = NULL;
2687 struct ocfs2_extent_block *eb;
2688 struct ocfs2_extent_list *el;
2691 ret = ocfs2_et_sanity_check(inode, et);
2695 * There's two ways we handle this depending on
2696 * whether path is the only existing one.
2698 ret = ocfs2_extend_rotate_transaction(handle, 0,
2699 handle->h_buffer_credits,
2706 ret = ocfs2_journal_access_path(inode, handle, path);
2712 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2720 * We have a path to the left of this one - it needs
2723 left_path = ocfs2_new_path(path_root_bh(path),
2724 path_root_el(path));
2731 ret = ocfs2_find_path(inode, left_path, cpos);
2737 ret = ocfs2_journal_access_path(inode, handle, left_path);
2743 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2745 ocfs2_unlink_subtree(inode, handle, left_path, path,
2746 subtree_index, dealloc);
2747 ocfs2_update_edge_lengths(inode, handle, left_path);
2749 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2750 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2753 * 'path' is also the leftmost path which
2754 * means it must be the only one. This gets
2755 * handled differently because we want to
2756 * revert the inode back to having extents
2759 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2761 el = et->et_root_el;
2762 el->l_tree_depth = 0;
2763 el->l_next_free_rec = 0;
2764 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2766 ocfs2_et_set_last_eb_blk(et, 0);
2769 ocfs2_journal_dirty(handle, path_root_bh(path));
2772 ocfs2_free_path(left_path);
2777 * Left rotation of btree records.
2779 * In many ways, this is (unsurprisingly) the opposite of right
2780 * rotation. We start at some non-rightmost path containing an empty
2781 * extent in the leaf block. The code works its way to the rightmost
2782 * path by rotating records to the left in every subtree.
2784 * This is used by any code which reduces the number of extent records
2785 * in a leaf. After removal, an empty record should be placed in the
2786 * leftmost list position.
2788 * This won't handle a length update of the rightmost path records if
2789 * the rightmost tree leaf record is removed so the caller is
2790 * responsible for detecting and correcting that.
2792 static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2793 struct ocfs2_path *path,
2794 struct ocfs2_cached_dealloc_ctxt *dealloc,
2795 struct ocfs2_extent_tree *et)
2797 int ret, orig_credits = handle->h_buffer_credits;
2798 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2799 struct ocfs2_extent_block *eb;
2800 struct ocfs2_extent_list *el;
2802 el = path_leaf_el(path);
2803 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2806 if (path->p_tree_depth == 0) {
2807 rightmost_no_delete:
2809 * Inline extents. This is trivially handled, so do
2812 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2814 path_leaf_el(path));
2821 * Handle rightmost branch now. There's several cases:
2822 * 1) simple rotation leaving records in there. That's trivial.
2823 * 2) rotation requiring a branch delete - there's no more
2824 * records left. Two cases of this:
2825 * a) There are branches to the left.
2826 * b) This is also the leftmost (the only) branch.
2828 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2829 * 2a) we need the left branch so that we can update it with the unlink
2830 * 2b) we need to bring the inode back to inline extents.
2833 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2835 if (eb->h_next_leaf_blk == 0) {
2837 * This gets a bit tricky if we're going to delete the
2838 * rightmost path. Get the other cases out of the way
2841 if (le16_to_cpu(el->l_next_free_rec) > 1)
2842 goto rightmost_no_delete;
2844 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2846 ocfs2_error(inode->i_sb,
2847 "Inode %llu has empty extent block at %llu",
2848 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2849 (unsigned long long)le64_to_cpu(eb->h_blkno));
2854 * XXX: The caller can not trust "path" any more after
2855 * this as it will have been deleted. What do we do?
2857 * In theory the rotate-for-merge code will never get
2858 * here because it'll always ask for a rotate in a
2862 ret = ocfs2_remove_rightmost_path(inode, handle, path,
2870 * Now we can loop, remembering the path we get from -EAGAIN
2871 * and restarting from there.
2874 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
2875 dealloc, &restart_path, et);
2876 if (ret && ret != -EAGAIN) {
2881 while (ret == -EAGAIN) {
2882 tmp_path = restart_path;
2883 restart_path = NULL;
2885 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2888 if (ret && ret != -EAGAIN) {
2893 ocfs2_free_path(tmp_path);
2901 ocfs2_free_path(tmp_path);
2902 ocfs2_free_path(restart_path);
2906 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2909 struct ocfs2_extent_rec *rec = &el->l_recs[index];
2912 if (rec->e_leaf_clusters == 0) {
2914 * We consumed all of the merged-from record. An empty
2915 * extent cannot exist anywhere but the 1st array
2916 * position, so move things over if the merged-from
2917 * record doesn't occupy that position.
2919 * This creates a new empty extent so the caller
2920 * should be smart enough to have removed any existing
2924 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
2925 size = index * sizeof(struct ocfs2_extent_rec);
2926 memmove(&el->l_recs[1], &el->l_recs[0], size);
2930 * Always memset - the caller doesn't check whether it
2931 * created an empty extent, so there could be junk in
2934 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2938 static int ocfs2_get_right_path(struct inode *inode,
2939 struct ocfs2_path *left_path,
2940 struct ocfs2_path **ret_right_path)
2944 struct ocfs2_path *right_path = NULL;
2945 struct ocfs2_extent_list *left_el;
2947 *ret_right_path = NULL;
2949 /* This function shouldn't be called for non-trees. */
2950 BUG_ON(left_path->p_tree_depth == 0);
2952 left_el = path_leaf_el(left_path);
2953 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
2955 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2962 /* This function shouldn't be called for the rightmost leaf. */
2963 BUG_ON(right_cpos == 0);
2965 right_path = ocfs2_new_path(path_root_bh(left_path),
2966 path_root_el(left_path));
2973 ret = ocfs2_find_path(inode, right_path, right_cpos);
2979 *ret_right_path = right_path;
2982 ocfs2_free_path(right_path);
2987 * Remove split_rec clusters from the record at index and merge them
2988 * onto the beginning of the record "next" to it.
2989 * For index < l_count - 1, the next means the extent rec at index + 1.
2990 * For index == l_count - 1, the "next" means the 1st extent rec of the
2991 * next extent block.
2993 static int ocfs2_merge_rec_right(struct inode *inode,
2994 struct ocfs2_path *left_path,
2996 struct ocfs2_extent_rec *split_rec,
2999 int ret, next_free, i;
3000 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3001 struct ocfs2_extent_rec *left_rec;
3002 struct ocfs2_extent_rec *right_rec;
3003 struct ocfs2_extent_list *right_el;
3004 struct ocfs2_path *right_path = NULL;
3005 int subtree_index = 0;
3006 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3007 struct buffer_head *bh = path_leaf_bh(left_path);
3008 struct buffer_head *root_bh = NULL;
3010 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3011 left_rec = &el->l_recs[index];
3013 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3014 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3015 /* we meet with a cross extent block merge. */
3016 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3022 right_el = path_leaf_el(right_path);
3023 next_free = le16_to_cpu(right_el->l_next_free_rec);
3024 BUG_ON(next_free <= 0);
3025 right_rec = &right_el->l_recs[0];
3026 if (ocfs2_is_empty_extent(right_rec)) {
3027 BUG_ON(next_free <= 1);
3028 right_rec = &right_el->l_recs[1];
3031 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3032 le16_to_cpu(left_rec->e_leaf_clusters) !=
3033 le32_to_cpu(right_rec->e_cpos));
3035 subtree_index = ocfs2_find_subtree_root(inode,
3036 left_path, right_path);
3038 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3039 handle->h_buffer_credits,
3046 root_bh = left_path->p_node[subtree_index].bh;
3047 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3049 ret = ocfs2_journal_access(handle, inode, root_bh,
3050 OCFS2_JOURNAL_ACCESS_WRITE);
3056 for (i = subtree_index + 1;
3057 i < path_num_items(right_path); i++) {
3058 ret = ocfs2_journal_access(handle, inode,
3059 right_path->p_node[i].bh,
3060 OCFS2_JOURNAL_ACCESS_WRITE);
3066 ret = ocfs2_journal_access(handle, inode,
3067 left_path->p_node[i].bh,
3068 OCFS2_JOURNAL_ACCESS_WRITE);
3076 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3077 right_rec = &el->l_recs[index + 1];
3080 ret = ocfs2_journal_access(handle, inode, bh,
3081 OCFS2_JOURNAL_ACCESS_WRITE);
3087 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3089 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3090 le64_add_cpu(&right_rec->e_blkno,
3091 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3092 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3094 ocfs2_cleanup_merge(el, index);
3096 ret = ocfs2_journal_dirty(handle, bh);
3101 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3105 ocfs2_complete_edge_insert(inode, handle, left_path,
3106 right_path, subtree_index);
3110 ocfs2_free_path(right_path);
3114 static int ocfs2_get_left_path(struct inode *inode,
3115 struct ocfs2_path *right_path,
3116 struct ocfs2_path **ret_left_path)
3120 struct ocfs2_path *left_path = NULL;
3122 *ret_left_path = NULL;
3124 /* This function shouldn't be called for non-trees. */
3125 BUG_ON(right_path->p_tree_depth == 0);
3127 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3128 right_path, &left_cpos);
3134 /* This function shouldn't be called for the leftmost leaf. */
3135 BUG_ON(left_cpos == 0);
3137 left_path = ocfs2_new_path(path_root_bh(right_path),
3138 path_root_el(right_path));
3145 ret = ocfs2_find_path(inode, left_path, left_cpos);
3151 *ret_left_path = left_path;
3154 ocfs2_free_path(left_path);
3159 * Remove split_rec clusters from the record at index and merge them
3160 * onto the tail of the record "before" it.
3161 * For index > 0, the "before" means the extent rec at index - 1.
3163 * For index == 0, the "before" means the last record of the previous
3164 * extent block. And there is also a situation that we may need to
3165 * remove the rightmost leaf extent block in the right_path and change
3166 * the right path to indicate the new rightmost path.
3168 static int ocfs2_merge_rec_left(struct inode *inode,
3169 struct ocfs2_path *right_path,
3171 struct ocfs2_extent_rec *split_rec,
3172 struct ocfs2_cached_dealloc_ctxt *dealloc,
3173 struct ocfs2_extent_tree *et,
3176 int ret, i, subtree_index = 0, has_empty_extent = 0;
3177 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3178 struct ocfs2_extent_rec *left_rec;
3179 struct ocfs2_extent_rec *right_rec;
3180 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3181 struct buffer_head *bh = path_leaf_bh(right_path);
3182 struct buffer_head *root_bh = NULL;
3183 struct ocfs2_path *left_path = NULL;
3184 struct ocfs2_extent_list *left_el;
3188 right_rec = &el->l_recs[index];
3190 /* we meet with a cross extent block merge. */
3191 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3197 left_el = path_leaf_el(left_path);
3198 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3199 le16_to_cpu(left_el->l_count));
3201 left_rec = &left_el->l_recs[
3202 le16_to_cpu(left_el->l_next_free_rec) - 1];
3203 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3204 le16_to_cpu(left_rec->e_leaf_clusters) !=
3205 le32_to_cpu(split_rec->e_cpos));
3207 subtree_index = ocfs2_find_subtree_root(inode,
3208 left_path, right_path);
3210 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3211 handle->h_buffer_credits,
3218 root_bh = left_path->p_node[subtree_index].bh;
3219 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3221 ret = ocfs2_journal_access(handle, inode, root_bh,
3222 OCFS2_JOURNAL_ACCESS_WRITE);
3228 for (i = subtree_index + 1;
3229 i < path_num_items(right_path); i++) {
3230 ret = ocfs2_journal_access(handle, inode,
3231 right_path->p_node[i].bh,
3232 OCFS2_JOURNAL_ACCESS_WRITE);
3238 ret = ocfs2_journal_access(handle, inode,
3239 left_path->p_node[i].bh,
3240 OCFS2_JOURNAL_ACCESS_WRITE);
3247 left_rec = &el->l_recs[index - 1];
3248 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3249 has_empty_extent = 1;
3252 ret = ocfs2_journal_access(handle, inode, bh,
3253 OCFS2_JOURNAL_ACCESS_WRITE);
3259 if (has_empty_extent && index == 1) {
3261 * The easy case - we can just plop the record right in.
3263 *left_rec = *split_rec;
3265 has_empty_extent = 0;
3267 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3269 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3270 le64_add_cpu(&right_rec->e_blkno,
3271 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3272 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3274 ocfs2_cleanup_merge(el, index);
3276 ret = ocfs2_journal_dirty(handle, bh);
3281 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3286 * In the situation that the right_rec is empty and the extent
3287 * block is empty also, ocfs2_complete_edge_insert can't handle
3288 * it and we need to delete the right extent block.
3290 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3291 le16_to_cpu(el->l_next_free_rec) == 1) {
3293 ret = ocfs2_remove_rightmost_path(inode, handle,
3301 /* Now the rightmost extent block has been deleted.
3302 * So we use the new rightmost path.
3304 ocfs2_mv_path(right_path, left_path);
3307 ocfs2_complete_edge_insert(inode, handle, left_path,
3308 right_path, subtree_index);
3312 ocfs2_free_path(left_path);
3316 static int ocfs2_try_to_merge_extent(struct inode *inode,
3318 struct ocfs2_path *path,
3320 struct ocfs2_extent_rec *split_rec,
3321 struct ocfs2_cached_dealloc_ctxt *dealloc,
3322 struct ocfs2_merge_ctxt *ctxt,
3323 struct ocfs2_extent_tree *et)
3327 struct ocfs2_extent_list *el = path_leaf_el(path);
3328 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3330 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3332 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3334 * The merge code will need to create an empty
3335 * extent to take the place of the newly
3336 * emptied slot. Remove any pre-existing empty
3337 * extents - having more than one in a leaf is
3340 ret = ocfs2_rotate_tree_left(inode, handle, path,
3347 rec = &el->l_recs[split_index];
3350 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3352 * Left-right contig implies this.
3354 BUG_ON(!ctxt->c_split_covers_rec);
3357 * Since the leftright insert always covers the entire
3358 * extent, this call will delete the insert record
3359 * entirely, resulting in an empty extent record added to
3362 * Since the adding of an empty extent shifts
3363 * everything back to the right, there's no need to
3364 * update split_index here.
3366 * When the split_index is zero, we need to merge it to the
3367 * prevoius extent block. It is more efficient and easier
3368 * if we do merge_right first and merge_left later.
3370 ret = ocfs2_merge_rec_right(inode, path,
3379 * We can only get this from logic error above.
3381 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3383 /* The merge left us with an empty extent, remove it. */
3384 ret = ocfs2_rotate_tree_left(inode, handle, path,
3391 rec = &el->l_recs[split_index];
3394 * Note that we don't pass split_rec here on purpose -
3395 * we've merged it into the rec already.
3397 ret = ocfs2_merge_rec_left(inode, path,
3407 ret = ocfs2_rotate_tree_left(inode, handle, path,
3410 * Error from this last rotate is not critical, so
3411 * print but don't bubble it up.
3418 * Merge a record to the left or right.
3420 * 'contig_type' is relative to the existing record,
3421 * so for example, if we're "right contig", it's to
3422 * the record on the left (hence the left merge).
3424 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3425 ret = ocfs2_merge_rec_left(inode,
3435 ret = ocfs2_merge_rec_right(inode,
3445 if (ctxt->c_split_covers_rec) {
3447 * The merge may have left an empty extent in
3448 * our leaf. Try to rotate it away.
3450 ret = ocfs2_rotate_tree_left(inode, handle, path,
3462 static void ocfs2_subtract_from_rec(struct super_block *sb,
3463 enum ocfs2_split_type split,
3464 struct ocfs2_extent_rec *rec,
3465 struct ocfs2_extent_rec *split_rec)
3469 len_blocks = ocfs2_clusters_to_blocks(sb,
3470 le16_to_cpu(split_rec->e_leaf_clusters));
3472 if (split == SPLIT_LEFT) {
3474 * Region is on the left edge of the existing
3477 le32_add_cpu(&rec->e_cpos,
3478 le16_to_cpu(split_rec->e_leaf_clusters));
3479 le64_add_cpu(&rec->e_blkno, len_blocks);
3480 le16_add_cpu(&rec->e_leaf_clusters,
3481 -le16_to_cpu(split_rec->e_leaf_clusters));
3484 * Region is on the right edge of the existing
3487 le16_add_cpu(&rec->e_leaf_clusters,
3488 -le16_to_cpu(split_rec->e_leaf_clusters));
3493 * Do the final bits of extent record insertion at the target leaf
3494 * list. If this leaf is part of an allocation tree, it is assumed
3495 * that the tree above has been prepared.
3497 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3498 struct ocfs2_extent_list *el,
3499 struct ocfs2_insert_type *insert,
3500 struct inode *inode)
3502 int i = insert->ins_contig_index;
3504 struct ocfs2_extent_rec *rec;
3506 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3508 if (insert->ins_split != SPLIT_NONE) {
3509 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3511 rec = &el->l_recs[i];
3512 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3518 * Contiguous insert - either left or right.
3520 if (insert->ins_contig != CONTIG_NONE) {
3521 rec = &el->l_recs[i];
3522 if (insert->ins_contig == CONTIG_LEFT) {
3523 rec->e_blkno = insert_rec->e_blkno;
3524 rec->e_cpos = insert_rec->e_cpos;
3526 le16_add_cpu(&rec->e_leaf_clusters,
3527 le16_to_cpu(insert_rec->e_leaf_clusters));
3532 * Handle insert into an empty leaf.
3534 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3535 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3536 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3537 el->l_recs[0] = *insert_rec;
3538 el->l_next_free_rec = cpu_to_le16(1);
3545 if (insert->ins_appending == APPEND_TAIL) {
3546 i = le16_to_cpu(el->l_next_free_rec) - 1;
3547 rec = &el->l_recs[i];
3548 range = le32_to_cpu(rec->e_cpos)
3549 + le16_to_cpu(rec->e_leaf_clusters);
3550 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3552 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3553 le16_to_cpu(el->l_count),
3554 "inode %lu, depth %u, count %u, next free %u, "
3555 "rec.cpos %u, rec.clusters %u, "
3556 "insert.cpos %u, insert.clusters %u\n",
3558 le16_to_cpu(el->l_tree_depth),
3559 le16_to_cpu(el->l_count),
3560 le16_to_cpu(el->l_next_free_rec),
3561 le32_to_cpu(el->l_recs[i].e_cpos),
3562 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3563 le32_to_cpu(insert_rec->e_cpos),
3564 le16_to_cpu(insert_rec->e_leaf_clusters));
3566 el->l_recs[i] = *insert_rec;
3567 le16_add_cpu(&el->l_next_free_rec, 1);
3573 * Ok, we have to rotate.
3575 * At this point, it is safe to assume that inserting into an
3576 * empty leaf and appending to a leaf have both been handled
3579 * This leaf needs to have space, either by the empty 1st
3580 * extent record, or by virtue of an l_next_rec < l_count.
3582 ocfs2_rotate_leaf(el, insert_rec);
3585 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3587 struct ocfs2_path *path,
3588 struct ocfs2_extent_rec *insert_rec)
3590 int ret, i, next_free;
3591 struct buffer_head *bh;
3592 struct ocfs2_extent_list *el;
3593 struct ocfs2_extent_rec *rec;
3596 * Update everything except the leaf block.
3598 for (i = 0; i < path->p_tree_depth; i++) {
3599 bh = path->p_node[i].bh;
3600 el = path->p_node[i].el;
3602 next_free = le16_to_cpu(el->l_next_free_rec);
3603 if (next_free == 0) {
3604 ocfs2_error(inode->i_sb,
3605 "Dinode %llu has a bad extent list",
3606 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3611 rec = &el->l_recs[next_free - 1];
3613 rec->e_int_clusters = insert_rec->e_cpos;
3614 le32_add_cpu(&rec->e_int_clusters,
3615 le16_to_cpu(insert_rec->e_leaf_clusters));
3616 le32_add_cpu(&rec->e_int_clusters,
3617 -le32_to_cpu(rec->e_cpos));
3619 ret = ocfs2_journal_dirty(handle, bh);
3626 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3627 struct ocfs2_extent_rec *insert_rec,
3628 struct ocfs2_path *right_path,
3629 struct ocfs2_path **ret_left_path)
3632 struct ocfs2_extent_list *el;
3633 struct ocfs2_path *left_path = NULL;
3635 *ret_left_path = NULL;
3638 * This shouldn't happen for non-trees. The extent rec cluster
3639 * count manipulation below only works for interior nodes.
3641 BUG_ON(right_path->p_tree_depth == 0);
3644 * If our appending insert is at the leftmost edge of a leaf,
3645 * then we might need to update the rightmost records of the
3648 el = path_leaf_el(right_path);
3649 next_free = le16_to_cpu(el->l_next_free_rec);
3650 if (next_free == 0 ||
3651 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3654 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3661 mlog(0, "Append may need a left path update. cpos: %u, "
3662 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3666 * No need to worry if the append is already in the
3670 left_path = ocfs2_new_path(path_root_bh(right_path),
3671 path_root_el(right_path));
3678 ret = ocfs2_find_path(inode, left_path, left_cpos);
3685 * ocfs2_insert_path() will pass the left_path to the
3691 ret = ocfs2_journal_access_path(inode, handle, right_path);
3697 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
3699 *ret_left_path = left_path;
3703 ocfs2_free_path(left_path);
3708 static void ocfs2_split_record(struct inode *inode,
3709 struct ocfs2_path *left_path,
3710 struct ocfs2_path *right_path,
3711 struct ocfs2_extent_rec *split_rec,
3712 enum ocfs2_split_type split)
3715 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3716 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3717 struct ocfs2_extent_rec *rec, *tmprec;
3719 right_el = path_leaf_el(right_path);;
3721 left_el = path_leaf_el(left_path);
3724 insert_el = right_el;
3725 index = ocfs2_search_extent_list(el, cpos);
3727 if (index == 0 && left_path) {
3728 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3731 * This typically means that the record
3732 * started in the left path but moved to the
3733 * right as a result of rotation. We either
3734 * move the existing record to the left, or we
3735 * do the later insert there.
3737 * In this case, the left path should always
3738 * exist as the rotate code will have passed
3739 * it back for a post-insert update.
3742 if (split == SPLIT_LEFT) {
3744 * It's a left split. Since we know
3745 * that the rotate code gave us an
3746 * empty extent in the left path, we
3747 * can just do the insert there.
3749 insert_el = left_el;
3752 * Right split - we have to move the
3753 * existing record over to the left
3754 * leaf. The insert will be into the
3755 * newly created empty extent in the
3758 tmprec = &right_el->l_recs[index];
3759 ocfs2_rotate_leaf(left_el, tmprec);
3762 memset(tmprec, 0, sizeof(*tmprec));
3763 index = ocfs2_search_extent_list(left_el, cpos);
3764 BUG_ON(index == -1);
3769 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3771 * Left path is easy - we can just allow the insert to
3775 insert_el = left_el;
3776 index = ocfs2_search_extent_list(el, cpos);
3777 BUG_ON(index == -1);
3780 rec = &el->l_recs[index];
3781 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3782 ocfs2_rotate_leaf(insert_el, split_rec);
3786 * This function only does inserts on an allocation b-tree. For tree
3787 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3789 * right_path is the path we want to do the actual insert
3790 * in. left_path should only be passed in if we need to update that
3791 * portion of the tree after an edge insert.
3793 static int ocfs2_insert_path(struct inode *inode,
3795 struct ocfs2_path *left_path,
3796 struct ocfs2_path *right_path,
3797 struct ocfs2_extent_rec *insert_rec,
3798 struct ocfs2_insert_type *insert)
3800 int ret, subtree_index;
3801 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
3804 int credits = handle->h_buffer_credits;
3807 * There's a chance that left_path got passed back to
3808 * us without being accounted for in the
3809 * journal. Extend our transaction here to be sure we
3810 * can change those blocks.
3812 credits += left_path->p_tree_depth;
3814 ret = ocfs2_extend_trans(handle, credits);
3820 ret = ocfs2_journal_access_path(inode, handle, left_path);
3828 * Pass both paths to the journal. The majority of inserts
3829 * will be touching all components anyway.
3831 ret = ocfs2_journal_access_path(inode, handle, right_path);
3837 if (insert->ins_split != SPLIT_NONE) {
3839 * We could call ocfs2_insert_at_leaf() for some types
3840 * of splits, but it's easier to just let one separate
3841 * function sort it all out.
3843 ocfs2_split_record(inode, left_path, right_path,
3844 insert_rec, insert->ins_split);
3847 * Split might have modified either leaf and we don't
3848 * have a guarantee that the later edge insert will
3849 * dirty this for us.
3852 ret = ocfs2_journal_dirty(handle,
3853 path_leaf_bh(left_path));
3857 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3860 ret = ocfs2_journal_dirty(handle, leaf_bh);
3866 * The rotate code has indicated that we need to fix
3867 * up portions of the tree after the insert.
3869 * XXX: Should we extend the transaction here?
3871 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3873 ocfs2_complete_edge_insert(inode, handle, left_path,
3874 right_path, subtree_index);
3882 static int ocfs2_do_insert_extent(struct inode *inode,
3884 struct ocfs2_extent_tree *et,
3885 struct ocfs2_extent_rec *insert_rec,
3886 struct ocfs2_insert_type *type)
3888 int ret, rotate = 0;
3890 struct ocfs2_path *right_path = NULL;
3891 struct ocfs2_path *left_path = NULL;
3892 struct ocfs2_extent_list *el;
3894 el = et->et_root_el;
3896 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
3897 OCFS2_JOURNAL_ACCESS_WRITE);
3903 if (le16_to_cpu(el->l_tree_depth) == 0) {
3904 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3905 goto out_update_clusters;
3908 right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
3916 * Determine the path to start with. Rotations need the
3917 * rightmost path, everything else can go directly to the
3920 cpos = le32_to_cpu(insert_rec->e_cpos);
3921 if (type->ins_appending == APPEND_NONE &&
3922 type->ins_contig == CONTIG_NONE) {
3927 ret = ocfs2_find_path(inode, right_path, cpos);
3934 * Rotations and appends need special treatment - they modify
3935 * parts of the tree's above them.
3937 * Both might pass back a path immediate to the left of the
3938 * one being inserted to. This will be cause
3939 * ocfs2_insert_path() to modify the rightmost records of
3940 * left_path to account for an edge insert.
3942 * XXX: When modifying this code, keep in mind that an insert
3943 * can wind up skipping both of these two special cases...
3946 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
3947 le32_to_cpu(insert_rec->e_cpos),
3948 right_path, &left_path);
3955 * ocfs2_rotate_tree_right() might have extended the
3956 * transaction without re-journaling our tree root.
3958 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
3959 OCFS2_JOURNAL_ACCESS_WRITE);
3964 } else if (type->ins_appending == APPEND_TAIL
3965 && type->ins_contig != CONTIG_LEFT) {
3966 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
3967 right_path, &left_path);
3974 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
3981 out_update_clusters:
3982 if (type->ins_split == SPLIT_NONE)
3983 ocfs2_et_update_clusters(inode, et,
3984 le16_to_cpu(insert_rec->e_leaf_clusters));
3986 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
3991 ocfs2_free_path(left_path);
3992 ocfs2_free_path(right_path);
3997 static enum ocfs2_contig_type
3998 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
3999 struct ocfs2_extent_list *el, int index,
4000 struct ocfs2_extent_rec *split_rec)
4003 enum ocfs2_contig_type ret = CONTIG_NONE;
4004 u32 left_cpos, right_cpos;
4005 struct ocfs2_extent_rec *rec = NULL;
4006 struct ocfs2_extent_list *new_el;
4007 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4008 struct buffer_head *bh;
4009 struct ocfs2_extent_block *eb;
4012 rec = &el->l_recs[index - 1];
4013 } else if (path->p_tree_depth > 0) {
4014 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4019 if (left_cpos != 0) {
4020 left_path = ocfs2_new_path(path_root_bh(path),
4021 path_root_el(path));
4025 status = ocfs2_find_path(inode, left_path, left_cpos);
4029 new_el = path_leaf_el(left_path);
4031 if (le16_to_cpu(new_el->l_next_free_rec) !=
4032 le16_to_cpu(new_el->l_count)) {
4033 bh = path_leaf_bh(left_path);
4034 eb = (struct ocfs2_extent_block *)bh->b_data;
4035 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4039 rec = &new_el->l_recs[
4040 le16_to_cpu(new_el->l_next_free_rec) - 1];
4045 * We're careful to check for an empty extent record here -
4046 * the merge code will know what to do if it sees one.
4049 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4050 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4053 ret = ocfs2_extent_contig(inode, rec, split_rec);
4058 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4059 rec = &el->l_recs[index + 1];
4060 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4061 path->p_tree_depth > 0) {
4062 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4067 if (right_cpos == 0)
4070 right_path = ocfs2_new_path(path_root_bh(path),
4071 path_root_el(path));
4075 status = ocfs2_find_path(inode, right_path, right_cpos);
4079 new_el = path_leaf_el(right_path);
4080 rec = &new_el->l_recs[0];
4081 if (ocfs2_is_empty_extent(rec)) {
4082 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4083 bh = path_leaf_bh(right_path);
4084 eb = (struct ocfs2_extent_block *)bh->b_data;
4085 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4089 rec = &new_el->l_recs[1];
4094 enum ocfs2_contig_type contig_type;
4096 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4098 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4099 ret = CONTIG_LEFTRIGHT;
4100 else if (ret == CONTIG_NONE)
4106 ocfs2_free_path(left_path);
4108 ocfs2_free_path(right_path);
4113 static void ocfs2_figure_contig_type(struct inode *inode,
4114 struct ocfs2_insert_type *insert,
4115 struct ocfs2_extent_list *el,
4116 struct ocfs2_extent_rec *insert_rec,
4117 struct ocfs2_extent_tree *et)
4120 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4122 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4124 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4125 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4127 if (contig_type != CONTIG_NONE) {
4128 insert->ins_contig_index = i;
4132 insert->ins_contig = contig_type;
4134 if (insert->ins_contig != CONTIG_NONE) {
4135 struct ocfs2_extent_rec *rec =
4136 &el->l_recs[insert->ins_contig_index];
4137 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4138 le16_to_cpu(insert_rec->e_leaf_clusters);
4141 * Caller might want us to limit the size of extents, don't
4142 * calculate contiguousness if we might exceed that limit.
4144 if (et->et_max_leaf_clusters &&
4145 (len > et->et_max_leaf_clusters))
4146 insert->ins_contig = CONTIG_NONE;
4151 * This should only be called against the righmost leaf extent list.
4153 * ocfs2_figure_appending_type() will figure out whether we'll have to
4154 * insert at the tail of the rightmost leaf.
4156 * This should also work against the root extent list for tree's with 0
4157 * depth. If we consider the root extent list to be the rightmost leaf node
4158 * then the logic here makes sense.
4160 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4161 struct ocfs2_extent_list *el,
4162 struct ocfs2_extent_rec *insert_rec)
4165 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4166 struct ocfs2_extent_rec *rec;
4168 insert->ins_appending = APPEND_NONE;
4170 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4172 if (!el->l_next_free_rec)
4173 goto set_tail_append;
4175 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4176 /* Were all records empty? */
4177 if (le16_to_cpu(el->l_next_free_rec) == 1)
4178 goto set_tail_append;
4181 i = le16_to_cpu(el->l_next_free_rec) - 1;
4182 rec = &el->l_recs[i];
4185 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4186 goto set_tail_append;
4191 insert->ins_appending = APPEND_TAIL;
4195 * Helper function called at the begining of an insert.
4197 * This computes a few things that are commonly used in the process of
4198 * inserting into the btree:
4199 * - Whether the new extent is contiguous with an existing one.
4200 * - The current tree depth.
4201 * - Whether the insert is an appending one.
4202 * - The total # of free records in the tree.
4204 * All of the information is stored on the ocfs2_insert_type
4207 static int ocfs2_figure_insert_type(struct inode *inode,
4208 struct ocfs2_extent_tree *et,
4209 struct buffer_head **last_eb_bh,
4210 struct ocfs2_extent_rec *insert_rec,
4212 struct ocfs2_insert_type *insert)
4215 struct ocfs2_extent_block *eb;
4216 struct ocfs2_extent_list *el;
4217 struct ocfs2_path *path = NULL;
4218 struct buffer_head *bh = NULL;
4220 insert->ins_split = SPLIT_NONE;
4222 el = et->et_root_el;
4223 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4225 if (el->l_tree_depth) {
4227 * If we have tree depth, we read in the
4228 * rightmost extent block ahead of time as
4229 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4230 * may want it later.
4232 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4233 ocfs2_et_get_last_eb_blk(et), &bh,
4234 OCFS2_BH_CACHED, inode);
4239 eb = (struct ocfs2_extent_block *) bh->b_data;
4244 * Unless we have a contiguous insert, we'll need to know if
4245 * there is room left in our allocation tree for another
4248 * XXX: This test is simplistic, we can search for empty
4249 * extent records too.
4251 *free_records = le16_to_cpu(el->l_count) -
4252 le16_to_cpu(el->l_next_free_rec);
4254 if (!insert->ins_tree_depth) {
4255 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4256 ocfs2_figure_appending_type(insert, el, insert_rec);
4260 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4268 * In the case that we're inserting past what the tree
4269 * currently accounts for, ocfs2_find_path() will return for
4270 * us the rightmost tree path. This is accounted for below in
4271 * the appending code.
4273 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4279 el = path_leaf_el(path);
4282 * Now that we have the path, there's two things we want to determine:
4283 * 1) Contiguousness (also set contig_index if this is so)
4285 * 2) Are we doing an append? We can trivially break this up
4286 * into two types of appends: simple record append, or a
4287 * rotate inside the tail leaf.
4289 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4292 * The insert code isn't quite ready to deal with all cases of
4293 * left contiguousness. Specifically, if it's an insert into
4294 * the 1st record in a leaf, it will require the adjustment of
4295 * cluster count on the last record of the path directly to it's
4296 * left. For now, just catch that case and fool the layers
4297 * above us. This works just fine for tree_depth == 0, which
4298 * is why we allow that above.
4300 if (insert->ins_contig == CONTIG_LEFT &&
4301 insert->ins_contig_index == 0)
4302 insert->ins_contig = CONTIG_NONE;
4305 * Ok, so we can simply compare against last_eb to figure out
4306 * whether the path doesn't exist. This will only happen in
4307 * the case that we're doing a tail append, so maybe we can
4308 * take advantage of that information somehow.
4310 if (ocfs2_et_get_last_eb_blk(et) ==
4311 path_leaf_bh(path)->b_blocknr) {
4313 * Ok, ocfs2_find_path() returned us the rightmost
4314 * tree path. This might be an appending insert. There are
4316 * 1) We're doing a true append at the tail:
4317 * -This might even be off the end of the leaf
4318 * 2) We're "appending" by rotating in the tail
4320 ocfs2_figure_appending_type(insert, el, insert_rec);
4324 ocfs2_free_path(path);
4334 * Insert an extent into an inode btree.
4336 * The caller needs to update fe->i_clusters
4338 static int ocfs2_insert_extent(struct ocfs2_super *osb,
4340 struct inode *inode,
4341 struct buffer_head *root_bh,
4346 struct ocfs2_alloc_context *meta_ac,
4347 struct ocfs2_extent_tree *et)
4350 int uninitialized_var(free_records);
4351 struct buffer_head *last_eb_bh = NULL;
4352 struct ocfs2_insert_type insert = {0, };
4353 struct ocfs2_extent_rec rec;
4355 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
4357 mlog(0, "add %u clusters at position %u to inode %llu\n",
4358 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4360 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
4361 (OCFS2_I(inode)->ip_clusters != cpos),
4362 "Device %s, asking for sparse allocation: inode %llu, "
4363 "cpos %u, clusters %u\n",
4365 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
4366 OCFS2_I(inode)->ip_clusters);
4368 memset(&rec, 0, sizeof(rec));
4369 rec.e_cpos = cpu_to_le32(cpos);
4370 rec.e_blkno = cpu_to_le64(start_blk);
4371 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4372 rec.e_flags = flags;
4374 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4375 &free_records, &insert);
4381 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4382 "Insert.contig_index: %d, Insert.free_records: %d, "
4383 "Insert.tree_depth: %d\n",
4384 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4385 free_records, insert.ins_tree_depth);
4387 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4388 status = ocfs2_grow_tree(inode, handle, et,
4389 &insert.ins_tree_depth, &last_eb_bh,
4397 /* Finally, we can add clusters. This might rotate the tree for us. */
4398 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4401 else if (et->et_type == OCFS2_DINODE_EXTENT)
4402 ocfs2_extent_map_insert_rec(inode, &rec);
4412 int ocfs2_dinode_insert_extent(struct ocfs2_super *osb,
4414 struct inode *inode,
4415 struct buffer_head *root_bh,
4420 struct ocfs2_alloc_context *meta_ac)
4423 struct ocfs2_extent_tree et;
4425 ocfs2_get_extent_tree(&et, inode, root_bh, OCFS2_DINODE_EXTENT,
4427 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4428 cpos, start_blk, new_clusters,
4429 flags, meta_ac, &et);
4430 ocfs2_put_extent_tree(&et);
4435 int ocfs2_xattr_value_insert_extent(struct ocfs2_super *osb,
4437 struct inode *inode,
4438 struct buffer_head *root_bh,
4443 struct ocfs2_alloc_context *meta_ac,
4447 struct ocfs2_extent_tree et;
4449 ocfs2_get_extent_tree(&et, inode, root_bh,
4450 OCFS2_XATTR_VALUE_EXTENT, private);
4451 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4452 cpos, start_blk, new_clusters,
4453 flags, meta_ac, &et);
4454 ocfs2_put_extent_tree(&et);
4459 int ocfs2_xattr_tree_insert_extent(struct ocfs2_super *osb,
4461 struct inode *inode,
4462 struct buffer_head *root_bh,
4467 struct ocfs2_alloc_context *meta_ac)
4470 struct ocfs2_extent_tree et;
4472 ocfs2_get_extent_tree(&et, inode, root_bh, OCFS2_XATTR_TREE_EXTENT,
4474 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4475 cpos, start_blk, new_clusters,
4476 flags, meta_ac, &et);
4477 ocfs2_put_extent_tree(&et);
4483 * Allcate and add clusters into the extent b-tree.
4484 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4485 * The extent b-tree's root is root_el and it should be in root_bh, and
4486 * it is not limited to the file storage. Any extent tree can use this
4487 * function if it implements the proper ocfs2_extent_tree.
4489 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4490 struct inode *inode,
4491 u32 *logical_offset,
4492 u32 clusters_to_add,
4494 struct buffer_head *root_bh,
4495 struct ocfs2_extent_list *root_el,
4497 struct ocfs2_alloc_context *data_ac,
4498 struct ocfs2_alloc_context *meta_ac,
4499 enum ocfs2_alloc_restarted *reason_ret,
4500 enum ocfs2_extent_tree_type type,
4505 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4506 u32 bit_off, num_bits;
4510 BUG_ON(!clusters_to_add);
4513 flags = OCFS2_EXT_UNWRITTEN;
4515 free_extents = ocfs2_num_free_extents(osb, inode, root_bh, type,
4517 if (free_extents < 0) {
4518 status = free_extents;
4523 /* there are two cases which could cause us to EAGAIN in the
4524 * we-need-more-metadata case:
4525 * 1) we haven't reserved *any*
4526 * 2) we are so fragmented, we've needed to add metadata too
4528 if (!free_extents && !meta_ac) {
4529 mlog(0, "we haven't reserved any metadata!\n");
4531 reason = RESTART_META;
4533 } else if ((!free_extents)
4534 && (ocfs2_alloc_context_bits_left(meta_ac)
4535 < ocfs2_extend_meta_needed(root_el))) {
4536 mlog(0, "filesystem is really fragmented...\n");
4538 reason = RESTART_META;
4542 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4543 clusters_to_add, &bit_off, &num_bits);
4545 if (status != -ENOSPC)
4550 BUG_ON(num_bits > clusters_to_add);
4552 /* reserve our write early -- insert_extent may update the inode */
4553 status = ocfs2_journal_access(handle, inode, root_bh,
4554 OCFS2_JOURNAL_ACCESS_WRITE);
4560 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4561 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4562 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4563 if (type == OCFS2_DINODE_EXTENT)
4564 status = ocfs2_dinode_insert_extent(osb, handle, inode, root_bh,
4565 *logical_offset, block,
4566 num_bits, flags, meta_ac);
4567 else if (type == OCFS2_XATTR_TREE_EXTENT)
4568 status = ocfs2_xattr_tree_insert_extent(osb, handle,
4571 block, num_bits, flags,
4574 status = ocfs2_xattr_value_insert_extent(osb, handle,
4577 block, num_bits, flags,
4584 status = ocfs2_journal_dirty(handle, root_bh);
4590 clusters_to_add -= num_bits;
4591 *logical_offset += num_bits;
4593 if (clusters_to_add) {
4594 mlog(0, "need to alloc once more, wanted = %u\n",
4597 reason = RESTART_TRANS;
4603 *reason_ret = reason;
4607 static void ocfs2_make_right_split_rec(struct super_block *sb,
4608 struct ocfs2_extent_rec *split_rec,
4610 struct ocfs2_extent_rec *rec)
4612 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4613 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4615 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4617 split_rec->e_cpos = cpu_to_le32(cpos);
4618 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4620 split_rec->e_blkno = rec->e_blkno;
4621 le64_add_cpu(&split_rec->e_blkno,
4622 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4624 split_rec->e_flags = rec->e_flags;
4627 static int ocfs2_split_and_insert(struct inode *inode,
4629 struct ocfs2_path *path,
4630 struct ocfs2_extent_tree *et,
4631 struct buffer_head **last_eb_bh,
4633 struct ocfs2_extent_rec *orig_split_rec,
4634 struct ocfs2_alloc_context *meta_ac)
4637 unsigned int insert_range, rec_range, do_leftright = 0;
4638 struct ocfs2_extent_rec tmprec;
4639 struct ocfs2_extent_list *rightmost_el;
4640 struct ocfs2_extent_rec rec;
4641 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4642 struct ocfs2_insert_type insert;
4643 struct ocfs2_extent_block *eb;
4647 * Store a copy of the record on the stack - it might move
4648 * around as the tree is manipulated below.
4650 rec = path_leaf_el(path)->l_recs[split_index];
4652 rightmost_el = et->et_root_el;
4654 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4656 BUG_ON(!(*last_eb_bh));
4657 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4658 rightmost_el = &eb->h_list;
4661 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4662 le16_to_cpu(rightmost_el->l_count)) {
4663 ret = ocfs2_grow_tree(inode, handle, et,
4664 &depth, last_eb_bh, meta_ac);
4671 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4672 insert.ins_appending = APPEND_NONE;
4673 insert.ins_contig = CONTIG_NONE;
4674 insert.ins_tree_depth = depth;
4676 insert_range = le32_to_cpu(split_rec.e_cpos) +
4677 le16_to_cpu(split_rec.e_leaf_clusters);
4678 rec_range = le32_to_cpu(rec.e_cpos) +
4679 le16_to_cpu(rec.e_leaf_clusters);
4681 if (split_rec.e_cpos == rec.e_cpos) {
4682 insert.ins_split = SPLIT_LEFT;
4683 } else if (insert_range == rec_range) {
4684 insert.ins_split = SPLIT_RIGHT;
4687 * Left/right split. We fake this as a right split
4688 * first and then make a second pass as a left split.
4690 insert.ins_split = SPLIT_RIGHT;
4692 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4697 BUG_ON(do_leftright);
4701 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4707 if (do_leftright == 1) {
4709 struct ocfs2_extent_list *el;
4712 split_rec = *orig_split_rec;
4714 ocfs2_reinit_path(path, 1);
4716 cpos = le32_to_cpu(split_rec.e_cpos);
4717 ret = ocfs2_find_path(inode, path, cpos);
4723 el = path_leaf_el(path);
4724 split_index = ocfs2_search_extent_list(el, cpos);
4733 * Mark part or all of the extent record at split_index in the leaf
4734 * pointed to by path as written. This removes the unwritten
4737 * Care is taken to handle contiguousness so as to not grow the tree.
4739 * meta_ac is not strictly necessary - we only truly need it if growth
4740 * of the tree is required. All other cases will degrade into a less
4741 * optimal tree layout.
4743 * last_eb_bh should be the rightmost leaf block for any extent
4744 * btree. Since a split may grow the tree or a merge might shrink it,
4745 * the caller cannot trust the contents of that buffer after this call.
4747 * This code is optimized for readability - several passes might be
4748 * made over certain portions of the tree. All of those blocks will
4749 * have been brought into cache (and pinned via the journal), so the
4750 * extra overhead is not expressed in terms of disk reads.
4752 static int __ocfs2_mark_extent_written(struct inode *inode,
4753 struct ocfs2_extent_tree *et,
4755 struct ocfs2_path *path,
4757 struct ocfs2_extent_rec *split_rec,
4758 struct ocfs2_alloc_context *meta_ac,
4759 struct ocfs2_cached_dealloc_ctxt *dealloc)
4762 struct ocfs2_extent_list *el = path_leaf_el(path);
4763 struct buffer_head *last_eb_bh = NULL;
4764 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4765 struct ocfs2_merge_ctxt ctxt;
4766 struct ocfs2_extent_list *rightmost_el;
4768 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
4774 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4775 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4776 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4782 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
4787 * The core merge / split code wants to know how much room is
4788 * left in this inodes allocation tree, so we pass the
4789 * rightmost extent list.
4791 if (path->p_tree_depth) {
4792 struct ocfs2_extent_block *eb;
4794 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4795 ocfs2_et_get_last_eb_blk(et),
4796 &last_eb_bh, OCFS2_BH_CACHED, inode);
4802 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4803 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4804 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4809 rightmost_el = &eb->h_list;
4811 rightmost_el = path_root_el(path);
4813 if (rec->e_cpos == split_rec->e_cpos &&
4814 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4815 ctxt.c_split_covers_rec = 1;
4817 ctxt.c_split_covers_rec = 0;
4819 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4821 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4822 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4823 ctxt.c_split_covers_rec);
4825 if (ctxt.c_contig_type == CONTIG_NONE) {
4826 if (ctxt.c_split_covers_rec)
4827 el->l_recs[split_index] = *split_rec;
4829 ret = ocfs2_split_and_insert(inode, handle, path, et,
4830 &last_eb_bh, split_index,
4831 split_rec, meta_ac);
4835 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4836 split_index, split_rec,
4837 dealloc, &ctxt, et);
4848 * Mark the already-existing extent at cpos as written for len clusters.
4850 * If the existing extent is larger than the request, initiate a
4851 * split. An attempt will be made at merging with adjacent extents.
4853 * The caller is responsible for passing down meta_ac if we'll need it.
4855 int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh,
4856 handle_t *handle, u32 cpos, u32 len, u32 phys,
4857 struct ocfs2_alloc_context *meta_ac,
4858 struct ocfs2_cached_dealloc_ctxt *dealloc,
4859 enum ocfs2_extent_tree_type et_type,
4863 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4864 struct ocfs2_extent_rec split_rec;
4865 struct ocfs2_path *left_path = NULL;
4866 struct ocfs2_extent_list *el;
4867 struct ocfs2_extent_tree et;
4869 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4870 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4872 ocfs2_get_extent_tree(&et, inode, root_bh, et_type, private);
4874 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4875 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4876 "that are being written to, but the feature bit "
4877 "is not set in the super block.",
4878 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4884 * XXX: This should be fixed up so that we just re-insert the
4885 * next extent records.
4887 if (et_type == OCFS2_DINODE_EXTENT)
4888 ocfs2_extent_map_trunc(inode, 0);
4890 left_path = ocfs2_new_path(et.et_root_bh, et.et_root_el);
4897 ret = ocfs2_find_path(inode, left_path, cpos);
4902 el = path_leaf_el(left_path);
4904 index = ocfs2_search_extent_list(el, cpos);
4905 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4906 ocfs2_error(inode->i_sb,
4907 "Inode %llu has an extent at cpos %u which can no "
4908 "longer be found.\n",
4909 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4914 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4915 split_rec.e_cpos = cpu_to_le32(cpos);
4916 split_rec.e_leaf_clusters = cpu_to_le16(len);
4917 split_rec.e_blkno = cpu_to_le64(start_blkno);
4918 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4919 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4921 ret = __ocfs2_mark_extent_written(inode, &et, handle, left_path,
4922 index, &split_rec, meta_ac,
4928 ocfs2_free_path(left_path);
4929 ocfs2_put_extent_tree(&et);
4933 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
4934 handle_t *handle, struct ocfs2_path *path,
4935 int index, u32 new_range,
4936 struct ocfs2_alloc_context *meta_ac)
4938 int ret, depth, credits = handle->h_buffer_credits;
4939 struct buffer_head *last_eb_bh = NULL;
4940 struct ocfs2_extent_block *eb;
4941 struct ocfs2_extent_list *rightmost_el, *el;
4942 struct ocfs2_extent_rec split_rec;
4943 struct ocfs2_extent_rec *rec;
4944 struct ocfs2_insert_type insert;
4947 * Setup the record to split before we grow the tree.
4949 el = path_leaf_el(path);
4950 rec = &el->l_recs[index];
4951 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4953 depth = path->p_tree_depth;
4955 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4956 ocfs2_et_get_last_eb_blk(et),
4957 &last_eb_bh, OCFS2_BH_CACHED, inode);
4963 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4964 rightmost_el = &eb->h_list;
4966 rightmost_el = path_leaf_el(path);
4968 credits += path->p_tree_depth +
4969 ocfs2_extend_meta_needed(et->et_root_el);
4970 ret = ocfs2_extend_trans(handle, credits);
4976 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4977 le16_to_cpu(rightmost_el->l_count)) {
4978 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
4986 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4987 insert.ins_appending = APPEND_NONE;
4988 insert.ins_contig = CONTIG_NONE;
4989 insert.ins_split = SPLIT_RIGHT;
4990 insert.ins_tree_depth = depth;
4992 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
5001 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5002 struct ocfs2_path *path, int index,
5003 struct ocfs2_cached_dealloc_ctxt *dealloc,
5005 struct ocfs2_extent_tree *et)
5008 u32 left_cpos, rec_range, trunc_range;
5009 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5010 struct super_block *sb = inode->i_sb;
5011 struct ocfs2_path *left_path = NULL;
5012 struct ocfs2_extent_list *el = path_leaf_el(path);
5013 struct ocfs2_extent_rec *rec;
5014 struct ocfs2_extent_block *eb;
5016 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5017 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5026 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5027 path->p_tree_depth) {
5029 * Check whether this is the rightmost tree record. If
5030 * we remove all of this record or part of its right
5031 * edge then an update of the record lengths above it
5034 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5035 if (eb->h_next_leaf_blk == 0)
5036 is_rightmost_tree_rec = 1;
5039 rec = &el->l_recs[index];
5040 if (index == 0 && path->p_tree_depth &&
5041 le32_to_cpu(rec->e_cpos) == cpos) {
5043 * Changing the leftmost offset (via partial or whole
5044 * record truncate) of an interior (or rightmost) path
5045 * means we have to update the subtree that is formed
5046 * by this leaf and the one to it's left.
5048 * There are two cases we can skip:
5049 * 1) Path is the leftmost one in our inode tree.
5050 * 2) The leaf is rightmost and will be empty after
5051 * we remove the extent record - the rotate code
5052 * knows how to update the newly formed edge.
5055 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5062 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5063 left_path = ocfs2_new_path(path_root_bh(path),
5064 path_root_el(path));
5071 ret = ocfs2_find_path(inode, left_path, left_cpos);
5079 ret = ocfs2_extend_rotate_transaction(handle, 0,
5080 handle->h_buffer_credits,
5087 ret = ocfs2_journal_access_path(inode, handle, path);
5093 ret = ocfs2_journal_access_path(inode, handle, left_path);
5099 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5100 trunc_range = cpos + len;
5102 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5105 memset(rec, 0, sizeof(*rec));
5106 ocfs2_cleanup_merge(el, index);
5109 next_free = le16_to_cpu(el->l_next_free_rec);
5110 if (is_rightmost_tree_rec && next_free > 1) {
5112 * We skip the edge update if this path will
5113 * be deleted by the rotate code.
5115 rec = &el->l_recs[next_free - 1];
5116 ocfs2_adjust_rightmost_records(inode, handle, path,
5119 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5120 /* Remove leftmost portion of the record. */
5121 le32_add_cpu(&rec->e_cpos, len);
5122 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5123 le16_add_cpu(&rec->e_leaf_clusters, -len);
5124 } else if (rec_range == trunc_range) {
5125 /* Remove rightmost portion of the record */
5126 le16_add_cpu(&rec->e_leaf_clusters, -len);
5127 if (is_rightmost_tree_rec)
5128 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5130 /* Caller should have trapped this. */
5131 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5132 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5133 le32_to_cpu(rec->e_cpos),
5134 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5141 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5142 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5146 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5148 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5155 ocfs2_free_path(left_path);
5159 int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
5160 u32 cpos, u32 len, handle_t *handle,
5161 struct ocfs2_alloc_context *meta_ac,
5162 struct ocfs2_cached_dealloc_ctxt *dealloc,
5163 enum ocfs2_extent_tree_type et_type,
5167 u32 rec_range, trunc_range;
5168 struct ocfs2_extent_rec *rec;
5169 struct ocfs2_extent_list *el;
5170 struct ocfs2_path *path = NULL;
5171 struct ocfs2_extent_tree et;
5173 ocfs2_get_extent_tree(&et, inode, root_bh, et_type, private);
5175 ocfs2_extent_map_trunc(inode, 0);
5177 path = ocfs2_new_path(et.et_root_bh, et.et_root_el);
5184 ret = ocfs2_find_path(inode, path, cpos);
5190 el = path_leaf_el(path);
5191 index = ocfs2_search_extent_list(el, cpos);
5192 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5193 ocfs2_error(inode->i_sb,
5194 "Inode %llu has an extent at cpos %u which can no "
5195 "longer be found.\n",
5196 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5202 * We have 3 cases of extent removal:
5203 * 1) Range covers the entire extent rec
5204 * 2) Range begins or ends on one edge of the extent rec
5205 * 3) Range is in the middle of the extent rec (no shared edges)
5207 * For case 1 we remove the extent rec and left rotate to
5210 * For case 2 we just shrink the existing extent rec, with a
5211 * tree update if the shrinking edge is also the edge of an
5214 * For case 3 we do a right split to turn the extent rec into
5215 * something case 2 can handle.
5217 rec = &el->l_recs[index];
5218 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5219 trunc_range = cpos + len;
5221 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5223 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5224 "(cpos %u, len %u)\n",
5225 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5226 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5228 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5229 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5236 ret = ocfs2_split_tree(inode, &et, handle, path, index,
5237 trunc_range, meta_ac);
5244 * The split could have manipulated the tree enough to
5245 * move the record location, so we have to look for it again.
5247 ocfs2_reinit_path(path, 1);
5249 ret = ocfs2_find_path(inode, path, cpos);
5255 el = path_leaf_el(path);
5256 index = ocfs2_search_extent_list(el, cpos);
5257 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5258 ocfs2_error(inode->i_sb,
5259 "Inode %llu: split at cpos %u lost record.",
5260 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5267 * Double check our values here. If anything is fishy,
5268 * it's easier to catch it at the top level.
5270 rec = &el->l_recs[index];
5271 rec_range = le32_to_cpu(rec->e_cpos) +
5272 ocfs2_rec_clusters(el, rec);
5273 if (rec_range != trunc_range) {
5274 ocfs2_error(inode->i_sb,
5275 "Inode %llu: error after split at cpos %u"
5276 "trunc len %u, existing record is (%u,%u)",
5277 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5278 cpos, len, le32_to_cpu(rec->e_cpos),
5279 ocfs2_rec_clusters(el, rec));
5284 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5293 ocfs2_free_path(path);
5294 ocfs2_put_extent_tree(&et);
5298 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5300 struct buffer_head *tl_bh = osb->osb_tl_bh;
5301 struct ocfs2_dinode *di;
5302 struct ocfs2_truncate_log *tl;
5304 di = (struct ocfs2_dinode *) tl_bh->b_data;
5305 tl = &di->id2.i_dealloc;
5307 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5308 "slot %d, invalid truncate log parameters: used = "
5309 "%u, count = %u\n", osb->slot_num,
5310 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5311 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5314 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5315 unsigned int new_start)
5317 unsigned int tail_index;
5318 unsigned int current_tail;
5320 /* No records, nothing to coalesce */
5321 if (!le16_to_cpu(tl->tl_used))
5324 tail_index = le16_to_cpu(tl->tl_used) - 1;
5325 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5326 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5328 return current_tail == new_start;
5331 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5334 unsigned int num_clusters)
5337 unsigned int start_cluster, tl_count;
5338 struct inode *tl_inode = osb->osb_tl_inode;
5339 struct buffer_head *tl_bh = osb->osb_tl_bh;
5340 struct ocfs2_dinode *di;
5341 struct ocfs2_truncate_log *tl;
5343 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5344 (unsigned long long)start_blk, num_clusters);
5346 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5348 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5350 di = (struct ocfs2_dinode *) tl_bh->b_data;
5351 tl = &di->id2.i_dealloc;
5352 if (!OCFS2_IS_VALID_DINODE(di)) {
5353 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5358 tl_count = le16_to_cpu(tl->tl_count);
5359 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5361 "Truncate record count on #%llu invalid "
5362 "wanted %u, actual %u\n",
5363 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5364 ocfs2_truncate_recs_per_inode(osb->sb),
5365 le16_to_cpu(tl->tl_count));
5367 /* Caller should have known to flush before calling us. */
5368 index = le16_to_cpu(tl->tl_used);
5369 if (index >= tl_count) {
5375 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5376 OCFS2_JOURNAL_ACCESS_WRITE);
5382 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5383 "%llu (index = %d)\n", num_clusters, start_cluster,
5384 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5386 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5388 * Move index back to the record we are coalescing with.
5389 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5393 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5394 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5395 index, le32_to_cpu(tl->tl_recs[index].t_start),
5398 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5399 tl->tl_used = cpu_to_le16(index + 1);
5401 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5403 status = ocfs2_journal_dirty(handle, tl_bh);
5414 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5416 struct inode *data_alloc_inode,
5417 struct buffer_head *data_alloc_bh)
5421 unsigned int num_clusters;
5423 struct ocfs2_truncate_rec rec;
5424 struct ocfs2_dinode *di;
5425 struct ocfs2_truncate_log *tl;
5426 struct inode *tl_inode = osb->osb_tl_inode;
5427 struct buffer_head *tl_bh = osb->osb_tl_bh;
5431 di = (struct ocfs2_dinode *) tl_bh->b_data;
5432 tl = &di->id2.i_dealloc;
5433 i = le16_to_cpu(tl->tl_used) - 1;
5435 /* Caller has given us at least enough credits to
5436 * update the truncate log dinode */
5437 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5438 OCFS2_JOURNAL_ACCESS_WRITE);
5444 tl->tl_used = cpu_to_le16(i);
5446 status = ocfs2_journal_dirty(handle, tl_bh);
5452 /* TODO: Perhaps we can calculate the bulk of the
5453 * credits up front rather than extending like
5455 status = ocfs2_extend_trans(handle,
5456 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5462 rec = tl->tl_recs[i];
5463 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5464 le32_to_cpu(rec.t_start));
5465 num_clusters = le32_to_cpu(rec.t_clusters);
5467 /* if start_blk is not set, we ignore the record as
5470 mlog(0, "free record %d, start = %u, clusters = %u\n",
5471 i, le32_to_cpu(rec.t_start), num_clusters);
5473 status = ocfs2_free_clusters(handle, data_alloc_inode,
5474 data_alloc_bh, start_blk,
5489 /* Expects you to already be holding tl_inode->i_mutex */
5490 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5493 unsigned int num_to_flush;
5495 struct inode *tl_inode = osb->osb_tl_inode;
5496 struct inode *data_alloc_inode = NULL;
5497 struct buffer_head *tl_bh = osb->osb_tl_bh;
5498 struct buffer_head *data_alloc_bh = NULL;
5499 struct ocfs2_dinode *di;
5500 struct ocfs2_truncate_log *tl;
5504 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5506 di = (struct ocfs2_dinode *) tl_bh->b_data;
5507 tl = &di->id2.i_dealloc;
5508 if (!OCFS2_IS_VALID_DINODE(di)) {
5509 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5514 num_to_flush = le16_to_cpu(tl->tl_used);
5515 mlog(0, "Flush %u records from truncate log #%llu\n",
5516 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5517 if (!num_to_flush) {
5522 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5523 GLOBAL_BITMAP_SYSTEM_INODE,
5524 OCFS2_INVALID_SLOT);
5525 if (!data_alloc_inode) {
5527 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5531 mutex_lock(&data_alloc_inode->i_mutex);
5533 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5539 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5540 if (IS_ERR(handle)) {
5541 status = PTR_ERR(handle);
5546 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5551 ocfs2_commit_trans(osb, handle);
5554 brelse(data_alloc_bh);
5555 ocfs2_inode_unlock(data_alloc_inode, 1);
5558 mutex_unlock(&data_alloc_inode->i_mutex);
5559 iput(data_alloc_inode);
5566 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5569 struct inode *tl_inode = osb->osb_tl_inode;
5571 mutex_lock(&tl_inode->i_mutex);
5572 status = __ocfs2_flush_truncate_log(osb);
5573 mutex_unlock(&tl_inode->i_mutex);
5578 static void ocfs2_truncate_log_worker(struct work_struct *work)
5581 struct ocfs2_super *osb =
5582 container_of(work, struct ocfs2_super,
5583 osb_truncate_log_wq.work);
5587 status = ocfs2_flush_truncate_log(osb);
5591 ocfs2_init_inode_steal_slot(osb);
5596 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5597 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5600 if (osb->osb_tl_inode) {
5601 /* We want to push off log flushes while truncates are
5604 cancel_delayed_work(&osb->osb_truncate_log_wq);
5606 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5607 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5611 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5613 struct inode **tl_inode,
5614 struct buffer_head **tl_bh)
5617 struct inode *inode = NULL;
5618 struct buffer_head *bh = NULL;
5620 inode = ocfs2_get_system_file_inode(osb,
5621 TRUNCATE_LOG_SYSTEM_INODE,
5625 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5629 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
5630 OCFS2_BH_CACHED, inode);
5644 /* called during the 1st stage of node recovery. we stamp a clean
5645 * truncate log and pass back a copy for processing later. if the
5646 * truncate log does not require processing, a *tl_copy is set to
5648 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5650 struct ocfs2_dinode **tl_copy)
5653 struct inode *tl_inode = NULL;
5654 struct buffer_head *tl_bh = NULL;
5655 struct ocfs2_dinode *di;
5656 struct ocfs2_truncate_log *tl;
5660 mlog(0, "recover truncate log from slot %d\n", slot_num);
5662 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5668 di = (struct ocfs2_dinode *) tl_bh->b_data;
5669 tl = &di->id2.i_dealloc;
5670 if (!OCFS2_IS_VALID_DINODE(di)) {
5671 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
5676 if (le16_to_cpu(tl->tl_used)) {
5677 mlog(0, "We'll have %u logs to recover\n",
5678 le16_to_cpu(tl->tl_used));
5680 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5687 /* Assuming the write-out below goes well, this copy
5688 * will be passed back to recovery for processing. */
5689 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5691 /* All we need to do to clear the truncate log is set
5695 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5708 if (status < 0 && (*tl_copy)) {
5717 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5718 struct ocfs2_dinode *tl_copy)
5722 unsigned int clusters, num_recs, start_cluster;
5725 struct inode *tl_inode = osb->osb_tl_inode;
5726 struct ocfs2_truncate_log *tl;
5730 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5731 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5735 tl = &tl_copy->id2.i_dealloc;
5736 num_recs = le16_to_cpu(tl->tl_used);
5737 mlog(0, "cleanup %u records from %llu\n", num_recs,
5738 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
5740 mutex_lock(&tl_inode->i_mutex);
5741 for(i = 0; i < num_recs; i++) {
5742 if (ocfs2_truncate_log_needs_flush(osb)) {
5743 status = __ocfs2_flush_truncate_log(osb);
5750 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5751 if (IS_ERR(handle)) {
5752 status = PTR_ERR(handle);
5757 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5758 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5759 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5761 status = ocfs2_truncate_log_append(osb, handle,
5762 start_blk, clusters);
5763 ocfs2_commit_trans(osb, handle);
5771 mutex_unlock(&tl_inode->i_mutex);
5777 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5780 struct inode *tl_inode = osb->osb_tl_inode;
5785 cancel_delayed_work(&osb->osb_truncate_log_wq);
5786 flush_workqueue(ocfs2_wq);
5788 status = ocfs2_flush_truncate_log(osb);
5792 brelse(osb->osb_tl_bh);
5793 iput(osb->osb_tl_inode);
5799 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5802 struct inode *tl_inode = NULL;
5803 struct buffer_head *tl_bh = NULL;
5807 status = ocfs2_get_truncate_log_info(osb,
5814 /* ocfs2_truncate_log_shutdown keys on the existence of
5815 * osb->osb_tl_inode so we don't set any of the osb variables
5816 * until we're sure all is well. */
5817 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5818 ocfs2_truncate_log_worker);
5819 osb->osb_tl_bh = tl_bh;
5820 osb->osb_tl_inode = tl_inode;
5827 * Delayed de-allocation of suballocator blocks.
5829 * Some sets of block de-allocations might involve multiple suballocator inodes.
5831 * The locking for this can get extremely complicated, especially when
5832 * the suballocator inodes to delete from aren't known until deep
5833 * within an unrelated codepath.
5835 * ocfs2_extent_block structures are a good example of this - an inode
5836 * btree could have been grown by any number of nodes each allocating
5837 * out of their own suballoc inode.
5839 * These structures allow the delay of block de-allocation until a
5840 * later time, when locking of multiple cluster inodes won't cause
5845 * Describes a single block free from a suballocator
5847 struct ocfs2_cached_block_free {
5848 struct ocfs2_cached_block_free *free_next;
5850 unsigned int free_bit;
5853 struct ocfs2_per_slot_free_list {
5854 struct ocfs2_per_slot_free_list *f_next_suballocator;
5857 struct ocfs2_cached_block_free *f_first;
5860 static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5863 struct ocfs2_cached_block_free *head)
5868 struct inode *inode;
5869 struct buffer_head *di_bh = NULL;
5870 struct ocfs2_cached_block_free *tmp;
5872 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5879 mutex_lock(&inode->i_mutex);
5881 ret = ocfs2_inode_lock(inode, &di_bh, 1);
5887 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5888 if (IS_ERR(handle)) {
5889 ret = PTR_ERR(handle);
5895 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5897 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5898 head->free_bit, (unsigned long long)head->free_blk);
5900 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5901 head->free_bit, bg_blkno, 1);
5907 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5914 head = head->free_next;
5919 ocfs2_commit_trans(osb, handle);
5922 ocfs2_inode_unlock(inode, 1);
5925 mutex_unlock(&inode->i_mutex);
5929 /* Premature exit may have left some dangling items. */
5931 head = head->free_next;
5938 int ocfs2_run_deallocs(struct ocfs2_super *osb,
5939 struct ocfs2_cached_dealloc_ctxt *ctxt)
5942 struct ocfs2_per_slot_free_list *fl;
5947 while (ctxt->c_first_suballocator) {
5948 fl = ctxt->c_first_suballocator;
5951 mlog(0, "Free items: (type %u, slot %d)\n",
5952 fl->f_inode_type, fl->f_slot);
5953 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
5954 fl->f_slot, fl->f_first);
5961 ctxt->c_first_suballocator = fl->f_next_suballocator;
5968 static struct ocfs2_per_slot_free_list *
5969 ocfs2_find_per_slot_free_list(int type,
5971 struct ocfs2_cached_dealloc_ctxt *ctxt)
5973 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
5976 if (fl->f_inode_type == type && fl->f_slot == slot)
5979 fl = fl->f_next_suballocator;
5982 fl = kmalloc(sizeof(*fl), GFP_NOFS);
5984 fl->f_inode_type = type;
5987 fl->f_next_suballocator = ctxt->c_first_suballocator;
5989 ctxt->c_first_suballocator = fl;
5994 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
5995 int type, int slot, u64 blkno,
5999 struct ocfs2_per_slot_free_list *fl;
6000 struct ocfs2_cached_block_free *item;
6002 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6009 item = kmalloc(sizeof(*item), GFP_NOFS);
6016 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6017 type, slot, bit, (unsigned long long)blkno);
6019 item->free_blk = blkno;
6020 item->free_bit = bit;
6021 item->free_next = fl->f_first;
6030 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6031 struct ocfs2_extent_block *eb)
6033 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6034 le16_to_cpu(eb->h_suballoc_slot),
6035 le64_to_cpu(eb->h_blkno),
6036 le16_to_cpu(eb->h_suballoc_bit));
6039 /* This function will figure out whether the currently last extent
6040 * block will be deleted, and if it will, what the new last extent
6041 * block will be so we can update his h_next_leaf_blk field, as well
6042 * as the dinodes i_last_eb_blk */
6043 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6044 unsigned int clusters_to_del,
6045 struct ocfs2_path *path,
6046 struct buffer_head **new_last_eb)
6048 int next_free, ret = 0;
6050 struct ocfs2_extent_rec *rec;
6051 struct ocfs2_extent_block *eb;
6052 struct ocfs2_extent_list *el;
6053 struct buffer_head *bh = NULL;
6055 *new_last_eb = NULL;
6057 /* we have no tree, so of course, no last_eb. */
6058 if (!path->p_tree_depth)
6061 /* trunc to zero special case - this makes tree_depth = 0
6062 * regardless of what it is. */
6063 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6066 el = path_leaf_el(path);
6067 BUG_ON(!el->l_next_free_rec);
6070 * Make sure that this extent list will actually be empty
6071 * after we clear away the data. We can shortcut out if
6072 * there's more than one non-empty extent in the
6073 * list. Otherwise, a check of the remaining extent is
6076 next_free = le16_to_cpu(el->l_next_free_rec);
6078 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6082 /* We may have a valid extent in index 1, check it. */
6084 rec = &el->l_recs[1];
6087 * Fall through - no more nonempty extents, so we want
6088 * to delete this leaf.
6094 rec = &el->l_recs[0];
6099 * Check it we'll only be trimming off the end of this
6102 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6106 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6112 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6118 eb = (struct ocfs2_extent_block *) bh->b_data;
6120 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6121 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6127 get_bh(*new_last_eb);
6128 mlog(0, "returning block %llu, (cpos: %u)\n",
6129 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6137 * Trim some clusters off the rightmost edge of a tree. Only called
6140 * The caller needs to:
6141 * - start journaling of each path component.
6142 * - compute and fully set up any new last ext block
6144 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6145 handle_t *handle, struct ocfs2_truncate_context *tc,
6146 u32 clusters_to_del, u64 *delete_start)
6148 int ret, i, index = path->p_tree_depth;
6151 struct buffer_head *bh;
6152 struct ocfs2_extent_list *el;
6153 struct ocfs2_extent_rec *rec;
6157 while (index >= 0) {
6158 bh = path->p_node[index].bh;
6159 el = path->p_node[index].el;
6161 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6162 index, (unsigned long long)bh->b_blocknr);
6164 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6167 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6168 ocfs2_error(inode->i_sb,
6169 "Inode %lu has invalid ext. block %llu",
6171 (unsigned long long)bh->b_blocknr);
6177 i = le16_to_cpu(el->l_next_free_rec) - 1;
6178 rec = &el->l_recs[i];
6180 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6181 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6182 ocfs2_rec_clusters(el, rec),
6183 (unsigned long long)le64_to_cpu(rec->e_blkno),
6184 le16_to_cpu(el->l_next_free_rec));
6186 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6188 if (le16_to_cpu(el->l_tree_depth) == 0) {
6190 * If the leaf block contains a single empty
6191 * extent and no records, we can just remove
6194 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6196 sizeof(struct ocfs2_extent_rec));
6197 el->l_next_free_rec = cpu_to_le16(0);
6203 * Remove any empty extents by shifting things
6204 * left. That should make life much easier on
6205 * the code below. This condition is rare
6206 * enough that we shouldn't see a performance
6209 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6210 le16_add_cpu(&el->l_next_free_rec, -1);
6213 i < le16_to_cpu(el->l_next_free_rec); i++)
6214 el->l_recs[i] = el->l_recs[i + 1];
6216 memset(&el->l_recs[i], 0,
6217 sizeof(struct ocfs2_extent_rec));
6220 * We've modified our extent list. The
6221 * simplest way to handle this change
6222 * is to being the search from the
6225 goto find_tail_record;
6228 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6231 * We'll use "new_edge" on our way back up the
6232 * tree to know what our rightmost cpos is.
6234 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6235 new_edge += le32_to_cpu(rec->e_cpos);
6238 * The caller will use this to delete data blocks.
6240 *delete_start = le64_to_cpu(rec->e_blkno)
6241 + ocfs2_clusters_to_blocks(inode->i_sb,
6242 le16_to_cpu(rec->e_leaf_clusters));
6245 * If it's now empty, remove this record.
6247 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6249 sizeof(struct ocfs2_extent_rec));
6250 le16_add_cpu(&el->l_next_free_rec, -1);
6253 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6255 sizeof(struct ocfs2_extent_rec));
6256 le16_add_cpu(&el->l_next_free_rec, -1);
6261 /* Can this actually happen? */
6262 if (le16_to_cpu(el->l_next_free_rec) == 0)
6266 * We never actually deleted any clusters
6267 * because our leaf was empty. There's no
6268 * reason to adjust the rightmost edge then.
6273 rec->e_int_clusters = cpu_to_le32(new_edge);
6274 le32_add_cpu(&rec->e_int_clusters,
6275 -le32_to_cpu(rec->e_cpos));
6278 * A deleted child record should have been
6281 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6285 ret = ocfs2_journal_dirty(handle, bh);
6291 mlog(0, "extent list container %llu, after: record %d: "
6292 "(%u, %u, %llu), next = %u.\n",
6293 (unsigned long long)bh->b_blocknr, i,
6294 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6295 (unsigned long long)le64_to_cpu(rec->e_blkno),
6296 le16_to_cpu(el->l_next_free_rec));
6299 * We must be careful to only attempt delete of an
6300 * extent block (and not the root inode block).
6302 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6303 struct ocfs2_extent_block *eb =
6304 (struct ocfs2_extent_block *)bh->b_data;
6307 * Save this for use when processing the
6310 deleted_eb = le64_to_cpu(eb->h_blkno);
6312 mlog(0, "deleting this extent block.\n");
6314 ocfs2_remove_from_cache(inode, bh);
6316 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6317 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6318 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6320 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6321 /* An error here is not fatal. */
6336 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6337 unsigned int clusters_to_del,
6338 struct inode *inode,
6339 struct buffer_head *fe_bh,
6341 struct ocfs2_truncate_context *tc,
6342 struct ocfs2_path *path)
6345 struct ocfs2_dinode *fe;
6346 struct ocfs2_extent_block *last_eb = NULL;
6347 struct ocfs2_extent_list *el;
6348 struct buffer_head *last_eb_bh = NULL;
6351 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6353 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6361 * Each component will be touched, so we might as well journal
6362 * here to avoid having to handle errors later.
6364 status = ocfs2_journal_access_path(inode, handle, path);
6371 status = ocfs2_journal_access(handle, inode, last_eb_bh,
6372 OCFS2_JOURNAL_ACCESS_WRITE);
6378 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6381 el = &(fe->id2.i_list);
6384 * Lower levels depend on this never happening, but it's best
6385 * to check it up here before changing the tree.
6387 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6388 ocfs2_error(inode->i_sb,
6389 "Inode %lu has an empty extent record, depth %u\n",
6390 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6395 spin_lock(&OCFS2_I(inode)->ip_lock);
6396 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6398 spin_unlock(&OCFS2_I(inode)->ip_lock);
6399 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6400 inode->i_blocks = ocfs2_inode_sector_count(inode);
6402 status = ocfs2_trim_tree(inode, path, handle, tc,
6403 clusters_to_del, &delete_blk);
6409 if (le32_to_cpu(fe->i_clusters) == 0) {
6410 /* trunc to zero is a special case. */
6411 el->l_tree_depth = 0;
6412 fe->i_last_eb_blk = 0;
6414 fe->i_last_eb_blk = last_eb->h_blkno;
6416 status = ocfs2_journal_dirty(handle, fe_bh);
6423 /* If there will be a new last extent block, then by
6424 * definition, there cannot be any leaves to the right of
6426 last_eb->h_next_leaf_blk = 0;
6427 status = ocfs2_journal_dirty(handle, last_eb_bh);
6435 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6449 static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
6451 set_buffer_uptodate(bh);
6452 mark_buffer_dirty(bh);
6456 static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
6458 set_buffer_uptodate(bh);
6459 mark_buffer_dirty(bh);
6460 return ocfs2_journal_dirty_data(handle, bh);
6463 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6464 unsigned int from, unsigned int to,
6465 struct page *page, int zero, u64 *phys)
6467 int ret, partial = 0;
6469 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6474 zero_user_segment(page, from, to);
6477 * Need to set the buffers we zero'd into uptodate
6478 * here if they aren't - ocfs2_map_page_blocks()
6479 * might've skipped some
6481 if (ocfs2_should_order_data(inode)) {
6482 ret = walk_page_buffers(handle,
6485 ocfs2_ordered_zero_func);
6489 ret = walk_page_buffers(handle, page_buffers(page),
6491 ocfs2_writeback_zero_func);
6497 SetPageUptodate(page);
6499 flush_dcache_page(page);
6502 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6503 loff_t end, struct page **pages,
6504 int numpages, u64 phys, handle_t *handle)
6508 unsigned int from, to = PAGE_CACHE_SIZE;
6509 struct super_block *sb = inode->i_sb;
6511 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6516 to = PAGE_CACHE_SIZE;
6517 for(i = 0; i < numpages; i++) {
6520 from = start & (PAGE_CACHE_SIZE - 1);
6521 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6522 to = end & (PAGE_CACHE_SIZE - 1);
6524 BUG_ON(from > PAGE_CACHE_SIZE);
6525 BUG_ON(to > PAGE_CACHE_SIZE);
6527 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6530 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6534 ocfs2_unlock_and_free_pages(pages, numpages);
6537 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6538 struct page **pages, int *num)
6540 int numpages, ret = 0;
6541 struct super_block *sb = inode->i_sb;
6542 struct address_space *mapping = inode->i_mapping;
6543 unsigned long index;
6544 loff_t last_page_bytes;
6546 BUG_ON(start > end);
6548 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6549 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6552 last_page_bytes = PAGE_ALIGN(end);
6553 index = start >> PAGE_CACHE_SHIFT;
6555 pages[numpages] = grab_cache_page(mapping, index);
6556 if (!pages[numpages]) {
6564 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6569 ocfs2_unlock_and_free_pages(pages, numpages);
6579 * Zero the area past i_size but still within an allocated
6580 * cluster. This avoids exposing nonzero data on subsequent file
6583 * We need to call this before i_size is updated on the inode because
6584 * otherwise block_write_full_page() will skip writeout of pages past
6585 * i_size. The new_i_size parameter is passed for this reason.
6587 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6588 u64 range_start, u64 range_end)
6590 int ret = 0, numpages;
6591 struct page **pages = NULL;
6593 unsigned int ext_flags;
6594 struct super_block *sb = inode->i_sb;
6597 * File systems which don't support sparse files zero on every
6600 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6603 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6604 sizeof(struct page *), GFP_NOFS);
6605 if (pages == NULL) {
6611 if (range_start == range_end)
6614 ret = ocfs2_extent_map_get_blocks(inode,
6615 range_start >> sb->s_blocksize_bits,
6616 &phys, NULL, &ext_flags);
6623 * Tail is a hole, or is marked unwritten. In either case, we
6624 * can count on read and write to return/push zero's.
6626 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6629 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6636 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6637 numpages, phys, handle);
6640 * Initiate writeout of the pages we zero'd here. We don't
6641 * wait on them - the truncate_inode_pages() call later will
6644 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6645 range_end - 1, SYNC_FILE_RANGE_WRITE);
6656 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6657 struct ocfs2_dinode *di)
6659 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6660 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6662 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6663 memset(&di->id2, 0, blocksize -
6664 offsetof(struct ocfs2_dinode, id2) -
6667 memset(&di->id2, 0, blocksize -
6668 offsetof(struct ocfs2_dinode, id2));
6671 void ocfs2_dinode_new_extent_list(struct inode *inode,
6672 struct ocfs2_dinode *di)
6674 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6675 di->id2.i_list.l_tree_depth = 0;
6676 di->id2.i_list.l_next_free_rec = 0;
6677 di->id2.i_list.l_count = cpu_to_le16(
6678 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6681 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6683 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6684 struct ocfs2_inline_data *idata = &di->id2.i_data;
6686 spin_lock(&oi->ip_lock);
6687 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6688 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6689 spin_unlock(&oi->ip_lock);
6692 * We clear the entire i_data structure here so that all
6693 * fields can be properly initialized.
6695 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6697 idata->id_count = cpu_to_le16(
6698 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6701 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6702 struct buffer_head *di_bh)
6704 int ret, i, has_data, num_pages = 0;
6706 u64 uninitialized_var(block);
6707 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6708 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6709 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6710 struct ocfs2_alloc_context *data_ac = NULL;
6711 struct page **pages = NULL;
6712 loff_t end = osb->s_clustersize;
6714 has_data = i_size_read(inode) ? 1 : 0;
6717 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6718 sizeof(struct page *), GFP_NOFS);
6719 if (pages == NULL) {
6725 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6732 handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
6733 if (IS_ERR(handle)) {
6734 ret = PTR_ERR(handle);
6739 ret = ocfs2_journal_access(handle, inode, di_bh,
6740 OCFS2_JOURNAL_ACCESS_WRITE);
6748 unsigned int page_end;
6751 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6759 * Save two copies, one for insert, and one that can
6760 * be changed by ocfs2_map_and_dirty_page() below.
6762 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6765 * Non sparse file systems zero on extend, so no need
6768 if (!ocfs2_sparse_alloc(osb) &&
6769 PAGE_CACHE_SIZE < osb->s_clustersize)
6770 end = PAGE_CACHE_SIZE;
6772 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6779 * This should populate the 1st page for us and mark
6782 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6788 page_end = PAGE_CACHE_SIZE;
6789 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6790 page_end = osb->s_clustersize;
6792 for (i = 0; i < num_pages; i++)
6793 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6794 pages[i], i > 0, &phys);
6797 spin_lock(&oi->ip_lock);
6798 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6799 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6800 spin_unlock(&oi->ip_lock);
6802 ocfs2_dinode_new_extent_list(inode, di);
6804 ocfs2_journal_dirty(handle, di_bh);
6808 * An error at this point should be extremely rare. If
6809 * this proves to be false, we could always re-build
6810 * the in-inode data from our pages.
6812 ret = ocfs2_dinode_insert_extent(osb, handle, inode, di_bh,
6813 0, block, 1, 0, NULL);
6819 inode->i_blocks = ocfs2_inode_sector_count(inode);
6823 ocfs2_commit_trans(osb, handle);
6827 ocfs2_free_alloc_context(data_ac);
6831 ocfs2_unlock_and_free_pages(pages, num_pages);
6839 * It is expected, that by the time you call this function,
6840 * inode->i_size and fe->i_size have been adjusted.
6842 * WARNING: This will kfree the truncate context
6844 int ocfs2_commit_truncate(struct ocfs2_super *osb,
6845 struct inode *inode,
6846 struct buffer_head *fe_bh,
6847 struct ocfs2_truncate_context *tc)
6849 int status, i, credits, tl_sem = 0;
6850 u32 clusters_to_del, new_highest_cpos, range;
6851 struct ocfs2_extent_list *el;
6852 handle_t *handle = NULL;
6853 struct inode *tl_inode = osb->osb_tl_inode;
6854 struct ocfs2_path *path = NULL;
6855 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6859 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
6860 i_size_read(inode));
6862 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
6869 ocfs2_extent_map_trunc(inode, new_highest_cpos);
6873 * Check that we still have allocation to delete.
6875 if (OCFS2_I(inode)->ip_clusters == 0) {
6881 * Truncate always works against the rightmost tree branch.
6883 status = ocfs2_find_path(inode, path, UINT_MAX);
6889 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6890 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
6893 * By now, el will point to the extent list on the bottom most
6894 * portion of this tree. Only the tail record is considered in
6897 * We handle the following cases, in order:
6898 * - empty extent: delete the remaining branch
6899 * - remove the entire record
6900 * - remove a partial record
6901 * - no record needs to be removed (truncate has completed)
6903 el = path_leaf_el(path);
6904 if (le16_to_cpu(el->l_next_free_rec) == 0) {
6905 ocfs2_error(inode->i_sb,
6906 "Inode %llu has empty extent block at %llu\n",
6907 (unsigned long long)OCFS2_I(inode)->ip_blkno,
6908 (unsigned long long)path_leaf_bh(path)->b_blocknr);
6913 i = le16_to_cpu(el->l_next_free_rec) - 1;
6914 range = le32_to_cpu(el->l_recs[i].e_cpos) +
6915 ocfs2_rec_clusters(el, &el->l_recs[i]);
6916 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
6917 clusters_to_del = 0;
6918 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
6919 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
6920 } else if (range > new_highest_cpos) {
6921 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
6922 le32_to_cpu(el->l_recs[i].e_cpos)) -
6929 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6930 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
6932 mutex_lock(&tl_inode->i_mutex);
6934 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6935 * record is free for use. If there isn't any, we flush to get
6936 * an empty truncate log. */
6937 if (ocfs2_truncate_log_needs_flush(osb)) {
6938 status = __ocfs2_flush_truncate_log(osb);
6945 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
6946 (struct ocfs2_dinode *)fe_bh->b_data,
6948 handle = ocfs2_start_trans(osb, credits);
6949 if (IS_ERR(handle)) {
6950 status = PTR_ERR(handle);
6956 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
6963 mutex_unlock(&tl_inode->i_mutex);
6966 ocfs2_commit_trans(osb, handle);
6969 ocfs2_reinit_path(path, 1);
6972 * The check above will catch the case where we've truncated
6973 * away all allocation.
6979 ocfs2_schedule_truncate_log_flush(osb, 1);
6982 mutex_unlock(&tl_inode->i_mutex);
6985 ocfs2_commit_trans(osb, handle);
6987 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
6989 ocfs2_free_path(path);
6991 /* This will drop the ext_alloc cluster lock for us */
6992 ocfs2_free_truncate_context(tc);
6999 * Expects the inode to already be locked.
7001 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7002 struct inode *inode,
7003 struct buffer_head *fe_bh,
7004 struct ocfs2_truncate_context **tc)
7007 unsigned int new_i_clusters;
7008 struct ocfs2_dinode *fe;
7009 struct ocfs2_extent_block *eb;
7010 struct buffer_head *last_eb_bh = NULL;
7016 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7017 i_size_read(inode));
7018 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7020 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7021 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7022 (unsigned long long)le64_to_cpu(fe->i_size));
7024 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7030 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7032 if (fe->id2.i_list.l_tree_depth) {
7033 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
7034 &last_eb_bh, OCFS2_BH_CACHED, inode);
7039 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7040 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
7041 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
7049 (*tc)->tc_last_eb_bh = last_eb_bh;
7055 ocfs2_free_truncate_context(*tc);
7063 * 'start' is inclusive, 'end' is not.
7065 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7066 unsigned int start, unsigned int end, int trunc)
7069 unsigned int numbytes;
7071 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7072 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7073 struct ocfs2_inline_data *idata = &di->id2.i_data;
7075 if (end > i_size_read(inode))
7076 end = i_size_read(inode);
7078 BUG_ON(start >= end);
7080 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7081 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7082 !ocfs2_supports_inline_data(osb)) {
7083 ocfs2_error(inode->i_sb,
7084 "Inline data flags for inode %llu don't agree! "
7085 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7086 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7087 le16_to_cpu(di->i_dyn_features),
7088 OCFS2_I(inode)->ip_dyn_features,
7089 osb->s_feature_incompat);
7094 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7095 if (IS_ERR(handle)) {
7096 ret = PTR_ERR(handle);
7101 ret = ocfs2_journal_access(handle, inode, di_bh,
7102 OCFS2_JOURNAL_ACCESS_WRITE);
7108 numbytes = end - start;
7109 memset(idata->id_data + start, 0, numbytes);
7112 * No need to worry about the data page here - it's been
7113 * truncated already and inline data doesn't need it for
7114 * pushing zero's to disk, so we'll let readpage pick it up
7118 i_size_write(inode, start);
7119 di->i_size = cpu_to_le64(start);
7122 inode->i_blocks = ocfs2_inode_sector_count(inode);
7123 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7125 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7126 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7128 ocfs2_journal_dirty(handle, di_bh);
7131 ocfs2_commit_trans(osb, handle);
7137 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7140 * The caller is responsible for completing deallocation
7141 * before freeing the context.
7143 if (tc->tc_dealloc.c_first_suballocator != NULL)
7145 "Truncate completion has non-empty dealloc context\n");
7147 if (tc->tc_last_eb_bh)
7148 brelse(tc->tc_last_eb_bh);