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"
54 * Operations for a specific extent tree type.
56 * To implement an on-disk btree (extent tree) type in ocfs2, add
57 * an ocfs2_extent_tree_operations structure and the matching
58 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
59 * for the allocation portion of the extent tree.
61 struct ocfs2_extent_tree_operations
{
63 * last_eb_blk is the block number of the right most leaf extent
64 * block. Most on-disk structures containing an extent tree store
65 * this value for fast access. The ->eo_set_last_eb_blk() and
66 * ->eo_get_last_eb_blk() operations access this value. They are
69 void (*eo_set_last_eb_blk
)(struct ocfs2_extent_tree
*et
,
71 u64 (*eo_get_last_eb_blk
)(struct ocfs2_extent_tree
*et
);
74 * The on-disk structure usually keeps track of how many total
75 * clusters are stored in this extent tree. This function updates
76 * that value. new_clusters is the delta, and must be
77 * added to the total. Required.
79 void (*eo_update_clusters
)(struct inode
*inode
,
80 struct ocfs2_extent_tree
*et
,
84 * If ->eo_insert_check() exists, it is called before rec is
85 * inserted into the extent tree. It is optional.
87 int (*eo_insert_check
)(struct inode
*inode
,
88 struct ocfs2_extent_tree
*et
,
89 struct ocfs2_extent_rec
*rec
);
90 int (*eo_sanity_check
)(struct inode
*inode
, struct ocfs2_extent_tree
*et
);
93 * --------------------------------------------------------------
94 * The remaining are internal to ocfs2_extent_tree and don't have
99 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
102 void (*eo_fill_root_el
)(struct ocfs2_extent_tree
*et
);
105 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
106 * it exists. If it does not, et->et_max_leaf_clusters is set
107 * to 0 (unlimited). Optional.
109 void (*eo_fill_max_leaf_clusters
)(struct inode
*inode
,
110 struct ocfs2_extent_tree
*et
);
115 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
118 static u64
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree
*et
);
119 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
121 static void ocfs2_dinode_update_clusters(struct inode
*inode
,
122 struct ocfs2_extent_tree
*et
,
124 static int ocfs2_dinode_insert_check(struct inode
*inode
,
125 struct ocfs2_extent_tree
*et
,
126 struct ocfs2_extent_rec
*rec
);
127 static int ocfs2_dinode_sanity_check(struct inode
*inode
,
128 struct ocfs2_extent_tree
*et
);
129 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree
*et
);
130 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops
= {
131 .eo_set_last_eb_blk
= ocfs2_dinode_set_last_eb_blk
,
132 .eo_get_last_eb_blk
= ocfs2_dinode_get_last_eb_blk
,
133 .eo_update_clusters
= ocfs2_dinode_update_clusters
,
134 .eo_insert_check
= ocfs2_dinode_insert_check
,
135 .eo_sanity_check
= ocfs2_dinode_sanity_check
,
136 .eo_fill_root_el
= ocfs2_dinode_fill_root_el
,
139 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
142 struct ocfs2_dinode
*di
= et
->et_object
;
144 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
145 di
->i_last_eb_blk
= cpu_to_le64(blkno
);
148 static u64
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
150 struct ocfs2_dinode
*di
= et
->et_object
;
152 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
153 return le64_to_cpu(di
->i_last_eb_blk
);
156 static void ocfs2_dinode_update_clusters(struct inode
*inode
,
157 struct ocfs2_extent_tree
*et
,
160 struct ocfs2_dinode
*di
= et
->et_object
;
162 le32_add_cpu(&di
->i_clusters
, clusters
);
163 spin_lock(&OCFS2_I(inode
)->ip_lock
);
164 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(di
->i_clusters
);
165 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
168 static int ocfs2_dinode_insert_check(struct inode
*inode
,
169 struct ocfs2_extent_tree
*et
,
170 struct ocfs2_extent_rec
*rec
)
172 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
174 BUG_ON(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
);
175 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb
) &&
176 (OCFS2_I(inode
)->ip_clusters
!= rec
->e_cpos
),
177 "Device %s, asking for sparse allocation: inode %llu, "
178 "cpos %u, clusters %u\n",
180 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
182 OCFS2_I(inode
)->ip_clusters
);
187 static int ocfs2_dinode_sanity_check(struct inode
*inode
,
188 struct ocfs2_extent_tree
*et
)
190 struct ocfs2_dinode
*di
= et
->et_object
;
192 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
193 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
198 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree
*et
)
200 struct ocfs2_dinode
*di
= et
->et_object
;
202 et
->et_root_el
= &di
->id2
.i_list
;
206 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree
*et
)
208 struct ocfs2_xattr_value_root
*xv
= et
->et_object
;
210 et
->et_root_el
= &xv
->xr_list
;
213 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
216 struct ocfs2_xattr_value_root
*xv
=
217 (struct ocfs2_xattr_value_root
*)et
->et_object
;
219 xv
->xr_last_eb_blk
= cpu_to_le64(blkno
);
222 static u64
ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
224 struct ocfs2_xattr_value_root
*xv
=
225 (struct ocfs2_xattr_value_root
*) et
->et_object
;
227 return le64_to_cpu(xv
->xr_last_eb_blk
);
230 static void ocfs2_xattr_value_update_clusters(struct inode
*inode
,
231 struct ocfs2_extent_tree
*et
,
234 struct ocfs2_xattr_value_root
*xv
=
235 (struct ocfs2_xattr_value_root
*)et
->et_object
;
237 le32_add_cpu(&xv
->xr_clusters
, clusters
);
240 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops
= {
241 .eo_set_last_eb_blk
= ocfs2_xattr_value_set_last_eb_blk
,
242 .eo_get_last_eb_blk
= ocfs2_xattr_value_get_last_eb_blk
,
243 .eo_update_clusters
= ocfs2_xattr_value_update_clusters
,
244 .eo_fill_root_el
= ocfs2_xattr_value_fill_root_el
,
247 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree
*et
)
249 struct ocfs2_xattr_block
*xb
= et
->et_object
;
251 et
->et_root_el
= &xb
->xb_attrs
.xb_root
.xt_list
;
254 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode
*inode
,
255 struct ocfs2_extent_tree
*et
)
257 et
->et_max_leaf_clusters
=
258 ocfs2_clusters_for_bytes(inode
->i_sb
,
259 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
);
262 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
265 struct ocfs2_xattr_block
*xb
= et
->et_object
;
266 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
268 xt
->xt_last_eb_blk
= cpu_to_le64(blkno
);
271 static u64
ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
273 struct ocfs2_xattr_block
*xb
= et
->et_object
;
274 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
276 return le64_to_cpu(xt
->xt_last_eb_blk
);
279 static void ocfs2_xattr_tree_update_clusters(struct inode
*inode
,
280 struct ocfs2_extent_tree
*et
,
283 struct ocfs2_xattr_block
*xb
= et
->et_object
;
285 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, clusters
);
288 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops
= {
289 .eo_set_last_eb_blk
= ocfs2_xattr_tree_set_last_eb_blk
,
290 .eo_get_last_eb_blk
= ocfs2_xattr_tree_get_last_eb_blk
,
291 .eo_update_clusters
= ocfs2_xattr_tree_update_clusters
,
292 .eo_fill_root_el
= ocfs2_xattr_tree_fill_root_el
,
293 .eo_fill_max_leaf_clusters
= ocfs2_xattr_tree_fill_max_leaf_clusters
,
296 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree
*et
,
298 struct buffer_head
*bh
,
300 struct ocfs2_extent_tree_operations
*ops
)
305 obj
= (void *)bh
->b_data
;
308 et
->et_ops
->eo_fill_root_el(et
);
309 if (!et
->et_ops
->eo_fill_max_leaf_clusters
)
310 et
->et_max_leaf_clusters
= 0;
312 et
->et_ops
->eo_fill_max_leaf_clusters(inode
, et
);
315 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree
*et
,
317 struct buffer_head
*bh
)
319 __ocfs2_init_extent_tree(et
, inode
, bh
, NULL
, &ocfs2_dinode_et_ops
);
322 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree
*et
,
324 struct buffer_head
*bh
)
326 __ocfs2_init_extent_tree(et
, inode
, bh
, NULL
,
327 &ocfs2_xattr_tree_et_ops
);
330 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree
*et
,
332 struct buffer_head
*bh
,
333 struct ocfs2_xattr_value_root
*xv
)
335 __ocfs2_init_extent_tree(et
, inode
, bh
, xv
,
336 &ocfs2_xattr_value_et_ops
);
339 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
342 et
->et_ops
->eo_set_last_eb_blk(et
, new_last_eb_blk
);
345 static inline u64
ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
347 return et
->et_ops
->eo_get_last_eb_blk(et
);
350 static inline void ocfs2_et_update_clusters(struct inode
*inode
,
351 struct ocfs2_extent_tree
*et
,
354 et
->et_ops
->eo_update_clusters(inode
, et
, clusters
);
357 static inline int ocfs2_et_insert_check(struct inode
*inode
,
358 struct ocfs2_extent_tree
*et
,
359 struct ocfs2_extent_rec
*rec
)
363 if (et
->et_ops
->eo_insert_check
)
364 ret
= et
->et_ops
->eo_insert_check(inode
, et
, rec
);
368 static inline int ocfs2_et_sanity_check(struct inode
*inode
,
369 struct ocfs2_extent_tree
*et
)
373 if (et
->et_ops
->eo_sanity_check
)
374 ret
= et
->et_ops
->eo_sanity_check(inode
, et
);
378 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
);
379 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
380 struct ocfs2_extent_block
*eb
);
383 * Structures which describe a path through a btree, and functions to
386 * The idea here is to be as generic as possible with the tree
389 struct ocfs2_path_item
{
390 struct buffer_head
*bh
;
391 struct ocfs2_extent_list
*el
;
394 #define OCFS2_MAX_PATH_DEPTH 5
398 struct ocfs2_path_item p_node
[OCFS2_MAX_PATH_DEPTH
];
401 #define path_root_bh(_path) ((_path)->p_node[0].bh)
402 #define path_root_el(_path) ((_path)->p_node[0].el)
403 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
404 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
405 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
408 * Reset the actual path elements so that we can re-use the structure
409 * to build another path. Generally, this involves freeing the buffer
412 static void ocfs2_reinit_path(struct ocfs2_path
*path
, int keep_root
)
414 int i
, start
= 0, depth
= 0;
415 struct ocfs2_path_item
*node
;
420 for(i
= start
; i
< path_num_items(path
); i
++) {
421 node
= &path
->p_node
[i
];
429 * Tree depth may change during truncate, or insert. If we're
430 * keeping the root extent list, then make sure that our path
431 * structure reflects the proper depth.
434 depth
= le16_to_cpu(path_root_el(path
)->l_tree_depth
);
436 path
->p_tree_depth
= depth
;
439 static void ocfs2_free_path(struct ocfs2_path
*path
)
442 ocfs2_reinit_path(path
, 0);
448 * All the elements of src into dest. After this call, src could be freed
449 * without affecting dest.
451 * Both paths should have the same root. Any non-root elements of dest
454 static void ocfs2_cp_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
458 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
459 BUG_ON(path_root_el(dest
) != path_root_el(src
));
461 ocfs2_reinit_path(dest
, 1);
463 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
464 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
465 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
467 if (dest
->p_node
[i
].bh
)
468 get_bh(dest
->p_node
[i
].bh
);
473 * Make the *dest path the same as src and re-initialize src path to
476 static void ocfs2_mv_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
480 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
482 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
483 brelse(dest
->p_node
[i
].bh
);
485 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
486 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
488 src
->p_node
[i
].bh
= NULL
;
489 src
->p_node
[i
].el
= NULL
;
494 * Insert an extent block at given index.
496 * This will not take an additional reference on eb_bh.
498 static inline void ocfs2_path_insert_eb(struct ocfs2_path
*path
, int index
,
499 struct buffer_head
*eb_bh
)
501 struct ocfs2_extent_block
*eb
= (struct ocfs2_extent_block
*)eb_bh
->b_data
;
504 * Right now, no root bh is an extent block, so this helps
505 * catch code errors with dinode trees. The assertion can be
506 * safely removed if we ever need to insert extent block
507 * structures at the root.
511 path
->p_node
[index
].bh
= eb_bh
;
512 path
->p_node
[index
].el
= &eb
->h_list
;
515 static struct ocfs2_path
*ocfs2_new_path(struct buffer_head
*root_bh
,
516 struct ocfs2_extent_list
*root_el
)
518 struct ocfs2_path
*path
;
520 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) >= OCFS2_MAX_PATH_DEPTH
);
522 path
= kzalloc(sizeof(*path
), GFP_NOFS
);
524 path
->p_tree_depth
= le16_to_cpu(root_el
->l_tree_depth
);
526 path_root_bh(path
) = root_bh
;
527 path_root_el(path
) = root_el
;
534 * Convenience function to journal all components in a path.
536 static int ocfs2_journal_access_path(struct inode
*inode
, handle_t
*handle
,
537 struct ocfs2_path
*path
)
544 for(i
= 0; i
< path_num_items(path
); i
++) {
545 ret
= ocfs2_journal_access(handle
, inode
, path
->p_node
[i
].bh
,
546 OCFS2_JOURNAL_ACCESS_WRITE
);
558 * Return the index of the extent record which contains cluster #v_cluster.
559 * -1 is returned if it was not found.
561 * Should work fine on interior and exterior nodes.
563 int ocfs2_search_extent_list(struct ocfs2_extent_list
*el
, u32 v_cluster
)
567 struct ocfs2_extent_rec
*rec
;
568 u32 rec_end
, rec_start
, clusters
;
570 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
571 rec
= &el
->l_recs
[i
];
573 rec_start
= le32_to_cpu(rec
->e_cpos
);
574 clusters
= ocfs2_rec_clusters(el
, rec
);
576 rec_end
= rec_start
+ clusters
;
578 if (v_cluster
>= rec_start
&& v_cluster
< rec_end
) {
587 enum ocfs2_contig_type
{
596 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
597 * ocfs2_extent_contig only work properly against leaf nodes!
599 static int ocfs2_block_extent_contig(struct super_block
*sb
,
600 struct ocfs2_extent_rec
*ext
,
603 u64 blk_end
= le64_to_cpu(ext
->e_blkno
);
605 blk_end
+= ocfs2_clusters_to_blocks(sb
,
606 le16_to_cpu(ext
->e_leaf_clusters
));
608 return blkno
== blk_end
;
611 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec
*left
,
612 struct ocfs2_extent_rec
*right
)
616 left_range
= le32_to_cpu(left
->e_cpos
) +
617 le16_to_cpu(left
->e_leaf_clusters
);
619 return (left_range
== le32_to_cpu(right
->e_cpos
));
622 static enum ocfs2_contig_type
623 ocfs2_extent_contig(struct inode
*inode
,
624 struct ocfs2_extent_rec
*ext
,
625 struct ocfs2_extent_rec
*insert_rec
)
627 u64 blkno
= le64_to_cpu(insert_rec
->e_blkno
);
630 * Refuse to coalesce extent records with different flag
631 * fields - we don't want to mix unwritten extents with user
634 if (ext
->e_flags
!= insert_rec
->e_flags
)
637 if (ocfs2_extents_adjacent(ext
, insert_rec
) &&
638 ocfs2_block_extent_contig(inode
->i_sb
, ext
, blkno
))
641 blkno
= le64_to_cpu(ext
->e_blkno
);
642 if (ocfs2_extents_adjacent(insert_rec
, ext
) &&
643 ocfs2_block_extent_contig(inode
->i_sb
, insert_rec
, blkno
))
650 * NOTE: We can have pretty much any combination of contiguousness and
653 * The usefulness of APPEND_TAIL is more in that it lets us know that
654 * we'll have to update the path to that leaf.
656 enum ocfs2_append_type
{
661 enum ocfs2_split_type
{
667 struct ocfs2_insert_type
{
668 enum ocfs2_split_type ins_split
;
669 enum ocfs2_append_type ins_appending
;
670 enum ocfs2_contig_type ins_contig
;
671 int ins_contig_index
;
675 struct ocfs2_merge_ctxt
{
676 enum ocfs2_contig_type c_contig_type
;
677 int c_has_empty_extent
;
678 int c_split_covers_rec
;
681 static int ocfs2_validate_extent_block(struct super_block
*sb
,
682 struct buffer_head
*bh
)
684 struct ocfs2_extent_block
*eb
=
685 (struct ocfs2_extent_block
*)bh
->b_data
;
687 mlog(0, "Validating extent block %llu\n",
688 (unsigned long long)bh
->b_blocknr
);
690 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
692 "Extent block #%llu has bad signature %.*s",
693 (unsigned long long)bh
->b_blocknr
, 7,
698 if (le64_to_cpu(eb
->h_blkno
) != bh
->b_blocknr
) {
700 "Extent block #%llu has an invalid h_blkno "
702 (unsigned long long)bh
->b_blocknr
,
703 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
707 if (le32_to_cpu(eb
->h_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
709 "Extent block #%llu has an invalid "
710 "h_fs_generation of #%u",
711 (unsigned long long)bh
->b_blocknr
,
712 le32_to_cpu(eb
->h_fs_generation
));
719 int ocfs2_read_extent_block(struct inode
*inode
, u64 eb_blkno
,
720 struct buffer_head
**bh
)
723 struct buffer_head
*tmp
= *bh
;
725 rc
= ocfs2_read_block(inode
, eb_blkno
, &tmp
,
726 ocfs2_validate_extent_block
);
728 /* If ocfs2_read_block() got us a new bh, pass it up. */
737 * How many free extents have we got before we need more meta data?
739 int ocfs2_num_free_extents(struct ocfs2_super
*osb
,
741 struct ocfs2_extent_tree
*et
)
744 struct ocfs2_extent_list
*el
= NULL
;
745 struct ocfs2_extent_block
*eb
;
746 struct buffer_head
*eb_bh
= NULL
;
752 last_eb_blk
= ocfs2_et_get_last_eb_blk(et
);
755 retval
= ocfs2_read_extent_block(inode
, last_eb_blk
, &eb_bh
);
760 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
764 BUG_ON(el
->l_tree_depth
!= 0);
766 retval
= le16_to_cpu(el
->l_count
) - le16_to_cpu(el
->l_next_free_rec
);
774 /* expects array to already be allocated
776 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
779 static int ocfs2_create_new_meta_bhs(struct ocfs2_super
*osb
,
783 struct ocfs2_alloc_context
*meta_ac
,
784 struct buffer_head
*bhs
[])
786 int count
, status
, i
;
787 u16 suballoc_bit_start
;
790 struct ocfs2_extent_block
*eb
;
795 while (count
< wanted
) {
796 status
= ocfs2_claim_metadata(osb
,
808 for(i
= count
; i
< (num_got
+ count
); i
++) {
809 bhs
[i
] = sb_getblk(osb
->sb
, first_blkno
);
810 if (bhs
[i
] == NULL
) {
815 ocfs2_set_new_buffer_uptodate(inode
, bhs
[i
]);
817 status
= ocfs2_journal_access(handle
, inode
, bhs
[i
],
818 OCFS2_JOURNAL_ACCESS_CREATE
);
824 memset(bhs
[i
]->b_data
, 0, osb
->sb
->s_blocksize
);
825 eb
= (struct ocfs2_extent_block
*) bhs
[i
]->b_data
;
826 /* Ok, setup the minimal stuff here. */
827 strcpy(eb
->h_signature
, OCFS2_EXTENT_BLOCK_SIGNATURE
);
828 eb
->h_blkno
= cpu_to_le64(first_blkno
);
829 eb
->h_fs_generation
= cpu_to_le32(osb
->fs_generation
);
830 eb
->h_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
831 eb
->h_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
833 cpu_to_le16(ocfs2_extent_recs_per_eb(osb
->sb
));
835 suballoc_bit_start
++;
838 /* We'll also be dirtied by the caller, so
839 * this isn't absolutely necessary. */
840 status
= ocfs2_journal_dirty(handle
, bhs
[i
]);
853 for(i
= 0; i
< wanted
; i
++) {
863 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
865 * Returns the sum of the rightmost extent rec logical offset and
868 * ocfs2_add_branch() uses this to determine what logical cluster
869 * value should be populated into the leftmost new branch records.
871 * ocfs2_shift_tree_depth() uses this to determine the # clusters
872 * value for the new topmost tree record.
874 static inline u32
ocfs2_sum_rightmost_rec(struct ocfs2_extent_list
*el
)
878 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
880 return le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
881 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
885 * Add an entire tree branch to our inode. eb_bh is the extent block
886 * to start at, if we don't want to start the branch at the dinode
889 * last_eb_bh is required as we have to update it's next_leaf pointer
890 * for the new last extent block.
892 * the new branch will be 'empty' in the sense that every block will
893 * contain a single record with cluster count == 0.
895 static int ocfs2_add_branch(struct ocfs2_super
*osb
,
898 struct ocfs2_extent_tree
*et
,
899 struct buffer_head
*eb_bh
,
900 struct buffer_head
**last_eb_bh
,
901 struct ocfs2_alloc_context
*meta_ac
)
903 int status
, new_blocks
, i
;
904 u64 next_blkno
, new_last_eb_blk
;
905 struct buffer_head
*bh
;
906 struct buffer_head
**new_eb_bhs
= NULL
;
907 struct ocfs2_extent_block
*eb
;
908 struct ocfs2_extent_list
*eb_el
;
909 struct ocfs2_extent_list
*el
;
914 BUG_ON(!last_eb_bh
|| !*last_eb_bh
);
917 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
922 /* we never add a branch to a leaf. */
923 BUG_ON(!el
->l_tree_depth
);
925 new_blocks
= le16_to_cpu(el
->l_tree_depth
);
927 /* allocate the number of new eb blocks we need */
928 new_eb_bhs
= kcalloc(new_blocks
, sizeof(struct buffer_head
*),
936 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, new_blocks
,
937 meta_ac
, new_eb_bhs
);
943 eb
= (struct ocfs2_extent_block
*)(*last_eb_bh
)->b_data
;
944 new_cpos
= ocfs2_sum_rightmost_rec(&eb
->h_list
);
946 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
947 * linked with the rest of the tree.
948 * conversly, new_eb_bhs[0] is the new bottommost leaf.
950 * when we leave the loop, new_last_eb_blk will point to the
951 * newest leaf, and next_blkno will point to the topmost extent
953 next_blkno
= new_last_eb_blk
= 0;
954 for(i
= 0; i
< new_blocks
; i
++) {
956 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
957 /* ocfs2_create_new_meta_bhs() should create it right! */
958 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
961 status
= ocfs2_journal_access(handle
, inode
, bh
,
962 OCFS2_JOURNAL_ACCESS_CREATE
);
968 eb
->h_next_leaf_blk
= 0;
969 eb_el
->l_tree_depth
= cpu_to_le16(i
);
970 eb_el
->l_next_free_rec
= cpu_to_le16(1);
972 * This actually counts as an empty extent as
975 eb_el
->l_recs
[0].e_cpos
= cpu_to_le32(new_cpos
);
976 eb_el
->l_recs
[0].e_blkno
= cpu_to_le64(next_blkno
);
978 * eb_el isn't always an interior node, but even leaf
979 * nodes want a zero'd flags and reserved field so
980 * this gets the whole 32 bits regardless of use.
982 eb_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(0);
983 if (!eb_el
->l_tree_depth
)
984 new_last_eb_blk
= le64_to_cpu(eb
->h_blkno
);
986 status
= ocfs2_journal_dirty(handle
, bh
);
992 next_blkno
= le64_to_cpu(eb
->h_blkno
);
995 /* This is a bit hairy. We want to update up to three blocks
996 * here without leaving any of them in an inconsistent state
997 * in case of error. We don't have to worry about
998 * journal_dirty erroring as it won't unless we've aborted the
999 * handle (in which case we would never be here) so reserving
1000 * the write with journal_access is all we need to do. */
1001 status
= ocfs2_journal_access(handle
, inode
, *last_eb_bh
,
1002 OCFS2_JOURNAL_ACCESS_WRITE
);
1007 status
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
1008 OCFS2_JOURNAL_ACCESS_WRITE
);
1014 status
= ocfs2_journal_access(handle
, inode
, eb_bh
,
1015 OCFS2_JOURNAL_ACCESS_WRITE
);
1022 /* Link the new branch into the rest of the tree (el will
1023 * either be on the root_bh, or the extent block passed in. */
1024 i
= le16_to_cpu(el
->l_next_free_rec
);
1025 el
->l_recs
[i
].e_blkno
= cpu_to_le64(next_blkno
);
1026 el
->l_recs
[i
].e_cpos
= cpu_to_le32(new_cpos
);
1027 el
->l_recs
[i
].e_int_clusters
= 0;
1028 le16_add_cpu(&el
->l_next_free_rec
, 1);
1030 /* fe needs a new last extent block pointer, as does the
1031 * next_leaf on the previously last-extent-block. */
1032 ocfs2_et_set_last_eb_blk(et
, new_last_eb_blk
);
1034 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
1035 eb
->h_next_leaf_blk
= cpu_to_le64(new_last_eb_blk
);
1037 status
= ocfs2_journal_dirty(handle
, *last_eb_bh
);
1040 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1044 status
= ocfs2_journal_dirty(handle
, eb_bh
);
1050 * Some callers want to track the rightmost leaf so pass it
1053 brelse(*last_eb_bh
);
1054 get_bh(new_eb_bhs
[0]);
1055 *last_eb_bh
= new_eb_bhs
[0];
1060 for (i
= 0; i
< new_blocks
; i
++)
1061 brelse(new_eb_bhs
[i
]);
1070 * adds another level to the allocation tree.
1071 * returns back the new extent block so you can add a branch to it
1074 static int ocfs2_shift_tree_depth(struct ocfs2_super
*osb
,
1076 struct inode
*inode
,
1077 struct ocfs2_extent_tree
*et
,
1078 struct ocfs2_alloc_context
*meta_ac
,
1079 struct buffer_head
**ret_new_eb_bh
)
1083 struct buffer_head
*new_eb_bh
= NULL
;
1084 struct ocfs2_extent_block
*eb
;
1085 struct ocfs2_extent_list
*root_el
;
1086 struct ocfs2_extent_list
*eb_el
;
1090 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, 1, meta_ac
,
1097 eb
= (struct ocfs2_extent_block
*) new_eb_bh
->b_data
;
1098 /* ocfs2_create_new_meta_bhs() should create it right! */
1099 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
1101 eb_el
= &eb
->h_list
;
1102 root_el
= et
->et_root_el
;
1104 status
= ocfs2_journal_access(handle
, inode
, new_eb_bh
,
1105 OCFS2_JOURNAL_ACCESS_CREATE
);
1111 /* copy the root extent list data into the new extent block */
1112 eb_el
->l_tree_depth
= root_el
->l_tree_depth
;
1113 eb_el
->l_next_free_rec
= root_el
->l_next_free_rec
;
1114 for (i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1115 eb_el
->l_recs
[i
] = root_el
->l_recs
[i
];
1117 status
= ocfs2_journal_dirty(handle
, new_eb_bh
);
1123 status
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
1124 OCFS2_JOURNAL_ACCESS_WRITE
);
1130 new_clusters
= ocfs2_sum_rightmost_rec(eb_el
);
1132 /* update root_bh now */
1133 le16_add_cpu(&root_el
->l_tree_depth
, 1);
1134 root_el
->l_recs
[0].e_cpos
= 0;
1135 root_el
->l_recs
[0].e_blkno
= eb
->h_blkno
;
1136 root_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(new_clusters
);
1137 for (i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1138 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
1139 root_el
->l_next_free_rec
= cpu_to_le16(1);
1141 /* If this is our 1st tree depth shift, then last_eb_blk
1142 * becomes the allocated extent block */
1143 if (root_el
->l_tree_depth
== cpu_to_le16(1))
1144 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
1146 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1152 *ret_new_eb_bh
= new_eb_bh
;
1163 * Should only be called when there is no space left in any of the
1164 * leaf nodes. What we want to do is find the lowest tree depth
1165 * non-leaf extent block with room for new records. There are three
1166 * valid results of this search:
1168 * 1) a lowest extent block is found, then we pass it back in
1169 * *lowest_eb_bh and return '0'
1171 * 2) the search fails to find anything, but the root_el has room. We
1172 * pass NULL back in *lowest_eb_bh, but still return '0'
1174 * 3) the search fails to find anything AND the root_el is full, in
1175 * which case we return > 0
1177 * return status < 0 indicates an error.
1179 static int ocfs2_find_branch_target(struct ocfs2_super
*osb
,
1180 struct inode
*inode
,
1181 struct ocfs2_extent_tree
*et
,
1182 struct buffer_head
**target_bh
)
1186 struct ocfs2_extent_block
*eb
;
1187 struct ocfs2_extent_list
*el
;
1188 struct buffer_head
*bh
= NULL
;
1189 struct buffer_head
*lowest_bh
= NULL
;
1195 el
= et
->et_root_el
;
1197 while(le16_to_cpu(el
->l_tree_depth
) > 1) {
1198 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1199 ocfs2_error(inode
->i_sb
, "Dinode %llu has empty "
1200 "extent list (next_free_rec == 0)",
1201 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1205 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1206 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1208 ocfs2_error(inode
->i_sb
, "Dinode %llu has extent "
1209 "list where extent # %d has no physical "
1211 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, i
);
1219 status
= ocfs2_read_extent_block(inode
, blkno
, &bh
);
1225 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1228 if (le16_to_cpu(el
->l_next_free_rec
) <
1229 le16_to_cpu(el
->l_count
)) {
1236 /* If we didn't find one and the fe doesn't have any room,
1237 * then return '1' */
1238 el
= et
->et_root_el
;
1239 if (!lowest_bh
&& (el
->l_next_free_rec
== el
->l_count
))
1242 *target_bh
= lowest_bh
;
1251 * Grow a b-tree so that it has more records.
1253 * We might shift the tree depth in which case existing paths should
1254 * be considered invalid.
1256 * Tree depth after the grow is returned via *final_depth.
1258 * *last_eb_bh will be updated by ocfs2_add_branch().
1260 static int ocfs2_grow_tree(struct inode
*inode
, handle_t
*handle
,
1261 struct ocfs2_extent_tree
*et
, int *final_depth
,
1262 struct buffer_head
**last_eb_bh
,
1263 struct ocfs2_alloc_context
*meta_ac
)
1266 struct ocfs2_extent_list
*el
= et
->et_root_el
;
1267 int depth
= le16_to_cpu(el
->l_tree_depth
);
1268 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1269 struct buffer_head
*bh
= NULL
;
1271 BUG_ON(meta_ac
== NULL
);
1273 shift
= ocfs2_find_branch_target(osb
, inode
, et
, &bh
);
1280 /* We traveled all the way to the bottom of the allocation tree
1281 * and didn't find room for any more extents - we need to add
1282 * another tree level */
1285 mlog(0, "need to shift tree depth (current = %d)\n", depth
);
1287 /* ocfs2_shift_tree_depth will return us a buffer with
1288 * the new extent block (so we can pass that to
1289 * ocfs2_add_branch). */
1290 ret
= ocfs2_shift_tree_depth(osb
, handle
, inode
, et
,
1299 * Special case: we have room now if we shifted from
1300 * tree_depth 0, so no more work needs to be done.
1302 * We won't be calling add_branch, so pass
1303 * back *last_eb_bh as the new leaf. At depth
1304 * zero, it should always be null so there's
1305 * no reason to brelse.
1307 BUG_ON(*last_eb_bh
);
1314 /* call ocfs2_add_branch to add the final part of the tree with
1316 mlog(0, "add branch. bh = %p\n", bh
);
1317 ret
= ocfs2_add_branch(osb
, handle
, inode
, et
, bh
, last_eb_bh
,
1326 *final_depth
= depth
;
1332 * This function will discard the rightmost extent record.
1334 static void ocfs2_shift_records_right(struct ocfs2_extent_list
*el
)
1336 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1337 int count
= le16_to_cpu(el
->l_count
);
1338 unsigned int num_bytes
;
1341 /* This will cause us to go off the end of our extent list. */
1342 BUG_ON(next_free
>= count
);
1344 num_bytes
= sizeof(struct ocfs2_extent_rec
) * next_free
;
1346 memmove(&el
->l_recs
[1], &el
->l_recs
[0], num_bytes
);
1349 static void ocfs2_rotate_leaf(struct ocfs2_extent_list
*el
,
1350 struct ocfs2_extent_rec
*insert_rec
)
1352 int i
, insert_index
, next_free
, has_empty
, num_bytes
;
1353 u32 insert_cpos
= le32_to_cpu(insert_rec
->e_cpos
);
1354 struct ocfs2_extent_rec
*rec
;
1356 next_free
= le16_to_cpu(el
->l_next_free_rec
);
1357 has_empty
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
1361 /* The tree code before us didn't allow enough room in the leaf. */
1362 BUG_ON(el
->l_next_free_rec
== el
->l_count
&& !has_empty
);
1365 * The easiest way to approach this is to just remove the
1366 * empty extent and temporarily decrement next_free.
1370 * If next_free was 1 (only an empty extent), this
1371 * loop won't execute, which is fine. We still want
1372 * the decrement above to happen.
1374 for(i
= 0; i
< (next_free
- 1); i
++)
1375 el
->l_recs
[i
] = el
->l_recs
[i
+1];
1381 * Figure out what the new record index should be.
1383 for(i
= 0; i
< next_free
; i
++) {
1384 rec
= &el
->l_recs
[i
];
1386 if (insert_cpos
< le32_to_cpu(rec
->e_cpos
))
1391 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1392 insert_cpos
, insert_index
, has_empty
, next_free
, le16_to_cpu(el
->l_count
));
1394 BUG_ON(insert_index
< 0);
1395 BUG_ON(insert_index
>= le16_to_cpu(el
->l_count
));
1396 BUG_ON(insert_index
> next_free
);
1399 * No need to memmove if we're just adding to the tail.
1401 if (insert_index
!= next_free
) {
1402 BUG_ON(next_free
>= le16_to_cpu(el
->l_count
));
1404 num_bytes
= next_free
- insert_index
;
1405 num_bytes
*= sizeof(struct ocfs2_extent_rec
);
1406 memmove(&el
->l_recs
[insert_index
+ 1],
1407 &el
->l_recs
[insert_index
],
1412 * Either we had an empty extent, and need to re-increment or
1413 * there was no empty extent on a non full rightmost leaf node,
1414 * in which case we still need to increment.
1417 el
->l_next_free_rec
= cpu_to_le16(next_free
);
1419 * Make sure none of the math above just messed up our tree.
1421 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) > le16_to_cpu(el
->l_count
));
1423 el
->l_recs
[insert_index
] = *insert_rec
;
1427 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list
*el
)
1429 int size
, num_recs
= le16_to_cpu(el
->l_next_free_rec
);
1431 BUG_ON(num_recs
== 0);
1433 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
1435 size
= num_recs
* sizeof(struct ocfs2_extent_rec
);
1436 memmove(&el
->l_recs
[0], &el
->l_recs
[1], size
);
1437 memset(&el
->l_recs
[num_recs
], 0,
1438 sizeof(struct ocfs2_extent_rec
));
1439 el
->l_next_free_rec
= cpu_to_le16(num_recs
);
1444 * Create an empty extent record .
1446 * l_next_free_rec may be updated.
1448 * If an empty extent already exists do nothing.
1450 static void ocfs2_create_empty_extent(struct ocfs2_extent_list
*el
)
1452 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1454 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
1459 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
1462 mlog_bug_on_msg(el
->l_count
== el
->l_next_free_rec
,
1463 "Asked to create an empty extent in a full list:\n"
1464 "count = %u, tree depth = %u",
1465 le16_to_cpu(el
->l_count
),
1466 le16_to_cpu(el
->l_tree_depth
));
1468 ocfs2_shift_records_right(el
);
1471 le16_add_cpu(&el
->l_next_free_rec
, 1);
1472 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1476 * For a rotation which involves two leaf nodes, the "root node" is
1477 * the lowest level tree node which contains a path to both leafs. This
1478 * resulting set of information can be used to form a complete "subtree"
1480 * This function is passed two full paths from the dinode down to a
1481 * pair of adjacent leaves. It's task is to figure out which path
1482 * index contains the subtree root - this can be the root index itself
1483 * in a worst-case rotation.
1485 * The array index of the subtree root is passed back.
1487 static int ocfs2_find_subtree_root(struct inode
*inode
,
1488 struct ocfs2_path
*left
,
1489 struct ocfs2_path
*right
)
1494 * Check that the caller passed in two paths from the same tree.
1496 BUG_ON(path_root_bh(left
) != path_root_bh(right
));
1502 * The caller didn't pass two adjacent paths.
1504 mlog_bug_on_msg(i
> left
->p_tree_depth
,
1505 "Inode %lu, left depth %u, right depth %u\n"
1506 "left leaf blk %llu, right leaf blk %llu\n",
1507 inode
->i_ino
, left
->p_tree_depth
,
1508 right
->p_tree_depth
,
1509 (unsigned long long)path_leaf_bh(left
)->b_blocknr
,
1510 (unsigned long long)path_leaf_bh(right
)->b_blocknr
);
1511 } while (left
->p_node
[i
].bh
->b_blocknr
==
1512 right
->p_node
[i
].bh
->b_blocknr
);
1517 typedef void (path_insert_t
)(void *, struct buffer_head
*);
1520 * Traverse a btree path in search of cpos, starting at root_el.
1522 * This code can be called with a cpos larger than the tree, in which
1523 * case it will return the rightmost path.
1525 static int __ocfs2_find_path(struct inode
*inode
,
1526 struct ocfs2_extent_list
*root_el
, u32 cpos
,
1527 path_insert_t
*func
, void *data
)
1532 struct buffer_head
*bh
= NULL
;
1533 struct ocfs2_extent_block
*eb
;
1534 struct ocfs2_extent_list
*el
;
1535 struct ocfs2_extent_rec
*rec
;
1536 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1539 while (el
->l_tree_depth
) {
1540 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1541 ocfs2_error(inode
->i_sb
,
1542 "Inode %llu has empty extent list at "
1544 (unsigned long long)oi
->ip_blkno
,
1545 le16_to_cpu(el
->l_tree_depth
));
1551 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
) - 1; i
++) {
1552 rec
= &el
->l_recs
[i
];
1555 * In the case that cpos is off the allocation
1556 * tree, this should just wind up returning the
1559 range
= le32_to_cpu(rec
->e_cpos
) +
1560 ocfs2_rec_clusters(el
, rec
);
1561 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
1565 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1567 ocfs2_error(inode
->i_sb
,
1568 "Inode %llu has bad blkno in extent list "
1569 "at depth %u (index %d)\n",
1570 (unsigned long long)oi
->ip_blkno
,
1571 le16_to_cpu(el
->l_tree_depth
), i
);
1578 ret
= ocfs2_read_extent_block(inode
, blkno
, &bh
);
1584 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1587 if (le16_to_cpu(el
->l_next_free_rec
) >
1588 le16_to_cpu(el
->l_count
)) {
1589 ocfs2_error(inode
->i_sb
,
1590 "Inode %llu has bad count in extent list "
1591 "at block %llu (next free=%u, count=%u)\n",
1592 (unsigned long long)oi
->ip_blkno
,
1593 (unsigned long long)bh
->b_blocknr
,
1594 le16_to_cpu(el
->l_next_free_rec
),
1595 le16_to_cpu(el
->l_count
));
1606 * Catch any trailing bh that the loop didn't handle.
1614 * Given an initialized path (that is, it has a valid root extent
1615 * list), this function will traverse the btree in search of the path
1616 * which would contain cpos.
1618 * The path traveled is recorded in the path structure.
1620 * Note that this will not do any comparisons on leaf node extent
1621 * records, so it will work fine in the case that we just added a tree
1624 struct find_path_data
{
1626 struct ocfs2_path
*path
;
1628 static void find_path_ins(void *data
, struct buffer_head
*bh
)
1630 struct find_path_data
*fp
= data
;
1633 ocfs2_path_insert_eb(fp
->path
, fp
->index
, bh
);
1636 static int ocfs2_find_path(struct inode
*inode
, struct ocfs2_path
*path
,
1639 struct find_path_data data
;
1643 return __ocfs2_find_path(inode
, path_root_el(path
), cpos
,
1644 find_path_ins
, &data
);
1647 static void find_leaf_ins(void *data
, struct buffer_head
*bh
)
1649 struct ocfs2_extent_block
*eb
=(struct ocfs2_extent_block
*)bh
->b_data
;
1650 struct ocfs2_extent_list
*el
= &eb
->h_list
;
1651 struct buffer_head
**ret
= data
;
1653 /* We want to retain only the leaf block. */
1654 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
1660 * Find the leaf block in the tree which would contain cpos. No
1661 * checking of the actual leaf is done.
1663 * Some paths want to call this instead of allocating a path structure
1664 * and calling ocfs2_find_path().
1666 * This function doesn't handle non btree extent lists.
1668 int ocfs2_find_leaf(struct inode
*inode
, struct ocfs2_extent_list
*root_el
,
1669 u32 cpos
, struct buffer_head
**leaf_bh
)
1672 struct buffer_head
*bh
= NULL
;
1674 ret
= __ocfs2_find_path(inode
, root_el
, cpos
, find_leaf_ins
, &bh
);
1686 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1688 * Basically, we've moved stuff around at the bottom of the tree and
1689 * we need to fix up the extent records above the changes to reflect
1692 * left_rec: the record on the left.
1693 * left_child_el: is the child list pointed to by left_rec
1694 * right_rec: the record to the right of left_rec
1695 * right_child_el: is the child list pointed to by right_rec
1697 * By definition, this only works on interior nodes.
1699 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec
*left_rec
,
1700 struct ocfs2_extent_list
*left_child_el
,
1701 struct ocfs2_extent_rec
*right_rec
,
1702 struct ocfs2_extent_list
*right_child_el
)
1704 u32 left_clusters
, right_end
;
1707 * Interior nodes never have holes. Their cpos is the cpos of
1708 * the leftmost record in their child list. Their cluster
1709 * count covers the full theoretical range of their child list
1710 * - the range between their cpos and the cpos of the record
1711 * immediately to their right.
1713 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[0].e_cpos
);
1714 if (ocfs2_is_empty_extent(&right_child_el
->l_recs
[0])) {
1715 BUG_ON(le16_to_cpu(right_child_el
->l_next_free_rec
) <= 1);
1716 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[1].e_cpos
);
1718 left_clusters
-= le32_to_cpu(left_rec
->e_cpos
);
1719 left_rec
->e_int_clusters
= cpu_to_le32(left_clusters
);
1722 * Calculate the rightmost cluster count boundary before
1723 * moving cpos - we will need to adjust clusters after
1724 * updating e_cpos to keep the same highest cluster count.
1726 right_end
= le32_to_cpu(right_rec
->e_cpos
);
1727 right_end
+= le32_to_cpu(right_rec
->e_int_clusters
);
1729 right_rec
->e_cpos
= left_rec
->e_cpos
;
1730 le32_add_cpu(&right_rec
->e_cpos
, left_clusters
);
1732 right_end
-= le32_to_cpu(right_rec
->e_cpos
);
1733 right_rec
->e_int_clusters
= cpu_to_le32(right_end
);
1737 * Adjust the adjacent root node records involved in a
1738 * rotation. left_el_blkno is passed in as a key so that we can easily
1739 * find it's index in the root list.
1741 static void ocfs2_adjust_root_records(struct ocfs2_extent_list
*root_el
,
1742 struct ocfs2_extent_list
*left_el
,
1743 struct ocfs2_extent_list
*right_el
,
1748 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) <=
1749 le16_to_cpu(left_el
->l_tree_depth
));
1751 for(i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
) - 1; i
++) {
1752 if (le64_to_cpu(root_el
->l_recs
[i
].e_blkno
) == left_el_blkno
)
1757 * The path walking code should have never returned a root and
1758 * two paths which are not adjacent.
1760 BUG_ON(i
>= (le16_to_cpu(root_el
->l_next_free_rec
) - 1));
1762 ocfs2_adjust_adjacent_records(&root_el
->l_recs
[i
], left_el
,
1763 &root_el
->l_recs
[i
+ 1], right_el
);
1767 * We've changed a leaf block (in right_path) and need to reflect that
1768 * change back up the subtree.
1770 * This happens in multiple places:
1771 * - When we've moved an extent record from the left path leaf to the right
1772 * path leaf to make room for an empty extent in the left path leaf.
1773 * - When our insert into the right path leaf is at the leftmost edge
1774 * and requires an update of the path immediately to it's left. This
1775 * can occur at the end of some types of rotation and appending inserts.
1776 * - When we've adjusted the last extent record in the left path leaf and the
1777 * 1st extent record in the right path leaf during cross extent block merge.
1779 static void ocfs2_complete_edge_insert(struct inode
*inode
, handle_t
*handle
,
1780 struct ocfs2_path
*left_path
,
1781 struct ocfs2_path
*right_path
,
1785 struct ocfs2_extent_list
*el
, *left_el
, *right_el
;
1786 struct ocfs2_extent_rec
*left_rec
, *right_rec
;
1787 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
1790 * Update the counts and position values within all the
1791 * interior nodes to reflect the leaf rotation we just did.
1793 * The root node is handled below the loop.
1795 * We begin the loop with right_el and left_el pointing to the
1796 * leaf lists and work our way up.
1798 * NOTE: within this loop, left_el and right_el always refer
1799 * to the *child* lists.
1801 left_el
= path_leaf_el(left_path
);
1802 right_el
= path_leaf_el(right_path
);
1803 for(i
= left_path
->p_tree_depth
- 1; i
> subtree_index
; i
--) {
1804 mlog(0, "Adjust records at index %u\n", i
);
1807 * One nice property of knowing that all of these
1808 * nodes are below the root is that we only deal with
1809 * the leftmost right node record and the rightmost
1812 el
= left_path
->p_node
[i
].el
;
1813 idx
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
1814 left_rec
= &el
->l_recs
[idx
];
1816 el
= right_path
->p_node
[i
].el
;
1817 right_rec
= &el
->l_recs
[0];
1819 ocfs2_adjust_adjacent_records(left_rec
, left_el
, right_rec
,
1822 ret
= ocfs2_journal_dirty(handle
, left_path
->p_node
[i
].bh
);
1826 ret
= ocfs2_journal_dirty(handle
, right_path
->p_node
[i
].bh
);
1831 * Setup our list pointers now so that the current
1832 * parents become children in the next iteration.
1834 left_el
= left_path
->p_node
[i
].el
;
1835 right_el
= right_path
->p_node
[i
].el
;
1839 * At the root node, adjust the two adjacent records which
1840 * begin our path to the leaves.
1843 el
= left_path
->p_node
[subtree_index
].el
;
1844 left_el
= left_path
->p_node
[subtree_index
+ 1].el
;
1845 right_el
= right_path
->p_node
[subtree_index
+ 1].el
;
1847 ocfs2_adjust_root_records(el
, left_el
, right_el
,
1848 left_path
->p_node
[subtree_index
+ 1].bh
->b_blocknr
);
1850 root_bh
= left_path
->p_node
[subtree_index
].bh
;
1852 ret
= ocfs2_journal_dirty(handle
, root_bh
);
1857 static int ocfs2_rotate_subtree_right(struct inode
*inode
,
1859 struct ocfs2_path
*left_path
,
1860 struct ocfs2_path
*right_path
,
1864 struct buffer_head
*right_leaf_bh
;
1865 struct buffer_head
*left_leaf_bh
= NULL
;
1866 struct buffer_head
*root_bh
;
1867 struct ocfs2_extent_list
*right_el
, *left_el
;
1868 struct ocfs2_extent_rec move_rec
;
1870 left_leaf_bh
= path_leaf_bh(left_path
);
1871 left_el
= path_leaf_el(left_path
);
1873 if (left_el
->l_next_free_rec
!= left_el
->l_count
) {
1874 ocfs2_error(inode
->i_sb
,
1875 "Inode %llu has non-full interior leaf node %llu"
1877 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1878 (unsigned long long)left_leaf_bh
->b_blocknr
,
1879 le16_to_cpu(left_el
->l_next_free_rec
));
1884 * This extent block may already have an empty record, so we
1885 * return early if so.
1887 if (ocfs2_is_empty_extent(&left_el
->l_recs
[0]))
1890 root_bh
= left_path
->p_node
[subtree_index
].bh
;
1891 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
1893 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
1894 OCFS2_JOURNAL_ACCESS_WRITE
);
1900 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
1901 ret
= ocfs2_journal_access(handle
, inode
,
1902 right_path
->p_node
[i
].bh
,
1903 OCFS2_JOURNAL_ACCESS_WRITE
);
1909 ret
= ocfs2_journal_access(handle
, inode
,
1910 left_path
->p_node
[i
].bh
,
1911 OCFS2_JOURNAL_ACCESS_WRITE
);
1918 right_leaf_bh
= path_leaf_bh(right_path
);
1919 right_el
= path_leaf_el(right_path
);
1921 /* This is a code error, not a disk corruption. */
1922 mlog_bug_on_msg(!right_el
->l_next_free_rec
, "Inode %llu: Rotate fails "
1923 "because rightmost leaf block %llu is empty\n",
1924 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1925 (unsigned long long)right_leaf_bh
->b_blocknr
);
1927 ocfs2_create_empty_extent(right_el
);
1929 ret
= ocfs2_journal_dirty(handle
, right_leaf_bh
);
1935 /* Do the copy now. */
1936 i
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
1937 move_rec
= left_el
->l_recs
[i
];
1938 right_el
->l_recs
[0] = move_rec
;
1941 * Clear out the record we just copied and shift everything
1942 * over, leaving an empty extent in the left leaf.
1944 * We temporarily subtract from next_free_rec so that the
1945 * shift will lose the tail record (which is now defunct).
1947 le16_add_cpu(&left_el
->l_next_free_rec
, -1);
1948 ocfs2_shift_records_right(left_el
);
1949 memset(&left_el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1950 le16_add_cpu(&left_el
->l_next_free_rec
, 1);
1952 ret
= ocfs2_journal_dirty(handle
, left_leaf_bh
);
1958 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
1966 * Given a full path, determine what cpos value would return us a path
1967 * containing the leaf immediately to the left of the current one.
1969 * Will return zero if the path passed in is already the leftmost path.
1971 static int ocfs2_find_cpos_for_left_leaf(struct super_block
*sb
,
1972 struct ocfs2_path
*path
, u32
*cpos
)
1976 struct ocfs2_extent_list
*el
;
1978 BUG_ON(path
->p_tree_depth
== 0);
1982 blkno
= path_leaf_bh(path
)->b_blocknr
;
1984 /* Start at the tree node just above the leaf and work our way up. */
1985 i
= path
->p_tree_depth
- 1;
1987 el
= path
->p_node
[i
].el
;
1990 * Find the extent record just before the one in our
1993 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
1994 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
1998 * We've determined that the
1999 * path specified is already
2000 * the leftmost one - return a
2006 * The leftmost record points to our
2007 * leaf - we need to travel up the
2013 *cpos
= le32_to_cpu(el
->l_recs
[j
- 1].e_cpos
);
2014 *cpos
= *cpos
+ ocfs2_rec_clusters(el
,
2015 &el
->l_recs
[j
- 1]);
2022 * If we got here, we never found a valid node where
2023 * the tree indicated one should be.
2026 "Invalid extent tree at extent block %llu\n",
2027 (unsigned long long)blkno
);
2032 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2041 * Extend the transaction by enough credits to complete the rotation,
2042 * and still leave at least the original number of credits allocated
2043 * to this transaction.
2045 static int ocfs2_extend_rotate_transaction(handle_t
*handle
, int subtree_depth
,
2047 struct ocfs2_path
*path
)
2049 int credits
= (path
->p_tree_depth
- subtree_depth
) * 2 + 1 + op_credits
;
2051 if (handle
->h_buffer_credits
< credits
)
2052 return ocfs2_extend_trans(handle
, credits
);
2058 * Trap the case where we're inserting into the theoretical range past
2059 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2060 * whose cpos is less than ours into the right leaf.
2062 * It's only necessary to look at the rightmost record of the left
2063 * leaf because the logic that calls us should ensure that the
2064 * theoretical ranges in the path components above the leaves are
2067 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path
*left_path
,
2070 struct ocfs2_extent_list
*left_el
;
2071 struct ocfs2_extent_rec
*rec
;
2074 left_el
= path_leaf_el(left_path
);
2075 next_free
= le16_to_cpu(left_el
->l_next_free_rec
);
2076 rec
= &left_el
->l_recs
[next_free
- 1];
2078 if (insert_cpos
> le32_to_cpu(rec
->e_cpos
))
2083 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list
*el
, u32 cpos
)
2085 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
2087 struct ocfs2_extent_rec
*rec
;
2092 rec
= &el
->l_recs
[0];
2093 if (ocfs2_is_empty_extent(rec
)) {
2097 rec
= &el
->l_recs
[1];
2100 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2101 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
2107 * Rotate all the records in a btree right one record, starting at insert_cpos.
2109 * The path to the rightmost leaf should be passed in.
2111 * The array is assumed to be large enough to hold an entire path (tree depth).
2113 * Upon succesful return from this function:
2115 * - The 'right_path' array will contain a path to the leaf block
2116 * whose range contains e_cpos.
2117 * - That leaf block will have a single empty extent in list index 0.
2118 * - In the case that the rotation requires a post-insert update,
2119 * *ret_left_path will contain a valid path which can be passed to
2120 * ocfs2_insert_path().
2122 static int ocfs2_rotate_tree_right(struct inode
*inode
,
2124 enum ocfs2_split_type split
,
2126 struct ocfs2_path
*right_path
,
2127 struct ocfs2_path
**ret_left_path
)
2129 int ret
, start
, orig_credits
= handle
->h_buffer_credits
;
2131 struct ocfs2_path
*left_path
= NULL
;
2133 *ret_left_path
= NULL
;
2135 left_path
= ocfs2_new_path(path_root_bh(right_path
),
2136 path_root_el(right_path
));
2143 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
, &cpos
);
2149 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos
, cpos
);
2152 * What we want to do here is:
2154 * 1) Start with the rightmost path.
2156 * 2) Determine a path to the leaf block directly to the left
2159 * 3) Determine the 'subtree root' - the lowest level tree node
2160 * which contains a path to both leaves.
2162 * 4) Rotate the subtree.
2164 * 5) Find the next subtree by considering the left path to be
2165 * the new right path.
2167 * The check at the top of this while loop also accepts
2168 * insert_cpos == cpos because cpos is only a _theoretical_
2169 * value to get us the left path - insert_cpos might very well
2170 * be filling that hole.
2172 * Stop at a cpos of '0' because we either started at the
2173 * leftmost branch (i.e., a tree with one branch and a
2174 * rotation inside of it), or we've gone as far as we can in
2175 * rotating subtrees.
2177 while (cpos
&& insert_cpos
<= cpos
) {
2178 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2181 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2187 mlog_bug_on_msg(path_leaf_bh(left_path
) ==
2188 path_leaf_bh(right_path
),
2189 "Inode %lu: error during insert of %u "
2190 "(left path cpos %u) results in two identical "
2191 "paths ending at %llu\n",
2192 inode
->i_ino
, insert_cpos
, cpos
,
2193 (unsigned long long)
2194 path_leaf_bh(left_path
)->b_blocknr
);
2196 if (split
== SPLIT_NONE
&&
2197 ocfs2_rotate_requires_path_adjustment(left_path
,
2201 * We've rotated the tree as much as we
2202 * should. The rest is up to
2203 * ocfs2_insert_path() to complete, after the
2204 * record insertion. We indicate this
2205 * situation by returning the left path.
2207 * The reason we don't adjust the records here
2208 * before the record insert is that an error
2209 * later might break the rule where a parent
2210 * record e_cpos will reflect the actual
2211 * e_cpos of the 1st nonempty record of the
2214 *ret_left_path
= left_path
;
2218 start
= ocfs2_find_subtree_root(inode
, left_path
, right_path
);
2220 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2222 (unsigned long long) right_path
->p_node
[start
].bh
->b_blocknr
,
2223 right_path
->p_tree_depth
);
2225 ret
= ocfs2_extend_rotate_transaction(handle
, start
,
2226 orig_credits
, right_path
);
2232 ret
= ocfs2_rotate_subtree_right(inode
, handle
, left_path
,
2239 if (split
!= SPLIT_NONE
&&
2240 ocfs2_leftmost_rec_contains(path_leaf_el(right_path
),
2243 * A rotate moves the rightmost left leaf
2244 * record over to the leftmost right leaf
2245 * slot. If we're doing an extent split
2246 * instead of a real insert, then we have to
2247 * check that the extent to be split wasn't
2248 * just moved over. If it was, then we can
2249 * exit here, passing left_path back -
2250 * ocfs2_split_extent() is smart enough to
2251 * search both leaves.
2253 *ret_left_path
= left_path
;
2258 * There is no need to re-read the next right path
2259 * as we know that it'll be our current left
2260 * path. Optimize by copying values instead.
2262 ocfs2_mv_path(right_path
, left_path
);
2264 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
2273 ocfs2_free_path(left_path
);
2279 static void ocfs2_update_edge_lengths(struct inode
*inode
, handle_t
*handle
,
2280 struct ocfs2_path
*path
)
2283 struct ocfs2_extent_rec
*rec
;
2284 struct ocfs2_extent_list
*el
;
2285 struct ocfs2_extent_block
*eb
;
2288 /* Path should always be rightmost. */
2289 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2290 BUG_ON(eb
->h_next_leaf_blk
!= 0ULL);
2293 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
2294 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2295 rec
= &el
->l_recs
[idx
];
2296 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2298 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
2299 el
= path
->p_node
[i
].el
;
2300 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2301 rec
= &el
->l_recs
[idx
];
2303 rec
->e_int_clusters
= cpu_to_le32(range
);
2304 le32_add_cpu(&rec
->e_int_clusters
, -le32_to_cpu(rec
->e_cpos
));
2306 ocfs2_journal_dirty(handle
, path
->p_node
[i
].bh
);
2310 static void ocfs2_unlink_path(struct inode
*inode
, handle_t
*handle
,
2311 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2312 struct ocfs2_path
*path
, int unlink_start
)
2315 struct ocfs2_extent_block
*eb
;
2316 struct ocfs2_extent_list
*el
;
2317 struct buffer_head
*bh
;
2319 for(i
= unlink_start
; i
< path_num_items(path
); i
++) {
2320 bh
= path
->p_node
[i
].bh
;
2322 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
2324 * Not all nodes might have had their final count
2325 * decremented by the caller - handle this here.
2328 if (le16_to_cpu(el
->l_next_free_rec
) > 1) {
2330 "Inode %llu, attempted to remove extent block "
2331 "%llu with %u records\n",
2332 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2333 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
2334 le16_to_cpu(el
->l_next_free_rec
));
2336 ocfs2_journal_dirty(handle
, bh
);
2337 ocfs2_remove_from_cache(inode
, bh
);
2341 el
->l_next_free_rec
= 0;
2342 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2344 ocfs2_journal_dirty(handle
, bh
);
2346 ret
= ocfs2_cache_extent_block_free(dealloc
, eb
);
2350 ocfs2_remove_from_cache(inode
, bh
);
2354 static void ocfs2_unlink_subtree(struct inode
*inode
, handle_t
*handle
,
2355 struct ocfs2_path
*left_path
,
2356 struct ocfs2_path
*right_path
,
2358 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2361 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
2362 struct ocfs2_extent_list
*root_el
= left_path
->p_node
[subtree_index
].el
;
2363 struct ocfs2_extent_list
*el
;
2364 struct ocfs2_extent_block
*eb
;
2366 el
= path_leaf_el(left_path
);
2368 eb
= (struct ocfs2_extent_block
*)right_path
->p_node
[subtree_index
+ 1].bh
->b_data
;
2370 for(i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
2371 if (root_el
->l_recs
[i
].e_blkno
== eb
->h_blkno
)
2374 BUG_ON(i
>= le16_to_cpu(root_el
->l_next_free_rec
));
2376 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
2377 le16_add_cpu(&root_el
->l_next_free_rec
, -1);
2379 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2380 eb
->h_next_leaf_blk
= 0;
2382 ocfs2_journal_dirty(handle
, root_bh
);
2383 ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2385 ocfs2_unlink_path(inode
, handle
, dealloc
, right_path
,
2389 static int ocfs2_rotate_subtree_left(struct inode
*inode
, handle_t
*handle
,
2390 struct ocfs2_path
*left_path
,
2391 struct ocfs2_path
*right_path
,
2393 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2395 struct ocfs2_extent_tree
*et
)
2397 int ret
, i
, del_right_subtree
= 0, right_has_empty
= 0;
2398 struct buffer_head
*root_bh
, *et_root_bh
= path_root_bh(right_path
);
2399 struct ocfs2_extent_list
*right_leaf_el
, *left_leaf_el
;
2400 struct ocfs2_extent_block
*eb
;
2404 right_leaf_el
= path_leaf_el(right_path
);
2405 left_leaf_el
= path_leaf_el(left_path
);
2406 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2407 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2409 if (!ocfs2_is_empty_extent(&left_leaf_el
->l_recs
[0]))
2412 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(right_path
)->b_data
;
2413 if (ocfs2_is_empty_extent(&right_leaf_el
->l_recs
[0])) {
2415 * It's legal for us to proceed if the right leaf is
2416 * the rightmost one and it has an empty extent. There
2417 * are two cases to handle - whether the leaf will be
2418 * empty after removal or not. If the leaf isn't empty
2419 * then just remove the empty extent up front. The
2420 * next block will handle empty leaves by flagging
2423 * Non rightmost leaves will throw -EAGAIN and the
2424 * caller can manually move the subtree and retry.
2427 if (eb
->h_next_leaf_blk
!= 0ULL)
2430 if (le16_to_cpu(right_leaf_el
->l_next_free_rec
) > 1) {
2431 ret
= ocfs2_journal_access(handle
, inode
,
2432 path_leaf_bh(right_path
),
2433 OCFS2_JOURNAL_ACCESS_WRITE
);
2439 ocfs2_remove_empty_extent(right_leaf_el
);
2441 right_has_empty
= 1;
2444 if (eb
->h_next_leaf_blk
== 0ULL &&
2445 le16_to_cpu(right_leaf_el
->l_next_free_rec
) == 1) {
2447 * We have to update i_last_eb_blk during the meta
2450 ret
= ocfs2_journal_access(handle
, inode
, et_root_bh
,
2451 OCFS2_JOURNAL_ACCESS_WRITE
);
2457 del_right_subtree
= 1;
2461 * Getting here with an empty extent in the right path implies
2462 * that it's the rightmost path and will be deleted.
2464 BUG_ON(right_has_empty
&& !del_right_subtree
);
2466 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
2467 OCFS2_JOURNAL_ACCESS_WRITE
);
2473 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2474 ret
= ocfs2_journal_access(handle
, inode
,
2475 right_path
->p_node
[i
].bh
,
2476 OCFS2_JOURNAL_ACCESS_WRITE
);
2482 ret
= ocfs2_journal_access(handle
, inode
,
2483 left_path
->p_node
[i
].bh
,
2484 OCFS2_JOURNAL_ACCESS_WRITE
);
2491 if (!right_has_empty
) {
2493 * Only do this if we're moving a real
2494 * record. Otherwise, the action is delayed until
2495 * after removal of the right path in which case we
2496 * can do a simple shift to remove the empty extent.
2498 ocfs2_rotate_leaf(left_leaf_el
, &right_leaf_el
->l_recs
[0]);
2499 memset(&right_leaf_el
->l_recs
[0], 0,
2500 sizeof(struct ocfs2_extent_rec
));
2502 if (eb
->h_next_leaf_blk
== 0ULL) {
2504 * Move recs over to get rid of empty extent, decrease
2505 * next_free. This is allowed to remove the last
2506 * extent in our leaf (setting l_next_free_rec to
2507 * zero) - the delete code below won't care.
2509 ocfs2_remove_empty_extent(right_leaf_el
);
2512 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2515 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
2519 if (del_right_subtree
) {
2520 ocfs2_unlink_subtree(inode
, handle
, left_path
, right_path
,
2521 subtree_index
, dealloc
);
2522 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2524 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2525 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2528 * Removal of the extent in the left leaf was skipped
2529 * above so we could delete the right path
2532 if (right_has_empty
)
2533 ocfs2_remove_empty_extent(left_leaf_el
);
2535 ret
= ocfs2_journal_dirty(handle
, et_root_bh
);
2541 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
2549 * Given a full path, determine what cpos value would return us a path
2550 * containing the leaf immediately to the right of the current one.
2552 * Will return zero if the path passed in is already the rightmost path.
2554 * This looks similar, but is subtly different to
2555 * ocfs2_find_cpos_for_left_leaf().
2557 static int ocfs2_find_cpos_for_right_leaf(struct super_block
*sb
,
2558 struct ocfs2_path
*path
, u32
*cpos
)
2562 struct ocfs2_extent_list
*el
;
2566 if (path
->p_tree_depth
== 0)
2569 blkno
= path_leaf_bh(path
)->b_blocknr
;
2571 /* Start at the tree node just above the leaf and work our way up. */
2572 i
= path
->p_tree_depth
- 1;
2576 el
= path
->p_node
[i
].el
;
2579 * Find the extent record just after the one in our
2582 next_free
= le16_to_cpu(el
->l_next_free_rec
);
2583 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2584 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2585 if (j
== (next_free
- 1)) {
2588 * We've determined that the
2589 * path specified is already
2590 * the rightmost one - return a
2596 * The rightmost record points to our
2597 * leaf - we need to travel up the
2603 *cpos
= le32_to_cpu(el
->l_recs
[j
+ 1].e_cpos
);
2609 * If we got here, we never found a valid node where
2610 * the tree indicated one should be.
2613 "Invalid extent tree at extent block %llu\n",
2614 (unsigned long long)blkno
);
2619 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2627 static int ocfs2_rotate_rightmost_leaf_left(struct inode
*inode
,
2629 struct buffer_head
*bh
,
2630 struct ocfs2_extent_list
*el
)
2634 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2637 ret
= ocfs2_journal_access(handle
, inode
, bh
,
2638 OCFS2_JOURNAL_ACCESS_WRITE
);
2644 ocfs2_remove_empty_extent(el
);
2646 ret
= ocfs2_journal_dirty(handle
, bh
);
2654 static int __ocfs2_rotate_tree_left(struct inode
*inode
,
2655 handle_t
*handle
, int orig_credits
,
2656 struct ocfs2_path
*path
,
2657 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2658 struct ocfs2_path
**empty_extent_path
,
2659 struct ocfs2_extent_tree
*et
)
2661 int ret
, subtree_root
, deleted
;
2663 struct ocfs2_path
*left_path
= NULL
;
2664 struct ocfs2_path
*right_path
= NULL
;
2666 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path
)->l_recs
[0])));
2668 *empty_extent_path
= NULL
;
2670 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, path
,
2677 left_path
= ocfs2_new_path(path_root_bh(path
),
2678 path_root_el(path
));
2685 ocfs2_cp_path(left_path
, path
);
2687 right_path
= ocfs2_new_path(path_root_bh(path
),
2688 path_root_el(path
));
2695 while (right_cpos
) {
2696 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
2702 subtree_root
= ocfs2_find_subtree_root(inode
, left_path
,
2705 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2707 (unsigned long long)
2708 right_path
->p_node
[subtree_root
].bh
->b_blocknr
,
2709 right_path
->p_tree_depth
);
2711 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_root
,
2712 orig_credits
, left_path
);
2719 * Caller might still want to make changes to the
2720 * tree root, so re-add it to the journal here.
2722 ret
= ocfs2_journal_access(handle
, inode
,
2723 path_root_bh(left_path
),
2724 OCFS2_JOURNAL_ACCESS_WRITE
);
2730 ret
= ocfs2_rotate_subtree_left(inode
, handle
, left_path
,
2731 right_path
, subtree_root
,
2732 dealloc
, &deleted
, et
);
2733 if (ret
== -EAGAIN
) {
2735 * The rotation has to temporarily stop due to
2736 * the right subtree having an empty
2737 * extent. Pass it back to the caller for a
2740 *empty_extent_path
= right_path
;
2750 * The subtree rotate might have removed records on
2751 * the rightmost edge. If so, then rotation is
2757 ocfs2_mv_path(left_path
, right_path
);
2759 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
2768 ocfs2_free_path(right_path
);
2769 ocfs2_free_path(left_path
);
2774 static int ocfs2_remove_rightmost_path(struct inode
*inode
, handle_t
*handle
,
2775 struct ocfs2_path
*path
,
2776 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2777 struct ocfs2_extent_tree
*et
)
2779 int ret
, subtree_index
;
2781 struct ocfs2_path
*left_path
= NULL
;
2782 struct ocfs2_extent_block
*eb
;
2783 struct ocfs2_extent_list
*el
;
2786 ret
= ocfs2_et_sanity_check(inode
, et
);
2790 * There's two ways we handle this depending on
2791 * whether path is the only existing one.
2793 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
2794 handle
->h_buffer_credits
,
2801 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
2807 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
2815 * We have a path to the left of this one - it needs
2818 left_path
= ocfs2_new_path(path_root_bh(path
),
2819 path_root_el(path
));
2826 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2832 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
2838 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
2840 ocfs2_unlink_subtree(inode
, handle
, left_path
, path
,
2841 subtree_index
, dealloc
);
2842 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2844 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2845 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2848 * 'path' is also the leftmost path which
2849 * means it must be the only one. This gets
2850 * handled differently because we want to
2851 * revert the inode back to having extents
2854 ocfs2_unlink_path(inode
, handle
, dealloc
, path
, 1);
2856 el
= et
->et_root_el
;
2857 el
->l_tree_depth
= 0;
2858 el
->l_next_free_rec
= 0;
2859 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2861 ocfs2_et_set_last_eb_blk(et
, 0);
2864 ocfs2_journal_dirty(handle
, path_root_bh(path
));
2867 ocfs2_free_path(left_path
);
2872 * Left rotation of btree records.
2874 * In many ways, this is (unsurprisingly) the opposite of right
2875 * rotation. We start at some non-rightmost path containing an empty
2876 * extent in the leaf block. The code works its way to the rightmost
2877 * path by rotating records to the left in every subtree.
2879 * This is used by any code which reduces the number of extent records
2880 * in a leaf. After removal, an empty record should be placed in the
2881 * leftmost list position.
2883 * This won't handle a length update of the rightmost path records if
2884 * the rightmost tree leaf record is removed so the caller is
2885 * responsible for detecting and correcting that.
2887 static int ocfs2_rotate_tree_left(struct inode
*inode
, handle_t
*handle
,
2888 struct ocfs2_path
*path
,
2889 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2890 struct ocfs2_extent_tree
*et
)
2892 int ret
, orig_credits
= handle
->h_buffer_credits
;
2893 struct ocfs2_path
*tmp_path
= NULL
, *restart_path
= NULL
;
2894 struct ocfs2_extent_block
*eb
;
2895 struct ocfs2_extent_list
*el
;
2897 el
= path_leaf_el(path
);
2898 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2901 if (path
->p_tree_depth
== 0) {
2902 rightmost_no_delete
:
2904 * Inline extents. This is trivially handled, so do
2907 ret
= ocfs2_rotate_rightmost_leaf_left(inode
, handle
,
2909 path_leaf_el(path
));
2916 * Handle rightmost branch now. There's several cases:
2917 * 1) simple rotation leaving records in there. That's trivial.
2918 * 2) rotation requiring a branch delete - there's no more
2919 * records left. Two cases of this:
2920 * a) There are branches to the left.
2921 * b) This is also the leftmost (the only) branch.
2923 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2924 * 2a) we need the left branch so that we can update it with the unlink
2925 * 2b) we need to bring the inode back to inline extents.
2928 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2930 if (eb
->h_next_leaf_blk
== 0) {
2932 * This gets a bit tricky if we're going to delete the
2933 * rightmost path. Get the other cases out of the way
2936 if (le16_to_cpu(el
->l_next_free_rec
) > 1)
2937 goto rightmost_no_delete
;
2939 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
2941 ocfs2_error(inode
->i_sb
,
2942 "Inode %llu has empty extent block at %llu",
2943 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2944 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
2949 * XXX: The caller can not trust "path" any more after
2950 * this as it will have been deleted. What do we do?
2952 * In theory the rotate-for-merge code will never get
2953 * here because it'll always ask for a rotate in a
2957 ret
= ocfs2_remove_rightmost_path(inode
, handle
, path
,
2965 * Now we can loop, remembering the path we get from -EAGAIN
2966 * and restarting from there.
2969 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
, path
,
2970 dealloc
, &restart_path
, et
);
2971 if (ret
&& ret
!= -EAGAIN
) {
2976 while (ret
== -EAGAIN
) {
2977 tmp_path
= restart_path
;
2978 restart_path
= NULL
;
2980 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
,
2983 if (ret
&& ret
!= -EAGAIN
) {
2988 ocfs2_free_path(tmp_path
);
2996 ocfs2_free_path(tmp_path
);
2997 ocfs2_free_path(restart_path
);
3001 static void ocfs2_cleanup_merge(struct ocfs2_extent_list
*el
,
3004 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[index
];
3007 if (rec
->e_leaf_clusters
== 0) {
3009 * We consumed all of the merged-from record. An empty
3010 * extent cannot exist anywhere but the 1st array
3011 * position, so move things over if the merged-from
3012 * record doesn't occupy that position.
3014 * This creates a new empty extent so the caller
3015 * should be smart enough to have removed any existing
3019 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3020 size
= index
* sizeof(struct ocfs2_extent_rec
);
3021 memmove(&el
->l_recs
[1], &el
->l_recs
[0], size
);
3025 * Always memset - the caller doesn't check whether it
3026 * created an empty extent, so there could be junk in
3029 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
3033 static int ocfs2_get_right_path(struct inode
*inode
,
3034 struct ocfs2_path
*left_path
,
3035 struct ocfs2_path
**ret_right_path
)
3039 struct ocfs2_path
*right_path
= NULL
;
3040 struct ocfs2_extent_list
*left_el
;
3042 *ret_right_path
= NULL
;
3044 /* This function shouldn't be called for non-trees. */
3045 BUG_ON(left_path
->p_tree_depth
== 0);
3047 left_el
= path_leaf_el(left_path
);
3048 BUG_ON(left_el
->l_next_free_rec
!= left_el
->l_count
);
3050 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
3057 /* This function shouldn't be called for the rightmost leaf. */
3058 BUG_ON(right_cpos
== 0);
3060 right_path
= ocfs2_new_path(path_root_bh(left_path
),
3061 path_root_el(left_path
));
3068 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
3074 *ret_right_path
= right_path
;
3077 ocfs2_free_path(right_path
);
3082 * Remove split_rec clusters from the record at index and merge them
3083 * onto the beginning of the record "next" to it.
3084 * For index < l_count - 1, the next means the extent rec at index + 1.
3085 * For index == l_count - 1, the "next" means the 1st extent rec of the
3086 * next extent block.
3088 static int ocfs2_merge_rec_right(struct inode
*inode
,
3089 struct ocfs2_path
*left_path
,
3091 struct ocfs2_extent_rec
*split_rec
,
3094 int ret
, next_free
, i
;
3095 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3096 struct ocfs2_extent_rec
*left_rec
;
3097 struct ocfs2_extent_rec
*right_rec
;
3098 struct ocfs2_extent_list
*right_el
;
3099 struct ocfs2_path
*right_path
= NULL
;
3100 int subtree_index
= 0;
3101 struct ocfs2_extent_list
*el
= path_leaf_el(left_path
);
3102 struct buffer_head
*bh
= path_leaf_bh(left_path
);
3103 struct buffer_head
*root_bh
= NULL
;
3105 BUG_ON(index
>= le16_to_cpu(el
->l_next_free_rec
));
3106 left_rec
= &el
->l_recs
[index
];
3108 if (index
== le16_to_cpu(el
->l_next_free_rec
) - 1 &&
3109 le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
)) {
3110 /* we meet with a cross extent block merge. */
3111 ret
= ocfs2_get_right_path(inode
, left_path
, &right_path
);
3117 right_el
= path_leaf_el(right_path
);
3118 next_free
= le16_to_cpu(right_el
->l_next_free_rec
);
3119 BUG_ON(next_free
<= 0);
3120 right_rec
= &right_el
->l_recs
[0];
3121 if (ocfs2_is_empty_extent(right_rec
)) {
3122 BUG_ON(next_free
<= 1);
3123 right_rec
= &right_el
->l_recs
[1];
3126 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3127 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3128 le32_to_cpu(right_rec
->e_cpos
));
3130 subtree_index
= ocfs2_find_subtree_root(inode
,
3131 left_path
, right_path
);
3133 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3134 handle
->h_buffer_credits
,
3141 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3142 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3144 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
3145 OCFS2_JOURNAL_ACCESS_WRITE
);
3151 for (i
= subtree_index
+ 1;
3152 i
< path_num_items(right_path
); i
++) {
3153 ret
= ocfs2_journal_access(handle
, inode
,
3154 right_path
->p_node
[i
].bh
,
3155 OCFS2_JOURNAL_ACCESS_WRITE
);
3161 ret
= ocfs2_journal_access(handle
, inode
,
3162 left_path
->p_node
[i
].bh
,
3163 OCFS2_JOURNAL_ACCESS_WRITE
);
3171 BUG_ON(index
== le16_to_cpu(el
->l_next_free_rec
) - 1);
3172 right_rec
= &el
->l_recs
[index
+ 1];
3175 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3176 OCFS2_JOURNAL_ACCESS_WRITE
);
3182 le16_add_cpu(&left_rec
->e_leaf_clusters
, -split_clusters
);
3184 le32_add_cpu(&right_rec
->e_cpos
, -split_clusters
);
3185 le64_add_cpu(&right_rec
->e_blkno
,
3186 -ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3187 le16_add_cpu(&right_rec
->e_leaf_clusters
, split_clusters
);
3189 ocfs2_cleanup_merge(el
, index
);
3191 ret
= ocfs2_journal_dirty(handle
, bh
);
3196 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
3200 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3201 right_path
, subtree_index
);
3205 ocfs2_free_path(right_path
);
3209 static int ocfs2_get_left_path(struct inode
*inode
,
3210 struct ocfs2_path
*right_path
,
3211 struct ocfs2_path
**ret_left_path
)
3215 struct ocfs2_path
*left_path
= NULL
;
3217 *ret_left_path
= NULL
;
3219 /* This function shouldn't be called for non-trees. */
3220 BUG_ON(right_path
->p_tree_depth
== 0);
3222 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
3223 right_path
, &left_cpos
);
3229 /* This function shouldn't be called for the leftmost leaf. */
3230 BUG_ON(left_cpos
== 0);
3232 left_path
= ocfs2_new_path(path_root_bh(right_path
),
3233 path_root_el(right_path
));
3240 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3246 *ret_left_path
= left_path
;
3249 ocfs2_free_path(left_path
);
3254 * Remove split_rec clusters from the record at index and merge them
3255 * onto the tail of the record "before" it.
3256 * For index > 0, the "before" means the extent rec at index - 1.
3258 * For index == 0, the "before" means the last record of the previous
3259 * extent block. And there is also a situation that we may need to
3260 * remove the rightmost leaf extent block in the right_path and change
3261 * the right path to indicate the new rightmost path.
3263 static int ocfs2_merge_rec_left(struct inode
*inode
,
3264 struct ocfs2_path
*right_path
,
3266 struct ocfs2_extent_rec
*split_rec
,
3267 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3268 struct ocfs2_extent_tree
*et
,
3271 int ret
, i
, subtree_index
= 0, has_empty_extent
= 0;
3272 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3273 struct ocfs2_extent_rec
*left_rec
;
3274 struct ocfs2_extent_rec
*right_rec
;
3275 struct ocfs2_extent_list
*el
= path_leaf_el(right_path
);
3276 struct buffer_head
*bh
= path_leaf_bh(right_path
);
3277 struct buffer_head
*root_bh
= NULL
;
3278 struct ocfs2_path
*left_path
= NULL
;
3279 struct ocfs2_extent_list
*left_el
;
3283 right_rec
= &el
->l_recs
[index
];
3285 /* we meet with a cross extent block merge. */
3286 ret
= ocfs2_get_left_path(inode
, right_path
, &left_path
);
3292 left_el
= path_leaf_el(left_path
);
3293 BUG_ON(le16_to_cpu(left_el
->l_next_free_rec
) !=
3294 le16_to_cpu(left_el
->l_count
));
3296 left_rec
= &left_el
->l_recs
[
3297 le16_to_cpu(left_el
->l_next_free_rec
) - 1];
3298 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3299 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3300 le32_to_cpu(split_rec
->e_cpos
));
3302 subtree_index
= ocfs2_find_subtree_root(inode
,
3303 left_path
, right_path
);
3305 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3306 handle
->h_buffer_credits
,
3313 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3314 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3316 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
3317 OCFS2_JOURNAL_ACCESS_WRITE
);
3323 for (i
= subtree_index
+ 1;
3324 i
< path_num_items(right_path
); i
++) {
3325 ret
= ocfs2_journal_access(handle
, inode
,
3326 right_path
->p_node
[i
].bh
,
3327 OCFS2_JOURNAL_ACCESS_WRITE
);
3333 ret
= ocfs2_journal_access(handle
, inode
,
3334 left_path
->p_node
[i
].bh
,
3335 OCFS2_JOURNAL_ACCESS_WRITE
);
3342 left_rec
= &el
->l_recs
[index
- 1];
3343 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
3344 has_empty_extent
= 1;
3347 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3348 OCFS2_JOURNAL_ACCESS_WRITE
);
3354 if (has_empty_extent
&& index
== 1) {
3356 * The easy case - we can just plop the record right in.
3358 *left_rec
= *split_rec
;
3360 has_empty_extent
= 0;
3362 le16_add_cpu(&left_rec
->e_leaf_clusters
, split_clusters
);
3364 le32_add_cpu(&right_rec
->e_cpos
, split_clusters
);
3365 le64_add_cpu(&right_rec
->e_blkno
,
3366 ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3367 le16_add_cpu(&right_rec
->e_leaf_clusters
, -split_clusters
);
3369 ocfs2_cleanup_merge(el
, index
);
3371 ret
= ocfs2_journal_dirty(handle
, bh
);
3376 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
3381 * In the situation that the right_rec is empty and the extent
3382 * block is empty also, ocfs2_complete_edge_insert can't handle
3383 * it and we need to delete the right extent block.
3385 if (le16_to_cpu(right_rec
->e_leaf_clusters
) == 0 &&
3386 le16_to_cpu(el
->l_next_free_rec
) == 1) {
3388 ret
= ocfs2_remove_rightmost_path(inode
, handle
,
3396 /* Now the rightmost extent block has been deleted.
3397 * So we use the new rightmost path.
3399 ocfs2_mv_path(right_path
, left_path
);
3402 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3403 right_path
, subtree_index
);
3407 ocfs2_free_path(left_path
);
3411 static int ocfs2_try_to_merge_extent(struct inode
*inode
,
3413 struct ocfs2_path
*path
,
3415 struct ocfs2_extent_rec
*split_rec
,
3416 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3417 struct ocfs2_merge_ctxt
*ctxt
,
3418 struct ocfs2_extent_tree
*et
)
3422 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
3423 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
3425 BUG_ON(ctxt
->c_contig_type
== CONTIG_NONE
);
3427 if (ctxt
->c_split_covers_rec
&& ctxt
->c_has_empty_extent
) {
3429 * The merge code will need to create an empty
3430 * extent to take the place of the newly
3431 * emptied slot. Remove any pre-existing empty
3432 * extents - having more than one in a leaf is
3435 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3442 rec
= &el
->l_recs
[split_index
];
3445 if (ctxt
->c_contig_type
== CONTIG_LEFTRIGHT
) {
3447 * Left-right contig implies this.
3449 BUG_ON(!ctxt
->c_split_covers_rec
);
3452 * Since the leftright insert always covers the entire
3453 * extent, this call will delete the insert record
3454 * entirely, resulting in an empty extent record added to
3457 * Since the adding of an empty extent shifts
3458 * everything back to the right, there's no need to
3459 * update split_index here.
3461 * When the split_index is zero, we need to merge it to the
3462 * prevoius extent block. It is more efficient and easier
3463 * if we do merge_right first and merge_left later.
3465 ret
= ocfs2_merge_rec_right(inode
, path
,
3474 * We can only get this from logic error above.
3476 BUG_ON(!ocfs2_is_empty_extent(&el
->l_recs
[0]));
3478 /* The merge left us with an empty extent, remove it. */
3479 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3486 rec
= &el
->l_recs
[split_index
];
3489 * Note that we don't pass split_rec here on purpose -
3490 * we've merged it into the rec already.
3492 ret
= ocfs2_merge_rec_left(inode
, path
,
3502 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3505 * Error from this last rotate is not critical, so
3506 * print but don't bubble it up.
3513 * Merge a record to the left or right.
3515 * 'contig_type' is relative to the existing record,
3516 * so for example, if we're "right contig", it's to
3517 * the record on the left (hence the left merge).
3519 if (ctxt
->c_contig_type
== CONTIG_RIGHT
) {
3520 ret
= ocfs2_merge_rec_left(inode
,
3530 ret
= ocfs2_merge_rec_right(inode
,
3540 if (ctxt
->c_split_covers_rec
) {
3542 * The merge may have left an empty extent in
3543 * our leaf. Try to rotate it away.
3545 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3557 static void ocfs2_subtract_from_rec(struct super_block
*sb
,
3558 enum ocfs2_split_type split
,
3559 struct ocfs2_extent_rec
*rec
,
3560 struct ocfs2_extent_rec
*split_rec
)
3564 len_blocks
= ocfs2_clusters_to_blocks(sb
,
3565 le16_to_cpu(split_rec
->e_leaf_clusters
));
3567 if (split
== SPLIT_LEFT
) {
3569 * Region is on the left edge of the existing
3572 le32_add_cpu(&rec
->e_cpos
,
3573 le16_to_cpu(split_rec
->e_leaf_clusters
));
3574 le64_add_cpu(&rec
->e_blkno
, len_blocks
);
3575 le16_add_cpu(&rec
->e_leaf_clusters
,
3576 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3579 * Region is on the right edge of the existing
3582 le16_add_cpu(&rec
->e_leaf_clusters
,
3583 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3588 * Do the final bits of extent record insertion at the target leaf
3589 * list. If this leaf is part of an allocation tree, it is assumed
3590 * that the tree above has been prepared.
3592 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec
*insert_rec
,
3593 struct ocfs2_extent_list
*el
,
3594 struct ocfs2_insert_type
*insert
,
3595 struct inode
*inode
)
3597 int i
= insert
->ins_contig_index
;
3599 struct ocfs2_extent_rec
*rec
;
3601 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
3603 if (insert
->ins_split
!= SPLIT_NONE
) {
3604 i
= ocfs2_search_extent_list(el
, le32_to_cpu(insert_rec
->e_cpos
));
3606 rec
= &el
->l_recs
[i
];
3607 ocfs2_subtract_from_rec(inode
->i_sb
, insert
->ins_split
, rec
,
3613 * Contiguous insert - either left or right.
3615 if (insert
->ins_contig
!= CONTIG_NONE
) {
3616 rec
= &el
->l_recs
[i
];
3617 if (insert
->ins_contig
== CONTIG_LEFT
) {
3618 rec
->e_blkno
= insert_rec
->e_blkno
;
3619 rec
->e_cpos
= insert_rec
->e_cpos
;
3621 le16_add_cpu(&rec
->e_leaf_clusters
,
3622 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3627 * Handle insert into an empty leaf.
3629 if (le16_to_cpu(el
->l_next_free_rec
) == 0 ||
3630 ((le16_to_cpu(el
->l_next_free_rec
) == 1) &&
3631 ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3632 el
->l_recs
[0] = *insert_rec
;
3633 el
->l_next_free_rec
= cpu_to_le16(1);
3640 if (insert
->ins_appending
== APPEND_TAIL
) {
3641 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
3642 rec
= &el
->l_recs
[i
];
3643 range
= le32_to_cpu(rec
->e_cpos
)
3644 + le16_to_cpu(rec
->e_leaf_clusters
);
3645 BUG_ON(le32_to_cpu(insert_rec
->e_cpos
) < range
);
3647 mlog_bug_on_msg(le16_to_cpu(el
->l_next_free_rec
) >=
3648 le16_to_cpu(el
->l_count
),
3649 "inode %lu, depth %u, count %u, next free %u, "
3650 "rec.cpos %u, rec.clusters %u, "
3651 "insert.cpos %u, insert.clusters %u\n",
3653 le16_to_cpu(el
->l_tree_depth
),
3654 le16_to_cpu(el
->l_count
),
3655 le16_to_cpu(el
->l_next_free_rec
),
3656 le32_to_cpu(el
->l_recs
[i
].e_cpos
),
3657 le16_to_cpu(el
->l_recs
[i
].e_leaf_clusters
),
3658 le32_to_cpu(insert_rec
->e_cpos
),
3659 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3661 el
->l_recs
[i
] = *insert_rec
;
3662 le16_add_cpu(&el
->l_next_free_rec
, 1);
3668 * Ok, we have to rotate.
3670 * At this point, it is safe to assume that inserting into an
3671 * empty leaf and appending to a leaf have both been handled
3674 * This leaf needs to have space, either by the empty 1st
3675 * extent record, or by virtue of an l_next_rec < l_count.
3677 ocfs2_rotate_leaf(el
, insert_rec
);
3680 static void ocfs2_adjust_rightmost_records(struct inode
*inode
,
3682 struct ocfs2_path
*path
,
3683 struct ocfs2_extent_rec
*insert_rec
)
3685 int ret
, i
, next_free
;
3686 struct buffer_head
*bh
;
3687 struct ocfs2_extent_list
*el
;
3688 struct ocfs2_extent_rec
*rec
;
3691 * Update everything except the leaf block.
3693 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
3694 bh
= path
->p_node
[i
].bh
;
3695 el
= path
->p_node
[i
].el
;
3697 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3698 if (next_free
== 0) {
3699 ocfs2_error(inode
->i_sb
,
3700 "Dinode %llu has a bad extent list",
3701 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3706 rec
= &el
->l_recs
[next_free
- 1];
3708 rec
->e_int_clusters
= insert_rec
->e_cpos
;
3709 le32_add_cpu(&rec
->e_int_clusters
,
3710 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3711 le32_add_cpu(&rec
->e_int_clusters
,
3712 -le32_to_cpu(rec
->e_cpos
));
3714 ret
= ocfs2_journal_dirty(handle
, bh
);
3721 static int ocfs2_append_rec_to_path(struct inode
*inode
, handle_t
*handle
,
3722 struct ocfs2_extent_rec
*insert_rec
,
3723 struct ocfs2_path
*right_path
,
3724 struct ocfs2_path
**ret_left_path
)
3727 struct ocfs2_extent_list
*el
;
3728 struct ocfs2_path
*left_path
= NULL
;
3730 *ret_left_path
= NULL
;
3733 * This shouldn't happen for non-trees. The extent rec cluster
3734 * count manipulation below only works for interior nodes.
3736 BUG_ON(right_path
->p_tree_depth
== 0);
3739 * If our appending insert is at the leftmost edge of a leaf,
3740 * then we might need to update the rightmost records of the
3743 el
= path_leaf_el(right_path
);
3744 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3745 if (next_free
== 0 ||
3746 (next_free
== 1 && ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3749 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
3756 mlog(0, "Append may need a left path update. cpos: %u, "
3757 "left_cpos: %u\n", le32_to_cpu(insert_rec
->e_cpos
),
3761 * No need to worry if the append is already in the
3765 left_path
= ocfs2_new_path(path_root_bh(right_path
),
3766 path_root_el(right_path
));
3773 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3780 * ocfs2_insert_path() will pass the left_path to the
3786 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
3792 ocfs2_adjust_rightmost_records(inode
, handle
, right_path
, insert_rec
);
3794 *ret_left_path
= left_path
;
3798 ocfs2_free_path(left_path
);
3803 static void ocfs2_split_record(struct inode
*inode
,
3804 struct ocfs2_path
*left_path
,
3805 struct ocfs2_path
*right_path
,
3806 struct ocfs2_extent_rec
*split_rec
,
3807 enum ocfs2_split_type split
)
3810 u32 cpos
= le32_to_cpu(split_rec
->e_cpos
);
3811 struct ocfs2_extent_list
*left_el
= NULL
, *right_el
, *insert_el
, *el
;
3812 struct ocfs2_extent_rec
*rec
, *tmprec
;
3814 right_el
= path_leaf_el(right_path
);;
3816 left_el
= path_leaf_el(left_path
);
3819 insert_el
= right_el
;
3820 index
= ocfs2_search_extent_list(el
, cpos
);
3822 if (index
== 0 && left_path
) {
3823 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3826 * This typically means that the record
3827 * started in the left path but moved to the
3828 * right as a result of rotation. We either
3829 * move the existing record to the left, or we
3830 * do the later insert there.
3832 * In this case, the left path should always
3833 * exist as the rotate code will have passed
3834 * it back for a post-insert update.
3837 if (split
== SPLIT_LEFT
) {
3839 * It's a left split. Since we know
3840 * that the rotate code gave us an
3841 * empty extent in the left path, we
3842 * can just do the insert there.
3844 insert_el
= left_el
;
3847 * Right split - we have to move the
3848 * existing record over to the left
3849 * leaf. The insert will be into the
3850 * newly created empty extent in the
3853 tmprec
= &right_el
->l_recs
[index
];
3854 ocfs2_rotate_leaf(left_el
, tmprec
);
3857 memset(tmprec
, 0, sizeof(*tmprec
));
3858 index
= ocfs2_search_extent_list(left_el
, cpos
);
3859 BUG_ON(index
== -1);
3864 BUG_ON(!ocfs2_is_empty_extent(&left_el
->l_recs
[0]));
3866 * Left path is easy - we can just allow the insert to
3870 insert_el
= left_el
;
3871 index
= ocfs2_search_extent_list(el
, cpos
);
3872 BUG_ON(index
== -1);
3875 rec
= &el
->l_recs
[index
];
3876 ocfs2_subtract_from_rec(inode
->i_sb
, split
, rec
, split_rec
);
3877 ocfs2_rotate_leaf(insert_el
, split_rec
);
3881 * This function only does inserts on an allocation b-tree. For tree
3882 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3884 * right_path is the path we want to do the actual insert
3885 * in. left_path should only be passed in if we need to update that
3886 * portion of the tree after an edge insert.
3888 static int ocfs2_insert_path(struct inode
*inode
,
3890 struct ocfs2_path
*left_path
,
3891 struct ocfs2_path
*right_path
,
3892 struct ocfs2_extent_rec
*insert_rec
,
3893 struct ocfs2_insert_type
*insert
)
3895 int ret
, subtree_index
;
3896 struct buffer_head
*leaf_bh
= path_leaf_bh(right_path
);
3899 int credits
= handle
->h_buffer_credits
;
3902 * There's a chance that left_path got passed back to
3903 * us without being accounted for in the
3904 * journal. Extend our transaction here to be sure we
3905 * can change those blocks.
3907 credits
+= left_path
->p_tree_depth
;
3909 ret
= ocfs2_extend_trans(handle
, credits
);
3915 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
3923 * Pass both paths to the journal. The majority of inserts
3924 * will be touching all components anyway.
3926 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
3932 if (insert
->ins_split
!= SPLIT_NONE
) {
3934 * We could call ocfs2_insert_at_leaf() for some types
3935 * of splits, but it's easier to just let one separate
3936 * function sort it all out.
3938 ocfs2_split_record(inode
, left_path
, right_path
,
3939 insert_rec
, insert
->ins_split
);
3942 * Split might have modified either leaf and we don't
3943 * have a guarantee that the later edge insert will
3944 * dirty this for us.
3947 ret
= ocfs2_journal_dirty(handle
,
3948 path_leaf_bh(left_path
));
3952 ocfs2_insert_at_leaf(insert_rec
, path_leaf_el(right_path
),
3955 ret
= ocfs2_journal_dirty(handle
, leaf_bh
);
3961 * The rotate code has indicated that we need to fix
3962 * up portions of the tree after the insert.
3964 * XXX: Should we extend the transaction here?
3966 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
,
3968 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3969 right_path
, subtree_index
);
3977 static int ocfs2_do_insert_extent(struct inode
*inode
,
3979 struct ocfs2_extent_tree
*et
,
3980 struct ocfs2_extent_rec
*insert_rec
,
3981 struct ocfs2_insert_type
*type
)
3983 int ret
, rotate
= 0;
3985 struct ocfs2_path
*right_path
= NULL
;
3986 struct ocfs2_path
*left_path
= NULL
;
3987 struct ocfs2_extent_list
*el
;
3989 el
= et
->et_root_el
;
3991 ret
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
3992 OCFS2_JOURNAL_ACCESS_WRITE
);
3998 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
3999 ocfs2_insert_at_leaf(insert_rec
, el
, type
, inode
);
4000 goto out_update_clusters
;
4003 right_path
= ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
);
4011 * Determine the path to start with. Rotations need the
4012 * rightmost path, everything else can go directly to the
4015 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4016 if (type
->ins_appending
== APPEND_NONE
&&
4017 type
->ins_contig
== CONTIG_NONE
) {
4022 ret
= ocfs2_find_path(inode
, right_path
, cpos
);
4029 * Rotations and appends need special treatment - they modify
4030 * parts of the tree's above them.
4032 * Both might pass back a path immediate to the left of the
4033 * one being inserted to. This will be cause
4034 * ocfs2_insert_path() to modify the rightmost records of
4035 * left_path to account for an edge insert.
4037 * XXX: When modifying this code, keep in mind that an insert
4038 * can wind up skipping both of these two special cases...
4041 ret
= ocfs2_rotate_tree_right(inode
, handle
, type
->ins_split
,
4042 le32_to_cpu(insert_rec
->e_cpos
),
4043 right_path
, &left_path
);
4050 * ocfs2_rotate_tree_right() might have extended the
4051 * transaction without re-journaling our tree root.
4053 ret
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
4054 OCFS2_JOURNAL_ACCESS_WRITE
);
4059 } else if (type
->ins_appending
== APPEND_TAIL
4060 && type
->ins_contig
!= CONTIG_LEFT
) {
4061 ret
= ocfs2_append_rec_to_path(inode
, handle
, insert_rec
,
4062 right_path
, &left_path
);
4069 ret
= ocfs2_insert_path(inode
, handle
, left_path
, right_path
,
4076 out_update_clusters
:
4077 if (type
->ins_split
== SPLIT_NONE
)
4078 ocfs2_et_update_clusters(inode
, et
,
4079 le16_to_cpu(insert_rec
->e_leaf_clusters
));
4081 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4086 ocfs2_free_path(left_path
);
4087 ocfs2_free_path(right_path
);
4092 static enum ocfs2_contig_type
4093 ocfs2_figure_merge_contig_type(struct inode
*inode
, struct ocfs2_path
*path
,
4094 struct ocfs2_extent_list
*el
, int index
,
4095 struct ocfs2_extent_rec
*split_rec
)
4098 enum ocfs2_contig_type ret
= CONTIG_NONE
;
4099 u32 left_cpos
, right_cpos
;
4100 struct ocfs2_extent_rec
*rec
= NULL
;
4101 struct ocfs2_extent_list
*new_el
;
4102 struct ocfs2_path
*left_path
= NULL
, *right_path
= NULL
;
4103 struct buffer_head
*bh
;
4104 struct ocfs2_extent_block
*eb
;
4107 rec
= &el
->l_recs
[index
- 1];
4108 } else if (path
->p_tree_depth
> 0) {
4109 status
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
4114 if (left_cpos
!= 0) {
4115 left_path
= ocfs2_new_path(path_root_bh(path
),
4116 path_root_el(path
));
4120 status
= ocfs2_find_path(inode
, left_path
, left_cpos
);
4124 new_el
= path_leaf_el(left_path
);
4126 if (le16_to_cpu(new_el
->l_next_free_rec
) !=
4127 le16_to_cpu(new_el
->l_count
)) {
4128 bh
= path_leaf_bh(left_path
);
4129 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4130 ocfs2_error(inode
->i_sb
,
4131 "Extent block #%llu has an "
4132 "invalid l_next_free_rec of "
4133 "%d. It should have "
4134 "matched the l_count of %d",
4135 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4136 le16_to_cpu(new_el
->l_next_free_rec
),
4137 le16_to_cpu(new_el
->l_count
));
4141 rec
= &new_el
->l_recs
[
4142 le16_to_cpu(new_el
->l_next_free_rec
) - 1];
4147 * We're careful to check for an empty extent record here -
4148 * the merge code will know what to do if it sees one.
4151 if (index
== 1 && ocfs2_is_empty_extent(rec
)) {
4152 if (split_rec
->e_cpos
== el
->l_recs
[index
].e_cpos
)
4155 ret
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4160 if (index
< (le16_to_cpu(el
->l_next_free_rec
) - 1))
4161 rec
= &el
->l_recs
[index
+ 1];
4162 else if (le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
) &&
4163 path
->p_tree_depth
> 0) {
4164 status
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
,
4169 if (right_cpos
== 0)
4172 right_path
= ocfs2_new_path(path_root_bh(path
),
4173 path_root_el(path
));
4177 status
= ocfs2_find_path(inode
, right_path
, right_cpos
);
4181 new_el
= path_leaf_el(right_path
);
4182 rec
= &new_el
->l_recs
[0];
4183 if (ocfs2_is_empty_extent(rec
)) {
4184 if (le16_to_cpu(new_el
->l_next_free_rec
) <= 1) {
4185 bh
= path_leaf_bh(right_path
);
4186 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4187 ocfs2_error(inode
->i_sb
,
4188 "Extent block #%llu has an "
4189 "invalid l_next_free_rec of %d",
4190 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4191 le16_to_cpu(new_el
->l_next_free_rec
));
4195 rec
= &new_el
->l_recs
[1];
4200 enum ocfs2_contig_type contig_type
;
4202 contig_type
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4204 if (contig_type
== CONTIG_LEFT
&& ret
== CONTIG_RIGHT
)
4205 ret
= CONTIG_LEFTRIGHT
;
4206 else if (ret
== CONTIG_NONE
)
4212 ocfs2_free_path(left_path
);
4214 ocfs2_free_path(right_path
);
4219 static void ocfs2_figure_contig_type(struct inode
*inode
,
4220 struct ocfs2_insert_type
*insert
,
4221 struct ocfs2_extent_list
*el
,
4222 struct ocfs2_extent_rec
*insert_rec
,
4223 struct ocfs2_extent_tree
*et
)
4226 enum ocfs2_contig_type contig_type
= CONTIG_NONE
;
4228 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4230 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
4231 contig_type
= ocfs2_extent_contig(inode
, &el
->l_recs
[i
],
4233 if (contig_type
!= CONTIG_NONE
) {
4234 insert
->ins_contig_index
= i
;
4238 insert
->ins_contig
= contig_type
;
4240 if (insert
->ins_contig
!= CONTIG_NONE
) {
4241 struct ocfs2_extent_rec
*rec
=
4242 &el
->l_recs
[insert
->ins_contig_index
];
4243 unsigned int len
= le16_to_cpu(rec
->e_leaf_clusters
) +
4244 le16_to_cpu(insert_rec
->e_leaf_clusters
);
4247 * Caller might want us to limit the size of extents, don't
4248 * calculate contiguousness if we might exceed that limit.
4250 if (et
->et_max_leaf_clusters
&&
4251 (len
> et
->et_max_leaf_clusters
))
4252 insert
->ins_contig
= CONTIG_NONE
;
4257 * This should only be called against the righmost leaf extent list.
4259 * ocfs2_figure_appending_type() will figure out whether we'll have to
4260 * insert at the tail of the rightmost leaf.
4262 * This should also work against the root extent list for tree's with 0
4263 * depth. If we consider the root extent list to be the rightmost leaf node
4264 * then the logic here makes sense.
4266 static void ocfs2_figure_appending_type(struct ocfs2_insert_type
*insert
,
4267 struct ocfs2_extent_list
*el
,
4268 struct ocfs2_extent_rec
*insert_rec
)
4271 u32 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4272 struct ocfs2_extent_rec
*rec
;
4274 insert
->ins_appending
= APPEND_NONE
;
4276 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4278 if (!el
->l_next_free_rec
)
4279 goto set_tail_append
;
4281 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
4282 /* Were all records empty? */
4283 if (le16_to_cpu(el
->l_next_free_rec
) == 1)
4284 goto set_tail_append
;
4287 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
4288 rec
= &el
->l_recs
[i
];
4291 (le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)))
4292 goto set_tail_append
;
4297 insert
->ins_appending
= APPEND_TAIL
;
4301 * Helper function called at the begining of an insert.
4303 * This computes a few things that are commonly used in the process of
4304 * inserting into the btree:
4305 * - Whether the new extent is contiguous with an existing one.
4306 * - The current tree depth.
4307 * - Whether the insert is an appending one.
4308 * - The total # of free records in the tree.
4310 * All of the information is stored on the ocfs2_insert_type
4313 static int ocfs2_figure_insert_type(struct inode
*inode
,
4314 struct ocfs2_extent_tree
*et
,
4315 struct buffer_head
**last_eb_bh
,
4316 struct ocfs2_extent_rec
*insert_rec
,
4318 struct ocfs2_insert_type
*insert
)
4321 struct ocfs2_extent_block
*eb
;
4322 struct ocfs2_extent_list
*el
;
4323 struct ocfs2_path
*path
= NULL
;
4324 struct buffer_head
*bh
= NULL
;
4326 insert
->ins_split
= SPLIT_NONE
;
4328 el
= et
->et_root_el
;
4329 insert
->ins_tree_depth
= le16_to_cpu(el
->l_tree_depth
);
4331 if (el
->l_tree_depth
) {
4333 * If we have tree depth, we read in the
4334 * rightmost extent block ahead of time as
4335 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4336 * may want it later.
4338 ret
= ocfs2_read_extent_block(inode
,
4339 ocfs2_et_get_last_eb_blk(et
),
4345 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
4350 * Unless we have a contiguous insert, we'll need to know if
4351 * there is room left in our allocation tree for another
4354 * XXX: This test is simplistic, we can search for empty
4355 * extent records too.
4357 *free_records
= le16_to_cpu(el
->l_count
) -
4358 le16_to_cpu(el
->l_next_free_rec
);
4360 if (!insert
->ins_tree_depth
) {
4361 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4362 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4366 path
= ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
);
4374 * In the case that we're inserting past what the tree
4375 * currently accounts for, ocfs2_find_path() will return for
4376 * us the rightmost tree path. This is accounted for below in
4377 * the appending code.
4379 ret
= ocfs2_find_path(inode
, path
, le32_to_cpu(insert_rec
->e_cpos
));
4385 el
= path_leaf_el(path
);
4388 * Now that we have the path, there's two things we want to determine:
4389 * 1) Contiguousness (also set contig_index if this is so)
4391 * 2) Are we doing an append? We can trivially break this up
4392 * into two types of appends: simple record append, or a
4393 * rotate inside the tail leaf.
4395 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4398 * The insert code isn't quite ready to deal with all cases of
4399 * left contiguousness. Specifically, if it's an insert into
4400 * the 1st record in a leaf, it will require the adjustment of
4401 * cluster count on the last record of the path directly to it's
4402 * left. For now, just catch that case and fool the layers
4403 * above us. This works just fine for tree_depth == 0, which
4404 * is why we allow that above.
4406 if (insert
->ins_contig
== CONTIG_LEFT
&&
4407 insert
->ins_contig_index
== 0)
4408 insert
->ins_contig
= CONTIG_NONE
;
4411 * Ok, so we can simply compare against last_eb to figure out
4412 * whether the path doesn't exist. This will only happen in
4413 * the case that we're doing a tail append, so maybe we can
4414 * take advantage of that information somehow.
4416 if (ocfs2_et_get_last_eb_blk(et
) ==
4417 path_leaf_bh(path
)->b_blocknr
) {
4419 * Ok, ocfs2_find_path() returned us the rightmost
4420 * tree path. This might be an appending insert. There are
4422 * 1) We're doing a true append at the tail:
4423 * -This might even be off the end of the leaf
4424 * 2) We're "appending" by rotating in the tail
4426 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4430 ocfs2_free_path(path
);
4440 * Insert an extent into an inode btree.
4442 * The caller needs to update fe->i_clusters
4444 int ocfs2_insert_extent(struct ocfs2_super
*osb
,
4446 struct inode
*inode
,
4447 struct ocfs2_extent_tree
*et
,
4452 struct ocfs2_alloc_context
*meta_ac
)
4455 int uninitialized_var(free_records
);
4456 struct buffer_head
*last_eb_bh
= NULL
;
4457 struct ocfs2_insert_type insert
= {0, };
4458 struct ocfs2_extent_rec rec
;
4460 mlog(0, "add %u clusters at position %u to inode %llu\n",
4461 new_clusters
, cpos
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4463 memset(&rec
, 0, sizeof(rec
));
4464 rec
.e_cpos
= cpu_to_le32(cpos
);
4465 rec
.e_blkno
= cpu_to_le64(start_blk
);
4466 rec
.e_leaf_clusters
= cpu_to_le16(new_clusters
);
4467 rec
.e_flags
= flags
;
4468 status
= ocfs2_et_insert_check(inode
, et
, &rec
);
4474 status
= ocfs2_figure_insert_type(inode
, et
, &last_eb_bh
, &rec
,
4475 &free_records
, &insert
);
4481 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4482 "Insert.contig_index: %d, Insert.free_records: %d, "
4483 "Insert.tree_depth: %d\n",
4484 insert
.ins_appending
, insert
.ins_contig
, insert
.ins_contig_index
,
4485 free_records
, insert
.ins_tree_depth
);
4487 if (insert
.ins_contig
== CONTIG_NONE
&& free_records
== 0) {
4488 status
= ocfs2_grow_tree(inode
, handle
, et
,
4489 &insert
.ins_tree_depth
, &last_eb_bh
,
4497 /* Finally, we can add clusters. This might rotate the tree for us. */
4498 status
= ocfs2_do_insert_extent(inode
, handle
, et
, &rec
, &insert
);
4501 else if (et
->et_ops
== &ocfs2_dinode_et_ops
)
4502 ocfs2_extent_map_insert_rec(inode
, &rec
);
4512 * Allcate and add clusters into the extent b-tree.
4513 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4514 * The extent b-tree's root is specified by et, and
4515 * it is not limited to the file storage. Any extent tree can use this
4516 * function if it implements the proper ocfs2_extent_tree.
4518 int ocfs2_add_clusters_in_btree(struct ocfs2_super
*osb
,
4519 struct inode
*inode
,
4520 u32
*logical_offset
,
4521 u32 clusters_to_add
,
4523 struct ocfs2_extent_tree
*et
,
4525 struct ocfs2_alloc_context
*data_ac
,
4526 struct ocfs2_alloc_context
*meta_ac
,
4527 enum ocfs2_alloc_restarted
*reason_ret
)
4531 enum ocfs2_alloc_restarted reason
= RESTART_NONE
;
4532 u32 bit_off
, num_bits
;
4536 BUG_ON(!clusters_to_add
);
4539 flags
= OCFS2_EXT_UNWRITTEN
;
4541 free_extents
= ocfs2_num_free_extents(osb
, inode
, et
);
4542 if (free_extents
< 0) {
4543 status
= free_extents
;
4548 /* there are two cases which could cause us to EAGAIN in the
4549 * we-need-more-metadata case:
4550 * 1) we haven't reserved *any*
4551 * 2) we are so fragmented, we've needed to add metadata too
4553 if (!free_extents
&& !meta_ac
) {
4554 mlog(0, "we haven't reserved any metadata!\n");
4556 reason
= RESTART_META
;
4558 } else if ((!free_extents
)
4559 && (ocfs2_alloc_context_bits_left(meta_ac
)
4560 < ocfs2_extend_meta_needed(et
->et_root_el
))) {
4561 mlog(0, "filesystem is really fragmented...\n");
4563 reason
= RESTART_META
;
4567 status
= __ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
4568 clusters_to_add
, &bit_off
, &num_bits
);
4570 if (status
!= -ENOSPC
)
4575 BUG_ON(num_bits
> clusters_to_add
);
4577 /* reserve our write early -- insert_extent may update the inode */
4578 status
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
4579 OCFS2_JOURNAL_ACCESS_WRITE
);
4585 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
4586 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4587 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4588 status
= ocfs2_insert_extent(osb
, handle
, inode
, et
,
4589 *logical_offset
, block
,
4590 num_bits
, flags
, meta_ac
);
4596 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4602 clusters_to_add
-= num_bits
;
4603 *logical_offset
+= num_bits
;
4605 if (clusters_to_add
) {
4606 mlog(0, "need to alloc once more, wanted = %u\n",
4609 reason
= RESTART_TRANS
;
4615 *reason_ret
= reason
;
4619 static void ocfs2_make_right_split_rec(struct super_block
*sb
,
4620 struct ocfs2_extent_rec
*split_rec
,
4622 struct ocfs2_extent_rec
*rec
)
4624 u32 rec_cpos
= le32_to_cpu(rec
->e_cpos
);
4625 u32 rec_range
= rec_cpos
+ le16_to_cpu(rec
->e_leaf_clusters
);
4627 memset(split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4629 split_rec
->e_cpos
= cpu_to_le32(cpos
);
4630 split_rec
->e_leaf_clusters
= cpu_to_le16(rec_range
- cpos
);
4632 split_rec
->e_blkno
= rec
->e_blkno
;
4633 le64_add_cpu(&split_rec
->e_blkno
,
4634 ocfs2_clusters_to_blocks(sb
, cpos
- rec_cpos
));
4636 split_rec
->e_flags
= rec
->e_flags
;
4639 static int ocfs2_split_and_insert(struct inode
*inode
,
4641 struct ocfs2_path
*path
,
4642 struct ocfs2_extent_tree
*et
,
4643 struct buffer_head
**last_eb_bh
,
4645 struct ocfs2_extent_rec
*orig_split_rec
,
4646 struct ocfs2_alloc_context
*meta_ac
)
4649 unsigned int insert_range
, rec_range
, do_leftright
= 0;
4650 struct ocfs2_extent_rec tmprec
;
4651 struct ocfs2_extent_list
*rightmost_el
;
4652 struct ocfs2_extent_rec rec
;
4653 struct ocfs2_extent_rec split_rec
= *orig_split_rec
;
4654 struct ocfs2_insert_type insert
;
4655 struct ocfs2_extent_block
*eb
;
4659 * Store a copy of the record on the stack - it might move
4660 * around as the tree is manipulated below.
4662 rec
= path_leaf_el(path
)->l_recs
[split_index
];
4664 rightmost_el
= et
->et_root_el
;
4666 depth
= le16_to_cpu(rightmost_el
->l_tree_depth
);
4668 BUG_ON(!(*last_eb_bh
));
4669 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
4670 rightmost_el
= &eb
->h_list
;
4673 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4674 le16_to_cpu(rightmost_el
->l_count
)) {
4675 ret
= ocfs2_grow_tree(inode
, handle
, et
,
4676 &depth
, last_eb_bh
, meta_ac
);
4683 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4684 insert
.ins_appending
= APPEND_NONE
;
4685 insert
.ins_contig
= CONTIG_NONE
;
4686 insert
.ins_tree_depth
= depth
;
4688 insert_range
= le32_to_cpu(split_rec
.e_cpos
) +
4689 le16_to_cpu(split_rec
.e_leaf_clusters
);
4690 rec_range
= le32_to_cpu(rec
.e_cpos
) +
4691 le16_to_cpu(rec
.e_leaf_clusters
);
4693 if (split_rec
.e_cpos
== rec
.e_cpos
) {
4694 insert
.ins_split
= SPLIT_LEFT
;
4695 } else if (insert_range
== rec_range
) {
4696 insert
.ins_split
= SPLIT_RIGHT
;
4699 * Left/right split. We fake this as a right split
4700 * first and then make a second pass as a left split.
4702 insert
.ins_split
= SPLIT_RIGHT
;
4704 ocfs2_make_right_split_rec(inode
->i_sb
, &tmprec
, insert_range
,
4709 BUG_ON(do_leftright
);
4713 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
4719 if (do_leftright
== 1) {
4721 struct ocfs2_extent_list
*el
;
4724 split_rec
= *orig_split_rec
;
4726 ocfs2_reinit_path(path
, 1);
4728 cpos
= le32_to_cpu(split_rec
.e_cpos
);
4729 ret
= ocfs2_find_path(inode
, path
, cpos
);
4735 el
= path_leaf_el(path
);
4736 split_index
= ocfs2_search_extent_list(el
, cpos
);
4745 * Mark part or all of the extent record at split_index in the leaf
4746 * pointed to by path as written. This removes the unwritten
4749 * Care is taken to handle contiguousness so as to not grow the tree.
4751 * meta_ac is not strictly necessary - we only truly need it if growth
4752 * of the tree is required. All other cases will degrade into a less
4753 * optimal tree layout.
4755 * last_eb_bh should be the rightmost leaf block for any extent
4756 * btree. Since a split may grow the tree or a merge might shrink it,
4757 * the caller cannot trust the contents of that buffer after this call.
4759 * This code is optimized for readability - several passes might be
4760 * made over certain portions of the tree. All of those blocks will
4761 * have been brought into cache (and pinned via the journal), so the
4762 * extra overhead is not expressed in terms of disk reads.
4764 static int __ocfs2_mark_extent_written(struct inode
*inode
,
4765 struct ocfs2_extent_tree
*et
,
4767 struct ocfs2_path
*path
,
4769 struct ocfs2_extent_rec
*split_rec
,
4770 struct ocfs2_alloc_context
*meta_ac
,
4771 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
4774 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
4775 struct buffer_head
*last_eb_bh
= NULL
;
4776 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
4777 struct ocfs2_merge_ctxt ctxt
;
4778 struct ocfs2_extent_list
*rightmost_el
;
4780 if (!(rec
->e_flags
& OCFS2_EXT_UNWRITTEN
)) {
4786 if (le32_to_cpu(rec
->e_cpos
) > le32_to_cpu(split_rec
->e_cpos
) ||
4787 ((le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)) <
4788 (le32_to_cpu(split_rec
->e_cpos
) + le16_to_cpu(split_rec
->e_leaf_clusters
)))) {
4794 ctxt
.c_contig_type
= ocfs2_figure_merge_contig_type(inode
, path
, el
,
4799 * The core merge / split code wants to know how much room is
4800 * left in this inodes allocation tree, so we pass the
4801 * rightmost extent list.
4803 if (path
->p_tree_depth
) {
4804 struct ocfs2_extent_block
*eb
;
4806 ret
= ocfs2_read_extent_block(inode
,
4807 ocfs2_et_get_last_eb_blk(et
),
4814 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
4815 rightmost_el
= &eb
->h_list
;
4817 rightmost_el
= path_root_el(path
);
4819 if (rec
->e_cpos
== split_rec
->e_cpos
&&
4820 rec
->e_leaf_clusters
== split_rec
->e_leaf_clusters
)
4821 ctxt
.c_split_covers_rec
= 1;
4823 ctxt
.c_split_covers_rec
= 0;
4825 ctxt
.c_has_empty_extent
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
4827 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4828 split_index
, ctxt
.c_contig_type
, ctxt
.c_has_empty_extent
,
4829 ctxt
.c_split_covers_rec
);
4831 if (ctxt
.c_contig_type
== CONTIG_NONE
) {
4832 if (ctxt
.c_split_covers_rec
)
4833 el
->l_recs
[split_index
] = *split_rec
;
4835 ret
= ocfs2_split_and_insert(inode
, handle
, path
, et
,
4836 &last_eb_bh
, split_index
,
4837 split_rec
, meta_ac
);
4841 ret
= ocfs2_try_to_merge_extent(inode
, handle
, path
,
4842 split_index
, split_rec
,
4843 dealloc
, &ctxt
, et
);
4854 * Mark the already-existing extent at cpos as written for len clusters.
4856 * If the existing extent is larger than the request, initiate a
4857 * split. An attempt will be made at merging with adjacent extents.
4859 * The caller is responsible for passing down meta_ac if we'll need it.
4861 int ocfs2_mark_extent_written(struct inode
*inode
,
4862 struct ocfs2_extent_tree
*et
,
4863 handle_t
*handle
, u32 cpos
, u32 len
, u32 phys
,
4864 struct ocfs2_alloc_context
*meta_ac
,
4865 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
4868 u64 start_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys
);
4869 struct ocfs2_extent_rec split_rec
;
4870 struct ocfs2_path
*left_path
= NULL
;
4871 struct ocfs2_extent_list
*el
;
4873 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4874 inode
->i_ino
, cpos
, len
, phys
, (unsigned long long)start_blkno
);
4876 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode
->i_sb
))) {
4877 ocfs2_error(inode
->i_sb
, "Inode %llu has unwritten extents "
4878 "that are being written to, but the feature bit "
4879 "is not set in the super block.",
4880 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4886 * XXX: This should be fixed up so that we just re-insert the
4887 * next extent records.
4889 * XXX: This is a hack on the extent tree, maybe it should be
4892 if (et
->et_ops
== &ocfs2_dinode_et_ops
)
4893 ocfs2_extent_map_trunc(inode
, 0);
4895 left_path
= ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
);
4902 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
4907 el
= path_leaf_el(left_path
);
4909 index
= ocfs2_search_extent_list(el
, cpos
);
4910 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
4911 ocfs2_error(inode
->i_sb
,
4912 "Inode %llu has an extent at cpos %u which can no "
4913 "longer be found.\n",
4914 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
4919 memset(&split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4920 split_rec
.e_cpos
= cpu_to_le32(cpos
);
4921 split_rec
.e_leaf_clusters
= cpu_to_le16(len
);
4922 split_rec
.e_blkno
= cpu_to_le64(start_blkno
);
4923 split_rec
.e_flags
= path_leaf_el(left_path
)->l_recs
[index
].e_flags
;
4924 split_rec
.e_flags
&= ~OCFS2_EXT_UNWRITTEN
;
4926 ret
= __ocfs2_mark_extent_written(inode
, et
, handle
, left_path
,
4927 index
, &split_rec
, meta_ac
,
4933 ocfs2_free_path(left_path
);
4937 static int ocfs2_split_tree(struct inode
*inode
, struct ocfs2_extent_tree
*et
,
4938 handle_t
*handle
, struct ocfs2_path
*path
,
4939 int index
, u32 new_range
,
4940 struct ocfs2_alloc_context
*meta_ac
)
4942 int ret
, depth
, credits
= handle
->h_buffer_credits
;
4943 struct buffer_head
*last_eb_bh
= NULL
;
4944 struct ocfs2_extent_block
*eb
;
4945 struct ocfs2_extent_list
*rightmost_el
, *el
;
4946 struct ocfs2_extent_rec split_rec
;
4947 struct ocfs2_extent_rec
*rec
;
4948 struct ocfs2_insert_type insert
;
4951 * Setup the record to split before we grow the tree.
4953 el
= path_leaf_el(path
);
4954 rec
= &el
->l_recs
[index
];
4955 ocfs2_make_right_split_rec(inode
->i_sb
, &split_rec
, new_range
, rec
);
4957 depth
= path
->p_tree_depth
;
4959 ret
= ocfs2_read_extent_block(inode
,
4960 ocfs2_et_get_last_eb_blk(et
),
4967 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
4968 rightmost_el
= &eb
->h_list
;
4970 rightmost_el
= path_leaf_el(path
);
4972 credits
+= path
->p_tree_depth
+
4973 ocfs2_extend_meta_needed(et
->et_root_el
);
4974 ret
= ocfs2_extend_trans(handle
, credits
);
4980 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4981 le16_to_cpu(rightmost_el
->l_count
)) {
4982 ret
= ocfs2_grow_tree(inode
, handle
, et
, &depth
, &last_eb_bh
,
4990 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4991 insert
.ins_appending
= APPEND_NONE
;
4992 insert
.ins_contig
= CONTIG_NONE
;
4993 insert
.ins_split
= SPLIT_RIGHT
;
4994 insert
.ins_tree_depth
= depth
;
4996 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
5005 static int ocfs2_truncate_rec(struct inode
*inode
, handle_t
*handle
,
5006 struct ocfs2_path
*path
, int index
,
5007 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5009 struct ocfs2_extent_tree
*et
)
5012 u32 left_cpos
, rec_range
, trunc_range
;
5013 int wants_rotate
= 0, is_rightmost_tree_rec
= 0;
5014 struct super_block
*sb
= inode
->i_sb
;
5015 struct ocfs2_path
*left_path
= NULL
;
5016 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
5017 struct ocfs2_extent_rec
*rec
;
5018 struct ocfs2_extent_block
*eb
;
5020 if (ocfs2_is_empty_extent(&el
->l_recs
[0]) && index
> 0) {
5021 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5030 if (index
== (le16_to_cpu(el
->l_next_free_rec
) - 1) &&
5031 path
->p_tree_depth
) {
5033 * Check whether this is the rightmost tree record. If
5034 * we remove all of this record or part of its right
5035 * edge then an update of the record lengths above it
5038 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
5039 if (eb
->h_next_leaf_blk
== 0)
5040 is_rightmost_tree_rec
= 1;
5043 rec
= &el
->l_recs
[index
];
5044 if (index
== 0 && path
->p_tree_depth
&&
5045 le32_to_cpu(rec
->e_cpos
) == cpos
) {
5047 * Changing the leftmost offset (via partial or whole
5048 * record truncate) of an interior (or rightmost) path
5049 * means we have to update the subtree that is formed
5050 * by this leaf and the one to it's left.
5052 * There are two cases we can skip:
5053 * 1) Path is the leftmost one in our inode tree.
5054 * 2) The leaf is rightmost and will be empty after
5055 * we remove the extent record - the rotate code
5056 * knows how to update the newly formed edge.
5059 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
,
5066 if (left_cpos
&& le16_to_cpu(el
->l_next_free_rec
) > 1) {
5067 left_path
= ocfs2_new_path(path_root_bh(path
),
5068 path_root_el(path
));
5075 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
5083 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
5084 handle
->h_buffer_credits
,
5091 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
5097 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
5103 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5104 trunc_range
= cpos
+ len
;
5106 if (le32_to_cpu(rec
->e_cpos
) == cpos
&& rec_range
== trunc_range
) {
5109 memset(rec
, 0, sizeof(*rec
));
5110 ocfs2_cleanup_merge(el
, index
);
5113 next_free
= le16_to_cpu(el
->l_next_free_rec
);
5114 if (is_rightmost_tree_rec
&& next_free
> 1) {
5116 * We skip the edge update if this path will
5117 * be deleted by the rotate code.
5119 rec
= &el
->l_recs
[next_free
- 1];
5120 ocfs2_adjust_rightmost_records(inode
, handle
, path
,
5123 } else if (le32_to_cpu(rec
->e_cpos
) == cpos
) {
5124 /* Remove leftmost portion of the record. */
5125 le32_add_cpu(&rec
->e_cpos
, len
);
5126 le64_add_cpu(&rec
->e_blkno
, ocfs2_clusters_to_blocks(sb
, len
));
5127 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5128 } else if (rec_range
== trunc_range
) {
5129 /* Remove rightmost portion of the record */
5130 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5131 if (is_rightmost_tree_rec
)
5132 ocfs2_adjust_rightmost_records(inode
, handle
, path
, rec
);
5134 /* Caller should have trapped this. */
5135 mlog(ML_ERROR
, "Inode %llu: Invalid record truncate: (%u, %u) "
5136 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5137 le32_to_cpu(rec
->e_cpos
),
5138 le16_to_cpu(rec
->e_leaf_clusters
), cpos
, len
);
5145 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
5146 ocfs2_complete_edge_insert(inode
, handle
, left_path
, path
,
5150 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
5152 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5159 ocfs2_free_path(left_path
);
5163 int ocfs2_remove_extent(struct inode
*inode
,
5164 struct ocfs2_extent_tree
*et
,
5165 u32 cpos
, u32 len
, handle_t
*handle
,
5166 struct ocfs2_alloc_context
*meta_ac
,
5167 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5170 u32 rec_range
, trunc_range
;
5171 struct ocfs2_extent_rec
*rec
;
5172 struct ocfs2_extent_list
*el
;
5173 struct ocfs2_path
*path
= NULL
;
5175 ocfs2_extent_map_trunc(inode
, 0);
5177 path
= ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
);
5184 ret
= ocfs2_find_path(inode
, path
, cpos
);
5190 el
= path_leaf_el(path
);
5191 index
= ocfs2_search_extent_list(el
, cpos
);
5192 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5193 ocfs2_error(inode
->i_sb
,
5194 "Inode %llu has an extent at cpos %u which can no "
5195 "longer be found.\n",
5196 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
5202 * We have 3 cases of extent removal:
5203 * 1) Range covers the entire extent rec
5204 * 2) Range begins or ends on one edge of the extent rec
5205 * 3) Range is in the middle of the extent rec (no shared edges)
5207 * For case 1 we remove the extent rec and left rotate to
5210 * For case 2 we just shrink the existing extent rec, with a
5211 * tree update if the shrinking edge is also the edge of an
5214 * For case 3 we do a right split to turn the extent rec into
5215 * something case 2 can handle.
5217 rec
= &el
->l_recs
[index
];
5218 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5219 trunc_range
= cpos
+ len
;
5221 BUG_ON(cpos
< le32_to_cpu(rec
->e_cpos
) || trunc_range
> rec_range
);
5223 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5224 "(cpos %u, len %u)\n",
5225 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
, len
, index
,
5226 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
));
5228 if (le32_to_cpu(rec
->e_cpos
) == cpos
|| rec_range
== trunc_range
) {
5229 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5236 ret
= ocfs2_split_tree(inode
, et
, handle
, path
, index
,
5237 trunc_range
, meta_ac
);
5244 * The split could have manipulated the tree enough to
5245 * move the record location, so we have to look for it again.
5247 ocfs2_reinit_path(path
, 1);
5249 ret
= ocfs2_find_path(inode
, path
, cpos
);
5255 el
= path_leaf_el(path
);
5256 index
= ocfs2_search_extent_list(el
, cpos
);
5257 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5258 ocfs2_error(inode
->i_sb
,
5259 "Inode %llu: split at cpos %u lost record.",
5260 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5267 * Double check our values here. If anything is fishy,
5268 * it's easier to catch it at the top level.
5270 rec
= &el
->l_recs
[index
];
5271 rec_range
= le32_to_cpu(rec
->e_cpos
) +
5272 ocfs2_rec_clusters(el
, rec
);
5273 if (rec_range
!= trunc_range
) {
5274 ocfs2_error(inode
->i_sb
,
5275 "Inode %llu: error after split at cpos %u"
5276 "trunc len %u, existing record is (%u,%u)",
5277 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5278 cpos
, len
, le32_to_cpu(rec
->e_cpos
),
5279 ocfs2_rec_clusters(el
, rec
));
5284 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5293 ocfs2_free_path(path
);
5297 int ocfs2_remove_btree_range(struct inode
*inode
,
5298 struct ocfs2_extent_tree
*et
,
5299 u32 cpos
, u32 phys_cpos
, u32 len
,
5300 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5303 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
5304 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5305 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5307 struct ocfs2_alloc_context
*meta_ac
= NULL
;
5309 ret
= ocfs2_lock_allocators(inode
, et
, 0, 1, NULL
, &meta_ac
);
5315 mutex_lock(&tl_inode
->i_mutex
);
5317 if (ocfs2_truncate_log_needs_flush(osb
)) {
5318 ret
= __ocfs2_flush_truncate_log(osb
);
5325 handle
= ocfs2_start_trans(osb
, OCFS2_REMOVE_EXTENT_CREDITS
);
5326 if (IS_ERR(handle
)) {
5327 ret
= PTR_ERR(handle
);
5332 ret
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
5333 OCFS2_JOURNAL_ACCESS_WRITE
);
5339 ret
= ocfs2_remove_extent(inode
, et
, cpos
, len
, handle
, meta_ac
,
5346 ocfs2_et_update_clusters(inode
, et
, -len
);
5348 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
5354 ret
= ocfs2_truncate_log_append(osb
, handle
, phys_blkno
, len
);
5359 ocfs2_commit_trans(osb
, handle
);
5361 mutex_unlock(&tl_inode
->i_mutex
);
5364 ocfs2_free_alloc_context(meta_ac
);
5369 int ocfs2_truncate_log_needs_flush(struct ocfs2_super
*osb
)
5371 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5372 struct ocfs2_dinode
*di
;
5373 struct ocfs2_truncate_log
*tl
;
5375 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5376 tl
= &di
->id2
.i_dealloc
;
5378 mlog_bug_on_msg(le16_to_cpu(tl
->tl_used
) > le16_to_cpu(tl
->tl_count
),
5379 "slot %d, invalid truncate log parameters: used = "
5380 "%u, count = %u\n", osb
->slot_num
,
5381 le16_to_cpu(tl
->tl_used
), le16_to_cpu(tl
->tl_count
));
5382 return le16_to_cpu(tl
->tl_used
) == le16_to_cpu(tl
->tl_count
);
5385 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log
*tl
,
5386 unsigned int new_start
)
5388 unsigned int tail_index
;
5389 unsigned int current_tail
;
5391 /* No records, nothing to coalesce */
5392 if (!le16_to_cpu(tl
->tl_used
))
5395 tail_index
= le16_to_cpu(tl
->tl_used
) - 1;
5396 current_tail
= le32_to_cpu(tl
->tl_recs
[tail_index
].t_start
);
5397 current_tail
+= le32_to_cpu(tl
->tl_recs
[tail_index
].t_clusters
);
5399 return current_tail
== new_start
;
5402 int ocfs2_truncate_log_append(struct ocfs2_super
*osb
,
5405 unsigned int num_clusters
)
5408 unsigned int start_cluster
, tl_count
;
5409 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5410 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5411 struct ocfs2_dinode
*di
;
5412 struct ocfs2_truncate_log
*tl
;
5414 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5415 (unsigned long long)start_blk
, num_clusters
);
5417 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5419 start_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, start_blk
);
5421 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5423 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5424 * by the underlying call to ocfs2_read_inode_block(), so any
5425 * corruption is a code bug */
5426 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5428 tl
= &di
->id2
.i_dealloc
;
5429 tl_count
= le16_to_cpu(tl
->tl_count
);
5430 mlog_bug_on_msg(tl_count
> ocfs2_truncate_recs_per_inode(osb
->sb
) ||
5432 "Truncate record count on #%llu invalid "
5433 "wanted %u, actual %u\n",
5434 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
,
5435 ocfs2_truncate_recs_per_inode(osb
->sb
),
5436 le16_to_cpu(tl
->tl_count
));
5438 /* Caller should have known to flush before calling us. */
5439 index
= le16_to_cpu(tl
->tl_used
);
5440 if (index
>= tl_count
) {
5446 status
= ocfs2_journal_access(handle
, tl_inode
, tl_bh
,
5447 OCFS2_JOURNAL_ACCESS_WRITE
);
5453 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5454 "%llu (index = %d)\n", num_clusters
, start_cluster
,
5455 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
, index
);
5457 if (ocfs2_truncate_log_can_coalesce(tl
, start_cluster
)) {
5459 * Move index back to the record we are coalescing with.
5460 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5464 num_clusters
+= le32_to_cpu(tl
->tl_recs
[index
].t_clusters
);
5465 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5466 index
, le32_to_cpu(tl
->tl_recs
[index
].t_start
),
5469 tl
->tl_recs
[index
].t_start
= cpu_to_le32(start_cluster
);
5470 tl
->tl_used
= cpu_to_le16(index
+ 1);
5472 tl
->tl_recs
[index
].t_clusters
= cpu_to_le32(num_clusters
);
5474 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5485 static int ocfs2_replay_truncate_records(struct ocfs2_super
*osb
,
5487 struct inode
*data_alloc_inode
,
5488 struct buffer_head
*data_alloc_bh
)
5492 unsigned int num_clusters
;
5494 struct ocfs2_truncate_rec rec
;
5495 struct ocfs2_dinode
*di
;
5496 struct ocfs2_truncate_log
*tl
;
5497 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5498 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5502 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5503 tl
= &di
->id2
.i_dealloc
;
5504 i
= le16_to_cpu(tl
->tl_used
) - 1;
5506 /* Caller has given us at least enough credits to
5507 * update the truncate log dinode */
5508 status
= ocfs2_journal_access(handle
, tl_inode
, tl_bh
,
5509 OCFS2_JOURNAL_ACCESS_WRITE
);
5515 tl
->tl_used
= cpu_to_le16(i
);
5517 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5523 /* TODO: Perhaps we can calculate the bulk of the
5524 * credits up front rather than extending like
5526 status
= ocfs2_extend_trans(handle
,
5527 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC
);
5533 rec
= tl
->tl_recs
[i
];
5534 start_blk
= ocfs2_clusters_to_blocks(data_alloc_inode
->i_sb
,
5535 le32_to_cpu(rec
.t_start
));
5536 num_clusters
= le32_to_cpu(rec
.t_clusters
);
5538 /* if start_blk is not set, we ignore the record as
5541 mlog(0, "free record %d, start = %u, clusters = %u\n",
5542 i
, le32_to_cpu(rec
.t_start
), num_clusters
);
5544 status
= ocfs2_free_clusters(handle
, data_alloc_inode
,
5545 data_alloc_bh
, start_blk
,
5560 /* Expects you to already be holding tl_inode->i_mutex */
5561 int __ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5564 unsigned int num_to_flush
;
5566 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5567 struct inode
*data_alloc_inode
= NULL
;
5568 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5569 struct buffer_head
*data_alloc_bh
= NULL
;
5570 struct ocfs2_dinode
*di
;
5571 struct ocfs2_truncate_log
*tl
;
5575 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5577 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5579 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5580 * by the underlying call to ocfs2_read_inode_block(), so any
5581 * corruption is a code bug */
5582 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5584 tl
= &di
->id2
.i_dealloc
;
5585 num_to_flush
= le16_to_cpu(tl
->tl_used
);
5586 mlog(0, "Flush %u records from truncate log #%llu\n",
5587 num_to_flush
, (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
);
5588 if (!num_to_flush
) {
5593 data_alloc_inode
= ocfs2_get_system_file_inode(osb
,
5594 GLOBAL_BITMAP_SYSTEM_INODE
,
5595 OCFS2_INVALID_SLOT
);
5596 if (!data_alloc_inode
) {
5598 mlog(ML_ERROR
, "Could not get bitmap inode!\n");
5602 mutex_lock(&data_alloc_inode
->i_mutex
);
5604 status
= ocfs2_inode_lock(data_alloc_inode
, &data_alloc_bh
, 1);
5610 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5611 if (IS_ERR(handle
)) {
5612 status
= PTR_ERR(handle
);
5617 status
= ocfs2_replay_truncate_records(osb
, handle
, data_alloc_inode
,
5622 ocfs2_commit_trans(osb
, handle
);
5625 brelse(data_alloc_bh
);
5626 ocfs2_inode_unlock(data_alloc_inode
, 1);
5629 mutex_unlock(&data_alloc_inode
->i_mutex
);
5630 iput(data_alloc_inode
);
5637 int ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5640 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5642 mutex_lock(&tl_inode
->i_mutex
);
5643 status
= __ocfs2_flush_truncate_log(osb
);
5644 mutex_unlock(&tl_inode
->i_mutex
);
5649 static void ocfs2_truncate_log_worker(struct work_struct
*work
)
5652 struct ocfs2_super
*osb
=
5653 container_of(work
, struct ocfs2_super
,
5654 osb_truncate_log_wq
.work
);
5658 status
= ocfs2_flush_truncate_log(osb
);
5662 ocfs2_init_inode_steal_slot(osb
);
5667 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5668 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super
*osb
,
5671 if (osb
->osb_tl_inode
) {
5672 /* We want to push off log flushes while truncates are
5675 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5677 queue_delayed_work(ocfs2_wq
, &osb
->osb_truncate_log_wq
,
5678 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL
);
5682 static int ocfs2_get_truncate_log_info(struct ocfs2_super
*osb
,
5684 struct inode
**tl_inode
,
5685 struct buffer_head
**tl_bh
)
5688 struct inode
*inode
= NULL
;
5689 struct buffer_head
*bh
= NULL
;
5691 inode
= ocfs2_get_system_file_inode(osb
,
5692 TRUNCATE_LOG_SYSTEM_INODE
,
5696 mlog(ML_ERROR
, "Could not get load truncate log inode!\n");
5700 status
= ocfs2_read_inode_block(inode
, &bh
);
5714 /* called during the 1st stage of node recovery. we stamp a clean
5715 * truncate log and pass back a copy for processing later. if the
5716 * truncate log does not require processing, a *tl_copy is set to
5718 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super
*osb
,
5720 struct ocfs2_dinode
**tl_copy
)
5723 struct inode
*tl_inode
= NULL
;
5724 struct buffer_head
*tl_bh
= NULL
;
5725 struct ocfs2_dinode
*di
;
5726 struct ocfs2_truncate_log
*tl
;
5730 mlog(0, "recover truncate log from slot %d\n", slot_num
);
5732 status
= ocfs2_get_truncate_log_info(osb
, slot_num
, &tl_inode
, &tl_bh
);
5738 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5740 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5741 * validated by the underlying call to ocfs2_read_inode_block(),
5742 * so any corruption is a code bug */
5743 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5745 tl
= &di
->id2
.i_dealloc
;
5746 if (le16_to_cpu(tl
->tl_used
)) {
5747 mlog(0, "We'll have %u logs to recover\n",
5748 le16_to_cpu(tl
->tl_used
));
5750 *tl_copy
= kmalloc(tl_bh
->b_size
, GFP_KERNEL
);
5757 /* Assuming the write-out below goes well, this copy
5758 * will be passed back to recovery for processing. */
5759 memcpy(*tl_copy
, tl_bh
->b_data
, tl_bh
->b_size
);
5761 /* All we need to do to clear the truncate log is set
5765 status
= ocfs2_write_block(osb
, tl_bh
, tl_inode
);
5777 if (status
< 0 && (*tl_copy
)) {
5786 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super
*osb
,
5787 struct ocfs2_dinode
*tl_copy
)
5791 unsigned int clusters
, num_recs
, start_cluster
;
5794 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5795 struct ocfs2_truncate_log
*tl
;
5799 if (OCFS2_I(tl_inode
)->ip_blkno
== le64_to_cpu(tl_copy
->i_blkno
)) {
5800 mlog(ML_ERROR
, "Asked to recover my own truncate log!\n");
5804 tl
= &tl_copy
->id2
.i_dealloc
;
5805 num_recs
= le16_to_cpu(tl
->tl_used
);
5806 mlog(0, "cleanup %u records from %llu\n", num_recs
,
5807 (unsigned long long)le64_to_cpu(tl_copy
->i_blkno
));
5809 mutex_lock(&tl_inode
->i_mutex
);
5810 for(i
= 0; i
< num_recs
; i
++) {
5811 if (ocfs2_truncate_log_needs_flush(osb
)) {
5812 status
= __ocfs2_flush_truncate_log(osb
);
5819 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5820 if (IS_ERR(handle
)) {
5821 status
= PTR_ERR(handle
);
5826 clusters
= le32_to_cpu(tl
->tl_recs
[i
].t_clusters
);
5827 start_cluster
= le32_to_cpu(tl
->tl_recs
[i
].t_start
);
5828 start_blk
= ocfs2_clusters_to_blocks(osb
->sb
, start_cluster
);
5830 status
= ocfs2_truncate_log_append(osb
, handle
,
5831 start_blk
, clusters
);
5832 ocfs2_commit_trans(osb
, handle
);
5840 mutex_unlock(&tl_inode
->i_mutex
);
5846 void ocfs2_truncate_log_shutdown(struct ocfs2_super
*osb
)
5849 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5854 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5855 flush_workqueue(ocfs2_wq
);
5857 status
= ocfs2_flush_truncate_log(osb
);
5861 brelse(osb
->osb_tl_bh
);
5862 iput(osb
->osb_tl_inode
);
5868 int ocfs2_truncate_log_init(struct ocfs2_super
*osb
)
5871 struct inode
*tl_inode
= NULL
;
5872 struct buffer_head
*tl_bh
= NULL
;
5876 status
= ocfs2_get_truncate_log_info(osb
,
5883 /* ocfs2_truncate_log_shutdown keys on the existence of
5884 * osb->osb_tl_inode so we don't set any of the osb variables
5885 * until we're sure all is well. */
5886 INIT_DELAYED_WORK(&osb
->osb_truncate_log_wq
,
5887 ocfs2_truncate_log_worker
);
5888 osb
->osb_tl_bh
= tl_bh
;
5889 osb
->osb_tl_inode
= tl_inode
;
5896 * Delayed de-allocation of suballocator blocks.
5898 * Some sets of block de-allocations might involve multiple suballocator inodes.
5900 * The locking for this can get extremely complicated, especially when
5901 * the suballocator inodes to delete from aren't known until deep
5902 * within an unrelated codepath.
5904 * ocfs2_extent_block structures are a good example of this - an inode
5905 * btree could have been grown by any number of nodes each allocating
5906 * out of their own suballoc inode.
5908 * These structures allow the delay of block de-allocation until a
5909 * later time, when locking of multiple cluster inodes won't cause
5914 * Describe a single bit freed from a suballocator. For the block
5915 * suballocators, it represents one block. For the global cluster
5916 * allocator, it represents some clusters and free_bit indicates
5919 struct ocfs2_cached_block_free
{
5920 struct ocfs2_cached_block_free
*free_next
;
5922 unsigned int free_bit
;
5925 struct ocfs2_per_slot_free_list
{
5926 struct ocfs2_per_slot_free_list
*f_next_suballocator
;
5929 struct ocfs2_cached_block_free
*f_first
;
5932 static int ocfs2_free_cached_blocks(struct ocfs2_super
*osb
,
5935 struct ocfs2_cached_block_free
*head
)
5940 struct inode
*inode
;
5941 struct buffer_head
*di_bh
= NULL
;
5942 struct ocfs2_cached_block_free
*tmp
;
5944 inode
= ocfs2_get_system_file_inode(osb
, sysfile_type
, slot
);
5951 mutex_lock(&inode
->i_mutex
);
5953 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
5959 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
5960 if (IS_ERR(handle
)) {
5961 ret
= PTR_ERR(handle
);
5967 bg_blkno
= ocfs2_which_suballoc_group(head
->free_blk
,
5969 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5970 head
->free_bit
, (unsigned long long)head
->free_blk
);
5972 ret
= ocfs2_free_suballoc_bits(handle
, inode
, di_bh
,
5973 head
->free_bit
, bg_blkno
, 1);
5979 ret
= ocfs2_extend_trans(handle
, OCFS2_SUBALLOC_FREE
);
5986 head
= head
->free_next
;
5991 ocfs2_commit_trans(osb
, handle
);
5994 ocfs2_inode_unlock(inode
, 1);
5997 mutex_unlock(&inode
->i_mutex
);
6001 /* Premature exit may have left some dangling items. */
6003 head
= head
->free_next
;
6010 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6011 u64 blkno
, unsigned int bit
)
6014 struct ocfs2_cached_block_free
*item
;
6016 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6023 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6024 bit
, (unsigned long long)blkno
);
6026 item
->free_blk
= blkno
;
6027 item
->free_bit
= bit
;
6028 item
->free_next
= ctxt
->c_global_allocator
;
6030 ctxt
->c_global_allocator
= item
;
6034 static int ocfs2_free_cached_clusters(struct ocfs2_super
*osb
,
6035 struct ocfs2_cached_block_free
*head
)
6037 struct ocfs2_cached_block_free
*tmp
;
6038 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6042 mutex_lock(&tl_inode
->i_mutex
);
6045 if (ocfs2_truncate_log_needs_flush(osb
)) {
6046 ret
= __ocfs2_flush_truncate_log(osb
);
6053 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
6054 if (IS_ERR(handle
)) {
6055 ret
= PTR_ERR(handle
);
6060 ret
= ocfs2_truncate_log_append(osb
, handle
, head
->free_blk
,
6063 ocfs2_commit_trans(osb
, handle
);
6065 head
= head
->free_next
;
6074 mutex_unlock(&tl_inode
->i_mutex
);
6077 /* Premature exit may have left some dangling items. */
6079 head
= head
->free_next
;
6086 int ocfs2_run_deallocs(struct ocfs2_super
*osb
,
6087 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6090 struct ocfs2_per_slot_free_list
*fl
;
6095 while (ctxt
->c_first_suballocator
) {
6096 fl
= ctxt
->c_first_suballocator
;
6099 mlog(0, "Free items: (type %u, slot %d)\n",
6100 fl
->f_inode_type
, fl
->f_slot
);
6101 ret2
= ocfs2_free_cached_blocks(osb
,
6111 ctxt
->c_first_suballocator
= fl
->f_next_suballocator
;
6115 if (ctxt
->c_global_allocator
) {
6116 ret2
= ocfs2_free_cached_clusters(osb
,
6117 ctxt
->c_global_allocator
);
6123 ctxt
->c_global_allocator
= NULL
;
6129 static struct ocfs2_per_slot_free_list
*
6130 ocfs2_find_per_slot_free_list(int type
,
6132 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6134 struct ocfs2_per_slot_free_list
*fl
= ctxt
->c_first_suballocator
;
6137 if (fl
->f_inode_type
== type
&& fl
->f_slot
== slot
)
6140 fl
= fl
->f_next_suballocator
;
6143 fl
= kmalloc(sizeof(*fl
), GFP_NOFS
);
6145 fl
->f_inode_type
= type
;
6148 fl
->f_next_suballocator
= ctxt
->c_first_suballocator
;
6150 ctxt
->c_first_suballocator
= fl
;
6155 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6156 int type
, int slot
, u64 blkno
,
6160 struct ocfs2_per_slot_free_list
*fl
;
6161 struct ocfs2_cached_block_free
*item
;
6163 fl
= ocfs2_find_per_slot_free_list(type
, slot
, ctxt
);
6170 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6177 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6178 type
, slot
, bit
, (unsigned long long)blkno
);
6180 item
->free_blk
= blkno
;
6181 item
->free_bit
= bit
;
6182 item
->free_next
= fl
->f_first
;
6191 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6192 struct ocfs2_extent_block
*eb
)
6194 return ocfs2_cache_block_dealloc(ctxt
, EXTENT_ALLOC_SYSTEM_INODE
,
6195 le16_to_cpu(eb
->h_suballoc_slot
),
6196 le64_to_cpu(eb
->h_blkno
),
6197 le16_to_cpu(eb
->h_suballoc_bit
));
6200 /* This function will figure out whether the currently last extent
6201 * block will be deleted, and if it will, what the new last extent
6202 * block will be so we can update his h_next_leaf_blk field, as well
6203 * as the dinodes i_last_eb_blk */
6204 static int ocfs2_find_new_last_ext_blk(struct inode
*inode
,
6205 unsigned int clusters_to_del
,
6206 struct ocfs2_path
*path
,
6207 struct buffer_head
**new_last_eb
)
6209 int next_free
, ret
= 0;
6211 struct ocfs2_extent_rec
*rec
;
6212 struct ocfs2_extent_block
*eb
;
6213 struct ocfs2_extent_list
*el
;
6214 struct buffer_head
*bh
= NULL
;
6216 *new_last_eb
= NULL
;
6218 /* we have no tree, so of course, no last_eb. */
6219 if (!path
->p_tree_depth
)
6222 /* trunc to zero special case - this makes tree_depth = 0
6223 * regardless of what it is. */
6224 if (OCFS2_I(inode
)->ip_clusters
== clusters_to_del
)
6227 el
= path_leaf_el(path
);
6228 BUG_ON(!el
->l_next_free_rec
);
6231 * Make sure that this extent list will actually be empty
6232 * after we clear away the data. We can shortcut out if
6233 * there's more than one non-empty extent in the
6234 * list. Otherwise, a check of the remaining extent is
6237 next_free
= le16_to_cpu(el
->l_next_free_rec
);
6239 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6243 /* We may have a valid extent in index 1, check it. */
6245 rec
= &el
->l_recs
[1];
6248 * Fall through - no more nonempty extents, so we want
6249 * to delete this leaf.
6255 rec
= &el
->l_recs
[0];
6260 * Check it we'll only be trimming off the end of this
6263 if (le16_to_cpu(rec
->e_leaf_clusters
) > clusters_to_del
)
6267 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
6273 ret
= ocfs2_find_leaf(inode
, path_root_el(path
), cpos
, &bh
);
6279 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
6282 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6283 * Any corruption is a code bug. */
6284 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
6287 get_bh(*new_last_eb
);
6288 mlog(0, "returning block %llu, (cpos: %u)\n",
6289 (unsigned long long)le64_to_cpu(eb
->h_blkno
), cpos
);
6297 * Trim some clusters off the rightmost edge of a tree. Only called
6300 * The caller needs to:
6301 * - start journaling of each path component.
6302 * - compute and fully set up any new last ext block
6304 static int ocfs2_trim_tree(struct inode
*inode
, struct ocfs2_path
*path
,
6305 handle_t
*handle
, struct ocfs2_truncate_context
*tc
,
6306 u32 clusters_to_del
, u64
*delete_start
)
6308 int ret
, i
, index
= path
->p_tree_depth
;
6311 struct buffer_head
*bh
;
6312 struct ocfs2_extent_list
*el
;
6313 struct ocfs2_extent_rec
*rec
;
6317 while (index
>= 0) {
6318 bh
= path
->p_node
[index
].bh
;
6319 el
= path
->p_node
[index
].el
;
6321 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6322 index
, (unsigned long long)bh
->b_blocknr
);
6324 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
6327 (path
->p_tree_depth
- le16_to_cpu(el
->l_tree_depth
))) {
6328 ocfs2_error(inode
->i_sb
,
6329 "Inode %lu has invalid ext. block %llu",
6331 (unsigned long long)bh
->b_blocknr
);
6337 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
6338 rec
= &el
->l_recs
[i
];
6340 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6341 "next = %u\n", i
, le32_to_cpu(rec
->e_cpos
),
6342 ocfs2_rec_clusters(el
, rec
),
6343 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6344 le16_to_cpu(el
->l_next_free_rec
));
6346 BUG_ON(ocfs2_rec_clusters(el
, rec
) < clusters_to_del
);
6348 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
6350 * If the leaf block contains a single empty
6351 * extent and no records, we can just remove
6354 if (i
== 0 && ocfs2_is_empty_extent(rec
)) {
6356 sizeof(struct ocfs2_extent_rec
));
6357 el
->l_next_free_rec
= cpu_to_le16(0);
6363 * Remove any empty extents by shifting things
6364 * left. That should make life much easier on
6365 * the code below. This condition is rare
6366 * enough that we shouldn't see a performance
6369 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6370 le16_add_cpu(&el
->l_next_free_rec
, -1);
6373 i
< le16_to_cpu(el
->l_next_free_rec
); i
++)
6374 el
->l_recs
[i
] = el
->l_recs
[i
+ 1];
6376 memset(&el
->l_recs
[i
], 0,
6377 sizeof(struct ocfs2_extent_rec
));
6380 * We've modified our extent list. The
6381 * simplest way to handle this change
6382 * is to being the search from the
6385 goto find_tail_record
;
6388 le16_add_cpu(&rec
->e_leaf_clusters
, -clusters_to_del
);
6391 * We'll use "new_edge" on our way back up the
6392 * tree to know what our rightmost cpos is.
6394 new_edge
= le16_to_cpu(rec
->e_leaf_clusters
);
6395 new_edge
+= le32_to_cpu(rec
->e_cpos
);
6398 * The caller will use this to delete data blocks.
6400 *delete_start
= le64_to_cpu(rec
->e_blkno
)
6401 + ocfs2_clusters_to_blocks(inode
->i_sb
,
6402 le16_to_cpu(rec
->e_leaf_clusters
));
6405 * If it's now empty, remove this record.
6407 if (le16_to_cpu(rec
->e_leaf_clusters
) == 0) {
6409 sizeof(struct ocfs2_extent_rec
));
6410 le16_add_cpu(&el
->l_next_free_rec
, -1);
6413 if (le64_to_cpu(rec
->e_blkno
) == deleted_eb
) {
6415 sizeof(struct ocfs2_extent_rec
));
6416 le16_add_cpu(&el
->l_next_free_rec
, -1);
6421 /* Can this actually happen? */
6422 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
6426 * We never actually deleted any clusters
6427 * because our leaf was empty. There's no
6428 * reason to adjust the rightmost edge then.
6433 rec
->e_int_clusters
= cpu_to_le32(new_edge
);
6434 le32_add_cpu(&rec
->e_int_clusters
,
6435 -le32_to_cpu(rec
->e_cpos
));
6438 * A deleted child record should have been
6441 BUG_ON(le32_to_cpu(rec
->e_int_clusters
) == 0);
6445 ret
= ocfs2_journal_dirty(handle
, bh
);
6451 mlog(0, "extent list container %llu, after: record %d: "
6452 "(%u, %u, %llu), next = %u.\n",
6453 (unsigned long long)bh
->b_blocknr
, i
,
6454 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
),
6455 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6456 le16_to_cpu(el
->l_next_free_rec
));
6459 * We must be careful to only attempt delete of an
6460 * extent block (and not the root inode block).
6462 if (index
> 0 && le16_to_cpu(el
->l_next_free_rec
) == 0) {
6463 struct ocfs2_extent_block
*eb
=
6464 (struct ocfs2_extent_block
*)bh
->b_data
;
6467 * Save this for use when processing the
6470 deleted_eb
= le64_to_cpu(eb
->h_blkno
);
6472 mlog(0, "deleting this extent block.\n");
6474 ocfs2_remove_from_cache(inode
, bh
);
6476 BUG_ON(ocfs2_rec_clusters(el
, &el
->l_recs
[0]));
6477 BUG_ON(le32_to_cpu(el
->l_recs
[0].e_cpos
));
6478 BUG_ON(le64_to_cpu(el
->l_recs
[0].e_blkno
));
6480 ret
= ocfs2_cache_extent_block_free(&tc
->tc_dealloc
, eb
);
6481 /* An error here is not fatal. */
6496 static int ocfs2_do_truncate(struct ocfs2_super
*osb
,
6497 unsigned int clusters_to_del
,
6498 struct inode
*inode
,
6499 struct buffer_head
*fe_bh
,
6501 struct ocfs2_truncate_context
*tc
,
6502 struct ocfs2_path
*path
)
6505 struct ocfs2_dinode
*fe
;
6506 struct ocfs2_extent_block
*last_eb
= NULL
;
6507 struct ocfs2_extent_list
*el
;
6508 struct buffer_head
*last_eb_bh
= NULL
;
6511 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
6513 status
= ocfs2_find_new_last_ext_blk(inode
, clusters_to_del
,
6521 * Each component will be touched, so we might as well journal
6522 * here to avoid having to handle errors later.
6524 status
= ocfs2_journal_access_path(inode
, handle
, path
);
6531 status
= ocfs2_journal_access(handle
, inode
, last_eb_bh
,
6532 OCFS2_JOURNAL_ACCESS_WRITE
);
6538 last_eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
6541 el
= &(fe
->id2
.i_list
);
6544 * Lower levels depend on this never happening, but it's best
6545 * to check it up here before changing the tree.
6547 if (el
->l_tree_depth
&& el
->l_recs
[0].e_int_clusters
== 0) {
6548 ocfs2_error(inode
->i_sb
,
6549 "Inode %lu has an empty extent record, depth %u\n",
6550 inode
->i_ino
, le16_to_cpu(el
->l_tree_depth
));
6555 spin_lock(&OCFS2_I(inode
)->ip_lock
);
6556 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
) -
6558 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
6559 le32_add_cpu(&fe
->i_clusters
, -clusters_to_del
);
6560 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6562 status
= ocfs2_trim_tree(inode
, path
, handle
, tc
,
6563 clusters_to_del
, &delete_blk
);
6569 if (le32_to_cpu(fe
->i_clusters
) == 0) {
6570 /* trunc to zero is a special case. */
6571 el
->l_tree_depth
= 0;
6572 fe
->i_last_eb_blk
= 0;
6574 fe
->i_last_eb_blk
= last_eb
->h_blkno
;
6576 status
= ocfs2_journal_dirty(handle
, fe_bh
);
6583 /* If there will be a new last extent block, then by
6584 * definition, there cannot be any leaves to the right of
6586 last_eb
->h_next_leaf_blk
= 0;
6587 status
= ocfs2_journal_dirty(handle
, last_eb_bh
);
6595 status
= ocfs2_truncate_log_append(osb
, handle
, delete_blk
,
6609 static int ocfs2_zero_func(handle_t
*handle
, struct buffer_head
*bh
)
6611 set_buffer_uptodate(bh
);
6612 mark_buffer_dirty(bh
);
6616 static void ocfs2_map_and_dirty_page(struct inode
*inode
, handle_t
*handle
,
6617 unsigned int from
, unsigned int to
,
6618 struct page
*page
, int zero
, u64
*phys
)
6620 int ret
, partial
= 0;
6622 ret
= ocfs2_map_page_blocks(page
, phys
, inode
, from
, to
, 0);
6627 zero_user_segment(page
, from
, to
);
6630 * Need to set the buffers we zero'd into uptodate
6631 * here if they aren't - ocfs2_map_page_blocks()
6632 * might've skipped some
6634 ret
= walk_page_buffers(handle
, page_buffers(page
),
6639 else if (ocfs2_should_order_data(inode
)) {
6640 ret
= ocfs2_jbd2_file_inode(handle
, inode
);
6646 SetPageUptodate(page
);
6648 flush_dcache_page(page
);
6651 static void ocfs2_zero_cluster_pages(struct inode
*inode
, loff_t start
,
6652 loff_t end
, struct page
**pages
,
6653 int numpages
, u64 phys
, handle_t
*handle
)
6657 unsigned int from
, to
= PAGE_CACHE_SIZE
;
6658 struct super_block
*sb
= inode
->i_sb
;
6660 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb
)));
6665 to
= PAGE_CACHE_SIZE
;
6666 for(i
= 0; i
< numpages
; i
++) {
6669 from
= start
& (PAGE_CACHE_SIZE
- 1);
6670 if ((end
>> PAGE_CACHE_SHIFT
) == page
->index
)
6671 to
= end
& (PAGE_CACHE_SIZE
- 1);
6673 BUG_ON(from
> PAGE_CACHE_SIZE
);
6674 BUG_ON(to
> PAGE_CACHE_SIZE
);
6676 ocfs2_map_and_dirty_page(inode
, handle
, from
, to
, page
, 1,
6679 start
= (page
->index
+ 1) << PAGE_CACHE_SHIFT
;
6683 ocfs2_unlock_and_free_pages(pages
, numpages
);
6686 static int ocfs2_grab_eof_pages(struct inode
*inode
, loff_t start
, loff_t end
,
6687 struct page
**pages
, int *num
)
6689 int numpages
, ret
= 0;
6690 struct super_block
*sb
= inode
->i_sb
;
6691 struct address_space
*mapping
= inode
->i_mapping
;
6692 unsigned long index
;
6693 loff_t last_page_bytes
;
6695 BUG_ON(start
> end
);
6697 BUG_ON(start
>> OCFS2_SB(sb
)->s_clustersize_bits
!=
6698 (end
- 1) >> OCFS2_SB(sb
)->s_clustersize_bits
);
6701 last_page_bytes
= PAGE_ALIGN(end
);
6702 index
= start
>> PAGE_CACHE_SHIFT
;
6704 pages
[numpages
] = grab_cache_page(mapping
, index
);
6705 if (!pages
[numpages
]) {
6713 } while (index
< (last_page_bytes
>> PAGE_CACHE_SHIFT
));
6718 ocfs2_unlock_and_free_pages(pages
, numpages
);
6728 * Zero the area past i_size but still within an allocated
6729 * cluster. This avoids exposing nonzero data on subsequent file
6732 * We need to call this before i_size is updated on the inode because
6733 * otherwise block_write_full_page() will skip writeout of pages past
6734 * i_size. The new_i_size parameter is passed for this reason.
6736 int ocfs2_zero_range_for_truncate(struct inode
*inode
, handle_t
*handle
,
6737 u64 range_start
, u64 range_end
)
6739 int ret
= 0, numpages
;
6740 struct page
**pages
= NULL
;
6742 unsigned int ext_flags
;
6743 struct super_block
*sb
= inode
->i_sb
;
6746 * File systems which don't support sparse files zero on every
6749 if (!ocfs2_sparse_alloc(OCFS2_SB(sb
)))
6752 pages
= kcalloc(ocfs2_pages_per_cluster(sb
),
6753 sizeof(struct page
*), GFP_NOFS
);
6754 if (pages
== NULL
) {
6760 if (range_start
== range_end
)
6763 ret
= ocfs2_extent_map_get_blocks(inode
,
6764 range_start
>> sb
->s_blocksize_bits
,
6765 &phys
, NULL
, &ext_flags
);
6772 * Tail is a hole, or is marked unwritten. In either case, we
6773 * can count on read and write to return/push zero's.
6775 if (phys
== 0 || ext_flags
& OCFS2_EXT_UNWRITTEN
)
6778 ret
= ocfs2_grab_eof_pages(inode
, range_start
, range_end
, pages
,
6785 ocfs2_zero_cluster_pages(inode
, range_start
, range_end
, pages
,
6786 numpages
, phys
, handle
);
6789 * Initiate writeout of the pages we zero'd here. We don't
6790 * wait on them - the truncate_inode_pages() call later will
6793 ret
= do_sync_mapping_range(inode
->i_mapping
, range_start
,
6794 range_end
- 1, SYNC_FILE_RANGE_WRITE
);
6805 static void ocfs2_zero_dinode_id2_with_xattr(struct inode
*inode
,
6806 struct ocfs2_dinode
*di
)
6808 unsigned int blocksize
= 1 << inode
->i_sb
->s_blocksize_bits
;
6809 unsigned int xattrsize
= le16_to_cpu(di
->i_xattr_inline_size
);
6811 if (le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_XATTR_FL
)
6812 memset(&di
->id2
, 0, blocksize
-
6813 offsetof(struct ocfs2_dinode
, id2
) -
6816 memset(&di
->id2
, 0, blocksize
-
6817 offsetof(struct ocfs2_dinode
, id2
));
6820 void ocfs2_dinode_new_extent_list(struct inode
*inode
,
6821 struct ocfs2_dinode
*di
)
6823 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
6824 di
->id2
.i_list
.l_tree_depth
= 0;
6825 di
->id2
.i_list
.l_next_free_rec
= 0;
6826 di
->id2
.i_list
.l_count
= cpu_to_le16(
6827 ocfs2_extent_recs_per_inode_with_xattr(inode
->i_sb
, di
));
6830 void ocfs2_set_inode_data_inline(struct inode
*inode
, struct ocfs2_dinode
*di
)
6832 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6833 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
6835 spin_lock(&oi
->ip_lock
);
6836 oi
->ip_dyn_features
|= OCFS2_INLINE_DATA_FL
;
6837 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
6838 spin_unlock(&oi
->ip_lock
);
6841 * We clear the entire i_data structure here so that all
6842 * fields can be properly initialized.
6844 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
6846 idata
->id_count
= cpu_to_le16(
6847 ocfs2_max_inline_data_with_xattr(inode
->i_sb
, di
));
6850 int ocfs2_convert_inline_data_to_extents(struct inode
*inode
,
6851 struct buffer_head
*di_bh
)
6853 int ret
, i
, has_data
, num_pages
= 0;
6855 u64
uninitialized_var(block
);
6856 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6857 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6858 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
6859 struct ocfs2_alloc_context
*data_ac
= NULL
;
6860 struct page
**pages
= NULL
;
6861 loff_t end
= osb
->s_clustersize
;
6862 struct ocfs2_extent_tree et
;
6864 has_data
= i_size_read(inode
) ? 1 : 0;
6867 pages
= kcalloc(ocfs2_pages_per_cluster(osb
->sb
),
6868 sizeof(struct page
*), GFP_NOFS
);
6869 if (pages
== NULL
) {
6875 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
6882 handle
= ocfs2_start_trans(osb
, OCFS2_INLINE_TO_EXTENTS_CREDITS
);
6883 if (IS_ERR(handle
)) {
6884 ret
= PTR_ERR(handle
);
6889 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
6890 OCFS2_JOURNAL_ACCESS_WRITE
);
6898 unsigned int page_end
;
6901 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
,
6909 * Save two copies, one for insert, and one that can
6910 * be changed by ocfs2_map_and_dirty_page() below.
6912 block
= phys
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
6915 * Non sparse file systems zero on extend, so no need
6918 if (!ocfs2_sparse_alloc(osb
) &&
6919 PAGE_CACHE_SIZE
< osb
->s_clustersize
)
6920 end
= PAGE_CACHE_SIZE
;
6922 ret
= ocfs2_grab_eof_pages(inode
, 0, end
, pages
, &num_pages
);
6929 * This should populate the 1st page for us and mark
6932 ret
= ocfs2_read_inline_data(inode
, pages
[0], di_bh
);
6938 page_end
= PAGE_CACHE_SIZE
;
6939 if (PAGE_CACHE_SIZE
> osb
->s_clustersize
)
6940 page_end
= osb
->s_clustersize
;
6942 for (i
= 0; i
< num_pages
; i
++)
6943 ocfs2_map_and_dirty_page(inode
, handle
, 0, page_end
,
6944 pages
[i
], i
> 0, &phys
);
6947 spin_lock(&oi
->ip_lock
);
6948 oi
->ip_dyn_features
&= ~OCFS2_INLINE_DATA_FL
;
6949 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
6950 spin_unlock(&oi
->ip_lock
);
6952 ocfs2_dinode_new_extent_list(inode
, di
);
6954 ocfs2_journal_dirty(handle
, di_bh
);
6958 * An error at this point should be extremely rare. If
6959 * this proves to be false, we could always re-build
6960 * the in-inode data from our pages.
6962 ocfs2_init_dinode_extent_tree(&et
, inode
, di_bh
);
6963 ret
= ocfs2_insert_extent(osb
, handle
, inode
, &et
,
6964 0, block
, 1, 0, NULL
);
6970 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6974 ocfs2_commit_trans(osb
, handle
);
6978 ocfs2_free_alloc_context(data_ac
);
6982 ocfs2_unlock_and_free_pages(pages
, num_pages
);
6990 * It is expected, that by the time you call this function,
6991 * inode->i_size and fe->i_size have been adjusted.
6993 * WARNING: This will kfree the truncate context
6995 int ocfs2_commit_truncate(struct ocfs2_super
*osb
,
6996 struct inode
*inode
,
6997 struct buffer_head
*fe_bh
,
6998 struct ocfs2_truncate_context
*tc
)
7000 int status
, i
, credits
, tl_sem
= 0;
7001 u32 clusters_to_del
, new_highest_cpos
, range
;
7002 struct ocfs2_extent_list
*el
;
7003 handle_t
*handle
= NULL
;
7004 struct inode
*tl_inode
= osb
->osb_tl_inode
;
7005 struct ocfs2_path
*path
= NULL
;
7006 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
7010 new_highest_cpos
= ocfs2_clusters_for_bytes(osb
->sb
,
7011 i_size_read(inode
));
7013 path
= ocfs2_new_path(fe_bh
, &di
->id2
.i_list
);
7020 ocfs2_extent_map_trunc(inode
, new_highest_cpos
);
7024 * Check that we still have allocation to delete.
7026 if (OCFS2_I(inode
)->ip_clusters
== 0) {
7032 * Truncate always works against the rightmost tree branch.
7034 status
= ocfs2_find_path(inode
, path
, UINT_MAX
);
7040 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7041 OCFS2_I(inode
)->ip_clusters
, path
->p_tree_depth
);
7044 * By now, el will point to the extent list on the bottom most
7045 * portion of this tree. Only the tail record is considered in
7048 * We handle the following cases, in order:
7049 * - empty extent: delete the remaining branch
7050 * - remove the entire record
7051 * - remove a partial record
7052 * - no record needs to be removed (truncate has completed)
7054 el
= path_leaf_el(path
);
7055 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
7056 ocfs2_error(inode
->i_sb
,
7057 "Inode %llu has empty extent block at %llu\n",
7058 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7059 (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7064 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
7065 range
= le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
7066 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7067 if (i
== 0 && ocfs2_is_empty_extent(&el
->l_recs
[i
])) {
7068 clusters_to_del
= 0;
7069 } else if (le32_to_cpu(el
->l_recs
[i
].e_cpos
) >= new_highest_cpos
) {
7070 clusters_to_del
= ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7071 } else if (range
> new_highest_cpos
) {
7072 clusters_to_del
= (ocfs2_rec_clusters(el
, &el
->l_recs
[i
]) +
7073 le32_to_cpu(el
->l_recs
[i
].e_cpos
)) -
7080 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7081 clusters_to_del
, (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7083 mutex_lock(&tl_inode
->i_mutex
);
7085 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7086 * record is free for use. If there isn't any, we flush to get
7087 * an empty truncate log. */
7088 if (ocfs2_truncate_log_needs_flush(osb
)) {
7089 status
= __ocfs2_flush_truncate_log(osb
);
7096 credits
= ocfs2_calc_tree_trunc_credits(osb
->sb
, clusters_to_del
,
7097 (struct ocfs2_dinode
*)fe_bh
->b_data
,
7099 handle
= ocfs2_start_trans(osb
, credits
);
7100 if (IS_ERR(handle
)) {
7101 status
= PTR_ERR(handle
);
7107 status
= ocfs2_do_truncate(osb
, clusters_to_del
, inode
, fe_bh
, handle
,
7114 mutex_unlock(&tl_inode
->i_mutex
);
7117 ocfs2_commit_trans(osb
, handle
);
7120 ocfs2_reinit_path(path
, 1);
7123 * The check above will catch the case where we've truncated
7124 * away all allocation.
7130 ocfs2_schedule_truncate_log_flush(osb
, 1);
7133 mutex_unlock(&tl_inode
->i_mutex
);
7136 ocfs2_commit_trans(osb
, handle
);
7138 ocfs2_run_deallocs(osb
, &tc
->tc_dealloc
);
7140 ocfs2_free_path(path
);
7142 /* This will drop the ext_alloc cluster lock for us */
7143 ocfs2_free_truncate_context(tc
);
7150 * Expects the inode to already be locked.
7152 int ocfs2_prepare_truncate(struct ocfs2_super
*osb
,
7153 struct inode
*inode
,
7154 struct buffer_head
*fe_bh
,
7155 struct ocfs2_truncate_context
**tc
)
7158 unsigned int new_i_clusters
;
7159 struct ocfs2_dinode
*fe
;
7160 struct ocfs2_extent_block
*eb
;
7161 struct buffer_head
*last_eb_bh
= NULL
;
7167 new_i_clusters
= ocfs2_clusters_for_bytes(osb
->sb
,
7168 i_size_read(inode
));
7169 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
7171 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7172 "%llu\n", le32_to_cpu(fe
->i_clusters
), new_i_clusters
,
7173 (unsigned long long)le64_to_cpu(fe
->i_size
));
7175 *tc
= kzalloc(sizeof(struct ocfs2_truncate_context
), GFP_KERNEL
);
7181 ocfs2_init_dealloc_ctxt(&(*tc
)->tc_dealloc
);
7183 if (fe
->id2
.i_list
.l_tree_depth
) {
7184 status
= ocfs2_read_extent_block(inode
,
7185 le64_to_cpu(fe
->i_last_eb_blk
),
7191 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
7194 (*tc
)->tc_last_eb_bh
= last_eb_bh
;
7200 ocfs2_free_truncate_context(*tc
);
7208 * 'start' is inclusive, 'end' is not.
7210 int ocfs2_truncate_inline(struct inode
*inode
, struct buffer_head
*di_bh
,
7211 unsigned int start
, unsigned int end
, int trunc
)
7214 unsigned int numbytes
;
7216 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7217 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
7218 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
7220 if (end
> i_size_read(inode
))
7221 end
= i_size_read(inode
);
7223 BUG_ON(start
>= end
);
7225 if (!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) ||
7226 !(le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_DATA_FL
) ||
7227 !ocfs2_supports_inline_data(osb
)) {
7228 ocfs2_error(inode
->i_sb
,
7229 "Inline data flags for inode %llu don't agree! "
7230 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7231 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7232 le16_to_cpu(di
->i_dyn_features
),
7233 OCFS2_I(inode
)->ip_dyn_features
,
7234 osb
->s_feature_incompat
);
7239 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
7240 if (IS_ERR(handle
)) {
7241 ret
= PTR_ERR(handle
);
7246 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
7247 OCFS2_JOURNAL_ACCESS_WRITE
);
7253 numbytes
= end
- start
;
7254 memset(idata
->id_data
+ start
, 0, numbytes
);
7257 * No need to worry about the data page here - it's been
7258 * truncated already and inline data doesn't need it for
7259 * pushing zero's to disk, so we'll let readpage pick it up
7263 i_size_write(inode
, start
);
7264 di
->i_size
= cpu_to_le64(start
);
7267 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7268 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
7270 di
->i_ctime
= di
->i_mtime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
7271 di
->i_ctime_nsec
= di
->i_mtime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
7273 ocfs2_journal_dirty(handle
, di_bh
);
7276 ocfs2_commit_trans(osb
, handle
);
7282 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
)
7285 * The caller is responsible for completing deallocation
7286 * before freeing the context.
7288 if (tc
->tc_dealloc
.c_first_suballocator
!= NULL
)
7290 "Truncate completion has non-empty dealloc context\n");
7292 brelse(tc
->tc_last_eb_bh
);