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"
52 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
);
53 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
54 struct ocfs2_extent_block
*eb
);
57 * Structures which describe a path through a btree, and functions to
60 * The idea here is to be as generic as possible with the tree
63 struct ocfs2_path_item
{
64 struct buffer_head
*bh
;
65 struct ocfs2_extent_list
*el
;
68 #define OCFS2_MAX_PATH_DEPTH 5
72 struct ocfs2_path_item p_node
[OCFS2_MAX_PATH_DEPTH
];
75 #define path_root_bh(_path) ((_path)->p_node[0].bh)
76 #define path_root_el(_path) ((_path)->p_node[0].el)
77 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
78 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
79 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
82 * Reset the actual path elements so that we can re-use the structure
83 * to build another path. Generally, this involves freeing the buffer
86 static void ocfs2_reinit_path(struct ocfs2_path
*path
, int keep_root
)
88 int i
, start
= 0, depth
= 0;
89 struct ocfs2_path_item
*node
;
94 for(i
= start
; i
< path_num_items(path
); i
++) {
95 node
= &path
->p_node
[i
];
103 * Tree depth may change during truncate, or insert. If we're
104 * keeping the root extent list, then make sure that our path
105 * structure reflects the proper depth.
108 depth
= le16_to_cpu(path_root_el(path
)->l_tree_depth
);
110 path
->p_tree_depth
= depth
;
113 static void ocfs2_free_path(struct ocfs2_path
*path
)
116 ocfs2_reinit_path(path
, 0);
122 * All the elements of src into dest. After this call, src could be freed
123 * without affecting dest.
125 * Both paths should have the same root. Any non-root elements of dest
128 static void ocfs2_cp_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
132 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
133 BUG_ON(path_root_el(dest
) != path_root_el(src
));
135 ocfs2_reinit_path(dest
, 1);
137 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
138 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
139 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
141 if (dest
->p_node
[i
].bh
)
142 get_bh(dest
->p_node
[i
].bh
);
147 * Make the *dest path the same as src and re-initialize src path to
150 static void ocfs2_mv_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
154 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
156 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
157 brelse(dest
->p_node
[i
].bh
);
159 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
160 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
162 src
->p_node
[i
].bh
= NULL
;
163 src
->p_node
[i
].el
= NULL
;
168 * Insert an extent block at given index.
170 * This will not take an additional reference on eb_bh.
172 static inline void ocfs2_path_insert_eb(struct ocfs2_path
*path
, int index
,
173 struct buffer_head
*eb_bh
)
175 struct ocfs2_extent_block
*eb
= (struct ocfs2_extent_block
*)eb_bh
->b_data
;
178 * Right now, no root bh is an extent block, so this helps
179 * catch code errors with dinode trees. The assertion can be
180 * safely removed if we ever need to insert extent block
181 * structures at the root.
185 path
->p_node
[index
].bh
= eb_bh
;
186 path
->p_node
[index
].el
= &eb
->h_list
;
189 static struct ocfs2_path
*ocfs2_new_path(struct buffer_head
*root_bh
,
190 struct ocfs2_extent_list
*root_el
)
192 struct ocfs2_path
*path
;
194 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) >= OCFS2_MAX_PATH_DEPTH
);
196 path
= kzalloc(sizeof(*path
), GFP_NOFS
);
198 path
->p_tree_depth
= le16_to_cpu(root_el
->l_tree_depth
);
200 path_root_bh(path
) = root_bh
;
201 path_root_el(path
) = root_el
;
208 * Allocate and initialize a new path based on a disk inode tree.
210 static struct ocfs2_path
*ocfs2_new_inode_path(struct buffer_head
*di_bh
)
212 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
213 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
215 return ocfs2_new_path(di_bh
, el
);
219 * Convenience function to journal all components in a path.
221 static int ocfs2_journal_access_path(struct inode
*inode
, handle_t
*handle
,
222 struct ocfs2_path
*path
)
229 for(i
= 0; i
< path_num_items(path
); i
++) {
230 ret
= ocfs2_journal_access(handle
, inode
, path
->p_node
[i
].bh
,
231 OCFS2_JOURNAL_ACCESS_WRITE
);
243 * Return the index of the extent record which contains cluster #v_cluster.
244 * -1 is returned if it was not found.
246 * Should work fine on interior and exterior nodes.
248 int ocfs2_search_extent_list(struct ocfs2_extent_list
*el
, u32 v_cluster
)
252 struct ocfs2_extent_rec
*rec
;
253 u32 rec_end
, rec_start
, clusters
;
255 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
256 rec
= &el
->l_recs
[i
];
258 rec_start
= le32_to_cpu(rec
->e_cpos
);
259 clusters
= ocfs2_rec_clusters(el
, rec
);
261 rec_end
= rec_start
+ clusters
;
263 if (v_cluster
>= rec_start
&& v_cluster
< rec_end
) {
272 enum ocfs2_contig_type
{
281 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
282 * ocfs2_extent_contig only work properly against leaf nodes!
284 static int ocfs2_block_extent_contig(struct super_block
*sb
,
285 struct ocfs2_extent_rec
*ext
,
288 u64 blk_end
= le64_to_cpu(ext
->e_blkno
);
290 blk_end
+= ocfs2_clusters_to_blocks(sb
,
291 le16_to_cpu(ext
->e_leaf_clusters
));
293 return blkno
== blk_end
;
296 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec
*left
,
297 struct ocfs2_extent_rec
*right
)
301 left_range
= le32_to_cpu(left
->e_cpos
) +
302 le16_to_cpu(left
->e_leaf_clusters
);
304 return (left_range
== le32_to_cpu(right
->e_cpos
));
307 static enum ocfs2_contig_type
308 ocfs2_extent_contig(struct inode
*inode
,
309 struct ocfs2_extent_rec
*ext
,
310 struct ocfs2_extent_rec
*insert_rec
)
312 u64 blkno
= le64_to_cpu(insert_rec
->e_blkno
);
315 * Refuse to coalesce extent records with different flag
316 * fields - we don't want to mix unwritten extents with user
319 if (ext
->e_flags
!= insert_rec
->e_flags
)
322 if (ocfs2_extents_adjacent(ext
, insert_rec
) &&
323 ocfs2_block_extent_contig(inode
->i_sb
, ext
, blkno
))
326 blkno
= le64_to_cpu(ext
->e_blkno
);
327 if (ocfs2_extents_adjacent(insert_rec
, ext
) &&
328 ocfs2_block_extent_contig(inode
->i_sb
, insert_rec
, blkno
))
335 * NOTE: We can have pretty much any combination of contiguousness and
338 * The usefulness of APPEND_TAIL is more in that it lets us know that
339 * we'll have to update the path to that leaf.
341 enum ocfs2_append_type
{
346 enum ocfs2_split_type
{
352 struct ocfs2_insert_type
{
353 enum ocfs2_split_type ins_split
;
354 enum ocfs2_append_type ins_appending
;
355 enum ocfs2_contig_type ins_contig
;
356 int ins_contig_index
;
360 struct ocfs2_merge_ctxt
{
361 enum ocfs2_contig_type c_contig_type
;
362 int c_has_empty_extent
;
363 int c_split_covers_rec
;
367 * How many free extents have we got before we need more meta data?
369 int ocfs2_num_free_extents(struct ocfs2_super
*osb
,
371 struct ocfs2_dinode
*fe
)
374 struct ocfs2_extent_list
*el
;
375 struct ocfs2_extent_block
*eb
;
376 struct buffer_head
*eb_bh
= NULL
;
380 if (!OCFS2_IS_VALID_DINODE(fe
)) {
381 OCFS2_RO_ON_INVALID_DINODE(inode
->i_sb
, fe
);
386 if (fe
->i_last_eb_blk
) {
387 retval
= ocfs2_read_block(osb
, le64_to_cpu(fe
->i_last_eb_blk
),
388 &eb_bh
, OCFS2_BH_CACHED
, inode
);
393 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
396 el
= &fe
->id2
.i_list
;
398 BUG_ON(el
->l_tree_depth
!= 0);
400 retval
= le16_to_cpu(el
->l_count
) - le16_to_cpu(el
->l_next_free_rec
);
409 /* expects array to already be allocated
411 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
414 static int ocfs2_create_new_meta_bhs(struct ocfs2_super
*osb
,
418 struct ocfs2_alloc_context
*meta_ac
,
419 struct buffer_head
*bhs
[])
421 int count
, status
, i
;
422 u16 suballoc_bit_start
;
425 struct ocfs2_extent_block
*eb
;
430 while (count
< wanted
) {
431 status
= ocfs2_claim_metadata(osb
,
443 for(i
= count
; i
< (num_got
+ count
); i
++) {
444 bhs
[i
] = sb_getblk(osb
->sb
, first_blkno
);
445 if (bhs
[i
] == NULL
) {
450 ocfs2_set_new_buffer_uptodate(inode
, bhs
[i
]);
452 status
= ocfs2_journal_access(handle
, inode
, bhs
[i
],
453 OCFS2_JOURNAL_ACCESS_CREATE
);
459 memset(bhs
[i
]->b_data
, 0, osb
->sb
->s_blocksize
);
460 eb
= (struct ocfs2_extent_block
*) bhs
[i
]->b_data
;
461 /* Ok, setup the minimal stuff here. */
462 strcpy(eb
->h_signature
, OCFS2_EXTENT_BLOCK_SIGNATURE
);
463 eb
->h_blkno
= cpu_to_le64(first_blkno
);
464 eb
->h_fs_generation
= cpu_to_le32(osb
->fs_generation
);
465 eb
->h_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
466 eb
->h_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
468 cpu_to_le16(ocfs2_extent_recs_per_eb(osb
->sb
));
470 suballoc_bit_start
++;
473 /* We'll also be dirtied by the caller, so
474 * this isn't absolutely necessary. */
475 status
= ocfs2_journal_dirty(handle
, bhs
[i
]);
488 for(i
= 0; i
< wanted
; i
++) {
499 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
501 * Returns the sum of the rightmost extent rec logical offset and
504 * ocfs2_add_branch() uses this to determine what logical cluster
505 * value should be populated into the leftmost new branch records.
507 * ocfs2_shift_tree_depth() uses this to determine the # clusters
508 * value for the new topmost tree record.
510 static inline u32
ocfs2_sum_rightmost_rec(struct ocfs2_extent_list
*el
)
514 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
516 return le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
517 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
521 * Add an entire tree branch to our inode. eb_bh is the extent block
522 * to start at, if we don't want to start the branch at the dinode
525 * last_eb_bh is required as we have to update it's next_leaf pointer
526 * for the new last extent block.
528 * the new branch will be 'empty' in the sense that every block will
529 * contain a single record with cluster count == 0.
531 static int ocfs2_add_branch(struct ocfs2_super
*osb
,
534 struct buffer_head
*fe_bh
,
535 struct buffer_head
*eb_bh
,
536 struct buffer_head
**last_eb_bh
,
537 struct ocfs2_alloc_context
*meta_ac
)
539 int status
, new_blocks
, i
;
540 u64 next_blkno
, new_last_eb_blk
;
541 struct buffer_head
*bh
;
542 struct buffer_head
**new_eb_bhs
= NULL
;
543 struct ocfs2_dinode
*fe
;
544 struct ocfs2_extent_block
*eb
;
545 struct ocfs2_extent_list
*eb_el
;
546 struct ocfs2_extent_list
*el
;
551 BUG_ON(!last_eb_bh
|| !*last_eb_bh
);
553 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
556 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
559 el
= &fe
->id2
.i_list
;
561 /* we never add a branch to a leaf. */
562 BUG_ON(!el
->l_tree_depth
);
564 new_blocks
= le16_to_cpu(el
->l_tree_depth
);
566 /* allocate the number of new eb blocks we need */
567 new_eb_bhs
= kcalloc(new_blocks
, sizeof(struct buffer_head
*),
575 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, new_blocks
,
576 meta_ac
, new_eb_bhs
);
582 eb
= (struct ocfs2_extent_block
*)(*last_eb_bh
)->b_data
;
583 new_cpos
= ocfs2_sum_rightmost_rec(&eb
->h_list
);
585 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
586 * linked with the rest of the tree.
587 * conversly, new_eb_bhs[0] is the new bottommost leaf.
589 * when we leave the loop, new_last_eb_blk will point to the
590 * newest leaf, and next_blkno will point to the topmost extent
592 next_blkno
= new_last_eb_blk
= 0;
593 for(i
= 0; i
< new_blocks
; i
++) {
595 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
596 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
597 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
603 status
= ocfs2_journal_access(handle
, inode
, bh
,
604 OCFS2_JOURNAL_ACCESS_CREATE
);
610 eb
->h_next_leaf_blk
= 0;
611 eb_el
->l_tree_depth
= cpu_to_le16(i
);
612 eb_el
->l_next_free_rec
= cpu_to_le16(1);
614 * This actually counts as an empty extent as
617 eb_el
->l_recs
[0].e_cpos
= cpu_to_le32(new_cpos
);
618 eb_el
->l_recs
[0].e_blkno
= cpu_to_le64(next_blkno
);
620 * eb_el isn't always an interior node, but even leaf
621 * nodes want a zero'd flags and reserved field so
622 * this gets the whole 32 bits regardless of use.
624 eb_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(0);
625 if (!eb_el
->l_tree_depth
)
626 new_last_eb_blk
= le64_to_cpu(eb
->h_blkno
);
628 status
= ocfs2_journal_dirty(handle
, bh
);
634 next_blkno
= le64_to_cpu(eb
->h_blkno
);
637 /* This is a bit hairy. We want to update up to three blocks
638 * here without leaving any of them in an inconsistent state
639 * in case of error. We don't have to worry about
640 * journal_dirty erroring as it won't unless we've aborted the
641 * handle (in which case we would never be here) so reserving
642 * the write with journal_access is all we need to do. */
643 status
= ocfs2_journal_access(handle
, inode
, *last_eb_bh
,
644 OCFS2_JOURNAL_ACCESS_WRITE
);
649 status
= ocfs2_journal_access(handle
, inode
, fe_bh
,
650 OCFS2_JOURNAL_ACCESS_WRITE
);
656 status
= ocfs2_journal_access(handle
, inode
, eb_bh
,
657 OCFS2_JOURNAL_ACCESS_WRITE
);
664 /* Link the new branch into the rest of the tree (el will
665 * either be on the fe, or the extent block passed in. */
666 i
= le16_to_cpu(el
->l_next_free_rec
);
667 el
->l_recs
[i
].e_blkno
= cpu_to_le64(next_blkno
);
668 el
->l_recs
[i
].e_cpos
= cpu_to_le32(new_cpos
);
669 el
->l_recs
[i
].e_int_clusters
= 0;
670 le16_add_cpu(&el
->l_next_free_rec
, 1);
672 /* fe needs a new last extent block pointer, as does the
673 * next_leaf on the previously last-extent-block. */
674 fe
->i_last_eb_blk
= cpu_to_le64(new_last_eb_blk
);
676 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
677 eb
->h_next_leaf_blk
= cpu_to_le64(new_last_eb_blk
);
679 status
= ocfs2_journal_dirty(handle
, *last_eb_bh
);
682 status
= ocfs2_journal_dirty(handle
, fe_bh
);
686 status
= ocfs2_journal_dirty(handle
, eb_bh
);
692 * Some callers want to track the rightmost leaf so pass it
696 get_bh(new_eb_bhs
[0]);
697 *last_eb_bh
= new_eb_bhs
[0];
702 for (i
= 0; i
< new_blocks
; i
++)
704 brelse(new_eb_bhs
[i
]);
713 * adds another level to the allocation tree.
714 * returns back the new extent block so you can add a branch to it
717 static int ocfs2_shift_tree_depth(struct ocfs2_super
*osb
,
720 struct buffer_head
*fe_bh
,
721 struct ocfs2_alloc_context
*meta_ac
,
722 struct buffer_head
**ret_new_eb_bh
)
726 struct buffer_head
*new_eb_bh
= NULL
;
727 struct ocfs2_dinode
*fe
;
728 struct ocfs2_extent_block
*eb
;
729 struct ocfs2_extent_list
*fe_el
;
730 struct ocfs2_extent_list
*eb_el
;
734 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, 1, meta_ac
,
741 eb
= (struct ocfs2_extent_block
*) new_eb_bh
->b_data
;
742 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
743 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
749 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
750 fe_el
= &fe
->id2
.i_list
;
752 status
= ocfs2_journal_access(handle
, inode
, new_eb_bh
,
753 OCFS2_JOURNAL_ACCESS_CREATE
);
759 /* copy the fe data into the new extent block */
760 eb_el
->l_tree_depth
= fe_el
->l_tree_depth
;
761 eb_el
->l_next_free_rec
= fe_el
->l_next_free_rec
;
762 for(i
= 0; i
< le16_to_cpu(fe_el
->l_next_free_rec
); i
++)
763 eb_el
->l_recs
[i
] = fe_el
->l_recs
[i
];
765 status
= ocfs2_journal_dirty(handle
, new_eb_bh
);
771 status
= ocfs2_journal_access(handle
, inode
, fe_bh
,
772 OCFS2_JOURNAL_ACCESS_WRITE
);
778 new_clusters
= ocfs2_sum_rightmost_rec(eb_el
);
781 le16_add_cpu(&fe_el
->l_tree_depth
, 1);
782 fe_el
->l_recs
[0].e_cpos
= 0;
783 fe_el
->l_recs
[0].e_blkno
= eb
->h_blkno
;
784 fe_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(new_clusters
);
785 for(i
= 1; i
< le16_to_cpu(fe_el
->l_next_free_rec
); i
++)
786 memset(&fe_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
787 fe_el
->l_next_free_rec
= cpu_to_le16(1);
789 /* If this is our 1st tree depth shift, then last_eb_blk
790 * becomes the allocated extent block */
791 if (fe_el
->l_tree_depth
== cpu_to_le16(1))
792 fe
->i_last_eb_blk
= eb
->h_blkno
;
794 status
= ocfs2_journal_dirty(handle
, fe_bh
);
800 *ret_new_eb_bh
= new_eb_bh
;
812 * Should only be called when there is no space left in any of the
813 * leaf nodes. What we want to do is find the lowest tree depth
814 * non-leaf extent block with room for new records. There are three
815 * valid results of this search:
817 * 1) a lowest extent block is found, then we pass it back in
818 * *lowest_eb_bh and return '0'
820 * 2) the search fails to find anything, but the dinode has room. We
821 * pass NULL back in *lowest_eb_bh, but still return '0'
823 * 3) the search fails to find anything AND the dinode is full, in
824 * which case we return > 0
826 * return status < 0 indicates an error.
828 static int ocfs2_find_branch_target(struct ocfs2_super
*osb
,
830 struct buffer_head
*fe_bh
,
831 struct buffer_head
**target_bh
)
835 struct ocfs2_dinode
*fe
;
836 struct ocfs2_extent_block
*eb
;
837 struct ocfs2_extent_list
*el
;
838 struct buffer_head
*bh
= NULL
;
839 struct buffer_head
*lowest_bh
= NULL
;
845 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
846 el
= &fe
->id2
.i_list
;
848 while(le16_to_cpu(el
->l_tree_depth
) > 1) {
849 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
850 ocfs2_error(inode
->i_sb
, "Dinode %llu has empty "
851 "extent list (next_free_rec == 0)",
852 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
856 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
857 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
859 ocfs2_error(inode
->i_sb
, "Dinode %llu has extent "
860 "list where extent # %d has no physical "
862 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, i
);
872 status
= ocfs2_read_block(osb
, blkno
, &bh
, OCFS2_BH_CACHED
,
879 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
880 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
881 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
887 if (le16_to_cpu(el
->l_next_free_rec
) <
888 le16_to_cpu(el
->l_count
)) {
896 /* If we didn't find one and the fe doesn't have any room,
899 && (fe
->id2
.i_list
.l_next_free_rec
== fe
->id2
.i_list
.l_count
))
902 *target_bh
= lowest_bh
;
912 * Grow a b-tree so that it has more records.
914 * We might shift the tree depth in which case existing paths should
915 * be considered invalid.
917 * Tree depth after the grow is returned via *final_depth.
919 * *last_eb_bh will be updated by ocfs2_add_branch().
921 static int ocfs2_grow_tree(struct inode
*inode
, handle_t
*handle
,
922 struct buffer_head
*di_bh
, int *final_depth
,
923 struct buffer_head
**last_eb_bh
,
924 struct ocfs2_alloc_context
*meta_ac
)
927 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
928 int depth
= le16_to_cpu(di
->id2
.i_list
.l_tree_depth
);
929 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
930 struct buffer_head
*bh
= NULL
;
932 BUG_ON(meta_ac
== NULL
);
934 shift
= ocfs2_find_branch_target(osb
, inode
, di_bh
, &bh
);
941 /* We traveled all the way to the bottom of the allocation tree
942 * and didn't find room for any more extents - we need to add
943 * another tree level */
946 mlog(0, "need to shift tree depth (current = %d)\n", depth
);
948 /* ocfs2_shift_tree_depth will return us a buffer with
949 * the new extent block (so we can pass that to
950 * ocfs2_add_branch). */
951 ret
= ocfs2_shift_tree_depth(osb
, handle
, inode
, di_bh
,
960 * Special case: we have room now if we shifted from
961 * tree_depth 0, so no more work needs to be done.
963 * We won't be calling add_branch, so pass
964 * back *last_eb_bh as the new leaf. At depth
965 * zero, it should always be null so there's
966 * no reason to brelse.
975 /* call ocfs2_add_branch to add the final part of the tree with
977 mlog(0, "add branch. bh = %p\n", bh
);
978 ret
= ocfs2_add_branch(osb
, handle
, inode
, di_bh
, bh
, last_eb_bh
,
987 *final_depth
= depth
;
993 * This is only valid for leaf nodes, which are the only ones that can
994 * have empty extents anyway.
996 static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec
*rec
)
998 return !rec
->e_leaf_clusters
;
1002 * This function will discard the rightmost extent record.
1004 static void ocfs2_shift_records_right(struct ocfs2_extent_list
*el
)
1006 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1007 int count
= le16_to_cpu(el
->l_count
);
1008 unsigned int num_bytes
;
1011 /* This will cause us to go off the end of our extent list. */
1012 BUG_ON(next_free
>= count
);
1014 num_bytes
= sizeof(struct ocfs2_extent_rec
) * next_free
;
1016 memmove(&el
->l_recs
[1], &el
->l_recs
[0], num_bytes
);
1019 static void ocfs2_rotate_leaf(struct ocfs2_extent_list
*el
,
1020 struct ocfs2_extent_rec
*insert_rec
)
1022 int i
, insert_index
, next_free
, has_empty
, num_bytes
;
1023 u32 insert_cpos
= le32_to_cpu(insert_rec
->e_cpos
);
1024 struct ocfs2_extent_rec
*rec
;
1026 next_free
= le16_to_cpu(el
->l_next_free_rec
);
1027 has_empty
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
1031 /* The tree code before us didn't allow enough room in the leaf. */
1032 if (el
->l_next_free_rec
== el
->l_count
&& !has_empty
)
1036 * The easiest way to approach this is to just remove the
1037 * empty extent and temporarily decrement next_free.
1041 * If next_free was 1 (only an empty extent), this
1042 * loop won't execute, which is fine. We still want
1043 * the decrement above to happen.
1045 for(i
= 0; i
< (next_free
- 1); i
++)
1046 el
->l_recs
[i
] = el
->l_recs
[i
+1];
1052 * Figure out what the new record index should be.
1054 for(i
= 0; i
< next_free
; i
++) {
1055 rec
= &el
->l_recs
[i
];
1057 if (insert_cpos
< le32_to_cpu(rec
->e_cpos
))
1062 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1063 insert_cpos
, insert_index
, has_empty
, next_free
, le16_to_cpu(el
->l_count
));
1065 BUG_ON(insert_index
< 0);
1066 BUG_ON(insert_index
>= le16_to_cpu(el
->l_count
));
1067 BUG_ON(insert_index
> next_free
);
1070 * No need to memmove if we're just adding to the tail.
1072 if (insert_index
!= next_free
) {
1073 BUG_ON(next_free
>= le16_to_cpu(el
->l_count
));
1075 num_bytes
= next_free
- insert_index
;
1076 num_bytes
*= sizeof(struct ocfs2_extent_rec
);
1077 memmove(&el
->l_recs
[insert_index
+ 1],
1078 &el
->l_recs
[insert_index
],
1083 * Either we had an empty extent, and need to re-increment or
1084 * there was no empty extent on a non full rightmost leaf node,
1085 * in which case we still need to increment.
1088 el
->l_next_free_rec
= cpu_to_le16(next_free
);
1090 * Make sure none of the math above just messed up our tree.
1092 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) > le16_to_cpu(el
->l_count
));
1094 el
->l_recs
[insert_index
] = *insert_rec
;
1098 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list
*el
)
1100 int size
, num_recs
= le16_to_cpu(el
->l_next_free_rec
);
1102 BUG_ON(num_recs
== 0);
1104 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
1106 size
= num_recs
* sizeof(struct ocfs2_extent_rec
);
1107 memmove(&el
->l_recs
[0], &el
->l_recs
[1], size
);
1108 memset(&el
->l_recs
[num_recs
], 0,
1109 sizeof(struct ocfs2_extent_rec
));
1110 el
->l_next_free_rec
= cpu_to_le16(num_recs
);
1115 * Create an empty extent record .
1117 * l_next_free_rec may be updated.
1119 * If an empty extent already exists do nothing.
1121 static void ocfs2_create_empty_extent(struct ocfs2_extent_list
*el
)
1123 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1125 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
1130 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
1133 mlog_bug_on_msg(el
->l_count
== el
->l_next_free_rec
,
1134 "Asked to create an empty extent in a full list:\n"
1135 "count = %u, tree depth = %u",
1136 le16_to_cpu(el
->l_count
),
1137 le16_to_cpu(el
->l_tree_depth
));
1139 ocfs2_shift_records_right(el
);
1142 le16_add_cpu(&el
->l_next_free_rec
, 1);
1143 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1147 * For a rotation which involves two leaf nodes, the "root node" is
1148 * the lowest level tree node which contains a path to both leafs. This
1149 * resulting set of information can be used to form a complete "subtree"
1151 * This function is passed two full paths from the dinode down to a
1152 * pair of adjacent leaves. It's task is to figure out which path
1153 * index contains the subtree root - this can be the root index itself
1154 * in a worst-case rotation.
1156 * The array index of the subtree root is passed back.
1158 static int ocfs2_find_subtree_root(struct inode
*inode
,
1159 struct ocfs2_path
*left
,
1160 struct ocfs2_path
*right
)
1165 * Check that the caller passed in two paths from the same tree.
1167 BUG_ON(path_root_bh(left
) != path_root_bh(right
));
1173 * The caller didn't pass two adjacent paths.
1175 mlog_bug_on_msg(i
> left
->p_tree_depth
,
1176 "Inode %lu, left depth %u, right depth %u\n"
1177 "left leaf blk %llu, right leaf blk %llu\n",
1178 inode
->i_ino
, left
->p_tree_depth
,
1179 right
->p_tree_depth
,
1180 (unsigned long long)path_leaf_bh(left
)->b_blocknr
,
1181 (unsigned long long)path_leaf_bh(right
)->b_blocknr
);
1182 } while (left
->p_node
[i
].bh
->b_blocknr
==
1183 right
->p_node
[i
].bh
->b_blocknr
);
1188 typedef void (path_insert_t
)(void *, struct buffer_head
*);
1191 * Traverse a btree path in search of cpos, starting at root_el.
1193 * This code can be called with a cpos larger than the tree, in which
1194 * case it will return the rightmost path.
1196 static int __ocfs2_find_path(struct inode
*inode
,
1197 struct ocfs2_extent_list
*root_el
, u32 cpos
,
1198 path_insert_t
*func
, void *data
)
1203 struct buffer_head
*bh
= NULL
;
1204 struct ocfs2_extent_block
*eb
;
1205 struct ocfs2_extent_list
*el
;
1206 struct ocfs2_extent_rec
*rec
;
1207 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1210 while (el
->l_tree_depth
) {
1211 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1212 ocfs2_error(inode
->i_sb
,
1213 "Inode %llu has empty extent list at "
1215 (unsigned long long)oi
->ip_blkno
,
1216 le16_to_cpu(el
->l_tree_depth
));
1222 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
) - 1; i
++) {
1223 rec
= &el
->l_recs
[i
];
1226 * In the case that cpos is off the allocation
1227 * tree, this should just wind up returning the
1230 range
= le32_to_cpu(rec
->e_cpos
) +
1231 ocfs2_rec_clusters(el
, rec
);
1232 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
1236 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1238 ocfs2_error(inode
->i_sb
,
1239 "Inode %llu has bad blkno in extent list "
1240 "at depth %u (index %d)\n",
1241 (unsigned long long)oi
->ip_blkno
,
1242 le16_to_cpu(el
->l_tree_depth
), i
);
1249 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
), blkno
,
1250 &bh
, OCFS2_BH_CACHED
, inode
);
1256 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1258 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
1259 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
1264 if (le16_to_cpu(el
->l_next_free_rec
) >
1265 le16_to_cpu(el
->l_count
)) {
1266 ocfs2_error(inode
->i_sb
,
1267 "Inode %llu has bad count in extent list "
1268 "at block %llu (next free=%u, count=%u)\n",
1269 (unsigned long long)oi
->ip_blkno
,
1270 (unsigned long long)bh
->b_blocknr
,
1271 le16_to_cpu(el
->l_next_free_rec
),
1272 le16_to_cpu(el
->l_count
));
1283 * Catch any trailing bh that the loop didn't handle.
1291 * Given an initialized path (that is, it has a valid root extent
1292 * list), this function will traverse the btree in search of the path
1293 * which would contain cpos.
1295 * The path traveled is recorded in the path structure.
1297 * Note that this will not do any comparisons on leaf node extent
1298 * records, so it will work fine in the case that we just added a tree
1301 struct find_path_data
{
1303 struct ocfs2_path
*path
;
1305 static void find_path_ins(void *data
, struct buffer_head
*bh
)
1307 struct find_path_data
*fp
= data
;
1310 ocfs2_path_insert_eb(fp
->path
, fp
->index
, bh
);
1313 static int ocfs2_find_path(struct inode
*inode
, struct ocfs2_path
*path
,
1316 struct find_path_data data
;
1320 return __ocfs2_find_path(inode
, path_root_el(path
), cpos
,
1321 find_path_ins
, &data
);
1324 static void find_leaf_ins(void *data
, struct buffer_head
*bh
)
1326 struct ocfs2_extent_block
*eb
=(struct ocfs2_extent_block
*)bh
->b_data
;
1327 struct ocfs2_extent_list
*el
= &eb
->h_list
;
1328 struct buffer_head
**ret
= data
;
1330 /* We want to retain only the leaf block. */
1331 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
1337 * Find the leaf block in the tree which would contain cpos. No
1338 * checking of the actual leaf is done.
1340 * Some paths want to call this instead of allocating a path structure
1341 * and calling ocfs2_find_path().
1343 * This function doesn't handle non btree extent lists.
1345 int ocfs2_find_leaf(struct inode
*inode
, struct ocfs2_extent_list
*root_el
,
1346 u32 cpos
, struct buffer_head
**leaf_bh
)
1349 struct buffer_head
*bh
= NULL
;
1351 ret
= __ocfs2_find_path(inode
, root_el
, cpos
, find_leaf_ins
, &bh
);
1363 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1365 * Basically, we've moved stuff around at the bottom of the tree and
1366 * we need to fix up the extent records above the changes to reflect
1369 * left_rec: the record on the left.
1370 * left_child_el: is the child list pointed to by left_rec
1371 * right_rec: the record to the right of left_rec
1372 * right_child_el: is the child list pointed to by right_rec
1374 * By definition, this only works on interior nodes.
1376 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec
*left_rec
,
1377 struct ocfs2_extent_list
*left_child_el
,
1378 struct ocfs2_extent_rec
*right_rec
,
1379 struct ocfs2_extent_list
*right_child_el
)
1381 u32 left_clusters
, right_end
;
1384 * Interior nodes never have holes. Their cpos is the cpos of
1385 * the leftmost record in their child list. Their cluster
1386 * count covers the full theoretical range of their child list
1387 * - the range between their cpos and the cpos of the record
1388 * immediately to their right.
1390 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[0].e_cpos
);
1391 if (ocfs2_is_empty_extent(&right_child_el
->l_recs
[0])) {
1392 BUG_ON(le16_to_cpu(right_child_el
->l_next_free_rec
) <= 1);
1393 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[1].e_cpos
);
1395 left_clusters
-= le32_to_cpu(left_rec
->e_cpos
);
1396 left_rec
->e_int_clusters
= cpu_to_le32(left_clusters
);
1399 * Calculate the rightmost cluster count boundary before
1400 * moving cpos - we will need to adjust clusters after
1401 * updating e_cpos to keep the same highest cluster count.
1403 right_end
= le32_to_cpu(right_rec
->e_cpos
);
1404 right_end
+= le32_to_cpu(right_rec
->e_int_clusters
);
1406 right_rec
->e_cpos
= left_rec
->e_cpos
;
1407 le32_add_cpu(&right_rec
->e_cpos
, left_clusters
);
1409 right_end
-= le32_to_cpu(right_rec
->e_cpos
);
1410 right_rec
->e_int_clusters
= cpu_to_le32(right_end
);
1414 * Adjust the adjacent root node records involved in a
1415 * rotation. left_el_blkno is passed in as a key so that we can easily
1416 * find it's index in the root list.
1418 static void ocfs2_adjust_root_records(struct ocfs2_extent_list
*root_el
,
1419 struct ocfs2_extent_list
*left_el
,
1420 struct ocfs2_extent_list
*right_el
,
1425 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) <=
1426 le16_to_cpu(left_el
->l_tree_depth
));
1428 for(i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
) - 1; i
++) {
1429 if (le64_to_cpu(root_el
->l_recs
[i
].e_blkno
) == left_el_blkno
)
1434 * The path walking code should have never returned a root and
1435 * two paths which are not adjacent.
1437 BUG_ON(i
>= (le16_to_cpu(root_el
->l_next_free_rec
) - 1));
1439 ocfs2_adjust_adjacent_records(&root_el
->l_recs
[i
], left_el
,
1440 &root_el
->l_recs
[i
+ 1], right_el
);
1444 * We've changed a leaf block (in right_path) and need to reflect that
1445 * change back up the subtree.
1447 * This happens in multiple places:
1448 * - When we've moved an extent record from the left path leaf to the right
1449 * path leaf to make room for an empty extent in the left path leaf.
1450 * - When our insert into the right path leaf is at the leftmost edge
1451 * and requires an update of the path immediately to it's left. This
1452 * can occur at the end of some types of rotation and appending inserts.
1453 * - When we've adjusted the last extent record in the left path leaf and the
1454 * 1st extent record in the right path leaf during cross extent block merge.
1456 static void ocfs2_complete_edge_insert(struct inode
*inode
, handle_t
*handle
,
1457 struct ocfs2_path
*left_path
,
1458 struct ocfs2_path
*right_path
,
1462 struct ocfs2_extent_list
*el
, *left_el
, *right_el
;
1463 struct ocfs2_extent_rec
*left_rec
, *right_rec
;
1464 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
1467 * Update the counts and position values within all the
1468 * interior nodes to reflect the leaf rotation we just did.
1470 * The root node is handled below the loop.
1472 * We begin the loop with right_el and left_el pointing to the
1473 * leaf lists and work our way up.
1475 * NOTE: within this loop, left_el and right_el always refer
1476 * to the *child* lists.
1478 left_el
= path_leaf_el(left_path
);
1479 right_el
= path_leaf_el(right_path
);
1480 for(i
= left_path
->p_tree_depth
- 1; i
> subtree_index
; i
--) {
1481 mlog(0, "Adjust records at index %u\n", i
);
1484 * One nice property of knowing that all of these
1485 * nodes are below the root is that we only deal with
1486 * the leftmost right node record and the rightmost
1489 el
= left_path
->p_node
[i
].el
;
1490 idx
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
1491 left_rec
= &el
->l_recs
[idx
];
1493 el
= right_path
->p_node
[i
].el
;
1494 right_rec
= &el
->l_recs
[0];
1496 ocfs2_adjust_adjacent_records(left_rec
, left_el
, right_rec
,
1499 ret
= ocfs2_journal_dirty(handle
, left_path
->p_node
[i
].bh
);
1503 ret
= ocfs2_journal_dirty(handle
, right_path
->p_node
[i
].bh
);
1508 * Setup our list pointers now so that the current
1509 * parents become children in the next iteration.
1511 left_el
= left_path
->p_node
[i
].el
;
1512 right_el
= right_path
->p_node
[i
].el
;
1516 * At the root node, adjust the two adjacent records which
1517 * begin our path to the leaves.
1520 el
= left_path
->p_node
[subtree_index
].el
;
1521 left_el
= left_path
->p_node
[subtree_index
+ 1].el
;
1522 right_el
= right_path
->p_node
[subtree_index
+ 1].el
;
1524 ocfs2_adjust_root_records(el
, left_el
, right_el
,
1525 left_path
->p_node
[subtree_index
+ 1].bh
->b_blocknr
);
1527 root_bh
= left_path
->p_node
[subtree_index
].bh
;
1529 ret
= ocfs2_journal_dirty(handle
, root_bh
);
1534 static int ocfs2_rotate_subtree_right(struct inode
*inode
,
1536 struct ocfs2_path
*left_path
,
1537 struct ocfs2_path
*right_path
,
1541 struct buffer_head
*right_leaf_bh
;
1542 struct buffer_head
*left_leaf_bh
= NULL
;
1543 struct buffer_head
*root_bh
;
1544 struct ocfs2_extent_list
*right_el
, *left_el
;
1545 struct ocfs2_extent_rec move_rec
;
1547 left_leaf_bh
= path_leaf_bh(left_path
);
1548 left_el
= path_leaf_el(left_path
);
1550 if (left_el
->l_next_free_rec
!= left_el
->l_count
) {
1551 ocfs2_error(inode
->i_sb
,
1552 "Inode %llu has non-full interior leaf node %llu"
1554 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1555 (unsigned long long)left_leaf_bh
->b_blocknr
,
1556 le16_to_cpu(left_el
->l_next_free_rec
));
1561 * This extent block may already have an empty record, so we
1562 * return early if so.
1564 if (ocfs2_is_empty_extent(&left_el
->l_recs
[0]))
1567 root_bh
= left_path
->p_node
[subtree_index
].bh
;
1568 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
1570 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
1571 OCFS2_JOURNAL_ACCESS_WRITE
);
1577 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
1578 ret
= ocfs2_journal_access(handle
, inode
,
1579 right_path
->p_node
[i
].bh
,
1580 OCFS2_JOURNAL_ACCESS_WRITE
);
1586 ret
= ocfs2_journal_access(handle
, inode
,
1587 left_path
->p_node
[i
].bh
,
1588 OCFS2_JOURNAL_ACCESS_WRITE
);
1595 right_leaf_bh
= path_leaf_bh(right_path
);
1596 right_el
= path_leaf_el(right_path
);
1598 /* This is a code error, not a disk corruption. */
1599 mlog_bug_on_msg(!right_el
->l_next_free_rec
, "Inode %llu: Rotate fails "
1600 "because rightmost leaf block %llu is empty\n",
1601 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1602 (unsigned long long)right_leaf_bh
->b_blocknr
);
1604 ocfs2_create_empty_extent(right_el
);
1606 ret
= ocfs2_journal_dirty(handle
, right_leaf_bh
);
1612 /* Do the copy now. */
1613 i
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
1614 move_rec
= left_el
->l_recs
[i
];
1615 right_el
->l_recs
[0] = move_rec
;
1618 * Clear out the record we just copied and shift everything
1619 * over, leaving an empty extent in the left leaf.
1621 * We temporarily subtract from next_free_rec so that the
1622 * shift will lose the tail record (which is now defunct).
1624 le16_add_cpu(&left_el
->l_next_free_rec
, -1);
1625 ocfs2_shift_records_right(left_el
);
1626 memset(&left_el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1627 le16_add_cpu(&left_el
->l_next_free_rec
, 1);
1629 ret
= ocfs2_journal_dirty(handle
, left_leaf_bh
);
1635 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
1643 * Given a full path, determine what cpos value would return us a path
1644 * containing the leaf immediately to the left of the current one.
1646 * Will return zero if the path passed in is already the leftmost path.
1648 static int ocfs2_find_cpos_for_left_leaf(struct super_block
*sb
,
1649 struct ocfs2_path
*path
, u32
*cpos
)
1653 struct ocfs2_extent_list
*el
;
1655 BUG_ON(path
->p_tree_depth
== 0);
1659 blkno
= path_leaf_bh(path
)->b_blocknr
;
1661 /* Start at the tree node just above the leaf and work our way up. */
1662 i
= path
->p_tree_depth
- 1;
1664 el
= path
->p_node
[i
].el
;
1667 * Find the extent record just before the one in our
1670 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
1671 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
1675 * We've determined that the
1676 * path specified is already
1677 * the leftmost one - return a
1683 * The leftmost record points to our
1684 * leaf - we need to travel up the
1690 *cpos
= le32_to_cpu(el
->l_recs
[j
- 1].e_cpos
);
1691 *cpos
= *cpos
+ ocfs2_rec_clusters(el
,
1692 &el
->l_recs
[j
- 1]);
1699 * If we got here, we never found a valid node where
1700 * the tree indicated one should be.
1703 "Invalid extent tree at extent block %llu\n",
1704 (unsigned long long)blkno
);
1709 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
1718 * Extend the transaction by enough credits to complete the rotation,
1719 * and still leave at least the original number of credits allocated
1720 * to this transaction.
1722 static int ocfs2_extend_rotate_transaction(handle_t
*handle
, int subtree_depth
,
1724 struct ocfs2_path
*path
)
1726 int credits
= (path
->p_tree_depth
- subtree_depth
) * 2 + 1 + op_credits
;
1728 if (handle
->h_buffer_credits
< credits
)
1729 return ocfs2_extend_trans(handle
, credits
);
1735 * Trap the case where we're inserting into the theoretical range past
1736 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1737 * whose cpos is less than ours into the right leaf.
1739 * It's only necessary to look at the rightmost record of the left
1740 * leaf because the logic that calls us should ensure that the
1741 * theoretical ranges in the path components above the leaves are
1744 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path
*left_path
,
1747 struct ocfs2_extent_list
*left_el
;
1748 struct ocfs2_extent_rec
*rec
;
1751 left_el
= path_leaf_el(left_path
);
1752 next_free
= le16_to_cpu(left_el
->l_next_free_rec
);
1753 rec
= &left_el
->l_recs
[next_free
- 1];
1755 if (insert_cpos
> le32_to_cpu(rec
->e_cpos
))
1760 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list
*el
, u32 cpos
)
1762 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1764 struct ocfs2_extent_rec
*rec
;
1769 rec
= &el
->l_recs
[0];
1770 if (ocfs2_is_empty_extent(rec
)) {
1774 rec
= &el
->l_recs
[1];
1777 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
1778 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
1784 * Rotate all the records in a btree right one record, starting at insert_cpos.
1786 * The path to the rightmost leaf should be passed in.
1788 * The array is assumed to be large enough to hold an entire path (tree depth).
1790 * Upon succesful return from this function:
1792 * - The 'right_path' array will contain a path to the leaf block
1793 * whose range contains e_cpos.
1794 * - That leaf block will have a single empty extent in list index 0.
1795 * - In the case that the rotation requires a post-insert update,
1796 * *ret_left_path will contain a valid path which can be passed to
1797 * ocfs2_insert_path().
1799 static int ocfs2_rotate_tree_right(struct inode
*inode
,
1801 enum ocfs2_split_type split
,
1803 struct ocfs2_path
*right_path
,
1804 struct ocfs2_path
**ret_left_path
)
1806 int ret
, start
, orig_credits
= handle
->h_buffer_credits
;
1808 struct ocfs2_path
*left_path
= NULL
;
1810 *ret_left_path
= NULL
;
1812 left_path
= ocfs2_new_path(path_root_bh(right_path
),
1813 path_root_el(right_path
));
1820 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
, &cpos
);
1826 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos
, cpos
);
1829 * What we want to do here is:
1831 * 1) Start with the rightmost path.
1833 * 2) Determine a path to the leaf block directly to the left
1836 * 3) Determine the 'subtree root' - the lowest level tree node
1837 * which contains a path to both leaves.
1839 * 4) Rotate the subtree.
1841 * 5) Find the next subtree by considering the left path to be
1842 * the new right path.
1844 * The check at the top of this while loop also accepts
1845 * insert_cpos == cpos because cpos is only a _theoretical_
1846 * value to get us the left path - insert_cpos might very well
1847 * be filling that hole.
1849 * Stop at a cpos of '0' because we either started at the
1850 * leftmost branch (i.e., a tree with one branch and a
1851 * rotation inside of it), or we've gone as far as we can in
1852 * rotating subtrees.
1854 while (cpos
&& insert_cpos
<= cpos
) {
1855 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
1858 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
1864 mlog_bug_on_msg(path_leaf_bh(left_path
) ==
1865 path_leaf_bh(right_path
),
1866 "Inode %lu: error during insert of %u "
1867 "(left path cpos %u) results in two identical "
1868 "paths ending at %llu\n",
1869 inode
->i_ino
, insert_cpos
, cpos
,
1870 (unsigned long long)
1871 path_leaf_bh(left_path
)->b_blocknr
);
1873 if (split
== SPLIT_NONE
&&
1874 ocfs2_rotate_requires_path_adjustment(left_path
,
1878 * We've rotated the tree as much as we
1879 * should. The rest is up to
1880 * ocfs2_insert_path() to complete, after the
1881 * record insertion. We indicate this
1882 * situation by returning the left path.
1884 * The reason we don't adjust the records here
1885 * before the record insert is that an error
1886 * later might break the rule where a parent
1887 * record e_cpos will reflect the actual
1888 * e_cpos of the 1st nonempty record of the
1891 *ret_left_path
= left_path
;
1895 start
= ocfs2_find_subtree_root(inode
, left_path
, right_path
);
1897 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
1899 (unsigned long long) right_path
->p_node
[start
].bh
->b_blocknr
,
1900 right_path
->p_tree_depth
);
1902 ret
= ocfs2_extend_rotate_transaction(handle
, start
,
1903 orig_credits
, right_path
);
1909 ret
= ocfs2_rotate_subtree_right(inode
, handle
, left_path
,
1916 if (split
!= SPLIT_NONE
&&
1917 ocfs2_leftmost_rec_contains(path_leaf_el(right_path
),
1920 * A rotate moves the rightmost left leaf
1921 * record over to the leftmost right leaf
1922 * slot. If we're doing an extent split
1923 * instead of a real insert, then we have to
1924 * check that the extent to be split wasn't
1925 * just moved over. If it was, then we can
1926 * exit here, passing left_path back -
1927 * ocfs2_split_extent() is smart enough to
1928 * search both leaves.
1930 *ret_left_path
= left_path
;
1935 * There is no need to re-read the next right path
1936 * as we know that it'll be our current left
1937 * path. Optimize by copying values instead.
1939 ocfs2_mv_path(right_path
, left_path
);
1941 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
1950 ocfs2_free_path(left_path
);
1956 static void ocfs2_update_edge_lengths(struct inode
*inode
, handle_t
*handle
,
1957 struct ocfs2_path
*path
)
1960 struct ocfs2_extent_rec
*rec
;
1961 struct ocfs2_extent_list
*el
;
1962 struct ocfs2_extent_block
*eb
;
1965 /* Path should always be rightmost. */
1966 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
1967 BUG_ON(eb
->h_next_leaf_blk
!= 0ULL);
1970 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
1971 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1972 rec
= &el
->l_recs
[idx
];
1973 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
1975 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
1976 el
= path
->p_node
[i
].el
;
1977 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1978 rec
= &el
->l_recs
[idx
];
1980 rec
->e_int_clusters
= cpu_to_le32(range
);
1981 le32_add_cpu(&rec
->e_int_clusters
, -le32_to_cpu(rec
->e_cpos
));
1983 ocfs2_journal_dirty(handle
, path
->p_node
[i
].bh
);
1987 static void ocfs2_unlink_path(struct inode
*inode
, handle_t
*handle
,
1988 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
1989 struct ocfs2_path
*path
, int unlink_start
)
1992 struct ocfs2_extent_block
*eb
;
1993 struct ocfs2_extent_list
*el
;
1994 struct buffer_head
*bh
;
1996 for(i
= unlink_start
; i
< path_num_items(path
); i
++) {
1997 bh
= path
->p_node
[i
].bh
;
1999 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
2001 * Not all nodes might have had their final count
2002 * decremented by the caller - handle this here.
2005 if (le16_to_cpu(el
->l_next_free_rec
) > 1) {
2007 "Inode %llu, attempted to remove extent block "
2008 "%llu with %u records\n",
2009 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2010 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
2011 le16_to_cpu(el
->l_next_free_rec
));
2013 ocfs2_journal_dirty(handle
, bh
);
2014 ocfs2_remove_from_cache(inode
, bh
);
2018 el
->l_next_free_rec
= 0;
2019 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2021 ocfs2_journal_dirty(handle
, bh
);
2023 ret
= ocfs2_cache_extent_block_free(dealloc
, eb
);
2027 ocfs2_remove_from_cache(inode
, bh
);
2031 static void ocfs2_unlink_subtree(struct inode
*inode
, handle_t
*handle
,
2032 struct ocfs2_path
*left_path
,
2033 struct ocfs2_path
*right_path
,
2035 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2038 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
2039 struct ocfs2_extent_list
*root_el
= left_path
->p_node
[subtree_index
].el
;
2040 struct ocfs2_extent_list
*el
;
2041 struct ocfs2_extent_block
*eb
;
2043 el
= path_leaf_el(left_path
);
2045 eb
= (struct ocfs2_extent_block
*)right_path
->p_node
[subtree_index
+ 1].bh
->b_data
;
2047 for(i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
2048 if (root_el
->l_recs
[i
].e_blkno
== eb
->h_blkno
)
2051 BUG_ON(i
>= le16_to_cpu(root_el
->l_next_free_rec
));
2053 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
2054 le16_add_cpu(&root_el
->l_next_free_rec
, -1);
2056 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2057 eb
->h_next_leaf_blk
= 0;
2059 ocfs2_journal_dirty(handle
, root_bh
);
2060 ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2062 ocfs2_unlink_path(inode
, handle
, dealloc
, right_path
,
2066 static int ocfs2_rotate_subtree_left(struct inode
*inode
, handle_t
*handle
,
2067 struct ocfs2_path
*left_path
,
2068 struct ocfs2_path
*right_path
,
2070 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2073 int ret
, i
, del_right_subtree
= 0, right_has_empty
= 0;
2074 struct buffer_head
*root_bh
, *di_bh
= path_root_bh(right_path
);
2075 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2076 struct ocfs2_extent_list
*right_leaf_el
, *left_leaf_el
;
2077 struct ocfs2_extent_block
*eb
;
2081 right_leaf_el
= path_leaf_el(right_path
);
2082 left_leaf_el
= path_leaf_el(left_path
);
2083 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2084 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2086 if (!ocfs2_is_empty_extent(&left_leaf_el
->l_recs
[0]))
2089 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(right_path
)->b_data
;
2090 if (ocfs2_is_empty_extent(&right_leaf_el
->l_recs
[0])) {
2092 * It's legal for us to proceed if the right leaf is
2093 * the rightmost one and it has an empty extent. There
2094 * are two cases to handle - whether the leaf will be
2095 * empty after removal or not. If the leaf isn't empty
2096 * then just remove the empty extent up front. The
2097 * next block will handle empty leaves by flagging
2100 * Non rightmost leaves will throw -EAGAIN and the
2101 * caller can manually move the subtree and retry.
2104 if (eb
->h_next_leaf_blk
!= 0ULL)
2107 if (le16_to_cpu(right_leaf_el
->l_next_free_rec
) > 1) {
2108 ret
= ocfs2_journal_access(handle
, inode
,
2109 path_leaf_bh(right_path
),
2110 OCFS2_JOURNAL_ACCESS_WRITE
);
2116 ocfs2_remove_empty_extent(right_leaf_el
);
2118 right_has_empty
= 1;
2121 if (eb
->h_next_leaf_blk
== 0ULL &&
2122 le16_to_cpu(right_leaf_el
->l_next_free_rec
) == 1) {
2124 * We have to update i_last_eb_blk during the meta
2127 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
2128 OCFS2_JOURNAL_ACCESS_WRITE
);
2134 del_right_subtree
= 1;
2138 * Getting here with an empty extent in the right path implies
2139 * that it's the rightmost path and will be deleted.
2141 BUG_ON(right_has_empty
&& !del_right_subtree
);
2143 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
2144 OCFS2_JOURNAL_ACCESS_WRITE
);
2150 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2151 ret
= ocfs2_journal_access(handle
, inode
,
2152 right_path
->p_node
[i
].bh
,
2153 OCFS2_JOURNAL_ACCESS_WRITE
);
2159 ret
= ocfs2_journal_access(handle
, inode
,
2160 left_path
->p_node
[i
].bh
,
2161 OCFS2_JOURNAL_ACCESS_WRITE
);
2168 if (!right_has_empty
) {
2170 * Only do this if we're moving a real
2171 * record. Otherwise, the action is delayed until
2172 * after removal of the right path in which case we
2173 * can do a simple shift to remove the empty extent.
2175 ocfs2_rotate_leaf(left_leaf_el
, &right_leaf_el
->l_recs
[0]);
2176 memset(&right_leaf_el
->l_recs
[0], 0,
2177 sizeof(struct ocfs2_extent_rec
));
2179 if (eb
->h_next_leaf_blk
== 0ULL) {
2181 * Move recs over to get rid of empty extent, decrease
2182 * next_free. This is allowed to remove the last
2183 * extent in our leaf (setting l_next_free_rec to
2184 * zero) - the delete code below won't care.
2186 ocfs2_remove_empty_extent(right_leaf_el
);
2189 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2192 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
2196 if (del_right_subtree
) {
2197 ocfs2_unlink_subtree(inode
, handle
, left_path
, right_path
,
2198 subtree_index
, dealloc
);
2199 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2201 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2202 di
->i_last_eb_blk
= eb
->h_blkno
;
2205 * Removal of the extent in the left leaf was skipped
2206 * above so we could delete the right path
2209 if (right_has_empty
)
2210 ocfs2_remove_empty_extent(left_leaf_el
);
2212 ret
= ocfs2_journal_dirty(handle
, di_bh
);
2218 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
2226 * Given a full path, determine what cpos value would return us a path
2227 * containing the leaf immediately to the right of the current one.
2229 * Will return zero if the path passed in is already the rightmost path.
2231 * This looks similar, but is subtly different to
2232 * ocfs2_find_cpos_for_left_leaf().
2234 static int ocfs2_find_cpos_for_right_leaf(struct super_block
*sb
,
2235 struct ocfs2_path
*path
, u32
*cpos
)
2239 struct ocfs2_extent_list
*el
;
2243 if (path
->p_tree_depth
== 0)
2246 blkno
= path_leaf_bh(path
)->b_blocknr
;
2248 /* Start at the tree node just above the leaf and work our way up. */
2249 i
= path
->p_tree_depth
- 1;
2253 el
= path
->p_node
[i
].el
;
2256 * Find the extent record just after the one in our
2259 next_free
= le16_to_cpu(el
->l_next_free_rec
);
2260 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2261 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2262 if (j
== (next_free
- 1)) {
2265 * We've determined that the
2266 * path specified is already
2267 * the rightmost one - return a
2273 * The rightmost record points to our
2274 * leaf - we need to travel up the
2280 *cpos
= le32_to_cpu(el
->l_recs
[j
+ 1].e_cpos
);
2286 * If we got here, we never found a valid node where
2287 * the tree indicated one should be.
2290 "Invalid extent tree at extent block %llu\n",
2291 (unsigned long long)blkno
);
2296 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2304 static int ocfs2_rotate_rightmost_leaf_left(struct inode
*inode
,
2306 struct buffer_head
*bh
,
2307 struct ocfs2_extent_list
*el
)
2311 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2314 ret
= ocfs2_journal_access(handle
, inode
, bh
,
2315 OCFS2_JOURNAL_ACCESS_WRITE
);
2321 ocfs2_remove_empty_extent(el
);
2323 ret
= ocfs2_journal_dirty(handle
, bh
);
2331 static int __ocfs2_rotate_tree_left(struct inode
*inode
,
2332 handle_t
*handle
, int orig_credits
,
2333 struct ocfs2_path
*path
,
2334 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2335 struct ocfs2_path
**empty_extent_path
)
2337 int ret
, subtree_root
, deleted
;
2339 struct ocfs2_path
*left_path
= NULL
;
2340 struct ocfs2_path
*right_path
= NULL
;
2342 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path
)->l_recs
[0])));
2344 *empty_extent_path
= NULL
;
2346 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, path
,
2353 left_path
= ocfs2_new_path(path_root_bh(path
),
2354 path_root_el(path
));
2361 ocfs2_cp_path(left_path
, path
);
2363 right_path
= ocfs2_new_path(path_root_bh(path
),
2364 path_root_el(path
));
2371 while (right_cpos
) {
2372 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
2378 subtree_root
= ocfs2_find_subtree_root(inode
, left_path
,
2381 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2383 (unsigned long long)
2384 right_path
->p_node
[subtree_root
].bh
->b_blocknr
,
2385 right_path
->p_tree_depth
);
2387 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_root
,
2388 orig_credits
, left_path
);
2395 * Caller might still want to make changes to the
2396 * tree root, so re-add it to the journal here.
2398 ret
= ocfs2_journal_access(handle
, inode
,
2399 path_root_bh(left_path
),
2400 OCFS2_JOURNAL_ACCESS_WRITE
);
2406 ret
= ocfs2_rotate_subtree_left(inode
, handle
, left_path
,
2407 right_path
, subtree_root
,
2409 if (ret
== -EAGAIN
) {
2411 * The rotation has to temporarily stop due to
2412 * the right subtree having an empty
2413 * extent. Pass it back to the caller for a
2416 *empty_extent_path
= right_path
;
2426 * The subtree rotate might have removed records on
2427 * the rightmost edge. If so, then rotation is
2433 ocfs2_mv_path(left_path
, right_path
);
2435 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
2444 ocfs2_free_path(right_path
);
2445 ocfs2_free_path(left_path
);
2450 static int ocfs2_remove_rightmost_path(struct inode
*inode
, handle_t
*handle
,
2451 struct ocfs2_path
*path
,
2452 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2454 int ret
, subtree_index
;
2456 struct ocfs2_path
*left_path
= NULL
;
2457 struct ocfs2_dinode
*di
;
2458 struct ocfs2_extent_block
*eb
;
2459 struct ocfs2_extent_list
*el
;
2462 * XXX: This code assumes that the root is an inode, which is
2463 * true for now but may change as tree code gets generic.
2465 di
= (struct ocfs2_dinode
*)path_root_bh(path
)->b_data
;
2466 if (!OCFS2_IS_VALID_DINODE(di
)) {
2468 ocfs2_error(inode
->i_sb
,
2469 "Inode %llu has invalid path root",
2470 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
2475 * There's two ways we handle this depending on
2476 * whether path is the only existing one.
2478 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
2479 handle
->h_buffer_credits
,
2486 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
2492 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
2500 * We have a path to the left of this one - it needs
2503 left_path
= ocfs2_new_path(path_root_bh(path
),
2504 path_root_el(path
));
2511 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2517 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
2523 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
2525 ocfs2_unlink_subtree(inode
, handle
, left_path
, path
,
2526 subtree_index
, dealloc
);
2527 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2529 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2530 di
->i_last_eb_blk
= eb
->h_blkno
;
2533 * 'path' is also the leftmost path which
2534 * means it must be the only one. This gets
2535 * handled differently because we want to
2536 * revert the inode back to having extents
2539 ocfs2_unlink_path(inode
, handle
, dealloc
, path
, 1);
2541 el
= &di
->id2
.i_list
;
2542 el
->l_tree_depth
= 0;
2543 el
->l_next_free_rec
= 0;
2544 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2546 di
->i_last_eb_blk
= 0;
2549 ocfs2_journal_dirty(handle
, path_root_bh(path
));
2552 ocfs2_free_path(left_path
);
2557 * Left rotation of btree records.
2559 * In many ways, this is (unsurprisingly) the opposite of right
2560 * rotation. We start at some non-rightmost path containing an empty
2561 * extent in the leaf block. The code works its way to the rightmost
2562 * path by rotating records to the left in every subtree.
2564 * This is used by any code which reduces the number of extent records
2565 * in a leaf. After removal, an empty record should be placed in the
2566 * leftmost list position.
2568 * This won't handle a length update of the rightmost path records if
2569 * the rightmost tree leaf record is removed so the caller is
2570 * responsible for detecting and correcting that.
2572 static int ocfs2_rotate_tree_left(struct inode
*inode
, handle_t
*handle
,
2573 struct ocfs2_path
*path
,
2574 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2576 int ret
, orig_credits
= handle
->h_buffer_credits
;
2577 struct ocfs2_path
*tmp_path
= NULL
, *restart_path
= NULL
;
2578 struct ocfs2_extent_block
*eb
;
2579 struct ocfs2_extent_list
*el
;
2581 el
= path_leaf_el(path
);
2582 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2585 if (path
->p_tree_depth
== 0) {
2586 rightmost_no_delete
:
2588 * In-inode extents. This is trivially handled, so do
2591 ret
= ocfs2_rotate_rightmost_leaf_left(inode
, handle
,
2593 path_leaf_el(path
));
2600 * Handle rightmost branch now. There's several cases:
2601 * 1) simple rotation leaving records in there. That's trivial.
2602 * 2) rotation requiring a branch delete - there's no more
2603 * records left. Two cases of this:
2604 * a) There are branches to the left.
2605 * b) This is also the leftmost (the only) branch.
2607 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2608 * 2a) we need the left branch so that we can update it with the unlink
2609 * 2b) we need to bring the inode back to inline extents.
2612 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2614 if (eb
->h_next_leaf_blk
== 0) {
2616 * This gets a bit tricky if we're going to delete the
2617 * rightmost path. Get the other cases out of the way
2620 if (le16_to_cpu(el
->l_next_free_rec
) > 1)
2621 goto rightmost_no_delete
;
2623 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
2625 ocfs2_error(inode
->i_sb
,
2626 "Inode %llu has empty extent block at %llu",
2627 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2628 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
2633 * XXX: The caller can not trust "path" any more after
2634 * this as it will have been deleted. What do we do?
2636 * In theory the rotate-for-merge code will never get
2637 * here because it'll always ask for a rotate in a
2641 ret
= ocfs2_remove_rightmost_path(inode
, handle
, path
,
2649 * Now we can loop, remembering the path we get from -EAGAIN
2650 * and restarting from there.
2653 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
, path
,
2654 dealloc
, &restart_path
);
2655 if (ret
&& ret
!= -EAGAIN
) {
2660 while (ret
== -EAGAIN
) {
2661 tmp_path
= restart_path
;
2662 restart_path
= NULL
;
2664 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
,
2667 if (ret
&& ret
!= -EAGAIN
) {
2672 ocfs2_free_path(tmp_path
);
2680 ocfs2_free_path(tmp_path
);
2681 ocfs2_free_path(restart_path
);
2685 static void ocfs2_cleanup_merge(struct ocfs2_extent_list
*el
,
2688 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[index
];
2691 if (rec
->e_leaf_clusters
== 0) {
2693 * We consumed all of the merged-from record. An empty
2694 * extent cannot exist anywhere but the 1st array
2695 * position, so move things over if the merged-from
2696 * record doesn't occupy that position.
2698 * This creates a new empty extent so the caller
2699 * should be smart enough to have removed any existing
2703 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
2704 size
= index
* sizeof(struct ocfs2_extent_rec
);
2705 memmove(&el
->l_recs
[1], &el
->l_recs
[0], size
);
2709 * Always memset - the caller doesn't check whether it
2710 * created an empty extent, so there could be junk in
2713 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2717 static int ocfs2_get_right_path(struct inode
*inode
,
2718 struct ocfs2_path
*left_path
,
2719 struct ocfs2_path
**ret_right_path
)
2723 struct ocfs2_path
*right_path
= NULL
;
2724 struct ocfs2_extent_list
*left_el
;
2726 *ret_right_path
= NULL
;
2728 /* This function shouldn't be called for non-trees. */
2729 BUG_ON(left_path
->p_tree_depth
== 0);
2731 left_el
= path_leaf_el(left_path
);
2732 BUG_ON(left_el
->l_next_free_rec
!= left_el
->l_count
);
2734 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
2741 /* This function shouldn't be called for the rightmost leaf. */
2742 BUG_ON(right_cpos
== 0);
2744 right_path
= ocfs2_new_path(path_root_bh(left_path
),
2745 path_root_el(left_path
));
2752 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
2758 *ret_right_path
= right_path
;
2761 ocfs2_free_path(right_path
);
2766 * Remove split_rec clusters from the record at index and merge them
2767 * onto the beginning of the record "next" to it.
2768 * For index < l_count - 1, the next means the extent rec at index + 1.
2769 * For index == l_count - 1, the "next" means the 1st extent rec of the
2770 * next extent block.
2772 static int ocfs2_merge_rec_right(struct inode
*inode
,
2773 struct ocfs2_path
*left_path
,
2775 struct ocfs2_extent_rec
*split_rec
,
2778 int ret
, next_free
, i
;
2779 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
2780 struct ocfs2_extent_rec
*left_rec
;
2781 struct ocfs2_extent_rec
*right_rec
;
2782 struct ocfs2_extent_list
*right_el
;
2783 struct ocfs2_path
*right_path
= NULL
;
2784 int subtree_index
= 0;
2785 struct ocfs2_extent_list
*el
= path_leaf_el(left_path
);
2786 struct buffer_head
*bh
= path_leaf_bh(left_path
);
2787 struct buffer_head
*root_bh
= NULL
;
2789 BUG_ON(index
>= le16_to_cpu(el
->l_next_free_rec
));
2790 left_rec
= &el
->l_recs
[index
];
2792 if (index
== le16_to_cpu(el
->l_next_free_rec
- 1) &&
2793 le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
)) {
2794 /* we meet with a cross extent block merge. */
2795 ret
= ocfs2_get_right_path(inode
, left_path
, &right_path
);
2801 right_el
= path_leaf_el(right_path
);
2802 next_free
= le16_to_cpu(right_el
->l_next_free_rec
);
2803 BUG_ON(next_free
<= 0);
2804 right_rec
= &right_el
->l_recs
[0];
2805 if (ocfs2_is_empty_extent(right_rec
)) {
2806 BUG_ON(le16_to_cpu(next_free
) <= 1);
2807 right_rec
= &right_el
->l_recs
[1];
2810 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
2811 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
2812 le32_to_cpu(right_rec
->e_cpos
));
2814 subtree_index
= ocfs2_find_subtree_root(inode
,
2815 left_path
, right_path
);
2817 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
2818 handle
->h_buffer_credits
,
2825 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2826 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2828 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
2829 OCFS2_JOURNAL_ACCESS_WRITE
);
2835 for (i
= subtree_index
+ 1;
2836 i
< path_num_items(right_path
); i
++) {
2837 ret
= ocfs2_journal_access(handle
, inode
,
2838 right_path
->p_node
[i
].bh
,
2839 OCFS2_JOURNAL_ACCESS_WRITE
);
2845 ret
= ocfs2_journal_access(handle
, inode
,
2846 left_path
->p_node
[i
].bh
,
2847 OCFS2_JOURNAL_ACCESS_WRITE
);
2855 BUG_ON(index
== le16_to_cpu(el
->l_next_free_rec
) - 1);
2856 right_rec
= &el
->l_recs
[index
+ 1];
2859 ret
= ocfs2_journal_access(handle
, inode
, bh
,
2860 OCFS2_JOURNAL_ACCESS_WRITE
);
2866 le16_add_cpu(&left_rec
->e_leaf_clusters
, -split_clusters
);
2868 le32_add_cpu(&right_rec
->e_cpos
, -split_clusters
);
2869 le64_add_cpu(&right_rec
->e_blkno
,
2870 -ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
2871 le16_add_cpu(&right_rec
->e_leaf_clusters
, split_clusters
);
2873 ocfs2_cleanup_merge(el
, index
);
2875 ret
= ocfs2_journal_dirty(handle
, bh
);
2880 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
2884 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
2885 right_path
, subtree_index
);
2889 ocfs2_free_path(right_path
);
2893 static int ocfs2_get_left_path(struct inode
*inode
,
2894 struct ocfs2_path
*right_path
,
2895 struct ocfs2_path
**ret_left_path
)
2899 struct ocfs2_path
*left_path
= NULL
;
2901 *ret_left_path
= NULL
;
2903 /* This function shouldn't be called for non-trees. */
2904 BUG_ON(right_path
->p_tree_depth
== 0);
2906 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
2907 right_path
, &left_cpos
);
2913 /* This function shouldn't be called for the leftmost leaf. */
2914 BUG_ON(left_cpos
== 0);
2916 left_path
= ocfs2_new_path(path_root_bh(right_path
),
2917 path_root_el(right_path
));
2924 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
2930 *ret_left_path
= left_path
;
2933 ocfs2_free_path(left_path
);
2938 * Remove split_rec clusters from the record at index and merge them
2939 * onto the tail of the record "before" it.
2940 * For index > 0, the "before" means the extent rec at index - 1.
2942 * For index == 0, the "before" means the last record of the previous
2943 * extent block. And there is also a situation that we may need to
2944 * remove the rightmost leaf extent block in the right_path and change
2945 * the right path to indicate the new rightmost path.
2947 static int ocfs2_merge_rec_left(struct inode
*inode
,
2948 struct ocfs2_path
*right_path
,
2950 struct ocfs2_extent_rec
*split_rec
,
2951 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2954 int ret
, i
, subtree_index
= 0, has_empty_extent
= 0;
2955 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
2956 struct ocfs2_extent_rec
*left_rec
;
2957 struct ocfs2_extent_rec
*right_rec
;
2958 struct ocfs2_extent_list
*el
= path_leaf_el(right_path
);
2959 struct buffer_head
*bh
= path_leaf_bh(right_path
);
2960 struct buffer_head
*root_bh
= NULL
;
2961 struct ocfs2_path
*left_path
= NULL
;
2962 struct ocfs2_extent_list
*left_el
;
2966 right_rec
= &el
->l_recs
[index
];
2968 /* we meet with a cross extent block merge. */
2969 ret
= ocfs2_get_left_path(inode
, right_path
, &left_path
);
2975 left_el
= path_leaf_el(left_path
);
2976 BUG_ON(le16_to_cpu(left_el
->l_next_free_rec
) !=
2977 le16_to_cpu(left_el
->l_count
));
2979 left_rec
= &left_el
->l_recs
[
2980 le16_to_cpu(left_el
->l_next_free_rec
) - 1];
2981 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
2982 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
2983 le32_to_cpu(split_rec
->e_cpos
));
2985 subtree_index
= ocfs2_find_subtree_root(inode
,
2986 left_path
, right_path
);
2988 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
2989 handle
->h_buffer_credits
,
2996 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2997 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2999 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
3000 OCFS2_JOURNAL_ACCESS_WRITE
);
3006 for (i
= subtree_index
+ 1;
3007 i
< path_num_items(right_path
); i
++) {
3008 ret
= ocfs2_journal_access(handle
, inode
,
3009 right_path
->p_node
[i
].bh
,
3010 OCFS2_JOURNAL_ACCESS_WRITE
);
3016 ret
= ocfs2_journal_access(handle
, inode
,
3017 left_path
->p_node
[i
].bh
,
3018 OCFS2_JOURNAL_ACCESS_WRITE
);
3025 left_rec
= &el
->l_recs
[index
- 1];
3026 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
3027 has_empty_extent
= 1;
3030 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3031 OCFS2_JOURNAL_ACCESS_WRITE
);
3037 if (has_empty_extent
&& index
== 1) {
3039 * The easy case - we can just plop the record right in.
3041 *left_rec
= *split_rec
;
3043 has_empty_extent
= 0;
3045 le16_add_cpu(&left_rec
->e_leaf_clusters
, split_clusters
);
3047 le32_add_cpu(&right_rec
->e_cpos
, split_clusters
);
3048 le64_add_cpu(&right_rec
->e_blkno
,
3049 ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3050 le16_add_cpu(&right_rec
->e_leaf_clusters
, -split_clusters
);
3052 ocfs2_cleanup_merge(el
, index
);
3054 ret
= ocfs2_journal_dirty(handle
, bh
);
3059 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
3064 * In the situation that the right_rec is empty and the extent
3065 * block is empty also, ocfs2_complete_edge_insert can't handle
3066 * it and we need to delete the right extent block.
3068 if (le16_to_cpu(right_rec
->e_leaf_clusters
) == 0 &&
3069 le16_to_cpu(el
->l_next_free_rec
) == 1) {
3071 ret
= ocfs2_remove_rightmost_path(inode
, handle
,
3072 right_path
, dealloc
);
3078 /* Now the rightmost extent block has been deleted.
3079 * So we use the new rightmost path.
3081 ocfs2_mv_path(right_path
, left_path
);
3084 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3085 right_path
, subtree_index
);
3089 ocfs2_free_path(left_path
);
3093 static int ocfs2_try_to_merge_extent(struct inode
*inode
,
3095 struct ocfs2_path
*path
,
3097 struct ocfs2_extent_rec
*split_rec
,
3098 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3099 struct ocfs2_merge_ctxt
*ctxt
)
3103 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
3104 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
3106 BUG_ON(ctxt
->c_contig_type
== CONTIG_NONE
);
3108 if (ctxt
->c_split_covers_rec
&& ctxt
->c_has_empty_extent
) {
3110 * The merge code will need to create an empty
3111 * extent to take the place of the newly
3112 * emptied slot. Remove any pre-existing empty
3113 * extents - having more than one in a leaf is
3116 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3123 rec
= &el
->l_recs
[split_index
];
3126 if (ctxt
->c_contig_type
== CONTIG_LEFTRIGHT
) {
3128 * Left-right contig implies this.
3130 BUG_ON(!ctxt
->c_split_covers_rec
);
3133 * Since the leftright insert always covers the entire
3134 * extent, this call will delete the insert record
3135 * entirely, resulting in an empty extent record added to
3138 * Since the adding of an empty extent shifts
3139 * everything back to the right, there's no need to
3140 * update split_index here.
3142 * When the split_index is zero, we need to merge it to the
3143 * prevoius extent block. It is more efficient and easier
3144 * if we do merge_right first and merge_left later.
3146 ret
= ocfs2_merge_rec_right(inode
, path
,
3155 * We can only get this from logic error above.
3157 BUG_ON(!ocfs2_is_empty_extent(&el
->l_recs
[0]));
3159 /* The merge left us with an empty extent, remove it. */
3160 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
);
3166 rec
= &el
->l_recs
[split_index
];
3169 * Note that we don't pass split_rec here on purpose -
3170 * we've merged it into the rec already.
3172 ret
= ocfs2_merge_rec_left(inode
, path
,
3182 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3185 * Error from this last rotate is not critical, so
3186 * print but don't bubble it up.
3193 * Merge a record to the left or right.
3195 * 'contig_type' is relative to the existing record,
3196 * so for example, if we're "right contig", it's to
3197 * the record on the left (hence the left merge).
3199 if (ctxt
->c_contig_type
== CONTIG_RIGHT
) {
3200 ret
= ocfs2_merge_rec_left(inode
,
3210 ret
= ocfs2_merge_rec_right(inode
,
3220 if (ctxt
->c_split_covers_rec
) {
3222 * The merge may have left an empty extent in
3223 * our leaf. Try to rotate it away.
3225 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3237 static void ocfs2_subtract_from_rec(struct super_block
*sb
,
3238 enum ocfs2_split_type split
,
3239 struct ocfs2_extent_rec
*rec
,
3240 struct ocfs2_extent_rec
*split_rec
)
3244 len_blocks
= ocfs2_clusters_to_blocks(sb
,
3245 le16_to_cpu(split_rec
->e_leaf_clusters
));
3247 if (split
== SPLIT_LEFT
) {
3249 * Region is on the left edge of the existing
3252 le32_add_cpu(&rec
->e_cpos
,
3253 le16_to_cpu(split_rec
->e_leaf_clusters
));
3254 le64_add_cpu(&rec
->e_blkno
, len_blocks
);
3255 le16_add_cpu(&rec
->e_leaf_clusters
,
3256 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3259 * Region is on the right edge of the existing
3262 le16_add_cpu(&rec
->e_leaf_clusters
,
3263 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3268 * Do the final bits of extent record insertion at the target leaf
3269 * list. If this leaf is part of an allocation tree, it is assumed
3270 * that the tree above has been prepared.
3272 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec
*insert_rec
,
3273 struct ocfs2_extent_list
*el
,
3274 struct ocfs2_insert_type
*insert
,
3275 struct inode
*inode
)
3277 int i
= insert
->ins_contig_index
;
3279 struct ocfs2_extent_rec
*rec
;
3281 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
3283 if (insert
->ins_split
!= SPLIT_NONE
) {
3284 i
= ocfs2_search_extent_list(el
, le32_to_cpu(insert_rec
->e_cpos
));
3286 rec
= &el
->l_recs
[i
];
3287 ocfs2_subtract_from_rec(inode
->i_sb
, insert
->ins_split
, rec
,
3293 * Contiguous insert - either left or right.
3295 if (insert
->ins_contig
!= CONTIG_NONE
) {
3296 rec
= &el
->l_recs
[i
];
3297 if (insert
->ins_contig
== CONTIG_LEFT
) {
3298 rec
->e_blkno
= insert_rec
->e_blkno
;
3299 rec
->e_cpos
= insert_rec
->e_cpos
;
3301 le16_add_cpu(&rec
->e_leaf_clusters
,
3302 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3307 * Handle insert into an empty leaf.
3309 if (le16_to_cpu(el
->l_next_free_rec
) == 0 ||
3310 ((le16_to_cpu(el
->l_next_free_rec
) == 1) &&
3311 ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3312 el
->l_recs
[0] = *insert_rec
;
3313 el
->l_next_free_rec
= cpu_to_le16(1);
3320 if (insert
->ins_appending
== APPEND_TAIL
) {
3321 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
3322 rec
= &el
->l_recs
[i
];
3323 range
= le32_to_cpu(rec
->e_cpos
)
3324 + le16_to_cpu(rec
->e_leaf_clusters
);
3325 BUG_ON(le32_to_cpu(insert_rec
->e_cpos
) < range
);
3327 mlog_bug_on_msg(le16_to_cpu(el
->l_next_free_rec
) >=
3328 le16_to_cpu(el
->l_count
),
3329 "inode %lu, depth %u, count %u, next free %u, "
3330 "rec.cpos %u, rec.clusters %u, "
3331 "insert.cpos %u, insert.clusters %u\n",
3333 le16_to_cpu(el
->l_tree_depth
),
3334 le16_to_cpu(el
->l_count
),
3335 le16_to_cpu(el
->l_next_free_rec
),
3336 le32_to_cpu(el
->l_recs
[i
].e_cpos
),
3337 le16_to_cpu(el
->l_recs
[i
].e_leaf_clusters
),
3338 le32_to_cpu(insert_rec
->e_cpos
),
3339 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3341 el
->l_recs
[i
] = *insert_rec
;
3342 le16_add_cpu(&el
->l_next_free_rec
, 1);
3348 * Ok, we have to rotate.
3350 * At this point, it is safe to assume that inserting into an
3351 * empty leaf and appending to a leaf have both been handled
3354 * This leaf needs to have space, either by the empty 1st
3355 * extent record, or by virtue of an l_next_rec < l_count.
3357 ocfs2_rotate_leaf(el
, insert_rec
);
3360 static inline void ocfs2_update_dinode_clusters(struct inode
*inode
,
3361 struct ocfs2_dinode
*di
,
3364 le32_add_cpu(&di
->i_clusters
, clusters
);
3365 spin_lock(&OCFS2_I(inode
)->ip_lock
);
3366 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(di
->i_clusters
);
3367 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
3370 static void ocfs2_adjust_rightmost_records(struct inode
*inode
,
3372 struct ocfs2_path
*path
,
3373 struct ocfs2_extent_rec
*insert_rec
)
3375 int ret
, i
, next_free
;
3376 struct buffer_head
*bh
;
3377 struct ocfs2_extent_list
*el
;
3378 struct ocfs2_extent_rec
*rec
;
3381 * Update everything except the leaf block.
3383 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
3384 bh
= path
->p_node
[i
].bh
;
3385 el
= path
->p_node
[i
].el
;
3387 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3388 if (next_free
== 0) {
3389 ocfs2_error(inode
->i_sb
,
3390 "Dinode %llu has a bad extent list",
3391 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3396 rec
= &el
->l_recs
[next_free
- 1];
3398 rec
->e_int_clusters
= insert_rec
->e_cpos
;
3399 le32_add_cpu(&rec
->e_int_clusters
,
3400 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3401 le32_add_cpu(&rec
->e_int_clusters
,
3402 -le32_to_cpu(rec
->e_cpos
));
3404 ret
= ocfs2_journal_dirty(handle
, bh
);
3411 static int ocfs2_append_rec_to_path(struct inode
*inode
, handle_t
*handle
,
3412 struct ocfs2_extent_rec
*insert_rec
,
3413 struct ocfs2_path
*right_path
,
3414 struct ocfs2_path
**ret_left_path
)
3417 struct ocfs2_extent_list
*el
;
3418 struct ocfs2_path
*left_path
= NULL
;
3420 *ret_left_path
= NULL
;
3423 * This shouldn't happen for non-trees. The extent rec cluster
3424 * count manipulation below only works for interior nodes.
3426 BUG_ON(right_path
->p_tree_depth
== 0);
3429 * If our appending insert is at the leftmost edge of a leaf,
3430 * then we might need to update the rightmost records of the
3433 el
= path_leaf_el(right_path
);
3434 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3435 if (next_free
== 0 ||
3436 (next_free
== 1 && ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3439 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
3446 mlog(0, "Append may need a left path update. cpos: %u, "
3447 "left_cpos: %u\n", le32_to_cpu(insert_rec
->e_cpos
),
3451 * No need to worry if the append is already in the
3455 left_path
= ocfs2_new_path(path_root_bh(right_path
),
3456 path_root_el(right_path
));
3463 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3470 * ocfs2_insert_path() will pass the left_path to the
3476 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
3482 ocfs2_adjust_rightmost_records(inode
, handle
, right_path
, insert_rec
);
3484 *ret_left_path
= left_path
;
3488 ocfs2_free_path(left_path
);
3493 static void ocfs2_split_record(struct inode
*inode
,
3494 struct ocfs2_path
*left_path
,
3495 struct ocfs2_path
*right_path
,
3496 struct ocfs2_extent_rec
*split_rec
,
3497 enum ocfs2_split_type split
)
3500 u32 cpos
= le32_to_cpu(split_rec
->e_cpos
);
3501 struct ocfs2_extent_list
*left_el
= NULL
, *right_el
, *insert_el
, *el
;
3502 struct ocfs2_extent_rec
*rec
, *tmprec
;
3504 right_el
= path_leaf_el(right_path
);;
3506 left_el
= path_leaf_el(left_path
);
3509 insert_el
= right_el
;
3510 index
= ocfs2_search_extent_list(el
, cpos
);
3512 if (index
== 0 && left_path
) {
3513 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3516 * This typically means that the record
3517 * started in the left path but moved to the
3518 * right as a result of rotation. We either
3519 * move the existing record to the left, or we
3520 * do the later insert there.
3522 * In this case, the left path should always
3523 * exist as the rotate code will have passed
3524 * it back for a post-insert update.
3527 if (split
== SPLIT_LEFT
) {
3529 * It's a left split. Since we know
3530 * that the rotate code gave us an
3531 * empty extent in the left path, we
3532 * can just do the insert there.
3534 insert_el
= left_el
;
3537 * Right split - we have to move the
3538 * existing record over to the left
3539 * leaf. The insert will be into the
3540 * newly created empty extent in the
3543 tmprec
= &right_el
->l_recs
[index
];
3544 ocfs2_rotate_leaf(left_el
, tmprec
);
3547 memset(tmprec
, 0, sizeof(*tmprec
));
3548 index
= ocfs2_search_extent_list(left_el
, cpos
);
3549 BUG_ON(index
== -1);
3554 BUG_ON(!ocfs2_is_empty_extent(&left_el
->l_recs
[0]));
3556 * Left path is easy - we can just allow the insert to
3560 insert_el
= left_el
;
3561 index
= ocfs2_search_extent_list(el
, cpos
);
3562 BUG_ON(index
== -1);
3565 rec
= &el
->l_recs
[index
];
3566 ocfs2_subtract_from_rec(inode
->i_sb
, split
, rec
, split_rec
);
3567 ocfs2_rotate_leaf(insert_el
, split_rec
);
3571 * This function only does inserts on an allocation b-tree. For dinode
3572 * lists, ocfs2_insert_at_leaf() is called directly.
3574 * right_path is the path we want to do the actual insert
3575 * in. left_path should only be passed in if we need to update that
3576 * portion of the tree after an edge insert.
3578 static int ocfs2_insert_path(struct inode
*inode
,
3580 struct ocfs2_path
*left_path
,
3581 struct ocfs2_path
*right_path
,
3582 struct ocfs2_extent_rec
*insert_rec
,
3583 struct ocfs2_insert_type
*insert
)
3585 int ret
, subtree_index
;
3586 struct buffer_head
*leaf_bh
= path_leaf_bh(right_path
);
3589 int credits
= handle
->h_buffer_credits
;
3592 * There's a chance that left_path got passed back to
3593 * us without being accounted for in the
3594 * journal. Extend our transaction here to be sure we
3595 * can change those blocks.
3597 credits
+= left_path
->p_tree_depth
;
3599 ret
= ocfs2_extend_trans(handle
, credits
);
3605 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
3613 * Pass both paths to the journal. The majority of inserts
3614 * will be touching all components anyway.
3616 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
3622 if (insert
->ins_split
!= SPLIT_NONE
) {
3624 * We could call ocfs2_insert_at_leaf() for some types
3625 * of splits, but it's easier to just let one separate
3626 * function sort it all out.
3628 ocfs2_split_record(inode
, left_path
, right_path
,
3629 insert_rec
, insert
->ins_split
);
3632 * Split might have modified either leaf and we don't
3633 * have a guarantee that the later edge insert will
3634 * dirty this for us.
3637 ret
= ocfs2_journal_dirty(handle
,
3638 path_leaf_bh(left_path
));
3642 ocfs2_insert_at_leaf(insert_rec
, path_leaf_el(right_path
),
3645 ret
= ocfs2_journal_dirty(handle
, leaf_bh
);
3651 * The rotate code has indicated that we need to fix
3652 * up portions of the tree after the insert.
3654 * XXX: Should we extend the transaction here?
3656 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
,
3658 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3659 right_path
, subtree_index
);
3667 static int ocfs2_do_insert_extent(struct inode
*inode
,
3669 struct buffer_head
*di_bh
,
3670 struct ocfs2_extent_rec
*insert_rec
,
3671 struct ocfs2_insert_type
*type
)
3673 int ret
, rotate
= 0;
3675 struct ocfs2_path
*right_path
= NULL
;
3676 struct ocfs2_path
*left_path
= NULL
;
3677 struct ocfs2_dinode
*di
;
3678 struct ocfs2_extent_list
*el
;
3680 di
= (struct ocfs2_dinode
*) di_bh
->b_data
;
3681 el
= &di
->id2
.i_list
;
3683 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
3684 OCFS2_JOURNAL_ACCESS_WRITE
);
3690 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
3691 ocfs2_insert_at_leaf(insert_rec
, el
, type
, inode
);
3692 goto out_update_clusters
;
3695 right_path
= ocfs2_new_inode_path(di_bh
);
3703 * Determine the path to start with. Rotations need the
3704 * rightmost path, everything else can go directly to the
3707 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
3708 if (type
->ins_appending
== APPEND_NONE
&&
3709 type
->ins_contig
== CONTIG_NONE
) {
3714 ret
= ocfs2_find_path(inode
, right_path
, cpos
);
3721 * Rotations and appends need special treatment - they modify
3722 * parts of the tree's above them.
3724 * Both might pass back a path immediate to the left of the
3725 * one being inserted to. This will be cause
3726 * ocfs2_insert_path() to modify the rightmost records of
3727 * left_path to account for an edge insert.
3729 * XXX: When modifying this code, keep in mind that an insert
3730 * can wind up skipping both of these two special cases...
3733 ret
= ocfs2_rotate_tree_right(inode
, handle
, type
->ins_split
,
3734 le32_to_cpu(insert_rec
->e_cpos
),
3735 right_path
, &left_path
);
3742 * ocfs2_rotate_tree_right() might have extended the
3743 * transaction without re-journaling our tree root.
3745 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
3746 OCFS2_JOURNAL_ACCESS_WRITE
);
3751 } else if (type
->ins_appending
== APPEND_TAIL
3752 && type
->ins_contig
!= CONTIG_LEFT
) {
3753 ret
= ocfs2_append_rec_to_path(inode
, handle
, insert_rec
,
3754 right_path
, &left_path
);
3761 ret
= ocfs2_insert_path(inode
, handle
, left_path
, right_path
,
3768 out_update_clusters
:
3769 if (type
->ins_split
== SPLIT_NONE
)
3770 ocfs2_update_dinode_clusters(inode
, di
,
3771 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3773 ret
= ocfs2_journal_dirty(handle
, di_bh
);
3778 ocfs2_free_path(left_path
);
3779 ocfs2_free_path(right_path
);
3784 static enum ocfs2_contig_type
3785 ocfs2_figure_merge_contig_type(struct inode
*inode
,
3786 struct ocfs2_extent_list
*el
, int index
,
3787 struct ocfs2_extent_rec
*split_rec
)
3789 struct ocfs2_extent_rec
*rec
;
3790 enum ocfs2_contig_type ret
= CONTIG_NONE
;
3793 * We're careful to check for an empty extent record here -
3794 * the merge code will know what to do if it sees one.
3798 rec
= &el
->l_recs
[index
- 1];
3799 if (index
== 1 && ocfs2_is_empty_extent(rec
)) {
3800 if (split_rec
->e_cpos
== el
->l_recs
[index
].e_cpos
)
3803 ret
= ocfs2_extent_contig(inode
, rec
, split_rec
);
3807 if (index
< (le16_to_cpu(el
->l_next_free_rec
) - 1)) {
3808 enum ocfs2_contig_type contig_type
;
3810 rec
= &el
->l_recs
[index
+ 1];
3811 contig_type
= ocfs2_extent_contig(inode
, rec
, split_rec
);
3813 if (contig_type
== CONTIG_LEFT
&& ret
== CONTIG_RIGHT
)
3814 ret
= CONTIG_LEFTRIGHT
;
3815 else if (ret
== CONTIG_NONE
)
3822 static void ocfs2_figure_contig_type(struct inode
*inode
,
3823 struct ocfs2_insert_type
*insert
,
3824 struct ocfs2_extent_list
*el
,
3825 struct ocfs2_extent_rec
*insert_rec
)
3828 enum ocfs2_contig_type contig_type
= CONTIG_NONE
;
3830 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
3832 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
3833 contig_type
= ocfs2_extent_contig(inode
, &el
->l_recs
[i
],
3835 if (contig_type
!= CONTIG_NONE
) {
3836 insert
->ins_contig_index
= i
;
3840 insert
->ins_contig
= contig_type
;
3844 * This should only be called against the righmost leaf extent list.
3846 * ocfs2_figure_appending_type() will figure out whether we'll have to
3847 * insert at the tail of the rightmost leaf.
3849 * This should also work against the dinode list for tree's with 0
3850 * depth. If we consider the dinode list to be the rightmost leaf node
3851 * then the logic here makes sense.
3853 static void ocfs2_figure_appending_type(struct ocfs2_insert_type
*insert
,
3854 struct ocfs2_extent_list
*el
,
3855 struct ocfs2_extent_rec
*insert_rec
)
3858 u32 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
3859 struct ocfs2_extent_rec
*rec
;
3861 insert
->ins_appending
= APPEND_NONE
;
3863 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
3865 if (!el
->l_next_free_rec
)
3866 goto set_tail_append
;
3868 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
3869 /* Were all records empty? */
3870 if (le16_to_cpu(el
->l_next_free_rec
) == 1)
3871 goto set_tail_append
;
3874 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
3875 rec
= &el
->l_recs
[i
];
3878 (le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)))
3879 goto set_tail_append
;
3884 insert
->ins_appending
= APPEND_TAIL
;
3888 * Helper function called at the begining of an insert.
3890 * This computes a few things that are commonly used in the process of
3891 * inserting into the btree:
3892 * - Whether the new extent is contiguous with an existing one.
3893 * - The current tree depth.
3894 * - Whether the insert is an appending one.
3895 * - The total # of free records in the tree.
3897 * All of the information is stored on the ocfs2_insert_type
3900 static int ocfs2_figure_insert_type(struct inode
*inode
,
3901 struct buffer_head
*di_bh
,
3902 struct buffer_head
**last_eb_bh
,
3903 struct ocfs2_extent_rec
*insert_rec
,
3905 struct ocfs2_insert_type
*insert
)
3908 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3909 struct ocfs2_extent_block
*eb
;
3910 struct ocfs2_extent_list
*el
;
3911 struct ocfs2_path
*path
= NULL
;
3912 struct buffer_head
*bh
= NULL
;
3914 insert
->ins_split
= SPLIT_NONE
;
3916 el
= &di
->id2
.i_list
;
3917 insert
->ins_tree_depth
= le16_to_cpu(el
->l_tree_depth
);
3919 if (el
->l_tree_depth
) {
3921 * If we have tree depth, we read in the
3922 * rightmost extent block ahead of time as
3923 * ocfs2_figure_insert_type() and ocfs2_add_branch()
3924 * may want it later.
3926 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
),
3927 le64_to_cpu(di
->i_last_eb_blk
), &bh
,
3928 OCFS2_BH_CACHED
, inode
);
3933 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
3938 * Unless we have a contiguous insert, we'll need to know if
3939 * there is room left in our allocation tree for another
3942 * XXX: This test is simplistic, we can search for empty
3943 * extent records too.
3945 *free_records
= le16_to_cpu(el
->l_count
) -
3946 le16_to_cpu(el
->l_next_free_rec
);
3948 if (!insert
->ins_tree_depth
) {
3949 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
);
3950 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
3954 path
= ocfs2_new_inode_path(di_bh
);
3962 * In the case that we're inserting past what the tree
3963 * currently accounts for, ocfs2_find_path() will return for
3964 * us the rightmost tree path. This is accounted for below in
3965 * the appending code.
3967 ret
= ocfs2_find_path(inode
, path
, le32_to_cpu(insert_rec
->e_cpos
));
3973 el
= path_leaf_el(path
);
3976 * Now that we have the path, there's two things we want to determine:
3977 * 1) Contiguousness (also set contig_index if this is so)
3979 * 2) Are we doing an append? We can trivially break this up
3980 * into two types of appends: simple record append, or a
3981 * rotate inside the tail leaf.
3983 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
);
3986 * The insert code isn't quite ready to deal with all cases of
3987 * left contiguousness. Specifically, if it's an insert into
3988 * the 1st record in a leaf, it will require the adjustment of
3989 * cluster count on the last record of the path directly to it's
3990 * left. For now, just catch that case and fool the layers
3991 * above us. This works just fine for tree_depth == 0, which
3992 * is why we allow that above.
3994 if (insert
->ins_contig
== CONTIG_LEFT
&&
3995 insert
->ins_contig_index
== 0)
3996 insert
->ins_contig
= CONTIG_NONE
;
3999 * Ok, so we can simply compare against last_eb to figure out
4000 * whether the path doesn't exist. This will only happen in
4001 * the case that we're doing a tail append, so maybe we can
4002 * take advantage of that information somehow.
4004 if (le64_to_cpu(di
->i_last_eb_blk
) == path_leaf_bh(path
)->b_blocknr
) {
4006 * Ok, ocfs2_find_path() returned us the rightmost
4007 * tree path. This might be an appending insert. There are
4009 * 1) We're doing a true append at the tail:
4010 * -This might even be off the end of the leaf
4011 * 2) We're "appending" by rotating in the tail
4013 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4017 ocfs2_free_path(path
);
4027 * Insert an extent into an inode btree.
4029 * The caller needs to update fe->i_clusters
4031 int ocfs2_insert_extent(struct ocfs2_super
*osb
,
4033 struct inode
*inode
,
4034 struct buffer_head
*fe_bh
,
4039 struct ocfs2_alloc_context
*meta_ac
)
4042 int uninitialized_var(free_records
);
4043 struct buffer_head
*last_eb_bh
= NULL
;
4044 struct ocfs2_insert_type insert
= {0, };
4045 struct ocfs2_extent_rec rec
;
4047 BUG_ON(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
);
4049 mlog(0, "add %u clusters at position %u to inode %llu\n",
4050 new_clusters
, cpos
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4052 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb
) &&
4053 (OCFS2_I(inode
)->ip_clusters
!= cpos
),
4054 "Device %s, asking for sparse allocation: inode %llu, "
4055 "cpos %u, clusters %u\n",
4057 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
,
4058 OCFS2_I(inode
)->ip_clusters
);
4060 memset(&rec
, 0, sizeof(rec
));
4061 rec
.e_cpos
= cpu_to_le32(cpos
);
4062 rec
.e_blkno
= cpu_to_le64(start_blk
);
4063 rec
.e_leaf_clusters
= cpu_to_le16(new_clusters
);
4064 rec
.e_flags
= flags
;
4066 status
= ocfs2_figure_insert_type(inode
, fe_bh
, &last_eb_bh
, &rec
,
4067 &free_records
, &insert
);
4073 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4074 "Insert.contig_index: %d, Insert.free_records: %d, "
4075 "Insert.tree_depth: %d\n",
4076 insert
.ins_appending
, insert
.ins_contig
, insert
.ins_contig_index
,
4077 free_records
, insert
.ins_tree_depth
);
4079 if (insert
.ins_contig
== CONTIG_NONE
&& free_records
== 0) {
4080 status
= ocfs2_grow_tree(inode
, handle
, fe_bh
,
4081 &insert
.ins_tree_depth
, &last_eb_bh
,
4089 /* Finally, we can add clusters. This might rotate the tree for us. */
4090 status
= ocfs2_do_insert_extent(inode
, handle
, fe_bh
, &rec
, &insert
);
4094 ocfs2_extent_map_insert_rec(inode
, &rec
);
4104 static void ocfs2_make_right_split_rec(struct super_block
*sb
,
4105 struct ocfs2_extent_rec
*split_rec
,
4107 struct ocfs2_extent_rec
*rec
)
4109 u32 rec_cpos
= le32_to_cpu(rec
->e_cpos
);
4110 u32 rec_range
= rec_cpos
+ le16_to_cpu(rec
->e_leaf_clusters
);
4112 memset(split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4114 split_rec
->e_cpos
= cpu_to_le32(cpos
);
4115 split_rec
->e_leaf_clusters
= cpu_to_le16(rec_range
- cpos
);
4117 split_rec
->e_blkno
= rec
->e_blkno
;
4118 le64_add_cpu(&split_rec
->e_blkno
,
4119 ocfs2_clusters_to_blocks(sb
, cpos
- rec_cpos
));
4121 split_rec
->e_flags
= rec
->e_flags
;
4124 static int ocfs2_split_and_insert(struct inode
*inode
,
4126 struct ocfs2_path
*path
,
4127 struct buffer_head
*di_bh
,
4128 struct buffer_head
**last_eb_bh
,
4130 struct ocfs2_extent_rec
*orig_split_rec
,
4131 struct ocfs2_alloc_context
*meta_ac
)
4134 unsigned int insert_range
, rec_range
, do_leftright
= 0;
4135 struct ocfs2_extent_rec tmprec
;
4136 struct ocfs2_extent_list
*rightmost_el
;
4137 struct ocfs2_extent_rec rec
;
4138 struct ocfs2_extent_rec split_rec
= *orig_split_rec
;
4139 struct ocfs2_insert_type insert
;
4140 struct ocfs2_extent_block
*eb
;
4141 struct ocfs2_dinode
*di
;
4145 * Store a copy of the record on the stack - it might move
4146 * around as the tree is manipulated below.
4148 rec
= path_leaf_el(path
)->l_recs
[split_index
];
4150 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
4151 rightmost_el
= &di
->id2
.i_list
;
4153 depth
= le16_to_cpu(rightmost_el
->l_tree_depth
);
4155 BUG_ON(!(*last_eb_bh
));
4156 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
4157 rightmost_el
= &eb
->h_list
;
4160 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4161 le16_to_cpu(rightmost_el
->l_count
)) {
4162 ret
= ocfs2_grow_tree(inode
, handle
, di_bh
, &depth
, last_eb_bh
,
4170 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4171 insert
.ins_appending
= APPEND_NONE
;
4172 insert
.ins_contig
= CONTIG_NONE
;
4173 insert
.ins_tree_depth
= depth
;
4175 insert_range
= le32_to_cpu(split_rec
.e_cpos
) +
4176 le16_to_cpu(split_rec
.e_leaf_clusters
);
4177 rec_range
= le32_to_cpu(rec
.e_cpos
) +
4178 le16_to_cpu(rec
.e_leaf_clusters
);
4180 if (split_rec
.e_cpos
== rec
.e_cpos
) {
4181 insert
.ins_split
= SPLIT_LEFT
;
4182 } else if (insert_range
== rec_range
) {
4183 insert
.ins_split
= SPLIT_RIGHT
;
4186 * Left/right split. We fake this as a right split
4187 * first and then make a second pass as a left split.
4189 insert
.ins_split
= SPLIT_RIGHT
;
4191 ocfs2_make_right_split_rec(inode
->i_sb
, &tmprec
, insert_range
,
4196 BUG_ON(do_leftright
);
4200 ret
= ocfs2_do_insert_extent(inode
, handle
, di_bh
, &split_rec
,
4207 if (do_leftright
== 1) {
4209 struct ocfs2_extent_list
*el
;
4212 split_rec
= *orig_split_rec
;
4214 ocfs2_reinit_path(path
, 1);
4216 cpos
= le32_to_cpu(split_rec
.e_cpos
);
4217 ret
= ocfs2_find_path(inode
, path
, cpos
);
4223 el
= path_leaf_el(path
);
4224 split_index
= ocfs2_search_extent_list(el
, cpos
);
4233 * Mark part or all of the extent record at split_index in the leaf
4234 * pointed to by path as written. This removes the unwritten
4237 * Care is taken to handle contiguousness so as to not grow the tree.
4239 * meta_ac is not strictly necessary - we only truly need it if growth
4240 * of the tree is required. All other cases will degrade into a less
4241 * optimal tree layout.
4243 * last_eb_bh should be the rightmost leaf block for any inode with a
4244 * btree. Since a split may grow the tree or a merge might shrink it, the caller cannot trust the contents of that buffer after this call.
4246 * This code is optimized for readability - several passes might be
4247 * made over certain portions of the tree. All of those blocks will
4248 * have been brought into cache (and pinned via the journal), so the
4249 * extra overhead is not expressed in terms of disk reads.
4251 static int __ocfs2_mark_extent_written(struct inode
*inode
,
4252 struct buffer_head
*di_bh
,
4254 struct ocfs2_path
*path
,
4256 struct ocfs2_extent_rec
*split_rec
,
4257 struct ocfs2_alloc_context
*meta_ac
,
4258 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
4261 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
4262 struct buffer_head
*last_eb_bh
= NULL
;
4263 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
4264 struct ocfs2_merge_ctxt ctxt
;
4265 struct ocfs2_extent_list
*rightmost_el
;
4267 if (!(rec
->e_flags
& OCFS2_EXT_UNWRITTEN
)) {
4273 if (le32_to_cpu(rec
->e_cpos
) > le32_to_cpu(split_rec
->e_cpos
) ||
4274 ((le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)) <
4275 (le32_to_cpu(split_rec
->e_cpos
) + le16_to_cpu(split_rec
->e_leaf_clusters
)))) {
4281 ctxt
.c_contig_type
= ocfs2_figure_merge_contig_type(inode
, el
,
4286 * The core merge / split code wants to know how much room is
4287 * left in this inodes allocation tree, so we pass the
4288 * rightmost extent list.
4290 if (path
->p_tree_depth
) {
4291 struct ocfs2_extent_block
*eb
;
4292 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
4294 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
),
4295 le64_to_cpu(di
->i_last_eb_blk
),
4296 &last_eb_bh
, OCFS2_BH_CACHED
, inode
);
4302 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
4303 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
4304 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
4309 rightmost_el
= &eb
->h_list
;
4311 rightmost_el
= path_root_el(path
);
4313 if (rec
->e_cpos
== split_rec
->e_cpos
&&
4314 rec
->e_leaf_clusters
== split_rec
->e_leaf_clusters
)
4315 ctxt
.c_split_covers_rec
= 1;
4317 ctxt
.c_split_covers_rec
= 0;
4319 ctxt
.c_has_empty_extent
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
4321 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4322 split_index
, ctxt
.c_contig_type
, ctxt
.c_has_empty_extent
,
4323 ctxt
.c_split_covers_rec
);
4325 if (ctxt
.c_contig_type
== CONTIG_NONE
) {
4326 if (ctxt
.c_split_covers_rec
)
4327 el
->l_recs
[split_index
] = *split_rec
;
4329 ret
= ocfs2_split_and_insert(inode
, handle
, path
, di_bh
,
4330 &last_eb_bh
, split_index
,
4331 split_rec
, meta_ac
);
4335 ret
= ocfs2_try_to_merge_extent(inode
, handle
, path
,
4336 split_index
, split_rec
,
4348 * Mark the already-existing extent at cpos as written for len clusters.
4350 * If the existing extent is larger than the request, initiate a
4351 * split. An attempt will be made at merging with adjacent extents.
4353 * The caller is responsible for passing down meta_ac if we'll need it.
4355 int ocfs2_mark_extent_written(struct inode
*inode
, struct buffer_head
*di_bh
,
4356 handle_t
*handle
, u32 cpos
, u32 len
, u32 phys
,
4357 struct ocfs2_alloc_context
*meta_ac
,
4358 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
4361 u64 start_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys
);
4362 struct ocfs2_extent_rec split_rec
;
4363 struct ocfs2_path
*left_path
= NULL
;
4364 struct ocfs2_extent_list
*el
;
4366 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4367 inode
->i_ino
, cpos
, len
, phys
, (unsigned long long)start_blkno
);
4369 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode
->i_sb
))) {
4370 ocfs2_error(inode
->i_sb
, "Inode %llu has unwritten extents "
4371 "that are being written to, but the feature bit "
4372 "is not set in the super block.",
4373 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4379 * XXX: This should be fixed up so that we just re-insert the
4380 * next extent records.
4382 ocfs2_extent_map_trunc(inode
, 0);
4384 left_path
= ocfs2_new_inode_path(di_bh
);
4391 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
4396 el
= path_leaf_el(left_path
);
4398 index
= ocfs2_search_extent_list(el
, cpos
);
4399 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
4400 ocfs2_error(inode
->i_sb
,
4401 "Inode %llu has an extent at cpos %u which can no "
4402 "longer be found.\n",
4403 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
4408 memset(&split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4409 split_rec
.e_cpos
= cpu_to_le32(cpos
);
4410 split_rec
.e_leaf_clusters
= cpu_to_le16(len
);
4411 split_rec
.e_blkno
= cpu_to_le64(start_blkno
);
4412 split_rec
.e_flags
= path_leaf_el(left_path
)->l_recs
[index
].e_flags
;
4413 split_rec
.e_flags
&= ~OCFS2_EXT_UNWRITTEN
;
4415 ret
= __ocfs2_mark_extent_written(inode
, di_bh
, handle
, left_path
,
4416 index
, &split_rec
, meta_ac
, dealloc
);
4421 ocfs2_free_path(left_path
);
4425 static int ocfs2_split_tree(struct inode
*inode
, struct buffer_head
*di_bh
,
4426 handle_t
*handle
, struct ocfs2_path
*path
,
4427 int index
, u32 new_range
,
4428 struct ocfs2_alloc_context
*meta_ac
)
4430 int ret
, depth
, credits
= handle
->h_buffer_credits
;
4431 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
4432 struct buffer_head
*last_eb_bh
= NULL
;
4433 struct ocfs2_extent_block
*eb
;
4434 struct ocfs2_extent_list
*rightmost_el
, *el
;
4435 struct ocfs2_extent_rec split_rec
;
4436 struct ocfs2_extent_rec
*rec
;
4437 struct ocfs2_insert_type insert
;
4440 * Setup the record to split before we grow the tree.
4442 el
= path_leaf_el(path
);
4443 rec
= &el
->l_recs
[index
];
4444 ocfs2_make_right_split_rec(inode
->i_sb
, &split_rec
, new_range
, rec
);
4446 depth
= path
->p_tree_depth
;
4448 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
),
4449 le64_to_cpu(di
->i_last_eb_blk
),
4450 &last_eb_bh
, OCFS2_BH_CACHED
, inode
);
4456 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
4457 rightmost_el
= &eb
->h_list
;
4459 rightmost_el
= path_leaf_el(path
);
4461 credits
+= path
->p_tree_depth
+ ocfs2_extend_meta_needed(di
);
4462 ret
= ocfs2_extend_trans(handle
, credits
);
4468 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4469 le16_to_cpu(rightmost_el
->l_count
)) {
4470 ret
= ocfs2_grow_tree(inode
, handle
, di_bh
, &depth
, &last_eb_bh
,
4478 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4479 insert
.ins_appending
= APPEND_NONE
;
4480 insert
.ins_contig
= CONTIG_NONE
;
4481 insert
.ins_split
= SPLIT_RIGHT
;
4482 insert
.ins_tree_depth
= depth
;
4484 ret
= ocfs2_do_insert_extent(inode
, handle
, di_bh
, &split_rec
, &insert
);
4493 static int ocfs2_truncate_rec(struct inode
*inode
, handle_t
*handle
,
4494 struct ocfs2_path
*path
, int index
,
4495 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
4499 u32 left_cpos
, rec_range
, trunc_range
;
4500 int wants_rotate
= 0, is_rightmost_tree_rec
= 0;
4501 struct super_block
*sb
= inode
->i_sb
;
4502 struct ocfs2_path
*left_path
= NULL
;
4503 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
4504 struct ocfs2_extent_rec
*rec
;
4505 struct ocfs2_extent_block
*eb
;
4507 if (ocfs2_is_empty_extent(&el
->l_recs
[0]) && index
> 0) {
4508 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
);
4517 if (index
== (le16_to_cpu(el
->l_next_free_rec
) - 1) &&
4518 path
->p_tree_depth
) {
4520 * Check whether this is the rightmost tree record. If
4521 * we remove all of this record or part of its right
4522 * edge then an update of the record lengths above it
4525 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
4526 if (eb
->h_next_leaf_blk
== 0)
4527 is_rightmost_tree_rec
= 1;
4530 rec
= &el
->l_recs
[index
];
4531 if (index
== 0 && path
->p_tree_depth
&&
4532 le32_to_cpu(rec
->e_cpos
) == cpos
) {
4534 * Changing the leftmost offset (via partial or whole
4535 * record truncate) of an interior (or rightmost) path
4536 * means we have to update the subtree that is formed
4537 * by this leaf and the one to it's left.
4539 * There are two cases we can skip:
4540 * 1) Path is the leftmost one in our inode tree.
4541 * 2) The leaf is rightmost and will be empty after
4542 * we remove the extent record - the rotate code
4543 * knows how to update the newly formed edge.
4546 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
,
4553 if (left_cpos
&& le16_to_cpu(el
->l_next_free_rec
) > 1) {
4554 left_path
= ocfs2_new_path(path_root_bh(path
),
4555 path_root_el(path
));
4562 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
4570 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
4571 handle
->h_buffer_credits
,
4578 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
4584 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
4590 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
4591 trunc_range
= cpos
+ len
;
4593 if (le32_to_cpu(rec
->e_cpos
) == cpos
&& rec_range
== trunc_range
) {
4596 memset(rec
, 0, sizeof(*rec
));
4597 ocfs2_cleanup_merge(el
, index
);
4600 next_free
= le16_to_cpu(el
->l_next_free_rec
);
4601 if (is_rightmost_tree_rec
&& next_free
> 1) {
4603 * We skip the edge update if this path will
4604 * be deleted by the rotate code.
4606 rec
= &el
->l_recs
[next_free
- 1];
4607 ocfs2_adjust_rightmost_records(inode
, handle
, path
,
4610 } else if (le32_to_cpu(rec
->e_cpos
) == cpos
) {
4611 /* Remove leftmost portion of the record. */
4612 le32_add_cpu(&rec
->e_cpos
, len
);
4613 le64_add_cpu(&rec
->e_blkno
, ocfs2_clusters_to_blocks(sb
, len
));
4614 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
4615 } else if (rec_range
== trunc_range
) {
4616 /* Remove rightmost portion of the record */
4617 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
4618 if (is_rightmost_tree_rec
)
4619 ocfs2_adjust_rightmost_records(inode
, handle
, path
, rec
);
4621 /* Caller should have trapped this. */
4622 mlog(ML_ERROR
, "Inode %llu: Invalid record truncate: (%u, %u) "
4623 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
4624 le32_to_cpu(rec
->e_cpos
),
4625 le16_to_cpu(rec
->e_leaf_clusters
), cpos
, len
);
4632 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
4633 ocfs2_complete_edge_insert(inode
, handle
, left_path
, path
,
4637 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
4639 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
);
4646 ocfs2_free_path(left_path
);
4650 int ocfs2_remove_extent(struct inode
*inode
, struct buffer_head
*di_bh
,
4651 u32 cpos
, u32 len
, handle_t
*handle
,
4652 struct ocfs2_alloc_context
*meta_ac
,
4653 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
4656 u32 rec_range
, trunc_range
;
4657 struct ocfs2_extent_rec
*rec
;
4658 struct ocfs2_extent_list
*el
;
4659 struct ocfs2_path
*path
;
4661 ocfs2_extent_map_trunc(inode
, 0);
4663 path
= ocfs2_new_inode_path(di_bh
);
4670 ret
= ocfs2_find_path(inode
, path
, cpos
);
4676 el
= path_leaf_el(path
);
4677 index
= ocfs2_search_extent_list(el
, cpos
);
4678 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
4679 ocfs2_error(inode
->i_sb
,
4680 "Inode %llu has an extent at cpos %u which can no "
4681 "longer be found.\n",
4682 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
4688 * We have 3 cases of extent removal:
4689 * 1) Range covers the entire extent rec
4690 * 2) Range begins or ends on one edge of the extent rec
4691 * 3) Range is in the middle of the extent rec (no shared edges)
4693 * For case 1 we remove the extent rec and left rotate to
4696 * For case 2 we just shrink the existing extent rec, with a
4697 * tree update if the shrinking edge is also the edge of an
4700 * For case 3 we do a right split to turn the extent rec into
4701 * something case 2 can handle.
4703 rec
= &el
->l_recs
[index
];
4704 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
4705 trunc_range
= cpos
+ len
;
4707 BUG_ON(cpos
< le32_to_cpu(rec
->e_cpos
) || trunc_range
> rec_range
);
4709 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
4710 "(cpos %u, len %u)\n",
4711 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
, len
, index
,
4712 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
));
4714 if (le32_to_cpu(rec
->e_cpos
) == cpos
|| rec_range
== trunc_range
) {
4715 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
4722 ret
= ocfs2_split_tree(inode
, di_bh
, handle
, path
, index
,
4723 trunc_range
, meta_ac
);
4730 * The split could have manipulated the tree enough to
4731 * move the record location, so we have to look for it again.
4733 ocfs2_reinit_path(path
, 1);
4735 ret
= ocfs2_find_path(inode
, path
, cpos
);
4741 el
= path_leaf_el(path
);
4742 index
= ocfs2_search_extent_list(el
, cpos
);
4743 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
4744 ocfs2_error(inode
->i_sb
,
4745 "Inode %llu: split at cpos %u lost record.",
4746 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
4753 * Double check our values here. If anything is fishy,
4754 * it's easier to catch it at the top level.
4756 rec
= &el
->l_recs
[index
];
4757 rec_range
= le32_to_cpu(rec
->e_cpos
) +
4758 ocfs2_rec_clusters(el
, rec
);
4759 if (rec_range
!= trunc_range
) {
4760 ocfs2_error(inode
->i_sb
,
4761 "Inode %llu: error after split at cpos %u"
4762 "trunc len %u, existing record is (%u,%u)",
4763 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
4764 cpos
, len
, le32_to_cpu(rec
->e_cpos
),
4765 ocfs2_rec_clusters(el
, rec
));
4770 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
4779 ocfs2_free_path(path
);
4783 int ocfs2_truncate_log_needs_flush(struct ocfs2_super
*osb
)
4785 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
4786 struct ocfs2_dinode
*di
;
4787 struct ocfs2_truncate_log
*tl
;
4789 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
4790 tl
= &di
->id2
.i_dealloc
;
4792 mlog_bug_on_msg(le16_to_cpu(tl
->tl_used
) > le16_to_cpu(tl
->tl_count
),
4793 "slot %d, invalid truncate log parameters: used = "
4794 "%u, count = %u\n", osb
->slot_num
,
4795 le16_to_cpu(tl
->tl_used
), le16_to_cpu(tl
->tl_count
));
4796 return le16_to_cpu(tl
->tl_used
) == le16_to_cpu(tl
->tl_count
);
4799 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log
*tl
,
4800 unsigned int new_start
)
4802 unsigned int tail_index
;
4803 unsigned int current_tail
;
4805 /* No records, nothing to coalesce */
4806 if (!le16_to_cpu(tl
->tl_used
))
4809 tail_index
= le16_to_cpu(tl
->tl_used
) - 1;
4810 current_tail
= le32_to_cpu(tl
->tl_recs
[tail_index
].t_start
);
4811 current_tail
+= le32_to_cpu(tl
->tl_recs
[tail_index
].t_clusters
);
4813 return current_tail
== new_start
;
4816 int ocfs2_truncate_log_append(struct ocfs2_super
*osb
,
4819 unsigned int num_clusters
)
4822 unsigned int start_cluster
, tl_count
;
4823 struct inode
*tl_inode
= osb
->osb_tl_inode
;
4824 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
4825 struct ocfs2_dinode
*di
;
4826 struct ocfs2_truncate_log
*tl
;
4828 mlog_entry("start_blk = %llu, num_clusters = %u\n",
4829 (unsigned long long)start_blk
, num_clusters
);
4831 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
4833 start_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, start_blk
);
4835 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
4836 tl
= &di
->id2
.i_dealloc
;
4837 if (!OCFS2_IS_VALID_DINODE(di
)) {
4838 OCFS2_RO_ON_INVALID_DINODE(osb
->sb
, di
);
4843 tl_count
= le16_to_cpu(tl
->tl_count
);
4844 mlog_bug_on_msg(tl_count
> ocfs2_truncate_recs_per_inode(osb
->sb
) ||
4846 "Truncate record count on #%llu invalid "
4847 "wanted %u, actual %u\n",
4848 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
,
4849 ocfs2_truncate_recs_per_inode(osb
->sb
),
4850 le16_to_cpu(tl
->tl_count
));
4852 /* Caller should have known to flush before calling us. */
4853 index
= le16_to_cpu(tl
->tl_used
);
4854 if (index
>= tl_count
) {
4860 status
= ocfs2_journal_access(handle
, tl_inode
, tl_bh
,
4861 OCFS2_JOURNAL_ACCESS_WRITE
);
4867 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
4868 "%llu (index = %d)\n", num_clusters
, start_cluster
,
4869 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
, index
);
4871 if (ocfs2_truncate_log_can_coalesce(tl
, start_cluster
)) {
4873 * Move index back to the record we are coalescing with.
4874 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
4878 num_clusters
+= le32_to_cpu(tl
->tl_recs
[index
].t_clusters
);
4879 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
4880 index
, le32_to_cpu(tl
->tl_recs
[index
].t_start
),
4883 tl
->tl_recs
[index
].t_start
= cpu_to_le32(start_cluster
);
4884 tl
->tl_used
= cpu_to_le16(index
+ 1);
4886 tl
->tl_recs
[index
].t_clusters
= cpu_to_le32(num_clusters
);
4888 status
= ocfs2_journal_dirty(handle
, tl_bh
);
4899 static int ocfs2_replay_truncate_records(struct ocfs2_super
*osb
,
4901 struct inode
*data_alloc_inode
,
4902 struct buffer_head
*data_alloc_bh
)
4906 unsigned int num_clusters
;
4908 struct ocfs2_truncate_rec rec
;
4909 struct ocfs2_dinode
*di
;
4910 struct ocfs2_truncate_log
*tl
;
4911 struct inode
*tl_inode
= osb
->osb_tl_inode
;
4912 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
4916 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
4917 tl
= &di
->id2
.i_dealloc
;
4918 i
= le16_to_cpu(tl
->tl_used
) - 1;
4920 /* Caller has given us at least enough credits to
4921 * update the truncate log dinode */
4922 status
= ocfs2_journal_access(handle
, tl_inode
, tl_bh
,
4923 OCFS2_JOURNAL_ACCESS_WRITE
);
4929 tl
->tl_used
= cpu_to_le16(i
);
4931 status
= ocfs2_journal_dirty(handle
, tl_bh
);
4937 /* TODO: Perhaps we can calculate the bulk of the
4938 * credits up front rather than extending like
4940 status
= ocfs2_extend_trans(handle
,
4941 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC
);
4947 rec
= tl
->tl_recs
[i
];
4948 start_blk
= ocfs2_clusters_to_blocks(data_alloc_inode
->i_sb
,
4949 le32_to_cpu(rec
.t_start
));
4950 num_clusters
= le32_to_cpu(rec
.t_clusters
);
4952 /* if start_blk is not set, we ignore the record as
4955 mlog(0, "free record %d, start = %u, clusters = %u\n",
4956 i
, le32_to_cpu(rec
.t_start
), num_clusters
);
4958 status
= ocfs2_free_clusters(handle
, data_alloc_inode
,
4959 data_alloc_bh
, start_blk
,
4974 /* Expects you to already be holding tl_inode->i_mutex */
4975 int __ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
4978 unsigned int num_to_flush
;
4980 struct inode
*tl_inode
= osb
->osb_tl_inode
;
4981 struct inode
*data_alloc_inode
= NULL
;
4982 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
4983 struct buffer_head
*data_alloc_bh
= NULL
;
4984 struct ocfs2_dinode
*di
;
4985 struct ocfs2_truncate_log
*tl
;
4989 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
4991 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
4992 tl
= &di
->id2
.i_dealloc
;
4993 if (!OCFS2_IS_VALID_DINODE(di
)) {
4994 OCFS2_RO_ON_INVALID_DINODE(osb
->sb
, di
);
4999 num_to_flush
= le16_to_cpu(tl
->tl_used
);
5000 mlog(0, "Flush %u records from truncate log #%llu\n",
5001 num_to_flush
, (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
);
5002 if (!num_to_flush
) {
5007 data_alloc_inode
= ocfs2_get_system_file_inode(osb
,
5008 GLOBAL_BITMAP_SYSTEM_INODE
,
5009 OCFS2_INVALID_SLOT
);
5010 if (!data_alloc_inode
) {
5012 mlog(ML_ERROR
, "Could not get bitmap inode!\n");
5016 mutex_lock(&data_alloc_inode
->i_mutex
);
5018 status
= ocfs2_inode_lock(data_alloc_inode
, &data_alloc_bh
, 1);
5024 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5025 if (IS_ERR(handle
)) {
5026 status
= PTR_ERR(handle
);
5031 status
= ocfs2_replay_truncate_records(osb
, handle
, data_alloc_inode
,
5036 ocfs2_commit_trans(osb
, handle
);
5039 brelse(data_alloc_bh
);
5040 ocfs2_inode_unlock(data_alloc_inode
, 1);
5043 mutex_unlock(&data_alloc_inode
->i_mutex
);
5044 iput(data_alloc_inode
);
5051 int ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5054 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5056 mutex_lock(&tl_inode
->i_mutex
);
5057 status
= __ocfs2_flush_truncate_log(osb
);
5058 mutex_unlock(&tl_inode
->i_mutex
);
5063 static void ocfs2_truncate_log_worker(struct work_struct
*work
)
5066 struct ocfs2_super
*osb
=
5067 container_of(work
, struct ocfs2_super
,
5068 osb_truncate_log_wq
.work
);
5072 status
= ocfs2_flush_truncate_log(osb
);
5079 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5080 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super
*osb
,
5083 if (osb
->osb_tl_inode
) {
5084 /* We want to push off log flushes while truncates are
5087 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5089 queue_delayed_work(ocfs2_wq
, &osb
->osb_truncate_log_wq
,
5090 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL
);
5094 static int ocfs2_get_truncate_log_info(struct ocfs2_super
*osb
,
5096 struct inode
**tl_inode
,
5097 struct buffer_head
**tl_bh
)
5100 struct inode
*inode
= NULL
;
5101 struct buffer_head
*bh
= NULL
;
5103 inode
= ocfs2_get_system_file_inode(osb
,
5104 TRUNCATE_LOG_SYSTEM_INODE
,
5108 mlog(ML_ERROR
, "Could not get load truncate log inode!\n");
5112 status
= ocfs2_read_block(osb
, OCFS2_I(inode
)->ip_blkno
, &bh
,
5113 OCFS2_BH_CACHED
, inode
);
5127 /* called during the 1st stage of node recovery. we stamp a clean
5128 * truncate log and pass back a copy for processing later. if the
5129 * truncate log does not require processing, a *tl_copy is set to
5131 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super
*osb
,
5133 struct ocfs2_dinode
**tl_copy
)
5136 struct inode
*tl_inode
= NULL
;
5137 struct buffer_head
*tl_bh
= NULL
;
5138 struct ocfs2_dinode
*di
;
5139 struct ocfs2_truncate_log
*tl
;
5143 mlog(0, "recover truncate log from slot %d\n", slot_num
);
5145 status
= ocfs2_get_truncate_log_info(osb
, slot_num
, &tl_inode
, &tl_bh
);
5151 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5152 tl
= &di
->id2
.i_dealloc
;
5153 if (!OCFS2_IS_VALID_DINODE(di
)) {
5154 OCFS2_RO_ON_INVALID_DINODE(tl_inode
->i_sb
, di
);
5159 if (le16_to_cpu(tl
->tl_used
)) {
5160 mlog(0, "We'll have %u logs to recover\n",
5161 le16_to_cpu(tl
->tl_used
));
5163 *tl_copy
= kmalloc(tl_bh
->b_size
, GFP_KERNEL
);
5170 /* Assuming the write-out below goes well, this copy
5171 * will be passed back to recovery for processing. */
5172 memcpy(*tl_copy
, tl_bh
->b_data
, tl_bh
->b_size
);
5174 /* All we need to do to clear the truncate log is set
5178 status
= ocfs2_write_block(osb
, tl_bh
, tl_inode
);
5191 if (status
< 0 && (*tl_copy
)) {
5200 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super
*osb
,
5201 struct ocfs2_dinode
*tl_copy
)
5205 unsigned int clusters
, num_recs
, start_cluster
;
5208 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5209 struct ocfs2_truncate_log
*tl
;
5213 if (OCFS2_I(tl_inode
)->ip_blkno
== le64_to_cpu(tl_copy
->i_blkno
)) {
5214 mlog(ML_ERROR
, "Asked to recover my own truncate log!\n");
5218 tl
= &tl_copy
->id2
.i_dealloc
;
5219 num_recs
= le16_to_cpu(tl
->tl_used
);
5220 mlog(0, "cleanup %u records from %llu\n", num_recs
,
5221 (unsigned long long)le64_to_cpu(tl_copy
->i_blkno
));
5223 mutex_lock(&tl_inode
->i_mutex
);
5224 for(i
= 0; i
< num_recs
; i
++) {
5225 if (ocfs2_truncate_log_needs_flush(osb
)) {
5226 status
= __ocfs2_flush_truncate_log(osb
);
5233 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5234 if (IS_ERR(handle
)) {
5235 status
= PTR_ERR(handle
);
5240 clusters
= le32_to_cpu(tl
->tl_recs
[i
].t_clusters
);
5241 start_cluster
= le32_to_cpu(tl
->tl_recs
[i
].t_start
);
5242 start_blk
= ocfs2_clusters_to_blocks(osb
->sb
, start_cluster
);
5244 status
= ocfs2_truncate_log_append(osb
, handle
,
5245 start_blk
, clusters
);
5246 ocfs2_commit_trans(osb
, handle
);
5254 mutex_unlock(&tl_inode
->i_mutex
);
5260 void ocfs2_truncate_log_shutdown(struct ocfs2_super
*osb
)
5263 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5268 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5269 flush_workqueue(ocfs2_wq
);
5271 status
= ocfs2_flush_truncate_log(osb
);
5275 brelse(osb
->osb_tl_bh
);
5276 iput(osb
->osb_tl_inode
);
5282 int ocfs2_truncate_log_init(struct ocfs2_super
*osb
)
5285 struct inode
*tl_inode
= NULL
;
5286 struct buffer_head
*tl_bh
= NULL
;
5290 status
= ocfs2_get_truncate_log_info(osb
,
5297 /* ocfs2_truncate_log_shutdown keys on the existence of
5298 * osb->osb_tl_inode so we don't set any of the osb variables
5299 * until we're sure all is well. */
5300 INIT_DELAYED_WORK(&osb
->osb_truncate_log_wq
,
5301 ocfs2_truncate_log_worker
);
5302 osb
->osb_tl_bh
= tl_bh
;
5303 osb
->osb_tl_inode
= tl_inode
;
5310 * Delayed de-allocation of suballocator blocks.
5312 * Some sets of block de-allocations might involve multiple suballocator inodes.
5314 * The locking for this can get extremely complicated, especially when
5315 * the suballocator inodes to delete from aren't known until deep
5316 * within an unrelated codepath.
5318 * ocfs2_extent_block structures are a good example of this - an inode
5319 * btree could have been grown by any number of nodes each allocating
5320 * out of their own suballoc inode.
5322 * These structures allow the delay of block de-allocation until a
5323 * later time, when locking of multiple cluster inodes won't cause
5328 * Describes a single block free from a suballocator
5330 struct ocfs2_cached_block_free
{
5331 struct ocfs2_cached_block_free
*free_next
;
5333 unsigned int free_bit
;
5336 struct ocfs2_per_slot_free_list
{
5337 struct ocfs2_per_slot_free_list
*f_next_suballocator
;
5340 struct ocfs2_cached_block_free
*f_first
;
5343 static int ocfs2_free_cached_items(struct ocfs2_super
*osb
,
5346 struct ocfs2_cached_block_free
*head
)
5351 struct inode
*inode
;
5352 struct buffer_head
*di_bh
= NULL
;
5353 struct ocfs2_cached_block_free
*tmp
;
5355 inode
= ocfs2_get_system_file_inode(osb
, sysfile_type
, slot
);
5362 mutex_lock(&inode
->i_mutex
);
5364 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
5370 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
5371 if (IS_ERR(handle
)) {
5372 ret
= PTR_ERR(handle
);
5378 bg_blkno
= ocfs2_which_suballoc_group(head
->free_blk
,
5380 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5381 head
->free_bit
, (unsigned long long)head
->free_blk
);
5383 ret
= ocfs2_free_suballoc_bits(handle
, inode
, di_bh
,
5384 head
->free_bit
, bg_blkno
, 1);
5390 ret
= ocfs2_extend_trans(handle
, OCFS2_SUBALLOC_FREE
);
5397 head
= head
->free_next
;
5402 ocfs2_commit_trans(osb
, handle
);
5405 ocfs2_inode_unlock(inode
, 1);
5408 mutex_unlock(&inode
->i_mutex
);
5412 /* Premature exit may have left some dangling items. */
5414 head
= head
->free_next
;
5421 int ocfs2_run_deallocs(struct ocfs2_super
*osb
,
5422 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
5425 struct ocfs2_per_slot_free_list
*fl
;
5430 while (ctxt
->c_first_suballocator
) {
5431 fl
= ctxt
->c_first_suballocator
;
5434 mlog(0, "Free items: (type %u, slot %d)\n",
5435 fl
->f_inode_type
, fl
->f_slot
);
5436 ret2
= ocfs2_free_cached_items(osb
, fl
->f_inode_type
,
5437 fl
->f_slot
, fl
->f_first
);
5444 ctxt
->c_first_suballocator
= fl
->f_next_suballocator
;
5451 static struct ocfs2_per_slot_free_list
*
5452 ocfs2_find_per_slot_free_list(int type
,
5454 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
5456 struct ocfs2_per_slot_free_list
*fl
= ctxt
->c_first_suballocator
;
5459 if (fl
->f_inode_type
== type
&& fl
->f_slot
== slot
)
5462 fl
= fl
->f_next_suballocator
;
5465 fl
= kmalloc(sizeof(*fl
), GFP_NOFS
);
5467 fl
->f_inode_type
= type
;
5470 fl
->f_next_suballocator
= ctxt
->c_first_suballocator
;
5472 ctxt
->c_first_suballocator
= fl
;
5477 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
5478 int type
, int slot
, u64 blkno
,
5482 struct ocfs2_per_slot_free_list
*fl
;
5483 struct ocfs2_cached_block_free
*item
;
5485 fl
= ocfs2_find_per_slot_free_list(type
, slot
, ctxt
);
5492 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
5499 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
5500 type
, slot
, bit
, (unsigned long long)blkno
);
5502 item
->free_blk
= blkno
;
5503 item
->free_bit
= bit
;
5504 item
->free_next
= fl
->f_first
;
5513 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
5514 struct ocfs2_extent_block
*eb
)
5516 return ocfs2_cache_block_dealloc(ctxt
, EXTENT_ALLOC_SYSTEM_INODE
,
5517 le16_to_cpu(eb
->h_suballoc_slot
),
5518 le64_to_cpu(eb
->h_blkno
),
5519 le16_to_cpu(eb
->h_suballoc_bit
));
5522 /* This function will figure out whether the currently last extent
5523 * block will be deleted, and if it will, what the new last extent
5524 * block will be so we can update his h_next_leaf_blk field, as well
5525 * as the dinodes i_last_eb_blk */
5526 static int ocfs2_find_new_last_ext_blk(struct inode
*inode
,
5527 unsigned int clusters_to_del
,
5528 struct ocfs2_path
*path
,
5529 struct buffer_head
**new_last_eb
)
5531 int next_free
, ret
= 0;
5533 struct ocfs2_extent_rec
*rec
;
5534 struct ocfs2_extent_block
*eb
;
5535 struct ocfs2_extent_list
*el
;
5536 struct buffer_head
*bh
= NULL
;
5538 *new_last_eb
= NULL
;
5540 /* we have no tree, so of course, no last_eb. */
5541 if (!path
->p_tree_depth
)
5544 /* trunc to zero special case - this makes tree_depth = 0
5545 * regardless of what it is. */
5546 if (OCFS2_I(inode
)->ip_clusters
== clusters_to_del
)
5549 el
= path_leaf_el(path
);
5550 BUG_ON(!el
->l_next_free_rec
);
5553 * Make sure that this extent list will actually be empty
5554 * after we clear away the data. We can shortcut out if
5555 * there's more than one non-empty extent in the
5556 * list. Otherwise, a check of the remaining extent is
5559 next_free
= le16_to_cpu(el
->l_next_free_rec
);
5561 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
5565 /* We may have a valid extent in index 1, check it. */
5567 rec
= &el
->l_recs
[1];
5570 * Fall through - no more nonempty extents, so we want
5571 * to delete this leaf.
5577 rec
= &el
->l_recs
[0];
5582 * Check it we'll only be trimming off the end of this
5585 if (le16_to_cpu(rec
->e_leaf_clusters
) > clusters_to_del
)
5589 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
5595 ret
= ocfs2_find_leaf(inode
, path_root_el(path
), cpos
, &bh
);
5601 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
5603 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
5604 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
5610 get_bh(*new_last_eb
);
5611 mlog(0, "returning block %llu, (cpos: %u)\n",
5612 (unsigned long long)le64_to_cpu(eb
->h_blkno
), cpos
);
5620 * Trim some clusters off the rightmost edge of a tree. Only called
5623 * The caller needs to:
5624 * - start journaling of each path component.
5625 * - compute and fully set up any new last ext block
5627 static int ocfs2_trim_tree(struct inode
*inode
, struct ocfs2_path
*path
,
5628 handle_t
*handle
, struct ocfs2_truncate_context
*tc
,
5629 u32 clusters_to_del
, u64
*delete_start
)
5631 int ret
, i
, index
= path
->p_tree_depth
;
5634 struct buffer_head
*bh
;
5635 struct ocfs2_extent_list
*el
;
5636 struct ocfs2_extent_rec
*rec
;
5640 while (index
>= 0) {
5641 bh
= path
->p_node
[index
].bh
;
5642 el
= path
->p_node
[index
].el
;
5644 mlog(0, "traveling tree (index = %d, block = %llu)\n",
5645 index
, (unsigned long long)bh
->b_blocknr
);
5647 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
5650 (path
->p_tree_depth
- le16_to_cpu(el
->l_tree_depth
))) {
5651 ocfs2_error(inode
->i_sb
,
5652 "Inode %lu has invalid ext. block %llu",
5654 (unsigned long long)bh
->b_blocknr
);
5660 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
5661 rec
= &el
->l_recs
[i
];
5663 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
5664 "next = %u\n", i
, le32_to_cpu(rec
->e_cpos
),
5665 ocfs2_rec_clusters(el
, rec
),
5666 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
5667 le16_to_cpu(el
->l_next_free_rec
));
5669 BUG_ON(ocfs2_rec_clusters(el
, rec
) < clusters_to_del
);
5671 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
5673 * If the leaf block contains a single empty
5674 * extent and no records, we can just remove
5677 if (i
== 0 && ocfs2_is_empty_extent(rec
)) {
5679 sizeof(struct ocfs2_extent_rec
));
5680 el
->l_next_free_rec
= cpu_to_le16(0);
5686 * Remove any empty extents by shifting things
5687 * left. That should make life much easier on
5688 * the code below. This condition is rare
5689 * enough that we shouldn't see a performance
5692 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
5693 le16_add_cpu(&el
->l_next_free_rec
, -1);
5696 i
< le16_to_cpu(el
->l_next_free_rec
); i
++)
5697 el
->l_recs
[i
] = el
->l_recs
[i
+ 1];
5699 memset(&el
->l_recs
[i
], 0,
5700 sizeof(struct ocfs2_extent_rec
));
5703 * We've modified our extent list. The
5704 * simplest way to handle this change
5705 * is to being the search from the
5708 goto find_tail_record
;
5711 le16_add_cpu(&rec
->e_leaf_clusters
, -clusters_to_del
);
5714 * We'll use "new_edge" on our way back up the
5715 * tree to know what our rightmost cpos is.
5717 new_edge
= le16_to_cpu(rec
->e_leaf_clusters
);
5718 new_edge
+= le32_to_cpu(rec
->e_cpos
);
5721 * The caller will use this to delete data blocks.
5723 *delete_start
= le64_to_cpu(rec
->e_blkno
)
5724 + ocfs2_clusters_to_blocks(inode
->i_sb
,
5725 le16_to_cpu(rec
->e_leaf_clusters
));
5728 * If it's now empty, remove this record.
5730 if (le16_to_cpu(rec
->e_leaf_clusters
) == 0) {
5732 sizeof(struct ocfs2_extent_rec
));
5733 le16_add_cpu(&el
->l_next_free_rec
, -1);
5736 if (le64_to_cpu(rec
->e_blkno
) == deleted_eb
) {
5738 sizeof(struct ocfs2_extent_rec
));
5739 le16_add_cpu(&el
->l_next_free_rec
, -1);
5744 /* Can this actually happen? */
5745 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
5749 * We never actually deleted any clusters
5750 * because our leaf was empty. There's no
5751 * reason to adjust the rightmost edge then.
5756 rec
->e_int_clusters
= cpu_to_le32(new_edge
);
5757 le32_add_cpu(&rec
->e_int_clusters
,
5758 -le32_to_cpu(rec
->e_cpos
));
5761 * A deleted child record should have been
5764 BUG_ON(le32_to_cpu(rec
->e_int_clusters
) == 0);
5768 ret
= ocfs2_journal_dirty(handle
, bh
);
5774 mlog(0, "extent list container %llu, after: record %d: "
5775 "(%u, %u, %llu), next = %u.\n",
5776 (unsigned long long)bh
->b_blocknr
, i
,
5777 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
),
5778 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
5779 le16_to_cpu(el
->l_next_free_rec
));
5782 * We must be careful to only attempt delete of an
5783 * extent block (and not the root inode block).
5785 if (index
> 0 && le16_to_cpu(el
->l_next_free_rec
) == 0) {
5786 struct ocfs2_extent_block
*eb
=
5787 (struct ocfs2_extent_block
*)bh
->b_data
;
5790 * Save this for use when processing the
5793 deleted_eb
= le64_to_cpu(eb
->h_blkno
);
5795 mlog(0, "deleting this extent block.\n");
5797 ocfs2_remove_from_cache(inode
, bh
);
5799 BUG_ON(ocfs2_rec_clusters(el
, &el
->l_recs
[0]));
5800 BUG_ON(le32_to_cpu(el
->l_recs
[0].e_cpos
));
5801 BUG_ON(le64_to_cpu(el
->l_recs
[0].e_blkno
));
5803 ret
= ocfs2_cache_extent_block_free(&tc
->tc_dealloc
, eb
);
5804 /* An error here is not fatal. */
5819 static int ocfs2_do_truncate(struct ocfs2_super
*osb
,
5820 unsigned int clusters_to_del
,
5821 struct inode
*inode
,
5822 struct buffer_head
*fe_bh
,
5824 struct ocfs2_truncate_context
*tc
,
5825 struct ocfs2_path
*path
)
5828 struct ocfs2_dinode
*fe
;
5829 struct ocfs2_extent_block
*last_eb
= NULL
;
5830 struct ocfs2_extent_list
*el
;
5831 struct buffer_head
*last_eb_bh
= NULL
;
5834 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
5836 status
= ocfs2_find_new_last_ext_blk(inode
, clusters_to_del
,
5844 * Each component will be touched, so we might as well journal
5845 * here to avoid having to handle errors later.
5847 status
= ocfs2_journal_access_path(inode
, handle
, path
);
5854 status
= ocfs2_journal_access(handle
, inode
, last_eb_bh
,
5855 OCFS2_JOURNAL_ACCESS_WRITE
);
5861 last_eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
5864 el
= &(fe
->id2
.i_list
);
5867 * Lower levels depend on this never happening, but it's best
5868 * to check it up here before changing the tree.
5870 if (el
->l_tree_depth
&& el
->l_recs
[0].e_int_clusters
== 0) {
5871 ocfs2_error(inode
->i_sb
,
5872 "Inode %lu has an empty extent record, depth %u\n",
5873 inode
->i_ino
, le16_to_cpu(el
->l_tree_depth
));
5878 spin_lock(&OCFS2_I(inode
)->ip_lock
);
5879 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
) -
5881 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
5882 le32_add_cpu(&fe
->i_clusters
, -clusters_to_del
);
5883 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
5885 status
= ocfs2_trim_tree(inode
, path
, handle
, tc
,
5886 clusters_to_del
, &delete_blk
);
5892 if (le32_to_cpu(fe
->i_clusters
) == 0) {
5893 /* trunc to zero is a special case. */
5894 el
->l_tree_depth
= 0;
5895 fe
->i_last_eb_blk
= 0;
5897 fe
->i_last_eb_blk
= last_eb
->h_blkno
;
5899 status
= ocfs2_journal_dirty(handle
, fe_bh
);
5906 /* If there will be a new last extent block, then by
5907 * definition, there cannot be any leaves to the right of
5909 last_eb
->h_next_leaf_blk
= 0;
5910 status
= ocfs2_journal_dirty(handle
, last_eb_bh
);
5918 status
= ocfs2_truncate_log_append(osb
, handle
, delete_blk
,
5932 static int ocfs2_writeback_zero_func(handle_t
*handle
, struct buffer_head
*bh
)
5934 set_buffer_uptodate(bh
);
5935 mark_buffer_dirty(bh
);
5939 static int ocfs2_ordered_zero_func(handle_t
*handle
, struct buffer_head
*bh
)
5941 set_buffer_uptodate(bh
);
5942 mark_buffer_dirty(bh
);
5943 return ocfs2_journal_dirty_data(handle
, bh
);
5946 static void ocfs2_map_and_dirty_page(struct inode
*inode
, handle_t
*handle
,
5947 unsigned int from
, unsigned int to
,
5948 struct page
*page
, int zero
, u64
*phys
)
5950 int ret
, partial
= 0;
5952 ret
= ocfs2_map_page_blocks(page
, phys
, inode
, from
, to
, 0);
5957 zero_user_segment(page
, from
, to
);
5960 * Need to set the buffers we zero'd into uptodate
5961 * here if they aren't - ocfs2_map_page_blocks()
5962 * might've skipped some
5964 if (ocfs2_should_order_data(inode
)) {
5965 ret
= walk_page_buffers(handle
,
5968 ocfs2_ordered_zero_func
);
5972 ret
= walk_page_buffers(handle
, page_buffers(page
),
5974 ocfs2_writeback_zero_func
);
5980 SetPageUptodate(page
);
5982 flush_dcache_page(page
);
5985 static void ocfs2_zero_cluster_pages(struct inode
*inode
, loff_t start
,
5986 loff_t end
, struct page
**pages
,
5987 int numpages
, u64 phys
, handle_t
*handle
)
5991 unsigned int from
, to
= PAGE_CACHE_SIZE
;
5992 struct super_block
*sb
= inode
->i_sb
;
5994 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb
)));
5999 to
= PAGE_CACHE_SIZE
;
6000 for(i
= 0; i
< numpages
; i
++) {
6003 from
= start
& (PAGE_CACHE_SIZE
- 1);
6004 if ((end
>> PAGE_CACHE_SHIFT
) == page
->index
)
6005 to
= end
& (PAGE_CACHE_SIZE
- 1);
6007 BUG_ON(from
> PAGE_CACHE_SIZE
);
6008 BUG_ON(to
> PAGE_CACHE_SIZE
);
6010 ocfs2_map_and_dirty_page(inode
, handle
, from
, to
, page
, 1,
6013 start
= (page
->index
+ 1) << PAGE_CACHE_SHIFT
;
6017 ocfs2_unlock_and_free_pages(pages
, numpages
);
6020 static int ocfs2_grab_eof_pages(struct inode
*inode
, loff_t start
, loff_t end
,
6021 struct page
**pages
, int *num
)
6023 int numpages
, ret
= 0;
6024 struct super_block
*sb
= inode
->i_sb
;
6025 struct address_space
*mapping
= inode
->i_mapping
;
6026 unsigned long index
;
6027 loff_t last_page_bytes
;
6029 BUG_ON(start
> end
);
6031 BUG_ON(start
>> OCFS2_SB(sb
)->s_clustersize_bits
!=
6032 (end
- 1) >> OCFS2_SB(sb
)->s_clustersize_bits
);
6035 last_page_bytes
= PAGE_ALIGN(end
);
6036 index
= start
>> PAGE_CACHE_SHIFT
;
6038 pages
[numpages
] = grab_cache_page(mapping
, index
);
6039 if (!pages
[numpages
]) {
6047 } while (index
< (last_page_bytes
>> PAGE_CACHE_SHIFT
));
6052 ocfs2_unlock_and_free_pages(pages
, numpages
);
6062 * Zero the area past i_size but still within an allocated
6063 * cluster. This avoids exposing nonzero data on subsequent file
6066 * We need to call this before i_size is updated on the inode because
6067 * otherwise block_write_full_page() will skip writeout of pages past
6068 * i_size. The new_i_size parameter is passed for this reason.
6070 int ocfs2_zero_range_for_truncate(struct inode
*inode
, handle_t
*handle
,
6071 u64 range_start
, u64 range_end
)
6073 int ret
= 0, numpages
;
6074 struct page
**pages
= NULL
;
6076 unsigned int ext_flags
;
6077 struct super_block
*sb
= inode
->i_sb
;
6080 * File systems which don't support sparse files zero on every
6083 if (!ocfs2_sparse_alloc(OCFS2_SB(sb
)))
6086 pages
= kcalloc(ocfs2_pages_per_cluster(sb
),
6087 sizeof(struct page
*), GFP_NOFS
);
6088 if (pages
== NULL
) {
6094 if (range_start
== range_end
)
6097 ret
= ocfs2_extent_map_get_blocks(inode
,
6098 range_start
>> sb
->s_blocksize_bits
,
6099 &phys
, NULL
, &ext_flags
);
6106 * Tail is a hole, or is marked unwritten. In either case, we
6107 * can count on read and write to return/push zero's.
6109 if (phys
== 0 || ext_flags
& OCFS2_EXT_UNWRITTEN
)
6112 ret
= ocfs2_grab_eof_pages(inode
, range_start
, range_end
, pages
,
6119 ocfs2_zero_cluster_pages(inode
, range_start
, range_end
, pages
,
6120 numpages
, phys
, handle
);
6123 * Initiate writeout of the pages we zero'd here. We don't
6124 * wait on them - the truncate_inode_pages() call later will
6127 ret
= do_sync_mapping_range(inode
->i_mapping
, range_start
,
6128 range_end
- 1, SYNC_FILE_RANGE_WRITE
);
6139 static void ocfs2_zero_dinode_id2(struct inode
*inode
, struct ocfs2_dinode
*di
)
6141 unsigned int blocksize
= 1 << inode
->i_sb
->s_blocksize_bits
;
6143 memset(&di
->id2
, 0, blocksize
- offsetof(struct ocfs2_dinode
, id2
));
6146 void ocfs2_dinode_new_extent_list(struct inode
*inode
,
6147 struct ocfs2_dinode
*di
)
6149 ocfs2_zero_dinode_id2(inode
, di
);
6150 di
->id2
.i_list
.l_tree_depth
= 0;
6151 di
->id2
.i_list
.l_next_free_rec
= 0;
6152 di
->id2
.i_list
.l_count
= cpu_to_le16(ocfs2_extent_recs_per_inode(inode
->i_sb
));
6155 void ocfs2_set_inode_data_inline(struct inode
*inode
, struct ocfs2_dinode
*di
)
6157 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6158 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
6160 spin_lock(&oi
->ip_lock
);
6161 oi
->ip_dyn_features
|= OCFS2_INLINE_DATA_FL
;
6162 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
6163 spin_unlock(&oi
->ip_lock
);
6166 * We clear the entire i_data structure here so that all
6167 * fields can be properly initialized.
6169 ocfs2_zero_dinode_id2(inode
, di
);
6171 idata
->id_count
= cpu_to_le16(ocfs2_max_inline_data(inode
->i_sb
));
6174 int ocfs2_convert_inline_data_to_extents(struct inode
*inode
,
6175 struct buffer_head
*di_bh
)
6177 int ret
, i
, has_data
, num_pages
= 0;
6179 u64
uninitialized_var(block
);
6180 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6181 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6182 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
6183 struct ocfs2_alloc_context
*data_ac
= NULL
;
6184 struct page
**pages
= NULL
;
6185 loff_t end
= osb
->s_clustersize
;
6187 has_data
= i_size_read(inode
) ? 1 : 0;
6190 pages
= kcalloc(ocfs2_pages_per_cluster(osb
->sb
),
6191 sizeof(struct page
*), GFP_NOFS
);
6192 if (pages
== NULL
) {
6198 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
6205 handle
= ocfs2_start_trans(osb
, OCFS2_INLINE_TO_EXTENTS_CREDITS
);
6206 if (IS_ERR(handle
)) {
6207 ret
= PTR_ERR(handle
);
6212 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
6213 OCFS2_JOURNAL_ACCESS_WRITE
);
6221 unsigned int page_end
;
6224 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
,
6232 * Save two copies, one for insert, and one that can
6233 * be changed by ocfs2_map_and_dirty_page() below.
6235 block
= phys
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
6238 * Non sparse file systems zero on extend, so no need
6241 if (!ocfs2_sparse_alloc(osb
) &&
6242 PAGE_CACHE_SIZE
< osb
->s_clustersize
)
6243 end
= PAGE_CACHE_SIZE
;
6245 ret
= ocfs2_grab_eof_pages(inode
, 0, end
, pages
, &num_pages
);
6252 * This should populate the 1st page for us and mark
6255 ret
= ocfs2_read_inline_data(inode
, pages
[0], di_bh
);
6261 page_end
= PAGE_CACHE_SIZE
;
6262 if (PAGE_CACHE_SIZE
> osb
->s_clustersize
)
6263 page_end
= osb
->s_clustersize
;
6265 for (i
= 0; i
< num_pages
; i
++)
6266 ocfs2_map_and_dirty_page(inode
, handle
, 0, page_end
,
6267 pages
[i
], i
> 0, &phys
);
6270 spin_lock(&oi
->ip_lock
);
6271 oi
->ip_dyn_features
&= ~OCFS2_INLINE_DATA_FL
;
6272 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
6273 spin_unlock(&oi
->ip_lock
);
6275 ocfs2_dinode_new_extent_list(inode
, di
);
6277 ocfs2_journal_dirty(handle
, di_bh
);
6281 * An error at this point should be extremely rare. If
6282 * this proves to be false, we could always re-build
6283 * the in-inode data from our pages.
6285 ret
= ocfs2_insert_extent(osb
, handle
, inode
, di_bh
,
6286 0, block
, 1, 0, NULL
);
6292 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6296 ocfs2_commit_trans(osb
, handle
);
6300 ocfs2_free_alloc_context(data_ac
);
6304 ocfs2_unlock_and_free_pages(pages
, num_pages
);
6312 * It is expected, that by the time you call this function,
6313 * inode->i_size and fe->i_size have been adjusted.
6315 * WARNING: This will kfree the truncate context
6317 int ocfs2_commit_truncate(struct ocfs2_super
*osb
,
6318 struct inode
*inode
,
6319 struct buffer_head
*fe_bh
,
6320 struct ocfs2_truncate_context
*tc
)
6322 int status
, i
, credits
, tl_sem
= 0;
6323 u32 clusters_to_del
, new_highest_cpos
, range
;
6324 struct ocfs2_extent_list
*el
;
6325 handle_t
*handle
= NULL
;
6326 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6327 struct ocfs2_path
*path
= NULL
;
6331 new_highest_cpos
= ocfs2_clusters_for_bytes(osb
->sb
,
6332 i_size_read(inode
));
6334 path
= ocfs2_new_inode_path(fe_bh
);
6341 ocfs2_extent_map_trunc(inode
, new_highest_cpos
);
6345 * Check that we still have allocation to delete.
6347 if (OCFS2_I(inode
)->ip_clusters
== 0) {
6353 * Truncate always works against the rightmost tree branch.
6355 status
= ocfs2_find_path(inode
, path
, UINT_MAX
);
6361 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6362 OCFS2_I(inode
)->ip_clusters
, path
->p_tree_depth
);
6365 * By now, el will point to the extent list on the bottom most
6366 * portion of this tree. Only the tail record is considered in
6369 * We handle the following cases, in order:
6370 * - empty extent: delete the remaining branch
6371 * - remove the entire record
6372 * - remove a partial record
6373 * - no record needs to be removed (truncate has completed)
6375 el
= path_leaf_el(path
);
6376 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
6377 ocfs2_error(inode
->i_sb
,
6378 "Inode %llu has empty extent block at %llu\n",
6379 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
6380 (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
6385 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
6386 range
= le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
6387 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
6388 if (i
== 0 && ocfs2_is_empty_extent(&el
->l_recs
[i
])) {
6389 clusters_to_del
= 0;
6390 } else if (le32_to_cpu(el
->l_recs
[i
].e_cpos
) >= new_highest_cpos
) {
6391 clusters_to_del
= ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
6392 } else if (range
> new_highest_cpos
) {
6393 clusters_to_del
= (ocfs2_rec_clusters(el
, &el
->l_recs
[i
]) +
6394 le32_to_cpu(el
->l_recs
[i
].e_cpos
)) -
6401 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6402 clusters_to_del
, (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
6404 mutex_lock(&tl_inode
->i_mutex
);
6406 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6407 * record is free for use. If there isn't any, we flush to get
6408 * an empty truncate log. */
6409 if (ocfs2_truncate_log_needs_flush(osb
)) {
6410 status
= __ocfs2_flush_truncate_log(osb
);
6417 credits
= ocfs2_calc_tree_trunc_credits(osb
->sb
, clusters_to_del
,
6418 (struct ocfs2_dinode
*)fe_bh
->b_data
,
6420 handle
= ocfs2_start_trans(osb
, credits
);
6421 if (IS_ERR(handle
)) {
6422 status
= PTR_ERR(handle
);
6428 status
= ocfs2_do_truncate(osb
, clusters_to_del
, inode
, fe_bh
, handle
,
6435 mutex_unlock(&tl_inode
->i_mutex
);
6438 ocfs2_commit_trans(osb
, handle
);
6441 ocfs2_reinit_path(path
, 1);
6444 * The check above will catch the case where we've truncated
6445 * away all allocation.
6451 ocfs2_schedule_truncate_log_flush(osb
, 1);
6454 mutex_unlock(&tl_inode
->i_mutex
);
6457 ocfs2_commit_trans(osb
, handle
);
6459 ocfs2_run_deallocs(osb
, &tc
->tc_dealloc
);
6461 ocfs2_free_path(path
);
6463 /* This will drop the ext_alloc cluster lock for us */
6464 ocfs2_free_truncate_context(tc
);
6471 * Expects the inode to already be locked.
6473 int ocfs2_prepare_truncate(struct ocfs2_super
*osb
,
6474 struct inode
*inode
,
6475 struct buffer_head
*fe_bh
,
6476 struct ocfs2_truncate_context
**tc
)
6479 unsigned int new_i_clusters
;
6480 struct ocfs2_dinode
*fe
;
6481 struct ocfs2_extent_block
*eb
;
6482 struct buffer_head
*last_eb_bh
= NULL
;
6488 new_i_clusters
= ocfs2_clusters_for_bytes(osb
->sb
,
6489 i_size_read(inode
));
6490 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
6492 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
6493 "%llu\n", le32_to_cpu(fe
->i_clusters
), new_i_clusters
,
6494 (unsigned long long)le64_to_cpu(fe
->i_size
));
6496 *tc
= kzalloc(sizeof(struct ocfs2_truncate_context
), GFP_KERNEL
);
6502 ocfs2_init_dealloc_ctxt(&(*tc
)->tc_dealloc
);
6504 if (fe
->id2
.i_list
.l_tree_depth
) {
6505 status
= ocfs2_read_block(osb
, le64_to_cpu(fe
->i_last_eb_blk
),
6506 &last_eb_bh
, OCFS2_BH_CACHED
, inode
);
6511 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
6512 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
6513 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
6521 (*tc
)->tc_last_eb_bh
= last_eb_bh
;
6527 ocfs2_free_truncate_context(*tc
);
6535 * 'start' is inclusive, 'end' is not.
6537 int ocfs2_truncate_inline(struct inode
*inode
, struct buffer_head
*di_bh
,
6538 unsigned int start
, unsigned int end
, int trunc
)
6541 unsigned int numbytes
;
6543 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6544 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
6545 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
6547 if (end
> i_size_read(inode
))
6548 end
= i_size_read(inode
);
6550 BUG_ON(start
>= end
);
6552 if (!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) ||
6553 !(le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_DATA_FL
) ||
6554 !ocfs2_supports_inline_data(osb
)) {
6555 ocfs2_error(inode
->i_sb
,
6556 "Inline data flags for inode %llu don't agree! "
6557 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
6558 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
6559 le16_to_cpu(di
->i_dyn_features
),
6560 OCFS2_I(inode
)->ip_dyn_features
,
6561 osb
->s_feature_incompat
);
6566 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
6567 if (IS_ERR(handle
)) {
6568 ret
= PTR_ERR(handle
);
6573 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
6574 OCFS2_JOURNAL_ACCESS_WRITE
);
6580 numbytes
= end
- start
;
6581 memset(idata
->id_data
+ start
, 0, numbytes
);
6584 * No need to worry about the data page here - it's been
6585 * truncated already and inline data doesn't need it for
6586 * pushing zero's to disk, so we'll let readpage pick it up
6590 i_size_write(inode
, start
);
6591 di
->i_size
= cpu_to_le64(start
);
6594 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6595 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
6597 di
->i_ctime
= di
->i_mtime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
6598 di
->i_ctime_nsec
= di
->i_mtime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
6600 ocfs2_journal_dirty(handle
, di_bh
);
6603 ocfs2_commit_trans(osb
, handle
);
6609 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
)
6612 * The caller is responsible for completing deallocation
6613 * before freeing the context.
6615 if (tc
->tc_dealloc
.c_first_suballocator
!= NULL
)
6617 "Truncate completion has non-empty dealloc context\n");
6619 if (tc
->tc_last_eb_bh
)
6620 brelse(tc
->tc_last_eb_bh
);