1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Extent allocs and frees
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
30 #include <linux/swap.h>
31 #include <linux/quotaops.h>
33 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
34 #include <cluster/masklog.h>
41 #include "extent_map.h"
44 #include "localalloc.h"
51 #include "buffer_head_io.h"
55 * Operations for a specific extent tree type.
57 * To implement an on-disk btree (extent tree) type in ocfs2, add
58 * an ocfs2_extent_tree_operations structure and the matching
59 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
60 * for the allocation portion of the extent tree.
62 struct ocfs2_extent_tree_operations
{
64 * last_eb_blk is the block number of the right most leaf extent
65 * block. Most on-disk structures containing an extent tree store
66 * this value for fast access. The ->eo_set_last_eb_blk() and
67 * ->eo_get_last_eb_blk() operations access this value. They are
70 void (*eo_set_last_eb_blk
)(struct ocfs2_extent_tree
*et
,
72 u64 (*eo_get_last_eb_blk
)(struct ocfs2_extent_tree
*et
);
75 * The on-disk structure usually keeps track of how many total
76 * clusters are stored in this extent tree. This function updates
77 * that value. new_clusters is the delta, and must be
78 * added to the total. Required.
80 void (*eo_update_clusters
)(struct inode
*inode
,
81 struct ocfs2_extent_tree
*et
,
85 * If ->eo_insert_check() exists, it is called before rec is
86 * inserted into the extent tree. It is optional.
88 int (*eo_insert_check
)(struct inode
*inode
,
89 struct ocfs2_extent_tree
*et
,
90 struct ocfs2_extent_rec
*rec
);
91 int (*eo_sanity_check
)(struct inode
*inode
, struct ocfs2_extent_tree
*et
);
94 * --------------------------------------------------------------
95 * The remaining are internal to ocfs2_extent_tree and don't have
100 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
103 void (*eo_fill_root_el
)(struct ocfs2_extent_tree
*et
);
106 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
107 * it exists. If it does not, et->et_max_leaf_clusters is set
108 * to 0 (unlimited). Optional.
110 void (*eo_fill_max_leaf_clusters
)(struct inode
*inode
,
111 struct ocfs2_extent_tree
*et
);
116 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
119 static u64
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree
*et
);
120 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
122 static void ocfs2_dinode_update_clusters(struct inode
*inode
,
123 struct ocfs2_extent_tree
*et
,
125 static int ocfs2_dinode_insert_check(struct inode
*inode
,
126 struct ocfs2_extent_tree
*et
,
127 struct ocfs2_extent_rec
*rec
);
128 static int ocfs2_dinode_sanity_check(struct inode
*inode
,
129 struct ocfs2_extent_tree
*et
);
130 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree
*et
);
131 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops
= {
132 .eo_set_last_eb_blk
= ocfs2_dinode_set_last_eb_blk
,
133 .eo_get_last_eb_blk
= ocfs2_dinode_get_last_eb_blk
,
134 .eo_update_clusters
= ocfs2_dinode_update_clusters
,
135 .eo_insert_check
= ocfs2_dinode_insert_check
,
136 .eo_sanity_check
= ocfs2_dinode_sanity_check
,
137 .eo_fill_root_el
= ocfs2_dinode_fill_root_el
,
140 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
143 struct ocfs2_dinode
*di
= et
->et_object
;
145 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
146 di
->i_last_eb_blk
= cpu_to_le64(blkno
);
149 static u64
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
151 struct ocfs2_dinode
*di
= et
->et_object
;
153 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
154 return le64_to_cpu(di
->i_last_eb_blk
);
157 static void ocfs2_dinode_update_clusters(struct inode
*inode
,
158 struct ocfs2_extent_tree
*et
,
161 struct ocfs2_dinode
*di
= et
->et_object
;
163 le32_add_cpu(&di
->i_clusters
, clusters
);
164 spin_lock(&OCFS2_I(inode
)->ip_lock
);
165 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(di
->i_clusters
);
166 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
169 static int ocfs2_dinode_insert_check(struct inode
*inode
,
170 struct ocfs2_extent_tree
*et
,
171 struct ocfs2_extent_rec
*rec
)
173 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
175 BUG_ON(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
);
176 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb
) &&
177 (OCFS2_I(inode
)->ip_clusters
!= rec
->e_cpos
),
178 "Device %s, asking for sparse allocation: inode %llu, "
179 "cpos %u, clusters %u\n",
181 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
183 OCFS2_I(inode
)->ip_clusters
);
188 static int ocfs2_dinode_sanity_check(struct inode
*inode
,
189 struct ocfs2_extent_tree
*et
)
191 struct ocfs2_dinode
*di
= et
->et_object
;
193 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
194 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
199 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree
*et
)
201 struct ocfs2_dinode
*di
= et
->et_object
;
203 et
->et_root_el
= &di
->id2
.i_list
;
207 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree
*et
)
209 struct ocfs2_xattr_value_root
*xv
= et
->et_object
;
211 et
->et_root_el
= &xv
->xr_list
;
214 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
217 struct ocfs2_xattr_value_root
*xv
=
218 (struct ocfs2_xattr_value_root
*)et
->et_object
;
220 xv
->xr_last_eb_blk
= cpu_to_le64(blkno
);
223 static u64
ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
225 struct ocfs2_xattr_value_root
*xv
=
226 (struct ocfs2_xattr_value_root
*) et
->et_object
;
228 return le64_to_cpu(xv
->xr_last_eb_blk
);
231 static void ocfs2_xattr_value_update_clusters(struct inode
*inode
,
232 struct ocfs2_extent_tree
*et
,
235 struct ocfs2_xattr_value_root
*xv
=
236 (struct ocfs2_xattr_value_root
*)et
->et_object
;
238 le32_add_cpu(&xv
->xr_clusters
, clusters
);
241 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops
= {
242 .eo_set_last_eb_blk
= ocfs2_xattr_value_set_last_eb_blk
,
243 .eo_get_last_eb_blk
= ocfs2_xattr_value_get_last_eb_blk
,
244 .eo_update_clusters
= ocfs2_xattr_value_update_clusters
,
245 .eo_fill_root_el
= ocfs2_xattr_value_fill_root_el
,
248 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree
*et
)
250 struct ocfs2_xattr_block
*xb
= et
->et_object
;
252 et
->et_root_el
= &xb
->xb_attrs
.xb_root
.xt_list
;
255 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode
*inode
,
256 struct ocfs2_extent_tree
*et
)
258 et
->et_max_leaf_clusters
=
259 ocfs2_clusters_for_bytes(inode
->i_sb
,
260 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
);
263 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
266 struct ocfs2_xattr_block
*xb
= et
->et_object
;
267 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
269 xt
->xt_last_eb_blk
= cpu_to_le64(blkno
);
272 static u64
ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
274 struct ocfs2_xattr_block
*xb
= et
->et_object
;
275 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
277 return le64_to_cpu(xt
->xt_last_eb_blk
);
280 static void ocfs2_xattr_tree_update_clusters(struct inode
*inode
,
281 struct ocfs2_extent_tree
*et
,
284 struct ocfs2_xattr_block
*xb
= et
->et_object
;
286 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, clusters
);
289 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops
= {
290 .eo_set_last_eb_blk
= ocfs2_xattr_tree_set_last_eb_blk
,
291 .eo_get_last_eb_blk
= ocfs2_xattr_tree_get_last_eb_blk
,
292 .eo_update_clusters
= ocfs2_xattr_tree_update_clusters
,
293 .eo_fill_root_el
= ocfs2_xattr_tree_fill_root_el
,
294 .eo_fill_max_leaf_clusters
= ocfs2_xattr_tree_fill_max_leaf_clusters
,
297 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree
*et
,
299 struct buffer_head
*bh
,
301 struct ocfs2_extent_tree_operations
*ops
)
306 obj
= (void *)bh
->b_data
;
309 et
->et_ops
->eo_fill_root_el(et
);
310 if (!et
->et_ops
->eo_fill_max_leaf_clusters
)
311 et
->et_max_leaf_clusters
= 0;
313 et
->et_ops
->eo_fill_max_leaf_clusters(inode
, et
);
316 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree
*et
,
318 struct buffer_head
*bh
)
320 __ocfs2_init_extent_tree(et
, inode
, bh
, NULL
, &ocfs2_dinode_et_ops
);
323 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree
*et
,
325 struct buffer_head
*bh
)
327 __ocfs2_init_extent_tree(et
, inode
, bh
, NULL
,
328 &ocfs2_xattr_tree_et_ops
);
331 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree
*et
,
333 struct buffer_head
*bh
,
334 struct ocfs2_xattr_value_root
*xv
)
336 __ocfs2_init_extent_tree(et
, inode
, bh
, xv
,
337 &ocfs2_xattr_value_et_ops
);
340 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
343 et
->et_ops
->eo_set_last_eb_blk(et
, new_last_eb_blk
);
346 static inline u64
ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
348 return et
->et_ops
->eo_get_last_eb_blk(et
);
351 static inline void ocfs2_et_update_clusters(struct inode
*inode
,
352 struct ocfs2_extent_tree
*et
,
355 et
->et_ops
->eo_update_clusters(inode
, et
, clusters
);
358 static inline int ocfs2_et_insert_check(struct inode
*inode
,
359 struct ocfs2_extent_tree
*et
,
360 struct ocfs2_extent_rec
*rec
)
364 if (et
->et_ops
->eo_insert_check
)
365 ret
= et
->et_ops
->eo_insert_check(inode
, et
, rec
);
369 static inline int ocfs2_et_sanity_check(struct inode
*inode
,
370 struct ocfs2_extent_tree
*et
)
374 if (et
->et_ops
->eo_sanity_check
)
375 ret
= et
->et_ops
->eo_sanity_check(inode
, et
);
379 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
);
380 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
381 struct ocfs2_extent_block
*eb
);
384 * Structures which describe a path through a btree, and functions to
387 * The idea here is to be as generic as possible with the tree
390 struct ocfs2_path_item
{
391 struct buffer_head
*bh
;
392 struct ocfs2_extent_list
*el
;
395 #define OCFS2_MAX_PATH_DEPTH 5
399 struct ocfs2_path_item p_node
[OCFS2_MAX_PATH_DEPTH
];
402 #define path_root_bh(_path) ((_path)->p_node[0].bh)
403 #define path_root_el(_path) ((_path)->p_node[0].el)
404 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
405 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
406 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
409 * Reset the actual path elements so that we can re-use the structure
410 * to build another path. Generally, this involves freeing the buffer
413 static void ocfs2_reinit_path(struct ocfs2_path
*path
, int keep_root
)
415 int i
, start
= 0, depth
= 0;
416 struct ocfs2_path_item
*node
;
421 for(i
= start
; i
< path_num_items(path
); i
++) {
422 node
= &path
->p_node
[i
];
430 * Tree depth may change during truncate, or insert. If we're
431 * keeping the root extent list, then make sure that our path
432 * structure reflects the proper depth.
435 depth
= le16_to_cpu(path_root_el(path
)->l_tree_depth
);
437 path
->p_tree_depth
= depth
;
440 static void ocfs2_free_path(struct ocfs2_path
*path
)
443 ocfs2_reinit_path(path
, 0);
449 * All the elements of src into dest. After this call, src could be freed
450 * without affecting dest.
452 * Both paths should have the same root. Any non-root elements of dest
455 static void ocfs2_cp_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
459 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
460 BUG_ON(path_root_el(dest
) != path_root_el(src
));
462 ocfs2_reinit_path(dest
, 1);
464 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
465 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
466 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
468 if (dest
->p_node
[i
].bh
)
469 get_bh(dest
->p_node
[i
].bh
);
474 * Make the *dest path the same as src and re-initialize src path to
477 static void ocfs2_mv_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
481 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
483 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
484 brelse(dest
->p_node
[i
].bh
);
486 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
487 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
489 src
->p_node
[i
].bh
= NULL
;
490 src
->p_node
[i
].el
= NULL
;
495 * Insert an extent block at given index.
497 * This will not take an additional reference on eb_bh.
499 static inline void ocfs2_path_insert_eb(struct ocfs2_path
*path
, int index
,
500 struct buffer_head
*eb_bh
)
502 struct ocfs2_extent_block
*eb
= (struct ocfs2_extent_block
*)eb_bh
->b_data
;
505 * Right now, no root bh is an extent block, so this helps
506 * catch code errors with dinode trees. The assertion can be
507 * safely removed if we ever need to insert extent block
508 * structures at the root.
512 path
->p_node
[index
].bh
= eb_bh
;
513 path
->p_node
[index
].el
= &eb
->h_list
;
516 static struct ocfs2_path
*ocfs2_new_path(struct buffer_head
*root_bh
,
517 struct ocfs2_extent_list
*root_el
)
519 struct ocfs2_path
*path
;
521 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) >= OCFS2_MAX_PATH_DEPTH
);
523 path
= kzalloc(sizeof(*path
), GFP_NOFS
);
525 path
->p_tree_depth
= le16_to_cpu(root_el
->l_tree_depth
);
527 path_root_bh(path
) = root_bh
;
528 path_root_el(path
) = root_el
;
535 * Convenience function to journal all components in a path.
537 static int ocfs2_journal_access_path(struct inode
*inode
, handle_t
*handle
,
538 struct ocfs2_path
*path
)
545 for(i
= 0; i
< path_num_items(path
); i
++) {
546 ret
= ocfs2_journal_access(handle
, inode
, path
->p_node
[i
].bh
,
547 OCFS2_JOURNAL_ACCESS_WRITE
);
559 * Return the index of the extent record which contains cluster #v_cluster.
560 * -1 is returned if it was not found.
562 * Should work fine on interior and exterior nodes.
564 int ocfs2_search_extent_list(struct ocfs2_extent_list
*el
, u32 v_cluster
)
568 struct ocfs2_extent_rec
*rec
;
569 u32 rec_end
, rec_start
, clusters
;
571 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
572 rec
= &el
->l_recs
[i
];
574 rec_start
= le32_to_cpu(rec
->e_cpos
);
575 clusters
= ocfs2_rec_clusters(el
, rec
);
577 rec_end
= rec_start
+ clusters
;
579 if (v_cluster
>= rec_start
&& v_cluster
< rec_end
) {
588 enum ocfs2_contig_type
{
597 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
598 * ocfs2_extent_contig only work properly against leaf nodes!
600 static int ocfs2_block_extent_contig(struct super_block
*sb
,
601 struct ocfs2_extent_rec
*ext
,
604 u64 blk_end
= le64_to_cpu(ext
->e_blkno
);
606 blk_end
+= ocfs2_clusters_to_blocks(sb
,
607 le16_to_cpu(ext
->e_leaf_clusters
));
609 return blkno
== blk_end
;
612 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec
*left
,
613 struct ocfs2_extent_rec
*right
)
617 left_range
= le32_to_cpu(left
->e_cpos
) +
618 le16_to_cpu(left
->e_leaf_clusters
);
620 return (left_range
== le32_to_cpu(right
->e_cpos
));
623 static enum ocfs2_contig_type
624 ocfs2_extent_contig(struct inode
*inode
,
625 struct ocfs2_extent_rec
*ext
,
626 struct ocfs2_extent_rec
*insert_rec
)
628 u64 blkno
= le64_to_cpu(insert_rec
->e_blkno
);
631 * Refuse to coalesce extent records with different flag
632 * fields - we don't want to mix unwritten extents with user
635 if (ext
->e_flags
!= insert_rec
->e_flags
)
638 if (ocfs2_extents_adjacent(ext
, insert_rec
) &&
639 ocfs2_block_extent_contig(inode
->i_sb
, ext
, blkno
))
642 blkno
= le64_to_cpu(ext
->e_blkno
);
643 if (ocfs2_extents_adjacent(insert_rec
, ext
) &&
644 ocfs2_block_extent_contig(inode
->i_sb
, insert_rec
, blkno
))
651 * NOTE: We can have pretty much any combination of contiguousness and
654 * The usefulness of APPEND_TAIL is more in that it lets us know that
655 * we'll have to update the path to that leaf.
657 enum ocfs2_append_type
{
662 enum ocfs2_split_type
{
668 struct ocfs2_insert_type
{
669 enum ocfs2_split_type ins_split
;
670 enum ocfs2_append_type ins_appending
;
671 enum ocfs2_contig_type ins_contig
;
672 int ins_contig_index
;
676 struct ocfs2_merge_ctxt
{
677 enum ocfs2_contig_type c_contig_type
;
678 int c_has_empty_extent
;
679 int c_split_covers_rec
;
682 static int ocfs2_validate_extent_block(struct super_block
*sb
,
683 struct buffer_head
*bh
)
685 struct ocfs2_extent_block
*eb
=
686 (struct ocfs2_extent_block
*)bh
->b_data
;
688 mlog(0, "Validating extent block %llu\n",
689 (unsigned long long)bh
->b_blocknr
);
691 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
693 "Extent block #%llu has bad signature %.*s",
694 (unsigned long long)bh
->b_blocknr
, 7,
699 if (le64_to_cpu(eb
->h_blkno
) != bh
->b_blocknr
) {
701 "Extent block #%llu has an invalid h_blkno "
703 (unsigned long long)bh
->b_blocknr
,
704 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
708 if (le32_to_cpu(eb
->h_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
710 "Extent block #%llu has an invalid "
711 "h_fs_generation of #%u",
712 (unsigned long long)bh
->b_blocknr
,
713 le32_to_cpu(eb
->h_fs_generation
));
720 int ocfs2_read_extent_block(struct inode
*inode
, u64 eb_blkno
,
721 struct buffer_head
**bh
)
724 struct buffer_head
*tmp
= *bh
;
726 rc
= ocfs2_read_block(inode
, eb_blkno
, &tmp
,
727 ocfs2_validate_extent_block
);
729 /* If ocfs2_read_block() got us a new bh, pass it up. */
738 * How many free extents have we got before we need more meta data?
740 int ocfs2_num_free_extents(struct ocfs2_super
*osb
,
742 struct ocfs2_extent_tree
*et
)
745 struct ocfs2_extent_list
*el
= NULL
;
746 struct ocfs2_extent_block
*eb
;
747 struct buffer_head
*eb_bh
= NULL
;
753 last_eb_blk
= ocfs2_et_get_last_eb_blk(et
);
756 retval
= ocfs2_read_extent_block(inode
, last_eb_blk
, &eb_bh
);
761 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
765 BUG_ON(el
->l_tree_depth
!= 0);
767 retval
= le16_to_cpu(el
->l_count
) - le16_to_cpu(el
->l_next_free_rec
);
775 /* expects array to already be allocated
777 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
780 static int ocfs2_create_new_meta_bhs(struct ocfs2_super
*osb
,
784 struct ocfs2_alloc_context
*meta_ac
,
785 struct buffer_head
*bhs
[])
787 int count
, status
, i
;
788 u16 suballoc_bit_start
;
791 struct ocfs2_extent_block
*eb
;
796 while (count
< wanted
) {
797 status
= ocfs2_claim_metadata(osb
,
809 for(i
= count
; i
< (num_got
+ count
); i
++) {
810 bhs
[i
] = sb_getblk(osb
->sb
, first_blkno
);
811 if (bhs
[i
] == NULL
) {
816 ocfs2_set_new_buffer_uptodate(inode
, bhs
[i
]);
818 status
= ocfs2_journal_access(handle
, inode
, bhs
[i
],
819 OCFS2_JOURNAL_ACCESS_CREATE
);
825 memset(bhs
[i
]->b_data
, 0, osb
->sb
->s_blocksize
);
826 eb
= (struct ocfs2_extent_block
*) bhs
[i
]->b_data
;
827 /* Ok, setup the minimal stuff here. */
828 strcpy(eb
->h_signature
, OCFS2_EXTENT_BLOCK_SIGNATURE
);
829 eb
->h_blkno
= cpu_to_le64(first_blkno
);
830 eb
->h_fs_generation
= cpu_to_le32(osb
->fs_generation
);
831 eb
->h_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
832 eb
->h_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
834 cpu_to_le16(ocfs2_extent_recs_per_eb(osb
->sb
));
836 suballoc_bit_start
++;
839 /* We'll also be dirtied by the caller, so
840 * this isn't absolutely necessary. */
841 status
= ocfs2_journal_dirty(handle
, bhs
[i
]);
854 for(i
= 0; i
< wanted
; i
++) {
864 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
866 * Returns the sum of the rightmost extent rec logical offset and
869 * ocfs2_add_branch() uses this to determine what logical cluster
870 * value should be populated into the leftmost new branch records.
872 * ocfs2_shift_tree_depth() uses this to determine the # clusters
873 * value for the new topmost tree record.
875 static inline u32
ocfs2_sum_rightmost_rec(struct ocfs2_extent_list
*el
)
879 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
881 return le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
882 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
886 * Add an entire tree branch to our inode. eb_bh is the extent block
887 * to start at, if we don't want to start the branch at the dinode
890 * last_eb_bh is required as we have to update it's next_leaf pointer
891 * for the new last extent block.
893 * the new branch will be 'empty' in the sense that every block will
894 * contain a single record with cluster count == 0.
896 static int ocfs2_add_branch(struct ocfs2_super
*osb
,
899 struct ocfs2_extent_tree
*et
,
900 struct buffer_head
*eb_bh
,
901 struct buffer_head
**last_eb_bh
,
902 struct ocfs2_alloc_context
*meta_ac
)
904 int status
, new_blocks
, i
;
905 u64 next_blkno
, new_last_eb_blk
;
906 struct buffer_head
*bh
;
907 struct buffer_head
**new_eb_bhs
= NULL
;
908 struct ocfs2_extent_block
*eb
;
909 struct ocfs2_extent_list
*eb_el
;
910 struct ocfs2_extent_list
*el
;
915 BUG_ON(!last_eb_bh
|| !*last_eb_bh
);
918 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
923 /* we never add a branch to a leaf. */
924 BUG_ON(!el
->l_tree_depth
);
926 new_blocks
= le16_to_cpu(el
->l_tree_depth
);
928 /* allocate the number of new eb blocks we need */
929 new_eb_bhs
= kcalloc(new_blocks
, sizeof(struct buffer_head
*),
937 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, new_blocks
,
938 meta_ac
, new_eb_bhs
);
944 eb
= (struct ocfs2_extent_block
*)(*last_eb_bh
)->b_data
;
945 new_cpos
= ocfs2_sum_rightmost_rec(&eb
->h_list
);
947 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
948 * linked with the rest of the tree.
949 * conversly, new_eb_bhs[0] is the new bottommost leaf.
951 * when we leave the loop, new_last_eb_blk will point to the
952 * newest leaf, and next_blkno will point to the topmost extent
954 next_blkno
= new_last_eb_blk
= 0;
955 for(i
= 0; i
< new_blocks
; i
++) {
957 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
958 /* ocfs2_create_new_meta_bhs() should create it right! */
959 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
962 status
= ocfs2_journal_access(handle
, inode
, bh
,
963 OCFS2_JOURNAL_ACCESS_CREATE
);
969 eb
->h_next_leaf_blk
= 0;
970 eb_el
->l_tree_depth
= cpu_to_le16(i
);
971 eb_el
->l_next_free_rec
= cpu_to_le16(1);
973 * This actually counts as an empty extent as
976 eb_el
->l_recs
[0].e_cpos
= cpu_to_le32(new_cpos
);
977 eb_el
->l_recs
[0].e_blkno
= cpu_to_le64(next_blkno
);
979 * eb_el isn't always an interior node, but even leaf
980 * nodes want a zero'd flags and reserved field so
981 * this gets the whole 32 bits regardless of use.
983 eb_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(0);
984 if (!eb_el
->l_tree_depth
)
985 new_last_eb_blk
= le64_to_cpu(eb
->h_blkno
);
987 status
= ocfs2_journal_dirty(handle
, bh
);
993 next_blkno
= le64_to_cpu(eb
->h_blkno
);
996 /* This is a bit hairy. We want to update up to three blocks
997 * here without leaving any of them in an inconsistent state
998 * in case of error. We don't have to worry about
999 * journal_dirty erroring as it won't unless we've aborted the
1000 * handle (in which case we would never be here) so reserving
1001 * the write with journal_access is all we need to do. */
1002 status
= ocfs2_journal_access(handle
, inode
, *last_eb_bh
,
1003 OCFS2_JOURNAL_ACCESS_WRITE
);
1008 status
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
1009 OCFS2_JOURNAL_ACCESS_WRITE
);
1015 status
= ocfs2_journal_access(handle
, inode
, eb_bh
,
1016 OCFS2_JOURNAL_ACCESS_WRITE
);
1023 /* Link the new branch into the rest of the tree (el will
1024 * either be on the root_bh, or the extent block passed in. */
1025 i
= le16_to_cpu(el
->l_next_free_rec
);
1026 el
->l_recs
[i
].e_blkno
= cpu_to_le64(next_blkno
);
1027 el
->l_recs
[i
].e_cpos
= cpu_to_le32(new_cpos
);
1028 el
->l_recs
[i
].e_int_clusters
= 0;
1029 le16_add_cpu(&el
->l_next_free_rec
, 1);
1031 /* fe needs a new last extent block pointer, as does the
1032 * next_leaf on the previously last-extent-block. */
1033 ocfs2_et_set_last_eb_blk(et
, new_last_eb_blk
);
1035 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
1036 eb
->h_next_leaf_blk
= cpu_to_le64(new_last_eb_blk
);
1038 status
= ocfs2_journal_dirty(handle
, *last_eb_bh
);
1041 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1045 status
= ocfs2_journal_dirty(handle
, eb_bh
);
1051 * Some callers want to track the rightmost leaf so pass it
1054 brelse(*last_eb_bh
);
1055 get_bh(new_eb_bhs
[0]);
1056 *last_eb_bh
= new_eb_bhs
[0];
1061 for (i
= 0; i
< new_blocks
; i
++)
1062 brelse(new_eb_bhs
[i
]);
1071 * adds another level to the allocation tree.
1072 * returns back the new extent block so you can add a branch to it
1075 static int ocfs2_shift_tree_depth(struct ocfs2_super
*osb
,
1077 struct inode
*inode
,
1078 struct ocfs2_extent_tree
*et
,
1079 struct ocfs2_alloc_context
*meta_ac
,
1080 struct buffer_head
**ret_new_eb_bh
)
1084 struct buffer_head
*new_eb_bh
= NULL
;
1085 struct ocfs2_extent_block
*eb
;
1086 struct ocfs2_extent_list
*root_el
;
1087 struct ocfs2_extent_list
*eb_el
;
1091 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, 1, meta_ac
,
1098 eb
= (struct ocfs2_extent_block
*) new_eb_bh
->b_data
;
1099 /* ocfs2_create_new_meta_bhs() should create it right! */
1100 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
1102 eb_el
= &eb
->h_list
;
1103 root_el
= et
->et_root_el
;
1105 status
= ocfs2_journal_access(handle
, inode
, new_eb_bh
,
1106 OCFS2_JOURNAL_ACCESS_CREATE
);
1112 /* copy the root extent list data into the new extent block */
1113 eb_el
->l_tree_depth
= root_el
->l_tree_depth
;
1114 eb_el
->l_next_free_rec
= root_el
->l_next_free_rec
;
1115 for (i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1116 eb_el
->l_recs
[i
] = root_el
->l_recs
[i
];
1118 status
= ocfs2_journal_dirty(handle
, new_eb_bh
);
1124 status
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
1125 OCFS2_JOURNAL_ACCESS_WRITE
);
1131 new_clusters
= ocfs2_sum_rightmost_rec(eb_el
);
1133 /* update root_bh now */
1134 le16_add_cpu(&root_el
->l_tree_depth
, 1);
1135 root_el
->l_recs
[0].e_cpos
= 0;
1136 root_el
->l_recs
[0].e_blkno
= eb
->h_blkno
;
1137 root_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(new_clusters
);
1138 for (i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1139 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
1140 root_el
->l_next_free_rec
= cpu_to_le16(1);
1142 /* If this is our 1st tree depth shift, then last_eb_blk
1143 * becomes the allocated extent block */
1144 if (root_el
->l_tree_depth
== cpu_to_le16(1))
1145 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
1147 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1153 *ret_new_eb_bh
= new_eb_bh
;
1164 * Should only be called when there is no space left in any of the
1165 * leaf nodes. What we want to do is find the lowest tree depth
1166 * non-leaf extent block with room for new records. There are three
1167 * valid results of this search:
1169 * 1) a lowest extent block is found, then we pass it back in
1170 * *lowest_eb_bh and return '0'
1172 * 2) the search fails to find anything, but the root_el has room. We
1173 * pass NULL back in *lowest_eb_bh, but still return '0'
1175 * 3) the search fails to find anything AND the root_el is full, in
1176 * which case we return > 0
1178 * return status < 0 indicates an error.
1180 static int ocfs2_find_branch_target(struct ocfs2_super
*osb
,
1181 struct inode
*inode
,
1182 struct ocfs2_extent_tree
*et
,
1183 struct buffer_head
**target_bh
)
1187 struct ocfs2_extent_block
*eb
;
1188 struct ocfs2_extent_list
*el
;
1189 struct buffer_head
*bh
= NULL
;
1190 struct buffer_head
*lowest_bh
= NULL
;
1196 el
= et
->et_root_el
;
1198 while(le16_to_cpu(el
->l_tree_depth
) > 1) {
1199 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1200 ocfs2_error(inode
->i_sb
, "Dinode %llu has empty "
1201 "extent list (next_free_rec == 0)",
1202 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1206 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1207 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1209 ocfs2_error(inode
->i_sb
, "Dinode %llu has extent "
1210 "list where extent # %d has no physical "
1212 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, i
);
1220 status
= ocfs2_read_extent_block(inode
, blkno
, &bh
);
1226 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1229 if (le16_to_cpu(el
->l_next_free_rec
) <
1230 le16_to_cpu(el
->l_count
)) {
1237 /* If we didn't find one and the fe doesn't have any room,
1238 * then return '1' */
1239 el
= et
->et_root_el
;
1240 if (!lowest_bh
&& (el
->l_next_free_rec
== el
->l_count
))
1243 *target_bh
= lowest_bh
;
1252 * Grow a b-tree so that it has more records.
1254 * We might shift the tree depth in which case existing paths should
1255 * be considered invalid.
1257 * Tree depth after the grow is returned via *final_depth.
1259 * *last_eb_bh will be updated by ocfs2_add_branch().
1261 static int ocfs2_grow_tree(struct inode
*inode
, handle_t
*handle
,
1262 struct ocfs2_extent_tree
*et
, int *final_depth
,
1263 struct buffer_head
**last_eb_bh
,
1264 struct ocfs2_alloc_context
*meta_ac
)
1267 struct ocfs2_extent_list
*el
= et
->et_root_el
;
1268 int depth
= le16_to_cpu(el
->l_tree_depth
);
1269 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1270 struct buffer_head
*bh
= NULL
;
1272 BUG_ON(meta_ac
== NULL
);
1274 shift
= ocfs2_find_branch_target(osb
, inode
, et
, &bh
);
1281 /* We traveled all the way to the bottom of the allocation tree
1282 * and didn't find room for any more extents - we need to add
1283 * another tree level */
1286 mlog(0, "need to shift tree depth (current = %d)\n", depth
);
1288 /* ocfs2_shift_tree_depth will return us a buffer with
1289 * the new extent block (so we can pass that to
1290 * ocfs2_add_branch). */
1291 ret
= ocfs2_shift_tree_depth(osb
, handle
, inode
, et
,
1300 * Special case: we have room now if we shifted from
1301 * tree_depth 0, so no more work needs to be done.
1303 * We won't be calling add_branch, so pass
1304 * back *last_eb_bh as the new leaf. At depth
1305 * zero, it should always be null so there's
1306 * no reason to brelse.
1308 BUG_ON(*last_eb_bh
);
1315 /* call ocfs2_add_branch to add the final part of the tree with
1317 mlog(0, "add branch. bh = %p\n", bh
);
1318 ret
= ocfs2_add_branch(osb
, handle
, inode
, et
, bh
, last_eb_bh
,
1327 *final_depth
= depth
;
1333 * This function will discard the rightmost extent record.
1335 static void ocfs2_shift_records_right(struct ocfs2_extent_list
*el
)
1337 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1338 int count
= le16_to_cpu(el
->l_count
);
1339 unsigned int num_bytes
;
1342 /* This will cause us to go off the end of our extent list. */
1343 BUG_ON(next_free
>= count
);
1345 num_bytes
= sizeof(struct ocfs2_extent_rec
) * next_free
;
1347 memmove(&el
->l_recs
[1], &el
->l_recs
[0], num_bytes
);
1350 static void ocfs2_rotate_leaf(struct ocfs2_extent_list
*el
,
1351 struct ocfs2_extent_rec
*insert_rec
)
1353 int i
, insert_index
, next_free
, has_empty
, num_bytes
;
1354 u32 insert_cpos
= le32_to_cpu(insert_rec
->e_cpos
);
1355 struct ocfs2_extent_rec
*rec
;
1357 next_free
= le16_to_cpu(el
->l_next_free_rec
);
1358 has_empty
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
1362 /* The tree code before us didn't allow enough room in the leaf. */
1363 BUG_ON(el
->l_next_free_rec
== el
->l_count
&& !has_empty
);
1366 * The easiest way to approach this is to just remove the
1367 * empty extent and temporarily decrement next_free.
1371 * If next_free was 1 (only an empty extent), this
1372 * loop won't execute, which is fine. We still want
1373 * the decrement above to happen.
1375 for(i
= 0; i
< (next_free
- 1); i
++)
1376 el
->l_recs
[i
] = el
->l_recs
[i
+1];
1382 * Figure out what the new record index should be.
1384 for(i
= 0; i
< next_free
; i
++) {
1385 rec
= &el
->l_recs
[i
];
1387 if (insert_cpos
< le32_to_cpu(rec
->e_cpos
))
1392 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1393 insert_cpos
, insert_index
, has_empty
, next_free
, le16_to_cpu(el
->l_count
));
1395 BUG_ON(insert_index
< 0);
1396 BUG_ON(insert_index
>= le16_to_cpu(el
->l_count
));
1397 BUG_ON(insert_index
> next_free
);
1400 * No need to memmove if we're just adding to the tail.
1402 if (insert_index
!= next_free
) {
1403 BUG_ON(next_free
>= le16_to_cpu(el
->l_count
));
1405 num_bytes
= next_free
- insert_index
;
1406 num_bytes
*= sizeof(struct ocfs2_extent_rec
);
1407 memmove(&el
->l_recs
[insert_index
+ 1],
1408 &el
->l_recs
[insert_index
],
1413 * Either we had an empty extent, and need to re-increment or
1414 * there was no empty extent on a non full rightmost leaf node,
1415 * in which case we still need to increment.
1418 el
->l_next_free_rec
= cpu_to_le16(next_free
);
1420 * Make sure none of the math above just messed up our tree.
1422 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) > le16_to_cpu(el
->l_count
));
1424 el
->l_recs
[insert_index
] = *insert_rec
;
1428 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list
*el
)
1430 int size
, num_recs
= le16_to_cpu(el
->l_next_free_rec
);
1432 BUG_ON(num_recs
== 0);
1434 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
1436 size
= num_recs
* sizeof(struct ocfs2_extent_rec
);
1437 memmove(&el
->l_recs
[0], &el
->l_recs
[1], size
);
1438 memset(&el
->l_recs
[num_recs
], 0,
1439 sizeof(struct ocfs2_extent_rec
));
1440 el
->l_next_free_rec
= cpu_to_le16(num_recs
);
1445 * Create an empty extent record .
1447 * l_next_free_rec may be updated.
1449 * If an empty extent already exists do nothing.
1451 static void ocfs2_create_empty_extent(struct ocfs2_extent_list
*el
)
1453 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1455 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
1460 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
1463 mlog_bug_on_msg(el
->l_count
== el
->l_next_free_rec
,
1464 "Asked to create an empty extent in a full list:\n"
1465 "count = %u, tree depth = %u",
1466 le16_to_cpu(el
->l_count
),
1467 le16_to_cpu(el
->l_tree_depth
));
1469 ocfs2_shift_records_right(el
);
1472 le16_add_cpu(&el
->l_next_free_rec
, 1);
1473 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1477 * For a rotation which involves two leaf nodes, the "root node" is
1478 * the lowest level tree node which contains a path to both leafs. This
1479 * resulting set of information can be used to form a complete "subtree"
1481 * This function is passed two full paths from the dinode down to a
1482 * pair of adjacent leaves. It's task is to figure out which path
1483 * index contains the subtree root - this can be the root index itself
1484 * in a worst-case rotation.
1486 * The array index of the subtree root is passed back.
1488 static int ocfs2_find_subtree_root(struct inode
*inode
,
1489 struct ocfs2_path
*left
,
1490 struct ocfs2_path
*right
)
1495 * Check that the caller passed in two paths from the same tree.
1497 BUG_ON(path_root_bh(left
) != path_root_bh(right
));
1503 * The caller didn't pass two adjacent paths.
1505 mlog_bug_on_msg(i
> left
->p_tree_depth
,
1506 "Inode %lu, left depth %u, right depth %u\n"
1507 "left leaf blk %llu, right leaf blk %llu\n",
1508 inode
->i_ino
, left
->p_tree_depth
,
1509 right
->p_tree_depth
,
1510 (unsigned long long)path_leaf_bh(left
)->b_blocknr
,
1511 (unsigned long long)path_leaf_bh(right
)->b_blocknr
);
1512 } while (left
->p_node
[i
].bh
->b_blocknr
==
1513 right
->p_node
[i
].bh
->b_blocknr
);
1518 typedef void (path_insert_t
)(void *, struct buffer_head
*);
1521 * Traverse a btree path in search of cpos, starting at root_el.
1523 * This code can be called with a cpos larger than the tree, in which
1524 * case it will return the rightmost path.
1526 static int __ocfs2_find_path(struct inode
*inode
,
1527 struct ocfs2_extent_list
*root_el
, u32 cpos
,
1528 path_insert_t
*func
, void *data
)
1533 struct buffer_head
*bh
= NULL
;
1534 struct ocfs2_extent_block
*eb
;
1535 struct ocfs2_extent_list
*el
;
1536 struct ocfs2_extent_rec
*rec
;
1537 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1540 while (el
->l_tree_depth
) {
1541 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1542 ocfs2_error(inode
->i_sb
,
1543 "Inode %llu has empty extent list at "
1545 (unsigned long long)oi
->ip_blkno
,
1546 le16_to_cpu(el
->l_tree_depth
));
1552 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
) - 1; i
++) {
1553 rec
= &el
->l_recs
[i
];
1556 * In the case that cpos is off the allocation
1557 * tree, this should just wind up returning the
1560 range
= le32_to_cpu(rec
->e_cpos
) +
1561 ocfs2_rec_clusters(el
, rec
);
1562 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
1566 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1568 ocfs2_error(inode
->i_sb
,
1569 "Inode %llu has bad blkno in extent list "
1570 "at depth %u (index %d)\n",
1571 (unsigned long long)oi
->ip_blkno
,
1572 le16_to_cpu(el
->l_tree_depth
), i
);
1579 ret
= ocfs2_read_extent_block(inode
, blkno
, &bh
);
1585 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1588 if (le16_to_cpu(el
->l_next_free_rec
) >
1589 le16_to_cpu(el
->l_count
)) {
1590 ocfs2_error(inode
->i_sb
,
1591 "Inode %llu has bad count in extent list "
1592 "at block %llu (next free=%u, count=%u)\n",
1593 (unsigned long long)oi
->ip_blkno
,
1594 (unsigned long long)bh
->b_blocknr
,
1595 le16_to_cpu(el
->l_next_free_rec
),
1596 le16_to_cpu(el
->l_count
));
1607 * Catch any trailing bh that the loop didn't handle.
1615 * Given an initialized path (that is, it has a valid root extent
1616 * list), this function will traverse the btree in search of the path
1617 * which would contain cpos.
1619 * The path traveled is recorded in the path structure.
1621 * Note that this will not do any comparisons on leaf node extent
1622 * records, so it will work fine in the case that we just added a tree
1625 struct find_path_data
{
1627 struct ocfs2_path
*path
;
1629 static void find_path_ins(void *data
, struct buffer_head
*bh
)
1631 struct find_path_data
*fp
= data
;
1634 ocfs2_path_insert_eb(fp
->path
, fp
->index
, bh
);
1637 static int ocfs2_find_path(struct inode
*inode
, struct ocfs2_path
*path
,
1640 struct find_path_data data
;
1644 return __ocfs2_find_path(inode
, path_root_el(path
), cpos
,
1645 find_path_ins
, &data
);
1648 static void find_leaf_ins(void *data
, struct buffer_head
*bh
)
1650 struct ocfs2_extent_block
*eb
=(struct ocfs2_extent_block
*)bh
->b_data
;
1651 struct ocfs2_extent_list
*el
= &eb
->h_list
;
1652 struct buffer_head
**ret
= data
;
1654 /* We want to retain only the leaf block. */
1655 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
1661 * Find the leaf block in the tree which would contain cpos. No
1662 * checking of the actual leaf is done.
1664 * Some paths want to call this instead of allocating a path structure
1665 * and calling ocfs2_find_path().
1667 * This function doesn't handle non btree extent lists.
1669 int ocfs2_find_leaf(struct inode
*inode
, struct ocfs2_extent_list
*root_el
,
1670 u32 cpos
, struct buffer_head
**leaf_bh
)
1673 struct buffer_head
*bh
= NULL
;
1675 ret
= __ocfs2_find_path(inode
, root_el
, cpos
, find_leaf_ins
, &bh
);
1687 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1689 * Basically, we've moved stuff around at the bottom of the tree and
1690 * we need to fix up the extent records above the changes to reflect
1693 * left_rec: the record on the left.
1694 * left_child_el: is the child list pointed to by left_rec
1695 * right_rec: the record to the right of left_rec
1696 * right_child_el: is the child list pointed to by right_rec
1698 * By definition, this only works on interior nodes.
1700 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec
*left_rec
,
1701 struct ocfs2_extent_list
*left_child_el
,
1702 struct ocfs2_extent_rec
*right_rec
,
1703 struct ocfs2_extent_list
*right_child_el
)
1705 u32 left_clusters
, right_end
;
1708 * Interior nodes never have holes. Their cpos is the cpos of
1709 * the leftmost record in their child list. Their cluster
1710 * count covers the full theoretical range of their child list
1711 * - the range between their cpos and the cpos of the record
1712 * immediately to their right.
1714 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[0].e_cpos
);
1715 if (ocfs2_is_empty_extent(&right_child_el
->l_recs
[0])) {
1716 BUG_ON(le16_to_cpu(right_child_el
->l_next_free_rec
) <= 1);
1717 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[1].e_cpos
);
1719 left_clusters
-= le32_to_cpu(left_rec
->e_cpos
);
1720 left_rec
->e_int_clusters
= cpu_to_le32(left_clusters
);
1723 * Calculate the rightmost cluster count boundary before
1724 * moving cpos - we will need to adjust clusters after
1725 * updating e_cpos to keep the same highest cluster count.
1727 right_end
= le32_to_cpu(right_rec
->e_cpos
);
1728 right_end
+= le32_to_cpu(right_rec
->e_int_clusters
);
1730 right_rec
->e_cpos
= left_rec
->e_cpos
;
1731 le32_add_cpu(&right_rec
->e_cpos
, left_clusters
);
1733 right_end
-= le32_to_cpu(right_rec
->e_cpos
);
1734 right_rec
->e_int_clusters
= cpu_to_le32(right_end
);
1738 * Adjust the adjacent root node records involved in a
1739 * rotation. left_el_blkno is passed in as a key so that we can easily
1740 * find it's index in the root list.
1742 static void ocfs2_adjust_root_records(struct ocfs2_extent_list
*root_el
,
1743 struct ocfs2_extent_list
*left_el
,
1744 struct ocfs2_extent_list
*right_el
,
1749 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) <=
1750 le16_to_cpu(left_el
->l_tree_depth
));
1752 for(i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
) - 1; i
++) {
1753 if (le64_to_cpu(root_el
->l_recs
[i
].e_blkno
) == left_el_blkno
)
1758 * The path walking code should have never returned a root and
1759 * two paths which are not adjacent.
1761 BUG_ON(i
>= (le16_to_cpu(root_el
->l_next_free_rec
) - 1));
1763 ocfs2_adjust_adjacent_records(&root_el
->l_recs
[i
], left_el
,
1764 &root_el
->l_recs
[i
+ 1], right_el
);
1768 * We've changed a leaf block (in right_path) and need to reflect that
1769 * change back up the subtree.
1771 * This happens in multiple places:
1772 * - When we've moved an extent record from the left path leaf to the right
1773 * path leaf to make room for an empty extent in the left path leaf.
1774 * - When our insert into the right path leaf is at the leftmost edge
1775 * and requires an update of the path immediately to it's left. This
1776 * can occur at the end of some types of rotation and appending inserts.
1777 * - When we've adjusted the last extent record in the left path leaf and the
1778 * 1st extent record in the right path leaf during cross extent block merge.
1780 static void ocfs2_complete_edge_insert(struct inode
*inode
, handle_t
*handle
,
1781 struct ocfs2_path
*left_path
,
1782 struct ocfs2_path
*right_path
,
1786 struct ocfs2_extent_list
*el
, *left_el
, *right_el
;
1787 struct ocfs2_extent_rec
*left_rec
, *right_rec
;
1788 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
1791 * Update the counts and position values within all the
1792 * interior nodes to reflect the leaf rotation we just did.
1794 * The root node is handled below the loop.
1796 * We begin the loop with right_el and left_el pointing to the
1797 * leaf lists and work our way up.
1799 * NOTE: within this loop, left_el and right_el always refer
1800 * to the *child* lists.
1802 left_el
= path_leaf_el(left_path
);
1803 right_el
= path_leaf_el(right_path
);
1804 for(i
= left_path
->p_tree_depth
- 1; i
> subtree_index
; i
--) {
1805 mlog(0, "Adjust records at index %u\n", i
);
1808 * One nice property of knowing that all of these
1809 * nodes are below the root is that we only deal with
1810 * the leftmost right node record and the rightmost
1813 el
= left_path
->p_node
[i
].el
;
1814 idx
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
1815 left_rec
= &el
->l_recs
[idx
];
1817 el
= right_path
->p_node
[i
].el
;
1818 right_rec
= &el
->l_recs
[0];
1820 ocfs2_adjust_adjacent_records(left_rec
, left_el
, right_rec
,
1823 ret
= ocfs2_journal_dirty(handle
, left_path
->p_node
[i
].bh
);
1827 ret
= ocfs2_journal_dirty(handle
, right_path
->p_node
[i
].bh
);
1832 * Setup our list pointers now so that the current
1833 * parents become children in the next iteration.
1835 left_el
= left_path
->p_node
[i
].el
;
1836 right_el
= right_path
->p_node
[i
].el
;
1840 * At the root node, adjust the two adjacent records which
1841 * begin our path to the leaves.
1844 el
= left_path
->p_node
[subtree_index
].el
;
1845 left_el
= left_path
->p_node
[subtree_index
+ 1].el
;
1846 right_el
= right_path
->p_node
[subtree_index
+ 1].el
;
1848 ocfs2_adjust_root_records(el
, left_el
, right_el
,
1849 left_path
->p_node
[subtree_index
+ 1].bh
->b_blocknr
);
1851 root_bh
= left_path
->p_node
[subtree_index
].bh
;
1853 ret
= ocfs2_journal_dirty(handle
, root_bh
);
1858 static int ocfs2_rotate_subtree_right(struct inode
*inode
,
1860 struct ocfs2_path
*left_path
,
1861 struct ocfs2_path
*right_path
,
1865 struct buffer_head
*right_leaf_bh
;
1866 struct buffer_head
*left_leaf_bh
= NULL
;
1867 struct buffer_head
*root_bh
;
1868 struct ocfs2_extent_list
*right_el
, *left_el
;
1869 struct ocfs2_extent_rec move_rec
;
1871 left_leaf_bh
= path_leaf_bh(left_path
);
1872 left_el
= path_leaf_el(left_path
);
1874 if (left_el
->l_next_free_rec
!= left_el
->l_count
) {
1875 ocfs2_error(inode
->i_sb
,
1876 "Inode %llu has non-full interior leaf node %llu"
1878 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1879 (unsigned long long)left_leaf_bh
->b_blocknr
,
1880 le16_to_cpu(left_el
->l_next_free_rec
));
1885 * This extent block may already have an empty record, so we
1886 * return early if so.
1888 if (ocfs2_is_empty_extent(&left_el
->l_recs
[0]))
1891 root_bh
= left_path
->p_node
[subtree_index
].bh
;
1892 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
1894 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
1895 OCFS2_JOURNAL_ACCESS_WRITE
);
1901 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
1902 ret
= ocfs2_journal_access(handle
, inode
,
1903 right_path
->p_node
[i
].bh
,
1904 OCFS2_JOURNAL_ACCESS_WRITE
);
1910 ret
= ocfs2_journal_access(handle
, inode
,
1911 left_path
->p_node
[i
].bh
,
1912 OCFS2_JOURNAL_ACCESS_WRITE
);
1919 right_leaf_bh
= path_leaf_bh(right_path
);
1920 right_el
= path_leaf_el(right_path
);
1922 /* This is a code error, not a disk corruption. */
1923 mlog_bug_on_msg(!right_el
->l_next_free_rec
, "Inode %llu: Rotate fails "
1924 "because rightmost leaf block %llu is empty\n",
1925 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1926 (unsigned long long)right_leaf_bh
->b_blocknr
);
1928 ocfs2_create_empty_extent(right_el
);
1930 ret
= ocfs2_journal_dirty(handle
, right_leaf_bh
);
1936 /* Do the copy now. */
1937 i
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
1938 move_rec
= left_el
->l_recs
[i
];
1939 right_el
->l_recs
[0] = move_rec
;
1942 * Clear out the record we just copied and shift everything
1943 * over, leaving an empty extent in the left leaf.
1945 * We temporarily subtract from next_free_rec so that the
1946 * shift will lose the tail record (which is now defunct).
1948 le16_add_cpu(&left_el
->l_next_free_rec
, -1);
1949 ocfs2_shift_records_right(left_el
);
1950 memset(&left_el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1951 le16_add_cpu(&left_el
->l_next_free_rec
, 1);
1953 ret
= ocfs2_journal_dirty(handle
, left_leaf_bh
);
1959 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
1967 * Given a full path, determine what cpos value would return us a path
1968 * containing the leaf immediately to the left of the current one.
1970 * Will return zero if the path passed in is already the leftmost path.
1972 static int ocfs2_find_cpos_for_left_leaf(struct super_block
*sb
,
1973 struct ocfs2_path
*path
, u32
*cpos
)
1977 struct ocfs2_extent_list
*el
;
1979 BUG_ON(path
->p_tree_depth
== 0);
1983 blkno
= path_leaf_bh(path
)->b_blocknr
;
1985 /* Start at the tree node just above the leaf and work our way up. */
1986 i
= path
->p_tree_depth
- 1;
1988 el
= path
->p_node
[i
].el
;
1991 * Find the extent record just before the one in our
1994 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
1995 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
1999 * We've determined that the
2000 * path specified is already
2001 * the leftmost one - return a
2007 * The leftmost record points to our
2008 * leaf - we need to travel up the
2014 *cpos
= le32_to_cpu(el
->l_recs
[j
- 1].e_cpos
);
2015 *cpos
= *cpos
+ ocfs2_rec_clusters(el
,
2016 &el
->l_recs
[j
- 1]);
2023 * If we got here, we never found a valid node where
2024 * the tree indicated one should be.
2027 "Invalid extent tree at extent block %llu\n",
2028 (unsigned long long)blkno
);
2033 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2042 * Extend the transaction by enough credits to complete the rotation,
2043 * and still leave at least the original number of credits allocated
2044 * to this transaction.
2046 static int ocfs2_extend_rotate_transaction(handle_t
*handle
, int subtree_depth
,
2048 struct ocfs2_path
*path
)
2050 int credits
= (path
->p_tree_depth
- subtree_depth
) * 2 + 1 + op_credits
;
2052 if (handle
->h_buffer_credits
< credits
)
2053 return ocfs2_extend_trans(handle
, credits
);
2059 * Trap the case where we're inserting into the theoretical range past
2060 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2061 * whose cpos is less than ours into the right leaf.
2063 * It's only necessary to look at the rightmost record of the left
2064 * leaf because the logic that calls us should ensure that the
2065 * theoretical ranges in the path components above the leaves are
2068 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path
*left_path
,
2071 struct ocfs2_extent_list
*left_el
;
2072 struct ocfs2_extent_rec
*rec
;
2075 left_el
= path_leaf_el(left_path
);
2076 next_free
= le16_to_cpu(left_el
->l_next_free_rec
);
2077 rec
= &left_el
->l_recs
[next_free
- 1];
2079 if (insert_cpos
> le32_to_cpu(rec
->e_cpos
))
2084 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list
*el
, u32 cpos
)
2086 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
2088 struct ocfs2_extent_rec
*rec
;
2093 rec
= &el
->l_recs
[0];
2094 if (ocfs2_is_empty_extent(rec
)) {
2098 rec
= &el
->l_recs
[1];
2101 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2102 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
2108 * Rotate all the records in a btree right one record, starting at insert_cpos.
2110 * The path to the rightmost leaf should be passed in.
2112 * The array is assumed to be large enough to hold an entire path (tree depth).
2114 * Upon succesful return from this function:
2116 * - The 'right_path' array will contain a path to the leaf block
2117 * whose range contains e_cpos.
2118 * - That leaf block will have a single empty extent in list index 0.
2119 * - In the case that the rotation requires a post-insert update,
2120 * *ret_left_path will contain a valid path which can be passed to
2121 * ocfs2_insert_path().
2123 static int ocfs2_rotate_tree_right(struct inode
*inode
,
2125 enum ocfs2_split_type split
,
2127 struct ocfs2_path
*right_path
,
2128 struct ocfs2_path
**ret_left_path
)
2130 int ret
, start
, orig_credits
= handle
->h_buffer_credits
;
2132 struct ocfs2_path
*left_path
= NULL
;
2134 *ret_left_path
= NULL
;
2136 left_path
= ocfs2_new_path(path_root_bh(right_path
),
2137 path_root_el(right_path
));
2144 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
, &cpos
);
2150 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos
, cpos
);
2153 * What we want to do here is:
2155 * 1) Start with the rightmost path.
2157 * 2) Determine a path to the leaf block directly to the left
2160 * 3) Determine the 'subtree root' - the lowest level tree node
2161 * which contains a path to both leaves.
2163 * 4) Rotate the subtree.
2165 * 5) Find the next subtree by considering the left path to be
2166 * the new right path.
2168 * The check at the top of this while loop also accepts
2169 * insert_cpos == cpos because cpos is only a _theoretical_
2170 * value to get us the left path - insert_cpos might very well
2171 * be filling that hole.
2173 * Stop at a cpos of '0' because we either started at the
2174 * leftmost branch (i.e., a tree with one branch and a
2175 * rotation inside of it), or we've gone as far as we can in
2176 * rotating subtrees.
2178 while (cpos
&& insert_cpos
<= cpos
) {
2179 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2182 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2188 mlog_bug_on_msg(path_leaf_bh(left_path
) ==
2189 path_leaf_bh(right_path
),
2190 "Inode %lu: error during insert of %u "
2191 "(left path cpos %u) results in two identical "
2192 "paths ending at %llu\n",
2193 inode
->i_ino
, insert_cpos
, cpos
,
2194 (unsigned long long)
2195 path_leaf_bh(left_path
)->b_blocknr
);
2197 if (split
== SPLIT_NONE
&&
2198 ocfs2_rotate_requires_path_adjustment(left_path
,
2202 * We've rotated the tree as much as we
2203 * should. The rest is up to
2204 * ocfs2_insert_path() to complete, after the
2205 * record insertion. We indicate this
2206 * situation by returning the left path.
2208 * The reason we don't adjust the records here
2209 * before the record insert is that an error
2210 * later might break the rule where a parent
2211 * record e_cpos will reflect the actual
2212 * e_cpos of the 1st nonempty record of the
2215 *ret_left_path
= left_path
;
2219 start
= ocfs2_find_subtree_root(inode
, left_path
, right_path
);
2221 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2223 (unsigned long long) right_path
->p_node
[start
].bh
->b_blocknr
,
2224 right_path
->p_tree_depth
);
2226 ret
= ocfs2_extend_rotate_transaction(handle
, start
,
2227 orig_credits
, right_path
);
2233 ret
= ocfs2_rotate_subtree_right(inode
, handle
, left_path
,
2240 if (split
!= SPLIT_NONE
&&
2241 ocfs2_leftmost_rec_contains(path_leaf_el(right_path
),
2244 * A rotate moves the rightmost left leaf
2245 * record over to the leftmost right leaf
2246 * slot. If we're doing an extent split
2247 * instead of a real insert, then we have to
2248 * check that the extent to be split wasn't
2249 * just moved over. If it was, then we can
2250 * exit here, passing left_path back -
2251 * ocfs2_split_extent() is smart enough to
2252 * search both leaves.
2254 *ret_left_path
= left_path
;
2259 * There is no need to re-read the next right path
2260 * as we know that it'll be our current left
2261 * path. Optimize by copying values instead.
2263 ocfs2_mv_path(right_path
, left_path
);
2265 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
2274 ocfs2_free_path(left_path
);
2280 static void ocfs2_update_edge_lengths(struct inode
*inode
, handle_t
*handle
,
2281 struct ocfs2_path
*path
)
2284 struct ocfs2_extent_rec
*rec
;
2285 struct ocfs2_extent_list
*el
;
2286 struct ocfs2_extent_block
*eb
;
2289 /* Path should always be rightmost. */
2290 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2291 BUG_ON(eb
->h_next_leaf_blk
!= 0ULL);
2294 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
2295 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2296 rec
= &el
->l_recs
[idx
];
2297 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2299 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
2300 el
= path
->p_node
[i
].el
;
2301 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2302 rec
= &el
->l_recs
[idx
];
2304 rec
->e_int_clusters
= cpu_to_le32(range
);
2305 le32_add_cpu(&rec
->e_int_clusters
, -le32_to_cpu(rec
->e_cpos
));
2307 ocfs2_journal_dirty(handle
, path
->p_node
[i
].bh
);
2311 static void ocfs2_unlink_path(struct inode
*inode
, handle_t
*handle
,
2312 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2313 struct ocfs2_path
*path
, int unlink_start
)
2316 struct ocfs2_extent_block
*eb
;
2317 struct ocfs2_extent_list
*el
;
2318 struct buffer_head
*bh
;
2320 for(i
= unlink_start
; i
< path_num_items(path
); i
++) {
2321 bh
= path
->p_node
[i
].bh
;
2323 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
2325 * Not all nodes might have had their final count
2326 * decremented by the caller - handle this here.
2329 if (le16_to_cpu(el
->l_next_free_rec
) > 1) {
2331 "Inode %llu, attempted to remove extent block "
2332 "%llu with %u records\n",
2333 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2334 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
2335 le16_to_cpu(el
->l_next_free_rec
));
2337 ocfs2_journal_dirty(handle
, bh
);
2338 ocfs2_remove_from_cache(inode
, bh
);
2342 el
->l_next_free_rec
= 0;
2343 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2345 ocfs2_journal_dirty(handle
, bh
);
2347 ret
= ocfs2_cache_extent_block_free(dealloc
, eb
);
2351 ocfs2_remove_from_cache(inode
, bh
);
2355 static void ocfs2_unlink_subtree(struct inode
*inode
, handle_t
*handle
,
2356 struct ocfs2_path
*left_path
,
2357 struct ocfs2_path
*right_path
,
2359 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2362 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
2363 struct ocfs2_extent_list
*root_el
= left_path
->p_node
[subtree_index
].el
;
2364 struct ocfs2_extent_list
*el
;
2365 struct ocfs2_extent_block
*eb
;
2367 el
= path_leaf_el(left_path
);
2369 eb
= (struct ocfs2_extent_block
*)right_path
->p_node
[subtree_index
+ 1].bh
->b_data
;
2371 for(i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
2372 if (root_el
->l_recs
[i
].e_blkno
== eb
->h_blkno
)
2375 BUG_ON(i
>= le16_to_cpu(root_el
->l_next_free_rec
));
2377 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
2378 le16_add_cpu(&root_el
->l_next_free_rec
, -1);
2380 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2381 eb
->h_next_leaf_blk
= 0;
2383 ocfs2_journal_dirty(handle
, root_bh
);
2384 ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2386 ocfs2_unlink_path(inode
, handle
, dealloc
, right_path
,
2390 static int ocfs2_rotate_subtree_left(struct inode
*inode
, handle_t
*handle
,
2391 struct ocfs2_path
*left_path
,
2392 struct ocfs2_path
*right_path
,
2394 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2396 struct ocfs2_extent_tree
*et
)
2398 int ret
, i
, del_right_subtree
= 0, right_has_empty
= 0;
2399 struct buffer_head
*root_bh
, *et_root_bh
= path_root_bh(right_path
);
2400 struct ocfs2_extent_list
*right_leaf_el
, *left_leaf_el
;
2401 struct ocfs2_extent_block
*eb
;
2405 right_leaf_el
= path_leaf_el(right_path
);
2406 left_leaf_el
= path_leaf_el(left_path
);
2407 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2408 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2410 if (!ocfs2_is_empty_extent(&left_leaf_el
->l_recs
[0]))
2413 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(right_path
)->b_data
;
2414 if (ocfs2_is_empty_extent(&right_leaf_el
->l_recs
[0])) {
2416 * It's legal for us to proceed if the right leaf is
2417 * the rightmost one and it has an empty extent. There
2418 * are two cases to handle - whether the leaf will be
2419 * empty after removal or not. If the leaf isn't empty
2420 * then just remove the empty extent up front. The
2421 * next block will handle empty leaves by flagging
2424 * Non rightmost leaves will throw -EAGAIN and the
2425 * caller can manually move the subtree and retry.
2428 if (eb
->h_next_leaf_blk
!= 0ULL)
2431 if (le16_to_cpu(right_leaf_el
->l_next_free_rec
) > 1) {
2432 ret
= ocfs2_journal_access(handle
, inode
,
2433 path_leaf_bh(right_path
),
2434 OCFS2_JOURNAL_ACCESS_WRITE
);
2440 ocfs2_remove_empty_extent(right_leaf_el
);
2442 right_has_empty
= 1;
2445 if (eb
->h_next_leaf_blk
== 0ULL &&
2446 le16_to_cpu(right_leaf_el
->l_next_free_rec
) == 1) {
2448 * We have to update i_last_eb_blk during the meta
2451 ret
= ocfs2_journal_access(handle
, inode
, et_root_bh
,
2452 OCFS2_JOURNAL_ACCESS_WRITE
);
2458 del_right_subtree
= 1;
2462 * Getting here with an empty extent in the right path implies
2463 * that it's the rightmost path and will be deleted.
2465 BUG_ON(right_has_empty
&& !del_right_subtree
);
2467 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
2468 OCFS2_JOURNAL_ACCESS_WRITE
);
2474 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2475 ret
= ocfs2_journal_access(handle
, inode
,
2476 right_path
->p_node
[i
].bh
,
2477 OCFS2_JOURNAL_ACCESS_WRITE
);
2483 ret
= ocfs2_journal_access(handle
, inode
,
2484 left_path
->p_node
[i
].bh
,
2485 OCFS2_JOURNAL_ACCESS_WRITE
);
2492 if (!right_has_empty
) {
2494 * Only do this if we're moving a real
2495 * record. Otherwise, the action is delayed until
2496 * after removal of the right path in which case we
2497 * can do a simple shift to remove the empty extent.
2499 ocfs2_rotate_leaf(left_leaf_el
, &right_leaf_el
->l_recs
[0]);
2500 memset(&right_leaf_el
->l_recs
[0], 0,
2501 sizeof(struct ocfs2_extent_rec
));
2503 if (eb
->h_next_leaf_blk
== 0ULL) {
2505 * Move recs over to get rid of empty extent, decrease
2506 * next_free. This is allowed to remove the last
2507 * extent in our leaf (setting l_next_free_rec to
2508 * zero) - the delete code below won't care.
2510 ocfs2_remove_empty_extent(right_leaf_el
);
2513 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2516 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
2520 if (del_right_subtree
) {
2521 ocfs2_unlink_subtree(inode
, handle
, left_path
, right_path
,
2522 subtree_index
, dealloc
);
2523 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2525 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2526 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2529 * Removal of the extent in the left leaf was skipped
2530 * above so we could delete the right path
2533 if (right_has_empty
)
2534 ocfs2_remove_empty_extent(left_leaf_el
);
2536 ret
= ocfs2_journal_dirty(handle
, et_root_bh
);
2542 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
2550 * Given a full path, determine what cpos value would return us a path
2551 * containing the leaf immediately to the right of the current one.
2553 * Will return zero if the path passed in is already the rightmost path.
2555 * This looks similar, but is subtly different to
2556 * ocfs2_find_cpos_for_left_leaf().
2558 static int ocfs2_find_cpos_for_right_leaf(struct super_block
*sb
,
2559 struct ocfs2_path
*path
, u32
*cpos
)
2563 struct ocfs2_extent_list
*el
;
2567 if (path
->p_tree_depth
== 0)
2570 blkno
= path_leaf_bh(path
)->b_blocknr
;
2572 /* Start at the tree node just above the leaf and work our way up. */
2573 i
= path
->p_tree_depth
- 1;
2577 el
= path
->p_node
[i
].el
;
2580 * Find the extent record just after the one in our
2583 next_free
= le16_to_cpu(el
->l_next_free_rec
);
2584 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2585 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2586 if (j
== (next_free
- 1)) {
2589 * We've determined that the
2590 * path specified is already
2591 * the rightmost one - return a
2597 * The rightmost record points to our
2598 * leaf - we need to travel up the
2604 *cpos
= le32_to_cpu(el
->l_recs
[j
+ 1].e_cpos
);
2610 * If we got here, we never found a valid node where
2611 * the tree indicated one should be.
2614 "Invalid extent tree at extent block %llu\n",
2615 (unsigned long long)blkno
);
2620 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2628 static int ocfs2_rotate_rightmost_leaf_left(struct inode
*inode
,
2630 struct buffer_head
*bh
,
2631 struct ocfs2_extent_list
*el
)
2635 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2638 ret
= ocfs2_journal_access(handle
, inode
, bh
,
2639 OCFS2_JOURNAL_ACCESS_WRITE
);
2645 ocfs2_remove_empty_extent(el
);
2647 ret
= ocfs2_journal_dirty(handle
, bh
);
2655 static int __ocfs2_rotate_tree_left(struct inode
*inode
,
2656 handle_t
*handle
, int orig_credits
,
2657 struct ocfs2_path
*path
,
2658 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2659 struct ocfs2_path
**empty_extent_path
,
2660 struct ocfs2_extent_tree
*et
)
2662 int ret
, subtree_root
, deleted
;
2664 struct ocfs2_path
*left_path
= NULL
;
2665 struct ocfs2_path
*right_path
= NULL
;
2667 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path
)->l_recs
[0])));
2669 *empty_extent_path
= NULL
;
2671 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, path
,
2678 left_path
= ocfs2_new_path(path_root_bh(path
),
2679 path_root_el(path
));
2686 ocfs2_cp_path(left_path
, path
);
2688 right_path
= ocfs2_new_path(path_root_bh(path
),
2689 path_root_el(path
));
2696 while (right_cpos
) {
2697 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
2703 subtree_root
= ocfs2_find_subtree_root(inode
, left_path
,
2706 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2708 (unsigned long long)
2709 right_path
->p_node
[subtree_root
].bh
->b_blocknr
,
2710 right_path
->p_tree_depth
);
2712 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_root
,
2713 orig_credits
, left_path
);
2720 * Caller might still want to make changes to the
2721 * tree root, so re-add it to the journal here.
2723 ret
= ocfs2_journal_access(handle
, inode
,
2724 path_root_bh(left_path
),
2725 OCFS2_JOURNAL_ACCESS_WRITE
);
2731 ret
= ocfs2_rotate_subtree_left(inode
, handle
, left_path
,
2732 right_path
, subtree_root
,
2733 dealloc
, &deleted
, et
);
2734 if (ret
== -EAGAIN
) {
2736 * The rotation has to temporarily stop due to
2737 * the right subtree having an empty
2738 * extent. Pass it back to the caller for a
2741 *empty_extent_path
= right_path
;
2751 * The subtree rotate might have removed records on
2752 * the rightmost edge. If so, then rotation is
2758 ocfs2_mv_path(left_path
, right_path
);
2760 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
2769 ocfs2_free_path(right_path
);
2770 ocfs2_free_path(left_path
);
2775 static int ocfs2_remove_rightmost_path(struct inode
*inode
, handle_t
*handle
,
2776 struct ocfs2_path
*path
,
2777 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2778 struct ocfs2_extent_tree
*et
)
2780 int ret
, subtree_index
;
2782 struct ocfs2_path
*left_path
= NULL
;
2783 struct ocfs2_extent_block
*eb
;
2784 struct ocfs2_extent_list
*el
;
2787 ret
= ocfs2_et_sanity_check(inode
, et
);
2791 * There's two ways we handle this depending on
2792 * whether path is the only existing one.
2794 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
2795 handle
->h_buffer_credits
,
2802 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
2808 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
2816 * We have a path to the left of this one - it needs
2819 left_path
= ocfs2_new_path(path_root_bh(path
),
2820 path_root_el(path
));
2827 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2833 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
2839 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
2841 ocfs2_unlink_subtree(inode
, handle
, left_path
, path
,
2842 subtree_index
, dealloc
);
2843 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2845 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2846 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2849 * 'path' is also the leftmost path which
2850 * means it must be the only one. This gets
2851 * handled differently because we want to
2852 * revert the inode back to having extents
2855 ocfs2_unlink_path(inode
, handle
, dealloc
, path
, 1);
2857 el
= et
->et_root_el
;
2858 el
->l_tree_depth
= 0;
2859 el
->l_next_free_rec
= 0;
2860 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2862 ocfs2_et_set_last_eb_blk(et
, 0);
2865 ocfs2_journal_dirty(handle
, path_root_bh(path
));
2868 ocfs2_free_path(left_path
);
2873 * Left rotation of btree records.
2875 * In many ways, this is (unsurprisingly) the opposite of right
2876 * rotation. We start at some non-rightmost path containing an empty
2877 * extent in the leaf block. The code works its way to the rightmost
2878 * path by rotating records to the left in every subtree.
2880 * This is used by any code which reduces the number of extent records
2881 * in a leaf. After removal, an empty record should be placed in the
2882 * leftmost list position.
2884 * This won't handle a length update of the rightmost path records if
2885 * the rightmost tree leaf record is removed so the caller is
2886 * responsible for detecting and correcting that.
2888 static int ocfs2_rotate_tree_left(struct inode
*inode
, handle_t
*handle
,
2889 struct ocfs2_path
*path
,
2890 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2891 struct ocfs2_extent_tree
*et
)
2893 int ret
, orig_credits
= handle
->h_buffer_credits
;
2894 struct ocfs2_path
*tmp_path
= NULL
, *restart_path
= NULL
;
2895 struct ocfs2_extent_block
*eb
;
2896 struct ocfs2_extent_list
*el
;
2898 el
= path_leaf_el(path
);
2899 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2902 if (path
->p_tree_depth
== 0) {
2903 rightmost_no_delete
:
2905 * Inline extents. This is trivially handled, so do
2908 ret
= ocfs2_rotate_rightmost_leaf_left(inode
, handle
,
2910 path_leaf_el(path
));
2917 * Handle rightmost branch now. There's several cases:
2918 * 1) simple rotation leaving records in there. That's trivial.
2919 * 2) rotation requiring a branch delete - there's no more
2920 * records left. Two cases of this:
2921 * a) There are branches to the left.
2922 * b) This is also the leftmost (the only) branch.
2924 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2925 * 2a) we need the left branch so that we can update it with the unlink
2926 * 2b) we need to bring the inode back to inline extents.
2929 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2931 if (eb
->h_next_leaf_blk
== 0) {
2933 * This gets a bit tricky if we're going to delete the
2934 * rightmost path. Get the other cases out of the way
2937 if (le16_to_cpu(el
->l_next_free_rec
) > 1)
2938 goto rightmost_no_delete
;
2940 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
2942 ocfs2_error(inode
->i_sb
,
2943 "Inode %llu has empty extent block at %llu",
2944 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2945 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
2950 * XXX: The caller can not trust "path" any more after
2951 * this as it will have been deleted. What do we do?
2953 * In theory the rotate-for-merge code will never get
2954 * here because it'll always ask for a rotate in a
2958 ret
= ocfs2_remove_rightmost_path(inode
, handle
, path
,
2966 * Now we can loop, remembering the path we get from -EAGAIN
2967 * and restarting from there.
2970 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
, path
,
2971 dealloc
, &restart_path
, et
);
2972 if (ret
&& ret
!= -EAGAIN
) {
2977 while (ret
== -EAGAIN
) {
2978 tmp_path
= restart_path
;
2979 restart_path
= NULL
;
2981 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
,
2984 if (ret
&& ret
!= -EAGAIN
) {
2989 ocfs2_free_path(tmp_path
);
2997 ocfs2_free_path(tmp_path
);
2998 ocfs2_free_path(restart_path
);
3002 static void ocfs2_cleanup_merge(struct ocfs2_extent_list
*el
,
3005 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[index
];
3008 if (rec
->e_leaf_clusters
== 0) {
3010 * We consumed all of the merged-from record. An empty
3011 * extent cannot exist anywhere but the 1st array
3012 * position, so move things over if the merged-from
3013 * record doesn't occupy that position.
3015 * This creates a new empty extent so the caller
3016 * should be smart enough to have removed any existing
3020 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3021 size
= index
* sizeof(struct ocfs2_extent_rec
);
3022 memmove(&el
->l_recs
[1], &el
->l_recs
[0], size
);
3026 * Always memset - the caller doesn't check whether it
3027 * created an empty extent, so there could be junk in
3030 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
3034 static int ocfs2_get_right_path(struct inode
*inode
,
3035 struct ocfs2_path
*left_path
,
3036 struct ocfs2_path
**ret_right_path
)
3040 struct ocfs2_path
*right_path
= NULL
;
3041 struct ocfs2_extent_list
*left_el
;
3043 *ret_right_path
= NULL
;
3045 /* This function shouldn't be called for non-trees. */
3046 BUG_ON(left_path
->p_tree_depth
== 0);
3048 left_el
= path_leaf_el(left_path
);
3049 BUG_ON(left_el
->l_next_free_rec
!= left_el
->l_count
);
3051 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
3058 /* This function shouldn't be called for the rightmost leaf. */
3059 BUG_ON(right_cpos
== 0);
3061 right_path
= ocfs2_new_path(path_root_bh(left_path
),
3062 path_root_el(left_path
));
3069 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
3075 *ret_right_path
= right_path
;
3078 ocfs2_free_path(right_path
);
3083 * Remove split_rec clusters from the record at index and merge them
3084 * onto the beginning of the record "next" to it.
3085 * For index < l_count - 1, the next means the extent rec at index + 1.
3086 * For index == l_count - 1, the "next" means the 1st extent rec of the
3087 * next extent block.
3089 static int ocfs2_merge_rec_right(struct inode
*inode
,
3090 struct ocfs2_path
*left_path
,
3092 struct ocfs2_extent_rec
*split_rec
,
3095 int ret
, next_free
, i
;
3096 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3097 struct ocfs2_extent_rec
*left_rec
;
3098 struct ocfs2_extent_rec
*right_rec
;
3099 struct ocfs2_extent_list
*right_el
;
3100 struct ocfs2_path
*right_path
= NULL
;
3101 int subtree_index
= 0;
3102 struct ocfs2_extent_list
*el
= path_leaf_el(left_path
);
3103 struct buffer_head
*bh
= path_leaf_bh(left_path
);
3104 struct buffer_head
*root_bh
= NULL
;
3106 BUG_ON(index
>= le16_to_cpu(el
->l_next_free_rec
));
3107 left_rec
= &el
->l_recs
[index
];
3109 if (index
== le16_to_cpu(el
->l_next_free_rec
) - 1 &&
3110 le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
)) {
3111 /* we meet with a cross extent block merge. */
3112 ret
= ocfs2_get_right_path(inode
, left_path
, &right_path
);
3118 right_el
= path_leaf_el(right_path
);
3119 next_free
= le16_to_cpu(right_el
->l_next_free_rec
);
3120 BUG_ON(next_free
<= 0);
3121 right_rec
= &right_el
->l_recs
[0];
3122 if (ocfs2_is_empty_extent(right_rec
)) {
3123 BUG_ON(next_free
<= 1);
3124 right_rec
= &right_el
->l_recs
[1];
3127 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3128 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3129 le32_to_cpu(right_rec
->e_cpos
));
3131 subtree_index
= ocfs2_find_subtree_root(inode
,
3132 left_path
, right_path
);
3134 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3135 handle
->h_buffer_credits
,
3142 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3143 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3145 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
3146 OCFS2_JOURNAL_ACCESS_WRITE
);
3152 for (i
= subtree_index
+ 1;
3153 i
< path_num_items(right_path
); i
++) {
3154 ret
= ocfs2_journal_access(handle
, inode
,
3155 right_path
->p_node
[i
].bh
,
3156 OCFS2_JOURNAL_ACCESS_WRITE
);
3162 ret
= ocfs2_journal_access(handle
, inode
,
3163 left_path
->p_node
[i
].bh
,
3164 OCFS2_JOURNAL_ACCESS_WRITE
);
3172 BUG_ON(index
== le16_to_cpu(el
->l_next_free_rec
) - 1);
3173 right_rec
= &el
->l_recs
[index
+ 1];
3176 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3177 OCFS2_JOURNAL_ACCESS_WRITE
);
3183 le16_add_cpu(&left_rec
->e_leaf_clusters
, -split_clusters
);
3185 le32_add_cpu(&right_rec
->e_cpos
, -split_clusters
);
3186 le64_add_cpu(&right_rec
->e_blkno
,
3187 -ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3188 le16_add_cpu(&right_rec
->e_leaf_clusters
, split_clusters
);
3190 ocfs2_cleanup_merge(el
, index
);
3192 ret
= ocfs2_journal_dirty(handle
, bh
);
3197 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
3201 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3202 right_path
, subtree_index
);
3206 ocfs2_free_path(right_path
);
3210 static int ocfs2_get_left_path(struct inode
*inode
,
3211 struct ocfs2_path
*right_path
,
3212 struct ocfs2_path
**ret_left_path
)
3216 struct ocfs2_path
*left_path
= NULL
;
3218 *ret_left_path
= NULL
;
3220 /* This function shouldn't be called for non-trees. */
3221 BUG_ON(right_path
->p_tree_depth
== 0);
3223 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
3224 right_path
, &left_cpos
);
3230 /* This function shouldn't be called for the leftmost leaf. */
3231 BUG_ON(left_cpos
== 0);
3233 left_path
= ocfs2_new_path(path_root_bh(right_path
),
3234 path_root_el(right_path
));
3241 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3247 *ret_left_path
= left_path
;
3250 ocfs2_free_path(left_path
);
3255 * Remove split_rec clusters from the record at index and merge them
3256 * onto the tail of the record "before" it.
3257 * For index > 0, the "before" means the extent rec at index - 1.
3259 * For index == 0, the "before" means the last record of the previous
3260 * extent block. And there is also a situation that we may need to
3261 * remove the rightmost leaf extent block in the right_path and change
3262 * the right path to indicate the new rightmost path.
3264 static int ocfs2_merge_rec_left(struct inode
*inode
,
3265 struct ocfs2_path
*right_path
,
3267 struct ocfs2_extent_rec
*split_rec
,
3268 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3269 struct ocfs2_extent_tree
*et
,
3272 int ret
, i
, subtree_index
= 0, has_empty_extent
= 0;
3273 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3274 struct ocfs2_extent_rec
*left_rec
;
3275 struct ocfs2_extent_rec
*right_rec
;
3276 struct ocfs2_extent_list
*el
= path_leaf_el(right_path
);
3277 struct buffer_head
*bh
= path_leaf_bh(right_path
);
3278 struct buffer_head
*root_bh
= NULL
;
3279 struct ocfs2_path
*left_path
= NULL
;
3280 struct ocfs2_extent_list
*left_el
;
3284 right_rec
= &el
->l_recs
[index
];
3286 /* we meet with a cross extent block merge. */
3287 ret
= ocfs2_get_left_path(inode
, right_path
, &left_path
);
3293 left_el
= path_leaf_el(left_path
);
3294 BUG_ON(le16_to_cpu(left_el
->l_next_free_rec
) !=
3295 le16_to_cpu(left_el
->l_count
));
3297 left_rec
= &left_el
->l_recs
[
3298 le16_to_cpu(left_el
->l_next_free_rec
) - 1];
3299 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3300 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3301 le32_to_cpu(split_rec
->e_cpos
));
3303 subtree_index
= ocfs2_find_subtree_root(inode
,
3304 left_path
, right_path
);
3306 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3307 handle
->h_buffer_credits
,
3314 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3315 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3317 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
3318 OCFS2_JOURNAL_ACCESS_WRITE
);
3324 for (i
= subtree_index
+ 1;
3325 i
< path_num_items(right_path
); i
++) {
3326 ret
= ocfs2_journal_access(handle
, inode
,
3327 right_path
->p_node
[i
].bh
,
3328 OCFS2_JOURNAL_ACCESS_WRITE
);
3334 ret
= ocfs2_journal_access(handle
, inode
,
3335 left_path
->p_node
[i
].bh
,
3336 OCFS2_JOURNAL_ACCESS_WRITE
);
3343 left_rec
= &el
->l_recs
[index
- 1];
3344 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
3345 has_empty_extent
= 1;
3348 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3349 OCFS2_JOURNAL_ACCESS_WRITE
);
3355 if (has_empty_extent
&& index
== 1) {
3357 * The easy case - we can just plop the record right in.
3359 *left_rec
= *split_rec
;
3361 has_empty_extent
= 0;
3363 le16_add_cpu(&left_rec
->e_leaf_clusters
, split_clusters
);
3365 le32_add_cpu(&right_rec
->e_cpos
, split_clusters
);
3366 le64_add_cpu(&right_rec
->e_blkno
,
3367 ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3368 le16_add_cpu(&right_rec
->e_leaf_clusters
, -split_clusters
);
3370 ocfs2_cleanup_merge(el
, index
);
3372 ret
= ocfs2_journal_dirty(handle
, bh
);
3377 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
3382 * In the situation that the right_rec is empty and the extent
3383 * block is empty also, ocfs2_complete_edge_insert can't handle
3384 * it and we need to delete the right extent block.
3386 if (le16_to_cpu(right_rec
->e_leaf_clusters
) == 0 &&
3387 le16_to_cpu(el
->l_next_free_rec
) == 1) {
3389 ret
= ocfs2_remove_rightmost_path(inode
, handle
,
3397 /* Now the rightmost extent block has been deleted.
3398 * So we use the new rightmost path.
3400 ocfs2_mv_path(right_path
, left_path
);
3403 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3404 right_path
, subtree_index
);
3408 ocfs2_free_path(left_path
);
3412 static int ocfs2_try_to_merge_extent(struct inode
*inode
,
3414 struct ocfs2_path
*path
,
3416 struct ocfs2_extent_rec
*split_rec
,
3417 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3418 struct ocfs2_merge_ctxt
*ctxt
,
3419 struct ocfs2_extent_tree
*et
)
3423 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
3424 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
3426 BUG_ON(ctxt
->c_contig_type
== CONTIG_NONE
);
3428 if (ctxt
->c_split_covers_rec
&& ctxt
->c_has_empty_extent
) {
3430 * The merge code will need to create an empty
3431 * extent to take the place of the newly
3432 * emptied slot. Remove any pre-existing empty
3433 * extents - having more than one in a leaf is
3436 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3443 rec
= &el
->l_recs
[split_index
];
3446 if (ctxt
->c_contig_type
== CONTIG_LEFTRIGHT
) {
3448 * Left-right contig implies this.
3450 BUG_ON(!ctxt
->c_split_covers_rec
);
3453 * Since the leftright insert always covers the entire
3454 * extent, this call will delete the insert record
3455 * entirely, resulting in an empty extent record added to
3458 * Since the adding of an empty extent shifts
3459 * everything back to the right, there's no need to
3460 * update split_index here.
3462 * When the split_index is zero, we need to merge it to the
3463 * prevoius extent block. It is more efficient and easier
3464 * if we do merge_right first and merge_left later.
3466 ret
= ocfs2_merge_rec_right(inode
, path
,
3475 * We can only get this from logic error above.
3477 BUG_ON(!ocfs2_is_empty_extent(&el
->l_recs
[0]));
3479 /* The merge left us with an empty extent, remove it. */
3480 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3487 rec
= &el
->l_recs
[split_index
];
3490 * Note that we don't pass split_rec here on purpose -
3491 * we've merged it into the rec already.
3493 ret
= ocfs2_merge_rec_left(inode
, path
,
3503 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3506 * Error from this last rotate is not critical, so
3507 * print but don't bubble it up.
3514 * Merge a record to the left or right.
3516 * 'contig_type' is relative to the existing record,
3517 * so for example, if we're "right contig", it's to
3518 * the record on the left (hence the left merge).
3520 if (ctxt
->c_contig_type
== CONTIG_RIGHT
) {
3521 ret
= ocfs2_merge_rec_left(inode
,
3531 ret
= ocfs2_merge_rec_right(inode
,
3541 if (ctxt
->c_split_covers_rec
) {
3543 * The merge may have left an empty extent in
3544 * our leaf. Try to rotate it away.
3546 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3558 static void ocfs2_subtract_from_rec(struct super_block
*sb
,
3559 enum ocfs2_split_type split
,
3560 struct ocfs2_extent_rec
*rec
,
3561 struct ocfs2_extent_rec
*split_rec
)
3565 len_blocks
= ocfs2_clusters_to_blocks(sb
,
3566 le16_to_cpu(split_rec
->e_leaf_clusters
));
3568 if (split
== SPLIT_LEFT
) {
3570 * Region is on the left edge of the existing
3573 le32_add_cpu(&rec
->e_cpos
,
3574 le16_to_cpu(split_rec
->e_leaf_clusters
));
3575 le64_add_cpu(&rec
->e_blkno
, len_blocks
);
3576 le16_add_cpu(&rec
->e_leaf_clusters
,
3577 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3580 * Region is on the right edge of the existing
3583 le16_add_cpu(&rec
->e_leaf_clusters
,
3584 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3589 * Do the final bits of extent record insertion at the target leaf
3590 * list. If this leaf is part of an allocation tree, it is assumed
3591 * that the tree above has been prepared.
3593 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec
*insert_rec
,
3594 struct ocfs2_extent_list
*el
,
3595 struct ocfs2_insert_type
*insert
,
3596 struct inode
*inode
)
3598 int i
= insert
->ins_contig_index
;
3600 struct ocfs2_extent_rec
*rec
;
3602 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
3604 if (insert
->ins_split
!= SPLIT_NONE
) {
3605 i
= ocfs2_search_extent_list(el
, le32_to_cpu(insert_rec
->e_cpos
));
3607 rec
= &el
->l_recs
[i
];
3608 ocfs2_subtract_from_rec(inode
->i_sb
, insert
->ins_split
, rec
,
3614 * Contiguous insert - either left or right.
3616 if (insert
->ins_contig
!= CONTIG_NONE
) {
3617 rec
= &el
->l_recs
[i
];
3618 if (insert
->ins_contig
== CONTIG_LEFT
) {
3619 rec
->e_blkno
= insert_rec
->e_blkno
;
3620 rec
->e_cpos
= insert_rec
->e_cpos
;
3622 le16_add_cpu(&rec
->e_leaf_clusters
,
3623 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3628 * Handle insert into an empty leaf.
3630 if (le16_to_cpu(el
->l_next_free_rec
) == 0 ||
3631 ((le16_to_cpu(el
->l_next_free_rec
) == 1) &&
3632 ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3633 el
->l_recs
[0] = *insert_rec
;
3634 el
->l_next_free_rec
= cpu_to_le16(1);
3641 if (insert
->ins_appending
== APPEND_TAIL
) {
3642 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
3643 rec
= &el
->l_recs
[i
];
3644 range
= le32_to_cpu(rec
->e_cpos
)
3645 + le16_to_cpu(rec
->e_leaf_clusters
);
3646 BUG_ON(le32_to_cpu(insert_rec
->e_cpos
) < range
);
3648 mlog_bug_on_msg(le16_to_cpu(el
->l_next_free_rec
) >=
3649 le16_to_cpu(el
->l_count
),
3650 "inode %lu, depth %u, count %u, next free %u, "
3651 "rec.cpos %u, rec.clusters %u, "
3652 "insert.cpos %u, insert.clusters %u\n",
3654 le16_to_cpu(el
->l_tree_depth
),
3655 le16_to_cpu(el
->l_count
),
3656 le16_to_cpu(el
->l_next_free_rec
),
3657 le32_to_cpu(el
->l_recs
[i
].e_cpos
),
3658 le16_to_cpu(el
->l_recs
[i
].e_leaf_clusters
),
3659 le32_to_cpu(insert_rec
->e_cpos
),
3660 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3662 el
->l_recs
[i
] = *insert_rec
;
3663 le16_add_cpu(&el
->l_next_free_rec
, 1);
3669 * Ok, we have to rotate.
3671 * At this point, it is safe to assume that inserting into an
3672 * empty leaf and appending to a leaf have both been handled
3675 * This leaf needs to have space, either by the empty 1st
3676 * extent record, or by virtue of an l_next_rec < l_count.
3678 ocfs2_rotate_leaf(el
, insert_rec
);
3681 static void ocfs2_adjust_rightmost_records(struct inode
*inode
,
3683 struct ocfs2_path
*path
,
3684 struct ocfs2_extent_rec
*insert_rec
)
3686 int ret
, i
, next_free
;
3687 struct buffer_head
*bh
;
3688 struct ocfs2_extent_list
*el
;
3689 struct ocfs2_extent_rec
*rec
;
3692 * Update everything except the leaf block.
3694 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
3695 bh
= path
->p_node
[i
].bh
;
3696 el
= path
->p_node
[i
].el
;
3698 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3699 if (next_free
== 0) {
3700 ocfs2_error(inode
->i_sb
,
3701 "Dinode %llu has a bad extent list",
3702 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3707 rec
= &el
->l_recs
[next_free
- 1];
3709 rec
->e_int_clusters
= insert_rec
->e_cpos
;
3710 le32_add_cpu(&rec
->e_int_clusters
,
3711 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3712 le32_add_cpu(&rec
->e_int_clusters
,
3713 -le32_to_cpu(rec
->e_cpos
));
3715 ret
= ocfs2_journal_dirty(handle
, bh
);
3722 static int ocfs2_append_rec_to_path(struct inode
*inode
, handle_t
*handle
,
3723 struct ocfs2_extent_rec
*insert_rec
,
3724 struct ocfs2_path
*right_path
,
3725 struct ocfs2_path
**ret_left_path
)
3728 struct ocfs2_extent_list
*el
;
3729 struct ocfs2_path
*left_path
= NULL
;
3731 *ret_left_path
= NULL
;
3734 * This shouldn't happen for non-trees. The extent rec cluster
3735 * count manipulation below only works for interior nodes.
3737 BUG_ON(right_path
->p_tree_depth
== 0);
3740 * If our appending insert is at the leftmost edge of a leaf,
3741 * then we might need to update the rightmost records of the
3744 el
= path_leaf_el(right_path
);
3745 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3746 if (next_free
== 0 ||
3747 (next_free
== 1 && ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3750 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
3757 mlog(0, "Append may need a left path update. cpos: %u, "
3758 "left_cpos: %u\n", le32_to_cpu(insert_rec
->e_cpos
),
3762 * No need to worry if the append is already in the
3766 left_path
= ocfs2_new_path(path_root_bh(right_path
),
3767 path_root_el(right_path
));
3774 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3781 * ocfs2_insert_path() will pass the left_path to the
3787 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
3793 ocfs2_adjust_rightmost_records(inode
, handle
, right_path
, insert_rec
);
3795 *ret_left_path
= left_path
;
3799 ocfs2_free_path(left_path
);
3804 static void ocfs2_split_record(struct inode
*inode
,
3805 struct ocfs2_path
*left_path
,
3806 struct ocfs2_path
*right_path
,
3807 struct ocfs2_extent_rec
*split_rec
,
3808 enum ocfs2_split_type split
)
3811 u32 cpos
= le32_to_cpu(split_rec
->e_cpos
);
3812 struct ocfs2_extent_list
*left_el
= NULL
, *right_el
, *insert_el
, *el
;
3813 struct ocfs2_extent_rec
*rec
, *tmprec
;
3815 right_el
= path_leaf_el(right_path
);;
3817 left_el
= path_leaf_el(left_path
);
3820 insert_el
= right_el
;
3821 index
= ocfs2_search_extent_list(el
, cpos
);
3823 if (index
== 0 && left_path
) {
3824 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3827 * This typically means that the record
3828 * started in the left path but moved to the
3829 * right as a result of rotation. We either
3830 * move the existing record to the left, or we
3831 * do the later insert there.
3833 * In this case, the left path should always
3834 * exist as the rotate code will have passed
3835 * it back for a post-insert update.
3838 if (split
== SPLIT_LEFT
) {
3840 * It's a left split. Since we know
3841 * that the rotate code gave us an
3842 * empty extent in the left path, we
3843 * can just do the insert there.
3845 insert_el
= left_el
;
3848 * Right split - we have to move the
3849 * existing record over to the left
3850 * leaf. The insert will be into the
3851 * newly created empty extent in the
3854 tmprec
= &right_el
->l_recs
[index
];
3855 ocfs2_rotate_leaf(left_el
, tmprec
);
3858 memset(tmprec
, 0, sizeof(*tmprec
));
3859 index
= ocfs2_search_extent_list(left_el
, cpos
);
3860 BUG_ON(index
== -1);
3865 BUG_ON(!ocfs2_is_empty_extent(&left_el
->l_recs
[0]));
3867 * Left path is easy - we can just allow the insert to
3871 insert_el
= left_el
;
3872 index
= ocfs2_search_extent_list(el
, cpos
);
3873 BUG_ON(index
== -1);
3876 rec
= &el
->l_recs
[index
];
3877 ocfs2_subtract_from_rec(inode
->i_sb
, split
, rec
, split_rec
);
3878 ocfs2_rotate_leaf(insert_el
, split_rec
);
3882 * This function only does inserts on an allocation b-tree. For tree
3883 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3885 * right_path is the path we want to do the actual insert
3886 * in. left_path should only be passed in if we need to update that
3887 * portion of the tree after an edge insert.
3889 static int ocfs2_insert_path(struct inode
*inode
,
3891 struct ocfs2_path
*left_path
,
3892 struct ocfs2_path
*right_path
,
3893 struct ocfs2_extent_rec
*insert_rec
,
3894 struct ocfs2_insert_type
*insert
)
3896 int ret
, subtree_index
;
3897 struct buffer_head
*leaf_bh
= path_leaf_bh(right_path
);
3900 int credits
= handle
->h_buffer_credits
;
3903 * There's a chance that left_path got passed back to
3904 * us without being accounted for in the
3905 * journal. Extend our transaction here to be sure we
3906 * can change those blocks.
3908 credits
+= left_path
->p_tree_depth
;
3910 ret
= ocfs2_extend_trans(handle
, credits
);
3916 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
3924 * Pass both paths to the journal. The majority of inserts
3925 * will be touching all components anyway.
3927 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
3933 if (insert
->ins_split
!= SPLIT_NONE
) {
3935 * We could call ocfs2_insert_at_leaf() for some types
3936 * of splits, but it's easier to just let one separate
3937 * function sort it all out.
3939 ocfs2_split_record(inode
, left_path
, right_path
,
3940 insert_rec
, insert
->ins_split
);
3943 * Split might have modified either leaf and we don't
3944 * have a guarantee that the later edge insert will
3945 * dirty this for us.
3948 ret
= ocfs2_journal_dirty(handle
,
3949 path_leaf_bh(left_path
));
3953 ocfs2_insert_at_leaf(insert_rec
, path_leaf_el(right_path
),
3956 ret
= ocfs2_journal_dirty(handle
, leaf_bh
);
3962 * The rotate code has indicated that we need to fix
3963 * up portions of the tree after the insert.
3965 * XXX: Should we extend the transaction here?
3967 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
,
3969 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3970 right_path
, subtree_index
);
3978 static int ocfs2_do_insert_extent(struct inode
*inode
,
3980 struct ocfs2_extent_tree
*et
,
3981 struct ocfs2_extent_rec
*insert_rec
,
3982 struct ocfs2_insert_type
*type
)
3984 int ret
, rotate
= 0;
3986 struct ocfs2_path
*right_path
= NULL
;
3987 struct ocfs2_path
*left_path
= NULL
;
3988 struct ocfs2_extent_list
*el
;
3990 el
= et
->et_root_el
;
3992 ret
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
3993 OCFS2_JOURNAL_ACCESS_WRITE
);
3999 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
4000 ocfs2_insert_at_leaf(insert_rec
, el
, type
, inode
);
4001 goto out_update_clusters
;
4004 right_path
= ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
);
4012 * Determine the path to start with. Rotations need the
4013 * rightmost path, everything else can go directly to the
4016 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4017 if (type
->ins_appending
== APPEND_NONE
&&
4018 type
->ins_contig
== CONTIG_NONE
) {
4023 ret
= ocfs2_find_path(inode
, right_path
, cpos
);
4030 * Rotations and appends need special treatment - they modify
4031 * parts of the tree's above them.
4033 * Both might pass back a path immediate to the left of the
4034 * one being inserted to. This will be cause
4035 * ocfs2_insert_path() to modify the rightmost records of
4036 * left_path to account for an edge insert.
4038 * XXX: When modifying this code, keep in mind that an insert
4039 * can wind up skipping both of these two special cases...
4042 ret
= ocfs2_rotate_tree_right(inode
, handle
, type
->ins_split
,
4043 le32_to_cpu(insert_rec
->e_cpos
),
4044 right_path
, &left_path
);
4051 * ocfs2_rotate_tree_right() might have extended the
4052 * transaction without re-journaling our tree root.
4054 ret
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
4055 OCFS2_JOURNAL_ACCESS_WRITE
);
4060 } else if (type
->ins_appending
== APPEND_TAIL
4061 && type
->ins_contig
!= CONTIG_LEFT
) {
4062 ret
= ocfs2_append_rec_to_path(inode
, handle
, insert_rec
,
4063 right_path
, &left_path
);
4070 ret
= ocfs2_insert_path(inode
, handle
, left_path
, right_path
,
4077 out_update_clusters
:
4078 if (type
->ins_split
== SPLIT_NONE
)
4079 ocfs2_et_update_clusters(inode
, et
,
4080 le16_to_cpu(insert_rec
->e_leaf_clusters
));
4082 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4087 ocfs2_free_path(left_path
);
4088 ocfs2_free_path(right_path
);
4093 static enum ocfs2_contig_type
4094 ocfs2_figure_merge_contig_type(struct inode
*inode
, struct ocfs2_path
*path
,
4095 struct ocfs2_extent_list
*el
, int index
,
4096 struct ocfs2_extent_rec
*split_rec
)
4099 enum ocfs2_contig_type ret
= CONTIG_NONE
;
4100 u32 left_cpos
, right_cpos
;
4101 struct ocfs2_extent_rec
*rec
= NULL
;
4102 struct ocfs2_extent_list
*new_el
;
4103 struct ocfs2_path
*left_path
= NULL
, *right_path
= NULL
;
4104 struct buffer_head
*bh
;
4105 struct ocfs2_extent_block
*eb
;
4108 rec
= &el
->l_recs
[index
- 1];
4109 } else if (path
->p_tree_depth
> 0) {
4110 status
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
4115 if (left_cpos
!= 0) {
4116 left_path
= ocfs2_new_path(path_root_bh(path
),
4117 path_root_el(path
));
4121 status
= ocfs2_find_path(inode
, left_path
, left_cpos
);
4125 new_el
= path_leaf_el(left_path
);
4127 if (le16_to_cpu(new_el
->l_next_free_rec
) !=
4128 le16_to_cpu(new_el
->l_count
)) {
4129 bh
= path_leaf_bh(left_path
);
4130 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4131 ocfs2_error(inode
->i_sb
,
4132 "Extent block #%llu has an "
4133 "invalid l_next_free_rec of "
4134 "%d. It should have "
4135 "matched the l_count of %d",
4136 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4137 le16_to_cpu(new_el
->l_next_free_rec
),
4138 le16_to_cpu(new_el
->l_count
));
4142 rec
= &new_el
->l_recs
[
4143 le16_to_cpu(new_el
->l_next_free_rec
) - 1];
4148 * We're careful to check for an empty extent record here -
4149 * the merge code will know what to do if it sees one.
4152 if (index
== 1 && ocfs2_is_empty_extent(rec
)) {
4153 if (split_rec
->e_cpos
== el
->l_recs
[index
].e_cpos
)
4156 ret
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4161 if (index
< (le16_to_cpu(el
->l_next_free_rec
) - 1))
4162 rec
= &el
->l_recs
[index
+ 1];
4163 else if (le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
) &&
4164 path
->p_tree_depth
> 0) {
4165 status
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
,
4170 if (right_cpos
== 0)
4173 right_path
= ocfs2_new_path(path_root_bh(path
),
4174 path_root_el(path
));
4178 status
= ocfs2_find_path(inode
, right_path
, right_cpos
);
4182 new_el
= path_leaf_el(right_path
);
4183 rec
= &new_el
->l_recs
[0];
4184 if (ocfs2_is_empty_extent(rec
)) {
4185 if (le16_to_cpu(new_el
->l_next_free_rec
) <= 1) {
4186 bh
= path_leaf_bh(right_path
);
4187 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4188 ocfs2_error(inode
->i_sb
,
4189 "Extent block #%llu has an "
4190 "invalid l_next_free_rec of %d",
4191 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4192 le16_to_cpu(new_el
->l_next_free_rec
));
4196 rec
= &new_el
->l_recs
[1];
4201 enum ocfs2_contig_type contig_type
;
4203 contig_type
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4205 if (contig_type
== CONTIG_LEFT
&& ret
== CONTIG_RIGHT
)
4206 ret
= CONTIG_LEFTRIGHT
;
4207 else if (ret
== CONTIG_NONE
)
4213 ocfs2_free_path(left_path
);
4215 ocfs2_free_path(right_path
);
4220 static void ocfs2_figure_contig_type(struct inode
*inode
,
4221 struct ocfs2_insert_type
*insert
,
4222 struct ocfs2_extent_list
*el
,
4223 struct ocfs2_extent_rec
*insert_rec
,
4224 struct ocfs2_extent_tree
*et
)
4227 enum ocfs2_contig_type contig_type
= CONTIG_NONE
;
4229 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4231 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
4232 contig_type
= ocfs2_extent_contig(inode
, &el
->l_recs
[i
],
4234 if (contig_type
!= CONTIG_NONE
) {
4235 insert
->ins_contig_index
= i
;
4239 insert
->ins_contig
= contig_type
;
4241 if (insert
->ins_contig
!= CONTIG_NONE
) {
4242 struct ocfs2_extent_rec
*rec
=
4243 &el
->l_recs
[insert
->ins_contig_index
];
4244 unsigned int len
= le16_to_cpu(rec
->e_leaf_clusters
) +
4245 le16_to_cpu(insert_rec
->e_leaf_clusters
);
4248 * Caller might want us to limit the size of extents, don't
4249 * calculate contiguousness if we might exceed that limit.
4251 if (et
->et_max_leaf_clusters
&&
4252 (len
> et
->et_max_leaf_clusters
))
4253 insert
->ins_contig
= CONTIG_NONE
;
4258 * This should only be called against the righmost leaf extent list.
4260 * ocfs2_figure_appending_type() will figure out whether we'll have to
4261 * insert at the tail of the rightmost leaf.
4263 * This should also work against the root extent list for tree's with 0
4264 * depth. If we consider the root extent list to be the rightmost leaf node
4265 * then the logic here makes sense.
4267 static void ocfs2_figure_appending_type(struct ocfs2_insert_type
*insert
,
4268 struct ocfs2_extent_list
*el
,
4269 struct ocfs2_extent_rec
*insert_rec
)
4272 u32 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4273 struct ocfs2_extent_rec
*rec
;
4275 insert
->ins_appending
= APPEND_NONE
;
4277 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4279 if (!el
->l_next_free_rec
)
4280 goto set_tail_append
;
4282 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
4283 /* Were all records empty? */
4284 if (le16_to_cpu(el
->l_next_free_rec
) == 1)
4285 goto set_tail_append
;
4288 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
4289 rec
= &el
->l_recs
[i
];
4292 (le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)))
4293 goto set_tail_append
;
4298 insert
->ins_appending
= APPEND_TAIL
;
4302 * Helper function called at the begining of an insert.
4304 * This computes a few things that are commonly used in the process of
4305 * inserting into the btree:
4306 * - Whether the new extent is contiguous with an existing one.
4307 * - The current tree depth.
4308 * - Whether the insert is an appending one.
4309 * - The total # of free records in the tree.
4311 * All of the information is stored on the ocfs2_insert_type
4314 static int ocfs2_figure_insert_type(struct inode
*inode
,
4315 struct ocfs2_extent_tree
*et
,
4316 struct buffer_head
**last_eb_bh
,
4317 struct ocfs2_extent_rec
*insert_rec
,
4319 struct ocfs2_insert_type
*insert
)
4322 struct ocfs2_extent_block
*eb
;
4323 struct ocfs2_extent_list
*el
;
4324 struct ocfs2_path
*path
= NULL
;
4325 struct buffer_head
*bh
= NULL
;
4327 insert
->ins_split
= SPLIT_NONE
;
4329 el
= et
->et_root_el
;
4330 insert
->ins_tree_depth
= le16_to_cpu(el
->l_tree_depth
);
4332 if (el
->l_tree_depth
) {
4334 * If we have tree depth, we read in the
4335 * rightmost extent block ahead of time as
4336 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4337 * may want it later.
4339 ret
= ocfs2_read_extent_block(inode
,
4340 ocfs2_et_get_last_eb_blk(et
),
4346 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
4351 * Unless we have a contiguous insert, we'll need to know if
4352 * there is room left in our allocation tree for another
4355 * XXX: This test is simplistic, we can search for empty
4356 * extent records too.
4358 *free_records
= le16_to_cpu(el
->l_count
) -
4359 le16_to_cpu(el
->l_next_free_rec
);
4361 if (!insert
->ins_tree_depth
) {
4362 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4363 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4367 path
= ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
);
4375 * In the case that we're inserting past what the tree
4376 * currently accounts for, ocfs2_find_path() will return for
4377 * us the rightmost tree path. This is accounted for below in
4378 * the appending code.
4380 ret
= ocfs2_find_path(inode
, path
, le32_to_cpu(insert_rec
->e_cpos
));
4386 el
= path_leaf_el(path
);
4389 * Now that we have the path, there's two things we want to determine:
4390 * 1) Contiguousness (also set contig_index if this is so)
4392 * 2) Are we doing an append? We can trivially break this up
4393 * into two types of appends: simple record append, or a
4394 * rotate inside the tail leaf.
4396 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4399 * The insert code isn't quite ready to deal with all cases of
4400 * left contiguousness. Specifically, if it's an insert into
4401 * the 1st record in a leaf, it will require the adjustment of
4402 * cluster count on the last record of the path directly to it's
4403 * left. For now, just catch that case and fool the layers
4404 * above us. This works just fine for tree_depth == 0, which
4405 * is why we allow that above.
4407 if (insert
->ins_contig
== CONTIG_LEFT
&&
4408 insert
->ins_contig_index
== 0)
4409 insert
->ins_contig
= CONTIG_NONE
;
4412 * Ok, so we can simply compare against last_eb to figure out
4413 * whether the path doesn't exist. This will only happen in
4414 * the case that we're doing a tail append, so maybe we can
4415 * take advantage of that information somehow.
4417 if (ocfs2_et_get_last_eb_blk(et
) ==
4418 path_leaf_bh(path
)->b_blocknr
) {
4420 * Ok, ocfs2_find_path() returned us the rightmost
4421 * tree path. This might be an appending insert. There are
4423 * 1) We're doing a true append at the tail:
4424 * -This might even be off the end of the leaf
4425 * 2) We're "appending" by rotating in the tail
4427 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4431 ocfs2_free_path(path
);
4441 * Insert an extent into an inode btree.
4443 * The caller needs to update fe->i_clusters
4445 int ocfs2_insert_extent(struct ocfs2_super
*osb
,
4447 struct inode
*inode
,
4448 struct ocfs2_extent_tree
*et
,
4453 struct ocfs2_alloc_context
*meta_ac
)
4456 int uninitialized_var(free_records
);
4457 struct buffer_head
*last_eb_bh
= NULL
;
4458 struct ocfs2_insert_type insert
= {0, };
4459 struct ocfs2_extent_rec rec
;
4461 mlog(0, "add %u clusters at position %u to inode %llu\n",
4462 new_clusters
, cpos
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4464 memset(&rec
, 0, sizeof(rec
));
4465 rec
.e_cpos
= cpu_to_le32(cpos
);
4466 rec
.e_blkno
= cpu_to_le64(start_blk
);
4467 rec
.e_leaf_clusters
= cpu_to_le16(new_clusters
);
4468 rec
.e_flags
= flags
;
4469 status
= ocfs2_et_insert_check(inode
, et
, &rec
);
4475 status
= ocfs2_figure_insert_type(inode
, et
, &last_eb_bh
, &rec
,
4476 &free_records
, &insert
);
4482 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4483 "Insert.contig_index: %d, Insert.free_records: %d, "
4484 "Insert.tree_depth: %d\n",
4485 insert
.ins_appending
, insert
.ins_contig
, insert
.ins_contig_index
,
4486 free_records
, insert
.ins_tree_depth
);
4488 if (insert
.ins_contig
== CONTIG_NONE
&& free_records
== 0) {
4489 status
= ocfs2_grow_tree(inode
, handle
, et
,
4490 &insert
.ins_tree_depth
, &last_eb_bh
,
4498 /* Finally, we can add clusters. This might rotate the tree for us. */
4499 status
= ocfs2_do_insert_extent(inode
, handle
, et
, &rec
, &insert
);
4502 else if (et
->et_ops
== &ocfs2_dinode_et_ops
)
4503 ocfs2_extent_map_insert_rec(inode
, &rec
);
4513 * Allcate and add clusters into the extent b-tree.
4514 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4515 * The extent b-tree's root is specified by et, and
4516 * it is not limited to the file storage. Any extent tree can use this
4517 * function if it implements the proper ocfs2_extent_tree.
4519 int ocfs2_add_clusters_in_btree(struct ocfs2_super
*osb
,
4520 struct inode
*inode
,
4521 u32
*logical_offset
,
4522 u32 clusters_to_add
,
4524 struct ocfs2_extent_tree
*et
,
4526 struct ocfs2_alloc_context
*data_ac
,
4527 struct ocfs2_alloc_context
*meta_ac
,
4528 enum ocfs2_alloc_restarted
*reason_ret
)
4532 enum ocfs2_alloc_restarted reason
= RESTART_NONE
;
4533 u32 bit_off
, num_bits
;
4537 BUG_ON(!clusters_to_add
);
4540 flags
= OCFS2_EXT_UNWRITTEN
;
4542 free_extents
= ocfs2_num_free_extents(osb
, inode
, et
);
4543 if (free_extents
< 0) {
4544 status
= free_extents
;
4549 /* there are two cases which could cause us to EAGAIN in the
4550 * we-need-more-metadata case:
4551 * 1) we haven't reserved *any*
4552 * 2) we are so fragmented, we've needed to add metadata too
4554 if (!free_extents
&& !meta_ac
) {
4555 mlog(0, "we haven't reserved any metadata!\n");
4557 reason
= RESTART_META
;
4559 } else if ((!free_extents
)
4560 && (ocfs2_alloc_context_bits_left(meta_ac
)
4561 < ocfs2_extend_meta_needed(et
->et_root_el
))) {
4562 mlog(0, "filesystem is really fragmented...\n");
4564 reason
= RESTART_META
;
4568 status
= __ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
4569 clusters_to_add
, &bit_off
, &num_bits
);
4571 if (status
!= -ENOSPC
)
4576 BUG_ON(num_bits
> clusters_to_add
);
4578 /* reserve our write early -- insert_extent may update the inode */
4579 status
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
4580 OCFS2_JOURNAL_ACCESS_WRITE
);
4586 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
4587 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4588 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4589 status
= ocfs2_insert_extent(osb
, handle
, inode
, et
,
4590 *logical_offset
, block
,
4591 num_bits
, flags
, meta_ac
);
4597 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4603 clusters_to_add
-= num_bits
;
4604 *logical_offset
+= num_bits
;
4606 if (clusters_to_add
) {
4607 mlog(0, "need to alloc once more, wanted = %u\n",
4610 reason
= RESTART_TRANS
;
4616 *reason_ret
= reason
;
4620 static void ocfs2_make_right_split_rec(struct super_block
*sb
,
4621 struct ocfs2_extent_rec
*split_rec
,
4623 struct ocfs2_extent_rec
*rec
)
4625 u32 rec_cpos
= le32_to_cpu(rec
->e_cpos
);
4626 u32 rec_range
= rec_cpos
+ le16_to_cpu(rec
->e_leaf_clusters
);
4628 memset(split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4630 split_rec
->e_cpos
= cpu_to_le32(cpos
);
4631 split_rec
->e_leaf_clusters
= cpu_to_le16(rec_range
- cpos
);
4633 split_rec
->e_blkno
= rec
->e_blkno
;
4634 le64_add_cpu(&split_rec
->e_blkno
,
4635 ocfs2_clusters_to_blocks(sb
, cpos
- rec_cpos
));
4637 split_rec
->e_flags
= rec
->e_flags
;
4640 static int ocfs2_split_and_insert(struct inode
*inode
,
4642 struct ocfs2_path
*path
,
4643 struct ocfs2_extent_tree
*et
,
4644 struct buffer_head
**last_eb_bh
,
4646 struct ocfs2_extent_rec
*orig_split_rec
,
4647 struct ocfs2_alloc_context
*meta_ac
)
4650 unsigned int insert_range
, rec_range
, do_leftright
= 0;
4651 struct ocfs2_extent_rec tmprec
;
4652 struct ocfs2_extent_list
*rightmost_el
;
4653 struct ocfs2_extent_rec rec
;
4654 struct ocfs2_extent_rec split_rec
= *orig_split_rec
;
4655 struct ocfs2_insert_type insert
;
4656 struct ocfs2_extent_block
*eb
;
4660 * Store a copy of the record on the stack - it might move
4661 * around as the tree is manipulated below.
4663 rec
= path_leaf_el(path
)->l_recs
[split_index
];
4665 rightmost_el
= et
->et_root_el
;
4667 depth
= le16_to_cpu(rightmost_el
->l_tree_depth
);
4669 BUG_ON(!(*last_eb_bh
));
4670 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
4671 rightmost_el
= &eb
->h_list
;
4674 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4675 le16_to_cpu(rightmost_el
->l_count
)) {
4676 ret
= ocfs2_grow_tree(inode
, handle
, et
,
4677 &depth
, last_eb_bh
, meta_ac
);
4684 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4685 insert
.ins_appending
= APPEND_NONE
;
4686 insert
.ins_contig
= CONTIG_NONE
;
4687 insert
.ins_tree_depth
= depth
;
4689 insert_range
= le32_to_cpu(split_rec
.e_cpos
) +
4690 le16_to_cpu(split_rec
.e_leaf_clusters
);
4691 rec_range
= le32_to_cpu(rec
.e_cpos
) +
4692 le16_to_cpu(rec
.e_leaf_clusters
);
4694 if (split_rec
.e_cpos
== rec
.e_cpos
) {
4695 insert
.ins_split
= SPLIT_LEFT
;
4696 } else if (insert_range
== rec_range
) {
4697 insert
.ins_split
= SPLIT_RIGHT
;
4700 * Left/right split. We fake this as a right split
4701 * first and then make a second pass as a left split.
4703 insert
.ins_split
= SPLIT_RIGHT
;
4705 ocfs2_make_right_split_rec(inode
->i_sb
, &tmprec
, insert_range
,
4710 BUG_ON(do_leftright
);
4714 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
4720 if (do_leftright
== 1) {
4722 struct ocfs2_extent_list
*el
;
4725 split_rec
= *orig_split_rec
;
4727 ocfs2_reinit_path(path
, 1);
4729 cpos
= le32_to_cpu(split_rec
.e_cpos
);
4730 ret
= ocfs2_find_path(inode
, path
, cpos
);
4736 el
= path_leaf_el(path
);
4737 split_index
= ocfs2_search_extent_list(el
, cpos
);
4746 * Mark part or all of the extent record at split_index in the leaf
4747 * pointed to by path as written. This removes the unwritten
4750 * Care is taken to handle contiguousness so as to not grow the tree.
4752 * meta_ac is not strictly necessary - we only truly need it if growth
4753 * of the tree is required. All other cases will degrade into a less
4754 * optimal tree layout.
4756 * last_eb_bh should be the rightmost leaf block for any extent
4757 * btree. Since a split may grow the tree or a merge might shrink it,
4758 * the caller cannot trust the contents of that buffer after this call.
4760 * This code is optimized for readability - several passes might be
4761 * made over certain portions of the tree. All of those blocks will
4762 * have been brought into cache (and pinned via the journal), so the
4763 * extra overhead is not expressed in terms of disk reads.
4765 static int __ocfs2_mark_extent_written(struct inode
*inode
,
4766 struct ocfs2_extent_tree
*et
,
4768 struct ocfs2_path
*path
,
4770 struct ocfs2_extent_rec
*split_rec
,
4771 struct ocfs2_alloc_context
*meta_ac
,
4772 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
4775 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
4776 struct buffer_head
*last_eb_bh
= NULL
;
4777 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
4778 struct ocfs2_merge_ctxt ctxt
;
4779 struct ocfs2_extent_list
*rightmost_el
;
4781 if (!(rec
->e_flags
& OCFS2_EXT_UNWRITTEN
)) {
4787 if (le32_to_cpu(rec
->e_cpos
) > le32_to_cpu(split_rec
->e_cpos
) ||
4788 ((le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)) <
4789 (le32_to_cpu(split_rec
->e_cpos
) + le16_to_cpu(split_rec
->e_leaf_clusters
)))) {
4795 ctxt
.c_contig_type
= ocfs2_figure_merge_contig_type(inode
, path
, el
,
4800 * The core merge / split code wants to know how much room is
4801 * left in this inodes allocation tree, so we pass the
4802 * rightmost extent list.
4804 if (path
->p_tree_depth
) {
4805 struct ocfs2_extent_block
*eb
;
4807 ret
= ocfs2_read_extent_block(inode
,
4808 ocfs2_et_get_last_eb_blk(et
),
4815 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
4816 rightmost_el
= &eb
->h_list
;
4818 rightmost_el
= path_root_el(path
);
4820 if (rec
->e_cpos
== split_rec
->e_cpos
&&
4821 rec
->e_leaf_clusters
== split_rec
->e_leaf_clusters
)
4822 ctxt
.c_split_covers_rec
= 1;
4824 ctxt
.c_split_covers_rec
= 0;
4826 ctxt
.c_has_empty_extent
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
4828 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4829 split_index
, ctxt
.c_contig_type
, ctxt
.c_has_empty_extent
,
4830 ctxt
.c_split_covers_rec
);
4832 if (ctxt
.c_contig_type
== CONTIG_NONE
) {
4833 if (ctxt
.c_split_covers_rec
)
4834 el
->l_recs
[split_index
] = *split_rec
;
4836 ret
= ocfs2_split_and_insert(inode
, handle
, path
, et
,
4837 &last_eb_bh
, split_index
,
4838 split_rec
, meta_ac
);
4842 ret
= ocfs2_try_to_merge_extent(inode
, handle
, path
,
4843 split_index
, split_rec
,
4844 dealloc
, &ctxt
, et
);
4855 * Mark the already-existing extent at cpos as written for len clusters.
4857 * If the existing extent is larger than the request, initiate a
4858 * split. An attempt will be made at merging with adjacent extents.
4860 * The caller is responsible for passing down meta_ac if we'll need it.
4862 int ocfs2_mark_extent_written(struct inode
*inode
,
4863 struct ocfs2_extent_tree
*et
,
4864 handle_t
*handle
, u32 cpos
, u32 len
, u32 phys
,
4865 struct ocfs2_alloc_context
*meta_ac
,
4866 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
4869 u64 start_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys
);
4870 struct ocfs2_extent_rec split_rec
;
4871 struct ocfs2_path
*left_path
= NULL
;
4872 struct ocfs2_extent_list
*el
;
4874 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4875 inode
->i_ino
, cpos
, len
, phys
, (unsigned long long)start_blkno
);
4877 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode
->i_sb
))) {
4878 ocfs2_error(inode
->i_sb
, "Inode %llu has unwritten extents "
4879 "that are being written to, but the feature bit "
4880 "is not set in the super block.",
4881 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4887 * XXX: This should be fixed up so that we just re-insert the
4888 * next extent records.
4890 * XXX: This is a hack on the extent tree, maybe it should be
4893 if (et
->et_ops
== &ocfs2_dinode_et_ops
)
4894 ocfs2_extent_map_trunc(inode
, 0);
4896 left_path
= ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
);
4903 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
4908 el
= path_leaf_el(left_path
);
4910 index
= ocfs2_search_extent_list(el
, cpos
);
4911 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
4912 ocfs2_error(inode
->i_sb
,
4913 "Inode %llu has an extent at cpos %u which can no "
4914 "longer be found.\n",
4915 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
4920 memset(&split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4921 split_rec
.e_cpos
= cpu_to_le32(cpos
);
4922 split_rec
.e_leaf_clusters
= cpu_to_le16(len
);
4923 split_rec
.e_blkno
= cpu_to_le64(start_blkno
);
4924 split_rec
.e_flags
= path_leaf_el(left_path
)->l_recs
[index
].e_flags
;
4925 split_rec
.e_flags
&= ~OCFS2_EXT_UNWRITTEN
;
4927 ret
= __ocfs2_mark_extent_written(inode
, et
, handle
, left_path
,
4928 index
, &split_rec
, meta_ac
,
4934 ocfs2_free_path(left_path
);
4938 static int ocfs2_split_tree(struct inode
*inode
, struct ocfs2_extent_tree
*et
,
4939 handle_t
*handle
, struct ocfs2_path
*path
,
4940 int index
, u32 new_range
,
4941 struct ocfs2_alloc_context
*meta_ac
)
4943 int ret
, depth
, credits
= handle
->h_buffer_credits
;
4944 struct buffer_head
*last_eb_bh
= NULL
;
4945 struct ocfs2_extent_block
*eb
;
4946 struct ocfs2_extent_list
*rightmost_el
, *el
;
4947 struct ocfs2_extent_rec split_rec
;
4948 struct ocfs2_extent_rec
*rec
;
4949 struct ocfs2_insert_type insert
;
4952 * Setup the record to split before we grow the tree.
4954 el
= path_leaf_el(path
);
4955 rec
= &el
->l_recs
[index
];
4956 ocfs2_make_right_split_rec(inode
->i_sb
, &split_rec
, new_range
, rec
);
4958 depth
= path
->p_tree_depth
;
4960 ret
= ocfs2_read_extent_block(inode
,
4961 ocfs2_et_get_last_eb_blk(et
),
4968 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
4969 rightmost_el
= &eb
->h_list
;
4971 rightmost_el
= path_leaf_el(path
);
4973 credits
+= path
->p_tree_depth
+
4974 ocfs2_extend_meta_needed(et
->et_root_el
);
4975 ret
= ocfs2_extend_trans(handle
, credits
);
4981 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4982 le16_to_cpu(rightmost_el
->l_count
)) {
4983 ret
= ocfs2_grow_tree(inode
, handle
, et
, &depth
, &last_eb_bh
,
4991 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4992 insert
.ins_appending
= APPEND_NONE
;
4993 insert
.ins_contig
= CONTIG_NONE
;
4994 insert
.ins_split
= SPLIT_RIGHT
;
4995 insert
.ins_tree_depth
= depth
;
4997 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
5006 static int ocfs2_truncate_rec(struct inode
*inode
, handle_t
*handle
,
5007 struct ocfs2_path
*path
, int index
,
5008 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5010 struct ocfs2_extent_tree
*et
)
5013 u32 left_cpos
, rec_range
, trunc_range
;
5014 int wants_rotate
= 0, is_rightmost_tree_rec
= 0;
5015 struct super_block
*sb
= inode
->i_sb
;
5016 struct ocfs2_path
*left_path
= NULL
;
5017 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
5018 struct ocfs2_extent_rec
*rec
;
5019 struct ocfs2_extent_block
*eb
;
5021 if (ocfs2_is_empty_extent(&el
->l_recs
[0]) && index
> 0) {
5022 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5031 if (index
== (le16_to_cpu(el
->l_next_free_rec
) - 1) &&
5032 path
->p_tree_depth
) {
5034 * Check whether this is the rightmost tree record. If
5035 * we remove all of this record or part of its right
5036 * edge then an update of the record lengths above it
5039 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
5040 if (eb
->h_next_leaf_blk
== 0)
5041 is_rightmost_tree_rec
= 1;
5044 rec
= &el
->l_recs
[index
];
5045 if (index
== 0 && path
->p_tree_depth
&&
5046 le32_to_cpu(rec
->e_cpos
) == cpos
) {
5048 * Changing the leftmost offset (via partial or whole
5049 * record truncate) of an interior (or rightmost) path
5050 * means we have to update the subtree that is formed
5051 * by this leaf and the one to it's left.
5053 * There are two cases we can skip:
5054 * 1) Path is the leftmost one in our inode tree.
5055 * 2) The leaf is rightmost and will be empty after
5056 * we remove the extent record - the rotate code
5057 * knows how to update the newly formed edge.
5060 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
,
5067 if (left_cpos
&& le16_to_cpu(el
->l_next_free_rec
) > 1) {
5068 left_path
= ocfs2_new_path(path_root_bh(path
),
5069 path_root_el(path
));
5076 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
5084 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
5085 handle
->h_buffer_credits
,
5092 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
5098 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
5104 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5105 trunc_range
= cpos
+ len
;
5107 if (le32_to_cpu(rec
->e_cpos
) == cpos
&& rec_range
== trunc_range
) {
5110 memset(rec
, 0, sizeof(*rec
));
5111 ocfs2_cleanup_merge(el
, index
);
5114 next_free
= le16_to_cpu(el
->l_next_free_rec
);
5115 if (is_rightmost_tree_rec
&& next_free
> 1) {
5117 * We skip the edge update if this path will
5118 * be deleted by the rotate code.
5120 rec
= &el
->l_recs
[next_free
- 1];
5121 ocfs2_adjust_rightmost_records(inode
, handle
, path
,
5124 } else if (le32_to_cpu(rec
->e_cpos
) == cpos
) {
5125 /* Remove leftmost portion of the record. */
5126 le32_add_cpu(&rec
->e_cpos
, len
);
5127 le64_add_cpu(&rec
->e_blkno
, ocfs2_clusters_to_blocks(sb
, len
));
5128 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5129 } else if (rec_range
== trunc_range
) {
5130 /* Remove rightmost portion of the record */
5131 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5132 if (is_rightmost_tree_rec
)
5133 ocfs2_adjust_rightmost_records(inode
, handle
, path
, rec
);
5135 /* Caller should have trapped this. */
5136 mlog(ML_ERROR
, "Inode %llu: Invalid record truncate: (%u, %u) "
5137 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5138 le32_to_cpu(rec
->e_cpos
),
5139 le16_to_cpu(rec
->e_leaf_clusters
), cpos
, len
);
5146 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
5147 ocfs2_complete_edge_insert(inode
, handle
, left_path
, path
,
5151 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
5153 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5160 ocfs2_free_path(left_path
);
5164 int ocfs2_remove_extent(struct inode
*inode
,
5165 struct ocfs2_extent_tree
*et
,
5166 u32 cpos
, u32 len
, handle_t
*handle
,
5167 struct ocfs2_alloc_context
*meta_ac
,
5168 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5171 u32 rec_range
, trunc_range
;
5172 struct ocfs2_extent_rec
*rec
;
5173 struct ocfs2_extent_list
*el
;
5174 struct ocfs2_path
*path
= NULL
;
5176 ocfs2_extent_map_trunc(inode
, 0);
5178 path
= ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
);
5185 ret
= ocfs2_find_path(inode
, path
, cpos
);
5191 el
= path_leaf_el(path
);
5192 index
= ocfs2_search_extent_list(el
, cpos
);
5193 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5194 ocfs2_error(inode
->i_sb
,
5195 "Inode %llu has an extent at cpos %u which can no "
5196 "longer be found.\n",
5197 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
5203 * We have 3 cases of extent removal:
5204 * 1) Range covers the entire extent rec
5205 * 2) Range begins or ends on one edge of the extent rec
5206 * 3) Range is in the middle of the extent rec (no shared edges)
5208 * For case 1 we remove the extent rec and left rotate to
5211 * For case 2 we just shrink the existing extent rec, with a
5212 * tree update if the shrinking edge is also the edge of an
5215 * For case 3 we do a right split to turn the extent rec into
5216 * something case 2 can handle.
5218 rec
= &el
->l_recs
[index
];
5219 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5220 trunc_range
= cpos
+ len
;
5222 BUG_ON(cpos
< le32_to_cpu(rec
->e_cpos
) || trunc_range
> rec_range
);
5224 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5225 "(cpos %u, len %u)\n",
5226 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
, len
, index
,
5227 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
));
5229 if (le32_to_cpu(rec
->e_cpos
) == cpos
|| rec_range
== trunc_range
) {
5230 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5237 ret
= ocfs2_split_tree(inode
, et
, handle
, path
, index
,
5238 trunc_range
, meta_ac
);
5245 * The split could have manipulated the tree enough to
5246 * move the record location, so we have to look for it again.
5248 ocfs2_reinit_path(path
, 1);
5250 ret
= ocfs2_find_path(inode
, path
, cpos
);
5256 el
= path_leaf_el(path
);
5257 index
= ocfs2_search_extent_list(el
, cpos
);
5258 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5259 ocfs2_error(inode
->i_sb
,
5260 "Inode %llu: split at cpos %u lost record.",
5261 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5268 * Double check our values here. If anything is fishy,
5269 * it's easier to catch it at the top level.
5271 rec
= &el
->l_recs
[index
];
5272 rec_range
= le32_to_cpu(rec
->e_cpos
) +
5273 ocfs2_rec_clusters(el
, rec
);
5274 if (rec_range
!= trunc_range
) {
5275 ocfs2_error(inode
->i_sb
,
5276 "Inode %llu: error after split at cpos %u"
5277 "trunc len %u, existing record is (%u,%u)",
5278 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5279 cpos
, len
, le32_to_cpu(rec
->e_cpos
),
5280 ocfs2_rec_clusters(el
, rec
));
5285 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5294 ocfs2_free_path(path
);
5298 int ocfs2_remove_btree_range(struct inode
*inode
,
5299 struct ocfs2_extent_tree
*et
,
5300 u32 cpos
, u32 phys_cpos
, u32 len
,
5301 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5304 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
5305 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5306 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5308 struct ocfs2_alloc_context
*meta_ac
= NULL
;
5310 ret
= ocfs2_lock_allocators(inode
, et
, 0, 1, NULL
, &meta_ac
);
5316 mutex_lock(&tl_inode
->i_mutex
);
5318 if (ocfs2_truncate_log_needs_flush(osb
)) {
5319 ret
= __ocfs2_flush_truncate_log(osb
);
5326 handle
= ocfs2_start_trans(osb
, ocfs2_remove_extent_credits(osb
->sb
));
5327 if (IS_ERR(handle
)) {
5328 ret
= PTR_ERR(handle
);
5333 ret
= ocfs2_journal_access(handle
, inode
, et
->et_root_bh
,
5334 OCFS2_JOURNAL_ACCESS_WRITE
);
5340 ret
= ocfs2_remove_extent(inode
, et
, cpos
, len
, handle
, meta_ac
,
5347 ocfs2_et_update_clusters(inode
, et
, -len
);
5349 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
5355 ret
= ocfs2_truncate_log_append(osb
, handle
, phys_blkno
, len
);
5360 ocfs2_commit_trans(osb
, handle
);
5362 mutex_unlock(&tl_inode
->i_mutex
);
5365 ocfs2_free_alloc_context(meta_ac
);
5370 int ocfs2_truncate_log_needs_flush(struct ocfs2_super
*osb
)
5372 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5373 struct ocfs2_dinode
*di
;
5374 struct ocfs2_truncate_log
*tl
;
5376 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5377 tl
= &di
->id2
.i_dealloc
;
5379 mlog_bug_on_msg(le16_to_cpu(tl
->tl_used
) > le16_to_cpu(tl
->tl_count
),
5380 "slot %d, invalid truncate log parameters: used = "
5381 "%u, count = %u\n", osb
->slot_num
,
5382 le16_to_cpu(tl
->tl_used
), le16_to_cpu(tl
->tl_count
));
5383 return le16_to_cpu(tl
->tl_used
) == le16_to_cpu(tl
->tl_count
);
5386 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log
*tl
,
5387 unsigned int new_start
)
5389 unsigned int tail_index
;
5390 unsigned int current_tail
;
5392 /* No records, nothing to coalesce */
5393 if (!le16_to_cpu(tl
->tl_used
))
5396 tail_index
= le16_to_cpu(tl
->tl_used
) - 1;
5397 current_tail
= le32_to_cpu(tl
->tl_recs
[tail_index
].t_start
);
5398 current_tail
+= le32_to_cpu(tl
->tl_recs
[tail_index
].t_clusters
);
5400 return current_tail
== new_start
;
5403 int ocfs2_truncate_log_append(struct ocfs2_super
*osb
,
5406 unsigned int num_clusters
)
5409 unsigned int start_cluster
, tl_count
;
5410 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5411 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5412 struct ocfs2_dinode
*di
;
5413 struct ocfs2_truncate_log
*tl
;
5415 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5416 (unsigned long long)start_blk
, num_clusters
);
5418 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5420 start_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, start_blk
);
5422 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5424 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5425 * by the underlying call to ocfs2_read_inode_block(), so any
5426 * corruption is a code bug */
5427 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5429 tl
= &di
->id2
.i_dealloc
;
5430 tl_count
= le16_to_cpu(tl
->tl_count
);
5431 mlog_bug_on_msg(tl_count
> ocfs2_truncate_recs_per_inode(osb
->sb
) ||
5433 "Truncate record count on #%llu invalid "
5434 "wanted %u, actual %u\n",
5435 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
,
5436 ocfs2_truncate_recs_per_inode(osb
->sb
),
5437 le16_to_cpu(tl
->tl_count
));
5439 /* Caller should have known to flush before calling us. */
5440 index
= le16_to_cpu(tl
->tl_used
);
5441 if (index
>= tl_count
) {
5447 status
= ocfs2_journal_access(handle
, tl_inode
, tl_bh
,
5448 OCFS2_JOURNAL_ACCESS_WRITE
);
5454 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5455 "%llu (index = %d)\n", num_clusters
, start_cluster
,
5456 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
, index
);
5458 if (ocfs2_truncate_log_can_coalesce(tl
, start_cluster
)) {
5460 * Move index back to the record we are coalescing with.
5461 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5465 num_clusters
+= le32_to_cpu(tl
->tl_recs
[index
].t_clusters
);
5466 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5467 index
, le32_to_cpu(tl
->tl_recs
[index
].t_start
),
5470 tl
->tl_recs
[index
].t_start
= cpu_to_le32(start_cluster
);
5471 tl
->tl_used
= cpu_to_le16(index
+ 1);
5473 tl
->tl_recs
[index
].t_clusters
= cpu_to_le32(num_clusters
);
5475 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5486 static int ocfs2_replay_truncate_records(struct ocfs2_super
*osb
,
5488 struct inode
*data_alloc_inode
,
5489 struct buffer_head
*data_alloc_bh
)
5493 unsigned int num_clusters
;
5495 struct ocfs2_truncate_rec rec
;
5496 struct ocfs2_dinode
*di
;
5497 struct ocfs2_truncate_log
*tl
;
5498 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5499 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5503 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5504 tl
= &di
->id2
.i_dealloc
;
5505 i
= le16_to_cpu(tl
->tl_used
) - 1;
5507 /* Caller has given us at least enough credits to
5508 * update the truncate log dinode */
5509 status
= ocfs2_journal_access(handle
, tl_inode
, tl_bh
,
5510 OCFS2_JOURNAL_ACCESS_WRITE
);
5516 tl
->tl_used
= cpu_to_le16(i
);
5518 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5524 /* TODO: Perhaps we can calculate the bulk of the
5525 * credits up front rather than extending like
5527 status
= ocfs2_extend_trans(handle
,
5528 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC
);
5534 rec
= tl
->tl_recs
[i
];
5535 start_blk
= ocfs2_clusters_to_blocks(data_alloc_inode
->i_sb
,
5536 le32_to_cpu(rec
.t_start
));
5537 num_clusters
= le32_to_cpu(rec
.t_clusters
);
5539 /* if start_blk is not set, we ignore the record as
5542 mlog(0, "free record %d, start = %u, clusters = %u\n",
5543 i
, le32_to_cpu(rec
.t_start
), num_clusters
);
5545 status
= ocfs2_free_clusters(handle
, data_alloc_inode
,
5546 data_alloc_bh
, start_blk
,
5561 /* Expects you to already be holding tl_inode->i_mutex */
5562 int __ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5565 unsigned int num_to_flush
;
5567 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5568 struct inode
*data_alloc_inode
= NULL
;
5569 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5570 struct buffer_head
*data_alloc_bh
= NULL
;
5571 struct ocfs2_dinode
*di
;
5572 struct ocfs2_truncate_log
*tl
;
5576 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5578 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5580 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5581 * by the underlying call to ocfs2_read_inode_block(), so any
5582 * corruption is a code bug */
5583 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5585 tl
= &di
->id2
.i_dealloc
;
5586 num_to_flush
= le16_to_cpu(tl
->tl_used
);
5587 mlog(0, "Flush %u records from truncate log #%llu\n",
5588 num_to_flush
, (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
);
5589 if (!num_to_flush
) {
5594 data_alloc_inode
= ocfs2_get_system_file_inode(osb
,
5595 GLOBAL_BITMAP_SYSTEM_INODE
,
5596 OCFS2_INVALID_SLOT
);
5597 if (!data_alloc_inode
) {
5599 mlog(ML_ERROR
, "Could not get bitmap inode!\n");
5603 mutex_lock(&data_alloc_inode
->i_mutex
);
5605 status
= ocfs2_inode_lock(data_alloc_inode
, &data_alloc_bh
, 1);
5611 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5612 if (IS_ERR(handle
)) {
5613 status
= PTR_ERR(handle
);
5618 status
= ocfs2_replay_truncate_records(osb
, handle
, data_alloc_inode
,
5623 ocfs2_commit_trans(osb
, handle
);
5626 brelse(data_alloc_bh
);
5627 ocfs2_inode_unlock(data_alloc_inode
, 1);
5630 mutex_unlock(&data_alloc_inode
->i_mutex
);
5631 iput(data_alloc_inode
);
5638 int ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5641 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5643 mutex_lock(&tl_inode
->i_mutex
);
5644 status
= __ocfs2_flush_truncate_log(osb
);
5645 mutex_unlock(&tl_inode
->i_mutex
);
5650 static void ocfs2_truncate_log_worker(struct work_struct
*work
)
5653 struct ocfs2_super
*osb
=
5654 container_of(work
, struct ocfs2_super
,
5655 osb_truncate_log_wq
.work
);
5659 status
= ocfs2_flush_truncate_log(osb
);
5663 ocfs2_init_inode_steal_slot(osb
);
5668 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5669 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super
*osb
,
5672 if (osb
->osb_tl_inode
) {
5673 /* We want to push off log flushes while truncates are
5676 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5678 queue_delayed_work(ocfs2_wq
, &osb
->osb_truncate_log_wq
,
5679 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL
);
5683 static int ocfs2_get_truncate_log_info(struct ocfs2_super
*osb
,
5685 struct inode
**tl_inode
,
5686 struct buffer_head
**tl_bh
)
5689 struct inode
*inode
= NULL
;
5690 struct buffer_head
*bh
= NULL
;
5692 inode
= ocfs2_get_system_file_inode(osb
,
5693 TRUNCATE_LOG_SYSTEM_INODE
,
5697 mlog(ML_ERROR
, "Could not get load truncate log inode!\n");
5701 status
= ocfs2_read_inode_block(inode
, &bh
);
5715 /* called during the 1st stage of node recovery. we stamp a clean
5716 * truncate log and pass back a copy for processing later. if the
5717 * truncate log does not require processing, a *tl_copy is set to
5719 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super
*osb
,
5721 struct ocfs2_dinode
**tl_copy
)
5724 struct inode
*tl_inode
= NULL
;
5725 struct buffer_head
*tl_bh
= NULL
;
5726 struct ocfs2_dinode
*di
;
5727 struct ocfs2_truncate_log
*tl
;
5731 mlog(0, "recover truncate log from slot %d\n", slot_num
);
5733 status
= ocfs2_get_truncate_log_info(osb
, slot_num
, &tl_inode
, &tl_bh
);
5739 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5741 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5742 * validated by the underlying call to ocfs2_read_inode_block(),
5743 * so any corruption is a code bug */
5744 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5746 tl
= &di
->id2
.i_dealloc
;
5747 if (le16_to_cpu(tl
->tl_used
)) {
5748 mlog(0, "We'll have %u logs to recover\n",
5749 le16_to_cpu(tl
->tl_used
));
5751 *tl_copy
= kmalloc(tl_bh
->b_size
, GFP_KERNEL
);
5758 /* Assuming the write-out below goes well, this copy
5759 * will be passed back to recovery for processing. */
5760 memcpy(*tl_copy
, tl_bh
->b_data
, tl_bh
->b_size
);
5762 /* All we need to do to clear the truncate log is set
5766 status
= ocfs2_write_block(osb
, tl_bh
, tl_inode
);
5778 if (status
< 0 && (*tl_copy
)) {
5787 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super
*osb
,
5788 struct ocfs2_dinode
*tl_copy
)
5792 unsigned int clusters
, num_recs
, start_cluster
;
5795 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5796 struct ocfs2_truncate_log
*tl
;
5800 if (OCFS2_I(tl_inode
)->ip_blkno
== le64_to_cpu(tl_copy
->i_blkno
)) {
5801 mlog(ML_ERROR
, "Asked to recover my own truncate log!\n");
5805 tl
= &tl_copy
->id2
.i_dealloc
;
5806 num_recs
= le16_to_cpu(tl
->tl_used
);
5807 mlog(0, "cleanup %u records from %llu\n", num_recs
,
5808 (unsigned long long)le64_to_cpu(tl_copy
->i_blkno
));
5810 mutex_lock(&tl_inode
->i_mutex
);
5811 for(i
= 0; i
< num_recs
; i
++) {
5812 if (ocfs2_truncate_log_needs_flush(osb
)) {
5813 status
= __ocfs2_flush_truncate_log(osb
);
5820 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5821 if (IS_ERR(handle
)) {
5822 status
= PTR_ERR(handle
);
5827 clusters
= le32_to_cpu(tl
->tl_recs
[i
].t_clusters
);
5828 start_cluster
= le32_to_cpu(tl
->tl_recs
[i
].t_start
);
5829 start_blk
= ocfs2_clusters_to_blocks(osb
->sb
, start_cluster
);
5831 status
= ocfs2_truncate_log_append(osb
, handle
,
5832 start_blk
, clusters
);
5833 ocfs2_commit_trans(osb
, handle
);
5841 mutex_unlock(&tl_inode
->i_mutex
);
5847 void ocfs2_truncate_log_shutdown(struct ocfs2_super
*osb
)
5850 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5855 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5856 flush_workqueue(ocfs2_wq
);
5858 status
= ocfs2_flush_truncate_log(osb
);
5862 brelse(osb
->osb_tl_bh
);
5863 iput(osb
->osb_tl_inode
);
5869 int ocfs2_truncate_log_init(struct ocfs2_super
*osb
)
5872 struct inode
*tl_inode
= NULL
;
5873 struct buffer_head
*tl_bh
= NULL
;
5877 status
= ocfs2_get_truncate_log_info(osb
,
5884 /* ocfs2_truncate_log_shutdown keys on the existence of
5885 * osb->osb_tl_inode so we don't set any of the osb variables
5886 * until we're sure all is well. */
5887 INIT_DELAYED_WORK(&osb
->osb_truncate_log_wq
,
5888 ocfs2_truncate_log_worker
);
5889 osb
->osb_tl_bh
= tl_bh
;
5890 osb
->osb_tl_inode
= tl_inode
;
5897 * Delayed de-allocation of suballocator blocks.
5899 * Some sets of block de-allocations might involve multiple suballocator inodes.
5901 * The locking for this can get extremely complicated, especially when
5902 * the suballocator inodes to delete from aren't known until deep
5903 * within an unrelated codepath.
5905 * ocfs2_extent_block structures are a good example of this - an inode
5906 * btree could have been grown by any number of nodes each allocating
5907 * out of their own suballoc inode.
5909 * These structures allow the delay of block de-allocation until a
5910 * later time, when locking of multiple cluster inodes won't cause
5915 * Describe a single bit freed from a suballocator. For the block
5916 * suballocators, it represents one block. For the global cluster
5917 * allocator, it represents some clusters and free_bit indicates
5920 struct ocfs2_cached_block_free
{
5921 struct ocfs2_cached_block_free
*free_next
;
5923 unsigned int free_bit
;
5926 struct ocfs2_per_slot_free_list
{
5927 struct ocfs2_per_slot_free_list
*f_next_suballocator
;
5930 struct ocfs2_cached_block_free
*f_first
;
5933 static int ocfs2_free_cached_blocks(struct ocfs2_super
*osb
,
5936 struct ocfs2_cached_block_free
*head
)
5941 struct inode
*inode
;
5942 struct buffer_head
*di_bh
= NULL
;
5943 struct ocfs2_cached_block_free
*tmp
;
5945 inode
= ocfs2_get_system_file_inode(osb
, sysfile_type
, slot
);
5952 mutex_lock(&inode
->i_mutex
);
5954 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
5960 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
5961 if (IS_ERR(handle
)) {
5962 ret
= PTR_ERR(handle
);
5968 bg_blkno
= ocfs2_which_suballoc_group(head
->free_blk
,
5970 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5971 head
->free_bit
, (unsigned long long)head
->free_blk
);
5973 ret
= ocfs2_free_suballoc_bits(handle
, inode
, di_bh
,
5974 head
->free_bit
, bg_blkno
, 1);
5980 ret
= ocfs2_extend_trans(handle
, OCFS2_SUBALLOC_FREE
);
5987 head
= head
->free_next
;
5992 ocfs2_commit_trans(osb
, handle
);
5995 ocfs2_inode_unlock(inode
, 1);
5998 mutex_unlock(&inode
->i_mutex
);
6002 /* Premature exit may have left some dangling items. */
6004 head
= head
->free_next
;
6011 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6012 u64 blkno
, unsigned int bit
)
6015 struct ocfs2_cached_block_free
*item
;
6017 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6024 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6025 bit
, (unsigned long long)blkno
);
6027 item
->free_blk
= blkno
;
6028 item
->free_bit
= bit
;
6029 item
->free_next
= ctxt
->c_global_allocator
;
6031 ctxt
->c_global_allocator
= item
;
6035 static int ocfs2_free_cached_clusters(struct ocfs2_super
*osb
,
6036 struct ocfs2_cached_block_free
*head
)
6038 struct ocfs2_cached_block_free
*tmp
;
6039 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6043 mutex_lock(&tl_inode
->i_mutex
);
6046 if (ocfs2_truncate_log_needs_flush(osb
)) {
6047 ret
= __ocfs2_flush_truncate_log(osb
);
6054 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
6055 if (IS_ERR(handle
)) {
6056 ret
= PTR_ERR(handle
);
6061 ret
= ocfs2_truncate_log_append(osb
, handle
, head
->free_blk
,
6064 ocfs2_commit_trans(osb
, handle
);
6066 head
= head
->free_next
;
6075 mutex_unlock(&tl_inode
->i_mutex
);
6078 /* Premature exit may have left some dangling items. */
6080 head
= head
->free_next
;
6087 int ocfs2_run_deallocs(struct ocfs2_super
*osb
,
6088 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6091 struct ocfs2_per_slot_free_list
*fl
;
6096 while (ctxt
->c_first_suballocator
) {
6097 fl
= ctxt
->c_first_suballocator
;
6100 mlog(0, "Free items: (type %u, slot %d)\n",
6101 fl
->f_inode_type
, fl
->f_slot
);
6102 ret2
= ocfs2_free_cached_blocks(osb
,
6112 ctxt
->c_first_suballocator
= fl
->f_next_suballocator
;
6116 if (ctxt
->c_global_allocator
) {
6117 ret2
= ocfs2_free_cached_clusters(osb
,
6118 ctxt
->c_global_allocator
);
6124 ctxt
->c_global_allocator
= NULL
;
6130 static struct ocfs2_per_slot_free_list
*
6131 ocfs2_find_per_slot_free_list(int type
,
6133 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6135 struct ocfs2_per_slot_free_list
*fl
= ctxt
->c_first_suballocator
;
6138 if (fl
->f_inode_type
== type
&& fl
->f_slot
== slot
)
6141 fl
= fl
->f_next_suballocator
;
6144 fl
= kmalloc(sizeof(*fl
), GFP_NOFS
);
6146 fl
->f_inode_type
= type
;
6149 fl
->f_next_suballocator
= ctxt
->c_first_suballocator
;
6151 ctxt
->c_first_suballocator
= fl
;
6156 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6157 int type
, int slot
, u64 blkno
,
6161 struct ocfs2_per_slot_free_list
*fl
;
6162 struct ocfs2_cached_block_free
*item
;
6164 fl
= ocfs2_find_per_slot_free_list(type
, slot
, ctxt
);
6171 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6178 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6179 type
, slot
, bit
, (unsigned long long)blkno
);
6181 item
->free_blk
= blkno
;
6182 item
->free_bit
= bit
;
6183 item
->free_next
= fl
->f_first
;
6192 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6193 struct ocfs2_extent_block
*eb
)
6195 return ocfs2_cache_block_dealloc(ctxt
, EXTENT_ALLOC_SYSTEM_INODE
,
6196 le16_to_cpu(eb
->h_suballoc_slot
),
6197 le64_to_cpu(eb
->h_blkno
),
6198 le16_to_cpu(eb
->h_suballoc_bit
));
6201 /* This function will figure out whether the currently last extent
6202 * block will be deleted, and if it will, what the new last extent
6203 * block will be so we can update his h_next_leaf_blk field, as well
6204 * as the dinodes i_last_eb_blk */
6205 static int ocfs2_find_new_last_ext_blk(struct inode
*inode
,
6206 unsigned int clusters_to_del
,
6207 struct ocfs2_path
*path
,
6208 struct buffer_head
**new_last_eb
)
6210 int next_free
, ret
= 0;
6212 struct ocfs2_extent_rec
*rec
;
6213 struct ocfs2_extent_block
*eb
;
6214 struct ocfs2_extent_list
*el
;
6215 struct buffer_head
*bh
= NULL
;
6217 *new_last_eb
= NULL
;
6219 /* we have no tree, so of course, no last_eb. */
6220 if (!path
->p_tree_depth
)
6223 /* trunc to zero special case - this makes tree_depth = 0
6224 * regardless of what it is. */
6225 if (OCFS2_I(inode
)->ip_clusters
== clusters_to_del
)
6228 el
= path_leaf_el(path
);
6229 BUG_ON(!el
->l_next_free_rec
);
6232 * Make sure that this extent list will actually be empty
6233 * after we clear away the data. We can shortcut out if
6234 * there's more than one non-empty extent in the
6235 * list. Otherwise, a check of the remaining extent is
6238 next_free
= le16_to_cpu(el
->l_next_free_rec
);
6240 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6244 /* We may have a valid extent in index 1, check it. */
6246 rec
= &el
->l_recs
[1];
6249 * Fall through - no more nonempty extents, so we want
6250 * to delete this leaf.
6256 rec
= &el
->l_recs
[0];
6261 * Check it we'll only be trimming off the end of this
6264 if (le16_to_cpu(rec
->e_leaf_clusters
) > clusters_to_del
)
6268 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
6274 ret
= ocfs2_find_leaf(inode
, path_root_el(path
), cpos
, &bh
);
6280 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
6283 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6284 * Any corruption is a code bug. */
6285 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
6288 get_bh(*new_last_eb
);
6289 mlog(0, "returning block %llu, (cpos: %u)\n",
6290 (unsigned long long)le64_to_cpu(eb
->h_blkno
), cpos
);
6298 * Trim some clusters off the rightmost edge of a tree. Only called
6301 * The caller needs to:
6302 * - start journaling of each path component.
6303 * - compute and fully set up any new last ext block
6305 static int ocfs2_trim_tree(struct inode
*inode
, struct ocfs2_path
*path
,
6306 handle_t
*handle
, struct ocfs2_truncate_context
*tc
,
6307 u32 clusters_to_del
, u64
*delete_start
)
6309 int ret
, i
, index
= path
->p_tree_depth
;
6312 struct buffer_head
*bh
;
6313 struct ocfs2_extent_list
*el
;
6314 struct ocfs2_extent_rec
*rec
;
6318 while (index
>= 0) {
6319 bh
= path
->p_node
[index
].bh
;
6320 el
= path
->p_node
[index
].el
;
6322 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6323 index
, (unsigned long long)bh
->b_blocknr
);
6325 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
6328 (path
->p_tree_depth
- le16_to_cpu(el
->l_tree_depth
))) {
6329 ocfs2_error(inode
->i_sb
,
6330 "Inode %lu has invalid ext. block %llu",
6332 (unsigned long long)bh
->b_blocknr
);
6338 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
6339 rec
= &el
->l_recs
[i
];
6341 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6342 "next = %u\n", i
, le32_to_cpu(rec
->e_cpos
),
6343 ocfs2_rec_clusters(el
, rec
),
6344 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6345 le16_to_cpu(el
->l_next_free_rec
));
6347 BUG_ON(ocfs2_rec_clusters(el
, rec
) < clusters_to_del
);
6349 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
6351 * If the leaf block contains a single empty
6352 * extent and no records, we can just remove
6355 if (i
== 0 && ocfs2_is_empty_extent(rec
)) {
6357 sizeof(struct ocfs2_extent_rec
));
6358 el
->l_next_free_rec
= cpu_to_le16(0);
6364 * Remove any empty extents by shifting things
6365 * left. That should make life much easier on
6366 * the code below. This condition is rare
6367 * enough that we shouldn't see a performance
6370 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6371 le16_add_cpu(&el
->l_next_free_rec
, -1);
6374 i
< le16_to_cpu(el
->l_next_free_rec
); i
++)
6375 el
->l_recs
[i
] = el
->l_recs
[i
+ 1];
6377 memset(&el
->l_recs
[i
], 0,
6378 sizeof(struct ocfs2_extent_rec
));
6381 * We've modified our extent list. The
6382 * simplest way to handle this change
6383 * is to being the search from the
6386 goto find_tail_record
;
6389 le16_add_cpu(&rec
->e_leaf_clusters
, -clusters_to_del
);
6392 * We'll use "new_edge" on our way back up the
6393 * tree to know what our rightmost cpos is.
6395 new_edge
= le16_to_cpu(rec
->e_leaf_clusters
);
6396 new_edge
+= le32_to_cpu(rec
->e_cpos
);
6399 * The caller will use this to delete data blocks.
6401 *delete_start
= le64_to_cpu(rec
->e_blkno
)
6402 + ocfs2_clusters_to_blocks(inode
->i_sb
,
6403 le16_to_cpu(rec
->e_leaf_clusters
));
6406 * If it's now empty, remove this record.
6408 if (le16_to_cpu(rec
->e_leaf_clusters
) == 0) {
6410 sizeof(struct ocfs2_extent_rec
));
6411 le16_add_cpu(&el
->l_next_free_rec
, -1);
6414 if (le64_to_cpu(rec
->e_blkno
) == deleted_eb
) {
6416 sizeof(struct ocfs2_extent_rec
));
6417 le16_add_cpu(&el
->l_next_free_rec
, -1);
6422 /* Can this actually happen? */
6423 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
6427 * We never actually deleted any clusters
6428 * because our leaf was empty. There's no
6429 * reason to adjust the rightmost edge then.
6434 rec
->e_int_clusters
= cpu_to_le32(new_edge
);
6435 le32_add_cpu(&rec
->e_int_clusters
,
6436 -le32_to_cpu(rec
->e_cpos
));
6439 * A deleted child record should have been
6442 BUG_ON(le32_to_cpu(rec
->e_int_clusters
) == 0);
6446 ret
= ocfs2_journal_dirty(handle
, bh
);
6452 mlog(0, "extent list container %llu, after: record %d: "
6453 "(%u, %u, %llu), next = %u.\n",
6454 (unsigned long long)bh
->b_blocknr
, i
,
6455 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
),
6456 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6457 le16_to_cpu(el
->l_next_free_rec
));
6460 * We must be careful to only attempt delete of an
6461 * extent block (and not the root inode block).
6463 if (index
> 0 && le16_to_cpu(el
->l_next_free_rec
) == 0) {
6464 struct ocfs2_extent_block
*eb
=
6465 (struct ocfs2_extent_block
*)bh
->b_data
;
6468 * Save this for use when processing the
6471 deleted_eb
= le64_to_cpu(eb
->h_blkno
);
6473 mlog(0, "deleting this extent block.\n");
6475 ocfs2_remove_from_cache(inode
, bh
);
6477 BUG_ON(ocfs2_rec_clusters(el
, &el
->l_recs
[0]));
6478 BUG_ON(le32_to_cpu(el
->l_recs
[0].e_cpos
));
6479 BUG_ON(le64_to_cpu(el
->l_recs
[0].e_blkno
));
6481 ret
= ocfs2_cache_extent_block_free(&tc
->tc_dealloc
, eb
);
6482 /* An error here is not fatal. */
6497 static int ocfs2_do_truncate(struct ocfs2_super
*osb
,
6498 unsigned int clusters_to_del
,
6499 struct inode
*inode
,
6500 struct buffer_head
*fe_bh
,
6502 struct ocfs2_truncate_context
*tc
,
6503 struct ocfs2_path
*path
)
6506 struct ocfs2_dinode
*fe
;
6507 struct ocfs2_extent_block
*last_eb
= NULL
;
6508 struct ocfs2_extent_list
*el
;
6509 struct buffer_head
*last_eb_bh
= NULL
;
6512 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
6514 status
= ocfs2_find_new_last_ext_blk(inode
, clusters_to_del
,
6522 * Each component will be touched, so we might as well journal
6523 * here to avoid having to handle errors later.
6525 status
= ocfs2_journal_access_path(inode
, handle
, path
);
6532 status
= ocfs2_journal_access(handle
, inode
, last_eb_bh
,
6533 OCFS2_JOURNAL_ACCESS_WRITE
);
6539 last_eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
6542 el
= &(fe
->id2
.i_list
);
6545 * Lower levels depend on this never happening, but it's best
6546 * to check it up here before changing the tree.
6548 if (el
->l_tree_depth
&& el
->l_recs
[0].e_int_clusters
== 0) {
6549 ocfs2_error(inode
->i_sb
,
6550 "Inode %lu has an empty extent record, depth %u\n",
6551 inode
->i_ino
, le16_to_cpu(el
->l_tree_depth
));
6556 vfs_dq_free_space_nodirty(inode
,
6557 ocfs2_clusters_to_bytes(osb
->sb
, clusters_to_del
));
6558 spin_lock(&OCFS2_I(inode
)->ip_lock
);
6559 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
) -
6561 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
6562 le32_add_cpu(&fe
->i_clusters
, -clusters_to_del
);
6563 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6565 status
= ocfs2_trim_tree(inode
, path
, handle
, tc
,
6566 clusters_to_del
, &delete_blk
);
6572 if (le32_to_cpu(fe
->i_clusters
) == 0) {
6573 /* trunc to zero is a special case. */
6574 el
->l_tree_depth
= 0;
6575 fe
->i_last_eb_blk
= 0;
6577 fe
->i_last_eb_blk
= last_eb
->h_blkno
;
6579 status
= ocfs2_journal_dirty(handle
, fe_bh
);
6586 /* If there will be a new last extent block, then by
6587 * definition, there cannot be any leaves to the right of
6589 last_eb
->h_next_leaf_blk
= 0;
6590 status
= ocfs2_journal_dirty(handle
, last_eb_bh
);
6598 status
= ocfs2_truncate_log_append(osb
, handle
, delete_blk
,
6612 static int ocfs2_zero_func(handle_t
*handle
, struct buffer_head
*bh
)
6614 set_buffer_uptodate(bh
);
6615 mark_buffer_dirty(bh
);
6619 static void ocfs2_map_and_dirty_page(struct inode
*inode
, handle_t
*handle
,
6620 unsigned int from
, unsigned int to
,
6621 struct page
*page
, int zero
, u64
*phys
)
6623 int ret
, partial
= 0;
6625 ret
= ocfs2_map_page_blocks(page
, phys
, inode
, from
, to
, 0);
6630 zero_user_segment(page
, from
, to
);
6633 * Need to set the buffers we zero'd into uptodate
6634 * here if they aren't - ocfs2_map_page_blocks()
6635 * might've skipped some
6637 ret
= walk_page_buffers(handle
, page_buffers(page
),
6642 else if (ocfs2_should_order_data(inode
)) {
6643 ret
= ocfs2_jbd2_file_inode(handle
, inode
);
6649 SetPageUptodate(page
);
6651 flush_dcache_page(page
);
6654 static void ocfs2_zero_cluster_pages(struct inode
*inode
, loff_t start
,
6655 loff_t end
, struct page
**pages
,
6656 int numpages
, u64 phys
, handle_t
*handle
)
6660 unsigned int from
, to
= PAGE_CACHE_SIZE
;
6661 struct super_block
*sb
= inode
->i_sb
;
6663 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb
)));
6668 to
= PAGE_CACHE_SIZE
;
6669 for(i
= 0; i
< numpages
; i
++) {
6672 from
= start
& (PAGE_CACHE_SIZE
- 1);
6673 if ((end
>> PAGE_CACHE_SHIFT
) == page
->index
)
6674 to
= end
& (PAGE_CACHE_SIZE
- 1);
6676 BUG_ON(from
> PAGE_CACHE_SIZE
);
6677 BUG_ON(to
> PAGE_CACHE_SIZE
);
6679 ocfs2_map_and_dirty_page(inode
, handle
, from
, to
, page
, 1,
6682 start
= (page
->index
+ 1) << PAGE_CACHE_SHIFT
;
6686 ocfs2_unlock_and_free_pages(pages
, numpages
);
6689 static int ocfs2_grab_eof_pages(struct inode
*inode
, loff_t start
, loff_t end
,
6690 struct page
**pages
, int *num
)
6692 int numpages
, ret
= 0;
6693 struct super_block
*sb
= inode
->i_sb
;
6694 struct address_space
*mapping
= inode
->i_mapping
;
6695 unsigned long index
;
6696 loff_t last_page_bytes
;
6698 BUG_ON(start
> end
);
6700 BUG_ON(start
>> OCFS2_SB(sb
)->s_clustersize_bits
!=
6701 (end
- 1) >> OCFS2_SB(sb
)->s_clustersize_bits
);
6704 last_page_bytes
= PAGE_ALIGN(end
);
6705 index
= start
>> PAGE_CACHE_SHIFT
;
6707 pages
[numpages
] = grab_cache_page(mapping
, index
);
6708 if (!pages
[numpages
]) {
6716 } while (index
< (last_page_bytes
>> PAGE_CACHE_SHIFT
));
6721 ocfs2_unlock_and_free_pages(pages
, numpages
);
6731 * Zero the area past i_size but still within an allocated
6732 * cluster. This avoids exposing nonzero data on subsequent file
6735 * We need to call this before i_size is updated on the inode because
6736 * otherwise block_write_full_page() will skip writeout of pages past
6737 * i_size. The new_i_size parameter is passed for this reason.
6739 int ocfs2_zero_range_for_truncate(struct inode
*inode
, handle_t
*handle
,
6740 u64 range_start
, u64 range_end
)
6742 int ret
= 0, numpages
;
6743 struct page
**pages
= NULL
;
6745 unsigned int ext_flags
;
6746 struct super_block
*sb
= inode
->i_sb
;
6749 * File systems which don't support sparse files zero on every
6752 if (!ocfs2_sparse_alloc(OCFS2_SB(sb
)))
6755 pages
= kcalloc(ocfs2_pages_per_cluster(sb
),
6756 sizeof(struct page
*), GFP_NOFS
);
6757 if (pages
== NULL
) {
6763 if (range_start
== range_end
)
6766 ret
= ocfs2_extent_map_get_blocks(inode
,
6767 range_start
>> sb
->s_blocksize_bits
,
6768 &phys
, NULL
, &ext_flags
);
6775 * Tail is a hole, or is marked unwritten. In either case, we
6776 * can count on read and write to return/push zero's.
6778 if (phys
== 0 || ext_flags
& OCFS2_EXT_UNWRITTEN
)
6781 ret
= ocfs2_grab_eof_pages(inode
, range_start
, range_end
, pages
,
6788 ocfs2_zero_cluster_pages(inode
, range_start
, range_end
, pages
,
6789 numpages
, phys
, handle
);
6792 * Initiate writeout of the pages we zero'd here. We don't
6793 * wait on them - the truncate_inode_pages() call later will
6796 ret
= do_sync_mapping_range(inode
->i_mapping
, range_start
,
6797 range_end
- 1, SYNC_FILE_RANGE_WRITE
);
6808 static void ocfs2_zero_dinode_id2_with_xattr(struct inode
*inode
,
6809 struct ocfs2_dinode
*di
)
6811 unsigned int blocksize
= 1 << inode
->i_sb
->s_blocksize_bits
;
6812 unsigned int xattrsize
= le16_to_cpu(di
->i_xattr_inline_size
);
6814 if (le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_XATTR_FL
)
6815 memset(&di
->id2
, 0, blocksize
-
6816 offsetof(struct ocfs2_dinode
, id2
) -
6819 memset(&di
->id2
, 0, blocksize
-
6820 offsetof(struct ocfs2_dinode
, id2
));
6823 void ocfs2_dinode_new_extent_list(struct inode
*inode
,
6824 struct ocfs2_dinode
*di
)
6826 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
6827 di
->id2
.i_list
.l_tree_depth
= 0;
6828 di
->id2
.i_list
.l_next_free_rec
= 0;
6829 di
->id2
.i_list
.l_count
= cpu_to_le16(
6830 ocfs2_extent_recs_per_inode_with_xattr(inode
->i_sb
, di
));
6833 void ocfs2_set_inode_data_inline(struct inode
*inode
, struct ocfs2_dinode
*di
)
6835 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6836 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
6838 spin_lock(&oi
->ip_lock
);
6839 oi
->ip_dyn_features
|= OCFS2_INLINE_DATA_FL
;
6840 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
6841 spin_unlock(&oi
->ip_lock
);
6844 * We clear the entire i_data structure here so that all
6845 * fields can be properly initialized.
6847 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
6849 idata
->id_count
= cpu_to_le16(
6850 ocfs2_max_inline_data_with_xattr(inode
->i_sb
, di
));
6853 int ocfs2_convert_inline_data_to_extents(struct inode
*inode
,
6854 struct buffer_head
*di_bh
)
6856 int ret
, i
, has_data
, num_pages
= 0;
6858 u64
uninitialized_var(block
);
6859 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6860 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6861 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
6862 struct ocfs2_alloc_context
*data_ac
= NULL
;
6863 struct page
**pages
= NULL
;
6864 loff_t end
= osb
->s_clustersize
;
6865 struct ocfs2_extent_tree et
;
6868 has_data
= i_size_read(inode
) ? 1 : 0;
6871 pages
= kcalloc(ocfs2_pages_per_cluster(osb
->sb
),
6872 sizeof(struct page
*), GFP_NOFS
);
6873 if (pages
== NULL
) {
6879 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
6886 handle
= ocfs2_start_trans(osb
,
6887 ocfs2_inline_to_extents_credits(osb
->sb
));
6888 if (IS_ERR(handle
)) {
6889 ret
= PTR_ERR(handle
);
6894 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
6895 OCFS2_JOURNAL_ACCESS_WRITE
);
6903 unsigned int page_end
;
6906 if (vfs_dq_alloc_space_nodirty(inode
,
6907 ocfs2_clusters_to_bytes(osb
->sb
, 1))) {
6913 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
,
6921 * Save two copies, one for insert, and one that can
6922 * be changed by ocfs2_map_and_dirty_page() below.
6924 block
= phys
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
6927 * Non sparse file systems zero on extend, so no need
6930 if (!ocfs2_sparse_alloc(osb
) &&
6931 PAGE_CACHE_SIZE
< osb
->s_clustersize
)
6932 end
= PAGE_CACHE_SIZE
;
6934 ret
= ocfs2_grab_eof_pages(inode
, 0, end
, pages
, &num_pages
);
6941 * This should populate the 1st page for us and mark
6944 ret
= ocfs2_read_inline_data(inode
, pages
[0], di_bh
);
6950 page_end
= PAGE_CACHE_SIZE
;
6951 if (PAGE_CACHE_SIZE
> osb
->s_clustersize
)
6952 page_end
= osb
->s_clustersize
;
6954 for (i
= 0; i
< num_pages
; i
++)
6955 ocfs2_map_and_dirty_page(inode
, handle
, 0, page_end
,
6956 pages
[i
], i
> 0, &phys
);
6959 spin_lock(&oi
->ip_lock
);
6960 oi
->ip_dyn_features
&= ~OCFS2_INLINE_DATA_FL
;
6961 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
6962 spin_unlock(&oi
->ip_lock
);
6964 ocfs2_dinode_new_extent_list(inode
, di
);
6966 ocfs2_journal_dirty(handle
, di_bh
);
6970 * An error at this point should be extremely rare. If
6971 * this proves to be false, we could always re-build
6972 * the in-inode data from our pages.
6974 ocfs2_init_dinode_extent_tree(&et
, inode
, di_bh
);
6975 ret
= ocfs2_insert_extent(osb
, handle
, inode
, &et
,
6976 0, block
, 1, 0, NULL
);
6982 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6986 if (ret
< 0 && did_quota
)
6987 vfs_dq_free_space_nodirty(inode
,
6988 ocfs2_clusters_to_bytes(osb
->sb
, 1));
6990 ocfs2_commit_trans(osb
, handle
);
6994 ocfs2_free_alloc_context(data_ac
);
6998 ocfs2_unlock_and_free_pages(pages
, num_pages
);
7006 * It is expected, that by the time you call this function,
7007 * inode->i_size and fe->i_size have been adjusted.
7009 * WARNING: This will kfree the truncate context
7011 int ocfs2_commit_truncate(struct ocfs2_super
*osb
,
7012 struct inode
*inode
,
7013 struct buffer_head
*fe_bh
,
7014 struct ocfs2_truncate_context
*tc
)
7016 int status
, i
, credits
, tl_sem
= 0;
7017 u32 clusters_to_del
, new_highest_cpos
, range
;
7018 struct ocfs2_extent_list
*el
;
7019 handle_t
*handle
= NULL
;
7020 struct inode
*tl_inode
= osb
->osb_tl_inode
;
7021 struct ocfs2_path
*path
= NULL
;
7022 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
7026 new_highest_cpos
= ocfs2_clusters_for_bytes(osb
->sb
,
7027 i_size_read(inode
));
7029 path
= ocfs2_new_path(fe_bh
, &di
->id2
.i_list
);
7036 ocfs2_extent_map_trunc(inode
, new_highest_cpos
);
7040 * Check that we still have allocation to delete.
7042 if (OCFS2_I(inode
)->ip_clusters
== 0) {
7048 * Truncate always works against the rightmost tree branch.
7050 status
= ocfs2_find_path(inode
, path
, UINT_MAX
);
7056 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7057 OCFS2_I(inode
)->ip_clusters
, path
->p_tree_depth
);
7060 * By now, el will point to the extent list on the bottom most
7061 * portion of this tree. Only the tail record is considered in
7064 * We handle the following cases, in order:
7065 * - empty extent: delete the remaining branch
7066 * - remove the entire record
7067 * - remove a partial record
7068 * - no record needs to be removed (truncate has completed)
7070 el
= path_leaf_el(path
);
7071 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
7072 ocfs2_error(inode
->i_sb
,
7073 "Inode %llu has empty extent block at %llu\n",
7074 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7075 (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7080 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
7081 range
= le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
7082 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7083 if (i
== 0 && ocfs2_is_empty_extent(&el
->l_recs
[i
])) {
7084 clusters_to_del
= 0;
7085 } else if (le32_to_cpu(el
->l_recs
[i
].e_cpos
) >= new_highest_cpos
) {
7086 clusters_to_del
= ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7087 } else if (range
> new_highest_cpos
) {
7088 clusters_to_del
= (ocfs2_rec_clusters(el
, &el
->l_recs
[i
]) +
7089 le32_to_cpu(el
->l_recs
[i
].e_cpos
)) -
7096 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7097 clusters_to_del
, (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7099 mutex_lock(&tl_inode
->i_mutex
);
7101 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7102 * record is free for use. If there isn't any, we flush to get
7103 * an empty truncate log. */
7104 if (ocfs2_truncate_log_needs_flush(osb
)) {
7105 status
= __ocfs2_flush_truncate_log(osb
);
7112 credits
= ocfs2_calc_tree_trunc_credits(osb
->sb
, clusters_to_del
,
7113 (struct ocfs2_dinode
*)fe_bh
->b_data
,
7115 handle
= ocfs2_start_trans(osb
, credits
);
7116 if (IS_ERR(handle
)) {
7117 status
= PTR_ERR(handle
);
7123 status
= ocfs2_do_truncate(osb
, clusters_to_del
, inode
, fe_bh
, handle
,
7130 mutex_unlock(&tl_inode
->i_mutex
);
7133 ocfs2_commit_trans(osb
, handle
);
7136 ocfs2_reinit_path(path
, 1);
7139 * The check above will catch the case where we've truncated
7140 * away all allocation.
7146 ocfs2_schedule_truncate_log_flush(osb
, 1);
7149 mutex_unlock(&tl_inode
->i_mutex
);
7152 ocfs2_commit_trans(osb
, handle
);
7154 ocfs2_run_deallocs(osb
, &tc
->tc_dealloc
);
7156 ocfs2_free_path(path
);
7158 /* This will drop the ext_alloc cluster lock for us */
7159 ocfs2_free_truncate_context(tc
);
7166 * Expects the inode to already be locked.
7168 int ocfs2_prepare_truncate(struct ocfs2_super
*osb
,
7169 struct inode
*inode
,
7170 struct buffer_head
*fe_bh
,
7171 struct ocfs2_truncate_context
**tc
)
7174 unsigned int new_i_clusters
;
7175 struct ocfs2_dinode
*fe
;
7176 struct ocfs2_extent_block
*eb
;
7177 struct buffer_head
*last_eb_bh
= NULL
;
7183 new_i_clusters
= ocfs2_clusters_for_bytes(osb
->sb
,
7184 i_size_read(inode
));
7185 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
7187 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7188 "%llu\n", le32_to_cpu(fe
->i_clusters
), new_i_clusters
,
7189 (unsigned long long)le64_to_cpu(fe
->i_size
));
7191 *tc
= kzalloc(sizeof(struct ocfs2_truncate_context
), GFP_KERNEL
);
7197 ocfs2_init_dealloc_ctxt(&(*tc
)->tc_dealloc
);
7199 if (fe
->id2
.i_list
.l_tree_depth
) {
7200 status
= ocfs2_read_extent_block(inode
,
7201 le64_to_cpu(fe
->i_last_eb_blk
),
7207 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
7210 (*tc
)->tc_last_eb_bh
= last_eb_bh
;
7216 ocfs2_free_truncate_context(*tc
);
7224 * 'start' is inclusive, 'end' is not.
7226 int ocfs2_truncate_inline(struct inode
*inode
, struct buffer_head
*di_bh
,
7227 unsigned int start
, unsigned int end
, int trunc
)
7230 unsigned int numbytes
;
7232 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7233 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
7234 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
7236 if (end
> i_size_read(inode
))
7237 end
= i_size_read(inode
);
7239 BUG_ON(start
>= end
);
7241 if (!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) ||
7242 !(le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_DATA_FL
) ||
7243 !ocfs2_supports_inline_data(osb
)) {
7244 ocfs2_error(inode
->i_sb
,
7245 "Inline data flags for inode %llu don't agree! "
7246 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7247 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7248 le16_to_cpu(di
->i_dyn_features
),
7249 OCFS2_I(inode
)->ip_dyn_features
,
7250 osb
->s_feature_incompat
);
7255 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
7256 if (IS_ERR(handle
)) {
7257 ret
= PTR_ERR(handle
);
7262 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
7263 OCFS2_JOURNAL_ACCESS_WRITE
);
7269 numbytes
= end
- start
;
7270 memset(idata
->id_data
+ start
, 0, numbytes
);
7273 * No need to worry about the data page here - it's been
7274 * truncated already and inline data doesn't need it for
7275 * pushing zero's to disk, so we'll let readpage pick it up
7279 i_size_write(inode
, start
);
7280 di
->i_size
= cpu_to_le64(start
);
7283 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7284 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
7286 di
->i_ctime
= di
->i_mtime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
7287 di
->i_ctime_nsec
= di
->i_mtime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
7289 ocfs2_journal_dirty(handle
, di_bh
);
7292 ocfs2_commit_trans(osb
, handle
);
7298 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
)
7301 * The caller is responsible for completing deallocation
7302 * before freeing the context.
7304 if (tc
->tc_dealloc
.c_first_suballocator
!= NULL
)
7306 "Truncate completion has non-empty dealloc context\n");
7308 brelse(tc
->tc_last_eb_bh
);